2014-11-20 13:53:51 +05:30
|
|
|
/*-
|
2020-04-29 17:42:10 +05:30
|
|
|
* Copyright (c) 2014-2020 Juan Romero Pardines.
|
|
|
|
* 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.
|
2014-11-20 13:53:51 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "xbps_api_impl.h"
|
2020-02-12 13:44:25 +05:30
|
|
|
#include "uthash.h"
|
2014-11-20 13:53:51 +05:30
|
|
|
|
2019-04-19 18:20:23 +05:30
|
|
|
struct item;
|
|
|
|
|
|
|
|
struct depn {
|
|
|
|
struct depn *dnext;
|
|
|
|
struct item *item;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct item {
|
2020-02-12 13:44:25 +05:30
|
|
|
char *pkgn; /* hash key */
|
2019-04-19 18:20:23 +05:30
|
|
|
const char *pkgver;
|
2014-11-20 13:53:51 +05:30
|
|
|
xbps_array_t rdeps;
|
2020-02-12 13:44:25 +05:30
|
|
|
struct depn *dbase;
|
|
|
|
UT_hash_handle hh;
|
2014-11-20 13:53:51 +05:30
|
|
|
};
|
|
|
|
|
2020-02-12 13:44:25 +05:30
|
|
|
static struct item *items = NULL;
|
2019-04-19 18:20:23 +05:30
|
|
|
static xbps_array_t result;
|
2014-11-20 13:53:51 +05:30
|
|
|
|
2019-04-19 18:20:23 +05:30
|
|
|
static struct item *
|
|
|
|
lookupItem(const char *pkgn)
|
|
|
|
{
|
2020-02-12 13:44:25 +05:30
|
|
|
struct item *item = NULL;
|
2019-04-19 18:20:23 +05:30
|
|
|
|
|
|
|
assert(pkgn);
|
|
|
|
|
2020-02-12 13:44:25 +05:30
|
|
|
HASH_FIND_STR(items, pkgn, item);
|
|
|
|
return item;
|
2019-04-19 18:20:23 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static struct item *
|
2020-02-12 13:44:25 +05:30
|
|
|
addItem(xbps_array_t rdeps, const char *pkgn, const char *pkgver)
|
2019-04-19 18:20:23 +05:30
|
|
|
{
|
2020-02-12 13:44:25 +05:30
|
|
|
struct item *item = NULL;
|
2019-04-19 18:20:23 +05:30
|
|
|
|
|
|
|
assert(pkgn);
|
2020-02-12 13:44:25 +05:30
|
|
|
assert(pkgver);
|
|
|
|
|
|
|
|
HASH_FIND_STR(items, pkgn, item);
|
|
|
|
if (item)
|
|
|
|
return item;
|
2019-04-19 18:20:23 +05:30
|
|
|
|
2020-02-12 13:44:25 +05:30
|
|
|
item = malloc(sizeof(*item));
|
|
|
|
assert(item);
|
2019-04-19 18:20:23 +05:30
|
|
|
item->pkgn = strdup(pkgn);
|
2020-02-12 13:44:25 +05:30
|
|
|
item->pkgver = pkgver;
|
|
|
|
item->rdeps = rdeps;
|
|
|
|
item->dbase = NULL;
|
|
|
|
HASH_ADD_KEYPTR(hh, items, item->pkgn, strlen(pkgn), item);
|
2019-04-19 18:20:23 +05:30
|
|
|
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
addDepn(struct item *item, struct item *xitem)
|
|
|
|
{
|
2020-02-12 13:44:25 +05:30
|
|
|
struct depn *depn = calloc(1, sizeof(*depn));
|
2019-04-19 18:20:23 +05:30
|
|
|
|
2020-02-12 13:44:25 +05:30
|
|
|
assert(depn);
|
2019-04-19 18:20:23 +05:30
|
|
|
assert(item);
|
|
|
|
assert(xitem);
|
|
|
|
|
|
|
|
depn->item = item;
|
|
|
|
depn->dnext = xitem->dbase;
|
|
|
|
xitem->dbase = depn;
|
|
|
|
}
|
|
|
|
|
2019-07-01 01:30:58 +05:30
|
|
|
static void
|
2019-07-01 01:48:04 +05:30
|
|
|
add_deps_recursive(struct item *item, bool first)
|
2019-07-01 01:30:58 +05:30
|
|
|
{
|
|
|
|
struct depn *dep;
|
|
|
|
xbps_string_t str;
|
|
|
|
|
|
|
|
if (xbps_match_string_in_array(result, item->pkgver))
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (dep = item->dbase; dep; dep = dep->dnext)
|
2019-07-01 01:48:04 +05:30
|
|
|
add_deps_recursive(dep->item, false);
|
|
|
|
|
|
|
|
if (first)
|
|
|
|
return;
|
2019-07-01 01:30:58 +05:30
|
|
|
|
|
|
|
str = xbps_string_create_cstring(item->pkgver);
|
|
|
|
assert(str);
|
|
|
|
xbps_array_add_first(result, str);
|
|
|
|
xbps_object_release(str);
|
|
|
|
}
|
|
|
|
|
2020-02-12 13:44:25 +05:30
|
|
|
static void
|
|
|
|
cleanup(void)
|
|
|
|
{
|
|
|
|
struct item *item, *itmp;
|
|
|
|
|
|
|
|
HASH_ITER(hh, items, item, itmp) {
|
|
|
|
HASH_DEL(items, item);
|
|
|
|
if (item->dbase)
|
|
|
|
free(item->dbase);
|
|
|
|
free(item->pkgn);
|
|
|
|
free(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-19 18:20:23 +05:30
|
|
|
/*
|
|
|
|
* Recursively calculate all dependencies.
|
|
|
|
*/
|
|
|
|
static struct item *
|
2019-07-01 01:31:37 +05:30
|
|
|
ordered_depends(struct xbps_handle *xhp, xbps_dictionary_t pkgd, bool rpool,
|
|
|
|
size_t depth)
|
2014-11-20 13:53:51 +05:30
|
|
|
{
|
2019-04-19 18:20:23 +05:30
|
|
|
xbps_array_t rdeps, provides;
|
|
|
|
xbps_string_t str;
|
2020-02-12 13:44:25 +05:30
|
|
|
struct item *item = NULL, *xitem = NULL;
|
2020-02-21 13:38:22 +05:30
|
|
|
const char *pkgver = NULL, *pkgname = NULL;
|
2019-04-19 18:20:23 +05:30
|
|
|
|
|
|
|
assert(xhp);
|
|
|
|
assert(pkgd);
|
2015-03-20 12:33:06 +05:30
|
|
|
|
|
|
|
rdeps = xbps_dictionary_get(pkgd, "run_depends");
|
|
|
|
provides = xbps_dictionary_get(pkgd, "provides");
|
2020-02-21 13:38:22 +05:30
|
|
|
xbps_dictionary_get_cstring_nocopy(pkgd, "pkgname", &pkgname);
|
2019-04-19 18:20:23 +05:30
|
|
|
|
2020-02-21 13:38:22 +05:30
|
|
|
item = lookupItem(pkgname);
|
2019-07-01 01:48:04 +05:30
|
|
|
if (item) {
|
|
|
|
add_deps_recursive(item, depth == 0);
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2020-02-12 13:44:25 +05:30
|
|
|
if (!xbps_dictionary_get_cstring_nocopy(pkgd, "pkgver", &pkgver)) {
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
item = addItem(rdeps, pkgname, pkgver);
|
2019-04-19 18:20:23 +05:30
|
|
|
assert(item);
|
2014-11-20 13:53:51 +05:30
|
|
|
|
|
|
|
for (unsigned int i = 0; i < xbps_array_count(rdeps); i++) {
|
2015-03-20 12:33:06 +05:30
|
|
|
xbps_dictionary_t curpkgd;
|
2019-06-27 20:39:43 +05:30
|
|
|
const char *curdep = NULL;
|
2020-02-09 00:01:29 +05:30
|
|
|
char curdepname[XBPS_NAME_SIZE];
|
2014-11-20 13:53:51 +05:30
|
|
|
|
|
|
|
xbps_array_get_cstring_nocopy(rdeps, i, &curdep);
|
|
|
|
if (rpool) {
|
2019-04-19 18:20:23 +05:30
|
|
|
if ((curpkgd = xbps_rpool_get_pkg(xhp, curdep)) == NULL)
|
2015-03-20 12:33:06 +05:30
|
|
|
curpkgd = xbps_rpool_get_virtualpkg(xhp, curdep);
|
2014-11-20 13:53:51 +05:30
|
|
|
} else {
|
2019-04-19 18:20:23 +05:30
|
|
|
if ((curpkgd = xbps_pkgdb_get_pkg(xhp, curdep)) == NULL)
|
2015-03-20 12:33:06 +05:30
|
|
|
curpkgd = xbps_pkgdb_get_virtualpkg(xhp, curdep);
|
2019-05-27 16:09:28 +05:30
|
|
|
/* Ignore missing local runtime dependencies, because ignorepkg */
|
|
|
|
if (curpkgd == NULL)
|
|
|
|
continue;
|
2014-11-20 13:53:51 +05:30
|
|
|
}
|
2019-06-15 21:23:02 +05:30
|
|
|
if (curpkgd == NULL) {
|
|
|
|
/* package depends on missing dependencies */
|
2022-12-24 18:16:33 +05:30
|
|
|
xbps_dbg_printf("%s: missing dependency '%s'\n", pkgver, curdep);
|
2019-06-15 21:23:02 +05:30
|
|
|
errno = ENODEV;
|
|
|
|
return NULL;
|
|
|
|
}
|
2020-02-09 00:01:29 +05:30
|
|
|
if ((!xbps_pkgpattern_name(curdepname, XBPS_NAME_SIZE, curdep)) &&
|
|
|
|
(!xbps_pkg_name(curdepname, XBPS_NAME_SIZE, curdep))) {
|
|
|
|
abort();
|
|
|
|
}
|
2015-03-20 12:33:06 +05:30
|
|
|
|
|
|
|
if (provides && xbps_match_pkgname_in_array(provides, curdepname)) {
|
2022-12-24 18:16:33 +05:30
|
|
|
xbps_dbg_printf("%s: ignoring dependency %s "
|
2015-03-20 12:33:06 +05:30
|
|
|
"already in provides\n", pkgver, curdep);
|
|
|
|
continue;
|
|
|
|
}
|
2019-04-19 18:20:23 +05:30
|
|
|
xitem = lookupItem(curdepname);
|
2019-07-01 01:30:58 +05:30
|
|
|
if (xitem) {
|
2019-07-01 01:48:04 +05:30
|
|
|
add_deps_recursive(xitem, false);
|
2019-07-01 01:30:58 +05:30
|
|
|
continue;
|
|
|
|
}
|
2019-07-01 01:31:37 +05:30
|
|
|
xitem = ordered_depends(xhp, curpkgd, rpool, depth+1);
|
2019-06-15 21:45:13 +05:30
|
|
|
if (xitem == NULL) {
|
|
|
|
/* package depends on missing dependencies */
|
2022-12-24 18:16:33 +05:30
|
|
|
xbps_dbg_printf("%s: missing dependency '%s'\n", pkgver, curdep);
|
2019-06-15 21:45:13 +05:30
|
|
|
errno = ENODEV;
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-04-19 18:20:23 +05:30
|
|
|
assert(xitem);
|
|
|
|
addDepn(item, xitem);
|
2014-11-20 13:53:51 +05:30
|
|
|
}
|
2019-04-19 18:20:23 +05:30
|
|
|
/* all deps were processed, add item to head */
|
2019-07-01 01:31:37 +05:30
|
|
|
if (depth > 0 && !xbps_match_string_in_array(result, item->pkgver)) {
|
2019-04-20 22:34:13 +05:30
|
|
|
str = xbps_string_create_cstring(item->pkgver);
|
|
|
|
assert(str);
|
|
|
|
xbps_array_add_first(result, str);
|
|
|
|
xbps_object_release(str);
|
|
|
|
}
|
2019-04-19 18:20:23 +05:30
|
|
|
return item;
|
2014-11-20 13:53:51 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
xbps_array_t HIDDEN
|
|
|
|
xbps_get_pkg_fulldeptree(struct xbps_handle *xhp, const char *pkg, bool rpool)
|
|
|
|
{
|
|
|
|
xbps_dictionary_t pkgd;
|
2019-04-19 18:20:23 +05:30
|
|
|
|
|
|
|
result = xbps_array_create();
|
|
|
|
assert(result);
|
2014-11-20 13:53:51 +05:30
|
|
|
|
|
|
|
if (rpool) {
|
|
|
|
if (((pkgd = xbps_rpool_get_pkg(xhp, pkg)) == NULL) &&
|
|
|
|
((pkgd = xbps_rpool_get_virtualpkg(xhp, pkg)) == NULL))
|
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
if (((pkgd = xbps_pkgdb_get_pkg(xhp, pkg)) == NULL) &&
|
|
|
|
((pkgd = xbps_pkgdb_get_virtualpkg(xhp, pkg)) == NULL))
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-07-01 01:31:37 +05:30
|
|
|
if (ordered_depends(xhp, pkgd, rpool, 0) == NULL)
|
2019-06-15 21:23:02 +05:30
|
|
|
return NULL;
|
2014-11-20 13:53:51 +05:30
|
|
|
|
2020-02-12 13:44:25 +05:30
|
|
|
cleanup();
|
2019-04-19 18:20:23 +05:30
|
|
|
return result;
|
2014-11-20 13:53:51 +05:30
|
|
|
}
|