3 #include "tap-interface.h"
11 #include "external-agent.h"
16 static enum NTDB_ERROR
clear_if_first(int fd
, void *arg
)
18 /* We hold a lock offset 4 always, so we can tell if anyone is holding it.
19 * (This is compatible with tdb's TDB_CLEAR_IF_FIRST flag). */
22 if (arg
!= clear_if_first
)
23 return NTDB_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 ntdb_context
*ntdb
;
49 union ntdb_attribute cif
;
50 NTDB_DATA key
= ntdb_mkdata(KEY_STR
, strlen(KEY_STR
));
51 int flags
[] = { NTDB_DEFAULT
, NTDB_NOMMAP
,
52 NTDB_CONVERT
, NTDB_NOMMAP
|NTDB_CONVERT
};
54 cif
.openhook
.base
.attr
= NTDB_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 ntdb
= ntdb_open("run-83-openhook.ntdb", flags
[i
]|MAYBE_NOSYNC
,
64 O_RDWR
|O_CREAT
|O_TRUNC
, 0600, NULL
);
66 ok1(ntdb_store(ntdb
, key
, key
, NTDB_REPLACE
) == 0);
69 /* Now, open with CIF, should clear it. */
70 ntdb
= ntdb_open("run-83-openhook.ntdb", flags
[i
]|MAYBE_NOSYNC
,
73 ok1(!ntdb_exists(ntdb
, key
));
74 ok1(ntdb_store(ntdb
, key
, key
, NTDB_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.ntdb") == SUCCESS
);
79 ok1(external_agent_operation(agent
, FETCH
, KEY_STR
"=" KEY_STR
)
81 ok1(external_agent_operation(agent
, CLOSE
, "") == SUCCESS
);
83 /* Still exists for us too. */
84 ok1(ntdb_exists(ntdb
, key
));
86 /* Close it, now agent should clear it. */
89 ok1(external_agent_operation(agent
, OPEN_WITH_HOOK
,
90 "run-83-openhook.ntdb") == SUCCESS
);
91 ok1(external_agent_operation(agent
, FETCH
, KEY_STR
"=" KEY_STR
)
93 ok1(external_agent_operation(agent
, CLOSE
, "") == SUCCESS
);
95 ok1(tap_log_messages
== 0);
98 free_external_agent(agent
);