1998-10-09 22:27:31 +04:00
|
|
|
/* $NetBSD: perform.c,v 1.10 1998/10/09 18:27:33 agc Exp $ */
|
1997-06-05 16:59:18 +04:00
|
|
|
|
1997-10-17 18:53:18 +04:00
|
|
|
#include <sys/cdefs.h>
|
1997-06-05 12:54:23 +04:00
|
|
|
#ifndef lint
|
1997-06-05 16:59:18 +04:00
|
|
|
#if 0
|
1997-10-16 04:31:32 +04:00
|
|
|
static const char *rcsid = "from FreeBSD Id: perform.c,v 1.15 1997/10/13 15:03:52 jkh Exp";
|
1997-06-05 16:59:18 +04:00
|
|
|
#else
|
1998-10-09 22:27:31 +04:00
|
|
|
__RCSID("$NetBSD: perform.c,v 1.10 1998/10/09 18:27:33 agc Exp $");
|
1997-06-05 16:59:18 +04:00
|
|
|
#endif
|
1997-06-05 12:54:23 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
1997-10-16 04:31:32 +04:00
|
|
|
#include <err.h>
|
1997-06-05 12:54:23 +04:00
|
|
|
#include "lib.h"
|
|
|
|
#include "delete.h"
|
|
|
|
|
|
|
|
static int pkg_do(char *);
|
|
|
|
static void sanity_check(char *);
|
1998-10-03 20:24:07 +04:00
|
|
|
static int undepend(const char *, char *);
|
1997-06-05 12:54:23 +04:00
|
|
|
static char LogDir[FILENAME_MAX];
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
pkg_perform(char **pkgs)
|
|
|
|
{
|
|
|
|
int i, err_cnt = 0;
|
|
|
|
|
|
|
|
for (i = 0; pkgs[i]; i++)
|
|
|
|
err_cnt += pkg_do(pkgs[i]);
|
|
|
|
return err_cnt;
|
|
|
|
}
|
|
|
|
|
1998-10-09 22:27:31 +04:00
|
|
|
static package_t Plist;
|
1997-06-05 12:54:23 +04:00
|
|
|
|
|
|
|
/* This is seriously ugly code following. Written very fast! */
|
|
|
|
static int
|
|
|
|
pkg_do(char *pkg)
|
|
|
|
{
|
|
|
|
FILE *cfile;
|
|
|
|
char home[FILENAME_MAX];
|
1998-10-09 22:27:31 +04:00
|
|
|
plist_t *p;
|
1997-06-05 12:54:23 +04:00
|
|
|
char *tmp;
|
|
|
|
|
|
|
|
/* Reset some state */
|
|
|
|
if (Plist.head)
|
|
|
|
free_plist(&Plist);
|
|
|
|
|
1998-10-08 16:57:58 +04:00
|
|
|
(void) snprintf(LogDir, sizeof(LogDir), "%s/%s", (tmp = getenv(PKG_DBDIR)) ? tmp : DEF_LOG_DIR,
|
1997-06-05 12:54:23 +04:00
|
|
|
pkg);
|
|
|
|
if (!fexists(LogDir)) {
|
1997-10-16 04:31:32 +04:00
|
|
|
warnx("no such package '%s' installed", pkg);
|
1997-06-05 12:54:23 +04:00
|
|
|
return 1;
|
|
|
|
}
|
1998-10-09 15:16:57 +04:00
|
|
|
if (!getcwd(home, FILENAME_MAX)) {
|
|
|
|
cleanup(0);
|
|
|
|
errx(2, "unable to get current working directory!");
|
|
|
|
}
|
1997-06-05 12:54:23 +04:00
|
|
|
if (chdir(LogDir) == FAIL) {
|
1997-10-16 04:31:32 +04:00
|
|
|
warnx("unable to change directory to %s! deinstall failed", LogDir);
|
1997-06-05 12:54:23 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (!isemptyfile(REQUIRED_BY_FNAME)) {
|
|
|
|
char buf[512];
|
1997-10-16 04:31:32 +04:00
|
|
|
warnx("package `%s' is required by these other packages\n"
|
|
|
|
"and may not be deinstalled%s:",
|
|
|
|
pkg, Force ? " (but I'll delete it anyway)" : "" );
|
1997-06-05 12:54:23 +04:00
|
|
|
cfile = fopen(REQUIRED_BY_FNAME, "r");
|
|
|
|
if (cfile) {
|
|
|
|
while (fgets(buf, sizeof(buf), cfile))
|
|
|
|
fprintf(stderr, "%s", buf);
|
|
|
|
fclose(cfile);
|
|
|
|
} else
|
1997-10-16 04:31:32 +04:00
|
|
|
warnx("cannot open requirements file `%s'", REQUIRED_BY_FNAME);
|
1997-06-05 12:54:23 +04:00
|
|
|
if (!Force)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
sanity_check(LogDir);
|
|
|
|
cfile = fopen(CONTENTS_FNAME, "r");
|
|
|
|
if (!cfile) {
|
1997-10-16 04:31:32 +04:00
|
|
|
warnx("unable to open '%s' file", CONTENTS_FNAME);
|
1997-06-05 12:54:23 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/* If we have a prefix, add it now */
|
|
|
|
if (Prefix)
|
|
|
|
add_plist(&Plist, PLIST_CWD, Prefix);
|
|
|
|
read_plist(&Plist, cfile);
|
|
|
|
fclose(cfile);
|
|
|
|
p = find_plist(&Plist, PLIST_CWD);
|
|
|
|
if (!p) {
|
1997-10-16 04:31:32 +04:00
|
|
|
warnx("package '%s' doesn't have a prefix", pkg);
|
1997-06-05 12:54:23 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
setenv(PKG_PREFIX_VNAME, p->name, 1);
|
|
|
|
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-16 04:31:32 +04:00
|
|
|
warnx("package %s fails requirements %s", pkg,
|
|
|
|
Force ? "" : "- not deleted");
|
1997-06-05 12:54:23 +04:00
|
|
|
if (!Force)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!NoDeInstall && fexists(DEINSTALL_FNAME)) {
|
|
|
|
if (Fake)
|
|
|
|
printf("Would execute de-install script at this point.\n");
|
|
|
|
else {
|
|
|
|
vsystem("chmod +x %s", DEINSTALL_FNAME); /* make sure */
|
|
|
|
if (vsystem("./%s %s DEINSTALL", DEINSTALL_FNAME, pkg)) {
|
1997-10-16 04:31:32 +04:00
|
|
|
warnx("deinstall script returned error status");
|
1997-06-05 12:54:23 +04:00
|
|
|
if (!Force)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1998-10-09 15:16:57 +04:00
|
|
|
if (chdir(home) == FAIL) {
|
|
|
|
cleanup(0);
|
|
|
|
errx(2, "Toto! This doesn't look like Kansas anymore!");
|
|
|
|
}
|
1997-06-05 12:54:23 +04:00
|
|
|
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) == FAIL)
|
1997-10-16 04:31:32 +04:00
|
|
|
warnx(
|
|
|
|
"couldn't entirely delete package (perhaps the packing list is\n"
|
|
|
|
"incorrectly specified?)");
|
1997-06-05 12:54:23 +04:00
|
|
|
if (vsystem("%s -r %s", REMOVE_CMD, LogDir)) {
|
1997-10-16 04:31:32 +04:00
|
|
|
warnx("couldn't remove log entry in %s, deinstall failed", LogDir);
|
1997-06-05 12:54:23 +04:00
|
|
|
if (!Force)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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)
|
1998-10-03 20:24:07 +04:00
|
|
|
findmatchingname((tmp = getenv(PKG_DBDIR)) ? tmp : DEF_LOG_DIR,
|
|
|
|
p->name, undepend, pkg);
|
1997-06-05 12:54:23 +04:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1998-10-03 20:24:07 +04:00
|
|
|
|
1997-06-05 12:54:23 +04:00
|
|
|
static void
|
|
|
|
sanity_check(char *pkg)
|
|
|
|
{
|
1998-10-09 15:16:57 +04:00
|
|
|
if (!fexists(CONTENTS_FNAME)) {
|
|
|
|
cleanup(0);
|
|
|
|
errx(2, "installed package %s has no %s file!", pkg, CONTENTS_FNAME);
|
|
|
|
}
|
1997-06-05 12:54:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cleanup(int sig)
|
|
|
|
{
|
|
|
|
/* Nothing to do */
|
1998-08-25 04:12:16 +04:00
|
|
|
if(sig) /* in case this is ever used as a signal handler */
|
|
|
|
exit(1);
|
1997-06-05 12:54:23 +04:00
|
|
|
}
|
|
|
|
|
1998-10-03 20:24:07 +04:00
|
|
|
/* deppkgname is the pkg from which's +REQUIRED_BY file we are
|
|
|
|
* about to remove pkg2delname. This function is called from
|
|
|
|
* findmatchingname(), deppkgname is expanded from a (possible) pattern.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
undepend(const char *deppkgname, char *pkg2delname)
|
1997-06-05 12:54:23 +04:00
|
|
|
{
|
|
|
|
char fname[FILENAME_MAX], ftmp[FILENAME_MAX];
|
|
|
|
char fbuf[FILENAME_MAX];
|
|
|
|
FILE *fp, *fpwr;
|
|
|
|
char *tmp;
|
|
|
|
int s;
|
|
|
|
|
1998-10-08 16:57:58 +04:00
|
|
|
(void) snprintf(fname, sizeof(fname), "%s/%s/%s",
|
1997-06-05 12:54:23 +04:00
|
|
|
(tmp = getenv(PKG_DBDIR)) ? tmp : DEF_LOG_DIR,
|
1998-10-03 20:24:07 +04:00
|
|
|
deppkgname, REQUIRED_BY_FNAME);
|
1997-06-05 12:54:23 +04:00
|
|
|
fp = fopen(fname, "r");
|
|
|
|
if (fp == NULL) {
|
1997-10-16 04:31:32 +04:00
|
|
|
warnx("couldn't open dependency file `%s'", fname);
|
1998-10-03 20:24:07 +04:00
|
|
|
return 0;
|
1997-06-05 12:54:23 +04:00
|
|
|
}
|
1998-10-08 16:57:58 +04:00
|
|
|
(void) snprintf(ftmp, sizeof(ftmp), "%s.XXXXXX", fname);
|
1997-06-05 12:54:23 +04:00
|
|
|
s = mkstemp(ftmp);
|
|
|
|
if (s == -1) {
|
|
|
|
fclose(fp);
|
1997-10-16 04:31:32 +04:00
|
|
|
warnx("couldn't open temp file `%s'", ftmp);
|
1998-10-03 20:24:07 +04:00
|
|
|
return 0;
|
1997-06-05 12:54:23 +04:00
|
|
|
}
|
|
|
|
fpwr = fdopen(s, "w");
|
|
|
|
if (fpwr == NULL) {
|
|
|
|
close(s);
|
|
|
|
fclose(fp);
|
1997-10-16 04:31:32 +04:00
|
|
|
warnx("couldn't fdopen temp file `%s'", ftmp);
|
1997-06-05 12:54:23 +04:00
|
|
|
remove(ftmp);
|
1998-10-03 20:24:07 +04:00
|
|
|
return 0;
|
1997-06-05 12:54:23 +04:00
|
|
|
}
|
|
|
|
while (fgets(fbuf, sizeof(fbuf), fp) != NULL) {
|
|
|
|
if (fbuf[strlen(fbuf)-1] == '\n')
|
|
|
|
fbuf[strlen(fbuf)-1] = '\0';
|
1998-10-03 20:24:07 +04:00
|
|
|
if (strcmp(fbuf, pkg2delname)) /* no match */
|
1997-06-05 12:54:23 +04:00
|
|
|
fputs(fbuf, fpwr), putc('\n', fpwr);
|
|
|
|
}
|
|
|
|
(void) fclose(fp);
|
|
|
|
if (fchmod(s, 0644) == FAIL) {
|
1997-10-16 04:31:32 +04:00
|
|
|
warnx("error changing permission of temp file `%s'", ftmp);
|
1997-06-05 12:54:23 +04:00
|
|
|
fclose(fpwr);
|
|
|
|
remove(ftmp);
|
1998-10-03 20:24:07 +04:00
|
|
|
return 0;
|
1997-06-05 12:54:23 +04:00
|
|
|
}
|
|
|
|
if (fclose(fpwr) == EOF) {
|
1997-10-16 04:31:32 +04:00
|
|
|
warnx("error closing temp file `%s'", ftmp);
|
1997-06-05 12:54:23 +04:00
|
|
|
remove(ftmp);
|
1998-10-03 20:24:07 +04:00
|
|
|
return 0;
|
1997-06-05 12:54:23 +04:00
|
|
|
}
|
|
|
|
if (rename(ftmp, fname) == -1)
|
1997-10-16 04:31:32 +04:00
|
|
|
warnx("error renaming `%s' to `%s'", ftmp, fname);
|
1997-06-05 12:54:23 +04:00
|
|
|
remove(ftmp); /* just in case */
|
1998-10-03 20:24:07 +04:00
|
|
|
|
|
|
|
return 0;
|
1997-06-05 12:54:23 +04:00
|
|
|
}
|