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: src/usr.bin/tar/read.c,v 1.40 2008/08/21 06:41:14 kientzle Exp $");
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, struct archive *);
82 tar_mode_t(struct bsdtar *bsdtar)
84 read_archive(bsdtar, 't', NULL);
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 struct archive *writer;
94 writer = archive_write_disk_new();
96 lafe_errc(1, ENOMEM, "Cannot allocate disk writer object");
97 if (!bsdtar->option_numeric_owner)
98 archive_write_disk_set_standard_lookup(writer);
99 archive_write_disk_set_options(writer, bsdtar->extract_flags);
101 read_archive(bsdtar, 'x', writer);
103 if (lafe_unmatched_inclusions_warn(bsdtar->matching, "Not found in archive") != 0)
104 bsdtar->return_value = 1;
105 archive_write_free(writer);
109 progress_func(void *cookie)
111 struct progress_data *progress_data = cookie;
112 struct bsdtar *bsdtar = progress_data->bsdtar;
113 struct archive *a = progress_data->archive;
114 struct archive_entry *entry = progress_data->entry;
115 uint64_t comp, uncomp;
122 fprintf(stderr, "\n");
124 comp = archive_position_compressed(a);
125 uncomp = archive_position_uncompressed(a);
129 compression = (int)((uncomp - comp) * 100 / uncomp);
131 "In: %s bytes, compression %d%%;",
132 tar_i64toa(comp), compression);
133 fprintf(stderr, " Out: %d files, %s bytes\n",
134 archive_file_count(a), tar_i64toa(uncomp));
137 safe_fprintf(stderr, "Current: %s",
138 archive_entry_pathname(entry));
139 fprintf(stderr, " (%s bytes)\n",
140 tar_i64toa(archive_entry_size(entry)));
145 * Handle 'x' and 't' modes.
148 read_archive(struct bsdtar *bsdtar, char mode, struct archive *writer)
150 struct progress_data progress_data;
153 struct archive_entry *entry;
158 while (*bsdtar->argv) {
159 lafe_include(&bsdtar->matching, *bsdtar->argv);
163 if (bsdtar->names_from_file != NULL)
164 lafe_include_from_file(&bsdtar->matching,
165 bsdtar->names_from_file, bsdtar->option_null);
167 a = archive_read_new();
168 if (bsdtar->compress_program != NULL)
169 archive_read_support_filter_program(a, bsdtar->compress_program);
171 archive_read_support_filter_all(a);
172 archive_read_support_format_all(a);
173 if (ARCHIVE_OK != archive_read_set_options(a, bsdtar->option_options))
174 lafe_errc(1, 0, "%s", archive_error_string(a));
175 if (archive_read_open_file(a, bsdtar->filename, bsdtar->bytes_per_block))
176 lafe_errc(1, 0, "Error opening archive: %s",
177 archive_error_string(a));
182 /* Set an extract callback so that we can handle SIGINFO. */
183 progress_data.bsdtar = bsdtar;
184 progress_data.archive = a;
185 archive_read_extract_set_progress_callback(a, progress_func,
189 if (mode == 'x' && bsdtar->option_chroot) {
191 if (chroot(".") != 0)
192 lafe_errc(1, errno, "Can't chroot to \".\"");
195 "chroot isn't supported on this platform");
200 /* Support --fast-read option */
201 if (bsdtar->option_fast_read &&
202 lafe_unmatched_inclusions(bsdtar->matching) == 0)
205 r = archive_read_next_header(a, &entry);
206 progress_data.entry = entry;
207 if (r == ARCHIVE_EOF)
210 lafe_warnc(0, "%s", archive_error_string(a));
211 if (r <= ARCHIVE_WARN)
212 bsdtar->return_value = 1;
213 if (r == ARCHIVE_RETRY) {
214 /* Retryable error: try again */
215 lafe_warnc(0, "Retrying...");
218 if (r == ARCHIVE_FATAL)
221 if (bsdtar->uid >= 0) {
222 archive_entry_set_uid(entry, bsdtar->uid);
223 archive_entry_set_uname(entry, NULL);
225 if (bsdtar->gid >= 0) {
226 archive_entry_set_gid(entry, bsdtar->gid);
227 archive_entry_set_gname(entry, NULL);
230 archive_entry_set_uname(entry, bsdtar->uname);
232 archive_entry_set_gname(entry, bsdtar->gname);
235 * Exclude entries that are too old.
237 if (bsdtar->newer_ctime_filter) {
238 /* Use ctime if format provides, else mtime. */
239 if (archive_entry_ctime_is_set(entry)) {
240 sec = archive_entry_ctime(entry);
241 nsec = archive_entry_ctime_nsec(entry);
242 } else if (archive_entry_mtime_is_set(entry)) {
243 sec = archive_entry_mtime(entry);
244 nsec = archive_entry_mtime_nsec(entry);
249 if (sec < bsdtar->newer_ctime_sec)
250 continue; /* Too old, skip it. */
251 if (sec == bsdtar->newer_ctime_sec
252 && nsec <= bsdtar->newer_ctime_nsec)
253 continue; /* Too old, skip it. */
255 if (bsdtar->newer_mtime_filter) {
256 if (archive_entry_mtime_is_set(entry)) {
257 sec = archive_entry_mtime(entry);
258 nsec = archive_entry_mtime_nsec(entry);
263 if (sec < bsdtar->newer_mtime_sec)
264 continue; /* Too old, skip it. */
265 if (sec == bsdtar->newer_mtime_sec
266 && nsec <= bsdtar->newer_mtime_nsec)
267 continue; /* Too old, skip it. */
271 * Note that pattern exclusions are checked before
272 * pathname rewrites are handled. This gives more
273 * control over exclusions, since rewrites always lose
274 * information. (For example, consider a rewrite
275 * s/foo[0-9]/foo/. If we check exclusions after the
276 * rewrite, there would be no way to exclude foo1/bar
277 * while allowing foo2/bar.)
279 if (lafe_excluded(bsdtar->matching, archive_entry_pathname(entry)))
280 continue; /* Excluded by a pattern test. */
283 /* Perversely, gtar uses -O to mean "send to stderr"
284 * when used with -t. */
285 out = bsdtar->option_stdout ? stderr : stdout;
288 * TODO: Provide some reasonable way to
289 * preview rewrites. gtar always displays
290 * the unedited path in -t output, which means
291 * you cannot easily preview rewrites.
293 if (bsdtar->verbose < 2)
294 safe_fprintf(out, "%s",
295 archive_entry_pathname(entry));
297 list_item_verbose(bsdtar, out, entry);
299 r = archive_read_data_skip(a);
300 if (r == ARCHIVE_WARN) {
303 archive_error_string(a));
305 if (r == ARCHIVE_RETRY) {
308 archive_error_string(a));
310 if (r == ARCHIVE_FATAL) {
313 archive_error_string(a));
314 bsdtar->return_value = 1;
319 /* Note: some rewrite failures prevent extraction. */
320 if (edit_pathname(bsdtar, entry))
321 continue; /* Excluded by a rewrite failure. */
323 if (bsdtar->option_interactive &&
324 !yes("extract '%s'", archive_entry_pathname(entry)))
328 * Format here is from SUSv2, including the
331 if (bsdtar->verbose) {
332 safe_fprintf(stderr, "x %s",
333 archive_entry_pathname(entry));
337 /* TODO siginfo_printinfo(bsdtar, 0); */
339 if (bsdtar->option_stdout)
340 r = archive_read_data_into_fd(a, 1);
342 r = archive_read_extract2(a, entry, writer);
343 if (r != ARCHIVE_OK) {
344 if (!bsdtar->verbose)
345 safe_fprintf(stderr, "%s",
346 archive_entry_pathname(entry));
347 safe_fprintf(stderr, ": %s",
348 archive_error_string(a));
349 if (!bsdtar->verbose)
350 fprintf(stderr, "\n");
351 bsdtar->return_value = 1;
354 fprintf(stderr, "\n");
355 if (r == ARCHIVE_FATAL)
361 r = archive_read_close(a);
363 lafe_warnc(0, "%s", archive_error_string(a));
364 if (r <= ARCHIVE_WARN)
365 bsdtar->return_value = 1;
367 if (bsdtar->verbose > 2)
368 fprintf(stdout, "Archive Format: %s, Compression: %s\n",
369 archive_format_name(a), archive_compression_name(a));
371 archive_read_free(a);
376 * Display information about the current file.
378 * The format here roughly duplicates the output of 'ls -l'.
379 * This is based on SUSv2, where 'tar tv' is documented as
380 * listing additional information in an "unspecified format,"
381 * and 'pax -l' is documented as using the same format as 'ls -l'.
384 list_item_verbose(struct bsdtar *bsdtar, FILE *out, struct archive_entry *entry)
394 * We avoid collecting the entire list in memory at once by
395 * listing things as we see them. However, that also means we can't
396 * just pre-compute the field widths. Instead, we start with guesses
397 * and just widen them as necessary. These numbers are completely
400 if (!bsdtar->u_width) {
402 bsdtar->gs_width = 13;
406 fprintf(out, "%s %d ",
407 archive_entry_strmode(entry),
408 archive_entry_nlink(entry));
410 /* Use uname if it's present, else uid. */
411 p = archive_entry_uname(entry);
412 if ((p == NULL) || (*p == '\0')) {
414 (unsigned long)archive_entry_uid(entry));
418 if (w > bsdtar->u_width)
420 fprintf(out, "%-*s ", (int)bsdtar->u_width, p);
422 /* Use gname if it's present, else gid. */
423 p = archive_entry_gname(entry);
424 if (p != NULL && p[0] != '\0') {
425 fprintf(out, "%s", p);
429 (unsigned long)archive_entry_gid(entry));
431 fprintf(out, "%s", tmp);
435 * Print device number or file size, right-aligned so as to make
436 * total width of group and devnum/filesize fields be gs_width.
437 * If gs_width is too small, grow it.
439 if (archive_entry_filetype(entry) == AE_IFCHR
440 || archive_entry_filetype(entry) == AE_IFBLK) {
441 sprintf(tmp, "%lu,%lu",
442 (unsigned long)archive_entry_rdevmajor(entry),
443 (unsigned long)archive_entry_rdevminor(entry));
445 strcpy(tmp, tar_i64toa(archive_entry_size(entry)));
447 if (w + strlen(tmp) >= bsdtar->gs_width)
448 bsdtar->gs_width = w+strlen(tmp)+1;
449 fprintf(out, "%*s", (int)(bsdtar->gs_width - w), tmp);
451 /* Format the time using 'ls -l' conventions. */
452 tim = archive_entry_mtime(entry);
453 #define HALF_YEAR (time_t)365 * 86400 / 2
454 #if defined(_WIN32) && !defined(__CYGWIN__)
455 #define DAY_FMT "%d" /* Windows' strftime function does not support %e format. */
457 #define DAY_FMT "%e" /* Day number without leading zeros */
459 if (tim < now - HALF_YEAR || tim > now + HALF_YEAR)
460 fmt = bsdtar->day_first ? DAY_FMT " %b %Y" : "%b " DAY_FMT " %Y";
462 fmt = bsdtar->day_first ? DAY_FMT " %b %H:%M" : "%b " DAY_FMT " %H:%M";
463 strftime(tmp, sizeof(tmp), fmt, localtime(&tim));
464 fprintf(out, " %s ", tmp);
465 safe_fprintf(out, "%s", archive_entry_pathname(entry));
467 /* Extra information for links. */
468 if (archive_entry_hardlink(entry)) /* Hard link */
469 safe_fprintf(out, " link to %s",
470 archive_entry_hardlink(entry));
471 else if (archive_entry_symlink(entry)) /* Symbolic link */
472 safe_fprintf(out, " -> %s", archive_entry_symlink(entry));