]> git.ozlabs.org Git - ccan/blobdiff - ccan/tdb/tools/tdbtool.c
tdb: fix test helper to include own header, fix function definition.
[ccan] / ccan / tdb / tools / tdbtool.c
index ce712e0e83863013a99d627feccfc040139f3b04..494d1f3800fb8be5b599a1c59277fc67510be033 100644 (file)
@@ -21,6 +21,7 @@
 */
 
 #include <ccan/tdb/tdb.h>
+#include <ccan/hash/hash.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <ctype.h>
@@ -30,6 +31,7 @@
 #include <fcntl.h>
 #include <errno.h>
 #include <string.h>
+#include <stdarg.h>
 
 static int do_command(void);
 const char *cmdname;
@@ -44,6 +46,7 @@ static int disable_mmap;
 enum commands {
        CMD_CREATE_TDB,
        CMD_OPEN_TDB,
+       CMD_OPENJH_TDB,
        CMD_TRANSACTION_START,
        CMD_TRANSACTION_COMMIT,
        CMD_TRANSACTION_CANCEL,
@@ -77,6 +80,7 @@ typedef struct {
 COMMAND_TABLE cmd_table[] = {
        {"create",      CMD_CREATE_TDB},
        {"open",        CMD_OPEN_TDB},
+       {"openjh",      CMD_OPENJH_TDB},
        {"transaction_start",   CMD_TRANSACTION_START},
        {"transaction_commit",  CMD_TRANSACTION_COMMIT},
        {"transaction_cancel",  CMD_TRANSACTION_CANCEL},
@@ -119,6 +123,15 @@ static double _end_timer(void)
               (tp2.tv_usec - tp1.tv_usec)*1.0e-6);
 }
 
+static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, const char *format, ...)
+{
+       va_list ap;
+    
+       va_start(ap, format);
+       vfprintf(stderr, format, ap);
+       va_end(ap);
+}
+
 /* a tdb tool for manipulating a tdb database */
 
 static TDB_CONTEXT *tdb;
@@ -179,6 +192,7 @@ static void help(void)
 "tdbtool: \n"
 "  create    dbname     : create a database\n"
 "  open      dbname     : open an existing database\n"
+"  openjh    dbname     : open an existing database (jenkins hash)\n"
 "  transaction_start    : start a transaction\n"
 "  transaction_commit   : commit a transaction\n"
 "  transaction_cancel   : cancel a transaction\n"
@@ -211,18 +225,30 @@ static void terror(const char *why)
 
 static void create_tdb(const char *tdbname)
 {
+       struct tdb_logging_context log_ctx;
+       log_ctx.log_fn = tdb_log;
+
        if (tdb) tdb_close(tdb);
-       tdb = tdb_open(tdbname, 0, TDB_CLEAR_IF_FIRST | (disable_mmap?TDB_NOMMAP:0),
-                      O_RDWR | O_CREAT | O_TRUNC, 0600);
+       tdb = tdb_open_ex(tdbname, 0, TDB_CLEAR_IF_FIRST | (disable_mmap?TDB_NOMMAP:0),
+                         O_RDWR | O_CREAT | O_TRUNC, 0600, &log_ctx, NULL);
        if (!tdb) {
                printf("Could not create %s: %s\n", tdbname, strerror(errno));
        }
 }
 
-static void open_tdb(const char *tdbname)
+static unsigned int jenkins_hash(TDB_DATA *key)
 {
+       return hash_any(key->dptr, key->dsize, 0);
+}
+
+static void open_tdb(const char *tdbname, tdb_hash_func hashfn)
+{
+       struct tdb_logging_context log_ctx;
+       log_ctx.log_fn = tdb_log;
+
        if (tdb) tdb_close(tdb);
-       tdb = tdb_open(tdbname, 0, disable_mmap?TDB_NOMMAP:0, O_RDWR, 0600);
+       tdb = tdb_open_ex(tdbname, 0, disable_mmap?TDB_NOMMAP:0, O_RDWR, 0600,
+                         &log_ctx, hashfn);
        if (!tdb) {
                printf("Could not open %s: %s\n", tdbname, strerror(errno));
        }
@@ -522,24 +548,15 @@ static void next_record(TDB_CONTEXT *the_tdb, TDB_DATA *pkey)
                print_rec(the_tdb, *pkey, dbuf, NULL);
 }
 
-static int test_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
-{
-       return 0;
-}
-
 static void check_db(TDB_CONTEXT *the_tdb)
 {
-       int tdbcount=-1;
-       if (the_tdb) {
-               tdbcount = tdb_traverse(the_tdb, test_fn, NULL);
-       } else {
+       if (!the_tdb) {
                printf("Error: No database opened!\n");
-       }
-
-       if (tdbcount<0) {
-               printf("Integrity check for the opened database failed.\n");
        } else {
-               printf("Database integrity is OK and has %d records.\n", tdbcount);
+               if (tdb_check(the_tdb, NULL, NULL) == -1)
+                       printf("Integrity check for the opened database failed.\n");
+               else
+                       printf("Database integrity is OK.\n");
        }
 }
 
@@ -569,7 +586,11 @@ static int do_command(void)
                return 0;
        case CMD_OPEN_TDB:
                bIterate = 0;
-               open_tdb(arg1);
+               open_tdb(arg1, NULL);
+               return 0;
+       case CMD_OPENJH_TDB:
+               bIterate = 0;
+               open_tdb(arg1, jenkins_hash);
                return 0;
        case CMD_SYSTEM:
                /* Shell command */
@@ -665,6 +686,7 @@ static int do_command(void)
                        return 0;
                case CMD_CREATE_TDB:
                case CMD_OPEN_TDB:
+               case CMD_OPENJH_TDB:
                case CMD_SYSTEM:
                case CMD_QUIT:
                        /*