]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/pkg_install/delete/perform.c
This commit was generated by cvs2svn to compensate for changes in r163356,
[FreeBSD/FreeBSD.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, **depnames, 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;
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->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             depnames = (deporigin != NULL) ? matchbyorigin(deporigin, NULL) :
279                                              NULL;
280             if (depnames == NULL) {
281                 depnames = alloca(sizeof(*depnames) * 2);
282                 depnames[0] = p->name;
283                 depnames[1] = NULL;
284             }
285             for (i = 0; depnames[i] != NULL; i++)
286                 undepend(depnames[i], pkg);
287         }
288     }
289
290     if (chdir(home) == FAIL) {
291         cleanup(0);
292         errx(2, "%s: unable to return to working directory %s!", __func__,
293             home);
294     }
295
296     /*
297      * Some packages aren't packed right, so we need to just ignore
298      * delete_package()'s status.  Ugh! :-(
299      */
300     if (delete_package(FALSE, CleanDirs, &Plist) == FAIL)
301         warnx(
302         "couldn't entirely delete package (perhaps the packing list is\n"
303         "incorrectly specified?)");
304
305     if (chdir(LogDir) == FAIL) {
306         warnx("unable to change directory to %s! deinstall failed", LogDir);
307         return 1;
308     }
309
310     if (!NoDeInstall && post_script != NULL && fexists(post_script)) {
311         if (Fake)
312             printf("Would execute post-deinstall script at this point.\n");
313         else {
314             vsystem("/bin/chmod +x %s", post_script);   /* make sure */
315             if (vsystem("./%s %s %s", post_script, pkg, post_arg)) {
316                 warnx("post-deinstall script returned error status");
317                 if (!Force)
318                     return 1;
319             }
320         }
321     }
322
323     if (chdir(home) == FAIL) {
324         cleanup(0);
325         errx(2, "%s: unable to return to working directory %s!", __func__,
326             home);
327     }
328
329     if (!Fake) {
330         if (vsystem("%s -r%c %s", REMOVE_CMD, Force ? 'f' : ' ', LogDir)) {
331             warnx("couldn't remove log entry in %s, deinstall failed", LogDir);
332             if (!Force)
333                 return 1;
334         }
335     }
336     return 0;
337 }
338
339 static void
340 sanity_check(char *pkg)
341 {
342     if (!fexists(CONTENTS_FNAME)) {
343         cleanup(0);
344         errx(2, "%s: installed package %s has no %s file!", __func__,
345             pkg, CONTENTS_FNAME);
346     }
347 }
348
349 void
350 cleanup(int sig)
351 {
352     if (sig)
353         exit(1);
354 }
355
356 static void
357 undepend(char *p, char *pkgname)
358 {
359     char fname[FILENAME_MAX], ftmp[FILENAME_MAX];
360     FILE *fpwr;
361     int s;
362     struct reqr_by_entry *rb_entry;
363     struct reqr_by_head *rb_list;
364
365
366     if (requiredby(p, &rb_list, Verbose, FALSE) <= 0)
367         return;
368     snprintf(fname, sizeof(fname), "%s/%s/%s", LOG_DIR, p, REQUIRED_BY_FNAME);
369     snprintf(ftmp, sizeof(ftmp), "%s.XXXXXX", fname);
370     s = mkstemp(ftmp);
371     if (s == -1) {
372         warnx("couldn't open temp file '%s'", ftmp);
373         return;
374     }
375     fpwr = fdopen(s, "w");
376     if (fpwr == NULL) {
377         close(s);
378         warnx("couldn't fdopen temp file '%s'", ftmp);
379         goto cleanexit;
380     }
381     STAILQ_FOREACH(rb_entry, rb_list, link)
382         if (strcmp(rb_entry->pkgname, pkgname))         /* no match */
383             fputs(rb_entry->pkgname, fpwr), putc('\n', fpwr);
384     if (fchmod(s, 0644) == FAIL) {
385         warnx("error changing permission of temp file '%s'", ftmp);
386         fclose(fpwr);
387         goto cleanexit;
388     }
389     if (fclose(fpwr) == EOF) {
390         warnx("error closing temp file '%s'", ftmp);
391         goto cleanexit;
392     }
393     if (rename(ftmp, fname) == -1)
394         warnx("error renaming '%s' to '%s'", ftmp, fname);
395 cleanexit:
396     remove(ftmp);
397     return;
398 }