1 #include <ccan/tdb2/tdb.c>
2 #include <ccan/tdb2/open.c>
3 #include <ccan/tdb2/free.c>
4 #include <ccan/tdb2/lock.c>
5 #include <ccan/tdb2/io.c>
6 #include <ccan/tdb2/hash.c>
7 #include <ccan/tdb2/check.c>
8 #include <ccan/tdb2/transaction.c>
9 #include <ccan/tap/tap.h>
12 int main(int argc
, char *argv
[])
15 struct tdb_context
*tdb
, *tdb2
;
16 struct tdb_data key
= { (unsigned char *)&i
, sizeof(i
) };
17 struct tdb_data data
= { (unsigned char *)&i
, sizeof(i
) };
18 struct tdb_data d
= { NULL
, 0 }; /* Bogus GCC warning */
19 int flags
[] = { TDB_DEFAULT
, TDB_NOMMAP
,
20 TDB_CONVERT
, TDB_NOMMAP
|TDB_CONVERT
};
22 plan_tests(sizeof(flags
) / sizeof(flags
[0]) * 28);
23 for (i
= 0; i
< sizeof(flags
) / sizeof(flags
[0]); i
++) {
24 tdb
= tdb_open("run-open-multiple-times.tdb", flags
[i
],
25 O_RDWR
|O_CREAT
|O_TRUNC
, 0600, &tap_log_attr
);
29 tdb2
= tdb_open("run-open-multiple-times.tdb", flags
[i
],
30 O_RDWR
|O_CREAT
, 0600, &tap_log_attr
);
31 ok1(tdb_check(tdb
, NULL
, NULL
) == 0);
32 ok1(tdb_check(tdb2
, NULL
, NULL
) == 0);
34 /* Store in one, fetch in the other. */
35 ok1(tdb_store(tdb
, key
, data
, TDB_REPLACE
) == 0);
36 ok1(tdb_fetch(tdb2
, key
, &d
) == TDB_SUCCESS
);
37 ok1(tdb_deq(d
, data
));
40 /* Vice versa, with delete. */
41 ok1(tdb_delete(tdb2
, key
) == 0);
42 ok1(tdb_fetch(tdb
, key
, &d
) == TDB_ERR_NOEXIST
);
44 /* OK, now close first one, check second still good. */
45 ok1(tdb_close(tdb
) == 0);
47 ok1(tdb_store(tdb2
, key
, data
, TDB_REPLACE
) == 0);
48 ok1(tdb_fetch(tdb2
, key
, &d
) == TDB_SUCCESS
);
49 ok1(tdb_deq(d
, data
));
53 tdb
= tdb_open("run-open-multiple-times.tdb", flags
[i
],
54 O_RDWR
|O_CREAT
, 0600, &tap_log_attr
);
57 ok1(tdb_transaction_start(tdb2
) == 0);
59 /* Anything in the other one should fail. */
60 ok1(tdb_fetch(tdb
, key
, &d
) == TDB_ERR_LOCK
);
61 ok1(tap_log_messages
== 1);
62 ok1(tdb_store(tdb
, key
, data
, TDB_REPLACE
) == TDB_ERR_LOCK
);
63 ok1(tap_log_messages
== 2);
64 ok1(tdb_transaction_start(tdb
) == TDB_ERR_LOCK
);
65 ok1(tap_log_messages
== 3);
66 ok1(tdb_chainlock(tdb
, key
) == TDB_ERR_LOCK
);
67 ok1(tap_log_messages
== 4);
69 /* Transaciton should work as normal. */
70 ok1(tdb_store(tdb2
, key
, data
, TDB_REPLACE
) == TDB_SUCCESS
);
72 /* Now... try closing with locks held. */
73 ok1(tdb_close(tdb2
) == 0);
75 ok1(tdb_fetch(tdb
, key
, &d
) == TDB_SUCCESS
);
76 ok1(tdb_deq(d
, data
));
78 ok1(tdb_close(tdb
) == 0);
79 ok1(tap_log_messages
== 4);