package.c revision 11672
111499Sjkh/*
211499Sjkh * The new sysinstall program.
311499Sjkh *
411499Sjkh * This is probably the last program in the `sysinstall' line - the next
511499Sjkh * generation being essentially a complete rewrite.
611499Sjkh *
711672Sjkh * $Id: package.c,v 1.11 1995/10/22 12:04:11 jkh Exp $
811499Sjkh *
911499Sjkh * Copyright (c) 1995
1011499Sjkh *	Jordan Hubbard.  All rights reserved.
1111499Sjkh *
1211499Sjkh * Redistribution and use in source and binary forms, with or without
1311499Sjkh * modification, are permitted provided that the following conditions
1411499Sjkh * are met:
1511499Sjkh * 1. Redistributions of source code must retain the above copyright
1611499Sjkh *    notice, this list of conditions and the following disclaimer,
1711499Sjkh *    verbatim and that no modifications are made prior to this
1811499Sjkh *    point in the file.
1911499Sjkh * 2. Redistributions in binary form must reproduce the above copyright
2011499Sjkh *    notice, this list of conditions and the following disclaimer in the
2111499Sjkh *    documentation and/or other materials provided with the distribution.
2211499Sjkh * 3. All advertising materials mentioning features or use of this software
2311499Sjkh *    must display the following acknowledgement:
2411499Sjkh *	This product includes software developed by Jordan Hubbard
2511499Sjkh *	for the FreeBSD Project.
2611499Sjkh * 4. The name of Jordan Hubbard or the FreeBSD project may not be used to
2711499Sjkh *    endorse or promote products derived from this software without specific
2811499Sjkh *    prior written permission.
2911499Sjkh *
3011499Sjkh * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
3111499Sjkh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3211499Sjkh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3311499Sjkh * ARE DISCLAIMED.  IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
3411499Sjkh * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3511499Sjkh * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3611499Sjkh * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
3711499Sjkh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3811499Sjkh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3911499Sjkh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
4011499Sjkh * SUCH DAMAGE.
4111499Sjkh *
4211499Sjkh */
4311499Sjkh
4411499Sjkh#include <stdio.h>
4511499Sjkh#include <string.h>
4611499Sjkh#include <stdlib.h>
4711499Sjkh#include <sys/errno.h>
4811499Sjkh#include <sys/param.h>
4911499Sjkh#include <sys/mount.h>
5011499Sjkh#include <sys/stat.h>
5111499Sjkh#include "sysinstall.h"
5211499Sjkh
5311499Sjkhstatic char *make_playpen(char *pen, size_t sz);
5411499Sjkh
5511650Sjkh/* Like package_extract, but assumes current media device */
5611650Sjkhint
5711650Sjkhpackage_add(char *name)
5811650Sjkh{
5911650Sjkh    if (!mediaVerify())
6011650Sjkh	return RET_FAIL;
6111650Sjkh    return package_extract(mediaDevice, name);
6211650Sjkh}
6311650Sjkh
6411499Sjkh/* Extract a package based on a namespec and a media device */
6511499Sjkhint
6611499Sjkhpackage_extract(Device *dev, char *name)
6711499Sjkh{
6811499Sjkh    char path[511];
6911499Sjkh    char pen[FILENAME_MAX];
7011499Sjkh    char *where;
7111499Sjkh    int i, fd, ret;
7211499Sjkh
7311536Sjkh    /* Check to make sure it's not already there */
7411672Sjkh    if (!vsystem("pkg_info -e %s", name)) {
7511672Sjkh	msgDebug("package %s marked as already installed - return SUCCESS.\n");
7611536Sjkh	return RET_SUCCESS;
7711672Sjkh    }
7811536Sjkh
7911553Sjkh    if (!dev->init(dev)) {
8011672Sjkh	dialog_clear();
8111672Sjkh	msgConfirm("Unable to initialize media type for package extract.");
8211499Sjkh	return RET_FAIL;
8311553Sjkh    }
8411499Sjkh
8511499Sjkh    ret = RET_FAIL;
8611553Sjkh    sprintf(path, "packages/All/%s%s", name, strstr(name, ".tgz") ? "" : ".tgz");
8711672Sjkh    msgNotify("pkg_extract: Attempting to fetch %s from %s", path, dev->name);
8811553Sjkh    fd = dev->get(dev, path, TRUE);
8911499Sjkh    if (fd >= 0) {
9011499Sjkh	pid_t tpid;
9111499Sjkh
9211499Sjkh	pen[0] = '\0';
9311499Sjkh	if ((where = make_playpen(pen, 0)) != NULL) {
9411499Sjkh	    if (isDebug())
9511499Sjkh		msgDebug("Working in temporary directory %s, will return to %s\n", pen, where);
9611499Sjkh	    tpid = fork();
9711499Sjkh	    if (!tpid) {
9811672Sjkh		dup2(fd, 0); close(fd);
9911672Sjkh		dup2(DebugFD, 1);
10011672Sjkh		dup2(DebugFD, 2);
10111672Sjkh		i = vsystem("tar %s-xpzf -", !strcmp(variable_get(VAR_CPIO_VERBOSITY), "high") ? "-v " : "");
10211672Sjkh		if (i)
10311672Sjkh		    msgDebug("tar command returns %d status (errno: %d)\n", i, errno);
10411499Sjkh		exit(i);
10511499Sjkh	    }
10611499Sjkh	    else {
10711499Sjkh		int pstat;
10811499Sjkh
10911499Sjkh		tpid = waitpid(tpid, &pstat, 0);
11011499Sjkh		if (vsystem("(pwd; cat +CONTENTS) | pkg_add %s-S",
11111672Sjkh			    !strcmp(variable_get(VAR_CPIO_VERBOSITY), "high") ? "-v " : "")) {
11211672Sjkh		    dialog_clear();
11311499Sjkh		    msgConfirm("An error occurred while trying to pkg_add %s.\n"
11411499Sjkh			       "Please check debugging screen for possible further details.", path);
11511672Sjkh		}
11611499Sjkh		else
11711499Sjkh		    ret = RET_SUCCESS;
11811567Sjkh		close(fd);
11911499Sjkh	    }
12011499Sjkh	    if (chdir(where) == -1)
12111627Sjkh		msgFatal("Unable to get back to where I was before, Jojo! (That was: %s)", where);
12211499Sjkh	    vsystem("rm -rf %s", pen);
12311499Sjkh	    if (isDebug())
12411499Sjkh		msgDebug("Nuked pen: %s\n", pen);
12511499Sjkh	}
12611672Sjkh	else {
12711672Sjkh	    dialog_clear();
12811499Sjkh	    msgConfirm("Unable to find a temporary location to unpack this stuff in.\n"
12911499Sjkh		       "You must simply not have enough space or you've configured your\n"
13011499Sjkh		       "system oddly.  Sorry!");
13111672Sjkh	    ret = RET_FAIL;
13211672Sjkh	}
13311499Sjkh	dev->close(dev, fd);
13411499Sjkh	if (dev->type == DEVICE_TYPE_TAPE)
13511499Sjkh	    unlink(path);
13611499Sjkh    }
13711672Sjkh    else {
13811553Sjkh	msgDebug("pkg_extract: get operation returned %d\n", fd);
13911672Sjkh	if (variable_get(VAR_NO_CONFIRM))
14011672Sjkh	    msgNotify("Unable to fetch package %s from selected media.\n"
14111672Sjkh		      "No package add will be done.");
14211672Sjkh	else {
14311672Sjkh	    dialog_clear();
14411672Sjkh	    msgConfirm("Unable to fetch package %s from selected media.\n"
14511672Sjkh		       "No package add will be done.");
14611672Sjkh	}
14711672Sjkh    }
14811499Sjkh    return ret;
14911499Sjkh}
15011499Sjkh
15111499Sjkhstatic size_t
15211499Sjkhmin_free(char *tmpdir)
15311499Sjkh{
15411499Sjkh    struct statfs buf;
15511499Sjkh
15611499Sjkh    if (statfs(tmpdir, &buf) != 0) {
15711499Sjkh	msgDebug("Error in statfs, errno = %d\n", errno);
15811499Sjkh	return -1;
15911499Sjkh    }
16011499Sjkh    return buf.f_bavail * buf.f_bsize;
16111499Sjkh}
16211499Sjkh
16311499Sjkh/* Find a good place to play. */
16411499Sjkhstatic char *
16511499Sjkhfind_play_pen(char *pen, size_t sz)
16611499Sjkh{
16711499Sjkh    struct stat sb;
16811499Sjkh
16911499Sjkh    if (pen[0] && stat(pen, &sb) != RET_FAIL && (min_free(pen) >= sz))
17011499Sjkh	return pen;
17111499Sjkh    else if (stat("/var/tmp", &sb) != RET_FAIL && min_free("/var/tmp") >= sz)
17211499Sjkh	strcpy(pen, "/var/tmp/instmp.XXXXXX");
17311499Sjkh    else if (stat("/tmp", &sb) != RET_FAIL && min_free("/tmp") >= sz)
17411499Sjkh	strcpy(pen, "/tmp/instmp.XXXXXX");
17511499Sjkh    else if ((stat("/usr/tmp", &sb) == RET_SUCCESS || mkdir("/usr/tmp", 01777) == RET_SUCCESS) &&
17611499Sjkh	     min_free("/usr/tmp") >= sz)
17711499Sjkh	strcpy(pen, "/usr/tmp/instmp.XXXXXX");
17811499Sjkh    else {
17911672Sjkh	dialog_clear();
18011499Sjkh	msgConfirm("Can't find enough temporary space to extract the files, please try\n"
18111499Sjkh		   "This again after your system is up (you can run /stand/sysinstall\n"
18211499Sjkh		   "directly) and you've had a chance to point /var/tmp somewhere with\n"
18311499Sjkh		   "sufficient temporary space available.");
18411499Sjkh	return NULL;
18511499Sjkh    }
18611499Sjkh    return pen;
18711499Sjkh}
18811499Sjkh
18911499Sjkh/*
19011499Sjkh * Make a temporary directory to play in and chdir() to it, returning
19111499Sjkh * pathname of previous working directory.
19211499Sjkh */
19311499Sjkhstatic char *
19411499Sjkhmake_playpen(char *pen, size_t sz)
19511499Sjkh{
19611499Sjkh    static char Previous[FILENAME_MAX];
19711499Sjkh
19811499Sjkh    if (!find_play_pen(pen, sz))
19911499Sjkh	return NULL;
20011499Sjkh
20111499Sjkh    if (!mktemp(pen)) {
20211672Sjkh	dialog_clear();
20311499Sjkh	msgConfirm("Can't mktemp '%s'.", pen);
20411499Sjkh	return NULL;
20511499Sjkh    }
20611499Sjkh    if (mkdir(pen, 0755) == RET_FAIL) {
20711672Sjkh	dialog_clear();
20811499Sjkh	msgConfirm("Can't mkdir '%s'.", pen);
20911499Sjkh	return NULL;
21011499Sjkh    }
21111499Sjkh    if (isDebug()) {
21211499Sjkh	if (sz)
21311499Sjkh	    msgDebug("Requested space: %d bytes, free space: %d bytes in %s\n", (int)sz, min_free(pen), pen);
21411499Sjkh    }
21511499Sjkh    if (min_free(pen) < sz) {
21611499Sjkh	rmdir(pen);
21711672Sjkh	dialog_clear();
21811499Sjkh	msgConfirm("Not enough free space to create: `%s'\n"
21911499Sjkh		   "Please try this again after your system is up (you can run\n"
22011499Sjkh		   "/stand/sysinstall directly) and you've had a chance to point\n"
22111499Sjkh		   "/var/tmp somewhere with sufficient temporary space available.");
22211499Sjkh        return NULL;
22311499Sjkh    }
22411499Sjkh    if (!getcwd(Previous, FILENAME_MAX)) {
22511672Sjkh	dialog_clear();
22611499Sjkh	msgConfirm("getcwd");
22711499Sjkh	return NULL;
22811499Sjkh    }
22911672Sjkh    if (chdir(pen) == RET_FAIL) {
23011672Sjkh	dialog_clear();
23111499Sjkh	msgConfirm("Can't chdir to '%s'.", pen);
23211672Sjkh    }
23311499Sjkh    return Previous;
23411499Sjkh}
235