X-Git-Url: https://git.archive.openwrt.org/?p=project%2Fuci.git;a=blobdiff_plain;f=file.c;h=152d175da122a218b0828e8c2814abe19c82e363;hp=23ea9595974826c8a8cf85fb37909794c12d4a00;hb=f2520c27cd7942dc01d3386b4083b128648343c0;hpb=27cef8e19174dacd5744c6944a76ebc5afa27130 diff --git a/file.c b/file.c index 23ea959..152d175 100644 --- a/file.c +++ b/file.c @@ -2,9 +2,9 @@ * libuci - Library for the Unified Configuration Interface * Copyright (C) 2008 Felix Fietkau * - * this program is free software; you can redistribute it and/or modify - * it under the terms of the gnu lesser general public license version 2.1 - * as published by the free software foundation + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 2.1 + * as published by the Free Software Foundation * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -210,6 +210,9 @@ static void parse_str(struct uci_context *ctx, char **str, char **target) case '"': parse_double_quote(ctx, str, target); break; + case '#': + **str = 0; + /* fall through */ case 0: goto done; case '\\': @@ -450,7 +453,12 @@ static void uci_parse_line(struct uci_context *ctx, bool single) char *pbrk = NULL; word = strtok_r(word, " \t", &pbrk); + if (!word) + continue; + switch(word[0]) { + case '#': + return; case 'p': if ((word[1] == 0) || !strcmp(word + 1, "ackage")) uci_parse_package(ctx, &word, single); @@ -736,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); @@ -770,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; } @@ -841,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; @@ -976,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); @@ -1007,6 +1037,7 @@ int uci_list_configs(struct uci_context *ctx, char ***list) buf += strlen(buf) + 1; } *list = configs; + free(dir); return 0; }