Skip to content

Commit

Permalink
tdb: tdbdump should log errors, and fail in that case.
Browse files Browse the repository at this point in the history
Dumping a corrupt database should not exit silently with 0 status!

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell committed Oct 3, 2012
1 parent 90f463b commit ffde867
Showing 1 changed file with 38 additions and 2 deletions.
40 changes: 38 additions & 2 deletions lib/tdb/tools/tdbdump.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,55 @@ static int traverse_fn(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf, void *stat
return 0;
}

static void log_stderr(struct tdb_context *tdb, enum tdb_debug_level level,
const char *fmt, ...)
{
va_list ap;
char *ptr = NULL;
int debuglevel = 0;
int ret;
const char *name = tdb_name(tdb);
const char *prefix = "";

if (!name)
name = "unnamed";

switch (level) {
case TDB_DEBUG_ERROR:
prefix = "ERROR: ";
break;
case TDB_DEBUG_WARNING:
prefix = "WARNING: ";
break;
case TDB_DEBUG_TRACE:
return;

default:
case TDB_DEBUG_FATAL:
prefix = "FATAL: ";
break;
}

va_start(ap, fmt);
fprintf(stderr, "tdb(%s): %s", name, prefix);
vfprintf(stderr, fmt, ap);
va_end(ap);
}

static int dump_tdb(const char *fname, const char *keyname)
{
TDB_CONTEXT *tdb;
TDB_DATA key, value;
struct tdb_logging_context logfn = { log_stderr };

tdb = tdb_open(fname, 0, 0, O_RDONLY, 0);
tdb = tdb_open_ex(fname, 0, 0, O_RDONLY, 0, &logfn, NULL);
if (!tdb) {
printf("Failed to open %s\n", fname);
return 1;
}

if (!keyname) {
tdb_traverse(tdb, traverse_fn, NULL);
return tdb_traverse(tdb, traverse_fn, NULL) == -1 ? 1 : 0;
} else {
key.dptr = discard_const_p(uint8_t, keyname);
key.dsize = strlen(keyname);
Expand Down

0 comments on commit ffde867

Please sign in to comment.