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.3 2003/11/19 15:17:29 hmp 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;
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 /* Are we coming in for a second pass, everything already extracted? */
91 fgets(playpen, FILENAME_MAX, stdin);
92 playpen[strlen(playpen) - 1] = '\0'; /* pesky newline! */
93 if (chdir(playpen) == FAIL) {
94 warnx("pkg_add in SLAVE mode can't chdir to %s", playpen);
97 read_plist(&Plist, stdin);
100 /* Nope - do it now */
102 /* Is it an ftp://foo.bar.baz/file.t[bg]z specification? */
104 if (!(Home = fileGetURL(NULL, pkg))) {
105 warnx("unable to fetch '%s' by URL", pkg);
109 strcpy(pkg_fullname, pkg);
110 cfile = fopen(CONTENTS_FNAME, "r");
113 "unable to open table of contents file '%s' - not a package?",
117 read_plist(&Plist, cfile);
121 strcpy(pkg_fullname, pkg); /*
122 * Copy for sanity's sake,
123 * could remove pkg_fullname
125 if (strcmp(pkg, "-")) {
126 if (stat(pkg_fullname, &sb) == FAIL) {
127 warnx("can't stat package file '%s'", pkg_fullname);
130 sprintf(extract_contents, "--fast-read %s", CONTENTS_FNAME);
131 extract = extract_contents;
135 sb.st_size = 100000; /* Make up a plausible average size */
137 Home = make_playpen(playpen, sb.st_size * 4);
139 errx(1, "unable to make playpen for %qd bytes", (long long)sb.st_size * 4);
141 /* Since we can call ourselves recursively, keep notes on where we came from */
143 setenv("_TOP", Home, 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("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 %qd 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) ||
246 matchbyorigin(Plist.origin, NULL) != NULL) && !Force) {
247 warnx("package '%s' or its older version already installed",
250 goto success; /* close enough for government work */
253 /* Now check the packing list for conflicts */
254 for (p = Plist.head; p != NULL; p = p->next) {
255 if (p->type == PLIST_CONFLICTS) {
256 conflict[0] = strdup(p->name);
258 matched = matchinstalled(MATCH_GLOB, conflict, &errcode);
260 if (errcode == 0 && matched != NULL) {
262 for (i = 0; matched[i] != NULL; i++)
263 if (isinstalledpkg(matched[i])) {
264 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 /* Now check the packing list for dependencies */
283 for (p = Plist.head; p ; p = p->next) {
286 if (p->type != PLIST_PKGDEP)
288 deporigin = (p->next->type == PLIST_DEPORIGIN) ? p->next->name : NULL;
290 printf("Package '%s' depends on '%s'", Plist.name, p->name);
291 if (deporigin != NULL)
292 printf(" with '%s' origin", deporigin);
295 if (!isinstalledpkg(p->name) &&
296 !(deporigin != NULL && matchbyorigin(deporigin, NULL) != NULL)) {
297 char path[FILENAME_MAX], *cp = NULL;
300 if (!isURL(pkg) && !getenv("PKG_ADD_BASE")) {
303 ext = strrchr(pkg_fullname, '.');
306 snprintf(path, FILENAME_MAX, "%s/%s%s", getenv("_TOP"), p->name, ext);
310 cp = fileFindByPath(pkg, p->name);
313 printf("Loading it from %s.\n", cp);
314 if (vsystem("pkg_add %s'%s'", Verbose ? "-v " : "", cp)) {
315 warnx("autoload of dependency '%s' failed%s",
316 cp, Force ? " (proceeding anyway)" : "!");
322 warnx("could not find package %s %s",
323 p->name, Force ? " (proceeding anyway)" : "!");
328 else if ((cp = fileGetURL(pkg, p->name)) != NULL) {
330 printf("Finished loading %s over FTP.\n", p->name);
331 if (!fexists("+CONTENTS")) {
332 warnx("autoloaded package %s has no +CONTENTS file?",
337 else if (vsystem("(pwd; cat +CONTENTS) | pkg_add %s-S", Verbose ? "-v " : "")) {
338 warnx("pkg_add of dependency '%s' failed%s",
339 p->name, Force ? " (proceeding anyway)" : "!");
344 printf("\t'%s' loaded successfully.\n", p->name);
345 /* Nuke the temporary playpen */
351 printf("and was not found%s.\n", Force ? " (proceeding anyway)" : "");
353 printf("Package dependency %s for %s not found%s\n", p->name, pkg,
354 Force ? " (proceeding anyway)" : "!");
360 printf(" - already installed.\n");
366 /* Look for the requirements file */
367 if (fexists(REQUIRE_FNAME)) {
368 vsystem("chmod +x %s", REQUIRE_FNAME); /* be sure */
370 printf("Running requirements file first for %s..\n", Plist.name);
371 if (!Fake && vsystem("./%s %s INSTALL", REQUIRE_FNAME, Plist.name)) {
372 warnx("package %s fails requirements %s", pkg_fullname,
373 Force ? "installing anyway" : "- not installed");
376 goto success; /* close enough for government work */
382 * Test whether to use the old method of passing tokens to installation
383 * scripts, and set appropriate variables..
386 if (fexists(POST_INSTALL_FNAME)) {
388 sprintf(post_script, "%s", POST_INSTALL_FNAME);
392 if (fexists(INSTALL_FNAME)) {
393 sprintf(post_script, "%s", INSTALL_FNAME);
394 sprintf(pre_arg, "PRE-INSTALL");
395 sprintf(post_arg, "POST-INSTALL");
399 /* If we're really installing, and have an installation file, run it */
400 if (!NoInstall && fexists(pre_script)) {
401 vsystem("chmod +x %s", pre_script); /* make sure */
403 printf("Running pre-install for %s..\n", Plist.name);
404 if (!Fake && vsystem("./%s %s %s", pre_script, Plist.name, pre_arg)) {
405 warnx("install script returned error status");
408 goto success; /* nothing to uninstall yet */
412 /* Now finally extract the entire show if we're not going direct */
413 if (!inPlace && !Fake)
414 extract_plist(".", &Plist);
416 if (!Fake && fexists(MTREE_FNAME)) {
418 printf("Running mtree for %s..\n", Plist.name);
419 p = find_plist(&Plist, PLIST_CWD);
421 printf("mtree -U -f %s -d -e -p %s >%s\n", MTREE_FNAME, p ? p->name : "/", _PATH_DEVNULL);
423 if (vsystem("/usr/sbin/mtree -U -f %s -d -e -p %s >%s", MTREE_FNAME, p ? p->name : "/", _PATH_DEVNULL))
424 warnx("mtree returned a non-zero status - continuing");
428 /* Run the installation script one last time? */
429 if (!NoInstall && fexists(post_script)) {
430 vsystem("chmod +x %s", post_script); /* make sure */
432 printf("Running post-install for %s..\n", Plist.name);
433 if (!Fake && vsystem("./%s %s %s", post_script, Plist.name, post_arg)) {
434 warnx("install script returned error status");
441 /* Time to record the deed? */
442 if (!NoRecord && !Fake) {
443 char contents[FILENAME_MAX];
447 warnx("not running as root - trying to record install anyway");
448 sprintf(LogDir, "%s/%s", LOG_DIR, Plist.name);
451 printf("Attempting to record package into %s..\n", LogDir);
452 if (make_hierarchy(LogDir)) {
453 warnx("can't record package into '%s', you're on your own!",
455 bzero(LogDir, FILENAME_MAX);
457 goto success; /* close enough for government work */
459 /* Make sure pkg_info can read the entry */
460 vsystem("chmod a+rx %s", LogDir);
461 move_file(".", DESC_FNAME, LogDir);
462 move_file(".", COMMENT_FNAME, LogDir);
463 if (fexists(INSTALL_FNAME))
464 move_file(".", INSTALL_FNAME, LogDir);
465 if (fexists(POST_INSTALL_FNAME))
466 move_file(".", POST_INSTALL_FNAME, LogDir);
467 if (fexists(DEINSTALL_FNAME))
468 move_file(".", DEINSTALL_FNAME, LogDir);
469 if (fexists(POST_DEINSTALL_FNAME))
470 move_file(".", POST_DEINSTALL_FNAME, LogDir);
471 if (fexists(REQUIRE_FNAME))
472 move_file(".", REQUIRE_FNAME, LogDir);
473 if (fexists(DISPLAY_FNAME))
474 move_file(".", DISPLAY_FNAME, LogDir);
475 if (fexists(MTREE_FNAME))
476 move_file(".", MTREE_FNAME, LogDir);
477 sprintf(contents, "%s/%s", LogDir, CONTENTS_FNAME);
478 contfile = fopen(contents, "w");
480 warnx("can't open new contents file '%s'! can't register pkg",
482 goto success; /* can't log, but still keep pkg */
484 write_plist(&Plist, contfile);
486 for (p = Plist.head; p ; p = p->next) {
487 char *deporigin, **depnames;
490 if (p->type != PLIST_PKGDEP)
492 deporigin = (p->next->type == PLIST_DEPORIGIN) ? p->next->name :
495 printf("Trying to record dependency on package '%s'", p->name);
496 if (deporigin != NULL)
497 printf(" with '%s' origin", deporigin);
501 depnames = (deporigin != NULL) ? matchbyorigin(deporigin, NULL) :
503 if (depnames == NULL) {
504 depnames = alloca(sizeof(*depnames) * 2);
505 depnames[0] = p->name;
508 for (i = 0; depnames[i] != NULL; i++) {
509 sprintf(contents, "%s/%s/%s", LOG_DIR, depnames[i],
511 if (strcmp(p->name, depnames[i]) != 0)
512 warnx("warning: package '%s' requires '%s', but '%s' "
513 "is installed", Plist.name, p->name, depnames[i]);
514 contfile = fopen(contents, "a");
516 warnx("can't open dependency file '%s'!\n"
517 "dependency registration is incomplete", contents);
519 fprintf(contfile, "%s\n", Plist.name);
520 if (fclose(contfile) == EOF)
521 warnx("cannot properly close file %s", contents);
526 printf("Package %s registered in %s\n", Plist.name, LogDir);
529 if ((p = find_plist(&Plist, PLIST_DISPLAY)) != NULL) {
533 snprintf(buf, sizeof buf, "%s/%s", LogDir, p->name);
534 fp = fopen(buf, "r");
537 while (fgets(buf, sizeof(buf), fp))
542 warnx("cannot open %s as display file", buf);
552 /* Nuke the whole (installed) show, XXX but don't clean directories */
554 delete_package(FALSE, FALSE, &Plist);
557 /* delete the packing list contents */
564 sanity_check(char *pkg)
568 if (!fexists(CONTENTS_FNAME)) {
569 warnx("package %s has no CONTENTS file!", pkg);
572 else if (!fexists(COMMENT_FNAME)) {
573 warnx("package %s has no COMMENT file!", pkg);
576 else if (!fexists(DESC_FNAME)) {
577 warnx("package %s has no DESC file!", pkg);
586 static int in_cleanup = 0;
591 printf("Signal %d received, cleaning up..\n", sig);
592 if (!Fake && zapLogDir && LogDir[0])
593 vsystem("%s -rf %s", REMOVE_CMD, LogDir);