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$");
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");
69 warnx("no packages match pattern(s)");
76 err_cnt += sortdeps(pkgs);
77 for (i = 0; pkgs[i]; i++) {
78 if (Recursive == TRUE) {
79 errcode = requiredby(pkgs[i], &rb_list, FALSE, TRUE);
82 } else if (errcode > 0) {
84 * Copy values from the rb_list queue into argv-like NULL
85 * terminated list because requiredby() uses some static
86 * storage, while pkg_do() below will call this function,
87 * thus blowing our rb_list away.
89 rbtmp = rb = alloca((errcode + 1) * sizeof(*rb));
91 warnx("%s(): alloca() failed", __func__);
95 STAILQ_FOREACH(rb_entry, rb_list, link) {
96 *rbtmp = alloca(strlen(rb_entry->pkgname) + 1);
98 warnx("%s(): alloca() failed", __func__);
102 strcpy(*rbtmp, rb_entry->pkgname);
107 err_cnt += sortdeps(rb);
108 for (j = 0; rb[j]; j++)
109 err_cnt += pkg_do(rb[j]);
112 err_cnt += pkg_do(pkgs[i]);
118 static Package Plist;
120 /* This is seriously ugly code following. Written very fast! */
125 char *deporigin, **deporigins = NULL, **depnames = NULL, ***depmatches, home[FILENAME_MAX];
129 /* support for separate pre/post install scripts */
130 int new_m = 0, dep_count = 0;
131 const char *pre_script = DEINSTALL_FNAME;
132 const char *post_script, *pre_arg, *post_arg;
133 struct reqr_by_entry *rb_entry;
134 struct reqr_by_head *rb_list;
138 if (!pkg || !(len = strlen(pkg)))
140 if (pkg[len - 1] == '/')
143 /* Reset some state */
147 sprintf(LogDir, "%s/%s", LOG_DIR, pkg);
149 isinstalled = isinstalledpkg(pkg);
150 if (isinstalled == 0) {
151 warnx("no such package '%s' installed", pkg);
153 } else if (isinstalled < 0) {
154 warnx("the package info for package '%s' is corrupt%s",
155 pkg, Force ? " (but I'll delete it anyway)" : " (use -f to force removal)");
159 if (vsystem("%s -rf %s", REMOVE_CMD, LogDir)) {
160 warnx("couldn't remove log entry in %s, deinstall failed", LogDir);
162 warnx("couldn't completely deinstall package '%s',\n"
163 "only the log entry in %s was removed", pkg, LogDir);
169 if (!getcwd(home, FILENAME_MAX)) {
171 errx(2, "%s: unable to get current working directory!", __func__);
174 if (chdir(LogDir) == FAIL) {
175 warnx("unable to change directory to %s! deinstall failed", LogDir);
179 if (Interactive == TRUE) {
182 (void)fprintf(stderr, "delete %s? ", pkg);
183 (void)fflush(stderr);
184 first = ch = getchar();
185 while (ch != '\n' && ch != EOF)
187 if (first != 'y' && first != 'Y')
192 if (requiredby(pkg, &rb_list, FALSE, TRUE) < 0)
194 if (!STAILQ_EMPTY(rb_list)) {
195 warnx("package '%s' is required by these other packages\n"
196 "and may not be deinstalled%s:",
197 pkg, Force ? " (but I'll delete it anyway)" : "");
198 STAILQ_FOREACH(rb_entry, rb_list, link)
199 fprintf(stderr, "%s\n", rb_entry->pkgname);
204 sanity_check(LogDir);
205 cfile = fopen(CONTENTS_FNAME, "r");
208 warnx("unable to open '%s' file", CONTENTS_FNAME);
212 /* If we have a prefix, add it now */
214 add_plist(&Plist, PLIST_CWD, Prefix);
215 read_plist(&Plist, cfile);
217 p = find_plist(&Plist, PLIST_CWD);
220 warnx("package '%s' doesn't have a prefix", pkg);
224 setenv(PKG_PREFIX_VNAME, p->name, 1);
226 if ((fd = open(REQUIRE_FNAME, O_RDWR)) != -1) {
228 fchmod(fd, sb.st_mode | S_IXALL); /* be sure, chmod a+x */
231 printf("Executing 'require' script.\n");
232 if (vsystem("./%s %s DEINSTALL", REQUIRE_FNAME, pkg)) {
233 warnx("package %s fails requirements %s", pkg,
234 Force ? "" : "- not deleted");
241 * Test whether to use the old method of passing tokens to deinstallation
242 * scripts, and set appropriate variables..
245 if (fexists(POST_DEINSTALL_FNAME)) {
247 post_script = POST_DEINSTALL_FNAME;
248 pre_arg = post_arg = "";
249 } else if (fexists(DEINSTALL_FNAME)) {
250 post_script = DEINSTALL_FNAME;
251 pre_arg = "DEINSTALL";
252 post_arg = "POST-DEINSTALL";
254 post_script = pre_arg = post_arg = NULL;
257 if (!NoDeInstall && pre_script != NULL && (fd = open(pre_script, O_RDWR)) != -1) {
259 printf("Would execute de-install script at this point.\n");
262 fchmod(fd, sb.st_mode | S_IXALL); /* be sure, chmod a+x */
264 if (vsystem("./%s %s %s", pre_script, pkg, pre_arg)) {
265 warnx("deinstall script returned error status");
272 for (p = Plist.head; p ; p = p->next) {
273 if (p->type != PLIST_PKGDEP)
275 deporigin = (p->next != NULL && p->next->type == PLIST_DEPORIGIN) ? p->next->name :
278 printf("Trying to remove dependency on package '%s'", p->name);
279 if (deporigin != NULL)
280 printf(" with '%s' origin", deporigin);
285 deporigins = realloc(deporigins, (dep_count + 2) * sizeof(*deporigins));
286 depnames = realloc(depnames, (dep_count + 1) * sizeof(*depnames));
287 deporigins[dep_count] = deporigin;
288 deporigins[dep_count + 1] = NULL;
289 depnames[dep_count] = p->name;
292 undepend(p->name, pkg);
298 /* Undepend all the dependencies at once */
299 depmatches = matchallbyorigin((const char **)deporigins, NULL);
302 for (i = 0; i < dep_count; i++) {
304 char **tmp = depmatches[i];
306 for (j = 0; tmp[j] != NULL; j++)
307 undepend(tmp[j], pkg);
308 } else if (depnames[i]) {
309 undepend(depnames[i], pkg);
315 if (chdir(home) == FAIL) {
317 errx(2, "%s: unable to return to working directory %s!", __func__,
322 * Some packages aren't packed right, so we need to just ignore
323 * delete_package()'s status. Ugh! :-(
325 if (delete_package(FALSE, CleanDirs, &Plist) == FAIL)
327 "couldn't entirely delete package `%s'\n"
328 "(perhaps the packing list is incorrectly specified?)", pkg);
330 if (chdir(LogDir) == FAIL) {
331 warnx("unable to change directory to %s! deinstall failed", LogDir);
335 if (!NoDeInstall && post_script != NULL && (fd = open(post_script, O_RDWR)) != -1) {
337 printf("Would execute post-deinstall script at this point.\n");
340 fchmod(fd, sb.st_mode | S_IXALL); /* be sure, chmod a+x */
342 if (vsystem("./%s %s %s", post_script, pkg, post_arg)) {
343 warnx("post-deinstall script returned error status");
350 if (chdir(home) == FAIL) {
352 errx(2, "%s: unable to return to working directory %s!", __func__,
357 if (vsystem("%s -r%c %s", REMOVE_CMD, Force ? 'f' : ' ', LogDir)) {
358 warnx("couldn't remove log entry in %s, deinstall failed", LogDir);
367 sanity_check(char *pkg)
369 if (!fexists(CONTENTS_FNAME)) {
371 errx(2, "%s: installed package %s has no %s file!", __func__,
372 pkg, CONTENTS_FNAME);
384 undepend(char *p, char *pkgname)
386 char fname[FILENAME_MAX], ftmp[FILENAME_MAX];
389 struct reqr_by_entry *rb_entry;
390 struct reqr_by_head *rb_list;
393 if (requiredby(p, &rb_list, Verbose, FALSE) <= 0)
395 snprintf(fname, sizeof(fname), "%s/%s/%s", LOG_DIR, p, REQUIRED_BY_FNAME);
396 snprintf(ftmp, sizeof(ftmp), "%s.XXXXXX", fname);
399 warnx("couldn't open temp file '%s'", ftmp);
402 fpwr = fdopen(s, "w");
405 warnx("couldn't fdopen temp file '%s'", ftmp);
408 STAILQ_FOREACH(rb_entry, rb_list, link)
409 if (strcmp(rb_entry->pkgname, pkgname)) /* no match */
410 fputs(rb_entry->pkgname, fpwr), putc('\n', fpwr);
411 if (fchmod(s, 0644) == FAIL) {
412 warnx("error changing permission of temp file '%s'", ftmp);
416 if (fclose(fpwr) == EOF) {
417 warnx("error closing temp file '%s'", ftmp);
420 if (rename(ftmp, fname) == -1)
421 warnx("error renaming '%s' to '%s'", ftmp, fname);