+ 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_commit(struct uci_context *ctx, struct uci_package **package, bool overwrite)
+{
+ struct uci_package *p;
+ FILE *f = NULL;
+ char *name = NULL;
+ char *path = NULL;
+
+ UCI_HANDLE_ERR(ctx);
+ UCI_ASSERT(ctx, package != NULL);
+ p = *package;
+
+ UCI_ASSERT(ctx, p != NULL);
+ if (!p->path) {
+ if (overwrite)
+ p->path = uci_config_path(ctx, p->e.name);
+ else
+ UCI_THROW(ctx, UCI_ERR_INVAL);
+ }
+
+
+ /* open the config file for writing now, so that it is locked */
+ f = uci_open_stream(ctx, p->path, SEEK_SET, true, true);
+
+ /* flush unsaved changes and reload from history file */
+ UCI_TRAP_SAVE(ctx, done);
+ if (p->confdir) {
+ if (!overwrite) {
+ name = uci_strdup(ctx, p->e.name);
+ path = uci_strdup(ctx, p->path);
+ /* dump our own changes to the history file */
+ if (!uci_list_empty(&p->history))
+ UCI_INTERNAL(uci_save, ctx, p);
+
+ /*
+ * other processes might have modified the config
+ * as well. dump and reload
+ */
+ uci_free_package(&p);
+ uci_file_cleanup(ctx);
+ UCI_INTERNAL(uci_import, ctx, f, name, &p, true);
+
+ p->path = path;
+ p->confdir = true;
+ *package = p;
+
+ /* 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;
+ }
+ }
+
+ rewind(f);
+ ftruncate(fileno(f), 0);
+
+ uci_export(ctx, f, p, false);
+ UCI_TRAP_RESTORE(ctx);
+
+done:
+ if (name)
+ free(name);
+ if (path)
+ free(path);
+ uci_close_stream(f);
+ if (ctx->errno)
+ UCI_THROW(ctx, ctx->errno);
+
+ return 0;
+}
+
+
+/*
+ * 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
+ * those with a trailing '/'
+ */
+static inline char *get_filename(char *path)
+{
+ char *p;
+
+ p = strrchr(path, '/');
+ p++;
+ if (!*p)
+ return NULL;
+ return p;
+}
+
+int uci_list_configs(struct uci_context *ctx, char ***list)
+{
+ char **configs;
+ glob_t globbuf;
+ int size, i;
+ char *buf;
+ char *dir;
+
+ UCI_HANDLE_ERR(ctx);
+
+ dir = uci_malloc(ctx, strlen(ctx->confdir) + 1 + sizeof("/*"));
+ sprintf(dir, "%s/*", ctx->confdir);
+ if (glob(dir, GLOB_MARK, NULL, &globbuf) != 0)