tdb2: allow multiple opens of the same file.
[ccan] / ccan / tdb2 / test / run-open-multiple-times.c
1 #include <ccan/tdb2/tdb.c>
2 #include <ccan/tdb2/open.c>
3 #include <ccan/tdb2/free.c>
4 #include <ccan/tdb2/lock.c>
5 #include <ccan/tdb2/io.c>
6 #include <ccan/tdb2/hash.c>
7 #include <ccan/tdb2/check.c>
8 #include <ccan/tdb2/transaction.c>
9 #include <ccan/tap/tap.h>
10 #include "logging.h"
11
12 int main(int argc, char *argv[])
13 {
14         unsigned int i;
15         struct tdb_context *tdb, *tdb2;
16         struct tdb_data key = { (unsigned char *)&i, sizeof(i) };
17         struct tdb_data data = { (unsigned char *)&i, sizeof(i) };
18         struct tdb_data d = { NULL, 0 }; /* Bogus GCC warning */
19         int flags[] = { TDB_DEFAULT, TDB_NOMMAP,
20                         TDB_CONVERT, TDB_NOMMAP|TDB_CONVERT };
21
22         plan_tests(sizeof(flags) / sizeof(flags[0]) * 28);
23         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
24                 tdb = tdb_open("run-open-multiple-times.tdb", flags[i],
25                                O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
26                 ok1(tdb);
27                 if (!tdb)
28                         continue;
29                 tdb2 = tdb_open("run-open-multiple-times.tdb", flags[i],
30                                 O_RDWR|O_CREAT, 0600, &tap_log_attr);
31                 ok1(tdb_check(tdb, NULL, NULL) == 0);
32                 ok1(tdb_check(tdb2, NULL, NULL) == 0);
33
34                 /* Store in one, fetch in the other. */
35                 ok1(tdb_store(tdb, key, data, TDB_REPLACE) == 0);
36                 ok1(tdb_fetch(tdb2, key, &d) == TDB_SUCCESS);
37                 ok1(d.dptr && d.dsize == data.dsize
38                     && memcmp(d.dptr, data.dptr, d.dsize) == 0);
39                 free(d.dptr);
40
41                 /* Vice versa, with delete. */
42                 ok1(tdb_delete(tdb2, key) == 0);
43                 ok1(tdb_fetch(tdb, key, &d) == TDB_ERR_NOEXIST);
44
45                 /* OK, now close first one, check second still good. */
46                 ok1(tdb_close(tdb) == 0);
47
48                 ok1(tdb_store(tdb2, key, data, TDB_REPLACE) == 0);
49                 ok1(tdb_fetch(tdb2, key, &d) == TDB_SUCCESS);
50                 ok1(d.dptr && d.dsize == data.dsize
51                     && memcmp(d.dptr, data.dptr, d.dsize) == 0);
52                 free(d.dptr);
53
54                 /* Reopen */
55                 tdb = tdb_open("run-open-multiple-times.tdb", flags[i],
56                                O_RDWR|O_CREAT, 0600, &tap_log_attr);
57                 ok1(tdb);
58
59                 ok1(tdb_transaction_start(tdb2) == 0);
60
61                 /* Anything in the other one should fail. */
62                 ok1(tdb_fetch(tdb, key, &d) == TDB_ERR_LOCK);
63                 ok1(tap_log_messages == 1);
64                 ok1(tdb_store(tdb, key, data, TDB_REPLACE) == TDB_ERR_LOCK);
65                 ok1(tap_log_messages == 2);
66                 ok1(tdb_transaction_start(tdb) == TDB_ERR_LOCK);
67                 ok1(tap_log_messages == 3);
68                 ok1(tdb_chainlock(tdb, key) == TDB_ERR_LOCK);
69                 ok1(tap_log_messages == 4);
70
71                 /* Transaciton should work as normal. */
72                 ok1(tdb_store(tdb2, key, data, TDB_REPLACE) == TDB_SUCCESS);
73
74                 /* Now... try closing with locks held. */
75                 ok1(tdb_close(tdb2) == 0);
76
77                 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
78                 ok1(d.dptr && d.dsize == data.dsize
79                     && memcmp(d.dptr, data.dptr, d.dsize) == 0);
80                 free(d.dptr);
81                 ok1(tdb_close(tdb) == 0);
82                 ok1(tap_log_messages == 4);
83                 tap_log_messages = 0;
84         }
85
86         return exit_status();
87 }