run-3G-file.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* We need this otherwise fcntl locking fails. */
  2. #define _FILE_OFFSET_BITS 64
  3. #define _XOPEN_SOURCE 500
  4. #include <ccan/tdb/tdb.h>
  5. #include <ccan/tdb/io.c>
  6. #include <ccan/tdb/tdb.c>
  7. #include <ccan/tdb/lock.c>
  8. #include <ccan/tdb/freelist.c>
  9. #include <ccan/tdb/traverse.c>
  10. #include <ccan/tdb/transaction.c>
  11. #include <ccan/tdb/error.c>
  12. #include <ccan/tdb/open.c>
  13. #include <ccan/tdb/check.c>
  14. #include <ccan/tap/tap.h>
  15. #include <stdlib.h>
  16. #include <err.h>
  17. static int tdb_expand_file_sparse(struct tdb_context *tdb,
  18. tdb_off_t size,
  19. tdb_off_t addition)
  20. {
  21. if (tdb->read_only || tdb->traverse_read) {
  22. tdb->ecode = TDB_ERR_RDONLY;
  23. return -1;
  24. }
  25. if (ftruncate(tdb->fd, size+addition) == -1) {
  26. char b = 0;
  27. ssize_t written = pwrite(tdb->fd, &b, 1, (size+addition) - 1);
  28. if (written == 0) {
  29. /* try once more, potentially revealing errno */
  30. written = pwrite(tdb->fd, &b, 1, (size+addition) - 1);
  31. }
  32. if (written == 0) {
  33. /* again - give up, guessing errno */
  34. errno = ENOSPC;
  35. }
  36. if (written != 1) {
  37. TDB_LOG((tdb, TDB_DEBUG_FATAL, "expand_file to %d failed (%s)\n",
  38. size+addition, strerror(errno)));
  39. return -1;
  40. }
  41. }
  42. return 0;
  43. }
  44. static const struct tdb_methods large_io_methods = {
  45. tdb_read,
  46. tdb_write,
  47. tdb_next_hash_chain,
  48. tdb_oob,
  49. tdb_expand_file_sparse,
  50. tdb_brlock,
  51. tdb_brunlock
  52. };
  53. static int test_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
  54. void *_data)
  55. {
  56. TDB_DATA *expect = _data;
  57. ok1(key.dsize == strlen("hi"));
  58. ok1(memcmp(key.dptr, "hi", strlen("hi")) == 0);
  59. ok1(data.dsize == expect->dsize);
  60. ok1(memcmp(data.dptr, expect->dptr, data.dsize) == 0);
  61. return 0;
  62. }
  63. int main(int argc, char *argv[])
  64. {
  65. struct tdb_context *tdb;
  66. TDB_DATA key, orig_data, data;
  67. uint32_t hash;
  68. tdb_off_t rec_ptr;
  69. struct tdb_record rec;
  70. plan_tests(24);
  71. tdb = tdb_open("/tmp/test.tdb", 1024, TDB_CLEAR_IF_FIRST,
  72. O_CREAT|O_TRUNC|O_RDWR, 0600);
  73. ok1(tdb);
  74. tdb->methods = &large_io_methods;
  75. /* Enlarge the file (internally multiplies by 100). */
  76. ok1(tdb_expand(tdb, 30000000) == 0);
  77. /* Put an entry in, and check it. */
  78. key.dsize = strlen("hi");
  79. key.dptr = (void *)"hi";
  80. orig_data.dsize = strlen("world");
  81. orig_data.dptr = (void *)"world";
  82. ok1(tdb_store(tdb, key, orig_data, TDB_INSERT) == 0);
  83. data = tdb_fetch(tdb, key);
  84. ok1(data.dsize == strlen("world"));
  85. ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
  86. free(data.dptr);
  87. /* That currently fills at the end, make sure that's true. */
  88. hash = tdb->hash_fn(&key);
  89. rec_ptr = tdb_find_lock_hash(tdb, key, hash, F_RDLCK, &rec);
  90. ok1(rec_ptr);
  91. ok1(rec_ptr > 2U*1024*1024*1024);
  92. tdb_unlock(tdb, BUCKET(rec.full_hash), F_RDLCK);
  93. /* Traverse must work. */
  94. ok1(tdb_traverse(tdb, test_traverse, &orig_data) == 1);
  95. /* Delete should work. */
  96. ok1(tdb_delete(tdb, key) == 0);
  97. ok1(tdb_traverse(tdb, test_traverse, NULL) == 0);
  98. /* Transactions should work. */
  99. ok1(tdb_transaction_start(tdb) == 0);
  100. ok1(tdb_store(tdb, key, orig_data, TDB_INSERT) == 0);
  101. data = tdb_fetch(tdb, key);
  102. ok1(data.dsize == strlen("world"));
  103. ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
  104. free(data.dptr);
  105. ok1(tdb_transaction_commit(tdb) == 0);
  106. ok1(tdb_traverse(tdb, test_traverse, &orig_data) == 1);
  107. tdb_close(tdb);
  108. return exit_status();
  109. }