tdb_compat: use tdb_open_compat.
authorRusty Russell <rusty@rustcorp.com.au>
Mon, 20 Jun 2011 09:10:32 +0000 (18:40 +0930)
committerRusty Russell <rusty@rustcorp.com.au>
Mon, 20 Jun 2011 09:18:35 +0000 (11:18 +0200)
This is a helper for the common case of opening a tdb with a logging
function, but it doesn't do all the work, since TDB1 and TDB2's log
functions are different types.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
lib/util/tdb_wrap.c
source3/lib/util_tdb.c
source3/libsmb/smb_share_modes.c
source3/printing/tests/vlp.c
source3/smbd/mangle_hash.c
source3/torture/mangle_test.c
source4/lib/ldb/ldb_tdb/ldb_index.c
source4/lib/ldb/ldb_tdb/ldb_tdb_wrap.c
source4/torture/basic/mangle_test.c
testsuite/printing/psec.c

index b19b4fe67f0b87675a67f1444b7450241e6fef04..71aea5e36cb469edec13b1338006877aacf7d6b9 100644 (file)
 #include "lib/util/dlinklist.h"
 #include "lib/util/tdb_wrap.h"
 
+/* FIXME: TDB2 does this internally, so no need to wrap multiple opens! */
+#if BUILD_TDB2
+static void tdb_wrap_log(struct tdb_context *tdb,
+                        enum tdb_log_level level,
+                        const char *message,
+                        void *unused)
+{
+       int dl;
+       const char *name = tdb_name(tdb);
+
+       switch (level) {
+       case TDB_LOG_USE_ERROR:
+       case TDB_LOG_ERROR:
+               dl = 0;
+               break;
+       case TDB_LOG_WARNING:
+               dl = 2;
+               break;
+       default:
+               dl = 0;
+       }
+
+       DEBUG(dl, ("tdb(%s): %s", name ? name : "unnamed", message));
+}
+#else
 /*
  Log tdb messages via DEBUG().
 */
@@ -64,6 +89,7 @@ static void tdb_wrap_log(TDB_CONTEXT *tdb, enum tdb_debug_level level,
                free(ptr);
        }
 }
+#endif
 
 struct tdb_wrap_private {
        struct tdb_context *tdb;
@@ -89,7 +115,6 @@ static struct tdb_wrap_private *tdb_wrap_private_open(TALLOC_CTX *mem_ctx,
                                                      mode_t mode)
 {
        struct tdb_wrap_private *result;
-       struct tdb_logging_context log_ctx;
 
        result = talloc(mem_ctx, struct tdb_wrap_private);
        if (result == NULL) {
@@ -100,8 +125,6 @@ static struct tdb_wrap_private *tdb_wrap_private_open(TALLOC_CTX *mem_ctx,
                goto fail;
        }
 
-       log_ctx.log_fn = tdb_wrap_log;
-
 #if _SAMBA_BUILD_ == 3 
        /* This #if _SAMBA_BUILD == 3 is very unfortunate, as it means
         * that in the top level build, these options are not
@@ -126,8 +149,8 @@ static struct tdb_wrap_private *tdb_wrap_private_open(TALLOC_CTX *mem_ctx,
        }
 #endif
 
-       result->tdb = tdb_open_ex(name, hash_size, tdb_flags,
-                                 open_flags, mode, &log_ctx, NULL);
+       result->tdb = tdb_open_compat(name, hash_size, tdb_flags,
+                                     open_flags, mode, tdb_wrap_log, NULL);
        if (result->tdb == NULL) {
                goto fail;
        }
index 92c43d86057366e218a2ed506fab1dc5cedc12d9..2d49b9e2ec3858533693eaf55eaa5f832df7f3af 100644 (file)
@@ -372,6 +372,14 @@ int tdb_unpack(const uint8 *buf, int bufsize, const char *fmt, ...)
  Log tdb messages via DEBUG().
 ****************************************************************************/
 
+#ifdef BUILD_TDB2
+static void tdb_log(TDB_CONTEXT *tdb, enum tdb_log_level level,
+                   const char *message, void *unused)
+{
+       DEBUG((int)level, ("tdb(%s): %s",
+                          tdb_name(tdb) ? tdb_name(tdb) : "unnamed", message));
+}
+#else
 static void tdb_log(TDB_CONTEXT *tdb, enum tdb_debug_level level, const char *format, ...)
 {
        va_list ap;
@@ -388,6 +396,7 @@ static void tdb_log(TDB_CONTEXT *tdb, enum tdb_debug_level level, const char *fo
        DEBUG((int)level, ("tdb(%s): %s", tdb_name(tdb) ? tdb_name(tdb) : "unnamed", ptr));
        SAFE_FREE(ptr);
 }
+#endif /* TDB1 */
 
 /****************************************************************************
  Like tdb_open() but also setup a logging function that redirects to
@@ -398,14 +407,10 @@ TDB_CONTEXT *tdb_open_log(const char *name, int hash_size, int tdb_flags,
                          int open_flags, mode_t mode)
 {
        TDB_CONTEXT *tdb;
-       struct tdb_logging_context log_ctx;
 
        if (!lp_use_mmap())
                tdb_flags |= TDB_NOMMAP;
 
-       log_ctx.log_fn = tdb_log;
-       log_ctx.log_private = NULL;
-
        if ((hash_size == 0) && (name != NULL)) {
                const char *base = strrchr_m(name, '/');
                if (base != NULL) {
@@ -417,8 +422,8 @@ TDB_CONTEXT *tdb_open_log(const char *name, int hash_size, int tdb_flags,
                hash_size = lp_parm_int(-1, "tdb_hashsize", base, 0);
        }
 
-       tdb = tdb_open_ex(name, hash_size, tdb_flags, 
-                         open_flags, mode, &log_ctx, NULL);
+       tdb = tdb_open_compat(name, hash_size, tdb_flags,
+                             open_flags, mode, tdb_log, NULL);
        if (!tdb)
                return NULL;
 
index a4a54972c90ade3fbfa524886e815eed4dee7687..7c0a6d2a4e67b046e9641d6dffe89cbaf7f6176b 100644 (file)
@@ -69,10 +69,12 @@ struct smbdb_ctx *smb_share_mode_db_open(const char *db_path)
 
        memset(smb_db, '\0', sizeof(struct smbdb_ctx));
 
-       smb_db->smb_tdb = tdb_open(db_path,
-                               0, TDB_DEFAULT|TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH,
-                               O_RDWR|O_CREAT,
-                               0644);
+       /* FIXME: We should *never* open a tdb without logging! */
+       smb_db->smb_tdb = tdb_open_compat(db_path,
+                                         0, TDB_DEFAULT|TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH,
+                                         O_RDWR|O_CREAT,
+                                         0644,
+                                         NULL, NULL);
 
        if (!smb_db->smb_tdb) {
                free(smb_db);
index 927224cb2ee10df975609786f96de7252886f1be..66860e5ada31efc59c73934bb382d7bd2b9df62c 100644 (file)
@@ -392,8 +392,9 @@ int main(int argc, char **argv)
                return 1;
        }
 
-       if (!(tdb = tdb_open(printdb_path, 0, 0, O_RDWR | O_CREAT,
-                            0666))) {
+       /* FIXME: We should *never* open a tdb without logging! */
+       if (!(tdb = tdb_open_compat(printdb_path, 0, 0, O_RDWR | O_CREAT,
+                                   0666, NULL, NULL))) {
                printf("%s: unable to open %s\n", argv[0], printdb_path);
                return 1;
        }
index 988251e87879e1a10d0da34ad452fbe1d17717d4..c196a13ed029486d0094a6b971a4e7e9b3277a20 100644 (file)
@@ -773,8 +773,13 @@ const struct mangle_fns *mangle_hash_init(void)
        mangle_reset();
 
        /* Create the in-memory tdb using our custom hash function. */
+#ifndef BUILD_TDB2
        tdb_mangled_cache = tdb_open_ex("mangled_cache", 1031, TDB_INTERNAL,
                                (O_RDWR|O_CREAT), 0644, NULL, fast_string_hash);
+#else
+       /* FIXME: We should *never* open a tdb without logging! */
+       tdb_mangled_cache = tdb_open("mangled_cache", TDB_INTERNAL, 0, 0, NULL);
+#endif
 
        return &mangle_hash_fns;
 }
index b9e3f6a8cf4e8c70eca46929e92964a53305f539..4fc91dc4b3af6e76c27ebd4186adc1f67f456399 100644 (file)
@@ -181,7 +181,7 @@ bool torture_mangle(int dummy)
        }
 
        /* we will use an internal tdb to store the names we have used */
-       tdb = tdb_open(NULL, 100000, TDB_INTERNAL, 0, 0);
+       tdb = tdb_open_compat(NULL, 100000, TDB_INTERNAL, 0, 0, NULL, NULL);
        if (!tdb) {
                printf("ERROR: Failed to open tdb\n");
                return False;
index 45c36f09c9f9d91c719f53b20b14046beaf0f916..24cc93feb94f486e5f5b6033f80c07676a1beef6 100644 (file)
@@ -261,7 +261,7 @@ static int ltdb_dn_list_store(struct ldb_module *module, struct ldb_dn *dn,
        }
 
        if (ltdb->idxptr->itdb == NULL) {
-               ltdb->idxptr->itdb = tdb_open(NULL, 1000, TDB_INTERNAL, O_RDWR, 0);
+               ltdb->idxptr->itdb = tdb_open_compat(NULL, 1000, TDB_INTERNAL, O_RDWR, 0, NULL, NULL);
                if (ltdb->idxptr->itdb == NULL) {
                        return LDB_ERR_OPERATIONS_ERROR;
                }
index b9f3e79f205342de2c809d16045bd1cf0d117e32..16a037a6c3364ee6db9a8cc76653d48a253a848f 100644 (file)
 #include "ldb_tdb.h"
 #include "dlinklist.h"
 
-/*
-  the purpose of this code is to work around the braindead posix locking
-  rules, to allow us to have a ldb open more than once while allowing 
-  locking to work
-*/
-
-struct ltdb_wrap {
-       struct ltdb_wrap *next, *prev;
-       struct tdb_context *tdb;
-       dev_t device;
-       ino_t inode;
-};
-
-static struct ltdb_wrap *tdb_list;
-
-/* destroy the last connection to a tdb */
-static int ltdb_wrap_destructor(struct ltdb_wrap *w)
+/* FIXME: TDB2 does this internally, so no need to wrap multiple opens! */
+#if BUILD_TDB2
+static void ltdb_log_fn(struct tdb_context *tdb,
+                       enum tdb_log_level level,
+                       const char *message,
+                       struct ldb_context *ldb)
 {
-       tdb_close(w->tdb);
-       DLIST_REMOVE(tdb_list, w);
-       return 0;
-}                               
+       enum ldb_debug_level ldb_level;
+       const char *name = tdb_name(tdb);
+
+       switch (level) {
+       case TDB_LOG_WARNING:
+               ldb_level = LDB_DEBUG_WARNING;
+       case TDB_LOG_USE_ERROR:
+       case TDB_LOG_ERROR:
+               ldb_level = LDB_DEBUG_FATAL;
+               break;
+       default:
+               ldb_level = LDB_DEBUG_FATAL;
+       }
 
+       ldb_debug(ldb, ldb_level, "ltdb: tdb(%s): %s", name, message);
+}
+#else /* !TDB2 */
 static void ltdb_log_fn(struct tdb_context *tdb, enum tdb_debug_level level, const char *fmt, ...) PRINTF_ATTRIBUTE(3, 4);
 static void ltdb_log_fn(struct tdb_context *tdb, enum tdb_debug_level level, const char *fmt, ...)
 {
@@ -83,6 +84,32 @@ static void ltdb_log_fn(struct tdb_context *tdb, enum tdb_debug_level level, con
        ldb_debug(ldb, ldb_level, "ltdb: tdb(%s): %s", name, message);
        talloc_free(message);
 }
+#endif
+
+/*
+  the purpose of this code is to work around the braindead posix locking
+  rules, to allow us to have a ldb open more than once while allowing
+  locking to work
+
+  TDB2 handles multiple opens, so we don't have this problem there.
+*/
+
+struct ltdb_wrap {
+       struct ltdb_wrap *next, *prev;
+       struct tdb_context *tdb;
+       dev_t device;
+       ino_t inode;
+};
+
+static struct ltdb_wrap *tdb_list;
+
+/* destroy the last connection to a tdb */
+static int ltdb_wrap_destructor(struct ltdb_wrap *w)
+{
+       tdb_close(w->tdb);
+       DLIST_REMOVE(tdb_list, w);
+       return 0;
+}
 
 /*
   wrapped connection to a tdb database. The caller should _not_ free
@@ -98,10 +125,6 @@ struct tdb_context *ltdb_wrap_open(TALLOC_CTX *mem_ctx,
 {
        struct ltdb_wrap *w;
        struct stat st;
-       struct tdb_logging_context log_ctx;
-
-       log_ctx.log_fn = ltdb_log_fn;
-       log_ctx.log_private = ldb;
 
        if (stat(path, &st) == 0) {
                for (w=tdb_list;w;w=w->next) {
@@ -119,7 +142,7 @@ struct tdb_context *ltdb_wrap_open(TALLOC_CTX *mem_ctx,
                return NULL;
        }
 
-       w->tdb = tdb_open_ex(path, hash_size, tdb_flags, open_flags, mode, &log_ctx, NULL);
+       w->tdb = tdb_open_compat(path, hash_size, tdb_flags, open_flags, mode, ltdb_log_fn, ldb);
        if (w->tdb == NULL) {
                talloc_free(w);
                return NULL;
@@ -140,4 +163,3 @@ struct tdb_context *ltdb_wrap_open(TALLOC_CTX *mem_ctx,
        
        return w->tdb;
 }
-
index a15dc00939bc95b4be06ca67518e51e225ae4886..d549a0c1eb3cf6e601da1eaf65beaae5e534b1b6 100644 (file)
@@ -167,7 +167,7 @@ bool torture_mangle(struct torture_context *torture,
        int i;
 
        /* we will use an internal tdb to store the names we have used */
-       tdb = tdb_open(NULL, 100000, TDB_INTERNAL, 0, 0);
+       tdb = tdb_open_compat(NULL, 100000, TDB_INTERNAL, 0, 0, NULL, NULL);
        if (!tdb) {
                printf("ERROR: Failed to open tdb\n");
                return false;
index 243861499669d082d11be4be5a867b1411421d0b..59205446d05e0c027b715ef5903be1b08138caba 100644 (file)
@@ -171,7 +171,7 @@ int psec_getsec(char *printer)
        slprintf(tdb_path, sizeof(tdb_path) - 1, "%s/ntdrivers.tdb", 
                 lp_lockdir());
 
-       tdb = tdb_open(tdb_path, 0, 0, O_RDONLY, 0600);
+       tdb = tdb_open_compat(tdb_path, 0, 0, O_RDONLY, 0600, NULL, NULL);
 
        if (!tdb) {
                printf("psec: failed to open nt drivers database: %s\n",
@@ -275,7 +275,7 @@ int psec_setsec(char *printer)
        slprintf(tdb_path, sizeof(tdb_path) - 1, "%s/ntdrivers.tdb", 
                 lp_lockdir());
 
-       tdb = tdb_open(tdb_path, 0, 0, O_RDWR, 0600);
+       tdb = tdb_open_compat(tdb_path, 0, 0, O_RDWR, 0600, NULL, NULL);
 
        if (!tdb) {
                printf("psec: failed to open nt drivers database: %s\n",