]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/test/run-tdb1-3G-file.c
tdb2: Remove unused tdb1 functions.
[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->ecode = TDB1_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                         TDB1_LOG((tdb, TDB1_DEBUG_FATAL, "expand_file to %d failed (%s)\n",
31                                  size+addition, strerror(errno)));
32                         return -1;
33                 }
34         }
35
36         return 0;
37 }
38
39 static const struct tdb1_methods large_io_methods = {
40         tdb1_read,
41         tdb1_write,
42         tdb1_next_hash_chain,
43         tdb1_oob,
44         tdb1_expand_file_sparse
45 };
46
47 static int test_traverse(struct tdb1_context *tdb, TDB1_DATA key, TDB1_DATA data,
48                          void *_data)
49 {
50         TDB1_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);
55         return 0;
56 }
57
58 int main(int argc, char *argv[])
59 {
60         struct tdb1_context *tdb;
61         TDB1_DATA key, orig_data, data;
62         uint32_t hash;
63         tdb1_off_t rec_ptr;
64         struct tdb1_record rec;
65
66         plan_tests(24);
67         tdb = tdb1_open_ex("run-36-file.tdb", 1024, TDB1_CLEAR_IF_FIRST,
68                           O_CREAT|O_TRUNC|O_RDWR, 0600, &taplogctx, NULL);
69
70         ok1(tdb);
71         tdb->methods = &large_io_methods;
72
73         /* Enlarge the file (internally multiplies by 2). */
74         ok1(tdb1_expand(tdb, 1500000000) == 0);
75
76         /* Put an entry in, and check it. */
77         key.dsize = strlen("hi");
78         key.dptr = (void *)"hi";
79         orig_data.dsize = strlen("world");
80         orig_data.dptr = (void *)"world";
81
82         ok1(tdb1_store(tdb, key, orig_data, TDB1_INSERT) == 0);
83
84         data = tdb1_fetch(tdb, key);
85         ok1(data.dsize == strlen("world"));
86         ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
87         free(data.dptr);
88
89         /* That currently fills at the end, make sure that's true. */
90         hash = tdb->hash_fn(&key);
91         rec_ptr = tdb1_find_lock_hash(tdb, key, hash, F_RDLCK, &rec);
92         ok1(rec_ptr);
93         ok1(rec_ptr > 2U*1024*1024*1024);
94         tdb1_unlock(tdb, TDB1_BUCKET(rec.full_hash), F_RDLCK);
95
96         /* Traverse must work. */
97         ok1(tdb1_traverse(tdb, test_traverse, &orig_data) == 1);
98
99         /* Delete should work. */
100         ok1(tdb1_delete(tdb, key) == 0);
101
102         ok1(tdb1_traverse(tdb, test_traverse, NULL) == 0);
103
104         /* Transactions should work. */
105         ok1(tdb1_transaction_start(tdb) == 0);
106         ok1(tdb1_store(tdb, key, orig_data, TDB1_INSERT) == 0);
107
108         data = tdb1_fetch(tdb, key);
109         ok1(data.dsize == strlen("world"));
110         ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
111         free(data.dptr);
112         ok1(tdb1_transaction_commit(tdb) == 0);
113
114         ok1(tdb1_traverse(tdb, test_traverse, &orig_data) == 1);
115         tdb1_close(tdb);
116
117         return exit_status();
118 }