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 "../common/mutex.c"
14 #include "tap-interface.h"
25 static inline bool tdb_deq(TDB_DATA a
, TDB_DATA b
)
27 return a
.dsize
== b
.dsize
&& memcmp(a
.dptr
, b
.dptr
, a
.dsize
) == 0;
30 static inline TDB_DATA
tdb_mkdata(const void *p
, size_t len
)
33 d
.dptr
= discard_const_p(uint8_t, p
);
38 static void walk(TDB_DATA key
, TDB_DATA data
, void *_wd
)
40 struct walk_data
*wd
= _wd
;
42 if (!tdb_deq(key
, wd
->key
)) {
46 if (!tdb_deq(data
, wd
->data
)) {
52 static void count_records(TDB_DATA key
, TDB_DATA data
, void *_wd
)
54 struct walk_data
*wd
= _wd
;
56 if (!tdb_deq(key
, wd
->key
) || !tdb_deq(data
, wd
->data
))
58 (int)key
.dsize
, key
.dptr
, (int)data
.dsize
, data
.dptr
);
62 static void log_fn(struct tdb_context
*tdb
, enum tdb_debug_level level
, const char *fmt
, ...)
64 unsigned int *count
= tdb_get_logging_private(tdb
);
68 int main(int argc
, char *argv
[])
70 struct tdb_context
*tdb
;
72 unsigned int i
, size
, log_count
= 0;
73 struct tdb_logging_context log_ctx
= { log_fn
, &log_count
};
76 tdb
= tdb_open_ex("run-rescue.tdb", 1, TDB_CLEAR_IF_FIRST
,
77 O_CREAT
|O_TRUNC
|O_RDWR
, 0600, &log_ctx
, NULL
);
79 wd
.key
.dsize
= strlen("hi");
80 wd
.key
.dptr
= discard_const_p(uint8_t, "hi");
81 wd
.data
.dsize
= strlen("world");
82 wd
.data
.dptr
= discard_const_p(uint8_t, "world");
86 ok1(tdb_store(tdb
, wd
.key
, wd
.data
, TDB_INSERT
) == 0);
88 ok1(tdb_rescue(tdb
, walk
, &wd
) == 0);
92 /* Corrupt the database, walk should either get it or not. */
94 for (i
= sizeof(struct tdb_header
); i
< size
; i
++) {
96 if (tdb
->methods
->tdb_read(tdb
, i
, &c
, 1, false) != 0)
97 fail("Reading offset %i", i
);
98 if (tdb
->methods
->tdb_write(tdb
, i
, "X", 1) != 0)
99 fail("Writing X at offset %i", i
);
102 if (tdb_rescue(tdb
, count_records
, &wd
) != 0) {
106 /* Could be 0 or 1. */
111 if (tdb
->methods
->tdb_write(tdb
, i
, &c
, 1) != 0)
112 fail("Restoring offset %i", i
);
118 /* Now try our known-corrupt db. */
119 tdb
= tdb_open_ex("test/tdb.corrupt", 1024, 0, O_RDWR
, 0,
122 ok1(tdb_rescue(tdb
, count_records
, &wd
) == 0);
123 ok1(wd
.count
== 1627);
126 return exit_status();