X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fuci.git;a=blobdiff_plain;f=file.c;h=a5e242f4908409bd9dfe9c9bbec67deb56bb9ce3;hp=01205052b7ff6bc75494835154953bcea16601cb;hb=39d4d8f96099e22cba821736b584628df1810838;hpb=c4402a9e59721172395d9403cfbe467209bcd6ad diff --git a/file.c b/file.c index 0120505..a5e242f 100644 --- a/file.c +++ b/file.c @@ -9,7 +9,7 @@ * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. + * GNU Lesser General Public License for more details. */ /* @@ -18,6 +18,7 @@ #define _GNU_SOURCE #include +#include #include #include #include @@ -60,7 +61,10 @@ __private void uci_getln(struct uci_context *ctx, int offset) ofs += strlen(p); if (pctx->buf[ofs - 1] == '\n') { pctx->line++; - pctx->buf[ofs - 1] = 0; + if (ofs >= 2 && pctx->buf[ofs - 2] == '\r') + pctx->buf[ofs - 2] = 0; + else + pctx->buf[ofs - 1] = 0; return; } @@ -200,7 +204,7 @@ static void parse_str(struct uci_context *ctx, char **str, char **target) } while (**str && !isspace(**str)); done: - /* + /* * if the string was unquoted and we've stopped at a whitespace * character, skip to the next one, because the whitespace will * be overwritten by a null byte here @@ -266,7 +270,7 @@ int uci_parse_argument(struct uci_context *ctx, FILE *stream, char **str, char * } static int -uci_fill_ptr(struct uci_context *ctx, struct uci_ptr *ptr, struct uci_element *e, bool complete) +uci_fill_ptr(struct uci_context *ctx, struct uci_ptr *ptr, struct uci_element *e) { UCI_ASSERT(ctx, ptr != NULL); UCI_ASSERT(ctx, e != NULL); @@ -296,8 +300,6 @@ fill_package: ptr->package = ptr->p->e.name; ptr->flags |= UCI_LOOKUP_DONE; - if (complete) - ptr->flags |= UCI_LOOKUP_COMPLETE; return 0; } @@ -318,7 +320,7 @@ static void assert_eol(struct uci_context *ctx, char **str) uci_parse_error(ctx, *str, "too many arguments"); } -/* +/* * switch to a different config, either triggered by uci_load, or by a * 'package <...>' statement in the import file */ @@ -343,7 +345,7 @@ static void uci_switch_config(struct uci_context *ctx) if (!name) return; - /* + /* * if an older config under the same name exists, unload it * ignore errors here, e.g. if the config was not found */ @@ -400,11 +402,11 @@ static void uci_parse_config(struct uci_context *ctx, char **str) name = next_arg(ctx, str, false, true); assert_eol(ctx, str); - if (!name) { + if (!name || !name[0]) { ctx->internal = !pctx->merge; UCI_NESTED(uci_add_section, ctx, pctx->package, type, &pctx->section); } else { - uci_fill_ptr(ctx, &ptr, &pctx->package->e, false); + uci_fill_ptr(ctx, &ptr, &pctx->package->e); e = uci_lookup_list(&pctx->package->sections, name); if (e) ptr.s = uci_to_section(e); @@ -438,7 +440,7 @@ static void uci_parse_option(struct uci_context *ctx, char **str, bool list) value = next_arg(ctx, str, false, false); assert_eol(ctx, str); - uci_fill_ptr(ctx, &ptr, &pctx->section->e, false); + uci_fill_ptr(ctx, &ptr, &pctx->section->e); e = uci_lookup_list(&pctx->section->options, name); if (e) ptr.o = uci_to_option(e); @@ -510,7 +512,7 @@ invalid: /* * escape an uci string for export */ -static char *uci_escape(struct uci_context *ctx, const char *str) +static const char *uci_escape(struct uci_context *ctx, const char *str) { const char *end; int ofs = 0; @@ -518,6 +520,9 @@ static char *uci_escape(struct uci_context *ctx, const char *str) if (!ctx->buf) { ctx->bufsz = LINEBUF; ctx->buf = malloc(LINEBUF); + + if (!ctx->buf) + return str; } while (1) { @@ -560,10 +565,10 @@ static void uci_export_package(struct uci_package *p, FILE *stream, bool header) struct uci_element *s, *o, *i; if (header) - fprintf(stream, "package '%s'\n", uci_escape(ctx, p->e.name)); + fprintf(stream, "package %s\n", uci_escape(ctx, p->e.name)); uci_foreach_element(&p->sections, s) { struct uci_section *sec = uci_to_section(s); - fprintf(stream, "\nconfig '%s'", uci_escape(ctx, sec->type)); + fprintf(stream, "\nconfig %s", uci_escape(ctx, sec->type)); if (!sec->anonymous || (ctx->flags & UCI_FLAG_EXPORT_NAME)) fprintf(stream, " '%s'", uci_escape(ctx, sec->e.name)); fprintf(stream, "\n"); @@ -571,12 +576,12 @@ static void uci_export_package(struct uci_package *p, FILE *stream, bool header) struct uci_option *opt = uci_to_option(o); switch(opt->type) { case UCI_TYPE_STRING: - fprintf(stream, "\toption '%s'", uci_escape(ctx, opt->e.name)); + fprintf(stream, "\toption %s", uci_escape(ctx, opt->e.name)); fprintf(stream, " '%s'\n", uci_escape(ctx, opt->v.string)); break; case UCI_TYPE_LIST: uci_foreach_element(&opt->v.list, i) { - fprintf(stream, "\tlist '%s'", uci_escape(ctx, opt->e.name)); + fprintf(stream, "\tlist %s", uci_escape(ctx, opt->e.name)); fprintf(stream, " '%s'\n", uci_escape(ctx, i->name)); } break; @@ -618,7 +623,7 @@ int uci_import(struct uci_context *ctx, FILE *stream, const char *name, struct u uci_alloc_parse_context(ctx); pctx = ctx->pctx; pctx->file = stream; - if (*package && single) { + if (package && *package && single) { pctx->package = *package; pctx->merge = true; } @@ -677,12 +682,15 @@ static char *uci_config_path(struct uci_context *ctx, const char *name) return filename; } -void uci_file_commit(struct uci_context *ctx, struct uci_package **package, bool overwrite) +static void uci_file_commit(struct uci_context *ctx, struct uci_package **package, bool overwrite) { struct uci_package *p = *package; - FILE *f = NULL; + FILE *f1, *f2 = NULL; char *name = NULL; char *path = NULL; + char *filename = NULL; + struct stat statbuf; + bool do_rename = false; if (!p->path) { if (overwrite) @@ -691,8 +699,22 @@ void uci_file_commit(struct uci_context *ctx, struct uci_package **package, bool UCI_THROW(ctx, UCI_ERR_INVAL); } + if ((asprintf(&filename, "%s/.%s.uci-XXXXXX", ctx->confdir, p->e.name) < 0) || !filename) + UCI_THROW(ctx, UCI_ERR_MEM); + + if (!mktemp(filename)) + *filename = 0; + + if (!*filename) { + free(filename); + UCI_THROW(ctx, UCI_ERR_IO); + } + + if ((stat(filename, &statbuf) == 0) && ((statbuf.st_mode & S_IFMT) != S_IFREG)) + UCI_THROW(ctx, UCI_ERR_IO); + /* open the config file for writing now, so that it is locked */ - f = uci_open_stream(ctx, p->path, SEEK_SET, true, true); + f1 = uci_open_stream(ctx, p->path, NULL, SEEK_SET, true, true); /* flush unsaved changes and reload from delta file */ UCI_TRAP_SAVE(ctx, done); @@ -704,13 +726,13 @@ void uci_file_commit(struct uci_context *ctx, struct uci_package **package, bool if (!uci_list_empty(&p->delta)) UCI_INTERNAL(uci_save, ctx, p); - /* - * other processes might have modified the config - * as well. dump and reload + /* + * other processes might have modified the config + * as well. dump and reload */ uci_free_package(&p); uci_cleanup(ctx); - UCI_INTERNAL(uci_import, ctx, f, name, &p, true); + UCI_INTERNAL(uci_import, ctx, f1, name, &p, true); p->path = path; p->has_delta = true; @@ -725,25 +747,33 @@ void uci_file_commit(struct uci_context *ctx, struct uci_package **package, bool goto done; } - rewind(f); - if (ftruncate(fileno(f), 0) < 0) - UCI_THROW(ctx, UCI_ERR_IO); + f2 = uci_open_stream(ctx, filename, p->path, SEEK_SET, true, true); + uci_export(ctx, f2, p, false); + + fflush(f2); + fsync(fileno(f2)); + uci_close_stream(f2); + + do_rename = true; - uci_export(ctx, f, p, false); UCI_TRAP_RESTORE(ctx); done: - if (name) - free(name); - if (path) - free(path); - uci_close_stream(f); + free(name); + free(path); + uci_close_stream(f1); + if (do_rename && rename(filename, p->path)) { + unlink(filename); + UCI_THROW(ctx, UCI_ERR_IO); + } + free(filename); + sync(); if (ctx->err) UCI_THROW(ctx, ctx->err); } -/* +/* * This function returns the filename by returning the string * after the last '/' character. By checking for a non-'\0' * character afterwards, directories are ignored (glob marks @@ -833,9 +863,9 @@ static struct uci_package *uci_file_load(struct uci_context *ctx, const char *na break; } - file = uci_open_stream(ctx, filename, SEEK_SET, false, false); - ctx->err = 0; UCI_TRAP_SAVE(ctx, done); + file = uci_open_stream(ctx, filename, NULL, SEEK_SET, false, false); + ctx->err = 0; UCI_INTERNAL(uci_import, ctx, file, name, &package, true); UCI_TRAP_RESTORE(ctx); @@ -847,8 +877,10 @@ static struct uci_package *uci_file_load(struct uci_context *ctx, const char *na done: uci_close_stream(file); - if (ctx->err) + if (ctx->err) { + free(filename); UCI_THROW(ctx, ctx->err); + } return package; }