From 394291281a0be7a150086fc459614f4fe3f56c1f Mon Sep 17 00:00:00 2001 From: Michael Adam Date: Fri, 22 Jun 2007 11:21:59 +0000 Subject: r23583: Add a utility function to recursively delete a Registry key with all its subkeys. (reg_deletekey will refuse to delete a key with subkeys with WERR_ACCESS_DENIED). Michael (This used to be commit 41c3ff6e277601a9c7ac29009fd89ff9c961ca46) --- source3/registry/reg_api.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) (limited to 'source3/registry/reg_api.c') diff --git a/source3/registry/reg_api.c b/source3/registry/reg_api.c index 4fbf54e753..e2bb56deea 100644 --- a/source3/registry/reg_api.c +++ b/source3/registry/reg_api.c @@ -677,3 +677,59 @@ WERROR reg_open_path(TALLOC_CTX *mem_ctx, const char *orig_path, *pkey = key; return WERR_OK; } + + +/* + * Utility function to delete a registry key with all its subkeys. + * Note that reg_deletekey returns ACCESS_DENIED when called on a + * key that has subkeys. + */ +WERROR reg_deletekey_recursive(TALLOC_CTX *ctx, + struct registry_key *parent, + const char *path) +{ + TALLOC_CTX *mem_ctx = NULL; + WERROR werr = WERR_OK; + struct registry_key *key; + uint32 idx = 0; + char *subkey_name = NULL; + + mem_ctx = talloc_new(ctx); + if (mem_ctx == NULL) { + werr = WERR_NOMEM; + goto done; + } + + /* recurse through subkeys first */ + werr = reg_openkey(mem_ctx, parent, path, REG_KEY_WRITE, &key); + if (!W_ERROR_IS_OK(werr)) { + goto done; + } + + /* NOTE: we *must not* increment idx in this loop since + * the list of subkeys shrinks with each loop body. + * so this way, we repeatedly delete the *first* entry + * of a shrinking list. */ + for (idx = 0; + W_ERROR_IS_OK(werr = reg_enumkey(mem_ctx, key, idx, + &subkey_name, NULL)); + ) + { + werr = reg_deletekey_recursive(mem_ctx, key, subkey_name); + if (!W_ERROR_IS_OK(werr)) { + goto done; + } + } + if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) { + DEBUG(1, ("reg_deletekey_recursive: Error enumerating " + "subkeys: %s\n", dos_errstr(werr))); + goto done; + } + + /* now delete the actual key */ + werr = reg_deletekey(parent, path); + +done: + TALLOC_FREE(mem_ctx); + return werr; +} -- cgit