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>
|
|
|
|
#include <sys/types.h>
|
2016-01-26 18:52:23 +05:30
|
|
|
#ifdef __FreeBSD__
|
|
|
|
#define _WITH_GETLINE /* getline() */
|
|
|
|
#endif
|
2010-11-19 18:10:13 +05:30
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-02-25 21:12:52 +05:30
|
|
|
#include <strings.h>
|
2010-11-19 18:10:13 +05:30
|
|
|
#include <errno.h>
|
|
|
|
#include <stdarg.h>
|
2012-11-19 20:12:24 +05:30
|
|
|
#include <dirent.h>
|
2014-02-25 21:12:52 +05:30
|
|
|
#include <ctype.h>
|
2014-05-06 02:40:18 +05:30
|
|
|
#include <glob.h>
|
2014-05-13 15:07:09 +05:30
|
|
|
#include <libgen.h>
|
2010-11-19 18:10:13 +05:30
|
|
|
|
|
|
|
#include "xbps_api_impl.h"
|
|
|
|
|
2014-03-11 13:58:50 +05:30
|
|
|
#ifdef __clang__
|
|
|
|
#pragma clang diagnostic ignored "-Wformat-nonliteral"
|
|
|
|
#endif
|
|
|
|
|
2014-11-06 14:28:04 +05:30
|
|
|
static int parse_file(struct xbps_handle *, const char *, const char *, bool);
|
2014-05-06 13:51:44 +05:30
|
|
|
|
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
|
|
|
|
2014-02-25 21:12:52 +05:30
|
|
|
static void
|
2015-09-15 12:56:07 +05:30
|
|
|
store_vars(struct xbps_handle *xhp, xbps_dictionary_t *d,
|
|
|
|
const char *key, const char *path, size_t line, char *buf)
|
2014-02-25 21:12:52 +05:30
|
|
|
{
|
2015-09-15 12:56:07 +05:30
|
|
|
char *lp, *rp, *tc;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
if (*d == NULL)
|
|
|
|
*d = xbps_dictionary_create();
|
2019-03-23 03:04:11 +05:30
|
|
|
if (xhp->vpkgd_conf)
|
2016-03-24 14:53:20 +05:30
|
|
|
xhp->vpkgd_conf = xbps_dictionary_create();
|
|
|
|
|
2014-02-25 21:12:52 +05:30
|
|
|
/*
|
2015-09-15 12:56:07 +05:30
|
|
|
* Parse strings delimited by ':' i.e
|
|
|
|
* <left>:<right>
|
2014-02-25 21:12:52 +05:30
|
|
|
*/
|
2015-09-15 12:56:07 +05:30
|
|
|
lp = buf;
|
|
|
|
rp = strchr(buf, ':');
|
|
|
|
if (rp == NULL || *rp == '\0') {
|
2014-02-25 21:12:52 +05:30
|
|
|
xbps_dbg_printf(xhp, "%s: ignoring invalid "
|
2015-09-15 12:56:07 +05:30
|
|
|
"%s option at line %zu\n", path, key, line);
|
2014-02-25 21:12:52 +05:30
|
|
|
return;
|
|
|
|
}
|
2015-09-15 12:56:07 +05:30
|
|
|
tc = strchr(buf, ':');
|
|
|
|
len = strlen(buf) - strlen(tc);
|
|
|
|
lp[len] = '\0';
|
|
|
|
|
|
|
|
rp++;
|
|
|
|
xbps_dictionary_set_cstring(*d, lp, rp);
|
2016-03-24 14:53:20 +05:30
|
|
|
xbps_dictionary_set_cstring(xhp->vpkgd_conf, lp, rp);
|
2015-09-15 12:56:07 +05:30
|
|
|
xbps_dbg_printf(xhp, "%s: added %s %s for %s\n", path, key, lp, rp);
|
2014-02-25 21:12:52 +05:30
|
|
|
}
|
|
|
|
|
2014-08-01 18:39:51 +05:30
|
|
|
static void
|
|
|
|
store_preserved_file(struct xbps_handle *xhp, const char *file)
|
|
|
|
{
|
|
|
|
glob_t globbuf;
|
|
|
|
char *p = NULL, *rfile = NULL;
|
|
|
|
size_t len;
|
|
|
|
int rv = 0;
|
|
|
|
|
|
|
|
if (xhp->preserved_files == NULL) {
|
|
|
|
xhp->preserved_files = xbps_array_create();
|
|
|
|
assert(xhp->preserved_files);
|
|
|
|
}
|
|
|
|
|
|
|
|
rfile = xbps_xasprintf("%s%s", xhp->rootdir, file);
|
|
|
|
|
|
|
|
rv = glob(rfile, 0, NULL, &globbuf);
|
|
|
|
if (rv == GLOB_NOMATCH) {
|
|
|
|
if (xbps_match_string_in_array(xhp->preserved_files, file))
|
|
|
|
goto out;
|
|
|
|
xbps_array_add_cstring(xhp->preserved_files, file);
|
|
|
|
xbps_dbg_printf(xhp, "Added preserved file: %s\n", file);
|
|
|
|
goto out;
|
|
|
|
} else if (rv != 0) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
for (size_t i = 0; i < globbuf.gl_pathc; i++) {
|
|
|
|
if (xbps_match_string_in_array(xhp->preserved_files, globbuf.gl_pathv[i]))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
len = strlen(globbuf.gl_pathv[i]) - strlen(xhp->rootdir) + 1;
|
|
|
|
p = malloc(len);
|
2014-10-07 11:29:07 +05:30
|
|
|
assert(p);
|
2016-04-17 23:47:37 +05:30
|
|
|
xbps_strlcpy(p, globbuf.gl_pathv[i] + strlen(xhp->rootdir), len);
|
2014-08-01 18:39:51 +05:30
|
|
|
xbps_array_add_cstring(xhp->preserved_files, p);
|
|
|
|
xbps_dbg_printf(xhp, "Added preserved file: %s (expanded from %s)\n", p, file);
|
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
globfree(&globbuf);
|
2014-10-05 16:25:07 +05:30
|
|
|
free(rfile);
|
2014-08-01 18:39:51 +05:30
|
|
|
}
|
|
|
|
|
2014-02-25 21:12:52 +05:30
|
|
|
static bool
|
|
|
|
store_repo(struct xbps_handle *xhp, const char *repo)
|
|
|
|
{
|
2014-11-21 15:26:41 +05:30
|
|
|
if (xhp->flags & XBPS_FLAG_IGNORE_CONF_REPOS)
|
|
|
|
return false;
|
|
|
|
|
2015-01-10 16:29:31 +05:30
|
|
|
return xbps_repo_store(xhp, repo);
|
2014-02-25 21:12:52 +05:30
|
|
|
}
|
|
|
|
|
2019-03-05 21:15:29 +05:30
|
|
|
static void
|
|
|
|
store_ignored_pkg(struct xbps_handle *xhp, const char *pkgname)
|
|
|
|
{
|
|
|
|
if (xhp->ignored_pkgs == NULL) {
|
|
|
|
xhp->ignored_pkgs = xbps_array_create();
|
|
|
|
assert(xhp->ignored_pkgs);
|
|
|
|
}
|
|
|
|
xbps_array_add_cstring(xhp->ignored_pkgs, pkgname);
|
|
|
|
xbps_dbg_printf(xhp, "Added ignored package: %s\n", pkgname);
|
|
|
|
}
|
|
|
|
|
2014-02-25 21:12:52 +05:30
|
|
|
static bool
|
|
|
|
parse_option(char *buf, char **k, char **v)
|
|
|
|
{
|
|
|
|
size_t klen;
|
|
|
|
char *key, *value;
|
|
|
|
const char *keys[] = {
|
|
|
|
"rootdir",
|
|
|
|
"cachedir",
|
|
|
|
"syslog",
|
|
|
|
"repository",
|
|
|
|
"virtualpkg",
|
2014-08-01 18:39:51 +05:30
|
|
|
"include",
|
2019-03-05 21:15:29 +05:30
|
|
|
"ignorepkg",
|
2014-11-07 14:37:51 +05:30
|
|
|
"preserve",
|
2014-11-19 16:06:09 +05:30
|
|
|
"bestmatching",
|
|
|
|
"architecture"
|
2014-02-25 21:12:52 +05:30
|
|
|
};
|
|
|
|
bool found = false;
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < __arraycount(keys); i++) {
|
|
|
|
key = __UNCONST(keys[i]);
|
|
|
|
klen = strlen(key);
|
|
|
|
if (strncmp(buf, key, klen) == 0) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* non matching option */
|
|
|
|
if (!found)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* check if next char is the equal sign */
|
|
|
|
if (buf[klen] != '=')
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* skip equal sign */
|
|
|
|
value = buf + klen + 1;
|
|
|
|
/* eat blanks */
|
|
|
|
while (isblank((unsigned char)*value))
|
|
|
|
value++;
|
|
|
|
/* eat final newline */
|
|
|
|
value[strlen(value)-1] = '\0';
|
|
|
|
/* option processed successfully */
|
|
|
|
*k = key;
|
|
|
|
*v = value;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-06 02:40:18 +05:30
|
|
|
static int
|
2014-11-06 14:28:04 +05:30
|
|
|
parse_files_glob(struct xbps_handle *xhp, const char *cwd, const char *path, bool nested)
|
2014-05-15 20:30:29 +05:30
|
|
|
{
|
|
|
|
glob_t globbuf;
|
2014-08-01 18:39:51 +05:30
|
|
|
int rv = 0;
|
2014-05-06 02:40:18 +05:30
|
|
|
|
|
|
|
glob(path, 0, NULL, &globbuf);
|
2014-08-01 18:39:51 +05:30
|
|
|
for (size_t i = 0; i < globbuf.gl_pathc; i++) {
|
2014-11-06 14:28:04 +05:30
|
|
|
if ((rv = parse_file(xhp, cwd, globbuf.gl_pathv[i], nested)) != 0)
|
2014-05-06 02:40:18 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
globfree(&globbuf);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-02-25 21:12:52 +05:30
|
|
|
static int
|
2014-11-06 14:28:04 +05:30
|
|
|
parse_file(struct xbps_handle *xhp, const char *cwd, const char *path, bool nested)
|
2014-02-25 21:12:52 +05:30
|
|
|
{
|
|
|
|
FILE *fp;
|
2014-08-01 18:39:51 +05:30
|
|
|
char tmppath[XBPS_MAXPATH] = {0};
|
2014-02-25 21:12:52 +05:30
|
|
|
size_t len, nlines = 0;
|
2014-10-05 01:32:56 +05:30
|
|
|
ssize_t nread;
|
2014-08-01 18:39:51 +05:30
|
|
|
char *cfcwd, *line = NULL;
|
2014-02-25 21:12:52 +05:30
|
|
|
int rv = 0;
|
|
|
|
|
|
|
|
if ((fp = fopen(path, "r")) == NULL) {
|
|
|
|
rv = errno;
|
|
|
|
xbps_dbg_printf(xhp, "cannot read configuration file %s: %s\n", path, strerror(rv));
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-11-06 14:28:04 +05:30
|
|
|
xbps_dbg_printf(xhp, "Parsing configuration file: %s\n", path);
|
2014-02-25 21:12:52 +05:30
|
|
|
|
2014-10-05 01:32:56 +05:30
|
|
|
while ((nread = getline(&line, &len, fp)) != -1) {
|
2014-02-25 21:12:52 +05:30
|
|
|
char *p, *k, *v;
|
|
|
|
|
|
|
|
nlines++;
|
|
|
|
p = line;
|
|
|
|
/* eat blanks */
|
|
|
|
while (isblank((unsigned char)*p))
|
|
|
|
p++;
|
|
|
|
/* ignore comments or empty lines */
|
|
|
|
if (*p == '#' || *p == '\n')
|
|
|
|
continue;
|
|
|
|
if (!parse_option(p, &k, &v)) {
|
|
|
|
xbps_dbg_printf(xhp, "%s: ignoring invalid option at "
|
|
|
|
"line %zu\n", path, nlines);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (strcmp(k, "rootdir") == 0) {
|
|
|
|
xbps_dbg_printf(xhp, "%s: rootdir set to %s\n",
|
|
|
|
path, v);
|
|
|
|
snprintf(xhp->rootdir, sizeof(xhp->rootdir), "%s", v);
|
|
|
|
} else if (strcmp(k, "cachedir") == 0) {
|
|
|
|
xbps_dbg_printf(xhp, "%s: cachedir set to %s\n",
|
|
|
|
path, v);
|
|
|
|
snprintf(xhp->cachedir, sizeof(xhp->cachedir), "%s", v);
|
2014-11-19 16:06:09 +05:30
|
|
|
} else if (strcmp(k, "architecture") == 0) {
|
|
|
|
xbps_dbg_printf(xhp, "%s: native architecture set to %s\n",
|
|
|
|
path, v);
|
|
|
|
snprintf(xhp->native_arch, sizeof(xhp->native_arch), "%s", v);
|
2014-02-25 21:12:52 +05:30
|
|
|
} else if (strcmp(k, "syslog") == 0) {
|
|
|
|
if (strcasecmp(v, "true") == 0) {
|
2014-06-04 13:09:02 +05:30
|
|
|
xhp->flags &= ~XBPS_FLAG_DISABLE_SYSLOG;
|
2014-02-25 21:12:52 +05:30
|
|
|
xbps_dbg_printf(xhp, "%s: syslog enabled\n", path);
|
2014-06-04 13:09:02 +05:30
|
|
|
} else {
|
|
|
|
xhp->flags |= XBPS_FLAG_DISABLE_SYSLOG;
|
|
|
|
xbps_dbg_printf(xhp, "%s: syslog disabled\n", path);
|
2014-02-25 21:12:52 +05:30
|
|
|
}
|
|
|
|
} else if (strcmp(k, "repository") == 0) {
|
|
|
|
if (store_repo(xhp, v))
|
|
|
|
xbps_dbg_printf(xhp, "%s: added repository %s\n", path, v);
|
|
|
|
} else if (strcmp(k, "virtualpkg") == 0) {
|
2015-09-15 12:56:07 +05:30
|
|
|
store_vars(xhp, &xhp->vpkgd, k, path, nlines, v);
|
2019-03-05 21:15:29 +05:30
|
|
|
} else if (strcmp(k, "ignorepkg") == 0) {
|
|
|
|
store_ignored_pkg(xhp, v);
|
2014-08-01 18:39:51 +05:30
|
|
|
} else if (strcmp(k, "preserve") == 0) {
|
|
|
|
store_preserved_file(xhp, v);
|
2014-11-07 14:37:51 +05:30
|
|
|
} else if (strcmp(k, "bestmatching") == 0) {
|
|
|
|
if (strcasecmp(v, "true") == 0) {
|
|
|
|
xhp->flags |= XBPS_FLAG_BESTMATCH;
|
|
|
|
xbps_dbg_printf(xhp, "%s: pkg best matching enabled\n", path);
|
|
|
|
} else {
|
|
|
|
xhp->flags &= ~XBPS_FLAG_BESTMATCH;
|
|
|
|
xbps_dbg_printf(xhp, "%s: pkg best matching disabled\n", path);
|
|
|
|
}
|
|
|
|
xbps_dbg_printf(xhp, "%s: enabling pkg best matching\n", path);
|
2014-02-25 21:12:52 +05:30
|
|
|
}
|
|
|
|
/* Avoid double-nested parsing, only allow it once */
|
|
|
|
if (nested)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (strcmp(k, "include"))
|
|
|
|
continue;
|
|
|
|
|
2014-08-01 19:00:43 +05:30
|
|
|
/* cwd to the dir containing the config file */
|
2016-04-17 23:47:37 +05:30
|
|
|
xbps_strlcpy(tmppath, path, sizeof(tmppath));
|
2014-08-01 19:00:43 +05:30
|
|
|
cfcwd = dirname(tmppath);
|
|
|
|
if (chdir(cfcwd) == -1) {
|
|
|
|
rv = errno;
|
|
|
|
xbps_dbg_printf(xhp, "cannot chdir to %s: %s\n", cfcwd, strerror(rv));
|
|
|
|
return rv;
|
|
|
|
}
|
2014-11-06 14:28:04 +05:30
|
|
|
if ((rv = parse_files_glob(xhp, cwd, v, true)) != 0)
|
2014-02-25 21:12:52 +05:30
|
|
|
break;
|
2014-05-06 02:40:18 +05:30
|
|
|
|
2014-02-25 21:12:52 +05:30
|
|
|
}
|
|
|
|
free(line);
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2011-12-15 15:49:20 +05:30
|
|
|
static int
|
2014-12-09 16:35:18 +05:30
|
|
|
parse_dir(struct xbps_handle *xhp, const char *cwd, const char *confdir, const char *sysconfdir)
|
2011-12-15 15:49:20 +05:30
|
|
|
{
|
2014-06-04 11:35:18 +05:30
|
|
|
struct dirent **namelist;
|
2014-11-06 14:28:04 +05:30
|
|
|
char *ext, conf[PATH_MAX];
|
2014-06-04 11:35:18 +05:30
|
|
|
int i, n, rv = 0;
|
2014-02-25 21:12:52 +05:30
|
|
|
|
2014-12-09 16:35:18 +05:30
|
|
|
if (confdir == NULL)
|
2014-11-04 15:47:27 +05:30
|
|
|
goto stage2;
|
2014-05-31 10:35:57 +05:30
|
|
|
/*
|
2014-06-04 11:35:18 +05:30
|
|
|
* Read all configuration files stored in the system
|
|
|
|
* foo.d directory.
|
2014-05-31 10:35:57 +05:30
|
|
|
*/
|
2014-12-09 16:35:18 +05:30
|
|
|
xbps_dbg_printf(xhp, "Processing configuration directory: %s\n", confdir);
|
2014-06-04 11:35:18 +05:30
|
|
|
|
2014-12-09 16:35:18 +05:30
|
|
|
if ((n = scandir(confdir, &namelist, 0, alphasort)) < 0)
|
2014-05-31 10:35:57 +05:30
|
|
|
goto stage2;
|
2014-02-25 21:12:52 +05:30
|
|
|
|
2014-06-04 11:35:18 +05:30
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
if ((strcmp(namelist[i]->d_name, "..") == 0) ||
|
|
|
|
(strcmp(namelist[i]->d_name, ".") == 0)) {
|
|
|
|
free(namelist[i]);
|
2014-02-25 21:12:52 +05:30
|
|
|
continue;
|
2014-06-04 11:35:18 +05:30
|
|
|
}
|
2014-12-09 16:35:18 +05:30
|
|
|
/* only process .conf files, ignore something else */
|
2014-06-04 11:35:18 +05:30
|
|
|
if ((ext = strrchr(namelist[i]->d_name, '.')) == NULL) {
|
|
|
|
free(namelist[i]);
|
2014-02-25 21:12:52 +05:30
|
|
|
continue;
|
2014-06-04 11:35:18 +05:30
|
|
|
}
|
2014-12-09 16:35:18 +05:30
|
|
|
if (strcmp(ext, ".conf")) {
|
|
|
|
xbps_dbg_printf(xhp, "%s: ignoring %s\n", confdir, namelist[i]->d_name);
|
2014-06-04 11:35:18 +05:30
|
|
|
free(namelist[i]);
|
2014-05-31 10:35:57 +05:30
|
|
|
continue;
|
|
|
|
}
|
2014-06-04 11:35:18 +05:30
|
|
|
/* parse conf file */
|
2014-12-09 16:35:18 +05:30
|
|
|
snprintf(conf, sizeof(conf), "%s/%s", confdir, namelist[i]->d_name);
|
2014-11-06 14:28:04 +05:30
|
|
|
if ((rv = parse_file(xhp, cwd, conf, false)) != 0) {
|
2014-06-04 11:35:18 +05:30
|
|
|
free(namelist[i]);
|
2014-05-31 10:35:57 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-06-04 11:35:18 +05:30
|
|
|
free(namelist);
|
2014-05-31 10:35:57 +05:30
|
|
|
if (rv != 0)
|
|
|
|
return rv;
|
2014-02-25 21:12:52 +05:30
|
|
|
|
2014-05-31 10:35:57 +05:30
|
|
|
stage2:
|
2014-12-09 16:35:18 +05:30
|
|
|
if (sysconfdir == NULL)
|
2014-11-04 15:47:27 +05:30
|
|
|
return rv;
|
|
|
|
|
2014-05-31 10:35:57 +05:30
|
|
|
/*
|
2014-06-04 11:35:18 +05:30
|
|
|
* Read all configuration files stored in the configuration foo.d directory.
|
2014-05-31 10:35:57 +05:30
|
|
|
*/
|
2014-12-09 16:35:18 +05:30
|
|
|
xbps_dbg_printf(xhp, "Processing system configuration directory: %s\n", sysconfdir);
|
2011-10-17 16:07:15 +05:30
|
|
|
|
2014-12-09 16:35:18 +05:30
|
|
|
if ((n = scandir(sysconfdir, &namelist, 0, alphasort)) < 0)
|
2014-06-04 11:35:18 +05:30
|
|
|
return 0;
|
2014-05-30 15:18:10 +05:30
|
|
|
|
2014-06-04 11:35:18 +05:30
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
if ((strcmp(namelist[i]->d_name, "..") == 0) ||
|
|
|
|
(strcmp(namelist[i]->d_name, ".") == 0)) {
|
|
|
|
free(namelist[i]);
|
2014-05-30 15:18:10 +05:30
|
|
|
continue;
|
|
|
|
}
|
2014-12-09 16:35:18 +05:30
|
|
|
/* only process .conf files, ignore something else */
|
2014-06-04 11:35:18 +05:30
|
|
|
if ((ext = strrchr(namelist[i]->d_name, '.')) == NULL) {
|
|
|
|
free(namelist[i]);
|
2014-05-30 15:18:10 +05:30
|
|
|
continue;
|
|
|
|
}
|
2014-12-09 16:35:18 +05:30
|
|
|
if (strcmp(ext, ".conf")) {
|
|
|
|
xbps_dbg_printf(xhp, "%s: ignoring %s\n", sysconfdir, namelist[i]->d_name);
|
2014-06-04 11:35:18 +05:30
|
|
|
free(namelist[i]);
|
2014-05-30 15:18:10 +05:30
|
|
|
continue;
|
|
|
|
}
|
2014-12-09 16:35:18 +05:30
|
|
|
/* if the same file exists in configuration directory, ignore it */
|
2014-11-06 14:28:04 +05:30
|
|
|
snprintf(conf, sizeof(conf), "%s/%s", confdir, namelist[i]->d_name);
|
2014-12-09 16:35:18 +05:30
|
|
|
if (access(conf, R_OK) == 0) {
|
|
|
|
xbps_dbg_printf(xhp, "%s: ignoring %s (exists in confdir)\n", confdir, namelist[i]->d_name);
|
|
|
|
free(namelist[i]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* parse conf file */
|
|
|
|
snprintf(conf, sizeof(conf), "%s/%s", sysconfdir, namelist[i]->d_name);
|
2014-11-06 14:28:04 +05:30
|
|
|
if ((rv = parse_file(xhp, cwd, conf, false)) != 0) {
|
2014-06-04 11:35:18 +05:30
|
|
|
free(namelist[i]);
|
2014-05-30 15:18:10 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-06-04 11:35:18 +05:30
|
|
|
free(namelist);
|
2014-05-30 15:18:10 +05:30
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2012-06-15 19:03:11 +05:30
|
|
|
struct utsname un;
|
Fix compilation of initend for gcc 7
This is a trickier situation.
The original message:
```
initend.c:423:10: error: ‘%s’ directive output may be truncated writing 15 bytes into a region of size between 1 and 512 [-Werror=format-truncation=]
"%s%s", strcmp(xhp->rootdir, "/") ? xhp->rootdir : "",
^~
initend.c:422:3: note: ‘snprintf’ output between 16 and 527 bytes into a destination of size 512
snprintf(xhp->confdir, sizeof(xhp->confdir),
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"%s%s", strcmp(xhp->rootdir, "/") ? xhp->rootdir : "",
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
XBPS_SYSCONF_PATH);
~~~~~~~~~~~~~~~~~~
initend.c:429:7: error: ‘snprintf’ output may be truncated before the last format character [-Werror=format-truncation ]
"%s/%s", strcmp(xhp->rootdir, "/") ? xhp->rootdir : "", buf);
^~~~~~~
initend.c:428:3: note: ‘snprintf’ output 2 or more bytes (assuming 513) into a destination of size 512
snprintf(xhp->confdir, sizeof(xhp->confdir),
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"%s/%s", strcmp(xhp->rootdir, "/") ? xhp->rootdir : "", buf);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
initend.c:434:9: error: ‘%s’ directive output may be truncated writing 17 bytes into a region of size between 1 and 512 [-Werror=format-truncation=]
"%s%s", strcmp(xhp->rootdir, "/") ? xhp->rootdir : "",
^~
initend.c:433:2: note: ‘snprintf’ output between 18 and 529 bytes into a destination of size 512
snprintf(sysconfdir, sizeof(sysconfdir),
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"%s%s", strcmp(xhp->rootdir, "/") ? xhp->rootdir : "",
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
XBPS_SYSDEFCONF_PATH);
~~~~~~~~~~~~~~~~~~~~~
initend.c:455:11: error: ‘%s’ directive output may be truncated writing 14 bytes into a region of size between 0 and 511 [-Werror=format-truncation=]
"%s/%s", strcmp(xhp->rootdir, "/") ? xhp->rootdir : "",
^~
initend.c:454:3: note: ‘snprintf’ output between 16 and 527 bytes into a destination of size 512
snprintf(xhp->cachedir, sizeof(xhp->cachedir),
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"%s/%s", strcmp(xhp->rootdir, "/") ? xhp->rootdir : "",
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
XBPS_CACHE_PATH);
~~~~~~~~~~~~~~~~
initend.c:461:7: error: ‘snprintf’ output may be truncated before the last format character [-Werror=format-truncation ]
"%s/%s", strcmp(xhp->rootdir, "/") ? xhp->rootdir : "", buf);
^~~~~~~
initend.c:460:3: note: ‘snprintf’ output 2 or more bytes (assuming 513) into a destination of size 512
snprintf(xhp->cachedir, sizeof(xhp->cachedir),
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"%s/%s", strcmp(xhp->rootdir, "/") ? xhp->rootdir : "", buf);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
initend.c:467:11: error: ‘%s’ directive output may be truncated writing 12 bytes into a region of size between 0 and 511 [-Werror=format-truncation=]
"%s/%s", strcmp(xhp->rootdir, "/") ? xhp->rootdir : "",
^~
initend.c:466:3: note: ‘snprintf’ output between 14 and 525 bytes into a destination of size 512
snprintf(xhp->metadir, sizeof(xhp->metadir),
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"%s/%s", strcmp(xhp->rootdir, "/") ? xhp->rootdir : "",
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
XBPS_META_PATH);
~~~~~~~~~~~~~~~
initend.c:473:7: error: ‘snprintf’ output may be truncated before the last format character [-Werror=format-truncation ]
"%s/%s", strcmp(xhp->rootdir, "/") ? xhp->rootdir : "", buf);
^~~~~~~
initend.c:472:3: note: ‘snprintf’ output 2 or more bytes (assuming 513) into a destination of size 512
snprintf(xhp->metadir, sizeof(xhp->metadir),
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"%s/%s", strcmp(xhp->rootdir, "/") ? xhp->rootdir : "", buf);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cc1: all warnings being treated as errors
```
It's basically warning about dangerous operations on strings. And as
far as I could tell, is a valid warning and not a false alarm!
This fix makes the concept of `XBPS_MAXPATH` lose a little bit of sense
as now it doesn't necessarily represent the max size of the paths used
by xbps, but instead the max allowed size of the path configured.
I think this change is ok, but I wasn't able to find any reference to
why it was chosen to be 512. POSIX mandates at least 256, so I'm not
breaking anything that wasn't broken already, and Linux seems to have
a maximum size of 4096, which is pretty safe.
Therefore, this changes should be harmless. I think.
2017-08-23 04:05:45 +05:30
|
|
|
char cwd[PATH_MAX-1], sysconfdir[XBPS_MAXPATH+sizeof(XBPS_SYSDEFCONF_PATH)], *buf;
|
2014-01-09 16:11:25 +05:30
|
|
|
const char *repodir, *native_arch;
|
2014-02-25 21:12:52 +05:30
|
|
|
int rv;
|
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
|
|
|
|
2014-08-01 18:39:51 +05:30
|
|
|
/* get cwd */
|
|
|
|
if (getcwd(cwd, sizeof(cwd)) == NULL)
|
|
|
|
return ENOTSUP;
|
2011-12-15 15:49:20 +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] != '/') {
|
|
|
|
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
|
|
|
}
|
2014-08-01 18:39:51 +05:30
|
|
|
xbps_dbg_printf(xhp, "%s\n", XBPS_RELVER);
|
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
|
|
|
}
|
|
|
|
/* set sysconfdir */
|
2019-02-23 11:45:56 +05:30
|
|
|
size = sizeof(sysconfdir);
|
|
|
|
rv = snprintf(sysconfdir, size,
|
2014-11-06 14:28:04 +05:30
|
|
|
"%s%s", strcmp(xhp->rootdir, "/") ? xhp->rootdir : "",
|
|
|
|
XBPS_SYSDEFCONF_PATH);
|
2019-02-23 11:45:56 +05:30
|
|
|
if (rv < 0 || (size_t)rv >= size)
|
|
|
|
return 1;
|
2014-05-31 10:14:07 +05:30
|
|
|
|
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) {
|
2016-04-17 23:47:37 +05:30
|
|
|
xbps_strlcpy(xhp->native_arch, native_arch, sizeof(xhp->native_arch));
|
2014-01-09 16:11:25 +05:30
|
|
|
} else {
|
|
|
|
uname(&un);
|
2016-04-17 23:47:37 +05:30
|
|
|
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);
|
|
|
|
|
2014-11-06 14:28:04 +05:30
|
|
|
/* process xbps.d */
|
2014-12-09 16:35:18 +05:30
|
|
|
if ((rv = parse_dir(xhp, cwd, xhp->confdir, sysconfdir)) != 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);
|
|
|
|
xbps_dbg_printf(xhp, "sysconfdir=%s\n", 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);
|
|
|
|
}
|
|
|
|
}
|
2014-08-01 18:39:51 +05:30
|
|
|
/* Going back to old working directory */
|
2015-01-10 16:29:31 +05:30
|
|
|
if (chdir(cwd) == -1) {
|
|
|
|
xbps_dbg_printf(xhp, "%s: cannot chdir to %s: %s\n",
|
|
|
|
__func__, cwd, strerror(errno));
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|