xbps-bin: use a single point to cleanup resources.

--HG--
extra : convert_revision : xtraeme%40gmail.com-20091023120154-wj4937kmu6ryacpk
This commit is contained in:
Juan RP 2009-10-23 14:01:54 +02:00
parent ca39d1667e
commit b689725270
4 changed files with 35 additions and 48 deletions

View File

@ -26,9 +26,9 @@
#ifndef _XBPS_BIN_DEFS_H_
#define _XBPS_BIN_DEFS_H_
void xbps_exec_transaction(const char *, bool, bool);
void xbps_autoremove_pkgs(void);
void xbps_remove_installed_pkg(const char *, bool);
int xbps_exec_transaction(const char *, bool, bool);
int xbps_autoremove_pkgs(void);
int xbps_remove_installed_pkg(const char *, bool);
int xbps_check_pkg_integrity(const char *);
int xbps_check_pkg_integrity_all(void);
int xbps_show_pkg_deps(const char *);

View File

@ -39,7 +39,6 @@ struct transaction {
bool force;
};
static void cleanup(int);
static int exec_transaction(struct transaction *);
static void show_missing_deps(prop_dictionary_t, const char *);
static int show_missing_dep_cb(prop_object_t, void *, bool *);
@ -206,7 +205,7 @@ show_transaction_sizes(prop_object_iterator_t iter)
return 0;
}
void
int
xbps_exec_transaction(const char *pkgname, bool force, bool update)
{
struct transaction *trans;
@ -225,10 +224,10 @@ xbps_exec_transaction(const char *pkgname, bool force, bool update)
if ((rv = xbps_find_new_packages()) != 0) {
if (rv == ENOENT) {
printf("No packages currently registered.\n");
cleanup(0);
return 0;
} else if (rv == ENOPKG) {
printf("All packages are up-to-date.\n");
cleanup(0);
return 0;
}
goto out;
}
@ -246,21 +245,21 @@ xbps_exec_transaction(const char *pkgname, bool force, bool update)
printf("Package '%s' is up to date.\n",
pkgname);
prop_object_release(pkgd);
cleanup(rv);
return rv;
} else if (rv == ENOENT) {
printf("Package '%s' not found in "
"repository pool.\n", pkgname);
prop_object_release(pkgd);
cleanup(rv);
return rv;
} else if (rv != 0) {
prop_object_release(pkgd);
cleanup(rv);
return rv;
}
prop_object_release(pkgd);
} else {
printf("Package '%s' not installed.\n",
pkgname);
cleanup(rv);
return rv;
}
} else {
/*
@ -271,16 +270,16 @@ xbps_exec_transaction(const char *pkgname, bool force, bool update)
printf("Package '%s' is already installed.\n",
pkgname);
prop_object_release(pkgd);
cleanup(rv);
return rv;
}
rv = xbps_prepare_pkg(pkgname);
if (rv != 0 && rv == EAGAIN) {
printf("Unable to locate '%s' in "
"repository pool.\n", pkgname);
cleanup(rv);
return rv;
} else if (rv != 0 && rv != ENOENT) {
printf("Unexpected error: %s", strerror(rv));
cleanup(rv);
return rv;
}
}
}
@ -337,7 +336,7 @@ out2:
out1:
free(trans);
out:
cleanup(rv);
return rv;
}
static int
@ -544,11 +543,3 @@ exec_transaction(struct transaction *trans)
return 0;
}
static void
cleanup(int rv)
{
xbps_release_repolist_data();
xbps_release_regpkgdb_dict();
exit(rv == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
}

View File

@ -168,21 +168,21 @@ main(int argc, char **argv)
if (argc != 2)
usage();
xbps_exec_transaction(argv[1], force, false);
rv = xbps_exec_transaction(argv[1], force, false);
} else if (strcasecmp(argv[0], "update") == 0) {
/* Update an installed package. */
if (argc != 2)
usage();
xbps_exec_transaction(argv[1], force, true);
rv = xbps_exec_transaction(argv[1], force, true);
} else if (strcasecmp(argv[0], "remove") == 0) {
/* Removes a binary package. */
if (argc != 2)
usage();
xbps_remove_installed_pkg(argv[1], force);
rv = xbps_remove_installed_pkg(argv[1], force);
} else if (strcasecmp(argv[0], "show") == 0) {
/* Shows info about an installed binary package. */
@ -192,7 +192,7 @@ main(int argc, char **argv)
rv = show_pkg_info_from_metadir(argv[1]);
if (rv != 0) {
printf("Package %s not installed.\n", argv[1]);
exit(EXIT_FAILURE);
goto out;
}
} else if (strcasecmp(argv[0], "show-files") == 0) {
@ -203,7 +203,7 @@ main(int argc, char **argv)
rv = show_pkg_files_from_metadir(argv[1]);
if (rv != 0) {
printf("Package %s not installed.\n", argv[1]);
exit(EXIT_FAILURE);
goto out;
}
} else if (strcasecmp(argv[0], "check") == 0) {
@ -223,7 +223,7 @@ main(int argc, char **argv)
if (argc != 1)
usage();
xbps_exec_transaction("all", force, true);
rv = xbps_exec_transaction("all", force, true);
} else if (strcasecmp(argv[0], "autoremove") == 0) {
/*
@ -234,7 +234,7 @@ main(int argc, char **argv)
if (argc != 1)
usage();
xbps_autoremove_pkgs();
rv = xbps_autoremove_pkgs();
} else if (strcasecmp(argv[0], "purge") == 0) {
/*

View File

@ -33,7 +33,7 @@
#include "defs.h"
#include "../xbps-repo/util.h"
void
int
xbps_autoremove_pkgs(void)
{
prop_array_t orphans;
@ -52,15 +52,18 @@ xbps_autoremove_pkgs(void)
orphans = xbps_find_orphan_packages();
if (orphans == NULL)
exit(EXIT_FAILURE);
return errno;
if (orphans != NULL && prop_array_count(orphans) == 0) {
printf("There are not orphaned packages currently.\n");
exit(EXIT_SUCCESS);
goto out;
}
iter = prop_array_iterator(orphans);
if (iter == NULL)
if (iter == NULL) {
rv = errno;
goto out;
}
printf("The following packages were installed automatically\n"
"(as dependencies) and aren't needed anymore:\n\n");
@ -99,13 +102,11 @@ out2:
prop_object_iterator_release(iter);
out:
prop_object_release(orphans);
if (rv != 0)
exit(EXIT_FAILURE);
exit(EXIT_SUCCESS);
return rv;
}
void
int
xbps_remove_installed_pkg(const char *pkgname, bool force)
{
prop_array_t reqby;
@ -119,7 +120,7 @@ xbps_remove_installed_pkg(const char *pkgname, bool force)
dict = xbps_find_pkg_installed_from_plist(pkgname);
if (dict == NULL) {
printf("Package %s is not installed.\n", pkgname);
goto out;
return 0;
}
prop_dictionary_get_cstring_nocopy(dict, "version", &version);
@ -133,7 +134,7 @@ xbps_remove_installed_pkg(const char *pkgname, bool force)
if (!force) {
if (!xbps_noyes("Do you want to remove %s?", pkgname)) {
printf("Cancelling!\n");
goto out;
return 0;
}
}
printf("Forcing %s-%s for deletion!\n", pkgname, version);
@ -141,7 +142,7 @@ xbps_remove_installed_pkg(const char *pkgname, bool force)
if (!force) {
if (!xbps_noyes("Do you want to remove %s?", pkgname)) {
printf("Cancelling!\n");
goto out;
return 0;
}
}
}
@ -150,13 +151,8 @@ xbps_remove_installed_pkg(const char *pkgname, bool force)
if ((rv = xbps_remove_pkg(pkgname, version, false)) != 0) {
printf("Unable to remove %s-%s (%s).\n",
pkgname, version, strerror(errno));
goto out;
return rv;
}
out:
xbps_release_regpkgdb_dict();
if (rv != 0)
exit(EXIT_FAILURE);
exit(EXIT_SUCCESS);
return 0;
}