From 29bdad8620257c45dcbff5d15f26ac0b61c7b419 Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Fri, 22 Aug 2008 20:39:37 +0200 Subject: [PATCH] uci_revert api cleanup --- cli.c | 2 +- history.c | 37 ++++++++++++++++++++++--------------- uci.h | 6 ++---- 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/cli.c b/cli.c index 290dcbf..a1f4ce3 100644 --- a/cli.c +++ b/cli.c @@ -354,7 +354,7 @@ static int uci_do_section_cmd(int cmd, int argc, char **argv) ret = uci_rename(ctx, &ptr); break; case CMD_REVERT: - ret = uci_revert(ctx, &ptr.p, ptr.section, ptr.option); + ret = uci_revert(ctx, &ptr); break; case CMD_SET: ret = uci_set(ctx, ptr.p, ptr.section, ptr.option, ptr.value, NULL); diff --git a/history.c b/history.c index 07db1d3..ae66f66 100644 --- a/history.c +++ b/history.c @@ -324,16 +324,15 @@ done: uci_cleanup(ctx); } -int uci_revert(struct uci_context *ctx, struct uci_package **pkg, const char *section, const char *option) +int uci_revert(struct uci_context *ctx, struct uci_ptr *ptr) { - struct uci_package *p; - char *name = NULL; + char *package = NULL; + char *section = NULL; + char *option = NULL; UCI_HANDLE_ERR(ctx); - UCI_ASSERT(ctx, pkg != NULL); - p = *pkg; - UCI_ASSERT(ctx, p != NULL); - UCI_ASSERT(ctx, p->has_history); + expand_ptr(ctx, ptr, true); + UCI_ASSERT(ctx, ptr->p->has_history); /* * - flush unwritten changes @@ -343,20 +342,28 @@ int uci_revert(struct uci_context *ctx, struct uci_package **pkg, const char *se * - reload the package */ UCI_TRAP_SAVE(ctx, error); - UCI_INTERNAL(uci_save, ctx, p); - name = uci_strdup(ctx, p->e.name); + UCI_INTERNAL(uci_save, ctx, ptr->p); + + /* NB: need to clone package, section and option names, + * as they may get freed on uci_free_package() */ + package = uci_strdup(ctx, ptr->p->e.name); + section = uci_strdup(ctx, ptr->section); + option = uci_strdup(ctx, ptr->option); - *pkg = NULL; - uci_free_package(&p); - uci_filter_history(ctx, name, section, option); + uci_free_package(&ptr->p); + uci_filter_history(ctx, package, section, option); - UCI_INTERNAL(uci_load, ctx, name, &p); + UCI_INTERNAL(uci_load, ctx, package, &ptr->p); UCI_TRAP_RESTORE(ctx); ctx->err = 0; error: - if (name) - free(name); + if (package) + free(package); + if (section) + free(section); + if (option) + free(option); if (ctx->err) UCI_THROW(ctx, ctx->err); return 0; diff --git a/uci.h b/uci.h index ed1a834..7866a75 100644 --- a/uci.h +++ b/uci.h @@ -247,11 +247,9 @@ extern int uci_add_history_path(struct uci_context *ctx, const char *dir); /** * uci_revert: revert all changes to a config item * @ctx: uci context - * @p: pointer to a uci_package struct ptr (will be replaced by the revert) - * @section: section name (optional) - * @option option name (optional) + * @ptr: uci pointer */ -extern int uci_revert(struct uci_context *ctx, struct uci_package **p, const char *section, const char *option); +extern int uci_revert(struct uci_context *ctx, struct uci_ptr *ptr); /** * uci_parse_argument: parse a shell-style argument, with an arbitrary quoting style -- 2.11.0