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
72 static void list_item_verbose(struct bsdtar *, FILE *,
73 struct archive_entry *);
74 static void read_archive(struct bsdtar *bsdtar, char mode);
77 tar_mode_t(struct bsdtar *bsdtar)
79 read_archive(bsdtar, 't');
80 unmatched_inclusions_warn(bsdtar, "Not found in archive");
84 tar_mode_x(struct bsdtar *bsdtar)
86 /* We want to catch SIGINFO and SIGUSR1. */
89 read_archive(bsdtar, 'x');
91 unmatched_inclusions_warn(bsdtar, "Not found in archive");
92 /* Restore old SIGINFO + SIGUSR1 handlers. */
97 progress_func(void * cookie)
99 struct bsdtar * bsdtar = cookie;
101 siginfo_printinfo(bsdtar, 0);
105 * Handle 'x' and 't' modes.
108 read_archive(struct bsdtar *bsdtar, char mode)
112 struct archive_entry *entry;
113 const struct stat *st;
116 while (*bsdtar->argv) {
117 include(bsdtar, *bsdtar->argv);
121 if (bsdtar->names_from_file != NULL)
122 include_from_file(bsdtar, bsdtar->names_from_file);
124 a = archive_read_new();
125 if (bsdtar->compress_program != NULL)
126 archive_read_support_compression_program(a, bsdtar->compress_program);
128 archive_read_support_compression_all(a);
129 archive_read_support_format_all(a);
130 if (archive_read_open_file(a, bsdtar->filename,
131 bsdtar->bytes_per_block != 0 ? bsdtar->bytes_per_block :
132 DEFAULT_BYTES_PER_BLOCK))
133 bsdtar_errc(bsdtar, 1, 0, "Error opening archive: %s",
134 archive_error_string(a));
139 /* Set an extract callback so that we can handle SIGINFO. */
140 archive_read_extract_set_progress_callback(a, progress_func,
144 if (mode == 'x' && bsdtar->option_chroot) {
146 if (chroot(".") != 0)
147 bsdtar_errc(bsdtar, 1, errno, "Can't chroot to \".\"");
149 bsdtar_errc(bsdtar, 1, 0,
150 "chroot isn't supported on this platform");
155 /* Support --fast-read option */
156 if (bsdtar->option_fast_read &&
157 unmatched_inclusions(bsdtar) == 0)
160 r = archive_read_next_header(a, &entry);
161 if (r == ARCHIVE_EOF)
164 bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(a));
165 if (r <= ARCHIVE_WARN)
166 bsdtar->return_value = 1;
167 if (r == ARCHIVE_RETRY) {
168 /* Retryable error: try again */
169 bsdtar_warnc(bsdtar, 0, "Retrying...");
172 if (r == ARCHIVE_FATAL)
175 if (bsdtar->option_numeric_owner) {
176 archive_entry_set_uname(entry, NULL);
177 archive_entry_set_gname(entry, NULL);
181 * Exclude entries that are too old.
183 st = archive_entry_stat(entry);
184 if (bsdtar->newer_ctime_sec > 0) {
185 if (st->st_ctime < bsdtar->newer_ctime_sec)
186 continue; /* Too old, skip it. */
187 if (st->st_ctime == bsdtar->newer_ctime_sec
188 && ARCHIVE_STAT_CTIME_NANOS(st)
189 <= bsdtar->newer_ctime_nsec)
190 continue; /* Too old, skip it. */
192 if (bsdtar->newer_mtime_sec > 0) {
193 if (st->st_mtime < bsdtar->newer_mtime_sec)
194 continue; /* Too old, skip it. */
195 if (st->st_mtime == bsdtar->newer_mtime_sec
196 && ARCHIVE_STAT_MTIME_NANOS(st)
197 <= bsdtar->newer_mtime_nsec)
198 continue; /* Too old, skip it. */
202 * Note that pattern exclusions are checked before
203 * pathname rewrites are handled. This gives more
204 * control over exclusions, since rewrites always lose
205 * information. (For example, consider a rewrite
206 * s/foo[0-9]/foo/. If we check exclusions after the
207 * rewrite, there would be no way to exclude foo1/bar
208 * while allowing foo2/bar.)
210 if (excluded(bsdtar, archive_entry_pathname(entry)))
211 continue; /* Excluded by a pattern test. */
214 /* Perversely, gtar uses -O to mean "send to stderr"
215 * when used with -t. */
216 out = bsdtar->option_stdout ? stderr : stdout;
219 * TODO: Provide some reasonable way to
220 * preview rewrites. gtar always displays
221 * the unedited path in -t output, which means
222 * you cannot easily preview rewrites.
224 if (bsdtar->verbose < 2)
225 safe_fprintf(out, "%s",
226 archive_entry_pathname(entry));
228 list_item_verbose(bsdtar, out, entry);
230 r = archive_read_data_skip(a);
231 if (r == ARCHIVE_WARN) {
233 bsdtar_warnc(bsdtar, 0, "%s",
234 archive_error_string(a));
236 if (r == ARCHIVE_RETRY) {
238 bsdtar_warnc(bsdtar, 0, "%s",
239 archive_error_string(a));
241 if (r == ARCHIVE_FATAL) {
243 bsdtar_warnc(bsdtar, 0, "%s",
244 archive_error_string(a));
245 bsdtar->return_value = 1;
250 /* Note: some rewrite failures prevent extraction. */
251 if (edit_pathname(bsdtar, entry))
252 continue; /* Excluded by a rewrite failure. */
254 if (bsdtar->option_interactive &&
255 !yes("extract '%s'", archive_entry_pathname(entry)))
259 * Format here is from SUSv2, including the
262 if (bsdtar->verbose) {
263 safe_fprintf(stderr, "x %s",
264 archive_entry_pathname(entry));
268 /* Tell the SIGINFO-handler code what we're doing. */
269 siginfo_setinfo(bsdtar, "extracting",
270 archive_entry_pathname(entry), 0);
271 siginfo_printinfo(bsdtar, 0);
273 if (bsdtar->option_stdout)
274 r = archive_read_data_into_fd(a, 1);
276 r = archive_read_extract(a, entry,
277 bsdtar->extract_flags);
278 if (r != ARCHIVE_OK) {
279 if (!bsdtar->verbose)
280 safe_fprintf(stderr, "%s",
281 archive_entry_pathname(entry));
282 safe_fprintf(stderr, ": %s",
283 archive_error_string(a));
284 if (!bsdtar->verbose)
285 fprintf(stderr, "\n");
286 bsdtar->return_value = 1;
289 fprintf(stderr, "\n");
290 if (r == ARCHIVE_FATAL)
295 if (bsdtar->verbose > 2)
296 fprintf(stdout, "Archive Format: %s, Compression: %s\n",
297 archive_format_name(a), archive_compression_name(a));
299 archive_read_finish(a);
304 * Display information about the current file.
306 * The format here roughly duplicates the output of 'ls -l'.
307 * This is based on SUSv2, where 'tar tv' is documented as
308 * listing additional information in an "unspecified format,"
309 * and 'pax -l' is documented as using the same format as 'ls -l'.
312 list_item_verbose(struct bsdtar *bsdtar, FILE *out, struct archive_entry *entry)
314 const struct stat *st;
322 st = archive_entry_stat(entry);
325 * We avoid collecting the entire list in memory at once by
326 * listing things as we see them. However, that also means we can't
327 * just pre-compute the field widths. Instead, we start with guesses
328 * and just widen them as necessary. These numbers are completely
331 if (!bsdtar->u_width) {
333 bsdtar->gs_width = 13;
337 fprintf(out, "%s %d ",
338 archive_entry_strmode(entry),
339 (int)(st->st_nlink));
341 /* Use uname if it's present, else uid. */
342 p = archive_entry_uname(entry);
343 if ((p == NULL) || (*p == '\0')) {
344 sprintf(tmp, "%lu ", (unsigned long)st->st_uid);
348 if (w > bsdtar->u_width)
350 fprintf(out, "%-*s ", (int)bsdtar->u_width, p);
352 /* Use gname if it's present, else gid. */
353 p = archive_entry_gname(entry);
354 if (p != NULL && p[0] != '\0') {
355 fprintf(out, "%s", p);
358 sprintf(tmp, "%lu", (unsigned long)st->st_gid);
360 fprintf(out, "%s", tmp);
364 * Print device number or file size, right-aligned so as to make
365 * total width of group and devnum/filesize fields be gs_width.
366 * If gs_width is too small, grow it.
368 if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
369 sprintf(tmp, "%lu,%lu",
370 (unsigned long)major(st->st_rdev),
371 (unsigned long)minor(st->st_rdev)); /* ls(1) also casts here. */
374 * Note the use of platform-dependent macros to format
375 * the filesize here. We need the format string and the
376 * corresponding type for the cast.
378 sprintf(tmp, BSDTAR_FILESIZE_PRINTF,
379 (BSDTAR_FILESIZE_TYPE)st->st_size);
381 if (w + strlen(tmp) >= bsdtar->gs_width)
382 bsdtar->gs_width = w+strlen(tmp)+1;
383 fprintf(out, "%*s", (int)(bsdtar->gs_width - w), tmp);
385 /* Format the time using 'ls -l' conventions. */
386 tim = (time_t)st->st_mtime;
387 if (abs(tim - now) > (365/2)*86400)
388 fmt = bsdtar->day_first ? "%e %b %Y" : "%b %e %Y";
390 fmt = bsdtar->day_first ? "%e %b %H:%M" : "%b %e %H:%M";
391 strftime(tmp, sizeof(tmp), fmt, localtime(&tim));
392 fprintf(out, " %s ", tmp);
393 safe_fprintf(out, "%s", archive_entry_pathname(entry));
395 /* Extra information for links. */
396 if (archive_entry_hardlink(entry)) /* Hard link */
397 safe_fprintf(out, " link to %s",
398 archive_entry_hardlink(entry));
399 else if (S_ISLNK(st->st_mode)) /* Symbolic link */
400 safe_fprintf(out, " -> %s", archive_entry_symlink(entry));