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