3 #include "../private.h"
4 #include "tap-interface.h"
5 #include "external-agent.h"
7 #include "helpapi-external-agent.h"
11 static enum NTDB_ERROR clear_if_first(int fd, void *arg)
13 /* We hold a lock offset 4 always, so we can tell if anyone is holding it.
14 * (This is compatible with tdb's TDB_CLEAR_IF_FIRST flag). */
17 if (arg != clear_if_first)
18 return NTDB_ERR_CORRUPT;
21 fl.l_whence = SEEK_SET;
25 if (fcntl(fd, F_SETLK, &fl) == 0) {
26 /* We must be first ones to open it! */
27 diag("truncating file!");
28 if (ftruncate(fd, 0) != 0) {
33 if (fcntl(fd, F_SETLKW, &fl) != 0) {
39 int main(int argc, char *argv[])
42 struct ntdb_context *ntdb, *ntdb2;
44 union ntdb_attribute cif;
45 NTDB_DATA key = ntdb_mkdata(KEY_STR, strlen(KEY_STR));
46 int flags[] = { NTDB_DEFAULT, NTDB_NOMMAP,
47 NTDB_CONVERT, NTDB_NOMMAP|NTDB_CONVERT };
49 cif.openhook.base.attr = NTDB_ATTRIBUTE_OPENHOOK;
50 cif.openhook.base.next = &tap_log_attr;
51 cif.openhook.fn = clear_if_first;
52 cif.openhook.data = clear_if_first;
54 agent = prepare_external_agent();
55 plan_tests(sizeof(flags) / sizeof(flags[0]) * 16);
56 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
58 ntdb = ntdb_open("run-83-openhook.ntdb", flags[i]|MAYBE_NOSYNC,
59 O_RDWR|O_CREAT|O_TRUNC, 0600, NULL);
61 ok1(ntdb_store(ntdb, key, key, NTDB_REPLACE) == 0);
64 /* Now, open with CIF, should clear it. */
65 ntdb = ntdb_open("run-83-openhook.ntdb", flags[i]|MAYBE_NOSYNC,
68 ok1(!ntdb_exists(ntdb, key));
69 ok1(ntdb_store(ntdb, key, key, NTDB_REPLACE) == 0);
71 /* Agent should not clear it, since it's still open. */
72 ok1(external_agent_operation(agent, OPEN_WITH_HOOK,
73 "run-83-openhook.ntdb") == SUCCESS);
74 ok1(external_agent_operation(agent, FETCH, KEY_STR "=" KEY_STR)
76 ok1(external_agent_operation(agent, CLOSE, "") == SUCCESS);
78 /* Still exists for us too. */
79 ok1(ntdb_exists(ntdb, key));
81 /* Nested open should not erase db. */
82 ntdb2 = ntdb_open("run-83-openhook.ntdb", flags[i]|MAYBE_NOSYNC,
84 ok1(ntdb_exists(ntdb2, key));
85 ok1(ntdb_exists(ntdb, key));
88 ok1(ntdb_exists(ntdb, key));
90 /* Close it, now agent should clear it. */
93 ok1(external_agent_operation(agent, OPEN_WITH_HOOK,
94 "run-83-openhook.ntdb") == SUCCESS);
95 ok1(external_agent_operation(agent, FETCH, KEY_STR "=" KEY_STR)
97 ok1(external_agent_operation(agent, CLOSE, "") == SUCCESS);
99 ok1(tap_log_messages == 0);
102 free_external_agent(agent);
103 return exit_status();