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