2 * FreeBSD install - a package for the installation and maintainance
3 * of non-core utilities.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
17 * This is the main body of the delete module.
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD: src/usr.sbin/pkg_install/delete/perform.c,v 1.22.2.12 2003/02/28 13:58:14 des Exp $");
28 static int pkg_do(char *);
29 static void sanity_check(char *);
30 static void undepend(char *, char *);
31 static char LogDir[FILENAME_MAX];
35 pkg_perform(char **pkgs)
37 char **matched, **rb, **rbtmp;
40 struct reqr_by_entry *rb_entry;
41 struct reqr_by_head *rb_list;
43 if (MatchType != MATCH_EXACT) {
44 matched = matchinstalled(MatchType, pkgs, &errcode);
50 * Copy matched[] into pkgs[], because we'll need to use
51 * matchinstalled() later on.
53 if (matched != NULL) {
55 for (i = 0; matched[i] != NULL; i++) {
56 pkgs = realloc(pkgs, sizeof(*pkgs) * (i + 2));
57 pkgs[i] = strdup(matched[i]);
61 else switch (MatchType) {
65 warnx("no packages installed");
68 warnx("no packages match pattern(s)");
75 err_cnt += sortdeps(pkgs);
76 for (i = 0; pkgs[i]; i++) {
77 if (Recursive == TRUE) {
78 errcode = requiredby(pkgs[i], &rb_list, FALSE, TRUE);
81 } else if (errcode > 0) {
83 * Copy values from the rb_list queue into argv-like NULL
84 * terminated list because requiredby() uses some static
85 * storage, while pkg_do() below will call this function,
86 * thus blowing our rb_list away.
88 rbtmp = rb = alloca((errcode + 1) * sizeof(*rb));
90 warnx("%s(): alloca() failed", __func__);
94 STAILQ_FOREACH(rb_entry, rb_list, link) {
95 *rbtmp = alloca(strlen(rb_entry->pkgname) + 1);
97 warnx("%s(): alloca() failed", __func__);
101 strcpy(*rbtmp, rb_entry->pkgname);
106 err_cnt += sortdeps(rb);
107 for (j = 0; rb[j]; j++)
108 err_cnt += pkg_do(rb[j]);
111 err_cnt += pkg_do(pkgs[i]);
117 static Package Plist;
119 /* This is seriously ugly code following. Written very fast! */
124 char *deporigin, **depnames, home[FILENAME_MAX];
127 /* support for separate pre/post install scripts */
129 const char *pre_script = DEINSTALL_FNAME;
130 const char *post_script, *pre_arg, *post_arg;
131 struct reqr_by_entry *rb_entry;
132 struct reqr_by_head *rb_list;
134 if (!pkg || !(len = strlen(pkg)))
136 if (pkg[len - 1] == '/')
139 /* Reset some state */
143 if (!isinstalledpkg(pkg)) {
144 warnx("no such package '%s' installed", pkg);
148 if (!getcwd(home, FILENAME_MAX)) {
150 errx(2, "%s: unable to get current working directory!", __func__);
153 sprintf(LogDir, "%s/%s", LOG_DIR, pkg);
155 if (chdir(LogDir) == FAIL) {
156 warnx("unable to change directory to %s! deinstall failed", LogDir);
160 if (Interactive == TRUE) {
163 (void)fprintf(stderr, "delete %s? ", pkg);
164 (void)fflush(stderr);
165 first = ch = getchar();
166 while (ch != '\n' && ch != EOF)
168 if (first != 'y' && first != 'Y')
173 if (requiredby(pkg, &rb_list, FALSE, TRUE) < 0)
175 if (!STAILQ_EMPTY(rb_list)) {
176 warnx("package '%s' is required by these other packages\n"
177 "and may not be deinstalled%s:",
178 pkg, Force ? " (but I'll delete it anyway)" : "");
179 STAILQ_FOREACH(rb_entry, rb_list, link)
180 fprintf(stderr, "%s\n", rb_entry->pkgname);
185 sanity_check(LogDir);
186 cfile = fopen(CONTENTS_FNAME, "r");
189 warnx("unable to open '%s' file", CONTENTS_FNAME);
193 /* If we have a prefix, add it now */
195 add_plist(&Plist, PLIST_CWD, Prefix);
196 read_plist(&Plist, cfile);
198 p = find_plist(&Plist, PLIST_CWD);
201 warnx("package '%s' doesn't have a prefix", pkg);
205 setenv(PKG_PREFIX_VNAME, p->name, 1);
207 if (fexists(REQUIRE_FNAME)) {
209 printf("Executing 'require' script.\n");
210 vsystem("chmod +x %s", REQUIRE_FNAME); /* be sure */
211 if (vsystem("./%s %s DEINSTALL", REQUIRE_FNAME, pkg)) {
212 warnx("package %s fails requirements %s", pkg,
213 Force ? "" : "- not deleted");
220 * Test whether to use the old method of passing tokens to deinstallation
221 * scripts, and set appropriate variables..
224 if (fexists(POST_DEINSTALL_FNAME)) {
226 post_script = POST_DEINSTALL_FNAME;
227 pre_arg = post_arg = "";
228 } else if (fexists(DEINSTALL_FNAME)) {
229 post_script = DEINSTALL_FNAME;
230 pre_arg = "DEINSTALL";
231 post_arg = "POST-DEINSTALL";
233 post_script = pre_arg = post_arg = NULL;
236 if (!NoDeInstall && pre_script != NULL && fexists(pre_script)) {
238 printf("Would execute de-install script at this point.\n");
240 vsystem("chmod +x %s", pre_script); /* make sure */
241 if (vsystem("./%s %s %s", pre_script, pkg, pre_arg)) {
242 warnx("deinstall script returned error status");
249 if (chdir(home) == FAIL) {
251 errx(2, "%s: unable to return to working directory %s!", __func__,
256 * Some packages aren't packed right, so we need to just ignore
257 * delete_package()'s status. Ugh! :-(
259 if (delete_package(FALSE, CleanDirs, &Plist) == FAIL)
261 "couldn't entirely delete package (perhaps the packing list is\n"
262 "incorrectly specified?)");
264 if (chdir(LogDir) == FAIL) {
265 warnx("unable to change directory to %s! deinstall failed", LogDir);
269 if (!NoDeInstall && post_script != NULL && fexists(post_script)) {
271 printf("Would execute post-deinstall script at this point.\n");
273 vsystem("chmod +x %s", post_script); /* make sure */
274 if (vsystem("./%s %s %s", post_script, pkg, post_arg)) {
275 warnx("post-deinstall script returned error status");
282 if (chdir(home) == FAIL) {
284 errx(2, "%s: unable to return to working directory %s!", __func__,
289 if (vsystem("%s -r%c %s", REMOVE_CMD, Force ? 'f' : ' ', LogDir)) {
290 warnx("couldn't remove log entry in %s, deinstall failed", LogDir);
296 for (p = Plist.head; p ; p = p->next) {
297 if (p->type != PLIST_PKGDEP)
299 deporigin = (p->next->type == PLIST_DEPORIGIN) ? p->next->name :
302 printf("Trying to remove dependency on package '%s'", p->name);
303 if (deporigin != NULL)
304 printf(" with '%s' origin", deporigin);
308 depnames = (deporigin != NULL) ? matchbyorigin(deporigin, NULL) :
310 if (depnames == NULL) {
311 depnames = alloca(sizeof(*depnames) * 2);
312 depnames[0] = p->name;
315 for (i = 0; depnames[i] != NULL; i++)
316 undepend(depnames[i], pkg);
323 sanity_check(char *pkg)
325 if (!fexists(CONTENTS_FNAME)) {
327 errx(2, "%s: installed package %s has no %s file!", __func__,
328 pkg, CONTENTS_FNAME);
340 undepend(char *p, char *pkgname)
342 char fname[FILENAME_MAX], ftmp[FILENAME_MAX];
345 struct reqr_by_entry *rb_entry;
346 struct reqr_by_head *rb_list;
349 if (requiredby(p, &rb_list, Verbose, FALSE) <= 0)
351 snprintf(fname, sizeof(fname), "%s/%s/%s", LOG_DIR, p, REQUIRED_BY_FNAME);
352 snprintf(ftmp, sizeof(ftmp), "%s.XXXXXX", fname);
355 warnx("couldn't open temp file '%s'", ftmp);
358 fpwr = fdopen(s, "w");
361 warnx("couldn't fdopen temp file '%s'", ftmp);
364 STAILQ_FOREACH(rb_entry, rb_list, link)
365 if (strcmp(rb_entry->pkgname, pkgname)) /* no match */
366 fputs(rb_entry->pkgname, fpwr), putc('\n', fpwr);
367 if (fchmod(s, 0644) == FAIL) {
368 warnx("error changing permission of temp file '%s'", ftmp);
372 if (fclose(fpwr) == EOF) {
373 warnx("error closing temp file '%s'", ftmp);
376 if (rename(ftmp, fname) == -1)
377 warnx("error renaming '%s' to '%s'", ftmp, fname);