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