]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/test/run-tdb1-3G-file.c
tdb2: Make TDB1 code use TDB2's open flags.
[ccan] / ccan / tdb2 / test / run-tdb1-3G-file.c
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>
5 #include <stdlib.h>
6 #include <err.h>
7 #include "tdb1-logging.h"
8
9 static int tdb1_expand_file_sparse(struct tdb1_context *tdb,
10                                   tdb1_off_t size,
11                                   tdb1_off_t addition)
12 {
13         if (tdb->read_only || tdb->traverse_read) {
14                 tdb->last_error = TDB_ERR_RDONLY;
15                 return -1;
16         }
17
18         if (ftruncate(tdb->fd, size+addition) == -1) {
19                 char b = 0;
20                 ssize_t written = pwrite(tdb->fd,  &b, 1, (size+addition) - 1);
21                 if (written == 0) {
22                         /* try once more, potentially revealing errno */
23                         written = pwrite(tdb->fd,  &b, 1, (size+addition) - 1);
24                 }
25                 if (written == 0) {
26                         /* again - give up, guessing errno */
27                         errno = ENOSPC;
28                 }
29                 if (written != 1) {
30                         tdb->last_error = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
31                                                 "expand_file to %d failed (%s)",
32                                                 size+addition,
33                                                 strerror(errno));
34                         return -1;
35                 }
36         }
37
38         return 0;
39 }
40
41 static const struct tdb1_methods large_io_methods = {
42         tdb1_read,
43         tdb1_write,
44         tdb1_next_hash_chain,
45         tdb1_oob,
46         tdb1_expand_file_sparse
47 };
48
49 static int test_traverse(struct tdb1_context *tdb, TDB_DATA key, TDB_DATA data,
50                          void *_data)
51 {
52         TDB_DATA *expect = _data;
53         ok1(key.dsize == strlen("hi"));
54         ok1(memcmp(key.dptr, "hi", strlen("hi")) == 0);
55         ok1(data.dsize == expect->dsize);
56         ok1(memcmp(data.dptr, expect->dptr, data.dsize) == 0);
57         return 0;
58 }
59
60 int main(int argc, char *argv[])
61 {
62         struct tdb1_context *tdb;
63         TDB_DATA key, orig_data, data;
64         uint32_t hash;
65         tdb1_off_t rec_ptr;
66         struct tdb1_record rec;
67
68         plan_tests(24);
69         tdb = tdb1_open_ex("run-36-file.tdb", 1024, TDB_DEFAULT,
70                           O_CREAT|O_TRUNC|O_RDWR, 0600, &taplogctx, NULL);
71
72         ok1(tdb);
73         tdb->methods = &large_io_methods;
74
75         /* Enlarge the file (internally multiplies by 2). */
76         ok1(tdb1_expand(tdb, 1500000000) == 0);
77
78         /* Put an entry in, and check it. */
79         key.dsize = strlen("hi");
80         key.dptr = (void *)"hi";
81         orig_data.dsize = strlen("world");
82         orig_data.dptr = (void *)"world";
83
84         ok1(tdb1_store(tdb, key, orig_data, TDB_INSERT) == 0);
85
86         data = tdb1_fetch(tdb, key);
87         ok1(data.dsize == strlen("world"));
88         ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
89         free(data.dptr);
90
91         /* That currently fills at the end, make sure that's true. */
92         hash = tdb->hash_fn(&key);
93         rec_ptr = tdb1_find_lock_hash(tdb, key, hash, F_RDLCK, &rec);
94         ok1(rec_ptr);
95         ok1(rec_ptr > 2U*1024*1024*1024);
96         tdb1_unlock(tdb, TDB1_BUCKET(rec.full_hash), F_RDLCK);
97
98         /* Traverse must work. */
99         ok1(tdb1_traverse(tdb, test_traverse, &orig_data) == 1);
100
101         /* Delete should work. */
102         ok1(tdb1_delete(tdb, key) == 0);
103
104         ok1(tdb1_traverse(tdb, test_traverse, NULL) == 0);
105
106         /* Transactions should work. */
107         ok1(tdb1_transaction_start(tdb) == 0);
108         ok1(tdb1_store(tdb, key, orig_data, TDB_INSERT) == 0);
109
110         data = tdb1_fetch(tdb, key);
111         ok1(data.dsize == strlen("world"));
112         ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
113         free(data.dptr);
114         ok1(tdb1_transaction_commit(tdb) == 0);
115
116         ok1(tdb1_traverse(tdb, test_traverse, &orig_data) == 1);
117         tdb1_close(tdb);
118
119         return exit_status();
120 }