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 63 always, so we can tell if anyone is holding it. */
15 if (arg != clear_if_first)
16 return TDB_ERR_CORRUPT;
19 fl.l_whence = SEEK_SET;
23 if (fcntl(fd, F_SETLK, &fl) == 0) {
24 /* We must be first ones to open it! */
25 diag("truncating file!");
26 if (ftruncate(fd, 0) != 0) {
31 if (fcntl(fd, F_SETLKW, &fl) != 0) {
37 int main(int argc, char *argv[])
40 struct tdb_context *tdb;
42 union tdb_attribute cif;
43 struct tdb_data key = tdb_mkdata("key", 3);
44 int flags[] = { TDB_DEFAULT, TDB_NOMMAP,
45 TDB_CONVERT, TDB_NOMMAP|TDB_CONVERT };
47 cif.openhook.base.attr = TDB_ATTRIBUTE_OPENHOOK;
48 cif.openhook.base.next = &tap_log_attr;
49 cif.openhook.fn = clear_if_first;
50 cif.openhook.data = clear_if_first;
52 agent = prepare_external_agent();
53 plan_tests(sizeof(flags) / sizeof(flags[0]) * 13);
54 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
56 tdb = tdb_open("run-83-openhook.tdb", flags[i],
57 O_RDWR|O_CREAT|O_TRUNC, 0600, NULL);
59 ok1(tdb_store(tdb, key, key, TDB_REPLACE) == 0);
62 /* Now, open with CIF, should clear it. */
63 tdb = tdb_open("run-83-openhook.tdb", flags[i],
66 ok1(!tdb_exists(tdb, key));
67 ok1(tdb_store(tdb, key, key, TDB_REPLACE) == 0);
69 /* Agent should not clear it, since it's still open. */
70 ok1(external_agent_operation(agent, OPEN_WITH_HOOK,
71 "run-83-openhook.tdb") == SUCCESS);
72 ok1(external_agent_operation(agent, FETCH, "key") == SUCCESS);
73 ok1(external_agent_operation(agent, CLOSE, "") == SUCCESS);
75 /* Still exists for us too. */
76 ok1(tdb_exists(tdb, key));
78 /* Close it, now agent should clear it. */
81 ok1(external_agent_operation(agent, OPEN_WITH_HOOK,
82 "run-83-openhook.tdb") == SUCCESS);
83 ok1(external_agent_operation(agent, FETCH, "key") == FAILED);
84 ok1(external_agent_operation(agent, CLOSE, "") == SUCCESS);
86 ok1(tap_log_messages == 0);
89 free_external_agent(agent);