2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2011 Nathan Whitehorn
5 * Copyright (c) 2014 Devin Teske <dteske@FreeBSD.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
44 static int fetch_files(int nfiles, char **urls);
54 char error[PATH_MAX + 512];
56 if (getenv("DISTRIBUTIONS") == NULL)
57 errx(EXIT_FAILURE, "DISTRIBUTIONS variable is not set");
59 diststring = strdup(getenv("DISTRIBUTIONS"));
60 for (i = 0; diststring[i] != 0; i++)
61 if (isspace(diststring[i]) && !isspace(diststring[i+1]))
63 ndists++; /* Last one */
65 urls = calloc(ndists, sizeof(const char *));
68 errx(EXIT_FAILURE, "Out of memory!");
71 init_dialog(stdin, stdout);
72 dialog_vars.backtitle = __DECONST(char *, "FreeBSD Installer");
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"));
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);
87 return (EXIT_FAILURE);
90 nfetched = fetch_files(ndists, urls);
95 for (i = 0; i < ndists; i++)
99 return ((nfetched == ndists) ? EXIT_SUCCESS : EXIT_FAILURE);
103 fetch_files(int nfiles, char **urls)
110 int nsuccess = 0; /* Number of files successfully downloaded */
117 struct url_stat ustat;
118 char errormsg[PATH_MAX + 512];
121 /* Make the transfer list for dialog */
122 items = calloc(sizeof(char *), nfiles * 2);
124 errx(EXIT_FAILURE, "Out of memory!");
126 for (i = 0; i < nfiles; i++) {
127 items[i*2] = strrchr(urls[i], '/');
128 if (items[i*2] != NULL)
131 items[i*2] = urls[i];
132 items[i*2 + 1] = "Pending";
135 dialog_msgbox("", "Connecting to server.\nPlease wait...", 0, 0, FALSE);
137 /* Try to stat all the files */
139 for (i = 0; i < nfiles; i++) {
140 if (fetchStatURL(urls[i], &ustat, "") == 0 && ustat.size > 0)
141 total_bytes += ustat.size;
145 for (i = 0; i < nfiles; i++) {
146 last_progress = progress;
147 if (total_bytes == 0)
148 progress = (i*100)/nfiles;
150 fetchLastErrCode = 0;
151 fetch_out = fetchXGetURL(urls[i], &ustat, "");
152 if (fetch_out == NULL) {
153 snprintf(errormsg, sizeof(errormsg),
154 "Error while fetching %s: %s\n", urls[i],
156 items[i*2 + 1] = "Failed";
157 dialog_msgbox("Fetch Error", errormsg, 0, 0,
162 items[i*2 + 1] = "In Progress";
164 file_out = fopen(items[i*2], "w+");
165 if (file_out == NULL) {
166 snprintf(errormsg, sizeof(errormsg),
167 "Error while fetching %s: %s\n",
168 urls[i], strerror(errno));
169 items[i*2 + 1] = "Failed";
170 dialog_msgbox("Fetch Error", errormsg, 0, 0,
176 while ((chunk = fread(block, 1, sizeof(block), fetch_out))
178 if (fwrite(block, 1, chunk, file_out) < chunk)
181 current_bytes += chunk;
184 if (total_bytes > 0) {
185 last_progress = progress;
186 progress = (current_bytes*100)/total_bytes;
189 if (ustat.size > 0) {
190 snprintf(status, sizeof(status), "-%jd",
191 (fsize*100)/ustat.size);
192 items[i*2 + 1] = status;
195 if (progress > last_progress)
196 dialog_mixedgauge("Fetching Distribution",
197 "Fetching distribution files...", 0, 0,
199 __DECONST(char **, items));
202 if (ustat.size > 0 && fsize < ustat.size) {
203 if (fetchLastErrCode == 0)
204 snprintf(errormsg, sizeof(errormsg),
205 "Error while fetching %s: %s\n",
206 urls[i], strerror(errno));
208 snprintf(errormsg, sizeof(errormsg),
209 "Error while fetching %s: %s\n",
210 urls[i], fetchLastErrString);
211 items[i*2 + 1] = "Failed";
212 dialog_msgbox("Fetch Error", errormsg, 0, 0,
215 items[i*2 + 1] = "Done";