package.c revision 15470
1/*
2 * The new sysinstall program.
3 *
4 * This is probably the last program in the `sysinstall' line - the next
5 * generation being essentially a complete rewrite.
6 *
7 * $Id: package.c,v 1.34 1996/04/30 05:40:15 jkh Exp $
8 *
9 * Copyright (c) 1995
10 *	Jordan Hubbard.  All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 *    notice, this list of conditions and the following disclaimer,
17 *    verbatim and that no modifications are made prior to this
18 *    point in the file.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 *    notice, this list of conditions and the following disclaimer in the
21 *    documentation and/or other materials provided with the distribution.
22 *
23 * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 */
36
37#include <stdio.h>
38#include <string.h>
39#include <stdlib.h>
40#include <sys/errno.h>
41#include <sys/param.h>
42#include <sys/mount.h>
43#include <sys/stat.h>
44#include "sysinstall.h"
45
46/* Like package_extract, but assumes current media device */
47int
48package_add(char *name)
49{
50    if (!mediaVerify())
51	return DITEM_FAILURE;
52    return package_extract(mediaDevice, name, FALSE);
53}
54
55/* Extract a package based on a namespec and a media device */
56int
57package_extract(Device *dev, char *name, Boolean depended)
58{
59    char path[511];
60    int fd, ret;
61
62    /* If necessary, initialize the ldconfig hints */
63    if (!file_readable("/var/run/ld.so.hints"))
64	vsystem("ldconfig /usr/lib /usr/local/lib /usr/X11R6/lib");
65
66    msgNotify("Checking for existence of %s package", name);
67    /* Check to make sure it's not already there */
68    if (!vsystem("pkg_info -e %s", name)) {
69	msgDebug("package %s marked as already installed - return SUCCESS.\n", name);
70	return DITEM_SUCCESS;
71    }
72
73    if (!dev->init(dev)) {
74	msgConfirm("Unable to initialize media type for package extract.");
75	return DITEM_FAILURE;
76    }
77
78    /* Be initially optimistic */
79    ret = DITEM_SUCCESS | DITEM_RESTORE;
80    /* Make a couple of paranoid locations for temp files to live if user specified none */
81    if (!variable_get("PKG_TMPDIR")) {
82	Mkdir("/usr/tmp", NULL);
83	Mkdir("/var/tmp", NULL);
84	/* Set it to a location with as much space as possible */
85	variable_set2("PKG_TMPDIR", "/usr/tmp");
86    }
87
88    sprintf(path, "packages/All/%s%s", name, strstr(name, ".tgz") ? "" : ".tgz");
89    fd = dev->get(dev, path, TRUE);
90    if (fd >= 0) {
91	int i, tot, pfd[2];
92	pid_t pid;
93
94	msgNotify("Adding %s%s\nfrom %s", path, depended ? " (as a dependency)" : "", dev->name);
95	pipe(pfd);
96	pid = fork();
97	if (!pid) {
98	    dup2(pfd[0], 0); close(pfd[0]);
99	    dup2(DebugFD, 1);
100	    close(2);
101	    close(pfd[1]);
102	    i = execl("/usr/sbin/pkg_add", "/usr/sbin/pkg_add", "-", 0);
103	    if (isDebug())
104		msgDebug("pkg_add returns %d status\n", i);
105	}
106	else {
107	    char buf[BUFSIZ];
108	    WINDOW *w = savescr();
109
110	    close(pfd[0]);
111	    tot = 0;
112	    while ((i = read(fd, buf, BUFSIZ)) > 0) {
113		char line[80];
114		int x, len;
115
116		write(pfd[1], buf, i);
117		tot += i;
118		sprintf(line, "%d bytes read from package %s", tot, name);
119		len = strlen(line);
120		for (x = len; x < 79; x++)
121		    line[x] = ' ';
122		line[79] = '\0';
123		mvprintw(0, 0, line);
124		clrtoeol();
125		refresh();
126	    }
127	    close(pfd[1]);
128	    dev->close(dev, fd);
129	    mvprintw(0, 0, "Package %s read successfully - waiting for pkg_add", name);
130	    refresh();
131	    i = waitpid(pid, &tot, 0);
132	    if (i < 0 || WEXITSTATUS(tot)) {
133		msgNotify("Add of package %s aborted due to some error -\n"
134			  "Please check the debug screen for more info.");
135	    }
136	    else
137		msgNotify("Package %s was added successfully", name);
138	    sleep(1);
139	    restorescr(w);
140	}
141    }
142    else {
143	msgDebug("pkg_extract: get operation returned %d\n", fd);
144	if (variable_get(VAR_NO_CONFIRM))
145	    msgNotify("Unable to fetch package %s from selected media.\n"
146		      "No package add will be done.", name);
147	else {
148	    msgConfirm("Unable to fetch package %s from selected media.\n"
149		       "No package add will be done.", name);
150	}
151	ret = DITEM_FAILURE | DITEM_RESTORE;
152    }
153    return ret;
154}
155