xref: /dragonfly/contrib/libarchive/tar/read.c (revision dca3c15d)
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"
27 __FBSDID("$FreeBSD: src/usr.bin/tar/read.c,v 1.40 2008/08/21 06:41:14 kientzle Exp $");
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 
72 static void	list_item_verbose(struct bsdtar *, FILE *,
73 		    struct archive_entry *);
74 static void	read_archive(struct bsdtar *bsdtar, char mode);
75 
76 void
77 tar_mode_t(struct bsdtar *bsdtar)
78 {
79 	read_archive(bsdtar, 't');
80 	unmatched_inclusions_warn(bsdtar, "Not found in archive");
81 }
82 
83 void
84 tar_mode_x(struct bsdtar *bsdtar)
85 {
86 	/* We want to catch SIGINFO and SIGUSR1. */
87 	siginfo_init(bsdtar);
88 
89 	read_archive(bsdtar, 'x');
90 
91 	unmatched_inclusions_warn(bsdtar, "Not found in archive");
92 	/* Restore old SIGINFO + SIGUSR1 handlers. */
93 	siginfo_done(bsdtar);
94 }
95 
96 static void
97 progress_func(void * cookie)
98 {
99 	struct bsdtar * bsdtar = cookie;
100 
101 	siginfo_printinfo(bsdtar, 0);
102 }
103 
104 /*
105  * Handle 'x' and 't' modes.
106  */
107 static void
108 read_archive(struct bsdtar *bsdtar, char mode)
109 {
110 	FILE			 *out;
111 	struct archive		 *a;
112 	struct archive_entry	 *entry;
113 	const struct stat	 *st;
114 	int			  r;
115 
116 	while (*bsdtar->argv) {
117 		include(bsdtar, *bsdtar->argv);
118 		bsdtar->argv++;
119 	}
120 
121 	if (bsdtar->names_from_file != NULL)
122 		include_from_file(bsdtar, bsdtar->names_from_file);
123 
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_OK != archive_read_set_options(a, bsdtar->option_options))
131 		bsdtar_errc(bsdtar, 1, 0, archive_error_string(a));
132 	if (archive_read_open_file(a, bsdtar->filename,
133 	    bsdtar->bytes_per_block != 0 ? bsdtar->bytes_per_block :
134 	    DEFAULT_BYTES_PER_BLOCK))
135 		bsdtar_errc(bsdtar, 1, 0, "Error opening archive: %s",
136 		    archive_error_string(a));
137 
138 	do_chdir(bsdtar);
139 
140 	if (mode == 'x') {
141 		/* Set an extract callback so that we can handle SIGINFO. */
142 		archive_read_extract_set_progress_callback(a, progress_func,
143 		    bsdtar);
144 	}
145 
146 	if (mode == 'x' && bsdtar->option_chroot) {
147 #if HAVE_CHROOT
148 		if (chroot(".") != 0)
149 			bsdtar_errc(bsdtar, 1, errno, "Can't chroot to \".\"");
150 #else
151 		bsdtar_errc(bsdtar, 1, 0,
152 		    "chroot isn't supported on this platform");
153 #endif
154 	}
155 
156 	for (;;) {
157 		/* Support --fast-read option */
158 		if (bsdtar->option_fast_read &&
159 		    unmatched_inclusions(bsdtar) == 0)
160 			break;
161 
162 		r = archive_read_next_header(a, &entry);
163 		if (r == ARCHIVE_EOF)
164 			break;
165 		if (r < ARCHIVE_OK)
166 			bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(a));
167 		if (r <= ARCHIVE_WARN)
168 			bsdtar->return_value = 1;
169 		if (r == ARCHIVE_RETRY) {
170 			/* Retryable error: try again */
171 			bsdtar_warnc(bsdtar, 0, "Retrying...");
172 			continue;
173 		}
174 		if (r == ARCHIVE_FATAL)
175 			break;
176 
177 		if (bsdtar->option_numeric_owner) {
178 			archive_entry_set_uname(entry, NULL);
179 			archive_entry_set_gname(entry, NULL);
180 		}
181 
182 		/*
183 		 * Exclude entries that are too old.
184 		 */
185 		st = archive_entry_stat(entry);
186 		if (bsdtar->newer_ctime_sec > 0) {
187 			if (st->st_ctime < bsdtar->newer_ctime_sec)
188 				continue; /* Too old, skip it. */
189 			if (st->st_ctime == bsdtar->newer_ctime_sec
190 			    && ARCHIVE_STAT_CTIME_NANOS(st)
191 			    <= bsdtar->newer_ctime_nsec)
192 				continue; /* Too old, skip it. */
193 		}
194 		if (bsdtar->newer_mtime_sec > 0) {
195 			if (st->st_mtime < bsdtar->newer_mtime_sec)
196 				continue; /* Too old, skip it. */
197 			if (st->st_mtime == bsdtar->newer_mtime_sec
198 			    && ARCHIVE_STAT_MTIME_NANOS(st)
199 			    <= bsdtar->newer_mtime_nsec)
200 				continue; /* Too old, skip it. */
201 		}
202 
203 		/*
204 		 * Note that pattern exclusions are checked before
205 		 * pathname rewrites are handled.  This gives more
206 		 * control over exclusions, since rewrites always lose
207 		 * information.  (For example, consider a rewrite
208 		 * s/foo[0-9]/foo/.  If we check exclusions after the
209 		 * rewrite, there would be no way to exclude foo1/bar
210 		 * while allowing foo2/bar.)
211 		 */
212 		if (excluded(bsdtar, archive_entry_pathname(entry)))
213 			continue; /* Excluded by a pattern test. */
214 
215 		if (mode == 't') {
216 			/* Perversely, gtar uses -O to mean "send to stderr"
217 			 * when used with -t. */
218 			out = bsdtar->option_stdout ? stderr : stdout;
219 
220 			/*
221 			 * TODO: Provide some reasonable way to
222 			 * preview rewrites.  gtar always displays
223 			 * the unedited path in -t output, which means
224 			 * you cannot easily preview rewrites.
225 			 */
226 			if (bsdtar->verbose < 2)
227 				safe_fprintf(out, "%s",
228 				    archive_entry_pathname(entry));
229 			else
230 				list_item_verbose(bsdtar, out, entry);
231 			fflush(out);
232 			r = archive_read_data_skip(a);
233 			if (r == ARCHIVE_WARN) {
234 				fprintf(out, "\n");
235 				bsdtar_warnc(bsdtar, 0, "%s",
236 				    archive_error_string(a));
237 			}
238 			if (r == ARCHIVE_RETRY) {
239 				fprintf(out, "\n");
240 				bsdtar_warnc(bsdtar, 0, "%s",
241 				    archive_error_string(a));
242 			}
243 			if (r == ARCHIVE_FATAL) {
244 				fprintf(out, "\n");
245 				bsdtar_warnc(bsdtar, 0, "%s",
246 				    archive_error_string(a));
247 				bsdtar->return_value = 1;
248 				break;
249 			}
250 			fprintf(out, "\n");
251 		} else {
252 			/* Note: some rewrite failures prevent extraction. */
253 			if (edit_pathname(bsdtar, entry))
254 				continue; /* Excluded by a rewrite failure. */
255 
256 			if (bsdtar->option_interactive &&
257 			    !yes("extract '%s'", archive_entry_pathname(entry)))
258 				continue;
259 
260 			/*
261 			 * Format here is from SUSv2, including the
262 			 * deferred '\n'.
263 			 */
264 			if (bsdtar->verbose) {
265 				safe_fprintf(stderr, "x %s",
266 				    archive_entry_pathname(entry));
267 				fflush(stderr);
268 			}
269 
270 			/* Tell the SIGINFO-handler code what we're doing. */
271 			siginfo_setinfo(bsdtar, "extracting",
272 			    archive_entry_pathname(entry), 0);
273 			siginfo_printinfo(bsdtar, 0);
274 
275 			if (bsdtar->option_stdout)
276 				r = archive_read_data_into_fd(a, 1);
277 			else
278 				r = archive_read_extract(a, entry,
279 				    bsdtar->extract_flags);
280 			if (r != ARCHIVE_OK) {
281 				if (!bsdtar->verbose)
282 					safe_fprintf(stderr, "%s",
283 					    archive_entry_pathname(entry));
284 				safe_fprintf(stderr, ": %s",
285 				    archive_error_string(a));
286 				if (!bsdtar->verbose)
287 					fprintf(stderr, "\n");
288 				bsdtar->return_value = 1;
289 			}
290 			if (bsdtar->verbose)
291 				fprintf(stderr, "\n");
292 			if (r == ARCHIVE_FATAL)
293 				break;
294 		}
295 	}
296 
297 
298 	r = archive_read_close(a);
299 	if (r != ARCHIVE_OK)
300 		bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(a));
301 	if (r <= ARCHIVE_WARN)
302 		bsdtar->return_value = 1;
303 
304 	if (bsdtar->verbose > 2)
305 		fprintf(stdout, "Archive Format: %s,  Compression: %s\n",
306 		    archive_format_name(a), archive_compression_name(a));
307 
308 	archive_read_finish(a);
309 }
310 
311 
312 /*
313  * Display information about the current file.
314  *
315  * The format here roughly duplicates the output of 'ls -l'.
316  * This is based on SUSv2, where 'tar tv' is documented as
317  * listing additional information in an "unspecified format,"
318  * and 'pax -l' is documented as using the same format as 'ls -l'.
319  */
320 static void
321 list_item_verbose(struct bsdtar *bsdtar, FILE *out, struct archive_entry *entry)
322 {
323 	const struct stat	*st;
324 	char			 tmp[100];
325 	size_t			 w;
326 	const char		*p;
327 	const char		*fmt;
328 	time_t			 tim;
329 	static time_t		 now;
330 
331 	st = archive_entry_stat(entry);
332 
333 	/*
334 	 * We avoid collecting the entire list in memory at once by
335 	 * listing things as we see them.  However, that also means we can't
336 	 * just pre-compute the field widths.  Instead, we start with guesses
337 	 * and just widen them as necessary.  These numbers are completely
338 	 * arbitrary.
339 	 */
340 	if (!bsdtar->u_width) {
341 		bsdtar->u_width = 6;
342 		bsdtar->gs_width = 13;
343 	}
344 	if (!now)
345 		time(&now);
346 	fprintf(out, "%s %d ",
347 	    archive_entry_strmode(entry),
348 	    (int)(st->st_nlink));
349 
350 	/* Use uname if it's present, else uid. */
351 	p = archive_entry_uname(entry);
352 	if ((p == NULL) || (*p == '\0')) {
353 		sprintf(tmp, "%lu ", (unsigned long)st->st_uid);
354 		p = tmp;
355 	}
356 	w = strlen(p);
357 	if (w > bsdtar->u_width)
358 		bsdtar->u_width = w;
359 	fprintf(out, "%-*s ", (int)bsdtar->u_width, p);
360 
361 	/* Use gname if it's present, else gid. */
362 	p = archive_entry_gname(entry);
363 	if (p != NULL && p[0] != '\0') {
364 		fprintf(out, "%s", p);
365 		w = strlen(p);
366 	} else {
367 		sprintf(tmp, "%lu", (unsigned long)st->st_gid);
368 		w = strlen(tmp);
369 		fprintf(out, "%s", tmp);
370 	}
371 
372 	/*
373 	 * Print device number or file size, right-aligned so as to make
374 	 * total width of group and devnum/filesize fields be gs_width.
375 	 * If gs_width is too small, grow it.
376 	 */
377 	if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
378 		sprintf(tmp, "%lu,%lu",
379 		    (unsigned long)major(st->st_rdev),
380 		    (unsigned long)minor(st->st_rdev)); /* ls(1) also casts here. */
381 	} else {
382 		/*
383 		 * Note the use of platform-dependent macros to format
384 		 * the filesize here.  We need the format string and the
385 		 * corresponding type for the cast.
386 		 */
387 		sprintf(tmp, BSDTAR_FILESIZE_PRINTF,
388 		    (BSDTAR_FILESIZE_TYPE)st->st_size);
389 	}
390 	if (w + strlen(tmp) >= bsdtar->gs_width)
391 		bsdtar->gs_width = w+strlen(tmp)+1;
392 	fprintf(out, "%*s", (int)(bsdtar->gs_width - w), tmp);
393 
394 	/* Format the time using 'ls -l' conventions. */
395 	tim = (time_t)st->st_mtime;
396 #if defined(_WIN32) && !defined(__CYGWIN__)
397 	/* Windows' strftime function does not support %e format. */
398 	if (abs(tim - now) > (365/2)*86400)
399 		fmt = bsdtar->day_first ? "%d %b  %Y" : "%b %d  %Y";
400 	else
401 		fmt = bsdtar->day_first ? "%d %b %H:%M" : "%b %d %H:%M";
402 #else
403 	if (abs(tim - now) > (365/2)*86400)
404 		fmt = bsdtar->day_first ? "%e %b  %Y" : "%b %e  %Y";
405 	else
406 		fmt = bsdtar->day_first ? "%e %b %H:%M" : "%b %e %H:%M";
407 #endif
408 	strftime(tmp, sizeof(tmp), fmt, localtime(&tim));
409 	fprintf(out, " %s ", tmp);
410 	safe_fprintf(out, "%s", archive_entry_pathname(entry));
411 
412 	/* Extra information for links. */
413 	if (archive_entry_hardlink(entry)) /* Hard link */
414 		safe_fprintf(out, " link to %s",
415 		    archive_entry_hardlink(entry));
416 	else if (S_ISLNK(st->st_mode)) /* Symbolic link */
417 		safe_fprintf(out, " -> %s", archive_entry_symlink(entry));
418 }
419