Browse Source

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

Also remove TDB_DISALLOW_NESTING flag, since that's the default now.
Rusty Russell 15 years ago
parent
commit
b846677c1e
3 changed files with 88 additions and 4 deletions
  1. 18 0
      ccan/tdb2/tdb.c
  2. 3 4
      ccan/tdb2/tdb2.h
  3. 67 0
      ccan/tdb2/test/run-tdb_errorstr.c

+ 18 - 0
ccan/tdb2/tdb.c

@@ -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";
+}

+ 3 - 4
ccan/tdb2/tdb2.h

@@ -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_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 */
 /* 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,
 	      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;
 
 

+ 67 - 0
ccan/tdb2/test/run-tdb_errorstr.c

@@ -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();
+}