tdb2: Make TDB1 code use TDB2's open flags.
[Samba/gebeck_regimport.git] / lib / tdb2 / test / run-tdb1-no-lock-during-traverse.c
blobc1cb40a49e160cb9b41c691a1fd1c9e34bdc2903
1 #include <ccan/tdb2/private.h>
2 #include <unistd.h>
3 #include "tdb1-lock-tracking.h"
5 #define fcntl fcntl_with_lockcheck1
7 #include "tdb2-source.h"
8 #include <ccan/tap/tap.h>
9 #include <stdlib.h>
10 #include <err.h>
11 #include "tdb1-logging.h"
13 #undef fcntl
15 #define NUM_ENTRIES 10
17 static bool prepare_entries(struct tdb1_context *tdb)
19 unsigned int i;
20 TDB_DATA key, data;
22 for (i = 0; i < NUM_ENTRIES; i++) {
23 key.dsize = sizeof(i);
24 key.dptr = (void *)&i;
25 data.dsize = strlen("world");
26 data.dptr = (void *)"world";
28 if (tdb1_store(tdb, key, data, 0) != 0)
29 return false;
31 return true;
34 static void delete_entries(struct tdb1_context *tdb)
36 unsigned int i;
37 TDB_DATA key;
39 for (i = 0; i < NUM_ENTRIES; i++) {
40 key.dsize = sizeof(i);
41 key.dptr = (void *)&i;
43 ok1(tdb1_delete(tdb, key) == 0);
47 /* We don't know how many times this will run. */
48 static int delete_other(struct tdb1_context *tdb, TDB_DATA key, TDB_DATA data,
49 void *private_data)
51 unsigned int i;
52 memcpy(&i, key.dptr, 4);
53 i = (i + 1) % NUM_ENTRIES;
54 key.dptr = (void *)&i;
55 if (tdb1_delete(tdb, key) != 0)
56 (*(int *)private_data)++;
57 return 0;
60 static int delete_self(struct tdb1_context *tdb, TDB_DATA key, TDB_DATA data,
61 void *private_data)
63 ok1(tdb1_delete(tdb, key) == 0);
64 return 0;
67 int main(int argc, char *argv[])
69 struct tdb1_context *tdb;
70 int errors = 0;
72 plan_tests(41);
73 tdb = tdb1_open_ex("run-no-lock-during-traverse.tdb",
74 1024, TDB_DEFAULT, O_CREAT|O_TRUNC|O_RDWR,
75 0600, &taplogctx, NULL);
77 ok1(tdb);
78 ok1(prepare_entries(tdb));
79 ok1(locking_errors1 == 0);
80 ok1(tdb1_lockall(tdb) == 0);
81 ok1(locking_errors1 == 0);
82 tdb1_traverse(tdb, delete_other, &errors);
83 ok1(errors == 0);
84 ok1(locking_errors1 == 0);
85 ok1(tdb1_unlockall(tdb) == 0);
87 ok1(prepare_entries(tdb));
88 ok1(locking_errors1 == 0);
89 ok1(tdb1_lockall(tdb) == 0);
90 ok1(locking_errors1 == 0);
91 tdb1_traverse(tdb, delete_self, NULL);
92 ok1(locking_errors1 == 0);
93 ok1(tdb1_unlockall(tdb) == 0);
95 ok1(prepare_entries(tdb));
96 ok1(locking_errors1 == 0);
97 ok1(tdb1_lockall(tdb) == 0);
98 ok1(locking_errors1 == 0);
99 delete_entries(tdb);
100 ok1(locking_errors1 == 0);
101 ok1(tdb1_unlockall(tdb) == 0);
103 ok1(tdb1_close(tdb) == 0);
105 return exit_status();