1 #include "tdb2-source.h"
2 #include <ccan/tap/tap.h>
7 #include "external-agent.h"
10 static enum TDB_ERROR clear_if_first(int fd, void *arg)
12 /* We hold a lock offset 4 always, so we can tell if anyone is holding it.
13 * (This is compatible with tdb1's TDB_CLEAR_IF_FIRST flag). */
16 if (arg != clear_if_first)
17 return TDB_ERR_CORRUPT;
20 fl.l_whence = SEEK_SET;
24 if (fcntl(fd, F_SETLK, &fl) == 0) {
25 /* We must be first ones to open it! */
26 diag("truncating file!");
27 if (ftruncate(fd, 0) != 0) {
32 if (fcntl(fd, F_SETLKW, &fl) != 0) {
38 int main(int argc, char *argv[])
41 struct tdb_context *tdb;
43 union tdb_attribute cif;
44 struct tdb_data key = tdb_mkdata("key", 3);
45 int flags[] = { TDB_DEFAULT, TDB_NOMMAP,
46 TDB_CONVERT, TDB_NOMMAP|TDB_CONVERT,
47 TDB_VERSION1, TDB_NOMMAP|TDB_VERSION1,
48 TDB_CONVERT|TDB_VERSION1,
49 TDB_NOMMAP|TDB_CONVERT|TDB_VERSION1 };
51 cif.openhook.base.attr = TDB_ATTRIBUTE_OPENHOOK;
52 cif.openhook.base.next = &tap_log_attr;
53 cif.openhook.fn = clear_if_first;
54 cif.openhook.data = clear_if_first;
56 agent = prepare_external_agent();
57 plan_tests(sizeof(flags) / sizeof(flags[0]) * 13);
58 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
60 tdb = tdb_open("run-83-openhook.tdb", flags[i],
61 O_RDWR|O_CREAT|O_TRUNC, 0600, NULL);
63 ok1(tdb_store(tdb, key, key, TDB_REPLACE) == 0);
66 /* Now, open with CIF, should clear it. */
67 tdb = tdb_open("run-83-openhook.tdb", flags[i],
70 ok1(!tdb_exists(tdb, key));
71 ok1(tdb_store(tdb, key, key, TDB_REPLACE) == 0);
73 /* Agent should not clear it, since it's still open. */
74 ok1(external_agent_operation(agent, OPEN_WITH_HOOK,
75 "run-83-openhook.tdb") == SUCCESS);
76 ok1(external_agent_operation(agent, FETCH, "key") == SUCCESS);
77 ok1(external_agent_operation(agent, CLOSE, "") == SUCCESS);
79 /* Still exists for us too. */
80 ok1(tdb_exists(tdb, key));
82 /* Close it, now agent should clear it. */
85 ok1(external_agent_operation(agent, OPEN_WITH_HOOK,
86 "run-83-openhook.tdb") == SUCCESS);
87 ok1(external_agent_operation(agent, FETCH, "key") == FAILED);
88 ok1(external_agent_operation(agent, CLOSE, "") == SUCCESS);
90 ok1(tap_log_messages == 0);
93 free_external_agent(agent);