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