]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - usr.sbin/pkg_install/delete/perform.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / usr.sbin / pkg_install / delete / perform.c
1 /*
2  * FreeBSD install - a package for the installation and maintainance
3  * of non-core utilities.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
14  * Jordan K. Hubbard
15  * 18 July 1993
16  *
17  * This is the main body of the delete module.
18  *
19  */
20
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
23
24 #include <err.h>
25 #include "lib.h"
26 #include "delete.h"
27
28 static int pkg_do(char *);
29 static void sanity_check(char *);
30 static void undepend(char *, char *);
31 static char LogDir[FILENAME_MAX];
32
33
34 int
35 pkg_perform(char **pkgs)
36 {
37     char **matched, **rb, **rbtmp;
38     int errcode, i, j;
39     int err_cnt = 0;
40     struct reqr_by_entry *rb_entry;
41     struct reqr_by_head *rb_list;
42
43     if (MatchType != MATCH_EXACT) {
44         matched = matchinstalled(MatchType, pkgs, &errcode);
45         if (errcode != 0)
46             return 1;
47             /* Not reached */
48
49         /*
50          * Copy matched[] into pkgs[], because we'll need to use
51          * matchinstalled() later on.
52          */
53         if (matched != NULL) {
54             pkgs = NULL;
55             for (i = 0; matched[i] != NULL; i++) {
56                 pkgs = realloc(pkgs, sizeof(*pkgs) * (i + 2));
57                 pkgs[i] = strdup(matched[i]);
58             }
59             pkgs[i] = NULL;
60         }
61         else switch (MatchType) {
62             case MATCH_GLOB:
63                 break;
64             case MATCH_ALL:
65                 warnx("no packages installed");
66                 return 0;
67             case MATCH_EREGEX:
68             case MATCH_REGEX:
69                 warnx("no packages match pattern(s)");
70                 return 1;
71             default:
72                 break;
73         }
74     }
75
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);
80             if (errcode < 0) {
81                 err_cnt++;
82             } else if (errcode > 0) {
83                 /*
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.
88                  */
89                 rbtmp = rb = alloca((errcode + 1) * sizeof(*rb));
90                 if (rb == NULL) {
91                     warnx("%s(): alloca() failed", __func__);
92                     err_cnt++;
93                     continue;
94                 }
95                 STAILQ_FOREACH(rb_entry, rb_list, link) {
96                     *rbtmp = alloca(strlen(rb_entry->pkgname) + 1);
97                     if (*rbtmp == NULL) {
98                         warnx("%s(): alloca() failed", __func__);
99                         err_cnt++;
100                         continue;
101                     }
102                     strcpy(*rbtmp, rb_entry->pkgname);
103                     rbtmp++;
104                 }
105                 *rbtmp = NULL;
106
107                 err_cnt += sortdeps(rb);
108                 for (j = 0; rb[j]; j++)
109                     err_cnt += pkg_do(rb[j]);
110             }
111         }
112         err_cnt += pkg_do(pkgs[i]);
113     }
114
115     return err_cnt;
116 }
117
118 static Package Plist;
119
120 /* This is seriously ugly code following.  Written very fast! */
121 static int
122 pkg_do(char *pkg)
123 {
124     FILE *cfile;
125     char *deporigin, **deporigins = NULL, **depnames = NULL, ***depmatches, home[FILENAME_MAX];
126     PackingList p;
127     int i, len;
128     int isinstalled;
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;
135
136     if (!pkg || !(len = strlen(pkg)))
137         return 1;
138     if (pkg[len - 1] == '/')
139         pkg[len - 1] = '\0';
140
141     /* Reset some state */
142     if (Plist.head)
143         free_plist(&Plist);
144
145     sprintf(LogDir, "%s/%s", LOG_DIR, pkg);
146
147     isinstalled = isinstalledpkg(pkg);
148     if (isinstalled == 0) {
149         warnx("no such package '%s' installed", pkg);
150         return 1;
151     } else if (isinstalled < 0) {
152         warnx("the package info for package '%s' is corrupt%s",
153               pkg, Force ? " (but I'll delete it anyway)" : " (use -f to force removal)");
154         if (!Force)
155             return 1;
156         if (!Fake) {
157             if (vsystem("%s -rf %s", REMOVE_CMD, LogDir)) {
158                 warnx("couldn't remove log entry in %s, deinstall failed", LogDir);
159             } else {
160                 warnx("couldn't completely deinstall package '%s',\n"
161                       "only the log entry in %s was removed", pkg, LogDir);
162             }
163         }
164         return 0;
165     }
166
167     if (!getcwd(home, FILENAME_MAX)) {
168         cleanup(0);
169         errx(2, "%s: unable to get current working directory!", __func__);
170     }
171
172     if (chdir(LogDir) == FAIL) {
173         warnx("unable to change directory to %s! deinstall failed", LogDir);
174         return 1;
175     }
176
177     if (Interactive == TRUE) {
178         int first, ch;
179
180         (void)fprintf(stderr, "delete %s? ", pkg);
181         (void)fflush(stderr);
182         first = ch = getchar();
183         while (ch != '\n' && ch != EOF)
184             ch = getchar();
185         if (first != 'y' && first != 'Y')
186             return 0;
187             /* Not reached */
188     }
189
190     if (requiredby(pkg, &rb_list, FALSE, TRUE) < 0)
191         return 1;
192     if (!STAILQ_EMPTY(rb_list)) {
193         warnx("package '%s' is required by these other packages\n"
194               "and may not be deinstalled%s:",
195               pkg, Force ? " (but I'll delete it anyway)" : "");
196         STAILQ_FOREACH(rb_entry, rb_list, link)
197             fprintf(stderr, "%s\n", rb_entry->pkgname);
198         if (!Force)
199             return 1;
200     }
201
202     sanity_check(LogDir);
203     cfile = fopen(CONTENTS_FNAME, "r");
204
205     if (!cfile) {
206         warnx("unable to open '%s' file", CONTENTS_FNAME);
207         return 1;
208     }
209
210     /* If we have a prefix, add it now */
211     if (Prefix)
212         add_plist(&Plist, PLIST_CWD, Prefix);
213     read_plist(&Plist, cfile);
214     fclose(cfile);
215     p = find_plist(&Plist, PLIST_CWD);
216
217     if (!p) {
218         warnx("package '%s' doesn't have a prefix", pkg);
219         return 1;
220     }
221
222     setenv(PKG_PREFIX_VNAME, p->name, 1);
223
224     if (fexists(REQUIRE_FNAME)) {
225         if (Verbose)
226             printf("Executing 'require' script.\n");
227         vsystem("/bin/chmod +x %s", REQUIRE_FNAME);     /* be sure */
228         if (vsystem("./%s %s DEINSTALL", REQUIRE_FNAME, pkg)) {
229             warnx("package %s fails requirements %s", pkg,
230                    Force ? "" : "- not deleted");
231             if (!Force)
232                 return 1;
233         }
234     }
235
236     /*
237      * Test whether to use the old method of passing tokens to deinstallation
238      * scripts, and set appropriate variables..
239      */
240
241     if (fexists(POST_DEINSTALL_FNAME)) {
242         new_m = 1;
243         post_script = POST_DEINSTALL_FNAME;
244         pre_arg = post_arg = "";
245     } else if (fexists(DEINSTALL_FNAME)) {
246         post_script = DEINSTALL_FNAME;
247         pre_arg = "DEINSTALL";
248         post_arg = "POST-DEINSTALL";
249     } else {
250         post_script = pre_arg = post_arg = NULL;
251     }
252
253     if (!NoDeInstall && pre_script != NULL && fexists(pre_script)) {
254         if (Fake)
255             printf("Would execute de-install script at this point.\n");
256         else {
257             vsystem("/bin/chmod +x %s", pre_script);    /* make sure */
258             if (vsystem("./%s %s %s", pre_script, pkg, pre_arg)) {
259                 warnx("deinstall script returned error status");
260                 if (!Force)
261                     return 1;
262             }
263         }
264     }
265
266     for (p = Plist.head; p ; p = p->next) {
267         if (p->type != PLIST_PKGDEP)
268             continue;
269         deporigin = (p->next != NULL && p->next->type == PLIST_DEPORIGIN) ? p->next->name :
270                                                          NULL;
271         if (Verbose) {
272             printf("Trying to remove dependency on package '%s'", p->name);
273             if (deporigin != NULL)
274                 printf(" with '%s' origin", deporigin);
275             printf(".\n");
276         }
277         if (!Fake) {
278             if (deporigin) {
279                 deporigins = realloc(deporigins, (dep_count + 2) * sizeof(*deporigins));
280                 depnames = realloc(depnames, (dep_count + 1) * sizeof(*depnames));
281                 deporigins[dep_count] = deporigin;
282                 deporigins[dep_count + 1] = NULL;
283                 depnames[dep_count] = p->name;
284                 dep_count++;
285             } else {
286                 undepend(p->name, pkg);
287             }
288         }
289     }
290
291     if (dep_count > 0) {
292         /* Undepend all the dependencies at once */
293         depmatches = matchallbyorigin((const char **)deporigins, NULL);
294         free(deporigins);
295         if (depmatches) {
296             for (i = 0; i < dep_count; i++) {
297                 if (depmatches[i]) {
298                     char **tmp = depmatches[i];
299                     int j;
300                     for (j = 0; tmp[j] != NULL; j++)
301                         undepend(tmp[j], pkg);
302                 } else if (depnames[i]) {
303                     undepend(depnames[i], pkg);
304                 }
305             }
306         }
307     }
308
309     if (chdir(home) == FAIL) {
310         cleanup(0);
311         errx(2, "%s: unable to return to working directory %s!", __func__,
312             home);
313     }
314
315     /*
316      * Some packages aren't packed right, so we need to just ignore
317      * delete_package()'s status.  Ugh! :-(
318      */
319     if (delete_package(FALSE, CleanDirs, &Plist) == FAIL)
320         warnx(
321         "couldn't entirely delete package (perhaps the packing list is\n"
322         "incorrectly specified?)");
323
324     if (chdir(LogDir) == FAIL) {
325         warnx("unable to change directory to %s! deinstall failed", LogDir);
326         return 1;
327     }
328
329     if (!NoDeInstall && post_script != NULL && fexists(post_script)) {
330         if (Fake)
331             printf("Would execute post-deinstall script at this point.\n");
332         else {
333             vsystem("/bin/chmod +x %s", post_script);   /* make sure */
334             if (vsystem("./%s %s %s", post_script, pkg, post_arg)) {
335                 warnx("post-deinstall script returned error status");
336                 if (!Force)
337                     return 1;
338             }
339         }
340     }
341
342     if (chdir(home) == FAIL) {
343         cleanup(0);
344         errx(2, "%s: unable to return to working directory %s!", __func__,
345             home);
346     }
347
348     if (!Fake) {
349         if (vsystem("%s -r%c %s", REMOVE_CMD, Force ? 'f' : ' ', LogDir)) {
350             warnx("couldn't remove log entry in %s, deinstall failed", LogDir);
351             if (!Force)
352                 return 1;
353         }
354     }
355     return 0;
356 }
357
358 static void
359 sanity_check(char *pkg)
360 {
361     if (!fexists(CONTENTS_FNAME)) {
362         cleanup(0);
363         errx(2, "%s: installed package %s has no %s file!", __func__,
364             pkg, CONTENTS_FNAME);
365     }
366 }
367
368 void
369 cleanup(int sig)
370 {
371     if (sig)
372         exit(1);
373 }
374
375 static void
376 undepend(char *p, char *pkgname)
377 {
378     char fname[FILENAME_MAX], ftmp[FILENAME_MAX];
379     FILE *fpwr;
380     int s;
381     struct reqr_by_entry *rb_entry;
382     struct reqr_by_head *rb_list;
383
384
385     if (requiredby(p, &rb_list, Verbose, FALSE) <= 0)
386         return;
387     snprintf(fname, sizeof(fname), "%s/%s/%s", LOG_DIR, p, REQUIRED_BY_FNAME);
388     snprintf(ftmp, sizeof(ftmp), "%s.XXXXXX", fname);
389     s = mkstemp(ftmp);
390     if (s == -1) {
391         warnx("couldn't open temp file '%s'", ftmp);
392         return;
393     }
394     fpwr = fdopen(s, "w");
395     if (fpwr == NULL) {
396         close(s);
397         warnx("couldn't fdopen temp file '%s'", ftmp);
398         goto cleanexit;
399     }
400     STAILQ_FOREACH(rb_entry, rb_list, link)
401         if (strcmp(rb_entry->pkgname, pkgname))         /* no match */
402             fputs(rb_entry->pkgname, fpwr), putc('\n', fpwr);
403     if (fchmod(s, 0644) == FAIL) {
404         warnx("error changing permission of temp file '%s'", ftmp);
405         fclose(fpwr);
406         goto cleanexit;
407     }
408     if (fclose(fpwr) == EOF) {
409         warnx("error closing temp file '%s'", ftmp);
410         goto cleanexit;
411     }
412     if (rename(ftmp, fname) == -1)
413         warnx("error renaming '%s' to '%s'", ftmp, fname);
414 cleanexit:
415     remove(ftmp);
416     return;
417 }