2010-11-19 18:10:13 +05:30
|
|
|
/*-
|
2012-01-17 20:47:03 +05:30
|
|
|
* Copyright (c) 2011-2012 Juan Romero Pardines.
|
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>
|
|
|
|
#include <sys/types.h>
|
2010-11-19 18:10:13 +05:30
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdarg.h>
|
2012-11-19 20:12:24 +05:30
|
|
|
#include <dirent.h>
|
2010-11-19 18:10:13 +05:30
|
|
|
|
|
|
|
#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.
|
|
|
|
*/
|
2012-03-13 14:52:35 +05:30
|
|
|
static char *
|
|
|
|
set_cachedir(struct xbps_handle *xh)
|
2011-12-15 18:54:10 +05:30
|
|
|
{
|
2012-03-13 14:52:35 +05:30
|
|
|
if (xh->cachedir[0] == '/') {
|
2011-12-15 18:54:10 +05:30
|
|
|
/* full path */
|
2012-03-13 14:52:35 +05:30
|
|
|
return strdup(xh->cachedir);
|
|
|
|
} else {
|
2011-12-15 18:54:10 +05:30
|
|
|
/* relative to rootdir */
|
|
|
|
if (strcmp(xh->rootdir, "/") == 0)
|
2012-03-13 14:52:35 +05:30
|
|
|
return xbps_xasprintf("/%s", xh->cachedir);
|
2011-12-15 18:54:10 +05:30
|
|
|
else
|
2012-03-13 14:52:35 +05:30
|
|
|
return xbps_xasprintf("%s/%s", xh->rootdir,
|
2011-12-15 18:54:10 +05:30
|
|
|
xh->cachedir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-13 14:52:35 +05:30
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-19 20:12:24 +05:30
|
|
|
static void
|
|
|
|
config_inject_vpkgs(struct xbps_handle *xh)
|
|
|
|
{
|
|
|
|
DIR *dirp;
|
|
|
|
struct dirent *dp;
|
|
|
|
char *ext, *vpkgdir;
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
if (strcmp(xh->rootdir, "/"))
|
|
|
|
vpkgdir = xbps_xasprintf("%s/etc/xbps/virtualpkg.d",
|
|
|
|
xh->rootdir);
|
|
|
|
else
|
|
|
|
vpkgdir = strdup("/etc/xbps/virtualpkg.d");
|
|
|
|
|
|
|
|
if ((dirp = opendir(vpkgdir)) == NULL) {
|
|
|
|
xbps_dbg_printf(xh, "config: failed to open %s: %s\n",
|
|
|
|
vpkgdir, strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((dp = readdir(dirp)) != NULL) {
|
|
|
|
if ((strcmp(dp->d_name, "..") == 0) ||
|
|
|
|
(strcmp(dp->d_name, ".") == 0))
|
|
|
|
continue;
|
|
|
|
/* only process .conf files, ignore something else */
|
|
|
|
if ((ext = strrchr(dp->d_name, '.')) == NULL)
|
|
|
|
continue;
|
|
|
|
if (strcmp(ext, ".conf") == 0) {
|
|
|
|
char *path;
|
|
|
|
|
|
|
|
path = xbps_xasprintf("%s/%s", vpkgdir, dp->d_name);
|
|
|
|
fp = fopen(path, "r");
|
|
|
|
assert(fp);
|
|
|
|
free(path);
|
|
|
|
if (cfg_parse_fp(xh->cfg, fp) != 0) {
|
|
|
|
xbps_error_printf("Failed to parse "
|
|
|
|
"vpkg conf file %s:\n", dp->d_name);
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(dirp);
|
|
|
|
free(vpkgdir);
|
|
|
|
}
|
|
|
|
|
2011-12-15 15:49:20 +05:30
|
|
|
static int
|
|
|
|
cb_validate_virtual(cfg_t *cfg, cfg_opt_t *opt)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = 0; i < cfg_size(cfg, "virtual-package"); i++) {
|
|
|
|
cfg_t *sec = cfg_opt_getnsec(opt, i);
|
|
|
|
if (cfg_getstr(sec, "targets") == 0) {
|
|
|
|
cfg_error(cfg, "targets must be set for "
|
|
|
|
"virtual-package %s", cfg_title(sec));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2011-10-17 16:07:15 +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
|
|
|
{
|
2011-12-15 15:49:20 +05:30
|
|
|
cfg_opt_t vpkg_opts[] = {
|
2011-12-15 16:00:17 +05:30
|
|
|
CFG_STR_LIST(__UNCONST("targets"), NULL, CFGF_NONE),
|
2011-12-15 15:49:20 +05:30
|
|
|
CFG_END()
|
|
|
|
};
|
|
|
|
cfg_opt_t opts[] = {
|
|
|
|
/* Defaults if not set in configuration file */
|
2011-12-15 16:00:17 +05:30
|
|
|
CFG_STR(__UNCONST("rootdir"), __UNCONST("/"), CFGF_NONE),
|
|
|
|
CFG_STR(__UNCONST("cachedir"),
|
|
|
|
__UNCONST(XBPS_CACHE_PATH), CFGF_NONE),
|
2011-12-24 05:35:26 +05:30
|
|
|
CFG_INT(__UNCONST("FetchCacheConnections"),
|
2011-12-15 15:49:20 +05:30
|
|
|
XBPS_FETCH_CACHECONN, CFGF_NONE),
|
2011-12-24 05:35:26 +05:30
|
|
|
CFG_INT(__UNCONST("FetchCacheConnectionsPerHost"),
|
2011-12-15 15:49:20 +05:30
|
|
|
XBPS_FETCH_CACHECONN_HOST, CFGF_NONE),
|
2011-12-24 05:35:26 +05:30
|
|
|
CFG_INT(__UNCONST("FetchTimeoutConnection"),
|
2011-12-15 15:49:20 +05:30
|
|
|
XBPS_FETCH_TIMEOUT, CFGF_NONE),
|
2011-12-15 16:00:17 +05:30
|
|
|
CFG_BOOL(__UNCONST("syslog"), true, CFGF_NONE),
|
|
|
|
CFG_STR_LIST(__UNCONST("repositories"), NULL, CFGF_MULTI),
|
2012-05-08 12:49:43 +05:30
|
|
|
CFG_STR_LIST(__UNCONST("PackagesOnHold"), NULL, CFGF_MULTI),
|
2011-12-15 16:00:17 +05:30
|
|
|
CFG_SEC(__UNCONST("virtual-package"),
|
2011-12-15 15:49:20 +05:30
|
|
|
vpkg_opts, CFGF_MULTI|CFGF_TITLE),
|
2011-12-15 16:00:17 +05:30
|
|
|
CFG_FUNC(__UNCONST("include"), &cfg_include),
|
2011-12-15 15:49:20 +05:30
|
|
|
CFG_END()
|
|
|
|
};
|
2012-06-15 19:03:11 +05:30
|
|
|
struct utsname un;
|
2011-12-15 15:49:20 +05:30
|
|
|
int rv, cc, cch;
|
2012-01-26 16:13:35 +05:30
|
|
|
bool syslog_enabled = false;
|
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
|
|
|
|
2012-09-24 14:20:23 +05:30
|
|
|
if (xhp->initialized)
|
|
|
|
return 0;
|
|
|
|
|
2011-12-15 15:49:20 +05:30
|
|
|
if (xhp->conffile == NULL)
|
|
|
|
xhp->conffile = XBPS_CONF_DEF;
|
|
|
|
|
|
|
|
/* parse configuration file */
|
|
|
|
xhp->cfg = cfg_init(opts, CFGF_NOCASE);
|
|
|
|
cfg_set_validate_func(xhp->cfg, "virtual-package", &cb_validate_virtual);
|
|
|
|
|
|
|
|
if ((rv = cfg_parse(xhp->cfg, xhp->conffile)) != CFG_SUCCESS) {
|
2012-03-05 21:59:02 +05:30
|
|
|
if (rv == CFG_FILE_ERROR) {
|
2012-09-24 15:33:47 +05:30
|
|
|
/*
|
|
|
|
* Don't error out if config file not found.
|
|
|
|
* If a default repository is set, use it; otherwise
|
|
|
|
* use defaults (no repos and no virtual packages).
|
|
|
|
*/
|
|
|
|
if (errno != ENOENT)
|
2011-12-15 15:49:20 +05:30
|
|
|
return rv;
|
2012-09-24 15:33:47 +05:30
|
|
|
|
2012-03-05 22:03:36 +05:30
|
|
|
xhp->conffile = NULL;
|
2012-09-24 15:33:47 +05:30
|
|
|
if (xhp->repository) {
|
|
|
|
char *buf;
|
|
|
|
|
|
|
|
buf = xbps_xasprintf("repositories = { %s }",
|
|
|
|
xhp->repository);
|
|
|
|
if ((rv = cfg_parse_buf(xhp->cfg, buf)) != 0)
|
|
|
|
return rv;
|
|
|
|
free(buf);
|
|
|
|
}
|
2011-12-15 15:49:20 +05:30
|
|
|
} else if (rv == CFG_PARSE_ERROR) {
|
|
|
|
/*
|
|
|
|
* Parser error from configuration file.
|
|
|
|
*/
|
|
|
|
return ENOTSUP;
|
2011-06-04 19:21:32 +05:30
|
|
|
}
|
2011-06-04 20:38:44 +05:30
|
|
|
}
|
2012-11-19 20:12:24 +05:30
|
|
|
|
2012-06-14 11:52:11 +05:30
|
|
|
xbps_dbg_printf(xhp, "Configuration file: %s\n",
|
2012-03-05 22:03:36 +05:30
|
|
|
xhp->conffile ? xhp->conffile : "not found");
|
2011-06-04 20:38:44 +05:30
|
|
|
/*
|
2011-12-15 15:49:20 +05:30
|
|
|
* Respect client setting in struct xbps_handle for {root,cache}dir;
|
|
|
|
* otherwise use values from configuration file or defaults if unset.
|
2011-06-04 20:38:44 +05:30
|
|
|
*/
|
2011-12-15 15:49:20 +05:30
|
|
|
if (xhp->rootdir == NULL) {
|
|
|
|
if (xhp->cfg == NULL)
|
|
|
|
xhp->rootdir = "/";
|
|
|
|
else
|
|
|
|
xhp->rootdir = cfg_getstr(xhp->cfg, "rootdir");
|
2012-11-19 16:42:38 +05:30
|
|
|
} else {
|
|
|
|
if (xhp->rootdir[0] != '/') {
|
|
|
|
/* relative path */
|
|
|
|
char *buf, path[PATH_MAX-1];
|
|
|
|
|
|
|
|
if (getcwd(path, sizeof(path)) == NULL)
|
|
|
|
return ENOTSUP;
|
|
|
|
|
2012-11-19 16:45:32 +05:30
|
|
|
buf = xbps_xasprintf("%s/%s", path, xhp->rootdir);
|
2012-11-19 16:42:38 +05:30
|
|
|
xhp->rootdir = buf;
|
|
|
|
}
|
2011-06-04 17:07:53 +05:30
|
|
|
}
|
2012-11-19 16:42:38 +05:30
|
|
|
|
2011-12-15 15:49:20 +05:30
|
|
|
if (xhp->cachedir == NULL) {
|
|
|
|
if (xhp->cfg == NULL)
|
|
|
|
xhp->cachedir = XBPS_CACHE_PATH;
|
|
|
|
else
|
|
|
|
xhp->cachedir = cfg_getstr(xhp->cfg, "cachedir");
|
2011-06-04 20:38:44 +05:30
|
|
|
}
|
2012-03-13 14:52:35 +05:30
|
|
|
if ((xhp->cachedir_priv = set_cachedir(xhp)) == NULL)
|
2011-12-22 12:53:11 +05:30
|
|
|
return ENOMEM;
|
2011-12-15 18:54:10 +05:30
|
|
|
xhp->cachedir = xhp->cachedir_priv;
|
|
|
|
|
2012-03-13 14:52:35 +05:30
|
|
|
if ((xhp->metadir_priv = set_metadir(xhp)) == NULL)
|
|
|
|
return ENOMEM;
|
|
|
|
xhp->metadir = xhp->metadir_priv;
|
|
|
|
|
2012-06-15 19:03:11 +05:30
|
|
|
uname(&un);
|
|
|
|
xhp->un_machine = strdup(un.machine);
|
|
|
|
assert(xhp->un_machine);
|
|
|
|
|
2011-12-15 15:49:20 +05:30
|
|
|
if (xhp->cfg == NULL) {
|
2012-01-22 15:22:35 +05:30
|
|
|
xhp->flags |= XBPS_FLAG_SYSLOG;
|
2011-12-15 15:49:20 +05:30
|
|
|
xhp->fetch_timeout = XBPS_FETCH_TIMEOUT;
|
|
|
|
cc = XBPS_FETCH_CACHECONN;
|
|
|
|
cch = XBPS_FETCH_CACHECONN_HOST;
|
|
|
|
} else {
|
2012-01-26 16:13:35 +05:30
|
|
|
if (cfg_getbool(xhp->cfg, "syslog"))
|
2012-01-22 15:22:35 +05:30
|
|
|
xhp->flags |= XBPS_FLAG_SYSLOG;
|
2011-12-24 05:35:26 +05:30
|
|
|
xhp->fetch_timeout = cfg_getint(xhp->cfg, "FetchTimeoutConnection");
|
|
|
|
cc = cfg_getint(xhp->cfg, "FetchCacheConnections");
|
|
|
|
cch = cfg_getint(xhp->cfg, "FetchCacheConnectionsPerHost");
|
2011-12-15 15:49:20 +05:30
|
|
|
}
|
2012-01-26 16:13:35 +05:30
|
|
|
if (xhp->flags & XBPS_FLAG_SYSLOG)
|
2012-01-22 15:22:35 +05:30
|
|
|
syslog_enabled = true;
|
|
|
|
|
2012-11-19 20:12:24 +05:30
|
|
|
/* Inject virtual packages from virtualpkg.d files */
|
|
|
|
config_inject_vpkgs(xhp);
|
|
|
|
|
2011-12-15 15:49:20 +05:30
|
|
|
xbps_fetch_set_cache_connection(cc, cch);
|
|
|
|
|
2012-06-14 11:52:11 +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);
|
|
|
|
xbps_dbg_printf(xhp, "FetchTimeout=%u\n", xhp->fetch_timeout);
|
|
|
|
xbps_dbg_printf(xhp, "FetchCacheconn=%u\n", cc);
|
|
|
|
xbps_dbg_printf(xhp, "FetchCacheconnHost=%u\n", cch);
|
|
|
|
xbps_dbg_printf(xhp, "Syslog=%u\n", syslog_enabled);
|
2012-06-15 19:03:11 +05:30
|
|
|
xbps_dbg_printf(xhp, "Architecture: %s\n", xhp->un_machine);
|
2011-10-17 17:54:30 +05:30
|
|
|
|
2012-09-24 14:20:23 +05:30
|
|
|
xhp->initialized = true;
|
2012-01-17 21:26:17 +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);
|
|
|
|
|
|
|
|
if (!xhp->initialized)
|
2011-06-04 17:07:53 +05:30
|
|
|
return;
|
2011-12-22 21:25:35 +05:30
|
|
|
|
2012-01-20 15:40:52 +05:30
|
|
|
xbps_pkgdb_release(xhp);
|
2012-05-30 14:26:32 +05:30
|
|
|
xbps_rpool_release(xhp);
|
2011-12-22 21:25:35 +05:30
|
|
|
xbps_fetch_unset_cache_connection();
|
|
|
|
|
2012-06-18 14:13:05 +05:30
|
|
|
cfg_free(xhp->cfg);
|
2012-06-15 19:03:11 +05:30
|
|
|
free(xhp->cachedir_priv);
|
|
|
|
free(xhp->metadir_priv);
|
|
|
|
free(xhp->un_machine);
|
2011-06-04 17:07:53 +05:30
|
|
|
|
2012-09-24 14:20:23 +05:30
|
|
|
xhp->initialized = false;
|
2011-02-21 18:08:44 +05:30
|
|
|
xhp = NULL;
|
|
|
|
}
|
|
|
|
|
2011-01-22 17:10:19 +05:30
|
|
|
static void
|
|
|
|
common_printf(FILE *f, const char *msg, const char *fmt, va_list ap)
|
|
|
|
{
|
|
|
|
if (msg != NULL)
|
|
|
|
fprintf(f, "%s", msg);
|
|
|
|
|
|
|
|
vfprintf(f, fmt, ap);
|
|
|
|
}
|
|
|
|
|
2010-11-19 18:10:13 +05:30
|
|
|
void
|
2012-06-14 11:52:11 +05:30
|
|
|
xbps_dbg_printf_append(struct xbps_handle *xhp, const char *fmt, ...)
|
2010-11-19 18:10:13 +05:30
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
2012-06-14 11:52:11 +05:30
|
|
|
if ((xhp->flags & XBPS_FLAG_DEBUG) == 0)
|
2010-11-19 18:10:13 +05:30
|
|
|
return;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2011-01-22 17:10:19 +05:30
|
|
|
common_printf(stderr, NULL, fmt, ap);
|
2010-11-19 18:10:13 +05:30
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-06-14 11:52:11 +05:30
|
|
|
xbps_dbg_printf(struct xbps_handle *xhp, const char *fmt, ...)
|
2010-11-19 18:10:13 +05:30
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
2012-06-14 11:52:11 +05:30
|
|
|
if ((xhp->flags & XBPS_FLAG_DEBUG) == 0)
|
2010-11-19 18:10:13 +05:30
|
|
|
return;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2011-01-22 17:10:19 +05:30
|
|
|
common_printf(stderr, "[DEBUG] ", fmt, ap);
|
2010-11-19 18:10:13 +05:30
|
|
|
va_end(ap);
|
|
|
|
}
|
2011-01-21 21:46:58 +05:30
|
|
|
|
|
|
|
void
|
|
|
|
xbps_error_printf(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2011-01-22 17:10:19 +05:30
|
|
|
common_printf(stderr, "ERROR: ", fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
xbps_warn_printf(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
common_printf(stderr, "WARNING: ", fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|