1 /* We need this otherwise fcntl locking fails. */
2 #define _FILE_OFFSET_BITS 64
3 #define _XOPEN_SOURCE 500
8 #include "tdb/freelist.c"
9 #include "tdb/traverse.c"
10 #include "tdb/transaction.c"
11 #include "tdb/error.c"
17 static int tdb_expand_file_sparse(struct tdb_context *tdb,
21 if (tdb->read_only || tdb->traverse_read) {
22 tdb->ecode = TDB_ERR_RDONLY;
26 if (ftruncate(tdb->fd, size+addition) == -1) {
28 ssize_t written = pwrite(tdb->fd, &b, 1, (size+addition) - 1);
30 /* try once more, potentially revealing errno */
31 written = pwrite(tdb->fd, &b, 1, (size+addition) - 1);
34 /* again - give up, guessing errno */
38 TDB_LOG((tdb, TDB_DEBUG_FATAL, "expand_file to %d failed (%s)\n",
39 size+addition, strerror(errno)));
47 static const struct tdb_methods large_io_methods = {
52 tdb_expand_file_sparse,
56 static int test_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
59 TDB_DATA *expect = _data;
60 ok1(key.dsize == strlen("hi"));
61 ok1(memcmp(key.dptr, "hi", strlen("hi")) == 0);
62 ok1(data.dsize == expect->dsize);
63 ok1(memcmp(data.dptr, expect->dptr, data.dsize) == 0);
67 int main(int argc, char *argv[])
69 struct tdb_context *tdb;
70 TDB_DATA key, orig_data, data;
73 struct list_struct rec;
76 tdb = tdb_open("/tmp/test.tdb", 1024, TDB_CLEAR_IF_FIRST,
77 O_CREAT|O_TRUNC|O_RDWR, 0600);
80 tdb->methods = &large_io_methods;
82 /* Enlarge the file (internally multiplies by 100). */
83 ok1(tdb_expand(tdb, 30000000) == 0);
85 /* Put an entry in, and check it. */
86 key.dsize = strlen("hi");
87 key.dptr = (void *)"hi";
88 orig_data.dsize = strlen("world");
89 orig_data.dptr = (void *)"world";
91 ok1(tdb_store(tdb, key, orig_data, TDB_INSERT) == 0);
93 data = tdb_fetch(tdb, key);
94 ok1(data.dsize == strlen("world"));
95 ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
98 /* That currently fills at the end, make sure that's true. */
99 hash = tdb->hash_fn(&key);
100 rec_ptr = tdb_find_lock_hash(tdb, key, hash, F_RDLCK, &rec);
102 ok1(rec_ptr > 2U*1024*1024*1024);
103 tdb_unlock(tdb, BUCKET(rec.full_hash), F_RDLCK);
105 /* Traverse must work. */
106 ok1(tdb_traverse(tdb, test_traverse, &orig_data) == 1);
108 /* Delete should work. */
109 ok1(tdb_delete(tdb, key) == 0);
111 ok1(tdb_traverse(tdb, test_traverse, NULL) == 0);
113 /* Transactions should work. */
114 ok1(tdb_transaction_start(tdb) == 0);
115 ok1(tdb_store(tdb, key, orig_data, TDB_INSERT) == 0);
117 data = tdb_fetch(tdb, key);
118 ok1(data.dsize == strlen("world"));
119 ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
121 ok1(tdb_transaction_commit(tdb) == 0);
123 ok1(tdb_traverse(tdb, test_traverse, &orig_data) == 1);
126 return exit_status();