From 679117e9a94993dfdb60f7617ab2a08eb291303a Mon Sep 17 00:00:00 2001 From: Juan RP Date: Thu, 9 Apr 2009 11:56:40 +0200 Subject: [PATCH] xbps-bin: move (auto)remove code into its own file, use xbps_noyes(). --HG-- extra : convert_revision : 20398840d20154d90c7457b7318c651c513c1f72 --- bin/xbps-bin/Makefile | 2 +- bin/xbps-bin/defs.h | 2 + bin/xbps-bin/install.c | 3 +- bin/xbps-bin/main.c | 117 ++---------------------------- bin/xbps-bin/remove.c | 158 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 167 insertions(+), 115 deletions(-) create mode 100644 bin/xbps-bin/remove.c diff --git a/bin/xbps-bin/Makefile b/bin/xbps-bin/Makefile index 0ce2ac00a1..b1cf168624 100644 --- a/bin/xbps-bin/Makefile +++ b/bin/xbps-bin/Makefile @@ -1,5 +1,5 @@ BIN = xbps-bin -OBJS = install.o main.o ../xbps-repo/util.o +OBJS = install.o main.o remove.o ../xbps-repo/util.o TOPDIR = ../.. include $(TOPDIR)/prog.mk diff --git a/bin/xbps-bin/defs.h b/bin/xbps-bin/defs.h index 865a4c9514..ef6fa8eb99 100644 --- a/bin/xbps-bin/defs.h +++ b/bin/xbps-bin/defs.h @@ -27,5 +27,7 @@ #define _XBPS_BIN_DEFS_H_ void xbps_install_pkg(const char *, bool); +void xbps_autoremove_pkgs(void); +void xbps_remove_pkg(const char *); #endif /* !_XBPS_BIN_DEFS_H_ */ diff --git a/bin/xbps-bin/install.c b/bin/xbps-bin/install.c index d962dd8edb..2d9c63dffd 100644 --- a/bin/xbps-bin/install.c +++ b/bin/xbps-bin/install.c @@ -172,12 +172,11 @@ xbps_install_pkg(const char *pkg, bool update) printf(" "); first = true; } - printf("%s-%s ", pkgname, version); } else { printf("\n "); cols = strlen(pkgname) + strlen(version) + 4; - printf("%s-%s ", pkgname, version); } + printf("%s-%s ", pkgname, version); } prop_object_iterator_reset(iter); printf("\n\n"); diff --git a/bin/xbps-bin/main.c b/bin/xbps-bin/main.c index 514f3a87f8..2c98455d94 100644 --- a/bin/xbps-bin/main.c +++ b/bin/xbps-bin/main.c @@ -97,13 +97,10 @@ int main(int argc, char **argv) { prop_dictionary_t dict; - prop_array_t reqby, orphans; - prop_object_t obj; - prop_object_iterator_t iter; - const char *pkgname, *version, *sep, *rootdir; + const char *rootdir; char *plist; int c, flags = 0, rv = 0; - bool chkhash = false, forcerm = false, verbose = false; + bool chkhash = false, force = false, verbose = false; while ((c = getopt(argc, argv, "Cfr:v")) != -1) { switch (c) { @@ -111,7 +108,7 @@ main(int argc, char **argv) chkhash = true; break; case 'f': - forcerm = true; + force = true; break; case 'r': /* To specify the root directory */ @@ -182,57 +179,7 @@ main(int argc, char **argv) if (argc != 2) usage(); - /* - * First check if package is required by other packages. - */ - dict = xbps_find_pkg_installed_from_plist(argv[1]); - if (dict == NULL) { - printf("Package %s is not installed.\n", argv[1]); - exit(EXIT_FAILURE); - } - prop_dictionary_get_cstring_nocopy(dict, "version", &version); - - reqby = prop_dictionary_get(dict, "requiredby"); - if (reqby != NULL && prop_array_count(reqby) > 0) { - printf("WARNING! %s-%s is required by the following " - "packages:\n", argv[1], version); - sep = "\t"; - (void)xbps_callback_array_iter_in_dict(dict, - "requiredby", list_strings_in_array, (void *)sep); - if (!forcerm) { - prop_object_release(dict); - printf("If you are sure about this, use " - "-f to force deletion for this package.\n"); - exit(EXIT_FAILURE); - } else - printf("Forcing %s-%s for deletion!\n", - argv[1], version); - } - - printf("Removing package %s-%s ... ", argv[1], version); - if (verbose) - printf("\n"); - - (void)fflush(stdout); - - rv = xbps_remove_binary_pkg(argv[1], false); - if (rv != 0) { - if (!verbose) - printf("failed! (%s)\n", strerror(rv)); - else - printf("Unable to remove %s-%s (%s).\n", - argv[1], version, strerror(errno)); - - prop_object_release(dict); - exit(EXIT_FAILURE); - } - if (!verbose) - printf("done.\n"); - else - printf("Package %s-%s removed successfully.\n", - argv[1], version); - - prop_object_release(dict); + xbps_remove_pkg(argv[1]); } else if (strcasecmp(argv[0], "show") == 0) { /* Shows info about an installed binary package. */ @@ -265,61 +212,7 @@ main(int argc, char **argv) if (argc != 1) usage(); - orphans = xbps_find_orphan_packages(); - if (orphans == NULL) - exit(EXIT_FAILURE); - if (orphans != NULL && prop_array_count(orphans) == 0) { - printf("There are not orphaned packages currently.\n"); - exit(EXIT_SUCCESS); - } - - iter = prop_array_iterator(orphans); - if (iter == NULL) - exit(EXIT_FAILURE); - - printf("The following packages were installed automatically\n" - "(as dependencies) and aren't needed anymore:\n"); - while ((obj = prop_object_iterator_next(iter)) != NULL) { - prop_dictionary_get_cstring_nocopy(obj, "pkgname", - &pkgname); - prop_dictionary_get_cstring_nocopy(obj, "version", - &version); - printf("\t%s-%s\n", pkgname, version); - } - if (!forcerm) { - printf("If you are really sure you don't need them, " - "use -f to confirm.\n"); - goto out; - } - - 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, "version", - &version); - printf("Removing package %s-%s ... ", - pkgname, version); - if (verbose) - printf("\n"); - - (void)fflush(stdout); - - rv = xbps_remove_binary_pkg(pkgname, false); - if (rv != 0) { - if (!verbose) - printf("failed! (%s)\n", strerror(rv)); - prop_object_iterator_release(iter); - prop_object_release(orphans); - exit(EXIT_FAILURE); - } - if (!verbose) - printf("done.\n"); - } -out: - prop_object_iterator_release(iter); - prop_object_release(orphans); + xbps_autoremove_pkgs(); } else { usage(); diff --git a/bin/xbps-bin/remove.c b/bin/xbps-bin/remove.c new file mode 100644 index 0000000000..133c297d54 --- /dev/null +++ b/bin/xbps-bin/remove.c @@ -0,0 +1,158 @@ +/*- + * Copyright (c) 2008-2009 Juan Romero Pardines. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include "defs.h" +#include "../xbps-repo/util.h" + +void +xbps_autoremove_pkgs(void) +{ + prop_array_t orphans; + prop_object_t obj; + prop_object_iterator_t iter; + const char *pkgname, *version; + size_t cols = 0; + int rv = 0; + bool first = false; + + /* + * Removes orphan pkgs. These packages were installed + * as dependency and any installed package does not depend + * on it currently. + */ + + orphans = xbps_find_orphan_packages(); + if (orphans == NULL) + exit(EXIT_FAILURE); + if (orphans != NULL && prop_array_count(orphans) == 0) { + printf("There are not orphaned packages currently.\n"); + exit(EXIT_SUCCESS); + } + + iter = prop_array_iterator(orphans); + if (iter == NULL) + exit(EXIT_FAILURE); + + printf("The following packages were installed automatically\n" + "(as dependencies) and aren't needed anymore:\n\n"); + while ((obj = prop_object_iterator_next(iter)) != NULL) { + prop_dictionary_get_cstring_nocopy(obj, "pkgname", &pkgname); + prop_dictionary_get_cstring_nocopy(obj, "version", &version); + cols += strlen(pkgname) + strlen(version) + 4; + if (cols <= 80) { + if (first == false) { + printf(" "); + first = true; + } + } else { + printf("\n "); + cols = strlen(pkgname) + strlen(version) + 4; + } + printf("%s-%s ", pkgname, version); + } + prop_object_iterator_reset(iter); + printf("\n\n"); + + if (xbps_noyes("Do you want to remove them?") == false) { + printf("Cancelled!\n"); + exit(EXIT_SUCCESS); + } + + while ((obj = prop_object_iterator_next(iter)) != NULL) { + prop_dictionary_get_cstring_nocopy(obj, "pkgname", &pkgname); + prop_dictionary_get_cstring_nocopy(obj, "version", &version); + + printf("Removing package %s-%s ...\n", pkgname, version); + if ((rv = xbps_remove_binary_pkg(pkgname, false)) != 0) { + prop_object_iterator_release(iter); + prop_object_release(orphans); + exit(EXIT_FAILURE); + } + } + prop_object_iterator_release(iter); + prop_object_release(orphans); + + exit(EXIT_SUCCESS); +} + +void +xbps_remove_pkg(const char *pkgname) +{ + prop_array_t reqby; + prop_dictionary_t dict; + const char *version; + int rv = 0; + + /* + * First check if package is required by other packages. + */ + dict = xbps_find_pkg_installed_from_plist(pkgname); + if (dict == NULL) { + printf("Package %s is not installed.\n", pkgname); + exit(EXIT_FAILURE); + } + prop_dictionary_get_cstring_nocopy(dict, "version", &version); + + reqby = prop_dictionary_get(dict, "requiredby"); + if (reqby != NULL && prop_array_count(reqby) > 0) { + printf("WARNING! %s-%s is required by the following " + "packages:\n", pkgname, version); + (void)xbps_callback_array_iter_in_dict(dict, + "requiredby", list_strings_in_array, NULL); + if (xbps_noyes("Do you want to remove %s?", pkgname) == false) { + printf("Cancelling!\n"); + exit(EXIT_SUCCESS); + } + printf("Forcing %s-%s for deletion!\n", pkgname, version); + } else { + if (xbps_noyes("Do you want to remove %s?", pkgname) == false) { + printf("Cancelling!\n"); + exit(EXIT_SUCCESS); + } + } + + printf("Removing package %s-%s ...\n", pkgname, version); + if ((rv = xbps_remove_binary_pkg(pkgname, false)) != 0) { + printf("Unable to remove %s-%s (%s).\n", + pkgname, version, strerror(errno)); + prop_object_release(dict); + exit(EXIT_FAILURE); + } + printf("Package %s-%s removed successfully.\n", pkgname, version); + prop_object_release(dict); + + exit(EXIT_SUCCESS); +}