]> git.ozlabs.org Git - ccan/commitdiff
tdb2: tdb_name and tdb_fd functions.
authorRusty Russell <rusty@rustcorp.com.au>
Thu, 17 Mar 2011 11:42:20 +0000 (22:12 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Thu, 17 Mar 2011 11:42:20 +0000 (22:12 +1030)
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
ccan/tdb2/open.c
ccan/tdb2/tdb.c
ccan/tdb2/tdb2.h
ccan/tdb2/test/run-80-tdb_fd.c [new file with mode: 0644]

index 4a6e924411302655db1d9a5facacd717839e9238..b56a426ed4cfd11548afaa0786b0c78cb2a73d0d 100644 (file)
@@ -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.
index 6d930be14f6f4b14faeb44e0beae7dbc3a59b1e1..473dc98b7e4c68d78da0be268c5434ab70042b3a 100644 (file)
@@ -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);
index 6a2c3828a6e47defd2a26563ce1fa4b26f8bee1b..79ccd0f1c260cb476b35c146f15a9438f843a673 100644 (file)
@@ -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;
+}
index d9e2d9ecb520acd4e97468676c45051e109b4dcf..f934a7bf3baa70cb9fe27aa1731fe7dae771ba73 100644 (file)
@@ -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 (file)
index 0000000..ddfcb3e
--- /dev/null
@@ -0,0 +1,35 @@
+#include <ccan/tdb2/tdb.c>
+#include <ccan/tdb2/open.c>
+#include <ccan/tdb2/free.c>
+#include <ccan/tdb2/lock.c>
+#include <ccan/tdb2/io.c>
+#include <ccan/tdb2/hash.c>
+#include <ccan/tdb2/transaction.c>
+#include <ccan/tdb2/check.c>
+#include <ccan/tap/tap.h>
+#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();
+}