2 * Copyright (c) 2011 Nathan Whitehorn
3 * Copyright (c) 2014 Devin Teske <dteske@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
42 static int fetch_files(int nfiles, char **urls);
52 char error[PATH_MAX + 512];
54 if (getenv("DISTRIBUTIONS") == NULL)
55 errx(EXIT_FAILURE, "DISTRIBUTIONS variable is not set");
57 diststring = strdup(getenv("DISTRIBUTIONS"));
58 for (i = 0; diststring[i] != 0; i++)
59 if (isspace(diststring[i]) && !isspace(diststring[i+1]))
61 ndists++; /* Last one */
63 urls = calloc(ndists, sizeof(const char *));
66 errx(EXIT_FAILURE, "Out of memory!");
69 init_dialog(stdin, stdout);
70 dialog_vars.backtitle = __DECONST(char *, "FreeBSD Installer");
73 for (i = 0; i < ndists; i++) {
74 urls[i] = malloc(PATH_MAX);
75 snprintf(urls[i], PATH_MAX, "%s/%s",
76 getenv("BSDINSTALL_DISTSITE"), strsep(&diststring, " \t"));
79 if (chdir(getenv("BSDINSTALL_DISTDIR")) != 0) {
80 snprintf(error, sizeof(error),
81 "Could could change to directory %s: %s\n",
82 getenv("BSDINSTALL_DISTDIR"), strerror(errno));
83 dialog_msgbox("Error", error, 0, 0, TRUE);
85 return (EXIT_FAILURE);
88 nfetched = fetch_files(ndists, urls);
93 for (i = 0; i < ndists; i++)
97 return ((nfetched == ndists) ? EXIT_SUCCESS : EXIT_FAILURE);
101 fetch_files(int nfiles, char **urls)
108 int nsuccess = 0; /* Number of files successfully downloaded */
115 struct url_stat ustat;
116 char errormsg[PATH_MAX + 512];
119 /* Make the transfer list for dialog */
120 items = calloc(sizeof(char *), nfiles * 2);
122 errx(EXIT_FAILURE, "Out of memory!");
124 for (i = 0; i < nfiles; i++) {
125 items[i*2] = strrchr(urls[i], '/');
126 if (items[i*2] != NULL)
129 items[i*2] = urls[i];
130 items[i*2 + 1] = "Pending";
133 dialog_msgbox("", "Connecting to server.\nPlease wait...", 0, 0, FALSE);
135 /* Try to stat all the files */
137 for (i = 0; i < nfiles; i++) {
138 if (fetchStatURL(urls[i], &ustat, "") == 0 && ustat.size > 0)
139 total_bytes += ustat.size;
143 for (i = 0; i < nfiles; i++) {
144 last_progress = progress;
145 if (total_bytes == 0)
146 progress = (i*100)/nfiles;
148 fetchLastErrCode = 0;
149 fetch_out = fetchXGetURL(urls[i], &ustat, "");
150 if (fetch_out == NULL) {
151 snprintf(errormsg, sizeof(errormsg),
152 "Error while fetching %s: %s\n", urls[i],
154 items[i*2 + 1] = "Failed";
155 dialog_msgbox("Fetch Error", errormsg, 0, 0,
160 items[i*2 + 1] = "In Progress";
162 file_out = fopen(items[i*2], "w+");
163 if (file_out == NULL) {
164 snprintf(errormsg, sizeof(errormsg),
165 "Error while fetching %s: %s\n",
166 urls[i], strerror(errno));
167 items[i*2 + 1] = "Failed";
168 dialog_msgbox("Fetch Error", errormsg, 0, 0,
174 while ((chunk = fread(block, 1, sizeof(block), fetch_out))
176 if (fwrite(block, 1, chunk, file_out) < chunk)
179 current_bytes += chunk;
182 if (total_bytes > 0) {
183 last_progress = progress;
184 progress = (current_bytes*100)/total_bytes;
187 if (ustat.size > 0) {
188 snprintf(status, sizeof(status), "-%jd",
189 (fsize*100)/ustat.size);
190 items[i*2 + 1] = status;
193 if (progress > last_progress)
194 dialog_mixedgauge("Fetching Distribution",
195 "Fetching distribution files...", 0, 0,
197 __DECONST(char **, items));
200 if (ustat.size > 0 && fsize < ustat.size) {
201 if (fetchLastErrCode == 0)
202 snprintf(errormsg, sizeof(errormsg),
203 "Error while fetching %s: %s\n",
204 urls[i], strerror(errno));
206 snprintf(errormsg, sizeof(errormsg),
207 "Error while fetching %s: %s\n",
208 urls[i], fetchLastErrString);
209 items[i*2 + 1] = "Failed";
210 dialog_msgbox("Fetch Error", errormsg, 0, 0,
213 items[i*2 + 1] = "Done";