1 /* We need this otherwise fcntl locking fails. */
2 #define _FILE_OFFSET_BITS 64
3 #include "tdb2-source.h"
4 #include <ccan/tap/tap.h>
7 #include "tdb1-logging.h"
9 static int tdb1_expand_file_sparse(struct tdb1_context *tdb,
13 if (tdb->read_only || tdb->traverse_read) {
14 tdb->ecode = TDB1_ERR_RDONLY;
18 if (ftruncate(tdb->fd, size+addition) == -1) {
20 ssize_t written = pwrite(tdb->fd, &b, 1, (size+addition) - 1);
22 /* try once more, potentially revealing errno */
23 written = pwrite(tdb->fd, &b, 1, (size+addition) - 1);
26 /* again - give up, guessing errno */
30 TDB1_LOG((tdb, TDB1_DEBUG_FATAL, "expand_file to %d failed (%s)\n",
31 size+addition, strerror(errno)));
39 static const struct tdb1_methods large_io_methods = {
44 tdb1_expand_file_sparse
47 static int test_traverse(struct tdb1_context *tdb, TDB1_DATA key, TDB1_DATA data,
50 TDB1_DATA *expect = _data;
51 ok1(key.dsize == strlen("hi"));
52 ok1(memcmp(key.dptr, "hi", strlen("hi")) == 0);
53 ok1(data.dsize == expect->dsize);
54 ok1(memcmp(data.dptr, expect->dptr, data.dsize) == 0);
58 int main(int argc, char *argv[])
60 struct tdb1_context *tdb;
61 TDB1_DATA key, orig_data, data;
64 struct tdb1_record rec;
67 tdb = tdb1_open_ex("run-36-file.tdb", 1024, TDB1_CLEAR_IF_FIRST,
68 O_CREAT|O_TRUNC|O_RDWR, 0600, &taplogctx, NULL);
71 tdb->methods = &large_io_methods;
73 /* Enlarge the file (internally multiplies by 2). */
74 ok1(tdb1_expand(tdb, 1500000000) == 0);
76 /* Put an entry in, and check it. */
77 key.dsize = strlen("hi");
78 key.dptr = (void *)"hi";
79 orig_data.dsize = strlen("world");
80 orig_data.dptr = (void *)"world";
82 ok1(tdb1_store(tdb, key, orig_data, TDB1_INSERT) == 0);
84 data = tdb1_fetch(tdb, key);
85 ok1(data.dsize == strlen("world"));
86 ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
89 /* That currently fills at the end, make sure that's true. */
90 hash = tdb->hash_fn(&key);
91 rec_ptr = tdb1_find_lock_hash(tdb, key, hash, F_RDLCK, &rec);
93 ok1(rec_ptr > 2U*1024*1024*1024);
94 tdb1_unlock(tdb, TDB1_BUCKET(rec.full_hash), F_RDLCK);
96 /* Traverse must work. */
97 ok1(tdb1_traverse(tdb, test_traverse, &orig_data) == 1);
99 /* Delete should work. */
100 ok1(tdb1_delete(tdb, key) == 0);
102 ok1(tdb1_traverse(tdb, test_traverse, NULL) == 0);
104 /* Transactions should work. */
105 ok1(tdb1_transaction_start(tdb) == 0);
106 ok1(tdb1_store(tdb, key, orig_data, TDB1_INSERT) == 0);
108 data = tdb1_fetch(tdb, key);
109 ok1(data.dsize == strlen("world"));
110 ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
112 ok1(tdb1_transaction_commit(tdb) == 0);
114 ok1(tdb1_traverse(tdb, test_traverse, &orig_data) == 1);
117 return exit_status();