Initial import from FreeBSD RELENG_4:
[dragonfly.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: src/usr.sbin/pkg_install/delete/perform.c,v 1.22.2.12 2003/02/28 13:58:14 des Exp $");
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_REGEX:
68                 warnx("no packages match pattern(s)");
69                 return 1;
70             default:
71                 break;
72         }
73     }
74
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);
79             if (errcode < 0) {
80                 err_cnt++;
81             } else if (errcode > 0) {
82                 /*
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.
87                  */
88                 rbtmp = rb = alloca((errcode + 1) * sizeof(*rb));
89                 if (rb == NULL) {
90                     warnx("%s(): alloca() failed", __func__);
91                     err_cnt++;
92                     continue;
93                 }
94                 STAILQ_FOREACH(rb_entry, rb_list, link) {
95                     *rbtmp = alloca(strlen(rb_entry->pkgname) + 1);
96                     if (*rbtmp == NULL) {
97                         warnx("%s(): alloca() failed", __func__);
98                         err_cnt++;
99                         continue;
100                     }
101                     strcpy(*rbtmp, rb_entry->pkgname);
102                     rbtmp++;
103                 }
104                 *rbtmp = NULL;
105
106                 err_cnt += sortdeps(rb);
107                 for (j = 0; rb[j]; j++)
108                     err_cnt += pkg_do(rb[j]);
109             }
110         }
111         err_cnt += pkg_do(pkgs[i]);
112     }
113
114     return err_cnt;
115 }
116
117 static Package Plist;
118
119 /* This is seriously ugly code following.  Written very fast! */
120 static int
121 pkg_do(char *pkg)
122 {
123     FILE *cfile;
124     char *deporigin, **depnames, home[FILENAME_MAX];
125     PackingList p;
126     int i, len;
127     /* support for separate pre/post install scripts */
128     int new_m = 0;
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;
133
134     if (!pkg || !(len = strlen(pkg)))
135         return 1;
136     if (pkg[len - 1] == '/')
137         pkg[len - 1] = '\0';
138
139     /* Reset some state */
140     if (Plist.head)
141         free_plist(&Plist);
142
143     if (!isinstalledpkg(pkg)) {
144         warnx("no such package '%s' installed", pkg);
145         return 1;
146     }
147
148     if (!getcwd(home, FILENAME_MAX)) {
149         cleanup(0);
150         errx(2, "%s: unable to get current working directory!", __func__);
151     }
152
153     sprintf(LogDir, "%s/%s", LOG_DIR, pkg);
154
155     if (chdir(LogDir) == FAIL) {
156         warnx("unable to change directory to %s! deinstall failed", LogDir);
157         return 1;
158     }
159
160     if (Interactive == TRUE) {
161         int first, ch;
162
163         (void)fprintf(stderr, "delete %s? ", pkg);
164         (void)fflush(stderr);
165         first = ch = getchar();
166         while (ch != '\n' && ch != EOF)
167             ch = getchar();
168         if (first != 'y' && first != 'Y')
169             return 0;
170             /* Not reached */
171     }
172
173     if (requiredby(pkg, &rb_list, FALSE, TRUE) < 0)
174         return 1;
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);
181         if (!Force)
182             return 1;
183     }
184
185     sanity_check(LogDir);
186     cfile = fopen(CONTENTS_FNAME, "r");
187
188     if (!cfile) {
189         warnx("unable to open '%s' file", CONTENTS_FNAME);
190         return 1;
191     }
192
193     /* If we have a prefix, add it now */
194     if (Prefix)
195         add_plist(&Plist, PLIST_CWD, Prefix);
196     read_plist(&Plist, cfile);
197     fclose(cfile);
198     p = find_plist(&Plist, PLIST_CWD);
199
200     if (!p) {
201         warnx("package '%s' doesn't have a prefix", pkg);
202         return 1;
203     }
204
205     setenv(PKG_PREFIX_VNAME, p->name, 1);
206
207     if (fexists(REQUIRE_FNAME)) {
208         if (Verbose)
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");
214             if (!Force)
215                 return 1;
216         }
217     }
218
219     /*
220      * Test whether to use the old method of passing tokens to deinstallation
221      * scripts, and set appropriate variables..
222      */
223
224     if (fexists(POST_DEINSTALL_FNAME)) {
225         new_m = 1;
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";
232     } else {
233         post_script = pre_arg = post_arg = NULL;
234     }
235
236     if (!NoDeInstall && pre_script != NULL && fexists(pre_script)) {
237         if (Fake)
238             printf("Would execute de-install script at this point.\n");
239         else {
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");
243                 if (!Force)
244                     return 1;
245             }
246         }
247     }
248
249     if (chdir(home) == FAIL) {
250         cleanup(0);
251         errx(2, "%s: unable to return to working directory %s!", __func__,
252             home);
253     }
254
255     /*
256      * Some packages aren't packed right, so we need to just ignore
257      * delete_package()'s status.  Ugh! :-(
258      */
259     if (delete_package(FALSE, CleanDirs, &Plist) == FAIL)
260         warnx(
261         "couldn't entirely delete package (perhaps the packing list is\n"
262         "incorrectly specified?)");
263
264     if (chdir(LogDir) == FAIL) {
265         warnx("unable to change directory to %s! deinstall failed", LogDir);
266         return 1;
267     }
268
269     if (!NoDeInstall && post_script != NULL && fexists(post_script)) {
270         if (Fake)
271             printf("Would execute post-deinstall script at this point.\n");
272         else {
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");
276                 if (!Force)
277                     return 1;
278             }
279         }
280     }
281
282     if (chdir(home) == FAIL) {
283         cleanup(0);
284         errx(2, "%s: unable to return to working directory %s!", __func__,
285             home);
286     }
287
288     if (!Fake) {
289         if (vsystem("%s -r%c %s", REMOVE_CMD, Force ? 'f' : ' ', LogDir)) {
290             warnx("couldn't remove log entry in %s, deinstall failed", LogDir);
291             if (!Force)
292                 return 1;
293         }
294     }
295
296     for (p = Plist.head; p ; p = p->next) {
297         if (p->type != PLIST_PKGDEP)
298             continue;
299         deporigin = (p->next->type == PLIST_DEPORIGIN) ? p->next->name :
300                                                          NULL;
301         if (Verbose) {
302             printf("Trying to remove dependency on package '%s'", p->name);
303             if (deporigin != NULL)
304                 printf(" with '%s' origin", deporigin);
305             printf(".\n");
306         }
307         if (!Fake) {
308             depnames = (deporigin != NULL) ? matchbyorigin(deporigin, NULL) :
309                                              NULL;
310             if (depnames == NULL) {
311                 depnames = alloca(sizeof(*depnames) * 2);
312                 depnames[0] = p->name;
313                 depnames[1] = NULL;
314             }
315             for (i = 0; depnames[i] != NULL; i++)
316                 undepend(depnames[i], pkg);
317         }
318     }
319     return 0;
320 }
321
322 static void
323 sanity_check(char *pkg)
324 {
325     if (!fexists(CONTENTS_FNAME)) {
326         cleanup(0);
327         errx(2, "%s: installed package %s has no %s file!", __func__,
328             pkg, CONTENTS_FNAME);
329     }
330 }
331
332 void
333 cleanup(int sig)
334 {
335     if (sig)
336         exit(1);
337 }
338
339 static void
340 undepend(char *p, char *pkgname)
341 {
342     char fname[FILENAME_MAX], ftmp[FILENAME_MAX];
343     FILE *fpwr;
344     int s;
345     struct reqr_by_entry *rb_entry;
346     struct reqr_by_head *rb_list;
347
348
349     if (requiredby(p, &rb_list, Verbose, FALSE) <= 0)
350         return;
351     snprintf(fname, sizeof(fname), "%s/%s/%s", LOG_DIR, p, REQUIRED_BY_FNAME);
352     snprintf(ftmp, sizeof(ftmp), "%s.XXXXXX", fname);
353     s = mkstemp(ftmp);
354     if (s == -1) {
355         warnx("couldn't open temp file '%s'", ftmp);
356         return;
357     }
358     fpwr = fdopen(s, "w");
359     if (fpwr == NULL) {
360         close(s);
361         warnx("couldn't fdopen temp file '%s'", ftmp);
362         goto cleanexit;
363     }
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);
369         fclose(fpwr);
370         goto cleanexit;
371     }
372     if (fclose(fpwr) == EOF) {
373         warnx("error closing temp file '%s'", ftmp);
374         goto cleanexit;
375     }
376     if (rename(ftmp, fname) == -1)
377         warnx("error renaming '%s' to '%s'", ftmp, fname);
378 cleanexit:
379     remove(ftmp);
380     return;
381 }