Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / usr.sbin / pkg_install / add / 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 add module.
18  *
19  * $FreeBSD: src/usr.sbin/pkg_install/add/perform.c,v 1.57.2.15 2002/09/25 23:22:13 bmah Exp $
20  * $DragonFly: src/usr.sbin/pkg_install/add/Attic/perform.c,v 1.2 2003/06/17 04:29:59 dillon Exp $
21  */
22
23 #include <err.h>
24 #include <paths.h>
25 #include "lib.h"
26 #include "add.h"
27
28 #include <libgen.h>
29 #include <signal.h>
30 #include <sys/wait.h>
31
32 static int pkg_do(char *);
33 static int sanity_check(char *);
34 static char LogDir[FILENAME_MAX];
35 static int zapLogDir;           /* Should we delete LogDir? */
36
37 int
38 pkg_perform(char **pkgs)
39 {
40     int i, err_cnt = 0;
41
42     signal(SIGINT, cleanup);
43     signal(SIGHUP, cleanup);
44
45     if (AddMode == SLAVE)
46         err_cnt = pkg_do(NULL);
47     else {
48         for (i = 0; pkgs[i]; i++)
49             err_cnt += pkg_do(pkgs[i]);
50     }
51     return err_cnt;
52 }
53
54 static Package Plist;
55 static char *Home;
56
57 /*
58  * This is seriously ugly code following.  Written very fast!
59  * [And subsequently made even worse..  Sigh!  This code was just born
60  * to be hacked, I guess.. :) -jkh]
61  */
62 static int
63 pkg_do(char *pkg)
64 {
65     char pkg_fullname[FILENAME_MAX];
66     char playpen[FILENAME_MAX];
67     char extract_contents[FILENAME_MAX];
68     char *where_to, *extract;
69     FILE *cfile;
70     int code;
71     PackingList p;
72     struct stat sb;
73     int inPlace;
74     /* support for separate pre/post install scripts */
75     int new_m = 0;
76     char pre_script[FILENAME_MAX] = INSTALL_FNAME;
77     char post_script[FILENAME_MAX];
78     char pre_arg[FILENAME_MAX], post_arg[FILENAME_MAX];
79
80     code = 0;
81     zapLogDir = 0;
82     LogDir[0] = '\0';
83     strcpy(playpen, FirstPen);
84     inPlace = 0;
85
86     /* Are we coming in for a second pass, everything already extracted? */
87     if (!pkg) {
88         fgets(playpen, FILENAME_MAX, stdin);
89         playpen[strlen(playpen) - 1] = '\0'; /* pesky newline! */
90         if (chdir(playpen) == FAIL) {
91             warnx("pkg_add in SLAVE mode can't chdir to %s", playpen);
92             return 1;
93         }
94         read_plist(&Plist, stdin);
95         where_to = playpen;
96     }
97     /* Nope - do it now */
98     else {
99         /* Is it an ftp://foo.bar.baz/file.t[bg]z specification? */
100         if (isURL(pkg)) {
101             if (!(Home = fileGetURL(NULL, pkg))) {
102                 warnx("unable to fetch '%s' by URL", pkg);
103                 return 1;
104             }
105             where_to = Home;
106             strcpy(pkg_fullname, pkg);
107             cfile = fopen(CONTENTS_FNAME, "r");
108             if (!cfile) {
109                 warnx(
110                 "unable to open table of contents file '%s' - not a package?",
111                 CONTENTS_FNAME);
112                 goto bomb;
113             }
114             read_plist(&Plist, cfile);
115             fclose(cfile);
116         }
117         else {
118             strcpy(pkg_fullname, pkg);          /*
119                                                  * Copy for sanity's sake,
120                                                  * could remove pkg_fullname
121                                                  */
122             if (strcmp(pkg, "-")) {
123                 if (stat(pkg_fullname, &sb) == FAIL) {
124                     warnx("can't stat package file '%s'", pkg_fullname);
125                     goto bomb;
126                 }
127                 sprintf(extract_contents, "--fast-read %s", CONTENTS_FNAME);
128                 extract = extract_contents;
129             }
130             else {
131                 extract = NULL;
132                 sb.st_size = 100000;    /* Make up a plausible average size */
133             }
134             Home = make_playpen(playpen, sb.st_size * 4);
135             if (!Home)
136                 errx(1, "unable to make playpen for %qd bytes", (long long)sb.st_size * 4);
137             where_to = Home;
138             /* Since we can call ourselves recursively, keep notes on where we came from */
139             if (!getenv("_TOP"))
140                 setenv("_TOP", Home, 1);
141             if (unpack(pkg_fullname, extract)) {
142                 warnx(
143         "unable to extract table of contents file from '%s' - not a package?",
144                 pkg_fullname);
145                 goto bomb;
146             }
147             cfile = fopen(CONTENTS_FNAME, "r");
148             if (!cfile) {
149                 warnx(
150         "unable to open table of contents file '%s' - not a package?",
151                 CONTENTS_FNAME);
152                 goto bomb;
153             }
154             read_plist(&Plist, cfile);
155             fclose(cfile);
156
157             /* Extract directly rather than moving?  Oh goodie! */
158             if (find_plist_option(&Plist, "extract-in-place")) {
159                 if (Verbose)
160                     printf("Doing in-place extraction for %s\n", pkg_fullname);
161                 p = find_plist(&Plist, PLIST_CWD);
162                 if (p) {
163                     if (!isdir(p->name) && !Fake) {
164                         if (Verbose)
165                             printf("Desired prefix of %s does not exist, creating..\n", p->name);
166                         vsystem("mkdir -p %s", p->name);
167                         if (chdir(p->name) == -1) {
168                             warn("unable to change directory to '%s'", p->name);
169                             goto bomb;
170                         }
171                     }
172                     where_to = p->name;
173                     inPlace = 1;
174                 }
175                 else {
176                     warnx(
177                 "no prefix specified in '%s' - this is a bad package!",
178                         pkg_fullname);
179                     goto bomb;
180                 }
181             }
182
183             /*
184              * Apply a crude heuristic to see how much space the package will
185              * take up once it's unpacked.  I've noticed that most packages
186              * compress an average of 75%, so multiply by 4 for good measure.
187              */
188
189             if (!extract && !inPlace && min_free(playpen) < sb.st_size * 4) {
190                 warnx("projected size of %qd exceeds available free space.\n"
191 "Please set your PKG_TMPDIR variable to point to a location with more\n"
192                        "free space and try again", (long long)sb.st_size * 4);
193                 warnx("not extracting %s\ninto %s, sorry!",
194                         pkg_fullname, where_to);
195                 goto bomb;
196             }
197
198             /* If this is a direct extract and we didn't want it, stop now */
199             if (inPlace && Fake)
200                 goto success;
201
202             /* Finally unpack the whole mess.  If extract is null we
203                already + did so so don't bother doing it again. */
204             if (extract && unpack(pkg_fullname, NULL)) {
205                 warnx("unable to extract '%s'!", pkg_fullname);
206                 goto bomb;
207             }
208         }
209
210         /* Check for sanity and dependencies */
211         if (sanity_check(pkg))
212             goto bomb;
213
214         /* If we're running in MASTER mode, just output the plist and return */
215         if (AddMode == MASTER) {
216             printf("%s\n", where_playpen());
217             write_plist(&Plist, stdout);
218             return 0;
219         }
220     }
221
222     /*
223      * If we have a prefix, delete the first one we see and add this
224      * one in place of it.
225      */
226     if (Prefix) {
227         delete_plist(&Plist, FALSE, PLIST_CWD, NULL);
228         add_plist_top(&Plist, PLIST_CWD, Prefix);
229     }
230
231     setenv(PKG_PREFIX_VNAME, (p = find_plist(&Plist, PLIST_CWD)) ? p->name : ".", 1);
232     /* Protect against old packages with bogus @name and origin fields */
233     if (Plist.name == NULL)
234         Plist.name = "anonymous";
235     if (Plist.origin == NULL)
236         Plist.origin = "anonymous/anonymous";
237
238     /*
239      * See if we're already registered either with the same name (the same
240      * version) or some other version with the same origin.
241      */
242     if ((isinstalledpkg(Plist.name) ||
243          matchbyorigin(Plist.origin, NULL) != NULL) && !Force) {
244         warnx("package '%s' or its older version already installed",
245               Plist.name);
246         code = 1;
247         goto success;   /* close enough for government work */
248     }
249
250     /* Now check the packing list for dependencies */
251     for (p = Plist.head; p ; p = p->next) {
252         char *deporigin;
253
254         if (p->type != PLIST_PKGDEP)
255             continue;
256         deporigin = (p->next->type == PLIST_DEPORIGIN) ? p->next->name : NULL;
257         if (Verbose) {
258             printf("Package '%s' depends on '%s'", Plist.name, p->name);
259             if (deporigin != NULL)
260                 printf(" with '%s' origin", deporigin);
261             printf(".\n");
262         }
263         if (!isinstalledpkg(p->name) &&
264             !(deporigin != NULL && matchbyorigin(deporigin, NULL) != NULL)) {
265             char path[FILENAME_MAX], *cp = NULL;
266
267             if (!Fake) {
268                 if (!isURL(pkg) && !getenv("PKG_ADD_BASE")) {
269                     const char *ext;
270
271                     ext = strrchr(pkg_fullname, '.');
272                     if (ext == NULL)
273                         ext = ".tgz";
274                     snprintf(path, FILENAME_MAX, "%s/%s%s", getenv("_TOP"), p->name, ext);
275                     if (fexists(path))
276                         cp = path;
277                     else
278                         cp = fileFindByPath(pkg, p->name);
279                     if (cp) {
280                         if (Verbose)
281                             printf("Loading it from %s.\n", cp);
282                         if (vsystem("pkg_add %s'%s'", Verbose ? "-v " : "", cp)) {
283                             warnx("autoload of dependency '%s' failed%s",
284                                 cp, Force ? " (proceeding anyway)" : "!");
285                             if (!Force)
286                                 ++code;
287                         }
288                     }
289                     else {
290                         warnx("could not find package %s %s",
291                               p->name, Force ? " (proceeding anyway)" : "!");
292                         if (!Force)
293                             ++code;
294                     }
295                 }
296                 else if ((cp = fileGetURL(pkg, p->name)) != NULL) {
297                     if (Verbose)
298                         printf("Finished loading %s over FTP.\n", p->name);
299                     if (!fexists("+CONTENTS")) {
300                         warnx("autoloaded package %s has no +CONTENTS file?",
301                                 p->name);
302                         if (!Force)
303                             ++code;
304                     }
305                     else if (vsystem("(pwd; cat +CONTENTS) | pkg_add %s-S", Verbose ? "-v " : "")) {
306                         warnx("pkg_add of dependency '%s' failed%s",
307                                 p->name, Force ? " (proceeding anyway)" : "!");
308                         if (!Force)
309                             ++code;
310                     }
311                     else if (Verbose)
312                         printf("\t'%s' loaded successfully.\n", p->name);
313                     /* Nuke the temporary playpen */
314                     leave_playpen();
315                 }
316             }
317             else {
318                 if (Verbose)
319                     printf("and was not found%s.\n", Force ? " (proceeding anyway)" : "");
320                 else
321                     printf("Package dependency %s for %s not found%s\n", p->name, pkg,
322                            Force ? " (proceeding anyway)" : "!");
323                 if (!Force)
324                     ++code;
325             }
326         }
327         else if (Verbose)
328             printf(" - already installed.\n");
329     }
330
331     if (code != 0)
332         goto bomb;
333
334     /* Look for the requirements file */
335     if (fexists(REQUIRE_FNAME)) {
336         vsystem("chmod +x %s", REQUIRE_FNAME);  /* be sure */
337         if (Verbose)
338             printf("Running requirements file first for %s..\n", Plist.name);
339         if (!Fake && vsystem("./%s %s INSTALL", REQUIRE_FNAME, Plist.name)) {
340             warnx("package %s fails requirements %s", pkg_fullname,
341                    Force ? "installing anyway" : "- not installed");
342             if (!Force) {
343                 code = 1;
344                 goto success;   /* close enough for government work */
345             }
346         }
347     }
348
349     /*
350      * Test whether to use the old method of passing tokens to installation
351      * scripts, and set appropriate variables..
352      */
353
354     if (fexists(POST_INSTALL_FNAME)) {
355         new_m = 1;
356         sprintf(post_script, "%s", POST_INSTALL_FNAME);
357         pre_arg[0] = '\0';
358         post_arg[0] = '\0';
359     } else {
360         if (fexists(INSTALL_FNAME)) {
361             sprintf(post_script, "%s", INSTALL_FNAME);
362             sprintf(pre_arg, "PRE-INSTALL");
363             sprintf(post_arg, "POST-INSTALL");
364         }
365     }
366
367     /* If we're really installing, and have an installation file, run it */
368     if (!NoInstall && fexists(pre_script)) {
369         vsystem("chmod +x %s", pre_script);     /* make sure */
370         if (Verbose)
371             printf("Running pre-install for %s..\n", Plist.name);
372         if (!Fake && vsystem("./%s %s %s", pre_script, Plist.name, pre_arg)) {
373             warnx("install script returned error status");
374             unlink(pre_script);
375             code = 1;
376             goto success;               /* nothing to uninstall yet */
377         }
378     }
379
380     /* Now finally extract the entire show if we're not going direct */
381     if (!inPlace && !Fake)
382         extract_plist(".", &Plist);
383
384     if (!Fake && fexists(MTREE_FNAME)) {
385         if (Verbose)
386             printf("Running mtree for %s..\n", Plist.name);
387         p = find_plist(&Plist, PLIST_CWD);
388         if (Verbose)
389             printf("mtree -U -f %s -d -e -p %s >%s\n", MTREE_FNAME, p ? p->name : "/", _PATH_DEVNULL);
390         if (!Fake) {
391             if (vsystem("/usr/sbin/mtree -U -f %s -d -e -p %s >%s", MTREE_FNAME, p ? p->name : "/", _PATH_DEVNULL))
392                 warnx("mtree returned a non-zero status - continuing");
393         }
394     }
395
396     /* Run the installation script one last time? */
397     if (!NoInstall && fexists(post_script)) {
398         vsystem("chmod +x %s", post_script);    /* make sure */
399         if (Verbose)
400             printf("Running post-install for %s..\n", Plist.name);
401         if (!Fake && vsystem("./%s %s %s", post_script, Plist.name, post_arg)) {
402             warnx("install script returned error status");
403             unlink(post_script);
404             code = 1;
405             goto fail;
406         }
407     }
408
409     /* Time to record the deed? */
410     if (!NoRecord && !Fake) {
411         char contents[FILENAME_MAX];
412         FILE *contfile;
413
414         if (getuid() != 0)
415             warnx("not running as root - trying to record install anyway");
416         sprintf(LogDir, "%s/%s", LOG_DIR, Plist.name);
417         zapLogDir = 1;
418         if (Verbose)
419             printf("Attempting to record package into %s..\n", LogDir);
420         if (make_hierarchy(LogDir)) {
421             warnx("can't record package into '%s', you're on your own!",
422                    LogDir);
423             bzero(LogDir, FILENAME_MAX);
424             code = 1;
425             goto success;       /* close enough for government work */
426         }
427         /* Make sure pkg_info can read the entry */
428         vsystem("chmod a+rx %s", LogDir);
429         move_file(".", DESC_FNAME, LogDir);
430         move_file(".", COMMENT_FNAME, LogDir);
431         if (fexists(INSTALL_FNAME))
432             move_file(".", INSTALL_FNAME, LogDir);
433         if (fexists(POST_INSTALL_FNAME))
434             move_file(".", POST_INSTALL_FNAME, LogDir);
435         if (fexists(DEINSTALL_FNAME))
436             move_file(".", DEINSTALL_FNAME, LogDir);
437         if (fexists(POST_DEINSTALL_FNAME))
438             move_file(".", POST_DEINSTALL_FNAME, LogDir);
439         if (fexists(REQUIRE_FNAME))
440             move_file(".", REQUIRE_FNAME, LogDir);
441         if (fexists(DISPLAY_FNAME))
442             move_file(".", DISPLAY_FNAME, LogDir);
443         if (fexists(MTREE_FNAME))
444             move_file(".", MTREE_FNAME, LogDir);
445         sprintf(contents, "%s/%s", LogDir, CONTENTS_FNAME);
446         contfile = fopen(contents, "w");
447         if (!contfile) {
448             warnx("can't open new contents file '%s'! can't register pkg",
449                 contents);
450             goto success; /* can't log, but still keep pkg */
451         }
452         write_plist(&Plist, contfile);
453         fclose(contfile);
454         for (p = Plist.head; p ; p = p->next) {
455             char *deporigin, **depnames;
456             int i;
457
458             if (p->type != PLIST_PKGDEP)
459                 continue;
460             deporigin = (p->next->type == PLIST_DEPORIGIN) ? p->next->name :
461                                                              NULL;
462             if (Verbose) {
463                 printf("Trying to record dependency on package '%s'", p->name);
464                 if (deporigin != NULL)
465                     printf(" with '%s' origin", deporigin);
466                 printf(".\n");
467             }
468
469             depnames = (deporigin != NULL) ? matchbyorigin(deporigin, NULL) :
470                                              NULL;
471             if (depnames == NULL) {
472                 depnames = alloca(sizeof(*depnames) * 2);
473                 depnames[0] = p->name;
474                 depnames[1] = NULL;
475             }
476             for (i = 0; depnames[i] != NULL; i++) {
477                 sprintf(contents, "%s/%s/%s", LOG_DIR, depnames[i],
478                         REQUIRED_BY_FNAME);
479                 if (strcmp(p->name, depnames[i]) != 0)
480                     warnx("warning: package '%s' requires '%s', but '%s' "
481                           "is installed", Plist.name, p->name, depnames[i]);
482                 contfile = fopen(contents, "a");
483                 if (!contfile)
484                     warnx("can't open dependency file '%s'!\n"
485                           "dependency registration is incomplete", contents);
486                 else {
487                     fprintf(contfile, "%s\n", Plist.name);
488                     if (fclose(contfile) == EOF)
489                         warnx("cannot properly close file %s", contents);
490                 }
491             }
492         }
493         if (Verbose)
494             printf("Package %s registered in %s\n", Plist.name, LogDir);
495     }
496
497     if ((p = find_plist(&Plist, PLIST_DISPLAY)) != NULL) {
498         FILE *fp;
499         char buf[BUFSIZ];
500
501         snprintf(buf, sizeof buf, "%s/%s", LogDir, p->name);
502         fp = fopen(buf, "r");
503         if (fp) {
504             putc('\n', stdout);
505             while (fgets(buf, sizeof(buf), fp))
506                 fputs(buf, stdout);
507             putc('\n', stdout);
508             (void) fclose(fp);
509         } else
510             warnx("cannot open %s as display file", buf);
511     }
512
513     goto success;
514
515  bomb:
516     code = 1;
517     goto success;
518
519  fail:
520     /* Nuke the whole (installed) show, XXX but don't clean directories */
521     if (!Fake)
522         delete_package(FALSE, FALSE, &Plist);
523
524  success:
525     /* delete the packing list contents */
526     free_plist(&Plist);
527     leave_playpen();
528     return code;
529 }
530
531 static int
532 sanity_check(char *pkg)
533 {
534     int code = 0;
535
536     if (!fexists(CONTENTS_FNAME)) {
537         warnx("package %s has no CONTENTS file!", pkg);
538         code = 1;
539     }
540     else if (!fexists(COMMENT_FNAME)) {
541         warnx("package %s has no COMMENT file!", pkg);
542         code = 1;
543     }
544     else if (!fexists(DESC_FNAME)) {
545         warnx("package %s has no DESC file!", pkg);
546         code = 1;
547     }
548     return code;
549 }
550
551 void
552 cleanup(int sig)
553 {
554     static int in_cleanup = 0;
555
556     if (!in_cleanup) {
557         in_cleanup = 1;
558         if (sig)
559             printf("Signal %d received, cleaning up..\n", sig);
560         if (!Fake && zapLogDir && LogDir[0])
561             vsystem("%s -rf %s", REMOVE_CMD, LogDir);
562         leave_playpen();
563     }
564     if (sig)
565         exit(1);
566 }