1 #include "../common/tdb_private.h"
2 #include "lock-tracking.h"
4 static ssize_t pwrite_check(int fd, const void *buf, size_t count, off_t offset);
5 static ssize_t write_check(int fd, const void *buf, size_t count);
6 static int ftruncate_check(int fd, off_t length);
8 #define pwrite pwrite_check
9 #define write write_check
10 #define fcntl fcntl_with_lockcheck
11 #define ftruncate ftruncate_check
13 #include "../common/io.c"
14 #include "../common/tdb.c"
15 #include "../common/lock.c"
16 #include "../common/freelist.c"
17 #include "../common/traverse.c"
18 #include "../common/transaction.c"
19 #include "../common/error.c"
20 #include "../common/open.c"
21 #include "../common/check.c"
22 #include "../common/hash.c"
23 #include "tap-interface.h"
27 #include "external-agent.h"
30 static struct agent *agent;
32 static int errors = 0;
33 static bool clear_if_first;
34 #define TEST_DBNAME "run-open-during-transaction.tdb"
41 static bool is_same(const char *snapshot, const char *latest, off_t len)
45 for (i = 0; i < len; i++) {
46 if (snapshot[i] != latest[i])
52 static bool compare_file(int fd, const char *snapshot, off_t snapshot_len)
57 /* over-length read serves as length check. */
58 contents = malloc(snapshot_len+1);
59 same = pread(fd, contents, snapshot_len+1, 0) == snapshot_len
60 && is_same(snapshot, contents, snapshot_len);
65 static void check_file_intact(int fd)
67 enum agent_return ret;
72 contents = malloc(st.st_size);
73 if (pread(fd, contents, st.st_size, 0) != st.st_size) {
79 /* Ask agent to open file. */
80 ret = external_agent_operation(agent, clear_if_first ?
81 OPEN_WITH_CLEAR_IF_FIRST :
85 /* It's OK to open it, but it must not have changed! */
86 if (!compare_file(fd, contents, st.st_size)) {
87 diag("Agent changed file after opening %s",
88 agent_return_name(ret));
93 ret = external_agent_operation(agent, CLOSE, NULL);
95 diag("Agent failed to close tdb: %s",
96 agent_return_name(ret));
99 } else if (ret != WOULD_HAVE_BLOCKED) {
100 diag("Agent opening file gave %s",
101 agent_return_name(ret));
108 static void after_unlock(int fd)
111 check_file_intact(fd);
114 static ssize_t pwrite_check(int fd,
115 const void *buf, size_t count, off_t offset)
118 check_file_intact(fd);
120 return pwrite(fd, buf, count, offset);
123 static ssize_t write_check(int fd, const void *buf, size_t count)
126 check_file_intact(fd);
128 return write(fd, buf, count);
131 static int ftruncate_check(int fd, off_t length)
134 check_file_intact(fd);
136 return ftruncate(fd, length);
140 int main(int argc, char *argv[])
142 const int flags[] = { TDB_DEFAULT,
145 TDB_CLEAR_IF_FIRST | TDB_NOMMAP };
147 struct tdb_context *tdb;
151 agent = prepare_external_agent();
153 unlock_callback = after_unlock;
154 for (i = 0; i < sizeof(flags)/sizeof(flags[0]); i++) {
155 clear_if_first = (flags[i] & TDB_CLEAR_IF_FIRST);
156 diag("Test with %s and %s",
157 clear_if_first ? "CLEAR" : "DEFAULT",
158 (flags[i] & TDB_NOMMAP) ? "no mmap" : "mmap");
160 tdb = tdb_open_ex(TEST_DBNAME, 1024, flags[i],
161 O_CREAT|O_TRUNC|O_RDWR, 0600,
166 ok1(tdb_transaction_start(tdb) == 0);
167 key.dsize = strlen("hi");
168 key.dptr = (void *)"hi";
169 data.dptr = (void *)"world";
170 data.dsize = strlen("world");
172 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
173 ok1(tdb_transaction_commit(tdb) == 0);
174 ok(!errors, "We had %u open errors", errors);
180 return exit_status();