2009-11-26 07:52:50 +05:30
|
|
|
/*-
|
2015-01-10 23:41:31 +05:30
|
|
|
* Copyright (c) 2009-2015 Juan Romero Pardines.
|
2009-11-26 07:52:50 +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.
|
|
|
|
*/
|
|
|
|
|
2011-11-08 00:58:35 +05:30
|
|
|
#include <sys/utsname.h>
|
2009-11-26 07:52:50 +05:30
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
2012-01-15 21:53:50 +05:30
|
|
|
#include <libgen.h>
|
2009-11-26 07:52:50 +05:30
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2010-11-13 07:48:58 +05:30
|
|
|
#include "xbps_api_impl.h"
|
2009-11-26 07:52:50 +05:30
|
|
|
|
2013-12-25 13:18:24 +05:30
|
|
|
struct rpool_fpkg {
|
|
|
|
xbps_array_t revdeps;
|
|
|
|
xbps_dictionary_t pkgd;
|
|
|
|
const char *pattern;
|
|
|
|
const char *bestpkgver;
|
|
|
|
bool best;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
BEST_PKG = 1,
|
|
|
|
VIRTUAL_PKG,
|
|
|
|
REAL_PKG,
|
|
|
|
REVDEPS_PKG
|
|
|
|
} pkg_repo_type_t;
|
|
|
|
|
2013-12-16 13:19:33 +05:30
|
|
|
static SIMPLEQ_HEAD(rpool_head, xbps_repo) rpool_queue =
|
2013-06-10 13:58:39 +05:30
|
|
|
SIMPLEQ_HEAD_INITIALIZER(rpool_queue);
|
|
|
|
|
2010-01-21 07:40:19 +05:30
|
|
|
/**
|
2012-12-19 05:31:27 +05:30
|
|
|
* @file lib/rpool.c
|
2011-01-18 19:14:39 +05:30
|
|
|
* @brief Repository pool routines
|
|
|
|
* @defgroup repopool Repository pool functions
|
2010-01-21 07:40:19 +05:30
|
|
|
*/
|
|
|
|
|
2011-11-11 03:44:50 +05:30
|
|
|
int
|
2013-06-10 13:58:39 +05:30
|
|
|
xbps_rpool_sync(struct xbps_handle *xhp, const char *uri)
|
2011-11-11 03:44:50 +05:30
|
|
|
{
|
|
|
|
const char *repouri;
|
|
|
|
|
2013-09-18 20:15:05 +05:30
|
|
|
for (unsigned int i = 0; i < xbps_array_count(xhp->repositories); i++) {
|
|
|
|
xbps_array_get_cstring_nocopy(xhp->repositories, i, &repouri);
|
2012-06-01 18:32:06 +05:30
|
|
|
/* If argument was set just process that repository */
|
|
|
|
if (uri && strcmp(repouri, uri))
|
2012-01-15 18:54:44 +05:30
|
|
|
continue;
|
2012-11-07 15:07:58 +05:30
|
|
|
|
2013-06-10 13:58:39 +05:30
|
|
|
if (xbps_repo_sync(xhp, repouri) == -1) {
|
2012-06-14 11:52:11 +05:30
|
|
|
xbps_dbg_printf(xhp,
|
2013-06-10 13:58:39 +05:30
|
|
|
"[rpool] `%s' failed to fetch repository data: %s\n",
|
|
|
|
repouri, fetchLastErrCode == 0 ? strerror(errno) :
|
2012-06-01 18:32:06 +05:30
|
|
|
xbps_fetch_error_string());
|
2011-11-11 03:44:50 +05:30
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2012-06-01 19:34:47 +05:30
|
|
|
return 0;
|
2011-11-11 03:44:50 +05:30
|
|
|
}
|
|
|
|
|
2015-03-06 01:53:58 +05:30
|
|
|
struct xbps_repo HIDDEN *
|
|
|
|
xbps_regget_repo(struct xbps_handle *xhp, const char *url)
|
|
|
|
{
|
|
|
|
struct xbps_repo *repo;
|
|
|
|
const char *repouri;
|
|
|
|
|
|
|
|
if (SIMPLEQ_EMPTY(&rpool_queue)) {
|
|
|
|
/* iterate until we have a match */
|
|
|
|
for (unsigned int i = 0; i < xbps_array_count(xhp->repositories); i++) {
|
|
|
|
xbps_array_get_cstring_nocopy(xhp->repositories, i, &repouri);
|
|
|
|
if (strcmp(repouri, url))
|
|
|
|
continue;
|
|
|
|
|
2015-03-25 16:30:59 +05:30
|
|
|
repo = xbps_repo_open(xhp, repouri);
|
2015-03-06 01:53:58 +05:30
|
|
|
if (!repo)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
SIMPLEQ_INSERT_TAIL(&rpool_queue, repo, entries);
|
|
|
|
xbps_dbg_printf(xhp, "[rpool] `%s' registered.\n", repouri);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SIMPLEQ_FOREACH(repo, &rpool_queue, entries)
|
|
|
|
if (strcmp(url, repo->uri) == 0)
|
|
|
|
return repo;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-12-24 15:13:55 +05:30
|
|
|
struct xbps_repo *
|
|
|
|
xbps_rpool_get_repo(const char *url)
|
|
|
|
{
|
|
|
|
struct xbps_repo *repo;
|
|
|
|
|
|
|
|
SIMPLEQ_FOREACH(repo, &rpool_queue, entries)
|
|
|
|
if (strcmp(url, repo->uri) == 0)
|
|
|
|
return repo;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-01-10 23:41:31 +05:30
|
|
|
void
|
2018-07-18 07:54:26 +05:30
|
|
|
xbps_rpool_release(struct xbps_handle *xhp UNUSED)
|
2015-01-10 23:41:31 +05:30
|
|
|
{
|
|
|
|
struct xbps_repo *repo;
|
|
|
|
|
|
|
|
while ((repo = SIMPLEQ_FIRST(&rpool_queue))) {
|
|
|
|
SIMPLEQ_REMOVE(&rpool_queue, repo, xbps_repo, entries);
|
2015-01-11 13:41:38 +05:30
|
|
|
xbps_repo_close(repo);
|
2015-01-10 23:41:31 +05:30
|
|
|
}
|
|
|
|
if (xhp->repositories)
|
|
|
|
xbps_object_release(xhp->repositories);
|
|
|
|
}
|
|
|
|
|
2010-11-19 18:10:13 +05:30
|
|
|
int
|
2012-06-14 11:52:11 +05:30
|
|
|
xbps_rpool_foreach(struct xbps_handle *xhp,
|
2013-10-05 15:08:04 +05:30
|
|
|
int (*fn)(struct xbps_repo *, void *, bool *),
|
|
|
|
void *arg)
|
2010-11-19 18:10:13 +05:30
|
|
|
{
|
2013-12-16 13:19:33 +05:30
|
|
|
struct xbps_repo *repo;
|
2013-12-24 15:13:55 +05:30
|
|
|
const char *repouri;
|
2010-11-19 18:10:13 +05:30
|
|
|
int rv = 0;
|
2013-12-24 15:13:55 +05:30
|
|
|
bool foundrepo = false, done = false;
|
2019-06-19 03:14:36 +05:30
|
|
|
unsigned int n = 0;
|
2010-11-19 18:10:13 +05:30
|
|
|
|
2011-01-25 08:44:33 +05:30
|
|
|
assert(fn != NULL);
|
2013-12-24 15:13:55 +05:30
|
|
|
|
2019-06-19 03:14:36 +05:30
|
|
|
again:
|
|
|
|
for (unsigned int i = n; i < xbps_array_count(xhp->repositories); i++, n++) {
|
2013-12-24 15:13:55 +05:30
|
|
|
xbps_array_get_cstring_nocopy(xhp->repositories, i, &repouri);
|
2019-06-19 03:14:36 +05:30
|
|
|
xbps_dbg_printf(xhp, "[rpool] checking `%s' at index %u\n", repouri, n);
|
2014-09-07 13:05:38 +05:30
|
|
|
if ((repo = xbps_rpool_get_repo(repouri)) == NULL) {
|
2015-03-25 16:30:59 +05:30
|
|
|
repo = xbps_repo_open(xhp, repouri);
|
2019-06-15 22:45:22 +05:30
|
|
|
if (!repo) {
|
|
|
|
xbps_repo_remove(xhp, repouri);
|
2019-06-19 03:14:36 +05:30
|
|
|
goto again;
|
2019-06-15 22:45:22 +05:30
|
|
|
}
|
2013-12-24 15:13:55 +05:30
|
|
|
SIMPLEQ_INSERT_TAIL(&rpool_queue, repo, entries);
|
|
|
|
xbps_dbg_printf(xhp, "[rpool] `%s' registered.\n", repouri);
|
2011-06-22 15:25:02 +05:30
|
|
|
}
|
2013-12-24 15:13:55 +05:30
|
|
|
foundrepo = true;
|
2013-12-16 13:19:33 +05:30
|
|
|
rv = (*fn)(repo, arg, &done);
|
2012-02-01 07:00:23 +05:30
|
|
|
if (rv != 0 || done)
|
2010-11-19 18:10:13 +05:30
|
|
|
break;
|
|
|
|
}
|
2013-12-24 15:13:55 +05:30
|
|
|
if (!foundrepo)
|
|
|
|
rv = ENOTSUP;
|
2010-11-19 18:10:13 +05:30
|
|
|
|
|
|
|
return rv;
|
2009-11-26 07:52:50 +05:30
|
|
|
}
|
2013-12-25 13:18:24 +05:30
|
|
|
|
|
|
|
static int
|
|
|
|
find_virtualpkg_cb(struct xbps_repo *repo, void *arg, bool *done)
|
|
|
|
{
|
|
|
|
struct rpool_fpkg *rpf = arg;
|
|
|
|
|
|
|
|
rpf->pkgd = xbps_repo_get_virtualpkg(repo, rpf->pattern);
|
|
|
|
if (rpf->pkgd) {
|
|
|
|
/* found */
|
|
|
|
*done = true;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* not found */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
find_pkg_cb(struct xbps_repo *repo, void *arg, bool *done)
|
|
|
|
{
|
|
|
|
struct rpool_fpkg *rpf = arg;
|
|
|
|
|
|
|
|
rpf->pkgd = xbps_repo_get_pkg(repo, rpf->pattern);
|
|
|
|
if (rpf->pkgd) {
|
|
|
|
/* found */
|
|
|
|
*done = true;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* Not found */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2018-07-18 07:54:26 +05:30
|
|
|
find_pkg_revdeps_cb(struct xbps_repo *repo, void *arg, bool *done UNUSED)
|
2013-12-25 13:18:24 +05:30
|
|
|
{
|
|
|
|
struct rpool_fpkg *rpf = arg;
|
|
|
|
xbps_array_t revdeps = NULL;
|
|
|
|
const char *pkgver;
|
|
|
|
|
|
|
|
revdeps = xbps_repo_get_pkg_revdeps(repo, rpf->pattern);
|
|
|
|
if (xbps_array_count(revdeps)) {
|
|
|
|
/* found */
|
|
|
|
if (rpf->revdeps == NULL)
|
|
|
|
rpf->revdeps = xbps_array_create();
|
|
|
|
for (unsigned int i = 0; i < xbps_array_count(revdeps); i++) {
|
|
|
|
xbps_array_get_cstring_nocopy(revdeps, i, &pkgver);
|
|
|
|
xbps_array_add_cstring_nocopy(rpf->revdeps, pkgver);
|
|
|
|
}
|
|
|
|
xbps_object_release(revdeps);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2018-07-18 07:54:26 +05:30
|
|
|
find_best_pkg_cb(struct xbps_repo *repo, void *arg, bool *done UNUSED)
|
2013-12-25 13:18:24 +05:30
|
|
|
{
|
|
|
|
struct rpool_fpkg *rpf = arg;
|
|
|
|
xbps_dictionary_t pkgd;
|
|
|
|
const char *repopkgver;
|
|
|
|
|
|
|
|
pkgd = xbps_repo_get_pkg(repo, rpf->pattern);
|
|
|
|
if (pkgd == NULL) {
|
|
|
|
if (errno && errno != ENOENT)
|
|
|
|
return errno;
|
|
|
|
|
|
|
|
xbps_dbg_printf(repo->xhp,
|
|
|
|
"[rpool] Package '%s' not found in repository "
|
|
|
|
"'%s'.\n", rpf->pattern, repo->uri);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
xbps_dictionary_get_cstring_nocopy(pkgd,
|
|
|
|
"pkgver", &repopkgver);
|
|
|
|
if (rpf->bestpkgver == NULL) {
|
|
|
|
xbps_dbg_printf(repo->xhp,
|
|
|
|
"[rpool] Found match '%s' (%s).\n",
|
|
|
|
repopkgver, repo->uri);
|
|
|
|
rpf->pkgd = pkgd;
|
|
|
|
rpf->bestpkgver = repopkgver;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Compare current stored version against new
|
|
|
|
* version from current package in repository.
|
|
|
|
*/
|
|
|
|
if (xbps_cmpver(repopkgver, rpf->bestpkgver) == 1) {
|
|
|
|
xbps_dbg_printf(repo->xhp,
|
|
|
|
"[rpool] Found best match '%s' (%s).\n",
|
|
|
|
repopkgver, repo->uri);
|
|
|
|
rpf->pkgd = pkgd;
|
|
|
|
rpf->bestpkgver = repopkgver;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static xbps_object_t
|
|
|
|
repo_find_pkg(struct xbps_handle *xhp,
|
|
|
|
const char *pkg,
|
|
|
|
pkg_repo_type_t type)
|
|
|
|
{
|
|
|
|
struct rpool_fpkg rpf;
|
|
|
|
int rv = 0;
|
|
|
|
|
|
|
|
assert(xhp);
|
|
|
|
assert(pkg);
|
|
|
|
|
|
|
|
rpf.pattern = pkg;
|
|
|
|
rpf.pkgd = NULL;
|
|
|
|
rpf.revdeps = NULL;
|
|
|
|
rpf.bestpkgver = NULL;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case BEST_PKG:
|
|
|
|
/*
|
|
|
|
* Find best pkg version.
|
|
|
|
*/
|
|
|
|
rv = xbps_rpool_foreach(xhp, find_best_pkg_cb, &rpf);
|
|
|
|
break;
|
|
|
|
case VIRTUAL_PKG:
|
|
|
|
/*
|
|
|
|
* Find virtual pkg.
|
|
|
|
*/
|
|
|
|
rv = xbps_rpool_foreach(xhp, find_virtualpkg_cb, &rpf);
|
|
|
|
break;
|
|
|
|
case REAL_PKG:
|
|
|
|
/*
|
|
|
|
* Find real pkg.
|
|
|
|
*/
|
|
|
|
rv = xbps_rpool_foreach(xhp, find_pkg_cb, &rpf);
|
|
|
|
break;
|
|
|
|
case REVDEPS_PKG:
|
|
|
|
/*
|
|
|
|
* Find revdeps for pkg.
|
|
|
|
*/
|
|
|
|
rv = xbps_rpool_foreach(xhp, find_pkg_revdeps_cb, &rpf);
|
2015-10-19 20:35:55 +05:30
|
|
|
break;
|
2013-12-25 13:18:24 +05:30
|
|
|
}
|
|
|
|
if (rv != 0) {
|
|
|
|
errno = rv;
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-10-19 20:35:55 +05:30
|
|
|
if (type == REVDEPS_PKG) {
|
|
|
|
if (rpf.revdeps == NULL)
|
|
|
|
errno = ENOENT;
|
2013-12-25 13:18:24 +05:30
|
|
|
|
2015-10-19 20:35:55 +05:30
|
|
|
return rpf.revdeps;
|
|
|
|
} else {
|
|
|
|
if (rpf.pkgd == NULL)
|
|
|
|
errno = ENOENT;
|
|
|
|
}
|
2013-12-25 13:18:24 +05:30
|
|
|
return rpf.pkgd;
|
|
|
|
}
|
|
|
|
|
|
|
|
xbps_dictionary_t
|
|
|
|
xbps_rpool_get_virtualpkg(struct xbps_handle *xhp, const char *pkg)
|
|
|
|
{
|
|
|
|
return repo_find_pkg(xhp, pkg, VIRTUAL_PKG);
|
|
|
|
}
|
|
|
|
|
|
|
|
xbps_dictionary_t
|
|
|
|
xbps_rpool_get_pkg(struct xbps_handle *xhp, const char *pkg)
|
|
|
|
{
|
2014-11-07 14:37:51 +05:30
|
|
|
if (xhp->flags & XBPS_FLAG_BESTMATCH)
|
|
|
|
return repo_find_pkg(xhp, pkg, BEST_PKG);
|
|
|
|
|
2013-12-25 13:18:24 +05:30
|
|
|
return repo_find_pkg(xhp, pkg, REAL_PKG);
|
|
|
|
}
|
|
|
|
|
|
|
|
xbps_array_t
|
|
|
|
xbps_rpool_get_pkg_revdeps(struct xbps_handle *xhp, const char *pkg)
|
|
|
|
{
|
|
|
|
return repo_find_pkg(xhp, pkg, REVDEPS_PKG);
|
|
|
|
}
|
|
|
|
|
2014-11-20 13:53:51 +05:30
|
|
|
xbps_array_t
|
|
|
|
xbps_rpool_get_pkg_fulldeptree(struct xbps_handle *xhp, const char *pkg)
|
|
|
|
{
|
|
|
|
return xbps_get_pkg_fulldeptree(xhp, pkg, true);
|
|
|
|
}
|
|
|
|
|
2013-12-25 13:18:24 +05:30
|
|
|
xbps_dictionary_t
|
|
|
|
xbps_rpool_get_pkg_plist(struct xbps_handle *xhp,
|
|
|
|
const char *pkg,
|
|
|
|
const char *plistf)
|
|
|
|
{
|
|
|
|
xbps_dictionary_t pkgd = NULL, plistd = NULL;
|
|
|
|
char *url;
|
|
|
|
|
|
|
|
assert(pkg != NULL);
|
|
|
|
assert(plistf != NULL);
|
|
|
|
/*
|
|
|
|
* Iterate over the the repository pool and search for a plist file
|
|
|
|
* in the binary package matching `pattern'. The plist file will be
|
|
|
|
* 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!
|
|
|
|
*/
|
|
|
|
if (((pkgd = xbps_rpool_get_pkg(xhp, pkg)) == NULL) &&
|
|
|
|
((pkgd = xbps_rpool_get_virtualpkg(xhp, pkg)) == NULL))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
url = xbps_repository_pkg_path(xhp, pkgd);
|
|
|
|
if (url == NULL) {
|
|
|
|
errno = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
2014-11-21 14:21:57 +05:30
|
|
|
plistd = xbps_archive_fetch_plist(url, plistf);
|
2013-12-25 13:18:24 +05:30
|
|
|
free(url);
|
|
|
|
|
|
|
|
out:
|
|
|
|
if (plistd == NULL)
|
|
|
|
errno = ENOENT;
|
|
|
|
|
|
|
|
return plistd;
|
|
|
|
}
|