tdb2: add tdb_errorstr(), clean up error codes.
authorRusty Russell <rusty@rustcorp.com.au>
Tue, 14 Sep 2010 01:08:30 +0000 (10:38 +0930)
committerRusty Russell <rusty@rustcorp.com.au>
Tue, 14 Sep 2010 01:08:30 +0000 (10:38 +0930)
Also remove TDB_DISALLOW_NESTING flag, since that's the default now.

ccan/tdb2/tdb.c
ccan/tdb2/tdb2.h
ccan/tdb2/test/run-tdb_errorstr.c [new file with mode: 0644]

index 7dc5aafbfc254c7d865813e34172e0b29893423d..659d39b23b1ccefc99060c76d6196fa6be77df8e 100644 (file)
@@ -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";
+}
index fa0b6ce7d01651f468a08989dcffb88656e6a98c..98831b4a764c980244fb984b3680f7d84e0df5a0 100644 (file)
@@ -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 (file)
index 0000000..dd8e7b0
--- /dev/null
@@ -0,0 +1,67 @@
+#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();
+}