+ file = uci_open_stream(ctx, filename, SEEK_SET, false, false);
+ ctx->errno = 0;
+ UCI_TRAP_SAVE(ctx, done);
+ UCI_INTERNAL(uci_import, ctx, file, name, package, true);
+ UCI_TRAP_RESTORE(ctx);
+
+ if (*package) {
+ (*package)->path = filename;
+ (*package)->confdir = confdir;
+ uci_load_history(ctx, *package, false);
+ }
+
+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.
+ * does not modify the uci_package pointer
+ */
+ if (!p->confdir)
+ return uci_commit(ctx, &p, false);
+
+ 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, 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, "-");
+ else if (h->cmd == UCI_CMD_RENAME)
+ fprintf(f, "@");
+
+ fprintf(f, "%s.%s", p->e.name, h->section);
+ if (e->name)
+ fprintf(f, ".%s", e->name);
+
+ if (h->cmd == UCI_CMD_REMOVE)
+ fprintf(f, "\n");
+ else
+ fprintf(f, "=%s\n", h->value);
+ uci_free_history(h);