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$");
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]);
56 * This is seriously ugly code following. Written very fast!
57 * [And subsequently made even worse.. Sigh! This code was just born
58 * to be hacked, I guess.. :) -jkh]
64 char pkg_fullname[FILENAME_MAX];
65 char playpen[FILENAME_MAX];
66 char extract_contents[FILENAME_MAX];
73 int inPlace, conflictsfound, errcode;
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];
87 strcpy(playpen, FirstPen);
90 memset(&Plist, '\0', sizeof(Plist));
92 /* Are we coming in for a second pass, everything already extracted? */
94 fgets(playpen, FILENAME_MAX, stdin);
95 playpen[strlen(playpen) - 1] = '\0'; /* pesky newline! */
96 if (chdir(playpen) == FAIL) {
97 warnx("pkg_add in SLAVE mode can't chdir to %s", playpen);
100 read_plist(&Plist, stdin);
103 /* Nope - do it now */
105 /* Is it an ftp://foo.bar.baz/file.t[bg]z specification? */
107 if (!(where_to = fileGetURL(NULL, pkg, KeepPackage))) {
108 warnx("unable to fetch '%s' by URL", pkg);
111 strcpy(pkg_fullname, pkg);
112 cfile = fopen(CONTENTS_FNAME, "r");
115 "unable to open table of contents file '%s' - not a package?",
119 read_plist(&Plist, cfile);
123 strcpy(pkg_fullname, pkg); /*
124 * Copy for sanity's sake,
125 * could remove pkg_fullname
127 if (strcmp(pkg, "-")) {
128 if (stat(pkg_fullname, &sb) == FAIL) {
129 warnx("can't stat package file '%s'", pkg_fullname);
132 sprintf(extract_contents, "--fast-read %s", CONTENTS_FNAME);
133 extract = extract_contents;
137 sb.st_size = 100000; /* Make up a plausible average size */
139 if (!(where_to = make_playpen(playpen, sb.st_size * 4)))
140 errx(1, "unable to make playpen for %lld bytes", (long long)sb.st_size * 4);
141 /* Since we can call ourselves recursively, keep notes on where we came from */
143 setenv("_TOP", where_to, 1);
144 if (unpack(pkg_fullname, extract)) {
146 "unable to extract table of contents file from '%s' - not a package?",
150 cfile = fopen(CONTENTS_FNAME, "r");
153 "unable to open table of contents file '%s' - not a package?",
157 read_plist(&Plist, cfile);
160 /* Extract directly rather than moving? Oh goodie! */
161 if (find_plist_option(&Plist, "extract-in-place")) {
163 printf("Doing in-place extraction for %s\n", pkg_fullname);
164 p = find_plist(&Plist, PLIST_CWD);
166 if (!isdir(p->name) && !Fake) {
168 printf("Desired prefix of %s does not exist, creating..\n", p->name);
169 vsystem("/bin/mkdir -p %s", p->name);
170 if (chdir(p->name) == -1) {
171 warn("unable to change directory to '%s'", p->name);
180 "no prefix specified in '%s' - this is a bad package!",
187 * Apply a crude heuristic to see how much space the package will
188 * take up once it's unpacked. I've noticed that most packages
189 * compress an average of 75%, so multiply by 4 for good measure.
192 if (!extract && !inPlace && min_free(playpen) < sb.st_size * 4) {
193 warnx("projected size of %lld exceeds available free space.\n"
194 "Please set your PKG_TMPDIR variable to point to a location with more\n"
195 "free space and try again", (long long)sb.st_size * 4);
196 warnx("not extracting %s\ninto %s, sorry!",
197 pkg_fullname, where_to);
201 /* If this is a direct extract and we didn't want it, stop now */
205 /* Finally unpack the whole mess. If extract is null we
206 already + did so so don't bother doing it again. */
207 if (extract && unpack(pkg_fullname, NULL)) {
208 warnx("unable to extract '%s'!", pkg_fullname);
213 /* Check for sanity and dependencies */
214 if (sanity_check(pkg))
217 /* If we're running in MASTER mode, just output the plist and return */
218 if (AddMode == MASTER) {
219 printf("%s\n", where_playpen());
220 write_plist(&Plist, stdout);
226 * If we have a prefix, delete the first one we see and add this
227 * one in place of it.
230 delete_plist(&Plist, FALSE, PLIST_CWD, NULL);
231 add_plist_top(&Plist, PLIST_CWD, Prefix);
234 setenv(PKG_PREFIX_VNAME, (p = find_plist(&Plist, PLIST_CWD)) ? p->name : ".", 1);
235 /* Protect against old packages with bogus @name and origin fields */
236 if (Plist.name == NULL)
237 Plist.name = "anonymous";
238 if (Plist.origin == NULL)
239 Plist.origin = "anonymous/anonymous";
242 * See if we're already registered either with the same name (the same
243 * version) or some other version with the same origin.
245 if ((isinstalledpkg(Plist.name) > 0 ||
246 matchbyorigin(Plist.origin, NULL) != NULL) && !Force) {
247 warnx("package '%s' or its older version already installed%s",
248 Plist.name, FailOnAlreadyInstalled ? "" : " (ignored)");
249 code = FailOnAlreadyInstalled != FALSE;
250 goto success; /* close enough for government work */
253 /* Now check the packing list for conflicts */
255 for (p = Plist.head; p != NULL; p = p->next) {
256 if (p->type == PLIST_CONFLICTS) {
258 conflict[0] = strdup(p->name);
260 matched = matchinstalled(MATCH_GLOB, conflict, &errcode);
262 if (errcode == 0 && matched != NULL)
263 for (i = 0; matched[i] != NULL; i++)
264 if (isinstalledpkg(matched[i]) > 0) {
265 warnx("package '%s' conflicts with %s", Plist.name,
275 warnx("please use pkg_delete first to remove conflicting package(s) or -f to force installation");
279 warnx("-f specified; proceeding anyway");
282 #if ENSURE_THAT_ALL_REQUIREMENTS_ARE_MET
284 * Before attempting to do the slave mode bit, ensure that we've
285 * downloaded & processed everything we need.
286 * It's possible that we haven't already installed all of our
287 * dependencies if the dependency list was misgenerated due to
288 * other dynamic dependencies or if a dependency was added to a
289 * package without all REQUIRED_BY packages being regenerated.
291 for (p = pkg ? Plist.head : NULL; p; p = p->next) {
295 if (p->type != PLIST_PKGDEP)
297 deporigin = (p->next->type == PLIST_DEPORIGIN) ? p->next->name : NULL;
299 if (isinstalledpkg(p->name) <= 0 &&
300 !(deporigin != NULL && matchbyorigin(deporigin, NULL) != NULL)) {
301 char subpkg[FILENAME_MAX], *sep;
303 strlcpy(subpkg, pkg, sizeof subpkg);
304 if ((sep = strrchr(subpkg, '/')) != NULL) {
306 if ((sep = strrchr(subpkg, '/')) != NULL) {
308 strlcat(subpkg, "/All/", sizeof subpkg);
309 strlcat(subpkg, p->name, sizeof subpkg);
310 if ((ext = strrchr(pkg, '.')) == NULL) {
311 if (getenv("PACKAGESUFFIX"))
312 ext = getenv("PACKAGESUFFIX");
316 strlcat(subpkg, ext, sizeof subpkg);
324 /* Now check the packing list for dependencies */
325 for (p = Plist.head; p ; p = p->next) {
328 if (p->type != PLIST_PKGDEP)
330 deporigin = (p->next->type == PLIST_DEPORIGIN) ? p->next->name : NULL;
332 printf("Package '%s' depends on '%s'", Plist.name, p->name);
333 if (deporigin != NULL)
334 printf(" with '%s' origin", deporigin);
337 if (isinstalledpkg(p->name) <= 0 &&
338 !(deporigin != NULL && matchbyorigin(deporigin, NULL) != NULL)) {
339 char path[FILENAME_MAX];
340 const char *cp = NULL;
343 char prefixArg[2 + MAXPATHLEN]; /* "-P" + Prefix */
344 if (PrefixRecursive) {
345 strlcpy(prefixArg, "-P", sizeof(prefixArg));
346 strlcat(prefixArg, Prefix, sizeof(prefixArg));
348 if (!isURL(pkg) && !getenv("PKG_ADD_BASE")) {
351 ext = strrchr(pkg_fullname, '.');
353 if (getenv("PACKAGESUFFIX")) {
354 ext = getenv("PACKAGESUFFIX");
359 snprintf(path, FILENAME_MAX, "%s/%s%s", getenv("_TOP"), p->name, ext);
363 cp = fileFindByPath(pkg, p->name);
366 printf("Loading it from %s.\n", cp);
367 if (vsystem("%s %s %s '%s'", PkgAddCmd, Verbose ? "-v " : "", PrefixRecursive ? prefixArg : "", cp)) {
368 warnx("autoload of dependency '%s' failed%s",
369 cp, Force ? " (proceeding anyway)" : "!");
375 warnx("could not find package %s %s",
376 p->name, Force ? " (proceeding anyway)" : "!");
381 else if ((cp = fileGetURL(pkg, p->name, KeepPackage)) != NULL) {
383 printf("Finished loading %s via a URL\n", p->name);
384 if (!fexists("+CONTENTS")) {
385 warnx("autoloaded package %s has no +CONTENTS file?",
390 else if (vsystem("(pwd; /bin/cat +CONTENTS) | %s %s %s %s -S", PkgAddCmd, Verbose ? "-v" : "", PrefixRecursive ? prefixArg : "", KeepPackage ? "-K" : "")) {
391 warnx("pkg_add of dependency '%s' failed%s",
392 p->name, Force ? " (proceeding anyway)" : "!");
397 printf("\t'%s' loaded successfully.\n", p->name);
398 /* Nuke the temporary playpen */
404 printf("and was not found%s.\n", Force ? " (proceeding anyway)" : "");
406 printf("Package dependency %s for %s not found%s\n", p->name, pkg,
407 Force ? " (proceeding anyway)" : "!");
413 printf(" - already installed.\n");
415 } /* if (!IgnoreDeps) */
420 /* Look for the requirements file */
421 if ((fd = open(REQUIRE_FNAME, O_RDWR)) != -1) {
423 fchmod(fd, sb.st_mode | S_IXALL); /* be sure, chmod a+x */
426 printf("Running requirements file first for %s..\n", Plist.name);
427 if (!Fake && vsystem("./%s %s INSTALL", REQUIRE_FNAME, Plist.name)) {
428 warnx("package %s fails requirements %s", pkg_fullname,
429 Force ? "installing anyway" : "- not installed");
432 goto success; /* close enough for government work */
438 * Test whether to use the old method of passing tokens to installation
439 * scripts, and set appropriate variables..
442 if (fexists(POST_INSTALL_FNAME)) {
444 sprintf(post_script, "%s", POST_INSTALL_FNAME);
448 if (fexists(INSTALL_FNAME)) {
449 sprintf(post_script, "%s", INSTALL_FNAME);
450 sprintf(pre_arg, "PRE-INSTALL");
451 sprintf(post_arg, "POST-INSTALL");
455 /* If we're really installing, and have an installation file, run it */
456 if (!NoInstall && (fd = open(pre_script, O_RDWR)) != -1) {
458 fchmod(fd, sb.st_mode | S_IXALL); /* be sure, chmod a+x */
461 printf("Running pre-install for %s..\n", Plist.name);
462 if (!Fake && vsystem("./%s %s %s", pre_script, Plist.name, pre_arg)) {
463 warnx("install script returned error status");
466 goto success; /* nothing to uninstall yet */
470 /* Now finally extract the entire show if we're not going direct */
471 if (!inPlace && !Fake)
472 extract_plist(".", &Plist);
474 if (!Fake && fexists(MTREE_FNAME)) {
476 printf("Running mtree for %s..\n", Plist.name);
477 p = find_plist(&Plist, PLIST_CWD);
479 printf("mtree -U -f %s -d -e -p %s >%s\n", MTREE_FNAME, p ? p->name : "/", _PATH_DEVNULL);
481 if (vsystem("/usr/sbin/mtree -U -f %s -d -e -p %s >%s", MTREE_FNAME, p ? p->name : "/", _PATH_DEVNULL))
482 warnx("mtree returned a non-zero status - continuing");
486 /* Run the installation script one last time? */
487 if (!NoInstall && (fd = open(post_script, O_RDWR)) != -1) {
489 fchmod(fd, sb.st_mode | S_IXALL); /* be sure, chmod a+x */
492 printf("Running post-install for %s..\n", Plist.name);
493 if (!Fake && vsystem("./%s %s %s", post_script, Plist.name, post_arg)) {
494 warnx("install script returned error status");
501 /* Time to record the deed? */
502 if (!NoRecord && !Fake) {
503 char contents[FILENAME_MAX];
504 char **depnames = NULL, **deporigins = NULL, ***depmatches;
505 int i, dep_count = 0;
509 warnx("not running as root - trying to record install anyway");
510 sprintf(LogDir, "%s/%s", LOG_DIR, Plist.name);
513 printf("Attempting to record package into %s..\n", LogDir);
514 if (make_hierarchy(LogDir, FALSE)) {
515 warnx("can't record package into '%s', you're on your own!",
517 bzero(LogDir, FILENAME_MAX);
519 goto success; /* close enough for government work */
521 /* Make sure pkg_info can read the entry */
522 fd = open(LogDir, O_RDWR);
524 fchmod(fd, sb.st_mode | S_IRALL | S_IXALL); /* be sure, chmod a+rx */
526 move_file(".", DESC_FNAME, LogDir);
527 move_file(".", COMMENT_FNAME, LogDir);
528 if (fexists(INSTALL_FNAME))
529 move_file(".", INSTALL_FNAME, LogDir);
530 if (fexists(POST_INSTALL_FNAME))
531 move_file(".", POST_INSTALL_FNAME, LogDir);
532 if (fexists(DEINSTALL_FNAME))
533 move_file(".", DEINSTALL_FNAME, LogDir);
534 if (fexists(POST_DEINSTALL_FNAME))
535 move_file(".", POST_DEINSTALL_FNAME, LogDir);
536 if (fexists(REQUIRE_FNAME))
537 move_file(".", REQUIRE_FNAME, LogDir);
538 if (fexists(DISPLAY_FNAME))
539 move_file(".", DISPLAY_FNAME, LogDir);
540 if (fexists(MTREE_FNAME))
541 move_file(".", MTREE_FNAME, LogDir);
542 sprintf(contents, "%s/%s", LogDir, CONTENTS_FNAME);
543 contfile = fopen(contents, "w");
545 warnx("can't open new contents file '%s'! can't register pkg",
547 goto success; /* can't log, but still keep pkg */
549 write_plist(&Plist, contfile);
551 for (p = Plist.head; p ; p = p->next) {
554 if (p->type != PLIST_PKGDEP)
556 deporigin = (p->next->type == PLIST_DEPORIGIN) ? p->next->name :
559 printf("Trying to record dependency on package '%s'", p->name);
560 if (deporigin != NULL)
561 printf(" with '%s' origin", deporigin);
566 /* Defer to origin lookup */
567 depnames = realloc(depnames, (dep_count + 1) * sizeof(*depnames));
568 depnames[dep_count] = p->name;
569 deporigins = realloc(deporigins, (dep_count + 2) * sizeof(*deporigins));
570 deporigins[dep_count] = deporigin;
571 deporigins[dep_count + 1] = NULL;
574 /* No origin recorded, try to register on literal package name */
575 sprintf(contents, "%s/%s/%s", LOG_DIR, p->name,
577 contfile = fopen(contents, "a");
579 warnx("can't open dependency file '%s'!\n"
580 "dependency registration is incomplete", contents);
582 fprintf(contfile, "%s\n", Plist.name);
583 if (fclose(contfile) == EOF) {
584 warnx("cannot properly close file %s", contents);
590 depmatches = matchallbyorigin((const char **)deporigins, NULL);
592 if (!IgnoreDeps && depmatches) {
593 for (i = 0; i < dep_count; i++) {
596 char **tmp = depmatches[i];
597 for (j = 0; tmp[j] != NULL; j++) {
598 /* Origin looked up */
599 sprintf(contents, "%s/%s/%s", LOG_DIR, tmp[j],
601 if (depnames[i] && strcmp(depnames[i], tmp[j]) != 0)
602 warnx("warning: package '%s' requires '%s', but '%s' "
603 "is installed", Plist.name, depnames[i], tmp[j]);
604 contfile = fopen(contents, "a");
606 warnx("can't open dependency file '%s'!\n"
607 "dependency registration is incomplete", contents);
609 fprintf(contfile, "%s\n", Plist.name);
610 if (fclose(contfile) == EOF)
611 warnx("cannot properly close file %s", contents);
614 } else if (depnames[i]) {
615 /* No package present with this origin, try literal package name */
616 sprintf(contents, "%s/%s/%s", LOG_DIR, depnames[i],
618 contfile = fopen(contents, "a");
620 warnx("can't open dependency file '%s'!\n"
621 "dependency registration is incomplete", contents);
623 fprintf(contfile, "%s\n", Plist.name);
624 if (fclose(contfile) == EOF) {
625 warnx("cannot properly close file %s", contents);
633 printf("Package %s registered in %s\n", Plist.name, LogDir);
636 if ((p = find_plist(&Plist, PLIST_DISPLAY)) != NULL) {
640 snprintf(buf, sizeof buf, "%s/%s", LogDir, p->name);
641 fp = fopen(buf, "r");
644 while (fgets(buf, sizeof(buf), fp))
650 warnx("cannot open %s as display file", buf);
662 /* Nuke the whole (installed) show, XXX but don't clean directories */
664 delete_package(FALSE, FALSE, &Plist);
667 /* delete the packing list contents */
674 sanity_check(char *pkg)
678 if (!fexists(CONTENTS_FNAME)) {
679 warnx("package %s has no CONTENTS file!", pkg);
682 else if (!fexists(COMMENT_FNAME)) {
683 warnx("package %s has no COMMENT file!", pkg);
686 else if (!fexists(DESC_FNAME)) {
687 warnx("package %s has no DESC file!", pkg);
696 static int in_cleanup = 0;
701 printf("Signal %d received, cleaning up..\n", sig);
702 if (!Fake && zapLogDir && LogDir[0])
703 vsystem("%s -rf %s", REMOVE_CMD, LogDir);
704 while (leave_playpen())