From: Ralph Boehme Date: Thu, 23 Oct 2025 16:41:41 +0000 (+0200) Subject: dbwrap: add flags to dbwrap_wipe() X-Git-Tag: talloc-2.5.0~237 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=0fac57b1a422dacbda60f860debe63b17a49bf0f;p=thirdparty%2Fsamba.git dbwrap: add flags to dbwrap_wipe() Will be used later to pass flags=DBWRAP_DROP_PERSISTENT for databases with DBWRAP_FLAG_PER_REC_PERSISTENT, requesting to wipe the persistent backup database. Signed-off-by: Ralph Boehme Reviewed-by: Martin Schwenke --- diff --git a/lib/dbwrap/dbwrap.c b/lib/dbwrap/dbwrap.c index c4a6ed52864..8f3988c46bc 100644 --- a/lib/dbwrap/dbwrap.c +++ b/lib/dbwrap/dbwrap.c @@ -20,6 +20,7 @@ */ #include "replace.h" +#include #include "lib/util/debug.h" #include "lib/util/fault.h" #include "lib/util/talloc_stack.h" @@ -48,9 +49,14 @@ static int delete_record(struct db_record *rec, void *data) * Fallback wipe implementation using traverse and delete if no genuine * wipe operation is provided */ -static int dbwrap_fallback_wipe(struct db_context *db) +static int dbwrap_fallback_wipe(struct db_context *db, + struct dbwrap_wipe_flags flags) { - NTSTATUS status = dbwrap_trans_traverse(db, delete_record, NULL); + NTSTATUS status; + + assert(dbwrap_wipe_flags_default(flags)); + + status = dbwrap_trans_traverse(db, delete_record, NULL); return NT_STATUS_IS_OK(status) ? 0 : -1; } @@ -613,12 +619,20 @@ NTSTATUS dbwrap_do_locked(struct db_context *db, TDB_DATA key, return NT_STATUS_OK; } -int dbwrap_wipe(struct db_context *db) +int dbwrap_wipe(struct db_context *db, struct dbwrap_wipe_flags flags) { if (db->wipe == NULL) { - return dbwrap_fallback_wipe(db); + return dbwrap_fallback_wipe(db, flags); } - return db->wipe(db); + return db->wipe(db, flags); +} + +bool dbwrap_wipe_flags_default(struct dbwrap_wipe_flags flags) +{ + struct dbwrap_wipe_flags def_flags = { + .wipe_default = true, + }; + return memcmp(&flags, &def_flags, sizeof(flags)) == 0; } int dbwrap_check(struct db_context *db) diff --git a/lib/dbwrap/dbwrap.h b/lib/dbwrap/dbwrap.h index b1127bc612e..9df3576bb8a 100644 --- a/lib/dbwrap/dbwrap.h +++ b/lib/dbwrap/dbwrap.h @@ -161,7 +161,24 @@ struct tevent_req *dbwrap_parse_record_send( void *private_data, enum dbwrap_req_state *req_state); NTSTATUS dbwrap_parse_record_recv(struct tevent_req *req); -int dbwrap_wipe(struct db_context *db); + +/** + * Wipe a database + * + * @param[in] db Database to wipe + + * @param[in] flags DBWRAP_DROP_PERSISTENT: On databases opened with + * DBWRAP_FLAG_PER_REC_PERSISTENT, wipe the persistent + * backup database. + * + * @return 0 on success, -1 on error + **/ +struct dbwrap_wipe_flags { + bool wipe_default : 1; + bool wipe_persistent_backup_db : 1; +}; +int dbwrap_wipe(struct db_context *db, struct dbwrap_wipe_flags flags); +bool dbwrap_wipe_flags_default(struct dbwrap_wipe_flags flags); int dbwrap_check(struct db_context *db); int dbwrap_get_seqnum(struct db_context *db); /* Returns 0 if unknown. */ diff --git a/lib/dbwrap/dbwrap_private.h b/lib/dbwrap/dbwrap_private.h index 3dc7bb20e45..b88e9bf3d88 100644 --- a/lib/dbwrap/dbwrap_private.h +++ b/lib/dbwrap/dbwrap_private.h @@ -73,7 +73,7 @@ struct db_context { void *private_data), void *private_data); int (*exists)(struct db_context *db,TDB_DATA key); - int (*wipe)(struct db_context *db); + int (*wipe)(struct db_context *db, struct dbwrap_wipe_flags flags); int (*check)(struct db_context *db); size_t (*id)(struct db_context *db, uint8_t *id, size_t idlen); diff --git a/lib/dbwrap/dbwrap_rbt.c b/lib/dbwrap/dbwrap_rbt.c index 6cd8d936db6..e1c01623ef0 100644 --- a/lib/dbwrap/dbwrap_rbt.c +++ b/lib/dbwrap/dbwrap_rbt.c @@ -18,6 +18,7 @@ */ #include "includes.h" +#include #include "dbwrap/dbwrap.h" #include "dbwrap/dbwrap_private.h" #include "dbwrap/dbwrap_rbt.h" @@ -407,11 +408,15 @@ static int db_rbt_exists(struct db_context *db, TDB_DATA key) return db_rbt_search_internal(db, key, NULL); } -static int db_rbt_wipe(struct db_context *db) +static int db_rbt_wipe(struct db_context *db, struct dbwrap_wipe_flags flags) { struct db_rbt_ctx *old_ctx = talloc_get_type_abort( db->private_data, struct db_rbt_ctx); - struct db_rbt_ctx *new_ctx = talloc_zero(db, struct db_rbt_ctx); + struct db_rbt_ctx *new_ctx = NULL; + + assert(dbwrap_wipe_flags_default(flags)); + + new_ctx = talloc_zero(db, struct db_rbt_ctx); if (new_ctx == NULL) { return -1; } diff --git a/lib/dbwrap/dbwrap_tdb.c b/lib/dbwrap/dbwrap_tdb.c index 582cea6b97f..0a7552aab47 100644 --- a/lib/dbwrap/dbwrap_tdb.c +++ b/lib/dbwrap/dbwrap_tdb.c @@ -224,7 +224,7 @@ static int db_tdb_exists(struct db_context *db, TDB_DATA key) return tdb_exists(ctx->wtdb->tdb, key); } -static int db_tdb_wipe(struct db_context *db) +static int db_tdb_wipe(struct db_context *db, struct dbwrap_wipe_flags flags) { struct db_tdb_ctx *ctx = talloc_get_type_abort( db->private_data, struct db_tdb_ctx); diff --git a/source3/rpc_server/fss/srv_fss_state.c b/source3/rpc_server/fss/srv_fss_state.c index 8597c36a74e..0f684064c59 100644 --- a/source3/rpc_server/fss/srv_fss_state.c +++ b/source3/rpc_server/fss/srv_fss_state.c @@ -210,7 +210,7 @@ _PRIVATE_ NTSTATUS fss_state_store(TALLOC_CTX *mem_ctx, goto err_ctx_free; } - ret = dbwrap_wipe(db); + ret = dbwrap_wipe(db, (struct dbwrap_wipe_flags){.wipe_default=true}); if (ret != 0) { status = NT_STATUS_UNSUCCESSFUL; goto err_db_free; diff --git a/source3/utils/dbwrap_tool.c b/source3/utils/dbwrap_tool.c index 66380a98b5c..0888ce6a90f 100644 --- a/source3/utils/dbwrap_tool.c +++ b/source3/utils/dbwrap_tool.c @@ -291,7 +291,7 @@ static int dbwrap_tool_erase(struct db_context *db, { int ret; - ret = dbwrap_wipe(db); + ret = dbwrap_wipe(db, (struct dbwrap_wipe_flags){.wipe_default=true}); if (ret != 0) { d_fprintf(stderr, "ERROR erasing the database\n"); diff --git a/source3/utils/net_registry_check.c b/source3/utils/net_registry_check.c index aa5e65ecf81..71c62f7f201 100644 --- a/source3/utils/net_registry_check.c +++ b/source3/utils/net_registry_check.c @@ -1239,7 +1239,10 @@ static bool check_ctx_write_new_db(struct check_ctx *ctx) { assert(ctx->odb); if (ctx->opt.wipe) { - int ret = dbwrap_wipe(ctx->odb); + int ret = dbwrap_wipe(ctx->odb, + (struct dbwrap_wipe_flags){ + .wipe_default=true + }); if (ret != 0) { DEBUG(0, ("wiping %s failed\n", ctx->opt.output)); return false;