]> git.ozlabs.org Git - ccan/blob - ccan/tdb/test/run-3G-file.c
tdb2: fix tdb_summary reports
[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/tdb/hash.c>
15 #include <ccan/tap/tap.h>
16 #include <stdlib.h>
17 #include <err.h>
18 #include "logging.h"
19
20 static int tdb_expand_file_sparse(struct tdb_context *tdb,
21                                   tdb_off_t size,
22                                   tdb_off_t addition)
23 {
24         if (tdb->read_only || tdb->traverse_read) {
25                 tdb->ecode = TDB_ERR_RDONLY;
26                 return -1;
27         }
28
29         if (ftruncate(tdb->fd, size+addition) == -1) {
30                 char b = 0;
31                 ssize_t written = pwrite(tdb->fd,  &b, 1, (size+addition) - 1);
32                 if (written == 0) {
33                         /* try once more, potentially revealing errno */
34                         written = pwrite(tdb->fd,  &b, 1, (size+addition) - 1);
35                 }
36                 if (written == 0) {
37                         /* again - give up, guessing errno */
38                         errno = ENOSPC;
39                 }
40                 if (written != 1) {
41                         TDB_LOG((tdb, TDB_DEBUG_FATAL, "expand_file to %d failed (%s)\n", 
42                                  size+addition, strerror(errno)));
43                         return -1;
44                 }
45         }
46
47         return 0;
48 }
49
50 static const struct tdb_methods large_io_methods = {
51         tdb_read,
52         tdb_write,
53         tdb_next_hash_chain,
54         tdb_oob,
55         tdb_expand_file_sparse
56 };
57
58 static int test_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
59                          void *_data)
60 {
61         TDB_DATA *expect = _data;
62         ok1(key.dsize == strlen("hi"));
63         ok1(memcmp(key.dptr, "hi", strlen("hi")) == 0);
64         ok1(data.dsize == expect->dsize);
65         ok1(memcmp(data.dptr, expect->dptr, data.dsize) == 0);
66         return 0;
67 }
68
69 int main(int argc, char *argv[])
70 {
71         struct tdb_context *tdb;
72         TDB_DATA key, orig_data, data;
73         uint32_t hash;
74         tdb_off_t rec_ptr;
75         struct tdb_record rec;
76
77         plan_tests(24);
78         tdb = tdb_open_ex("run-36-file.tdb", 1024, TDB_CLEAR_IF_FIRST,
79                           O_CREAT|O_TRUNC|O_RDWR, 0600, &taplogctx, NULL);
80
81         ok1(tdb);
82         tdb->methods = &large_io_methods;
83
84         /* Enlarge the file (internally multiplies by 100). */
85         ok1(tdb_expand(tdb, 30000000) == 0);
86
87         /* Put an entry in, and check it. */
88         key.dsize = strlen("hi");
89         key.dptr = (void *)"hi";
90         orig_data.dsize = strlen("world");
91         orig_data.dptr = (void *)"world";
92
93         ok1(tdb_store(tdb, key, orig_data, TDB_INSERT) == 0);
94
95         data = tdb_fetch(tdb, key);
96         ok1(data.dsize == strlen("world"));
97         ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
98         free(data.dptr);
99
100         /* That currently fills at the end, make sure that's true. */
101         hash = tdb->hash_fn(&key);
102         rec_ptr = tdb_find_lock_hash(tdb, key, hash, F_RDLCK, &rec);
103         ok1(rec_ptr);
104         ok1(rec_ptr > 2U*1024*1024*1024);
105         tdb_unlock(tdb, BUCKET(rec.full_hash), F_RDLCK);
106
107         /* Traverse must work. */
108         ok1(tdb_traverse(tdb, test_traverse, &orig_data) == 1);
109
110         /* Delete should work. */
111         ok1(tdb_delete(tdb, key) == 0);
112
113         ok1(tdb_traverse(tdb, test_traverse, NULL) == 0);
114
115         /* Transactions should work. */
116         ok1(tdb_transaction_start(tdb) == 0);
117         ok1(tdb_store(tdb, key, orig_data, TDB_INSERT) == 0);
118
119         data = tdb_fetch(tdb, key);
120         ok1(data.dsize == strlen("world"));
121         ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
122         free(data.dptr);
123         ok1(tdb_transaction_commit(tdb) == 0);
124
125         ok1(tdb_traverse(tdb, test_traverse, &orig_data) == 1);
126         tdb_close(tdb);
127
128         return exit_status();
129 }