From 0382e0722221051f786b83cd8204a7484efc7a34 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Thu, 13 Jul 2023 01:19:43 +0200 Subject: [PATCH] dbwrap: Simplify dbwrap_change_int32_atomic_action() Use dbwrap_do_locked() Signed-off-by: Volker Lendecke Reviewed-by: Jeremy Allison --- lib/dbwrap/dbwrap_util.c | 64 +++++++++++++++++++++++----------------- 1 file changed, 37 insertions(+), 27 deletions(-) diff --git a/lib/dbwrap/dbwrap_util.c b/lib/dbwrap/dbwrap_util.c index 8a3ebf55876..312fb418754 100644 --- a/lib/dbwrap/dbwrap_util.c +++ b/lib/dbwrap/dbwrap_util.c @@ -257,49 +257,59 @@ struct dbwrap_change_int32_atomic_context { TDB_DATA key; int32_t *oldval; int32_t change_val; + NTSTATUS status; }; -static NTSTATUS dbwrap_change_int32_atomic_action(struct db_context *db, - void *private_data) +static void dbwrap_change_int32_atomic_action_fn(struct db_record *rec, + TDB_DATA value, + void *private_data) { - struct db_record *rec; + struct dbwrap_change_int32_atomic_context *state = private_data; + uint8_t v_store[4]; + TDB_DATA data = { + .dptr = v_store, + .dsize = sizeof(v_store), + }; int32_t val = -1; - int32_t v_store; - NTSTATUS ret; - struct dbwrap_change_int32_atomic_context *state; - TDB_DATA value; - - state = (struct dbwrap_change_int32_atomic_context *)private_data; - - rec = dbwrap_fetch_locked(db, talloc_tos(), state->key); - if (!rec) { - return NT_STATUS_UNSUCCESSFUL; - } - - value = dbwrap_record_get_value(rec); if (value.dptr == NULL) { val = *(state->oldval); } else if (value.dsize == sizeof(val)) { - val = IVAL(value.dptr, 0); + val = PULL_LE_U32(value.dptr, 0); *(state->oldval) = val; } else { - ret = NT_STATUS_UNSUCCESSFUL; - goto done; + state->status = NT_STATUS_UNSUCCESSFUL; + return; } val += state->change_val; + PUSH_LE_U32(v_store, 0, val); - SIVAL(&v_store, 0, val); + state->status = dbwrap_record_store(rec, data, TDB_REPLACE); +} - ret = dbwrap_record_store(rec, - make_tdb_data((const uint8_t *)&v_store, - sizeof(v_store)), - TDB_REPLACE); +static NTSTATUS dbwrap_change_int32_atomic_action(struct db_context *db, + void *private_data) +{ + struct dbwrap_change_int32_atomic_context *state = private_data; + NTSTATUS status; -done: - TALLOC_FREE(rec); - return ret; + status = dbwrap_do_locked(db, + state->key, + dbwrap_change_int32_atomic_action_fn, + state); + if (!NT_STATUS_IS_OK(status)) { + DBG_DEBUG("dbwrap_do_locked() failed: %s\n", + nt_errstr(status)); + return status; + } + if (!NT_STATUS_IS_OK(state->status)) { + DBG_DEBUG("dbwrap_change_int32_atomic_action_fn() " + "failed: %s\n", + nt_errstr(status)); + return status; + } + return NT_STATUS_OK; } NTSTATUS dbwrap_change_int32_atomic(struct db_context *db,