From 6fdff621a98f161701f79b3da64e461feaa21952 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Thu, 17 Mar 2011 22:12:20 +1030 Subject: [PATCH] tdb2: tdb_name and tdb_fd functions. As per TDB1, with one enhancement: a non-NULL name argument passed to tdb_open() with the TDB_INTERNAL flag is preserved so you can identify internal TDBs too. --- ccan/tdb2/doc/TDB1_porting.txt | 2 ++ ccan/tdb2/open.c | 10 ++++++++++ ccan/tdb2/tdb.c | 9 +++++++++ ccan/tdb2/tdb2.h | 20 +++++++++++++++++++ ccan/tdb2/test/run-80-tdb_fd.c | 35 ++++++++++++++++++++++++++++++++++ 5 files changed, 76 insertions(+) create mode 100644 ccan/tdb2/test/run-80-tdb_fd.c diff --git a/ccan/tdb2/doc/TDB1_porting.txt b/ccan/tdb2/doc/TDB1_porting.txt index 4a6e9244..b56a426e 100644 --- a/ccan/tdb2/doc/TDB1_porting.txt +++ b/ccan/tdb2/doc/TDB1_porting.txt @@ -25,3 +25,5 @@ Interface differences between TDB1 and TDB2. TDB_LOG_WARNING. - tdb2 provides tdb_deq() for comparing two struct tdb_data. + +- tdb2's tdb_name() returns a copy of the name even for TDB_INTERNAL dbs. diff --git a/ccan/tdb2/open.c b/ccan/tdb2/open.c index 6d930be1..473dc98b 100644 --- a/ccan/tdb2/open.c +++ b/ccan/tdb2/open.c @@ -276,6 +276,16 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags, if (ecode != TDB_SUCCESS) { goto fail; } + if (name) { + tdb->name = strdup(name); + if (!tdb->name) { + ecode = tdb_logerr(tdb, TDB_ERR_OOM, + TDB_LOG_ERROR, + "tdb_open: failed to" + " allocate name"); + goto fail; + } + } tdb_convert(tdb, &hdr.hash_seed, sizeof(hdr.hash_seed)); tdb->hash_seed = hdr.hash_seed; tdb_ftable_init(tdb); diff --git a/ccan/tdb2/tdb.c b/ccan/tdb2/tdb.c index 6a2c3828..79ccd0f1 100644 --- a/ccan/tdb2/tdb.c +++ b/ccan/tdb2/tdb.c @@ -428,3 +428,12 @@ enum TDB_ERROR tdb_parse_record_(struct tdb_context *tdb, return ecode; } +const char *tdb_name(const struct tdb_context *tdb) +{ + return tdb->name; +} + +int tdb_fd(const struct tdb_context *tdb) +{ + return tdb->file->fd; +} diff --git a/ccan/tdb2/tdb2.h b/ccan/tdb2/tdb2.h index d9e2d9ec..f934a7bf 100644 --- a/ccan/tdb2/tdb2.h +++ b/ccan/tdb2/tdb2.h @@ -466,6 +466,26 @@ void tdb_add_flag(struct tdb_context *tdb, unsigned flag); */ void tdb_remove_flag(struct tdb_context *tdb, unsigned flag); +/** + * tdb_name - get the name of a tdb + * @tdb: the tdb context returned from tdb_open() + * + * This returns a copy of the name string, made at tdb_open() time. If that + * argument was NULL (possible for a TDB_INTERNAL db) this will return NULL. + * + * This is mostly useful for logging. + */ +const char *tdb_name(const struct tdb_context *tdb); + +/** + * tdb_fd - get the file descriptor of a tdb + * @tdb: the tdb context returned from tdb_open() + * + * This returns the file descriptor for the underlying database file, or -1 + * for TDB_INTERNAL. + */ +int tdb_fd(const struct tdb_context *tdb); + /** * enum tdb_attribute_type - descriminator for union tdb_attribute. */ diff --git a/ccan/tdb2/test/run-80-tdb_fd.c b/ccan/tdb2/test/run-80-tdb_fd.c new file mode 100644 index 00000000..ddfcb3e5 --- /dev/null +++ b/ccan/tdb2/test/run-80-tdb_fd.c @@ -0,0 +1,35 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "logging.h" + +int main(int argc, char *argv[]) +{ + unsigned int i; + struct tdb_context *tdb; + int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP, + TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT, + TDB_NOMMAP|TDB_CONVERT }; + + plan_tests(sizeof(flags) / sizeof(flags[0]) * 3); + for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) { + tdb = tdb_open("run-new_database.tdb", flags[i], + O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr); + if (!ok1(tdb)) + continue; + + if (flags[i] & TDB_INTERNAL) + ok1(tdb_fd(tdb) == -1); + else + ok1(tdb_fd(tdb) > 2); + tdb_close(tdb); + ok1(tap_log_messages == 0); + } + return exit_status(); +} -- 2.39.2