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 #include <ctype.h>
35 #include <err.h>
36 #include <dialog.h>
37 #include <errno.h>
38 #include <fetch.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 
44 static int fetch_files(int nfiles, char **urls);
45 
46 int
47 main(void)
48 {
49 	char *diststring;
50 	char **urls;
51 	int i;
52 	int ndists = 0;
53 	int nfetched;
54 	char error[PATH_MAX + 512];
55 
56 	if (getenv("DISTRIBUTIONS") == NULL)
57 		errx(EXIT_FAILURE, "DISTRIBUTIONS variable is not set");
58 
59 	diststring = strdup(getenv("DISTRIBUTIONS"));
60 	for (i = 0; diststring[i] != 0; i++)
61 		if (isspace(diststring[i]) && !isspace(diststring[i+1]))
62 			ndists++;
63 	ndists++; /* Last one */
64 
65 	urls = calloc(ndists, sizeof(const char *));
66 	if (urls == NULL) {
67 		free(diststring);
68 		errx(EXIT_FAILURE, "Out of memory!");
69 	}
70 
71 	init_dialog(stdin, stdout);
72 	dialog_vars.backtitle = __DECONST(char *, "FreeBSD Installer");
73 	dlg_put_backtitle();
74 
75 	for (i = 0; i < ndists; i++) {
76 		urls[i] = malloc(PATH_MAX);
77 		snprintf(urls[i], PATH_MAX, "%s/%s",
78 		    getenv("BSDINSTALL_DISTSITE"), strsep(&diststring, " \t"));
79 	}
80 
81 	if (chdir(getenv("BSDINSTALL_DISTDIR")) != 0) {
82 		snprintf(error, sizeof(error),
83 		    "Could not change to directory %s: %s\n",
84 		    getenv("BSDINSTALL_DISTDIR"), strerror(errno));
85 		dialog_msgbox("Error", error, 0, 0, TRUE);
86 		end_dialog();
87 		return (EXIT_FAILURE);
88 	}
89 
90 	nfetched = fetch_files(ndists, urls);
91 
92 	end_dialog();
93 
94 	free(diststring);
95 	for (i = 0; i < ndists; i++)
96 		free(urls[i]);
97 	free(urls);
98 
99 	return ((nfetched == ndists) ? EXIT_SUCCESS : EXIT_FAILURE);
100 }
101 
102 static int
103 fetch_files(int nfiles, char **urls)
104 {
105 	FILE *fetch_out;
106 	FILE *file_out;
107 	const char **items;
108 	int i;
109 	int last_progress;
110 	int nsuccess = 0; /* Number of files successfully downloaded */
111 	int progress = 0;
112 	size_t chunk;
113 	off_t current_bytes;
114 	off_t fsize;
115 	off_t total_bytes;
116 	float file_perc;
117 	float mainperc_file;
118 	char status[8];
119 	struct url_stat ustat;
120 	char errormsg[PATH_MAX + 512];
121 	uint8_t block[4096];
122 
123 	/* Make the transfer list for dialog */
124 	items = calloc(sizeof(char *), nfiles * 2);
125 	if (items == NULL)
126 		errx(EXIT_FAILURE, "Out of memory!");
127 
128 	for (i = 0; i < nfiles; i++) {
129 		items[i*2] = strrchr(urls[i], '/');
130 		if (items[i*2] != NULL)
131 			items[i*2]++;
132 		else
133 			items[i*2] = urls[i];
134 		items[i*2 + 1] = "Pending";
135 	}
136 
137 	dialog_msgbox("", "Connecting to server.\nPlease wait...", 0, 0, FALSE);
138 
139 	/* Try to stat all the files */
140 	total_bytes = 0;
141 	for (i = 0; i < nfiles; i++) {
142 		if (fetchStatURL(urls[i], &ustat, "") == 0 && ustat.size > 0) {
143 			total_bytes += ustat.size;
144 		} else {
145 			total_bytes = 0;
146 			break;
147 		}
148 	}
149 
150 	mainperc_file = 100.0 / nfiles;
151 	current_bytes = 0;
152 	for (i = 0; i < nfiles; i++) {
153 		fetchLastErrCode = 0;
154 		fetch_out = fetchXGetURL(urls[i], &ustat, "");
155 		if (fetch_out == NULL) {
156 			snprintf(errormsg, sizeof(errormsg),
157 			    "Error while fetching %s: %s\n", urls[i],
158 			    fetchLastErrString);
159 			items[i*2 + 1] = "Failed";
160 			dialog_msgbox("Fetch Error", errormsg, 0, 0,
161 			    TRUE);
162 			total_bytes = 0;
163 			continue;
164 		}
165 
166 		items[i*2 + 1] = "In Progress";
167 		fsize = 0;
168 		file_out = fopen(items[i*2], "w+");
169 		if (file_out == NULL) {
170 			snprintf(errormsg, sizeof(errormsg),
171 			    "Error while fetching %s: %s\n",
172 			    urls[i], strerror(errno));
173 			items[i*2 + 1] = "Failed";
174 			dialog_msgbox("Fetch Error", errormsg, 0, 0,
175 			    TRUE);
176 			fclose(fetch_out);
177 			total_bytes = 0;
178 			continue;
179 		}
180 
181 		while ((chunk = fread(block, 1, sizeof(block), fetch_out))
182 		    > 0) {
183 			if (fwrite(block, 1, chunk, file_out) < chunk)
184 				break;
185 
186 			current_bytes += chunk;
187 			fsize += chunk;
188 
189 			last_progress = progress;
190 			if (total_bytes > 0) {
191 				progress = (current_bytes*100)/total_bytes;
192 			} else {
193 				file_perc = ustat.size > 0 ?
194 				    (fsize*100)/ustat.size : 0;
195 				progress = (i * mainperc_file) +
196 				    ((file_perc * mainperc_file) / 100);
197 			}
198 
199 			if (ustat.size > 0) {
200 				snprintf(status, sizeof(status), "-%jd",
201 				    (fsize*100)/ustat.size);
202 				items[i*2 + 1] = status;
203 			}
204 
205 			if (progress > last_progress)
206 				dialog_mixedgauge("Fetching Distribution",
207 				    "Fetching distribution files...", 0, 0,
208 				    progress, nfiles,
209 				    __DECONST(char **, items));
210 		}
211 
212 		if (ustat.size > 0 && fsize < ustat.size) {
213 			if (fetchLastErrCode == 0)
214 				snprintf(errormsg, sizeof(errormsg),
215 				    "Error while fetching %s: %s\n",
216 				    urls[i], strerror(errno));
217 			else
218 				snprintf(errormsg, sizeof(errormsg),
219 				    "Error while fetching %s: %s\n",
220 				    urls[i], fetchLastErrString);
221 			items[i*2 + 1] = "Failed";
222 			dialog_msgbox("Fetch Error", errormsg, 0, 0,
223 				    TRUE);
224 			total_bytes = 0;
225 		} else {
226 			items[i*2 + 1] = "Done";
227 			nsuccess++;
228 		}
229 
230 		fclose(fetch_out);
231 		fclose(file_out);
232 	}
233 
234 	dialog_mixedgauge("Fetching Distribution",
235 	    "Fetching distribution completed", 0, 0, progress, nfiles,
236 	    __DECONST(char **, items));
237 
238 	free(items);
239 	return (nsuccess);
240 }
241