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