package.c revision 14738
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 *
714738Sjkh * $Id: package.c,v 1.29 1996/03/18 15:28:05 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
5311650Sjkh/* Like package_extract, but assumes current media device */
5411650Sjkhint
5511650Sjkhpackage_add(char *name)
5611650Sjkh{
5711650Sjkh    if (!mediaVerify())
5811650Sjkh	return RET_FAIL;
5914738Sjkh    return package_extract(mediaDevice, name, FALSE);
6011650Sjkh}
6111650Sjkh
6211499Sjkh/* Extract a package based on a namespec and a media device */
6311499Sjkhint
6414738Sjkhpackage_extract(Device *dev, char *name, Boolean depended)
6511499Sjkh{
6611499Sjkh    char path[511];
6711718Sjkh    int fd, ret;
6811499Sjkh
6912232Sjkh    /* If necessary, initialize the ldconfig hints */
7012232Sjkh    if (!file_readable("/var/run/ld.so.hints"))
7112232Sjkh	vsystem("ldconfig /usr/lib /usr/local/lib /usr/X11R6/lib");
7212232Sjkh
7312612Sjkh    msgNotify("Checking for existence of %s package", name);
7411536Sjkh    /* Check to make sure it's not already there */
7511672Sjkh    if (!vsystem("pkg_info -e %s", name)) {
7611834Sjkh	msgDebug("package %s marked as already installed - return SUCCESS.\n", name);
7711536Sjkh	return RET_SUCCESS;
7811672Sjkh    }
7911536Sjkh
8011553Sjkh    if (!dev->init(dev)) {
8111672Sjkh	dialog_clear();
8211672Sjkh	msgConfirm("Unable to initialize media type for package extract.");
8311499Sjkh	return RET_FAIL;
8411553Sjkh    }
8511499Sjkh
8614738Sjkh    /* Be initially optimistic */
8714738Sjkh    ret = RET_SUCCESS;
8812129Sjkh    /* Make a couple of paranoid locations for temp files to live if user specified none */
8912129Sjkh    if (!variable_get("PKG_TMPDIR")) {
9012129Sjkh	Mkdir("/usr/tmp", NULL);
9112129Sjkh	Mkdir("/var/tmp", NULL);
9214670Sjkh	/* Set it to a location with as much space as possible */
9314670Sjkh	variable_set2("PKG_TMPDIR", "/usr/tmp");
9412129Sjkh    }
9512129Sjkh
9611553Sjkh    sprintf(path, "packages/All/%s%s", name, strstr(name, ".tgz") ? "" : ".tgz");
9711553Sjkh    fd = dev->get(dev, path, TRUE);
9811499Sjkh    if (fd >= 0) {
9914670Sjkh	int i, tot, pfd[2];
10014670Sjkh	pid_t pid;
10112184Sjkh
10214738Sjkh	dialog_clear();
10314738Sjkh	msgNotify("Adding %s%s\nfrom %s", path, depended ? " (as a dependency)" : "", dev->name);
10414670Sjkh	pipe(pfd);
10514670Sjkh	pid = fork();
10614670Sjkh	if (!pid) {
10714670Sjkh	    dup2(pfd[0], 0); close(pfd[0]);
10814670Sjkh	    dup2(DebugFD, 1);
10914670Sjkh	    dup2(DebugFD, 2);
11014670Sjkh	    close(pfd[1]);
11114670Sjkh	    i = execl("/usr/sbin/pkg_add", "/usr/sbin/pkg_add", "-", 0);
11211499Sjkh	    if (isDebug())
11314670Sjkh		msgDebug("pkg_add returns %d status\n", i);
11411499Sjkh	}
11511672Sjkh	else {
11614670Sjkh	    char buf[BUFSIZ];
11714670Sjkh
11814670Sjkh	    tot = 0;
11914670Sjkh	    while ((i = read(fd, buf, BUFSIZ)) > 0) {
12014670Sjkh		write(pfd[1], buf, i);
12114670Sjkh		tot += i;
12214670Sjkh		mvprintw(0, 0, "%d bytes read from package %s", tot, name);
12314670Sjkh		clrtoeol();
12414670Sjkh		refresh();
12514670Sjkh	    }
12614670Sjkh	    close(fd);
12714670Sjkh	    close(pfd[1]);
12814738Sjkh	    mvprintw(0, 0, "Package %s read successfully - waiting for pkg_add", name);
12914738Sjkh	    refresh();
13014670Sjkh	    i = waitpid(pid, &tot, 0);
13114670Sjkh	    if (i < 0 || WEXITSTATUS(tot)) {
13214670Sjkh		msgNotify("Add of package %s aborted due to some error -\n"
13314670Sjkh			  "Please check the debug screen for more info.");
13414670Sjkh	    }
13514670Sjkh	    else
13614670Sjkh		msgNotify("Package %s was added successfully", name);
13711672Sjkh	}
13811499Sjkh    }
13911672Sjkh    else {
14011553Sjkh	msgDebug("pkg_extract: get operation returned %d\n", fd);
14111672Sjkh	if (variable_get(VAR_NO_CONFIRM))
14211672Sjkh	    msgNotify("Unable to fetch package %s from selected media.\n"
14311799Sjkh		      "No package add will be done.", name);
14411672Sjkh	else {
14511672Sjkh	    dialog_clear();
14611672Sjkh	    msgConfirm("Unable to fetch package %s from selected media.\n"
14711799Sjkh		       "No package add will be done.", name);
14811672Sjkh	}
14914738Sjkh	ret = RET_FAIL;
15011672Sjkh    }
15111499Sjkh    return ret;
15211499Sjkh}
153