Commit ef92843f authored by Rusty Russell's avatar Rusty Russell

tdb2: unify tdb1_check and tdb1_summary into tdb_check and tdb_summary.

Switch on the TDB_VERSION1 flag.  Also, change tdb1_check's checkfn argument
to return an error code (and set tdb->last_error accordingly).
parent 6bc8ea01
......@@ -782,6 +782,12 @@ enum TDB_ERROR tdb_check_(struct tdb_context *tdb,
uint64_t features;
enum TDB_ERROR ecode;
if (tdb->flags & TDB_VERSION1) {
if (tdb1_check(tdb, check, data) == -1)
return tdb->last_error;
return TDB_SUCCESS;
}
ecode = tdb_allrecord_lock(tdb, F_RDLCK, TDB_LOCK_WAIT, false);
if (ecode != TDB_SUCCESS) {
return tdb->last_error = ecode;
......
......@@ -631,6 +631,12 @@ struct tdb_context {
#define TDB1_BYTEREV(x) (((((x)&0xff)<<24)|((x)&0xFF00)<<8)|(((x)>>8)&0xFF00)|((x)>>24))
/* tdb1_check.c: */
int tdb1_check(struct tdb_context *tdb,
enum TDB_ERROR (*check)(TDB_DATA key, TDB_DATA data, void *),
void *private_data);
/* tdb1_open.c: */
int tdb1_new_database(struct tdb_context *tdb,
struct tdb_attribute_tdb1_hashsize *hashsize);
......@@ -658,6 +664,9 @@ int tdb1_traverse(struct tdb_context *tdb,
int (*)(struct tdb_context *, TDB_DATA, TDB_DATA, void *),
void *private_data);
/* tdb1_summary.c: */
char *tdb1_summary(struct tdb_context *tdb);
/* tdb1_tdb.c: */
int tdb1_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, int flag);
enum TDB_ERROR tdb1_fetch(struct tdb_context *tdb, TDB_DATA key,
......
......@@ -161,6 +161,14 @@ enum TDB_ERROR tdb_summary(struct tdb_context *tdb,
char *hashesg, *freeg, *keysg, *datag, *extrag, *uncoalg;
enum TDB_ERROR ecode;
if (tdb->flags & TDB_VERSION1) {
/* tdb1 doesn't do graphs. */
*summary = tdb1_summary(tdb);
if (!*summary)
return tdb->last_error;
return TDB_SUCCESS;
}
hashesg = freeg = keysg = datag = extrag = uncoalg = NULL;
ecode = tdb_allrecord_lock(tdb, F_RDLCK, TDB_LOCK_WAIT, false);
......
......@@ -48,19 +48,12 @@ void tdb1_increment_seqnum_nonblock(struct tdb_context *tdb);
uint64_t tdb1_incompatible_hash(const void *key, size_t len, uint64_t seed, void *);
int tdb1_check(struct tdb_context *tdb,
int (*check) (TDB_DATA key, TDB_DATA data, void *private_data),
void *private_data);
/* @} ******************************************************************/
/* wipe and repack */
int tdb1_wipe_all(struct tdb_context *tdb);
int tdb1_repack(struct tdb_context *tdb);
/* Debug functions. Not used in production. */
char *tdb1_summary(struct tdb_context *tdb);
extern TDB_DATA tdb1_null;
#endif /* tdb1.h */
......@@ -236,7 +236,8 @@ static bool tdb1_check_used_record(struct tdb_context *tdb,
tdb1_off_t off,
const struct tdb1_record *rec,
unsigned char **hashes,
int (*check)(TDB_DATA, TDB_DATA, void *),
enum TDB_ERROR (*check)(TDB_DATA, TDB_DATA,
void *),
void *private_data)
{
TDB_DATA key, data;
......@@ -270,13 +271,18 @@ static bool tdb1_check_used_record(struct tdb_context *tdb,
/* If they supply a check function and this record isn't dead,
get data and feed it. */
if (check && rec->magic != TDB1_DEAD_MAGIC) {
enum TDB_ERROR ecode;
data = get_bytes(tdb, off + sizeof(*rec) + rec->key_len,
rec->data_len);
if (!data.dptr)
goto fail_put_key;
if (check(key, data, private_data) == -1)
ecode = check(key, data, private_data);
if (ecode != TDB_SUCCESS) {
tdb->last_error = ecode;
goto fail_put_data;
}
put_bytes(tdb, data);
}
......@@ -323,8 +329,8 @@ size_t tdb1_dead_space(struct tdb_context *tdb, tdb1_off_t off)
}
int tdb1_check(struct tdb_context *tdb,
int (*check)(TDB_DATA key, TDB_DATA data, void *private_data),
void *private_data)
enum TDB_ERROR (*check)(TDB_DATA key, TDB_DATA data, void *),
void *private_data)
{
unsigned int h;
unsigned char **hashes;
......
......@@ -19,7 +19,7 @@ int main(int argc, char *argv[])
O_CREAT|O_TRUNC|O_RDWR, 0600, &hsize);
ok1(tdb);
ok1(tdb1_check(tdb, NULL, NULL) == 0);
ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
key.dsize = strlen("hi");
key.dptr = (void *)"hi";
......@@ -27,18 +27,18 @@ int main(int argc, char *argv[])
data.dptr = (void *)"world";
ok1(tdb_store(tdb, key, data, TDB_INSERT) == TDB_SUCCESS);
ok1(tdb1_check(tdb, NULL, NULL) == 0);
ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
tdb_close(tdb);
tdb = tdb_open("run-check.tdb1", TDB_VERSION1, O_RDWR, 0, &tap_log_attr);
ok1(tdb);
ok1(tdb1_check(tdb, NULL, NULL) == 0);
ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
tdb_close(tdb);
tdb = tdb_open("test/tdb1.corrupt", TDB_VERSION1, O_RDWR, 0,
&tap_log_attr);
ok1(tdb);
ok1(tdb1_check(tdb, NULL, NULL) == -1);
ok1(tdb_check(tdb, NULL, NULL) == TDB_ERR_CORRUPT);
ok1(tdb_error(tdb) == TDB_ERR_CORRUPT);
tdb_close(tdb);
......@@ -46,13 +46,13 @@ int main(int argc, char *argv[])
tdb = tdb_open("test/old-nohash-le.tdb1", TDB_VERSION1, O_RDWR, 0,
&tap_log_attr);
ok1(tdb);
ok1(tdb1_check(tdb, NULL, NULL) == 0);
ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
tdb_close(tdb);
tdb = tdb_open("test/old-nohash-be.tdb1", TDB_VERSION1, O_RDWR, 0,
&tap_log_attr);
ok1(tdb);
ok1(tdb1_check(tdb, NULL, NULL) == 0);
ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
tdb_close(tdb);
return exit_status();
......
......@@ -45,7 +45,7 @@ static void check_test(struct tdb_context *tdb)
TDB_DATA key, data;
unsigned int i, verifiable, corrupt, sizes[2], dsize, ksize;
ok1(tdb1_check(tdb, NULL, NULL) == 0);
ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
key.dptr = (void *)"hello";
data.dsize = strlen("world");
......@@ -81,7 +81,7 @@ static void check_test(struct tdb_context *tdb)
for (i = 0, corrupt = 0; i < tdb->file->map_size * CHAR_BIT; i++) {
tdb1_flip_bit(tdb, i);
memset(sizes, 0, sizeof(sizes));
if (tdb1_check(tdb, check, sizes) != 0)
if (tdb_check(tdb, check, sizes) == TDB_ERR_CORRUPT)
corrupt++;
else if (sizes[0] != ksize || sizes[1] != dsize)
corrupt++;
......
......@@ -105,7 +105,7 @@ int main(int argc, char *argv[])
ok1(tdb_fetch(tdb, d, &d) == TDB_SUCCESS);
ok1(d.dsize == 5);
free(d.dptr);
ok1(tdb1_check(tdb, NULL, NULL) == 0);
ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
tdb_close(tdb);
log_count = 0;
......@@ -113,7 +113,7 @@ int main(int argc, char *argv[])
TDB_VERSION1, O_RDONLY, 0, &jhash_attr);
ok1(tdb);
ok1(log_count == 0);
ok1(tdb1_check(tdb, NULL, NULL) == 0);
ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
tdb_close(tdb);
log_count = 0;
......@@ -121,7 +121,7 @@ int main(int argc, char *argv[])
TDB_VERSION1, O_RDONLY, 0, &jhash_attr);
ok1(tdb);
ok1(log_count == 0);
ok1(tdb1_check(tdb, NULL, NULL) == 0);
ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
tdb_close(tdb);
/* OK, now create with incompatible hash. */
......@@ -156,7 +156,7 @@ int main(int argc, char *argv[])
ok1(tdb_fetch(tdb, d, &d) == TDB_SUCCESS);
ok1(d.dsize == 5);
free(d.dptr);
ok1(tdb1_check(tdb, NULL, NULL) == 0);
ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
tdb_close(tdb);
/* Can open by letting it figure it out itself. */
......@@ -170,7 +170,7 @@ int main(int argc, char *argv[])
ok1(tdb_fetch(tdb, d, &d) == TDB_SUCCESS);
ok1(d.dsize == 5);
free(d.dptr);
ok1(tdb1_check(tdb, NULL, NULL) == 0);
ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
tdb_close(tdb);
/* FIXME: Not possible with TDB2 :( */
......@@ -205,7 +205,7 @@ int main(int argc, char *argv[])
ok1(tdb_fetch(tdb, d, &d) == TDB_SUCCESS);
ok1(d.dsize == 5);
free(d.dptr);
ok1(tdb1_check(tdb, NULL, NULL) == 0);
ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
tdb_close(tdb);
}
......
......@@ -20,25 +20,25 @@ int main(int argc, char *argv[])
tdb = tdb_open("test/old-nohash-le.tdb1", TDB_VERSION1, O_RDWR, 0,
&tap_log_attr);
ok1(tdb);
ok1(tdb1_check(tdb, NULL, NULL) == 0);
ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
tdb_close(tdb);
tdb = tdb_open("test/old-nohash-be.tdb1", TDB_VERSION1, O_RDWR, 0,
&tap_log_attr);
ok1(tdb);
ok1(tdb1_check(tdb, NULL, NULL) == 0);
ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
tdb_close(tdb);
tdb = tdb_open("test/old-nohash-le.tdb1", TDB_VERSION1, O_RDWR, 0,
&incompat_hash_attr);
ok1(tdb);
ok1(tdb1_check(tdb, NULL, NULL) == 0);
ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
tdb_close(tdb);
tdb = tdb_open("test/old-nohash-be.tdb1", TDB_VERSION1, O_RDWR, 0,
&incompat_hash_attr);
ok1(tdb);
ok1(tdb1_check(tdb, NULL, NULL) == 0);
ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
tdb_close(tdb);
return exit_status();
......
/* We should be able to tdb1_check a O_RDONLY tdb, and we were previously allowed
* to tdb1_check() inside a transaction (though that's paranoia!). */
/* We should be able to tdb_check a O_RDONLY tdb, and we were previously allowed
* to tdb_check() inside a transaction (though that's paranoia!). */
#include "tdb2-source.h"
#include <ccan/tap/tap.h>
#include <stdlib.h>
......@@ -28,11 +28,11 @@ int main(int argc, char *argv[])
data.dptr = (void *)"world";
ok1(tdb_store(tdb, key, data, TDB_INSERT) == TDB_SUCCESS);
ok1(tdb1_check(tdb, NULL, NULL) == 0);
ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
/* We are also allowed to do a check inside a transaction. */
ok1(tdb_transaction_start(tdb) == TDB_SUCCESS);
ok1(tdb1_check(tdb, NULL, NULL) == 0);
ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
ok1(tdb_close(tdb) == 0);
tdb = tdb_open("run-readonly-check.tdb1",
......@@ -40,7 +40,7 @@ int main(int argc, char *argv[])
ok1(tdb);
ok1(tdb_store(tdb, key, data, TDB_MODIFY) == TDB_ERR_RDONLY);
ok1(tdb1_check(tdb, NULL, NULL) == 0);
ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
ok1(tdb_close(tdb) == 0);
return exit_status();
......
......@@ -102,7 +102,7 @@ int main(int argc, char *argv[])
0, &incompat_hash_attr);
ok1(tdb);
ok1(log_count == 0);
ok1(tdb1_check(tdb, NULL, NULL) == 0);
ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
tdb_close(tdb);
log_count = 0;
......@@ -110,7 +110,7 @@ int main(int argc, char *argv[])
0, &incompat_hash_attr);
ok1(tdb);
ok1(log_count == 0);
ok1(tdb1_check(tdb, NULL, NULL) == 0);
ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
tdb_close(tdb);
/* It should open with jenkins hash if we don't specify. */
......@@ -119,7 +119,7 @@ int main(int argc, char *argv[])
&log_attr);
ok1(tdb);
ok1(log_count == 0);
ok1(tdb1_check(tdb, NULL, NULL) == 0);
ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
tdb_close(tdb);
log_count = 0;
......@@ -127,7 +127,7 @@ int main(int argc, char *argv[])
&log_attr);
ok1(tdb);
ok1(log_count == 0);
ok1(tdb1_check(tdb, NULL, NULL) == 0);
ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
tdb_close(tdb);
log_count = 0;
......@@ -135,7 +135,7 @@ int main(int argc, char *argv[])
0, &log_attr);
ok1(tdb);
ok1(log_count == 0);
ok1(tdb1_check(tdb, NULL, NULL) == 0);
ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
tdb_close(tdb);
......
......@@ -74,7 +74,7 @@ static enum agent_return do_operation(enum operation op, const char *name)
ret = tdb_transaction_commit(tdb) == TDB_SUCCESS ? SUCCESS : OTHER_FAILURE;
break;
case CHECK:
ret = tdb1_check(tdb, NULL, NULL) == 0 ? SUCCESS : OTHER_FAILURE;
ret = tdb_check(tdb, NULL, NULL) == TDB_SUCCESS ? SUCCESS : OTHER_FAILURE;
break;
case NEEDS_RECOVERY:
ret = tdb1_needs_recovery(tdb) ? SUCCESS : FAILED;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment