1228753Smm/*-
2228753Smm * Copyright (c) 2003-2007 Tim Kientzle
3228753Smm * All rights reserved.
4228753Smm *
5228753Smm * Redistribution and use in source and binary forms, with or without
6228753Smm * modification, are permitted provided that the following conditions
7228753Smm * are met:
8228753Smm * 1. Redistributions of source code must retain the above copyright
9228753Smm *    notice, this list of conditions and the following disclaimer.
10228753Smm * 2. Redistributions in binary form must reproduce the above copyright
11228753Smm *    notice, this list of conditions and the following disclaimer in the
12228753Smm *    documentation and/or other materials provided with the distribution.
13228753Smm *
14228753Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15228753Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16228753Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17228753Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18228753Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19228753Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20228753Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21228753Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22228753Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23228753Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24228753Smm */
25228753Smm
26228753Smm#include "bsdtar_platform.h"
27229592Smm__FBSDID("$FreeBSD$");
28228753Smm
29228753Smm#ifdef HAVE_SYS_TYPES_H
30228753Smm#include <sys/types.h>
31228753Smm#endif
32228753Smm#ifdef HAVE_SYS_PARAM_H
33228753Smm#include <sys/param.h>
34228753Smm#endif
35228753Smm#ifdef HAVE_SYS_STAT_H
36228753Smm#include <sys/stat.h>
37228753Smm#endif
38228753Smm
39228753Smm#ifdef HAVE_ERRNO_H
40228753Smm#include <errno.h>
41228753Smm#endif
42228753Smm#ifdef HAVE_GRP_H
43228753Smm#include <grp.h>
44228753Smm#endif
45228753Smm#ifdef HAVE_LIMITS_H
46228753Smm#include <limits.h>
47228753Smm#endif
48228753Smm#ifdef HAVE_PWD_H
49228753Smm#include <pwd.h>
50228753Smm#endif
51228753Smm#ifdef HAVE_STDINT_H
52228753Smm#include <stdint.h>
53228753Smm#endif
54228753Smm#include <stdio.h>
55228753Smm#ifdef HAVE_STDLIB_H
56228753Smm#include <stdlib.h>
57228753Smm#endif
58228753Smm#ifdef HAVE_STRING_H
59228753Smm#include <string.h>
60228753Smm#endif
61228753Smm#ifdef HAVE_TIME_H
62228753Smm#include <time.h>
63228753Smm#endif
64228753Smm#ifdef HAVE_UNISTD_H
65228753Smm#include <unistd.h>
66228753Smm#endif
67228753Smm
68228753Smm#include "bsdtar.h"
69228753Smm#include "err.h"
70228753Smm
71228753Smmstruct progress_data {
72228753Smm	struct bsdtar *bsdtar;
73228753Smm	struct archive *archive;
74228753Smm	struct archive_entry *entry;
75228753Smm};
76228753Smm
77228753Smmstatic void	list_item_verbose(struct bsdtar *, FILE *,
78228753Smm		    struct archive_entry *);
79228753Smmstatic void	read_archive(struct bsdtar *bsdtar, char mode);
80228753Smm
81228753Smmvoid
82228753Smmtar_mode_t(struct bsdtar *bsdtar)
83228753Smm{
84228753Smm	read_archive(bsdtar, 't');
85228753Smm	if (lafe_unmatched_inclusions_warn(bsdtar->matching, "Not found in archive") != 0)
86228753Smm		bsdtar->return_value = 1;
87228753Smm}
88228753Smm
89228753Smmvoid
90228753Smmtar_mode_x(struct bsdtar *bsdtar)
91228753Smm{
92228753Smm	read_archive(bsdtar, 'x');
93228753Smm
94228753Smm	if (lafe_unmatched_inclusions_warn(bsdtar->matching, "Not found in archive") != 0)
95228753Smm		bsdtar->return_value = 1;
96228753Smm}
97228753Smm
98228753Smmstatic void
99228753Smmprogress_func(void *cookie)
100228753Smm{
101228753Smm	struct progress_data *progress_data = cookie;
102228753Smm	struct bsdtar *bsdtar = progress_data->bsdtar;
103228753Smm	struct archive *a = progress_data->archive;
104228753Smm	struct archive_entry *entry = progress_data->entry;
105228753Smm	uint64_t comp, uncomp;
106229592Smm	int compression;
107228753Smm
108228753Smm	if (!need_report())
109228753Smm		return;
110228753Smm
111228753Smm	if (bsdtar->verbose)
112228753Smm		fprintf(stderr, "\n");
113228753Smm	if (a != NULL) {
114228753Smm		comp = archive_position_compressed(a);
115228753Smm		uncomp = archive_position_uncompressed(a);
116229592Smm		if (comp > uncomp)
117229592Smm			compression = 0;
118229592Smm		else
119229592Smm			compression = (int)((uncomp - comp) * 100 / uncomp);
120228753Smm		fprintf(stderr,
121228753Smm		    "In: %s bytes, compression %d%%;",
122229592Smm		    tar_i64toa(comp), compression);
123228753Smm		fprintf(stderr, "  Out: %d files, %s bytes\n",
124228753Smm		    archive_file_count(a), tar_i64toa(uncomp));
125228753Smm	}
126228753Smm	if (entry != NULL) {
127228753Smm		safe_fprintf(stderr, "Current: %s",
128228753Smm		    archive_entry_pathname(entry));
129228753Smm		fprintf(stderr, " (%s bytes)\n",
130228753Smm		    tar_i64toa(archive_entry_size(entry)));
131228753Smm	}
132228753Smm}
133228753Smm
134228753Smm/*
135228753Smm * Handle 'x' and 't' modes.
136228753Smm */
137228753Smmstatic void
138228753Smmread_archive(struct bsdtar *bsdtar, char mode)
139228753Smm{
140228753Smm	struct progress_data	progress_data;
141228753Smm	FILE			 *out;
142228753Smm	struct archive		 *a;
143228753Smm	struct archive_entry	 *entry;
144228753Smm	const struct stat	 *st;
145228753Smm	int			  r;
146228753Smm
147228753Smm	while (*bsdtar->argv) {
148228753Smm		lafe_include(&bsdtar->matching, *bsdtar->argv);
149228753Smm		bsdtar->argv++;
150228753Smm	}
151228753Smm
152228753Smm	if (bsdtar->names_from_file != NULL)
153228753Smm		lafe_include_from_file(&bsdtar->matching,
154228753Smm		    bsdtar->names_from_file, bsdtar->option_null);
155228753Smm
156228753Smm	a = archive_read_new();
157228753Smm	if (bsdtar->compress_program != NULL)
158228753Smm		archive_read_support_compression_program(a, bsdtar->compress_program);
159228753Smm	else
160228753Smm		archive_read_support_compression_all(a);
161228753Smm	archive_read_support_format_all(a);
162228753Smm	if (ARCHIVE_OK != archive_read_set_options(a, bsdtar->option_options))
163228753Smm		lafe_errc(1, 0, "%s", archive_error_string(a));
164228753Smm	if (archive_read_open_file(a, bsdtar->filename,
165228753Smm	    bsdtar->bytes_per_block != 0 ? bsdtar->bytes_per_block :
166228753Smm	    DEFAULT_BYTES_PER_BLOCK))
167228753Smm		lafe_errc(1, 0, "Error opening archive: %s",
168228753Smm		    archive_error_string(a));
169228753Smm
170228753Smm	do_chdir(bsdtar);
171228753Smm
172228753Smm	if (mode == 'x') {
173228753Smm		/* Set an extract callback so that we can handle SIGINFO. */
174228753Smm		progress_data.bsdtar = bsdtar;
175228753Smm		progress_data.archive = a;
176228753Smm		archive_read_extract_set_progress_callback(a, progress_func,
177228753Smm		    &progress_data);
178228753Smm	}
179228753Smm
180228753Smm	if (mode == 'x' && bsdtar->option_chroot) {
181228753Smm#if HAVE_CHROOT
182228753Smm		if (chroot(".") != 0)
183228753Smm			lafe_errc(1, errno, "Can't chroot to \".\"");
184228753Smm#else
185228753Smm		lafe_errc(1, 0,
186228753Smm		    "chroot isn't supported on this platform");
187228753Smm#endif
188228753Smm	}
189228753Smm
190228753Smm	for (;;) {
191228753Smm		/* Support --fast-read option */
192228753Smm		if (bsdtar->option_fast_read &&
193228753Smm		    lafe_unmatched_inclusions(bsdtar->matching) == 0)
194228753Smm			break;
195228753Smm
196228753Smm		r = archive_read_next_header(a, &entry);
197228753Smm		progress_data.entry = entry;
198228753Smm		if (r == ARCHIVE_EOF)
199228753Smm			break;
200228753Smm		if (r < ARCHIVE_OK)
201228753Smm			lafe_warnc(0, "%s", archive_error_string(a));
202228753Smm		if (r <= ARCHIVE_WARN)
203228753Smm			bsdtar->return_value = 1;
204228753Smm		if (r == ARCHIVE_RETRY) {
205228753Smm			/* Retryable error: try again */
206228753Smm			lafe_warnc(0, "Retrying...");
207228753Smm			continue;
208228753Smm		}
209228753Smm		if (r == ARCHIVE_FATAL)
210228753Smm			break;
211228753Smm
212228753Smm		if (bsdtar->uid >= 0) {
213228753Smm			archive_entry_set_uid(entry, bsdtar->uid);
214228753Smm			archive_entry_set_uname(entry, NULL);
215228753Smm		}
216228753Smm		if (bsdtar->gid >= 0) {
217228753Smm			archive_entry_set_gid(entry, bsdtar->gid);
218228753Smm			archive_entry_set_gname(entry, NULL);
219228753Smm		}
220228753Smm		if (bsdtar->uname)
221228753Smm			archive_entry_set_uname(entry, bsdtar->uname);
222229592Smm		if (bsdtar->gname)
223228753Smm			archive_entry_set_gname(entry, bsdtar->gname);
224228753Smm
225228753Smm		/*
226228753Smm		 * Exclude entries that are too old.
227228753Smm		 */
228228753Smm		st = archive_entry_stat(entry);
229228753Smm		if (bsdtar->newer_ctime_sec > 0) {
230228753Smm			if (st->st_ctime < bsdtar->newer_ctime_sec)
231228753Smm				continue; /* Too old, skip it. */
232228753Smm			if (st->st_ctime == bsdtar->newer_ctime_sec
233228753Smm			    && ARCHIVE_STAT_CTIME_NANOS(st)
234228753Smm			    <= bsdtar->newer_ctime_nsec)
235228753Smm				continue; /* Too old, skip it. */
236228753Smm		}
237228753Smm		if (bsdtar->newer_mtime_sec > 0) {
238228753Smm			if (st->st_mtime < bsdtar->newer_mtime_sec)
239228753Smm				continue; /* Too old, skip it. */
240228753Smm			if (st->st_mtime == bsdtar->newer_mtime_sec
241228753Smm			    && ARCHIVE_STAT_MTIME_NANOS(st)
242228753Smm			    <= bsdtar->newer_mtime_nsec)
243228753Smm				continue; /* Too old, skip it. */
244228753Smm		}
245228753Smm
246228753Smm		/*
247228753Smm		 * Note that pattern exclusions are checked before
248228753Smm		 * pathname rewrites are handled.  This gives more
249228753Smm		 * control over exclusions, since rewrites always lose
250228753Smm		 * information.  (For example, consider a rewrite
251228753Smm		 * s/foo[0-9]/foo/.  If we check exclusions after the
252228753Smm		 * rewrite, there would be no way to exclude foo1/bar
253228753Smm		 * while allowing foo2/bar.)
254228753Smm		 */
255228753Smm		if (lafe_excluded(bsdtar->matching, archive_entry_pathname(entry)))
256228753Smm			continue; /* Excluded by a pattern test. */
257228753Smm
258228753Smm		if (mode == 't') {
259228753Smm			/* Perversely, gtar uses -O to mean "send to stderr"
260228753Smm			 * when used with -t. */
261228753Smm			out = bsdtar->option_stdout ? stderr : stdout;
262228753Smm
263228753Smm			/*
264228753Smm			 * TODO: Provide some reasonable way to
265228753Smm			 * preview rewrites.  gtar always displays
266228753Smm			 * the unedited path in -t output, which means
267228753Smm			 * you cannot easily preview rewrites.
268228753Smm			 */
269228753Smm			if (bsdtar->verbose < 2)
270228753Smm				safe_fprintf(out, "%s",
271228753Smm				    archive_entry_pathname(entry));
272228753Smm			else
273228753Smm				list_item_verbose(bsdtar, out, entry);
274228753Smm			fflush(out);
275228753Smm			r = archive_read_data_skip(a);
276228753Smm			if (r == ARCHIVE_WARN) {
277228753Smm				fprintf(out, "\n");
278228753Smm				lafe_warnc(0, "%s",
279228753Smm				    archive_error_string(a));
280228753Smm			}
281228753Smm			if (r == ARCHIVE_RETRY) {
282228753Smm				fprintf(out, "\n");
283228753Smm				lafe_warnc(0, "%s",
284228753Smm				    archive_error_string(a));
285228753Smm			}
286228753Smm			if (r == ARCHIVE_FATAL) {
287228753Smm				fprintf(out, "\n");
288228753Smm				lafe_warnc(0, "%s",
289228753Smm				    archive_error_string(a));
290228753Smm				bsdtar->return_value = 1;
291228753Smm				break;
292228753Smm			}
293228753Smm			fprintf(out, "\n");
294228753Smm		} else {
295228753Smm			/* Note: some rewrite failures prevent extraction. */
296228753Smm			if (edit_pathname(bsdtar, entry))
297228753Smm				continue; /* Excluded by a rewrite failure. */
298228753Smm
299228753Smm			if (bsdtar->option_interactive &&
300228753Smm			    !yes("extract '%s'", archive_entry_pathname(entry)))
301228753Smm				continue;
302228753Smm
303228753Smm			/*
304228753Smm			 * Format here is from SUSv2, including the
305228753Smm			 * deferred '\n'.
306228753Smm			 */
307228753Smm			if (bsdtar->verbose) {
308228753Smm				safe_fprintf(stderr, "x %s",
309228753Smm				    archive_entry_pathname(entry));
310228753Smm				fflush(stderr);
311228753Smm			}
312228753Smm
313228753Smm			// TODO siginfo_printinfo(bsdtar, 0);
314228753Smm
315228753Smm			if (bsdtar->option_stdout)
316228753Smm				r = archive_read_data_into_fd(a, 1);
317228753Smm			else
318228753Smm				r = archive_read_extract(a, entry,
319228753Smm				    bsdtar->extract_flags);
320228753Smm			if (r != ARCHIVE_OK) {
321228753Smm				if (!bsdtar->verbose)
322228753Smm					safe_fprintf(stderr, "%s",
323228753Smm					    archive_entry_pathname(entry));
324228753Smm				safe_fprintf(stderr, ": %s",
325228753Smm				    archive_error_string(a));
326228753Smm				if (!bsdtar->verbose)
327228753Smm					fprintf(stderr, "\n");
328228753Smm				bsdtar->return_value = 1;
329228753Smm			}
330228753Smm			if (bsdtar->verbose)
331228753Smm				fprintf(stderr, "\n");
332228753Smm			if (r == ARCHIVE_FATAL)
333228753Smm				break;
334228753Smm		}
335228753Smm	}
336228753Smm
337228753Smm
338228753Smm	r = archive_read_close(a);
339228753Smm	if (r != ARCHIVE_OK)
340228753Smm		lafe_warnc(0, "%s", archive_error_string(a));
341228753Smm	if (r <= ARCHIVE_WARN)
342228753Smm		bsdtar->return_value = 1;
343228753Smm
344228753Smm	if (bsdtar->verbose > 2)
345228753Smm		fprintf(stdout, "Archive Format: %s,  Compression: %s\n",
346228753Smm		    archive_format_name(a), archive_compression_name(a));
347228753Smm
348228753Smm	archive_read_finish(a);
349228753Smm}
350228753Smm
351228753Smm
352228753Smm/*
353228753Smm * Display information about the current file.
354228753Smm *
355228753Smm * The format here roughly duplicates the output of 'ls -l'.
356228753Smm * This is based on SUSv2, where 'tar tv' is documented as
357228753Smm * listing additional information in an "unspecified format,"
358228753Smm * and 'pax -l' is documented as using the same format as 'ls -l'.
359228753Smm */
360228753Smmstatic void
361228753Smmlist_item_verbose(struct bsdtar *bsdtar, FILE *out, struct archive_entry *entry)
362228753Smm{
363228753Smm	char			 tmp[100];
364228753Smm	size_t			 w;
365228753Smm	const char		*p;
366228753Smm	const char		*fmt;
367228753Smm	time_t			 tim;
368228753Smm	static time_t		 now;
369228753Smm
370228753Smm	/*
371228753Smm	 * We avoid collecting the entire list in memory at once by
372228753Smm	 * listing things as we see them.  However, that also means we can't
373228753Smm	 * just pre-compute the field widths.  Instead, we start with guesses
374228753Smm	 * and just widen them as necessary.  These numbers are completely
375228753Smm	 * arbitrary.
376228753Smm	 */
377228753Smm	if (!bsdtar->u_width) {
378228753Smm		bsdtar->u_width = 6;
379228753Smm		bsdtar->gs_width = 13;
380228753Smm	}
381228753Smm	if (!now)
382228753Smm		time(&now);
383228753Smm	fprintf(out, "%s %d ",
384228753Smm	    archive_entry_strmode(entry),
385228753Smm	    archive_entry_nlink(entry));
386228753Smm
387228753Smm	/* Use uname if it's present, else uid. */
388228753Smm	p = archive_entry_uname(entry);
389228753Smm	if ((p == NULL) || (*p == '\0')) {
390228753Smm		sprintf(tmp, "%lu ",
391228753Smm		    (unsigned long)archive_entry_uid(entry));
392228753Smm		p = tmp;
393228753Smm	}
394228753Smm	w = strlen(p);
395228753Smm	if (w > bsdtar->u_width)
396228753Smm		bsdtar->u_width = w;
397228753Smm	fprintf(out, "%-*s ", (int)bsdtar->u_width, p);
398228753Smm
399228753Smm	/* Use gname if it's present, else gid. */
400228753Smm	p = archive_entry_gname(entry);
401228753Smm	if (p != NULL && p[0] != '\0') {
402228753Smm		fprintf(out, "%s", p);
403228753Smm		w = strlen(p);
404228753Smm	} else {
405228753Smm		sprintf(tmp, "%lu",
406228753Smm		    (unsigned long)archive_entry_gid(entry));
407228753Smm		w = strlen(tmp);
408228753Smm		fprintf(out, "%s", tmp);
409228753Smm	}
410228753Smm
411228753Smm	/*
412228753Smm	 * Print device number or file size, right-aligned so as to make
413228753Smm	 * total width of group and devnum/filesize fields be gs_width.
414228753Smm	 * If gs_width is too small, grow it.
415228753Smm	 */
416228753Smm	if (archive_entry_filetype(entry) == AE_IFCHR
417228753Smm	    || archive_entry_filetype(entry) == AE_IFBLK) {
418228753Smm		sprintf(tmp, "%lu,%lu",
419228753Smm		    (unsigned long)archive_entry_rdevmajor(entry),
420228753Smm		    (unsigned long)archive_entry_rdevminor(entry));
421228753Smm	} else {
422228753Smm		strcpy(tmp, tar_i64toa(archive_entry_size(entry)));
423228753Smm	}
424228753Smm	if (w + strlen(tmp) >= bsdtar->gs_width)
425228753Smm		bsdtar->gs_width = w+strlen(tmp)+1;
426228753Smm	fprintf(out, "%*s", (int)(bsdtar->gs_width - w), tmp);
427228753Smm
428228753Smm	/* Format the time using 'ls -l' conventions. */
429228753Smm	tim = archive_entry_mtime(entry);
430228753Smm#define HALF_YEAR (time_t)365 * 86400 / 2
431228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
432228753Smm#define DAY_FMT  "%d"  /* Windows' strftime function does not support %e format. */
433228753Smm#else
434228753Smm#define DAY_FMT  "%e"  /* Day number without leading zeros */
435228753Smm#endif
436228753Smm	if (tim < now - HALF_YEAR || tim > now + HALF_YEAR)
437228753Smm		fmt = bsdtar->day_first ? DAY_FMT " %b  %Y" : "%b " DAY_FMT "  %Y";
438228753Smm	else
439228753Smm		fmt = bsdtar->day_first ? DAY_FMT " %b %H:%M" : "%b " DAY_FMT " %H:%M";
440228753Smm	strftime(tmp, sizeof(tmp), fmt, localtime(&tim));
441228753Smm	fprintf(out, " %s ", tmp);
442228753Smm	safe_fprintf(out, "%s", archive_entry_pathname(entry));
443228753Smm
444228753Smm	/* Extra information for links. */
445228753Smm	if (archive_entry_hardlink(entry)) /* Hard link */
446228753Smm		safe_fprintf(out, " link to %s",
447228753Smm		    archive_entry_hardlink(entry));
448228753Smm	else if (archive_entry_symlink(entry)) /* Symbolic link */
449228753Smm		safe_fprintf(out, " -> %s", archive_entry_symlink(entry));
450228753Smm}
451