package.c revision 11650
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 *
711650Sjkh * $Id: package.c,v 1.9 1995/10/22 01:32:58 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 */
7411599Sjkh    if (!vsystem("pkg_info -e %s", name))
7511536Sjkh	return RET_SUCCESS;
7611536Sjkh
7711553Sjkh    if (!dev->init(dev)) {
7811553Sjkh	msgConfirm("Unable to initialize media type for package add.");
7911499Sjkh	return RET_FAIL;
8011553Sjkh    }
8111499Sjkh
8211499Sjkh    ret = RET_FAIL;
8311553Sjkh    sprintf(path, "packages/All/%s%s", name, strstr(name, ".tgz") ? "" : ".tgz");
8411553Sjkh    msgDebug("pkg_extract: Attempting to fetch %s\n", path);
8511553Sjkh    fd = dev->get(dev, path, TRUE);
8611499Sjkh    if (fd >= 0) {
8711499Sjkh	pid_t tpid;
8811499Sjkh
8911567Sjkh	msgNotify("Fetching %s from %s", path, dev->name);
9011499Sjkh	pen[0] = '\0';
9111499Sjkh	if ((where = make_playpen(pen, 0)) != NULL) {
9211499Sjkh	    if (isDebug())
9311499Sjkh		msgDebug("Working in temporary directory %s, will return to %s\n", pen, where);
9411499Sjkh	    tpid = fork();
9511499Sjkh	    if (!tpid) {
9611499Sjkh		dup2(fd, 0);
9711640Sjkh		i = vsystem("tar %s-xzf -", !strcmp(variable_get(VAR_CPIO_VERBOSITY), "high") ? "-v " : "");
9811499Sjkh		if (isDebug())
9911499Sjkh		    msgDebug("tar command returns %d status\n", i);
10011499Sjkh		exit(i);
10111499Sjkh	    }
10211499Sjkh	    else {
10311499Sjkh		int pstat;
10411499Sjkh
10511499Sjkh		tpid = waitpid(tpid, &pstat, 0);
10611499Sjkh		if (vsystem("(pwd; cat +CONTENTS) | pkg_add %s-S",
10711640Sjkh			    !strcmp(variable_get(VAR_CPIO_VERBOSITY), "high") ? "-v " : ""))
10811499Sjkh		    msgConfirm("An error occurred while trying to pkg_add %s.\n"
10911499Sjkh			       "Please check debugging screen for possible further details.", path);
11011499Sjkh		else
11111499Sjkh		    ret = RET_SUCCESS;
11211567Sjkh		close(fd);
11311499Sjkh	    }
11411499Sjkh	    if (chdir(where) == -1)
11511627Sjkh		msgFatal("Unable to get back to where I was before, Jojo! (That was: %s)", where);
11611499Sjkh	    vsystem("rm -rf %s", pen);
11711499Sjkh	    if (isDebug())
11811499Sjkh		msgDebug("Nuked pen: %s\n", pen);
11911499Sjkh	}
12011499Sjkh	else
12111499Sjkh	    msgConfirm("Unable to find a temporary location to unpack this stuff in.\n"
12211499Sjkh		       "You must simply not have enough space or you've configured your\n"
12311499Sjkh		       "system oddly.  Sorry!");
12411499Sjkh	dev->close(dev, fd);
12511499Sjkh	if (dev->type == DEVICE_TYPE_TAPE)
12611499Sjkh	    unlink(path);
12711499Sjkh    }
12811553Sjkh    else
12911553Sjkh	msgDebug("pkg_extract: get operation returned %d\n", fd);
13011499Sjkh    return ret;
13111499Sjkh}
13211499Sjkh
13311499Sjkhstatic size_t
13411499Sjkhmin_free(char *tmpdir)
13511499Sjkh{
13611499Sjkh    struct statfs buf;
13711499Sjkh
13811499Sjkh    if (statfs(tmpdir, &buf) != 0) {
13911499Sjkh	msgDebug("Error in statfs, errno = %d\n", errno);
14011499Sjkh	return -1;
14111499Sjkh    }
14211499Sjkh    return buf.f_bavail * buf.f_bsize;
14311499Sjkh}
14411499Sjkh
14511499Sjkh/* Find a good place to play. */
14611499Sjkhstatic char *
14711499Sjkhfind_play_pen(char *pen, size_t sz)
14811499Sjkh{
14911499Sjkh    struct stat sb;
15011499Sjkh
15111499Sjkh    if (pen[0] && stat(pen, &sb) != RET_FAIL && (min_free(pen) >= sz))
15211499Sjkh	return pen;
15311499Sjkh    else if (stat("/var/tmp", &sb) != RET_FAIL && min_free("/var/tmp") >= sz)
15411499Sjkh	strcpy(pen, "/var/tmp/instmp.XXXXXX");
15511499Sjkh    else if (stat("/tmp", &sb) != RET_FAIL && min_free("/tmp") >= sz)
15611499Sjkh	strcpy(pen, "/tmp/instmp.XXXXXX");
15711499Sjkh    else if ((stat("/usr/tmp", &sb) == RET_SUCCESS || mkdir("/usr/tmp", 01777) == RET_SUCCESS) &&
15811499Sjkh	     min_free("/usr/tmp") >= sz)
15911499Sjkh	strcpy(pen, "/usr/tmp/instmp.XXXXXX");
16011499Sjkh    else {
16111499Sjkh	msgConfirm("Can't find enough temporary space to extract the files, please try\n"
16211499Sjkh		   "This again after your system is up (you can run /stand/sysinstall\n"
16311499Sjkh		   "directly) and you've had a chance to point /var/tmp somewhere with\n"
16411499Sjkh		   "sufficient temporary space available.");
16511499Sjkh	return NULL;
16611499Sjkh    }
16711499Sjkh    return pen;
16811499Sjkh}
16911499Sjkh
17011499Sjkh/*
17111499Sjkh * Make a temporary directory to play in and chdir() to it, returning
17211499Sjkh * pathname of previous working directory.
17311499Sjkh */
17411499Sjkhstatic char *
17511499Sjkhmake_playpen(char *pen, size_t sz)
17611499Sjkh{
17711499Sjkh    static char Previous[FILENAME_MAX];
17811499Sjkh
17911499Sjkh    if (!find_play_pen(pen, sz))
18011499Sjkh	return NULL;
18111499Sjkh
18211499Sjkh    if (!mktemp(pen)) {
18311499Sjkh	msgConfirm("Can't mktemp '%s'.", pen);
18411499Sjkh	return NULL;
18511499Sjkh    }
18611499Sjkh    if (mkdir(pen, 0755) == RET_FAIL) {
18711499Sjkh	msgConfirm("Can't mkdir '%s'.", pen);
18811499Sjkh	return NULL;
18911499Sjkh    }
19011499Sjkh    if (isDebug()) {
19111499Sjkh	if (sz)
19211499Sjkh	    msgDebug("Requested space: %d bytes, free space: %d bytes in %s\n", (int)sz, min_free(pen), pen);
19311499Sjkh    }
19411499Sjkh    if (min_free(pen) < sz) {
19511499Sjkh	rmdir(pen);
19611499Sjkh	msgConfirm("Not enough free space to create: `%s'\n"
19711499Sjkh		   "Please try this again after your system is up (you can run\n"
19811499Sjkh		   "/stand/sysinstall directly) and you've had a chance to point\n"
19911499Sjkh		   "/var/tmp somewhere with sufficient temporary space available.");
20011499Sjkh        return NULL;
20111499Sjkh    }
20211499Sjkh    if (!getcwd(Previous, FILENAME_MAX)) {
20311499Sjkh	msgConfirm("getcwd");
20411499Sjkh	return NULL;
20511499Sjkh    }
20611499Sjkh    if (chdir(pen) == RET_FAIL)
20711499Sjkh	msgConfirm("Can't chdir to '%s'.", pen);
20811499Sjkh    return Previous;
20911499Sjkh}
210