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.
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD: src/usr.sbin/pkg_install/add/perform.c,v 1.57.2.15 2002/09/25 23:22:13 bmah Exp $");
33 static int pkg_do(char *);
34 static int sanity_check(char *);
35 static char LogDir[FILENAME_MAX];
36 static int zapLogDir; /* Should we delete LogDir? */
39 pkg_perform(char **pkgs)
43 signal(SIGINT, cleanup);
44 signal(SIGHUP, cleanup);
47 err_cnt = pkg_do(NULL);
49 for (i = 0; pkgs[i]; i++)
50 err_cnt += pkg_do(pkgs[i]);
59 * This is seriously ugly code following. Written very fast!
60 * [And subsequently made even worse.. Sigh! This code was just born
61 * to be hacked, I guess.. :) -jkh]
66 char pkg_fullname[FILENAME_MAX];
67 char playpen[FILENAME_MAX];
68 char extract_contents[FILENAME_MAX];
69 char *where_to, *extract;
75 /* support for separate pre/post install scripts */
77 char pre_script[FILENAME_MAX] = INSTALL_FNAME;
78 char post_script[FILENAME_MAX];
79 char pre_arg[FILENAME_MAX], post_arg[FILENAME_MAX];
84 strcpy(playpen, FirstPen);
87 /* Are we coming in for a second pass, everything already extracted? */
89 fgets(playpen, FILENAME_MAX, stdin);
90 playpen[strlen(playpen) - 1] = '\0'; /* pesky newline! */
91 if (chdir(playpen) == FAIL) {
92 warnx("pkg_add in SLAVE mode can't chdir to %s", playpen);
95 read_plist(&Plist, stdin);
98 /* Nope - do it now */
100 /* Is it an ftp://foo.bar.baz/file.t[bg]z specification? */
102 if (!(Home = fileGetURL(NULL, pkg))) {
103 warnx("unable to fetch '%s' by URL", pkg);
107 strcpy(pkg_fullname, pkg);
108 cfile = fopen(CONTENTS_FNAME, "r");
111 "unable to open table of contents file '%s' - not a package?",
115 read_plist(&Plist, cfile);
119 strcpy(pkg_fullname, pkg); /*
120 * Copy for sanity's sake,
121 * could remove pkg_fullname
123 if (strcmp(pkg, "-")) {
124 if (stat(pkg_fullname, &sb) == FAIL) {
125 warnx("can't stat package file '%s'", pkg_fullname);
128 sprintf(extract_contents, "--fast-read %s", CONTENTS_FNAME);
129 extract = extract_contents;
133 sb.st_size = 100000; /* Make up a plausible average size */
135 Home = make_playpen(playpen, sb.st_size * 4);
137 errx(1, "unable to make playpen for %qd bytes", (long long)sb.st_size * 4);
139 /* Since we can call ourselves recursively, keep notes on where we came from */
141 setenv("_TOP", Home, 1);
142 if (unpack(pkg_fullname, extract)) {
144 "unable to extract table of contents file from '%s' - not a package?",
148 cfile = fopen(CONTENTS_FNAME, "r");
151 "unable to open table of contents file '%s' - not a package?",
155 read_plist(&Plist, cfile);
158 /* Extract directly rather than moving? Oh goodie! */
159 if (find_plist_option(&Plist, "extract-in-place")) {
161 printf("Doing in-place extraction for %s\n", pkg_fullname);
162 p = find_plist(&Plist, PLIST_CWD);
164 if (!isdir(p->name) && !Fake) {
166 printf("Desired prefix of %s does not exist, creating..\n", p->name);
167 vsystem("mkdir -p %s", p->name);
168 if (chdir(p->name) == -1) {
169 warn("unable to change directory to '%s'", p->name);
178 "no prefix specified in '%s' - this is a bad package!",
185 * Apply a crude heuristic to see how much space the package will
186 * take up once it's unpacked. I've noticed that most packages
187 * compress an average of 75%, so multiply by 4 for good measure.
190 if (!extract && !inPlace && min_free(playpen) < sb.st_size * 4) {
191 warnx("projected size of %qd exceeds available free space.\n"
192 "Please set your PKG_TMPDIR variable to point to a location with more\n"
193 "free space and try again", (long long)sb.st_size * 4);
194 warnx("not extracting %s\ninto %s, sorry!",
195 pkg_fullname, where_to);
199 /* If this is a direct extract and we didn't want it, stop now */
203 /* Finally unpack the whole mess. If extract is null we
204 already + did so so don't bother doing it again. */
205 if (extract && unpack(pkg_fullname, NULL)) {
206 warnx("unable to extract '%s'!", pkg_fullname);
211 /* Check for sanity and dependencies */
212 if (sanity_check(pkg))
215 /* If we're running in MASTER mode, just output the plist and return */
216 if (AddMode == MASTER) {
217 printf("%s\n", where_playpen());
218 write_plist(&Plist, stdout);
224 * If we have a prefix, delete the first one we see and add this
225 * one in place of it.
228 delete_plist(&Plist, FALSE, PLIST_CWD, NULL);
229 add_plist_top(&Plist, PLIST_CWD, Prefix);
232 setenv(PKG_PREFIX_VNAME, (p = find_plist(&Plist, PLIST_CWD)) ? p->name : ".", 1);
233 /* Protect against old packages with bogus @name and origin fields */
234 if (Plist.name == NULL)
235 Plist.name = "anonymous";
236 if (Plist.origin == NULL)
237 Plist.origin = "anonymous/anonymous";
240 * See if we're already registered either with the same name (the same
241 * version) or some other version with the same origin.
243 if ((isinstalledpkg(Plist.name) ||
244 matchbyorigin(Plist.origin, NULL) != NULL) && !Force) {
245 warnx("package '%s' or its older version already installed",
248 goto success; /* close enough for government work */
251 /* Now check the packing list for dependencies */
252 for (p = Plist.head; p ; p = p->next) {
255 if (p->type != PLIST_PKGDEP)
257 deporigin = (p->next->type == PLIST_DEPORIGIN) ? p->next->name : NULL;
259 printf("Package '%s' depends on '%s'", Plist.name, p->name);
260 if (deporigin != NULL)
261 printf(" with '%s' origin", deporigin);
264 if (!isinstalledpkg(p->name) &&
265 !(deporigin != NULL && matchbyorigin(deporigin, NULL) != NULL)) {
266 char path[FILENAME_MAX], *cp = NULL;
269 if (!isURL(pkg) && !getenv("PKG_ADD_BASE")) {
272 ext = strrchr(pkg_fullname, '.');
275 snprintf(path, FILENAME_MAX, "%s/%s%s", getenv("_TOP"), p->name, ext);
279 cp = fileFindByPath(pkg, p->name);
282 printf("Loading it from %s.\n", cp);
283 if (vsystem("pkg_add %s'%s'", Verbose ? "-v " : "", cp)) {
284 warnx("autoload of dependency '%s' failed%s",
285 cp, Force ? " (proceeding anyway)" : "!");
291 warnx("could not find package %s %s",
292 p->name, Force ? " (proceeding anyway)" : "!");
297 else if ((cp = fileGetURL(pkg, p->name)) != NULL) {
299 printf("Finished loading %s over FTP.\n", p->name);
300 if (!fexists("+CONTENTS")) {
301 warnx("autoloaded package %s has no +CONTENTS file?",
306 else if (vsystem("(pwd; cat +CONTENTS) | pkg_add %s-S", Verbose ? "-v " : "")) {
307 warnx("pkg_add of dependency '%s' failed%s",
308 p->name, Force ? " (proceeding anyway)" : "!");
313 printf("\t'%s' loaded successfully.\n", p->name);
314 /* Nuke the temporary playpen */
320 printf("and was not found%s.\n", Force ? " (proceeding anyway)" : "");
322 printf("Package dependency %s for %s not found%s\n", p->name, pkg,
323 Force ? " (proceeding anyway)" : "!");
329 printf(" - already installed.\n");
335 /* Look for the requirements file */
336 if (fexists(REQUIRE_FNAME)) {
337 vsystem("chmod +x %s", REQUIRE_FNAME); /* be sure */
339 printf("Running requirements file first for %s..\n", Plist.name);
340 if (!Fake && vsystem("./%s %s INSTALL", REQUIRE_FNAME, Plist.name)) {
341 warnx("package %s fails requirements %s", pkg_fullname,
342 Force ? "installing anyway" : "- not installed");
345 goto success; /* close enough for government work */
351 * Test whether to use the old method of passing tokens to installation
352 * scripts, and set appropriate variables..
355 if (fexists(POST_INSTALL_FNAME)) {
357 sprintf(post_script, "%s", POST_INSTALL_FNAME);
361 if (fexists(INSTALL_FNAME)) {
362 sprintf(post_script, "%s", INSTALL_FNAME);
363 sprintf(pre_arg, "PRE-INSTALL");
364 sprintf(post_arg, "POST-INSTALL");
368 /* If we're really installing, and have an installation file, run it */
369 if (!NoInstall && fexists(pre_script)) {
370 vsystem("chmod +x %s", pre_script); /* make sure */
372 printf("Running pre-install for %s..\n", Plist.name);
373 if (!Fake && vsystem("./%s %s %s", pre_script, Plist.name, pre_arg)) {
374 warnx("install script returned error status");
377 goto success; /* nothing to uninstall yet */
381 /* Now finally extract the entire show if we're not going direct */
382 if (!inPlace && !Fake)
383 extract_plist(".", &Plist);
385 if (!Fake && fexists(MTREE_FNAME)) {
387 printf("Running mtree for %s..\n", Plist.name);
388 p = find_plist(&Plist, PLIST_CWD);
390 printf("mtree -U -f %s -d -e -p %s >%s\n", MTREE_FNAME, p ? p->name : "/", _PATH_DEVNULL);
392 if (vsystem("/usr/sbin/mtree -U -f %s -d -e -p %s >%s", MTREE_FNAME, p ? p->name : "/", _PATH_DEVNULL))
393 warnx("mtree returned a non-zero status - continuing");
397 /* Run the installation script one last time? */
398 if (!NoInstall && fexists(post_script)) {
399 vsystem("chmod +x %s", post_script); /* make sure */
401 printf("Running post-install for %s..\n", Plist.name);
402 if (!Fake && vsystem("./%s %s %s", post_script, Plist.name, post_arg)) {
403 warnx("install script returned error status");
410 /* Time to record the deed? */
411 if (!NoRecord && !Fake) {
412 char contents[FILENAME_MAX];
416 warnx("not running as root - trying to record install anyway");
417 sprintf(LogDir, "%s/%s", LOG_DIR, Plist.name);
420 printf("Attempting to record package into %s..\n", LogDir);
421 if (make_hierarchy(LogDir)) {
422 warnx("can't record package into '%s', you're on your own!",
424 bzero(LogDir, FILENAME_MAX);
426 goto success; /* close enough for government work */
428 /* Make sure pkg_info can read the entry */
429 vsystem("chmod a+rx %s", LogDir);
430 move_file(".", DESC_FNAME, LogDir);
431 move_file(".", COMMENT_FNAME, LogDir);
432 if (fexists(INSTALL_FNAME))
433 move_file(".", INSTALL_FNAME, LogDir);
434 if (fexists(POST_INSTALL_FNAME))
435 move_file(".", POST_INSTALL_FNAME, LogDir);
436 if (fexists(DEINSTALL_FNAME))
437 move_file(".", DEINSTALL_FNAME, LogDir);
438 if (fexists(POST_DEINSTALL_FNAME))
439 move_file(".", POST_DEINSTALL_FNAME, LogDir);
440 if (fexists(REQUIRE_FNAME))
441 move_file(".", REQUIRE_FNAME, LogDir);
442 if (fexists(DISPLAY_FNAME))
443 move_file(".", DISPLAY_FNAME, LogDir);
444 if (fexists(MTREE_FNAME))
445 move_file(".", MTREE_FNAME, LogDir);
446 sprintf(contents, "%s/%s", LogDir, CONTENTS_FNAME);
447 contfile = fopen(contents, "w");
449 warnx("can't open new contents file '%s'! can't register pkg",
451 goto success; /* can't log, but still keep pkg */
453 write_plist(&Plist, contfile);
455 for (p = Plist.head; p ; p = p->next) {
456 char *deporigin, **depnames;
459 if (p->type != PLIST_PKGDEP)
461 deporigin = (p->next->type == PLIST_DEPORIGIN) ? p->next->name :
464 printf("Trying to record dependency on package '%s'", p->name);
465 if (deporigin != NULL)
466 printf(" with '%s' origin", deporigin);
470 depnames = (deporigin != NULL) ? matchbyorigin(deporigin, NULL) :
472 if (depnames == NULL) {
473 depnames = alloca(sizeof(*depnames) * 2);
474 depnames[0] = p->name;
477 for (i = 0; depnames[i] != NULL; i++) {
478 sprintf(contents, "%s/%s/%s", LOG_DIR, depnames[i],
480 if (strcmp(p->name, depnames[i]) != 0)
481 warnx("warning: package '%s' requires '%s', but '%s' "
482 "is installed", Plist.name, p->name, depnames[i]);
483 contfile = fopen(contents, "a");
485 warnx("can't open dependency file '%s'!\n"
486 "dependency registration is incomplete", contents);
488 fprintf(contfile, "%s\n", Plist.name);
489 if (fclose(contfile) == EOF)
490 warnx("cannot properly close file %s", contents);
495 printf("Package %s registered in %s\n", Plist.name, LogDir);
498 if ((p = find_plist(&Plist, PLIST_DISPLAY)) != NULL) {
502 snprintf(buf, sizeof buf, "%s/%s", LogDir, p->name);
503 fp = fopen(buf, "r");
506 while (fgets(buf, sizeof(buf), fp))
511 warnx("cannot open %s as display file", buf);
521 /* Nuke the whole (installed) show, XXX but don't clean directories */
523 delete_package(FALSE, FALSE, &Plist);
526 /* delete the packing list contents */
533 sanity_check(char *pkg)
537 if (!fexists(CONTENTS_FNAME)) {
538 warnx("package %s has no CONTENTS file!", pkg);
541 else if (!fexists(COMMENT_FNAME)) {
542 warnx("package %s has no COMMENT file!", pkg);
545 else if (!fexists(DESC_FNAME)) {
546 warnx("package %s has no DESC file!", pkg);
555 static int in_cleanup = 0;
560 printf("Signal %d received, cleaning up..\n", sig);
561 if (!Fake && zapLogDir && LogDir[0])
562 vsystem("%s -rf %s", REMOVE_CMD, LogDir);