94639113656d9b6967b46c0949f3006db1fab47a
[ccan] / ccan / tdb / test / run-3G-file.c
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
18 static int tdb_expand_file_sparse(struct tdb_context *tdb,
19                                   tdb_off_t size,
20                                   tdb_off_t addition)
21 {
22         if (tdb->read_only || tdb->traverse_read) {
23                 tdb->ecode = TDB_ERR_RDONLY;
24                 return -1;
25         }
26
27         if (ftruncate(tdb->fd, size+addition) == -1) {
28                 char b = 0;
29                 ssize_t written = pwrite(tdb->fd,  &b, 1, (size+addition) - 1);
30                 if (written == 0) {
31                         /* try once more, potentially revealing errno */
32                         written = pwrite(tdb->fd,  &b, 1, (size+addition) - 1);
33                 }
34                 if (written == 0) {
35                         /* again - give up, guessing errno */
36                         errno = ENOSPC;
37                 }
38                 if (written != 1) {
39                         TDB_LOG((tdb, TDB_DEBUG_FATAL, "expand_file to %d failed (%s)\n", 
40                                  size+addition, strerror(errno)));
41                         return -1;
42                 }
43         }
44
45         return 0;
46 }
47
48 static const struct tdb_methods large_io_methods = {
49         tdb_read,
50         tdb_write,
51         tdb_next_hash_chain,
52         tdb_oob,
53         tdb_expand_file_sparse
54 };
55
56 static int test_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
57                          void *_data)
58 {
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);
64         return 0;
65 }
66
67 int main(int argc, char *argv[])
68 {
69         struct tdb_context *tdb;
70         TDB_DATA key, orig_data, data;
71         uint32_t hash;
72         tdb_off_t rec_ptr;
73         struct tdb_record rec;
74
75         plan_tests(24);
76         tdb = tdb_open("/tmp/test.tdb", 1024, TDB_CLEAR_IF_FIRST,
77                        O_CREAT|O_TRUNC|O_RDWR, 0600);
78
79         ok1(tdb);
80         tdb->methods = &large_io_methods;
81
82         /* Enlarge the file (internally multiplies by 100). */
83         ok1(tdb_expand(tdb, 30000000) == 0);
84
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";
90
91         ok1(tdb_store(tdb, key, orig_data, TDB_INSERT) == 0);
92
93         data = tdb_fetch(tdb, key);
94         ok1(data.dsize == strlen("world"));
95         ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
96         free(data.dptr);
97
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);
101         ok1(rec_ptr);
102         ok1(rec_ptr > 2U*1024*1024*1024);
103         tdb_unlock(tdb, BUCKET(rec.full_hash), F_RDLCK);
104
105         /* Traverse must work. */
106         ok1(tdb_traverse(tdb, test_traverse, &orig_data) == 1);
107
108         /* Delete should work. */
109         ok1(tdb_delete(tdb, key) == 0);
110
111         ok1(tdb_traverse(tdb, test_traverse, NULL) == 0);
112
113         /* Transactions should work. */
114         ok1(tdb_transaction_start(tdb) == 0);
115         ok1(tdb_store(tdb, key, orig_data, TDB_INSERT) == 0);
116
117         data = tdb_fetch(tdb, key);
118         ok1(data.dsize == strlen("world"));
119         ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
120         free(data.dptr);
121         ok1(tdb_transaction_commit(tdb) == 0);
122
123         ok1(tdb_traverse(tdb, test_traverse, &orig_data) == 1);
124         tdb_close(tdb);
125
126         return exit_status();
127 }