1 #include "../private.h"
3 #include "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_lockcheck
12 #define ftruncate ftruncate_check
14 #include "ntdb-source.h"
15 #include "tap-interface.h"
19 #include "external-agent.h"
21 #include "helprun-external-agent.h"
23 static struct agent *agent;
25 static int errors = 0;
26 #define TEST_DBNAME "run-56-open-during-transaction.ntdb"
33 static bool is_same(const char *snapshot, const char *latest, off_t len)
37 for (i = 0; i < len; i++) {
38 if (snapshot[i] != latest[i])
44 static bool compare_file(int fd, const char *snapshot, off_t snapshot_len)
49 /* over-length read serves as length check. */
50 contents = malloc(snapshot_len+1);
51 ret = pread(fd, contents, snapshot_len+1, 0) == snapshot_len
52 && is_same(snapshot, contents, snapshot_len);
57 static void check_file_intact(int fd)
59 enum agent_return ret;
64 contents = malloc(st.st_size);
65 if (pread(fd, contents, st.st_size, 0) != st.st_size) {
71 /* Ask agent to open file. */
72 ret = external_agent_operation(agent, OPEN, TEST_DBNAME);
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_name(ret));
82 ret = external_agent_operation(agent, CLOSE, NULL);
84 diag("Agent failed to close ntdb: %s",
85 agent_return_name(ret));
88 } else if (ret != WOULD_HAVE_BLOCKED) {
89 diag("Agent opening file gave %s",
90 agent_return_name(ret));
97 static void after_unlock(int fd)
100 check_file_intact(fd);
103 static ssize_t pwrite_check(int fd,
104 const void *buf, size_t count, off_t offset)
107 check_file_intact(fd);
109 return pwrite(fd, buf, count, offset);
112 static ssize_t write_check(int fd, const void *buf, size_t count)
115 check_file_intact(fd);
117 return write(fd, buf, count);
120 static int ftruncate_check(int fd, off_t length)
123 check_file_intact(fd);
125 return ftruncate(fd, length);
129 int main(int argc, char *argv[])
131 const int flags[] = { NTDB_DEFAULT, NTDB_NOMMAP,
132 NTDB_CONVERT, NTDB_NOMMAP|NTDB_CONVERT };
134 struct ntdb_context *ntdb;
137 plan_tests(sizeof(flags)/sizeof(flags[0]) * 5);
138 agent = prepare_external_agent();
140 err(1, "preparing agent");
142 unlock_callback = after_unlock;
143 for (i = 0; i < sizeof(flags)/sizeof(flags[0]); i++) {
144 diag("Test with %s and %s\n",
145 (flags[i] & NTDB_CONVERT) ? "CONVERT" : "DEFAULT",
146 (flags[i] & NTDB_NOMMAP) ? "no mmap" : "mmap");
148 ntdb = ntdb_open(TEST_DBNAME, flags[i]|MAYBE_NOSYNC,
149 O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
153 ok1(ntdb_transaction_start(ntdb) == 0);
154 key = ntdb_mkdata("hi", strlen("hi"));
155 data = ntdb_mkdata("world", strlen("world"));
157 ok1(ntdb_store(ntdb, key, data, NTDB_INSERT) == 0);
158 ok1(ntdb_transaction_commit(ntdb) == 0);
159 ok(!errors, "We had %u open errors", errors);
165 return exit_status();