]> git.ozlabs.org Git - ccan/blob - ccan/tdb/test/run-no-lock-during-traverse.c
tdb: missing files from checkin.
[ccan] / ccan / tdb / test / run-no-lock-during-traverse.c
1 #define _XOPEN_SOURCE 500
2 #include <unistd.h>
3 #include "lock-tracking.h"
4
5 #define fcntl fcntl_with_lockcheck
6
7 #include <ccan/tdb/tdb.h>
8 #include <ccan/tdb/io.c>
9 #include <ccan/tdb/tdb.c>
10 #include <ccan/tdb/lock.c>
11 #include <ccan/tdb/freelist.c>
12 #include <ccan/tdb/traverse.c>
13 #include <ccan/tdb/transaction.c>
14 #include <ccan/tdb/error.c>
15 #include <ccan/tdb/open.c>
16 #include <ccan/tdb/check.c>
17 #include <ccan/tap/tap.h>
18 #include <stdlib.h>
19 #include <err.h>
20 #include "logging.h"
21
22 #undef fcntl
23
24 #define NUM_ENTRIES 10
25
26 static bool prepare_entries(struct tdb_context *tdb)
27 {
28         unsigned int i;
29         TDB_DATA key, data;
30         
31         for (i = 0; i < NUM_ENTRIES; i++) {
32                 key.dsize = sizeof(i);
33                 key.dptr = (void *)&i;
34                 data.dsize = strlen("world");
35                 data.dptr = (void *)"world";
36
37                 if (tdb_store(tdb, key, data, 0) != 0)
38                         return false;
39         }
40         return true;
41 }
42
43 static void delete_entries(struct tdb_context *tdb)
44 {
45         unsigned int i;
46         TDB_DATA key;
47
48         for (i = 0; i < NUM_ENTRIES; i++) {
49                 key.dsize = sizeof(i);
50                 key.dptr = (void *)&i;
51
52                 ok1(tdb_delete(tdb, key) == 0);
53         }
54 }
55
56 /* We don't know how many times this will run. */
57 static int delete_other(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
58                         void *private_data)
59 {
60         unsigned int i;
61         memcpy(&i, key.dptr, 4);
62         i = (i + 1) % NUM_ENTRIES;
63         key.dptr = (void *)&i;
64         if (tdb_delete(tdb, key) != 0)
65                 (*(int *)private_data)++;
66         return 0;
67 }
68
69 static int delete_self(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
70                         void *private_data)
71 {
72         ok1(tdb_delete(tdb, key) == 0);
73         return 0;
74 }
75
76 int main(int argc, char *argv[])
77 {
78         struct tdb_context *tdb;
79         int errors = 0;
80
81         plan_tests(41);
82         tdb = tdb_open_ex("run-no-lock-during-traverse.tdb",
83                           1024, TDB_CLEAR_IF_FIRST, O_CREAT|O_TRUNC|O_RDWR,
84                           0600, &taplogctx, NULL);
85
86         ok1(tdb);
87         ok1(prepare_entries(tdb));
88         ok1(locking_errors == 0);
89         ok1(tdb_lockall(tdb) == 0);
90         ok1(locking_errors == 0);
91         tdb_traverse(tdb, delete_other, &errors);
92         ok1(errors == 0);
93         ok1(locking_errors == 0);
94         ok1(tdb_unlockall(tdb) == 0);
95
96         ok1(prepare_entries(tdb));
97         ok1(locking_errors == 0);
98         ok1(tdb_lockall(tdb) == 0);
99         ok1(locking_errors == 0);
100         tdb_traverse(tdb, delete_self, NULL);
101         ok1(locking_errors == 0);
102         ok1(tdb_unlockall(tdb) == 0);
103
104         ok1(prepare_entries(tdb));
105         ok1(locking_errors == 0);
106         ok1(tdb_lockall(tdb) == 0);
107         ok1(locking_errors == 0);
108         delete_entries(tdb);
109         ok1(locking_errors == 0);
110         ok1(tdb_unlockall(tdb) == 0);
111
112         ok1(tdb_close(tdb) == 0);       
113
114         return exit_status();
115 }