summaryrefslogtreecommitdiff
path: root/source3/lib/dbwrap_tdb.c
diff options
context:
space:
mode:
Diffstat (limited to 'source3/lib/dbwrap_tdb.c')
-rw-r--r--source3/lib/dbwrap_tdb.c132
1 files changed, 85 insertions, 47 deletions
diff --git a/source3/lib/dbwrap_tdb.c b/source3/lib/dbwrap_tdb.c
index b24fd0618a..83a0d111aa 100644
--- a/source3/lib/dbwrap_tdb.c
+++ b/source3/lib/dbwrap_tdb.c
@@ -43,33 +43,50 @@ static int db_tdb_record_destr(struct db_record* data)
return 0;
}
-static struct db_record *db_tdb_fetch_locked(struct db_context *db,
- TALLOC_CTX *mem_ctx, TDB_DATA key)
-{
- struct db_tdb_ctx *ctx = talloc_get_type_abort(db->private_data,
- struct db_tdb_ctx);
+struct tdb_fetch_locked_state {
+ TALLOC_CTX *mem_ctx;
struct db_record *result;
- TDB_DATA value;
+};
- result = TALLOC_P(mem_ctx, struct db_record);
- if (result == NULL) {
- DEBUG(0, ("talloc failed\n"));
- return NULL;
+static int db_tdb_fetchlock_parse(TDB_DATA key, TDB_DATA data,
+ void *private_data)
+{
+ struct tdb_fetch_locked_state *state =
+ (struct tdb_fetch_locked_state *)private_data;
+
+ state->result = (struct db_record *)talloc_size(
+ state->mem_ctx,
+ sizeof(struct db_record) + key.dsize + data.dsize);
+
+ if (state->result == NULL) {
+ return 0;
}
- result->key.dsize = key.dsize;
- result->key.dptr = (uint8 *)talloc_memdup(result, key.dptr, key.dsize);
- if (result->key.dptr == NULL) {
- DEBUG(0, ("talloc failed\n"));
- TALLOC_FREE(result);
- return NULL;
+ state->result->key.dsize = key.dsize;
+ state->result->key.dptr = ((uint8 *)state->result)
+ + sizeof(struct db_record);
+ memcpy(state->result->key.dptr, key.dptr, key.dsize);
+
+ state->result->value.dsize = data.dsize;
+
+ if (data.dsize > 0) {
+ state->result->value.dptr = state->result->key.dptr+key.dsize;
+ memcpy(state->result->value.dptr, data.dptr, data.dsize);
}
+ else {
+ state->result->value.dptr = NULL;
+ }
+
+ return 0;
+}
- result->value.dptr = NULL;
- result->value.dsize = 0;
- result->private_data = talloc_reference(result, ctx);
- result->store = db_tdb_store;
- result->delete_rec = db_tdb_delete;
+static struct db_record *db_tdb_fetch_locked(struct db_context *db,
+ TALLOC_CTX *mem_ctx, TDB_DATA key)
+{
+ struct db_tdb_ctx *ctx = talloc_get_type_abort(db->private_data,
+ struct db_tdb_ctx);
+ struct tdb_fetch_locked_state state;
+ int res;
if (DEBUGLEVEL >= 10) {
char *keystr = hex_encode(NULL, key.dptr, key.dsize);
@@ -81,32 +98,56 @@ static struct db_record *db_tdb_fetch_locked(struct db_context *db,
if (tdb_chainlock(ctx->wtdb->tdb, key) != 0) {
DEBUG(3, ("tdb_chainlock failed\n"));
- TALLOC_FREE(result);
return NULL;
}
- talloc_set_destructor(result, db_tdb_record_destr);
+ state.mem_ctx = mem_ctx;
+ state.result = NULL;
- value = tdb_fetch(ctx->wtdb->tdb, key);
+ res = tdb_parse_record(ctx->wtdb->tdb, key, db_tdb_fetchlock_parse,
+ &state);
- if (value.dptr == NULL) {
- return result;
+ if (state.result == NULL) {
+ db_tdb_fetchlock_parse(key, tdb_null, &state);
}
- result->value.dsize = value.dsize;
- result->value.dptr = (uint8 *)talloc_memdup(result, value.dptr,
- value.dsize);
- if (result->value.dptr == NULL) {
- DEBUG(3, ("talloc failed\n"));
- TALLOC_FREE(result);
+ if (state.result == NULL) {
+ tdb_chainunlock(ctx->wtdb->tdb, key);
return NULL;
}
- SAFE_FREE(value.dptr);
+ talloc_set_destructor(state.result, db_tdb_record_destr);
- DEBUG(10, ("Allocated locked data 0x%p\n", result));
+ state.result->private_data = talloc_reference(state.result, ctx);
+ state.result->store = db_tdb_store;
+ state.result->delete_rec = db_tdb_delete;
- return result;
+ DEBUG(10, ("Allocated locked data 0x%p\n", state.result));
+
+ return state.result;
+}
+
+struct tdb_fetch_state {
+ TALLOC_CTX *mem_ctx;
+ int result;
+ TDB_DATA data;
+};
+
+static int db_tdb_fetch_parse(TDB_DATA key, TDB_DATA data,
+ void *private_data)
+{
+ struct tdb_fetch_state *state =
+ (struct tdb_fetch_state *)private_data;
+
+ state->data.dptr = (uint8 *)talloc_memdup(state->mem_ctx, data.dptr,
+ data.dsize);
+ if (state->data.dptr == NULL) {
+ state->result = -1;
+ return 0;
+ }
+
+ state->data.dsize = data.dsize;
+ return 0;
}
static int db_tdb_fetch(struct db_context *db, TALLOC_CTX *mem_ctx,
@@ -115,23 +156,20 @@ static int db_tdb_fetch(struct db_context *db, TALLOC_CTX *mem_ctx,
struct db_tdb_ctx *ctx = talloc_get_type_abort(
db->private_data, struct db_tdb_ctx);
- TDB_DATA data;
-
- data = tdb_fetch(ctx->wtdb->tdb, key);
+ struct tdb_fetch_state state;
- if (data.dptr == NULL) {
- pdata->dptr = NULL;
- pdata->dsize = 0;
- return 0;
- }
+ state.mem_ctx = mem_ctx;
+ state.result = 0;
+ state.data.dptr = NULL;
+ state.data.dsize = 0;
- pdata->dptr = (uint8 *)talloc_memdup(mem_ctx, data.dptr, data.dsize);
- SAFE_FREE(data.dptr);
+ tdb_parse_record(ctx->wtdb->tdb, key, db_tdb_fetch_parse, &state);
- if (pdata->dptr == NULL) {
+ if (state.result == -1) {
return -1;
}
- pdata->dsize = data.dsize;
+
+ *pdata = state.data;
return 0;
}