Import libarchive-2.5.5.
[dragonfly.git] / contrib / libarchive-2 / tar / read.c
blobfbb82533a72e326f918c1d5a2d74215886131364
1 /*-
2 * Copyright (c) 2003-2007 Tim Kientzle
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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: src/usr.bin/tar/read.c,v 1.38 2008/05/26 17:10:10 kientzle Exp $");
29 #ifdef HAVE_SYS_TYPES_H
30 #include <sys/types.h>
31 #endif
32 #ifdef MAJOR_IN_MKDEV
33 #include <sys/mkdev.h>
34 #elif defined(MAJOR_IN_SYSMACROS)
35 #include <sys/sysmacros.h>
36 #endif
37 #ifdef HAVE_SYS_PARAM_H
38 #include <sys/param.h>
39 #endif
40 #ifdef HAVE_SYS_STAT_H
41 #include <sys/stat.h>
42 #endif
44 #ifdef HAVE_ERRNO_H
45 #include <errno.h>
46 #endif
47 #ifdef HAVE_GRP_H
48 #include <grp.h>
49 #endif
50 #ifdef HAVE_LIMITS_H
51 #include <limits.h>
52 #endif
53 #ifdef HAVE_PWD_H
54 #include <pwd.h>
55 #endif
56 #include <stdio.h>
57 #ifdef HAVE_STDLIB_H
58 #include <stdlib.h>
59 #endif
60 #ifdef HAVE_STRING_H
61 #include <string.h>
62 #endif
63 #ifdef HAVE_TIME_H
64 #include <time.h>
65 #endif
66 #ifdef HAVE_UNISTD_H
67 #include <unistd.h>
68 #endif
70 #include "bsdtar.h"
72 static void list_item_verbose(struct bsdtar *, FILE *,
73 struct archive_entry *);
74 static void read_archive(struct bsdtar *bsdtar, char mode);
76 void
77 tar_mode_t(struct bsdtar *bsdtar)
79 read_archive(bsdtar, 't');
80 unmatched_inclusions_warn(bsdtar, "Not found in archive");
83 void
84 tar_mode_x(struct bsdtar *bsdtar)
86 /* We want to catch SIGINFO and SIGUSR1. */
87 siginfo_init(bsdtar);
89 read_archive(bsdtar, 'x');
91 unmatched_inclusions_warn(bsdtar, "Not found in archive");
92 /* Restore old SIGINFO + SIGUSR1 handlers. */
93 siginfo_done(bsdtar);
96 static void
97 progress_func(void * cookie)
99 struct bsdtar * bsdtar = cookie;
101 siginfo_printinfo(bsdtar, 0);
105 * Handle 'x' and 't' modes.
107 static void
108 read_archive(struct bsdtar *bsdtar, char mode)
110 FILE *out;
111 struct archive *a;
112 struct archive_entry *entry;
113 const struct stat *st;
114 int r;
116 while (*bsdtar->argv) {
117 include(bsdtar, *bsdtar->argv);
118 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);
127 else
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));
136 do_chdir(bsdtar);
138 if (mode == 'x') {
139 /* Set an extract callback so that we can handle SIGINFO. */
140 archive_read_extract_set_progress_callback(a, progress_func,
141 bsdtar);
144 if (mode == 'x' && bsdtar->option_chroot) {
145 #if HAVE_CHROOT
146 if (chroot(".") != 0)
147 bsdtar_errc(bsdtar, 1, errno, "Can't chroot to \".\"");
148 #else
149 bsdtar_errc(bsdtar, 1, 0,
150 "chroot isn't supported on this platform");
151 #endif
154 for (;;) {
155 /* Support --fast-read option */
156 if (bsdtar->option_fast_read &&
157 unmatched_inclusions(bsdtar) == 0)
158 break;
160 r = archive_read_next_header(a, &entry);
161 if (r == ARCHIVE_EOF)
162 break;
163 if (r < ARCHIVE_OK)
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...");
170 continue;
172 if (r == ARCHIVE_FATAL)
173 break;
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 * Modify the pathname as requested by the user. We
215 * do this for -t as well to give users a way to
216 * preview the effects of their rewrites. We also do
217 * this before extraction security checks (including
218 * leading '/' removal). Note that some rewrite
219 * failures prevent extraction.
221 if (edit_pathname(bsdtar, entry))
222 continue; /* Excluded by a rewrite failure. */
224 if (mode == 't') {
225 /* Perversely, gtar uses -O to mean "send to stderr"
226 * when used with -t. */
227 out = bsdtar->option_stdout ? stderr : stdout;
229 if (bsdtar->verbose < 2)
230 safe_fprintf(out, "%s",
231 archive_entry_pathname(entry));
232 else
233 list_item_verbose(bsdtar, out, entry);
234 fflush(out);
235 r = archive_read_data_skip(a);
236 if (r == ARCHIVE_WARN) {
237 fprintf(out, "\n");
238 bsdtar_warnc(bsdtar, 0, "%s",
239 archive_error_string(a));
241 if (r == ARCHIVE_RETRY) {
242 fprintf(out, "\n");
243 bsdtar_warnc(bsdtar, 0, "%s",
244 archive_error_string(a));
246 if (r == ARCHIVE_FATAL) {
247 fprintf(out, "\n");
248 bsdtar_warnc(bsdtar, 0, "%s",
249 archive_error_string(a));
250 bsdtar->return_value = 1;
251 break;
253 fprintf(out, "\n");
254 } else {
255 if (bsdtar->option_interactive &&
256 !yes("extract '%s'", archive_entry_pathname(entry)))
257 continue;
260 * Format here is from SUSv2, including the
261 * deferred '\n'.
263 if (bsdtar->verbose) {
264 safe_fprintf(stderr, "x %s",
265 archive_entry_pathname(entry));
266 fflush(stderr);
269 /* Tell the SIGINFO-handler code what we're doing. */
270 siginfo_setinfo(bsdtar, "extracting",
271 archive_entry_pathname(entry), 0);
272 siginfo_printinfo(bsdtar, 0);
274 if (bsdtar->option_stdout)
275 r = archive_read_data_into_fd(a, 1);
276 else
277 r = archive_read_extract(a, entry,
278 bsdtar->extract_flags);
279 if (r != ARCHIVE_OK) {
280 if (!bsdtar->verbose)
281 safe_fprintf(stderr, "%s",
282 archive_entry_pathname(entry));
283 safe_fprintf(stderr, ": %s",
284 archive_error_string(a));
285 if (!bsdtar->verbose)
286 fprintf(stderr, "\n");
287 bsdtar->return_value = 1;
289 if (bsdtar->verbose)
290 fprintf(stderr, "\n");
291 if (r == ARCHIVE_FATAL)
292 break;
296 if (bsdtar->verbose > 2)
297 fprintf(stdout, "Archive Format: %s, Compression: %s\n",
298 archive_format_name(a), archive_compression_name(a));
300 archive_read_finish(a);
305 * Display information about the current file.
307 * The format here roughly duplicates the output of 'ls -l'.
308 * This is based on SUSv2, where 'tar tv' is documented as
309 * listing additional information in an "unspecified format,"
310 * and 'pax -l' is documented as using the same format as 'ls -l'.
312 static void
313 list_item_verbose(struct bsdtar *bsdtar, FILE *out, struct archive_entry *entry)
315 const struct stat *st;
316 char tmp[100];
317 size_t w;
318 const char *p;
319 const char *fmt;
320 time_t tim;
321 static time_t now;
323 st = archive_entry_stat(entry);
326 * We avoid collecting the entire list in memory at once by
327 * listing things as we see them. However, that also means we can't
328 * just pre-compute the field widths. Instead, we start with guesses
329 * and just widen them as necessary. These numbers are completely
330 * arbitrary.
332 if (!bsdtar->u_width) {
333 bsdtar->u_width = 6;
334 bsdtar->gs_width = 13;
336 if (!now)
337 time(&now);
338 fprintf(out, "%s %d ",
339 archive_entry_strmode(entry),
340 (int)(st->st_nlink));
342 /* Use uname if it's present, else uid. */
343 p = archive_entry_uname(entry);
344 if ((p == NULL) || (*p == '\0')) {
345 sprintf(tmp, "%lu ", (unsigned long)st->st_uid);
346 p = tmp;
348 w = strlen(p);
349 if (w > bsdtar->u_width)
350 bsdtar->u_width = w;
351 fprintf(out, "%-*s ", (int)bsdtar->u_width, p);
353 /* Use gname if it's present, else gid. */
354 p = archive_entry_gname(entry);
355 if (p != NULL && p[0] != '\0') {
356 fprintf(out, "%s", p);
357 w = strlen(p);
358 } else {
359 sprintf(tmp, "%lu", (unsigned long)st->st_gid);
360 w = strlen(tmp);
361 fprintf(out, "%s", tmp);
365 * Print device number or file size, right-aligned so as to make
366 * total width of group and devnum/filesize fields be gs_width.
367 * If gs_width is too small, grow it.
369 if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
370 sprintf(tmp, "%lu,%lu",
371 (unsigned long)major(st->st_rdev),
372 (unsigned long)minor(st->st_rdev)); /* ls(1) also casts here. */
373 } else {
375 * Note the use of platform-dependent macros to format
376 * the filesize here. We need the format string and the
377 * corresponding type for the cast.
379 sprintf(tmp, BSDTAR_FILESIZE_PRINTF,
380 (BSDTAR_FILESIZE_TYPE)st->st_size);
382 if (w + strlen(tmp) >= bsdtar->gs_width)
383 bsdtar->gs_width = w+strlen(tmp)+1;
384 fprintf(out, "%*s", (int)(bsdtar->gs_width - w), tmp);
386 /* Format the time using 'ls -l' conventions. */
387 tim = (time_t)st->st_mtime;
388 if (abs(tim - now) > (365/2)*86400)
389 fmt = bsdtar->day_first ? "%e %b %Y" : "%b %e %Y";
390 else
391 fmt = bsdtar->day_first ? "%e %b %H:%M" : "%b %e %H:%M";
392 strftime(tmp, sizeof(tmp), fmt, localtime(&tim));
393 fprintf(out, " %s ", tmp);
394 safe_fprintf(out, "%s", archive_entry_pathname(entry));
396 /* Extra information for links. */
397 if (archive_entry_hardlink(entry)) /* Hard link */
398 safe_fprintf(out, " link to %s",
399 archive_entry_hardlink(entry));
400 else if (S_ISLNK(st->st_mode)) /* Symbolic link */
401 safe_fprintf(out, " -> %s", archive_entry_symlink(entry));