summaryrefslogtreecommitdiffstats
path: root/release/sysinstall/package.c
blob: 082cd4ea1c4e148f9b969edf3d4cad65c155a60e (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
/*
 * The new sysinstall program.
 *
 * This is probably the last program in the `sysinstall' line - the next
 * generation being essentially a complete rewrite.
 *
 * $Id: install.c,v 1.71.2.31 1995/10/15 04:37:05 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);

/* 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 i, fd, ret;

    if (!dev->init(dev))
	return RET_FAIL;

    ret = RET_FAIL;
    sprintf(path, "packages/%s%s", name, strstr(name, ".tgz") ? "" : ".tgz");
    fd = dev->get(dev, path, NULL);
    if (fd >= 0) {
	pid_t tpid;

	if (isDebug())
	    msgDebug("Got target %s from media type %d\n", path, dev->type);
	pen[0] = '\0';
	if ((where = make_playpen(pen, 0)) != NULL) {
	    if (isDebug())
		msgDebug("Working in temporary directory %s, will return to %s\n", pen, where);
	    tpid = fork();
	    if (!tpid) {
		dup2(fd, 0);
		i = vsystem("tar %s-xzf -", !strcmp(variable_get(CPIO_VERBOSITY_LEVEL), "high") ? "-v " : "");
		if (isDebug())
		    msgDebug("tar command returns %d status\n", i);
		exit(i);
	    }
	    else {
		int pstat;
		
		close(fd);
		tpid = waitpid(tpid, &pstat, 0);
		if (vsystem("(pwd; cat +CONTENTS) | pkg_add %s-S",
			    variable_get(CPIO_VERBOSITY_LEVEL), "high") ? "-v " : "")
		    msgConfirm("An error occurred while trying to pkg_add %s.\n"
			       "Please check debugging screen for possible further details.", path);
		else
		    ret = RET_SUCCESS;
	    }
	    if (chdir(where) == -1)
		msgFatal("Unable to get back to where I was before, Jojo!\n(That was: %s)\n", where);
	    vsystem("rm -rf %s", pen);
	    if (isDebug())
		msgDebug("Nuked pen: %s\n", pen);
	}
	else
	    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!");
	dev->close(dev, fd);
	if (dev->type == DEVICE_TYPE_TAPE)
	    unlink(path);
    }
    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 {
	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)) {
	msgConfirm("Can't mktemp '%s'.", pen);
	return NULL;
    }
    if (mkdir(pen, 0755) == RET_FAIL) {
	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);
	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)) {
	msgConfirm("getcwd");
	return NULL;
    }
    if (chdir(pen) == RET_FAIL)
	msgConfirm("Can't chdir to '%s'.", pen);
    return Previous;
}
OpenPOWER on IntegriCloud