Commit b846677c authored by Rusty Russell's avatar Rusty Russell

tdb2: add tdb_errorstr(), clean up error codes.

Also remove TDB_DISALLOW_NESTING flag, since that's the default now.
parent b8d05b19
...@@ -659,3 +659,21 @@ enum TDB_ERROR tdb_error(struct tdb_context *tdb) ...@@ -659,3 +659,21 @@ enum TDB_ERROR tdb_error(struct tdb_context *tdb)
{ {
return tdb->ecode; 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";
}
...@@ -57,13 +57,11 @@ extern "C" { ...@@ -57,13 +57,11 @@ extern "C" {
#define TDB_SEQNUM 128 /* maintain a sequence number */ #define TDB_SEQNUM 128 /* maintain a sequence number */
#define TDB_VOLATILE 256 /* Activate the per-hashchain freelist, default 5 */ #define TDB_VOLATILE 256 /* Activate the per-hashchain freelist, default 5 */
#define TDB_ALLOW_NESTING 512 /* Allow transactions to nest */ #define TDB_ALLOW_NESTING 512 /* Allow transactions to nest */
#define TDB_DISALLOW_NESTING 1024 /* Disallow transactions to nest */
/* error codes */ /* error codes */
enum TDB_ERROR {TDB_SUCCESS=0, TDB_ERR_CORRUPT, TDB_ERR_IO, TDB_ERR_LOCK, 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_OOM, TDB_ERR_EXISTS, TDB_ERR_NOEXIST,
TDB_ERR_NOEXIST, TDB_ERR_EINVAL, TDB_ERR_RDONLY, TDB_ERR_EINVAL, TDB_ERR_RDONLY, TDB_ERR_NESTING };
TDB_ERR_NESTING};
/* debugging uses one of the following levels */ /* debugging uses one of the following levels */
enum tdb_debug_level {TDB_DEBUG_FATAL = 0, TDB_DEBUG_ERROR, enum tdb_debug_level {TDB_DEBUG_FATAL = 0, TDB_DEBUG_ERROR,
...@@ -141,6 +139,7 @@ int tdb_check(struct tdb_context *tdb, ...@@ -141,6 +139,7 @@ int tdb_check(struct tdb_context *tdb,
void *private_data); void *private_data);
enum TDB_ERROR tdb_error(struct tdb_context *tdb); enum TDB_ERROR tdb_error(struct tdb_context *tdb);
const char *tdb_errorstr(struct tdb_context *tdb);
extern struct tdb_data tdb_null; extern struct tdb_data tdb_null;
......
#include <ccan/tdb2/tdb.c>
#include <ccan/tdb2/free.c>
#include <ccan/tdb2/lock.c>
#include <ccan/tdb2/io.c>
#include <ccan/tdb2/hash.c>
#include <ccan/tdb2/check.c>
#include <ccan/tap/tap.h>
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();
}
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