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>
14 #include "external-agent.h"
17 static enum TDB_ERROR clear_if_first(int fd, void *arg)
19 /* We hold a lock offset 63 always, so we can tell if anyone is holding it. */
22 if (arg != clear_if_first)
23 return TDB_ERR_CORRUPT;
26 fl.l_whence = SEEK_SET;
30 if (fcntl(fd, F_SETLK, &fl) == 0) {
31 /* We must be first ones to open it! */
32 diag("truncating file!");
33 if (ftruncate(fd, 0) != 0) {
38 if (fcntl(fd, F_SETLKW, &fl) != 0) {
44 int main(int argc, char *argv[])
47 struct tdb_context *tdb;
49 union tdb_attribute cif;
50 struct tdb_data key = tdb_mkdata("key", 3);
51 int flags[] = { TDB_DEFAULT, TDB_NOMMAP,
52 TDB_CONVERT, TDB_NOMMAP|TDB_CONVERT };
54 cif.openhook.base.attr = TDB_ATTRIBUTE_OPENHOOK;
55 cif.openhook.base.next = &tap_log_attr;
56 cif.openhook.fn = clear_if_first;
57 cif.openhook.data = clear_if_first;
59 agent = prepare_external_agent();
60 plan_tests(sizeof(flags) / sizeof(flags[0]) * 13);
61 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
63 tdb = tdb_open("run-83-openhook.tdb", flags[i],
64 O_RDWR|O_CREAT|O_TRUNC, 0600, NULL);
66 ok1(tdb_store(tdb, key, key, TDB_REPLACE) == 0);
69 /* Now, open with CIF, should clear it. */
70 tdb = tdb_open("run-83-openhook.tdb", flags[i],
73 ok1(!tdb_exists(tdb, key));
74 ok1(tdb_store(tdb, key, key, TDB_REPLACE) == 0);
76 /* Agent should not clear it, since it's still open. */
77 ok1(external_agent_operation(agent, OPEN_WITH_HOOK,
78 "run-83-openhook.tdb") == SUCCESS);
79 ok1(external_agent_operation(agent, FETCH, "key") == SUCCESS);
80 ok1(external_agent_operation(agent, CLOSE, "") == SUCCESS);
82 /* Still exists for us too. */
83 ok1(tdb_exists(tdb, key));
85 /* Close it, now agent should clear it. */
88 ok1(external_agent_operation(agent, OPEN_WITH_HOOK,
89 "run-83-openhook.tdb") == SUCCESS);
90 ok1(external_agent_operation(agent, FETCH, "key") == FAILED);
91 ok1(external_agent_operation(agent, CLOSE, "") == SUCCESS);
93 ok1(tap_log_messages == 0);
96 free_external_agent(agent);