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->option_numeric_owner) {
213 archive_entry_set_uname(entry, NULL);
214 archive_entry_set_gname(entry, NULL);
218 * Exclude entries that are too old.
220 st = archive_entry_stat(entry);
221 if (bsdtar->newer_ctime_sec > 0) {
222 if (st->st_ctime < bsdtar->newer_ctime_sec)
223 continue; /* Too old, skip it. */
224 if (st->st_ctime == bsdtar->newer_ctime_sec
225 && ARCHIVE_STAT_CTIME_NANOS(st)
226 <= bsdtar->newer_ctime_nsec)
227 continue; /* Too old, skip it. */
229 if (bsdtar->newer_mtime_sec > 0) {
230 if (st->st_mtime < bsdtar->newer_mtime_sec)
231 continue; /* Too old, skip it. */
232 if (st->st_mtime == bsdtar->newer_mtime_sec
233 && ARCHIVE_STAT_MTIME_NANOS(st)
234 <= bsdtar->newer_mtime_nsec)
235 continue; /* Too old, skip it. */
239 * Note that pattern exclusions are checked before
240 * pathname rewrites are handled. This gives more
241 * control over exclusions, since rewrites always lose
242 * information. (For example, consider a rewrite
243 * s/foo[0-9]/foo/. If we check exclusions after the
244 * rewrite, there would be no way to exclude foo1/bar
245 * while allowing foo2/bar.)
247 if (lafe_excluded(bsdtar->matching, archive_entry_pathname(entry)))
248 continue; /* Excluded by a pattern test. */
251 /* Perversely, gtar uses -O to mean "send to stderr"
252 * when used with -t. */
253 out = bsdtar->option_stdout ? stderr : stdout;
256 * TODO: Provide some reasonable way to
257 * preview rewrites. gtar always displays
258 * the unedited path in -t output, which means
259 * you cannot easily preview rewrites.
261 if (bsdtar->verbose < 2)
262 safe_fprintf(out, "%s",
263 archive_entry_pathname(entry));
265 list_item_verbose(bsdtar, out, entry);
267 r = archive_read_data_skip(a);
268 if (r == ARCHIVE_WARN) {
271 archive_error_string(a));
273 if (r == ARCHIVE_RETRY) {
276 archive_error_string(a));
278 if (r == ARCHIVE_FATAL) {
281 archive_error_string(a));
282 bsdtar->return_value = 1;
287 /* Note: some rewrite failures prevent extraction. */
288 if (edit_pathname(bsdtar, entry))
289 continue; /* Excluded by a rewrite failure. */
291 if (bsdtar->option_interactive &&
292 !yes("extract '%s'", archive_entry_pathname(entry)))
296 * Format here is from SUSv2, including the
299 if (bsdtar->verbose) {
300 safe_fprintf(stderr, "x %s",
301 archive_entry_pathname(entry));
305 // TODO siginfo_printinfo(bsdtar, 0);
307 if (bsdtar->option_stdout)
308 r = archive_read_data_into_fd(a, 1);
310 r = archive_read_extract(a, entry,
311 bsdtar->extract_flags);
312 if (r != ARCHIVE_OK) {
313 if (!bsdtar->verbose)
314 safe_fprintf(stderr, "%s",
315 archive_entry_pathname(entry));
316 safe_fprintf(stderr, ": %s",
317 archive_error_string(a));
318 if (!bsdtar->verbose)
319 fprintf(stderr, "\n");
320 bsdtar->return_value = 1;
323 fprintf(stderr, "\n");
324 if (r == ARCHIVE_FATAL)
330 r = archive_read_close(a);
332 lafe_warnc(0, "%s", archive_error_string(a));
333 if (r <= ARCHIVE_WARN)
334 bsdtar->return_value = 1;
336 if (bsdtar->verbose > 2)
337 fprintf(stdout, "Archive Format: %s, Compression: %s\n",
338 archive_format_name(a), archive_compression_name(a));
340 archive_read_finish(a);
345 * Display information about the current file.
347 * The format here roughly duplicates the output of 'ls -l'.
348 * This is based on SUSv2, where 'tar tv' is documented as
349 * listing additional information in an "unspecified format,"
350 * and 'pax -l' is documented as using the same format as 'ls -l'.
353 list_item_verbose(struct bsdtar *bsdtar, FILE *out, struct archive_entry *entry)
363 * We avoid collecting the entire list in memory at once by
364 * listing things as we see them. However, that also means we can't
365 * just pre-compute the field widths. Instead, we start with guesses
366 * and just widen them as necessary. These numbers are completely
369 if (!bsdtar->u_width) {
371 bsdtar->gs_width = 13;
375 fprintf(out, "%s %d ",
376 archive_entry_strmode(entry),
377 archive_entry_nlink(entry));
379 /* Use uname if it's present, else uid. */
380 p = archive_entry_uname(entry);
381 if ((p == NULL) || (*p == '\0')) {
383 (unsigned long)archive_entry_uid(entry));
387 if (w > bsdtar->u_width)
389 fprintf(out, "%-*s ", (int)bsdtar->u_width, p);
391 /* Use gname if it's present, else gid. */
392 p = archive_entry_gname(entry);
393 if (p != NULL && p[0] != '\0') {
394 fprintf(out, "%s", p);
398 (unsigned long)archive_entry_gid(entry));
400 fprintf(out, "%s", tmp);
404 * Print device number or file size, right-aligned so as to make
405 * total width of group and devnum/filesize fields be gs_width.
406 * If gs_width is too small, grow it.
408 if (archive_entry_filetype(entry) == AE_IFCHR
409 || archive_entry_filetype(entry) == AE_IFBLK) {
410 sprintf(tmp, "%lu,%lu",
411 (unsigned long)archive_entry_rdevmajor(entry),
412 (unsigned long)archive_entry_rdevminor(entry));
414 strcpy(tmp, tar_i64toa(archive_entry_size(entry)));
416 if (w + strlen(tmp) >= bsdtar->gs_width)
417 bsdtar->gs_width = w+strlen(tmp)+1;
418 fprintf(out, "%*s", (int)(bsdtar->gs_width - w), tmp);
420 /* Format the time using 'ls -l' conventions. */
421 tim = archive_entry_mtime(entry);
422 #define HALF_YEAR (time_t)365 * 86400 / 2
423 #if defined(_WIN32) && !defined(__CYGWIN__)
424 #define DAY_FMT "%d" /* Windows' strftime function does not support %e format. */
426 #define DAY_FMT "%e" /* Day number without leading zeros */
428 if (tim < now - HALF_YEAR || tim > now + HALF_YEAR)
429 fmt = bsdtar->day_first ? DAY_FMT " %b %Y" : "%b " DAY_FMT " %Y";
431 fmt = bsdtar->day_first ? DAY_FMT " %b %H:%M" : "%b " DAY_FMT " %H:%M";
432 strftime(tmp, sizeof(tmp), fmt, localtime(&tim));
433 fprintf(out, " %s ", tmp);
434 safe_fprintf(out, "%s", archive_entry_pathname(entry));
436 /* Extra information for links. */
437 if (archive_entry_hardlink(entry)) /* Hard link */
438 safe_fprintf(out, " link to %s",
439 archive_entry_hardlink(entry));
440 else if (archive_entry_symlink(entry)) /* Symbolic link */
441 safe_fprintf(out, " -> %s", archive_entry_symlink(entry));