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 struct progress_data {
74 struct bsdtar *bsdtar;
75 struct archive *archive;
76 struct archive_entry *entry;
79 static void list_item_verbose(struct bsdtar *, FILE *,
80 struct archive_entry *);
81 static void read_archive(struct bsdtar *bsdtar, char mode);
84 tar_mode_t(struct bsdtar *bsdtar)
86 read_archive(bsdtar, 't');
87 unmatched_inclusions_warn(bsdtar, "Not found in archive");
91 tar_mode_x(struct bsdtar *bsdtar)
93 read_archive(bsdtar, 'x');
95 unmatched_inclusions_warn(bsdtar, "Not found in archive");
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;
111 fprintf(stderr, "\n");
113 comp = archive_position_compressed(a);
114 uncomp = archive_position_uncompressed(a);
116 "In: %s bytes, compression %d%%;",
117 tar_i64toa(comp), (int)((uncomp - comp) * 100 / uncomp));
118 fprintf(stderr, " Out: %d files, %s bytes\n",
119 archive_file_count(a), tar_i64toa(uncomp));
122 safe_fprintf(stderr, "Current: %s",
123 archive_entry_pathname(entry));
124 fprintf(stderr, " (%s bytes)\n",
125 tar_i64toa(archive_entry_size(entry)));
130 * Handle 'x' and 't' modes.
133 read_archive(struct bsdtar *bsdtar, char mode)
135 struct progress_data progress_data;
138 struct archive_entry *entry;
139 const struct stat *st;
142 while (*bsdtar->argv) {
143 include(bsdtar, *bsdtar->argv);
147 if (bsdtar->names_from_file != NULL)
148 include_from_file(bsdtar, bsdtar->names_from_file);
150 a = archive_read_new();
151 if (bsdtar->compress_program != NULL)
152 archive_read_support_compression_program(a, bsdtar->compress_program);
154 archive_read_support_compression_all(a);
155 archive_read_support_format_all(a);
156 if (ARCHIVE_OK != archive_read_set_options(a, bsdtar->option_options))
157 bsdtar_errc(1, 0, archive_error_string(a));
158 if (archive_read_open_file(a, bsdtar->filename,
159 bsdtar->bytes_per_block != 0 ? bsdtar->bytes_per_block :
160 DEFAULT_BYTES_PER_BLOCK))
161 bsdtar_errc(1, 0, "Error opening archive: %s",
162 archive_error_string(a));
167 /* Set an extract callback so that we can handle SIGINFO. */
168 progress_data.bsdtar = bsdtar;
169 progress_data.archive = a;
170 archive_read_extract_set_progress_callback(a, progress_func,
174 if (mode == 'x' && bsdtar->option_chroot) {
176 if (chroot(".") != 0)
177 bsdtar_errc(1, errno, "Can't chroot to \".\"");
180 "chroot isn't supported on this platform");
185 /* Support --fast-read option */
186 if (bsdtar->option_fast_read &&
187 unmatched_inclusions(bsdtar) == 0)
190 r = archive_read_next_header(a, &entry);
191 progress_data.entry = entry;
192 if (r == ARCHIVE_EOF)
195 bsdtar_warnc(0, "%s", archive_error_string(a));
196 if (r <= ARCHIVE_WARN)
197 bsdtar->return_value = 1;
198 if (r == ARCHIVE_RETRY) {
199 /* Retryable error: try again */
200 bsdtar_warnc(0, "Retrying...");
203 if (r == ARCHIVE_FATAL)
206 if (bsdtar->option_numeric_owner) {
207 archive_entry_set_uname(entry, NULL);
208 archive_entry_set_gname(entry, NULL);
212 * Exclude entries that are too old.
214 st = archive_entry_stat(entry);
215 if (bsdtar->newer_ctime_sec > 0) {
216 if (st->st_ctime < bsdtar->newer_ctime_sec)
217 continue; /* Too old, skip it. */
218 if (st->st_ctime == bsdtar->newer_ctime_sec
219 && ARCHIVE_STAT_CTIME_NANOS(st)
220 <= bsdtar->newer_ctime_nsec)
221 continue; /* Too old, skip it. */
223 if (bsdtar->newer_mtime_sec > 0) {
224 if (st->st_mtime < bsdtar->newer_mtime_sec)
225 continue; /* Too old, skip it. */
226 if (st->st_mtime == bsdtar->newer_mtime_sec
227 && ARCHIVE_STAT_MTIME_NANOS(st)
228 <= bsdtar->newer_mtime_nsec)
229 continue; /* Too old, skip it. */
233 * Note that pattern exclusions are checked before
234 * pathname rewrites are handled. This gives more
235 * control over exclusions, since rewrites always lose
236 * information. (For example, consider a rewrite
237 * s/foo[0-9]/foo/. If we check exclusions after the
238 * rewrite, there would be no way to exclude foo1/bar
239 * while allowing foo2/bar.)
241 if (excluded(bsdtar, archive_entry_pathname(entry)))
242 continue; /* Excluded by a pattern test. */
245 /* Perversely, gtar uses -O to mean "send to stderr"
246 * when used with -t. */
247 out = bsdtar->option_stdout ? stderr : stdout;
250 * TODO: Provide some reasonable way to
251 * preview rewrites. gtar always displays
252 * the unedited path in -t output, which means
253 * you cannot easily preview rewrites.
255 if (bsdtar->verbose < 2)
256 safe_fprintf(out, "%s",
257 archive_entry_pathname(entry));
259 list_item_verbose(bsdtar, out, entry);
261 r = archive_read_data_skip(a);
262 if (r == ARCHIVE_WARN) {
264 bsdtar_warnc(0, "%s",
265 archive_error_string(a));
267 if (r == ARCHIVE_RETRY) {
269 bsdtar_warnc(0, "%s",
270 archive_error_string(a));
272 if (r == ARCHIVE_FATAL) {
274 bsdtar_warnc(0, "%s",
275 archive_error_string(a));
276 bsdtar->return_value = 1;
281 /* Note: some rewrite failures prevent extraction. */
282 if (edit_pathname(bsdtar, entry))
283 continue; /* Excluded by a rewrite failure. */
285 if (bsdtar->option_interactive &&
286 !yes("extract '%s'", archive_entry_pathname(entry)))
290 * Format here is from SUSv2, including the
293 if (bsdtar->verbose) {
294 safe_fprintf(stderr, "x %s",
295 archive_entry_pathname(entry));
299 // TODO siginfo_printinfo(bsdtar, 0);
301 if (bsdtar->option_stdout)
302 r = archive_read_data_into_fd(a, 1);
304 r = archive_read_extract(a, entry,
305 bsdtar->extract_flags);
306 if (r != ARCHIVE_OK) {
307 if (!bsdtar->verbose)
308 safe_fprintf(stderr, "%s",
309 archive_entry_pathname(entry));
310 safe_fprintf(stderr, ": %s",
311 archive_error_string(a));
312 if (!bsdtar->verbose)
313 fprintf(stderr, "\n");
314 bsdtar->return_value = 1;
317 fprintf(stderr, "\n");
318 if (r == ARCHIVE_FATAL)
324 r = archive_read_close(a);
326 bsdtar_warnc(0, "%s", archive_error_string(a));
327 if (r <= ARCHIVE_WARN)
328 bsdtar->return_value = 1;
330 if (bsdtar->verbose > 2)
331 fprintf(stdout, "Archive Format: %s, Compression: %s\n",
332 archive_format_name(a), archive_compression_name(a));
334 archive_read_finish(a);
339 * Display information about the current file.
341 * The format here roughly duplicates the output of 'ls -l'.
342 * This is based on SUSv2, where 'tar tv' is documented as
343 * listing additional information in an "unspecified format,"
344 * and 'pax -l' is documented as using the same format as 'ls -l'.
347 list_item_verbose(struct bsdtar *bsdtar, FILE *out, struct archive_entry *entry)
349 const struct stat *st;
357 st = archive_entry_stat(entry);
360 * We avoid collecting the entire list in memory at once by
361 * listing things as we see them. However, that also means we can't
362 * just pre-compute the field widths. Instead, we start with guesses
363 * and just widen them as necessary. These numbers are completely
366 if (!bsdtar->u_width) {
368 bsdtar->gs_width = 13;
372 fprintf(out, "%s %d ",
373 archive_entry_strmode(entry),
374 (int)(st->st_nlink));
376 /* Use uname if it's present, else uid. */
377 p = archive_entry_uname(entry);
378 if ((p == NULL) || (*p == '\0')) {
379 sprintf(tmp, "%lu ", (unsigned long)st->st_uid);
383 if (w > bsdtar->u_width)
385 fprintf(out, "%-*s ", (int)bsdtar->u_width, p);
387 /* Use gname if it's present, else gid. */
388 p = archive_entry_gname(entry);
389 if (p != NULL && p[0] != '\0') {
390 fprintf(out, "%s", p);
393 sprintf(tmp, "%lu", (unsigned long)st->st_gid);
395 fprintf(out, "%s", tmp);
399 * Print device number or file size, right-aligned so as to make
400 * total width of group and devnum/filesize fields be gs_width.
401 * If gs_width is too small, grow it.
403 if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
404 sprintf(tmp, "%lu,%lu",
405 (unsigned long)major(st->st_rdev),
406 (unsigned long)minor(st->st_rdev)); /* ls(1) also casts here. */
409 * Note the use of platform-dependent macros to format
410 * the filesize here. We need the format string and the
411 * corresponding type for the cast.
413 sprintf(tmp, BSDTAR_FILESIZE_PRINTF,
414 (BSDTAR_FILESIZE_TYPE)st->st_size);
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 = (time_t)st->st_mtime;
422 #if defined(_WIN32) && !defined(__CYGWIN__)
423 /* Windows' strftime function does not support %e format. */
424 if (abs(tim - now) > (365/2)*86400)
425 fmt = bsdtar->day_first ? "%d %b %Y" : "%b %d %Y";
427 fmt = bsdtar->day_first ? "%d %b %H:%M" : "%b %d %H:%M";
429 if (abs(tim - now) > (365/2)*86400)
430 fmt = bsdtar->day_first ? "%e %b %Y" : "%b %e %Y";
432 fmt = bsdtar->day_first ? "%e %b %H:%M" : "%b %e %H:%M";
434 strftime(tmp, sizeof(tmp), fmt, localtime(&tim));
435 fprintf(out, " %s ", tmp);
436 safe_fprintf(out, "%s", archive_entry_pathname(entry));
438 /* Extra information for links. */
439 if (archive_entry_hardlink(entry)) /* Hard link */
440 safe_fprintf(out, " link to %s",
441 archive_entry_hardlink(entry));
442 else if (S_ISLNK(st->st_mode)) /* Symbolic link */
443 safe_fprintf(out, " -> %s", archive_entry_symlink(entry));