2011-10-16 18:01:53 +05:30
|
|
|
/*-
|
2012-01-17 20:47:03 +05:30
|
|
|
* Copyright (c) 2009-2012 Juan Romero Pardines.
|
2011-10-16 18:01:53 +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>
|
|
|
|
|
|
|
|
#include "xbps_api_impl.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file lib/repository_pool_find.c
|
|
|
|
* @brief Repository pool routines
|
|
|
|
* @defgroup repopool Repository pool functions
|
|
|
|
*/
|
|
|
|
struct repo_pool_fpkg {
|
|
|
|
prop_dictionary_t pkgd;
|
|
|
|
const char *pattern;
|
2012-01-17 20:47:03 +05:30
|
|
|
const char *bestpkgver;
|
2011-10-16 18:01:53 +05:30
|
|
|
bool bypattern;
|
2012-01-17 20:47:03 +05:30
|
|
|
bool exact;
|
2011-10-16 18:01:53 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
2012-06-14 11:52:11 +05:30
|
|
|
repo_find_virtualpkg_cb(struct xbps_handle *xhp,
|
|
|
|
struct xbps_rpool_index *rpi,
|
|
|
|
void *arg,
|
|
|
|
bool *done)
|
2011-10-16 18:01:53 +05:30
|
|
|
{
|
|
|
|
struct repo_pool_fpkg *rpf = arg;
|
|
|
|
|
2012-06-14 11:52:11 +05:30
|
|
|
(void)xhp;
|
|
|
|
|
2012-05-30 13:52:53 +05:30
|
|
|
if (rpf->bypattern) {
|
|
|
|
rpf->pkgd =
|
2012-06-15 19:03:11 +05:30
|
|
|
xbps_find_virtualpkg_in_array_by_pattern(xhp, rpi->repo,
|
2012-05-30 13:52:53 +05:30
|
|
|
rpf->pattern);
|
|
|
|
} else {
|
|
|
|
rpf->pkgd =
|
2012-06-15 19:03:11 +05:30
|
|
|
xbps_find_virtualpkg_in_array_by_name(xhp, rpi->repo,
|
2012-05-30 13:52:53 +05:30
|
|
|
rpf->pattern);
|
|
|
|
}
|
|
|
|
if (rpf->pkgd) {
|
2012-06-18 14:13:05 +05:30
|
|
|
prop_dictionary_set_cstring_nocopy(rpf->pkgd,
|
|
|
|
"repository", rpi->uri);
|
2012-05-30 13:52:53 +05:30
|
|
|
*done = true;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* not found */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2012-06-14 11:52:11 +05:30
|
|
|
repo_find_virtualpkg_conf_cb(struct xbps_handle *xhp,
|
|
|
|
struct xbps_rpool_index *rpi,
|
|
|
|
void *arg,
|
|
|
|
bool *done)
|
2012-05-30 13:52:53 +05:30
|
|
|
{
|
|
|
|
struct repo_pool_fpkg *rpf = arg;
|
|
|
|
|
2011-10-16 18:01:53 +05:30
|
|
|
if (rpf->bypattern) {
|
|
|
|
rpf->pkgd =
|
2012-06-14 11:52:11 +05:30
|
|
|
xbps_find_virtualpkg_conf_in_array_by_pattern(xhp,
|
|
|
|
rpi->repo, rpf->pattern);
|
2011-10-16 18:01:53 +05:30
|
|
|
} else {
|
|
|
|
rpf->pkgd =
|
2012-06-14 11:52:11 +05:30
|
|
|
xbps_find_virtualpkg_conf_in_array_by_name(xhp,
|
|
|
|
rpi->repo, rpf->pattern);
|
2011-10-16 18:01:53 +05:30
|
|
|
}
|
|
|
|
if (rpf->pkgd) {
|
2012-06-18 14:13:05 +05:30
|
|
|
prop_dictionary_set_cstring_nocopy(rpf->pkgd,
|
|
|
|
"repository", rpi->uri);
|
2011-10-16 18:01:53 +05:30
|
|
|
*done = true;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* not found */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2012-06-14 11:52:11 +05:30
|
|
|
repo_find_pkg_cb(struct xbps_handle *xhp,
|
|
|
|
struct xbps_rpool_index *rpi,
|
|
|
|
void *arg,
|
|
|
|
bool *done)
|
2011-10-16 18:01:53 +05:30
|
|
|
{
|
|
|
|
struct repo_pool_fpkg *rpf = arg;
|
|
|
|
|
2012-06-14 11:52:11 +05:30
|
|
|
(void)xhp;
|
|
|
|
|
2012-01-17 20:47:03 +05:30
|
|
|
if (rpf->exact) {
|
|
|
|
/* exact match by pkgver */
|
2012-06-15 19:03:11 +05:30
|
|
|
rpf->pkgd = xbps_find_pkg_in_array_by_pkgver(xhp, rpi->repo,
|
2012-05-31 19:59:56 +05:30
|
|
|
rpf->pattern, NULL);
|
2012-01-17 20:47:03 +05:30
|
|
|
} else if (rpf->bypattern) {
|
|
|
|
/* match by pkgpattern in pkgver*/
|
2012-06-15 19:03:11 +05:30
|
|
|
rpf->pkgd = xbps_find_pkg_in_array_by_pattern(xhp, rpi->repo,
|
2012-05-31 19:59:56 +05:30
|
|
|
rpf->pattern, NULL);
|
2011-10-16 18:01:53 +05:30
|
|
|
} else {
|
2012-01-17 20:47:03 +05:30
|
|
|
/* match by pkgname */
|
2012-06-15 19:03:11 +05:30
|
|
|
rpf->pkgd = xbps_find_pkg_in_array_by_name(xhp, rpi->repo,
|
2012-05-31 19:59:56 +05:30
|
|
|
rpf->pattern, NULL);
|
2011-10-16 18:01:53 +05:30
|
|
|
}
|
|
|
|
if (rpf->pkgd) {
|
|
|
|
/*
|
|
|
|
* Package dictionary found, add the "repository"
|
|
|
|
* object with the URI.
|
|
|
|
*/
|
2012-06-18 14:13:05 +05:30
|
|
|
prop_dictionary_set_cstring_nocopy(rpf->pkgd,
|
|
|
|
"repository", rpi->uri);
|
2011-10-16 18:01:53 +05:30
|
|
|
*done = true;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* Not found */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2012-06-14 11:52:11 +05:30
|
|
|
repo_find_best_pkg_cb(struct xbps_handle *xhp,
|
|
|
|
struct xbps_rpool_index *rpi,
|
|
|
|
void *arg,
|
|
|
|
bool *done)
|
2011-10-16 18:01:53 +05:30
|
|
|
{
|
|
|
|
struct repo_pool_fpkg *rpf = arg;
|
2012-01-17 20:47:03 +05:30
|
|
|
const char *repopkgver;
|
2012-02-22 08:28:41 +05:30
|
|
|
prop_dictionary_t pkgd;
|
2011-10-16 18:01:53 +05:30
|
|
|
|
2012-01-17 20:47:03 +05:30
|
|
|
(void)done;
|
2012-06-14 11:52:11 +05:30
|
|
|
(void)xhp;
|
2012-01-17 20:47:03 +05:30
|
|
|
|
|
|
|
if (rpf->bypattern) {
|
2012-06-15 19:03:11 +05:30
|
|
|
pkgd = xbps_find_pkg_in_array_by_pattern(xhp, rpi->repo,
|
2012-05-31 19:59:56 +05:30
|
|
|
rpf->pattern, NULL);
|
2012-01-17 20:47:03 +05:30
|
|
|
} else {
|
2012-06-15 19:03:11 +05:30
|
|
|
pkgd = xbps_find_pkg_in_array_by_name(xhp, rpi->repo,
|
2012-05-31 19:59:56 +05:30
|
|
|
rpf->pattern, NULL);
|
2012-01-17 20:47:03 +05:30
|
|
|
}
|
2012-02-22 08:28:41 +05:30
|
|
|
if (pkgd == NULL) {
|
2011-10-16 18:01:53 +05:30
|
|
|
if (errno && errno != ENOENT)
|
|
|
|
return errno;
|
|
|
|
|
2012-06-14 11:52:11 +05:30
|
|
|
xbps_dbg_printf(xhp,
|
|
|
|
"[rpool] Package '%s' not found in repository "
|
2012-05-31 19:59:56 +05:30
|
|
|
"'%s'.\n", rpf->pattern, rpi->uri);
|
2012-01-17 20:47:03 +05:30
|
|
|
return 0;
|
|
|
|
}
|
2012-02-22 08:28:41 +05:30
|
|
|
prop_dictionary_get_cstring_nocopy(pkgd,
|
2012-01-17 20:47:03 +05:30
|
|
|
"pkgver", &repopkgver);
|
|
|
|
if (rpf->bestpkgver == NULL) {
|
2012-06-14 11:52:11 +05:30
|
|
|
xbps_dbg_printf(xhp,
|
|
|
|
"[rpool] Found best match '%s' (%s).\n",
|
2012-05-31 19:59:56 +05:30
|
|
|
repopkgver, rpi->uri);
|
2012-02-22 08:28:41 +05:30
|
|
|
rpf->pkgd = pkgd;
|
2012-06-18 14:13:05 +05:30
|
|
|
prop_dictionary_set_cstring_nocopy(rpf->pkgd,
|
|
|
|
"repository", rpi->uri);
|
2012-01-17 20:47:03 +05:30
|
|
|
rpf->bestpkgver = repopkgver;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Compare current stored version against new
|
|
|
|
* version from current package in repository.
|
|
|
|
*/
|
2012-01-29 16:42:00 +05:30
|
|
|
if (xbps_cmpver(repopkgver, rpf->bestpkgver) == 1) {
|
2012-06-14 11:52:11 +05:30
|
|
|
xbps_dbg_printf(xhp,
|
|
|
|
"[rpool] Found best match '%s' (%s).\n",
|
2012-05-31 19:59:56 +05:30
|
|
|
repopkgver, rpi->uri);
|
2012-02-22 08:28:41 +05:30
|
|
|
rpf->pkgd = pkgd;
|
2012-06-18 14:13:05 +05:30
|
|
|
prop_dictionary_set_cstring_nocopy(rpf->pkgd,
|
|
|
|
"repository", rpi->uri);
|
2012-01-17 20:47:03 +05:30
|
|
|
rpf->bestpkgver = repopkgver;
|
2011-10-16 18:01:53 +05:30
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-05-30 13:52:53 +05:30
|
|
|
typedef enum {
|
|
|
|
BEST_PKG = 1,
|
|
|
|
EXACT_PKG,
|
|
|
|
VIRTUAL_PKG,
|
|
|
|
VIRTUAL_CONF_PKG,
|
|
|
|
REAL_PKG
|
|
|
|
} pkg_repo_type_t;
|
|
|
|
|
2012-06-05 02:52:33 +05:30
|
|
|
static prop_dictionary_t
|
2012-06-14 11:52:11 +05:30
|
|
|
repo_find_pkg(struct xbps_handle *xhp,
|
|
|
|
const char *pkg,
|
|
|
|
bool bypattern,
|
|
|
|
pkg_repo_type_t type)
|
2011-10-16 18:01:53 +05:30
|
|
|
{
|
2012-06-05 02:52:33 +05:30
|
|
|
struct repo_pool_fpkg rpf;
|
2011-10-16 18:01:53 +05:30
|
|
|
int rv = 0;
|
|
|
|
|
2012-06-05 02:52:33 +05:30
|
|
|
rpf.pattern = pkg;
|
|
|
|
rpf.bypattern = bypattern;
|
|
|
|
rpf.exact = false;
|
|
|
|
rpf.pkgd = NULL;
|
|
|
|
rpf.bestpkgver = NULL;
|
2011-10-16 18:01:53 +05:30
|
|
|
|
2012-05-30 13:52:53 +05:30
|
|
|
switch (type) {
|
|
|
|
case EXACT_PKG:
|
2012-01-17 20:47:03 +05:30
|
|
|
/*
|
2012-05-30 13:52:53 +05:30
|
|
|
* Find exact pkg version.
|
2012-01-17 20:47:03 +05:30
|
|
|
*/
|
2012-06-05 02:52:33 +05:30
|
|
|
rpf.exact = true;
|
2012-06-14 11:52:11 +05:30
|
|
|
rv = xbps_rpool_foreach(xhp, repo_find_pkg_cb, &rpf);
|
2012-05-30 13:52:53 +05:30
|
|
|
break;
|
|
|
|
case BEST_PKG:
|
2011-10-16 18:01:53 +05:30
|
|
|
/*
|
2012-05-30 13:52:53 +05:30
|
|
|
* Find best pkg version.
|
2011-10-16 18:01:53 +05:30
|
|
|
*/
|
2012-06-14 11:52:11 +05:30
|
|
|
rv = xbps_rpool_foreach(xhp, repo_find_best_pkg_cb, &rpf);
|
2012-05-30 13:52:53 +05:30
|
|
|
break;
|
|
|
|
case VIRTUAL_PKG:
|
|
|
|
/*
|
|
|
|
* Find virtual pkg.
|
|
|
|
*/
|
2012-06-14 11:52:11 +05:30
|
|
|
rv = xbps_rpool_foreach(xhp, repo_find_virtualpkg_cb, &rpf);
|
2012-05-30 13:52:53 +05:30
|
|
|
break;
|
|
|
|
case VIRTUAL_CONF_PKG:
|
|
|
|
/*
|
|
|
|
* Find virtual pkg as specified in configuration file.
|
|
|
|
*/
|
2012-06-14 11:52:11 +05:30
|
|
|
rv = xbps_rpool_foreach(xhp, repo_find_virtualpkg_conf_cb, &rpf);
|
2012-05-30 13:52:53 +05:30
|
|
|
break;
|
|
|
|
case REAL_PKG:
|
|
|
|
/*
|
|
|
|
* Find real pkg.
|
|
|
|
*/
|
2012-06-14 11:52:11 +05:30
|
|
|
rv = xbps_rpool_foreach(xhp, repo_find_pkg_cb, &rpf);
|
2012-05-30 13:52:53 +05:30
|
|
|
break;
|
2011-10-16 18:01:53 +05:30
|
|
|
}
|
2012-06-05 02:52:33 +05:30
|
|
|
if (rv != 0) {
|
2012-05-30 13:52:53 +05:30
|
|
|
errno = rv;
|
2012-06-05 02:52:33 +05:30
|
|
|
return NULL;
|
|
|
|
}
|
2011-10-16 18:01:53 +05:30
|
|
|
|
2012-06-18 14:13:05 +05:30
|
|
|
return rpf.pkgd;
|
2011-10-16 18:01:53 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
prop_dictionary_t
|
2012-06-14 11:52:11 +05:30
|
|
|
xbps_rpool_find_virtualpkg(struct xbps_handle *xhp,
|
|
|
|
const char *pkg,
|
|
|
|
bool bypattern)
|
2011-10-16 18:01:53 +05:30
|
|
|
{
|
|
|
|
assert(pkg != NULL);
|
|
|
|
|
2012-06-14 11:52:11 +05:30
|
|
|
return repo_find_pkg(xhp, pkg, bypattern, VIRTUAL_PKG);
|
2012-05-30 13:52:53 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
prop_dictionary_t
|
2012-06-14 11:52:11 +05:30
|
|
|
xbps_rpool_find_virtualpkg_conf(struct xbps_handle *xhp,
|
|
|
|
const char *pkg,
|
|
|
|
bool bypattern)
|
2012-05-30 13:52:53 +05:30
|
|
|
{
|
|
|
|
assert(pkg != NULL);
|
|
|
|
|
2012-06-14 11:52:11 +05:30
|
|
|
return repo_find_pkg(xhp, pkg, bypattern, VIRTUAL_CONF_PKG);
|
2011-10-16 18:01:53 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
prop_dictionary_t
|
2012-06-14 11:52:11 +05:30
|
|
|
xbps_rpool_find_pkg(struct xbps_handle *xhp,
|
|
|
|
const char *pkg,
|
|
|
|
bool bypattern,
|
|
|
|
bool best)
|
2011-10-16 18:01:53 +05:30
|
|
|
{
|
|
|
|
assert(pkg != NULL);
|
|
|
|
|
2012-05-30 13:52:53 +05:30
|
|
|
if (best)
|
2012-06-14 11:52:11 +05:30
|
|
|
return repo_find_pkg(xhp, pkg, bypattern, BEST_PKG);
|
2012-01-17 20:47:03 +05:30
|
|
|
|
2012-06-14 11:52:11 +05:30
|
|
|
return repo_find_pkg(xhp, pkg, bypattern, REAL_PKG);
|
2012-01-17 20:47:03 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
prop_dictionary_t
|
2012-06-14 11:52:11 +05:30
|
|
|
xbps_rpool_find_pkg_exact(struct xbps_handle *xhp, const char *pkgver)
|
2012-01-17 20:47:03 +05:30
|
|
|
{
|
|
|
|
assert(pkgver != NULL);
|
|
|
|
|
2012-06-14 11:52:11 +05:30
|
|
|
return repo_find_pkg(xhp, pkgver, false, EXACT_PKG);
|
2011-10-16 18:01:53 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
prop_dictionary_t
|
2012-06-14 11:52:11 +05:30
|
|
|
xbps_rpool_dictionary_metadata_plist(struct xbps_handle *xhp,
|
|
|
|
const char *pattern,
|
|
|
|
const char *plistf)
|
2011-10-16 18:01:53 +05:30
|
|
|
{
|
|
|
|
prop_dictionary_t pkgd = NULL, plistd = NULL;
|
|
|
|
const char *repoloc;
|
|
|
|
char *url;
|
|
|
|
|
2012-02-16 13:25:07 +05:30
|
|
|
assert(pattern != NULL);
|
2011-10-16 18:01:53 +05:30
|
|
|
assert(plistf != NULL);
|
|
|
|
/*
|
|
|
|
* Iterate over the the repository pool and search for a plist file
|
2012-02-16 13:25:07 +05:30
|
|
|
* in the binary package matching `pattern'. The plist file will be
|
2011-10-16 18:01:53 +05:30
|
|
|
* internalized to a proplib dictionary.
|
|
|
|
*
|
|
|
|
* The first repository that has it wins and the loop is stopped.
|
|
|
|
* This will work locally and remotely, thanks to libarchive and
|
|
|
|
* libfetch!
|
|
|
|
*/
|
2012-02-16 13:25:07 +05:30
|
|
|
if (xbps_pkgpattern_version(pattern))
|
2012-06-14 11:52:11 +05:30
|
|
|
pkgd = xbps_rpool_find_pkg(xhp, pattern, true, false);
|
2012-02-16 13:25:07 +05:30
|
|
|
else
|
2012-06-14 11:52:11 +05:30
|
|
|
pkgd = xbps_rpool_find_pkg(xhp, pattern, false, true);
|
2012-02-16 13:25:07 +05:30
|
|
|
|
2011-10-16 18:01:53 +05:30
|
|
|
if (pkgd == NULL)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
prop_dictionary_get_cstring_nocopy(pkgd, "repository", &repoloc);
|
2012-06-14 11:52:11 +05:30
|
|
|
url = xbps_path_from_repository_uri(xhp, pkgd, repoloc);
|
2011-10-16 18:01:53 +05:30
|
|
|
if (url == NULL) {
|
|
|
|
errno = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
plistd = xbps_dictionary_metadata_plist_by_url(url, plistf);
|
|
|
|
free(url);
|
|
|
|
|
|
|
|
out:
|
|
|
|
if (plistd == NULL)
|
|
|
|
errno = ENOENT;
|
2011-12-22 12:53:11 +05:30
|
|
|
if (prop_object_type(pkgd) == PROP_TYPE_DICTIONARY)
|
2011-10-16 18:01:53 +05:30
|
|
|
prop_object_release(pkgd);
|
|
|
|
|
|
|
|
return plistd;
|
|
|
|
}
|