allow the user of the library to override the confdir/searchdir
authorFelix Fietkau <nbd@openwrt.org>
Sun, 3 Feb 2008 02:48:45 +0000 (03:48 +0100)
committerFelix Fietkau <nbd@openwrt.org>
Sun, 3 Feb 2008 02:48:45 +0000 (03:48 +0100)
file.c
libuci.c
uci.h

diff --git a/file.c b/file.c
index 416422d6182e08d0d5aa45f621b6a12bf2891ce2..4e3e828bb1a84add20012e42196712f49d3ccf52 100644 (file)
--- a/file.c
+++ b/file.c
@@ -752,7 +752,7 @@ static void uci_load_history(struct uci_context *ctx, struct uci_package *p, boo
        if (!p->confdir)
                return;
 
        if (!p->confdir)
                return;
 
-       if ((asprintf(&filename, "%s/%s", UCI_SAVEDIR, p->e.name) < 0) || !filename)
+       if ((asprintf(&filename, "%s/%s", ctx->savedir, p->e.name) < 0) || !filename)
                UCI_THROW(ctx, UCI_ERR_MEM);
 
        UCI_TRAP_SAVE(ctx, done);
                UCI_THROW(ctx, UCI_ERR_MEM);
 
        UCI_TRAP_SAVE(ctx, done);
@@ -778,8 +778,8 @@ static char *uci_config_path(struct uci_context *ctx, const char *name)
        char *filename;
 
        UCI_ASSERT(ctx, uci_validate_name(name));
        char *filename;
 
        UCI_ASSERT(ctx, uci_validate_name(name));
-       filename = uci_malloc(ctx, strlen(name) + sizeof(UCI_CONFDIR) + 2);
-       sprintf(filename, UCI_CONFDIR "/%s", name);
+       filename = uci_malloc(ctx, strlen(name) + strlen(ctx->confdir) + 2);
+       sprintf(filename, "%s/%s", ctx->confdir, name);
 
        return filename;
 }
 
        return filename;
 }
@@ -849,7 +849,7 @@ int uci_save(struct uci_context *ctx, struct uci_package *p)
        if (uci_list_empty(&p->history))
                return 0;
 
        if (uci_list_empty(&p->history))
                return 0;
 
-       if ((asprintf(&filename, "%s/%s", UCI_SAVEDIR, p->e.name) < 0) || !filename)
+       if ((asprintf(&filename, "%s/%s", ctx->savedir, p->e.name) < 0) || !filename)
                UCI_THROW(ctx, UCI_ERR_MEM);
 
        ctx->errno = 0;
                UCI_THROW(ctx, UCI_ERR_MEM);
 
        ctx->errno = 0;
@@ -984,10 +984,13 @@ int uci_list_configs(struct uci_context *ctx, char ***list)
        glob_t globbuf;
        int size, i;
        char *buf;
        glob_t globbuf;
        int size, i;
        char *buf;
+       char *dir;
 
        UCI_HANDLE_ERR(ctx);
 
 
        UCI_HANDLE_ERR(ctx);
 
-       if (glob(UCI_CONFDIR "/*", GLOB_MARK, NULL, &globbuf) != 0)
+       dir = uci_malloc(ctx, strlen(ctx->confdir) + 1 + sizeof("/*"));
+       sprintf(dir, "%s/*", ctx->confdir);
+       if (glob(dir, GLOB_MARK, NULL, &globbuf) != 0)
                UCI_THROW(ctx, UCI_ERR_NOTFOUND);
 
        size = sizeof(char *) * (globbuf.gl_pathc + 1);
                UCI_THROW(ctx, UCI_ERR_NOTFOUND);
 
        size = sizeof(char *) * (globbuf.gl_pathc + 1);
@@ -1015,6 +1018,7 @@ int uci_list_configs(struct uci_context *ctx, char ***list)
                buf += strlen(buf) + 1;
        }
        *list = configs;
                buf += strlen(buf) + 1;
        }
        *list = configs;
+       free(dir);
 
        return 0;
 }
 
        return 0;
 }
index d4ef8d86656884ed629fc4c454b8e3f837f916d0..b86c976b9daee96bba9ad775f07b49d0c42285ef 100644 (file)
--- a/libuci.c
+++ b/libuci.c
@@ -25,6 +25,9 @@
 #include "uci.h"
 #include "err.h"
 
 #include "uci.h"
 #include "err.h"
 
+static const char *uci_confdir = UCI_CONFDIR;
+static const char *uci_savedir = UCI_SAVEDIR;
+
 static const char *uci_errstr[] = {
        [UCI_OK] =            "Success",
        [UCI_ERR_MEM] =       "Out of memory",
 static const char *uci_errstr[] = {
        [UCI_OK] =            "Success",
        [UCI_ERR_MEM] =       "Out of memory",
@@ -50,6 +53,9 @@ struct uci_context *uci_alloc_context(void)
        uci_list_init(&ctx->root);
        ctx->flags = UCI_FLAG_STRICT;
 
        uci_list_init(&ctx->root);
        ctx->flags = UCI_FLAG_STRICT;
 
+       ctx->confdir = (char *) uci_confdir;
+       ctx->savedir = (char *) uci_savedir;
+
        return ctx;
 }
 
        return ctx;
 }
 
@@ -57,6 +63,11 @@ void uci_free_context(struct uci_context *ctx)
 {
        struct uci_element *e, *tmp;
 
 {
        struct uci_element *e, *tmp;
 
+       if (ctx->confdir != uci_confdir)
+               free(ctx->confdir);
+       if (ctx->savedir != uci_savedir)
+               free(ctx->savedir);
+
        UCI_TRAP_SAVE(ctx, ignore);
        uci_cleanup(ctx);
        uci_foreach_element_safe(&ctx->root, tmp, e) {
        UCI_TRAP_SAVE(ctx, ignore);
        uci_cleanup(ctx);
        uci_foreach_element_safe(&ctx->root, tmp, e) {
@@ -70,6 +81,24 @@ ignore:
        return;
 }
 
        return;
 }
 
+int uci_set_confdir(struct uci_context *ctx, char *dir)
+{
+       dir = uci_strdup(ctx, dir);
+       if (ctx->confdir != uci_confdir)
+               free(ctx->confdir);
+       ctx->confdir = dir;
+       return 0;
+}
+
+int uci_set_savedir(struct uci_context *ctx, char *dir)
+{
+       dir = uci_strdup(ctx, dir);
+       if (ctx->savedir != uci_savedir)
+               free(ctx->savedir);
+       ctx->savedir = dir;
+       return 0;
+}
+
 int uci_cleanup(struct uci_context *ctx)
 {
        UCI_HANDLE_ERR(ctx);
 int uci_cleanup(struct uci_context *ctx)
 {
        UCI_HANDLE_ERR(ctx);
diff --git a/uci.h b/uci.h
index e921269872a114f43611907bbea44983aba0bb51..2a1b73baea6d2c3ebd7bba84caacf176fa8d7fe1 100644 (file)
--- a/uci.h
+++ b/uci.h
@@ -223,11 +223,25 @@ extern int uci_commit(struct uci_context *ctx, struct uci_package **p, bool over
 
 /**
  * uci_list_configs: List available uci config files
 
 /**
  * uci_list_configs: List available uci config files
- *
  * @ctx: uci context
  */
 extern int uci_list_configs(struct uci_context *ctx, char ***list);
 
  * @ctx: uci context
  */
 extern int uci_list_configs(struct uci_context *ctx, char ***list);
 
+/** 
+ * uci_set_savedir: override the default history save directory
+ * @ctx: uci context
+ * @dir: directory name
+ */
+extern int uci_set_savedir(struct uci_context *ctx, char *dir);
+
+/** 
+ * uci_set_savedir: override the default config storage directory
+ * @ctx: uci context
+ * @dir: directory name
+ */
+extern int uci_set_confdir(struct uci_context *ctx, char *dir);
+
+
 /* UCI data structures */
 enum uci_type {
        UCI_TYPE_HISTORY = 0,
 /* UCI data structures */
 enum uci_type {
        UCI_TYPE_HISTORY = 0,
@@ -260,6 +274,9 @@ struct uci_context
        /* uci runtime flags */
        enum uci_flags flags;
 
        /* uci runtime flags */
        enum uci_flags flags;
 
+       char *confdir;
+       char *savedir;
+
        /* private: */
        int errno;
        const char *func;
        /* private: */
        int errno;
        const char *func;