lib/tdb: make tdbtool use tdb_check() for "check" command
authorRusty Russell <rusty@rustcorp.com.au>
Wed, 21 Oct 2009 13:41:34 +0000 (00:11 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Wed, 21 Oct 2009 13:41:34 +0000 (00:11 +1030)
Also, set logging function so we get more informative messages.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
lib/tdb/tools/tdbtool.c
source4/min_versions.m4

index e469cebf650bc0a72c8cef36a9efdfc0119cd763..d646515a04662ad7ae4d613c00f9c38f3f11b7e7 100644 (file)
@@ -115,6 +115,18 @@ static double _end_timer(void)
               (tp2.tv_usec - tp1.tv_usec)*1.0e-6);
 }
 
+#ifdef PRINTF_ATTRIBUTE
+static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, const char *format, ...) PRINTF_ATTRIBUTE(3,4);
+#endif
+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;
@@ -207,9 +219,12 @@ 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));
        }
@@ -217,8 +232,12 @@ static void create_tdb(const char *tdbname)
 
 static void open_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, disable_mmap?TDB_NOMMAP:0, O_RDWR, 0600);
+       tdb = tdb_open_ex(tdbname, 0, disable_mmap?TDB_NOMMAP:0, O_RDWR, 0600,
+                         &log_ctx, NULL);
        if (!tdb) {
                printf("Could not open %s: %s\n", tdbname, strerror(errno));
        }
@@ -518,25 +537,22 @@ 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)
+static int count(TDB_DATA key, TDB_DATA data, void *private)
 {
+       (*(unsigned int *)private)++;
        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 {
+       int tdbcount = 0;
+       if (!the_tdb)
                printf("Error: No database opened!\n");
-       }
-
-       if (tdbcount<0) {
+       else if (tdb_check(the_tdb, count, &tdbcount) == -1)
                printf("Integrity check for the opened database failed.\n");
-       } else {
-               printf("Database integrity is OK and has %d records.\n", tdbcount);
-       }
+       else
+               printf("Database integrity is OK and has %d records.\n",
+                      tdbcount);
 }
 
 static int do_command(void)
index 62b7806399c7c3385b8ed9236647ad1e2f94064c..6e49783778a635e01b7741f6566a9d0109363d0d 100644 (file)
@@ -1,6 +1,6 @@
 # Minimum and exact required versions for various libraries 
 # if we use the ones installed in the system.
-define(TDB_MIN_VERSION,1.1.6)
+define(TDB_MIN_VERSION,1.1.7)
 define(TALLOC_MIN_VERSION,2.0.0)
 define(LDB_REQUIRED_VERSION,0.9.7)
 define(TEVENT_REQUIRED_VERSION,0.9.8)