3 #include "tap-interface.h"
11 #include "external-agent.h"
14 static enum TDB_ERROR
clear_if_first(int fd
, void *arg
)
16 /* We hold a lock offset 4 always, so we can tell if anyone is holding it.
17 * (This is compatible with tdb1's TDB_CLEAR_IF_FIRST flag). */
20 if (arg
!= clear_if_first
)
21 return TDB_ERR_CORRUPT
;
24 fl
.l_whence
= SEEK_SET
;
28 if (fcntl(fd
, F_SETLK
, &fl
) == 0) {
29 /* We must be first ones to open it! */
30 diag("truncating file!");
31 if (ftruncate(fd
, 0) != 0) {
36 if (fcntl(fd
, F_SETLKW
, &fl
) != 0) {
42 int main(int argc
, char *argv
[])
45 struct tdb_context
*tdb
;
47 union tdb_attribute cif
;
48 struct tdb_data key
= tdb_mkdata("key", 3);
49 int flags
[] = { TDB_DEFAULT
, TDB_NOMMAP
,
50 TDB_CONVERT
, TDB_NOMMAP
|TDB_CONVERT
,
51 TDB_VERSION1
, TDB_NOMMAP
|TDB_VERSION1
,
52 TDB_CONVERT
|TDB_VERSION1
,
53 TDB_NOMMAP
|TDB_CONVERT
|TDB_VERSION1
};
55 cif
.openhook
.base
.attr
= TDB_ATTRIBUTE_OPENHOOK
;
56 cif
.openhook
.base
.next
= &tap_log_attr
;
57 cif
.openhook
.fn
= clear_if_first
;
58 cif
.openhook
.data
= clear_if_first
;
60 agent
= prepare_external_agent();
61 plan_tests(sizeof(flags
) / sizeof(flags
[0]) * 13);
62 for (i
= 0; i
< sizeof(flags
) / sizeof(flags
[0]); i
++) {
64 tdb
= tdb_open("run-83-openhook.tdb", flags
[i
],
65 O_RDWR
|O_CREAT
|O_TRUNC
, 0600, NULL
);
67 ok1(tdb_store(tdb
, key
, key
, TDB_REPLACE
) == 0);
70 /* Now, open with CIF, should clear it. */
71 tdb
= tdb_open("run-83-openhook.tdb", flags
[i
],
74 ok1(!tdb_exists(tdb
, key
));
75 ok1(tdb_store(tdb
, key
, key
, TDB_REPLACE
) == 0);
77 /* Agent should not clear it, since it's still open. */
78 ok1(external_agent_operation(agent
, OPEN_WITH_HOOK
,
79 "run-83-openhook.tdb") == SUCCESS
);
80 ok1(external_agent_operation(agent
, FETCH
, "key") == SUCCESS
);
81 ok1(external_agent_operation(agent
, CLOSE
, "") == SUCCESS
);
83 /* Still exists for us too. */
84 ok1(tdb_exists(tdb
, key
));
86 /* Close it, now agent should clear it. */
89 ok1(external_agent_operation(agent
, OPEN_WITH_HOOK
,
90 "run-83-openhook.tdb") == SUCCESS
);
91 ok1(external_agent_operation(agent
, FETCH
, "key") == FAILED
);
92 ok1(external_agent_operation(agent
, CLOSE
, "") == SUCCESS
);
94 ok1(tap_log_messages
== 0);
97 free_external_agent(agent
);