Remove knowledge from the library to install packages.

The library is only responsible now to find all binary packages
from repositories, sort them and create a dictionary with all
details. The frontend (xbps-bin) provides support for this,
and this allows for more flexibility and removes some stuff
that didn't belong in the library.

While here implemented support to show total download/installed
size for all packages that are going to be installed.

Next step will be to implement user interactivity before
installing/removing/updating packages.

--HG--
extra : convert_revision : 858e9a89bc6a60b348435f2416a8af3ebb6ea5c9
This commit is contained in:
Juan RP
2009-04-09 02:29:15 +02:00
parent acd07f61e3
commit f80e1753a8
13 changed files with 580 additions and 482 deletions

View File

@@ -32,70 +32,21 @@
#include <xbps_api.h>
static int add_missing_reqdep(const char *, const char *);
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 bool deps_dict;
/*
* Creates the dictionary to store the dependency chain.
*/
static int
create_deps_dictionary(void)
{
prop_array_t unsorted, missing;
int rv = 0;
chaindeps = prop_dictionary_create();
if (chaindeps == NULL)
return ENOMEM;
missing = prop_array_create();
if (missing == NULL) {
rv = ENOMEM;
goto fail;
}
unsorted = prop_array_create();
if (unsorted == NULL) {
rv = ENOMEM;
goto fail2;
}
if (!xbps_add_obj_to_dict(chaindeps, missing, "missing_deps")) {
rv = EINVAL;
goto fail3;
}
if (!xbps_add_obj_to_dict(chaindeps, unsorted, "unsorted_deps")) {
rv = EINVAL;
goto fail3;
}
return rv;
fail3:
prop_object_release(unsorted);
fail2:
prop_object_release(missing);
fail:
prop_object_release(chaindeps);
return rv;
}
static int add_missing_reqdep(prop_dictionary_t, const char *,
const char *);
static int find_repo_deps(prop_dictionary_t, prop_dictionary_t,
prop_dictionary_t, prop_array_t);
static int find_repo_missing_deps(prop_dictionary_t, prop_dictionary_t,
prop_dictionary_t);
static int
store_dependency(prop_dictionary_t origind, prop_dictionary_t depd,
prop_dictionary_t repod)
store_dependency(prop_dictionary_t master, prop_dictionary_t origind,
prop_dictionary_t depd, prop_dictionary_t repod)
{
prop_dictionary_t dict;
prop_array_t array, rundeps_array;
prop_array_t array;
size_t dirdepscnt = 0, indirdepscnt = 0;
const char *pkgname, *version, *reqbyname, *arch;
const char *repoloc, *binfile, *originpkg;
const char *short_desc, *sha256;
const char *reqbyname, *repoloc, *originpkg;
bool indirectdep = false;
assert(origind != NULL);
@@ -105,12 +56,6 @@ store_dependency(prop_dictionary_t origind, prop_dictionary_t depd,
/*
* 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_cstring_nocopy(depd, "short_desc", &short_desc);
prop_dictionary_get_cstring_nocopy(depd, "architecture", &arch);
prop_dictionary_get_cstring_nocopy(depd, "filename-sha256", &sha256);
prop_dictionary_get_cstring_nocopy(origind, "pkgname", &reqbyname);
prop_dictionary_get_cstring_nocopy(repod, "location-local", &repoloc);
@@ -119,48 +64,41 @@ store_dependency(prop_dictionary_t origind, prop_dictionary_t depd,
* already registered in the chain, and update some objects
* or add the object into array otherwise.
*/
prop_dictionary_get_cstring_nocopy(chaindeps, "origin", &originpkg);
prop_dictionary_get_cstring_nocopy(master, "origin", &originpkg);
if (strcmp(originpkg, reqbyname)) {
indirectdep = true;
prop_dictionary_get_uint32(chaindeps, "indirectdeps_count",
prop_dictionary_get_uint32(master, "indirectdeps_count",
&indirdepscnt);
prop_dictionary_set_uint32(chaindeps, "indirectdeps_count",
prop_dictionary_set_uint32(master, "indirectdeps_count",
++indirdepscnt);
} else {
prop_dictionary_get_uint32(chaindeps, "directdeps_count",
prop_dictionary_get_uint32(master, "directdeps_count",
&dirdepscnt);
prop_dictionary_set_uint32(chaindeps, "directdeps_count",
prop_dictionary_set_uint32(master, "directdeps_count",
++dirdepscnt);
}
/*
* Create package dep's dictionary and array.
*/
dict = prop_dictionary_create();
dict = prop_dictionary_copy(depd);
if (dict == NULL)
return errno;
array = prop_dictionary_get(chaindeps, "unsorted_deps");
array = prop_dictionary_get(master, "unsorted_deps");
if (array == NULL) {
prop_object_release(dict);
return errno;
}
/*
* Add required objects into package dep's dictionary.
*/
prop_dictionary_set_cstring(dict, "pkgname", pkgname);
prop_dictionary_set_cstring(dict, "version", version);
rundeps_array = prop_dictionary_get(depd, "run_depends");
if (rundeps_array && prop_array_count(rundeps_array) > 0)
prop_dictionary_set(dict, "run_depends", rundeps_array);
prop_dictionary_set_cstring(dict, "repository", repoloc);
prop_dictionary_set_cstring(dict, "filename", binfile);
prop_dictionary_set_cstring(dict, "short_desc", short_desc);
prop_dictionary_set_bool(dict, "indirect_dep", indirectdep);
prop_dictionary_set_cstring(dict, "architecture", arch);
prop_dictionary_set_cstring(dict, "filename-sha256", sha256);
/*
* Remove some unneeded objects.
*/
prop_dictionary_remove(dict, "conf_files");
prop_dictionary_remove(dict, "keep_dirs");
prop_dictionary_remove(dict, "maintainer");
prop_dictionary_remove(dict, "long_desc");
/*
* Add the dictionary into the array.
@@ -174,21 +112,23 @@ store_dependency(prop_dictionary_t origind, prop_dictionary_t depd,
}
static int
add_missing_reqdep(const char *pkgname, const char *version)
add_missing_reqdep(prop_dictionary_t master, const char *pkgname,
const char *version)
{
prop_array_t array;
prop_dictionary_t depd;
assert(master != NULL);
assert(pkgname != NULL);
assert(version != NULL);
/*
* Adds a package into the missing deps array.
*/
if (xbps_find_pkg_in_dict(chaindeps, "missing_deps", pkgname))
if (xbps_find_pkg_in_dict(master, "missing_deps", pkgname))
return EEXIST;
array = prop_dictionary_get(chaindeps, "missing_deps");
array = prop_dictionary_get(master, "missing_deps");
depd = prop_dictionary_create();
if (depd == NULL)
return ENOMEM;
@@ -204,7 +144,8 @@ add_missing_reqdep(const char *pkgname, const char *version)
}
int
xbps_find_deps_in_pkg(prop_dictionary_t pkg, prop_object_iterator_t iter)
xbps_find_deps_in_pkg(prop_dictionary_t master, prop_dictionary_t pkg,
prop_object_iterator_t iter)
{
prop_array_t pkg_rdeps, missing_rdeps;
prop_dictionary_t repod;
@@ -212,6 +153,7 @@ xbps_find_deps_in_pkg(prop_dictionary_t pkg, prop_object_iterator_t iter)
char *plist;
int rv = 0;
assert(pkg_props != NULL);
assert(pkg != NULL);
assert(iter != NULL);
@@ -241,7 +183,7 @@ xbps_find_deps_in_pkg(prop_dictionary_t pkg, prop_object_iterator_t iter)
* 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);
rv = find_repo_deps(master, repod, pkg, pkg_rdeps);
if (rv != 0) {
if (rv == ENOENT) {
rv = 0;
@@ -254,7 +196,7 @@ xbps_find_deps_in_pkg(prop_dictionary_t pkg, prop_object_iterator_t iter)
prop_object_release(repod);
}
missing_rdeps = prop_dictionary_get(chaindeps, "missing_deps");
missing_rdeps = prop_dictionary_get(master, "missing_deps");
if (prop_array_count(missing_rdeps) == 0)
return 0;
@@ -276,7 +218,7 @@ xbps_find_deps_in_pkg(prop_dictionary_t pkg, prop_object_iterator_t iter)
}
free(plist);
rv = find_pkg_missing_deps_from_repo(repod, pkg);
rv = find_repo_missing_deps(master, repod, pkg);
if (rv != 0 && rv != ENOENT) {
prop_object_release(repod);
return rv;
@@ -288,17 +230,9 @@ xbps_find_deps_in_pkg(prop_dictionary_t pkg, prop_object_iterator_t iter)
return 0;
}
prop_dictionary_t
xbps_get_pkg_deps_dictionary(void)
{
if (!deps_dict)
return NULL;
return prop_dictionary_copy(chaindeps);
}
static int
find_pkg_missing_deps_from_repo(prop_dictionary_t repo, prop_dictionary_t pkg)
find_repo_missing_deps(prop_dictionary_t master, prop_dictionary_t repo,
prop_dictionary_t pkg)
{
prop_array_t array;
prop_dictionary_t curpkgd;
@@ -310,7 +244,7 @@ find_pkg_missing_deps_from_repo(prop_dictionary_t repo, prop_dictionary_t pkg)
assert(repo != NULL);
assert(pkg != NULL);
array = prop_dictionary_get(chaindeps, "missing_deps");
array = prop_dictionary_get(master, "missing_deps");
if (prop_array_count(array) == 0)
return 0;
@@ -327,7 +261,7 @@ find_pkg_missing_deps_from_repo(prop_dictionary_t repo, prop_dictionary_t pkg)
*/
curpkgd = xbps_find_pkg_in_dict(repo, "packages", pkgname);
if (curpkgd == NULL) {
rv = add_missing_reqdep(pkgname, version);
rv = add_missing_reqdep(master, pkgname, version);
if (rv != 0 && rv != EEXIST)
break;
else {
@@ -338,12 +272,12 @@ find_pkg_missing_deps_from_repo(prop_dictionary_t repo, prop_dictionary_t pkg)
/*
* Package is on repo, add it into the dictionary.
*/
if ((rv = store_dependency(pkg, curpkgd, repo)) != 0)
if ((rv = store_dependency(master, pkg, curpkgd, repo)) != 0)
break;
/*
* Remove package from missing_deps array now.
*/
rv = xbps_remove_pkg_from_dict(chaindeps,
rv = xbps_remove_pkg_from_dict(master,
"missing_deps", pkgname);
if (rv != 0 && rv != ENOENT)
break;
@@ -356,8 +290,8 @@ find_pkg_missing_deps_from_repo(prop_dictionary_t repo, prop_dictionary_t pkg)
}
static int
find_pkg_deps_from_repo(prop_dictionary_t repo, prop_dictionary_t pkg,
prop_array_t pkg_rdeps)
find_repo_deps(prop_dictionary_t master, prop_dictionary_t repo,
prop_dictionary_t pkg, prop_array_t pkg_rdeps)
{
prop_dictionary_t curpkgd;
prop_array_t curpkg_rdeps;
@@ -376,20 +310,6 @@ find_pkg_deps_from_repo(prop_dictionary_t repo, prop_dictionary_t pkg,
iter = prop_array_iterator(pkg_rdeps);
if (iter == NULL)
return ENOMEM;
/*
* Save the name of the origin package once.
*/
if (deps_dict == false) {
deps_dict = true;
rv = create_deps_dictionary();
if (rv != 0) {
prop_object_iterator_release(iter);
return rv;
}
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
@@ -410,19 +330,17 @@ find_pkg_deps_from_repo(prop_dictionary_t repo, prop_dictionary_t pkg,
* Check if package is already added in the
* array of unsorted deps.
*/
if (xbps_find_pkg_in_dict(chaindeps, "unsorted_deps",
pkgname)) {
if (xbps_find_pkg_in_dict(master, "unsorted_deps", pkgname)) {
free(pkgname);
continue;
}
/*
* 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, reqvers);
rv = add_missing_reqdep(master, pkgname, reqvers);
free(pkgname);
if (rv != 0 && rv != EEXIST)
break;
@@ -431,104 +349,36 @@ find_pkg_deps_from_repo(prop_dictionary_t repo, prop_dictionary_t pkg,
continue;
}
}
/*
* Package is on repo, add it into the dictionary.
*/
if ((rv = store_dependency(pkg, curpkgd, repo)) != 0) {
if ((rv = store_dependency(master, pkg, curpkgd, repo)) != 0) {
free(pkgname);
break;
}
/*
* Remove package from missing_deps now it's been found.
*/
rv = xbps_remove_pkg_from_dict(chaindeps,
rv = xbps_remove_pkg_from_dict(master,
"missing_deps", 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.
*/
if (!find_pkg_deps_from_repo(repo, curpkgd, curpkg_rdeps))
if (!find_repo_deps(master, repo, curpkgd, curpkg_rdeps))
continue;
}
prop_object_iterator_release(iter);
return rv;
}
int
xbps_install_pkg_deps(const char *pkgname, bool update)
{
prop_array_t required, missing;
prop_object_t obj;
prop_object_iterator_t iter;
const char *repoloc;
int rv = 0;
/*
* If origin object in chaindeps is not the same, bail out.
*/
obj = prop_dictionary_get(chaindeps, "origin");
if (obj == NULL || !prop_string_equals_cstring(obj, pkgname))
return EINVAL;
/*
* 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.
*/
printf("Sorting dependency list...\n");
if ((rv = xbps_sort_pkg_deps(chaindeps)) != 0)
return rv;
required = prop_dictionary_get(chaindeps, "required_deps");
if (required == NULL)
return 0;
iter = prop_array_iterator(required);
if (iter == NULL)
return ENOMEM;
/*
* Check the SHA256 hash for any binary package that's going
* to be installed.
*/
while ((obj = prop_object_iterator_next(iter)) != NULL) {
prop_dictionary_get_cstring_nocopy(obj, "repository", &repoloc);
rv = xbps_check_pkg_file_hash(obj, repoloc);
if (rv != 0)
goto out;
}
prop_object_iterator_reset(iter);
/*
* Install all required dependencies, previously sorted.
*/
printf("Installing required dependencies...\n");
while ((obj = prop_object_iterator_next(iter)) != NULL) {
rv = xbps_install_binary_pkg_fini(NULL, obj, update);
if (rv != 0)
break;
}
out:
prop_object_iterator_release(iter);
return rv;
}