2009-08-17 22:37:20 +05:30
|
|
|
/*-
|
2011-01-17 21:47:02 +05:30
|
|
|
* Copyright (c) 2009-2011 Juan Romero Pardines.
|
2009-08-17 22:37:20 +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 <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <xbps_api.h>
|
2010-11-13 07:48:58 +05:30
|
|
|
#include "xbps_api_impl.h"
|
2009-08-17 22:37:20 +05:30
|
|
|
|
2011-01-17 21:47:02 +05:30
|
|
|
/*
|
|
|
|
* Sorting algorithm for packages in the transaction dictionary.
|
|
|
|
* The transaction dictionary contains all package dictionaries found from
|
|
|
|
* the repository plist index file in the "unsorted_deps" array.
|
|
|
|
*
|
|
|
|
* When a package has no rundeps or all rundeps are satisfied, the package
|
|
|
|
* dictionary is added into the "packages" array and it is removed from the
|
|
|
|
* "unsorted_deps" array; that means the package has been sorted in the
|
|
|
|
* transaction.
|
|
|
|
*
|
|
|
|
* It will loop until all packages are processed and will check that
|
|
|
|
* the number of packages added into the "packages" array is the same than
|
|
|
|
* it was in the "unsorted_deps" array.
|
|
|
|
*/
|
2010-01-21 07:40:19 +05:30
|
|
|
int HIDDEN
|
2011-01-24 21:25:58 +05:30
|
|
|
xbps_sort_pkg_deps(void)
|
2009-08-17 22:37:20 +05:30
|
|
|
{
|
2011-01-24 21:25:58 +05:30
|
|
|
prop_dictionary_t transd;
|
|
|
|
prop_array_t sorted, unsorted, rundeps;
|
2009-08-17 22:37:20 +05:30
|
|
|
prop_object_t obj, obj2;
|
|
|
|
prop_object_iterator_t iter, iter2;
|
|
|
|
size_t ndeps = 0, rundepscnt = 0, cnt = 0;
|
2009-11-29 10:23:27 +05:30
|
|
|
const char *pkgname, *pkgver, *str;
|
|
|
|
char *pkgnamedep;
|
2009-08-17 22:37:20 +05:30
|
|
|
int rv = 0;
|
|
|
|
|
2011-01-24 21:25:58 +05:30
|
|
|
if ((transd = xbps_transaction_dictionary_get()) == NULL)
|
|
|
|
return EINVAL;
|
2009-08-17 22:37:20 +05:30
|
|
|
|
|
|
|
sorted = prop_array_create();
|
|
|
|
if (sorted == NULL)
|
|
|
|
return ENOMEM;
|
2011-01-17 21:47:02 +05:30
|
|
|
/*
|
|
|
|
* Add sorted packages array into transaction dictionary (empty).
|
|
|
|
*/
|
|
|
|
if (!prop_dictionary_set(transd, "packages", sorted)) {
|
|
|
|
rv = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
2009-08-17 22:37:20 +05:30
|
|
|
/*
|
|
|
|
* All required deps are satisfied (already installed).
|
|
|
|
*/
|
2011-01-17 21:47:02 +05:30
|
|
|
unsorted = prop_dictionary_get(transd, "unsorted_deps");
|
2009-08-17 22:37:20 +05:30
|
|
|
if (prop_array_count(unsorted) == 0) {
|
2011-01-17 21:47:02 +05:30
|
|
|
prop_dictionary_set(transd, "packages", sorted);
|
2009-08-17 22:37:20 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
ndeps = prop_array_count(unsorted);
|
|
|
|
|
|
|
|
iter = prop_array_iterator(unsorted);
|
|
|
|
if (iter == NULL) {
|
|
|
|
prop_object_release(sorted);
|
|
|
|
return ENOMEM;
|
|
|
|
}
|
|
|
|
again:
|
|
|
|
/*
|
|
|
|
* Order all deps by looking at its run_depends array.
|
|
|
|
*/
|
|
|
|
while ((obj = prop_object_iterator_next(iter)) != NULL) {
|
2010-11-06 11:14:00 +05:30
|
|
|
prop_dictionary_get_cstring_nocopy(obj, "pkgname", &pkgname);
|
|
|
|
prop_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver);
|
2010-11-19 18:10:13 +05:30
|
|
|
xbps_dbg_printf("Sorting package '%s': ", pkgver);
|
2011-01-17 21:47:02 +05:30
|
|
|
|
|
|
|
if (xbps_find_pkg_in_dict_by_name(transd,
|
|
|
|
"packages", pkgname)) {
|
2010-11-19 18:10:13 +05:30
|
|
|
xbps_dbg_printf_append("skipping, already queued.\n",
|
|
|
|
pkgname);
|
2009-08-17 22:37:20 +05:30
|
|
|
continue;
|
2009-11-29 10:23:27 +05:30
|
|
|
}
|
2009-08-17 22:37:20 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Packages that don't have deps go unsorted, because
|
|
|
|
* it doesn't matter.
|
|
|
|
*/
|
|
|
|
rundeps = prop_dictionary_get(obj, "run_depends");
|
|
|
|
if (rundeps == NULL || prop_array_count(rundeps) == 0) {
|
2010-11-19 18:10:13 +05:30
|
|
|
xbps_dbg_printf_append("added (no rundeps) into "
|
|
|
|
"the sorted queue.\n");
|
2011-01-17 21:47:02 +05:30
|
|
|
prop_array_add(sorted, obj);
|
|
|
|
if (!xbps_remove_pkg_from_dict(transd,
|
|
|
|
"unsorted_deps", pkgname)) {
|
|
|
|
xbps_dbg_printf("can't remove %s from "
|
|
|
|
"unsorted_deps array!\n", pkgname);
|
|
|
|
}
|
2009-08-17 22:37:20 +05:30
|
|
|
cnt++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
iter2 = prop_array_iterator(rundeps);
|
|
|
|
if (iter2 == NULL) {
|
|
|
|
rv = ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
2010-11-19 18:10:13 +05:30
|
|
|
|
2009-08-17 22:37:20 +05:30
|
|
|
/*
|
|
|
|
* Iterate over the run_depends array, and find out if they
|
|
|
|
* were already added in the sorted list.
|
|
|
|
*/
|
2010-11-19 18:10:13 +05:30
|
|
|
xbps_dbg_printf_append("\n");
|
|
|
|
xbps_dbg_printf("Checking '%s' run depends for sorting...\n",
|
|
|
|
pkgver);
|
2009-08-17 22:37:20 +05:30
|
|
|
while ((obj2 = prop_object_iterator_next(iter2)) != NULL) {
|
2009-11-07 09:26:20 +05:30
|
|
|
str = prop_string_cstring_nocopy(obj2);
|
2009-11-23 15:16:51 +05:30
|
|
|
if (str == NULL) {
|
|
|
|
rv = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
2010-01-21 07:40:19 +05:30
|
|
|
pkgnamedep = xbps_get_pkgpattern_name(str);
|
2009-11-29 10:23:27 +05:30
|
|
|
if (pkgnamedep == NULL) {
|
2010-11-19 18:10:13 +05:30
|
|
|
rv = EINVAL;
|
2009-11-23 15:16:51 +05:30
|
|
|
goto out;
|
|
|
|
}
|
2010-11-19 18:10:13 +05:30
|
|
|
xbps_dbg_printf(" Required dependency '%s': ", str);
|
2009-08-17 22:37:20 +05:30
|
|
|
/*
|
2009-11-29 10:23:27 +05:30
|
|
|
* If dependency is already satisfied or queued,
|
2009-08-17 22:37:20 +05:30
|
|
|
* pass to the next one.
|
|
|
|
*/
|
2009-11-29 10:23:27 +05:30
|
|
|
if (xbps_check_is_installed_pkg(str)) {
|
2009-08-17 22:37:20 +05:30
|
|
|
rundepscnt++;
|
2010-11-19 18:10:13 +05:30
|
|
|
xbps_dbg_printf_append("installed.\n");
|
2011-01-17 21:47:02 +05:30
|
|
|
} else if (xbps_find_pkg_in_dict_by_name(transd,
|
|
|
|
"packages", pkgnamedep)) {
|
2010-11-19 18:10:13 +05:30
|
|
|
xbps_dbg_printf_append("queued.\n");
|
2009-08-17 22:37:20 +05:30
|
|
|
rundepscnt++;
|
2009-11-29 10:23:27 +05:30
|
|
|
} else {
|
2010-11-19 18:10:13 +05:30
|
|
|
xbps_dbg_printf_append("not installed.\n");
|
2009-11-29 10:23:27 +05:30
|
|
|
}
|
|
|
|
free(pkgnamedep);
|
2009-08-17 22:37:20 +05:30
|
|
|
}
|
|
|
|
prop_object_iterator_release(iter2);
|
|
|
|
|
|
|
|
/* Add dependency if all its required deps are already added */
|
|
|
|
if (prop_array_count(rundeps) == rundepscnt) {
|
2011-01-17 21:47:02 +05:30
|
|
|
prop_array_add(sorted, obj);
|
|
|
|
if (!xbps_remove_pkg_from_dict(transd,
|
|
|
|
"unsorted_deps", pkgname)) {
|
|
|
|
xbps_dbg_printf("can't remove %s from "
|
|
|
|
"unsorted_deps array!\n", pkgname);
|
|
|
|
}
|
2010-11-19 18:10:13 +05:30
|
|
|
xbps_dbg_printf("Added package '%s' to the sorted "
|
|
|
|
"queue (all rundeps satisfied).\n\n", pkgver);
|
2009-08-17 22:37:20 +05:30
|
|
|
rundepscnt = 0;
|
|
|
|
cnt++;
|
|
|
|
continue;
|
|
|
|
}
|
2010-11-19 18:10:13 +05:30
|
|
|
xbps_dbg_printf("Unsorted package '%s' has missing "
|
|
|
|
"rundeps (missing %zu).\n\n", pkgver,
|
|
|
|
prop_array_count(rundeps) - rundepscnt);
|
2009-08-17 22:37:20 +05:30
|
|
|
rundepscnt = 0;
|
|
|
|
}
|
|
|
|
/* Iterate until all deps are processed. */
|
|
|
|
if (cnt < ndeps) {
|
2010-11-19 18:10:13 +05:30
|
|
|
xbps_dbg_printf("Missing required deps! queued: %zu "
|
|
|
|
"required: %zu.\n", cnt, ndeps);
|
2009-08-17 22:37:20 +05:30
|
|
|
prop_object_iterator_reset(iter);
|
2011-01-17 21:47:02 +05:30
|
|
|
xbps_dbg_printf("total iteratons %zu\n", cnt);
|
2009-08-17 22:37:20 +05:30
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
prop_object_iterator_release(iter);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Sanity check that the array contains the same number of
|
|
|
|
* objects than the total number of required dependencies.
|
|
|
|
*/
|
|
|
|
if (ndeps != prop_array_count(sorted)) {
|
2011-01-18 19:14:39 +05:30
|
|
|
xbps_dbg_printf("wrong sorted deps cnt %zu vs %zu\n",
|
|
|
|
ndeps, prop_array_count(sorted));
|
2009-08-17 22:37:20 +05:30
|
|
|
rv = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
/*
|
2011-01-17 21:47:02 +05:30
|
|
|
* We are done, all packages were sorted... remove the
|
|
|
|
* temporary array with unsorted packages.
|
2009-08-17 22:37:20 +05:30
|
|
|
*/
|
2011-01-17 21:47:02 +05:30
|
|
|
prop_dictionary_remove(transd, "unsorted_deps");
|
2009-08-17 22:37:20 +05:30
|
|
|
|
2011-01-17 21:47:02 +05:30
|
|
|
out:
|
2011-01-18 19:14:39 +05:30
|
|
|
if (rv != 0)
|
2011-01-17 21:47:02 +05:30
|
|
|
prop_dictionary_remove(transd, "packages");
|
2011-01-18 19:14:39 +05:30
|
|
|
|
|
|
|
prop_object_release(sorted);
|
|
|
|
|
2009-08-17 22:37:20 +05:30
|
|
|
return rv;
|
|
|
|
}
|