From 3ad575178eedeb180178dd6e809a41047880c17a Mon Sep 17 00:00:00 2001 From: Duncan Overbruck Date: Fri, 14 Feb 2020 13:21:35 +0100 Subject: [PATCH] lib/initend.c: use new xbps_path_* functions --- lib/initend.c | 148 +++++++++++++++++++++----------------------------- 1 file changed, 62 insertions(+), 86 deletions(-) diff --git a/lib/initend.c b/lib/initend.c index fc053423..d3b7e1d1 100644 --- a/lib/initend.c +++ b/lib/initend.c @@ -43,89 +43,76 @@ int xbps_init(struct xbps_handle *xhp) { - char *buf = NULL; - const char *repodir = NULL, *native_arch = NULL; + const char *native_arch = NULL; int rv = 0; - size_t size; assert(xhp != NULL); + xbps_dbg_printf(xhp, "%s\n", XBPS_RELVER); + /* Set rootdir */ if (xhp->rootdir[0] == '\0') { xhp->rootdir[0] = '/'; xhp->rootdir[1] = '\0'; } else if (xhp->rootdir[0] != '/') { - char cwd[PATH_MAX-1]; - /* get cwd */ - if (getcwd(cwd, sizeof(cwd)) == NULL) - return ENOTSUP; - buf = strdup(xhp->rootdir); - if (!buf) - return ENOMEM; - size = sizeof(xhp->rootdir); - rv = snprintf(xhp->rootdir, size, "%s/%s", cwd, buf); - free(buf); - if (rv < 0 || (size_t)rv >= size) - return 1; - } + char cwd[XBPS_MAXPATH]; - xbps_dbg_printf(xhp, "%s\n", XBPS_RELVER); + if (getcwd(cwd, sizeof(cwd)) == NULL) + return ENOBUFS; + if (xbps_path_prepend(xhp->rootdir, sizeof xhp->rootdir, cwd) == -1) + return ENOBUFS; + } + if (xbps_path_clean(xhp->rootdir) == -1) + return ENOTSUP; /* set confdir */ if (xhp->confdir[0] == '\0') { - size = sizeof(xhp->confdir); - rv = snprintf(xhp->confdir, size, - "%s%s", strcmp(xhp->rootdir, "/") ? xhp->rootdir : "", - XBPS_SYSCONF_PATH); - if (rv < 0 || (size_t)rv >= size) - return 1; + if (xbps_path_join(xhp->confdir, sizeof xhp->confdir, + xhp->rootdir, XBPS_SYSCONF_PATH, (char *)NULL) == -1) + return ENOBUFS; } else if (xhp->confdir[0] != '/') { /* relative path */ - buf = strdup(xhp->confdir); - if (!buf) - return ENOMEM; - size = sizeof(xhp->confdir); - rv = snprintf(xhp->confdir, size, "%s/%s", - strcmp(xhp->rootdir, "/") ? xhp->rootdir : "", buf); - free(buf); - if (rv < 0 || (size_t)rv >= size) - return 1; + if (xbps_path_prepend(xhp->confdir, sizeof xhp->confdir, + xhp->rootdir) == -1) + return ENOBUFS; } + if (xbps_path_clean(xhp->confdir) == -1) + return ENOTSUP; /* set sysconfdir */ if (xhp->sysconfdir[0] == '\0') { - size = sizeof(xhp->sysconfdir); - snprintf(xhp->sysconfdir, size, - "%s%s", strcmp(xhp->rootdir, "/") ? xhp->rootdir : "", - XBPS_SYSDEFCONF_PATH); - if (rv < 0 || (size_t)rv >= size) - return 1; + if (xbps_path_join(xhp->sysconfdir, sizeof xhp->sysconfdir, + xhp->rootdir, XBPS_SYSDEFCONF_PATH, (char *)NULL) == -1) + return ENOBUFS; } + if (xbps_path_clean(xhp->sysconfdir) == -1) + return ENOTSUP; - /* set architecture and target architecture */ + /* target architecture */ xhp->target_arch = getenv("XBPS_TARGET_ARCH"); - if ((native_arch = getenv("XBPS_ARCH")) != NULL) { - xbps_strlcpy(xhp->native_arch, native_arch, sizeof (xhp->native_arch)); + if (xhp->target_arch && *xhp->target_arch == '\0') + xhp->target_arch = NULL; + + /* native architecture */ + if ((native_arch = getenv("XBPS_ARCH")) && *native_arch != '\0') { + if (xbps_strlcpy(xhp->native_arch, native_arch, + sizeof xhp->native_arch) >= sizeof xhp->native_arch) + return ENOBUFS; } else { -#if defined(__linux__) && !defined(__GLIBC__) - /* musl libc on linux */ - char *s = NULL; -#endif struct utsname un; if (uname(&un) == -1) return ENOTSUP; + if (xbps_strlcpy(xhp->native_arch, un.machine, + sizeof xhp->native_arch) >= sizeof xhp->native_arch) + return ENOBUFS; #if defined(__linux__) && !defined(__GLIBC__) - /* musl libc on linux */ - s = xbps_xasprintf("%s-musl", un.machine); - assert(s); - xbps_strlcpy(xhp->native_arch, s, sizeof(xhp->native_arch)); - free(s); -#else - /* glibc or any other os */ - xbps_strlcpy(xhp->native_arch, un.machine, sizeof (xhp->native_arch)); + /* musl libc on linux, just append -musl */ + if (xbps_strlcat(xhp->native_arch, "-musl", + sizeof xhp->native_arch) >= sizeof xhp->native_arch) + return ENOBUFS; #endif } - assert(xhp->native_arch); + assert(*xhp->native_arch); xbps_fetch_set_cache_connection(XBPS_FETCH_CACHECONN, XBPS_FETCH_CACHECONN_HOST); @@ -135,44 +122,31 @@ xbps_init(struct xbps_handle *xhp) /* Set cachedir */ if (xhp->cachedir[0] == '\0') { - size = sizeof(xhp->cachedir); - rv = snprintf(xhp->cachedir, size, - "%s/%s", strcmp(xhp->rootdir, "/") ? xhp->rootdir : "", - XBPS_CACHE_PATH); - if (rv < 0 || (size_t)rv >= size) - return 1; + if (xbps_path_join(xhp->cachedir, sizeof xhp->cachedir, + xhp->rootdir, XBPS_CACHE_PATH, (char *)NULL) == -1) + return ENOBUFS; } else if (xhp->cachedir[0] != '/') { /* relative path */ - buf = strdup(xhp->cachedir); - if (!buf) - return ENOMEM; - size = sizeof(xhp->cachedir); - rv = snprintf(xhp->cachedir, size, - "%s/%s", strcmp(xhp->rootdir, "/") ? xhp->rootdir : "", buf); - free(buf); - if (rv < 0 || (size_t)rv >= size) - return 1; + if (xbps_path_prepend(xhp->cachedir, sizeof xhp->cachedir, + xhp->rootdir) == -1) + return ENOBUFS; } + if (xbps_path_clean(xhp->cachedir) == -1) + return ENOTSUP; + /* Set metadir */ if (xhp->metadir[0] == '\0') { - size = sizeof(xhp->metadir); - rv = snprintf(xhp->metadir, size, - "%s/%s", strcmp(xhp->rootdir, "/") ? xhp->rootdir : "", - XBPS_META_PATH); - if (rv < 0 || (size_t)rv >= size) - return 1; + if (xbps_path_join(xhp->metadir, sizeof xhp->metadir, + xhp->rootdir, XBPS_META_PATH, (char *)NULL) == -1) + return ENOBUFS; } else if (xhp->metadir[0] != '/') { /* relative path */ - buf = strdup(xhp->metadir); - if (!buf) - return ENOMEM; - size = sizeof(xhp->metadir); - rv = snprintf(xhp->metadir, size, - "%s/%s", strcmp(xhp->rootdir, "/") ? xhp->rootdir : "", buf); - free(buf); - if (rv < 0 || (size_t)rv >= size) - return 1; + if (xbps_path_prepend(xhp->metadir, sizeof xhp->metadir, + xhp->rootdir) == -1) + return ENOBUFS; } + if (xbps_path_clean(xhp->metadir) == -1) + return ENOTSUP; xbps_dbg_printf(xhp, "rootdir=%s\n", xhp->rootdir); xbps_dbg_printf(xhp, "metadir=%s\n", xhp->metadir); @@ -183,11 +157,13 @@ xbps_init(struct xbps_handle *xhp) xbps_dbg_printf(xhp, "bestmatching=%s\n", xhp->flags & XBPS_FLAG_BESTMATCH ? "true" : "false"); xbps_dbg_printf(xhp, "keepconf=%s\n", xhp->flags & XBPS_FLAG_KEEP_CONFIG ? "true" : "false"); xbps_dbg_printf(xhp, "Architecture: %s\n", xhp->native_arch); - xbps_dbg_printf(xhp, "Target Architecture: %s\n", xhp->target_arch); + xbps_dbg_printf(xhp, "Target Architecture: %s\n", xhp->target_arch ? xhp->native_arch : "(null)"); if (xhp->flags & XBPS_FLAG_DEBUG) { + const char *repodir; for (unsigned int i = 0; i < xbps_array_count(xhp->repositories); i++) { - xbps_array_get_cstring_nocopy(xhp->repositories, i, &repodir); + if (!xbps_array_get_cstring_nocopy(xhp->repositories, i, &repodir)) + return errno; xbps_dbg_printf(xhp, "Repository[%u]=%s\n", i, repodir); } }