From c228bf838c82cc7cda868ac75f80d8aab203ca1a Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Sun, 3 Feb 2008 03:48:45 +0100 Subject: [PATCH] allow the user of the library to override the confdir/searchdir --- file.c | 14 +++++++++----- libuci.c | 29 +++++++++++++++++++++++++++++ uci.h | 19 ++++++++++++++++++- 3 files changed, 56 insertions(+), 6 deletions(-) diff --git a/file.c b/file.c index 416422d..4e3e828 100644 --- 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 ((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); @@ -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)); - 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; } @@ -849,7 +849,7 @@ int uci_save(struct uci_context *ctx, struct uci_package *p) 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; @@ -984,10 +984,13 @@ int uci_list_configs(struct uci_context *ctx, char ***list) glob_t globbuf; int size, i; char *buf; + char *dir; 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); @@ -1015,6 +1018,7 @@ int uci_list_configs(struct uci_context *ctx, char ***list) buf += strlen(buf) + 1; } *list = configs; + free(dir); return 0; } diff --git a/libuci.c b/libuci.c index d4ef8d8..b86c976 100644 --- a/libuci.c +++ b/libuci.c @@ -25,6 +25,9 @@ #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", @@ -50,6 +53,9 @@ struct uci_context *uci_alloc_context(void) uci_list_init(&ctx->root); ctx->flags = UCI_FLAG_STRICT; + ctx->confdir = (char *) uci_confdir; + ctx->savedir = (char *) uci_savedir; + return ctx; } @@ -57,6 +63,11 @@ void uci_free_context(struct uci_context *ctx) { 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) { @@ -70,6 +81,24 @@ ignore: 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); diff --git a/uci.h b/uci.h index e921269..2a1b73b 100644 --- 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 - * * @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, @@ -260,6 +274,9 @@ struct uci_context /* uci runtime flags */ enum uci_flags flags; + char *confdir; + char *savedir; + /* private: */ int errno; const char *func; -- 2.11.0