4 #include "tap-interface.h"
5 #include "external-agent.h"
10 static enum NTDB_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 tdb's TDB_CLEAR_IF_FIRST flag). */
16 if (arg != clear_if_first)
17 return NTDB_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 ntdb_context *ntdb, *ntdb2;
43 union ntdb_attribute cif;
44 NTDB_DATA key = ntdb_mkdata(KEY_STR, strlen(KEY_STR));
45 int flags[] = { NTDB_DEFAULT, NTDB_NOMMAP,
46 NTDB_CONVERT, NTDB_NOMMAP|NTDB_CONVERT };
48 cif.openhook.base.attr = NTDB_ATTRIBUTE_OPENHOOK;
49 cif.openhook.base.next = &tap_log_attr;
50 cif.openhook.fn = clear_if_first;
51 cif.openhook.data = clear_if_first;
53 agent = prepare_external_agent();
54 plan_tests(sizeof(flags) / sizeof(flags[0]) * 16);
55 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
57 ntdb = ntdb_open("run-83-openhook.ntdb", flags[i]|MAYBE_NOSYNC,
58 O_RDWR|O_CREAT|O_TRUNC, 0600, NULL);
60 ok1(ntdb_store(ntdb, key, key, NTDB_REPLACE) == 0);
63 /* Now, open with CIF, should clear it. */
64 ntdb = ntdb_open("run-83-openhook.ntdb", flags[i]|MAYBE_NOSYNC,
67 ok1(!ntdb_exists(ntdb, key));
68 ok1(ntdb_store(ntdb, key, key, NTDB_REPLACE) == 0);
70 /* Agent should not clear it, since it's still open. */
71 ok1(external_agent_operation(agent, OPEN_WITH_HOOK,
72 "run-83-openhook.ntdb") == SUCCESS);
73 ok1(external_agent_operation(agent, FETCH, KEY_STR "=" KEY_STR)
75 ok1(external_agent_operation(agent, CLOSE, "") == SUCCESS);
77 /* Still exists for us too. */
78 ok1(ntdb_exists(ntdb, key));
80 /* Nested open should not erase db. */
81 ntdb2 = ntdb_open("run-83-openhook.ntdb", flags[i]|MAYBE_NOSYNC,
83 ok1(ntdb_exists(ntdb2, key));
84 ok1(ntdb_exists(ntdb, key));
87 ok1(ntdb_exists(ntdb, key));
89 /* Close it, now agent should clear it. */
92 ok1(external_agent_operation(agent, OPEN_WITH_HOOK,
93 "run-83-openhook.ntdb") == SUCCESS);
94 ok1(external_agent_operation(agent, FETCH, KEY_STR "=" KEY_STR)
96 ok1(external_agent_operation(agent, CLOSE, "") == SUCCESS);
98 ok1(tap_log_messages == 0);
101 free_external_agent(agent);
102 return exit_status();