1993-08-26 01:19:55 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2002-04-01 09:39:07 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
1997-10-08 07:48:21 +00:00
|
|
|
#include <err.h>
|
1993-08-26 01:19:55 +00:00
|
|
|
#include "lib.h"
|
|
|
|
#include "delete.h"
|
|
|
|
|
|
|
|
static int pkg_do(char *);
|
|
|
|
static void sanity_check(char *);
|
1994-12-06 00:51:50 +00:00
|
|
|
static void undepend(PackingList, char *);
|
1993-08-26 01:19:55 +00:00
|
|
|
static char LogDir[FILENAME_MAX];
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
pkg_perform(char **pkgs)
|
|
|
|
{
|
2001-09-19 08:06:48 +00:00
|
|
|
char **matched, **rb, **rbtmp;
|
|
|
|
int errcode, i, j;
|
2001-02-19 13:26:13 +00:00
|
|
|
int err_cnt = 0;
|
2001-09-19 08:06:48 +00:00
|
|
|
struct reqr_by_entry *rb_entry;
|
|
|
|
struct reqr_by_head *rb_list;
|
2001-02-27 09:00:18 +00:00
|
|
|
|
|
|
|
if (MatchType != MATCH_EXACT) {
|
|
|
|
matched = matchinstalled(MatchType, pkgs, &errcode);
|
|
|
|
if (errcode != 0)
|
|
|
|
return 1;
|
|
|
|
/* Not reached */
|
|
|
|
|
|
|
|
if (matched != NULL)
|
|
|
|
pkgs = matched;
|
|
|
|
else switch (MatchType) {
|
|
|
|
case MATCH_GLOB:
|
|
|
|
break;
|
|
|
|
case MATCH_ALL:
|
|
|
|
warnx("no packages installed");
|
|
|
|
return 0;
|
|
|
|
case MATCH_REGEX:
|
|
|
|
warnx("no packages match pattern(s)");
|
|
|
|
return 1;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2001-02-19 13:26:13 +00:00
|
|
|
|
2001-03-15 10:47:00 +00:00
|
|
|
err_cnt += sortdeps(pkgs);
|
2001-02-19 13:26:13 +00:00
|
|
|
for (i = 0; pkgs[i]; i++) {
|
2001-09-19 08:06:48 +00:00
|
|
|
if (Recursive == TRUE) {
|
|
|
|
errcode = requiredby(pkgs[i], &rb_list, FALSE, TRUE);
|
|
|
|
if (errcode < 0) {
|
|
|
|
err_cnt++;
|
|
|
|
} else if (errcode > 0) {
|
|
|
|
/*
|
|
|
|
* Copy values from the rb_list queue into argv-like NULL
|
|
|
|
* terminated list because requiredby() uses some static
|
|
|
|
* storage, while pkg_do() below will call this function,
|
|
|
|
* thus blowing our rb_list away.
|
|
|
|
*/
|
|
|
|
rbtmp = rb = alloca((errcode + 1) * sizeof(*rb));
|
|
|
|
if (rb == NULL) {
|
|
|
|
warnx("%s(): alloca() failed", __FUNCTION__);
|
|
|
|
err_cnt++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
STAILQ_FOREACH(rb_entry, rb_list, link) {
|
|
|
|
*rbtmp = alloca(strlen(rb_entry->pkgname) + 1);
|
|
|
|
if (*rbtmp == NULL) {
|
|
|
|
warnx("%s(): alloca() failed", __FUNCTION__);
|
|
|
|
err_cnt++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
strcpy(*rbtmp, rb_entry->pkgname);
|
|
|
|
rbtmp++;
|
|
|
|
}
|
|
|
|
*rbtmp = NULL;
|
|
|
|
|
|
|
|
err_cnt += sortdeps(rb);
|
|
|
|
for (j = 0; rb[j]; j++)
|
|
|
|
err_cnt += pkg_do(rb[j]);
|
|
|
|
}
|
|
|
|
}
|
1993-08-26 01:19:55 +00:00
|
|
|
err_cnt += pkg_do(pkgs[i]);
|
2001-02-19 13:26:13 +00:00
|
|
|
}
|
|
|
|
|
1993-08-26 01:19:55 +00:00
|
|
|
return err_cnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Package Plist;
|
|
|
|
|
|
|
|
/* This is seriously ugly code following. Written very fast! */
|
|
|
|
static int
|
|
|
|
pkg_do(char *pkg)
|
|
|
|
{
|
|
|
|
FILE *cfile;
|
|
|
|
char home[FILENAME_MAX];
|
1994-05-25 18:00:04 +00:00
|
|
|
PackingList p;
|
1998-11-11 06:09:04 +00:00
|
|
|
int len;
|
1998-12-16 13:59:31 +00:00
|
|
|
/* support for separate pre/post install scripts */
|
|
|
|
int new_m = 0;
|
|
|
|
char pre_script[FILENAME_MAX] = DEINSTALL_FNAME;
|
|
|
|
char post_script[FILENAME_MAX];
|
|
|
|
char pre_arg[FILENAME_MAX], post_arg[FILENAME_MAX];
|
2001-09-19 08:06:48 +00:00
|
|
|
struct reqr_by_entry *rb_entry;
|
|
|
|
struct reqr_by_head *rb_list;
|
1998-11-11 06:09:04 +00:00
|
|
|
|
|
|
|
if (!pkg || !(len = strlen(pkg)))
|
|
|
|
return 1;
|
|
|
|
if (pkg[len - 1] == '/')
|
|
|
|
pkg[len - 1] = '\0';
|
1993-08-26 01:19:55 +00:00
|
|
|
|
|
|
|
/* Reset some state */
|
|
|
|
if (Plist.head)
|
|
|
|
free_plist(&Plist);
|
|
|
|
|
2001-08-02 13:13:06 +00:00
|
|
|
sprintf(LogDir, "%s/%s", LOG_DIR, pkg);
|
1998-12-16 13:59:31 +00:00
|
|
|
|
1993-08-26 01:19:55 +00:00
|
|
|
if (!fexists(LogDir)) {
|
1997-10-08 07:48:21 +00:00
|
|
|
warnx("no such package '%s' installed", pkg);
|
1993-08-26 01:19:55 +00:00
|
|
|
return 1;
|
|
|
|
}
|
1998-12-16 13:59:31 +00:00
|
|
|
|
1998-09-11 07:26:58 +00:00
|
|
|
if (!getcwd(home, FILENAME_MAX)) {
|
|
|
|
cleanup(0);
|
2002-05-11 03:48:49 +00:00
|
|
|
errx(2, "%s: unable to get current working directory!", __FUNCTION__);
|
1998-09-11 07:26:58 +00:00
|
|
|
}
|
1998-12-16 13:59:31 +00:00
|
|
|
|
1993-08-26 01:19:55 +00:00
|
|
|
if (chdir(LogDir) == FAIL) {
|
1997-10-08 07:48:21 +00:00
|
|
|
warnx("unable to change directory to %s! deinstall failed", LogDir);
|
1993-08-26 01:19:55 +00:00
|
|
|
return 1;
|
|
|
|
}
|
1998-12-16 13:59:31 +00:00
|
|
|
|
2001-02-27 09:00:18 +00:00
|
|
|
if (Interactive == TRUE) {
|
|
|
|
int first, ch;
|
|
|
|
|
|
|
|
(void)fprintf(stderr, "delete %s? ", pkg);
|
|
|
|
(void)fflush(stderr);
|
|
|
|
first = ch = getchar();
|
|
|
|
while (ch != '\n' && ch != EOF)
|
|
|
|
ch = getchar();
|
|
|
|
if (first != 'y' && first != 'Y')
|
|
|
|
return 0;
|
|
|
|
/* Not reached */
|
|
|
|
}
|
|
|
|
|
2001-09-19 08:06:48 +00:00
|
|
|
if (requiredby(pkg, &rb_list, FALSE, TRUE) < 0)
|
|
|
|
return 1;
|
|
|
|
if (!STAILQ_EMPTY(rb_list)) {
|
2001-08-02 12:38:29 +00:00
|
|
|
warnx("package '%s' is required by these other packages\n"
|
2001-09-19 08:06:48 +00:00
|
|
|
"and may not be deinstalled%s:",
|
|
|
|
pkg, Force ? " (but I'll delete it anyway)" : "");
|
|
|
|
STAILQ_FOREACH(rb_entry, rb_list, link)
|
|
|
|
fprintf(stderr, "%s\n", rb_entry->pkgname);
|
1994-12-06 00:51:50 +00:00
|
|
|
if (!Force)
|
|
|
|
return 1;
|
|
|
|
}
|
1998-12-16 13:59:31 +00:00
|
|
|
|
1993-08-26 01:19:55 +00:00
|
|
|
sanity_check(LogDir);
|
|
|
|
cfile = fopen(CONTENTS_FNAME, "r");
|
1998-12-16 13:59:31 +00:00
|
|
|
|
1993-08-26 01:19:55 +00:00
|
|
|
if (!cfile) {
|
1997-10-08 07:48:21 +00:00
|
|
|
warnx("unable to open '%s' file", CONTENTS_FNAME);
|
1993-08-26 01:19:55 +00:00
|
|
|
return 1;
|
|
|
|
}
|
1998-12-16 13:59:31 +00:00
|
|
|
|
1993-08-26 01:19:55 +00:00
|
|
|
/* If we have a prefix, add it now */
|
|
|
|
if (Prefix)
|
|
|
|
add_plist(&Plist, PLIST_CWD, Prefix);
|
|
|
|
read_plist(&Plist, cfile);
|
|
|
|
fclose(cfile);
|
1997-03-06 10:21:57 +00:00
|
|
|
p = find_plist(&Plist, PLIST_CWD);
|
1998-12-16 13:59:31 +00:00
|
|
|
|
1997-03-06 10:21:57 +00:00
|
|
|
if (!p) {
|
1997-10-08 07:48:21 +00:00
|
|
|
warnx("package '%s' doesn't have a prefix", pkg);
|
1997-03-06 10:21:57 +00:00
|
|
|
return 1;
|
|
|
|
}
|
1998-12-16 13:59:31 +00:00
|
|
|
|
1997-03-06 10:21:57 +00:00
|
|
|
setenv(PKG_PREFIX_VNAME, p->name, 1);
|
1998-12-16 13:59:31 +00:00
|
|
|
|
1994-05-25 18:00:04 +00:00
|
|
|
if (fexists(REQUIRE_FNAME)) {
|
|
|
|
if (Verbose)
|
|
|
|
printf("Executing 'require' script.\n");
|
|
|
|
vsystem("chmod +x %s", REQUIRE_FNAME); /* be sure */
|
|
|
|
if (vsystem("./%s %s DEINSTALL", REQUIRE_FNAME, pkg)) {
|
1997-10-08 07:48:21 +00:00
|
|
|
warnx("package %s fails requirements %s", pkg,
|
|
|
|
Force ? "" : "- not deleted");
|
1994-12-06 00:51:50 +00:00
|
|
|
if (!Force)
|
|
|
|
return 1;
|
1994-05-25 18:00:04 +00:00
|
|
|
}
|
|
|
|
}
|
1998-12-16 13:59:31 +00:00
|
|
|
|
2001-05-17 10:12:45 +00:00
|
|
|
/*
|
|
|
|
* Test whether to use the old method of passing tokens to deinstallation
|
1998-12-16 13:59:31 +00:00
|
|
|
* scripts, and set appropriate variables..
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (fexists(POST_DEINSTALL_FNAME)) {
|
|
|
|
new_m = 1;
|
|
|
|
sprintf(post_script, "%s", POST_DEINSTALL_FNAME);
|
1999-08-11 20:55:42 +00:00
|
|
|
pre_arg[0] = '\0';
|
|
|
|
post_arg[0] = '\0';
|
1998-12-16 13:59:31 +00:00
|
|
|
} else {
|
|
|
|
if (fexists(DEINSTALL_FNAME)) {
|
|
|
|
sprintf(post_script, "%s", DEINSTALL_FNAME);
|
|
|
|
sprintf(pre_arg, "DEINSTALL");
|
|
|
|
sprintf(post_arg, "POST-DEINSTALL");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!NoDeInstall && fexists(pre_script)) {
|
1993-08-26 01:19:55 +00:00
|
|
|
if (Fake)
|
|
|
|
printf("Would execute de-install script at this point.\n");
|
|
|
|
else {
|
1998-12-16 13:59:31 +00:00
|
|
|
vsystem("chmod +x %s", pre_script); /* make sure */
|
|
|
|
if (vsystem("./%s %s %s", pre_script, pkg, pre_arg)) {
|
1997-10-08 07:48:21 +00:00
|
|
|
warnx("deinstall script returned error status");
|
1994-12-06 00:51:50 +00:00
|
|
|
if (!Force)
|
|
|
|
return 1;
|
1993-08-26 01:19:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1998-12-16 13:59:31 +00:00
|
|
|
|
1998-09-11 07:26:58 +00:00
|
|
|
if (chdir(home) == FAIL) {
|
|
|
|
cleanup(0);
|
2002-05-11 03:48:49 +00:00
|
|
|
errx(2, "%s: unable to return to working directory %s!", __FUNCTION__,
|
|
|
|
home);
|
1998-09-11 07:26:58 +00:00
|
|
|
}
|
1998-12-16 13:59:31 +00:00
|
|
|
|
2001-05-17 10:12:45 +00:00
|
|
|
/*
|
|
|
|
* Some packages aren't packed right, so we need to just ignore
|
|
|
|
* delete_package()'s status. Ugh! :-(
|
|
|
|
*/
|
2001-03-04 17:38:38 +00:00
|
|
|
if (delete_package(FALSE, CleanDirs, &Plist) == FAIL)
|
|
|
|
warnx(
|
1997-10-08 07:48:21 +00:00
|
|
|
"couldn't entirely delete package (perhaps the packing list is\n"
|
|
|
|
"incorrectly specified?)");
|
1998-12-16 13:59:31 +00:00
|
|
|
|
|
|
|
if (chdir(LogDir) == FAIL) {
|
|
|
|
warnx("unable to change directory to %s! deinstall failed", LogDir);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!NoDeInstall && fexists(post_script)) {
|
|
|
|
if (Fake)
|
|
|
|
printf("Would execute post-deinstall script at this point.\n");
|
|
|
|
else {
|
|
|
|
vsystem("chmod +x %s", post_script); /* make sure */
|
|
|
|
if (vsystem("./%s %s %s", post_script, pkg, post_arg)) {
|
|
|
|
warnx("post-deinstall script returned error status");
|
|
|
|
if (!Force)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (chdir(home) == FAIL) {
|
|
|
|
cleanup(0);
|
2002-05-11 03:48:49 +00:00
|
|
|
errx(2, "%s: unable to return to working directory %s!", __FUNCTION__,
|
|
|
|
home);
|
1998-12-16 13:59:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!Fake) {
|
2000-06-02 04:32:37 +00:00
|
|
|
if (vsystem("%s -r%c %s", REMOVE_CMD, Force ? 'f' : ' ', LogDir)) {
|
1997-10-08 07:48:21 +00:00
|
|
|
warnx("couldn't remove log entry in %s, deinstall failed", LogDir);
|
1996-07-30 10:48:20 +00:00
|
|
|
if (!Force)
|
|
|
|
return 1;
|
1995-11-12 04:55:40 +00:00
|
|
|
}
|
1993-08-26 01:19:55 +00:00
|
|
|
}
|
1998-12-16 13:59:31 +00:00
|
|
|
|
1994-12-06 00:51:50 +00:00
|
|
|
for (p = Plist.head; p ; p = p->next) {
|
|
|
|
if (p->type != PLIST_PKGDEP)
|
|
|
|
continue;
|
|
|
|
if (Verbose)
|
2001-08-02 12:38:29 +00:00
|
|
|
printf("Attempting to remove dependency on package '%s'\n", p->name);
|
1994-12-06 00:51:50 +00:00
|
|
|
if (!Fake)
|
|
|
|
undepend(p, pkg);
|
|
|
|
}
|
1993-08-26 01:19:55 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sanity_check(char *pkg)
|
|
|
|
{
|
1998-09-11 07:26:58 +00:00
|
|
|
if (!fexists(CONTENTS_FNAME)) {
|
|
|
|
cleanup(0);
|
2002-05-11 03:48:49 +00:00
|
|
|
errx(2, "%s: installed package %s has no %s file!", __FUNCTION__,
|
|
|
|
pkg, CONTENTS_FNAME);
|
1998-09-11 07:26:58 +00:00
|
|
|
}
|
1993-08-26 01:19:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cleanup(int sig)
|
|
|
|
{
|
1998-09-11 07:26:58 +00:00
|
|
|
if (sig)
|
|
|
|
exit(1);
|
1993-08-26 01:19:55 +00:00
|
|
|
}
|
1994-12-06 00:51:50 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
undepend(PackingList p, char *pkgname)
|
|
|
|
{
|
2001-09-19 08:06:48 +00:00
|
|
|
char fname[FILENAME_MAX], ftmp[FILENAME_MAX];
|
|
|
|
FILE *fpwr;
|
|
|
|
int s;
|
|
|
|
struct reqr_by_entry *rb_entry;
|
|
|
|
struct reqr_by_head *rb_list;
|
|
|
|
|
|
|
|
|
|
|
|
if (requiredby(p->name, &rb_list, Verbose, FALSE) <= 0)
|
|
|
|
return;
|
|
|
|
snprintf(fname, sizeof(fname), "%s/%s/%s", LOG_DIR, p->name,
|
|
|
|
REQUIRED_BY_FNAME);
|
|
|
|
snprintf(ftmp, sizeof(ftmp), "%s.XXXXXX", fname);
|
|
|
|
s = mkstemp(ftmp);
|
|
|
|
if (s == -1) {
|
|
|
|
warnx("couldn't open temp file '%s'", ftmp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fpwr = fdopen(s, "w");
|
|
|
|
if (fpwr == NULL) {
|
|
|
|
close(s);
|
|
|
|
warnx("couldn't fdopen temp file '%s'", ftmp);
|
|
|
|
goto cleanexit;
|
|
|
|
}
|
|
|
|
STAILQ_FOREACH(rb_entry, rb_list, link)
|
|
|
|
if (strcmp(rb_entry->pkgname, pkgname)) /* no match */
|
|
|
|
fputs(rb_entry->pkgname, fpwr), putc('\n', fpwr);
|
|
|
|
if (fchmod(s, 0644) == FAIL) {
|
|
|
|
warnx("error changing permission of temp file '%s'", ftmp);
|
|
|
|
fclose(fpwr);
|
|
|
|
goto cleanexit;
|
|
|
|
}
|
|
|
|
if (fclose(fpwr) == EOF) {
|
|
|
|
warnx("error closing temp file '%s'", ftmp);
|
|
|
|
goto cleanexit;
|
|
|
|
}
|
|
|
|
if (rename(ftmp, fname) == -1)
|
|
|
|
warnx("error renaming '%s' to '%s'", ftmp, fname);
|
|
|
|
cleanexit:
|
|
|
|
remove(ftmp);
|
|
|
|
return;
|
1994-12-06 00:51:50 +00:00
|
|
|
}
|