1 #define _XOPEN_SOURCE 500
3 static ssize_t pwrite_check(int fd, const void *buf, size_t count, off_t offset);
4 static ssize_t write_check(int fd, const void *buf, size_t count);
5 static int fcntl_check(int fd, int cmd, ... /* arg */ );
6 static int ftruncate_check(int fd, off_t length);
8 #define pwrite pwrite_check
9 #define write write_check
10 #define fcntl fcntl_check
11 #define ftruncate ftruncate_check
13 #include <ccan/tdb/tdb.h>
14 #include <ccan/tdb/io.c>
15 #include <ccan/tdb/tdb.c>
16 #include <ccan/tdb/lock.c>
17 #include <ccan/tdb/freelist.c>
18 #include <ccan/tdb/traverse.c>
19 #include <ccan/tdb/transaction.c>
20 #include <ccan/tdb/error.c>
21 #include <ccan/tdb/open.c>
22 #include <ccan/tap/tap.h>
27 #include "external-transaction.h"
29 static struct agent *agent;
30 static bool agent_pending;
31 static bool in_transaction;
32 static int errors = 0;
33 static bool snapshot_uptodate;
34 static char *snapshot;
35 static off_t snapshot_len;
36 static bool clear_if_first;
37 #define TEST_DBNAME "/tmp/test7.tdb"
44 static void save_file_contents(int fd)
49 /* Save copy of file. */
50 stat(TEST_DBNAME, &st);
51 if (snapshot_len != st.st_size) {
52 snapshot = realloc(snapshot, st.st_size * 2);
53 snapshot_len = st.st_size;
55 res = pread(fd, snapshot, snapshot_len, 0);
56 if (res != snapshot_len)
57 err(1, "Reading %zu bytes = %u", (size_t)snapshot_len, res);
58 snapshot_uptodate = true;
61 static void check_for_agent(int fd, bool block)
66 if (!external_agent_operation_check(agent, block, &res))
70 err(1, "Agent failed open");
71 agent_pending = false;
73 if (!snapshot_uptodate)
76 stat(TEST_DBNAME, &st);
77 if (st.st_size != snapshot_len) {
78 diag("Other open changed size from %zu -> %zu",
79 (size_t)snapshot_len, (size_t)st.st_size);
84 if (pread(fd, snapshot+snapshot_len, snapshot_len, 0) != snapshot_len)
85 err(1, "Reading %zu bytes", (size_t)snapshot_len);
86 if (memcmp(snapshot, snapshot+snapshot_len, snapshot_len) != 0) {
93 static void check_file_contents(int fd)
96 check_for_agent(fd, false);
99 save_file_contents(fd);
101 /* Ask agent to open file. */
102 external_agent_operation_start(agent,
104 OPEN_WITH_CLEAR_IF_FIRST :
107 agent_pending = true;
108 /* Hack: give it a chance to run. */
112 check_for_agent(fd, false);
115 static ssize_t pwrite_check(int fd,
116 const void *buf, size_t count, off_t offset)
121 check_file_contents(fd);
123 snapshot_uptodate = false;
124 ret = pwrite(fd, buf, count, offset);
129 check_file_contents(fd);
133 static ssize_t write_check(int fd, const void *buf, size_t count)
138 check_file_contents(fd);
140 snapshot_uptodate = false;
142 ret = write(fd, buf, count);
147 check_file_contents(fd);
151 /* This seems to be a macro for glibc... */
152 extern int fcntl(int fd, int cmd, ... /* arg */ );
154 static int fcntl_check(int fd, int cmd, ... /* arg */ )
160 if (cmd != F_SETLK && cmd != F_SETLKW) {
161 /* This may be totally bogus, but we don't know in general. */
163 arg3 = va_arg(ap, int);
166 return fcntl(fd, cmd, arg3);
170 fl = va_arg(ap, struct flock *);
173 ret = fcntl(fd, cmd, fl);
175 if (in_transaction && fl->l_type == F_UNLCK)
176 check_file_contents(fd);
180 static int ftruncate_check(int fd, off_t length)
185 check_file_contents(fd);
187 snapshot_uptodate = false;
189 ret = ftruncate(fd, length);
192 check_file_contents(fd);
196 int main(int argc, char *argv[])
198 const int flags[] = { TDB_DEFAULT,
201 TDB_CLEAR_IF_FIRST | TDB_NOMMAP };
203 struct tdb_context *tdb;
207 agent = prepare_external_agent();
209 err(1, "preparing agent");
211 /* Nice ourselves down: we can't tell the difference between agent
212 * blocking on lock, and agent not scheduled. */
215 for (i = 0; i < sizeof(flags)/sizeof(flags[0]); i++) {
217 tdb = tdb_open(TEST_DBNAME, 1024, flags[i],
218 O_CREAT|O_TRUNC|O_RDWR, 0600);
220 clear_if_first = (flags[i] & TDB_CLEAR_IF_FIRST);
222 ok1(tdb_transaction_start(tdb) == 0);
223 in_transaction = true;
224 key.dsize = strlen("hi");
225 key.dptr = (void *)"hi";
226 data.dptr = (void *)"world";
227 data.dsize = strlen("world");
229 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
230 ok1(tdb_transaction_commit(tdb) == 0);
232 check_for_agent(tdb->fd, true);
233 ok(errors == 0, "We had %u errors", errors);
238 return exit_status();