xref: /freebsd/usr.sbin/bsdinstall/distfetch/distfetch.c (revision af23369a6deaaeb612ab266eb88b8bb8d560c322)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2011 Nathan Whitehorn
5  * Copyright (c) 2014 Devin Teske <dteske@FreeBSD.org>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 
35 #include <bsddialog.h>
36 #include <ctype.h>
37 #include <err.h>
38 #include <errno.h>
39 #include <stdio.h>
40 #include <fetch.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44 
45 #include "opt_osname.h"
46 
47 static int fetch_files(int nfiles, char **urls);
48 
49 int
50 main(void)
51 {
52 	char *diststring;
53 	char **urls;
54 	int i;
55 	int ndists = 0;
56 	int nfetched;
57 	char error[PATH_MAX + 512];
58 	struct bsddialog_conf conf;
59 
60 	if (getenv("DISTRIBUTIONS") == NULL)
61 		errx(EXIT_FAILURE, "DISTRIBUTIONS variable is not set");
62 
63 	diststring = strdup(getenv("DISTRIBUTIONS"));
64 	for (i = 0; diststring[i] != 0; i++)
65 		if (isspace(diststring[i]) && !isspace(diststring[i+1]))
66 			ndists++;
67 	ndists++; /* Last one */
68 
69 	urls = calloc(ndists, sizeof(const char *));
70 	if (urls == NULL) {
71 		free(diststring);
72 		errx(EXIT_FAILURE, "Error: distfetch URLs out of memory!");
73 	}
74 
75 	if (bsddialog_init() == BSDDIALOG_ERROR) {
76 		free(diststring);
77 		errx(EXIT_FAILURE, "Error libbsddialog: %s\n",
78 		    bsddialog_geterror());
79 	}
80 	bsddialog_initconf(&conf);
81 	bsddialog_backtitle(&conf, OSNAME " Installer");
82 
83 	for (i = 0; i < ndists; i++) {
84 		urls[i] = malloc(PATH_MAX);
85 		snprintf(urls[i], PATH_MAX, "%s/%s",
86 		    getenv("BSDINSTALL_DISTSITE"), strsep(&diststring, " \t"));
87 	}
88 
89 	if (chdir(getenv("BSDINSTALL_DISTDIR")) != 0) {
90 		snprintf(error, sizeof(error),
91 		    "Could not change to directory %s: %s\n",
92 		    getenv("BSDINSTALL_DISTDIR"), strerror(errno));
93 		conf.title = "Error";
94 		bsddialog_msgbox(&conf, error, 0, 0);
95 		bsddialog_end();
96 		return (EXIT_FAILURE);
97 	}
98 
99 	nfetched = fetch_files(ndists, urls);
100 
101 	bsddialog_end();
102 
103 	free(diststring);
104 	for (i = 0; i < ndists; i++)
105 		free(urls[i]);
106 	free(urls);
107 
108 	return ((nfetched == ndists) ? EXIT_SUCCESS : EXIT_FAILURE);
109 }
110 
111 static int
112 fetch_files(int nfiles, char **urls)
113 {
114 	FILE *fetch_out;
115 	FILE *file_out;
116 	const char **minilabel;
117 	int *miniperc;
118 	int perc;
119 	int i;
120 	int last_progress;
121 	int nsuccess = 0; /* Number of files successfully downloaded */
122 	int progress = 0;
123 	size_t chunk;
124 	off_t current_bytes;
125 	off_t fsize;
126 	off_t total_bytes;
127 	float file_perc;
128 	float mainperc_file;
129 	struct url_stat ustat;
130 	char errormsg[PATH_MAX + 512];
131 	uint8_t block[4096];
132 	struct bsddialog_conf errconf;
133 	struct bsddialog_conf mgconf;
134 
135 	/* Make the transfer list for mixedgauge */
136 	minilabel = calloc(sizeof(char *), nfiles);
137 	miniperc = calloc(sizeof(int), nfiles);
138 	if (minilabel == NULL || miniperc == NULL)
139 		errx(EXIT_FAILURE, "Error: distfetch minibars out of memory!");
140 
141 	for (i = 0; i < nfiles; i++) {
142 		minilabel[i] = strrchr(urls[i], '/');
143 		if (minilabel[i] != NULL)
144 			minilabel[i]++;
145 		else
146 			minilabel[i] = urls[i];
147 		miniperc[i] = BSDDIALOG_MG_PENDING;
148 	}
149 
150 	bsddialog_initconf(&errconf);
151 	bsddialog_infobox(&errconf, "Connecting to server.\nPlease wait...",
152 	    0, 0);
153 
154 	/* Try to stat all the files */
155 	total_bytes = 0;
156 	for (i = 0; i < nfiles; i++) {
157 		if (fetchStatURL(urls[i], &ustat, "") == 0 && ustat.size > 0) {
158 			total_bytes += ustat.size;
159 		} else {
160 			total_bytes = 0;
161 			break;
162 		}
163 	}
164 
165 	errconf.title = "Fetch Error";
166 	errconf.clear = true;
167 	bsddialog_initconf(&mgconf);
168 	mgconf.title = "Fetching Distribution";
169 	mgconf.auto_minwidth = 40;
170 
171 	mainperc_file = 100.0 / nfiles;
172 	current_bytes = 0;
173 	for (i = 0; i < nfiles; i++) {
174 		fetchLastErrCode = 0;
175 		fetch_out = fetchXGetURL(urls[i], &ustat, "");
176 		if (fetch_out == NULL) {
177 			snprintf(errormsg, sizeof(errormsg),
178 			    "Error (URL) while fetching %s: %s\n", urls[i],
179 			    fetchLastErrString);
180 			miniperc[2] = BSDDIALOG_MG_FAILED;
181 			bsddialog_msgbox(&errconf, errormsg, 0, 0);
182 			total_bytes = 0;
183 			continue;
184 		}
185 
186 		miniperc[i] = BSDDIALOG_MG_INPROGRESS;
187 		fsize = 0;
188 		file_out = fopen(minilabel[i], "w+");
189 		if (file_out == NULL) {
190 			snprintf(errormsg, sizeof(errormsg),
191 			    "Error (fopen) while fetching %s: %s\n",
192 			    urls[i], strerror(errno));
193 			miniperc[i] = BSDDIALOG_MG_FAILED;
194 			bsddialog_msgbox(&errconf, errormsg, 0, 0);
195 			fclose(fetch_out);
196 			total_bytes = 0;
197 			continue;
198 		}
199 
200 		while ((chunk = fread(block, 1, sizeof(block), fetch_out))
201 		    > 0) {
202 			if (fwrite(block, 1, chunk, file_out) < chunk)
203 				break;
204 
205 			current_bytes += chunk;
206 			fsize += chunk;
207 
208 			last_progress = progress;
209 			if (total_bytes > 0) {
210 				progress = (current_bytes * 100) / total_bytes;
211 			} else {
212 				file_perc = ustat.size > 0 ?
213 				    (fsize * 100) / ustat.size : 0;
214 				progress = (i * mainperc_file) +
215 				    ((file_perc * mainperc_file) / 100);
216 			}
217 
218 			if (ustat.size > 0) {
219 				perc = (fsize * 100) / ustat.size;
220 				miniperc[i] = perc;
221 			}
222 
223 			if (progress > last_progress) {
224 				bsddialog_mixedgauge(&mgconf,
225 				    "\nFetching distribution files...\n",
226 				    0, 0, progress, nfiles, minilabel,
227 				    miniperc);
228 			}
229 		}
230 
231 		if (ustat.size > 0 && fsize < ustat.size) {
232 			if (fetchLastErrCode == 0)
233 				snprintf(errormsg, sizeof(errormsg),
234 				    "Error (undone) while fetching %s: %s\n",
235 				    urls[i], strerror(errno));
236 			else
237 				snprintf(errormsg, sizeof(errormsg),
238 				    "Error (libfetch) while fetching %s: %s\n",
239 				    urls[i], fetchLastErrString);
240 			miniperc[i] = BSDDIALOG_MG_FAILED;
241 			bsddialog_msgbox(&errconf, errormsg, 0, 0);
242 			total_bytes = 0;
243 		} else {
244 			miniperc[i] = BSDDIALOG_MG_DONE;
245 			nsuccess++;
246 		}
247 
248 		fclose(fetch_out);
249 		fclose(file_out);
250 	}
251 
252 	bsddialog_mixedgauge(&mgconf, "\nFetching distribution completed\n",
253 	    0, 0, progress, nfiles, minilabel, miniperc);
254 
255 	free(minilabel);
256 	free(miniperc);
257 	return (nsuccess);
258 }
259