Line data Source code
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"
15 : #include <stdlib.h>
16 : #include "logging.h"
17 :
18 : struct walk_data {
19 : TDB_DATA key;
20 : TDB_DATA data;
21 : bool fail;
22 : unsigned count;
23 : };
24 :
25 9441 : static inline bool tdb_deq(TDB_DATA a, TDB_DATA b)
26 : {
27 9441 : return a.dsize == b.dsize && memcmp(a.dptr, b.dptr, a.dsize) == 0;
28 : }
29 :
30 : static inline TDB_DATA tdb_mkdata(const void *p, size_t len)
31 : {
32 : TDB_DATA d;
33 : d.dptr = discard_const_p(uint8_t, p);
34 : d.dsize = len;
35 : return d;
36 : }
37 :
38 1 : static void walk(TDB_DATA key, TDB_DATA data, void *_wd)
39 : {
40 1 : struct walk_data *wd = _wd;
41 :
42 1 : if (!tdb_deq(key, wd->key)) {
43 0 : wd->fail = true;
44 : }
45 :
46 1 : if (!tdb_deq(data, wd->data)) {
47 0 : wd->fail = true;
48 : }
49 1 : wd->count++;
50 1 : }
51 :
52 5533 : static void count_records(TDB_DATA key, TDB_DATA data, void *_wd)
53 : {
54 5533 : struct walk_data *wd = _wd;
55 :
56 5533 : if (!tdb_deq(key, wd->key) || !tdb_deq(data, wd->data))
57 1632 : diag("%.*s::%.*s",
58 : (int)key.dsize, key.dptr, (int)data.dsize, data.dptr);
59 5533 : wd->count++;
60 5533 : }
61 :
62 0 : static void log_fn(struct tdb_context *tdb, enum tdb_debug_level level, const char *fmt, ...)
63 : {
64 0 : unsigned int *count = tdb_get_logging_private(tdb);
65 0 : (*count)++;
66 0 : }
67 :
68 1 : int main(int argc, char *argv[])
69 : {
70 : struct tdb_context *tdb;
71 : struct walk_data wd;
72 1 : unsigned int i, size, log_count = 0;
73 1 : struct tdb_logging_context log_ctx = { log_fn, &log_count };
74 :
75 : plan_tests(8);
76 1 : tdb = tdb_open_ex("run-rescue.tdb", 1, TDB_CLEAR_IF_FIRST,
77 : O_CREAT|O_TRUNC|O_RDWR, 0600, &log_ctx, NULL);
78 :
79 1 : wd.key.dsize = strlen("hi");
80 1 : wd.key.dptr = discard_const_p(uint8_t, "hi");
81 1 : wd.data.dsize = strlen("world");
82 1 : wd.data.dptr = discard_const_p(uint8_t, "world");
83 1 : wd.count = 0;
84 1 : wd.fail = false;
85 :
86 1 : ok1(tdb_store(tdb, wd.key, wd.data, TDB_INSERT) == 0);
87 :
88 1 : ok1(tdb_rescue(tdb, walk, &wd) == 0);
89 1 : ok1(!wd.fail);
90 1 : ok1(wd.count == 1);
91 :
92 : /* Corrupt the database, walk should either get it or not. */
93 1 : size = tdb->map_size;
94 3929 : for (i = sizeof(struct tdb_header); i < size; i++) {
95 : char c;
96 3928 : if (tdb->methods->tdb_read(tdb, i, &c, 1, false) != 0)
97 0 : fail("Reading offset %i", i);
98 3928 : if (tdb->methods->tdb_write(tdb, i, "X", 1) != 0)
99 0 : fail("Writing X at offset %i", i);
100 :
101 3928 : wd.count = 0;
102 3928 : if (tdb_rescue(tdb, count_records, &wd) != 0) {
103 0 : wd.fail = true;
104 0 : break;
105 : }
106 : /* Could be 0 or 1. */
107 3928 : if (wd.count > 1) {
108 0 : wd.fail = true;
109 0 : break;
110 : }
111 3928 : if (tdb->methods->tdb_write(tdb, i, &c, 1) != 0)
112 0 : fail("Restoring offset %i", i);
113 : }
114 1 : ok1(log_count == 0);
115 1 : ok1(!wd.fail);
116 1 : tdb_close(tdb);
117 :
118 : /* Now try our known-corrupt db. */
119 1 : tdb = tdb_open_ex("test/tdb.corrupt", 1024, 0, O_RDWR, 0,
120 : &taplogctx, NULL);
121 1 : wd.count = 0;
122 1 : ok1(tdb_rescue(tdb, count_records, &wd) == 0);
123 1 : ok1(wd.count == 1627);
124 1 : tdb_close(tdb);
125 :
126 1 : return exit_status();
127 : }
|