lib/initend.c: use new xbps_path_* functions

This commit is contained in:
Duncan Overbruck 2020-02-14 13:21:35 +01:00 committed by Juan RP
parent 0f61a1a5a2
commit 3ad575178e

View File

@ -43,89 +43,76 @@
int int
xbps_init(struct xbps_handle *xhp) xbps_init(struct xbps_handle *xhp)
{ {
char *buf = NULL; const char *native_arch = NULL;
const char *repodir = NULL, *native_arch = NULL;
int rv = 0; int rv = 0;
size_t size;
assert(xhp != NULL); assert(xhp != NULL);
xbps_dbg_printf(xhp, "%s\n", XBPS_RELVER);
/* Set rootdir */ /* Set rootdir */
if (xhp->rootdir[0] == '\0') { if (xhp->rootdir[0] == '\0') {
xhp->rootdir[0] = '/'; xhp->rootdir[0] = '/';
xhp->rootdir[1] = '\0'; xhp->rootdir[1] = '\0';
} else if (xhp->rootdir[0] != '/') { } else if (xhp->rootdir[0] != '/') {
char cwd[PATH_MAX-1]; char cwd[XBPS_MAXPATH];
/* 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;
}
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 */ /* set confdir */
if (xhp->confdir[0] == '\0') { if (xhp->confdir[0] == '\0') {
size = sizeof(xhp->confdir); if (xbps_path_join(xhp->confdir, sizeof xhp->confdir,
rv = snprintf(xhp->confdir, size, xhp->rootdir, XBPS_SYSCONF_PATH, (char *)NULL) == -1)
"%s%s", strcmp(xhp->rootdir, "/") ? xhp->rootdir : "", return ENOBUFS;
XBPS_SYSCONF_PATH);
if (rv < 0 || (size_t)rv >= size)
return 1;
} else if (xhp->confdir[0] != '/') { } else if (xhp->confdir[0] != '/') {
/* relative path */ /* relative path */
buf = strdup(xhp->confdir); if (xbps_path_prepend(xhp->confdir, sizeof xhp->confdir,
if (!buf) xhp->rootdir) == -1)
return ENOMEM; return ENOBUFS;
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_clean(xhp->confdir) == -1)
return ENOTSUP;
/* set sysconfdir */ /* set sysconfdir */
if (xhp->sysconfdir[0] == '\0') { if (xhp->sysconfdir[0] == '\0') {
size = sizeof(xhp->sysconfdir); if (xbps_path_join(xhp->sysconfdir, sizeof xhp->sysconfdir,
snprintf(xhp->sysconfdir, size, xhp->rootdir, XBPS_SYSDEFCONF_PATH, (char *)NULL) == -1)
"%s%s", strcmp(xhp->rootdir, "/") ? xhp->rootdir : "", return ENOBUFS;
XBPS_SYSDEFCONF_PATH);
if (rv < 0 || (size_t)rv >= size)
return 1;
} }
if (xbps_path_clean(xhp->sysconfdir) == -1)
return ENOTSUP;
/* set architecture and target architecture */ /* target architecture */
xhp->target_arch = getenv("XBPS_TARGET_ARCH"); xhp->target_arch = getenv("XBPS_TARGET_ARCH");
if ((native_arch = getenv("XBPS_ARCH")) != NULL) { if (xhp->target_arch && *xhp->target_arch == '\0')
xbps_strlcpy(xhp->native_arch, native_arch, sizeof (xhp->native_arch)); 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 { } else {
#if defined(__linux__) && !defined(__GLIBC__)
/* musl libc on linux */
char *s = NULL;
#endif
struct utsname un; struct utsname un;
if (uname(&un) == -1) if (uname(&un) == -1)
return ENOTSUP; 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__) #if defined(__linux__) && !defined(__GLIBC__)
/* musl libc on linux */ /* musl libc on linux, just append -musl */
s = xbps_xasprintf("%s-musl", un.machine); if (xbps_strlcat(xhp->native_arch, "-musl",
assert(s); sizeof xhp->native_arch) >= sizeof xhp->native_arch)
xbps_strlcpy(xhp->native_arch, s, sizeof(xhp->native_arch)); return ENOBUFS;
free(s);
#else
/* glibc or any other os */
xbps_strlcpy(xhp->native_arch, un.machine, sizeof (xhp->native_arch));
#endif #endif
} }
assert(xhp->native_arch); assert(*xhp->native_arch);
xbps_fetch_set_cache_connection(XBPS_FETCH_CACHECONN, XBPS_FETCH_CACHECONN_HOST); xbps_fetch_set_cache_connection(XBPS_FETCH_CACHECONN, XBPS_FETCH_CACHECONN_HOST);
@ -135,44 +122,31 @@ xbps_init(struct xbps_handle *xhp)
/* Set cachedir */ /* Set cachedir */
if (xhp->cachedir[0] == '\0') { if (xhp->cachedir[0] == '\0') {
size = sizeof(xhp->cachedir); if (xbps_path_join(xhp->cachedir, sizeof xhp->cachedir,
rv = snprintf(xhp->cachedir, size, xhp->rootdir, XBPS_CACHE_PATH, (char *)NULL) == -1)
"%s/%s", strcmp(xhp->rootdir, "/") ? xhp->rootdir : "", return ENOBUFS;
XBPS_CACHE_PATH);
if (rv < 0 || (size_t)rv >= size)
return 1;
} else if (xhp->cachedir[0] != '/') { } else if (xhp->cachedir[0] != '/') {
/* relative path */ /* relative path */
buf = strdup(xhp->cachedir); if (xbps_path_prepend(xhp->cachedir, sizeof xhp->cachedir,
if (!buf) xhp->rootdir) == -1)
return ENOMEM; return ENOBUFS;
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_clean(xhp->cachedir) == -1)
return ENOTSUP;
/* Set metadir */ /* Set metadir */
if (xhp->metadir[0] == '\0') { if (xhp->metadir[0] == '\0') {
size = sizeof(xhp->metadir); if (xbps_path_join(xhp->metadir, sizeof xhp->metadir,
rv = snprintf(xhp->metadir, size, xhp->rootdir, XBPS_META_PATH, (char *)NULL) == -1)
"%s/%s", strcmp(xhp->rootdir, "/") ? xhp->rootdir : "", return ENOBUFS;
XBPS_META_PATH);
if (rv < 0 || (size_t)rv >= size)
return 1;
} else if (xhp->metadir[0] != '/') { } else if (xhp->metadir[0] != '/') {
/* relative path */ /* relative path */
buf = strdup(xhp->metadir); if (xbps_path_prepend(xhp->metadir, sizeof xhp->metadir,
if (!buf) xhp->rootdir) == -1)
return ENOMEM; return ENOBUFS;
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_clean(xhp->metadir) == -1)
return ENOTSUP;
xbps_dbg_printf(xhp, "rootdir=%s\n", xhp->rootdir); xbps_dbg_printf(xhp, "rootdir=%s\n", xhp->rootdir);
xbps_dbg_printf(xhp, "metadir=%s\n", xhp->metadir); 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, "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, "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, "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) { if (xhp->flags & XBPS_FLAG_DEBUG) {
const char *repodir;
for (unsigned int i = 0; i < xbps_array_count(xhp->repositories); i++) { 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); xbps_dbg_printf(xhp, "Repository[%u]=%s\n", i, repodir);
} }
} }