void-packages/lib/depends.c

293 lines
7.1 KiB
C
Raw Normal View History

/*-
* Copyright (c) 2008 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 pkg_dependency {
LIST_ENTRY(pkg_dependency) deps;
prop_dictionary_t repo;
char *name;
};
static LIST_HEAD(, pkg_dependency) pkg_deps_list =
LIST_HEAD_INITIALIZER(pkg_deps_list);
int
xbps_check_is_installed_pkg(const char *plist, const char *pkg)
{
prop_dictionary_t dict, pkgdict;
prop_object_t obj;
const char *reqver, *instver;
char *pkgname;
pkgname = xbps_get_pkg_name(pkg);
reqver = xbps_get_pkg_version(pkg);
/* Get package dictionary from plist */
dict = prop_dictionary_internalize_from_file(plist);
if (dict == NULL) {
free(pkgname);
return XBPS_PKG_EEMPTY;
}
pkgdict = xbps_find_pkg_in_dict(dict, pkgname);
if (pkgdict == NULL) {
prop_object_release(dict);
free(pkgname);
return 1; /* not installed */
}
/* Get version from installed package */
obj = prop_dictionary_get(pkgdict, "version");
assert(obj != NULL);
assert(prop_object_type(obj) == PROP_TYPE_STRING);
instver = prop_string_cstring_nocopy(obj);
assert(instver != NULL);
free(pkgname);
prop_object_release(dict);
return (xbps_cmpver_versions(instver, reqver) > 0) ? 1 : 0;
}
void
xbps_clean_pkg_depslist(void)
{
struct pkg_dependency *dep;
LIST_FOREACH(dep, &pkg_deps_list, deps) {
LIST_REMOVE(dep, deps);
free(dep->name);
prop_object_release(dep->repo);
}
}
void
xbps_add_pkg_dependency(const char *pkgname, prop_dictionary_t repo)
{
struct pkg_dependency *dep;
size_t len = 0;
assert(repo != NULL);
assert(pkgname != NULL);
assert(pkgdict != NULL);
LIST_FOREACH(dep, &pkg_deps_list, deps)
if (strcmp(dep->name, pkgname) == 0)
return;
dep = NULL;
dep = malloc(sizeof(*dep));
assert(dep != NULL);
len = strlen(pkgname) + 1;
dep->name = malloc(len);
assert(dep != NULL);
memcpy(dep->name, pkgname, len - 1);
dep->name[len - 1] = '\0';
dep->repo = prop_dictionary_copy(repo);
LIST_INSERT_HEAD(&pkg_deps_list, dep, deps);
}
static int
find_deps_in_pkg(prop_dictionary_t repo, prop_dictionary_t pkg)
{
prop_dictionary_t pkgdict;
prop_array_t array;
prop_object_t obj;
prop_object_iterator_t iter = NULL;
const char *reqpkg;
char *pkgname;
array = prop_dictionary_get(pkg, "run_depends");
if (array == NULL || prop_array_count(array) == 0)
return 0;
iter = prop_array_iterator(array);
if (iter == NULL)
return -1;
/*
* Iterate over the list of required run dependencies for
* a package.
*/
while ((obj = prop_object_iterator_next(iter))) {
/*
* If required package is not in repo, just pass to the
* next one.
*/
reqpkg = prop_string_cstring_nocopy(obj);
pkgname = xbps_get_pkg_name(reqpkg);
pkgdict = xbps_find_pkg_in_dict(repo, pkgname);
if (pkgdict == NULL) {
free(pkgname);
continue;
}
/*
* Package is on repo, add it into the list.
*/
xbps_add_pkg_dependency(pkgname, repo);
free(pkgname);
/* Iterate on required pkg to find more deps */
if (xbps_pkg_has_rundeps(pkgdict)) {
/* more deps? */
if (!find_deps_in_pkg(repo, pkgdict))
continue;
}
}
prop_object_iterator_release(iter);
return 0;
}
int
xbps_install_pkg_deps(prop_array_t repolist, prop_dictionary_t pkg)
{
prop_dictionary_t repo, pkgdict;
prop_array_t array;
prop_object_iterator_t iter;
prop_object_t obj;
prop_string_t pkgname, version;
struct pkg_dependency *dep;
size_t required_deps = 0, deps_found = 0;
const char *reqpkg, *verstr;
char plist[PATH_MAX], *namestr;
int rv = 0;
bool dep_found = false;
iter = prop_array_iterator(repolist);
if (iter == NULL)
return ENOMEM;
/*
* Iterate over the repository list and find out if we have
* all required dependencies.
*/
while ((obj = prop_object_iterator_next(iter)) != NULL) {
memset(plist, 0, sizeof(&plist));
if (!xbps_append_full_path(plist,
prop_string_cstring_nocopy(obj), XBPS_PKGINDEX)) {
xbps_clean_pkg_depslist();
prop_object_iterator_release(iter);
rv = EINVAL;
goto out;
}
repo = prop_dictionary_internalize_from_file(plist);
if (repo == NULL) {
prop_object_iterator_release(iter);
rv = errno;
goto out;
}
rv = find_deps_in_pkg(repo, pkg);
if (rv == -1) {
prop_object_release(repo);
prop_object_iterator_release(iter);
goto out;
}
prop_object_release(repo);
}
prop_object_iterator_release(iter);
/*
* Find out if all required dependencies and binary packages
* from repositories are there.
*/
array = prop_dictionary_get(pkg, "run_depends");
iter = prop_array_iterator(array);
if (iter == NULL) {
rv = ENOMEM;
goto out;
}
while ((obj = prop_object_iterator_next(iter)) != NULL) {
dep_found = false;
required_deps++;
reqpkg = prop_string_cstring_nocopy(obj);
namestr = xbps_get_pkg_name(reqpkg);
verstr = xbps_get_pkg_version(reqpkg);
LIST_FOREACH(dep, &pkg_deps_list, deps) {
if (strcmp(dep->name, namestr) == 0) {
deps_found++;
dep_found = true;
break;
}
}
if (dep_found == false) {
printf("Cannot find %s >= %s in repository list.\n",
namestr, verstr);
(void)fflush(stdout);
}
free(namestr);
}
prop_object_iterator_release(iter);
if (required_deps != deps_found) {
rv = XBPS_PKG_ENOTINREPO;
goto out;
}
/*
* Iterate over the list of dependencies and install them.
*/
LIST_FOREACH(dep, &pkg_deps_list, deps) {
pkgdict = xbps_find_pkg_in_dict(dep->repo, dep->name);
if (pkgdict == NULL) {
rv = EINVAL;
break;
}
pkgname = prop_dictionary_get(pkgdict, "pkgname");
version = prop_dictionary_get(pkgdict, "version");
printf("Required package: %s >= %s\n",
prop_string_cstring_nocopy(pkgname),
prop_string_cstring_nocopy(version));
(void)fflush(stdout);
rv = xbps_unpack_binary_pkg(dep->repo, pkgdict,
xbps_unpack_archive_cb);
if (rv != 0)
break;
}
out:
xbps_clean_pkg_depslist();
return rv;
}