Path: blob/main/usr.sbin/bsdinstall/distfetch/distfetch.c
102616 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2011 Nathan Whitehorn4* Copyright (c) 2014 Devin Teske <[email protected]>5* All rights reserved.6*7* Redistribution and use in source and binary forms, with or without8* modification, are permitted provided that the following conditions9* are met:10* 1. Redistributions of source code must retain the above copyright11* notice, this list of conditions and the following disclaimer.12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND17* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE18* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE19* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE20* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL21* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS22* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)23* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT24* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY25* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF26* SUCH DAMAGE.27*/2829#include <sys/param.h>3031#include <bsddialog.h>32#include <ctype.h>33#include <err.h>34#include <errno.h>35#include <stdio.h>36#include <fetch.h>37#include <stdlib.h>38#include <string.h>39#include <unistd.h>4041#include "opt_osname.h"4243static int fetch_files(int nfiles, char **urls);4445int46main(void)47{48char *diststring;49char **urls;50int i;51int ndists = 0;52int nfetched;53char error[PATH_MAX + 512];54struct bsddialog_conf conf;5556if (getenv("DISTRIBUTIONS") == NULL)57errx(EXIT_FAILURE, "DISTRIBUTIONS variable is not set");5859diststring = strdup(getenv("DISTRIBUTIONS"));60for (i = 0; diststring[i] != 0; i++)61if (isspace(diststring[i]) && !isspace(diststring[i+1]))62ndists++;63ndists++; /* Last one */6465urls = calloc(ndists, sizeof(const char *));66if (urls == NULL) {67free(diststring);68errx(EXIT_FAILURE, "Error: distfetch URLs out of memory!");69}7071if (bsddialog_init() == BSDDIALOG_ERROR) {72free(diststring);73errx(EXIT_FAILURE, "Error libbsddialog: %s\n",74bsddialog_geterror());75}76bsddialog_initconf(&conf);77bsddialog_backtitle(&conf, OSNAME " Installer");7879for (i = 0; i < ndists; i++) {80urls[i] = malloc(PATH_MAX);81snprintf(urls[i], PATH_MAX, "%s/%s",82getenv("BSDINSTALL_DISTSITE"), strsep(&diststring, " \t"));83}8485if (chdir(getenv("BSDINSTALL_DISTDIR")) != 0) {86snprintf(error, sizeof(error),87"Could not change to directory %s: %s\n",88getenv("BSDINSTALL_DISTDIR"), strerror(errno));89conf.title = "Error";90bsddialog_msgbox(&conf, error, 0, 0);91bsddialog_end();92return (EXIT_FAILURE);93}9495nfetched = fetch_files(ndists, urls);9697bsddialog_end();9899free(diststring);100for (i = 0; i < ndists; i++)101free(urls[i]);102free(urls);103104return ((nfetched == ndists) ? EXIT_SUCCESS : EXIT_FAILURE);105}106107static int108fetch_files(int nfiles, char **urls)109{110FILE *fetch_out;111FILE *file_out;112const char **minilabel;113int *miniperc;114int perc;115int i;116int last_progress;117int nsuccess = 0; /* Number of files successfully downloaded */118int progress = 0;119size_t chunk;120off_t current_bytes;121off_t fsize;122off_t total_bytes;123float file_perc;124float mainperc_file;125struct url_stat ustat;126char errormsg[PATH_MAX + 512];127uint8_t block[4096];128struct bsddialog_conf errconf;129struct bsddialog_conf mgconf;130131/* Make the transfer list for mixedgauge */132minilabel = calloc(nfiles, sizeof(char *));133miniperc = calloc(nfiles, sizeof(int));134if (minilabel == NULL || miniperc == NULL)135errx(EXIT_FAILURE, "Error: distfetch minibars out of memory!");136137for (i = 0; i < nfiles; i++) {138minilabel[i] = strrchr(urls[i], '/');139if (minilabel[i] != NULL)140minilabel[i]++;141else142minilabel[i] = urls[i];143miniperc[i] = BSDDIALOG_MG_PENDING;144}145146bsddialog_initconf(&errconf);147bsddialog_infobox(&errconf, "Connecting to server.\nPlease wait...",1480, 0);149150/* Try to stat all the files */151total_bytes = 0;152for (i = 0; i < nfiles; i++) {153if (fetchStatURL(urls[i], &ustat, "") == 0 && ustat.size > 0) {154total_bytes += ustat.size;155} else {156total_bytes = 0;157break;158}159}160161errconf.title = "Fetch Error";162errconf.clear = true;163bsddialog_initconf(&mgconf);164mgconf.title = "Fetching Distribution";165mgconf.auto_minwidth = 40;166167mainperc_file = 100.0 / nfiles;168current_bytes = 0;169for (i = 0; i < nfiles; i++) {170fetchLastErrCode = 0;171fetch_out = fetchXGetURL(urls[i], &ustat, "");172if (fetch_out == NULL) {173snprintf(errormsg, sizeof(errormsg),174"Error (URL) while fetching %s: %s\n", urls[i],175fetchLastErrString);176miniperc[2] = BSDDIALOG_MG_FAILED;177bsddialog_msgbox(&errconf, errormsg, 0, 0);178total_bytes = 0;179continue;180}181182miniperc[i] = BSDDIALOG_MG_INPROGRESS;183fsize = 0;184file_out = fopen(minilabel[i], "w+");185if (file_out == NULL) {186snprintf(errormsg, sizeof(errormsg),187"Error (fopen) while fetching %s: %s\n",188urls[i], strerror(errno));189miniperc[i] = BSDDIALOG_MG_FAILED;190bsddialog_msgbox(&errconf, errormsg, 0, 0);191fclose(fetch_out);192total_bytes = 0;193continue;194}195196while ((chunk = fread(block, 1, sizeof(block), fetch_out))197> 0) {198if (fwrite(block, 1, chunk, file_out) < chunk)199break;200201current_bytes += chunk;202fsize += chunk;203204last_progress = progress;205if (total_bytes > 0) {206progress = (current_bytes * 100) / total_bytes;207} else {208file_perc = ustat.size > 0 ?209(fsize * 100) / ustat.size : 0;210progress = (i * mainperc_file) +211((file_perc * mainperc_file) / 100);212}213214if (ustat.size > 0) {215perc = (fsize * 100) / ustat.size;216miniperc[i] = perc;217}218219if (progress > last_progress) {220bsddialog_mixedgauge(&mgconf,221"\nFetching distribution files...\n",2220, 0, progress, nfiles, minilabel,223miniperc);224}225}226227if (ustat.size > 0 && fsize < ustat.size) {228if (fetchLastErrCode == 0)229snprintf(errormsg, sizeof(errormsg),230"Error (undone) while fetching %s: %s\n",231urls[i], strerror(errno));232else233snprintf(errormsg, sizeof(errormsg),234"Error (libfetch) while fetching %s: %s\n",235urls[i], fetchLastErrString);236miniperc[i] = BSDDIALOG_MG_FAILED;237bsddialog_msgbox(&errconf, errormsg, 0, 0);238total_bytes = 0;239} else {240miniperc[i] = BSDDIALOG_MG_DONE;241nsuccess++;242}243244fclose(fetch_out);245fclose(file_out);246}247248bsddialog_mixedgauge(&mgconf, "\nFetching distribution completed\n",2490, 0, progress, nfiles, minilabel, miniperc);250251free(minilabel);252free(miniperc);253return (nsuccess);254}255256257