s3:include: remove typedef user_struct
[Samba/gebeck_regimport.git] / lib / tdb2 / test / run-tdb1-no-lock-during-traverse.c
blob127e69ad1cfdb6dec7248db9e153f1cc5a7b0fc0
1 #include "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 "tap-interface.h"
9 #include <stdlib.h>
10 #include <err.h>
11 #include "logging.h"
13 #undef fcntl
15 #define NUM_ENTRIES 10
17 static bool prepare_entries(struct tdb_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 = tdb_mkdata("world", strlen("world"));
27 if (tdb_store(tdb, key, data, 0) != TDB_SUCCESS)
28 return false;
30 return true;
33 static void delete_entries(struct tdb_context *tdb)
35 unsigned int i;
36 TDB_DATA key;
38 for (i = 0; i < NUM_ENTRIES; i++) {
39 key.dsize = sizeof(i);
40 key.dptr = (void *)&i;
42 ok1(tdb_delete(tdb, key) == TDB_SUCCESS);
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)
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;
59 static int delete_self(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
60 void *private_data)
62 ok1(tdb_delete(tdb, key) == TDB_SUCCESS);
63 return 0;
66 int main(int argc, char *argv[])
68 struct tdb_context *tdb;
69 int errors = 0;
70 union tdb_attribute hsize;
72 hsize.base.attr = TDB_ATTRIBUTE_TDB1_HASHSIZE;
73 hsize.base.next = &tap_log_attr;
74 hsize.tdb1_hashsize.hsize = 1024;
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);
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);
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);
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);
107 ok1(tdb_close(tdb) == 0);
109 return exit_status();