Merge from vendor branch LIBARCHIVE:
[dragonfly.git] / contrib / libarchive-2 / tar / read.c
CommitLineData
63551f52
PA
1/*-
2 * Copyright (c) 2003-2007 Tim Kientzle
3 * All rights reserved.
4 *
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.
13 *
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.
24 */
25
26#include "bsdtar_platform.h"
7186bf60 27__FBSDID("$FreeBSD: src/usr.bin/tar/read.c,v 1.33 2007/05/29 05:39:10 kientzle Exp $");
63551f52
PA
28
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
43
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
69
70#include "bsdtar.h"
71
72static void list_item_verbose(struct bsdtar *, FILE *,
73 struct archive_entry *);
74static void read_archive(struct bsdtar *bsdtar, char mode);
75
76void
77tar_mode_t(struct bsdtar *bsdtar)
78{
79 read_archive(bsdtar, 't');
80}
81
82void
83tar_mode_x(struct bsdtar *bsdtar)
84{
85 read_archive(bsdtar, 'x');
86}
87
88/*
89 * Handle 'x' and 't' modes.
90 */
91static void
92read_archive(struct bsdtar *bsdtar, char mode)
93{
94 FILE *out;
95 struct archive *a;
96 struct archive_entry *entry;
97 const struct stat *st;
98 int r;
99
100 while (*bsdtar->argv) {
101 include(bsdtar, *bsdtar->argv);
102 bsdtar->argv++;
103 }
104
105 if (bsdtar->names_from_file != NULL)
106 include_from_file(bsdtar, bsdtar->names_from_file);
107
108 a = archive_read_new();
109 if (bsdtar->compress_program != NULL)
110 archive_read_support_compression_program(a, bsdtar->compress_program);
111 else
112 archive_read_support_compression_all(a);
113 archive_read_support_format_all(a);
114 if (archive_read_open_file(a, bsdtar->filename,
115 bsdtar->bytes_per_block != 0 ? bsdtar->bytes_per_block :
116 DEFAULT_BYTES_PER_BLOCK))
117 bsdtar_errc(bsdtar, 1, 0, "Error opening archive: %s",
118 archive_error_string(a));
119
120 do_chdir(bsdtar);
121 for (;;) {
122 /* Support --fast-read option */
123 if (bsdtar->option_fast_read &&
124 unmatched_inclusions(bsdtar) == 0)
125 break;
126
127 r = archive_read_next_header(a, &entry);
128 if (r == ARCHIVE_EOF)
129 break;
130 if (r < ARCHIVE_OK)
131 bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(a));
132 if (r == ARCHIVE_RETRY) {
133 /* Retryable error: try again */
134 bsdtar_warnc(bsdtar, 0, "Retrying...");
135 continue;
136 }
137 if (r != ARCHIVE_OK) {
138 bsdtar->return_value = 1;
139 break;
140 }
141
142 /*
143 * Exclude entries that are too old.
144 */
145 st = archive_entry_stat(entry);
146 if (bsdtar->newer_ctime_sec > 0) {
147 if (st->st_ctime < bsdtar->newer_ctime_sec)
148 continue; /* Too old, skip it. */
149 if (st->st_ctime == bsdtar->newer_ctime_sec
150 && ARCHIVE_STAT_CTIME_NANOS(st)
151 <= bsdtar->newer_ctime_nsec)
152 continue; /* Too old, skip it. */
153 }
154 if (bsdtar->newer_mtime_sec > 0) {
155 if (st->st_mtime < bsdtar->newer_mtime_sec)
156 continue; /* Too old, skip it. */
157 if (st->st_mtime == bsdtar->newer_mtime_sec
158 && ARCHIVE_STAT_MTIME_NANOS(st)
159 <= bsdtar->newer_mtime_nsec)
160 continue; /* Too old, skip it. */
161 }
162
163 /*
164 * Note that pattern exclusions are checked before
165 * pathname rewrites are handled. This gives more
166 * control over exclusions, since rewrites always lose
167 * information. (For example, consider a rewrite
168 * s/foo[0-9]/foo/. If we check exclusions after the
169 * rewrite, there would be no way to exclude foo1/bar
170 * while allowing foo2/bar.)
171 */
172 if (excluded(bsdtar, archive_entry_pathname(entry)))
173 continue; /* Excluded by a pattern test. */
174
175 /*
176 * Modify the pathname as requested by the user. We
177 * do this for -t as well to give users a way to
178 * preview the effects of their rewrites. We also do
179 * this before extraction security checks (including
180 * leading '/' removal). Note that some rewrite
181 * failures prevent extraction.
182 */
183 if (edit_pathname(bsdtar, entry))
184 continue; /* Excluded by a rewrite failure. */
185
186 if (mode == 't') {
187 /* Perversely, gtar uses -O to mean "send to stderr"
188 * when used with -t. */
189 out = bsdtar->option_stdout ? stderr : stdout;
190
191 if (bsdtar->verbose < 2)
192 safe_fprintf(out, "%s",
193 archive_entry_pathname(entry));
194 else
195 list_item_verbose(bsdtar, out, entry);
196 fflush(out);
197 r = archive_read_data_skip(a);
198 if (r == ARCHIVE_WARN) {
199 fprintf(out, "\n");
200 bsdtar_warnc(bsdtar, 0, "%s",
201 archive_error_string(a));
202 }
203 if (r == ARCHIVE_RETRY) {
204 fprintf(out, "\n");
205 bsdtar_warnc(bsdtar, 0, "%s",
206 archive_error_string(a));
207 }
208 if (r == ARCHIVE_FATAL) {
209 fprintf(out, "\n");
210 bsdtar_warnc(bsdtar, 0, "%s",
211 archive_error_string(a));
eb792538 212 bsdtar->return_value = 1;
63551f52
PA
213 break;
214 }
215 fprintf(out, "\n");
216 } else {
217 if (bsdtar->option_interactive &&
218 !yes("extract '%s'", archive_entry_pathname(entry)))
219 continue;
220
221 /*
222 * Format here is from SUSv2, including the
223 * deferred '\n'.
224 */
225 if (bsdtar->verbose) {
226 safe_fprintf(stderr, "x %s",
227 archive_entry_pathname(entry));
228 fflush(stderr);
229 }
230 if (bsdtar->option_stdout)
231 r = archive_read_data_into_fd(a, 1);
232 else
233 r = archive_read_extract(a, entry,
234 bsdtar->extract_flags);
235 if (r != ARCHIVE_OK) {
236 if (!bsdtar->verbose)
237 safe_fprintf(stderr, "%s",
238 archive_entry_pathname(entry));
239 safe_fprintf(stderr, ": %s",
240 archive_error_string(a));
241 if (!bsdtar->verbose)
242 fprintf(stderr, "\n");
243 bsdtar->return_value = 1;
244 }
245 if (bsdtar->verbose)
246 fprintf(stderr, "\n");
247 if (r == ARCHIVE_FATAL)
248 break;
249 }
250 }
251
252 if (bsdtar->verbose > 2)
253 fprintf(stdout, "Archive Format: %s, Compression: %s\n",
254 archive_format_name(a), archive_compression_name(a));
255
256 archive_read_finish(a);
257}
258
259
260/*
261 * Display information about the current file.
262 *
263 * The format here roughly duplicates the output of 'ls -l'.
264 * This is based on SUSv2, where 'tar tv' is documented as
265 * listing additional information in an "unspecified format,"
266 * and 'pax -l' is documented as using the same format as 'ls -l'.
267 */
268static void
269list_item_verbose(struct bsdtar *bsdtar, FILE *out, struct archive_entry *entry)
270{
271 const struct stat *st;
272 char tmp[100];
273 size_t w;
274 const char *p;
275 const char *fmt;
276 time_t tim;
277 static time_t now;
278
279 st = archive_entry_stat(entry);
280
281 /*
282 * We avoid collecting the entire list in memory at once by
283 * listing things as we see them. However, that also means we can't
284 * just pre-compute the field widths. Instead, we start with guesses
285 * and just widen them as necessary. These numbers are completely
286 * arbitrary.
287 */
288 if (!bsdtar->u_width) {
289 bsdtar->u_width = 6;
290 bsdtar->gs_width = 13;
291 }
292 if (!now)
293 time(&now);
294 bsdtar_strmode(entry, tmp);
295 fprintf(out, "%s %d ", tmp, (int)(st->st_nlink));
296
297 /* Use uname if it's present, else uid. */
298 p = archive_entry_uname(entry);
299 if ((p == NULL) || (*p == '\0')) {
300 sprintf(tmp, "%lu ", (unsigned long)st->st_uid);
301 p = tmp;
302 }
303 w = strlen(p);
304 if (w > bsdtar->u_width)
305 bsdtar->u_width = w;
306 fprintf(out, "%-*s ", (int)bsdtar->u_width, p);
307
308 /* Use gname if it's present, else gid. */
309 p = archive_entry_gname(entry);
310 if (p != NULL && p[0] != '\0') {
311 fprintf(out, "%s", p);
312 w = strlen(p);
313 } else {
314 sprintf(tmp, "%lu", (unsigned long)st->st_gid);
315 w = strlen(tmp);
316 fprintf(out, "%s", tmp);
317 }
318
319 /*
320 * Print device number or file size, right-aligned so as to make
321 * total width of group and devnum/filesize fields be gs_width.
322 * If gs_width is too small, grow it.
323 */
324 if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
325 sprintf(tmp, "%lu,%lu",
326 (unsigned long)major(st->st_rdev),
327 (unsigned long)minor(st->st_rdev)); /* ls(1) also casts here. */
328 } else {
329 /*
330 * Note the use of platform-dependent macros to format
331 * the filesize here. We need the format string and the
332 * corresponding type for the cast.
333 */
334 sprintf(tmp, BSDTAR_FILESIZE_PRINTF,
335 (BSDTAR_FILESIZE_TYPE)st->st_size);
336 }
337 if (w + strlen(tmp) >= bsdtar->gs_width)
338 bsdtar->gs_width = w+strlen(tmp)+1;
339 fprintf(out, "%*s", (int)(bsdtar->gs_width - w), tmp);
340
341 /* Format the time using 'ls -l' conventions. */
342 tim = (time_t)st->st_mtime;
343 if (abs(tim - now) > (365/2)*86400)
344 fmt = bsdtar->day_first ? "%e %b %Y" : "%b %e %Y";
345 else
346 fmt = bsdtar->day_first ? "%e %b %R" : "%b %e %R";
347 strftime(tmp, sizeof(tmp), fmt, localtime(&tim));
348 fprintf(out, " %s ", tmp);
349 safe_fprintf(out, "%s", archive_entry_pathname(entry));
350
351 /* Extra information for links. */
352 if (archive_entry_hardlink(entry)) /* Hard link */
353 safe_fprintf(out, " link to %s",
354 archive_entry_hardlink(entry));
355 else if (S_ISLNK(st->st_mode)) /* Symbolic link */
356 safe_fprintf(out, " -> %s", archive_entry_symlink(entry));
357}