1 #include "../common/tdb_private.h"
2 #include "../common/io.c"
3 #include "../common/tdb.c"
4 #include "../common/lock.c"
5 #include "../common/freelist.c"
6 #include "../common/traverse.c"
7 #include "../common/transaction.c"
8 #include "../common/error.c"
9 #include "../common/open.c"
10 #include "../common/check.c"
11 #include "../common/hash.c"
12 #include "../common/rescue.c"
13 #include "tap-interface.h"
24 static inline bool tdb_deq(TDB_DATA a
, TDB_DATA b
)
26 return a
.dsize
== b
.dsize
&& memcmp(a
.dptr
, b
.dptr
, a
.dsize
) == 0;
29 static inline TDB_DATA
tdb_mkdata(const void *p
, size_t len
)
37 static void walk(TDB_DATA key
, TDB_DATA data
, void *_wd
)
39 struct walk_data
*wd
= _wd
;
41 if (!tdb_deq(key
, wd
->key
)) {
45 if (!tdb_deq(data
, wd
->data
)) {
51 static void count_records(TDB_DATA key
, TDB_DATA data
, void *_wd
)
53 struct walk_data
*wd
= _wd
;
55 if (!tdb_deq(key
, wd
->key
) || !tdb_deq(data
, wd
->data
))
57 (int)key
.dsize
, key
.dptr
, (int)data
.dsize
, data
.dptr
);
61 static void log_fn(struct tdb_context
*tdb
, enum tdb_debug_level level
, const char *fmt
, ...)
63 unsigned int *count
= tdb_get_logging_private(tdb
);
67 int main(int argc
, char *argv
[])
69 struct tdb_context
*tdb
;
71 unsigned int i
, size
, log_count
= 0;
72 struct tdb_logging_context log_ctx
= { log_fn
, &log_count
};
75 tdb
= tdb_open_ex("run-rescue.tdb", 1, TDB_CLEAR_IF_FIRST
,
76 O_CREAT
|O_TRUNC
|O_RDWR
, 0600, &log_ctx
, NULL
);
78 wd
.key
.dsize
= strlen("hi");
79 wd
.key
.dptr
= (void *)"hi";
80 wd
.data
.dsize
= strlen("world");
81 wd
.data
.dptr
= (void *)"world";
85 ok1(tdb_store(tdb
, wd
.key
, wd
.data
, TDB_INSERT
) == 0);
87 ok1(tdb_rescue(tdb
, walk
, &wd
) == 0);
91 /* Corrupt the database, walk should either get it or not. */
93 for (i
= sizeof(struct tdb_header
); i
< size
; i
++) {
95 if (tdb
->methods
->tdb_read(tdb
, i
, &c
, 1, false) != 0)
96 fail("Reading offset %i", i
);
97 if (tdb
->methods
->tdb_write(tdb
, i
, "X", 1) != 0)
98 fail("Writing X at offset %i", i
);
101 if (tdb_rescue(tdb
, count_records
, &wd
) != 0) {
105 /* Could be 0 or 1. */
110 if (tdb
->methods
->tdb_write(tdb
, i
, &c
, 1) != 0)
111 fail("Restoring offset %i", i
);
117 /* Now try our known-corrupt db. */
118 tdb
= tdb_open_ex("test/tdb.corrupt", 1024, 0, O_RDWR
, 0,
121 ok1(tdb_rescue(tdb
, count_records
, &wd
) == 0);
122 ok1(wd
.count
== 1627);
125 return exit_status();