]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/test/run-remap-in-read_traverse.c
tdb2: move open routines into a separate file (open.c)
[ccan] / ccan / tdb2 / test / run-remap-in-read_traverse.c
1 /* We had a bug where we marked the tdb read-only for a tdb_traverse_read.
2  * If we then expanded the tdb, we would remap read-only, and later SEGV. */
3 #include <ccan/tdb2/tdb.c>
4 #include <ccan/tdb2/open.c>
5 #include <ccan/tdb2/free.c>
6 #include <ccan/tdb2/lock.c>
7 #include <ccan/tdb2/io.c>
8 #include <ccan/tdb2/hash.c>
9 #include <ccan/tdb2/check.c>
10 #include <ccan/tdb2/traverse.c>
11 #include <ccan/tdb2/transaction.c>
12 #include <ccan/tap/tap.h>
13 #include "external-agent.h"
14 #include "logging.h"
15
16 static bool file_larger(int fd, tdb_len_t size)
17 {
18         struct stat st;
19
20         fstat(fd, &st);
21         return st.st_size != size;
22 }
23
24 static unsigned add_records_to_grow(struct agent *agent, int fd, tdb_len_t size)
25 {
26         unsigned int i;
27
28         for (i = 0; !file_larger(fd, size); i++) {
29                 char data[20];
30                 sprintf(data, "%i", i);
31                 if (external_agent_operation(agent, STORE, data) != SUCCESS)
32                         return 0;
33         }
34         diag("Added %u records to grow file", i);
35         return i;
36 }
37
38 int main(int argc, char *argv[])
39 {
40         unsigned int i;
41         struct agent *agent;
42         struct tdb_context *tdb;
43         struct tdb_data d = { (unsigned char *)"hello", 5 };
44         const char filename[] = "run-remap-in-read_traverse.tdb";
45
46         plan_tests(4);
47
48         agent = prepare_external_agent();
49
50         tdb = tdb_open(filename, TDB_DEFAULT,
51                        O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
52
53         ok1(external_agent_operation(agent, OPEN, filename) == SUCCESS);
54         i = add_records_to_grow(agent, tdb->fd, tdb->map_size);
55
56         /* Do a traverse. */
57         ok1(tdb_traverse(tdb, NULL, NULL) == i);
58
59         /* Now store something! */
60         ok1(tdb_store(tdb, d, d, TDB_INSERT) == 0);
61         ok1(tap_log_messages == 0);
62         free_external_agent(agent);
63         return exit_status();
64 }