2011-02-05 16:36:03 +05:30
|
|
|
/*-
|
2013-03-05 08:38:42 +05:30
|
|
|
* Copyright (c) 2008-2013 Juan Romero Pardines.
|
2011-02-05 16:36:03 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2013-06-14 13:30:33 +05:30
|
|
|
#include <fcntl.h>
|
|
|
|
#include <dirent.h>
|
2013-06-14 16:01:43 +05:30
|
|
|
#include <pthread.h>
|
2011-02-05 16:36:03 +05:30
|
|
|
|
|
|
|
#include "xbps_api_impl.h"
|
|
|
|
|
2013-06-14 16:01:43 +05:30
|
|
|
static pthread_mutex_t cfg_mtx = PTHREAD_MUTEX_INITIALIZER;
|
2013-06-14 13:30:33 +05:30
|
|
|
static bool cfg_vpkgs_init;
|
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
static xbps_dictionary_t
|
|
|
|
get_pkg_in_array(xbps_array_t array, const char *str, bool virtual)
|
2011-02-05 16:36:03 +05:30
|
|
|
{
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_object_t obj = NULL;
|
|
|
|
xbps_object_iterator_t iter;
|
2013-03-05 08:38:42 +05:30
|
|
|
const char *pkgver;
|
|
|
|
char *dpkgn;
|
2012-11-30 11:41:51 +05:30
|
|
|
bool found = false;
|
2012-06-05 02:52:33 +05:30
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
iter = xbps_array_iterator(array);
|
2012-11-30 14:19:09 +05:30
|
|
|
assert(iter);
|
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
while ((obj = xbps_object_iterator_next(iter))) {
|
2012-04-12 15:27:21 +05:30
|
|
|
if (virtual) {
|
|
|
|
/*
|
|
|
|
* Check if package pattern matches
|
|
|
|
* any virtual package version in dictionary.
|
|
|
|
*/
|
2012-11-30 11:41:51 +05:30
|
|
|
if (xbps_pkgpattern_version(str))
|
|
|
|
found = xbps_match_virtual_pkg_in_dict(obj, str, true);
|
|
|
|
else
|
|
|
|
found = xbps_match_virtual_pkg_in_dict(obj, str, false);
|
|
|
|
|
|
|
|
if (found)
|
2012-04-12 15:27:21 +05:30
|
|
|
break;
|
2012-11-30 11:41:51 +05:30
|
|
|
} else if (xbps_pkgpattern_version(str)) {
|
2013-06-27 19:56:19 +05:30
|
|
|
/* match by pattern against pkgver */
|
2013-06-20 13:56:12 +05:30
|
|
|
if (!xbps_dictionary_get_cstring_nocopy(obj,
|
2011-02-05 16:36:03 +05:30
|
|
|
"pkgver", &pkgver))
|
|
|
|
continue;
|
2012-11-30 11:41:51 +05:30
|
|
|
if (xbps_pkgpattern_match(pkgver, str)) {
|
|
|
|
found = true;
|
2011-02-05 16:36:03 +05:30
|
|
|
break;
|
2012-11-30 11:41:51 +05:30
|
|
|
}
|
|
|
|
} else if (xbps_pkg_version(str)) {
|
|
|
|
/* match by exact pkgver */
|
2013-06-20 13:56:12 +05:30
|
|
|
if (!xbps_dictionary_get_cstring_nocopy(obj,
|
2012-11-30 11:41:51 +05:30
|
|
|
"pkgver", &pkgver))
|
2011-02-05 16:36:03 +05:30
|
|
|
continue;
|
2012-11-30 11:41:51 +05:30
|
|
|
if (strcmp(str, pkgver) == 0) {
|
|
|
|
found = true;
|
2011-02-05 16:36:03 +05:30
|
|
|
break;
|
2012-06-05 14:28:39 +05:30
|
|
|
}
|
2012-11-30 11:41:51 +05:30
|
|
|
} else {
|
|
|
|
/* match by pkgname */
|
2013-06-20 13:56:12 +05:30
|
|
|
if (!xbps_dictionary_get_cstring_nocopy(obj,
|
2013-03-05 08:38:42 +05:30
|
|
|
"pkgver", &pkgver))
|
2012-11-30 11:41:51 +05:30
|
|
|
continue;
|
2013-03-05 08:38:42 +05:30
|
|
|
dpkgn = xbps_pkg_name(pkgver);
|
|
|
|
assert(dpkgn);
|
2012-11-30 11:41:51 +05:30
|
|
|
if (strcmp(dpkgn, str) == 0) {
|
2013-03-05 08:38:42 +05:30
|
|
|
free(dpkgn);
|
2012-11-30 11:41:51 +05:30
|
|
|
found = true;
|
2011-06-04 17:07:53 +05:30
|
|
|
break;
|
|
|
|
}
|
2013-03-05 08:38:42 +05:30
|
|
|
free(dpkgn);
|
2011-06-04 17:07:53 +05:30
|
|
|
}
|
2011-02-05 16:36:03 +05:30
|
|
|
}
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_object_iterator_release(iter);
|
2012-11-30 14:19:09 +05:30
|
|
|
|
2012-11-30 11:41:51 +05:30
|
|
|
return found ? obj : NULL;
|
2012-01-19 16:56:40 +05:30
|
|
|
}
|
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_t HIDDEN
|
|
|
|
xbps_find_pkg_in_array(xbps_array_t a, const char *s)
|
2011-02-05 16:36:03 +05:30
|
|
|
{
|
2013-06-20 13:56:12 +05:30
|
|
|
assert(xbps_object_type(a) == XBPS_TYPE_ARRAY);
|
2012-11-30 11:41:51 +05:30
|
|
|
assert(s);
|
2011-07-15 21:52:58 +05:30
|
|
|
|
2012-11-30 11:41:51 +05:30
|
|
|
return get_pkg_in_array(a, s, false);
|
2011-02-05 16:36:03 +05:30
|
|
|
}
|
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_t HIDDEN
|
2012-11-30 11:41:51 +05:30
|
|
|
xbps_find_virtualpkg_in_array(struct xbps_handle *x,
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_array_t a,
|
2012-11-30 11:41:51 +05:30
|
|
|
const char *s)
|
2011-02-05 16:36:03 +05:30
|
|
|
{
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_t pkgd;
|
2012-11-30 11:41:51 +05:30
|
|
|
const char *vpkg;
|
|
|
|
bool bypattern = false;
|
2011-02-05 16:36:03 +05:30
|
|
|
|
2012-11-30 11:41:51 +05:30
|
|
|
assert(x);
|
2013-06-20 13:56:12 +05:30
|
|
|
assert(xbps_object_type(a) == XBPS_TYPE_ARRAY);
|
2012-11-30 11:41:51 +05:30
|
|
|
assert(s);
|
2011-02-05 16:36:03 +05:30
|
|
|
|
2012-11-30 11:41:51 +05:30
|
|
|
if (xbps_pkgpattern_version(s))
|
|
|
|
bypattern = true;
|
2011-02-05 16:36:03 +05:30
|
|
|
|
2012-11-30 11:41:51 +05:30
|
|
|
if ((vpkg = vpkg_user_conf(x, s, bypattern))) {
|
|
|
|
if ((pkgd = get_pkg_in_array(a, vpkg, true)))
|
|
|
|
return pkgd;
|
2011-02-05 16:36:03 +05:30
|
|
|
}
|
2011-06-04 17:07:53 +05:30
|
|
|
|
2012-11-30 11:41:51 +05:30
|
|
|
return get_pkg_in_array(a, s, true);
|
2011-07-15 21:52:58 +05:30
|
|
|
}
|
2013-03-05 08:38:42 +05:30
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
static xbps_dictionary_t
|
|
|
|
match_pkg_by_pkgver(xbps_dictionary_t repod, const char *p)
|
2013-03-05 08:38:42 +05:30
|
|
|
{
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_t d = NULL;
|
2013-03-05 08:38:42 +05:30
|
|
|
const char *pkgver;
|
|
|
|
char *pkgname;
|
|
|
|
|
|
|
|
/* exact match by pkgver */
|
|
|
|
if ((pkgname = xbps_pkg_name(p)) == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
d = xbps_dictionary_get(repod, pkgname);
|
2013-03-05 08:38:42 +05:30
|
|
|
if (d) {
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_get_cstring_nocopy(d, "pkgver", &pkgver);
|
2013-03-05 08:38:42 +05:30
|
|
|
if (strcmp(pkgver, p))
|
|
|
|
d = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(pkgname);
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
static xbps_dictionary_t
|
|
|
|
match_pkg_by_pattern(xbps_dictionary_t repod, const char *p)
|
2013-03-05 08:38:42 +05:30
|
|
|
{
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_t d = NULL;
|
2013-03-05 08:38:42 +05:30
|
|
|
const char *pkgver;
|
|
|
|
char *pkgname;
|
|
|
|
|
|
|
|
/* match by pkgpattern in pkgver */
|
|
|
|
if ((pkgname = xbps_pkgpattern_name(p)) == NULL) {
|
2013-08-29 15:21:42 +05:30
|
|
|
if ((pkgname = xbps_pkg_name(p))) {
|
|
|
|
free(pkgname);
|
2013-03-05 08:38:42 +05:30
|
|
|
return match_pkg_by_pkgver(repod, p);
|
2013-08-29 15:21:42 +05:30
|
|
|
}
|
2013-03-05 08:38:42 +05:30
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
d = xbps_dictionary_get(repod, pkgname);
|
2013-03-05 08:38:42 +05:30
|
|
|
if (d) {
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_get_cstring_nocopy(d, "pkgver", &pkgver);
|
2013-03-05 08:38:42 +05:30
|
|
|
assert(pkgver);
|
|
|
|
if (!xbps_pkgpattern_match(pkgver, p))
|
|
|
|
d = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(pkgname);
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
2013-06-14 13:30:33 +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, "cannot access to %s: %s\n",
|
|
|
|
vpkgdir, strerror(errno));
|
2013-08-29 15:21:42 +05:30
|
|
|
free(vpkgdir);
|
2013-06-14 13:30:33 +05:30
|
|
|
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);
|
|
|
|
if (cfg_parse_fp(xh->cfg, fp) != 0) {
|
|
|
|
xbps_error_printf("Failed to parse "
|
|
|
|
"vpkg conf file %s:\n", dp->d_name);
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
xbps_dbg_printf(xh, "Injected vpkgs from %s\n", path);
|
|
|
|
free(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(dirp);
|
|
|
|
free(vpkgdir);
|
|
|
|
cfg_vpkgs_init = true;
|
|
|
|
}
|
|
|
|
|
2013-03-05 08:38:42 +05:30
|
|
|
const char HIDDEN *
|
|
|
|
vpkg_user_conf(struct xbps_handle *xhp,
|
|
|
|
const char *vpkg,
|
|
|
|
bool bypattern)
|
|
|
|
{
|
|
|
|
const char *vpkgver, *pkg = NULL;
|
|
|
|
char *vpkgname = NULL, *tmp;
|
2013-09-15 13:36:49 +05:30
|
|
|
unsigned int cnt;
|
2013-03-05 08:38:42 +05:30
|
|
|
|
|
|
|
if (xhp->cfg == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2013-06-14 13:30:33 +05:30
|
|
|
/* inject virtual packages from sysconfdir */
|
2013-06-14 16:01:43 +05:30
|
|
|
pthread_mutex_lock(&cfg_mtx);
|
2013-06-14 13:30:33 +05:30
|
|
|
if (!cfg_vpkgs_init)
|
|
|
|
config_inject_vpkgs(xhp);
|
2013-06-14 16:01:43 +05:30
|
|
|
pthread_mutex_unlock(&cfg_mtx);
|
2013-06-14 13:30:33 +05:30
|
|
|
|
2013-03-05 08:38:42 +05:30
|
|
|
if ((cnt = cfg_size(xhp->cfg, "virtual-package")) == 0) {
|
|
|
|
/* no virtual packages configured */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-09-15 13:36:49 +05:30
|
|
|
for (unsigned int i = 0; i < cnt; i++) {
|
2013-03-05 08:38:42 +05:30
|
|
|
cfg_t *sec = cfg_getnsec(xhp->cfg, "virtual-package", i);
|
2013-09-15 13:36:49 +05:30
|
|
|
for (unsigned int j = 0; j < cfg_size(sec, "targets"); j++) {
|
2013-03-05 08:38:42 +05:30
|
|
|
tmp = NULL;
|
|
|
|
vpkgver = cfg_getnstr(sec, "targets", j);
|
|
|
|
if (strchr(vpkgver, '_') == NULL) {
|
|
|
|
tmp = xbps_xasprintf("%s_1", vpkgver);
|
|
|
|
vpkgname = xbps_pkg_name(tmp);
|
|
|
|
free(tmp);
|
|
|
|
} else {
|
|
|
|
vpkgname = xbps_pkg_name(vpkgver);
|
|
|
|
}
|
|
|
|
if (vpkgname == NULL)
|
|
|
|
break;
|
|
|
|
if (bypattern) {
|
|
|
|
if (!xbps_pkgpattern_match(vpkgver, vpkg)) {
|
|
|
|
free(vpkgname);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (strcmp(vpkg, vpkgname)) {
|
|
|
|
free(vpkgname);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* virtual package matched in conffile */
|
|
|
|
pkg = cfg_title(sec);
|
|
|
|
xbps_dbg_printf(xhp,
|
|
|
|
"matched vpkg in conf `%s' for %s\n",
|
|
|
|
pkg, vpkg);
|
|
|
|
free(vpkgname);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pkg;
|
|
|
|
}
|
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_t
|
2013-03-05 08:38:42 +05:30
|
|
|
xbps_find_virtualpkg_in_dict(struct xbps_handle *xhp,
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_t d,
|
2013-03-05 08:38:42 +05:30
|
|
|
const char *pkg)
|
|
|
|
{
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_object_t obj;
|
|
|
|
xbps_object_iterator_t iter;
|
|
|
|
xbps_dictionary_t pkgd = NULL;
|
2013-03-05 08:38:42 +05:30
|
|
|
const char *vpkg;
|
2013-08-29 15:21:42 +05:30
|
|
|
bool bypattern = false;
|
2013-03-05 08:38:42 +05:30
|
|
|
|
|
|
|
if (xbps_pkgpattern_version(pkg))
|
|
|
|
bypattern = true;
|
|
|
|
|
|
|
|
/* Try matching vpkg from configuration files */
|
|
|
|
vpkg = vpkg_user_conf(xhp, pkg, bypattern);
|
|
|
|
if (vpkg != NULL) {
|
|
|
|
if (xbps_pkgpattern_version(vpkg))
|
|
|
|
pkgd = match_pkg_by_pattern(d, vpkg);
|
|
|
|
else if (xbps_pkg_version(vpkg))
|
|
|
|
pkgd = match_pkg_by_pkgver(d, vpkg);
|
|
|
|
else
|
2013-06-20 13:56:12 +05:30
|
|
|
pkgd = xbps_dictionary_get(d, vpkg);
|
2013-03-05 08:38:42 +05:30
|
|
|
|
2013-08-29 15:21:42 +05:30
|
|
|
if (pkgd)
|
|
|
|
return pkgd;
|
2013-03-05 08:38:42 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* ... otherwise match the first one in dictionary */
|
2013-06-20 13:56:12 +05:30
|
|
|
iter = xbps_dictionary_iterator(d);
|
2013-03-05 08:38:42 +05:30
|
|
|
assert(iter);
|
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
while ((obj = xbps_object_iterator_next(iter))) {
|
|
|
|
pkgd = xbps_dictionary_get_keysym(d, obj);
|
2013-03-05 08:38:42 +05:30
|
|
|
if (xbps_match_virtual_pkg_in_dict(pkgd, pkg, bypattern)) {
|
2013-08-29 15:21:42 +05:30
|
|
|
xbps_object_iterator_release(iter);
|
|
|
|
return pkgd;
|
2013-03-05 08:38:42 +05:30
|
|
|
}
|
|
|
|
}
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_object_iterator_release(iter);
|
2013-03-05 08:38:42 +05:30
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_t
|
|
|
|
xbps_find_pkg_in_dict(xbps_dictionary_t d, const char *pkg)
|
2013-03-05 08:38:42 +05:30
|
|
|
{
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_t pkgd = NULL;
|
2013-03-05 08:38:42 +05:30
|
|
|
|
|
|
|
if (xbps_pkgpattern_version(pkg))
|
|
|
|
pkgd = match_pkg_by_pattern(d, pkg);
|
|
|
|
else if (xbps_pkg_version(pkg))
|
|
|
|
pkgd = match_pkg_by_pkgver(d, pkg);
|
|
|
|
else
|
2013-06-20 13:56:12 +05:30
|
|
|
pkgd = xbps_dictionary_get(d, pkg);
|
2013-03-05 08:38:42 +05:30
|
|
|
|
|
|
|
return pkgd;
|
|
|
|
}
|