summaryrefslogtreecommitdiffstats
path: root/usr.sbin/sysinstall/package.c
blob: 5da13520915134f3b79ad6d565e8551291ccb170 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
 * The new sysinstall program.
 *
 * This is probably the last program in the `sysinstall' line - the next
 * generation being essentially a complete rewrite.
 *
 * $Id: package.c,v 1.21 1995/10/27 02:12:58 jkh Exp $
 *
 * Copyright (c) 1995
 *	Jordan Hubbard.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer,
 *    verbatim and that no modifications are made prior to this
 *    point in the file.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by Jordan Hubbard
 *	for the FreeBSD Project.
 * 4. The name of Jordan Hubbard or the FreeBSD project may not be used to
 *    endorse or promote products derived from this software without specific
 *    prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/errno.h>
#include <sys/param.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include "sysinstall.h"

static char *make_playpen(char *pen, size_t sz);

/* Like package_extract, but assumes current media device */
int
package_add(char *name)
{
    if (!mediaVerify())
	return RET_FAIL;
    return package_extract(mediaDevice, name);
}

/* Extract a package based on a namespec and a media device */
int
package_extract(Device *dev, char *name)
{
    char path[511];
    char pen[FILENAME_MAX];
    char *where;
    int fd, ret;

    /* Check to make sure it's not already there */
    if (!vsystem("pkg_info -e %s", name)) {
	msgDebug("package %s marked as already installed - return SUCCESS.\n", name);
	return RET_SUCCESS;
    }

    if (!dev->init(dev)) {
	dialog_clear();
	msgConfirm("Unable to initialize media type for package extract.");
	return RET_FAIL;
    }

    ret = RET_FAIL;
    sprintf(path, "packages/All/%s%s", name, strstr(name, ".tgz") ? "" : ".tgz");
    msgNotify("Adding %s\nfrom %s", path, dev->name);
    fd = dev->get(dev, path, TRUE);
    if (fd >= 0) {
	pen[0] = '\0';
	if ((where = make_playpen(pen, 0)) != NULL) {
	    if (mediaExtractDist(pen, fd)) {
		if (file_readable("+CONTENTS")) {
		    if (vsystem("(pwd; cat +CONTENTS) | pkg_add %s-S",
				!strcmp(variable_get(VAR_CPIO_VERBOSITY), "high") ? "-v " : "")) {
			dialog_clear();
			msgConfirm("An error occurred while trying to pkg_add %s.\n"
				   "Please check debugging screen for possible further details.", name);
		    }
		    else {
			msgNotify("Package %s added successfully!", name);
			ret = RET_SUCCESS;
		    }
		}
		else {
		    dialog_clear();
		    msgConfirm("The package specified (%s) has no CONTENTS file.  This means\n"
			       "that there was either a media error of some sort or the package\n"
			       "file itself is corrupted.\n"
			       "You may wish to look into this and try again.", name);
		}
	    }
	    else {
		ret = RET_FAIL;
		msgConfirm("Unable to extract the contents of package %s.  This means\n"
			   "that there was either a media error of some sort or the package\n"
			   "file itself is corrupted.\n"
			   "You may wish to look into this and try again.", name);
	    }
	    if (chdir(where) == -1)
		msgFatal("Unable to get back to where I was before, Jojo! (That was: %s)", where);
	    vsystem("rm -rf %s", pen);
	    if (isDebug())
		msgDebug("Nuked pen: %s\n", pen);
	}
	else {
	    dialog_clear();
	    msgConfirm("Unable to find a temporary location to unpack this stuff in.\n"
		       "You must simply not have enough space or you've configured your\n"
		       "system oddly.  Sorry!");
	    ret = RET_FAIL;
	}
	dev->close(dev, fd);
	if (dev->type == DEVICE_TYPE_TAPE)
	    unlink(path);
    }
    else {
	msgDebug("pkg_extract: get operation returned %d\n", fd);
	if (variable_get(VAR_NO_CONFIRM))
	    msgNotify("Unable to fetch package %s from selected media.\n"
		      "No package add will be done.", name);
	else {
	    dialog_clear();
	    msgConfirm("Unable to fetch package %s from selected media.\n"
		       "No package add will be done.", name);
	}
    }
    return ret;
}

static size_t
min_free(char *tmpdir)
{
    struct statfs buf;

    if (statfs(tmpdir, &buf) != 0) {
	msgDebug("Error in statfs, errno = %d\n", errno);
	return -1;
    }
    return buf.f_bavail * buf.f_bsize;
}

/* Find a good place to play. */
static char *
find_play_pen(char *pen, size_t sz)
{
    struct stat sb;

    if (pen[0] && stat(pen, &sb) != RET_FAIL && (min_free(pen) >= sz))
	return pen;
    else if (stat("/var/tmp", &sb) != RET_FAIL && min_free("/var/tmp") >= sz)
	strcpy(pen, "/var/tmp/instmp.XXXXXX");
    else if (stat("/tmp", &sb) != RET_FAIL && min_free("/tmp") >= sz)
	strcpy(pen, "/tmp/instmp.XXXXXX");
    else if ((stat("/usr/tmp", &sb) == RET_SUCCESS || mkdir("/usr/tmp", 01777) == RET_SUCCESS) &&
	     min_free("/usr/tmp") >= sz)
	strcpy(pen, "/usr/tmp/instmp.XXXXXX");
    else {
	dialog_clear();
	msgConfirm("Can't find enough temporary space to extract the files, please try\n"
		   "This again after your system is up (you can run /stand/sysinstall\n"
		   "directly) and you've had a chance to point /var/tmp somewhere with\n"
		   "sufficient temporary space available.");
	return NULL;
    }
    return pen;
}

/*
 * Make a temporary directory to play in and chdir() to it, returning
 * pathname of previous working directory.
 */
static char *
make_playpen(char *pen, size_t sz)
{
    static char Previous[FILENAME_MAX];

    if (!find_play_pen(pen, sz))
	return NULL;

    if (!mktemp(pen)) {
	dialog_clear();
	msgConfirm("Can't mktemp '%s'.", pen);
	return NULL;
    }
    if (mkdir(pen, 0755) == RET_FAIL) {
	dialog_clear();
	msgConfirm("Can't mkdir '%s'.", pen);
	return NULL;
    }
    if (isDebug()) {
	if (sz)
	    msgDebug("Requested space: %d bytes, free space: %d bytes in %s\n", (int)sz, min_free(pen), pen);
    }
    if (min_free(pen) < sz) {
	rmdir(pen);
	dialog_clear();
	msgConfirm("Not enough free space to create: `%s'\n"
		   "Please try this again after your system is up (you can run\n"
		   "/stand/sysinstall directly) and you've had a chance to point\n"
		   "/var/tmp somewhere with sufficient temporary space available.");
        return NULL;
    }
    if (!getcwd(Previous, FILENAME_MAX)) {
	dialog_clear();
	msgConfirm("getcwd");
	return NULL;
    }
    if (chdir(pen) == RET_FAIL) {
	dialog_clear();
	msgConfirm("Can't chdir to '%s'.", pen);
    }
    return Previous;
}
OpenPOWER on IntegriCloud