X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fuci.git;a=blobdiff_plain;f=history.c;h=e3e19de4e60dc0ce4a0e238df30b275796038ff9;hp=07db1d3b77124236b888503bb5872da505af9e01;hb=c60702b55cf8efe9dcd359fc9aa8e9c7cd421ada;hpb=563f8dd6f0eb56780a00f5856bf7fcf40e39d845 diff --git a/history.c b/history.c index 07db1d3..e3e19de 100644 --- a/history.c +++ b/history.c @@ -160,7 +160,8 @@ static void uci_parse_history_line(struct uci_context *ctx, struct uci_package * break; case UCI_CMD_ADD: case UCI_CMD_CHANGE: - UCI_INTERNAL(uci_set, ctx, p, ptr.section, ptr.option, ptr.value, &e); + UCI_INTERNAL(uci_set, ctx, &ptr); + e = ptr.last; if (!ptr.option && e && (cmd == UCI_CMD_ADD)) uci_to_section(e)->anonymous = true; break; @@ -249,7 +250,10 @@ static int uci_load_history(struct uci_context *ctx, struct uci_package *p, bool changes = uci_load_history_file(ctx, p, filename, &f, flush); if (flush && f && (changes > 0)) { rewind(f); - ftruncate(fileno(f), 0); + if (ftruncate(fileno(f), 0) < 0) { + uci_close_stream(f); + UCI_THROW(ctx, UCI_ERR_IO); + } } if (filename) free(filename); @@ -307,7 +311,8 @@ static void uci_filter_history(struct uci_context *ctx, const char *name, const /* rebuild the history file */ rewind(f); - ftruncate(fileno(f), 0); + if (ftruncate(fileno(f), 0) < 0) + UCI_THROW(ctx, UCI_ERR_IO); uci_foreach_element_safe(&list, tmp, e) { fprintf(f, "%s\n", e->name); uci_free_element(e); @@ -317,23 +322,22 @@ static void uci_filter_history(struct uci_context *ctx, const char *name, const done: if (filename) free(filename); - uci_close_stream(f); + uci_close_stream(pctx->file); uci_foreach_element_safe(&list, tmp, e) { uci_free_element(e); } 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, false); + UCI_ASSERT(ctx, ptr->p->has_history); /* * - flush unwritten changes @@ -343,20 +347,30 @@ 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); + if (ptr->section) + section = uci_strdup(ctx, ptr->section); + if (ptr->option) + 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;