X-Git-Url: https://git.archive.openwrt.org/?p=project%2Fuci.git;a=blobdiff_plain;f=file.c;h=6052c4010a4c74c1e7f1151b209ebf9e77e531f5;hp=416422d6182e08d0d5aa45f621b6a12bf2891ce2;hb=53ddba828005844d876b4516cb7355a410468ef7;hpb=abccd307f468012652f5347c46a3877a975fa108 diff --git a/file.c b/file.c index 416422d..6052c40 100644 --- a/file.c +++ b/file.c @@ -744,24 +744,43 @@ error: uci_file_cleanup(ctx); } +static void uci_load_history_file(struct uci_context *ctx, struct uci_package *p, char *filename, FILE **f, bool flush) +{ + FILE *stream = NULL; + + UCI_TRAP_SAVE(ctx, done); + stream = uci_open_stream(ctx, filename, SEEK_SET, flush, false); + if (p) + uci_parse_history(ctx, stream, p); + UCI_TRAP_RESTORE(ctx); +done: + if (f) + *f = stream; + else if (stream) + uci_close_stream(stream); +} + static void uci_load_history(struct uci_context *ctx, struct uci_package *p, bool flush) { + struct uci_element *e; char *filename = NULL; FILE *f = NULL; if (!p->confdir) return; - if ((asprintf(&filename, "%s/%s", UCI_SAVEDIR, p->e.name) < 0) || !filename) - UCI_THROW(ctx, UCI_ERR_MEM); + uci_foreach_element(&ctx->history_path, e) { + if ((asprintf(&filename, "%s/%s", e->name, p->e.name) < 0) || !filename) + UCI_THROW(ctx, UCI_ERR_MEM); - UCI_TRAP_SAVE(ctx, done); - f = uci_open_stream(ctx, filename, SEEK_SET, flush, false); - if (p) - uci_parse_history(ctx, f, p); - UCI_TRAP_RESTORE(ctx); + uci_load_history_file(ctx, p, filename, NULL, false); + free(filename); + } -done: + if ((asprintf(&filename, "%s/%s", ctx->savedir, p->e.name) < 0) || !filename) + UCI_THROW(ctx, UCI_ERR_MEM); + + uci_load_history_file(ctx, p, filename, &f, flush); if (flush && f) { rewind(f); ftruncate(fileno(f), 0); @@ -778,8 +797,8 @@ static char *uci_config_path(struct uci_context *ctx, const char *name) char *filename; UCI_ASSERT(ctx, uci_validate_name(name)); - filename = uci_malloc(ctx, strlen(name) + sizeof(UCI_CONFDIR) + 2); - sprintf(filename, UCI_CONFDIR "/%s", name); + filename = uci_malloc(ctx, strlen(name) + strlen(ctx->confdir) + 2); + sprintf(filename, "%s/%s", ctx->confdir, name); return filename; } @@ -849,7 +868,7 @@ int uci_save(struct uci_context *ctx, struct uci_package *p) if (uci_list_empty(&p->history)) return 0; - if ((asprintf(&filename, "%s/%s", UCI_SAVEDIR, p->e.name) < 0) || !filename) + if ((asprintf(&filename, "%s/%s", ctx->savedir, p->e.name) < 0) || !filename) UCI_THROW(ctx, UCI_ERR_MEM); ctx->errno = 0; @@ -984,10 +1003,13 @@ int uci_list_configs(struct uci_context *ctx, char ***list) glob_t globbuf; int size, i; char *buf; + char *dir; UCI_HANDLE_ERR(ctx); - if (glob(UCI_CONFDIR "/*", GLOB_MARK, NULL, &globbuf) != 0) + dir = uci_malloc(ctx, strlen(ctx->confdir) + 1 + sizeof("/*")); + sprintf(dir, "%s/*", ctx->confdir); + if (glob(dir, GLOB_MARK, NULL, &globbuf) != 0) UCI_THROW(ctx, UCI_ERR_NOTFOUND); size = sizeof(char *) * (globbuf.gl_pathc + 1); @@ -1015,6 +1037,7 @@ int uci_list_configs(struct uci_context *ctx, char ***list) buf += strlen(buf) + 1; } *list = configs; + free(dir); return 0; }