Simplify xbps_transaction_prepare()/commit() and related API changes.

- xbps_handle::transd -> new member with transaction dictionary.
- xbps_transaction_prepare: returns an int.
- xbps_transaction_commit: doesn't need any arg now.
- xbps_repository_pool_sync: doesn't need any arg now.
- xbps_pkgdb_update: removed xbps_handle * arg.
- xbps_transaction_missingdeps_get: removed, missing_deps array is in
  xbps_handle::transd("missing_deps") array object.
This commit is contained in:
Juan RP
2012-01-22 10:00:46 +01:00
parent 38db570c19
commit a166d6a2a3
14 changed files with 183 additions and 235 deletions

View File

@@ -62,12 +62,11 @@ configure_pkgs_cb(prop_object_t obj, void *arg, bool *done)
int
xbps_configure_packages(bool flush)
{
struct xbps_handle *xhp = xbps_handle_get();
int rv;
rv = xbps_pkgdb_foreach_cb(configure_pkgs_cb, NULL);
if (rv == 0 && flush)
rv = xbps_pkgdb_update(xhp, true);
rv = xbps_pkgdb_update(true);
return rv;
}
@@ -169,7 +168,7 @@ xbps_configure_pkg(const char *pkgname,
}
free(pkgver);
if (flush)
rv = xbps_pkgdb_update(xhp, true);
rv = xbps_pkgdb_update(true);
return rv;
}

View File

@@ -64,7 +64,7 @@ xbps_pkgdb_init(struct xbps_handle *xhp)
if (xhp->pkgdb != NULL)
return 0;
if ((rv = xbps_pkgdb_update(xhp, false)) != 0) {
if ((rv = xbps_pkgdb_update(false)) != 0) {
if (rv != ENOENT)
xbps_dbg_printf("[pkgdb] cannot internalize "
"pkgdb array: %s\n", strerror(rv));
@@ -77,8 +77,9 @@ xbps_pkgdb_init(struct xbps_handle *xhp)
}
int
xbps_pkgdb_update(struct xbps_handle *xhp, bool flush)
xbps_pkgdb_update(bool flush)
{
struct xbps_handle *xhp = xbps_handle_get();
char *plist, *metadir;
int rv = 0;
@@ -213,7 +214,7 @@ xbps_pkgdb_remove_pkgd(const char *pkg, bool bypattern, bool flush)
if (!flush || !rv)
return rv;
if ((xbps_pkgdb_update(xhp, true)) != 0)
if ((xbps_pkgdb_update(true)) != 0)
return false;
return true;
@@ -239,7 +240,7 @@ xbps_pkgdb_replace_pkgd(prop_dictionary_t pkgd,
if (!flush)
return rv != 0 ? false : true;
if ((xbps_pkgdb_update(xhp, true)) != 0)
if ((xbps_pkgdb_update(true)) != 0)
return false;
return true;

View File

@@ -172,25 +172,25 @@ out:
#define MAX_DEPTH 512
static int
find_repo_deps(prop_dictionary_t transd, /* transaction dictionary */
prop_array_t mrdeps, /* missing rundeps array */
find_repo_deps(prop_dictionary_t transd, /* transaction dictionary */
prop_array_t trans_mdeps, /* transaction missing deps array */
prop_array_t pkg_rdeps_array, /* current pkg rundeps array */
const char *curpkg, /* current pkgver */
size_t *depth) /* max recursion depth */
{
struct xbps_handle *xhp = xbps_handle_get();
prop_dictionary_t curpkgd, tmpd;
prop_array_t curpkgrdeps;
prop_object_t obj;
prop_object_iterator_t iter;
pkg_state_t state;
const struct xbps_handle *xhp = xbps_handle_get();
size_t x;
const char *reqpkg, *pkgver_q, *reason = NULL;
char *pkgname;
int rv = 0;
assert(prop_object_type(transd) == PROP_TYPE_DICTIONARY);
assert(prop_object_type(mrdeps) == PROP_TYPE_ARRAY);
assert(prop_object_type(trans_mdeps) == PROP_TYPE_ARRAY);
assert(prop_object_type(pkg_rdeps_array) == PROP_TYPE_ARRAY);
if (*depth >= MAX_DEPTH)
@@ -335,15 +335,15 @@ find_repo_deps(prop_dictionary_t transd, /* transaction dictionary */
* the array of unsorted dependencies, and check if the pattern
* was matched.
*/
curpkgd = xbps_find_virtualpkg_conf_in_dict_by_pattern(transd,
"unsorted_deps", reqpkg);
curpkgd = xbps_find_virtualpkg_conf_in_dict_by_pattern(
transd, "unsorted_deps", reqpkg);
if (curpkgd == NULL) {
/*
* Look for a real package matching pattern
* if no match.
*/
curpkgd = xbps_find_pkg_in_dict_by_pattern(transd,
"unsorted_deps", reqpkg);
curpkgd = xbps_find_pkg_in_dict_by_pattern(
transd, "unsorted_deps", reqpkg);
}
if (curpkgd != NULL) {
prop_dictionary_get_cstring_nocopy(curpkgd,
@@ -364,7 +364,8 @@ find_repo_deps(prop_dictionary_t transd, /* transaction dictionary */
*/
curpkgd = xbps_repository_pool_find_virtualpkg(reqpkg, true);
if (curpkgd == NULL) {
curpkgd = xbps_repository_pool_find_pkg(reqpkg, true, false);
curpkgd = xbps_repository_pool_find_pkg(reqpkg, true,
false);
if (curpkgd == NULL) {
/* pkg not found, there was some error */
if (errno && errno != ENOENT) {
@@ -375,19 +376,21 @@ find_repo_deps(prop_dictionary_t transd, /* transaction dictionary */
break;
}
rv = add_missing_reqdep(mrdeps, reqpkg);
rv = add_missing_reqdep(trans_mdeps, reqpkg);
if (rv != 0 && rv != EEXIST) {
xbps_dbg_printf_append("`%s': "
"add_missing_reqdep failed %s\n", reqpkg);
"add_missing_reqdep failed %s\n",
reqpkg);
break;
} else if (rv == EEXIST) {
xbps_dbg_printf_append("`%s' missing dep "
"already added.\n", reqpkg);
xbps_dbg_printf_append("`%s' missing "
"dep already added.\n", reqpkg);
rv = 0;
continue;
} else {
xbps_dbg_printf_append("`%s' added "
"into the missing deps array.\n", reqpkg);
"into the missing deps array.\n",
reqpkg);
continue;
}
}
@@ -428,7 +431,7 @@ find_repo_deps(prop_dictionary_t transd, /* transaction dictionary */
* Recursively find rundeps for current pkg dictionary.
*/
(*depth)++;
rv = find_repo_deps(transd, mrdeps, curpkgrdeps,
rv = find_repo_deps(transd, trans_mdeps, curpkgrdeps,
pkgver_q, depth);
if (rv != 0) {
xbps_dbg_printf("Error checking %s for rundeps: %s\n",
@@ -443,17 +446,15 @@ find_repo_deps(prop_dictionary_t transd, /* transaction dictionary */
}
int HIDDEN
xbps_repository_find_pkg_deps(prop_dictionary_t transd,
prop_array_t mdeps,
xbps_repository_find_pkg_deps(struct xbps_handle *xhp,
prop_dictionary_t repo_pkgd)
{
prop_array_t pkg_rdeps;
prop_array_t mdeps, pkg_rdeps;
const char *pkgver;
size_t depth = 0;
int rv = 0;
assert(prop_object_type(transd) == PROP_TYPE_DICTIONARY);
assert(prop_object_type(mdeps) == PROP_TYPE_ARRAY);
assert(prop_object_type(xhp->transd) == PROP_TYPE_DICTIONARY);
assert(prop_object_type(repo_pkgd) == PROP_TYPE_DICTIONARY);
pkg_rdeps = prop_dictionary_get(repo_pkgd, "run_depends");
@@ -462,11 +463,12 @@ xbps_repository_find_pkg_deps(prop_dictionary_t transd,
prop_dictionary_get_cstring_nocopy(repo_pkgd, "pkgver", &pkgver);
xbps_dbg_printf("Finding required dependencies for '%s':\n", pkgver);
mdeps = prop_dictionary_get(xhp->transd, "missing_deps");
/*
* This will find direct and indirect deps, if any of them is not
* there it will be added into the missing_deps array.
*/
if ((rv = find_repo_deps(transd, mdeps, pkg_rdeps,
if ((rv = find_repo_deps(xhp->transd, mdeps, pkg_rdeps,
pkgver, &depth)) != 0) {
xbps_dbg_printf("Error '%s' while checking rundeps!\n",
strerror(rv));

View File

@@ -181,8 +181,9 @@ xbps_repository_pool_release(struct xbps_handle *xhp)
}
int
xbps_repository_pool_sync(const struct xbps_handle *xhp)
xbps_repository_pool_sync(void)
{
const struct xbps_handle *xhp = xbps_handle_get();
const char *repouri;
size_t i;
int rv;

View File

@@ -180,9 +180,9 @@ download_binpkgs(struct xbps_handle *xhp, prop_object_iterator_t iter)
}
int
xbps_transaction_commit(prop_dictionary_t transd)
xbps_transaction_commit(void)
{
struct xbps_handle *xhp;
struct xbps_handle *xhp = xbps_handle_get();
prop_object_t obj;
prop_object_iterator_t iter;
size_t i;
@@ -190,11 +190,10 @@ xbps_transaction_commit(prop_dictionary_t transd)
int rv = 0;
bool update, install;
assert(prop_object_type(transd) == PROP_TYPE_DICTIONARY);
assert(prop_object_type(xhp->transd) == PROP_TYPE_DICTIONARY);
update = install = false;
xhp = xbps_handle_get();
iter = xbps_array_iter_from_dict(transd, "packages");
iter = xbps_array_iter_from_dict(xhp->transd, "packages");
if (iter == NULL)
return EINVAL;
/*
@@ -219,7 +218,7 @@ xbps_transaction_commit(prop_dictionary_t transd)
while ((obj = prop_object_iterator_next(iter)) != NULL) {
if ((xhp->transaction_frequency_flush > 0) &&
(++i >= xhp->transaction_frequency_flush)) {
rv = xbps_pkgdb_update(xhp, true);
rv = xbps_pkgdb_update(true);
if (rv != 0 && rv != ENOENT)
goto out;
@@ -295,7 +294,7 @@ xbps_transaction_commit(prop_dictionary_t transd)
prop_object_iterator_reset(iter);
/* force a flush now packages were removed/unpacked */
if ((rv = xbps_pkgdb_update(xhp, true)) != 0)
if ((rv = xbps_pkgdb_update(true)) != 0)
goto out;
/* if there are no packages to install or update we are done */
@@ -310,7 +309,7 @@ xbps_transaction_commit(prop_dictionary_t transd)
while ((obj = prop_object_iterator_next(iter)) != NULL) {
if (xhp->transaction_frequency_flush > 0 &&
++i >= xhp->transaction_frequency_flush) {
if ((rv = xbps_pkgdb_update(xhp, true)) != 0)
if ((rv = xbps_pkgdb_update(true)) != 0)
goto out;
i = 0;
@@ -344,7 +343,7 @@ xbps_transaction_commit(prop_dictionary_t transd)
}
/* Force a flush now that packages are configured */
rv = xbps_pkgdb_update(xhp, true);
rv = xbps_pkgdb_update(true);
out:
prop_object_iterator_release(iter);

View File

@@ -1,5 +1,5 @@
/*-
* Copyright (c) 2009-2011 Juan Romero Pardines.
* Copyright (c) 2009-2012 Juan Romero Pardines.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -52,55 +52,8 @@
* data type is specified on its edge, i.e string, array, integer, dictionary.
*/
static prop_dictionary_t transd;
static prop_array_t trans_mdeps;
static bool transd_initialized;
static bool trans_mdeps_initialized;
static int
create_transaction_dictionary(void)
{
prop_array_t unsorted;
if (transd_initialized)
return 0;
transd = prop_dictionary_create();
if (transd == NULL)
return ENOMEM;
unsorted = prop_array_create();
if (unsorted == NULL) {
prop_object_release(transd);
return ENOMEM;
}
if (!xbps_add_obj_to_dict(transd, unsorted, "unsorted_deps")) {
prop_object_release(unsorted);
prop_object_release(transd);
return EINVAL;
}
transd_initialized = true;
return 0;
}
static int
create_transaction_missingdeps(void)
{
if (trans_mdeps_initialized)
return 0;
trans_mdeps = prop_array_create();
if (trans_mdeps == NULL)
return ENOMEM;
trans_mdeps_initialized = true;
return 0;
}
static int
compute_transaction_stats(void)
compute_transaction_stats(prop_dictionary_t transd)
{
prop_dictionary_t pkg_metad;
prop_object_iterator_t iter;
@@ -229,74 +182,93 @@ out:
return rv;
}
prop_dictionary_t HIDDEN
xbps_transaction_dictionary_get(void)
int HIDDEN
xbps_transaction_init(struct xbps_handle *xhp)
{
if (create_transaction_dictionary() != 0)
return NULL;
prop_array_t unsorted, mdeps;
return transd;
if (xhp->transd != NULL)
return 0;
xhp->transd = prop_dictionary_create();
if (xhp->transd == NULL)
return ENOMEM;
unsorted = prop_array_create();
if (unsorted == NULL) {
prop_object_release(xhp->transd);
xhp->transd = NULL;
return ENOMEM;
}
if (!xbps_add_obj_to_dict(xhp->transd, unsorted, "unsorted_deps")) {
prop_object_release(xhp->transd);
xhp->transd = NULL;
return EINVAL;
}
mdeps = prop_array_create();
if (mdeps == NULL) {
prop_object_release(xhp->transd);
xhp->transd = NULL;
return ENOMEM;
}
if (!xbps_add_obj_to_dict(xhp->transd, mdeps, "missing_deps")) {
prop_object_release(xhp->transd);
xhp->transd = NULL;
return EINVAL;
}
return 0;
}
prop_array_t
xbps_transaction_missingdeps_get(void)
{
if (create_transaction_missingdeps() != 0)
return NULL;
return trans_mdeps;
}
prop_dictionary_t
int
xbps_transaction_prepare(void)
{
prop_array_t mdeps;
struct xbps_handle *xhp = xbps_handle_get();
int rv = 0;
if (!transd_initialized && !trans_mdeps_initialized) {
errno = ENXIO;
return NULL;
}
if (xhp->transd == NULL)
return ENXIO;
/*
* If there are missing deps bail out.
*/
if (prop_array_count(trans_mdeps) > 0) {
prop_object_release(transd);
errno = ENODEV;
return NULL;
}
mdeps = prop_dictionary_get(xhp->transd, "missing_deps");
if (prop_array_count(mdeps) > 0)
return ENODEV;
/*
* Check for packages to be replaced.
*/
if ((rv = xbps_transaction_package_replace(transd)) != 0) {
errno = rv;
prop_object_release(transd);
prop_object_release(trans_mdeps);
return NULL;
if ((rv = xbps_transaction_package_replace(xhp->transd)) != 0) {
prop_object_release(xhp->transd);
xhp->transd = NULL;
return rv;
}
/*
* Sort package dependencies if necessary.
*/
if ((rv = xbps_sort_pkg_deps()) != 0) {
errno = rv;
prop_object_release(transd);
prop_object_release(trans_mdeps);
return NULL;
if ((rv = xbps_transaction_sort_pkg_deps(xhp)) != 0) {
prop_object_release(xhp->transd);
xhp->transd = NULL;
return rv;
}
/*
* Add transaction stats for total download/installed size,
* number of packages to be installed, updated, configured
* and removed to the transaction dictionary.
*/
if ((rv = compute_transaction_stats()) != 0) {
errno = rv;
prop_object_release(transd);
prop_object_release(trans_mdeps);
return NULL;
if ((rv = compute_transaction_stats(xhp->transd)) != 0) {
prop_object_release(xhp->transd);
xhp->transd = NULL;
return rv;
}
/*
* The missing deps array is not necessary anymore.
*/
prop_object_release(trans_mdeps);
prop_dictionary_remove(xhp->transd, "missing_deps");
prop_dictionary_make_immutable(xhp->transd);
return prop_dictionary_copy(transd);
return 0;
}

View File

@@ -60,8 +60,8 @@ static int
transaction_find_pkg(const char *pattern, int action)
{
prop_dictionary_t pkg_pkgdb, pkg_repod = NULL;
prop_dictionary_t transd;
prop_array_t mdeps, unsorted;
prop_array_t unsorted;
struct xbps_handle *xhp = xbps_handle_get();
const char *pkgname, *pkgver, *repoloc, *repover, *instver, *reason;
int rv = 0;
bool bypattern, bestpkg;
@@ -125,21 +125,15 @@ transaction_find_pkg(const char *pattern, int action)
/*
* Prepare transaction dictionary and missing deps array.
*/
if ((transd = xbps_transaction_dictionary_get()) == NULL) {
rv = EINVAL;
if ((rv = xbps_transaction_init(xhp)) != 0)
goto out;
}
if ((mdeps = xbps_transaction_missingdeps_get()) == NULL) {
rv = EINVAL;
goto out;
}
/*
* Prepare required package dependencies and add them into the
* "unsorted" array in transaction dictionary.
*/
if (xbps_pkg_has_rundeps(pkg_repod)) {
rv = xbps_repository_find_pkg_deps(transd, mdeps, pkg_repod);
rv = xbps_repository_find_pkg_deps(xhp, pkg_repod);
if (rv != 0)
goto out;
}
@@ -174,7 +168,7 @@ transaction_find_pkg(const char *pattern, int action)
/*
* Add required package dictionary into the unsorted array.
*/
unsorted = prop_dictionary_get(transd, "unsorted_deps");
unsorted = prop_dictionary_get(xhp->transd, "unsorted_deps");
if (unsorted == NULL) {
rv = EINVAL;
goto out;
@@ -191,7 +185,7 @@ transaction_find_pkg(const char *pattern, int action)
pkgver, repoloc);
out:
if (prop_object_type(pkg_repod) == PROP_TYPE_DICTIONARY)
if (pkg_repod != NULL)
prop_object_release(pkg_repod);
return rv;
@@ -249,9 +243,10 @@ xbps_transaction_install_pkg(const char *pkgpattern)
int
xbps_transaction_remove_pkg(const char *pkgname, bool recursive)
{
prop_dictionary_t transd, pkgd;
prop_array_t mdeps, orphans, orphans_pkg, unsorted, reqby;
prop_dictionary_t pkgd;
prop_array_t orphans, orphans_pkg, unsorted, reqby;
prop_object_t obj;
struct xbps_handle *xhp = xbps_handle_get();
const char *pkgver;
size_t count;
int rv = 0;
@@ -265,15 +260,10 @@ xbps_transaction_remove_pkg(const char *pkgname, bool recursive)
/*
* Prepare transaction dictionary and missing deps array.
*/
if ((transd = xbps_transaction_dictionary_get()) == NULL) {
rv = ENXIO;
if ((rv = xbps_transaction_init(xhp)) != 0)
goto out;
}
if ((mdeps = xbps_transaction_missingdeps_get()) == NULL) {
rv = ENXIO;
goto out;
}
unsorted = prop_dictionary_get(transd, "unsorted_deps");
unsorted = prop_dictionary_get(xhp->transd, "unsorted_deps");
if (!recursive)
goto rmpkg;
/*
@@ -329,9 +319,9 @@ out:
int
xbps_transaction_autoremove_pkgs(void)
{
prop_dictionary_t transd;
prop_array_t orphans, mdeps, unsorted;
prop_array_t orphans, unsorted;
prop_object_t obj;
struct xbps_handle *xhp = xbps_handle_get();
const char *pkgver;
size_t count;
int rv = 0;
@@ -349,18 +339,12 @@ xbps_transaction_autoremove_pkgs(void)
/*
* Prepare transaction dictionary and missing deps array.
*/
if ((transd = xbps_transaction_dictionary_get()) == NULL) {
rv = ENXIO;
if ((rv = xbps_transaction_init(xhp)) != 0)
goto out;
}
if ((mdeps = xbps_transaction_missingdeps_get()) == NULL) {
rv = ENXIO;
goto out;
}
/*
* Add pkg orphan dictionary into the unsorted_deps array.
*/
unsorted = prop_dictionary_get(transd, "unsorted_deps");
unsorted = prop_dictionary_get(xhp->transd, "unsorted_deps");
while (count--) {
obj = prop_array_get(orphans, count);
prop_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver);
@@ -371,7 +355,7 @@ xbps_transaction_autoremove_pkgs(void)
pkgver);
}
out:
if (prop_object_type(orphans) == PROP_TYPE_ARRAY)
if (orphans != NULL)
prop_object_release(orphans);
return rv;
}

View File

@@ -1,5 +1,5 @@
/*-
* Copyright (c) 2009-2011 Juan Romero Pardines.
* Copyright (c) 2009-2012 Juan Romero Pardines.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -303,9 +303,8 @@ again:
}
int HIDDEN
xbps_sort_pkg_deps(void)
xbps_transaction_sort_pkg_deps(struct xbps_handle *xhp)
{
prop_dictionary_t transd;
prop_array_t sorted, unsorted, rundeps;
prop_object_t obj;
prop_object_iterator_t iter;
@@ -314,25 +313,22 @@ xbps_sort_pkg_deps(void)
const char *pkgname, *pkgver, *tract;
int rv = 0;
if ((transd = xbps_transaction_dictionary_get()) == NULL)
return EINVAL;
sorted = prop_array_create();
if (sorted == NULL)
return ENOMEM;
/*
* Add sorted packages array into transaction dictionary (empty).
*/
if (!prop_dictionary_set(transd, "packages", sorted)) {
if (!prop_dictionary_set(xhp->transd, "packages", sorted)) {
rv = EINVAL;
goto out;
}
/*
* All required deps are satisfied (already installed).
*/
unsorted = prop_dictionary_get(transd, "unsorted_deps");
unsorted = prop_dictionary_get(xhp->transd, "unsorted_deps");
if (prop_array_count(unsorted) == 0) {
prop_dictionary_set(transd, "packages", sorted);
prop_dictionary_set(xhp->transd, "packages", sorted);
return 0;
}
/*
@@ -395,7 +391,7 @@ xbps_sort_pkg_deps(void)
/*
* Sort package run-time dependencies for this package.
*/
if ((rv = sort_pkg_rundeps(transd, pd, rundeps)) != 0) {
if ((rv = sort_pkg_rundeps(xhp->transd, pd, rundeps)) != 0) {
pkgdep_end(NULL);
prop_object_iterator_release(iter);
goto out;
@@ -418,10 +414,10 @@ xbps_sort_pkg_deps(void)
* We are done, all packages were sorted... remove the
* temporary array with unsorted packages.
*/
prop_dictionary_remove(transd, "unsorted_deps");
prop_dictionary_remove(xhp->transd, "unsorted_deps");
out:
if (rv != 0)
prop_dictionary_remove(transd, "packages");
prop_dictionary_remove(xhp->transd, "packages");
prop_object_release(sorted);