xbps-bin: move (auto)remove code into its own file, use xbps_noyes().

--HG--
extra : convert_revision : 20398840d20154d90c7457b7318c651c513c1f72
This commit is contained in:
Juan RP 2009-04-09 11:56:40 +02:00
parent 3698644967
commit 679117e9a9
5 changed files with 167 additions and 115 deletions

View file

@ -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

View file

@ -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_ */

View file

@ -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");

View file

@ -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();

158
bin/xbps-bin/remove.c Normal file
View file

@ -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 <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <errno.h>
#include <limits.h>
#include <libgen.h>
#include <unistd.h>
#include <xbps_api.h>
#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);
}