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