ucimap: add custom free() callbacks for options, only used on custom datatypes
[project/uci.git] / file.c
diff --git a/file.c b/file.c
index a58ac559e55aedda25730ea2a0d4a86f978d5e53..6b41d683b3dd903067fe5cbba51249354f726e2a 100644 (file)
--- a/file.c
+++ b/file.c
@@ -119,7 +119,7 @@ static void uci_parse_config(struct uci_context *ctx, char **str)
        *str += strlen(*str) + 1;
 
        type = next_arg(ctx, str, true, false);
-       if (!uci_validate_str(type, false))
+       if (!uci_validate_type(type))
                uci_parse_error(ctx, type, "invalid character in field");
        name = next_arg(ctx, str, false, true);
        assert_eol(ctx, str);
@@ -353,7 +353,7 @@ int uci_import(struct uci_context *ctx, FILE *stream, const char *name, struct u
         * NB: the config file can still override the package name
         */
        if (name) {
-               UCI_ASSERT(ctx, uci_validate_str(name, false));
+               UCI_ASSERT(ctx, uci_validate_package(name));
                pctx->name = name;
        }
 
@@ -394,7 +394,7 @@ static char *uci_config_path(struct uci_context *ctx, const char *name)
 {
        char *filename;
 
-       UCI_ASSERT(ctx, uci_validate_str(name, false));
+       UCI_ASSERT(ctx, uci_validate_package(name));
        filename = uci_malloc(ctx, strlen(name) + strlen(ctx->confdir) + 2);
        sprintf(filename, "%s/%s", ctx->confdir, name);
 
@@ -442,19 +442,16 @@ void uci_file_commit(struct uci_context *ctx, struct uci_package **package, bool
 
                        /* freed together with the uci_package */
                        path = NULL;
-
-                       /* check for updated history, flush */
-                       if (!uci_load_history(ctx, p, true))
-                               goto done;
-               } else {
-                       /* flush history */
-                       if (!uci_load_history(ctx, NULL, true))
-                               goto done;
                }
+
+               /* flush history */
+               if (!uci_load_history(ctx, p, true))
+                       goto done;
        }
 
        rewind(f);
-       ftruncate(fileno(f), 0);
+       if (ftruncate(fileno(f), 0) < 0)
+               UCI_THROW(ctx, UCI_ERR_IO);
 
        uci_export(ctx, f, p, false);
        UCI_TRAP_RESTORE(ctx);
@@ -520,7 +517,7 @@ static char **uci_list_config_files(struct uci_context *ctx)
                if (!p)
                        continue;
 
-               if (!uci_validate_name(p))
+               if (!uci_validate_package(p))
                        continue;
 
                configs[i] = buf;
@@ -528,6 +525,7 @@ static char **uci_list_config_files(struct uci_context *ctx)
                buf += strlen(buf) + 1;
        }
        free(dir);
+       globfree(&globbuf);
        return configs;
 }