2 * Copyright (c) 2003-2007 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include "bsdtar_platform.h"
27 __FBSDID("$FreeBSD$");
29 #ifdef HAVE_SYS_TYPES_H
30 #include <sys/types.h>
32 #ifdef HAVE_SYS_PARAM_H
33 #include <sys/param.h>
35 #ifdef HAVE_SYS_STAT_H
71 struct progress_data {
72 struct bsdtar *bsdtar;
73 struct archive *archive;
74 struct archive_entry *entry;
77 static void list_item_verbose(struct bsdtar *, FILE *,
78 struct archive_entry *);
79 static void read_archive(struct bsdtar *bsdtar, char mode);
82 tar_mode_t(struct bsdtar *bsdtar)
84 read_archive(bsdtar, 't');
85 if (lafe_unmatched_inclusions_warn(bsdtar->matching, "Not found in archive") != 0)
86 bsdtar->return_value = 1;
90 tar_mode_x(struct bsdtar *bsdtar)
92 read_archive(bsdtar, 'x');
94 if (lafe_unmatched_inclusions_warn(bsdtar->matching, "Not found in archive") != 0)
95 bsdtar->return_value = 1;
99 progress_func(void *cookie)
101 struct progress_data *progress_data = cookie;
102 struct bsdtar *bsdtar = progress_data->bsdtar;
103 struct archive *a = progress_data->archive;
104 struct archive_entry *entry = progress_data->entry;
105 uint64_t comp, uncomp;
112 fprintf(stderr, "\n");
114 comp = archive_position_compressed(a);
115 uncomp = archive_position_uncompressed(a);
119 compression = (int)((uncomp - comp) * 100 / uncomp);
121 "In: %s bytes, compression %d%%;",
122 tar_i64toa(comp), compression);
123 fprintf(stderr, " Out: %d files, %s bytes\n",
124 archive_file_count(a), tar_i64toa(uncomp));
127 safe_fprintf(stderr, "Current: %s",
128 archive_entry_pathname(entry));
129 fprintf(stderr, " (%s bytes)\n",
130 tar_i64toa(archive_entry_size(entry)));
135 * Handle 'x' and 't' modes.
138 read_archive(struct bsdtar *bsdtar, char mode)
140 struct progress_data progress_data;
143 struct archive_entry *entry;
144 const struct stat *st;
147 while (*bsdtar->argv) {
148 lafe_include(&bsdtar->matching, *bsdtar->argv);
152 if (bsdtar->names_from_file != NULL)
153 lafe_include_from_file(&bsdtar->matching,
154 bsdtar->names_from_file, bsdtar->option_null);
156 a = archive_read_new();
157 if (bsdtar->compress_program != NULL)
158 archive_read_support_compression_program(a, bsdtar->compress_program);
160 archive_read_support_compression_all(a);
161 archive_read_support_format_all(a);
162 if (ARCHIVE_OK != archive_read_set_options(a, bsdtar->option_options))
163 lafe_errc(1, 0, "%s", archive_error_string(a));
164 if (archive_read_open_file(a, bsdtar->filename,
165 bsdtar->bytes_per_block != 0 ? bsdtar->bytes_per_block :
166 DEFAULT_BYTES_PER_BLOCK))
167 lafe_errc(1, 0, "Error opening archive: %s",
168 archive_error_string(a));
173 /* Set an extract callback so that we can handle SIGINFO. */
174 progress_data.bsdtar = bsdtar;
175 progress_data.archive = a;
176 archive_read_extract_set_progress_callback(a, progress_func,
180 if (mode == 'x' && bsdtar->option_chroot) {
182 if (chroot(".") != 0)
183 lafe_errc(1, errno, "Can't chroot to \".\"");
186 "chroot isn't supported on this platform");
191 /* Support --fast-read option */
192 if (bsdtar->option_fast_read &&
193 lafe_unmatched_inclusions(bsdtar->matching) == 0)
196 r = archive_read_next_header(a, &entry);
197 progress_data.entry = entry;
198 if (r == ARCHIVE_EOF)
201 lafe_warnc(0, "%s", archive_error_string(a));
202 if (r <= ARCHIVE_WARN)
203 bsdtar->return_value = 1;
204 if (r == ARCHIVE_RETRY) {
205 /* Retryable error: try again */
206 lafe_warnc(0, "Retrying...");
209 if (r == ARCHIVE_FATAL)
212 if (bsdtar->uid >= 0) {
213 archive_entry_set_uid(entry, bsdtar->uid);
214 archive_entry_set_uname(entry, NULL);
216 if (bsdtar->gid >= 0) {
217 archive_entry_set_gid(entry, bsdtar->gid);
218 archive_entry_set_gname(entry, NULL);
221 archive_entry_set_uname(entry, bsdtar->uname);
223 archive_entry_set_gname(entry, bsdtar->gname);
226 * Exclude entries that are too old.
228 st = archive_entry_stat(entry);
229 if (bsdtar->newer_ctime_sec > 0) {
230 if (st->st_ctime < bsdtar->newer_ctime_sec)
231 continue; /* Too old, skip it. */
232 if (st->st_ctime == bsdtar->newer_ctime_sec
233 && ARCHIVE_STAT_CTIME_NANOS(st)
234 <= bsdtar->newer_ctime_nsec)
235 continue; /* Too old, skip it. */
237 if (bsdtar->newer_mtime_sec > 0) {
238 if (st->st_mtime < bsdtar->newer_mtime_sec)
239 continue; /* Too old, skip it. */
240 if (st->st_mtime == bsdtar->newer_mtime_sec
241 && ARCHIVE_STAT_MTIME_NANOS(st)
242 <= bsdtar->newer_mtime_nsec)
243 continue; /* Too old, skip it. */
247 * Note that pattern exclusions are checked before
248 * pathname rewrites are handled. This gives more
249 * control over exclusions, since rewrites always lose
250 * information. (For example, consider a rewrite
251 * s/foo[0-9]/foo/. If we check exclusions after the
252 * rewrite, there would be no way to exclude foo1/bar
253 * while allowing foo2/bar.)
255 if (lafe_excluded(bsdtar->matching, archive_entry_pathname(entry)))
256 continue; /* Excluded by a pattern test. */
259 /* Perversely, gtar uses -O to mean "send to stderr"
260 * when used with -t. */
261 out = bsdtar->option_stdout ? stderr : stdout;
264 * TODO: Provide some reasonable way to
265 * preview rewrites. gtar always displays
266 * the unedited path in -t output, which means
267 * you cannot easily preview rewrites.
269 if (bsdtar->verbose < 2)
270 safe_fprintf(out, "%s",
271 archive_entry_pathname(entry));
273 list_item_verbose(bsdtar, out, entry);
275 r = archive_read_data_skip(a);
276 if (r == ARCHIVE_WARN) {
279 archive_error_string(a));
281 if (r == ARCHIVE_RETRY) {
284 archive_error_string(a));
286 if (r == ARCHIVE_FATAL) {
289 archive_error_string(a));
290 bsdtar->return_value = 1;
295 /* Note: some rewrite failures prevent extraction. */
296 if (edit_pathname(bsdtar, entry))
297 continue; /* Excluded by a rewrite failure. */
299 if (bsdtar->option_interactive &&
300 !yes("extract '%s'", archive_entry_pathname(entry)))
304 * Format here is from SUSv2, including the
307 if (bsdtar->verbose) {
308 safe_fprintf(stderr, "x %s",
309 archive_entry_pathname(entry));
313 // TODO siginfo_printinfo(bsdtar, 0);
315 if (bsdtar->option_stdout)
316 r = archive_read_data_into_fd(a, 1);
318 r = archive_read_extract(a, entry,
319 bsdtar->extract_flags);
320 if (r != ARCHIVE_OK) {
321 if (!bsdtar->verbose)
322 safe_fprintf(stderr, "%s",
323 archive_entry_pathname(entry));
324 safe_fprintf(stderr, ": %s",
325 archive_error_string(a));
326 if (!bsdtar->verbose)
327 fprintf(stderr, "\n");
328 bsdtar->return_value = 1;
331 fprintf(stderr, "\n");
332 if (r == ARCHIVE_FATAL)
338 r = archive_read_close(a);
340 lafe_warnc(0, "%s", archive_error_string(a));
341 if (r <= ARCHIVE_WARN)
342 bsdtar->return_value = 1;
344 if (bsdtar->verbose > 2)
345 fprintf(stdout, "Archive Format: %s, Compression: %s\n",
346 archive_format_name(a), archive_compression_name(a));
348 archive_read_finish(a);
353 * Display information about the current file.
355 * The format here roughly duplicates the output of 'ls -l'.
356 * This is based on SUSv2, where 'tar tv' is documented as
357 * listing additional information in an "unspecified format,"
358 * and 'pax -l' is documented as using the same format as 'ls -l'.
361 list_item_verbose(struct bsdtar *bsdtar, FILE *out, struct archive_entry *entry)
371 * We avoid collecting the entire list in memory at once by
372 * listing things as we see them. However, that also means we can't
373 * just pre-compute the field widths. Instead, we start with guesses
374 * and just widen them as necessary. These numbers are completely
377 if (!bsdtar->u_width) {
379 bsdtar->gs_width = 13;
383 fprintf(out, "%s %d ",
384 archive_entry_strmode(entry),
385 archive_entry_nlink(entry));
387 /* Use uname if it's present, else uid. */
388 p = archive_entry_uname(entry);
389 if ((p == NULL) || (*p == '\0')) {
391 (unsigned long)archive_entry_uid(entry));
395 if (w > bsdtar->u_width)
397 fprintf(out, "%-*s ", (int)bsdtar->u_width, p);
399 /* Use gname if it's present, else gid. */
400 p = archive_entry_gname(entry);
401 if (p != NULL && p[0] != '\0') {
402 fprintf(out, "%s", p);
406 (unsigned long)archive_entry_gid(entry));
408 fprintf(out, "%s", tmp);
412 * Print device number or file size, right-aligned so as to make
413 * total width of group and devnum/filesize fields be gs_width.
414 * If gs_width is too small, grow it.
416 if (archive_entry_filetype(entry) == AE_IFCHR
417 || archive_entry_filetype(entry) == AE_IFBLK) {
418 sprintf(tmp, "%lu,%lu",
419 (unsigned long)archive_entry_rdevmajor(entry),
420 (unsigned long)archive_entry_rdevminor(entry));
422 strcpy(tmp, tar_i64toa(archive_entry_size(entry)));
424 if (w + strlen(tmp) >= bsdtar->gs_width)
425 bsdtar->gs_width = w+strlen(tmp)+1;
426 fprintf(out, "%*s", (int)(bsdtar->gs_width - w), tmp);
428 /* Format the time using 'ls -l' conventions. */
429 tim = archive_entry_mtime(entry);
430 #define HALF_YEAR (time_t)365 * 86400 / 2
431 #if defined(_WIN32) && !defined(__CYGWIN__)
432 #define DAY_FMT "%d" /* Windows' strftime function does not support %e format. */
434 #define DAY_FMT "%e" /* Day number without leading zeros */
436 if (tim < now - HALF_YEAR || tim > now + HALF_YEAR)
437 fmt = bsdtar->day_first ? DAY_FMT " %b %Y" : "%b " DAY_FMT " %Y";
439 fmt = bsdtar->day_first ? DAY_FMT " %b %H:%M" : "%b " DAY_FMT " %H:%M";
440 strftime(tmp, sizeof(tmp), fmt, localtime(&tim));
441 fprintf(out, " %s ", tmp);
442 safe_fprintf(out, "%s", archive_entry_pathname(entry));
444 /* Extra information for links. */
445 if (archive_entry_hardlink(entry)) /* Hard link */
446 safe_fprintf(out, " link to %s",
447 archive_entry_hardlink(entry));
448 else if (archive_entry_symlink(entry)) /* Symbolic link */
449 safe_fprintf(out, " -> %s", archive_entry_symlink(entry));