Browse Source

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.
Rusty Russell 15 years ago
parent
commit
6fdff621a9
5 changed files with 76 additions and 0 deletions
  1. 2 0
      ccan/tdb2/doc/TDB1_porting.txt
  2. 10 0
      ccan/tdb2/open.c
  3. 9 0
      ccan/tdb2/tdb.c
  4. 20 0
      ccan/tdb2/tdb2.h
  5. 35 0
      ccan/tdb2/test/run-80-tdb_fd.c

+ 2 - 0
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.

+ 10 - 0
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);

+ 9 - 0
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;
+}

+ 20 - 0
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.
  */

+ 35 - 0
ccan/tdb2/test/run-80-tdb_fd.c

@@ -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();
+}