API clean up (part 2), plus misc changes and improvements.

- Rename regpkgs_dictionary to regpkgdb_dictionary to better describe what is is.
- Change some funcs in plist.c to return a boolean rather than int.
- Hide more internal funcs off the API.
- Simplify xbps_repository_update_pkg() and remove its second arg.
- Hide implementation details in xbps_repository_pool, now to iterate over the
  pool you have to use xbps_repository_pool_foreach and its struct
  repository_pool_index.
- Introduce xbps_{init,end}, to initialize/destroy some stuff in the library.
- Introduce xbps_dbg_printf to printf stuff for debugging purposes.
- xbps-{bin,repo}:  added -d arg to enable debugging output.
- Before checking if a config file needs to be installed or such, check that
  package contains the "conf_files" array.
- Remove obsolete dirs as well while updating packages.
- If transaction dictionary is ready remove the "missing_deps" array.

Bump XBPS_RELVER to 20101118.

--HG--
rename : lib/regpkgs_dictionary.c => lib/regpkgdb_dictionary.c
This commit is contained in:
Juan RP
2010-11-19 13:40:13 +01:00
parent ffc255b715
commit fdec663855
33 changed files with 1426 additions and 943 deletions

View File

@ -56,6 +56,16 @@
static prop_dictionary_t trans_dict;
static bool trans_dict_initialized;
/*
* This creates the transaction dictionary with two arrays, one for
* dependencies not yet sorted and another one for missing dependencies.
*
* Before returning the dictionary to the caller, package dependencies in
* the "unsorted_deps" array will be sorted and moved to another
* array called "packages". If there are no missing dependencies, the
* "missing_deps" array will be removed.
*
*/
static int
create_transaction_dictionary(void)
{
@ -77,31 +87,31 @@ create_transaction_dictionary(void)
unsorted = prop_array_create();
if (unsorted == NULL) {
rv = ENOMEM;
goto fail2;
}
rv = ENOMEM;
goto fail2;
}
if (!xbps_add_obj_to_dict(trans_dict, missing, "missing_deps")) {
rv = EINVAL;
goto fail3;
if (!xbps_add_obj_to_dict(trans_dict, missing, "missing_deps")) {
rv = EINVAL;
goto fail3;
}
if (!xbps_add_obj_to_dict(trans_dict, unsorted, "unsorted_deps")) {
rv = EINVAL;
goto fail3;
}
rv = EINVAL;
goto fail3;
}
trans_dict_initialized = true;
return rv;
return rv;
fail3:
prop_object_release(unsorted);
prop_object_release(unsorted);
fail2:
prop_object_release(missing);
prop_object_release(missing);
fail:
prop_object_release(trans_dict);
prop_object_release(trans_dict);
return rv;
return rv;
}
static int
@ -115,7 +125,7 @@ compute_transaction_sizes(void)
iter = xbps_get_array_iter_from_dict(trans_dict, "packages");
if (iter == NULL)
return -1;
return EINVAL;
while ((obj = prop_object_iterator_next(iter)) != NULL) {
prop_dictionary_get_cstring_nocopy(obj, "trans-action", &tract);
@ -139,7 +149,7 @@ compute_transaction_sizes(void)
*/
if (!prop_dictionary_set_uint64(trans_dict,
"total-installed-size", instsize)) {
rv = -1;
rv = EINVAL;
goto out;
}
/*
@ -148,7 +158,7 @@ compute_transaction_sizes(void)
*/
if (!prop_dictionary_set_uint64(trans_dict,
"total-download-size", dlsize)) {
rv = -1;
rv = EINVAL;
goto out;
}
out:
@ -172,11 +182,14 @@ set_pkg_state(prop_dictionary_t pkgd, const char *pkgname)
*/
rv = xbps_get_pkg_state_installed(pkgname, &state);
if (rv == 0) {
if (state == XBPS_PKG_STATE_INSTALLED)
return 0;
if ((rv = xbps_set_pkg_state_dictionary(pkgd, state)) != 0)
return rv;
} else if (rv == ENOENT)
} else if (rv == ENOENT)
rv = 0;
return rv;
}
@ -186,23 +199,23 @@ xbps_repository_get_transaction_dict(void)
int rv = 0;
if (trans_dict_initialized == false) {
errno = ENOENT;
errno = ENXIO;
return NULL;
}
/*
* Sort package list if necessary.
* Sort package dependencies if necessary.
*/
if ((rv = xbps_sort_pkg_deps(trans_dict)) != 0) {
errno = rv;
/*
* If there are missing deps (errno==ENOENT)
* If there are missing deps (ENOENT)
* return the dictionary, the client should always
* check if that's the case.
*/
if (errno == ENOENT)
if (rv == ENOENT)
return trans_dict;
errno = rv;
return NULL;
}
@ -213,6 +226,11 @@ xbps_repository_get_transaction_dict(void)
if (compute_transaction_sizes() != 0)
return NULL;
/*
* Remove the "missing_deps" array now that it's not needed.
*/
prop_dictionary_remove(trans_dict, "missing_deps");
return trans_dict;
}
@ -229,16 +247,13 @@ xbps_repository_update_allpkgs(void)
/*
* Prepare dictionary with all registered packages.
*/
dict = xbps_regpkgs_dictionary_init();
dict = xbps_regpkgdb_dictionary_get();
if (dict == NULL)
return ENOENT;
if ((rv = xbps_repository_pool_init()) != 0)
goto out;
return errno;
iter = xbps_get_array_iter_from_dict(dict, "packages");
if (iter == NULL) {
rv = EINVAL;
rv = errno;
goto out;
}
@ -248,40 +263,96 @@ xbps_repository_update_allpkgs(void)
*/
while ((obj = prop_object_iterator_next(iter)) != NULL) {
prop_dictionary_get_cstring_nocopy(obj, "pkgname", &pkgname);
rv = xbps_repository_update_pkg(pkgname, obj);
if (rv == ENOENT)
continue;
else if (rv == EEXIST) {
rv = 0;
continue;
} else if (rv != 0)
break;
if ((rv = xbps_repository_update_pkg(pkgname)) != 0) {
if (rv == ENOENT || rv == EEXIST)
continue;
xbps_dbg_printf("[update-all] '%s' returned: %s\n",
pkgname, strerror(rv));
goto out;
}
newpkg_found = true;
}
prop_object_iterator_release(iter);
if (rv != ENOENT && !newpkg_found)
rv = ENOPKG;
if (newpkg_found)
rv = 0;
else
rv = ENXIO;
out:
xbps_repository_pool_release();
xbps_regpkgs_dictionary_release();
xbps_regpkgdb_dictionary_release();
return rv;
}
int
xbps_repository_update_pkg(const char *pkgname, prop_dictionary_t instpkg)
struct rpool_index_data {
prop_dictionary_t pkg_repod;
const char *pkgname;
const char *repo_uri;
bool newpkgfound;
};
static int
repo_find_updated_pkg_cb(struct repository_pool_index *rpi, void *arg, bool *done)
{
prop_dictionary_t pkgrd = NULL;
prop_array_t unsorted;
struct repository_pool *rpool;
struct rpool_index_data *rid = arg;
prop_dictionary_t instpkgd;
const char *repover, *instver;
int flags = xbps_get_flags(), rv = 0;
bool newpkg_found = false;
/*
* Get the package dictionary from current repository.
* If it's not there, pass to the next repository.
*/
rid->pkg_repod = xbps_find_pkg_in_dict_by_name(rpi->rpi_repod,
"packages", rid->pkgname);
if (rid->pkg_repod == NULL) {
if (errno && errno != ENOENT)
return errno;
xbps_dbg_printf("Package '%s' not found in repository "
"'%s'.\n", rid->pkgname, rpi->rpi_uri);
} else {
/*
* Check if version in repository is greater than
* the version currently installed.
*/
instpkgd = xbps_find_pkg_dict_installed(rid->pkgname, false);
prop_dictionary_get_cstring_nocopy(instpkgd,
"version", &instver);
prop_dictionary_get_cstring_nocopy(rid->pkg_repod,
"version", &repover);
prop_object_release(instpkgd);
if (xbps_cmpver(repover, instver) > 0) {
xbps_dbg_printf("Found '%s-%s' (installed: %s) "
"in repository '%s'.\n", rid->pkgname, repover,
instver, rpi->rpi_uri);
/*
* New package version found, exit from the loop.
*/
rid->newpkgfound = true;
rid->repo_uri = rpi->rpi_uri;
*done = true;
return 0;
}
xbps_dbg_printf("Skipping '%s-%s' (installed: %s) "
"from repository '%s'\n", rid->pkgname, repover, instver,
rpi->rpi_uri);
}
return 0;
}
int
xbps_repository_update_pkg(const char *pkgname)
{
prop_array_t unsorted;
prop_dictionary_t pkgd;
struct rpool_index_data *rid;
int rv = 0;
assert(pkgname != NULL);
assert(instpkg != NULL);
/*
* Prepare repository pool queue.
@ -289,51 +360,42 @@ xbps_repository_update_pkg(const char *pkgname, prop_dictionary_t instpkg)
if ((rv = xbps_repository_pool_init()) != 0)
return rv;
SIMPLEQ_FOREACH(rpool, &rp_queue, rp_entries) {
/*
* Get the package dictionary from current repository.
* If it's not there, pass to the next repository.
*/
pkgrd = xbps_find_pkg_in_dict_by_name(rpool->rp_repod,
"packages", pkgname);
if (pkgrd == NULL) {
if (errno && errno != ENOENT) {
rv = errno;
goto out;
}
if (flags & XBPS_FLAG_VERBOSE)
printf("Package '%s' not found in repository "
"'%s'.\n", pkgname, rpool->rp_uri);
} else if (pkgrd != NULL) {
/*
* Check if version in repository is greater than
* the version currently installed.
*/
prop_dictionary_get_cstring_nocopy(instpkg,
"version", &instver);
prop_dictionary_get_cstring_nocopy(pkgrd,
"version", &repover);
if (xbps_cmpver(repover, instver) > 0) {
if (flags & XBPS_FLAG_VERBOSE) {
printf("Found '%s-%s' in repository "
"'%s'.\n", pkgname, repover,
rpool->rp_uri);
}
newpkg_found = true;
break;
}
if (flags & XBPS_FLAG_VERBOSE)
printf("Skipping '%s-%s' from repository "
"'%s'.\n", pkgname, repover, rpool->rp_uri);
continue;
}
rid = calloc(1, sizeof(struct rpool_index_data));
if (rid == NULL) {
rv = errno;
goto out;
}
if (!newpkg_found) {
/*
* Check if package is not installed.
*/
pkgd = xbps_find_pkg_dict_installed(pkgname, false);
if (pkgd == NULL) {
rv = ENODEV;
goto out;
}
prop_object_release(pkgd);
/*
* Find out if a new package version exists in repositories.
*/
rid->pkgname = pkgname;
rv = xbps_repository_pool_foreach(repo_find_updated_pkg_cb, rid);
if (rv != 0)
goto out;
/*
* No new versions found in repository pool.
*/
if (rid->newpkgfound == false) {
rv = EEXIST;
goto out;
}
if (pkgrd == NULL) {
/*
* Package couldn't be found in repository pool.
*/
if (rid->pkg_repod == NULL) {
rv = ENOENT;
goto out;
}
@ -346,7 +408,8 @@ xbps_repository_update_pkg(const char *pkgname, prop_dictionary_t instpkg)
/*
* Set repository in pkg dictionary.
*/
if (!prop_dictionary_set_cstring(pkgrd, "repository", rpool->rp_uri)) {
if (!prop_dictionary_set_cstring(rid->pkg_repod,
"repository", rid->repo_uri)) {
rv = errno;
goto out;
}
@ -354,7 +417,8 @@ xbps_repository_update_pkg(const char *pkgname, prop_dictionary_t instpkg)
/*
* Construct the dependency chain for this package.
*/
if ((rv = xbps_repository_find_pkg_deps(trans_dict, pkgrd)) != 0)
rv = xbps_repository_find_pkg_deps(trans_dict, rid->pkg_repod);
if (rv != 0)
goto out;
/*
@ -363,7 +427,7 @@ xbps_repository_update_pkg(const char *pkgname, prop_dictionary_t instpkg)
*/
unsorted = prop_dictionary_get(trans_dict, "unsorted_deps");
if (unsorted == NULL) {
rv = EINVAL;
rv = errno;
goto out;
}
@ -371,66 +435,96 @@ xbps_repository_update_pkg(const char *pkgname, prop_dictionary_t instpkg)
* Always set "not-installed" package state. Will be overwritten
* to its correct state later.
*/
if ((rv = set_pkg_state(pkgrd, pkgname)) != 0)
if ((rv = set_pkg_state(rid->pkg_repod, pkgname)) != 0)
goto out;
if (!prop_dictionary_set_cstring_nocopy(pkgrd,
/*
* Set trans-action obj in pkg dictionary to "update".
*/
if (!prop_dictionary_set_cstring_nocopy(rid->pkg_repod,
"trans-action", "update")) {
rv = errno;
goto out;
}
if (!prop_array_add(unsorted, pkgrd))
/*
* Added package dictionary from repository into the "unsorted"
* array in the transaction dictionary.
*/
if (!prop_array_add(unsorted, rid->pkg_repod)) {
rv = errno;
goto out;
}
out:
if (rid)
free(rid);
xbps_repository_pool_release();
return rv;
}
int
xbps_repository_install_pkg(const char *pkg, bool bypattern)
static int
repo_find_new_pkg_cb(struct repository_pool_index *rpi, void *arg, bool *done)
{
prop_dictionary_t origin_pkgrd = NULL, pkgrd = NULL;
struct rpool_index_data *rid = arg;
const char *pkgver;
/*
* Finds a package dictionary from a repository pkg-index dictionary.
*/
rid->pkg_repod = xbps_find_pkg_in_dict_by_pattern(
rpi->rpi_repod, "packages", rid->pkgname);
if (rid->pkg_repod == NULL) {
if (errno && errno != ENOENT)
return errno;
} else {
prop_dictionary_get_cstring_nocopy(rid->pkg_repod,
"pkgver", &pkgver);
xbps_dbg_printf("Found package '%s' from repository %s.\n",
pkgver, rpi->rpi_uri);
rid->repo_uri = rpi->rpi_uri;
*done = true;
}
return 0;
}
int
xbps_repository_install_pkg(const char *pkg)
{
prop_dictionary_t origin_pkgrd = NULL;
prop_array_t unsorted;
struct repository_pool *rpool;
const char *pkgname, *pkgver;
int flags = xbps_get_flags(), rv = 0;
struct rpool_index_data *rid;
const char *pkgname;
int rv = 0;
assert(pkg != NULL);
if ((rv = xbps_repository_pool_init()) != 0)
return rv;
SIMPLEQ_FOREACH(rpool, &rp_queue, rp_entries) {
/*
* Get the package dictionary from current repository.
* If it's not there, pass to the next repository.
*/
if (bypattern)
pkgrd = xbps_find_pkg_in_dict_by_pattern(
rpool->rp_repod, "packages", pkg);
else
pkgrd = xbps_find_pkg_in_dict_by_name(
rpool->rp_repod, "packages", pkg);
if (pkgrd == NULL) {
if (errno && errno != ENOENT) {
rv = errno;
goto out;
}
} else if (pkgrd != NULL) {
if (flags & XBPS_FLAG_VERBOSE) {
prop_dictionary_get_cstring_nocopy(pkgrd,
"pkgver", &pkgver);
printf("Found package '%s' (%s).\n",
pkgver, rpool->rp_uri);
}
break;
}
rid = calloc(1, sizeof(struct rpool_index_data));
if (rid == NULL) {
rv = errno;
goto out;
}
if (pkgrd == NULL) {
/*
* Get the package dictionary from current repository.
* If it's not there, pass to the next repository.
*/
rid->pkgname = pkg;
rv = xbps_repository_pool_foreach(repo_find_new_pkg_cb, rid);
if (rv != 0)
goto out;
/*
* Package couldn't be found in repository pool... EAGAIN.
*/
if (rid->pkg_repod == NULL) {
rv = EAGAIN;
goto out;
}
@ -438,44 +532,38 @@ xbps_repository_install_pkg(const char *pkg, bool bypattern)
/*
* Create the transaction dictionary.
*/
if ((rv = create_transaction_dictionary()) != 0)
if ((rv = create_transaction_dictionary()) != 0)
goto out;
/*
* Check that this pkg hasn't been added previously into
* the transaction.
*/
if (bypattern) {
if (xbps_find_pkg_in_dict_by_pattern(trans_dict,
"unsorted_deps", pkg))
goto out;
} else {
if (xbps_find_pkg_in_dict_by_name(trans_dict,
"unsorted_deps", pkg))
goto out;
}
/*
* Set repository in pkg dictionary.
*/
if (!prop_dictionary_set_cstring(pkgrd, "repository", rpool->rp_uri)) {
rv = errno;
goto out;
}
origin_pkgrd = prop_dictionary_copy(pkgrd);
prop_dictionary_get_cstring_nocopy(pkgrd, "pkgname", &pkgname);
/*
* Prepare required package dependencies.
*/
if ((rv = xbps_repository_find_pkg_deps(trans_dict, pkgrd)) != 0)
if (xbps_find_pkg_in_dict_by_pattern(trans_dict,
"unsorted_deps", pkg))
goto out;
if (flags & XBPS_FLAG_VERBOSE)
printf("\n");
/*
* Set repository location in pkg dictionary.
*/
if (!prop_dictionary_set_cstring(rid->pkg_repod,
"repository", rid->repo_uri)) {
rv = EINVAL;
goto out;
}
origin_pkgrd = prop_dictionary_copy(rid->pkg_repod);
prop_dictionary_get_cstring_nocopy(rid->pkg_repod, "pkgname", &pkgname);
/*
* Add required package dictionary into the unsorted deps dictionary,
* Prepare required package dependencies and add them into the
* "unsorted" array in transaction dictionary.
*/
rv = xbps_repository_find_pkg_deps(trans_dict, rid->pkg_repod);
if (rv != 0)
goto out;
/*
* Add required package dictionary into the unsorted array and
* set package state as not yet installed.
*/
unsorted = prop_dictionary_get(trans_dict, "unsorted_deps");
@ -486,15 +574,27 @@ xbps_repository_install_pkg(const char *pkg, bool bypattern)
if ((rv = set_pkg_state(origin_pkgrd, pkgname)) != 0)
goto out;
/*
* Set trans-action obj in pkg dictionary to "install".
*/
if (!prop_dictionary_set_cstring_nocopy(origin_pkgrd,
"trans-action", "install")) {
rv = EINVAL;
goto out;
}
/*
* Add the pkg dictionary from repository's index dictionary into
* the "unsorted" array in transaction dictionary.
*/
if (!prop_array_add(unsorted, origin_pkgrd)) {
rv = errno;
goto out;
}
if (!prop_array_add(unsorted, origin_pkgrd))
rv = errno;
out:
if (rid)
free(rid);
if (origin_pkgrd)
prop_object_release(origin_pkgrd);