dbwrap: add enum dbwrap_req_state
[kai/samba-autobuild/.git] / lib / dbwrap / dbwrap_tdb.c
index a6176b75c4712a7bbbc8ab11154db03dc05a1fa6..e12ec4405d604a5fe1698f424f4493a403aebd8b 100644 (file)
@@ -24,7 +24,7 @@
 #include "lib/tdb_wrap/tdb_wrap.h"
 #include "lib/util/util_tdb.h"
 #include "system/filesys.h"
-#include "ccan/str/str.h"
+#include "lib/param/param.h"
 
 struct db_tdb_ctx {
        struct tdb_wrap *wtdb;
@@ -42,10 +42,11 @@ static void db_tdb_log_key(const char *prefix, TDB_DATA key)
 {
        size_t len;
        char *keystr;
-
+       TALLOC_CTX *frame;
        if (DEBUGLEVEL < 10) {
                return;
        }
+       frame = talloc_stackframe();
        len = key.dsize;
        if (DEBUGLEVEL == 10) {
                /*
@@ -53,10 +54,10 @@ static void db_tdb_log_key(const char *prefix, TDB_DATA key)
                 */
                len = MIN(10, key.dsize);
        }
-       keystr = hex_encode_talloc(talloc_tos(), (unsigned char *)(key.dptr),
+       keystr = hex_encode_talloc(frame, (unsigned char *)(key.dptr),
                                   len);
        DEBUG(10, ("%s key %s\n", prefix, keystr));
-       TALLOC_FREE(keystr);
+       TALLOC_FREE(frame);
 }
 
 static int db_tdb_record_destr(struct db_record* data)
@@ -135,7 +136,7 @@ static struct db_record *db_tdb_fetch_locked_internal(
 
        talloc_set_destructor(state.result, db_tdb_record_destr);
 
-       state.result->private_data = talloc_reference(state.result, ctx);
+       state.result->private_data = ctx;
        state.result->store = db_tdb_store;
        state.result->delete_rec = db_tdb_delete;
 
@@ -158,21 +159,6 @@ static struct db_record *db_tdb_fetch_locked(
        return db_tdb_fetch_locked_internal(db, mem_ctx, key);
 }
 
-static struct db_record *db_tdb_fetch_locked_timeout(
-       struct db_context *db, TALLOC_CTX *mem_ctx, TDB_DATA key,
-       unsigned int timeout)
-{
-       struct db_tdb_ctx *ctx = talloc_get_type_abort(db->private_data,
-                                                      struct db_tdb_ctx);
-
-       db_tdb_log_key("Locking with timeout ", key);
-       if (tdb_chainlock_with_timeout(ctx->wtdb->tdb, key, timeout) != 0) {
-               DEBUG(3, ("tdb_chainlock_with_timeout failed\n"));
-               return NULL;
-       }
-       return db_tdb_fetch_locked_internal(db, mem_ctx, key);
-}
-
 static struct db_record *db_tdb_try_fetch_locked(
        struct db_context *db, TALLOC_CTX *mem_ctx, TDB_DATA key)
 {
@@ -402,31 +388,29 @@ static int db_tdb_transaction_cancel(struct db_context *db)
        return 0;
 }
 
-static void db_tdb_id(struct db_context *db, const uint8_t **id, size_t *idlen)
+static size_t db_tdb_id(struct db_context *db, uint8_t *id, size_t idlen)
 {
        struct db_tdb_ctx *db_ctx =
                talloc_get_type_abort(db->private_data, struct db_tdb_ctx);
-       *id = (uint8_t *)&db_ctx->id;
-       *idlen = sizeof(db_ctx->id);
+
+       if (idlen >= sizeof(db_ctx->id)) {
+               memcpy(id, &db_ctx->id, sizeof(db_ctx->id));
+       }
+
+       return sizeof(db_ctx->id);
 }
 
 struct db_context *db_open_tdb(TALLOC_CTX *mem_ctx,
-                              struct loadparm_context *lp_ctx,
                               const char *name,
                               int hash_size, int tdb_flags,
                               int open_flags, mode_t mode,
-                              enum dbwrap_lock_order lock_order)
+                              enum dbwrap_lock_order lock_order,
+                              uint64_t dbwrap_flags)
 {
        struct db_context *result = NULL;
        struct db_tdb_ctx *db_tdb;
        struct stat st;
 
-       /* Extra paranoia. */
-       if (name && strends(name, ".ntdb")) {
-               DEBUG(0, ("can't try to open %s with tdb!\n", name));
-               return NULL;
-       }
-
        result = talloc_zero(mem_ctx, struct db_context);
        if (result == NULL) {
                DEBUG(0, ("talloc failed\n"));
@@ -441,7 +425,7 @@ struct db_context *db_open_tdb(TALLOC_CTX *mem_ctx,
        result->lock_order = lock_order;
 
        db_tdb->wtdb = tdb_wrap_open(db_tdb, name, hash_size, tdb_flags,
-                                    open_flags, mode, lp_ctx);
+                                    open_flags, mode);
        if (db_tdb->wtdb == NULL) {
                DEBUG(3, ("Could not open tdb: %s\n", strerror(errno)));
                goto fail;
@@ -457,7 +441,6 @@ struct db_context *db_open_tdb(TALLOC_CTX *mem_ctx,
        db_tdb->id.ino = st.st_ino;
 
        result->fetch_locked = db_tdb_fetch_locked;
-       result->fetch_locked_timeout = db_tdb_fetch_locked_timeout;
        result->try_fetch_locked = db_tdb_try_fetch_locked;
        result->traverse = db_tdb_traverse;
        result->traverse_read = db_tdb_traverse_read;
@@ -472,13 +455,10 @@ struct db_context *db_open_tdb(TALLOC_CTX *mem_ctx,
        result->wipe = db_tdb_wipe;
        result->id = db_tdb_id;
        result->check = db_tdb_check;
-       result->stored_callback = NULL;
        result->name = tdb_name(db_tdb->wtdb->tdb);
        return result;
 
  fail:
-       if (result != NULL) {
-               TALLOC_FREE(result);
-       }
+       TALLOC_FREE(result);
        return NULL;
 }