From a446f1d4d161d66bbb19ba2551cf6429a4865964 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 31 Aug 2011 15:31:06 +0930 Subject: [PATCH] tdb2: make tdb1_open use attributes for logging, hash function. This brings it closer to tdb_open(), so we can unify more easily. --- ccan/tdb2/tdb1.h | 17 +--- ccan/tdb2/tdb1_open.c | 54 +++++++++---- ccan/tdb2/tdb1_tdb.c | 2 +- ccan/tdb2/test/run-tdb1-3G-file.c | 6 +- ccan/tdb2/test/run-tdb1-bad-tdb-header.c | 18 ++--- ccan/tdb2/test/run-tdb1-check.c | 22 +++--- ccan/tdb2/test/run-tdb1-corrupt.c | 10 +-- .../test/run-tdb1-die-during-transaction.c | 6 +- ccan/tdb2/test/run-tdb1-endian.c | 12 +-- ccan/tdb2/test/run-tdb1-incompatible.c | 79 ++++++++++++------- ccan/tdb2/test/run-tdb1-nested-transactions.c | 12 +-- ccan/tdb2/test/run-tdb1-nested-traverse.c | 6 +- .../test/run-tdb1-no-lock-during-traverse.c | 8 +- ccan/tdb2/test/run-tdb1-oldhash.c | 23 +++--- .../test/run-tdb1-open-during-transaction.c | 8 +- ccan/tdb2/test/run-tdb1-readonly-check.c | 12 +-- ccan/tdb2/test/run-tdb1-rwlock-check.c | 15 ++-- ccan/tdb2/test/run-tdb1-summary.c | 2 +- .../test/run-tdb1-traverse-in-transaction.c | 8 +- ccan/tdb2/test/run-tdb1-wronghash-fail.c | 66 ++++++++++------ ccan/tdb2/test/run-tdb1-zero-append.c | 6 +- ccan/tdb2/test/run-tdb1.c | 6 +- ccan/tdb2/test/tdb1-external-agent.c | 6 +- ccan/tdb2/test/tdb1-logging.c | 25 ------ ccan/tdb2/test/tdb1-logging.h | 10 --- 25 files changed, 229 insertions(+), 210 deletions(-) delete mode 100644 ccan/tdb2/test/tdb1-logging.c delete mode 100644 ccan/tdb2/test/tdb1-logging.h diff --git a/ccan/tdb2/tdb1.h b/ccan/tdb2/tdb1.h index 78d42b39..a2a0afe8 100644 --- a/ccan/tdb2/tdb1.h +++ b/ccan/tdb2/tdb1.h @@ -37,23 +37,10 @@ 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); diff --git a/ccan/tdb2/tdb1_open.c b/ccan/tdb2/tdb1_open.c index 5d0097b2..1f23db01 100644 --- a/ccan/tdb2/tdb1_open.c +++ b/ccan/tdb2/tdb1_open.c @@ -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 */ diff --git a/ccan/tdb2/tdb1_tdb.c b/ccan/tdb2/tdb1_tdb.c index 400c9866..e9696000 100644 --- a/ccan/tdb2/tdb1_tdb.c +++ b/ccan/tdb2/tdb1_tdb.c @@ -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"); diff --git a/ccan/tdb2/test/run-tdb1-3G-file.c b/ccan/tdb2/test/run-tdb1-3G-file.c index d5aeda5d..1eab4476 100644 --- a/ccan/tdb2/test/run-tdb1-3G-file.c +++ b/ccan/tdb2/test/run-tdb1-3G-file.c @@ -4,7 +4,7 @@ #include #include #include -#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; diff --git a/ccan/tdb2/test/run-tdb1-bad-tdb-header.c b/ccan/tdb2/test/run-tdb1-bad-tdb-header.c index fe250108..a32b817c 100644 --- a/ccan/tdb2/test/run-tdb1-bad-tdb-header.c +++ b/ccan/tdb2/test/run-tdb1-bad-tdb-header.c @@ -2,7 +2,7 @@ #include #include #include -#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); diff --git a/ccan/tdb2/test/run-tdb1-check.c b/ccan/tdb2/test/run-tdb1-check.c index 9864e42c..017eb832 100644 --- a/ccan/tdb2/test/run-tdb1-check.c +++ b/ccan/tdb2/test/run-tdb1-check.c @@ -2,7 +2,7 @@ #include #include #include -#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); diff --git a/ccan/tdb2/test/run-tdb1-corrupt.c b/ccan/tdb2/test/run-tdb1-corrupt.c index 2e70da7c..833e9c1e 100644 --- a/ccan/tdb2/test/run-tdb1-corrupt.c +++ b/ccan/tdb2/test/run-tdb1-corrupt.c @@ -2,7 +2,7 @@ #include #include #include -#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(); diff --git a/ccan/tdb2/test/run-tdb1-die-during-transaction.c b/ccan/tdb2/test/run-tdb1-die-during-transaction.c index 1d03a30a..9ff75d10 100644 --- a/ccan/tdb2/test/run-tdb1-die-during-transaction.c +++ b/ccan/tdb2/test/run-tdb1-die-during-transaction.c @@ -18,7 +18,7 @@ static int ftruncate_check(int fd, off_t length); #include #include #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. */ diff --git a/ccan/tdb2/test/run-tdb1-endian.c b/ccan/tdb2/test/run-tdb1-endian.c index 245b6544..a06bfb6e 100644 --- a/ccan/tdb2/test/run-tdb1-endian.c +++ b/ccan/tdb2/test/run-tdb1-endian.c @@ -2,7 +2,7 @@ #include #include #include -#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"); diff --git a/ccan/tdb2/test/run-tdb1-incompatible.c b/ccan/tdb2/test/run-tdb1-incompatible.c index 9e581614..baf40ed4 100644 --- a/ccan/tdb2/test/run-tdb1-incompatible.c +++ b/ccan/tdb2/test/run-tdb1-incompatible.c @@ -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); diff --git a/ccan/tdb2/test/run-tdb1-nested-transactions.c b/ccan/tdb2/test/run-tdb1-nested-transactions.c index be4e9269..d5886123 100644 --- a/ccan/tdb2/test/run-tdb1-nested-transactions.c +++ b/ccan/tdb2/test/run-tdb1-nested-transactions.c @@ -3,7 +3,7 @@ #include #include #include -#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); diff --git a/ccan/tdb2/test/run-tdb1-nested-traverse.c b/ccan/tdb2/test/run-tdb1-nested-traverse.c index 3d15082a..1fc8d092 100644 --- a/ccan/tdb2/test/run-tdb1-nested-traverse.c +++ b/ccan/tdb2/test/run-tdb1-nested-traverse.c @@ -7,7 +7,7 @@ #include #include #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); diff --git a/ccan/tdb2/test/run-tdb1-no-lock-during-traverse.c b/ccan/tdb2/test/run-tdb1-no-lock-during-traverse.c index 10d57bac..cb1c2360 100644 --- a/ccan/tdb2/test/run-tdb1-no-lock-during-traverse.c +++ b/ccan/tdb2/test/run-tdb1-no-lock-during-traverse.c @@ -8,7 +8,7 @@ #include #include #include -#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)); diff --git a/ccan/tdb2/test/run-tdb1-oldhash.c b/ccan/tdb2/test/run-tdb1-oldhash.c index ea56dd3c..5219d5c3 100644 --- a/ccan/tdb2/test/run-tdb1-oldhash.c +++ b/ccan/tdb2/test/run-tdb1-oldhash.c @@ -2,36 +2,41 @@ #include #include #include -#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); diff --git a/ccan/tdb2/test/run-tdb1-open-during-transaction.c b/ccan/tdb2/test/run-tdb1-open-during-transaction.c index 3cb0685b..e0b28a53 100644 --- a/ccan/tdb2/test/run-tdb1-open-during-transaction.c +++ b/ccan/tdb2/test/run-tdb1-open-during-transaction.c @@ -18,7 +18,7 @@ static int ftruncate_check(int fd, off_t length); #include #include #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; diff --git a/ccan/tdb2/test/run-tdb1-readonly-check.c b/ccan/tdb2/test/run-tdb1-readonly-check.c index 2eda33d9..762c576f 100644 --- a/ccan/tdb2/test/run-tdb1-readonly-check.c +++ b/ccan/tdb2/test/run-tdb1-readonly-check.c @@ -4,7 +4,7 @@ #include #include #include -#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); diff --git a/ccan/tdb2/test/run-tdb1-rwlock-check.c b/ccan/tdb2/test/run-tdb1-rwlock-check.c index cd227f4c..6f4406c0 100644 --- a/ccan/tdb2/test/run-tdb1-rwlock-check.c +++ b/ccan/tdb2/test/run-tdb1-rwlock-check.c @@ -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); diff --git a/ccan/tdb2/test/run-tdb1-summary.c b/ccan/tdb2/test/run-tdb1-summary.c index ffd8bc8c..8b9b74ff 100644 --- a/ccan/tdb2/test/run-tdb1-summary.c +++ b/ccan/tdb2/test/run-tdb1-summary.c @@ -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; diff --git a/ccan/tdb2/test/run-tdb1-traverse-in-transaction.c b/ccan/tdb2/test/run-tdb1-traverse-in-transaction.c index 243f2d65..0395bff7 100644 --- a/ccan/tdb2/test/run-tdb1-traverse-in-transaction.c +++ b/ccan/tdb2/test/run-tdb1-traverse-in-transaction.c @@ -8,7 +8,7 @@ #include #include #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"); diff --git a/ccan/tdb2/test/run-tdb1-wronghash-fail.c b/ccan/tdb2/test/run-tdb1-wronghash-fail.c index 90e16832..54524fc1 100644 --- a/ccan/tdb2/test/run-tdb1-wronghash-fail.c +++ b/ccan/tdb2/test/run-tdb1-wronghash-fail.c @@ -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); diff --git a/ccan/tdb2/test/run-tdb1-zero-append.c b/ccan/tdb2/test/run-tdb1-zero-append.c index 80458728..3541fe08 100644 --- a/ccan/tdb2/test/run-tdb1-zero-append.c +++ b/ccan/tdb2/test/run-tdb1-zero-append.c @@ -2,7 +2,7 @@ #include #include #include -#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. */ diff --git a/ccan/tdb2/test/run-tdb1.c b/ccan/tdb2/test/run-tdb1.c index 39b156b4..d206545c 100644 --- a/ccan/tdb2/test/run-tdb1.c +++ b/ccan/tdb2/test/run-tdb1.c @@ -2,7 +2,7 @@ #include #include #include -#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"); diff --git a/ccan/tdb2/test/tdb1-external-agent.c b/ccan/tdb2/test/tdb1-external-agent.c index 08c024b5..dcd825dc 100644 --- a/ccan/tdb2/test/tdb1-external-agent.c +++ b/ccan/tdb2/test/tdb1-external-agent.c @@ -1,6 +1,6 @@ #include "tdb1-external-agent.h" #include "tdb1-lock-tracking.h" -#include "tdb1-logging.h" +#include "logging.h" #include #include #include @@ -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/ccan/tdb2/test/tdb1-logging.c b/ccan/tdb2/test/tdb1-logging.c deleted file mode 100644 index cb058296..00000000 --- a/ccan/tdb2/test/tdb1-logging.c +++ /dev/null @@ -1,25 +0,0 @@ -#include "tdb1-logging.h" -#include -#include -#include -#include -#include - -/* 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/ccan/tdb2/test/tdb1-logging.h b/ccan/tdb2/test/tdb1-logging.h deleted file mode 100644 index 128ec7f8..00000000 --- a/ccan/tdb2/test/tdb1-logging.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef TDB_TEST_LOGGING_H -#define TDB_TEST_LOGGING_H -#include -#include - -extern bool suppress_logging; -extern const char *log_prefix; -extern struct tdb1_logging_context taplogctx; - -#endif /* TDB_TEST_LOGGING_H */ -- 2.39.2