tdb2: unify tdb1_store into tdb_store
[ccan] / ccan / tdb2 / test / run-tdb1-open-during-transaction.c
1 #include "config.h"
2 #include "tdb1-lock-tracking.h"
3 #include <unistd.h>
4
5 static ssize_t pwrite_check(int fd, const void *buf, size_t count, off_t offset);
6 static ssize_t write_check(int fd, const void *buf, size_t count);
7 static int ftruncate_check(int fd, off_t length);
8
9 #define pwrite pwrite_check
10 #define write write_check
11 #define fcntl fcntl_with_lockcheck1
12 #define ftruncate ftruncate_check
13
14 #include "tdb2-source.h"
15 #include <ccan/tap/tap.h>
16 #include <stdlib.h>
17 #include <stdbool.h>
18 #include <stdarg.h>
19 #include <err.h>
20 #include "tdb1-external-agent.h"
21 #include "logging.h"
22
23 static struct agent *agent;
24 static bool opened;
25 static int errors = 0;
26 #define TEST_DBNAME "run-open-during-transaction.tdb1"
27
28 #undef write
29 #undef pwrite
30 #undef fcntl
31 #undef ftruncate
32
33 static bool is_same(const char *snapshot, const char *latest, off_t len)
34 {
35         unsigned i;
36
37         for (i = 0; i < len; i++) {
38                 if (snapshot[i] != latest[i])
39                         return false;
40         }
41         return true;
42 }
43
44 static bool compare_file(int fd, const char *snapshot, off_t snapshot_len)
45 {
46         char *contents;
47         bool same;
48
49         /* over-length read serves as length check. */
50         contents = malloc(snapshot_len+1);
51         same = pread(fd, contents, snapshot_len+1, 0) == snapshot_len
52                 && is_same(snapshot, contents, snapshot_len);
53         free(contents);
54         return same;
55 }
56
57 static void check_file_intact(int fd)
58 {
59         enum agent_return ret;
60         struct stat st;
61         char *contents;
62
63         fstat(fd, &st);
64         contents = malloc(st.st_size);
65         if (pread(fd, contents, st.st_size, 0) != st.st_size) {
66                 diag("Read fail");
67                 errors++;
68                 return;
69         }
70
71         /* Ask agent to open file. */
72         ret = external_agent_operation1(agent, OPEN, TEST_DBNAME);
73
74         /* It's OK to open it, but it must not have changed! */
75         if (!compare_file(fd, contents, st.st_size)) {
76                 diag("Agent changed file after opening %s",
77                      agent_return_name1(ret));
78                 errors++;
79         }
80
81         if (ret == SUCCESS) {
82                 ret = external_agent_operation1(agent, CLOSE, NULL);
83                 if (ret != SUCCESS) {
84                         diag("Agent failed to close tdb: %s",
85                              agent_return_name1(ret));
86                         errors++;
87                 }
88         } else if (ret != WOULD_HAVE_BLOCKED) {
89                 diag("Agent opening file gave %s",
90                      agent_return_name1(ret));
91                 errors++;
92         }
93
94         free(contents);
95 }
96
97 static void after_unlock(int fd)
98 {
99         if (opened)
100                 check_file_intact(fd);
101 }
102
103 static ssize_t pwrite_check(int fd,
104                             const void *buf, size_t count, off_t offset)
105 {
106         if (opened)
107                 check_file_intact(fd);
108
109         return pwrite(fd, buf, count, offset);
110 }
111
112 static ssize_t write_check(int fd, const void *buf, size_t count)
113 {
114         if (opened)
115                 check_file_intact(fd);
116
117         return write(fd, buf, count);
118 }
119
120 static int ftruncate_check(int fd, off_t length)
121 {
122         if (opened)
123                 check_file_intact(fd);
124
125         return ftruncate(fd, length);
126
127 }
128
129 int main(int argc, char *argv[])
130 {
131         const int flags[] = { TDB_DEFAULT,
132                               TDB_NOMMAP };
133         int i;
134         struct tdb_context *tdb;
135         TDB_DATA key, data;
136         union tdb_attribute hsize;
137
138         hsize.base.attr = TDB_ATTRIBUTE_TDB1_HASHSIZE;
139         hsize.base.next = &tap_log_attr;
140         hsize.tdb1_hashsize.hsize = 1024;
141
142         plan_tests(10);
143         agent = prepare_external_agent1();
144         if (!agent)
145                 err(1, "preparing agent");
146
147         unlock_callback1 = after_unlock;
148         for (i = 0; i < sizeof(flags)/sizeof(flags[0]); i++) {
149                 diag("Test with %s and %s\n",
150                      "DEFAULT",
151                      (flags[i] & TDB_NOMMAP) ? "no mmap" : "mmap");
152                 unlink(TEST_DBNAME);
153                 tdb = tdb_open(TEST_DBNAME, flags[i]|TDB_VERSION1,
154                                O_CREAT|O_TRUNC|O_RDWR, 0600,
155                                &hsize);
156                 ok1(tdb);
157
158                 opened = true;
159                 ok1(tdb1_transaction_start(tdb) == 0);
160                 key.dsize = strlen("hi");
161                 key.dptr = (void *)"hi";
162                 data.dptr = (void *)"world";
163                 data.dsize = strlen("world");
164
165                 ok1(tdb_store(tdb, key, data, TDB_INSERT) == TDB_SUCCESS);
166                 ok1(tdb1_transaction_commit(tdb) == 0);
167                 ok(!errors, "We had %u open errors", errors);
168
169                 opened = false;
170                 tdb_close(tdb);
171         }
172
173         return exit_status();
174 }