2 #include "tdb1-lock-tracking.h"
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);
9 #define pwrite pwrite_check
10 #define write write_check
11 #define fcntl fcntl_with_lockcheck1
12 #define ftruncate ftruncate_check
14 #include "tdb2-source.h"
15 #include <ccan/tap/tap.h>
20 #include "tdb1-external-agent.h"
21 #include "tdb1-logging.h"
23 static struct agent *agent;
25 static int errors = 0;
26 static bool clear_if_first;
27 #define TEST_DBNAME "run-open-during-transaction.tdb"
34 static bool is_same(const char *snapshot, const char *latest, off_t len)
38 for (i = 0; i < len; i++) {
39 if (snapshot[i] != latest[i])
45 static bool compare_file(int fd, const char *snapshot, off_t snapshot_len)
50 /* over-length read serves as length check. */
51 contents = malloc(snapshot_len+1);
52 same = pread(fd, contents, snapshot_len+1, 0) == snapshot_len
53 && is_same(snapshot, contents, snapshot_len);
58 static void check_file_intact(int fd)
60 enum agent_return ret;
65 contents = malloc(st.st_size);
66 if (pread(fd, contents, st.st_size, 0) != st.st_size) {
72 /* Ask agent to open file. */
73 ret = external_agent_operation1(agent, clear_if_first ?
74 OPEN_WITH_CLEAR_IF_FIRST :
78 /* It's OK to open it, but it must not have changed! */
79 if (!compare_file(fd, contents, st.st_size)) {
80 diag("Agent changed file after opening %s",
81 agent_return_name1(ret));
86 ret = external_agent_operation1(agent, CLOSE, NULL);
88 diag("Agent failed to close tdb: %s",
89 agent_return_name1(ret));
92 } else if (ret != WOULD_HAVE_BLOCKED) {
93 diag("Agent opening file gave %s",
94 agent_return_name1(ret));
101 static void after_unlock(int fd)
104 check_file_intact(fd);
107 static ssize_t pwrite_check(int fd,
108 const void *buf, size_t count, off_t offset)
111 check_file_intact(fd);
113 return pwrite(fd, buf, count, offset);
116 static ssize_t write_check(int fd, const void *buf, size_t count)
119 check_file_intact(fd);
121 return write(fd, buf, count);
124 static int ftruncate_check(int fd, off_t length)
127 check_file_intact(fd);
129 return ftruncate(fd, length);
133 int main(int argc, char *argv[])
135 const int flags[] = { TDB1_DEFAULT,
138 TDB1_CLEAR_IF_FIRST | TDB1_NOMMAP };
140 struct tdb1_context *tdb;
144 agent = prepare_external_agent1();
146 err(1, "preparing agent");
148 unlock_callback1 = after_unlock;
149 for (i = 0; i < sizeof(flags)/sizeof(flags[0]); i++) {
150 clear_if_first = (flags[i] & TDB1_CLEAR_IF_FIRST);
151 diag("Test with %s and %s\n",
152 clear_if_first ? "CLEAR" : "DEFAULT",
153 (flags[i] & TDB1_NOMMAP) ? "no mmap" : "mmap");
155 tdb = tdb1_open_ex(TEST_DBNAME, 1024, flags[i],
156 O_CREAT|O_TRUNC|O_RDWR, 0600,
161 ok1(tdb1_transaction_start(tdb) == 0);
162 key.dsize = strlen("hi");
163 key.dptr = (void *)"hi";
164 data.dptr = (void *)"world";
165 data.dsize = strlen("world");
167 ok1(tdb1_store(tdb, key, data, TDB1_INSERT) == 0);
168 ok1(tdb1_transaction_commit(tdb) == 0);
169 ok(!errors, "We had %u open errors", errors);
175 return exit_status();