0f299a32fe1427d2ed7b598f1dff4b068d594865
[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 "logging.h"
8
9 static int tdb1_expand_file_sparse(struct tdb_context *tdb,
10                                   tdb1_off_t size,
11                                   tdb1_off_t addition)
12 {
13         if ((tdb->flags & TDB_RDONLY) || tdb->tdb1.traverse_read) {
14                 tdb->last_error = TDB_ERR_RDONLY;
15                 return -1;
16         }
17
18         if (ftruncate(tdb->file->fd, size+addition) == -1) {
19                 char b = 0;
20                 ssize_t written = pwrite(tdb->file->fd,  &b, 1, (size+addition) - 1);
21                 if (written == 0) {
22                         /* try once more, potentially revealing errno */
23                         written = pwrite(tdb->file->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 tdb_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 tdb_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         union tdb_attribute hsize;
68
69         hsize.base.attr = TDB_ATTRIBUTE_TDB1_HASHSIZE;
70         hsize.base.next = &tap_log_attr;
71         hsize.tdb1_hashsize.hsize = 1024;
72
73         plan_tests(24);
74         tdb = tdb_open("run-36-file.tdb1", TDB_VERSION1,
75                        O_CREAT|O_TRUNC|O_RDWR, 0600, &hsize);
76
77         ok1(tdb);
78         tdb->tdb1.io = &large_io_methods;
79
80         /* Enlarge the file (internally multiplies by 2). */
81         ok1(tdb1_expand(tdb, 1500000000) == 0);
82
83         /* Put an entry in, and check it. */
84         key.dsize = strlen("hi");
85         key.dptr = (void *)"hi";
86         orig_data.dsize = strlen("world");
87         orig_data.dptr = (void *)"world";
88
89         ok1(tdb_store(tdb, key, orig_data, TDB_INSERT) == TDB_SUCCESS);
90
91         data = tdb1_fetch(tdb, key);
92         ok1(data.dsize == strlen("world"));
93         ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
94         free(data.dptr);
95
96         /* That currently fills at the end, make sure that's true. */
97         hash = tdb_hash(tdb, key.dptr, key.dsize);
98         rec_ptr = tdb1_find_lock_hash(tdb, key, hash, F_RDLCK, &rec);
99         ok1(rec_ptr);
100         ok1(rec_ptr > 2U*1024*1024*1024);
101         tdb1_unlock(tdb, TDB1_BUCKET(rec.full_hash), F_RDLCK);
102
103         /* Traverse must work. */
104         ok1(tdb1_traverse(tdb, test_traverse, &orig_data) == 1);
105
106         /* Delete should work. */
107         ok1(tdb1_delete(tdb, key) == 0);
108
109         ok1(tdb1_traverse(tdb, test_traverse, NULL) == 0);
110
111         /* Transactions should work. */
112         ok1(tdb1_transaction_start(tdb) == 0);
113         ok1(tdb_store(tdb, key, orig_data, TDB_INSERT) == TDB_SUCCESS);
114
115         data = tdb1_fetch(tdb, key);
116         ok1(data.dsize == strlen("world"));
117         ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
118         free(data.dptr);
119         ok1(tdb1_transaction_commit(tdb) == 0);
120
121         ok1(tdb1_traverse(tdb, test_traverse, &orig_data) == 1);
122         tdb_close(tdb);
123
124         return exit_status();
125 }