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