diff options
author | joerg <joerg> | 2009-02-25 16:29:08 +0000 |
---|---|---|
committer | joerg <joerg> | 2009-02-25 16:29:08 +0000 |
commit | 908bb4953c8487f0defedaee394d55f59270825d (patch) | |
tree | bcb28aa54f2a9c0abb5c9103cb38fe0859ee377b /pkgtools | |
parent | 6128c10b8d3cabcd818718b0afeef4c74efbc498 (diff) | |
download | pkgsrc-908bb4953c8487f0defedaee394d55f59270825d.tar.gz |
pkg_install-20090225:
Rewrite pkg_delete to expand the list of packages to delete first and
reorder it if necessary. It will bail out if it knows in advance that it
can't remove a package. It will also fail for errors while removing one
package, unless forced. Add an option to remove automatically installed
packages that are no longer used.
The pkgviews support is kept, but untested. The error handling for
pkgviews most of all is as weak as before.
Basic review from hubertf@, man page changes by bad@.
Diffstat (limited to 'pkgtools')
-rw-r--r-- | pkgtools/pkg_install/files/delete/Makefile.in | 6 | ||||
-rw-r--r-- | pkgtools/pkg_install/files/delete/delete.h | 41 | ||||
-rw-r--r-- | pkgtools/pkg_install/files/delete/main.c | 229 | ||||
-rw-r--r-- | pkgtools/pkg_install/files/delete/perform.c | 873 | ||||
-rw-r--r-- | pkgtools/pkg_install/files/delete/pkg_delete.1 | 49 | ||||
-rw-r--r-- | pkgtools/pkg_install/files/delete/pkg_delete.c | 919 | ||||
-rw-r--r-- | pkgtools/pkg_install/files/delete/pkg_delete.cat1 | 35 | ||||
-rw-r--r-- | pkgtools/pkg_install/files/lib/version.h | 4 |
8 files changed, 973 insertions, 1183 deletions
diff --git a/pkgtools/pkg_install/files/delete/Makefile.in b/pkgtools/pkg_install/files/delete/Makefile.in index 795a06ff920..76239b03a17 100644 --- a/pkgtools/pkg_install/files/delete/Makefile.in +++ b/pkgtools/pkg_install/files/delete/Makefile.in @@ -1,4 +1,4 @@ -# $NetBSD: Makefile.in,v 1.12 2008/03/10 12:14:32 wiz Exp $ +# $NetBSD: Makefile.in,v 1.13 2009/02/25 16:29:08 joerg Exp $ srcdir= @srcdir@ @@ -14,7 +14,7 @@ cat1dir= $(mandir)/cat1 CC= @CC@ CCLD= $(CC) LIBS= -linstall @LIBS@ -CPPFLAGS= @CPPFLAGS@ -I. -I$(srcdir) -I../lib +CPPFLAGS= @CPPFLAGS@ -I. -I$(srcdir) -I../lib -DBINDIR=\"$(sbindir)\" DEFS= @DEFS@ CFLAGS= @CFLAGS@ LDFLAGS= @LDFLAGS@ -L../lib @@ -23,7 +23,7 @@ INSTALL= @INSTALL@ PROG= pkg_delete -OBJS= main.o perform.o +OBJS= pkg_delete.o all: $(PROG) diff --git a/pkgtools/pkg_install/files/delete/delete.h b/pkgtools/pkg_install/files/delete/delete.h deleted file mode 100644 index 02d3ea3dda6..00000000000 --- a/pkgtools/pkg_install/files/delete/delete.h +++ /dev/null @@ -1,41 +0,0 @@ -/* $NetBSD: delete.h,v 1.6 2009/02/02 12:35:01 joerg Exp $ */ - -/* from FreeBSD Id: delete.h,v 1.4 1997/02/22 16:09:35 peter Exp */ - -/* - * FreeBSD install - a package for the installation and maintainance - * of non-core utilities. - * - * 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. - * - * Jordan K. Hubbard - * 18 July 1993 - * - * Include and define various things wanted by the delete command. - * - */ - -#ifndef _INST_DELETE_H_INCLUDE -#define _INST_DELETE_H_INCLUDE - -extern char *Destdir; -extern char *Prefix; -extern char *ProgramPath; -extern Boolean NoDeleteFiles; -extern Boolean NoDeInstall; -extern Boolean CleanDirs; -extern Boolean Force; -extern Boolean Recurse_up; -extern Boolean Recurse_down; -extern lpkg_head_t pkgs; - -int pkg_perform(lpkg_head_t *); - -#endif /* _INST_DELETE_H_INCLUDE */ diff --git a/pkgtools/pkg_install/files/delete/main.c b/pkgtools/pkg_install/files/delete/main.c deleted file mode 100644 index 3ab5fb19c1a..00000000000 --- a/pkgtools/pkg_install/files/delete/main.c +++ /dev/null @@ -1,229 +0,0 @@ -/* $NetBSD: main.c,v 1.22 2009/02/02 12:35:01 joerg Exp $ */ - -#if HAVE_CONFIG_H -#include "config.h" -#endif -#include <nbcompat.h> -#if HAVE_SYS_CDEFS_H -#include <sys/cdefs.h> -#endif -__RCSID("$NetBSD: main.c,v 1.22 2009/02/02 12:35:01 joerg Exp $"); - -/* - * - * FreeBSD install - a package for the installation and maintainance - * of non-core utilities. - * - * 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. - * - * Jordan K. Hubbard - * 18 July 1993 - * - * This is the delete module. - * - */ - -#if HAVE_ERR_H -#include <err.h> -#endif -#include "lib.h" -#include "delete.h" - -static char Options[] = "DdFfhK:NnOp:P:RrVv"; - -char *Destdir = NULL; -char *Pkgdb = NULL; -char *Prefix = NULL; -char *ProgramPath = NULL; -Boolean NoDeleteFiles = FALSE; -Boolean NoDeInstall = FALSE; -Boolean CleanDirs = FALSE; -Boolean File2Pkg = FALSE; -Boolean Recurse_up = FALSE; -Boolean Recurse_down = FALSE; -Boolean OnlyDeleteFromPkgDB = FALSE; -lpkg_head_t pkgs; - -static void -usage(void) -{ - fprintf(stderr, "usage: pkg_delete [-DdFfNnORrVv] [-K pkg_dbdir] [-P destdir] [-p prefix] pkg-name ...\n"); - exit(1); -} - -int -main(int argc, char **argv) -{ - lpkg_t *lpp; - int ex; - int ch; - - setprogname(argv[0]); - - ProgramPath = argv[0]; - - while ((ch = getopt(argc, argv, Options)) != -1) - switch (ch) { - case 'D': - NoDeInstall = TRUE; - break; - - case 'd': - CleanDirs = TRUE; - break; - - case 'F': - File2Pkg = TRUE; - break; - - case 'f': - Force += 1; - break; - - case 'K': - Pkgdb = xstrdup(optarg); - break; - - case 'N': - NoDeleteFiles = TRUE; - NoDeInstall = TRUE; - break; - - case 'n': - Fake = TRUE; - Verbose = TRUE; - break; - - case 'O': - OnlyDeleteFromPkgDB = TRUE; - break; - - case 'P': - Destdir = optarg; - break; - - case 'p': - Prefix = optarg; - break; - - case 'R': - Recurse_down = TRUE; - break; - - case 'r': - Recurse_up = TRUE; - break; - - case 'V': - show_version(); - /* NOTREACHED */ - - case 'v': - Verbose = TRUE; - break; - - case 'h': - case '?': - default: - usage(); - break; - } - - argc -= optind; - argv += optind; - - TAILQ_INIT(&pkgs); - - if (Pkgdb == NULL) - Pkgdb = xstrdup(_pkgdb_getPKGDB_DIR()); - - if (Destdir != NULL) { - char *pkgdbdir; - - pkgdbdir = xasprintf("%s/%s", Destdir, Pkgdb); - _pkgdb_setPKGDB_DIR(pkgdbdir); - free(pkgdbdir); - } else { - _pkgdb_setPKGDB_DIR(Pkgdb); - } - - /* Get all the remaining package names, if any */ - if (File2Pkg && !pkgdb_open(ReadOnly)) { - err(EXIT_FAILURE, "cannot open pkgdb"); - } - - /* Get all the remaining package names, if any */ - for ( ; *argv ; argv++) { - /* pkgdb: if -F flag given, don't add pkgnames to pkgs but - * rather resolve the given filenames to pkgnames using - * pkgdb_retrieve, then add these. */ - if (File2Pkg) { - char *s; - - if ((s = pkgdb_retrieve(*argv)) == NULL) { - errx(EXIT_FAILURE, "No matching pkg for %s in pkgdb.", *argv); - } - lpp = alloc_lpkg(s); - TAILQ_INSERT_TAIL(&pkgs, lpp, lp_link); - } else if (ispkgpattern(*argv)) { - switch (add_installed_pkgs_by_pattern(*argv, &pkgs)) { - case 0: - errx(EXIT_FAILURE, "No matching pkg for %s.", *argv); - case -1: - errx(EXIT_FAILURE, "error expanding '%s' ('%s' nonexistent?)", *argv, _pkgdb_getPKGDB_DIR()); - } - } else { - if (**argv == '/' && strncmp(*argv, Pkgdb, strlen(Pkgdb)) == 0) { - *argv += strlen(Pkgdb) + 1; - if ((*argv)[strlen(*argv) - 1] == '/') { - (*argv)[strlen(*argv) - 1] = 0; - } - } - lpp = alloc_lpkg(*argv); - TAILQ_INSERT_TAIL(&pkgs, lpp, lp_link); - } - } - - if (File2Pkg) { - pkgdb_close(); - } - - /* If no packages, yelp */ - if (TAILQ_FIRST(&pkgs) == NULL) { - warnx("missing package name(s)"); - usage(); - } - - if (OnlyDeleteFromPkgDB) { - /* Only delete the given packages' files from pkgdb, do not - * touch the pkg itself. Used by "make reinstall" in - * bsd.pkg.mk */ - char cachename[MaxPathSize]; - - if (!pkgdb_open(ReadWrite)) { - err(EXIT_FAILURE, "cannot open %s", _pkgdb_getPKGDB_FILE(cachename, sizeof(cachename))); - } - ex = EXIT_SUCCESS; - for (lpp = TAILQ_FIRST(&pkgs); lpp ; lpp = TAILQ_NEXT(lpp, lp_link)) { - if (!pkgdb_remove_pkg(lpp->lp_name)) { - ex = EXIT_FAILURE; - } - } - pkgdb_close(); - return ex; - } - if ((ex = pkg_perform(&pkgs)) != 0) { - if (Verbose) { - warnx("%d package deletion(s) failed", ex); - } - return EXIT_FAILURE; - } - return EXIT_SUCCESS; -} diff --git a/pkgtools/pkg_install/files/delete/perform.c b/pkgtools/pkg_install/files/delete/perform.c deleted file mode 100644 index 626b9ee2dfd..00000000000 --- a/pkgtools/pkg_install/files/delete/perform.c +++ /dev/null @@ -1,873 +0,0 @@ -/* $NetBSD: perform.c,v 1.26 2009/02/12 22:23:06 joerg Exp $ */ - -#if HAVE_CONFIG_H -#include "config.h" -#endif -#include <nbcompat.h> -#if HAVE_SYS_CDEFS_H -#include <sys/cdefs.h> -#endif -__RCSID("$NetBSD: perform.c,v 1.26 2009/02/12 22:23:06 joerg Exp $"); - -/* - * FreeBSD install - a package for the installation and maintainance - * of non-core utilities. - * - * 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. - * - * Jordan K. Hubbard - * 18 July 1993 - * - * This is the main body of the delete module. - * - */ -/* - * Copyright (c) 1999 Christian E. Hopps - * 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission - * - * 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. - * - * Added the require find and require delete code - */ - -#if HAVE_ERR_H -#include <err.h> -#endif -#if HAVE_FCNTL_H -#include <fcntl.h> -#endif -#include "lib.h" -#include "delete.h" - - -/* In which direction to search in require_find() */ -typedef enum { - FIND_UP, FIND_DOWN -} rec_find_t; - -static int require_find_recursive_up(lpkg_t *); -static int require_find_recursive_down(lpkg_t *, package_t *); -static int require_find(char *, rec_find_t); -static int require_delete(int); -static void require_print(void); -static int undepend(const char *, void *); - -static char linebuf[MaxPathSize]; - -static package_t Plist; - -static lpkg_head_t lpfindq; -static lpkg_head_t lpdelq; - -static void -sanity_check(const char *pkg) -{ - char *fname; - - fname = pkgdb_pkg_file(pkg, CONTENTS_FNAME); - - if (!fexists(fname)) { - cleanup(0); - errx(2, "installed package %s has no %s file!", - pkg, CONTENTS_FNAME); - } - - free(fname); -} - -void -cleanup(int sig) -{ - /* Nothing to do */ - if (sig) /* in case this is ever used as a signal handler */ - exit(1); -} - -/* - * deppkgname is the pkg from which's +REQUIRED_BY file we are - * about to remove pkg2delname. This function is called from - * match_installed_pkgs(), deppkgname is expanded from a (possible) pattern. - */ -static int -undepend(const char *deppkgname, void *vp) -{ - char *fname, *fname_tmp; - char fbuf[MaxPathSize]; - const char *pkg2delname = vp; - FILE *fp, *fpwr; - - fname = pkgdb_pkg_file(deppkgname, REQUIRED_BY_FNAME); - fname_tmp = pkgdb_pkg_file(deppkgname, REQUIRED_BY_FNAME_TMP); - - if ((fp = fopen(fname, "r")) == NULL) { - warnx("couldn't open dependency file `%s'", fname); - free(fname); - free(fname_tmp); - return 0; - } - if ((fpwr = fopen(fname_tmp, "w")) == NULL) { - fclose(fp); - warnx("couldn't open temporary file `%s'", fname_tmp); - free(fname); - free(fname_tmp); - return 0; - } - while (fgets(fbuf, sizeof(fbuf), fp) != NULL) { - if (fbuf[strlen(fbuf) - 1] == '\n') - fbuf[strlen(fbuf) - 1] = '\0'; - if (strcmp(fbuf, pkg2delname)) /* no match */ - fputs(fbuf, fpwr), putc('\n', fpwr); - } - (void) fclose(fp); - if (fclose(fpwr) == EOF) { - warnx("error closing temporary file `%s'", fname_tmp); - remove(fname_tmp); - free(fname); - free(fname_tmp); - return 0; - } - if (rename(fname_tmp, fname) == -1) - warn("error renaming `%s' to `%s'", fname_tmp, fname); - remove(fname_tmp); /* just in case */ - - free(fname); - free(fname_tmp); - - return 0; -} - -/* - * Remove the current view's package dbdir from the +VIEWS file of the - * depoted package named by pkgname. - */ -static int -unview(const char *pkgname) -{ - const char *dbdir; - char *fname, *fname_tmp; - char fbuf[MaxPathSize]; - FILE *fp, *fpwr; - int rv; - int cc; - - dbdir = _pkgdb_getPKGDB_DIR(); - - fname = pkgdb_pkg_file(pkgname, DEPOT_FNAME); - if ((fp = fopen(fname, "r")) == NULL) { - warnx("unable to open `%s' file", fname); - return -1; - } - if (fgets(fbuf, sizeof(fbuf), fp) == NULL) { - (void) fclose(fp); - warnx("empty depot file `%s'", fname); - free(fname); - return -1; - } - if (fbuf[cc = strlen(fbuf) - 1] == '\n') { - fbuf[cc] = 0; - } - fclose(fp); - free(fname); - - /* - * Copy the contents of the +VIEWS file into a temp file, but - * skip copying the name of the current view's package dbdir. - */ - fname = pkgdb_pkg_file(pkgname, VIEWS_FNAME); - fname_tmp = pkgdb_pkg_file(pkgname, VIEWS_FNAME_TMP); - if ((fp = fopen(fname, "r")) == NULL) { - warnx("unable to open `%s' file", fname); - free(fname); - free(fname_tmp); - return -1; - } - if ((fpwr = fopen(fname_tmp, "w")) == NULL) { - (void) fclose(fp); - warnx("unable to fopen `%s' temporary file", fname_tmp); - free(fname); - free(fname_tmp); - return -1; - } - while (fgets(fbuf, sizeof(fbuf), fp) != NULL) { - if (fbuf[cc = strlen(fbuf) - 1] == '\n') { - fbuf[cc] = 0; - } - if (strcmp(fbuf, dbdir) != 0) { - (void) fputs(fbuf, fpwr); - (void) putc('\n', fpwr); - } - } - (void) fclose(fp); - - if (fclose(fpwr) == EOF) { - remove(fname_tmp); - warnx("unable to close `%s' temp file", fname_tmp); - free(fname); - free(fname_tmp); - return -1; - } - - /* Rename the temp file to the +VIEWS file */ - if ((rv = rename(fname_tmp, fname)) == -1) - warnx("unable to rename `%s' to `%s'", fname_tmp, fname); - - remove(fname_tmp); - free(fname); - free(fname_tmp); - - return rv; -} - -/* - * Delete from directory 'home' all packages on lpkg_list. - * If tryall is set, ignore errors from pkg_delete(1). - */ -static int -require_delete(int tryall) -{ - char *best_installed; - lpkg_t *lpp; - int rv, fail; - - best_installed = NULL; - - /* walk list of things to delete */ - fail = 0; - lpp = TAILQ_FIRST(&lpdelq); - for (; lpp; lpp = TAILQ_NEXT(lpp, lp_link)) { - free(best_installed); - best_installed = NULL; - - /* look to see if package was already deleted */ - best_installed = find_best_matching_installed_pkg(lpp->lp_name); - if (best_installed == NULL) { - warnx("%s appears to have been deleted", lpp->lp_name); - continue; - } - - if (Verbose) - printf("deinstalling %s\n", best_installed); - - /* delete the package */ - if (Fake) - rv = 0; - else - rv = fexec_skipempty(ProgramPath, "-K", - _pkgdb_getPKGDB_DIR(), - Prefix ? "-p" : "", Prefix ? Prefix : "", - Verbose ? "-v" : "", - (Force > 1) ? "-f -f" : (Force == 1) ? "-f" : "", - NoDeInstall ? "-D" : "", - NoDeleteFiles ? "-N" : "", - CleanDirs ? "-d" : "", - Fake ? "-n" : "", - best_installed, NULL); - - /* check for delete failure */ - if (rv && !tryall) { - fail = 1; - warnx("had problem removing %s%s", best_installed, - Force ? ", continuing" : ""); - if (!Force) - break; - } - } - - free(best_installed); - - /* cleanup list */ - while ((lpp = TAILQ_FIRST(&lpdelq))) { - TAILQ_REMOVE(&lpdelq, lpp, lp_link); - free_lpkg(lpp); - } - - return (fail); -} - -/* - * Recursively find all packages "up" the tree (follow +REQUIRED_BY). - * Return 1 on errors - */ -int -require_find_recursive_up(lpkg_t *thislpp) -{ - char *fname; - lpkg_head_t reqq; - lpkg_t *lpp = NULL; - FILE *cfile; - char *nl; - - /* see if we are on the find queue -- circular dependency */ - if ((lpp = find_on_queue(&lpfindq, thislpp->lp_name))) { - warnx("circular dependency found for pkg %s", lpp->lp_name); - return (1); - } - - TAILQ_INIT(&reqq); - - fname = pkgdb_pkg_file(thislpp->lp_name, REQUIRED_BY_FNAME); - - /* terminate recursion if no required by's */ - if (isemptyfile(fname)) { - free(fname); - return (0); - } - - /* get packages that directly require us */ - cfile = fopen(fname, "r"); - if (!cfile) { - warnx("cannot open requirements file `%s'", fname); - free(fname); - return (1); - } - free(fname); - - while (fgets(linebuf, sizeof(linebuf), cfile)) { - if ((nl = strrchr(linebuf, '\n'))) - *nl = 0; - lpp = alloc_lpkg(linebuf); - TAILQ_INSERT_TAIL(&reqq, lpp, lp_link); - } - fclose(cfile); - - /* put ourselves on the top of the find queue */ - TAILQ_INSERT_HEAD(&lpfindq, thislpp, lp_link); - - while ((lpp = TAILQ_FIRST(&reqq))) { - /* remove a direct req from our queue */ - TAILQ_REMOVE(&reqq, lpp, lp_link); - - /* find direct required requires */ - if (require_find_recursive_up(lpp)) - goto fail; - - /* all requires taken care of, add to tail of delete queue - * if not already there */ - if (find_on_queue(&lpdelq, lpp->lp_name)) - free_lpkg(lpp); - else - TAILQ_INSERT_TAIL(&lpdelq, lpp, lp_link); - } - - /* take ourselves off the find queue */ - TAILQ_REMOVE(&lpfindq, thislpp, lp_link); - - return (0); - -fail: - while ((lpp = TAILQ_FIRST(&reqq))) { - TAILQ_REMOVE(&reqq, lpp, lp_link); - free_lpkg(lpp); - } - return (1); -} - -/* - * Recursively find all packages "down" the tree (follow @pkgdep). - * Return 1 on errors - */ -int -require_find_recursive_down(lpkg_t *thislpp, package_t *plist) -{ - plist_t *p; - lpkg_t *lpp, *lpp2; - lpkg_head_t reqq; - int rc, fail = 0; - - /* see if we are on the find queue -- circular dependency */ - if ((lpp = find_on_queue(&lpfindq, thislpp->lp_name))) { - warnx("circular dependency found for pkg %s", lpp->lp_name); - return (1); - } - - TAILQ_INIT(&reqq); - - /* width-first scan */ - /* first enqueue all @pkgdep's to lpdelq, then (further below) - * go in recursively */ - for (p = plist->head; p; p = p->next) { - switch (p->type) { - case PLIST_PKGDEP: - lpp = alloc_lpkg(p->name); - TAILQ_INSERT_TAIL(&reqq, lpp, lp_link); - - lpp2 = find_on_queue(&lpdelq, p->name); - if (lpp2) { - TAILQ_REMOVE(&lpdelq, lpp2, lp_link); - free_lpkg(lpp2); - } - lpp = alloc_lpkg(p->name); - TAILQ_INSERT_TAIL(&lpdelq, lpp, lp_link); - - break; - default: - break; - } - } - - while ((lpp = TAILQ_FIRST(&reqq))) { - FILE *cfile; - package_t rPlist; - char *best_installed, *fname; - - /* remove a direct req from our queue */ - TAILQ_REMOVE(&reqq, lpp, lp_link); - - /* prepare for recursion */ - best_installed = find_best_matching_installed_pkg(lpp->lp_name); - - if (best_installed == NULL) { - warnx("cannot remove dependency for pkg-pattern %s", - lpp->lp_name); - fail = 1; - goto fail; - } - sanity_check(best_installed); - - fname = pkgdb_pkg_file(best_installed, CONTENTS_FNAME); - free(best_installed); - cfile = fopen(fname, "r"); - if (!cfile) { - warn("unable to open '%s' file", fname); - free(fname); - fail = 1; - goto fail; - } - free(fname); - read_plist(&rPlist, cfile); - fclose(cfile); - /* If we have a prefix, replace the first @cwd. */ - if (Prefix) { - delete_plist(&Plist, FALSE, PLIST_CWD, NULL); - add_plist_top(&Plist, PLIST_CWD, Prefix); - } - p = find_plist(&rPlist, PLIST_CWD); - if (!p) { - warnx("package '%s' doesn't have a prefix", lpp->lp_name); - free_plist(&rPlist); - fail = 1; - goto fail; - } - - /* put ourselves on the top of the find queue */ - TAILQ_INSERT_HEAD(&lpfindq, thislpp, lp_link); - - rc = require_find_recursive_down(lpp, &rPlist); - free_plist(&rPlist); - if (rc) { - fail = 1; - goto fail; - } - - /* take ourselves off the find queue */ - TAILQ_REMOVE(&lpfindq, thislpp, lp_link); - free_lpkg(lpp); - } - -fail: - /* Clean out reqq */ - while ((lpp = TAILQ_FIRST(&reqq))) { - TAILQ_REMOVE(&reqq, lpp, lp_link); - free_lpkg(lpp); - } - - return fail; -} - -/* - * Start recursion in the one or other direction. - */ -int -require_find(char *pkg, rec_find_t updown) -{ - lpkg_t *lpp; - int rv = 0; - - TAILQ_INIT(&lpfindq); - TAILQ_INIT(&lpdelq); - - lpp = alloc_lpkg(pkg); - switch (updown) { - case FIND_UP: - rv = require_find_recursive_up(lpp); - break; - case FIND_DOWN: - rv = require_find_recursive_down(lpp, &Plist); - break; - } - free_lpkg(lpp); - - return (rv); -} - -void -require_print(void) -{ - lpkg_t *lpp; - - /* print all but last -- deleting if requested */ - while ((lpp = TAILQ_FIRST(&lpdelq))) { - TAILQ_REMOVE(&lpdelq, lpp, lp_link); - fprintf(stderr, "\t%s\n", lpp->lp_name); - free_lpkg(lpp); - } -} - -/* - * This is seriously ugly code following. Written very fast! - */ -static int -pkg_do(char *pkg) -{ - char *pkgdir, *fname; - plist_t *p; - FILE *cfile; - FILE *fp; - int cc; - Boolean is_depoted_pkg = FALSE; - - /* Reset some state */ - if (Plist.head) - free_plist(&Plist); - - pkgdir = xasprintf("%s/%s", _pkgdb_getPKGDB_DIR(), pkg); - if (!fexists(pkgdir) || !(isdir(pkgdir) || islinktodir(pkgdir))) { - /* Check if the given package name matches something - * with 'pkg-[0-9]*' */ - lpkg_head_t trypkgs; - lpkg_t *lpp; - int qlen = 0; - - free(pkgdir); - - TAILQ_INIT(&trypkgs); - - switch (add_installed_pkgs_by_basename(pkg, &trypkgs)) { - case 0: - warnx("package '%s' not installed", pkg); - return 1; - case -1: - errx(EXIT_FAILURE, "Error during search in pkgdb for %s", pkg); - } - - TAILQ_FOREACH(lpp, &trypkgs, lp_link) - qlen++; - - if (qlen > 1) { - warnx("'%s' matches more than one package:", pkg); - while ((lpp = TAILQ_FIRST(&trypkgs))) { - TAILQ_REMOVE(&trypkgs, lpp, lp_link); - fprintf(stderr, "\t%s\n", lpp->lp_name); - free_lpkg(lpp); - } - return 1; - } - - /* - * Append the package names we've discovered to the - * pkgs list after this one, and return 0 so that we - * continue processing the pkgs list. - */ - TAILQ_FOREACH(lpp, &trypkgs, lp_link) - TAILQ_INSERT_TAIL(&pkgs, lpp, lp_link); - - return 0; - } - free(pkgdir); - setenv(PKG_REFCOUNT_DBDIR_VNAME, pkgdb_refcount_dir(), 1); - fname = pkgdb_pkg_file(pkg, CONTENTS_FNAME); - if (!fexists(fname)) { - warnx("package '%s' is not installed, %s missing", pkg, fname); - if (!Force) { - free(fname); - return 1; - } - } - free(fname); - - fname = pkgdb_pkg_file(pkg, PRESERVE_FNAME); - if (fexists(fname)) { - printf("Package `%s' is marked as not for deletion\n", pkg); - if (Force <= (NoDeleteFiles ? 0 : 1)) { - free(fname); - return 1; - } - printf("Deleting anyway\n"); - } - free(fname); - - fname = pkgdb_pkg_file(pkg, REQUIRED_BY_FNAME); - if (!isemptyfile(fname)) { - /* This package is required by others. Either nuke - * them (-r), or stop. */ - if (!Recurse_up) - warnx("package `%s' is required by other packages:", pkg); - else if (Verbose) - printf("Building list of packages that require `%s'" - " to deinstall\n", pkg); - if (require_find(pkg, FIND_UP)) { - if (!Force || Recurse_up) { - free(fname); - return (1); - } - } - if (!Recurse_up) { - require_print(); - if (!Force) { - free(fname); - return 1; - } - } else - require_delete(0); - } - free(fname); - - fname = pkgdb_pkg_file(pkg, VIEWS_FNAME); - if (!isemptyfile(fname)) { - char view[MaxPathSize]; - - /* This package has instances in other views */ - /* Delete them from the views */ - if ((fp = fopen(fname, "r")) == NULL) { - warnx("unable to open '%s' file", fname); - free(fname); - return 1; - } - while (fgets(view, sizeof(view), fp) != NULL) { - if (view[cc = strlen(view) - 1] == '\n') { - view[cc] = 0; - } - if (Verbose) { - printf("Deleting package %s instance from `%s' view\n", pkg, view); - } - if (fexec_skipempty(ProgramPath, "-K", view, - (Force > 1) ? "-f -f " : (Force == 1) ? "-f " : "", - pkg, NULL) != 0) { - warnx("unable to delete package %s from view %s", pkg, view); - (void) fclose(fp); - free(fname); - return 1; - } - } - (void) fclose(fp); - } - free(fname); - - sanity_check(pkg); - fname = pkgdb_pkg_file(pkg, CONTENTS_FNAME); - if ((cfile = fopen(fname, "r")) == NULL) { - warnx("unable to open '%s' file", fname); - free(fname); - return 1; - } - free(fname); - - read_plist(&Plist, cfile); - fclose(cfile); - p = find_plist(&Plist, PLIST_CWD); - /* If we have a prefix, replace the first @cwd. */ - if (Prefix) { - delete_plist(&Plist, FALSE, PLIST_CWD, NULL); - add_plist_top(&Plist, PLIST_CWD, Prefix); - } - if (!p) { - warnx("package '%s' doesn't have a prefix", pkg); - return 1; - } - if (Destdir != NULL) - setenv(PKG_DESTDIR_VNAME, Destdir, 1); - setenv(PKG_PREFIX_VNAME, p->name, 1); - fname = xasprintf("%s/%s", _pkgdb_getPKGDB_DIR(), pkg); - setenv(PKG_METADATA_DIR_VNAME, fname, 1); - free(fname); - /* - * Ensure that we don't do VIEW-DEINSTALL action for old packages - * or for the package in its depot directory. - */ - fname = pkgdb_pkg_file(pkg, DEINSTALL_FNAME); - if (!NoDeInstall && fexists(fname)) { - const char *target, *text; - char *fname2; - - fname2 = pkgdb_pkg_file(pkg, DEPOT_FNAME); - if (fexists(fname2)) { - target = "VIEW-DEINSTALL"; - text = "view deinstall"; - } else { - target = "DEINSTALL"; - text = "deinstall"; - } - free(fname2); - - if (Fake) { - printf("Would execute %s script at this point " - "(arg: %s).\n", text, target); - } else { - pkgdir = xasprintf("%s/%s", _pkgdb_getPKGDB_DIR(), pkg); - if (chmod(fname, 0555)) - warn("chmod of %s failed", fname); - if (fcexec(pkgdir, fname, pkg, target, NULL)) { - warnx("%s script returned error status", text); - if (!Force) { - free(pkgdir); - return 1; - } - } - free(pkgdir); - } - } - free(fname); - - if (!Fake) { - /* Some packages aren't packed right, so we need to just ignore delete_package()'s status. Ugh! :-( */ - if (delete_package(FALSE, CleanDirs, &Plist, NoDeleteFiles, Destdir) == FAIL) - warnx("couldn't entirely delete package `%s'\n", pkg); - } else { /* Fake means Verbose */ - printf("Attempting to delete package `%s'\n", pkg); - } - fname = pkgdb_pkg_file(pkg, DEPOT_FNAME); - if (!isemptyfile(fname)) { - if (Verbose) - printf("Attempting to remove the %s registration" - " on package `%s'\n", fname, pkg); - if (!Fake) - (void) unview(pkg); - } - /* - * If this isn't a package in a view, then remove this package - * from the +REQUIRED_BY list of the packages this depends on. - */ - if (!fexists(fname)) { - for (p = Plist.head; p; p = p->next) { - if (p->type != PLIST_PKGDEP) - continue; - if (Verbose) - printf("Attempting to remove dependency on package `%s'\n", p->name); - if (!Fake) - match_installed_pkgs(p->name, undepend, pkg); - } - } - free(fname); - if (Recurse_down) { - /* Also remove the packages further down, now that there's - * (most likely) nothing left which requires them. */ - if (Verbose) - printf("Building list of packages that `%s' required\n", pkg); - if (require_find(pkg, FIND_DOWN)) - return (1); - - require_delete(1); - } - - fname = pkgdb_pkg_file(pkg, DEINSTALL_FNAME); - if (!NoDeInstall && fexists(fname)) { - char *fname2; - - fname2 = pkgdb_pkg_file(pkg, DEPOT_FNAME); - if (!fexists(fname2)) { - if (Fake) - printf("Would execute post-de-install script at this point (arg: POST-DEINSTALL).\n"); - else { - pkgdir = xasprintf("%s/%s", _pkgdb_getPKGDB_DIR(), pkg); - if (chmod(fname, 0555)) - warn("chmod of %s failed", fname); - if (fcexec(pkgdir, fname, pkg, "POST-DEINSTALL", NULL)) { - warnx("post-deinstall script returned error status"); - if (!Force) { - free(pkgdir); - free(fname); - free(fname2); - return 1; - } - } - free(pkgdir); - } - } - free(fname2); - } - free(fname); - - fname = pkgdb_pkg_file(pkg, VIEWS_FNAME); - if (fexists(fname)) - is_depoted_pkg = TRUE; - free(fname); - - /* Change out of LogDir before we remove it. - * Do not fail here, as the package is not yet completely deleted! */ - if (!Fake) { - /* Finally nuke the +-files and the pkgdb-dir (/var/db/pkg/foo) */ - pkgdir = xasprintf("%s/%s", _pkgdb_getPKGDB_DIR(), pkg); - (void) remove_files(pkgdir, "+*"); - if (isemptydir(pkgdir)) - (void)rmdir(pkgdir); - else if (is_depoted_pkg) - warnx("%s is not empty", pkgdir); - else if (Force) { - if (recursive_remove(pkgdir, 1)) { - warn("Couldn't remove log entry in %s", pkgdir); - free(pkgdir); - return 1; - } else { - warnx("log entry forcefully removed in %s", pkgdir); - free(pkgdir); - return 0; - } - } else { - warnx("couldn't remove log entry in %s", pkgdir); - free(pkgdir); - return 1; - } - free(pkgdir); - } - return 0; -} - -int -pkg_perform(lpkg_head_t *pkghead) -{ - int err_cnt = 0; - int oldcwd; - lpkg_t *lpp; - - /* save cwd */ - oldcwd = open(".", O_RDONLY, 0); - if (oldcwd == -1) - err(EXIT_FAILURE, "cannot open \".\""); - - while ((lpp = TAILQ_FIRST(pkghead))) { - err_cnt += pkg_do(lpp->lp_name); - TAILQ_REMOVE(pkghead, lpp, lp_link); - free_lpkg(lpp); - if (fchdir(oldcwd) == FAIL) - err(EXIT_FAILURE, "unable to change to previous directory"); - } - close(oldcwd); - return err_cnt; -} diff --git a/pkgtools/pkg_install/files/delete/pkg_delete.1 b/pkgtools/pkg_install/files/delete/pkg_delete.1 index 27051ebebd4..66877d97d24 100644 --- a/pkgtools/pkg_install/files/delete/pkg_delete.1 +++ b/pkgtools/pkg_install/files/delete/pkg_delete.1 @@ -1,4 +1,4 @@ -.\" $NetBSD: pkg_delete.1,v 1.17 2009/02/02 12:35:01 joerg Exp $ +.\" $NetBSD: pkg_delete.1,v 1.18 2009/02/25 16:29:08 joerg Exp $ .\" .\" FreeBSD install - a package for the installation and maintenance .\" of non-core utilities. @@ -25,7 +25,7 @@ .Nd a utility for deleting previously installed software package distributions .Sh SYNOPSIS .Nm -.Op Fl DdFfNnORrVv +.Op Fl ADdFfNnORrVv .Bk -words .Op Fl K Ar pkg_dbdir .Ek @@ -41,6 +41,18 @@ command is used to delete packages that have been previously installed with the .Xr pkg_add 1 command. +The given packages are sorted, so that the dependencies of a package +are deleted after the package. +Before any action is executed, +.Nm +checks for packages that are marked as +.Cm preserved +or have depending packages left. +Unless the +.Fl f +flag is given, +.Nm +stops on the first error. .Sh WARNING .Bf -emphasis Since the @@ -89,6 +101,12 @@ flag is given, one or more (absolute) filenames may be specified and the Package Database will be consulted for the package to which the given file belongs. These packages are then deinstalled. +.It Fl A +Recursively remove all automatically installed packages that were needed +by the given packages and are no longer required. +See also the +.Fl R +flag. .It Fl D If a deinstallation script exists for a given package, do not execute it. .It Fl d @@ -148,24 +166,13 @@ For most packages, the prefix will be set automatically to the installed location by .Xr pkg_add 1 . .It Fl R -This option triggers a recursive delete of the given package and any -packages it depends on, unless some other package still needs a -dependent package. -This -.Fl R -option can be used to clean up by deleting a package and all its -then-unneeded dependent packages. +Recursively remove all packages that were needed by the given packages +and that have no other dependencies left. +This option overrides the +.Fl A +flag. .It Fl r -.Nm -first builds a list of all packages that require (directly and indirectly) -the one being deleted. -It then deletes these packages using -.Nm -with the given options before deleting the user specified package. -This -.Fl r -option can be used to recursively delete a package and all of the -packages which depend on that package. +Recursively remove all packages that require one of the packages given. .It Fl V Print version number and exit. .It Fl v @@ -291,7 +298,6 @@ appended to the path, e.g. .Xr pkg_admin 1 , .Xr pkg_create 1 , .Xr pkg_info 1 , -.Xr mktemp 3 , .Xr pkgsrc 7 .Sh AUTHORS .Bl -tag -width indent -compact @@ -304,4 +310,7 @@ refined it for .Nx wildcard dependency processing, pkgdb, recursive "down" delete, etc. +.It Joerg Sonnenberger +Rewrote most of the code to compute correct order of deinstallation +and to improve error handling. .El diff --git a/pkgtools/pkg_install/files/delete/pkg_delete.c b/pkgtools/pkg_install/files/delete/pkg_delete.c new file mode 100644 index 00000000000..08a558241d6 --- /dev/null +++ b/pkgtools/pkg_install/files/delete/pkg_delete.c @@ -0,0 +1,919 @@ +/*- + * Copyright (c) 2009 Joerg Sonnenberger <joerg@NetBSD.org>. + * Copyright (c) 2003 Johnny Lam <jlam@NetBSD.org>. + * 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 COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``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 + * COPYRIGHT HOLDERS OR CONTRIBUTORS 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. + */ +#if HAVE_CONFIG_H +#include "config.h" +#endif +#include <nbcompat.h> +#if HAVE_SYS_CDEFS_H +#include <sys/cdefs.h> +#endif +__RCSID("$NetBSD: pkg_delete.c,v 1.1 2009/02/25 16:29:08 joerg Exp $"); + +#if HAVE_ERR_H +#include <err.h> +#endif +#include <stdio.h> +#include <stdlib.h> + +#include "lib.h" + +#ifndef __UNCONST +#define __UNCONST(a) ((void *)(unsigned long)(const void *)(a)) +#endif + +static const char *pkgdb; +static const char *destdir; +static const char *prefix; + +static int no_deinstall; +static int prune_empty; +static int find_by_filename; +static int unregister_only; +static int pkgdb_update_only; +static int delete_recursive; +static int delete_new_leaves; +static int delete_automatic_leaves; + +static void +usage(void) +{ + fprintf(stderr, "usage: pkg_delete [-DdFfNnORrVv] [-K pkg_dbdir]" + " [-P destdir] [-p prefix] pkg-name ...\n"); + exit(1); +} + +static int +add_by_filename(lpkg_head_t *pkgs, const char *filename) +{ + lpkg_t *lpp; + char *s; + + if ((s = pkgdb_retrieve(filename)) == NULL) { + warnx("No matching package for file `%s' in pkgdb", filename); + return 1; + } + + /* XXX Verify that pkgdb is consistent? Trust it for now... */ + + lpp = alloc_lpkg(s); + TAILQ_INSERT_TAIL(pkgs, lpp, lp_link); + return 0; +} + +static int +add_by_pattern(lpkg_head_t *pkgs, const char *pattern) +{ + switch (add_installed_pkgs_by_pattern(pattern, pkgs)) { + case 0: + warnx("No package matching `%s' found", pattern); + return 1; + case -1: + warnx("Error while iterating package database for `%s'", + pattern); + return 1; + default: + return 0; + } +} + +/* + * The argument is either a fixed package name or an absolute path. + * The latter is recognized for legacy compatibility and must point + * into the package database. + */ +static int +add_by_pkgname(lpkg_head_t *pkgs, char *pkg) +{ + char *s; + lpkg_t *lpp; + size_t l; + const char *orig_pkg = pkg; + + if (pkg[0] == '/') { + l = strlen(pkgdb); + if (strncmp(pkg, pkgdb, l) || pkg[l] != '/') { + warnx("Absolute path is not relative to " + "package database, skipping: %s", pkg); + return 1; + } + pkg += l + 1; + } + l = strcspn(pkg, "/"); + if (pkg[l + strspn(pkg + l, "/")] != '\0') { + warnx("`%s' is not a package name, skipping", orig_pkg); + return 1; + } + pkg[l] = '\0'; + + s = pkgdb_pkg_file(pkg, CONTENTS_FNAME); + if (fexists(s)) { + free(s); + lpp = alloc_lpkg(pkg); + TAILQ_INSERT_TAIL(pkgs, lpp, lp_link); + return 0; + } + free(s); + + switch (add_installed_pkgs_by_basename(pkg, pkgs)) { + case 0: + warnx("No matching package for basename `%s' of `%s'", + pkg, orig_pkg); + return 1; + case -1: + warnx("Error expanding basename `%s' of `%s'", + pkg, orig_pkg); + return 1; + default: + return 0; + } +} + +/* + * Evaluate +REQUIRED_BY. This function is used for four different + * tasks: + * 0: check if no depending packages remain + * 1: like 0, but prepend the depending packages to pkgs if they exist + * 2: print remaining packages to stderr + * 3: check all and at least one depending packages have been removed + */ +static int +process_required_by(const char *pkg, lpkg_head_t *pkgs, + lpkg_head_t *sorted_pkgs, int action) +{ + char line[MaxPathSize], *eol, *fname; + FILE *fp; + lpkg_t *lpp; + int got_match, got_miss; + + fname = pkgdb_pkg_file(pkg, REQUIRED_BY_FNAME); + if (!fexists(fname)) { + free(fname); + return 0; + } + + if ((fp = fopen(fname, "r")) == NULL) { + warn("Failed to open `%s'", fname); + free(fname); + return -1; + } + free(fname); + + got_match = 0; + got_miss = 0; + + while (fgets(line, sizeof(line), fp)) { + if ((eol = strrchr(line, '\n')) != NULL) + *eol = '\0'; + TAILQ_FOREACH(lpp, sorted_pkgs, lp_link) { + if (strcmp(lpp->lp_name, line) == 0) + break; + } + if (lpp != NULL) { + got_match = 1; + continue; + } + got_miss = 1; + if (pkgs) { + TAILQ_FOREACH(lpp, pkgs, lp_link) { + if (strcmp(lpp->lp_name, line) == 0) + break; + } + if (lpp != NULL) + continue; + } + switch (action) { + case 0: + fclose(fp); + return 1; + case 1: + lpp = alloc_lpkg(line); + TAILQ_INSERT_HEAD(pkgs, lpp, lp_link); + break; + case 2: + fprintf(stderr, "\t%s\n", line); + break; + case 3: + fclose(fp); + return 0; + } + } + + fclose(fp); + + return (action == 3 ? got_match : got_miss); +} + +/* + * Main function to order the patterns from the command line and + * add the subtrees for -r processing as needed. + * + * The first part ensures that all packages are listed at most once + * in pkgs. Afterwards the list is scanned for packages without depending + * packages. Each such package is moved to sorted_pkgs in order. + * If -r is given, all dependencies are inserted at the head of pkgs. + * The loop has to continue as long as progress is made. This can happen + * either because another package has been added to pkgs due to recursion + * (head of pkgs changed) or because a package has no more depending packages + * (tail of sorted_pkgs changed). + * + * If no progress is made, the remaining packages are moved to sorted_pkgs + * and an error is returned for the !Force case. + */ +static int +sort_and_recurse(lpkg_head_t *pkgs, lpkg_head_t *sorted_pkgs) +{ + lpkg_t *lpp, *lpp2, *lpp_next, *lpp_old_tail, *lpp_old_head; + int rv; + + TAILQ_FOREACH_SAFE(lpp, pkgs, lp_link, lpp_next) { + TAILQ_FOREACH(lpp2, pkgs, lp_link) { + if (lpp != lpp2 && + strcmp(lpp->lp_name, lpp2->lp_name) == 0) + break; + } + if (lpp2 == NULL) + continue; + TAILQ_REMOVE(pkgs, lpp, lp_link); + free_lpkg(lpp); + } + + while (!TAILQ_EMPTY(pkgs)) { + lpp_old_tail = TAILQ_LAST(sorted_pkgs, _lpkg_head_t); + lpp_old_head = TAILQ_FIRST(pkgs); + + TAILQ_FOREACH_SAFE(lpp, pkgs, lp_link, lpp_next) { + rv = process_required_by(lpp->lp_name, pkgs, + sorted_pkgs, delete_recursive ? 1 : 0); + if (rv) + continue; + TAILQ_REMOVE(pkgs, lpp, lp_link); + TAILQ_INSERT_TAIL(sorted_pkgs, lpp, lp_link); + } + + if (lpp_old_tail == TAILQ_LAST(sorted_pkgs, _lpkg_head_t) && + lpp_old_head == TAILQ_FIRST(pkgs)) + break; + } + + if (TAILQ_EMPTY(pkgs)) + return 0; + + while (!TAILQ_EMPTY(pkgs)) { + lpp = TAILQ_FIRST(pkgs); + TAILQ_REMOVE(pkgs, lpp, lp_link); + fprintf(stderr, + "Package `%s' is still required by other packages:\n", + lpp->lp_name); + process_required_by(lpp->lp_name, NULL, sorted_pkgs, 2); + if (Force) + TAILQ_INSERT_TAIL(sorted_pkgs, lpp, lp_link); + else + free_lpkg(lpp); + } + + return !Force; +} + +struct find_leaves_data { + lpkg_head_t *pkgs; + int progress; +}; + +/* + * Iterator for finding leaf packages. + * Packages that are marked as not for deletion are not considered as + * leaves. For all other packages it is checked if at least one package + * that depended on them is to be removed AND no depending package remains. + * If that is the case, the package is appened to the sorted list. + * As this package can't have depending packages left, the topological order + * remains consistent. + */ +static int +find_new_leaves_iter(const char *pkg, void *cookie) +{ + char *fname; + struct find_leaves_data *data = cookie; + lpkg_t *lpp; + + fname = pkgdb_pkg_file(pkg, PRESERVE_FNAME); + if (fexists(fname)) { + free(fname); + return 0; + } + free(fname); + + if (delete_automatic_leaves && !delete_new_leaves && + !is_automatic_installed(pkg)) + return 0; + + /* Check whether this package is already on the list first. */ + TAILQ_FOREACH(lpp, data->pkgs, lp_link) { + if (strcmp(lpp->lp_name, pkg) == 0) + return 0; + } + + if (process_required_by(pkg, NULL, data->pkgs, 3) == 1) { + lpp = alloc_lpkg(pkg); + TAILQ_INSERT_TAIL(data->pkgs, lpp, lp_link); + data->progress = 0; + } + + return 0; +} + +/* + * Iterate over all installed packages and look for new leaf packages. + * As long as the loop adds one new leaf package, processing continues. + */ +static void +find_new_leaves(lpkg_head_t *pkgs) +{ + struct find_leaves_data data; + + data.pkgs = pkgs; + do { + data.progress = 0; + iterate_pkg_db(find_new_leaves_iter, &data); + } while (data.progress); +} + +/* + * Check that no entry on the package list is marked as not for deletion. + */ +static int +find_preserve_pkgs(lpkg_head_t *pkgs) +{ + lpkg_t *lpp; + char *fname; + int found_preserve; + + found_preserve = 0; + TAILQ_FOREACH(lpp, pkgs, lp_link) { + fname = pkgdb_pkg_file(lpp->lp_name, PRESERVE_FNAME); + if (!fexists(fname)) { + free(fname); + continue; + } + free(fname); + if (!found_preserve) + warnx("The following packages are marked as not " + "for deletion:"); + found_preserve = 1; + fprintf(stderr, "\t%s\n", lpp->lp_name); + } + if (!found_preserve) + return 0; + if (Force == 0 || (!unregister_only && Force == 1)) + return 1; + fprintf(stderr, "...but will delete them anyway\n"); + return 0; +} + +/* + * Remove package from view. This is calling pkg_deinstall again. + */ +static int +remove_pkg_from_view(const char *pkg) +{ + char line[MaxPathSize], *fname, *eol; + FILE *fp; + + fname = pkgdb_pkg_file(pkg, VIEWS_FNAME); + if (isemptyfile(fname)) { + free(fname); + return 0; + } + if ((fp = fopen(fname, "r")) == NULL) { + warn("Unable to open `%s', aborting", fname); + free(fname); + return 1; + } + free(fname); + while (fgets(line, sizeof(line), fp) != NULL) { + if ((eol = strrchr(line, '\n')) != NULL) + *eol = '\0'; + if (Verbose || Fake) + printf("Deleting package `%s' instance from `%s' view\n", + pkg, line); + if (Fake) + continue; + if (fexec_skipempty(BINDIR "/pkg_delete", "-K", line, + Fake ? "-n" : "", + (Force > 1) ? "-f" : "", + (Force > 0) ? "-f" : "", + pkg, NULL) != 0) { + warnx("Unable to delete package `%s' from view `%s'", + pkg, line); + fclose(fp); + return 1; + } + } + fclose(fp); + return 0; +} + +/* + * Run the +DEINSTALL script. Depending on whether this is + * a depoted package and whether this pre- or post-deinstall phase, + * different arguments are passed down. + */ +static int +run_deinstall_script(const char *pkg, int do_postdeinstall) +{ + const char *target, *text; + char *fname, *fname2, *pkgdir; + int rv; + + fname = pkgdb_pkg_file(pkg, DEINSTALL_FNAME); + if (!fexists(fname)) { + free(fname); + return 0; + } + + fname2 = pkgdb_pkg_file(pkg, DEPOT_FNAME); + if (fexists(fname2)) { + if (do_postdeinstall) { + free(fname); + free(fname2); + return 0; + } + target = "VIEW-DEINSTALL"; + text = "view deinstall"; + } else if (do_postdeinstall) { + target = "POST-DEINSTALL"; + text = "post-deinstall"; + } else { + target = "DEINSTALL"; + text = "deinstall"; + } + free(fname2); + + if (Fake) { + printf("Would execute %s script with argument %s now\n", + text, target); + free(fname); + return 0; + } + + pkgdir = xasprintf("%s/%s", _pkgdb_getPKGDB_DIR(), pkg); + if (chmod(fname, 0555)) + warn("chmod of `%s' failed", fname); + rv = fcexec(pkgdir, fname, pkg, target, NULL); + if (rv) + warnx("%s script returned error status", text); + free(pkgdir); + free(fname); + return rv; +} + +/* + * Copy lines from fname to fname_tmp, filtering out lines equal to text. + * Afterwards rename fname_tmp to fname; + */ +static int +remove_line(const char *fname, const char *fname_tmp, const char *text) +{ + FILE *fp, *fp_out; + char line[MaxPathSize], *eol; + int rv; + + if ((fp = fopen(fname, "r")) == NULL) { + warn("Unable to open `%s'", fname); + return 1; + } + if ((fp_out = fopen(fname_tmp, "w")) == NULL) { + warn("Unable to open `%s'", fname_tmp); + fclose(fp); + return 1; + } + + while (fgets(line, sizeof(line), fp) != NULL) { + if ((eol = strrchr(line, '\n')) != NULL) + *eol = '\0'; + if (strcmp(line, text) == 0) + continue; + fprintf(fp_out, "%s\n", line); + } + fclose(fp); + + if (fclose(fp_out) == EOF) { + remove(fname_tmp); + warnx("Failure while closing `%s' temp file", fname_tmp); + return 1; + } + + if (rename(fname_tmp, fname) == -1) { + warn("Unable to rename `%s' to `%s'", fname_tmp, fname); + rv = 1; + } + remove(fname_tmp); + + return rv; +} + +/* + * Unregister the package from the depot it is registered in. + */ +static int +remove_pkg_from_depot(const char *pkg) +{ + FILE *fp; + char line[MaxPathSize], *eol; + char *fname, *fname2; + int rv; + + fname = pkgdb_pkg_file(pkg, DEPOT_FNAME); + if (isemptyfile(fname)) { + free(fname); + return 0; + } + + if (Verbose) + printf("Attempting to remove the `%s' registration " + "on package `%s'\n", fname, pkg); + + if (Fake) { + free(fname); + return 1; + } + + if ((fp = fopen(fname, "r")) == NULL) { + warn("Unable to open `%s' file", fname); + free(fname); + return 1; + } + if (fgets(line, sizeof(line), fp) == NULL) { + fclose(fp); + warnx("Empty depot file `%s'", fname); + free(fname); + return 1; + } + if ((eol = strrchr(line, '\n')) != NULL) + *eol = '\0'; + fclose(fp); + free(fname); + + fname = pkgdb_pkg_file(pkg, VIEWS_FNAME); + fname2 = pkgdb_pkg_file(pkg, VIEWS_FNAME_TMP); + rv = remove_line(fname, fname2, line); + free(fname2); + free(fname); + + return rv; +} + +/* + * remove_depend is used as iterator function below. + * The passed-in package name should be removed from the + * +REQUIRED_BY list of the dependency. Such an entry + * can miss in a fully correct package database, if the pattern + * matches more than one package. + */ +static int +remove_depend(const char *cur_pkg, void *cookie) +{ + const char *pkg = cookie; + char *fname, *fname2; + int rv; + + fname = pkgdb_pkg_file(cur_pkg, REQUIRED_BY_FNAME); + if (isemptyfile(fname)) { + free(fname); + return 0; + } + fname2 = pkgdb_pkg_file(cur_pkg, REQUIRED_BY_FNAME_TMP); + + rv = remove_line(fname, fname2, pkg); + + free(fname2); + free(fname); + + return rv; +} + +static int +remove_pkg(const char *pkg) +{ + FILE *fp; + char *fname, *pkgdir; + package_t plist; + plist_t *p; + int is_depoted_pkg, rv, late_error; + + if (pkgdb_update_only) + return pkgdb_remove_pkg(pkg) ? 0 : 1; + + fname = pkgdb_pkg_file(pkg, CONTENTS_FNAME); + if (!fexists(fname)) { + warnx("package `%s' is not installed, `%s' missing", pkg, fname); + free(fname); + return 1; + } + free(fname); + + /* +REQUIRED_BY and +PRESERVE already checked */ + if (remove_pkg_from_view(pkg)) + return 1; + + /* + * The views related code has bad error handling, if e.g. + * the deinstall script fails, the package remains unregistered. + */ + + fname = pkgdb_pkg_file(pkg, CONTENTS_FNAME); + if ((fp = fopen(fname, "r")) == NULL) { + warnx("Failed to open `%s'", fname); + free(fname); + return 1; + } + read_plist(&plist, fp); + fclose(fp); + + /* + * If a prefix has been provided, remove the first @cwd and + * prepend that prefix. This allows removing packages without + * @cwd if really necessary. pkg_admin rebuild is likely needed + * afterwards though. + */ + if (prefix) { + delete_plist(&plist, FALSE, PLIST_CWD, NULL); + add_plist_top(&plist, PLIST_CWD, prefix); + } + if ((p = find_plist(&plist, PLIST_CWD)) == NULL) { + warnx("Package `%s' doesn't have a prefix", pkg); + return 1; + } + + setenv(PKG_PREFIX_VNAME, p->name, 1); + fname = xasprintf("%s/%s", _pkgdb_getPKGDB_DIR(), pkg); + setenv(PKG_METADATA_DIR_VNAME, fname, 1); + free(fname); + + if (!no_deinstall && !unregister_only) { + if (run_deinstall_script(pkg, 0) && !Force) + return 1; + } + + late_error = 0; + + if (Fake) + printf("Attempting to delete package `%s'\n", pkg); + else if (delete_package(FALSE, prune_empty, &plist, unregister_only, + destdir) == FAIL) { + warnx("couldn't entirely delete package `%s'\n", pkg); + /* + * XXX It could be nice to error out here explicitly, + * XXX but this is problematic for missing or changed files. + * XXX At least the inability to remove files at all should + * XXX be handled though. + */ + } + + /* + * Past the point of no return. Files are gone, all that is left + * is cleaning up registered dependencies and removing the meta data. + * Errors in the remaining part are counted, but don't stop the + * processing. + */ + + fname = pkgdb_pkg_file(pkg, DEPOT_FNAME); + if (fexists(fname)) { + late_error |= remove_pkg_from_depot(pkg); + /* XXX error checking */ + } else { + for (p = plist.head; p; p = p->next) { + if (p->type != PLIST_PKGDEP) + continue; + if (Verbose) + printf("Attempting to remove dependency " + "on package `%s'\n", p->name); + if (Fake) + continue; + match_installed_pkgs(p->name, remove_depend, + __UNCONST(pkg)); + } + } + free(fname); + + free_plist(&plist); + + if (!no_deinstall && !unregister_only) + late_error |= run_deinstall_script(pkg, 1); + + fname = pkgdb_pkg_file(pkg, VIEWS_FNAME); + if (fexists(fname)) + is_depoted_pkg = TRUE; + free(fname); + + if (Fake) + return 0; + + /* + * Kill the pkgdb subdirectory. The files have been removed, so + * this is way beyond the point of no return. + */ + pkgdir = xasprintf("%s/%s", _pkgdb_getPKGDB_DIR(), pkg); + (void) remove_files(pkgdir, "+*"); + rv = 1; + if (isemptydir(pkgdir)&& rmdir(pkgdir) == 0) + rv = 0; + else if (is_depoted_pkg) + warnx("Depot directory `%s' is not empty", pkgdir); + else if (!Force) + warnx("Couldn't remove package directory in `%s'", pkgdir); + else if (recursive_remove(pkgdir, 1)) + warn("Couldn't remove package directory `%s'", pkgdir); + else + warnx("Package directory `%s' forcefully removed", pkgdir); + free(pkgdir); + + return rv | late_error; +} + +int +main(int argc, char *argv[]) +{ + lpkg_head_t pkgs, sorted_pkgs; + int ch, r, has_error; + unsigned long bad_count; + + TAILQ_INIT(&pkgs); + TAILQ_INIT(&sorted_pkgs); + + while ((ch = getopt(argc, argv, "ADdFfNnORrVvK:P:p:")) != -1) { + switch (ch) { + case 'A': + delete_automatic_leaves = 1; + break; + case 'D': + no_deinstall = 1; + break; + case 'd': + prune_empty = 1; + break; + case 'F': + find_by_filename = 1; + break; + case 'f': + ++Force; + break; + case 'K': + pkgdb = optarg; + break; + case 'N': + unregister_only = 1; + break; + case 'n': + Fake = 1; + break; + case 'O': + pkgdb_update_only = 1; + break; + case 'P': + destdir = optarg; + break; + case 'p': + prefix = optarg; + break; + case 'R': + delete_new_leaves = 1; + break; + case 'r': + delete_recursive = 1; + break; + case 'V': + show_version(); + /* NOTREACHED */ + case 'v': + ++Verbose; + break; + default: + usage(); + break; + } + } + + if (destdir != NULL) { + char *pkgdbdir; + + if (pkgdb == NULL) + pkgdb = _pkgdb_getPKGDB_DIR(); + + pkgdbdir = xasprintf("%s/%s", destdir, pkgdb); + _pkgdb_setPKGDB_DIR(pkgdbdir); + free(pkgdbdir); + } else if (pkgdb != NULL) { + _pkgdb_setPKGDB_DIR(pkgdb); + } else { + pkgdb = _pkgdb_getPKGDB_DIR(); + } + + argc -= optind; + argv += optind; + + if (argc == 0) { + if (find_by_filename) + warnx("Missing filename(s)"); + else + warnx("Missing package name(s)"); + usage(); + } + + if (Fake) + r = pkgdb_open(ReadOnly); + else + r = pkgdb_open(ReadWrite); + + if (!r) + errx(EXIT_FAILURE, "Opening pkgdb failed"); + + /* First, process all command line options. */ + + has_error = 0; + for (; argc != 0; --argc, ++argv) { + if (find_by_filename) + has_error |= add_by_filename(&pkgs, *argv); + else if (ispkgpattern(*argv)) + has_error |= add_by_pattern(&pkgs, *argv); + else + has_error |= add_by_pkgname(&pkgs, *argv); + } + + if (has_error && !Force) { + pkgdb_close(); + return EXIT_FAILURE; + } + + /* Second, reorder and recursive if necessary. */ + + if (sort_and_recurse(&pkgs, &sorted_pkgs)) { + pkgdb_close(); + return EXIT_FAILURE; + } + + /* Third, add leaves if necessary. */ + + if (delete_new_leaves || delete_automatic_leaves) + find_new_leaves(&sorted_pkgs); + + /* + * Now that all packages to remove are known, check + * if all are removable. After that, start the actual + * removal. + */ + + if (find_preserve_pkgs(&sorted_pkgs)) { + pkgdb_close(); + return EXIT_FAILURE; + } + + setenv(PKG_REFCOUNT_DBDIR_VNAME, pkgdb_refcount_dir(), 1); + + bad_count = 0; + while (!TAILQ_EMPTY(&sorted_pkgs)) { + lpkg_t *lpp; + + lpp = TAILQ_FIRST(&sorted_pkgs); + TAILQ_REMOVE(&sorted_pkgs, lpp, lp_link); + if (remove_pkg(lpp->lp_name)) { + ++bad_count; + if (!Force) + break; + } + free_lpkg(lpp); + } + + pkgdb_close(); + + if (Force && bad_count && Verbose) + warnx("Removal of %lu packages failed", bad_count); + + return bad_count > 0 ? EXIT_FAILURE : EXIT_SUCCESS; +} diff --git a/pkgtools/pkg_install/files/delete/pkg_delete.cat1 b/pkgtools/pkg_install/files/delete/pkg_delete.cat1 index 309432c83fc..1af2f09f3ee 100644 --- a/pkgtools/pkg_install/files/delete/pkg_delete.cat1 +++ b/pkgtools/pkg_install/files/delete/pkg_delete.cat1 @@ -5,12 +5,16 @@ NNAAMMEE age distributions SSYYNNOOPPSSIISS - ppkkgg__ddeelleettee [--DDddFFffNNnnOORRrrVVvv] [--KK _p_k_g___d_b_d_i_r] [--PP _d_e_s_t_d_i_r] [--pp _p_r_e_f_i_x] + ppkkgg__ddeelleettee [--AADDddFFffNNnnOORRrrVVvv] [--KK _p_k_g___d_b_d_i_r] [--PP _d_e_s_t_d_i_r] [--pp _p_r_e_f_i_x] _p_k_g_-_n_a_m_e _._._. DDEESSCCRRIIPPTTIIOONN The ppkkgg__ddeelleettee command is used to delete packages that have been previ- - ously installed with the pkg_add(1) command. + ously installed with the pkg_add(1) command. The given packages are + sorted, so that the dependencies of a package are deleted after the pack- + age. Before any action is executed, ppkkgg__ddeelleettee checks for packages that + are marked as pprreesseerrvveedd or have depending packages left. Unless the --ff + flag is given, ppkkgg__ddeelleettee stops on the first error. WWAARRNNIINNGG _S_i_n_c_e _t_h_e ppkkgg__ddeelleettee _c_o_m_m_a_n_d _m_a_y _e_x_e_c_u_t_e _s_c_r_i_p_t_s _o_r _p_r_o_g_r_a_m_s _p_r_o_v_i_d_e_d _b_y @@ -36,6 +40,10 @@ OOPPTTIIOONNSS sulted for the package to which the given file belongs. These packages are then deinstalled. + --AA Recursively remove all automatically installed packages that were + needed by the given packages and are no longer required. See + also the --RR flag. + --DD If a deinstallation script exists for a given package, do not execute it. @@ -84,17 +92,12 @@ OOPPTTIIOONNSS packages, the prefix will be set automatically to the installed location by pkg_add(1). - --RR This option triggers a recursive delete of the given package and - any packages it depends on, unless some other package still needs - a dependent package. This --RR option can be used to clean up by - deleting a package and all its then-unneeded dependent packages. + --RR Recursively remove all packages that were needed by the given + packages and that have no other dependencies left. This option + overrides the --AA flag. - --rr ppkkgg__ddeelleettee first builds a list of all packages that require - (directly and indirectly) the one being deleted. It then deletes - these packages using ppkkgg__ddeelleettee with the given options before - deleting the user specified package. This --rr option can be used - to recursively delete a package and all of the packages which - depend on that package. + --rr Recursively remove all packages that require one of the packages + given. --VV Print version number and exit. @@ -157,8 +160,7 @@ EENNVVIIRROONNMMEENNTT _/_v_a_r_/_d_b_/_p_k_g_._r_e_f_c_o_u_n_t. SSEEEE AALLSSOO - pkg_add(1), pkg_admin(1), pkg_create(1), pkg_info(1), mktemp(3), - pkgsrc(7) + pkg_add(1), pkg_admin(1), pkg_create(1), pkg_info(1), pkgsrc(7) AAUUTTHHOORRSS Jordan Hubbard @@ -168,5 +170,8 @@ AAUUTTHHOORRSS Hubert Feyrer NetBSD wildcard dependency processing, pkgdb, recursive "down" delete, etc. + Joerg Sonnenberger + Rewrote most of the code to compute correct order of deinstalla- + tion and to improve error handling. -NetBSD 4.0 July 30, 2008 NetBSD 4.0 +NetBSD 5.0 July 30, 2008 NetBSD 5.0 diff --git a/pkgtools/pkg_install/files/lib/version.h b/pkgtools/pkg_install/files/lib/version.h index 3e16fcf3fd3..d7eb758d0ff 100644 --- a/pkgtools/pkg_install/files/lib/version.h +++ b/pkgtools/pkg_install/files/lib/version.h @@ -1,4 +1,4 @@ -/* $NetBSD: version.h,v 1.112 2009/02/14 17:08:05 joerg Exp $ */ +/* $NetBSD: version.h,v 1.113 2009/02/25 16:29:08 joerg Exp $ */ /* * Copyright (c) 2001 Thomas Klausner. All rights reserved. @@ -27,6 +27,6 @@ #ifndef _INST_LIB_VERSION_H_ #define _INST_LIB_VERSION_H_ -#define PKGTOOLS_VERSION "20090214" +#define PKGTOOLS_VERSION "20090225" #endif /* _INST_LIB_VERSION_H_ */ |