summaryrefslogtreecommitdiffstats
path: root/mdk-stage1/directory.c
blob: e322c8379705c132e7b3e12f924e4a3c738d856b (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
/*
 * Guillaume Cottenceau (gc@mandrakesoft.com)
 * Olivier Blin (oblin@mandrakesoft.com)
 *
 * Copyright 2000 MandrakeSoft
 *
 * This software may be freely redistributed under the terms of the GNU
 * public license.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

/*
 * Portions from Erik Troan (ewt@redhat.com)
 *
 * Copyright 1996 Red Hat Software 
 *
 */

#include <unistd.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <string.h>
#include <libgen.h>
#include "stage1.h"
#include "frontend.h"
#include "log.h"
#include "lomount.h"

char * extract_list_directory(char * direct)
{
	char ** full = list_directory(direct);
	char tmp[2000] = "";
	int i;
	for (i=0; i<5 ; i++) {
		if (!full || !*full)
			break;
		strcat(tmp, *full);
		strcat(tmp, "\n");
		full++;
	}
	return strdup(tmp);
}

enum return_type try_with_directory(char *directory, char *method_live, char *method_iso) {
	char location_full[500];
        char * loopdev = NULL;
	struct stat statbuf;
	enum return_type results;

	unlink(IMAGE_LOCATION);
	strcpy(location_full, directory);

#ifndef MANDRAKE_MOVE
	if (!stat(directory, &statbuf) && S_ISDIR(statbuf.st_mode)) {
		char **file;
		char *stage2_isos[100] = { "Use directory as a mirror tree", "-----" };
		int stage2_iso_number = 2;

		log_message("\"%s\" exists and is a directory, looking for iso files", directory);

		for (file = list_directory(directory); *file; file++) {
			char isofile[500], install_location[600];

			if (strstr(*file, ".iso") != *file + strlen(*file) - 4)
				/* file doesn't end in .iso, skipping */
				continue;
			
			strcpy(isofile, directory);
			strcat(isofile, "/");
			strcat(isofile, *file);

			if (lomount(isofile, IMAGE_LOCATION, &loopdev, 0)) {
				log_message("unable to mount iso file \"%s\", skipping", isofile);
				continue;
			}

			strcpy(install_location, IMAGE_LOCATION);

			if (IS_SPECIAL_STAGE2 || ramdisk_possible())
				strcat(install_location, get_ramdisk_realname()); /* RAMDISK install */
			else
				strcat(install_location, LIVE_LOCATION); /* LIVE install */

			if (access(install_location, R_OK)) {
				log_message("ISO image \"%s\" doesn't contain stage2 installer", isofile);
			} else {
				log_message("stage2 installer found in ISO image \"%s\"", isofile);
				stage2_isos[stage2_iso_number++] = strdup(*file);
			}

			umount(IMAGE_LOCATION);
			del_loop(loopdev);
		}

		stage2_isos[stage2_iso_number] = NULL;

		if (stage2_iso_number > 2) {
			do {
				results = ask_from_list("Please choose the ISO image to be used to install the "
							DISTRIB_NAME " Distribution.",
							stage2_isos, file);
				if (results == RETURN_BACK) {
					return RETURN_BACK;
				} else if (results == RETURN_OK) {
					if (!strcmp(*file, stage2_isos[0])) {
						/* use directory as a mirror tree */
						continue;
					} else if (!strcmp(*file, stage2_isos[1])) {
						/* the separator has been selected */
						results = RETURN_ERROR;
						continue;
					} else {
						/* use selected ISO image */
						strcat(location_full, "/");
						strcat(location_full, *file);
						log_message("installer will use ISO image \"%s\"", location_full);
					}
				}
			} while (results == RETURN_ERROR);
		} else {
			log_message("no ISO image found in \"%s\" directory", location_full);
		}
	}
#endif

	loopdev = NULL;
	if (!stat(location_full, &statbuf) && !S_ISDIR(statbuf.st_mode)) {
		log_message("%s exists and is not a directory, assuming this is an ISO image", location_full);
		if (lomount(location_full, IMAGE_LOCATION, &loopdev, 0)) {
			stg1_error_message("Could not mount file %s as an ISO image of the " DISTRIB_NAME " Distribution.", location_full);
			return RETURN_ERROR;
		}
		add_to_env("ISOPATH", location_full);
		add_to_env("METHOD", method_iso);
	} else {
		int offset = strncmp(location_full, IMAGE_LOCATION_DIR, sizeof(IMAGE_LOCATION_DIR) - 1) == 0 ? sizeof(IMAGE_LOCATION_DIR) - 1 : 0;
		log_message("assuming %s is a mirror tree", location_full + offset);
		symlink(location_full + offset, IMAGE_LOCATION);
		add_to_env("METHOD", method_live);
	}
#ifndef MANDRAKE_MOVE
	if (IS_SPECIAL_STAGE2 || (loopdev && ramdisk_possible())) {
		/* RAMDISK install */
		if (access(IMAGE_LOCATION RAMDISK_LOCATION, R_OK)) {
			stg1_error_message("I can't find the " DISTRIB_NAME " Distribution in the specified directory. "
				      "(I need the subdirectory " RAMDISK_LOCATION ")\n"
				      "Here's a short extract of the files in the directory:\n"
				      "%s", extract_list_directory(IMAGE_LOCATION));
			umount(IMAGE_LOCATION);
			del_loop(loopdev);
			return RETURN_ERROR;
		}
		if (load_ramdisk() != RETURN_OK) {
			stg1_error_message("Could not load program into memory.");
			umount(IMAGE_LOCATION);
			del_loop(loopdev);
			return RETURN_ERROR;
		}
	} else {
#endif
		/* LIVE install */
#ifdef MANDRAKE_MOVE
		if (access(IMAGE_LOCATION "/live_tree/etc/fstab", R_OK) && access(IMAGE_LOCATION "/live_tree.clp", R_OK)) {
#else
		char p;
		if (access(IMAGE_LOCATION LIVE_LOCATION, R_OK)) {
#endif
			stg1_error_message("I can't find the " DISTRIB_NAME " Distribution in the specified directory. "
				      "(I need the subdirectory " LIVE_LOCATION ")\n"
				      "Here's a short extract of the files in the directory:\n"
				      "%s", extract_list_directory(IMAGE_LOCATION));
			umount(IMAGE_LOCATION);
			del_loop(loopdev);
			return RETURN_ERROR;
		}
#ifndef MANDRAKE_MOVE
		if (readlink(IMAGE_LOCATION LIVE_LOCATION "/usr/bin/runinstall2", &p, 1) != 1) {
			stg1_error_message("The " DISTRIB_NAME " Distribution seems to be copied on a Windows partition. "
				      "You need more memory to perform an installation from a Windows partition. "
				      "Another solution if to copy the " DISTRIB_NAME " Distribution on a Linux partition.");
			umount(IMAGE_LOCATION);
			del_loop(loopdev);
			return RETURN_ERROR;
		}
		log_message("found the " DISTRIB_NAME " Installation, good news!");
	}
#endif

	if (IS_RESCUE) {
		umount(IMAGE_LOCATION);
		del_loop(loopdev);
	}

	return RETURN_OK;
}