2010-11-19 18:10:13 +05:30
|
|
|
/*-
|
2015-01-10 16:29:31 +05:30
|
|
|
* Copyright (c) 2011-2015 Juan Romero Pardines.
|
2014-05-06 12:59:26 +05:30
|
|
|
* Copyright (c) 2014 Enno Boland.
|
2010-11-19 18:10:13 +05:30
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2012-11-19 20:12:24 +05:30
|
|
|
#include <sys/utsname.h>
|
2010-11-19 18:10:13 +05:30
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "xbps_api_impl.h"
|
|
|
|
|
2011-01-22 18:54:51 +05:30
|
|
|
/**
|
|
|
|
* @file lib/initend.c
|
|
|
|
* @brief Initialization and finalization routines
|
|
|
|
* @defgroup initend Initialization and finalization functions
|
|
|
|
*
|
2011-02-21 22:12:47 +05:30
|
|
|
* Use these functions to initialize some parameters before start
|
2011-01-22 18:54:51 +05:30
|
|
|
* using libxbps and finalize usage to release resources at the end.
|
|
|
|
*/
|
2015-09-15 12:56:07 +05:30
|
|
|
|
2011-06-04 17:07:53 +05:30
|
|
|
int
|
2012-06-14 11:52:11 +05:30
|
|
|
xbps_init(struct xbps_handle *xhp)
|
2010-11-19 18:10:13 +05:30
|
|
|
{
|
2019-06-27 20:39:43 +05:30
|
|
|
char *buf = NULL;
|
|
|
|
const char *repodir = NULL, *native_arch = NULL;
|
2019-06-18 18:41:56 +05:30
|
|
|
int rv = 0;
|
2019-02-23 11:45:56 +05:30
|
|
|
size_t size;
|
2011-06-04 17:07:53 +05:30
|
|
|
|
2012-06-14 11:52:11 +05:30
|
|
|
assert(xhp != NULL);
|
2011-02-21 22:12:47 +05:30
|
|
|
|
2013-12-16 16:16:39 +05:30
|
|
|
/* Set rootdir */
|
|
|
|
if (xhp->rootdir[0] == '\0') {
|
2014-02-25 21:12:52 +05:30
|
|
|
xhp->rootdir[0] = '/';
|
|
|
|
xhp->rootdir[1] = '\0';
|
2013-12-16 16:16:39 +05:30
|
|
|
} else if (xhp->rootdir[0] != '/') {
|
2019-03-06 19:04:23 +05:30
|
|
|
char cwd[PATH_MAX-1];
|
|
|
|
/* get cwd */
|
|
|
|
if (getcwd(cwd, sizeof(cwd)) == NULL)
|
|
|
|
return ENOTSUP;
|
2013-12-16 16:16:39 +05:30
|
|
|
buf = strdup(xhp->rootdir);
|
2019-02-23 11:45:56 +05:30
|
|
|
if (!buf)
|
|
|
|
return ENOMEM;
|
|
|
|
size = sizeof(xhp->rootdir);
|
|
|
|
rv = snprintf(xhp->rootdir, size, "%s/%s", cwd, buf);
|
2013-12-16 16:16:39 +05:30
|
|
|
free(buf);
|
2019-02-23 11:45:56 +05:30
|
|
|
if (rv < 0 || (size_t)rv >= size)
|
|
|
|
return 1;
|
2013-12-16 16:16:39 +05:30
|
|
|
}
|
2019-03-06 19:04:23 +05:30
|
|
|
|
2014-08-01 18:39:51 +05:30
|
|
|
xbps_dbg_printf(xhp, "%s\n", XBPS_RELVER);
|
2019-03-06 19:04:23 +05:30
|
|
|
|
2014-11-06 14:28:04 +05:30
|
|
|
/* set confdir */
|
|
|
|
if (xhp->confdir[0] == '\0') {
|
2019-02-23 11:45:56 +05:30
|
|
|
size = sizeof(xhp->confdir);
|
|
|
|
rv = snprintf(xhp->confdir, size,
|
2014-11-06 14:28:04 +05:30
|
|
|
"%s%s", strcmp(xhp->rootdir, "/") ? xhp->rootdir : "",
|
|
|
|
XBPS_SYSCONF_PATH);
|
2019-02-23 11:45:56 +05:30
|
|
|
if (rv < 0 || (size_t)rv >= size)
|
|
|
|
return 1;
|
2014-11-06 14:28:04 +05:30
|
|
|
} else if (xhp->confdir[0] != '/') {
|
|
|
|
/* relative path */
|
|
|
|
buf = strdup(xhp->confdir);
|
2019-02-23 11:45:56 +05:30
|
|
|
if (!buf)
|
|
|
|
return ENOMEM;
|
|
|
|
size = sizeof(xhp->confdir);
|
|
|
|
rv = snprintf(xhp->confdir, size, "%s/%s",
|
|
|
|
strcmp(xhp->rootdir, "/") ? xhp->rootdir : "", buf);
|
2014-11-06 14:28:04 +05:30
|
|
|
free(buf);
|
2019-02-23 11:45:56 +05:30
|
|
|
if (rv < 0 || (size_t)rv >= size)
|
|
|
|
return 1;
|
2014-11-06 14:28:04 +05:30
|
|
|
}
|
2019-03-06 19:04:23 +05:30
|
|
|
|
2014-11-06 14:28:04 +05:30
|
|
|
/* set sysconfdir */
|
2019-03-06 19:04:23 +05:30
|
|
|
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;
|
|
|
|
}
|
2014-05-31 10:14:07 +05:30
|
|
|
|
2019-03-06 19:04:23 +05:30
|
|
|
/* set architecture and target architecture */
|
2013-03-07 22:38:12 +05:30
|
|
|
xhp->target_arch = getenv("XBPS_TARGET_ARCH");
|
2014-01-09 16:11:25 +05:30
|
|
|
if ((native_arch = getenv("XBPS_ARCH")) != NULL) {
|
2019-03-06 19:04:23 +05:30
|
|
|
xbps_strlcpy(xhp->native_arch, native_arch, sizeof (xhp->native_arch));
|
2014-01-09 16:11:25 +05:30
|
|
|
} else {
|
2019-03-06 19:04:23 +05:30
|
|
|
struct utsname un;
|
|
|
|
if (uname(&un) == -1)
|
|
|
|
return ENOTSUP;
|
|
|
|
xbps_strlcpy(xhp->native_arch, un.machine, sizeof (xhp->native_arch));
|
2014-01-09 16:11:25 +05:30
|
|
|
}
|
2013-03-07 22:38:12 +05:30
|
|
|
assert(xhp->native_arch);
|
2012-06-15 19:03:11 +05:30
|
|
|
|
2014-02-25 21:12:52 +05:30
|
|
|
xbps_fetch_set_cache_connection(XBPS_FETCH_CACHECONN, XBPS_FETCH_CACHECONN_HOST);
|
|
|
|
|
2019-03-06 19:04:23 +05:30
|
|
|
/* process xbps.d directories */
|
|
|
|
if ((rv = xbps_conf_init(xhp)) != 0)
|
2014-11-06 14:28:04 +05:30
|
|
|
return rv;
|
|
|
|
|
2015-09-15 12:56:07 +05:30
|
|
|
/* Set cachedir */
|
|
|
|
if (xhp->cachedir[0] == '\0') {
|
2019-02-23 11:45:56 +05:30
|
|
|
size = sizeof(xhp->cachedir);
|
|
|
|
rv = snprintf(xhp->cachedir, size,
|
2015-09-15 12:56:07 +05:30
|
|
|
"%s/%s", strcmp(xhp->rootdir, "/") ? xhp->rootdir : "",
|
|
|
|
XBPS_CACHE_PATH);
|
2019-02-23 11:45:56 +05:30
|
|
|
if (rv < 0 || (size_t)rv >= size)
|
|
|
|
return 1;
|
2015-09-15 12:56:07 +05:30
|
|
|
} else if (xhp->cachedir[0] != '/') {
|
|
|
|
/* relative path */
|
|
|
|
buf = strdup(xhp->cachedir);
|
2019-02-23 11:45:56 +05:30
|
|
|
if (!buf)
|
|
|
|
return ENOMEM;
|
|
|
|
size = sizeof(xhp->cachedir);
|
|
|
|
rv = snprintf(xhp->cachedir, size,
|
2015-09-15 12:56:07 +05:30
|
|
|
"%s/%s", strcmp(xhp->rootdir, "/") ? xhp->rootdir : "", buf);
|
|
|
|
free(buf);
|
2019-02-23 11:45:56 +05:30
|
|
|
if (rv < 0 || (size_t)rv >= size)
|
|
|
|
return 1;
|
2015-09-15 12:56:07 +05:30
|
|
|
}
|
|
|
|
/* Set metadir */
|
|
|
|
if (xhp->metadir[0] == '\0') {
|
2019-02-23 11:45:56 +05:30
|
|
|
size = sizeof(xhp->metadir);
|
|
|
|
rv = snprintf(xhp->metadir, size,
|
2015-09-15 12:56:07 +05:30
|
|
|
"%s/%s", strcmp(xhp->rootdir, "/") ? xhp->rootdir : "",
|
|
|
|
XBPS_META_PATH);
|
2019-02-23 11:45:56 +05:30
|
|
|
if (rv < 0 || (size_t)rv >= size)
|
|
|
|
return 1;
|
2015-09-15 12:56:07 +05:30
|
|
|
} else if (xhp->metadir[0] != '/') {
|
|
|
|
/* relative path */
|
|
|
|
buf = strdup(xhp->metadir);
|
2019-02-23 11:45:56 +05:30
|
|
|
if (!buf)
|
|
|
|
return ENOMEM;
|
|
|
|
size = sizeof(xhp->metadir);
|
|
|
|
rv = snprintf(xhp->metadir, size,
|
2015-09-15 12:56:07 +05:30
|
|
|
"%s/%s", strcmp(xhp->rootdir, "/") ? xhp->rootdir : "", buf);
|
|
|
|
free(buf);
|
2019-02-23 11:45:56 +05:30
|
|
|
if (rv < 0 || (size_t)rv >= size)
|
|
|
|
return 1;
|
2015-09-15 12:56:07 +05:30
|
|
|
}
|
|
|
|
|
2014-02-25 21:12:52 +05:30
|
|
|
xbps_dbg_printf(xhp, "rootdir=%s\n", xhp->rootdir);
|
|
|
|
xbps_dbg_printf(xhp, "metadir=%s\n", xhp->metadir);
|
|
|
|
xbps_dbg_printf(xhp, "cachedir=%s\n", xhp->cachedir);
|
2014-11-06 14:28:04 +05:30
|
|
|
xbps_dbg_printf(xhp, "confdir=%s\n", xhp->confdir);
|
2019-03-06 19:04:23 +05:30
|
|
|
xbps_dbg_printf(xhp, "sysconfdir=%s\n", xhp->sysconfdir);
|
2014-06-04 13:09:02 +05:30
|
|
|
xbps_dbg_printf(xhp, "syslog=%s\n", xhp->flags & XBPS_FLAG_DISABLE_SYSLOG ? "false" : "true");
|
2014-11-07 14:48:01 +05:30
|
|
|
xbps_dbg_printf(xhp, "bestmatching=%s\n", xhp->flags & XBPS_FLAG_BESTMATCH ? "true" : "false");
|
2013-03-07 22:38:12 +05:30
|
|
|
xbps_dbg_printf(xhp, "Architecture: %s\n", xhp->native_arch);
|
|
|
|
xbps_dbg_printf(xhp, "Target Architecture: %s\n", xhp->target_arch);
|
2011-10-17 17:54:30 +05:30
|
|
|
|
2013-09-18 20:15:05 +05:30
|
|
|
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);
|
|
|
|
xbps_dbg_printf(xhp, "Repository[%u]=%s\n", i, repodir);
|
|
|
|
}
|
|
|
|
}
|
2019-03-06 19:04:23 +05:30
|
|
|
|
2011-06-04 17:07:53 +05:30
|
|
|
return 0;
|
2010-11-19 18:10:13 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-06-14 11:52:11 +05:30
|
|
|
xbps_end(struct xbps_handle *xhp)
|
2010-11-19 18:10:13 +05:30
|
|
|
{
|
2012-09-24 14:20:23 +05:30
|
|
|
assert(xhp);
|
|
|
|
|
2012-01-20 15:40:52 +05:30
|
|
|
xbps_pkgdb_release(xhp);
|
2011-02-21 18:08:44 +05:30
|
|
|
}
|