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];
86 strcpy(playpen, FirstPen);
89 memset(&Plist, '\0', sizeof(Plist));
91 /* Are we coming in for a second pass, everything already extracted? */
93 fgets(playpen, FILENAME_MAX, stdin);
94 playpen[strlen(playpen) - 1] = '\0'; /* pesky newline! */
95 if (chdir(playpen) == FAIL) {
96 warnx("pkg_add in SLAVE mode can't chdir to %s", playpen);
99 read_plist(&Plist, stdin);
102 /* Nope - do it now */
104 /* Is it an ftp://foo.bar.baz/file.t[bg]z specification? */
106 if (!(where_to = fileGetURL(NULL, pkg, KeepPackage))) {
107 warnx("unable to fetch '%s' by URL", pkg);
110 strcpy(pkg_fullname, pkg);
111 cfile = fopen(CONTENTS_FNAME, "r");
114 "unable to open table of contents file '%s' - not a package?",
118 read_plist(&Plist, cfile);
122 strcpy(pkg_fullname, pkg); /*
123 * Copy for sanity's sake,
124 * could remove pkg_fullname
126 if (strcmp(pkg, "-")) {
127 if (stat(pkg_fullname, &sb) == FAIL) {
128 warnx("can't stat package file '%s'", pkg_fullname);
131 sprintf(extract_contents, "--fast-read %s", CONTENTS_FNAME);
132 extract = extract_contents;
136 sb.st_size = 100000; /* Make up a plausible average size */
138 if (!(where_to = make_playpen(playpen, sb.st_size * 4)))
139 errx(1, "unable to make playpen for %lld bytes", (long long)sb.st_size * 4);
140 /* Since we can call ourselves recursively, keep notes on where we came from */
142 setenv("_TOP", where_to, 1);
143 if (unpack(pkg_fullname, extract)) {
145 "unable to extract table of contents file from '%s' - not a package?",
149 cfile = fopen(CONTENTS_FNAME, "r");
152 "unable to open table of contents file '%s' - not a package?",
156 read_plist(&Plist, cfile);
159 /* Extract directly rather than moving? Oh goodie! */
160 if (find_plist_option(&Plist, "extract-in-place")) {
162 printf("Doing in-place extraction for %s\n", pkg_fullname);
163 p = find_plist(&Plist, PLIST_CWD);
165 if (!isdir(p->name) && !Fake) {
167 printf("Desired prefix of %s does not exist, creating..\n", p->name);
168 vsystem("/bin/mkdir -p %s", p->name);
169 if (chdir(p->name) == -1) {
170 warn("unable to change directory to '%s'", p->name);
179 "no prefix specified in '%s' - this is a bad package!",
186 * Apply a crude heuristic to see how much space the package will
187 * take up once it's unpacked. I've noticed that most packages
188 * compress an average of 75%, so multiply by 4 for good measure.
191 if (!extract && !inPlace && min_free(playpen) < sb.st_size * 4) {
192 warnx("projected size of %lld exceeds available free space.\n"
193 "Please set your PKG_TMPDIR variable to point to a location with more\n"
194 "free space and try again", (long long)sb.st_size * 4);
195 warnx("not extracting %s\ninto %s, sorry!",
196 pkg_fullname, where_to);
200 /* If this is a direct extract and we didn't want it, stop now */
204 /* Finally unpack the whole mess. If extract is null we
205 already + did so so don't bother doing it again. */
206 if (extract && unpack(pkg_fullname, NULL)) {
207 warnx("unable to extract '%s'!", pkg_fullname);
212 /* Check for sanity and dependencies */
213 if (sanity_check(pkg))
216 /* If we're running in MASTER mode, just output the plist and return */
217 if (AddMode == MASTER) {
218 printf("%s\n", where_playpen());
219 write_plist(&Plist, stdout);
225 * If we have a prefix, delete the first one we see and add this
226 * one in place of it.
229 delete_plist(&Plist, FALSE, PLIST_CWD, NULL);
230 add_plist_top(&Plist, PLIST_CWD, Prefix);
233 setenv(PKG_PREFIX_VNAME, (p = find_plist(&Plist, PLIST_CWD)) ? p->name : ".", 1);
234 /* Protect against old packages with bogus @name and origin fields */
235 if (Plist.name == NULL)
236 Plist.name = "anonymous";
237 if (Plist.origin == NULL)
238 Plist.origin = "anonymous/anonymous";
241 * See if we're already registered either with the same name (the same
242 * version) or some other version with the same origin.
244 if ((isinstalledpkg(Plist.name) > 0 ||
245 matchbyorigin(Plist.origin, NULL) != NULL) && !Force) {
246 warnx("package '%s' or its older version already installed%s",
247 Plist.name, FailOnAlreadyInstalled ? "" : " (ignored)");
248 code = FailOnAlreadyInstalled != FALSE;
249 goto success; /* close enough for government work */
252 /* Now check the packing list for conflicts */
254 for (p = Plist.head; p != NULL; p = p->next) {
255 if (p->type == PLIST_CONFLICTS) {
257 conflict[0] = strdup(p->name);
259 matched = matchinstalled(MATCH_GLOB, conflict, &errcode);
261 if (errcode == 0 && matched != NULL)
262 for (i = 0; matched[i] != NULL; i++)
263 if (isinstalledpkg(matched[i]) > 0) {
264 warnx("package '%s' conflicts with %s", Plist.name,
274 warnx("please use pkg_delete first to remove conflicting package(s) or -f to force installation");
278 warnx("-f specified; proceeding anyway");
281 #if ENSURE_THAT_ALL_REQUIREMENTS_ARE_MET
283 * Before attempting to do the slave mode bit, ensure that we've
284 * downloaded & processed everything we need.
285 * It's possible that we haven't already installed all of our
286 * dependencies if the dependency list was misgenerated due to
287 * other dynamic dependencies or if a dependency was added to a
288 * package without all REQUIRED_BY packages being regenerated.
290 for (p = pkg ? Plist.head : NULL; p; p = p->next) {
294 if (p->type != PLIST_PKGDEP)
296 deporigin = (p->next->type == PLIST_DEPORIGIN) ? p->next->name : NULL;
298 if (isinstalledpkg(p->name) <= 0 &&
299 !(deporigin != NULL && matchbyorigin(deporigin, NULL) != NULL)) {
300 char subpkg[FILENAME_MAX], *sep;
302 strlcpy(subpkg, pkg, sizeof subpkg);
303 if ((sep = strrchr(subpkg, '/')) != NULL) {
305 if ((sep = strrchr(subpkg, '/')) != NULL) {
307 strlcat(subpkg, "/All/", sizeof subpkg);
308 strlcat(subpkg, p->name, sizeof subpkg);
309 if ((ext = strrchr(pkg, '.')) == NULL)
311 strlcat(subpkg, ext, sizeof subpkg);
319 /* Now check the packing list for dependencies */
320 for (p = Plist.head; p ; p = p->next) {
323 if (p->type != PLIST_PKGDEP)
325 deporigin = (p->next->type == PLIST_DEPORIGIN) ? p->next->name : NULL;
327 printf("Package '%s' depends on '%s'", Plist.name, p->name);
328 if (deporigin != NULL)
329 printf(" with '%s' origin", deporigin);
332 if (isinstalledpkg(p->name) <= 0 &&
333 !(deporigin != NULL && matchbyorigin(deporigin, NULL) != NULL)) {
334 char path[FILENAME_MAX];
335 const char *cp = NULL;
338 char prefixArg[2 + MAXPATHLEN]; /* "-P" + Prefix */
339 if (PrefixRecursive) {
340 strlcpy(prefixArg, "-P", sizeof(prefixArg));
341 strlcat(prefixArg, Prefix, sizeof(prefixArg));
343 if (!isURL(pkg) && !getenv("PKG_ADD_BASE")) {
346 ext = strrchr(pkg_fullname, '.');
349 snprintf(path, FILENAME_MAX, "%s/%s%s", getenv("_TOP"), p->name, ext);
353 cp = fileFindByPath(pkg, p->name);
356 printf("Loading it from %s.\n", cp);
357 if (vsystem("%s %s %s '%s'", PkgAddCmd, Verbose ? "-v " : "", PrefixRecursive ? prefixArg : "", cp)) {
358 warnx("autoload of dependency '%s' failed%s",
359 cp, Force ? " (proceeding anyway)" : "!");
365 warnx("could not find package %s %s",
366 p->name, Force ? " (proceeding anyway)" : "!");
371 else if ((cp = fileGetURL(pkg, p->name, KeepPackage)) != NULL) {
373 printf("Finished loading %s via a URL\n", p->name);
374 if (!fexists("+CONTENTS")) {
375 warnx("autoloaded package %s has no +CONTENTS file?",
380 else if (vsystem("(pwd; /bin/cat +CONTENTS) | %s %s %s %s -S", PkgAddCmd, Verbose ? "-v" : "", PrefixRecursive ? prefixArg : "", KeepPackage ? "-K" : "")) {
381 warnx("pkg_add of dependency '%s' failed%s",
382 p->name, Force ? " (proceeding anyway)" : "!");
387 printf("\t'%s' loaded successfully.\n", p->name);
388 /* Nuke the temporary playpen */
394 printf("and was not found%s.\n", Force ? " (proceeding anyway)" : "");
396 printf("Package dependency %s for %s not found%s\n", p->name, pkg,
397 Force ? " (proceeding anyway)" : "!");
403 printf(" - already installed.\n");
405 } /* if (!IgnoreDeps) */
410 /* Look for the requirements file */
411 if (fexists(REQUIRE_FNAME)) {
412 vsystem("/bin/chmod +x %s", REQUIRE_FNAME); /* be sure */
414 printf("Running requirements file first for %s..\n", Plist.name);
415 if (!Fake && vsystem("./%s %s INSTALL", REQUIRE_FNAME, Plist.name)) {
416 warnx("package %s fails requirements %s", pkg_fullname,
417 Force ? "installing anyway" : "- not installed");
420 goto success; /* close enough for government work */
426 * Test whether to use the old method of passing tokens to installation
427 * scripts, and set appropriate variables..
430 if (fexists(POST_INSTALL_FNAME)) {
432 sprintf(post_script, "%s", POST_INSTALL_FNAME);
436 if (fexists(INSTALL_FNAME)) {
437 sprintf(post_script, "%s", INSTALL_FNAME);
438 sprintf(pre_arg, "PRE-INSTALL");
439 sprintf(post_arg, "POST-INSTALL");
443 /* If we're really installing, and have an installation file, run it */
444 if (!NoInstall && fexists(pre_script)) {
445 vsystem("/bin/chmod +x %s", pre_script); /* make sure */
447 printf("Running pre-install for %s..\n", Plist.name);
448 if (!Fake && vsystem("./%s %s %s", pre_script, Plist.name, pre_arg)) {
449 warnx("install script returned error status");
452 goto success; /* nothing to uninstall yet */
456 /* Now finally extract the entire show if we're not going direct */
457 if (!inPlace && !Fake)
458 extract_plist(".", &Plist);
460 if (!Fake && fexists(MTREE_FNAME)) {
462 printf("Running mtree for %s..\n", Plist.name);
463 p = find_plist(&Plist, PLIST_CWD);
465 printf("mtree -U -f %s -d -e -p %s >%s\n", MTREE_FNAME, p ? p->name : "/", _PATH_DEVNULL);
467 if (vsystem("/usr/sbin/mtree -U -f %s -d -e -p %s >%s", MTREE_FNAME, p ? p->name : "/", _PATH_DEVNULL))
468 warnx("mtree returned a non-zero status - continuing");
472 /* Run the installation script one last time? */
473 if (!NoInstall && fexists(post_script)) {
474 vsystem("/bin/chmod +x %s", post_script); /* make sure */
476 printf("Running post-install for %s..\n", Plist.name);
477 if (!Fake && vsystem("./%s %s %s", post_script, Plist.name, post_arg)) {
478 warnx("install script returned error status");
485 /* Time to record the deed? */
486 if (!NoRecord && !Fake) {
487 char contents[FILENAME_MAX];
488 char **depnames = NULL, **deporigins = NULL, ***depmatches;
489 int i, dep_count = 0;
493 warnx("not running as root - trying to record install anyway");
494 sprintf(LogDir, "%s/%s", LOG_DIR, Plist.name);
497 printf("Attempting to record package into %s..\n", LogDir);
498 if (make_hierarchy(LogDir)) {
499 warnx("can't record package into '%s', you're on your own!",
501 bzero(LogDir, FILENAME_MAX);
503 goto success; /* close enough for government work */
505 /* Make sure pkg_info can read the entry */
506 vsystem("/bin/chmod a+rx %s", LogDir);
507 move_file(".", DESC_FNAME, LogDir);
508 move_file(".", COMMENT_FNAME, LogDir);
509 if (fexists(INSTALL_FNAME))
510 move_file(".", INSTALL_FNAME, LogDir);
511 if (fexists(POST_INSTALL_FNAME))
512 move_file(".", POST_INSTALL_FNAME, LogDir);
513 if (fexists(DEINSTALL_FNAME))
514 move_file(".", DEINSTALL_FNAME, LogDir);
515 if (fexists(POST_DEINSTALL_FNAME))
516 move_file(".", POST_DEINSTALL_FNAME, LogDir);
517 if (fexists(REQUIRE_FNAME))
518 move_file(".", REQUIRE_FNAME, LogDir);
519 if (fexists(DISPLAY_FNAME))
520 move_file(".", DISPLAY_FNAME, LogDir);
521 if (fexists(MTREE_FNAME))
522 move_file(".", MTREE_FNAME, LogDir);
523 sprintf(contents, "%s/%s", LogDir, CONTENTS_FNAME);
524 contfile = fopen(contents, "w");
526 warnx("can't open new contents file '%s'! can't register pkg",
528 goto success; /* can't log, but still keep pkg */
530 write_plist(&Plist, contfile);
532 for (p = Plist.head; p ; p = p->next) {
535 if (p->type != PLIST_PKGDEP)
537 deporigin = (p->next->type == PLIST_DEPORIGIN) ? p->next->name :
540 printf("Trying to record dependency on package '%s'", p->name);
541 if (deporigin != NULL)
542 printf(" with '%s' origin", deporigin);
547 /* Defer to origin lookup */
548 depnames = realloc(depnames, (dep_count + 1) * sizeof(*depnames));
549 depnames[dep_count] = p->name;
550 deporigins = realloc(deporigins, (dep_count + 2) * sizeof(*deporigins));
551 deporigins[dep_count] = deporigin;
552 deporigins[dep_count + 1] = NULL;
555 /* No origin recorded, try to register on literal package name */
556 sprintf(contents, "%s/%s/%s", LOG_DIR, p->name,
558 contfile = fopen(contents, "a");
560 warnx("can't open dependency file '%s'!\n"
561 "dependency registration is incomplete", contents);
563 fprintf(contfile, "%s\n", Plist.name);
564 if (fclose(contfile) == EOF) {
565 warnx("cannot properly close file %s", contents);
571 depmatches = matchallbyorigin((const char **)deporigins, NULL);
573 if (!IgnoreDeps && depmatches) {
574 for (i = 0; i < dep_count; i++) {
577 char **tmp = depmatches[i];
578 for (j = 0; tmp[j] != NULL; j++) {
579 /* Origin looked up */
580 sprintf(contents, "%s/%s/%s", LOG_DIR, tmp[j],
582 if (depnames[i] && strcmp(depnames[i], tmp[j]) != 0)
583 warnx("warning: package '%s' requires '%s', but '%s' "
584 "is installed", Plist.name, depnames[i], tmp[j]);
585 contfile = fopen(contents, "a");
587 warnx("can't open dependency file '%s'!\n"
588 "dependency registration is incomplete", contents);
590 fprintf(contfile, "%s\n", Plist.name);
591 if (fclose(contfile) == EOF)
592 warnx("cannot properly close file %s", contents);
595 } else if (depnames[i]) {
596 /* No package present with this origin, try literal package name */
597 sprintf(contents, "%s/%s/%s", LOG_DIR, depnames[i],
599 contfile = fopen(contents, "a");
601 warnx("can't open dependency file '%s'!\n"
602 "dependency registration is incomplete", contents);
604 fprintf(contfile, "%s\n", Plist.name);
605 if (fclose(contfile) == EOF) {
606 warnx("cannot properly close file %s", contents);
614 printf("Package %s registered in %s\n", Plist.name, LogDir);
617 if ((p = find_plist(&Plist, PLIST_DISPLAY)) != NULL) {
621 snprintf(buf, sizeof buf, "%s/%s", LogDir, p->name);
622 fp = fopen(buf, "r");
625 while (fgets(buf, sizeof(buf), fp))
631 warnx("cannot open %s as display file", buf);
643 /* Nuke the whole (installed) show, XXX but don't clean directories */
645 delete_package(FALSE, FALSE, &Plist);
648 /* delete the packing list contents */
655 sanity_check(char *pkg)
659 if (!fexists(CONTENTS_FNAME)) {
660 warnx("package %s has no CONTENTS file!", pkg);
663 else if (!fexists(COMMENT_FNAME)) {
664 warnx("package %s has no COMMENT file!", pkg);
667 else if (!fexists(DESC_FNAME)) {
668 warnx("package %s has no DESC file!", pkg);
677 static int in_cleanup = 0;
682 printf("Signal %d received, cleaning up..\n", sig);
683 if (!Fake && zapLogDir && LogDir[0])
684 vsystem("%s -rf %s", REMOVE_CMD, LogDir);
685 while (leave_playpen())