diff --git a/bin/xbps-dgraph/main.c b/bin/xbps-dgraph/main.c index 2ece1e5c..82a04c7d 100644 --- a/bin/xbps-dgraph/main.c +++ b/bin/xbps-dgraph/main.c @@ -479,7 +479,7 @@ main(int argc, char **argv) /* Initialize libxbps */ memset(&xh, 0, sizeof(xh)); - xh.rootdir = rootdir; + strncpy(xh.rootdir, rootdir, sizeof(xh.rootdir)); if ((rv = xbps_init(&xh)) != 0) die("failed to initialize libxbps: %s", strerror(rv)); diff --git a/bin/xbps-install/main.c b/bin/xbps-install/main.c index 1b20687a..2b010933 100644 --- a/bin/xbps-install/main.c +++ b/bin/xbps-install/main.c @@ -187,8 +187,10 @@ main(int argc, char **argv) xh.state_cb = state_cb; xh.fetch_cb = fetch_file_progress_cb; xh.fetch_cb_data = &xfer; - xh.rootdir = rootdir; - xh.cachedir = cachedir; + if (rootdir) + strncpy(xh.rootdir, rootdir, sizeof(xh.rootdir)); + if (cachedir) + strncpy(xh.cachedir, cachedir, sizeof(xh.cachedir)); xh.conffile = conffile; xh.flags = flags; if (flags & XBPS_FLAG_VERBOSE) diff --git a/bin/xbps-pkgdb/main.c b/bin/xbps-pkgdb/main.c index b7e410c0..8b5602b1 100644 --- a/bin/xbps-pkgdb/main.c +++ b/bin/xbps-pkgdb/main.c @@ -135,7 +135,8 @@ main(int argc, char **argv) usage(true); memset(&xh, 0, sizeof(xh)); - xh.rootdir = rootdir; + if (rootdir) + strncpy(xh.rootdir, rootdir, sizeof(xh.rootdir)); xh.conffile = conffile; xh.flags = flags; diff --git a/bin/xbps-query/main.c b/bin/xbps-query/main.c index 69fc7617..d7770109 100644 --- a/bin/xbps-query/main.c +++ b/bin/xbps-query/main.c @@ -193,8 +193,10 @@ main(int argc, char **argv) /* * Initialize libxbps. */ - xh.rootdir = rootdir; - xh.cachedir = cachedir; + if (rootdir) + strncpy(xh.rootdir, rootdir, sizeof(xh.rootdir)); + if (cachedir) + strncpy(xh.cachedir, cachedir, sizeof(xh.cachedir)); xh.conffile = conffile; xh.flags = flags; diff --git a/bin/xbps-reconfigure/main.c b/bin/xbps-reconfigure/main.c index 96b76590..0702308a 100644 --- a/bin/xbps-reconfigure/main.c +++ b/bin/xbps-reconfigure/main.c @@ -145,7 +145,8 @@ main(int argc, char **argv) memset(&xh, 0, sizeof(xh)); xh.state_cb = state_cb; - xh.rootdir = rootdir; + if (rootdir) + strncpy(xh.rootdir, rootdir, sizeof(xh.rootdir)); xh.conffile = conffile; xh.flags = flags; diff --git a/bin/xbps-remove/main.c b/bin/xbps-remove/main.c index 24ff086d..d39ee9ce 100644 --- a/bin/xbps-remove/main.c +++ b/bin/xbps-remove/main.c @@ -310,8 +310,10 @@ main(int argc, char **argv) */ memset(&xh, 0, sizeof(xh)); xh.state_cb = state_cb_rm; - xh.rootdir = rootdir; - xh.cachedir = cachedir; + if (rootdir) + strncpy(xh.rootdir, rootdir, sizeof(xh.rootdir)); + if (cachedir) + strncpy(xh.cachedir, cachedir, sizeof(xh.cachedir)); xh.conffile = conffile; xh.flags = flags; diff --git a/bin/xbps-uhelper/main.c b/bin/xbps-uhelper/main.c index 1c9ca184..3f0a1aa9 100644 --- a/bin/xbps-uhelper/main.c +++ b/bin/xbps-uhelper/main.c @@ -128,7 +128,8 @@ main(int argc, char **argv) xh.flags = flags; xh.fetch_cb = fetch_file_progress_cb; xh.fetch_cb_data = &xfer; - xh.rootdir = rootdir; + if (rootdir) + strncpy(xh.rootdir, rootdir, sizeof(xh.rootdir)); xh.conffile = confdir; if ((rv = xbps_init(&xh)) != 0) { xbps_error_printf("xbps-uhelper: failed to " diff --git a/include/xbps.h.in b/include/xbps.h.in index c73bb438..10f0fc1e 100644 --- a/include/xbps.h.in +++ b/include/xbps.h.in @@ -29,6 +29,7 @@ #include #include +#include #include #include @@ -46,7 +47,7 @@ * * This header documents the full API for the XBPS Library. */ -#define XBPS_API_VERSION "20131216-1" +#define XBPS_API_VERSION "20131216-2" #ifndef XBPS_VERSION #define XBPS_VERSION "UNSET" @@ -529,27 +530,6 @@ struct xbps_handle { * the \a xbps_fetch_cb function callback. */ void *fetch_cb_data; - /** - * @var rootdir - * - * Root directory for all operations in XBPS. If NULL, - * by default it's set to /. - */ - const char *rootdir; - /** - * @var cachedir - * - * Cache directory to store downloaded binary packages. - * If NULL default value in \a XBPS_CACHE_PATH is used. - */ - const char *cachedir; - /** - * @var metadir - * - * Metadata directory for all operations in XBPS. - * If NULL, defaults to \a XBPS_CACHE_PATH (relative to rootdir). - */ - const char *metadir; /** * @var conffile * @@ -557,12 +537,38 @@ struct xbps_handle { */ const char *conffile; /** - * @private + * @var target_arch + * + * Target architecture, as set by XBPS_TARGET_ARCH from environment. */ - char *cachedir_priv; - char *metadir_priv; - char *native_arch; const char *target_arch; + /** + * @var rootdir + * + * Root directory for all operations in XBPS. + * If unset, defaults to '/'. + */ + char rootdir[PATH_MAX-1]; + /** + * @var cachedir + * + * Cache directory to store downloaded binary packages. + * If unset, defaults to \a XBPS_CACHE_PATH (relative to rootdir). + */ + char cachedir[PATH_MAX-1]; + /** + * @var metadir + * + * Metadata directory for all operations in XBPS. + * If unset, defaults to \a XBPS_CACHE_PATH (relative to rootdir). + */ + char metadir[PATH_MAX-1]; + /** + * @var native_arch + * + * Machine architecture, as returned by uname(2)::machine. + */ + char native_arch[16]; /** * @var fetch_timeout * diff --git a/include/xbps_api_impl.h b/include/xbps_api_impl.h index fcb2dd4a..861906f0 100644 --- a/include/xbps_api_impl.h +++ b/include/xbps_api_impl.h @@ -45,6 +45,7 @@ #include "queue.h" #include "fetch.h" +#include "compat.h" #define EXTRACT_FLAGS ARCHIVE_EXTRACT_SECURE_NODOTDOT | \ ARCHIVE_EXTRACT_SECURE_SYMLINKS diff --git a/lib/initend.c b/lib/initend.c index 93958154..d00fe566 100644 --- a/lib/initend.c +++ b/lib/initend.c @@ -46,35 +46,6 @@ * Use these functions to initialize some parameters before start * using libxbps and finalize usage to release resources at the end. */ -static char * -set_cachedir(struct xbps_handle *xh) -{ - if (xh->cachedir[0] == '/') { - /* full path */ - return strdup(xh->cachedir); - } else { - /* relative to rootdir */ - if (strcmp(xh->rootdir, "/") == 0) - return xbps_xasprintf("/%s", xh->cachedir); - else - return xbps_xasprintf("%s/%s", xh->rootdir, - xh->cachedir); - } -} - -static char * -set_metadir(struct xbps_handle *xh) -{ - if (xh->metadir == NULL) { - if (strcmp(xh->rootdir, "/") == 0) - return xbps_xasprintf("/%s", XBPS_META_PATH); - else - return xbps_xasprintf("%s/%s", xh->rootdir, XBPS_META_PATH); - } else { - return strdup(xh->metadir); - } -} - static int cb_validate_virtual(cfg_t *cfg, cfg_opt_t *opt) { @@ -115,6 +86,7 @@ xbps_init(struct xbps_handle *xhp) CFG_END() }; struct utsname un; + char *buf; const char *repodir; int rv, cc, cch; bool syslog_enabled = false; @@ -150,55 +122,57 @@ xbps_init(struct xbps_handle *xhp) } xbps_dbg_printf(xhp, "Configuration file: %s\n", xhp->conffile ? xhp->conffile : "not found"); - /* - * Respect client setting in struct xbps_handle for {root,cache}dir; - * otherwise use values from configuration file or defaults if unset. - */ - if (xhp->rootdir == NULL) { - if (xhp->cfg == NULL) - xhp->rootdir = "/"; - else - xhp->rootdir = cfg_getstr(xhp->cfg, "rootdir"); - } else { - if (xhp->rootdir[0] != '/') { - /* relative path */ - char *buf, path[PATH_MAX-1]; - if (getcwd(path, sizeof(path)) == NULL) - return ENOTSUP; - - buf = xbps_xasprintf("%s/%s", path, xhp->rootdir); - xhp->rootdir = buf; + /* Set rootdir */ + if (xhp->rootdir[0] == '\0') { + if (xhp->cfg != NULL) { + strlcpy(xhp->rootdir, + cfg_getstr(xhp->cfg, "rootdir"), + sizeof(xhp->rootdir)); + } else { + xhp->rootdir[0] = '/'; + xhp->rootdir[1] = '\0'; } - } - /* - * Append repository list specified in configuration file. - */ - for (unsigned int i = 0; i < cfg_size(xhp->cfg, "repositories"); i++) { - if (xhp->repositories == NULL) - xhp->repositories = xbps_array_create(); + } else if (xhp->rootdir[0] != '/') { + /* relative path */ + char path[PATH_MAX-1]; - repodir = cfg_getnstr(xhp->cfg, "repositories", i); - xbps_array_add_cstring_nocopy(xhp->repositories, repodir); - } + if (getcwd(path, sizeof(path)) == NULL) + return ENOTSUP; - if (xhp->cachedir == NULL) { - if (xhp->cfg == NULL) - xhp->cachedir = XBPS_CACHE_PATH; - else - xhp->cachedir = cfg_getstr(xhp->cfg, "cachedir"); + buf = strdup(xhp->rootdir); + snprintf(xhp->rootdir, sizeof(xhp->rootdir), + "%s/%s", path, buf); + free(buf); + } + /* Set cachedir */ + if (xhp->cachedir[0] == '\0') { + snprintf(xhp->cachedir, sizeof(xhp->cachedir), + "%s/%s", strcmp(xhp->rootdir, "/") ? xhp->rootdir : "", + xhp->cfg ? cfg_getstr(xhp->cfg, "cachedir") : XBPS_CACHE_PATH); + } else if (xhp->cachedir[0] != '/') { + /* relative path */ + buf = strdup(xhp->cachedir); + snprintf(xhp->cachedir, sizeof(xhp->cachedir), + "%s/%s", strcmp(xhp->rootdir, "/") ? xhp->rootdir : "", buf); + free(buf); + } + /* Set metadir */ + if (xhp->metadir[0] == '\0') { + snprintf(xhp->metadir, sizeof(xhp->metadir), + "%s/%s", strcmp(xhp->rootdir, "/") ? xhp->rootdir : "", + XBPS_META_PATH); + } else if (xhp->metadir[0] != '/') { + /* relative path */ + buf = strdup(xhp->metadir); + snprintf(xhp->metadir, sizeof(xhp->metadir), + "%s/%s", strcmp(xhp->rootdir, "/") ? xhp->rootdir : "", buf); + free(buf); } - if ((xhp->cachedir_priv = set_cachedir(xhp)) == NULL) - return ENOMEM; - xhp->cachedir = xhp->cachedir_priv; - - if ((xhp->metadir_priv = set_metadir(xhp)) == NULL) - return ENOMEM; - xhp->metadir = xhp->metadir_priv; xhp->target_arch = getenv("XBPS_TARGET_ARCH"); uname(&un); - xhp->native_arch = strdup(un.machine); + strlcpy(xhp->native_arch, un.machine, sizeof(xhp->native_arch)); assert(xhp->native_arch); if (xhp->cfg == NULL) { @@ -228,6 +202,17 @@ xbps_init(struct xbps_handle *xhp) xbps_dbg_printf(xhp, "Architecture: %s\n", xhp->native_arch); xbps_dbg_printf(xhp, "Target Architecture: %s\n", xhp->target_arch); + /* + * Append repository list specified in configuration file. + */ + for (unsigned int i = 0; i < cfg_size(xhp->cfg, "repositories"); i++) { + if (xhp->repositories == NULL) + xhp->repositories = xbps_array_create(); + + xbps_array_add_cstring_nocopy(xhp->repositories, + cfg_getnstr(xhp->cfg, "repositories", i)); + } + if (xhp->flags & XBPS_FLAG_DEBUG) { for (unsigned int i = 0; i < xbps_array_count(xhp->repositories); i++) { xbps_array_get_cstring_nocopy(xhp->repositories, i, &repodir); @@ -256,10 +241,6 @@ xbps_end(struct xbps_handle *xhp) xbps_fetch_unset_cache_connection(); cfg_free(xhp->cfg); - free(xhp->cachedir_priv); - free(xhp->metadir_priv); - free(xhp->native_arch); - xhp->initialized = false; }