2 * SPDX-License-Identifier: BSD-2-Clause
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 #include <sys/param.h>
35 #include <bsddialog.h>
36 #include <bsddialog_progressview.h>
46 #include "opt_osname.h"
49 static const char *distdir = NULL;
50 static struct archive *archive = NULL;
52 /* Function prototypes */
53 static void sig_int(int sig);
54 static int count_files(const char *file);
55 static int extract_files(struct bsddialog_fileminibar *file);
57 #define _errx(...) (bsddialog_end(), errx(__VA_ARGS__))
64 char *distribs, *distrib;
66 size_t minibar_size = sizeof(struct bsddialog_fileminibar);
67 unsigned int nminibars;
68 struct bsddialog_fileminibar *dists;
69 struct bsddialog_progviewconf pvconf;
70 struct bsddialog_conf conf;
72 char error[PATH_MAX + 512];
74 if ((distributions = getenv("DISTRIBUTIONS")) == NULL)
75 errx(EXIT_FAILURE, "DISTRIBUTIONS variable is not set");
76 if ((distdir = getenv("BSDINSTALL_DISTDIR")) == NULL)
78 if ((distribs = strdup(distributions)) == NULL)
79 errx(EXIT_FAILURE, "memory error");
81 if (bsddialog_init() == BSDDIALOG_ERROR)
82 errx(EXIT_FAILURE, "Error libbsdialog: %s",
83 bsddialog_geterror());
84 bsddialog_initconf(&conf);
85 bsddialog_backtitle(&conf, OSNAME " Installer");
86 bsddialog_infobox(&conf,
87 "Checking distribution archives.\nPlease wait...", 4, 35);
89 /* Parse $DISTRIBUTIONS */
92 while ((distrib = strsep(&distribs, "\t\n\v\f\r ")) != NULL) {
93 if (strlen(distrib) == 0)
96 /* Allocate a new struct for the distribution */
97 dists = realloc(dists, (nminibars + 1) * minibar_size);
99 _errx(EXIT_FAILURE, "Out of memory!");
102 dists[nminibars].path = distrib;
104 /* Set mini bar label */
105 dists[nminibars].label = strrchr(dists[nminibars].path, '/');
106 if (dists[nminibars].label == NULL)
107 dists[nminibars].label = dists[nminibars].path;
109 /* Set initial length in files (-1 == error) */
110 dists[nminibars].size = count_files(dists[nminibars].path);
111 if (dists[nminibars].size < 0) {
113 return (EXIT_FAILURE);
116 /* Set initial status and implicitly miniperc to pending */
117 dists[nminibars].status = BSDDIALOG_MG_PENDING;
119 /* Set initial read */
120 dists[nminibars].read = 0;
125 /* Optionally chdir(2) into $BSDINSTALL_CHROOT */
126 chrootdir = getenv("BSDINSTALL_CHROOT");
127 if (chrootdir != NULL && chdir(chrootdir) != 0) {
128 snprintf(error, sizeof(error),
129 "Could not change to directory %s: %s\n",
130 chrootdir, strerror(errno));
131 conf.title = "Error";
132 bsddialog_msgbox(&conf, error, 0, 0);
134 return (EXIT_FAILURE);
137 /* Set cleanup routine for Ctrl-C action */
138 act.sa_handler = sig_int;
139 sigaction(SIGINT, &act, 0);
141 conf.title = "Archive Extraction";
142 conf.auto_minwidth = 40;
143 pvconf.callback = extract_files;
145 pvconf.fmtbottomstr = "%10lli files read @ %'9.1f files/sec.";
146 bsddialog_total_progview = 0;
147 bsddialog_interruptprogview = bsddialog_abortprogview = false;
148 retval = bsddialog_progressview(&conf,
149 "\nExtracting distribution files...\n", 0, 0,
150 &pvconf, nminibars, dists);
152 if (retval == BSDDIALOG_ERROR) {
153 fprintf(stderr, "progressview error: %s\n",
154 bsddialog_geterror());
166 sig_int(int sig __unused)
168 bsddialog_interruptprogview = true;
172 * Returns number of files in archive file. Parses $BSDINSTALL_DISTDIR/MANIFEST
173 * if it exists, otherwise uses archive(3) to read the archive file.
176 count_files(const char *file)
178 static FILE *manifest = NULL;
183 struct archive_entry *entry;
186 char errormsg[PATH_MAX + 512];
187 struct bsddialog_conf conf;
189 if (manifest == NULL) {
190 snprintf(path, sizeof(path), "%s/MANIFEST", distdir);
191 manifest = fopen(path, "r");
194 if (manifest != NULL) {
196 while (fgets(line, sizeof(line), manifest) != NULL) {
198 span = strcspn(p, "\t") ;
199 if (span < 1 || strncmp(p, file, span) != 0)
203 * We're at the right manifest line. The file count is
204 * in the third element
206 span = strcspn(p += span + (*p != '\0' ? 1 : 0), "\t");
207 span = strcspn(p += span + (*p != '\0' ? 1 : 0), "\t");
209 file_count = (int)strtol(p, (char **)NULL, 10);
210 if (file_count == 0 && errno == EINVAL)
218 * Either no manifest, or manifest didn't mention this archive.
219 * Use archive(3) to read the archive, counting files within.
221 bsddialog_initconf(&conf);
222 if ((archive = archive_read_new()) == NULL) {
223 snprintf(errormsg, sizeof(errormsg),
224 "Error: %s\n", archive_error_string(NULL));
225 conf.title = "Extract Error";
226 bsddialog_msgbox(&conf, errormsg, 0, 0);
229 archive_read_support_format_all(archive);
230 archive_read_support_filter_all(archive);
231 snprintf(path, sizeof(path), "%s/%s", distdir, file);
232 retval = archive_read_open_filename(archive, path, 4096);
233 if (retval != ARCHIVE_OK) {
234 snprintf(errormsg, sizeof(errormsg),
235 "Error while extracting %s: %s\n", file,
236 archive_error_string(archive));
237 conf.title = "Extract Error";
238 bsddialog_msgbox(&conf, errormsg, 0, 0);
244 while (archive_read_next_header(archive, &entry) == ARCHIVE_OK)
246 archive_read_free(archive);
253 extract_files(struct bsddialog_fileminibar *file)
256 struct archive_entry *entry;
258 char errormsg[PATH_MAX + 512];
259 struct bsddialog_conf conf;
261 bsddialog_initconf(&conf);
263 /* Open the archive if necessary */
264 if (archive == NULL) {
265 if ((archive = archive_read_new()) == NULL) {
266 snprintf(errormsg, sizeof(errormsg),
267 "Error: %s\n", archive_error_string(NULL));
268 conf.title = "Extract Error";
269 bsddialog_msgbox(&conf, errormsg, 0, 0);
270 bsddialog_abortprogview = true;
273 archive_read_support_format_all(archive);
274 archive_read_support_filter_all(archive);
275 snprintf(path, sizeof(path), "%s/%s", distdir, file->path);
276 retval = archive_read_open_filename(archive, path, 4096);
278 snprintf(errormsg, sizeof(errormsg),
279 "Error opening %s: %s\n", file->label,
280 archive_error_string(archive));
281 conf.title = "Extract Error";
282 bsddialog_msgbox(&conf, errormsg, 0, 0);
283 file->status = BSDDIALOG_MG_FAILED;
284 bsddialog_abortprogview = true;
289 /* Read the next archive header */
290 retval = archive_read_next_header(archive, &entry);
292 /* If that went well, perform the extraction */
293 if (retval == ARCHIVE_OK)
294 retval = archive_read_extract(archive, entry,
295 ARCHIVE_EXTRACT_TIME | ARCHIVE_EXTRACT_OWNER |
296 ARCHIVE_EXTRACT_PERM | ARCHIVE_EXTRACT_ACL |
297 ARCHIVE_EXTRACT_XATTR | ARCHIVE_EXTRACT_FFLAGS);
299 /* Test for either EOF or error */
300 if (retval == ARCHIVE_EOF) {
301 archive_read_free(archive);
303 file->status = BSDDIALOG_MG_DONE; /*Done*/;
305 } else if (retval != ARCHIVE_OK &&
306 !(retval == ARCHIVE_WARN &&
307 strcmp(archive_error_string(archive), "Can't restore time") == 0)) {
309 * Print any warning/error messages except inability to set
310 * ctime/mtime, which is not fatal, or even interesting,
311 * for our purposes. Would be nice if this were a libarchive
314 snprintf(errormsg, sizeof(errormsg),
315 "Error while extracting %s: %s\n", file->label,
316 archive_error_string(archive));
317 conf.title = "Extract Error";
318 bsddialog_msgbox(&conf, errormsg, 0, 0);
319 file->status = BSDDIALOG_MG_FAILED; /* Failed */
320 bsddialog_abortprogview = true;
324 bsddialog_total_progview++;
327 /* Calculate [overall] percentage of completion (if possible) */
329 return (file->read * 100 / file->size);