2009-02-12 16:30:38 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2009 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
|
|
|
|
|
|
|
#include <xbps_api.h>
|
|
|
|
|
|
|
|
struct sorted_dependency {
|
2009-02-14 04:45:04 +00:00
|
|
|
SIMPLEQ_ENTRY(sorted_dependency) chain;
|
2009-02-12 16:30:38 +00:00
|
|
|
prop_dictionary_t dict;
|
2009-02-14 00:37:12 +00:00
|
|
|
prop_array_t reqby;
|
2009-02-12 16:30:38 +00:00
|
|
|
size_t idx;
|
2009-02-14 00:37:12 +00:00
|
|
|
size_t prio;
|
2009-02-12 16:30:38 +00:00
|
|
|
};
|
|
|
|
|
2009-02-14 04:45:04 +00:00
|
|
|
static SIMPLEQ_HEAD(sdep_head, sorted_dependency) sdep_list =
|
|
|
|
SIMPLEQ_HEAD_INITIALIZER(sdep_list);
|
2009-02-12 16:30:38 +00:00
|
|
|
|
|
|
|
static ssize_t
|
2009-02-14 05:06:01 +00:00
|
|
|
find_pkgdict_with_highest_prio(prop_array_t array, uint32_t *maxprio)
|
2009-02-12 16:30:38 +00:00
|
|
|
{
|
|
|
|
prop_object_t obj;
|
|
|
|
prop_object_iterator_t iter;
|
|
|
|
uint32_t prio = 0;
|
|
|
|
size_t idx = 0;
|
|
|
|
ssize_t curidx = -1;
|
|
|
|
|
|
|
|
assert(array != NULL);
|
|
|
|
|
|
|
|
iter = prop_array_iterator(array);
|
|
|
|
if (iter == NULL) {
|
|
|
|
errno = ENOMEM;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Finds the index of a package with the highest priority.
|
|
|
|
*/
|
|
|
|
while ((obj = prop_object_iterator_next(iter)) != NULL) {
|
|
|
|
prop_dictionary_get_uint32(obj, "priority", &prio);
|
2009-02-14 05:06:01 +00:00
|
|
|
if (*maxprio <= prio) {
|
|
|
|
curidx = idx;
|
|
|
|
*maxprio = prio;
|
2009-02-12 16:30:38 +00:00
|
|
|
}
|
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
prop_object_iterator_release(iter);
|
|
|
|
|
|
|
|
if (curidx == -1)
|
|
|
|
errno = ENOENT;
|
|
|
|
|
|
|
|
return curidx;
|
|
|
|
}
|
|
|
|
|
2009-02-14 00:37:12 +00:00
|
|
|
static struct sorted_dependency *
|
|
|
|
find_sorteddep_with_highest_prio(void)
|
|
|
|
{
|
|
|
|
struct sorted_dependency *sdep;
|
|
|
|
size_t maxprio = 0;
|
|
|
|
size_t curidx = 0, idx = 0;
|
|
|
|
|
2009-02-14 04:45:04 +00:00
|
|
|
SIMPLEQ_FOREACH(sdep, &sdep_list, chain) {
|
2009-02-14 00:37:12 +00:00
|
|
|
if (maxprio <= sdep->prio) {
|
|
|
|
curidx = idx;
|
|
|
|
maxprio = sdep->prio;
|
|
|
|
}
|
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
|
|
|
|
idx = 0;
|
2009-02-14 04:45:04 +00:00
|
|
|
SIMPLEQ_FOREACH(sdep, &sdep_list, chain) {
|
2009-02-14 00:37:12 +00:00
|
|
|
if (idx == curidx)
|
|
|
|
break;
|
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sdep;
|
|
|
|
}
|
|
|
|
|
2009-02-12 16:30:38 +00:00
|
|
|
static struct sorted_dependency *
|
|
|
|
find_sorteddep_by_name(const char *pkgname)
|
|
|
|
{
|
|
|
|
struct sorted_dependency *sdep;
|
|
|
|
const char *curname;
|
|
|
|
|
2009-02-14 04:45:04 +00:00
|
|
|
SIMPLEQ_FOREACH(sdep, &sdep_list, chain) {
|
2009-02-12 16:30:38 +00:00
|
|
|
prop_dictionary_get_cstring_nocopy(sdep->dict,
|
|
|
|
"pkgname", &curname);
|
|
|
|
if (strcmp(pkgname, curname) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sdep;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
xbps_sort_pkg_deps(prop_dictionary_t chaindeps)
|
|
|
|
{
|
2009-02-14 00:37:12 +00:00
|
|
|
prop_array_t sorted, unsorted, rundeps_array, reqby;
|
2009-02-12 16:30:38 +00:00
|
|
|
prop_dictionary_t dict;
|
|
|
|
prop_object_t obj;
|
|
|
|
prop_object_iterator_t iter;
|
|
|
|
struct sorted_dependency *sdep, *sdep2;
|
|
|
|
uint32_t maxprio = 0;
|
2009-02-18 00:07:51 +00:00
|
|
|
size_t indirdepscnt = 0, dirdepscnt = 0, cnt = 0;
|
|
|
|
ssize_t curidx = 0;
|
2009-02-12 16:30:38 +00:00
|
|
|
const char *curpkg, *rundep;
|
|
|
|
char *pkgname;
|
|
|
|
int rv = 0;
|
|
|
|
|
|
|
|
assert(chaindeps != NULL);
|
|
|
|
|
|
|
|
sorted = prop_array_create();
|
|
|
|
if (sorted == NULL)
|
|
|
|
return ENOMEM;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* All required deps are satisfied (already installed).
|
|
|
|
*/
|
|
|
|
unsorted = prop_dictionary_get(chaindeps, "unsorted_deps");
|
2009-02-14 00:37:12 +00:00
|
|
|
if (prop_array_count(unsorted) == 0) {
|
2009-02-12 18:00:03 +00:00
|
|
|
prop_object_release(sorted);
|
2009-02-12 16:30:38 +00:00
|
|
|
return 0;
|
2009-02-12 18:00:03 +00:00
|
|
|
}
|
2009-02-12 16:30:38 +00:00
|
|
|
|
|
|
|
prop_dictionary_get_uint32(chaindeps, "indirectdeps_count",
|
|
|
|
&indirdepscnt);
|
|
|
|
prop_dictionary_get_uint32(chaindeps, "directdeps_count",
|
|
|
|
&dirdepscnt);
|
|
|
|
unsorted = prop_dictionary_get(chaindeps, "unsorted_deps");
|
|
|
|
/*
|
2009-02-14 05:06:01 +00:00
|
|
|
* Pass 1: order all deps (direct/indirect) by priority.
|
2009-02-12 16:30:38 +00:00
|
|
|
*/
|
2009-02-14 05:06:01 +00:00
|
|
|
while (cnt < dirdepscnt + indirdepscnt) {
|
|
|
|
curidx = find_pkgdict_with_highest_prio(unsorted, &maxprio);
|
2009-02-12 16:30:38 +00:00
|
|
|
if (curidx == -1) {
|
|
|
|
rv = errno;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
dict = prop_array_get(unsorted, curidx);
|
|
|
|
if (dict == NULL) {
|
|
|
|
rv = errno;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
sdep = calloc(1, sizeof(*sdep));
|
|
|
|
if (sdep == NULL) {
|
|
|
|
rv = ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
sdep->dict = prop_dictionary_copy(dict);
|
|
|
|
sdep->idx = cnt;
|
2009-02-14 00:37:12 +00:00
|
|
|
prop_dictionary_get_uint32(dict, "priority", &sdep->prio);
|
|
|
|
reqby = prop_dictionary_get(dict, "required_by");
|
|
|
|
if (reqby && prop_array_count(reqby) > 0)
|
|
|
|
sdep->reqby = prop_array_copy(reqby);
|
2009-02-14 04:45:04 +00:00
|
|
|
SIMPLEQ_INSERT_TAIL(&sdep_list, sdep, chain);
|
2009-02-12 16:30:38 +00:00
|
|
|
prop_array_remove(unsorted, curidx);
|
|
|
|
maxprio = 0;
|
|
|
|
cnt++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2009-02-14 05:06:01 +00:00
|
|
|
* Pass 2: increase priority of dependencies any time
|
2009-02-14 00:37:12 +00:00
|
|
|
* a package requires them.
|
2009-02-12 16:30:38 +00:00
|
|
|
*/
|
2009-02-14 04:45:04 +00:00
|
|
|
SIMPLEQ_FOREACH(sdep, &sdep_list, chain) {
|
2009-02-12 16:30:38 +00:00
|
|
|
prop_dictionary_get_cstring_nocopy(sdep->dict,
|
|
|
|
"pkgname", &curpkg);
|
|
|
|
rundeps_array = prop_dictionary_get(sdep->dict, "run_depends");
|
2009-03-01 16:33:19 +00:00
|
|
|
if (rundeps_array == NULL) {
|
|
|
|
sdep->prio += 4;
|
2009-02-12 16:30:38 +00:00
|
|
|
continue;
|
2009-03-01 16:33:19 +00:00
|
|
|
}
|
2009-02-12 16:30:38 +00:00
|
|
|
|
|
|
|
iter = prop_array_iterator(rundeps_array);
|
|
|
|
if (iter == NULL) {
|
|
|
|
rv = ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((obj = prop_object_iterator_next(iter)) != NULL) {
|
|
|
|
rundep = prop_string_cstring_nocopy(obj);
|
|
|
|
pkgname = xbps_get_pkg_name(rundep);
|
|
|
|
/*
|
|
|
|
* If package is installed, pass to the next one.
|
|
|
|
*/
|
2009-03-17 03:32:03 +00:00
|
|
|
if (xbps_check_is_installed_pkg(rundep) >= 0) {
|
2009-02-12 16:30:38 +00:00
|
|
|
free(pkgname);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* Ignore itself */
|
|
|
|
if (strcmp(curpkg, pkgname) == 0) {
|
|
|
|
free(pkgname);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
sdep2 = find_sorteddep_by_name(pkgname);
|
|
|
|
free(pkgname);
|
2009-02-14 00:37:12 +00:00
|
|
|
sdep2->prio++;
|
2009-02-12 16:30:38 +00:00
|
|
|
}
|
|
|
|
prop_object_iterator_release(iter);
|
|
|
|
}
|
|
|
|
prop_dictionary_remove(chaindeps, "unsorted_deps");
|
|
|
|
|
|
|
|
/*
|
2009-02-14 05:06:01 +00:00
|
|
|
* Pass 3: increase priority of a package, by looking at
|
2009-02-14 00:37:12 +00:00
|
|
|
* its required_by array member's priority.
|
2009-02-12 16:30:38 +00:00
|
|
|
*/
|
2009-02-14 04:45:04 +00:00
|
|
|
SIMPLEQ_FOREACH(sdep, &sdep_list, chain) {
|
2009-02-14 00:37:12 +00:00
|
|
|
iter = prop_array_iterator(sdep->reqby);
|
|
|
|
if (iter == NULL)
|
2009-02-12 16:30:38 +00:00
|
|
|
continue;
|
|
|
|
|
2009-02-14 00:37:12 +00:00
|
|
|
while ((obj = prop_object_iterator_next(iter))) {
|
|
|
|
rundep = prop_string_cstring_nocopy(obj);
|
|
|
|
pkgname = xbps_get_pkg_name(rundep);
|
|
|
|
sdep2 = find_sorteddep_by_name(pkgname);
|
|
|
|
if (sdep2 == NULL) {
|
|
|
|
free(pkgname);
|
|
|
|
continue;
|
2009-02-12 16:30:38 +00:00
|
|
|
}
|
2009-02-14 00:37:12 +00:00
|
|
|
free(pkgname);
|
|
|
|
sdep->prio += sdep2->prio + 1;
|
2009-02-12 16:30:38 +00:00
|
|
|
}
|
2009-02-14 00:37:12 +00:00
|
|
|
prop_object_iterator_release(iter);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2009-02-14 05:06:01 +00:00
|
|
|
* Pass 4: copy dictionaries into the final array with the
|
2009-02-14 00:37:12 +00:00
|
|
|
* correct index position for all dependencies and release
|
|
|
|
* resources used by the sorting passes.
|
|
|
|
*/
|
|
|
|
while ((sdep = find_sorteddep_with_highest_prio()) != NULL) {
|
|
|
|
prop_array_add(sorted, sdep->dict);
|
2009-02-14 04:45:04 +00:00
|
|
|
SIMPLEQ_REMOVE(&sdep_list, sdep, sorted_dependency, chain);
|
2009-02-14 00:37:12 +00:00
|
|
|
prop_object_release(sdep->dict);
|
|
|
|
prop_object_release(sdep->reqby);
|
|
|
|
free(sdep);
|
2009-02-12 16:30:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Sanity check that the array contains the same number of
|
|
|
|
* objects than the total number of required dependencies.
|
|
|
|
*/
|
|
|
|
cnt = dirdepscnt + indirdepscnt;
|
|
|
|
if (cnt != prop_array_count(sorted)) {
|
|
|
|
rv = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!prop_dictionary_set(chaindeps, "required_deps", sorted))
|
|
|
|
rv = EINVAL;
|
|
|
|
out:
|
|
|
|
/*
|
|
|
|
* Release resources used by temporary sorting.
|
|
|
|
*/
|
|
|
|
prop_object_release(sorted);
|
2009-02-14 04:45:04 +00:00
|
|
|
while ((sdep = SIMPLEQ_FIRST(&sdep_list)) != NULL) {
|
|
|
|
SIMPLEQ_REMOVE(&sdep_list, sdep, sorted_dependency, chain);
|
2009-02-12 16:30:38 +00:00
|
|
|
prop_object_release(sdep->dict);
|
2009-02-14 00:37:12 +00:00
|
|
|
prop_object_release(sdep->reqby);
|
2009-02-12 16:30:38 +00:00
|
|
|
free(sdep);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|