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, struct archive *);
80 static int unmatched_inclusions_warn(struct archive *matching, const char *);
84 tar_mode_t(struct bsdtar *bsdtar)
86 read_archive(bsdtar, 't', NULL);
87 if (unmatched_inclusions_warn(bsdtar->matching,
88 "Not found in archive") != 0)
89 bsdtar->return_value = 1;
93 tar_mode_x(struct bsdtar *bsdtar)
95 struct archive *writer;
97 writer = archive_write_disk_new();
99 lafe_errc(1, ENOMEM, "Cannot allocate disk writer object");
100 if (!bsdtar->option_numeric_owner)
101 archive_write_disk_set_standard_lookup(writer);
102 archive_write_disk_set_options(writer, bsdtar->extract_flags);
104 read_archive(bsdtar, 'x', writer);
106 if (unmatched_inclusions_warn(bsdtar->matching,
107 "Not found in archive") != 0)
108 bsdtar->return_value = 1;
109 archive_write_free(writer);
113 progress_func(void *cookie)
115 struct progress_data *progress_data = cookie;
116 struct bsdtar *bsdtar = progress_data->bsdtar;
117 struct archive *a = progress_data->archive;
118 struct archive_entry *entry = progress_data->entry;
119 uint64_t comp, uncomp;
126 fprintf(stderr, "\n");
128 comp = archive_filter_bytes(a, -1);
129 uncomp = archive_filter_bytes(a, 0);
133 compression = (int)((uncomp - comp) * 100 / uncomp);
135 "In: %s bytes, compression %d%%;",
136 tar_i64toa(comp), compression);
137 fprintf(stderr, " Out: %d files, %s bytes\n",
138 archive_file_count(a), tar_i64toa(uncomp));
141 safe_fprintf(stderr, "Current: %s",
142 archive_entry_pathname(entry));
143 fprintf(stderr, " (%s bytes)\n",
144 tar_i64toa(archive_entry_size(entry)));
149 * Handle 'x' and 't' modes.
152 read_archive(struct bsdtar *bsdtar, char mode, struct archive *writer)
154 struct progress_data progress_data;
157 struct archive_entry *entry;
158 const char *reader_options;
161 while (*bsdtar->argv) {
162 if (archive_match_include_pattern(bsdtar->matching,
163 *bsdtar->argv) != ARCHIVE_OK)
164 lafe_errc(1, 0, "Error inclusion pattern: %s",
165 archive_error_string(bsdtar->matching));
169 if (bsdtar->names_from_file != NULL)
170 if (archive_match_include_pattern_from_file(
171 bsdtar->matching, bsdtar->names_from_file,
172 bsdtar->option_null) != ARCHIVE_OK)
173 lafe_errc(1, 0, "Error inclusion pattern: %s",
174 archive_error_string(bsdtar->matching));
176 a = archive_read_new();
177 if (cset_read_support_filter_program(bsdtar->cset, a) == 0)
178 archive_read_support_filter_all(a);
179 archive_read_support_format_all(a);
181 reader_options = getenv(ENV_READER_OPTIONS);
182 if (reader_options != NULL) {
184 /* Set default read options. */
185 p = malloc(sizeof(IGNORE_WRONG_MODULE_NAME)
186 + strlen(reader_options) + 1);
188 lafe_errc(1, errno, "Out of memory");
189 /* Prepend magic code to ignore options for
190 * a format or modules which are not added to
191 * the archive read object. */
192 strncpy(p, IGNORE_WRONG_MODULE_NAME,
193 sizeof(IGNORE_WRONG_MODULE_NAME) -1);
194 strcpy(p + sizeof(IGNORE_WRONG_MODULE_NAME) -1, reader_options);
195 r = archive_read_set_options(a, p);
197 if (r == ARCHIVE_FATAL)
198 lafe_errc(1, 0, "%s", archive_error_string(a));
200 archive_clear_error(a);
202 if (ARCHIVE_OK != archive_read_set_options(a, bsdtar->option_options))
203 lafe_errc(1, 0, "%s", archive_error_string(a));
204 if (archive_read_open_filename(a, bsdtar->filename,
205 bsdtar->bytes_per_block))
206 lafe_errc(1, 0, "Error opening archive: %s",
207 archive_error_string(a));
212 /* Set an extract callback so that we can handle SIGINFO. */
213 progress_data.bsdtar = bsdtar;
214 progress_data.archive = a;
215 archive_read_extract_set_progress_callback(a, progress_func,
219 if (mode == 'x' && bsdtar->option_chroot) {
221 if (chroot(".") != 0)
222 lafe_errc(1, errno, "Can't chroot to \".\"");
225 "chroot isn't supported on this platform");
230 /* Support --fast-read option */
231 if (bsdtar->option_fast_read &&
232 archive_match_path_unmatched_inclusions(bsdtar->matching) == 0)
235 r = archive_read_next_header(a, &entry);
236 progress_data.entry = entry;
237 if (r == ARCHIVE_EOF)
240 lafe_warnc(0, "%s", archive_error_string(a));
241 if (r <= ARCHIVE_WARN)
242 bsdtar->return_value = 1;
243 if (r == ARCHIVE_RETRY) {
244 /* Retryable error: try again */
245 lafe_warnc(0, "Retrying...");
248 if (r == ARCHIVE_FATAL)
251 if (bsdtar->uid >= 0) {
252 archive_entry_set_uid(entry, bsdtar->uid);
253 archive_entry_set_uname(entry, NULL);
255 if (bsdtar->gid >= 0) {
256 archive_entry_set_gid(entry, bsdtar->gid);
257 archive_entry_set_gname(entry, NULL);
260 archive_entry_set_uname(entry, bsdtar->uname);
262 archive_entry_set_gname(entry, bsdtar->gname);
265 * Note that pattern exclusions are checked before
266 * pathname rewrites are handled. This gives more
267 * control over exclusions, since rewrites always lose
268 * information. (For example, consider a rewrite
269 * s/foo[0-9]/foo/. If we check exclusions after the
270 * rewrite, there would be no way to exclude foo1/bar
271 * while allowing foo2/bar.)
273 if (archive_match_excluded(bsdtar->matching, entry))
274 continue; /* Excluded by a pattern test. */
277 /* Perversely, gtar uses -O to mean "send to stderr"
278 * when used with -t. */
279 out = bsdtar->option_stdout ? stderr : stdout;
282 * TODO: Provide some reasonable way to
283 * preview rewrites. gtar always displays
284 * the unedited path in -t output, which means
285 * you cannot easily preview rewrites.
287 if (bsdtar->verbose < 2)
288 safe_fprintf(out, "%s",
289 archive_entry_pathname(entry));
291 list_item_verbose(bsdtar, out, entry);
293 r = archive_read_data_skip(a);
294 if (r == ARCHIVE_WARN) {
297 archive_error_string(a));
299 if (r == ARCHIVE_RETRY) {
302 archive_error_string(a));
304 if (r == ARCHIVE_FATAL) {
307 archive_error_string(a));
308 bsdtar->return_value = 1;
313 /* Note: some rewrite failures prevent extraction. */
314 if (edit_pathname(bsdtar, entry))
315 continue; /* Excluded by a rewrite failure. */
317 if (bsdtar->option_interactive &&
318 !yes("extract '%s'", archive_entry_pathname(entry)))
322 * Format here is from SUSv2, including the
325 if (bsdtar->verbose) {
326 safe_fprintf(stderr, "x %s",
327 archive_entry_pathname(entry));
331 /* TODO siginfo_printinfo(bsdtar, 0); */
333 if (bsdtar->option_stdout)
334 r = archive_read_data_into_fd(a, 1);
336 r = archive_read_extract2(a, entry, writer);
337 if (r != ARCHIVE_OK) {
338 if (!bsdtar->verbose)
339 safe_fprintf(stderr, "%s",
340 archive_entry_pathname(entry));
341 safe_fprintf(stderr, ": %s",
342 archive_error_string(a));
343 if (!bsdtar->verbose)
344 fprintf(stderr, "\n");
345 bsdtar->return_value = 1;
348 fprintf(stderr, "\n");
349 if (r == ARCHIVE_FATAL)
355 r = archive_read_close(a);
357 lafe_warnc(0, "%s", archive_error_string(a));
358 if (r <= ARCHIVE_WARN)
359 bsdtar->return_value = 1;
361 if (bsdtar->verbose > 2)
362 fprintf(stdout, "Archive Format: %s, Compression: %s\n",
363 archive_format_name(a), archive_filter_name(a, 0));
365 archive_read_free(a);
370 * Display information about the current file.
372 * The format here roughly duplicates the output of 'ls -l'.
373 * This is based on SUSv2, where 'tar tv' is documented as
374 * listing additional information in an "unspecified format,"
375 * and 'pax -l' is documented as using the same format as 'ls -l'.
378 list_item_verbose(struct bsdtar *bsdtar, FILE *out, struct archive_entry *entry)
388 * We avoid collecting the entire list in memory at once by
389 * listing things as we see them. However, that also means we can't
390 * just pre-compute the field widths. Instead, we start with guesses
391 * and just widen them as necessary. These numbers are completely
394 if (!bsdtar->u_width) {
396 bsdtar->gs_width = 13;
400 fprintf(out, "%s %d ",
401 archive_entry_strmode(entry),
402 archive_entry_nlink(entry));
404 /* Use uname if it's present, else uid. */
405 p = archive_entry_uname(entry);
406 if ((p == NULL) || (*p == '\0')) {
408 (unsigned long)archive_entry_uid(entry));
412 if (w > bsdtar->u_width)
414 fprintf(out, "%-*s ", (int)bsdtar->u_width, p);
416 /* Use gname if it's present, else gid. */
417 p = archive_entry_gname(entry);
418 if (p != NULL && p[0] != '\0') {
419 fprintf(out, "%s", p);
423 (unsigned long)archive_entry_gid(entry));
425 fprintf(out, "%s", tmp);
429 * Print device number or file size, right-aligned so as to make
430 * total width of group and devnum/filesize fields be gs_width.
431 * If gs_width is too small, grow it.
433 if (archive_entry_filetype(entry) == AE_IFCHR
434 || archive_entry_filetype(entry) == AE_IFBLK) {
435 sprintf(tmp, "%lu,%lu",
436 (unsigned long)archive_entry_rdevmajor(entry),
437 (unsigned long)archive_entry_rdevminor(entry));
439 strcpy(tmp, tar_i64toa(archive_entry_size(entry)));
441 if (w + strlen(tmp) >= bsdtar->gs_width)
442 bsdtar->gs_width = w+strlen(tmp)+1;
443 fprintf(out, "%*s", (int)(bsdtar->gs_width - w), tmp);
445 /* Format the time using 'ls -l' conventions. */
446 tim = archive_entry_mtime(entry);
447 #define HALF_YEAR (time_t)365 * 86400 / 2
448 #if defined(_WIN32) && !defined(__CYGWIN__)
449 #define DAY_FMT "%d" /* Windows' strftime function does not support %e format. */
451 #define DAY_FMT "%e" /* Day number without leading zeros */
453 if (tim < now - HALF_YEAR || tim > now + HALF_YEAR)
454 fmt = bsdtar->day_first ? DAY_FMT " %b %Y" : "%b " DAY_FMT " %Y";
456 fmt = bsdtar->day_first ? DAY_FMT " %b %H:%M" : "%b " DAY_FMT " %H:%M";
457 strftime(tmp, sizeof(tmp), fmt, localtime(&tim));
458 fprintf(out, " %s ", tmp);
459 safe_fprintf(out, "%s", archive_entry_pathname(entry));
461 /* Extra information for links. */
462 if (archive_entry_hardlink(entry)) /* Hard link */
463 safe_fprintf(out, " link to %s",
464 archive_entry_hardlink(entry));
465 else if (archive_entry_symlink(entry)) /* Symbolic link */
466 safe_fprintf(out, " -> %s", archive_entry_symlink(entry));
470 unmatched_inclusions_warn(struct archive *matching, const char *msg)
475 if (matching == NULL)
478 while ((r = archive_match_path_unmatched_inclusions_next(
479 matching, &p)) == ARCHIVE_OK)
480 lafe_warnc(0, "%s: %s", p, msg);
481 if (r == ARCHIVE_FATAL)
482 lafe_errc(1, errno, "Out of memory");
484 return (archive_match_path_unmatched_inclusions(matching));