1 #include "tdb2-source.h"
2 #include "tap-interface.h"
7 static int tdb1_expand_file_sparse(struct tdb_context *tdb,
11 if ((tdb->flags & TDB_RDONLY) || tdb->tdb1.traverse_read) {
12 tdb->last_error = TDB_ERR_RDONLY;
16 if (ftruncate(tdb->file->fd, size+addition) == -1) {
18 ssize_t written = pwrite(tdb->file->fd, &b, 1, (size+addition) - 1);
20 /* try once more, potentially revealing errno */
21 written = pwrite(tdb->file->fd, &b, 1, (size+addition) - 1);
24 /* again - give up, guessing errno */
28 tdb->last_error = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
29 "expand_file to %d failed (%s)",
39 static const struct tdb1_methods large_io_methods = {
44 tdb1_expand_file_sparse
47 static int test_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
50 TDB_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 tdb_context *tdb;
61 TDB_DATA key, orig_data, data;
64 struct tdb1_record rec;
65 union tdb_attribute hsize;
68 hsize.base.attr = TDB_ATTRIBUTE_TDB1_HASHSIZE;
69 hsize.base.next = &tap_log_attr;
70 hsize.tdb1_hashsize.hsize = 1024;
73 tdb = tdb_open("run-36-file.tdb1", TDB_VERSION1,
74 O_CREAT|O_TRUNC|O_RDWR, 0600, &hsize);
77 tdb->tdb1.io = &large_io_methods;
79 key.dsize = strlen("hi");
80 key.dptr = (void *)"hi";
81 orig_data.dsize = strlen("world");
82 orig_data.dptr = (void *)"world";
84 /* Enlarge the file (internally multiplies by 2). */
85 ret = tdb1_expand(tdb, 1500000000);
87 #ifdef HAVE_INCOHERENT_MMAP
88 /* This can fail due to mmap failure on 32 bit systems. */
90 /* These should now fail. */
91 ok1(tdb_store(tdb, key, orig_data, TDB_INSERT) == TDB_ERR_IO);
92 ok1(tdb_fetch(tdb, key, &data) == TDB_ERR_IO);
93 ok1(tdb_traverse(tdb, test_traverse, &orig_data) == TDB_ERR_IO);
94 ok1(tdb_delete(tdb, key) == TDB_ERR_IO);
95 ok1(tdb_traverse(tdb, test_traverse, NULL) == TDB_ERR_IO);
96 /* Skip the rest... */
97 for (ret = 0; ret < 26 - 6; ret++)
100 return exit_status();
105 /* Put an entry in, and check it. */
106 ok1(tdb_store(tdb, key, orig_data, TDB_INSERT) == TDB_SUCCESS);
108 ok1(tdb_fetch(tdb, key, &data) == TDB_SUCCESS);
109 ok1(data.dsize == strlen("world"));
110 ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
113 /* That currently fills at the end, make sure that's true. */
114 hash = tdb_hash(tdb, key.dptr, key.dsize);
115 rec_ptr = tdb1_find_lock_hash(tdb, key, hash, F_RDLCK, &rec);
117 ok1(rec_ptr > 2U*1024*1024*1024);
118 tdb1_unlock(tdb, TDB1_BUCKET(rec.full_hash), F_RDLCK);
120 /* Traverse must work. */
121 ok1(tdb_traverse(tdb, test_traverse, &orig_data) == 1);
123 /* Delete should work. */
124 ok1(tdb_delete(tdb, key) == TDB_SUCCESS);
126 ok1(tdb_traverse(tdb, test_traverse, NULL) == 0);
128 /* Transactions should work. */
129 ok1(tdb_transaction_start(tdb) == TDB_SUCCESS);
130 ok1(tdb_store(tdb, key, orig_data, TDB_INSERT) == TDB_SUCCESS);
132 ok1(tdb_fetch(tdb, key, &data) == TDB_SUCCESS);
133 ok1(data.dsize == strlen("world"));
134 ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
136 ok1(tdb_transaction_commit(tdb) == TDB_SUCCESS);
138 ok1(tdb_traverse(tdb, test_traverse, &orig_data) == 1);
141 return exit_status();