New dependency/install/unpack WIP code.

This implementation will be faster and resolv the dependency chain
correctly taking into account indirect/direct deps and priority.

--HG--
extra : convert_revision : cc4ec186f06f944fa8825b176344c4d612658f85
This commit is contained in:
Juan RP
2009-02-07 13:27:24 +01:00
parent 3f72a92c98
commit 61f0b09698
12 changed files with 712 additions and 512 deletions

View File

@@ -32,137 +32,301 @@
#include <xbps_api.h>
struct pkg_dependency {
SIMPLEQ_ENTRY(pkg_dependency) deps;
prop_dictionary_t repo;
uint64_t priority;
uint64_t reqcount;
const char *namever;
char *name;
};
static int add_missing_reqdep(const char *, const char *);
static int check_missing_reqdep(const char *, const char *, size_t *);
static void remove_missing_reqdep(size_t *);
static SIMPLEQ_HEAD(, pkg_dependency) pkg_deps =
SIMPLEQ_HEAD_INITIALIZER(pkg_deps);
static prop_dictionary_t chaindeps;
static void xbps_destroy_dependency(struct pkg_dependency *);
void
xbps_clean_pkg_depslist(void)
/*
* Creates the dictionary to store the dependency chain.
*/
static int
create_deps_dictionary(void)
{
struct pkg_dependency *dep;
prop_array_t installed, direct, indirect, missing;
int rv = 0;
while (!SIMPLEQ_EMPTY(&pkg_deps)) {
dep = SIMPLEQ_FIRST(&pkg_deps);
SIMPLEQ_REMOVE_HEAD(&pkg_deps, deps);
xbps_destroy_dependency(dep);
}
}
chaindeps = prop_dictionary_create();
if (chaindeps == NULL)
return ENOMEM;
static struct pkg_dependency *
xbps_get_dependency(void)
{
struct pkg_dependency *dep = NULL;
uint64_t reqcount = 0 , priority = 0;
/* Set max reqcount and priority found */
SIMPLEQ_FOREACH(dep, &pkg_deps, deps) {
if (dep->reqcount > reqcount)
reqcount = dep->reqcount;
if (dep->priority > priority)
priority = dep->priority;
missing = prop_array_create();
if (missing == NULL) {
rv = ENOMEM;
goto fail;
}
/* Pass 1: return pkgs with highest reqcount and priority */
SIMPLEQ_FOREACH(dep, &pkg_deps, deps)
if (dep->reqcount == reqcount && dep->priority == priority)
goto out;
/* Pass 2: return pkgs with highest priority */
SIMPLEQ_FOREACH(dep, &pkg_deps, deps)
if (dep->priority == priority)
goto out;
/* Pass 3: return pkgs with highest reqcount */
SIMPLEQ_FOREACH(dep, &pkg_deps, deps)
if (dep->reqcount == reqcount)
goto out;
/* Last pass: return pkgs with default reqcount/priority */
dep = SIMPLEQ_FIRST(&pkg_deps);
out:
if (dep != NULL) {
/*
* Remove dep from queue, it will be freed later with
* xbps_destroy_dependency().
*/
SIMPLEQ_REMOVE(&pkg_deps, dep, pkg_dependency, deps);
installed = prop_array_create();
if (installed == NULL) {
rv = ENOMEM;
goto fail2;
}
return dep;
}
static void
xbps_destroy_dependency(struct pkg_dependency *dep)
{
assert(dep != NULL);
free(dep->name);
prop_object_release(dep->repo);
free(dep);
}
void
xbps_add_pkg_dependency(const char *pkg, uint64_t prio, prop_dictionary_t repo)
{
struct pkg_dependency *dep;
size_t len = 0;
char *pkgname;
assert(repo != NULL);
assert(pkg != NULL);
assert(pkgd != NULL);
pkgname = xbps_get_pkg_name(pkg);
SIMPLEQ_FOREACH(dep, &pkg_deps, deps) {
if (strcmp(dep->name, pkgname) == 0) {
dep->reqcount++;
free(pkgname);
return;
}
direct = prop_array_create();
if (direct == NULL) {
rv = ENOMEM;
goto fail3;
}
dep = NULL;
dep = malloc(sizeof(*dep));
assert(dep != NULL);
indirect = prop_array_create();
if (indirect == NULL) {
rv = ENOMEM;
goto fail4;
}
len = strlen(pkgname) + 1;
dep->name = malloc(len);
assert(dep->name != NULL);
if (!xbps_add_obj_to_dict(chaindeps, missing, "missing_deps")) {
rv = EINVAL;
goto fail5;
}
if (!xbps_add_obj_to_dict(chaindeps, installed, "installed_deps")) {
rv = EINVAL;
goto fail5;
}
if (!xbps_add_obj_to_dict(chaindeps, direct, "direct_deps")) {
rv = EINVAL;
goto fail5;
}
if (!xbps_add_obj_to_dict(chaindeps, indirect, "indirect_deps")) {
rv = EINVAL;
goto fail5;
}
return rv;
memcpy(dep->name, pkgname, len - 1);
dep->name[len - 1] = '\0';
free(pkgname);
dep->repo = prop_dictionary_copy(repo);
dep->namever = pkg;
dep->priority = prio;
dep->reqcount = 0;
fail5:
prop_object_release(indirect);
fail4:
prop_object_release(direct);
fail3:
prop_object_release(installed);
fail2:
prop_object_release(missing);
fail:
prop_object_release(chaindeps);
SIMPLEQ_INSERT_TAIL(&pkg_deps, dep, deps);
return rv;
}
static int
find_deps_in_pkg(prop_dictionary_t repo, prop_dictionary_t pkg)
store_dependency(prop_dictionary_t origind, prop_dictionary_t depd,
prop_dictionary_t repod)
{
prop_dictionary_t dict, curpkgdir, curpkgindir;
prop_array_t array;
uint32_t prio = 0;
size_t len = 0;
const char *pkgname, *version, *reqbyname, *reqbyver;
const char *repoloc, *binfile, *array_key = NULL, *originpkg;
char *reqby;
int rv = 0;
assert(origind != NULL);
assert(depd != NULL);
assert(repod != NULL);
/*
* Get some info about dependencies and current repository.
*/
prop_dictionary_get_cstring_nocopy(depd, "pkgname", &pkgname);
prop_dictionary_get_cstring_nocopy(depd, "version", &version);
prop_dictionary_get_cstring_nocopy(depd, "filename", &binfile);
prop_dictionary_get_uint32(depd, "priority", &prio);
prop_dictionary_get_cstring_nocopy(origind, "pkgname", &reqbyname);
prop_dictionary_get_cstring_nocopy(origind, "version", &reqbyver);
prop_dictionary_get_cstring_nocopy(repod, "location-local", &repoloc);
len = strlen(reqbyname) + strlen(reqbyver) + 2;
reqby = malloc(len + 1);
if (reqby == NULL)
return ENOMEM;
(void)snprintf(reqby, len, "%s-%s", reqbyname, reqbyver);
/*
* Check if dependency is already installed to select the
* correct array object.
*/
if (xbps_check_is_installed_pkgname(pkgname)) {
/*
* 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
* already registered in the chain, and update priority
* or add the object into array otherwise.
*/
prop_dictionary_get_cstring_nocopy(chaindeps, "origin",
&originpkg);
curpkgdir = xbps_find_pkg_in_dict(chaindeps,
"direct_deps", pkgname);
curpkgindir = xbps_find_pkg_in_dict(chaindeps,
"indirect_deps", pkgname);
if (strcmp(originpkg, reqbyname) == 0)
array_key = "direct_deps";
else
array_key = "indirect_deps";
if (curpkgdir && curpkgindir) {
goto out;
} else if (curpkgdir) {
/*
* Update the priority.
*/
prop_dictionary_get_uint32(curpkgdir,
"priority", &prio);
prop_dictionary_set_uint32(curpkgdir,
"priority", ++prio);
goto out;
} else if (curpkgindir) {
prop_dictionary_get_uint32(curpkgindir,
"priority", &prio);
prop_dictionary_set_uint32(curpkgindir,
"priority", ++prio);
goto out;
}
}
/*
* Create package dep's dictionary and array.
*/
dict = prop_dictionary_create();
if (dict == NULL) {
rv = ENOMEM;
goto out;
}
array = prop_dictionary_get(chaindeps, array_key);
if (array == NULL) {
prop_object_release(dict);
rv = ENOENT;
goto out;
}
/*
* Add required objects into package dep's dictionary.
*/
prop_dictionary_set_cstring(dict, "pkgname", pkgname);
prop_dictionary_set_cstring(dict, "version", version);
prop_dictionary_set_cstring(dict, "requiredby", reqby);
if ((strcmp(array_key, "direct_deps") == 0) ||
(strcmp(array_key, "indirect_deps") == 0)) {
prop_dictionary_set_cstring(dict, "repository", repoloc);
prop_dictionary_set_cstring(dict, "filename", binfile);
prop_dictionary_set_uint32(dict, "priority", prio);
}
/*
* Add the dictionary into the array.
*/
if (!xbps_add_obj_to_array(array, dict)) {
prop_object_release(dict);
rv = EINVAL;
goto out;
}
out:
free(reqby);
return rv;
}
static int
add_missing_reqdep(const char *pkgname, const char *version)
{
prop_array_t array;
prop_dictionary_t depd;
size_t idx = 0;
assert(pkgname != NULL);
assert(version != NULL);
if (check_missing_reqdep(pkgname, version, &idx) == 0)
return EEXIST;
array = prop_dictionary_get(chaindeps, "missing_deps");
depd = prop_dictionary_create();
if (depd == NULL)
return ENOMEM;
prop_dictionary_set_cstring_nocopy(depd, "pkgname", pkgname);
prop_dictionary_set_cstring_nocopy(depd, "version", version);
if (!xbps_add_obj_to_array(array, depd)) {
prop_object_release(depd);
return EINVAL;
}
return 0;
}
static int
check_missing_reqdep(const char *pkgname, const char *version,
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;
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_number_t prio_num;
prop_object_t obj;
prop_object_iterator_t iter = NULL;
uint64_t prio = 0;
const char *reqpkg;
prop_object_iterator_t iter;
size_t idx = 0;
const char *reqpkg, *reqvers;
char *pkgname;
int rv = 0;
static bool deps_dict;
array = prop_dictionary_get(pkg, "run_depends");
if (array == NULL || prop_array_count(array) == 0)
@@ -170,7 +334,18 @@ find_deps_in_pkg(prop_dictionary_t repo, prop_dictionary_t pkg)
iter = prop_array_iterator(array);
if (iter == NULL)
return -1;
return ENOMEM;
if (deps_dict == false) {
deps_dict = true;
rv = create_deps_dictionary();
if (rv != 0)
goto out;
prop_dictionary_get_cstring_nocopy(pkg, "pkgname", &reqpkg);
prop_dictionary_set_cstring_nocopy(chaindeps,
"origin", reqpkg);
}
/*
* Iterate over the list of required run dependencies for
@@ -178,114 +353,105 @@ find_deps_in_pkg(prop_dictionary_t repo, prop_dictionary_t pkg)
*/
while ((obj = prop_object_iterator_next(iter))) {
/*
* If required package is not in repo, just pass to the
* next one.
* If required package is not in repo, add it into the
* missing deps array and 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);
reqvers = xbps_get_pkg_version(reqpkg);
pkgdict = xbps_find_pkg_in_dict(repo, "packages", pkgname);
if (pkgdict == NULL) {
rv = add_missing_reqdep(pkgname, reqvers);
free(pkgname);
continue;
if (rv != 0 && rv != EEXIST)
break;
else if (rv == EEXIST)
continue;
else {
rv = XBPS_PKG_ENOTINREPO;
continue;
}
}
/*
* Package is on repo, add it into the list.
* Check if dependency wasn't found before.
*/
prio_num = prop_dictionary_get(pkgdict, "priority");
prio = prop_number_unsigned_integer_value(prio_num);
xbps_add_pkg_dependency(reqpkg, prio, repo);
rv = check_missing_reqdep(pkgname, reqvers, &idx);
free(pkgname);
if (rv == 0) {
/*
* Found in current repository, remove it.
*/
remove_missing_reqdep(&idx);
/* Iterate on required pkg to find more deps */
if (xbps_pkg_has_rundeps(pkgdict)) {
/* more deps? */
if (!find_deps_in_pkg(repo, pkgdict))
continue;
}
} else if (rv != 0 && rv != ENOENT)
break;
/*
* Package is on repo, add it into the dictionary.
*/
if ((rv = store_dependency(pkg, pkgdict, repo)) != 0)
break;
/*
* Iterate on required pkg to find more deps.
*/
if (!xbps_find_deps_in_pkg(repo, pkgdict))
continue;
}
out:
prop_object_iterator_release(iter);
return 0;
return rv;
}
int
xbps_install_pkg_deps(prop_dictionary_t pkg)
{
prop_dictionary_t repolistd, repod, pkgd;
prop_array_t array;
prop_object_iterator_t iter;
prop_array_t array, installed, direct, indirect;
prop_dictionary_t dict;
prop_object_t obj;
struct pkg_dependency *dep;
size_t required_deps = 0, deps_found = 0;
const char *reqpkg, *version, *pkgname, *desc;
char *plist, *namestr;
prop_object_iterator_t iter;
uint32_t maxprio = 0, prio = 0;
size_t curidx = 0, idx = 0;
const char *array_key, *reqby, *curname;
int rv = 0;
bool dep_found = false;
assert(pkg != NULL);
plist = xbps_append_full_path(true, NULL, XBPS_REPOLIST);
if (plist == NULL)
return EINVAL;
repolistd = prop_dictionary_internalize_from_file(plist);
if (repolistd == NULL) {
free(plist);
return EINVAL;
}
free(plist);
array = prop_dictionary_get(repolistd, "repository-list");
assert(array != NULL);
iter = prop_array_iterator(array);
if (iter == NULL) {
free(plist);
prop_object_release(repolistd);
return ENOMEM;
}
/*
* Install required dependencies of a package.
* The order for installation will be:
*
* - Indirect deps with high->low prio.
* - Direct deps with high->low prio.
*/
/*
* Iterate over the repository list and find out if we have
* all required dependencies.
* First case: all deps are satisfied.
*/
while ((obj = prop_object_iterator_next(iter)) != NULL) {
plist = xbps_append_full_path(false,
prop_string_cstring_nocopy(obj), XBPS_PKGINDEX);
if (plist == NULL) {
xbps_clean_pkg_depslist();
prop_object_iterator_release(iter);
rv = EINVAL;
goto out;
}
repod = prop_dictionary_internalize_from_file(plist);
if (repod == NULL) {
free(plist);
prop_object_iterator_release(iter);
rv = errno;
goto out;
}
free(plist);
rv = find_deps_in_pkg(repod, pkg);
if (rv == -1) {
prop_object_release(repod);
prop_object_iterator_release(iter);
goto out;
}
prop_object_release(repod);
}
prop_object_iterator_release(iter);
installed = prop_dictionary_get(chaindeps, "installed_deps");
direct = prop_dictionary_get(chaindeps, "direct_deps");
indirect = prop_dictionary_get(chaindeps, "indirect_deps");
if (prop_array_count(direct) == 0 && prop_array_count(indirect) == 0 &&
prop_array_count(installed) > 0)
return 0;
/*
* Find out if all required dependencies and binary packages
* from repositories are there.
* Second case: only direct deps are required.
*/
array = prop_dictionary_get(pkg, "run_depends");
if (prop_array_count(indirect) == 0 && prop_array_count(direct) > 0)
array_key = "direct_deps";
else
array_key = "indirect_deps";
again:
array = prop_dictionary_get(chaindeps, array_key);
if (array == NULL || prop_array_count(array) == 0) {
rv = EINVAL;
goto out;
}
iter = prop_array_iterator(array);
if (iter == NULL) {
rv = ENOMEM;
@@ -293,81 +459,42 @@ xbps_install_pkg_deps(prop_dictionary_t pkg)
}
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);
version = xbps_get_pkg_version(reqpkg);
SIMPLEQ_FOREACH(dep, &pkg_deps, deps) {
if (strcmp(dep->name, namestr) == 0) {
deps_found++;
dep_found = true;
break;
}
prop_dictionary_get_uint32(obj, "priority", &prio);
if (maxprio < prio) {
curidx = idx;
maxprio = prio;
}
if (dep_found == false) {
printf("Cannot find %s >= %s in repository list.\n",
namestr, version);
(void)fflush(stdout);
}
free(namestr);
idx++;
}
prop_object_iterator_release(iter);
if (required_deps != deps_found) {
rv = XBPS_PKG_ENOTINREPO;
dict = prop_array_get(array, curidx);
if (dict == NULL) {
rv = ENOENT;
goto out;
}
#ifdef XBPS_DEBUG_DEPS
while ((dep = xbps_get_dependency()) != NULL) {
printf("%s reqcount %ju priority %ju\n", dep->name,
dep->reqcount, dep->priority);
xbps_destroy_dependency(dep);
}
exit(0);
#endif
prop_dictionary_get_cstring_nocopy(dict, "pkgname", &curname);
prop_dictionary_get_cstring_nocopy(dict, "requiredby", &reqby);
printf("[%s] %s requiredby %s prio %u\n",
strcmp(array_key, "indirect_deps") == 0 ? "INDIRECT" : "DIRECT",
curname, reqby, maxprio);
/*
* Iterate over the list of dependencies and install them.
* The list is sorted by three rules, see xbps_get_dependency().
*/
while ((dep = xbps_get_dependency()) != NULL) {
pkgd = xbps_find_pkg_in_dict(dep->repo, dep->name);
if (pkgd == NULL) {
rv = EINVAL;
break;
}
rv = xbps_check_is_installed_pkg(dep->namever);
if (rv == -1) {
rv = EINVAL;
break;
} else if (rv == 0) {
/* Dependency is already installed. */
continue;
}
prop_dictionary_get_cstring_nocopy(pkgd, "pkgname", &pkgname);
prop_dictionary_get_cstring_nocopy(pkgd, "version", &version);
prop_dictionary_get_cstring_nocopy(pkgd, "short_desc", &desc);
rv = xbps_unpack_binary_pkg(dep->repo, pkgd,
xbps_unpack_archive_cb);
if (rv != 0)
break;
rv = xbps_register_pkg(pkgname, version, desc);
if (rv != 0)
break;
xbps_destroy_dependency(dep);
prop_array_remove(array, curidx);
if (prop_array_count(array) > 0) {
prio = maxprio = 0;
curidx = idx = 0;
goto again;
} else {
prio = maxprio = 0;
curidx = idx = 0;
array_key = "direct_deps";
goto again;
}
out:
prop_object_release(repolistd);
xbps_clean_pkg_depslist();
prop_object_release(chaindeps);
exit(0);
return rv;
}