Fix binpkg installation with multiple repositories.

New changes include a modified algorithm of the previous one, which
finally does not fail even with multiple repos, YAY!

Fixed some leaks as well, thanks to valgrind.

--HG--
extra : convert_revision : 3cffe1e9e9e9f7abf225ca3c6643377fc12a94bd
This commit is contained in:
Juan RP 2009-02-14 01:37:12 +01:00
parent 6afd85a491
commit 3c7b8186e6
9 changed files with 527 additions and 328 deletions

View file

@ -170,7 +170,7 @@ main(int argc, char **argv)
prop_dictionary_t dict; prop_dictionary_t dict;
repo_info_t *rinfo = NULL; repo_info_t *rinfo = NULL;
char dpkgidx[PATH_MAX], *root = NULL; char dpkgidx[PATH_MAX], *root = NULL;
int c; int c, rv = 0;
while ((c = getopt(argc, argv, "r:")) != -1) { while ((c = getopt(argc, argv, "r:")) != -1) {
switch (c) { switch (c) {
@ -291,16 +291,16 @@ main(int argc, char **argv)
usage(); usage();
dict = getrepolist_dict(root); dict = getrepolist_dict(root);
if (xbps_callback_array_iter_in_dict(dict, "repository-list", rv = xbps_callback_array_iter_in_dict(dict, "repository-list",
show_pkg_info_from_repolist, argv[1]) != 0) { show_pkg_info_from_repolist, argv[1]);
prop_object_release(dict); prop_object_release(dict);
free(plist); free(plist);
printf("ERROR: unable to locate package '%s'.\n",
argv[1]); if (rv == 0 && errno == ENOENT) {
printf("Unable to locate package '%s' from "
"repository pool.\n", argv[1]);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
prop_object_release(dict);
free(plist);
} else { } else {
usage(); usage();

View file

@ -180,7 +180,8 @@ show_pkg_info_from_repolist(prop_object_t obj, void *arg, bool *loop_done)
if (pkgdict == NULL) { if (pkgdict == NULL) {
prop_object_release(dict); prop_object_release(dict);
free(plist); free(plist);
return XBPS_PKG_ENOTINREPO; errno = ENOENT;
return 0;
} }
oloc = prop_dictionary_get(dict, "location-remote"); oloc = prop_dictionary_get(dict, "location-remote");

View file

@ -37,7 +37,7 @@ int xbps_unpack_binary_pkg(prop_dictionary_t, prop_dictionary_t,
const char *, const char *,
void (*cb_print)(prop_dictionary_t)); void (*cb_print)(prop_dictionary_t));
int xbps_update_pkg_requiredby(prop_array_t, prop_dictionary_t); int xbps_update_pkg_requiredby(prop_array_t, prop_dictionary_t);
int xbps_find_deps_in_pkg(prop_dictionary_t, prop_dictionary_t); int xbps_find_deps_in_pkg(prop_dictionary_t);
/* From lib/sortdeps.c */ /* From lib/sortdeps.c */
int xbps_sort_pkg_deps(prop_dictionary_t); int xbps_sort_pkg_deps(prop_dictionary_t);

View file

@ -44,9 +44,12 @@ bool xbps_add_obj_to_array(prop_array_t, prop_object_t);
*/ */
int int
xbps_callback_array_iter_in_dict(prop_dictionary_t, const char *, xbps_callback_array_iter_in_dict(prop_dictionary_t, const char *,
int (*func)(prop_object_t, void *, bool *), int (*fn)(prop_object_t, void *, bool *),
void *);
int
xbps_callback_array_iter_in_repolist(const char *,
int (*fn)(prop_object_t, void *, bool *),
void *); void *);
/* /*
* Finds a package's dictionary into the main dictionary. * Finds a package's dictionary into the main dictionary.
* *

View file

@ -47,12 +47,11 @@
/* Filename of the packages register. */ /* Filename of the packages register. */
#define XBPS_REGPKGDB "regpkgdb.plist" #define XBPS_REGPKGDB "regpkgdb.plist"
/* Return values for xbps_install_binary_pkg() */ #define XBPS_PKG_ENOTINREPO -1 /* Not found in repo */
#define XBPS_PKG_ENOTINREPO 1 /* Not found in repo */ #define XBPS_PKG_EEXIST -2 /* Already installed */
#define XBPS_PKG_EEXIST 2 /* Already installed */ #define XBPS_PKG_EINDEPS -3 /* Check deps failed */
#define XBPS_PKG_EINDEPS 3 /* Check deps failed */ #define XBPS_PKG_ECHDIRDEST -4 /* chdir() to dest failed */
#define XBPS_PKG_ECHDIRDEST 4 /* chdir() to dest failed */ #define XBPS_PKG_EEMPTY -5 /* empty pkg list */
#define XBPS_PKG_EEMPTY 5 /* empty pkg list */
#include "cmpver.h" #include "cmpver.h"
#include "fexec.h" #include "fexec.h"

View file

@ -33,8 +33,11 @@
#include <xbps_api.h> #include <xbps_api.h>
static int add_missing_reqdep(const char *, const char *); static int add_missing_reqdep(const char *, const char *);
static int check_missing_reqdep(const char *, const char *, size_t *); static int remove_missing_reqdep(const char *);
static void remove_missing_reqdep(size_t *); static int find_pkg_deps_from_repo(prop_dictionary_t, prop_dictionary_t,
prop_array_t);
static int find_pkg_missing_deps_from_repo(prop_dictionary_t,
prop_dictionary_t);
static prop_dictionary_t chaindeps; static prop_dictionary_t chaindeps;
@ -44,7 +47,7 @@ static prop_dictionary_t chaindeps;
static int static int
create_deps_dictionary(void) create_deps_dictionary(void)
{ {
prop_array_t installed, unsorted, missing; prop_array_t unsorted, missing;
int rv = 0; int rv = 0;
chaindeps = prop_dictionary_create(); chaindeps = prop_dictionary_create();
@ -57,36 +60,24 @@ create_deps_dictionary(void)
goto fail; goto fail;
} }
installed = prop_array_create(); unsorted = prop_array_create();
if (installed == NULL) { if (unsorted == NULL) {
rv = ENOMEM; rv = ENOMEM;
goto fail2; goto fail2;
} }
unsorted = prop_array_create();
if (unsorted == NULL) {
rv = ENOMEM;
goto fail3;
}
if (!xbps_add_obj_to_dict(chaindeps, missing, "missing_deps")) { if (!xbps_add_obj_to_dict(chaindeps, missing, "missing_deps")) {
rv = EINVAL; rv = EINVAL;
goto fail4; goto fail3;
}
if (!xbps_add_obj_to_dict(chaindeps, installed, "installed_deps")) {
rv = EINVAL;
goto fail4;
} }
if (!xbps_add_obj_to_dict(chaindeps, unsorted, "unsorted_deps")) { if (!xbps_add_obj_to_dict(chaindeps, unsorted, "unsorted_deps")) {
rv = EINVAL; rv = EINVAL;
goto fail4; goto fail3;
} }
return rv; return rv;
fail4:
prop_object_release(unsorted);
fail3: fail3:
prop_object_release(installed); prop_object_release(unsorted);
fail2: fail2:
prop_object_release(missing); prop_object_release(missing);
fail: fail:
@ -105,7 +96,7 @@ store_dependency(prop_dictionary_t origind, prop_dictionary_t depd,
uint32_t prio = 0; uint32_t prio = 0;
size_t len = 0, dirdepscnt = 0, indirdepscnt = 0; size_t len = 0, dirdepscnt = 0, indirdepscnt = 0;
const char *pkgname, *version, *reqbyname, *reqbyver; const char *pkgname, *version, *reqbyname, *reqbyver;
const char *repoloc, *binfile, *array_key, *originpkg, *short_desc; const char *repoloc, *binfile, *originpkg, *short_desc;
char *reqby, *pkgnver; char *reqby, *pkgnver;
int rv = 0; int rv = 0;
bool indirectdep = false; bool indirectdep = false;
@ -142,52 +133,35 @@ store_dependency(prop_dictionary_t origind, prop_dictionary_t depd,
} }
(void)snprintf(pkgnver, len, "%s-%s", pkgname, version); (void)snprintf(pkgnver, len, "%s-%s", pkgname, version);
/*
* Check if dependency is already installed to select the
* correct array object.
*/
if (xbps_check_is_installed_pkg(pkgnver) == 0) {
/*
* Dependency is already installed.
*/
array_key = "installed_deps";
dict = xbps_find_pkg_in_dict(chaindeps, array_key, pkgname);
if (dict)
goto out;
} else {
/* /*
* Required dependency is not installed. Check if it's * Required dependency is not installed. Check if it's
* already registered in the chain, and update some objects * already registered in the chain, and update some objects
* or add the object into array otherwise. * or add the object into array otherwise.
*/ */
array_key = "unsorted_deps"; prop_dictionary_get_cstring_nocopy(chaindeps, "origin", &originpkg);
prop_dictionary_get_cstring_nocopy(chaindeps, curdict = xbps_find_pkg_in_dict(chaindeps, "unsorted_deps", pkgname);
"origin", &originpkg);
curdict = xbps_find_pkg_in_dict(chaindeps, array_key, pkgname);
/* /*
* Update priority and required_by objects. * Update priority and required_by objects.
*/ */
if (curdict) { if (curdict) {
prop_dictionary_get_uint32(curdict, "priority", &prio); prop_dictionary_get_uint32(curdict, "priority", &prio);
prop_dictionary_set_uint32(curdict, "priority", ++prio); prop_dictionary_set_uint32(curdict, "priority", ++prio);
reqby_array = prop_dictionary_get(curdict, reqby_array = prop_dictionary_get(curdict, "required_by");
"required_by");
if (!xbps_find_string_in_array(reqby_array, reqby)) if (!xbps_find_string_in_array(reqby_array, reqby))
prop_array_add(reqby_array, reqbystr); prop_array_add(reqby_array, reqbystr);
goto out; goto out;
} }
if (strcmp(originpkg, reqbyname)) { if (strcmp(originpkg, reqbyname)) {
indirectdep = true; indirectdep = true;
prop_dictionary_get_uint32(chaindeps, prop_dictionary_get_uint32(chaindeps, "indirectdeps_count",
"indirectdeps_count", &indirdepscnt); &indirdepscnt);
prop_dictionary_set_uint32(chaindeps, prop_dictionary_set_uint32(chaindeps, "indirectdeps_count",
"indirectdeps_count", ++indirdepscnt); ++indirdepscnt);
} else { } else {
prop_dictionary_get_uint32(chaindeps, prop_dictionary_get_uint32(chaindeps, "directdeps_count",
"directdeps_count", &dirdepscnt); &dirdepscnt);
prop_dictionary_set_uint32(chaindeps, prop_dictionary_set_uint32(chaindeps, "directdeps_count",
"directdeps_count", ++dirdepscnt); ++dirdepscnt);
}
} }
/* /*
@ -199,7 +173,7 @@ store_dependency(prop_dictionary_t origind, prop_dictionary_t depd,
goto out; goto out;
} }
array = prop_dictionary_get(chaindeps, array_key); array = prop_dictionary_get(chaindeps, "unsorted_deps");
if (array == NULL) { if (array == NULL) {
prop_object_release(dict); prop_object_release(dict);
rv = ENOENT; rv = ENOENT;
@ -223,21 +197,18 @@ store_dependency(prop_dictionary_t origind, prop_dictionary_t depd,
} }
prop_array_add(reqby_array, reqbystr); prop_array_add(reqby_array, reqbystr);
prop_dictionary_set(dict, "required_by", reqby_array); prop_dictionary_set(dict, "required_by", reqby_array);
if (strcmp(array_key, "unsorted_deps") == 0) {
prop_dictionary_set_cstring(dict, "repository", repoloc); prop_dictionary_set_cstring(dict, "repository", repoloc);
prop_dictionary_set_cstring(dict, "filename", binfile); prop_dictionary_set_cstring(dict, "filename", binfile);
prop_dictionary_set_uint32(dict, "priority", prio); prop_dictionary_set_uint32(dict, "priority", prio);
prop_dictionary_set_cstring(dict, "short_desc", short_desc); prop_dictionary_set_cstring(dict, "short_desc", short_desc);
prop_dictionary_set_bool(dict, "indirect_dep", indirectdep); prop_dictionary_set_bool(dict, "indirect_dep", indirectdep);
}
/* /*
* Add the dictionary into the array. * Add the dictionary into the array.
*/ */
if (!xbps_add_obj_to_array(array, dict)) { if (!xbps_add_obj_to_array(array, dict)) {
prop_object_release(dict); prop_object_release(dict);
rv = EINVAL; rv = EINVAL;
goto out;
} }
out: out:
@ -253,7 +224,6 @@ add_missing_reqdep(const char *pkgname, const char *version)
{ {
prop_array_t array; prop_array_t array;
prop_dictionary_t depd; prop_dictionary_t depd;
size_t idx = 0;
assert(pkgname != NULL); assert(pkgname != NULL);
assert(version != NULL); assert(version != NULL);
@ -261,7 +231,7 @@ add_missing_reqdep(const char *pkgname, const char *version)
/* /*
* Adds a package into the missing deps array. * Adds a package into the missing deps array.
*/ */
if (check_missing_reqdep(pkgname, version, &idx) == 0) if (xbps_find_pkg_in_dict(chaindeps, "missing_deps", pkgname))
return EEXIST; return EEXIST;
array = prop_dictionary_get(chaindeps, "missing_deps"); array = prop_dictionary_get(chaindeps, "missing_deps");
@ -269,8 +239,8 @@ add_missing_reqdep(const char *pkgname, const char *version)
if (depd == NULL) if (depd == NULL)
return ENOMEM; return ENOMEM;
prop_dictionary_set_cstring_nocopy(depd, "pkgname", pkgname); prop_dictionary_set_cstring(depd, "pkgname", pkgname);
prop_dictionary_set_cstring_nocopy(depd, "version", version); prop_dictionary_set_cstring(depd, "version", version);
if (!xbps_add_obj_to_array(array, depd)) { if (!xbps_add_obj_to_array(array, depd)) {
prop_object_release(depd); prop_object_release(depd);
return EINVAL; return EINVAL;
@ -280,85 +250,250 @@ add_missing_reqdep(const char *pkgname, const char *version)
} }
static int static int
check_missing_reqdep(const char *pkgname, const char *version, remove_missing_reqdep(const char *pkgname)
size_t *idx)
{ {
prop_array_t array;
prop_object_t obj;
prop_object_iterator_t iter;
const char *missname, *missver;
size_t lidx = 0;
int rv = 0;
assert(pkgname != NULL);
assert(version != NULL);
assert(idx != NULL);
array = prop_dictionary_get(chaindeps, "missing_deps");
assert(array != NULL);
iter = prop_array_iterator(array);
if (iter == NULL)
return ENOMEM;
/*
* Finds the index of a package in the missing deps array.
*/
while ((obj = prop_object_iterator_next(iter)) != NULL) {
prop_dictionary_get_cstring_nocopy(obj, "pkgname", &missname);
prop_dictionary_get_cstring_nocopy(obj, "version", &missver);
if ((strcmp(pkgname, missname) == 0) &&
(strcmp(version, missver) == 0)) {
*idx = lidx;
goto out;
}
idx++;
}
rv = ENOENT;
out:
prop_object_iterator_release(iter);
return rv;
}
static void
remove_missing_reqdep(size_t *idx)
{
prop_array_t array;
array = prop_dictionary_get(chaindeps, "missing_deps");
assert(array != NULL);
prop_array_remove(array, *idx);
}
int
xbps_find_deps_in_pkg(prop_dictionary_t repo, prop_dictionary_t pkg)
{
prop_dictionary_t pkgdict;
prop_array_t array; prop_array_t array;
prop_object_t obj; prop_object_t obj;
prop_object_iterator_t iter; prop_object_iterator_t iter;
size_t idx = 0; size_t idx = 0;
const char *reqpkg, *reqvers; const char *curname;
char *pkgname; bool found = false;
int rv = 0;
static bool deps_dict;
array = prop_dictionary_get(pkg, "run_depends"); array = prop_dictionary_get(chaindeps, "missing_deps");
if (array == NULL || prop_array_count(array) == 0) assert(pkgname != NULL);
assert(version != NULL);
assert(array != NULL);
iter = prop_array_iterator(array);
if (iter == NULL)
return ENOMEM;
/*
* Finds the index of a package in the missing deps array.
*/
while ((obj = prop_object_iterator_next(iter)) != NULL) {
prop_dictionary_get_cstring_nocopy(obj, "pkgname", &curname);
if (strcmp(pkgname, curname) == 0) {
found = true;
break;
}
idx++;
}
prop_object_iterator_release(iter);
if (found) {
prop_array_remove(array, idx);
return 0;
}
return ENOENT;
}
int
xbps_find_deps_in_pkg(prop_dictionary_t pkg)
{
prop_array_t array, pkg_rdeps, missing_rdeps;
prop_dictionary_t repolistd, repod;
prop_object_t obj;
prop_object_iterator_t iter;
char *plist;
int rv = 0;
assert(pkg != NULL);
/*
* Get the dictionary with the list of registered repositories.
*/
plist = xbps_append_full_path(true, NULL, XBPS_REPOLIST);
if (plist == NULL)
return EINVAL;
/*
* Get the dictionary with the list of registered repositories.
*/
repolistd = prop_dictionary_internalize_from_file(plist);
if (repolistd == NULL) {
free(plist);
return EINVAL;
}
free(plist);
plist = NULL;
array = prop_dictionary_get(repolistd, "repository-list");
if (array == NULL) {
prop_object_release(repolistd);
return EINVAL;
}
iter = prop_array_iterator(array);
if (iter == NULL) {
prop_object_release(repolistd);
return ENOMEM;
}
pkg_rdeps = prop_dictionary_get(pkg, "run_depends");
/*
* Iterate over the repository pool and find out if we have
* all available binary packages.
*/
while ((obj = prop_object_iterator_next(iter)) != NULL) {
plist = xbps_append_full_path(false,
prop_string_cstring_nocopy(obj), XBPS_PKGINDEX);
if (plist == NULL) {
rv = EINVAL;
goto out;
}
repod = prop_dictionary_internalize_from_file(plist);
if (repod == NULL) {
free(plist);
rv = errno;
goto out;
}
free(plist);
/*
* This will find direct and indirect deps,
* if any of them is not there it will be added
* into the missing_deps array.
*/
rv = find_pkg_deps_from_repo(repod, pkg, pkg_rdeps);
if (rv != 0) {
if (rv == ENOENT) {
rv = 0;
prop_object_release(repod);
continue;
}
prop_object_release(repod);
break;
}
prop_object_release(repod);
}
missing_rdeps = prop_dictionary_get(chaindeps, "missing_deps");
if (prop_array_count(missing_rdeps) == 0)
goto out;
/*
* If there are missing deps, iterate one more time
* just in case that indirect deps weren't found.
*/
prop_object_iterator_reset(iter);
while ((obj = prop_object_iterator_next(iter)) != NULL) {
plist = xbps_append_full_path(false,
prop_string_cstring_nocopy(obj), XBPS_PKGINDEX);
if (plist == NULL) {
rv = EINVAL;
goto out;
}
repod = prop_dictionary_internalize_from_file(plist);
if (repod == NULL) {
free(plist);
rv = errno;
goto out;
}
free(plist);
rv = find_pkg_missing_deps_from_repo(repod, pkg);
if (rv != 0 && rv != ENOENT) {
prop_object_release(repod);
break;
}
prop_object_release(repod);
}
out:
prop_object_iterator_release(iter);
prop_object_release(repolistd);
return rv;
}
static int
find_pkg_missing_deps_from_repo(prop_dictionary_t repo, prop_dictionary_t pkg)
{
prop_array_t array;
prop_dictionary_t curpkgd;
prop_object_t obj;
prop_object_iterator_t iter;
const char *pkgname, *version;
int rv = 0;
assert(repo != NULL);
assert(pkg != NULL);
array = prop_dictionary_get(chaindeps, "missing_deps");
if (prop_array_count(array) == 0)
return 0; return 0;
iter = prop_array_iterator(array); iter = prop_array_iterator(array);
if (iter == NULL) if (iter == NULL)
return ENOMEM; return ENOMEM;
while ((obj = prop_object_iterator_next(iter)) != NULL) {
prop_dictionary_get_cstring_nocopy(obj, "pkgname", &pkgname);
prop_dictionary_get_cstring_nocopy(obj, "version", &version);
/*
* If required package is not in repo, add it into the
* missing deps array and pass to the next one.
*/
curpkgd = xbps_find_pkg_in_dict(repo, "packages", pkgname);
if (curpkgd == NULL) {
rv = add_missing_reqdep(pkgname, version);
if (rv != 0 && rv != EEXIST)
break;
else {
rv = ENOENT;
continue;
}
}
/*
* Package is on repo, add it into the dictionary.
*/
if ((rv = store_dependency(pkg, curpkgd, repo)) != 0)
break;
/*
* Remove package from missing now.
*/
rv = remove_missing_reqdep(pkgname);
if (rv != 0 && rv != ENOENT)
break;
prop_object_iterator_reset(iter);
}
prop_object_iterator_release(iter);
return rv;
}
static int
find_pkg_deps_from_repo(prop_dictionary_t repo, prop_dictionary_t pkg,
prop_array_t pkg_rdeps)
{
prop_dictionary_t curpkgd;
prop_array_t curpkg_rdeps;
prop_object_t obj;
prop_object_iterator_t iter;
const char *reqpkg, *reqvers;
char *pkgname;
int rv = 0;
static bool deps_dict;
/*
* Package doesn't have deps, check to be sure.
*/
if (pkg_rdeps == NULL || prop_array_count(pkg_rdeps) == 0)
return 0;
iter = prop_array_iterator(pkg_rdeps);
if (iter == NULL)
return ENOMEM;
/*
* Save the name of the origin package once.
*/
if (deps_dict == false) { if (deps_dict == false) {
deps_dict = true; deps_dict = true;
rv = create_deps_dictionary(); rv = create_deps_dictionary();
if (rv != 0) if (rv != 0) {
goto out; prop_object_iterator_release(iter);
return rv;
}
prop_dictionary_get_cstring_nocopy(pkg, "pkgname", &reqpkg); prop_dictionary_get_cstring_nocopy(pkg, "pkgname", &reqpkg);
prop_dictionary_set_cstring_nocopy(chaindeps, prop_dictionary_set_cstring_nocopy(chaindeps,
"origin", reqpkg); "origin", reqpkg);
@ -366,57 +501,76 @@ xbps_find_deps_in_pkg(prop_dictionary_t repo, prop_dictionary_t pkg)
/* /*
* Iterate over the list of required run dependencies for * Iterate over the list of required run dependencies for
* a package. * current package.
*/ */
while ((obj = prop_object_iterator_next(iter))) { while ((obj = prop_object_iterator_next(iter))) {
reqpkg = prop_string_cstring_nocopy(obj);
pkgname = xbps_get_pkg_name(reqpkg);
reqvers = xbps_get_pkg_version(reqpkg);
/*
* Check if required dep is satisfied and installed.
*/
if (xbps_check_is_installed_pkg(reqpkg) == 0) {
free(pkgname);
continue;
}
/*
* Check if package is already added in the
* array of unsorted deps.
*/
if (xbps_find_pkg_in_dict(chaindeps, "unsorted_deps",
pkgname)) {
free(pkgname);
continue;
}
/* /*
* If required package is not in repo, add it into the * If required package is not in repo, add it into the
* missing deps array and pass to the next one. * missing deps array and pass to the next one.
*/ */
reqpkg = prop_string_cstring_nocopy(obj); curpkgd = xbps_find_pkg_in_dict(repo, "packages", pkgname);
pkgname = xbps_get_pkg_name(reqpkg); if (curpkgd == NULL) {
reqvers = xbps_get_pkg_version(reqpkg);
pkgdict = xbps_find_pkg_in_dict(repo, "packages", pkgname);
if (pkgdict == NULL) {
rv = add_missing_reqdep(pkgname, reqvers); rv = add_missing_reqdep(pkgname, reqvers);
free(pkgname); free(pkgname);
if (rv != 0 && rv != EEXIST) if (rv != 0 && rv != EEXIST)
break; break;
else if (rv == EEXIST)
continue;
else { else {
rv = XBPS_PKG_ENOTINREPO; rv = ENOENT;
continue; continue;
} }
} }
/*
* Check if dependency wasn't found before.
*/
rv = check_missing_reqdep(pkgname, reqvers, &idx);
free(pkgname);
if (rv == 0) {
/*
* Found in current repository, remove it.
*/
remove_missing_reqdep(&idx);
} else if (rv != 0 && rv != ENOENT)
break;
/* /*
* Package is on repo, add it into the dictionary. * Package is on repo, add it into the dictionary.
*/ */
if ((rv = store_dependency(pkg, pkgdict, repo)) != 0) if ((rv = store_dependency(pkg, curpkgd, repo)) != 0) {
free(pkgname);
break; break;
}
/*
* Remove package from missing_deps now it's been found.
*/
rv = remove_missing_reqdep(pkgname);
if (rv != 0 && rv != ENOENT) {
free(pkgname);
break;
}
free(pkgname);
/*
* If package doesn't have rundeps, pass to the next one.
*/
curpkg_rdeps = prop_dictionary_get(curpkgd, "run_depends");
if (curpkg_rdeps == NULL)
continue;
/* /*
* Iterate on required pkg to find more deps. * Iterate on required pkg to find more deps.
*/ */
if (!xbps_find_deps_in_pkg(repo, pkgdict)) if (!find_pkg_deps_from_repo(repo, curpkgd, curpkg_rdeps))
continue; continue;
} }
out:
prop_object_iterator_release(iter); prop_object_iterator_release(iter);
return rv; return rv;
@ -425,28 +579,32 @@ out:
int int
xbps_install_pkg_deps(prop_dictionary_t pkg, const char *destdir) xbps_install_pkg_deps(prop_dictionary_t pkg, const char *destdir)
{ {
prop_array_t required; prop_array_t required, missing;
prop_object_t obj; prop_object_t obj;
prop_object_iterator_t iter; prop_object_iterator_t iter;
int rv = 0; int rv = 0;
assert(pkg != NULL); assert(pkg != NULL);
/*
* If there are missing deps, bail out.
*/
missing = prop_dictionary_get(chaindeps, "missing_deps");
if (prop_array_count(missing) > 0)
return ENOTSUP;
/* /*
* Sort the dependency chain into an array. * Sort the dependency chain into an array.
*/ */
if ((rv = xbps_sort_pkg_deps(chaindeps)) != 0) if ((rv = xbps_sort_pkg_deps(chaindeps)) != 0)
goto out; return rv;
required = prop_dictionary_get(chaindeps, "required_deps"); required = prop_dictionary_get(chaindeps, "required_deps");
if (required == NULL) if (required == NULL)
return 0; return 0;
iter = prop_array_iterator(required); iter = prop_array_iterator(required);
if (iter == NULL) { if (iter == NULL)
rv = ENOMEM; return ENOMEM;
goto out;
}
/* /*
* Install all required dependencies, previously sorted. * Install all required dependencies, previously sorted.
@ -458,6 +616,5 @@ xbps_install_pkg_deps(prop_dictionary_t pkg, const char *destdir)
} }
prop_object_iterator_release(iter); prop_object_iterator_release(iter);
out:
return rv; return rv;
} }

View file

@ -34,6 +34,13 @@
#include <xbps_api.h> #include <xbps_api.h>
struct cbargs {
const char *destdir;
const char *pkgname;
};
static int install_binpkg_repo_cb(prop_object_t, void *, bool *);
int int
xbps_install_binary_pkg_fini(prop_dictionary_t repo, prop_dictionary_t pkg, xbps_install_binary_pkg_fini(prop_dictionary_t repo, prop_dictionary_t pkg,
const char *destdir) const char *destdir)
@ -62,10 +69,7 @@ xbps_install_binary_pkg_fini(prop_dictionary_t repo, prop_dictionary_t pkg,
int int
xbps_install_binary_pkg(const char *pkgname, const char *destdir) xbps_install_binary_pkg(const char *pkgname, const char *destdir)
{ {
prop_array_t array; struct cbargs cb;
prop_dictionary_t repolistd, repod, pkgrd = NULL;
prop_object_t obj;
prop_object_iterator_t iter;
char *plist; char *plist;
int rv = 0; int rv = 0;
@ -83,39 +87,38 @@ xbps_install_binary_pkg(const char *pkgname, const char *destdir)
if (plist == NULL) if (plist == NULL)
return EINVAL; return EINVAL;
repolistd = prop_dictionary_internalize_from_file(plist); cb.pkgname = pkgname;
if (repolistd == NULL) { cb.destdir = destdir;
free(plist);
return EINVAL;
}
free(plist);
/* /*
* Iterate over the repository pool and find out if we have * Iterate over the repository pool and find out if we have
* all available binary packages. * all available binary packages.
*/ */
array = prop_dictionary_get(repolistd, "repository-list"); rv = xbps_callback_array_iter_in_repolist(plist,
assert(array != NULL); install_binpkg_repo_cb, (void *)&cb);
free(plist);
iter = prop_array_iterator(array); return rv;
if (iter == NULL) { }
prop_object_release(repolistd);
return ENOMEM; static int
} install_binpkg_repo_cb(prop_object_t obj, void *arg, bool *cbloop_done)
{
prop_dictionary_t repod, pkgrd;
struct cbargs *cb = arg;
const char *pkgname = cb->pkgname;
const char *destdir = cb->destdir;
char *plist;
int rv = 0;
while ((obj = prop_object_iterator_next(iter)) != NULL) {
plist = xbps_append_full_path(false, plist = xbps_append_full_path(false,
prop_string_cstring_nocopy(obj), XBPS_PKGINDEX); prop_string_cstring_nocopy(obj), XBPS_PKGINDEX);
if (plist == NULL) { if (plist == NULL)
rv = EINVAL; return EINVAL;
goto out;
}
repod = prop_dictionary_internalize_from_file(plist); repod = prop_dictionary_internalize_from_file(plist);
if (repod == NULL) { if (repod == NULL) {
free(plist); free(plist);
rv = errno; return errno;
goto out;
} }
free(plist); free(plist);
@ -126,8 +129,7 @@ xbps_install_binary_pkg(const char *pkgname, const char *destdir)
pkgrd = xbps_find_pkg_in_dict(repod, "packages", pkgname); pkgrd = xbps_find_pkg_in_dict(repod, "packages", pkgname);
if (pkgrd == NULL) { if (pkgrd == NULL) {
prop_object_release(repod); prop_object_release(repod);
rv = XBPS_PKG_ENOTINREPO; return 0;
continue;
} }
/* /*
@ -135,23 +137,21 @@ xbps_install_binary_pkg(const char *pkgname, const char *destdir)
*/ */
if (!xbps_pkg_has_rundeps(pkgrd)) { if (!xbps_pkg_has_rundeps(pkgrd)) {
/* pkg has no deps, just install it. */ /* pkg has no deps, just install it. */
rv = xbps_install_binary_pkg_fini(repod, pkgrd, rv = xbps_install_binary_pkg_fini(repod, pkgrd, destdir);
destdir);
prop_object_release(repod); prop_object_release(repod);
goto out; return rv;
} }
/* /*
* Construct the dependency chain for this package. If any * Construct the dependency chain for this package.
* dependency is not available, pass to the next repository.
*/ */
rv = xbps_find_deps_in_pkg(repod, pkgrd); rv = xbps_find_deps_in_pkg(pkgrd);
if (rv != 0) { if (rv != 0) {
prop_object_release(repod); prop_object_release(repod);
if (rv == XBPS_PKG_ENOTINREPO) if (rv == ENOENT)
continue; return 0;
goto out; return rv;
} }
/* /*
@ -159,17 +159,12 @@ xbps_install_binary_pkg(const char *pkgname, const char *destdir)
*/ */
rv = xbps_install_pkg_deps(pkgrd, destdir); rv = xbps_install_pkg_deps(pkgrd, destdir);
if (rv == 0) { if (rv == 0) {
rv = xbps_install_binary_pkg_fini(repod, pkgrd, rv = xbps_install_binary_pkg_fini(repod, pkgrd, destdir);
destdir);
}
prop_object_release(repod); prop_object_release(repod);
break; if (rv == 0)
*cbloop_done = true;
} }
out:
prop_object_release(repolistd);
prop_object_iterator_release(iter);
return rv; return rv;
} }

View file

@ -65,29 +65,57 @@ xbps_add_obj_to_array(prop_array_t array, prop_object_t obj)
return true; return true;
} }
int
xbps_callback_array_iter_in_repolist(const char *plist,
int (*fn)(prop_object_t, void *, bool *),
void *arg)
{
prop_dictionary_t repolistd;
int rv = 0;
assert(plist != NULL);
assert(fn != NULL);
/*
* Get the dictionary with the list of registered repositories.
*/
repolistd = prop_dictionary_internalize_from_file(plist);
if (repolistd == NULL)
return EINVAL;
/*
* Iterate over the repository pool and run the associated
* callback function. The loop is stopped when the bool
* argument is true or the cb returns non 0.
*/
rv = xbps_callback_array_iter_in_dict(repolistd, "repository-list",
fn, arg);
prop_object_release(repolistd);
return rv;
}
int int
xbps_callback_array_iter_in_dict(prop_dictionary_t dict, const char *key, xbps_callback_array_iter_in_dict(prop_dictionary_t dict, const char *key,
int (*func)(prop_object_t, void *, bool *), int (*fn)(prop_object_t, void *, bool *),
void *arg) void *arg)
{ {
prop_object_iterator_t iter; prop_object_iterator_t iter;
prop_object_t obj; prop_object_t obj;
int rv = 0; int rv = 0;
bool run, cbloop_done; bool cbloop_done = false;
assert(dict != NULL); assert(dict != NULL);
assert(key != NULL); assert(key != NULL);
assert(func != NULL); assert(fn != NULL);
run = cbloop_done = false;
iter = xbps_get_array_iter_from_dict(dict, key); iter = xbps_get_array_iter_from_dict(dict, key);
if (iter == NULL) if (iter == NULL)
return EINVAL; return EINVAL;
while ((obj = prop_object_iterator_next(iter))) { while ((obj = prop_object_iterator_next(iter))) {
rv = (*func)(obj, arg, &cbloop_done); rv = (*fn)(obj, arg, &cbloop_done);
if (rv == 0 && cbloop_done) if (rv != 0 || cbloop_done)
break; break;
} }

View file

@ -35,10 +35,9 @@
struct sorted_dependency { struct sorted_dependency {
TAILQ_ENTRY(sorted_dependency) chain; TAILQ_ENTRY(sorted_dependency) chain;
prop_dictionary_t dict; prop_dictionary_t dict;
prop_array_t reqby;
size_t idx; size_t idx;
ssize_t newidx; size_t prio;
bool unsorted;
bool reorg;
}; };
static TAILQ_HEAD(sdep_head, sorted_dependency) sdep_list = static TAILQ_HEAD(sdep_head, sorted_dependency) sdep_list =
@ -91,6 +90,31 @@ find_pkgdict_with_highest_prio(prop_array_t array, uint32_t *maxprio,
return curidx; return curidx;
} }
static struct sorted_dependency *
find_sorteddep_with_highest_prio(void)
{
struct sorted_dependency *sdep;
size_t maxprio = 0;
size_t curidx = 0, idx = 0;
TAILQ_FOREACH(sdep, &sdep_list, chain) {
if (maxprio <= sdep->prio) {
curidx = idx;
maxprio = sdep->prio;
}
idx++;
}
idx = 0;
TAILQ_FOREACH(sdep, &sdep_list, chain) {
if (idx == curidx)
break;
idx++;
}
return sdep;
}
static struct sorted_dependency * static struct sorted_dependency *
find_sorteddep_by_name(const char *pkgname) find_sorteddep_by_name(const char *pkgname)
{ {
@ -110,7 +134,7 @@ find_sorteddep_by_name(const char *pkgname)
int int
xbps_sort_pkg_deps(prop_dictionary_t chaindeps) xbps_sort_pkg_deps(prop_dictionary_t chaindeps)
{ {
prop_array_t installed, sorted, unsorted, rundeps_array; prop_array_t sorted, unsorted, rundeps_array, reqby;
prop_dictionary_t dict; prop_dictionary_t dict;
prop_object_t obj; prop_object_t obj;
prop_object_iterator_t iter; prop_object_iterator_t iter;
@ -130,10 +154,8 @@ xbps_sort_pkg_deps(prop_dictionary_t chaindeps)
/* /*
* All required deps are satisfied (already installed). * All required deps are satisfied (already installed).
*/ */
installed = prop_dictionary_get(chaindeps, "installed_deps");
unsorted = prop_dictionary_get(chaindeps, "unsorted_deps"); unsorted = prop_dictionary_get(chaindeps, "unsorted_deps");
if (prop_array_count(unsorted) == 0 && if (prop_array_count(unsorted) == 0) {
prop_array_count(installed) > 0) {
prop_object_release(sorted); prop_object_release(sorted);
return 0; return 0;
} }
@ -165,7 +187,10 @@ xbps_sort_pkg_deps(prop_dictionary_t chaindeps)
} }
sdep->dict = prop_dictionary_copy(dict); sdep->dict = prop_dictionary_copy(dict);
sdep->idx = cnt; sdep->idx = cnt;
sdep->newidx = -1; 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);
TAILQ_INSERT_TAIL(&sdep_list, sdep, chain); TAILQ_INSERT_TAIL(&sdep_list, sdep, chain);
prop_array_remove(unsorted, curidx); prop_array_remove(unsorted, curidx);
maxprio = 0; maxprio = 0;
@ -195,7 +220,10 @@ xbps_sort_pkg_deps(prop_dictionary_t chaindeps)
} }
sdep->dict = prop_dictionary_copy(dict); sdep->dict = prop_dictionary_copy(dict);
sdep->idx = cnt + indirdepscnt; sdep->idx = cnt + indirdepscnt;
sdep->newidx = -1; 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);
TAILQ_INSERT_TAIL(&sdep_list, sdep, chain); TAILQ_INSERT_TAIL(&sdep_list, sdep, chain);
prop_array_remove(unsorted, curidx); prop_array_remove(unsorted, curidx);
maxprio = 0; maxprio = 0;
@ -203,8 +231,8 @@ xbps_sort_pkg_deps(prop_dictionary_t chaindeps)
} }
/* /*
* Pass 3: update new index position by looking at run_depends and * Pass 3: increase priority of dependencies any time
* its current index position. * a package requires them.
*/ */
TAILQ_FOREACH(sdep, &sdep_list, chain) { TAILQ_FOREACH(sdep, &sdep_list, chain) {
prop_dictionary_get_cstring_nocopy(sdep->dict, prop_dictionary_get_cstring_nocopy(sdep->dict,
@ -218,7 +246,6 @@ xbps_sort_pkg_deps(prop_dictionary_t chaindeps)
rv = ENOMEM; rv = ENOMEM;
goto out; goto out;
} }
curidx = sdep->idx;
while ((obj = prop_object_iterator_next(iter)) != NULL) { while ((obj = prop_object_iterator_next(iter)) != NULL) {
rundep = prop_string_cstring_nocopy(obj); rundep = prop_string_cstring_nocopy(obj);
@ -238,58 +265,46 @@ xbps_sort_pkg_deps(prop_dictionary_t chaindeps)
sdep2 = find_sorteddep_by_name(pkgname); sdep2 = find_sorteddep_by_name(pkgname);
free(pkgname); free(pkgname);
/* sdep2->prio++;
* If required dependency is before current package,
* pass to the next one.
*/
if (curidx > sdep2->idx)
continue;
/*
* Update index position for the two objects.
*/
if (!sdep2->unsorted) {
sdep2->unsorted = true;
sdep2->newidx = curidx;
sdep->newidx = curidx + 1;
}
} }
prop_object_iterator_release(iter); prop_object_iterator_release(iter);
} }
prop_dictionary_remove(chaindeps, "unsorted_deps"); prop_dictionary_remove(chaindeps, "unsorted_deps");
/* /*
* Pass 4: copy dictionaries into the final array with the * Pass 4: increase priority of a package, by looking at
* correct index position for all dependencies. * its required_by array member's priority.
*/ */
TAILQ_FOREACH(sdep, &sdep_list, chain) { TAILQ_FOREACH(sdep, &sdep_list, chain) {
if (sdep->reorg) iter = prop_array_iterator(sdep->reqby);
if (iter == NULL)
continue; continue;
if (sdep->newidx != -1) { while ((obj = prop_object_iterator_next(iter))) {
TAILQ_FOREACH(sdep2, &sdep_list, chain) { rundep = prop_string_cstring_nocopy(obj);
if (sdep2->unsorted) { pkgname = xbps_get_pkg_name(rundep);
if (!prop_array_set(sorted, sdep2 = find_sorteddep_by_name(pkgname);
sdep2->newidx, sdep2->dict)) { if (sdep2 == NULL) {
rv = errno; free(pkgname);
goto out; continue;
} }
sdep2->newidx = -1; free(pkgname);
sdep2->unsorted = false; sdep->prio += sdep2->prio + 1;
sdep2->reorg = true;
break;
}
}
if (!prop_array_set(sorted, sdep->newidx, sdep->dict)) {
rv = errno;
goto out;
}
sdep->newidx = -1;
} else {
if (!prop_array_add(sorted, sdep->dict)) {
rv = errno;
goto out;
} }
prop_object_iterator_release(iter);
} }
/*
* Pass 5: copy dictionaries into the final array with the
* 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);
TAILQ_REMOVE(&sdep_list, sdep, chain);
prop_object_release(sdep->dict);
prop_object_release(sdep->reqby);
free(sdep);
} }
/* /*
@ -312,6 +327,7 @@ out:
while ((sdep = TAILQ_FIRST(&sdep_list)) != NULL) { while ((sdep = TAILQ_FIRST(&sdep_list)) != NULL) {
TAILQ_REMOVE(&sdep_list, sdep, chain); TAILQ_REMOVE(&sdep_list, sdep, chain);
prop_object_release(sdep->dict); prop_object_release(sdep->dict);
prop_object_release(sdep->reqby);
free(sdep); free(sdep);
} }