From: Rusty Russell Date: Tue, 14 Sep 2010 01:08:30 +0000 (+0930) Subject: tdb2: add tdb_errorstr(), clean up error codes. X-Git-Url: http://git.ozlabs.org/?p=ccan;a=commitdiff_plain;h=b846677c1ec77e986c7d6b7c913aa2a4b0c82d4e tdb2: add tdb_errorstr(), clean up error codes. Also remove TDB_DISALLOW_NESTING flag, since that's the default now. --- diff --git a/ccan/tdb2/tdb.c b/ccan/tdb2/tdb.c index 7dc5aafb..659d39b2 100644 --- a/ccan/tdb2/tdb.c +++ b/ccan/tdb2/tdb.c @@ -659,3 +659,21 @@ enum TDB_ERROR tdb_error(struct tdb_context *tdb) { return tdb->ecode; } + +const char *tdb_errorstr(struct tdb_context *tdb) +{ + /* Gcc warns if you miss a case in the switch, so use that. */ + switch (tdb->ecode) { + case TDB_SUCCESS: return "Success"; + case TDB_ERR_CORRUPT: return "Corrupt database"; + case TDB_ERR_IO: return "IO Error"; + case TDB_ERR_LOCK: return "Locking error"; + case TDB_ERR_OOM: return "Out of memory"; + case TDB_ERR_EXISTS: return "Record exists"; + case TDB_ERR_NESTING: return "Transaction already started"; + case TDB_ERR_EINVAL: return "Invalid parameter"; + case TDB_ERR_NOEXIST: return "Record does not exist"; + case TDB_ERR_RDONLY: return "write not permitted"; + } + return "Invalid error code"; +} diff --git a/ccan/tdb2/tdb2.h b/ccan/tdb2/tdb2.h index fa0b6ce7..98831b4a 100644 --- a/ccan/tdb2/tdb2.h +++ b/ccan/tdb2/tdb2.h @@ -57,13 +57,11 @@ extern "C" { #define TDB_SEQNUM 128 /* maintain a sequence number */ #define TDB_VOLATILE 256 /* Activate the per-hashchain freelist, default 5 */ #define TDB_ALLOW_NESTING 512 /* Allow transactions to nest */ -#define TDB_DISALLOW_NESTING 1024 /* Disallow transactions to nest */ /* error codes */ enum TDB_ERROR {TDB_SUCCESS=0, TDB_ERR_CORRUPT, TDB_ERR_IO, TDB_ERR_LOCK, - TDB_ERR_OOM, TDB_ERR_EXISTS, TDB_ERR_NOLOCK, TDB_ERR_LOCK_TIMEOUT, - TDB_ERR_NOEXIST, TDB_ERR_EINVAL, TDB_ERR_RDONLY, - TDB_ERR_NESTING}; + TDB_ERR_OOM, TDB_ERR_EXISTS, TDB_ERR_NOEXIST, + TDB_ERR_EINVAL, TDB_ERR_RDONLY, TDB_ERR_NESTING }; /* debugging uses one of the following levels */ enum tdb_debug_level {TDB_DEBUG_FATAL = 0, TDB_DEBUG_ERROR, @@ -141,6 +139,7 @@ int tdb_check(struct tdb_context *tdb, void *private_data); enum TDB_ERROR tdb_error(struct tdb_context *tdb); +const char *tdb_errorstr(struct tdb_context *tdb); extern struct tdb_data tdb_null; diff --git a/ccan/tdb2/test/run-tdb_errorstr.c b/ccan/tdb2/test/run-tdb_errorstr.c new file mode 100644 index 00000000..dd8e7b0c --- /dev/null +++ b/ccan/tdb2/test/run-tdb_errorstr.c @@ -0,0 +1,67 @@ +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char *argv[]) +{ + struct tdb_context *tdb; + + plan_tests(1 + TDB_ERR_NESTING + 2); + tdb = tdb_open("run-tdb_errorstr.tdb", TDB_DEFAULT, + O_RDWR|O_CREAT|O_TRUNC, 0600, NULL); + ok1(tdb); + if (tdb) { + enum TDB_ERROR err; + for (err = TDB_SUCCESS; err <= TDB_ERR_NESTING; err++) { + tdb->ecode = err; + switch (err) { + case TDB_SUCCESS: + ok1(!strcmp(tdb_errorstr(tdb), + "Success")); + break; + case TDB_ERR_NESTING: + ok1(!strcmp(tdb_errorstr(tdb), + "Transaction already started")); + break; + case TDB_ERR_IO: + ok1(!strcmp(tdb_errorstr(tdb), + "IO Error")); + break; + case TDB_ERR_LOCK: + ok1(!strcmp(tdb_errorstr(tdb), + "Locking error")); + break; + case TDB_ERR_OOM: + ok1(!strcmp(tdb_errorstr(tdb), + "Out of memory")); + break; + case TDB_ERR_EXISTS: + ok1(!strcmp(tdb_errorstr(tdb), + "Record exists")); + break; + case TDB_ERR_EINVAL: + ok1(!strcmp(tdb_errorstr(tdb), + "Invalid parameter")); + break; + case TDB_ERR_NOEXIST: + ok1(!strcmp(tdb_errorstr(tdb), + "Record does not exist")); + break; + case TDB_ERR_RDONLY: + ok1(!strcmp(tdb_errorstr(tdb), + "write not permitted")); + break; + case TDB_ERR_CORRUPT: + ok1(!strcmp(tdb_errorstr(tdb), + "Corrupt database")); + } + } + tdb->ecode = err; + ok1(!strcmp(tdb_errorstr(tdb), "Invalid error code")); + } + return exit_status(); +}