+ /* 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, just in case */
+ uci_load_history(ctx, p, true);
+ } else {
+ /* flush history */
+ uci_load_history(ctx, NULL, true);
+ }
+ }
+
+ 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;
+
+ UCI_HANDLE_ERR(ctx);
+
+ if (glob(UCI_CONFDIR "/*", GLOB_MARK, NULL, &globbuf) != 0)
+ UCI_THROW(ctx, UCI_ERR_NOTFOUND);
+
+ size = sizeof(char *) * (globbuf.gl_pathc + 1);
+ for(i = 0; i < globbuf.gl_pathc; i++) {
+ char *p;
+
+ p = get_filename(globbuf.gl_pathv[i]);
+ if (!p)
+ continue;
+
+ size += strlen(p) + 1;
+ }
+
+ configs = uci_malloc(ctx, size);
+ buf = (char *) &configs[globbuf.gl_pathc + 1];
+ for(i = 0; i < globbuf.gl_pathc; i++) {
+ char *p;
+
+ p = get_filename(globbuf.gl_pathv[i]);
+ if (!p)
+ continue;
+
+ configs[i] = buf;
+ strcpy(buf, p);
+ buf += strlen(buf) + 1;
+ }
+ *list = configs;
+
+ return 0;