2012-11-02 19:34:25 +05:30
|
|
|
/*-
|
2014-02-07 08:20:30 +05:30
|
|
|
* Copyright (c) 2008-2014 Juan Romero Pardines.
|
2012-11-02 19:34:25 +05:30
|
|
|
* 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 <signal.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <getopt.h>
|
2012-11-03 14:17:28 +05:30
|
|
|
#include <syslog.h>
|
2012-11-02 19:34:25 +05:30
|
|
|
|
2013-06-20 16:01:02 +05:30
|
|
|
#include <xbps.h>
|
2012-11-02 19:34:25 +05:30
|
|
|
#include "../xbps-install/defs.h"
|
2014-05-06 15:47:18 +05:30
|
|
|
#include "defs.h"
|
2012-11-02 19:34:25 +05:30
|
|
|
|
|
|
|
static void __attribute__((noreturn))
|
|
|
|
usage(bool fail)
|
|
|
|
{
|
|
|
|
fprintf(stdout,
|
|
|
|
"Usage: xbps-remove [OPTIONS] [PKGNAME...]\n\n"
|
|
|
|
"OPTIONS\n"
|
|
|
|
" -C --config <file> Full path to configuration file\n"
|
|
|
|
" -c --cachedir <dir> Full path to cachedir\n"
|
|
|
|
" -d --debug Debug mode shown to stderr\n"
|
2012-11-21 10:30:55 +05:30
|
|
|
" -F --force-revdeps Force package removal even with revdeps\n"
|
2012-11-02 19:34:25 +05:30
|
|
|
" -f --force Force package files removal\n"
|
|
|
|
" -h --help Print help usage\n"
|
|
|
|
" -n --dry-run Dry-run mode\n"
|
|
|
|
" -O --clean-cache Remove obsolete packages in cachedir\n"
|
|
|
|
" -o --remove-orphans Remove package orphans\n"
|
|
|
|
" -R --recursive Recursively remove dependencies\n"
|
|
|
|
" -r --rootdir <dir> Full path to rootdir\n"
|
|
|
|
" -v --verbose Verbose messages\n"
|
|
|
|
" -y --yes Assume yes to all questions\n"
|
|
|
|
" -V --version Show XBPS version\n");
|
|
|
|
exit(fail ? EXIT_FAILURE : EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2013-10-05 15:08:04 +05:30
|
|
|
static int
|
2013-08-29 14:15:30 +05:30
|
|
|
state_cb_rm(struct xbps_state_cb_data *xscd, void *cbdata _unused)
|
2012-11-03 14:17:28 +05:30
|
|
|
{
|
2014-06-04 13:09:02 +05:30
|
|
|
bool slog = false;
|
|
|
|
|
|
|
|
if ((xscd->xhp->flags & XBPS_FLAG_DISABLE_SYSLOG) == 0) {
|
|
|
|
slog = true;
|
2012-11-03 14:17:28 +05:30
|
|
|
openlog("xbps-remove", LOG_CONS, LOG_USER);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (xscd->state) {
|
|
|
|
/* notifications */
|
|
|
|
case XBPS_STATE_REMOVE:
|
2013-03-05 08:38:42 +05:30
|
|
|
printf("Removing `%s' ...\n", xscd->arg);
|
2012-11-03 14:17:28 +05:30
|
|
|
break;
|
|
|
|
/* success */
|
|
|
|
case XBPS_STATE_REMOVE_FILE:
|
|
|
|
case XBPS_STATE_REMOVE_FILE_OBSOLETE:
|
2012-11-30 11:41:51 +05:30
|
|
|
if (xscd->xhp->flags & XBPS_FLAG_VERBOSE)
|
2012-11-03 14:17:28 +05:30
|
|
|
printf("%s\n", xscd->desc);
|
|
|
|
break;
|
|
|
|
case XBPS_STATE_REMOVE_DONE:
|
2013-03-05 08:38:42 +05:30
|
|
|
printf("Removed `%s' successfully.\n", xscd->arg);
|
2014-06-04 13:09:02 +05:30
|
|
|
if (slog) {
|
2013-03-05 08:38:42 +05:30
|
|
|
syslog(LOG_NOTICE, "Removed `%s' successfully "
|
|
|
|
"(rootdir: %s).", xscd->arg,
|
2012-11-30 11:41:51 +05:30
|
|
|
xscd->xhp->rootdir);
|
2014-02-25 21:12:52 +05:30
|
|
|
}
|
2012-11-03 14:17:28 +05:30
|
|
|
break;
|
2014-07-27 14:54:49 +05:30
|
|
|
case XBPS_STATE_SHOW_REMOVE_MSG:
|
|
|
|
printf("%s: pre-remove message:\n%s", xscd->arg, xscd->desc);
|
|
|
|
break;
|
2012-11-03 14:17:28 +05:30
|
|
|
/* errors */
|
|
|
|
case XBPS_STATE_REMOVE_FAIL:
|
|
|
|
xbps_error_printf("%s\n", xscd->desc);
|
2014-06-04 13:09:02 +05:30
|
|
|
if (slog) {
|
2012-11-03 14:17:28 +05:30
|
|
|
syslog(LOG_ERR, "%s", xscd->desc);
|
2014-02-25 21:12:52 +05:30
|
|
|
}
|
2012-11-03 14:17:28 +05:30
|
|
|
break;
|
|
|
|
case XBPS_STATE_REMOVE_FILE_FAIL:
|
|
|
|
case XBPS_STATE_REMOVE_FILE_HASH_FAIL:
|
|
|
|
case XBPS_STATE_REMOVE_FILE_OBSOLETE_FAIL:
|
|
|
|
/* Ignore errors due to not empty directories */
|
|
|
|
if (xscd->err == ENOTEMPTY)
|
2013-10-05 15:08:04 +05:30
|
|
|
return 0;
|
2012-11-03 14:17:28 +05:30
|
|
|
|
|
|
|
xbps_error_printf("%s\n", xscd->desc);
|
2014-06-04 13:09:02 +05:30
|
|
|
if (slog) {
|
2012-11-03 14:17:28 +05:30
|
|
|
syslog(LOG_ERR, "%s", xscd->desc);
|
2014-02-25 21:12:52 +05:30
|
|
|
}
|
2012-11-03 14:17:28 +05:30
|
|
|
break;
|
|
|
|
default:
|
2012-11-30 11:41:51 +05:30
|
|
|
xbps_dbg_printf(xscd->xhp,
|
2013-03-05 08:38:42 +05:30
|
|
|
"%s: unknown state %d\n", xscd->arg, xscd->state);
|
2012-11-03 14:17:28 +05:30
|
|
|
break;
|
|
|
|
}
|
2013-10-05 15:08:04 +05:30
|
|
|
|
|
|
|
return 0;
|
2012-11-03 14:17:28 +05:30
|
|
|
}
|
|
|
|
|
2012-11-02 19:34:25 +05:30
|
|
|
static int
|
2013-06-12 13:34:10 +05:30
|
|
|
remove_pkg(struct xbps_handle *xhp, const char *pkgname, int cols,
|
2012-11-02 19:34:25 +05:30
|
|
|
bool recursive)
|
|
|
|
{
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_array_t reqby;
|
2012-11-02 19:34:25 +05:30
|
|
|
const char *pkgver;
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
rv = xbps_transaction_remove_pkg(xhp, pkgname, recursive);
|
|
|
|
if (rv == EEXIST) {
|
|
|
|
/* pkg has revdeps */
|
2012-11-30 22:10:52 +05:30
|
|
|
reqby = xbps_pkgdb_get_pkg_revdeps(xhp, pkgname);
|
2012-11-02 19:34:25 +05:30
|
|
|
printf("WARNING: %s IS REQUIRED BY %u PACKAGE%s:\n\n",
|
2013-06-20 13:56:12 +05:30
|
|
|
pkgname, xbps_array_count(reqby),
|
|
|
|
xbps_array_count(reqby) > 1 ? "S" : "");
|
2013-09-15 13:36:49 +05:30
|
|
|
for (unsigned int x = 0; x < xbps_array_count(reqby); x++) {
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_array_get_cstring_nocopy(reqby, x, &pkgver);
|
2012-11-02 19:34:25 +05:30
|
|
|
print_package_line(pkgver, cols, false);
|
|
|
|
}
|
|
|
|
printf("\n\n");
|
|
|
|
print_package_line(NULL, cols, true);
|
|
|
|
return rv;
|
|
|
|
} else if (rv == ENOENT) {
|
|
|
|
printf("Package `%s' is not currently installed.\n", pkgname);
|
|
|
|
return 0;
|
|
|
|
} else if (rv != 0) {
|
|
|
|
xbps_error_printf("Failed to queue `%s' for removing: %s\n",
|
|
|
|
pkgname, strerror(rv));
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2012-11-21 10:30:55 +05:30
|
|
|
const char *shortopts = "C:c:dFfhnOoRr:vVy";
|
2012-11-05 14:21:34 +05:30
|
|
|
const struct option longopts[] = {
|
2012-11-02 19:34:25 +05:30
|
|
|
{ "config", required_argument, NULL, 'C' },
|
|
|
|
{ "cachedir", required_argument, NULL, 'c' },
|
|
|
|
{ "debug", no_argument, NULL, 'd' },
|
2012-11-21 10:30:55 +05:30
|
|
|
{ "force-revdeps", no_argument, NULL, 'F' },
|
2012-11-02 19:34:25 +05:30
|
|
|
{ "force", no_argument, NULL, 'f' },
|
|
|
|
{ "help", no_argument, NULL, 'h' },
|
|
|
|
{ "dry-run", no_argument, NULL, 'n' },
|
|
|
|
{ "clean-cache", no_argument, NULL, 'O' },
|
|
|
|
{ "remove-orphans", no_argument, NULL, 'o' },
|
|
|
|
{ "recursive", no_argument, NULL, 'R' },
|
|
|
|
{ "rootdir", required_argument, NULL, 'r' },
|
|
|
|
{ "verbose", no_argument, NULL, 'v' },
|
|
|
|
{ "version", no_argument, NULL, 'V' },
|
|
|
|
{ "yes", no_argument, NULL, 'y' },
|
|
|
|
{ NULL, 0, NULL, 0 }
|
|
|
|
};
|
2012-12-14 16:28:30 +05:30
|
|
|
struct xbps_handle xh;
|
2012-11-21 10:30:55 +05:30
|
|
|
const char *rootdir, *cachedir, *conffile;
|
2013-09-15 13:36:49 +05:30
|
|
|
int c, flags, rv;
|
2012-11-02 19:34:25 +05:30
|
|
|
bool yes, drun, recursive, ignore_revdeps, clean_cache;
|
|
|
|
bool orphans, reqby_force;
|
2013-06-12 13:34:10 +05:30
|
|
|
int maxcols;
|
2012-11-02 19:34:25 +05:30
|
|
|
|
2012-11-21 10:30:55 +05:30
|
|
|
rootdir = cachedir = conffile = NULL;
|
2012-11-02 19:34:25 +05:30
|
|
|
flags = rv = 0;
|
|
|
|
drun = recursive = ignore_revdeps = clean_cache = false;
|
|
|
|
reqby_force = yes = orphans = false;
|
|
|
|
|
|
|
|
while ((c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) {
|
|
|
|
switch (c) {
|
|
|
|
case 'C':
|
|
|
|
conffile = optarg;
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
cachedir = optarg;
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
flags |= XBPS_FLAG_DEBUG;
|
|
|
|
break;
|
2012-11-21 10:30:55 +05:30
|
|
|
case 'F':
|
|
|
|
ignore_revdeps = true;
|
|
|
|
break;
|
2012-11-02 19:34:25 +05:30
|
|
|
case 'f':
|
|
|
|
flags |= XBPS_FLAG_FORCE_REMOVE_FILES;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
usage(false);
|
|
|
|
/* NOTREACHED */
|
|
|
|
case 'n':
|
|
|
|
drun = true;
|
|
|
|
break;
|
|
|
|
case 'O':
|
|
|
|
clean_cache = true;
|
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
orphans = true;
|
|
|
|
break;
|
|
|
|
case 'R':
|
|
|
|
recursive = true;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
rootdir = optarg;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
flags |= XBPS_FLAG_VERBOSE;
|
|
|
|
break;
|
|
|
|
case 'V':
|
|
|
|
printf("%s\n", XBPS_RELVER);
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
case 'y':
|
|
|
|
yes = true;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage(true);
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!clean_cache && !orphans && (argc == optind))
|
|
|
|
usage(true);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize libxbps.
|
|
|
|
*/
|
|
|
|
memset(&xh, 0, sizeof(xh));
|
2012-11-03 14:17:28 +05:30
|
|
|
xh.state_cb = state_cb_rm;
|
2013-12-16 16:16:39 +05:30
|
|
|
if (rootdir)
|
|
|
|
strncpy(xh.rootdir, rootdir, sizeof(xh.rootdir));
|
|
|
|
if (cachedir)
|
|
|
|
strncpy(xh.cachedir, cachedir, sizeof(xh.cachedir));
|
2014-08-01 18:39:51 +05:30
|
|
|
if (conffile)
|
|
|
|
strncpy(xh.conffile, conffile, sizeof(xh.conffile));
|
|
|
|
|
2012-11-02 19:34:25 +05:30
|
|
|
xh.flags = flags;
|
|
|
|
|
|
|
|
if ((rv = xbps_init(&xh)) != 0) {
|
|
|
|
xbps_error_printf("Failed to initialize libxbps: %s\n",
|
|
|
|
strerror(rv));
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
maxcols = get_maxcols();
|
|
|
|
|
|
|
|
if (clean_cache) {
|
2014-05-06 15:47:18 +05:30
|
|
|
rv = clean_cachedir(&xh);
|
2012-11-02 19:34:25 +05:30
|
|
|
if (rv != 0)
|
2012-12-14 16:28:30 +05:30
|
|
|
exit(rv);;
|
2012-11-02 19:34:25 +05:30
|
|
|
}
|
|
|
|
|
2014-05-06 19:55:52 +05:30
|
|
|
if (!drun && (rv = xbps_pkgdb_lock(&xh)) != 0) {
|
2014-02-23 12:53:14 +05:30
|
|
|
fprintf(stderr, "failed to lock pkgdb: %s\n", strerror(rv));
|
|
|
|
exit(rv);
|
|
|
|
}
|
|
|
|
|
2012-11-02 19:34:25 +05:30
|
|
|
if (orphans) {
|
|
|
|
if ((rv = xbps_transaction_autoremove_pkgs(&xh)) != 0) {
|
2014-02-23 12:53:14 +05:30
|
|
|
xbps_pkgdb_unlock(&xh);
|
2012-11-02 19:34:25 +05:30
|
|
|
if (rv != ENOENT) {
|
2012-11-06 19:16:43 +05:30
|
|
|
fprintf(stderr, "Failed to queue package "
|
2012-11-02 19:34:25 +05:30
|
|
|
"orphans: %s\n", strerror(rv));
|
2012-12-14 16:28:30 +05:30
|
|
|
exit(EXIT_FAILURE);
|
2012-11-02 19:34:25 +05:30
|
|
|
}
|
2012-12-14 16:28:30 +05:30
|
|
|
exit(EXIT_SUCCESS);
|
2012-11-02 19:34:25 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-15 13:36:49 +05:30
|
|
|
for (int i = optind; i < argc; i++) {
|
2012-11-02 19:34:25 +05:30
|
|
|
rv = remove_pkg(&xh, argv[i], maxcols, recursive);
|
|
|
|
if (rv == 0)
|
|
|
|
continue;
|
2014-02-23 15:01:36 +05:30
|
|
|
else if (rv != EEXIST) {
|
2014-05-06 19:55:52 +05:30
|
|
|
if (!drun) {
|
|
|
|
xbps_pkgdb_unlock(&xh);
|
|
|
|
}
|
2012-12-14 16:28:30 +05:30
|
|
|
exit(rv);
|
2014-02-23 15:01:36 +05:30
|
|
|
} else {
|
2012-11-02 19:34:25 +05:30
|
|
|
reqby_force = true;
|
2014-02-23 15:01:36 +05:30
|
|
|
}
|
2012-11-02 19:34:25 +05:30
|
|
|
}
|
2014-05-06 19:55:52 +05:30
|
|
|
if (reqby_force && !ignore_revdeps && !drun) {
|
2014-02-23 12:53:14 +05:30
|
|
|
xbps_pkgdb_unlock(&xh);
|
2012-12-14 16:28:30 +05:30
|
|
|
exit(EXIT_FAILURE);
|
2014-02-23 12:53:14 +05:30
|
|
|
}
|
|
|
|
if (orphans || (argc > optind)) {
|
2012-11-02 19:34:25 +05:30
|
|
|
rv = exec_transaction(&xh, maxcols, yes, drun);
|
2014-02-23 12:53:14 +05:30
|
|
|
}
|
2014-05-06 19:55:52 +05:30
|
|
|
if (!drun) {
|
|
|
|
xbps_pkgdb_unlock(&xh);
|
|
|
|
}
|
2012-11-02 19:34:25 +05:30
|
|
|
exit(rv);
|
|
|
|
}
|