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.
19 * $FreeBSD: src/usr.sbin/pkg_install/delete/perform.c,v 1.41 2004/06/29 19:06:41 eik Exp $
20 * $DragonFly: src/usr.sbin/pkg_install/delete/Attic/perform.c,v 1.4 2004/12/18 22:48:04 swildner Exp $
27 static int pkg_do(char *);
28 static void sanity_check(char *);
29 static void undepend(char *, char *);
30 static char LogDir[FILENAME_MAX];
34 pkg_perform(char **pkgs)
36 char **matched, **rb, **rbtmp;
39 struct reqr_by_entry *rb_entry;
40 struct reqr_by_head *rb_list;
42 if (MatchType != MATCH_EXACT) {
43 matched = matchinstalled(MatchType, pkgs, &errcode);
49 * Copy matched[] into pkgs[], because we'll need to use
50 * matchinstalled() later on.
52 if (matched != NULL) {
54 for (i = 0; matched[i] != NULL; i++) {
55 pkgs = realloc(pkgs, sizeof(*pkgs) * (i + 2));
56 pkgs[i] = strdup(matched[i]);
60 else switch (MatchType) {
64 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];
128 /* support for separate pre/post install scripts */
130 const char *pre_script = DEINSTALL_FNAME;
131 const char *post_script, *pre_arg, *post_arg;
132 struct reqr_by_entry *rb_entry;
133 struct reqr_by_head *rb_list;
135 if (!pkg || !(len = strlen(pkg)))
137 if (pkg[len - 1] == '/')
140 /* Reset some state */
144 sprintf(LogDir, "%s/%s", LOG_DIR, pkg);
146 isinstalled = isinstalledpkg(pkg);
147 if (isinstalled == 0) {
148 warnx("no such package '%s' installed", pkg);
150 } else if (isinstalled < 0) {
151 warnx("the package info for package '%s' is corrupt%s",
152 pkg, Force ? " (but I'll delete it anyway)" : " (use -f to force removal)");
156 if (vsystem("%s -rf %s", REMOVE_CMD, LogDir)) {
157 warnx("couldn't remove log entry in %s, deinstall failed", LogDir);
159 warnx("couldn't completely deinstall package '%s',\n"
160 "only the log entry in %s was removed", pkg, LogDir);
166 if (!getcwd(home, FILENAME_MAX)) {
168 errx(2, "%s: unable to get current working directory!", __func__);
171 if (chdir(LogDir) == FAIL) {
172 warnx("unable to change directory to %s! deinstall failed", LogDir);
176 if (Interactive == TRUE) {
179 fprintf(stderr, "delete %s? ", pkg);
181 first = ch = getchar();
182 while (ch != '\n' && ch != EOF)
184 if (first != 'y' && first != 'Y')
189 if (requiredby(pkg, &rb_list, FALSE, TRUE) < 0)
191 if (!STAILQ_EMPTY(rb_list)) {
192 warnx("package '%s' is required by these other packages\n"
193 "and may not be deinstalled%s:",
194 pkg, Force ? " (but I'll delete it anyway)" : "");
195 STAILQ_FOREACH(rb_entry, rb_list, link)
196 fprintf(stderr, "%s\n", rb_entry->pkgname);
201 sanity_check(LogDir);
202 cfile = fopen(CONTENTS_FNAME, "r");
205 warnx("unable to open '%s' file", CONTENTS_FNAME);
209 /* If we have a prefix, add it now */
211 add_plist(&Plist, PLIST_CWD, Prefix);
212 read_plist(&Plist, cfile);
214 p = find_plist(&Plist, PLIST_CWD);
217 warnx("package '%s' doesn't have a prefix", pkg);
221 setenv(PKG_PREFIX_VNAME, p->name, 1);
223 if (fexists(REQUIRE_FNAME)) {
225 printf("Executing 'require' script.\n");
226 vsystem("/bin/chmod +x %s", REQUIRE_FNAME); /* be sure */
227 if (vsystem("./%s %s DEINSTALL", REQUIRE_FNAME, pkg)) {
228 warnx("package %s fails requirements %s", pkg,
229 Force ? "" : "- not deleted");
236 * Test whether to use the old method of passing tokens to deinstallation
237 * scripts, and set appropriate variables..
240 if (fexists(POST_DEINSTALL_FNAME)) {
242 post_script = POST_DEINSTALL_FNAME;
243 pre_arg = post_arg = "";
244 } else if (fexists(DEINSTALL_FNAME)) {
245 post_script = DEINSTALL_FNAME;
246 pre_arg = "DEINSTALL";
247 post_arg = "POST-DEINSTALL";
249 post_script = pre_arg = post_arg = NULL;
252 if (!NoDeInstall && pre_script != NULL && fexists(pre_script)) {
254 printf("Would execute de-install script at this point.\n");
256 vsystem("/bin/chmod +x %s", pre_script); /* make sure */
257 if (vsystem("./%s %s %s", pre_script, pkg, pre_arg)) {
258 warnx("deinstall script returned error status");
265 for (p = Plist.head; p ; p = p->next) {
266 if (p->type != PLIST_PKGDEP)
268 deporigin = (p->next->type == PLIST_DEPORIGIN) ? p->next->name :
271 printf("Trying to remove dependency on package '%s'", p->name);
272 if (deporigin != NULL)
273 printf(" with '%s' origin", deporigin);
277 depnames = (deporigin != NULL) ? matchbyorigin(deporigin, NULL) :
279 if (depnames == NULL) {
280 depnames = alloca(sizeof(*depnames) * 2);
281 depnames[0] = p->name;
284 for (i = 0; depnames[i] != NULL; i++)
285 undepend(depnames[i], pkg);
289 if (chdir(home) == FAIL) {
291 errx(2, "%s: unable to return to working directory %s!", __func__,
296 * Some packages aren't packed right, so we need to just ignore
297 * delete_package()'s status. Ugh! :-(
299 if (delete_package(FALSE, CleanDirs, &Plist) == FAIL)
301 "couldn't entirely delete package (perhaps the packing list is\n"
302 "incorrectly specified?)");
304 if (chdir(LogDir) == FAIL) {
305 warnx("unable to change directory to %s! deinstall failed", LogDir);
309 if (!NoDeInstall && post_script != NULL && fexists(post_script)) {
311 printf("Would execute post-deinstall script at this point.\n");
313 vsystem("/bin/chmod +x %s", post_script); /* make sure */
314 if (vsystem("./%s %s %s", post_script, pkg, post_arg)) {
315 warnx("post-deinstall script returned error status");
322 if (chdir(home) == FAIL) {
324 errx(2, "%s: unable to return to working directory %s!", __func__,
329 if (vsystem("%s -r%c %s", REMOVE_CMD, Force ? 'f' : ' ', LogDir)) {
330 warnx("couldn't remove log entry in %s, deinstall failed", LogDir);
339 sanity_check(char *pkg)
341 if (!fexists(CONTENTS_FNAME)) {
343 errx(2, "%s: installed package %s has no %s file!", __func__,
344 pkg, CONTENTS_FNAME);
356 undepend(char *p, char *pkgname)
358 char fname[FILENAME_MAX], ftmp[FILENAME_MAX];
361 struct reqr_by_entry *rb_entry;
362 struct reqr_by_head *rb_list;
365 if (requiredby(p, &rb_list, Verbose, FALSE) <= 0)
367 snprintf(fname, sizeof(fname), "%s/%s/%s", LOG_DIR, p, REQUIRED_BY_FNAME);
368 snprintf(ftmp, sizeof(ftmp), "%s.XXXXXX", fname);
371 warnx("couldn't open temp file '%s'", ftmp);
374 fpwr = fdopen(s, "w");
377 warnx("couldn't fdopen temp file '%s'", ftmp);
380 STAILQ_FOREACH(rb_entry, rb_list, link)
381 if (strcmp(rb_entry->pkgname, pkgname)) /* no match */
382 fputs(rb_entry->pkgname, fpwr), putc('\n', fpwr);
383 if (fchmod(s, 0644) == FAIL) {
384 warnx("error changing permission of temp file '%s'", ftmp);
388 if (fclose(fpwr) == EOF) {
389 warnx("error closing temp file '%s'", ftmp);
392 if (rename(ftmp, fname) == -1)
393 warnx("error renaming '%s' to '%s'", ftmp, fname);