]> git.ozlabs.org Git - ccan/blobdiff - ccan/tdb2/test/run-remap-in-read_traverse.c
tdb2: fix remapping inside tdb_traverse_read
[ccan] / ccan / tdb2 / test / run-remap-in-read_traverse.c
diff --git a/ccan/tdb2/test/run-remap-in-read_traverse.c b/ccan/tdb2/test/run-remap-in-read_traverse.c
new file mode 100644 (file)
index 0000000..77fdc9e
--- /dev/null
@@ -0,0 +1,61 @@
+/* We had a bug where we marked the tdb read-only for a tdb_traverse_read.
+ * If we then expanded the tdb, we would remap read-only, and later SEGV. */
+#include <ccan/tdb2/tdb.c>
+#include <ccan/tdb2/free.c>
+#include <ccan/tdb2/lock.c>
+#include <ccan/tdb2/io.c>
+#include <ccan/tdb2/hash.c>
+#include <ccan/tdb2/check.c>
+#include <ccan/tdb2/traverse.c>
+#include <ccan/tap/tap.h>
+#include "external-agent.h"
+#include "logging.h"
+
+static bool file_larger(int fd, tdb_len_t size)
+{
+       struct stat st;
+
+       fstat(fd, &st);
+       return st.st_size != size;
+}
+
+static unsigned add_records_to_grow(struct agent *agent, int fd, tdb_len_t size)
+{
+       unsigned int i;
+
+       for (i = 0; !file_larger(fd, size); i++) {
+               char data[20];
+               sprintf(data, "%i", i);
+               if (external_agent_operation(agent, STORE, data) != SUCCESS)
+                       return 0;
+       }
+       diag("Added %u records to grow file", i);
+       return i;
+}
+
+int main(int argc, char *argv[])
+{
+       unsigned int i;
+       struct agent *agent;
+       struct tdb_context *tdb;
+       struct tdb_data d = { (unsigned char *)"hello", 5 };
+       const char filename[] = "run-remap-in-read_traverse.tdb";
+
+       plan_tests(4);
+
+       agent = prepare_external_agent();
+
+       tdb = tdb_open(filename, TDB_DEFAULT,
+                      O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
+
+       ok1(external_agent_operation(agent, OPEN, filename) == SUCCESS);
+       i = add_records_to_grow(agent, tdb->fd, tdb->map_size);
+
+       /* Do a read traverse. */
+       ok1(tdb_traverse_read(tdb, NULL, NULL) == i);
+
+       /* Now store something! */
+       ok1(tdb_store(tdb, d, d, TDB_INSERT) == 0);
+       ok1(tap_log_messages == 0);
+       return exit_status();
+}