perform.c revision 131280
1327Sjkh/*
2327Sjkh * FreeBSD install - a package for the installation and maintainance
3327Sjkh * of non-core utilities.
4327Sjkh *
5327Sjkh * Redistribution and use in source and binary forms, with or without
6327Sjkh * modification, are permitted provided that the following conditions
7327Sjkh * are met:
8327Sjkh * 1. Redistributions of source code must retain the above copyright
9327Sjkh *    notice, this list of conditions and the following disclaimer.
10327Sjkh * 2. Redistributions in binary form must reproduce the above copyright
11327Sjkh *    notice, this list of conditions and the following disclaimer in the
12327Sjkh *    documentation and/or other materials provided with the distribution.
13327Sjkh *
14327Sjkh * Jordan K. Hubbard
15327Sjkh * 18 July 1993
16327Sjkh *
17327Sjkh * This is the main body of the add module.
18327Sjkh *
19327Sjkh */
20327Sjkh
2193520Sobrien#include <sys/cdefs.h>
2293520Sobrien__FBSDID("$FreeBSD: head/usr.sbin/pkg_install/add/perform.c 131280 2004-06-29 18:59:19Z eik $");
2393520Sobrien
2430221Scharnier#include <err.h>
2569793Sobrien#include <paths.h>
26327Sjkh#include "lib.h"
27327Sjkh#include "add.h"
28327Sjkh
2984670Ssobomax#include <libgen.h>
30327Sjkh#include <signal.h>
314996Sjkh#include <sys/wait.h>
32327Sjkh
33327Sjkhstatic int pkg_do(char *);
34327Sjkhstatic int sanity_check(char *);
35327Sjkhstatic char LogDir[FILENAME_MAX];
3637728Seivindstatic int zapLogDir;		/* Should we delete LogDir? */
37327Sjkh
38327Sjkhint
39327Sjkhpkg_perform(char **pkgs)
40327Sjkh{
41327Sjkh    int i, err_cnt = 0;
42327Sjkh
43327Sjkh    signal(SIGINT, cleanup);
44327Sjkh    signal(SIGHUP, cleanup);
45327Sjkh
46382Sjkh    if (AddMode == SLAVE)
47382Sjkh	err_cnt = pkg_do(NULL);
48382Sjkh    else {
49382Sjkh	for (i = 0; pkgs[i]; i++)
50382Sjkh	    err_cnt += pkg_do(pkgs[i]);
51382Sjkh    }
52327Sjkh    return err_cnt;
53327Sjkh}
54327Sjkh
55327Sjkhstatic Package Plist;
5611780Sjkhstatic char *Home;
57327Sjkh
588083Sjkh/*
598083Sjkh * This is seriously ugly code following.  Written very fast!
608083Sjkh * [And subsequently made even worse..  Sigh!  This code was just born
618083Sjkh * to be hacked, I guess.. :) -jkh]
628083Sjkh */
63327Sjkhstatic int
64327Sjkhpkg_do(char *pkg)
65327Sjkh{
66327Sjkh    char pkg_fullname[FILENAME_MAX];
6711780Sjkh    char playpen[FILENAME_MAX];
687998Sjkh    char extract_contents[FILENAME_MAX];
6984670Ssobomax    char *where_to, *extract;
70327Sjkh    FILE *cfile;
7111780Sjkh    int code;
721545Sjkh    PackingList p;
733364Sjkh    struct stat sb;
74131280Seik    int inPlace, conflictsfound, errcode;
7541866Sjkh    /* support for separate pre/post install scripts */
7641866Sjkh    int new_m = 0;
7741866Sjkh    char pre_script[FILENAME_MAX] = INSTALL_FNAME;
7841866Sjkh    char post_script[FILENAME_MAX];
7941866Sjkh    char pre_arg[FILENAME_MAX], post_arg[FILENAME_MAX];
80113594Skris    char *conflict[2];
81113594Skris    char **matched;
82327Sjkh
83113594Skris    conflictsfound = 0;
8411780Sjkh    code = 0;
8537728Seivind    zapLogDir = 0;
86327Sjkh    LogDir[0] = '\0';
8711780Sjkh    strcpy(playpen, FirstPen);
8811780Sjkh    inPlace = 0;
898075Sjkh
908083Sjkh    /* Are we coming in for a second pass, everything already extracted? */
9111780Sjkh    if (!pkg) {
9211780Sjkh	fgets(playpen, FILENAME_MAX, stdin);
9311780Sjkh	playpen[strlen(playpen) - 1] = '\0'; /* pesky newline! */
9411780Sjkh	if (chdir(playpen) == FAIL) {
9530221Scharnier	    warnx("pkg_add in SLAVE mode can't chdir to %s", playpen);
96382Sjkh	    return 1;
97382Sjkh	}
98382Sjkh	read_plist(&Plist, stdin);
9911780Sjkh	where_to = playpen;
100327Sjkh    }
1018083Sjkh    /* Nope - do it now */
102382Sjkh    else {
103102383Sobrien	/* Is it an ftp://foo.bar.baz/file.t[bg]z specification? */
1048083Sjkh	if (isURL(pkg)) {
10511780Sjkh	    if (!(Home = fileGetURL(NULL, pkg))) {
10681046Ssobomax		warnx("unable to fetch '%s' by URL", pkg);
1078083Sjkh		return 1;
1088083Sjkh	    }
10911780Sjkh	    where_to = Home;
11011780Sjkh	    strcpy(pkg_fullname, pkg);
11111780Sjkh	    cfile = fopen(CONTENTS_FNAME, "r");
11211780Sjkh	    if (!cfile) {
11330221Scharnier		warnx(
11481046Ssobomax		"unable to open table of contents file '%s' - not a package?",
11530221Scharnier		CONTENTS_FNAME);
11611780Sjkh		goto bomb;
11711780Sjkh	    }
11811780Sjkh	    read_plist(&Plist, cfile);
11911780Sjkh	    fclose(cfile);
120382Sjkh	}
1218083Sjkh	else {
12276739Ssobomax	    strcpy(pkg_fullname, pkg);		/*
12376739Ssobomax						 * Copy for sanity's sake,
12476739Ssobomax						 * could remove pkg_fullname
12576739Ssobomax						 */
12614582Sjkh	    if (strcmp(pkg, "-")) {
12714582Sjkh		if (stat(pkg_fullname, &sb) == FAIL) {
12830221Scharnier		    warnx("can't stat package file '%s'", pkg_fullname);
12914582Sjkh		    goto bomb;
13014582Sjkh		}
13114582Sjkh		sprintf(extract_contents, "--fast-read %s", CONTENTS_FNAME);
13214582Sjkh		extract = extract_contents;
13311780Sjkh	    }
13422750Sjkh	    else {
13514582Sjkh		extract = NULL;
13622750Sjkh		sb.st_size = 100000;	/* Make up a plausible average size */
13722750Sjkh	    }
13811780Sjkh	    Home = make_playpen(playpen, sb.st_size * 4);
13911780Sjkh	    if (!Home)
14084745Ssobomax		errx(1, "unable to make playpen for %qd bytes", (long long)sb.st_size * 4);
14111780Sjkh	    where_to = Home;
14238583Sjkh	    /* Since we can call ourselves recursively, keep notes on where we came from */
14338583Sjkh	    if (!getenv("_TOP"))
14438583Sjkh		setenv("_TOP", Home, 1);
14514582Sjkh	    if (unpack(pkg_fullname, extract)) {
14630221Scharnier		warnx(
14781046Ssobomax	"unable to extract table of contents file from '%s' - not a package?",
14830221Scharnier		pkg_fullname);
14911780Sjkh		goto bomb;
15011780Sjkh	    }
15111780Sjkh	    cfile = fopen(CONTENTS_FNAME, "r");
15211780Sjkh	    if (!cfile) {
15330221Scharnier		warnx(
15481046Ssobomax	"unable to open table of contents file '%s' - not a package?",
15530221Scharnier		CONTENTS_FNAME);
15611780Sjkh		goto bomb;
15711780Sjkh	    }
15811780Sjkh	    read_plist(&Plist, cfile);
15911780Sjkh	    fclose(cfile);
1607998Sjkh
16111780Sjkh	    /* Extract directly rather than moving?  Oh goodie! */
16211780Sjkh	    if (find_plist_option(&Plist, "extract-in-place")) {
16311780Sjkh		if (Verbose)
16411780Sjkh		    printf("Doing in-place extraction for %s\n", pkg_fullname);
16511780Sjkh		p = find_plist(&Plist, PLIST_CWD);
16611780Sjkh		if (p) {
16711780Sjkh		    if (!isdir(p->name) && !Fake) {
16811780Sjkh			if (Verbose)
16911780Sjkh			    printf("Desired prefix of %s does not exist, creating..\n", p->name);
17011780Sjkh			vsystem("mkdir -p %s", p->name);
17111780Sjkh			if (chdir(p->name) == -1) {
17281046Ssobomax			    warn("unable to change directory to '%s'", p->name);
17311780Sjkh			    goto bomb;
17411780Sjkh			}
1757998Sjkh		    }
17611780Sjkh		    where_to = p->name;
17711780Sjkh		    inPlace = 1;
1787998Sjkh		}
17911780Sjkh		else {
18030221Scharnier		    warnx(
18181046Ssobomax		"no prefix specified in '%s' - this is a bad package!",
18230221Scharnier			pkg_fullname);
18311780Sjkh		    goto bomb;
18411780Sjkh		}
1857998Sjkh	    }
18611780Sjkh
18711780Sjkh	    /*
18811780Sjkh	     * Apply a crude heuristic to see how much space the package will
18911780Sjkh	     * take up once it's unpacked.  I've noticed that most packages
19011780Sjkh	     * compress an average of 75%, so multiply by 4 for good measure.
19111780Sjkh	     */
19211780Sjkh
19393660Smurray	    if (!extract && !inPlace && min_free(playpen) < sb.st_size * 4) {
19449637Sbillf		warnx("projected size of %qd exceeds available free space.\n"
19530221Scharnier"Please set your PKG_TMPDIR variable to point to a location with more\n"
19684745Ssobomax		       "free space and try again", (long long)sb.st_size * 4);
19730221Scharnier		warnx("not extracting %s\ninto %s, sorry!",
19830221Scharnier			pkg_fullname, where_to);
1998083Sjkh		goto bomb;
2007998Sjkh	    }
2019786Sjkh
20211780Sjkh	    /* If this is a direct extract and we didn't want it, stop now */
20311780Sjkh	    if (inPlace && Fake)
20411780Sjkh		goto success;
2057998Sjkh
20693660Smurray	    /* Finally unpack the whole mess.  If extract is null we
20793660Smurray	       already + did so so don't bother doing it again. */
20893660Smurray	    if (extract && unpack(pkg_fullname, NULL)) {
20981046Ssobomax		warnx("unable to extract '%s'!", pkg_fullname);
21011780Sjkh		goto bomb;
21111780Sjkh	    }
21211780Sjkh	}
21314582Sjkh
21411780Sjkh	/* Check for sanity and dependencies */
21511780Sjkh	if (sanity_check(pkg))
2167998Sjkh	    goto bomb;
21711780Sjkh
21811780Sjkh	/* If we're running in MASTER mode, just output the plist and return */
21911780Sjkh	if (AddMode == MASTER) {
22011780Sjkh	    printf("%s\n", where_playpen());
22111780Sjkh	    write_plist(&Plist, stdout);
22211780Sjkh	    return 0;
2237996Sjkh	}
22411780Sjkh    }
225327Sjkh
22611780Sjkh    /*
22711780Sjkh     * If we have a prefix, delete the first one we see and add this
22811780Sjkh     * one in place of it.
22911780Sjkh     */
23011780Sjkh    if (Prefix) {
23111780Sjkh	delete_plist(&Plist, FALSE, PLIST_CWD, NULL);
23211780Sjkh	add_plist_top(&Plist, PLIST_CWD, Prefix);
23311780Sjkh    }
2347998Sjkh
23511780Sjkh    setenv(PKG_PREFIX_VNAME, (p = find_plist(&Plist, PLIST_CWD)) ? p->name : ".", 1);
23696613Ssobomax    /* Protect against old packages with bogus @name and origin fields */
23796613Ssobomax    if (Plist.name == NULL)
23896613Ssobomax	Plist.name = "anonymous";
23996613Ssobomax    if (Plist.origin == NULL)
24096613Ssobomax	Plist.origin = "anonymous/anonymous";
2417998Sjkh
24296613Ssobomax    /*
24396613Ssobomax     * See if we're already registered either with the same name (the same
24496613Ssobomax     * version) or some other version with the same origin.
24596613Ssobomax     */
246131280Seik    if ((isinstalledpkg(Plist.name) > 0 ||
24796613Ssobomax         matchbyorigin(Plist.origin, NULL) != NULL) && !Force) {
24896613Ssobomax	warnx("package '%s' or its older version already installed",
24996613Ssobomax	      Plist.name);
25011780Sjkh	code = 1;
25111780Sjkh	goto success;	/* close enough for government work */
25211780Sjkh    }
2537998Sjkh
254113594Skris    /* Now check the packing list for conflicts */
255113594Skris    for (p = Plist.head; p != NULL; p = p->next) {
256113594Skris	if (p->type == PLIST_CONFLICTS) {
257131280Seik	    int i;
258113594Skris	    conflict[0] = strdup(p->name);
259113594Skris	    conflict[1] = NULL;
260113594Skris	    matched = matchinstalled(MATCH_GLOB, conflict, &errcode);
261113594Skris	    free(conflict[0]);
262113594Skris	    if (errcode == 0 && matched != NULL)
263113594Skris		for (i = 0; matched[i] != NULL; i++)
264131280Seik		    if (isinstalledpkg(matched[i]) > 0) {
265113594Skris			warnx("package '%s' conflicts with %s", Plist.name,
266113594Skris				matched[i]);
267113594Skris			conflictsfound = 1;
268113594Skris		    }
269113594Skris
270113594Skris	    continue;
271113594Skris	}
272113594Skris    }
273113594Skris    if(conflictsfound) {
274113594Skris	if(!Force) {
275113594Skris	    warnx("please use pkg_delete first to remove conflicting package(s) or -f to force installation");
276113594Skris	    code = 1;
277113594Skris	    goto bomb;
278113594Skris	} else
279113594Skris	    warnx("-f specified; proceeding anyway");
280113594Skris    }
281113594Skris
28211780Sjkh    /* Now check the packing list for dependencies */
28311780Sjkh    for (p = Plist.head; p ; p = p->next) {
28496613Ssobomax	char *deporigin;
28596613Ssobomax
28611780Sjkh	if (p->type != PLIST_PKGDEP)
28711780Sjkh	    continue;
28896613Ssobomax	deporigin = (p->next->type == PLIST_DEPORIGIN) ? p->next->name : NULL;
28996613Ssobomax	if (Verbose) {
29096613Ssobomax	    printf("Package '%s' depends on '%s'", Plist.name, p->name);
29196613Ssobomax	    if (deporigin != NULL)
29296613Ssobomax		printf(" with '%s' origin", deporigin);
29396613Ssobomax	    printf(".\n");
29496613Ssobomax	}
295131280Seik	if (isinstalledpkg(p->name) <= 0 &&
29696613Ssobomax	    !(deporigin != NULL && matchbyorigin(deporigin, NULL) != NULL)) {
29711780Sjkh	    char path[FILENAME_MAX], *cp = NULL;
2988083Sjkh
29918929Sjkh	    if (!Fake) {
30018929Sjkh		if (!isURL(pkg) && !getenv("PKG_ADD_BASE")) {
301102888Ssobomax		    const char *ext;
302102888Ssobomax
303102888Ssobomax		    ext = strrchr(pkg_fullname, '.');
304102888Ssobomax		    if (ext == NULL)
305102888Ssobomax			ext = ".tbz";
306102888Ssobomax		    snprintf(path, FILENAME_MAX, "%s/%s%s", getenv("_TOP"), p->name, ext);
30718929Sjkh		    if (fexists(path))
30818929Sjkh			cp = path;
30918929Sjkh		    else
31018929Sjkh			cp = fileFindByPath(pkg, p->name);
31118929Sjkh		    if (cp) {
31218929Sjkh			if (Verbose)
31318929Sjkh			    printf("Loading it from %s.\n", cp);
31478118Sjkh			if (vsystem("pkg_add %s'%s'", Verbose ? "-v " : "", cp)) {
31581046Ssobomax			    warnx("autoload of dependency '%s' failed%s",
31630221Scharnier				cp, Force ? " (proceeding anyway)" : "!");
31718929Sjkh			    if (!Force)
31818929Sjkh				++code;
31918929Sjkh			}
32018929Sjkh		    }
32138931Sjkh		    else {
32238931Sjkh			warnx("could not find package %s %s",
32338931Sjkh			      p->name, Force ? " (proceeding anyway)" : "!");
32438931Sjkh			if (!Force)
32538931Sjkh			    ++code;
32638931Sjkh		    }
32718929Sjkh		}
32818929Sjkh		else if ((cp = fileGetURL(pkg, p->name)) != NULL) {
3298075Sjkh		    if (Verbose)
33018929Sjkh			printf("Finished loading %s over FTP.\n", p->name);
33118929Sjkh		    if (!fexists("+CONTENTS")) {
33230221Scharnier			warnx("autoloaded package %s has no +CONTENTS file?",
33330221Scharnier				p->name);
3348075Sjkh			if (!Force)
3358075Sjkh			    ++code;
3368075Sjkh		    }
33718929Sjkh		    else if (vsystem("(pwd; cat +CONTENTS) | pkg_add %s-S", Verbose ? "-v " : "")) {
33881046Ssobomax			warnx("pkg_add of dependency '%s' failed%s",
33930221Scharnier				p->name, Force ? " (proceeding anyway)" : "!");
34018929Sjkh			if (!Force)
34118929Sjkh			    ++code;
34218929Sjkh		    }
34318929Sjkh		    else if (Verbose)
34481046Ssobomax			printf("\t'%s' loaded successfully.\n", p->name);
34518929Sjkh		    /* Nuke the temporary playpen */
34633427Sjkh		    leave_playpen();
3478075Sjkh		}
34811780Sjkh	    }
34911780Sjkh	    else {
35011780Sjkh		if (Verbose)
35111780Sjkh		    printf("and was not found%s.\n", Force ? " (proceeding anyway)" : "");
35211780Sjkh		else
35311780Sjkh		    printf("Package dependency %s for %s not found%s\n", p->name, pkg,
35411780Sjkh			   Force ? " (proceeding anyway)" : "!");
35511780Sjkh		if (!Force)
35611780Sjkh		    ++code;
35711780Sjkh	    }
3588075Sjkh	}
35911780Sjkh	else if (Verbose)
36011780Sjkh	    printf(" - already installed.\n");
361327Sjkh    }
3627713Sjkh
36316087Sjkh    if (code != 0)
36416087Sjkh	goto bomb;
36516087Sjkh
3668075Sjkh    /* Look for the requirements file */
367327Sjkh    if (fexists(REQUIRE_FNAME)) {
368327Sjkh	vsystem("chmod +x %s", REQUIRE_FNAME);	/* be sure */
369327Sjkh	if (Verbose)
37096613Ssobomax	    printf("Running requirements file first for %s..\n", Plist.name);
37196613Ssobomax	if (!Fake && vsystem("./%s %s INSTALL", REQUIRE_FNAME, Plist.name)) {
37230221Scharnier	    warnx("package %s fails requirements %s", pkg_fullname,
37330221Scharnier		   Force ? "installing anyway" : "- not installed");
3744996Sjkh	    if (!Force) {
3754996Sjkh		code = 1;
3764996Sjkh		goto success;	/* close enough for government work */
3774996Sjkh	    }
378327Sjkh	}
379327Sjkh    }
3808075Sjkh
38176739Ssobomax    /*
38276739Ssobomax     * Test whether to use the old method of passing tokens to installation
38341866Sjkh     * scripts, and set appropriate variables..
38441866Sjkh     */
38541866Sjkh
38641866Sjkh    if (fexists(POST_INSTALL_FNAME)) {
38741866Sjkh	new_m = 1;
38841866Sjkh	sprintf(post_script, "%s", POST_INSTALL_FNAME);
38949637Sbillf	pre_arg[0] = '\0';
39049637Sbillf	post_arg[0] = '\0';
39141866Sjkh    } else {
39241866Sjkh	if (fexists(INSTALL_FNAME)) {
39341866Sjkh	    sprintf(post_script, "%s", INSTALL_FNAME);
39441866Sjkh	    sprintf(pre_arg, "PRE-INSTALL");
39541866Sjkh	    sprintf(post_arg, "POST-INSTALL");
39641866Sjkh	}
39741866Sjkh    }
39841866Sjkh
3998075Sjkh    /* If we're really installing, and have an installation file, run it */
40041866Sjkh    if (!NoInstall && fexists(pre_script)) {
40141866Sjkh	vsystem("chmod +x %s", pre_script);	/* make sure */
402327Sjkh	if (Verbose)
40396613Ssobomax	    printf("Running pre-install for %s..\n", Plist.name);
40496613Ssobomax	if (!Fake && vsystem("./%s %s %s", pre_script, Plist.name, pre_arg)) {
40530221Scharnier	    warnx("install script returned error status");
40641866Sjkh	    unlink(pre_script);
4074996Sjkh	    code = 1;
4084996Sjkh	    goto success;		/* nothing to uninstall yet */
409327Sjkh	}
410327Sjkh    }
4118075Sjkh
4128075Sjkh    /* Now finally extract the entire show if we're not going direct */
41311780Sjkh    if (!inPlace && !Fake)
41411780Sjkh	extract_plist(".", &Plist);
4158075Sjkh
4168075Sjkh    if (!Fake && fexists(MTREE_FNAME)) {
4174996Sjkh	if (Verbose)
41896613Ssobomax	    printf("Running mtree for %s..\n", Plist.name);
4194996Sjkh	p = find_plist(&Plist, PLIST_CWD);
4204996Sjkh	if (Verbose)
42169793Sobrien	    printf("mtree -U -f %s -d -e -p %s >%s\n", MTREE_FNAME, p ? p->name : "/", _PATH_DEVNULL);
4228115Sjkh	if (!Fake) {
42371055Ssobomax	    if (vsystem("/usr/sbin/mtree -U -f %s -d -e -p %s >%s", MTREE_FNAME, p ? p->name : "/", _PATH_DEVNULL))
42430221Scharnier		warnx("mtree returned a non-zero status - continuing");
4258115Sjkh	}
4264996Sjkh    }
4278075Sjkh
4288083Sjkh    /* Run the installation script one last time? */
42941866Sjkh    if (!NoInstall && fexists(post_script)) {
43041866Sjkh	vsystem("chmod +x %s", post_script);	/* make sure */
431327Sjkh	if (Verbose)
43296613Ssobomax	    printf("Running post-install for %s..\n", Plist.name);
43396613Ssobomax	if (!Fake && vsystem("./%s %s %s", post_script, Plist.name, post_arg)) {
43430221Scharnier	    warnx("install script returned error status");
43541866Sjkh	    unlink(post_script);
4364996Sjkh	    code = 1;
437327Sjkh	    goto fail;
438327Sjkh	}
439327Sjkh    }
4408075Sjkh
4418083Sjkh    /* Time to record the deed? */
442327Sjkh    if (!NoRecord && !Fake) {
443382Sjkh	char contents[FILENAME_MAX];
44484745Ssobomax	FILE *contfile;
445382Sjkh
446327Sjkh	if (getuid() != 0)
44730221Scharnier	    warnx("not running as root - trying to record install anyway");
44896613Ssobomax	sprintf(LogDir, "%s/%s", LOG_DIR, Plist.name);
44937728Seivind	zapLogDir = 1;
450327Sjkh	if (Verbose)
451327Sjkh	    printf("Attempting to record package into %s..\n", LogDir);
452327Sjkh	if (make_hierarchy(LogDir)) {
45330221Scharnier	    warnx("can't record package into '%s', you're on your own!",
454327Sjkh		   LogDir);
455327Sjkh	    bzero(LogDir, FILENAME_MAX);
456327Sjkh	    code = 1;
457327Sjkh	    goto success;	/* close enough for government work */
458327Sjkh	}
459477Sjkh	/* Make sure pkg_info can read the entry */
460477Sjkh	vsystem("chmod a+rx %s", LogDir);
46184670Ssobomax	move_file(".", DESC_FNAME, LogDir);
46284670Ssobomax	move_file(".", COMMENT_FNAME, LogDir);
46384670Ssobomax	if (fexists(INSTALL_FNAME))
46484670Ssobomax	    move_file(".", INSTALL_FNAME, LogDir);
46584670Ssobomax	if (fexists(POST_INSTALL_FNAME))
46684670Ssobomax	    move_file(".", POST_INSTALL_FNAME, LogDir);
467327Sjkh	if (fexists(DEINSTALL_FNAME))
4687998Sjkh	    move_file(".", DEINSTALL_FNAME, LogDir);
46941866Sjkh	if (fexists(POST_DEINSTALL_FNAME))
47041866Sjkh	    move_file(".", POST_DEINSTALL_FNAME, LogDir);
471327Sjkh	if (fexists(REQUIRE_FNAME))
4727998Sjkh	    move_file(".", REQUIRE_FNAME, LogDir);
47384670Ssobomax	if (fexists(DISPLAY_FNAME))
47484670Ssobomax	    move_file(".", DISPLAY_FNAME, LogDir);
47584670Ssobomax	if (fexists(MTREE_FNAME))
47684670Ssobomax	    move_file(".", MTREE_FNAME, LogDir);
477382Sjkh	sprintf(contents, "%s/%s", LogDir, CONTENTS_FNAME);
47884745Ssobomax	contfile = fopen(contents, "w");
47984745Ssobomax	if (!contfile) {
48030221Scharnier	    warnx("can't open new contents file '%s'! can't register pkg",
48130221Scharnier		contents);
482382Sjkh	    goto success; /* can't log, but still keep pkg */
483382Sjkh	}
48484745Ssobomax	write_plist(&Plist, contfile);
48584745Ssobomax	fclose(contfile);
4864996Sjkh	for (p = Plist.head; p ; p = p->next) {
48796613Ssobomax	    char *deporigin, **depnames;
48896613Ssobomax	    int i;
48996613Ssobomax
4904996Sjkh	    if (p->type != PLIST_PKGDEP)
4914996Sjkh		continue;
49296613Ssobomax	    deporigin = (p->next->type == PLIST_DEPORIGIN) ? p->next->name :
49396613Ssobomax							     NULL;
49496613Ssobomax	    if (Verbose) {
49596613Ssobomax		printf("Trying to record dependency on package '%s'", p->name);
49696613Ssobomax		if (deporigin != NULL)
49796613Ssobomax		    printf(" with '%s' origin", deporigin);
49896613Ssobomax		printf(".\n");
4994996Sjkh	    }
50096613Ssobomax
50196613Ssobomax	    depnames = (deporigin != NULL) ? matchbyorigin(deporigin, NULL) :
50296613Ssobomax					     NULL;
50396613Ssobomax	    if (depnames == NULL) {
50496613Ssobomax		depnames = alloca(sizeof(*depnames) * 2);
50596613Ssobomax		depnames[0] = p->name;
50696613Ssobomax		depnames[1] = NULL;
50796613Ssobomax	    }
50896613Ssobomax	    for (i = 0; depnames[i] != NULL; i++) {
50996613Ssobomax		sprintf(contents, "%s/%s/%s", LOG_DIR, depnames[i],
51096613Ssobomax			REQUIRED_BY_FNAME);
51196613Ssobomax		if (strcmp(p->name, depnames[i]) != 0)
51296613Ssobomax		    warnx("warning: package '%s' requires '%s', but '%s' "
51396613Ssobomax			  "is installed", Plist.name, p->name, depnames[i]);
51496613Ssobomax		contfile = fopen(contents, "a");
51596613Ssobomax		if (!contfile)
51696613Ssobomax		    warnx("can't open dependency file '%s'!\n"
51796613Ssobomax			  "dependency registration is incomplete", contents);
51896613Ssobomax		else {
51996613Ssobomax		    fprintf(contfile, "%s\n", Plist.name);
52096613Ssobomax		    if (fclose(contfile) == EOF)
52196613Ssobomax			warnx("cannot properly close file %s", contents);
52296613Ssobomax		}
52396613Ssobomax	    }
5244996Sjkh	}
525327Sjkh	if (Verbose)
52696613Ssobomax	    printf("Package %s registered in %s\n", Plist.name, LogDir);
527327Sjkh    }
5288857Srgrimes
52916549Sjkh    if ((p = find_plist(&Plist, PLIST_DISPLAY)) != NULL) {
5304996Sjkh	FILE *fp;
5314996Sjkh	char buf[BUFSIZ];
53216549Sjkh
53321650Sjkh	snprintf(buf, sizeof buf, "%s/%s", LogDir, p->name);
53421650Sjkh	fp = fopen(buf, "r");
5354996Sjkh	if (fp) {
5364996Sjkh	    putc('\n', stdout);
5374996Sjkh	    while (fgets(buf, sizeof(buf), fp))
5384996Sjkh		fputs(buf, stdout);
5394996Sjkh	    putc('\n', stdout);
5404996Sjkh	    (void) fclose(fp);
5414996Sjkh	} else
54230221Scharnier	    warnx("cannot open %s as display file", buf);
5434996Sjkh    }
5444996Sjkh
545327Sjkh    goto success;
546327Sjkh
5477998Sjkh bomb:
5487998Sjkh    code = 1;
5497998Sjkh    goto success;
5507998Sjkh
551327Sjkh fail:
5524996Sjkh    /* Nuke the whole (installed) show, XXX but don't clean directories */
553327Sjkh    if (!Fake)
5544996Sjkh	delete_package(FALSE, FALSE, &Plist);
555327Sjkh
556327Sjkh success:
557327Sjkh    /* delete the packing list contents */
55811780Sjkh    free_plist(&Plist);
55933427Sjkh    leave_playpen();
560327Sjkh    return code;
561327Sjkh}
562327Sjkh
563327Sjkhstatic int
564327Sjkhsanity_check(char *pkg)
565327Sjkh{
5668075Sjkh    int code = 0;
5678075Sjkh
568327Sjkh    if (!fexists(CONTENTS_FNAME)) {
56930221Scharnier	warnx("package %s has no CONTENTS file!", pkg);
5708075Sjkh	code = 1;
571327Sjkh    }
5728075Sjkh    else if (!fexists(COMMENT_FNAME)) {
57330221Scharnier	warnx("package %s has no COMMENT file!", pkg);
5748075Sjkh	code = 1;
575327Sjkh    }
5768075Sjkh    else if (!fexists(DESC_FNAME)) {
57730221Scharnier	warnx("package %s has no DESC file!", pkg);
5788075Sjkh	code = 1;
579327Sjkh    }
5808075Sjkh    return code;
581327Sjkh}
582327Sjkh
583327Sjkhvoid
58439068Sjkhcleanup(int sig)
585327Sjkh{
58633427Sjkh    static int in_cleanup = 0;
58733427Sjkh
58833427Sjkh    if (!in_cleanup) {
58933427Sjkh	in_cleanup = 1;
59039068Sjkh    	if (sig)
59139068Sjkh	    printf("Signal %d received, cleaning up..\n", sig);
59237728Seivind    	if (!Fake && zapLogDir && LogDir[0])
59339068Sjkh	    vsystem("%s -rf %s", REMOVE_CMD, LogDir);
59433427Sjkh    	leave_playpen();
59533427Sjkh    }
59639068Sjkh    if (sig)
59739068Sjkh	exit(1);
598327Sjkh}
599