2 * FreeBSD install - a package for the installation and maintainance
3 * of non-core utilities.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
17 * This is the main body of the add module.
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 $
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? */
38 pkg_perform(char **pkgs)
42 signal(SIGINT, cleanup);
43 signal(SIGHUP, cleanup);
46 err_cnt = pkg_do(NULL);
48 for (i = 0; pkgs[i]; i++)
49 err_cnt += pkg_do(pkgs[i]);
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]
65 char pkg_fullname[FILENAME_MAX];
66 char playpen[FILENAME_MAX];
67 char extract_contents[FILENAME_MAX];
68 char *where_to, *extract;
74 /* support for separate pre/post install scripts */
76 char pre_script[FILENAME_MAX] = INSTALL_FNAME;
77 char post_script[FILENAME_MAX];
78 char pre_arg[FILENAME_MAX], post_arg[FILENAME_MAX];
83 strcpy(playpen, FirstPen);
86 /* Are we coming in for a second pass, everything already extracted? */
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);
94 read_plist(&Plist, stdin);
97 /* Nope - do it now */
99 /* Is it an ftp://foo.bar.baz/file.t[bg]z specification? */
101 if (!(Home = fileGetURL(NULL, pkg))) {
102 warnx("unable to fetch '%s' by URL", pkg);
106 strcpy(pkg_fullname, pkg);
107 cfile = fopen(CONTENTS_FNAME, "r");
110 "unable to open table of contents file '%s' - not a package?",
114 read_plist(&Plist, cfile);
118 strcpy(pkg_fullname, pkg); /*
119 * Copy for sanity's sake,
120 * could remove pkg_fullname
122 if (strcmp(pkg, "-")) {
123 if (stat(pkg_fullname, &sb) == FAIL) {
124 warnx("can't stat package file '%s'", pkg_fullname);
127 sprintf(extract_contents, "--fast-read %s", CONTENTS_FNAME);
128 extract = extract_contents;
132 sb.st_size = 100000; /* Make up a plausible average size */
134 Home = make_playpen(playpen, sb.st_size * 4);
136 errx(1, "unable to make playpen for %qd bytes", (long long)sb.st_size * 4);
138 /* Since we can call ourselves recursively, keep notes on where we came from */
140 setenv("_TOP", Home, 1);
141 if (unpack(pkg_fullname, extract)) {
143 "unable to extract table of contents file from '%s' - not a package?",
147 cfile = fopen(CONTENTS_FNAME, "r");
150 "unable to open table of contents file '%s' - not a package?",
154 read_plist(&Plist, cfile);
157 /* Extract directly rather than moving? Oh goodie! */
158 if (find_plist_option(&Plist, "extract-in-place")) {
160 printf("Doing in-place extraction for %s\n", pkg_fullname);
161 p = find_plist(&Plist, PLIST_CWD);
163 if (!isdir(p->name) && !Fake) {
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);
177 "no prefix specified in '%s' - this is a bad package!",
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.
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);
198 /* If this is a direct extract and we didn't want it, stop now */
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);
210 /* Check for sanity and dependencies */
211 if (sanity_check(pkg))
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);
223 * If we have a prefix, delete the first one we see and add this
224 * one in place of it.
227 delete_plist(&Plist, FALSE, PLIST_CWD, NULL);
228 add_plist_top(&Plist, PLIST_CWD, Prefix);
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";
239 * See if we're already registered either with the same name (the same
240 * version) or some other version with the same origin.
242 if ((isinstalledpkg(Plist.name) ||
243 matchbyorigin(Plist.origin, NULL) != NULL) && !Force) {
244 warnx("package '%s' or its older version already installed",
247 goto success; /* close enough for government work */
250 /* Now check the packing list for dependencies */
251 for (p = Plist.head; p ; p = p->next) {
254 if (p->type != PLIST_PKGDEP)
256 deporigin = (p->next->type == PLIST_DEPORIGIN) ? p->next->name : NULL;
258 printf("Package '%s' depends on '%s'", Plist.name, p->name);
259 if (deporigin != NULL)
260 printf(" with '%s' origin", deporigin);
263 if (!isinstalledpkg(p->name) &&
264 !(deporigin != NULL && matchbyorigin(deporigin, NULL) != NULL)) {
265 char path[FILENAME_MAX], *cp = NULL;
268 if (!isURL(pkg) && !getenv("PKG_ADD_BASE")) {
271 ext = strrchr(pkg_fullname, '.');
274 snprintf(path, FILENAME_MAX, "%s/%s%s", getenv("_TOP"), p->name, ext);
278 cp = fileFindByPath(pkg, p->name);
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)" : "!");
290 warnx("could not find package %s %s",
291 p->name, Force ? " (proceeding anyway)" : "!");
296 else if ((cp = fileGetURL(pkg, p->name)) != NULL) {
298 printf("Finished loading %s over FTP.\n", p->name);
299 if (!fexists("+CONTENTS")) {
300 warnx("autoloaded package %s has no +CONTENTS file?",
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)" : "!");
312 printf("\t'%s' loaded successfully.\n", p->name);
313 /* Nuke the temporary playpen */
319 printf("and was not found%s.\n", Force ? " (proceeding anyway)" : "");
321 printf("Package dependency %s for %s not found%s\n", p->name, pkg,
322 Force ? " (proceeding anyway)" : "!");
328 printf(" - already installed.\n");
334 /* Look for the requirements file */
335 if (fexists(REQUIRE_FNAME)) {
336 vsystem("chmod +x %s", REQUIRE_FNAME); /* be sure */
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");
344 goto success; /* close enough for government work */
350 * Test whether to use the old method of passing tokens to installation
351 * scripts, and set appropriate variables..
354 if (fexists(POST_INSTALL_FNAME)) {
356 sprintf(post_script, "%s", POST_INSTALL_FNAME);
360 if (fexists(INSTALL_FNAME)) {
361 sprintf(post_script, "%s", INSTALL_FNAME);
362 sprintf(pre_arg, "PRE-INSTALL");
363 sprintf(post_arg, "POST-INSTALL");
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 */
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");
376 goto success; /* nothing to uninstall yet */
380 /* Now finally extract the entire show if we're not going direct */
381 if (!inPlace && !Fake)
382 extract_plist(".", &Plist);
384 if (!Fake && fexists(MTREE_FNAME)) {
386 printf("Running mtree for %s..\n", Plist.name);
387 p = find_plist(&Plist, PLIST_CWD);
389 printf("mtree -U -f %s -d -e -p %s >%s\n", MTREE_FNAME, p ? p->name : "/", _PATH_DEVNULL);
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");
396 /* Run the installation script one last time? */
397 if (!NoInstall && fexists(post_script)) {
398 vsystem("chmod +x %s", post_script); /* make sure */
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");
409 /* Time to record the deed? */
410 if (!NoRecord && !Fake) {
411 char contents[FILENAME_MAX];
415 warnx("not running as root - trying to record install anyway");
416 sprintf(LogDir, "%s/%s", LOG_DIR, Plist.name);
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!",
423 bzero(LogDir, FILENAME_MAX);
425 goto success; /* close enough for government work */
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");
448 warnx("can't open new contents file '%s'! can't register pkg",
450 goto success; /* can't log, but still keep pkg */
452 write_plist(&Plist, contfile);
454 for (p = Plist.head; p ; p = p->next) {
455 char *deporigin, **depnames;
458 if (p->type != PLIST_PKGDEP)
460 deporigin = (p->next->type == PLIST_DEPORIGIN) ? p->next->name :
463 printf("Trying to record dependency on package '%s'", p->name);
464 if (deporigin != NULL)
465 printf(" with '%s' origin", deporigin);
469 depnames = (deporigin != NULL) ? matchbyorigin(deporigin, NULL) :
471 if (depnames == NULL) {
472 depnames = alloca(sizeof(*depnames) * 2);
473 depnames[0] = p->name;
476 for (i = 0; depnames[i] != NULL; i++) {
477 sprintf(contents, "%s/%s/%s", LOG_DIR, depnames[i],
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");
484 warnx("can't open dependency file '%s'!\n"
485 "dependency registration is incomplete", contents);
487 fprintf(contfile, "%s\n", Plist.name);
488 if (fclose(contfile) == EOF)
489 warnx("cannot properly close file %s", contents);
494 printf("Package %s registered in %s\n", Plist.name, LogDir);
497 if ((p = find_plist(&Plist, PLIST_DISPLAY)) != NULL) {
501 snprintf(buf, sizeof buf, "%s/%s", LogDir, p->name);
502 fp = fopen(buf, "r");
505 while (fgets(buf, sizeof(buf), fp))
510 warnx("cannot open %s as display file", buf);
520 /* Nuke the whole (installed) show, XXX but don't clean directories */
522 delete_package(FALSE, FALSE, &Plist);
525 /* delete the packing list contents */
532 sanity_check(char *pkg)
536 if (!fexists(CONTENTS_FNAME)) {
537 warnx("package %s has no CONTENTS file!", pkg);
540 else if (!fexists(COMMENT_FNAME)) {
541 warnx("package %s has no COMMENT file!", pkg);
544 else if (!fexists(DESC_FNAME)) {
545 warnx("package %s has no DESC file!", pkg);
554 static int in_cleanup = 0;
559 printf("Signal %d received, cleaning up..\n", sig);
560 if (!Fake && zapLogDir && LogDir[0])
561 vsystem("%s -rf %s", REMOVE_CMD, LogDir);