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>
33 #include <sys/mkdev.h>
34 #elif defined(MAJOR_IN_SYSMACROS)
35 #include <sys/sysmacros.h>
37 #ifdef HAVE_SYS_PARAM_H
38 #include <sys/param.h>
40 #ifdef HAVE_SYS_STAT_H
73 static void list_item_verbose(struct bsdtar *, FILE *,
74 struct archive_entry *);
75 static void read_archive(struct bsdtar *bsdtar, char mode);
78 tar_mode_t(struct bsdtar *bsdtar)
80 read_archive(bsdtar, 't');
81 unmatched_inclusions_warn(bsdtar, "Not found in archive");
85 tar_mode_x(struct bsdtar *bsdtar)
87 /* We want to catch SIGINFO and SIGUSR1. */
90 read_archive(bsdtar, 'x');
92 unmatched_inclusions_warn(bsdtar, "Not found in archive");
93 /* Restore old SIGINFO + SIGUSR1 handlers. */
98 progress_func(void * cookie)
100 struct bsdtar * bsdtar = cookie;
102 siginfo_printinfo(bsdtar, 0);
106 * Handle 'x' and 't' modes.
109 read_archive(struct bsdtar *bsdtar, char mode)
113 struct archive_entry *entry;
114 const struct stat *st;
117 while (*bsdtar->argv) {
118 include(bsdtar, *bsdtar->argv);
122 if (bsdtar->names_from_file != NULL)
123 include_from_file(bsdtar, bsdtar->names_from_file);
125 a = archive_read_new();
126 if (bsdtar->compress_program != NULL)
127 archive_read_support_compression_program(a, bsdtar->compress_program);
129 archive_read_support_compression_all(a);
130 archive_read_support_format_all(a);
131 if (ARCHIVE_OK != archive_read_set_options(a, bsdtar->option_options))
132 bsdtar_errc(1, 0, archive_error_string(a));
133 if (archive_read_open_file(a, bsdtar->filename,
134 bsdtar->bytes_per_block != 0 ? bsdtar->bytes_per_block :
135 DEFAULT_BYTES_PER_BLOCK))
136 bsdtar_errc(1, 0, "Error opening archive: %s",
137 archive_error_string(a));
142 /* Set an extract callback so that we can handle SIGINFO. */
143 archive_read_extract_set_progress_callback(a, progress_func,
147 if (mode == 'x' && bsdtar->option_chroot) {
149 if (chroot(".") != 0)
150 bsdtar_errc(1, errno, "Can't chroot to \".\"");
153 "chroot isn't supported on this platform");
158 /* Support --fast-read option */
159 if (bsdtar->option_fast_read &&
160 unmatched_inclusions(bsdtar) == 0)
163 r = archive_read_next_header(a, &entry);
164 if (r == ARCHIVE_EOF)
167 bsdtar_warnc(0, "%s", archive_error_string(a));
168 if (r <= ARCHIVE_WARN)
169 bsdtar->return_value = 1;
170 if (r == ARCHIVE_RETRY) {
171 /* Retryable error: try again */
172 bsdtar_warnc(0, "Retrying...");
175 if (r == ARCHIVE_FATAL)
178 if (bsdtar->option_numeric_owner) {
179 archive_entry_set_uname(entry, NULL);
180 archive_entry_set_gname(entry, NULL);
184 * Exclude entries that are too old.
186 st = archive_entry_stat(entry);
187 if (bsdtar->newer_ctime_sec > 0) {
188 if (st->st_ctime < bsdtar->newer_ctime_sec)
189 continue; /* Too old, skip it. */
190 if (st->st_ctime == bsdtar->newer_ctime_sec
191 && ARCHIVE_STAT_CTIME_NANOS(st)
192 <= bsdtar->newer_ctime_nsec)
193 continue; /* Too old, skip it. */
195 if (bsdtar->newer_mtime_sec > 0) {
196 if (st->st_mtime < bsdtar->newer_mtime_sec)
197 continue; /* Too old, skip it. */
198 if (st->st_mtime == bsdtar->newer_mtime_sec
199 && ARCHIVE_STAT_MTIME_NANOS(st)
200 <= bsdtar->newer_mtime_nsec)
201 continue; /* Too old, skip it. */
205 * Note that pattern exclusions are checked before
206 * pathname rewrites are handled. This gives more
207 * control over exclusions, since rewrites always lose
208 * information. (For example, consider a rewrite
209 * s/foo[0-9]/foo/. If we check exclusions after the
210 * rewrite, there would be no way to exclude foo1/bar
211 * while allowing foo2/bar.)
213 if (excluded(bsdtar, archive_entry_pathname(entry)))
214 continue; /* Excluded by a pattern test. */
217 /* Perversely, gtar uses -O to mean "send to stderr"
218 * when used with -t. */
219 out = bsdtar->option_stdout ? stderr : stdout;
222 * TODO: Provide some reasonable way to
223 * preview rewrites. gtar always displays
224 * the unedited path in -t output, which means
225 * you cannot easily preview rewrites.
227 if (bsdtar->verbose < 2)
228 safe_fprintf(out, "%s",
229 archive_entry_pathname(entry));
231 list_item_verbose(bsdtar, out, entry);
233 r = archive_read_data_skip(a);
234 if (r == ARCHIVE_WARN) {
236 bsdtar_warnc(0, "%s",
237 archive_error_string(a));
239 if (r == ARCHIVE_RETRY) {
241 bsdtar_warnc(0, "%s",
242 archive_error_string(a));
244 if (r == ARCHIVE_FATAL) {
246 bsdtar_warnc(0, "%s",
247 archive_error_string(a));
248 bsdtar->return_value = 1;
253 /* Note: some rewrite failures prevent extraction. */
254 if (edit_pathname(bsdtar, entry))
255 continue; /* Excluded by a rewrite failure. */
257 if (bsdtar->option_interactive &&
258 !yes("extract '%s'", archive_entry_pathname(entry)))
262 * Format here is from SUSv2, including the
265 if (bsdtar->verbose) {
266 safe_fprintf(stderr, "x %s",
267 archive_entry_pathname(entry));
271 /* Tell the SIGINFO-handler code what we're doing. */
272 siginfo_setinfo(bsdtar, "extracting",
273 archive_entry_pathname(entry), 0);
274 siginfo_printinfo(bsdtar, 0);
276 if (bsdtar->option_stdout)
277 r = archive_read_data_into_fd(a, 1);
279 r = archive_read_extract(a, entry,
280 bsdtar->extract_flags);
281 if (r != ARCHIVE_OK) {
282 if (!bsdtar->verbose)
283 safe_fprintf(stderr, "%s",
284 archive_entry_pathname(entry));
285 safe_fprintf(stderr, ": %s",
286 archive_error_string(a));
287 if (!bsdtar->verbose)
288 fprintf(stderr, "\n");
289 bsdtar->return_value = 1;
292 fprintf(stderr, "\n");
293 if (r == ARCHIVE_FATAL)
299 r = archive_read_close(a);
301 bsdtar_warnc(0, "%s", archive_error_string(a));
302 if (r <= ARCHIVE_WARN)
303 bsdtar->return_value = 1;
305 if (bsdtar->verbose > 2)
306 fprintf(stdout, "Archive Format: %s, Compression: %s\n",
307 archive_format_name(a), archive_compression_name(a));
309 archive_read_finish(a);
314 * Display information about the current file.
316 * The format here roughly duplicates the output of 'ls -l'.
317 * This is based on SUSv2, where 'tar tv' is documented as
318 * listing additional information in an "unspecified format,"
319 * and 'pax -l' is documented as using the same format as 'ls -l'.
322 list_item_verbose(struct bsdtar *bsdtar, FILE *out, struct archive_entry *entry)
324 const struct stat *st;
332 st = archive_entry_stat(entry);
335 * We avoid collecting the entire list in memory at once by
336 * listing things as we see them. However, that also means we can't
337 * just pre-compute the field widths. Instead, we start with guesses
338 * and just widen them as necessary. These numbers are completely
341 if (!bsdtar->u_width) {
343 bsdtar->gs_width = 13;
347 fprintf(out, "%s %d ",
348 archive_entry_strmode(entry),
349 (int)(st->st_nlink));
351 /* Use uname if it's present, else uid. */
352 p = archive_entry_uname(entry);
353 if ((p == NULL) || (*p == '\0')) {
354 sprintf(tmp, "%lu ", (unsigned long)st->st_uid);
358 if (w > bsdtar->u_width)
360 fprintf(out, "%-*s ", (int)bsdtar->u_width, p);
362 /* Use gname if it's present, else gid. */
363 p = archive_entry_gname(entry);
364 if (p != NULL && p[0] != '\0') {
365 fprintf(out, "%s", p);
368 sprintf(tmp, "%lu", (unsigned long)st->st_gid);
370 fprintf(out, "%s", tmp);
374 * Print device number or file size, right-aligned so as to make
375 * total width of group and devnum/filesize fields be gs_width.
376 * If gs_width is too small, grow it.
378 if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
379 sprintf(tmp, "%lu,%lu",
380 (unsigned long)major(st->st_rdev),
381 (unsigned long)minor(st->st_rdev)); /* ls(1) also casts here. */
384 * Note the use of platform-dependent macros to format
385 * the filesize here. We need the format string and the
386 * corresponding type for the cast.
388 sprintf(tmp, BSDTAR_FILESIZE_PRINTF,
389 (BSDTAR_FILESIZE_TYPE)st->st_size);
391 if (w + strlen(tmp) >= bsdtar->gs_width)
392 bsdtar->gs_width = w+strlen(tmp)+1;
393 fprintf(out, "%*s", (int)(bsdtar->gs_width - w), tmp);
395 /* Format the time using 'ls -l' conventions. */
396 tim = (time_t)st->st_mtime;
397 #if defined(_WIN32) && !defined(__CYGWIN__)
398 /* Windows' strftime function does not support %e format. */
399 if (abs(tim - now) > (365/2)*86400)
400 fmt = bsdtar->day_first ? "%d %b %Y" : "%b %d %Y";
402 fmt = bsdtar->day_first ? "%d %b %H:%M" : "%b %d %H:%M";
404 if (abs(tim - now) > (365/2)*86400)
405 fmt = bsdtar->day_first ? "%e %b %Y" : "%b %e %Y";
407 fmt = bsdtar->day_first ? "%e %b %H:%M" : "%b %e %H:%M";
409 strftime(tmp, sizeof(tmp), fmt, localtime(&tim));
410 fprintf(out, " %s ", tmp);
411 safe_fprintf(out, "%s", archive_entry_pathname(entry));
413 /* Extra information for links. */
414 if (archive_entry_hardlink(entry)) /* Hard link */
415 safe_fprintf(out, " link to %s",
416 archive_entry_hardlink(entry));
417 else if (S_ISLNK(st->st_mode)) /* Symbolic link */
418 safe_fprintf(out, " -> %s", archive_entry_symlink(entry));