Introduce xbps_transaction_remove_pkg() and use it for xbps-bin(8).

This commit is contained in:
Juan RP
2011-11-27 09:05:18 +01:00
parent dcac4ec153
commit 9d731ffe09
8 changed files with 226 additions and 208 deletions

View File

@@ -102,15 +102,16 @@ create_transaction_missingdeps(void)
static int
compute_transaction_stats(void)
{
prop_dictionary_t pkg_metad;
prop_object_iterator_t iter;
prop_object_t obj;
uint64_t tsize, dlsize, instsize;
uint64_t tsize, dlsize, instsize, rmsize;
uint32_t inst_pkgcnt, up_pkgcnt, cf_pkgcnt, rm_pkgcnt;
int rv = 0;
const char *tract;
const char *tract, *pkgname;
inst_pkgcnt = up_pkgcnt = cf_pkgcnt = rm_pkgcnt = 0;
tsize = dlsize = instsize = 0;
tsize = dlsize = instsize = rmsize = 0;
iter = xbps_array_iter_from_dict(transd, "packages");
if (iter == NULL)
@@ -160,22 +161,39 @@ compute_transaction_stats(void)
prop_object_iterator_reset(iter);
while ((obj = prop_object_iterator_next(iter)) != NULL) {
prop_dictionary_get_cstring_nocopy(obj, "pkgname", &pkgname);
prop_dictionary_get_cstring_nocopy(obj, "transaction", &tract);
/*
* Only process pkgs to be installed or updated.
*/
if ((strcmp(tract, "configure") == 0) ||
(strcmp(tract, "remove") == 0))
if (strcmp(tract, "configure") == 0)
continue;
prop_dictionary_get_uint64(obj, "filename-size", &tsize);
dlsize += tsize;
tsize = 0;
prop_dictionary_get_uint64(obj, "installed_size", &tsize);
instsize += tsize;
tsize = 0;
/*
* If removing a package, get installed_size from
* pkg's metadata dictionary.
*/
if (strcmp(tract, "remove") == 0) {
pkg_metad =
xbps_dictionary_from_metadata_plist(pkgname,
XBPS_PKGPROPS);
prop_dictionary_get_uint64(pkg_metad,
"installed_size", &tsize);
prop_object_release(pkg_metad);
rmsize += tsize;
} else {
prop_dictionary_get_uint64(obj,
"installed_size", &tsize);
instsize += tsize;
prop_dictionary_get_uint64(obj,
"filename-size", &tsize);
dlsize += tsize;
}
}
/* installed - removed */
if (rmsize > 0 && instsize > 0)
instsize -= rmsize;
/*
* Add object in transaction dictionary with total installed
* size that it will take.
@@ -194,6 +212,15 @@ compute_transaction_stats(void)
rv = EINVAL;
goto out;
}
/*
* Add object in transaction dictionary with total size to be
* freed from packages to be removed.
*/
if (!prop_dictionary_set_uint64(transd,
"total-removed-size", rmsize)) {
rv = EINVAL;
goto out;
}
out:
prop_object_iterator_release(iter);

View File

@@ -237,6 +237,90 @@ xbps_transaction_install_pkg(const char *pkgpattern)
return transaction_find_pkg(pkgpattern, "install");
}
int
xbps_transaction_remove_pkg(const char *pkgname, bool purge, bool recursive)
{
prop_dictionary_t transd, pkgd;
prop_array_t mdeps, orphans, orphans_pkg, unsorted, reqby;
prop_object_t obj;
const char *pkgver;
size_t count;
int rv = 0;
assert(pkgname != NULL);
pkgd = xbps_find_pkg_dict_installed(pkgname, false);
if (prop_object_type(pkgd) != PROP_TYPE_DICTIONARY) {
/* pkg not installed */
rv = ENOENT;
goto out;
}
/*
* Prepare transaction dictionary and missing deps array.
*/
if ((transd = xbps_transaction_dictionary_get()) == NULL) {
rv = ENXIO;
goto out;
}
if ((mdeps = xbps_transaction_missingdeps_get()) == NULL) {
rv = ENXIO;
goto out;
}
unsorted = prop_dictionary_get(transd, "unsorted_deps");
if (!recursive)
goto rmpkg;
/*
* If recursive is set, find out which packages would be orphans
* if the supplied package were already removed.
*/
orphans_pkg = prop_array_create();
if (orphans_pkg == NULL) {
rv = ENOMEM;
goto out;
}
prop_array_set_cstring_nocopy(orphans_pkg, 0, pkgname);
orphans = xbps_find_pkg_orphans(orphans_pkg);
prop_object_release(orphans_pkg);
if (prop_object_type(orphans) != PROP_TYPE_ARRAY) {
rv = EINVAL;
goto out;
}
count = prop_array_count(orphans);
while (count--) {
obj = prop_array_get(orphans, count);
prop_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver);
prop_dictionary_set_cstring_nocopy(obj, "transaction", "remove");
if (purge)
prop_dictionary_set_bool(obj, "remove-and-purge", true);
prop_array_add(unsorted, obj);
xbps_dbg_printf("%s: added into transaction (remove).\n", pkgver);
}
prop_object_release(orphans);
rmpkg:
/*
* Add pkg dictionary into the unsorted_deps array.
*/
prop_dictionary_get_cstring_nocopy(pkgd, "pkgver", &pkgver);
prop_dictionary_set_cstring_nocopy(pkgd, "transaction", "remove");
if (purge)
prop_dictionary_set_bool(pkgd, "remove-and-purge", true);
prop_array_add(unsorted, pkgd);
xbps_dbg_printf("%s: added into transaction (remove).\n", pkgver);
reqby = prop_dictionary_get(pkgd, "requiredby");
/*
* If target pkg is required by any installed pkg, the client must be aware
* of this to take appropiate action.
*/
if ((prop_object_type(reqby) == PROP_TYPE_ARRAY) &&
(prop_array_count(reqby) > 0))
rv = EEXIST;
out:
if (prop_object_type(pkgd) == PROP_TYPE_DICTIONARY)
prop_object_release(pkgd);
return rv;
}
int
xbps_transaction_autoremove_pkgs(bool purge)
{