tdb2: make tdb1_open use attributes for logging, hash function.
authorRusty Russell <rusty@rustcorp.com.au>
Tue, 13 Sep 2011 22:16:13 +0000 (07:46 +0930)
committerRusty Russell <rusty@rustcorp.com.au>
Tue, 13 Sep 2011 22:16:13 +0000 (07:46 +0930)
This brings it closer to tdb_open(), so we can unify more easily.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
(Imported from CCAN commit a446f1d4d161d66bbb19ba2551cf6429a4865964)

25 files changed:
lib/tdb2/tdb1.h
lib/tdb2/tdb1_open.c
lib/tdb2/tdb1_tdb.c
lib/tdb2/test/run-tdb1-3G-file.c
lib/tdb2/test/run-tdb1-bad-tdb-header.c
lib/tdb2/test/run-tdb1-check.c
lib/tdb2/test/run-tdb1-corrupt.c
lib/tdb2/test/run-tdb1-die-during-transaction.c
lib/tdb2/test/run-tdb1-endian.c
lib/tdb2/test/run-tdb1-incompatible.c
lib/tdb2/test/run-tdb1-nested-transactions.c
lib/tdb2/test/run-tdb1-nested-traverse.c
lib/tdb2/test/run-tdb1-no-lock-during-traverse.c
lib/tdb2/test/run-tdb1-oldhash.c
lib/tdb2/test/run-tdb1-open-during-transaction.c
lib/tdb2/test/run-tdb1-readonly-check.c
lib/tdb2/test/run-tdb1-rwlock-check.c
lib/tdb2/test/run-tdb1-summary.c
lib/tdb2/test/run-tdb1-traverse-in-transaction.c
lib/tdb2/test/run-tdb1-wronghash-fail.c
lib/tdb2/test/run-tdb1-zero-append.c
lib/tdb2/test/run-tdb1.c
lib/tdb2/test/tdb1-external-agent.c
lib/tdb2/test/tdb1-logging.c [deleted file]
lib/tdb2/test/tdb1-logging.h [deleted file]

index 78d42b398c50de5d77f07c20c8ca70b5278ac125..a2a0afe808ff56428680571903539ada76378673 100644 (file)
 
 
 typedef int (*tdb1_traverse_func)(struct tdb_context *, TDB_DATA, TDB_DATA, void *);
-typedef void (*tdb1_log_func)(struct tdb_context *, enum tdb_log_level, enum TDB_ERROR,
-                             const char *, void *);
-typedef uint64_t (*tdb1_hash_func)(const void *key, size_t len, uint64_t seed,
-                                  void *data);
-
-struct tdb1_logging_context {
-        tdb1_log_func log_fn;
-        void *log_private;
-};
 
 struct tdb_context *tdb1_open(const char *name, int hash_size, int tdb1_flags,
-                     int open_flags, mode_t mode);
-
-struct tdb_context *tdb1_open_ex(const char *name, int hash_size, int tdb1_flags,
-                        int open_flags, mode_t mode,
-                        const struct tdb1_logging_context *log_ctx,
-                        tdb1_hash_func hash_fn);
+                             int open_flags, mode_t mode,
+                             union tdb_attribute *attributes);
 
 void tdb1_set_max_dead(struct tdb_context *tdb, int max_dead);
 
index 5d0097b2a79ac2de54875414cf8c0485d3d7357e..1f23db0126ae07afcbf2629fa584714bb3b9bdcf 100644 (file)
@@ -95,23 +95,15 @@ static int tdb1_new_database(struct tdb_context *tdb, int hash_size)
        return ret;
 }
 
+typedef void (*tdb1_log_func)(struct tdb_context *, enum tdb_log_level, enum TDB_ERROR,
+                             const char *, void *);
+typedef uint64_t (*tdb1_hash_func)(const void *key, size_t len, uint64_t seed,
+                                  void *data);
 
-
-/* open the database, creating it if necessary
-
-   The open_flags and mode are passed straight to the open call on the
-   database file. A flags value of O_WRONLY is invalid. The hash size
-   is advisory, use zero for a default value.
-
-   Return is NULL on error, in which case errno is also set.  Don't
-   try to call tdb1_error or tdb1_errname, just do strerror(errno).
-
-   @param name may be NULL for internal databases. */
-struct tdb_context *tdb1_open(const char *name, int hash_size, int tdb1_flags,
-                     int open_flags, mode_t mode)
-{
-       return tdb1_open_ex(name, hash_size, tdb1_flags, open_flags, mode, NULL, NULL);
-}
+struct tdb1_logging_context {
+        tdb1_log_func log_fn;
+        void *log_private;
+};
 
 static bool hash_correct(struct tdb_context *tdb,
                         uint32_t *m1, uint32_t *m2)
@@ -137,7 +129,7 @@ static bool check_header_hash(struct tdb_context *tdb,
        return hash_correct(tdb, m1, m2);
 }
 
-struct tdb_context *tdb1_open_ex(const char *name, int hash_size, int tdb1_flags,
+static struct tdb_context *tdb1_open_ex(const char *name, int hash_size, int tdb1_flags,
                                int open_flags, mode_t mode,
                                const struct tdb1_logging_context *log_ctx,
                                tdb1_hash_func hash_fn)
@@ -366,6 +358,34 @@ struct tdb_context *tdb1_open_ex(const char *name, int hash_size, int tdb1_flags
        }
 }
 
+/* Temporart wrapper for transition. */
+struct tdb_context *tdb1_open(const char *name, int hash_size, int tdb1_flags,
+                             int open_flags, mode_t mode,
+                             union tdb_attribute *attr)
+{
+       struct tdb1_logging_context *log_ctx = NULL, log;
+       tdb1_hash_func hash_fn = NULL;
+
+       while (attr) {
+               switch (attr->base.attr) {
+               case TDB_ATTRIBUTE_HASH:
+                       hash_fn = attr->hash.fn;
+                       break;
+               case TDB_ATTRIBUTE_LOG:
+                       log.log_fn = attr->log.fn;
+                       log.log_private = attr->log.data;
+                       log_ctx = &log;
+                       break;
+               default:
+                       abort();
+               }
+               attr = attr->base.next;
+       }
+
+       return tdb1_open_ex(name, hash_size, tdb1_flags, open_flags, mode,
+                           log_ctx, hash_fn);
+}
+
 /*
  * Set the maximum number of dead records per hash chain
  */
index 400c9866e88ed32f7c675e445d6f5061e4b6fdcb..e9696000b86ec8b9937f8d398bf4e67a05c894a3 100644 (file)
@@ -843,7 +843,7 @@ int tdb1_repack(struct tdb_context *tdb)
                return -1;
        }
 
-       tmp_db = tdb1_open("tmpdb", tdb1_hash_size(tdb), TDB_INTERNAL, O_RDWR|O_CREAT, 0);
+       tmp_db = tdb1_open("tmpdb", tdb1_hash_size(tdb), TDB_INTERNAL, O_RDWR|O_CREAT, 0, NULL);
        if (tmp_db == NULL) {
                tdb->last_error = tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
                                        __location__ " Failed to create tmp_db");
index d5aeda5ddebd1f48be28a0b3b87717dc6f657384..1eab4476712a3a760ab408090b8d726de57c5833 100644 (file)
@@ -4,7 +4,7 @@
 #include <ccan/tap/tap.h>
 #include <stdlib.h>
 #include <err.h>
-#include "tdb1-logging.h"
+#include "logging.h"
 
 static int tdb1_expand_file_sparse(struct tdb_context *tdb,
                                  tdb1_off_t size,
@@ -66,8 +66,8 @@ int main(int argc, char *argv[])
        struct tdb1_record rec;
 
        plan_tests(24);
-       tdb = tdb1_open_ex("run-36-file.tdb", 1024, TDB_DEFAULT,
-                         O_CREAT|O_TRUNC|O_RDWR, 0600, &taplogctx, NULL);
+       tdb = tdb1_open("run-36-file.tdb", 1024, TDB_DEFAULT,
+                       O_CREAT|O_TRUNC|O_RDWR, 0600, &tap_log_attr);
 
        ok1(tdb);
        tdb->tdb1.io = &large_io_methods;
index fe2501081a90a94af7bc89a0bf65f62b80300e2e..a32b817c1deb0dd540d5d9ec432f83fef63d025f 100644 (file)
@@ -2,7 +2,7 @@
 #include <ccan/tap/tap.h>
 #include <stdlib.h>
 #include <err.h>
-#include "tdb1-logging.h"
+#include "logging.h"
 
 int main(int argc, char *argv[])
 {
@@ -16,11 +16,11 @@ int main(int argc, char *argv[])
        ok1(fd >= 0);
        ok1(write(fd, "hello world", 11) == 11);
        close(fd);
-       tdb = tdb1_open_ex("run-bad-tdb-header.tdb", 1024, 0, O_RDWR, 0,
-                         &taplogctx, NULL);
+       tdb = tdb1_open("run-bad-tdb-header.tdb", 1024, 0, O_RDWR, 0,
+                       &tap_log_attr);
        ok1(!tdb);
-       tdb = tdb1_open_ex("run-bad-tdb-header.tdb", 1024, 0, O_CREAT|O_RDWR,
-                         0600, &taplogctx, NULL);
+       tdb = tdb1_open("run-bad-tdb-header.tdb", 1024, 0, O_CREAT|O_RDWR,
+                       0600, &tap_log_attr);
        ok1(tdb);
        tdb1_close(tdb);
 
@@ -34,14 +34,14 @@ int main(int argc, char *argv[])
        ok1(write(fd, &hdr, sizeof(hdr)) == sizeof(hdr));
        close(fd);
 
-       tdb = tdb1_open_ex("run-bad-tdb-header.tdb", 1024, 0, O_RDWR|O_CREAT,
-                         0600, &taplogctx, NULL);
+       tdb = tdb1_open("run-bad-tdb-header.tdb", 1024, 0, O_RDWR|O_CREAT,
+                       0600, &tap_log_attr);
        ok1(errno == EIO);
        ok1(!tdb);
 
        /* With truncate, will be fine. */
-       tdb = tdb1_open_ex("run-bad-tdb-header.tdb", 1024, 0,
-                         O_RDWR|O_CREAT|O_TRUNC, 0600, &taplogctx, NULL);
+       tdb = tdb1_open("run-bad-tdb-header.tdb", 1024, 0,
+                       O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
        ok1(tdb);
        tdb1_close(tdb);
 
index 9864e42c56a182d524f0ce7414ac4ed079b1ff45..017eb8328acfa1a0e92e2b12d57a630b79c16925 100644 (file)
@@ -2,7 +2,7 @@
 #include <ccan/tap/tap.h>
 #include <stdlib.h>
 #include <err.h>
-#include "tdb1-logging.h"
+#include "logging.h"
 
 int main(int argc, char *argv[])
 {
@@ -10,8 +10,8 @@ int main(int argc, char *argv[])
        TDB_DATA key, data;
 
        plan_tests(13);
-       tdb = tdb1_open_ex("run-check.tdb", 1, TDB_DEFAULT,
-                         O_CREAT|O_TRUNC|O_RDWR, 0600, &taplogctx, NULL);
+       tdb = tdb1_open("run-check.tdb", 1, TDB_DEFAULT,
+                       O_CREAT|O_TRUNC|O_RDWR, 0600, &tap_log_attr);
 
        ok1(tdb);
        ok1(tdb1_check(tdb, NULL, NULL) == 0);
@@ -25,28 +25,28 @@ int main(int argc, char *argv[])
        ok1(tdb1_check(tdb, NULL, NULL) == 0);
        tdb1_close(tdb);
 
-       tdb = tdb1_open_ex("run-check.tdb", 1024, 0, O_RDWR, 0,
-                         &taplogctx, NULL);
+       tdb = tdb1_open("run-check.tdb", 1024, 0, O_RDWR, 0,
+                       &tap_log_attr);
        ok1(tdb);
        ok1(tdb1_check(tdb, NULL, NULL) == 0);
        tdb1_close(tdb);
 
-       tdb = tdb1_open_ex("test/tdb1.corrupt", 1024, 0, O_RDWR, 0,
-                         &taplogctx, NULL);
+       tdb = tdb1_open("test/tdb1.corrupt", 1024, 0, O_RDWR, 0,
+                       &tap_log_attr);
        ok1(tdb);
        ok1(tdb1_check(tdb, NULL, NULL) == -1);
        ok1(tdb_error(tdb) == TDB_ERR_CORRUPT);
        tdb1_close(tdb);
 
        /* Big and little endian should work! */
-       tdb = tdb1_open_ex("test/old-nohash-le.tdb1", 1024, 0, O_RDWR, 0,
-                         &taplogctx, NULL);
+       tdb = tdb1_open("test/old-nohash-le.tdb1", 1024, 0, O_RDWR, 0,
+                       &tap_log_attr);
        ok1(tdb);
        ok1(tdb1_check(tdb, NULL, NULL) == 0);
        tdb1_close(tdb);
 
-       tdb = tdb1_open_ex("test/old-nohash-be.tdb1", 1024, 0, O_RDWR, 0,
-                         &taplogctx, NULL);
+       tdb = tdb1_open("test/old-nohash-be.tdb1", 1024, 0, O_RDWR, 0,
+                       &tap_log_attr);
        ok1(tdb);
        ok1(tdb1_check(tdb, NULL, NULL) == 0);
        tdb1_close(tdb);
index 2e70da7ce5ddf53848bcc96fab64748fce84227c..833e9c1ee1a743ee8f7d63ebdbe0f59d5a976e90 100644 (file)
@@ -2,7 +2,7 @@
 #include <ccan/tap/tap.h>
 #include <stdlib.h>
 #include <err.h>
-#include "tdb1-logging.h"
+#include "logging.h"
 
 static int check(TDB_DATA key, TDB_DATA data, void *private)
 {
@@ -97,8 +97,8 @@ int main(int argc, char *argv[])
 
        plan_tests(4);
        /* This should use mmap. */
-       tdb = tdb1_open_ex("run-corrupt.tdb", 2, TDB_DEFAULT,
-                         O_CREAT|O_TRUNC|O_RDWR, 0600, &taplogctx, NULL);
+       tdb = tdb1_open("run-corrupt.tdb", 2, TDB_DEFAULT,
+                       O_CREAT|O_TRUNC|O_RDWR, 0600, &tap_log_attr);
 
        if (!tdb)
                abort();
@@ -106,8 +106,8 @@ int main(int argc, char *argv[])
        tdb1_close(tdb);
 
        /* This should not. */
-       tdb = tdb1_open_ex("run-corrupt.tdb", 2, TDB_NOMMAP,
-                         O_CREAT|O_TRUNC|O_RDWR, 0600, &taplogctx, NULL);
+       tdb = tdb1_open("run-corrupt.tdb", 2, TDB_NOMMAP,
+                       O_CREAT|O_TRUNC|O_RDWR, 0600, &tap_log_attr);
 
        if (!tdb)
                abort();
index 1d03a30a1a02ce65b73f5258a1a227f4bade01b8..9ff75d10201f36c3ea90b6e121793286d46b565b 100644 (file)
@@ -18,7 +18,7 @@ static int ftruncate_check(int fd, off_t length);
 #include <err.h>
 #include <setjmp.h>
 #include "tdb1-external-agent.h"
-#include "tdb1-logging.h"
+#include "logging.h"
 
 #undef write
 #undef pwrite
@@ -89,8 +89,8 @@ static bool test_death(enum operation op, struct agent *agent)
        current = target = 0;
 reset:
        unlink(TEST_DBNAME);
-       tdb = tdb1_open_ex(TEST_DBNAME, 1024, TDB_NOMMAP,
-                         O_CREAT|O_TRUNC|O_RDWR, 0600, &taplogctx, NULL);
+       tdb = tdb1_open(TEST_DBNAME, 1024, TDB_NOMMAP,
+                       O_CREAT|O_TRUNC|O_RDWR, 0600, &tap_log_attr);
 
        if (setjmp(jmpbuf) != 0) {
                /* We're partway through.  Simulate our death. */
index 245b65446c92785978b495287bbcdb545a20a4fd..a06bfb6e9906f479a42a55851a54e978b870723f 100644 (file)
@@ -2,7 +2,7 @@
 #include <ccan/tap/tap.h>
 #include <stdlib.h>
 #include <err.h>
-#include "tdb1-logging.h"
+#include "logging.h"
 
 int main(int argc, char *argv[])
 {
@@ -10,9 +10,9 @@ int main(int argc, char *argv[])
        TDB_DATA key, data;
 
        plan_tests(13);
-       tdb = tdb1_open_ex("run-endian.tdb", 1024,
-                         TDB_CONVERT,
-                         O_CREAT|O_TRUNC|O_RDWR, 0600, &taplogctx, NULL);
+       tdb = tdb1_open("run-endian.tdb", 1024,
+                       TDB_CONVERT,
+                       O_CREAT|O_TRUNC|O_RDWR, 0600, &tap_log_attr);
 
        ok1(tdb);
        key.dsize = strlen("hi");
@@ -38,8 +38,8 @@ int main(int argc, char *argv[])
        tdb1_close(tdb);
 
        /* Reopen: should read it */
-       tdb = tdb1_open_ex("run-endian.tdb", 1024, 0, O_RDWR, 0,
-                         &taplogctx, NULL);
+       tdb = tdb1_open("run-endian.tdb", 1024, 0, O_RDWR, 0,
+                       &tap_log_attr);
        ok1(tdb);
 
        key.dsize = strlen("hi");
index 9e581614622bd9f60a295a9869fb205527fca867..baf40ed4e68cd78cae66c47f70d65f17c9113b34 100644 (file)
@@ -49,7 +49,29 @@ int main(int argc, char *argv[])
        struct tdb_context *tdb;
        unsigned int log_count, flags;
        TDB_DATA d;
-       struct tdb1_logging_context log_ctx = { log_fn, &log_count };
+       union tdb_attribute log_attr, jhash_attr, ohash_attr,
+               incompat_hash_attr, dumbhash_attr;
+
+       log_attr.base.attr = TDB_ATTRIBUTE_LOG;
+       log_attr.base.next = NULL;
+       log_attr.log.fn = log_fn;
+       log_attr.log.data = &log_count;
+
+       jhash_attr.base.attr = TDB_ATTRIBUTE_HASH;
+       jhash_attr.base.next = &log_attr;
+       jhash_attr.hash.fn = jenkins_hashfn;
+
+       ohash_attr.base.attr = TDB_ATTRIBUTE_HASH;
+       ohash_attr.base.next = &log_attr;
+       ohash_attr.hash.fn = old_hash;
+
+       incompat_hash_attr.base.attr = TDB_ATTRIBUTE_HASH;
+       incompat_hash_attr.base.next = &log_attr;
+       incompat_hash_attr.hash.fn = tdb1_incompatible_hash;
+
+       dumbhash_attr.base.attr = TDB_ATTRIBUTE_HASH;
+       dumbhash_attr.base.next = &log_attr;
+       dumbhash_attr.hash.fn = tdb1_dumb_hash;
 
        plan_tests(38 * 2);
 
@@ -61,9 +83,8 @@ int main(int argc, char *argv[])
 
                /* Create an old-style hash. */
                log_count = 0;
-               tdb = tdb1_open_ex("run-incompatible.tdb", 0, flags,
-                                 O_CREAT|O_RDWR|O_TRUNC, 0600, &log_ctx,
-                                 NULL);
+               tdb = tdb1_open("run-incompatible.tdb", 0, flags,
+                               O_CREAT|O_RDWR|O_TRUNC, 0600, &log_attr);
                ok1(tdb);
                ok1(log_count == 0);
                d.dptr = (void *)"Hello";
@@ -76,10 +97,9 @@ int main(int argc, char *argv[])
 
                /* We can still open any old-style with incompat hash. */
                log_count = 0;
-               tdb = tdb1_open_ex("run-incompatible.tdb", 0,
-                                 TDB_DEFAULT,
-                                 O_RDWR, 0600, &log_ctx,
-                                  tdb1_incompatible_hash);
+               tdb = tdb1_open("run-incompatible.tdb", 0,
+                               TDB_DEFAULT,
+                               O_RDWR, 0600, &incompat_hash_attr);
                ok1(tdb);
                ok1(log_count == 0);
                d = tdb1_fetch(tdb, d);
@@ -89,16 +109,16 @@ int main(int argc, char *argv[])
                tdb1_close(tdb);
 
                log_count = 0;
-               tdb = tdb1_open_ex("test/jenkins-le-hash.tdb1", 0, 0, O_RDONLY,
-                                 0, &log_ctx, jenkins_hashfn);
+               tdb = tdb1_open("test/jenkins-le-hash.tdb1", 0, 0, O_RDONLY,
+                               0, &jhash_attr);
                ok1(tdb);
                ok1(log_count == 0);
                ok1(tdb1_check(tdb, NULL, NULL) == 0);
                tdb1_close(tdb);
 
                log_count = 0;
-               tdb = tdb1_open_ex("test/jenkins-be-hash.tdb1", 0, 0, O_RDONLY,
-                                 0, &log_ctx, jenkins_hashfn);
+               tdb = tdb1_open("test/jenkins-be-hash.tdb1", 0, 0, O_RDONLY,
+                               0, &jhash_attr);
                ok1(tdb);
                ok1(log_count == 0);
                ok1(tdb1_check(tdb, NULL, NULL) == 0);
@@ -106,10 +126,10 @@ int main(int argc, char *argv[])
 
                /* OK, now create with incompatible hash. */
                log_count = 0;
-               tdb = tdb1_open_ex("run-incompatible.tdb", 0,
-                                 flags,
-                                 O_CREAT|O_RDWR|O_TRUNC, 0600, &log_ctx,
-                                 tdb1_incompatible_hash);
+               tdb = tdb1_open("run-incompatible.tdb", 0,
+                               flags,
+                               O_CREAT|O_RDWR|O_TRUNC, 0600,
+                               &incompat_hash_attr);
                ok1(tdb);
                ok1(log_count == 0);
                d.dptr = (void *)"Hello";
@@ -122,15 +142,15 @@ int main(int argc, char *argv[])
 
                /* Cannot open with old hash. */
                log_count = 0;
-               tdb = tdb1_open_ex("run-incompatible.tdb", 0, 0,
-                                 O_RDWR, 0600, &log_ctx, old_hash);
+               tdb = tdb1_open("run-incompatible.tdb", 0, 0,
+                               O_RDWR, 0600, &ohash_attr);
                ok1(!tdb);
                ok1(log_count == 1);
 
                /* Can open with jenkins hash. */
                log_count = 0;
-               tdb = tdb1_open_ex("run-incompatible.tdb", 0, 0,
-                                 O_RDWR, 0600, &log_ctx, jenkins_hashfn);
+               tdb = tdb1_open("run-incompatible.tdb", 0, 0,
+                               O_RDWR, 0600, &jhash_attr);
                ok1(tdb);
                ok1(log_count == 0);
                d = tdb1_fetch(tdb, d);
@@ -141,8 +161,8 @@ int main(int argc, char *argv[])
 
                /* Can open by letting it figure it out itself. */
                log_count = 0;
-               tdb = tdb1_open_ex("run-incompatible.tdb", 0, 0,
-                                 O_RDWR, 0600, &log_ctx, NULL);
+               tdb = tdb1_open("run-incompatible.tdb", 0, 0,
+                               O_RDWR, 0600, &log_attr);
                ok1(tdb);
                ok1(log_count == 0);
                d.dptr = (void *)"Hello";
@@ -156,10 +176,9 @@ int main(int argc, char *argv[])
                /* FIXME: Not possible with TDB2 :( */
                /* We can also use incompatible hash with other hashes. */
                log_count = 0;
-               tdb = tdb1_open_ex("run-incompatible.tdb", 0,
-                                 flags,
-                                 O_CREAT|O_RDWR|O_TRUNC, 0600, &log_ctx,
-                                 tdb1_dumb_hash);
+               tdb = tdb1_open("run-incompatible.tdb", 0,
+                               flags,
+                               O_CREAT|O_RDWR|O_TRUNC, 0600, &dumbhash_attr);
                ok1(tdb);
                ok1(log_count == 0);
                d.dptr = (void *)"Hello";
@@ -172,15 +191,15 @@ int main(int argc, char *argv[])
 
                /* It should not open if we don't specify. */
                log_count = 0;
-               tdb = tdb1_open_ex("run-incompatible.tdb", 0, 0, O_RDWR, 0,
-                                 &log_ctx, NULL);
+               tdb = tdb1_open("run-incompatible.tdb", 0, 0, O_RDWR, 0,
+                               &log_attr);
                ok1(!tdb);
                ok1(log_count == 1);
 
                /* Should reopen with correct hash. */
                log_count = 0;
-               tdb = tdb1_open_ex("run-incompatible.tdb", 0, 0, O_RDWR, 0,
-                                 &log_ctx, tdb1_dumb_hash);
+               tdb = tdb1_open("run-incompatible.tdb", 0, 0, O_RDWR, 0,
+                               &dumbhash_attr);
                ok1(tdb);
                ok1(log_count == 0);
                d = tdb1_fetch(tdb, d);
index be4e926946c7e3a8a2fc5f913d94c97a521a32e6..d5886123eaa4d2b91ee4b35a5c59c886e3c16d26 100644 (file)
@@ -3,7 +3,7 @@
 #include <stdlib.h>
 #include <stdbool.h>
 #include <err.h>
-#include "tdb1-logging.h"
+#include "logging.h"
 
 int main(int argc, char *argv[])
 {
@@ -14,9 +14,9 @@ int main(int argc, char *argv[])
        key.dsize = strlen("hi");
        key.dptr = (void *)"hi";
 
-       tdb = tdb1_open_ex("run-nested-transactions.tdb",
-                         1024, TDB_DEFAULT,
-                         O_CREAT|O_TRUNC|O_RDWR, 0600, &taplogctx, NULL);
+       tdb = tdb1_open("run-nested-transactions.tdb",
+                       1024, TDB_DEFAULT,
+                       O_CREAT|O_TRUNC|O_RDWR, 0600, &tap_log_attr);
        ok1(tdb);
 
        /* No nesting by default. */
@@ -42,8 +42,8 @@ int main(int argc, char *argv[])
        free(data.dptr);
        tdb1_close(tdb);
 
-       tdb = tdb1_open_ex("run-nested-transactions.tdb",
-                         1024, TDB_ALLOW_NESTING, O_RDWR, 0, &taplogctx, NULL);
+       tdb = tdb1_open("run-nested-transactions.tdb",
+                       1024, TDB_ALLOW_NESTING, O_RDWR, 0, &tap_log_attr);
        ok1(tdb);
 
        ok1(tdb1_transaction_start(tdb) == 0);
index 3d15082a0359b306cfb68c6a2da4e7e49038bc33..1fc8d092773200a657c940970fecc5dae6bcc773 100644 (file)
@@ -7,7 +7,7 @@
 #include <stdbool.h>
 #include <err.h>
 #include "tdb1-external-agent.h"
-#include "tdb1-logging.h"
+#include "logging.h"
 
 static struct agent *agent;
 
@@ -56,8 +56,8 @@ int main(int argc, char *argv[])
        if (!agent)
                err(1, "preparing agent");
 
-       tdb = tdb1_open_ex("run-nested-traverse.tdb", 1024, TDB_DEFAULT,
-                         O_CREAT|O_TRUNC|O_RDWR, 0600, &taplogctx, NULL);
+       tdb = tdb1_open("run-nested-traverse.tdb", 1024, TDB_DEFAULT,
+                       O_CREAT|O_TRUNC|O_RDWR, 0600, &tap_log_attr);
        ok1(tdb);
 
        ok1(external_agent_operation1(agent, OPEN, tdb->name) == SUCCESS);
index 10d57bacfb78f7a867963cf2f39c6a7fa634bed9..cb1c23606b24a14a54ff92bb60655d6902fd62b5 100644 (file)
@@ -8,7 +8,7 @@
 #include <ccan/tap/tap.h>
 #include <stdlib.h>
 #include <err.h>
-#include "tdb1-logging.h"
+#include "logging.h"
 
 #undef fcntl
 
@@ -70,9 +70,9 @@ int main(int argc, char *argv[])
        int errors = 0;
 
        plan_tests(41);
-       tdb = tdb1_open_ex("run-no-lock-during-traverse.tdb",
-                         1024, TDB_DEFAULT, O_CREAT|O_TRUNC|O_RDWR,
-                         0600, &taplogctx, NULL);
+       tdb = tdb1_open("run-no-lock-during-traverse.tdb",
+                       1024, TDB_DEFAULT, O_CREAT|O_TRUNC|O_RDWR,
+                       0600, &tap_log_attr);
 
        ok1(tdb);
        ok1(prepare_entries(tdb));
index ea56dd3c8ce9c7a64e4bdd4744309a7ad1aeecf5..5219d5c30a246a25df81eae2f80da8c08833bb0a 100644 (file)
@@ -2,36 +2,41 @@
 #include <ccan/tap/tap.h>
 #include <stdlib.h>
 #include <err.h>
-#include "tdb1-logging.h"
+#include "logging.h"
 
 int main(int argc, char *argv[])
 {
        struct tdb_context *tdb;
+       union tdb_attribute incompat_hash_attr;
+
+       incompat_hash_attr.base.attr = TDB_ATTRIBUTE_HASH;
+       incompat_hash_attr.base.next = &tap_log_attr;
+       incompat_hash_attr.hash.fn = tdb1_incompatible_hash;
 
        plan_tests(8);
 
        /* Old format (with zeroes in the hash magic fields) should
         * open with any hash (since we don't know what hash they used). */
-       tdb = tdb1_open_ex("test/old-nohash-le.tdb1", 0, 0, O_RDWR, 0,
-                         &taplogctx, NULL);
+       tdb = tdb1_open("test/old-nohash-le.tdb1", 0, 0, O_RDWR, 0,
+                       &tap_log_attr);
        ok1(tdb);
        ok1(tdb1_check(tdb, NULL, NULL) == 0);
        tdb1_close(tdb);
 
-       tdb = tdb1_open_ex("test/old-nohash-be.tdb1", 0, 0, O_RDWR, 0,
-                         &taplogctx, NULL);
+       tdb = tdb1_open("test/old-nohash-be.tdb1", 0, 0, O_RDWR, 0,
+                       &tap_log_attr);
        ok1(tdb);
        ok1(tdb1_check(tdb, NULL, NULL) == 0);
        tdb1_close(tdb);
 
-       tdb = tdb1_open_ex("test/old-nohash-le.tdb1", 0, 0, O_RDWR, 0,
-                         &taplogctx, tdb1_incompatible_hash);
+       tdb = tdb1_open("test/old-nohash-le.tdb1", 0, 0, O_RDWR, 0,
+                       &incompat_hash_attr);
        ok1(tdb);
        ok1(tdb1_check(tdb, NULL, NULL) == 0);
        tdb1_close(tdb);
 
-       tdb = tdb1_open_ex("test/old-nohash-be.tdb1", 0, 0, O_RDWR, 0,
-                         &taplogctx, tdb1_incompatible_hash);
+       tdb = tdb1_open("test/old-nohash-be.tdb1", 0, 0, O_RDWR, 0,
+                       &incompat_hash_attr);
        ok1(tdb);
        ok1(tdb1_check(tdb, NULL, NULL) == 0);
        tdb1_close(tdb);
index 3cb0685bbc0b73104fc7beef7df76c0e934211df..e0b28a536e8f30578d6599688eab544a49da8fea 100644 (file)
@@ -18,7 +18,7 @@ static int ftruncate_check(int fd, off_t length);
 #include <stdarg.h>
 #include <err.h>
 #include "tdb1-external-agent.h"
-#include "tdb1-logging.h"
+#include "logging.h"
 
 static struct agent *agent;
 static bool opened;
@@ -145,9 +145,9 @@ int main(int argc, char *argv[])
                     "DEFAULT",
                     (flags[i] & TDB_NOMMAP) ? "no mmap" : "mmap");
                unlink(TEST_DBNAME);
-               tdb = tdb1_open_ex(TEST_DBNAME, 1024, flags[i],
-                                 O_CREAT|O_TRUNC|O_RDWR, 0600,
-                                 &taplogctx, NULL);
+               tdb = tdb1_open(TEST_DBNAME, 1024, flags[i],
+                               O_CREAT|O_TRUNC|O_RDWR, 0600,
+                               &tap_log_attr);
                ok1(tdb);
 
                opened = true;
index 2eda33d9b65734d95c8370a0f8aa8630166c62e0..762c576f9228a68462cca4c6afdb055c0b9b0027 100644 (file)
@@ -4,7 +4,7 @@
 #include <ccan/tap/tap.h>
 #include <stdlib.h>
 #include <err.h>
-#include "tdb1-logging.h"
+#include "logging.h"
 
 int main(int argc, char *argv[])
 {
@@ -12,9 +12,9 @@ int main(int argc, char *argv[])
        TDB_DATA key, data;
 
        plan_tests(11);
-       tdb = tdb1_open_ex("run-readonly-check.tdb", 1024,
-                         TDB_DEFAULT,
-                         O_CREAT|O_TRUNC|O_RDWR, 0600, &taplogctx, NULL);
+       tdb = tdb1_open("run-readonly-check.tdb", 1024,
+                       TDB_DEFAULT,
+                       O_CREAT|O_TRUNC|O_RDWR, 0600, &tap_log_attr);
 
        ok1(tdb);
        key.dsize = strlen("hi");
@@ -30,8 +30,8 @@ int main(int argc, char *argv[])
        ok1(tdb1_check(tdb, NULL, NULL) == 0);
        ok1(tdb1_close(tdb) == 0);
 
-       tdb = tdb1_open_ex("run-readonly-check.tdb", 1024,
-                         TDB_DEFAULT, O_RDONLY, 0, &taplogctx, NULL);
+       tdb = tdb1_open("run-readonly-check.tdb", 1024,
+                       TDB_DEFAULT, O_RDONLY, 0, &tap_log_attr);
 
        ok1(tdb);
        ok1(tdb1_store(tdb, key, data, TDB_MODIFY) == -1);
index cd227f4cd7b8a2c2ddcea7a1a562fb35151b3f98..6f4406c0265d68e0c71eea8aef9ef6dde9b3a2ce 100644 (file)
@@ -16,20 +16,25 @@ int main(int argc, char *argv[])
 {
        struct tdb_context *tdb;
        unsigned int log_count;
-       struct tdb1_logging_context log_ctx = { log_fn, &log_count };
+       union tdb_attribute log_attr;
+
+       log_attr.base.attr = TDB_ATTRIBUTE_LOG;
+       log_attr.base.next = NULL;
+       log_attr.log.fn = log_fn;
+       log_attr.log.data = &log_count;
 
        plan_tests(4);
 
        /* We should fail to open rwlock-using tdbs of either endian. */
        log_count = 0;
-       tdb = tdb1_open_ex("test/rwlock-le.tdb1", 0, 0, O_RDWR, 0,
-                         &log_ctx, NULL);
+       tdb = tdb1_open("test/rwlock-le.tdb1", 0, 0, O_RDWR, 0,
+                       &log_attr);
        ok1(!tdb);
        ok1(log_count == 1);
 
        log_count = 0;
-       tdb = tdb1_open_ex("test/rwlock-be.tdb1", 0, 0, O_RDWR, 0,
-                         &log_ctx, NULL);
+       tdb = tdb1_open("test/rwlock-be.tdb1", 0, 0, O_RDWR, 0,
+                       &log_attr);
        ok1(!tdb);
        ok1(log_count == 1);
 
index ffd8bc8caa60b58d2f0383f37ec263a309c90140..8b9b74ffe764802107af33aa767a4d43fb545206 100644 (file)
@@ -17,7 +17,7 @@ int main(int argc, char *argv[])
        plan_tests(sizeof(flags) / sizeof(flags[0]) * 14);
        for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
                tdb = tdb1_open("run-summary.tdb", 131, flags[i],
-                              O_RDWR|O_CREAT|O_TRUNC, 0600);
+                               O_RDWR|O_CREAT|O_TRUNC, 0600, NULL);
                ok1(tdb);
                if (!tdb)
                        continue;
index 243f2d653f504bfed14bb5e7da5882265e58790d..0395bff7ce66deeb30ebf465bf42869dee14f361 100644 (file)
@@ -8,7 +8,7 @@
 #include <stdbool.h>
 #include <err.h>
 #include "tdb1-external-agent.h"
-#include "tdb1-logging.h"
+#include "logging.h"
 
 static struct agent *agent;
 
@@ -42,9 +42,9 @@ int main(int argc, char *argv[])
        if (!agent)
                err(1, "preparing agent");
 
-       tdb = tdb1_open_ex("run-traverse-in-transaction.tdb",
-                         1024, TDB_DEFAULT, O_CREAT|O_TRUNC|O_RDWR,
-                         0600, &taplogctx, NULL);
+       tdb = tdb1_open("run-traverse-in-transaction.tdb",
+                       1024, TDB_DEFAULT, O_CREAT|O_TRUNC|O_RDWR,
+                       0600, &tap_log_attr);
        ok1(tdb);
 
        key.dsize = strlen("hi");
index 90e16832f4507a7d8dd8eeb3635416ed70f5f308..54524fc1ff6c30024494404eb61f53836bc350fd 100644 (file)
@@ -30,14 +30,32 @@ int main(int argc, char *argv[])
        struct tdb_context *tdb;
        unsigned int log_count;
        TDB_DATA d;
-       struct tdb1_logging_context log_ctx = { log_fn, &log_count };
+       union tdb_attribute log_attr, jhash_attr, ohash_attr,
+               incompat_hash_attr;
+
+       log_attr.base.attr = TDB_ATTRIBUTE_LOG;
+       log_attr.base.next = NULL;
+       log_attr.log.fn = log_fn;
+       log_attr.log.data = &log_count;
+
+       jhash_attr.base.attr = TDB_ATTRIBUTE_HASH;
+       jhash_attr.base.next = &log_attr;
+       jhash_attr.hash.fn = jenkins_hashfn;
+
+       ohash_attr.base.attr = TDB_ATTRIBUTE_HASH;
+       ohash_attr.base.next = &log_attr;
+       ohash_attr.hash.fn = old_hash;
+
+       incompat_hash_attr.base.attr = TDB_ATTRIBUTE_HASH;
+       incompat_hash_attr.base.next = &log_attr;
+       incompat_hash_attr.hash.fn = tdb1_incompatible_hash;
 
        plan_tests(28);
 
        /* Create with default hash. */
        log_count = 0;
-       tdb = tdb1_open_ex("run-wronghash-fail.tdb", 0, 0,
-                         O_CREAT|O_RDWR|O_TRUNC, 0600, &log_ctx, NULL);
+       tdb = tdb1_open("run-wronghash-fail.tdb", 0, 0,
+                       O_CREAT|O_RDWR|O_TRUNC, 0600, &log_attr);
        ok1(tdb);
        ok1(log_count == 0);
        d.dptr = (void *)"Hello";
@@ -46,51 +64,51 @@ int main(int argc, char *argv[])
        tdb1_close(tdb);
 
        /* Fail to open with different hash. */
-       tdb = tdb1_open_ex("run-wronghash-fail.tdb", 0, 0, O_RDWR, 0,
-                         &log_ctx, jenkins_hashfn);
+       tdb = tdb1_open("run-wronghash-fail.tdb", 0, 0, O_RDWR, 0,
+                       &jhash_attr);
        ok1(!tdb);
        ok1(log_count == 1);
 
        /* Create with different hash. */
        log_count = 0;
-       tdb = tdb1_open_ex("run-wronghash-fail.tdb", 0, 0,
-                         O_CREAT|O_RDWR|O_TRUNC,
-                         0600, &log_ctx, jenkins_hashfn);
+       tdb = tdb1_open("run-wronghash-fail.tdb", 0, 0,
+                       O_CREAT|O_RDWR|O_TRUNC,
+                       0600, &jhash_attr);
        ok1(tdb);
        ok1(log_count == 0);
        tdb1_close(tdb);
 
        /* Endian should be no problem. */
        log_count = 0;
-       tdb = tdb1_open_ex("test/jenkins-le-hash.tdb1", 0, 0, O_RDWR, 0,
-                         &log_ctx, old_hash);
+       tdb = tdb1_open("test/jenkins-le-hash.tdb1", 0, 0, O_RDWR, 0,
+                       &ohash_attr);
        ok1(!tdb);
        ok1(log_count == 1);
 
        log_count = 0;
-       tdb = tdb1_open_ex("test/jenkins-be-hash.tdb1", 0, 0, O_RDWR, 0,
-                         &log_ctx, old_hash);
+       tdb = tdb1_open("test/jenkins-be-hash.tdb1", 0, 0, O_RDWR, 0,
+                       &ohash_attr);
        ok1(!tdb);
        ok1(log_count == 1);
 
        log_count = 0;
        /* Fail to open with old default hash. */
-       tdb = tdb1_open_ex("run-wronghash-fail.tdb", 0, 0, O_RDWR, 0,
-                         &log_ctx, old_hash);
+       tdb = tdb1_open("run-wronghash-fail.tdb", 0, 0, O_RDWR, 0,
+                       &ohash_attr);
        ok1(!tdb);
        ok1(log_count == 1);
 
        log_count = 0;
-       tdb = tdb1_open_ex("test/jenkins-le-hash.tdb1", 0, 0, O_RDONLY,
-                         0, &log_ctx, tdb1_incompatible_hash);
+       tdb = tdb1_open("test/jenkins-le-hash.tdb1", 0, 0, O_RDONLY,
+                       0, &incompat_hash_attr);
        ok1(tdb);
        ok1(log_count == 0);
        ok1(tdb1_check(tdb, NULL, NULL) == 0);
        tdb1_close(tdb);
 
        log_count = 0;
-       tdb = tdb1_open_ex("test/jenkins-be-hash.tdb1", 0, 0, O_RDONLY,
-                         0, &log_ctx, tdb1_incompatible_hash);
+       tdb = tdb1_open("test/jenkins-be-hash.tdb1", 0, 0, O_RDONLY,
+                       0, &incompat_hash_attr);
        ok1(tdb);
        ok1(log_count == 0);
        ok1(tdb1_check(tdb, NULL, NULL) == 0);
@@ -98,24 +116,24 @@ int main(int argc, char *argv[])
 
        /* It should open with jenkins hash if we don't specify. */
        log_count = 0;
-       tdb = tdb1_open_ex("test/jenkins-le-hash.tdb1", 0, 0, O_RDWR, 0,
-                         &log_ctx, NULL);
+       tdb = tdb1_open("test/jenkins-le-hash.tdb1", 0, 0, O_RDWR, 0,
+                       &log_attr);
        ok1(tdb);
        ok1(log_count == 0);
        ok1(tdb1_check(tdb, NULL, NULL) == 0);
        tdb1_close(tdb);
 
        log_count = 0;
-       tdb = tdb1_open_ex("test/jenkins-be-hash.tdb1", 0, 0, O_RDWR, 0,
-                         &log_ctx, NULL);
+       tdb = tdb1_open("test/jenkins-be-hash.tdb1", 0, 0, O_RDWR, 0,
+                       &log_attr);
        ok1(tdb);
        ok1(log_count == 0);
        ok1(tdb1_check(tdb, NULL, NULL) == 0);
        tdb1_close(tdb);
 
        log_count = 0;
-       tdb = tdb1_open_ex("run-wronghash-fail.tdb", 0, 0, O_RDONLY,
-                         0, &log_ctx, NULL);
+       tdb = tdb1_open("run-wronghash-fail.tdb", 0, 0, O_RDONLY,
+                       0, &log_attr);
        ok1(tdb);
        ok1(log_count == 0);
        ok1(tdb1_check(tdb, NULL, NULL) == 0);
index 80458728d648bedd5e5fd27fbad71e8397fbfe52..3541fe08c8abd93b832cf9359de813a456e5cb37 100644 (file)
@@ -2,7 +2,7 @@
 #include <ccan/tap/tap.h>
 #include <stdlib.h>
 #include <err.h>
-#include "tdb1-logging.h"
+#include "logging.h"
 
 int main(int argc, char *argv[])
 {
@@ -10,8 +10,8 @@ int main(int argc, char *argv[])
        TDB_DATA key, data;
 
        plan_tests(4);
-       tdb = tdb1_open_ex(NULL, 1024, TDB_INTERNAL, O_CREAT|O_TRUNC|O_RDWR,
-                         0600, &taplogctx, NULL);
+       tdb = tdb1_open(NULL, 1024, TDB_INTERNAL, O_CREAT|O_TRUNC|O_RDWR,
+                       0600, &tap_log_attr);
        ok1(tdb);
 
        /* Tickle bug on appending zero length buffer to zero length buffer. */
index 39b156b499989f3f30e391a5059398fe6aebd2c4..d206545c68fa1d0f47bdaf36b14fd6fbb8318145 100644 (file)
@@ -2,7 +2,7 @@
 #include <ccan/tap/tap.h>
 #include <stdlib.h>
 #include <err.h>
-#include "tdb1-logging.h"
+#include "logging.h"
 
 int main(int argc, char *argv[])
 {
@@ -10,8 +10,8 @@ int main(int argc, char *argv[])
        TDB_DATA key, data;
 
        plan_tests(10);
-       tdb = tdb1_open_ex("run.tdb", 1024, TDB_DEFAULT,
-                         O_CREAT|O_TRUNC|O_RDWR, 0600, &taplogctx, NULL);
+       tdb = tdb1_open("run.tdb", 1024, TDB_DEFAULT,
+                       O_CREAT|O_TRUNC|O_RDWR, 0600, &tap_log_attr);
 
        ok1(tdb);
        key.dsize = strlen("hi");
index 08c024b5cc93936fff7f215b51fcb9a716ff2c52..dcd825dc82cf8568e727b9905f966d83fd6490b3 100644 (file)
@@ -1,6 +1,6 @@
 #include "tdb1-external-agent.h"
 #include "tdb1-lock-tracking.h"
-#include "tdb1-logging.h"
+#include "logging.h"
 #include <sys/types.h>
 #include <sys/wait.h>
 #include <unistd.h>
@@ -39,8 +39,8 @@ static enum agent_return do_operation(enum operation op, const char *name)
                        diag("Already have tdb %s open", tdb->name);
                        return OTHER_FAILURE;
                }
-               tdb = tdb1_open_ex(name, 0, TDB_DEFAULT, O_RDWR, 0,
-                                 &taplogctx, NULL);
+               tdb = tdb1_open(name, 0, TDB_DEFAULT, O_RDWR, 0,
+                               &tap_log_attr);
                if (!tdb) {
                        if (!locking_would_block1)
                                diag("Opening tdb gave %s", strerror(errno));
diff --git a/lib/tdb2/test/tdb1-logging.c b/lib/tdb2/test/tdb1-logging.c
deleted file mode 100644 (file)
index cb05829..0000000
+++ /dev/null
@@ -1,25 +0,0 @@
-#include "tdb1-logging.h"
-#include <ccan/tap/tap.h>
-#include <stdarg.h>
-#include <stdlib.h>
-#include <string.h>
-#include <stdio.h>
-
-/* Turn log messages into tap diag messages. */
-static void taplog(struct tdb_context *tdb,
-                  enum tdb_log_level level,
-                  enum TDB_ERROR ecode,
-                  const char *message,
-                  void *data)
-{
-       if (suppress_logging)
-               return;
-
-       /* Strip trailing \n: diag adds it. */
-       if (message[0] && message[strlen(message)-1] == '\n')
-               diag("%s%.*s", log_prefix, (unsigned)strlen(message)-1, message);
-       else
-               diag("%s%s", log_prefix, message);
-}
-
-struct tdb1_logging_context taplogctx = { taplog, NULL };
diff --git a/lib/tdb2/test/tdb1-logging.h b/lib/tdb2/test/tdb1-logging.h
deleted file mode 100644 (file)
index 128ec7f..0000000
+++ /dev/null
@@ -1,10 +0,0 @@
-#ifndef TDB_TEST_LOGGING_H
-#define TDB_TEST_LOGGING_H
-#include <ccan/tdb2/tdb1.h>
-#include <stdbool.h>
-
-extern bool suppress_logging;
-extern const char *log_prefix;
-extern struct tdb1_logging_context taplogctx;
-
-#endif /* TDB_TEST_LOGGING_H */