- uci_import(ctx, file, name, package, true);
- UCI_TRAP_RESTORE(ctx);
-
- if (*package) {
- (*package)->path = filename;
- (*package)->confdir = confdir;
- uci_load_history(ctx, *package);
- }
-
-done:
- uci_close_stream(file);
- return ctx->errno;
-}
-
-int uci_save(struct uci_context *ctx, struct uci_package *p)
-{
- FILE *f = NULL;
- char *filename = NULL;
- struct uci_element *e, *tmp;
-
- UCI_HANDLE_ERR(ctx);
- UCI_ASSERT(ctx, p != NULL);
-
- /*
- * if the config file was outside of the /etc/config path,
- * don't save the history to a file, update the real file
- * directly
- */
- if (!p->confdir)
- return uci_commit(ctx, p);
-
- if (uci_list_empty(&p->history))
- return 0;
-
- if ((asprintf(&filename, "%s/%s", UCI_SAVEDIR, p->e.name) < 0) || !filename)
- UCI_THROW(ctx, UCI_ERR_MEM);
-
- ctx->errno = 0;
- UCI_TRAP_SAVE(ctx, done);
- f = uci_open_stream(ctx, filename, SEEK_END, true);
- UCI_TRAP_RESTORE(ctx);
-
- uci_foreach_element_safe(&p->history, tmp, e) {
- struct uci_history *h = uci_to_history(e);
-
- if (h->cmd == UCI_CMD_REMOVE)
- fprintf(f, "-");
-
- fprintf(f, "%s.%s", p->e.name, h->section);
- if (e->name)
- fprintf(f, ".%s", e->name);
+ if (p->has_delta) {
+ if (!overwrite) {
+ name = uci_strdup(ctx, p->e.name);
+ path = uci_strdup(ctx, p->path);
+ /* dump our own changes to the delta file */
+ if (!uci_list_empty(&p->delta))
+ UCI_INTERNAL(uci_save, ctx, p);
+
+ /*
+ * 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);
+
+ p->path = path;
+ p->has_delta = true;
+ *package = p;
+
+ /* freed together with the uci_package */
+ path = NULL;
+ }