2004-04-05 21:32:18 +00:00
|
|
|
/*-
|
2007-01-09 08:12:17 +00:00
|
|
|
* Copyright (c) 2003-2007 Tim Kientzle
|
2004-04-05 21:32:18 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
2007-01-09 08:12:17 +00:00
|
|
|
* notice, this list of conditions and the following disclaimer.
|
2004-04-05 21:32:18 +00:00
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "bsdtar_platform.h"
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2007-03-11 10:36:42 +00:00
|
|
|
#ifdef HAVE_SYS_TYPES_H
|
|
|
|
#include <sys/types.h>
|
|
|
|
#endif
|
2006-07-30 18:34:40 +00:00
|
|
|
#ifdef MAJOR_IN_MKDEV
|
|
|
|
#include <sys/mkdev.h>
|
2007-04-13 16:08:23 +00:00
|
|
|
#elif defined(MAJOR_IN_SYSMACROS)
|
|
|
|
#include <sys/sysmacros.h>
|
2006-07-30 18:34:40 +00:00
|
|
|
#endif
|
2007-03-11 10:36:42 +00:00
|
|
|
#ifdef HAVE_SYS_PARAM_H
|
2004-05-02 00:43:02 +00:00
|
|
|
#include <sys/param.h>
|
2007-03-11 10:36:42 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_STAT_H
|
2004-04-05 21:32:18 +00:00
|
|
|
#include <sys/stat.h>
|
2007-03-11 10:36:42 +00:00
|
|
|
#endif
|
2004-04-05 21:32:18 +00:00
|
|
|
|
2007-03-11 10:36:42 +00:00
|
|
|
#ifdef HAVE_ERRNO_H
|
2004-04-05 21:32:18 +00:00
|
|
|
#include <errno.h>
|
2007-03-11 10:36:42 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_GRP_H
|
2004-04-05 21:32:18 +00:00
|
|
|
#include <grp.h>
|
2007-03-11 10:36:42 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_LIMITS_H
|
2004-04-05 21:32:18 +00:00
|
|
|
#include <limits.h>
|
2007-03-11 10:36:42 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_PWD_H
|
2004-04-05 21:32:18 +00:00
|
|
|
#include <pwd.h>
|
2007-03-11 10:36:42 +00:00
|
|
|
#endif
|
2004-04-05 21:32:18 +00:00
|
|
|
#include <stdio.h>
|
2007-03-11 10:36:42 +00:00
|
|
|
#ifdef HAVE_STDLIB_H
|
2004-04-05 21:32:18 +00:00
|
|
|
#include <stdlib.h>
|
2007-03-11 10:36:42 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRING_H
|
2004-04-05 21:32:18 +00:00
|
|
|
#include <string.h>
|
2007-03-11 10:36:42 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_TIME_H
|
2004-04-05 21:32:18 +00:00
|
|
|
#include <time.h>
|
2007-03-11 10:36:42 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_UNISTD_H
|
2004-04-05 21:32:18 +00:00
|
|
|
#include <unistd.h>
|
2007-03-11 10:36:42 +00:00
|
|
|
#endif
|
2004-04-05 21:32:18 +00:00
|
|
|
|
|
|
|
#include "bsdtar.h"
|
|
|
|
|
2004-08-27 04:13:15 +00:00
|
|
|
static void list_item_verbose(struct bsdtar *, FILE *,
|
|
|
|
struct archive_entry *);
|
2004-04-05 21:32:18 +00:00
|
|
|
static void read_archive(struct bsdtar *bsdtar, char mode);
|
|
|
|
|
|
|
|
void
|
|
|
|
tar_mode_t(struct bsdtar *bsdtar)
|
|
|
|
{
|
|
|
|
read_archive(bsdtar, 't');
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tar_mode_x(struct bsdtar *bsdtar)
|
|
|
|
{
|
|
|
|
read_archive(bsdtar, 'x');
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Handle 'x' and 't' modes.
|
|
|
|
*/
|
2004-07-24 22:13:44 +00:00
|
|
|
static void
|
2004-04-05 21:32:18 +00:00
|
|
|
read_archive(struct bsdtar *bsdtar, char mode)
|
|
|
|
{
|
2004-08-27 04:13:15 +00:00
|
|
|
FILE *out;
|
2004-04-05 21:32:18 +00:00
|
|
|
struct archive *a;
|
|
|
|
struct archive_entry *entry;
|
2005-04-17 17:20:54 +00:00
|
|
|
const struct stat *st;
|
2004-04-05 21:32:18 +00:00
|
|
|
int r;
|
|
|
|
|
2004-07-24 22:13:44 +00:00
|
|
|
while (*bsdtar->argv) {
|
2004-04-05 21:32:18 +00:00
|
|
|
include(bsdtar, *bsdtar->argv);
|
|
|
|
bsdtar->argv++;
|
2004-07-24 22:13:44 +00:00
|
|
|
}
|
2004-04-05 21:32:18 +00:00
|
|
|
|
2004-06-27 06:29:03 +00:00
|
|
|
if (bsdtar->names_from_file != NULL)
|
|
|
|
include_from_file(bsdtar, bsdtar->names_from_file);
|
|
|
|
|
2004-04-05 21:32:18 +00:00
|
|
|
a = archive_read_new();
|
2007-05-29 05:39:10 +00:00
|
|
|
if (bsdtar->compress_program != NULL)
|
|
|
|
archive_read_support_compression_program(a, bsdtar->compress_program);
|
|
|
|
else
|
|
|
|
archive_read_support_compression_all(a);
|
2004-04-05 21:32:18 +00:00
|
|
|
archive_read_support_format_all(a);
|
2004-06-07 03:19:57 +00:00
|
|
|
if (archive_read_open_file(a, bsdtar->filename,
|
|
|
|
bsdtar->bytes_per_block != 0 ? bsdtar->bytes_per_block :
|
|
|
|
DEFAULT_BYTES_PER_BLOCK))
|
2004-05-17 05:44:53 +00:00
|
|
|
bsdtar_errc(bsdtar, 1, 0, "Error opening archive: %s",
|
2004-04-05 21:32:18 +00:00
|
|
|
archive_error_string(a));
|
|
|
|
|
2004-08-08 05:50:10 +00:00
|
|
|
do_chdir(bsdtar);
|
2004-04-05 21:32:18 +00:00
|
|
|
for (;;) {
|
|
|
|
/* Support --fast-read option */
|
|
|
|
if (bsdtar->option_fast_read &&
|
|
|
|
unmatched_inclusions(bsdtar) == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
r = archive_read_next_header(a, &entry);
|
|
|
|
if (r == ARCHIVE_EOF)
|
|
|
|
break;
|
2007-04-07 05:56:40 +00:00
|
|
|
if (r < ARCHIVE_OK)
|
2004-05-17 05:44:53 +00:00
|
|
|
bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(a));
|
2004-04-05 21:32:18 +00:00
|
|
|
if (r == ARCHIVE_RETRY) {
|
|
|
|
/* Retryable error: try again */
|
2004-05-17 05:44:53 +00:00
|
|
|
bsdtar_warnc(bsdtar, 0, "Retrying...");
|
2004-04-05 21:32:18 +00:00
|
|
|
continue;
|
|
|
|
}
|
2007-04-07 05:56:40 +00:00
|
|
|
if (r != ARCHIVE_OK) {
|
|
|
|
bsdtar->return_value = 1;
|
|
|
|
break;
|
|
|
|
}
|
2004-04-05 21:32:18 +00:00
|
|
|
|
2005-03-13 04:12:30 +00:00
|
|
|
/*
|
2005-04-17 17:20:54 +00:00
|
|
|
* Exclude entries that are too old.
|
|
|
|
*/
|
|
|
|
st = archive_entry_stat(entry);
|
|
|
|
if (bsdtar->newer_ctime_sec > 0) {
|
|
|
|
if (st->st_ctime < bsdtar->newer_ctime_sec)
|
|
|
|
continue; /* Too old, skip it. */
|
|
|
|
if (st->st_ctime == bsdtar->newer_ctime_sec
|
|
|
|
&& ARCHIVE_STAT_CTIME_NANOS(st)
|
|
|
|
<= bsdtar->newer_ctime_nsec)
|
|
|
|
continue; /* Too old, skip it. */
|
|
|
|
}
|
|
|
|
if (bsdtar->newer_mtime_sec > 0) {
|
|
|
|
if (st->st_mtime < bsdtar->newer_mtime_sec)
|
|
|
|
continue; /* Too old, skip it. */
|
|
|
|
if (st->st_mtime == bsdtar->newer_mtime_sec
|
|
|
|
&& ARCHIVE_STAT_MTIME_NANOS(st)
|
|
|
|
<= bsdtar->newer_mtime_nsec)
|
|
|
|
continue; /* Too old, skip it. */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Note that pattern exclusions are checked before
|
|
|
|
* pathname rewrites are handled. This gives more
|
|
|
|
* control over exclusions, since rewrites always lose
|
|
|
|
* information. (For example, consider a rewrite
|
|
|
|
* s/foo[0-9]/foo/. If we check exclusions after the
|
|
|
|
* rewrite, there would be no way to exclude foo1/bar
|
|
|
|
* while allowing foo2/bar.)
|
2005-03-13 04:12:30 +00:00
|
|
|
*/
|
2004-04-05 21:32:18 +00:00
|
|
|
if (excluded(bsdtar, archive_entry_pathname(entry)))
|
2005-04-17 17:20:54 +00:00
|
|
|
continue; /* Excluded by a pattern test. */
|
2004-04-05 21:32:18 +00:00
|
|
|
|
2005-03-13 04:12:30 +00:00
|
|
|
/*
|
|
|
|
* Modify the pathname as requested by the user. We
|
|
|
|
* do this for -t as well to give users a way to
|
|
|
|
* preview the effects of their rewrites. We also do
|
|
|
|
* this before extraction security checks (including
|
|
|
|
* leading '/' removal). Note that some rewrite
|
|
|
|
* failures prevent extraction.
|
|
|
|
*/
|
|
|
|
if (edit_pathname(bsdtar, entry))
|
2005-04-17 17:20:54 +00:00
|
|
|
continue; /* Excluded by a rewrite failure. */
|
2005-03-13 04:12:30 +00:00
|
|
|
|
2004-04-05 21:32:18 +00:00
|
|
|
if (mode == 't') {
|
2004-08-27 04:13:15 +00:00
|
|
|
/* Perversely, gtar uses -O to mean "send to stderr"
|
|
|
|
* when used with -t. */
|
|
|
|
out = bsdtar->option_stdout ? stderr : stdout;
|
|
|
|
|
2004-04-05 21:32:18 +00:00
|
|
|
if (bsdtar->verbose < 2)
|
2004-08-27 04:13:15 +00:00
|
|
|
safe_fprintf(out, "%s",
|
2004-04-05 21:32:18 +00:00
|
|
|
archive_entry_pathname(entry));
|
|
|
|
else
|
2004-08-27 04:13:15 +00:00
|
|
|
list_item_verbose(bsdtar, out, entry);
|
|
|
|
fflush(out);
|
2004-05-27 04:35:36 +00:00
|
|
|
r = archive_read_data_skip(a);
|
|
|
|
if (r == ARCHIVE_WARN) {
|
2004-08-27 04:13:15 +00:00
|
|
|
fprintf(out, "\n");
|
2004-05-17 05:44:53 +00:00
|
|
|
bsdtar_warnc(bsdtar, 0, "%s",
|
|
|
|
archive_error_string(a));
|
2004-05-27 04:35:36 +00:00
|
|
|
}
|
|
|
|
if (r == ARCHIVE_RETRY) {
|
2004-08-27 04:13:15 +00:00
|
|
|
fprintf(out, "\n");
|
2004-05-27 04:35:36 +00:00
|
|
|
bsdtar_warnc(bsdtar, 0, "%s",
|
|
|
|
archive_error_string(a));
|
|
|
|
}
|
|
|
|
if (r == ARCHIVE_FATAL) {
|
2004-08-27 04:13:15 +00:00
|
|
|
fprintf(out, "\n");
|
2004-05-27 04:35:36 +00:00
|
|
|
bsdtar_warnc(bsdtar, 0, "%s",
|
2004-04-05 21:32:18 +00:00
|
|
|
archive_error_string(a));
|
2007-05-08 15:22:21 +00:00
|
|
|
bsdtar->return_value = 1;
|
2004-04-05 21:32:18 +00:00
|
|
|
break;
|
|
|
|
}
|
2004-08-27 04:13:15 +00:00
|
|
|
fprintf(out, "\n");
|
2004-04-05 21:32:18 +00:00
|
|
|
} else {
|
2005-04-17 17:20:54 +00:00
|
|
|
if (bsdtar->option_interactive &&
|
|
|
|
!yes("extract '%s'", archive_entry_pathname(entry)))
|
2004-04-28 18:53:07 +00:00
|
|
|
continue;
|
|
|
|
|
2004-04-05 21:32:18 +00:00
|
|
|
/*
|
|
|
|
* Format here is from SUSv2, including the
|
|
|
|
* deferred '\n'.
|
|
|
|
*/
|
|
|
|
if (bsdtar->verbose) {
|
|
|
|
safe_fprintf(stderr, "x %s",
|
|
|
|
archive_entry_pathname(entry));
|
|
|
|
fflush(stderr);
|
|
|
|
}
|
2007-04-16 04:04:50 +00:00
|
|
|
if (bsdtar->option_stdout)
|
|
|
|
r = archive_read_data_into_fd(a, 1);
|
|
|
|
else
|
|
|
|
r = archive_read_extract(a, entry,
|
|
|
|
bsdtar->extract_flags);
|
|
|
|
if (r != ARCHIVE_OK) {
|
2004-04-05 21:32:18 +00:00
|
|
|
if (!bsdtar->verbose)
|
|
|
|
safe_fprintf(stderr, "%s",
|
|
|
|
archive_entry_pathname(entry));
|
|
|
|
safe_fprintf(stderr, ": %s",
|
|
|
|
archive_error_string(a));
|
|
|
|
if (!bsdtar->verbose)
|
|
|
|
fprintf(stderr, "\n");
|
2004-05-27 04:35:36 +00:00
|
|
|
bsdtar->return_value = 1;
|
2004-04-05 21:32:18 +00:00
|
|
|
}
|
|
|
|
if (bsdtar->verbose)
|
|
|
|
fprintf(stderr, "\n");
|
2007-04-16 04:04:50 +00:00
|
|
|
if (r == ARCHIVE_FATAL)
|
|
|
|
break;
|
2004-04-05 21:32:18 +00:00
|
|
|
}
|
|
|
|
}
|
2004-05-27 04:35:36 +00:00
|
|
|
|
|
|
|
if (bsdtar->verbose > 2)
|
|
|
|
fprintf(stdout, "Archive Format: %s, Compression: %s\n",
|
|
|
|
archive_format_name(a), archive_compression_name(a));
|
|
|
|
|
2004-04-05 21:32:18 +00:00
|
|
|
archive_read_finish(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Display information about the current file.
|
|
|
|
*
|
|
|
|
* The format here roughly duplicates the output of 'ls -l'.
|
|
|
|
* This is based on SUSv2, where 'tar tv' is documented as
|
|
|
|
* listing additional information in an "unspecified format,"
|
|
|
|
* and 'pax -l' is documented as using the same format as 'ls -l'.
|
|
|
|
*/
|
|
|
|
static void
|
2004-08-27 04:13:15 +00:00
|
|
|
list_item_verbose(struct bsdtar *bsdtar, FILE *out, struct archive_entry *entry)
|
2004-04-05 21:32:18 +00:00
|
|
|
{
|
|
|
|
const struct stat *st;
|
|
|
|
char tmp[100];
|
|
|
|
size_t w;
|
|
|
|
const char *p;
|
2004-07-15 03:14:46 +00:00
|
|
|
const char *fmt;
|
2004-04-05 21:32:18 +00:00
|
|
|
time_t tim;
|
|
|
|
static time_t now;
|
|
|
|
|
|
|
|
st = archive_entry_stat(entry);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We avoid collecting the entire list in memory at once by
|
|
|
|
* listing things as we see them. However, that also means we can't
|
|
|
|
* just pre-compute the field widths. Instead, we start with guesses
|
|
|
|
* and just widen them as necessary. These numbers are completely
|
|
|
|
* arbitrary.
|
|
|
|
*/
|
|
|
|
if (!bsdtar->u_width) {
|
|
|
|
bsdtar->u_width = 6;
|
|
|
|
bsdtar->gs_width = 13;
|
|
|
|
}
|
|
|
|
if (!now)
|
|
|
|
time(&now);
|
2004-05-03 21:05:59 +00:00
|
|
|
bsdtar_strmode(entry, tmp);
|
2006-03-07 06:02:09 +00:00
|
|
|
fprintf(out, "%s %d ", tmp, (int)(st->st_nlink));
|
2004-04-05 21:32:18 +00:00
|
|
|
|
|
|
|
/* Use uname if it's present, else uid. */
|
|
|
|
p = archive_entry_uname(entry);
|
2004-07-25 04:15:50 +00:00
|
|
|
if ((p == NULL) || (*p == '\0')) {
|
2007-05-29 05:39:10 +00:00
|
|
|
sprintf(tmp, "%lu ", (unsigned long)st->st_uid);
|
2004-07-25 04:15:50 +00:00
|
|
|
p = tmp;
|
2004-04-05 21:32:18 +00:00
|
|
|
}
|
2004-07-25 04:15:50 +00:00
|
|
|
w = strlen(p);
|
2004-04-05 21:32:18 +00:00
|
|
|
if (w > bsdtar->u_width)
|
|
|
|
bsdtar->u_width = w;
|
2004-08-05 05:46:22 +00:00
|
|
|
fprintf(out, "%-*s ", (int)bsdtar->u_width, p);
|
2004-04-05 21:32:18 +00:00
|
|
|
|
|
|
|
/* Use gname if it's present, else gid. */
|
|
|
|
p = archive_entry_gname(entry);
|
2004-07-24 22:13:44 +00:00
|
|
|
if (p != NULL && p[0] != '\0') {
|
2004-04-05 21:32:18 +00:00
|
|
|
fprintf(out, "%s", p);
|
2004-07-24 22:13:44 +00:00
|
|
|
w = strlen(p);
|
2004-04-05 21:32:18 +00:00
|
|
|
} else {
|
2007-05-29 05:39:10 +00:00
|
|
|
sprintf(tmp, "%lu", (unsigned long)st->st_gid);
|
2004-07-24 22:13:44 +00:00
|
|
|
w = strlen(tmp);
|
2004-04-05 21:32:18 +00:00
|
|
|
fprintf(out, "%s", tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Print device number or file size, right-aligned so as to make
|
|
|
|
* total width of group and devnum/filesize fields be gs_width.
|
|
|
|
* If gs_width is too small, grow it.
|
|
|
|
*/
|
|
|
|
if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
|
2007-05-29 05:39:10 +00:00
|
|
|
sprintf(tmp, "%lu,%lu",
|
|
|
|
(unsigned long)major(st->st_rdev),
|
|
|
|
(unsigned long)minor(st->st_rdev)); /* ls(1) also casts here. */
|
2004-04-05 21:32:18 +00:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Note the use of platform-dependent macros to format
|
|
|
|
* the filesize here. We need the format string and the
|
|
|
|
* corresponding type for the cast.
|
|
|
|
*/
|
|
|
|
sprintf(tmp, BSDTAR_FILESIZE_PRINTF,
|
|
|
|
(BSDTAR_FILESIZE_TYPE)st->st_size);
|
|
|
|
}
|
|
|
|
if (w + strlen(tmp) >= bsdtar->gs_width)
|
|
|
|
bsdtar->gs_width = w+strlen(tmp)+1;
|
|
|
|
fprintf(out, "%*s", (int)(bsdtar->gs_width - w), tmp);
|
|
|
|
|
|
|
|
/* Format the time using 'ls -l' conventions. */
|
|
|
|
tim = (time_t)st->st_mtime;
|
2004-07-15 03:14:46 +00:00
|
|
|
if (abs(tim - now) > (365/2)*86400)
|
|
|
|
fmt = bsdtar->day_first ? "%e %b %Y" : "%b %e %Y";
|
2004-04-05 21:32:18 +00:00
|
|
|
else
|
2004-10-17 23:57:10 +00:00
|
|
|
fmt = bsdtar->day_first ? "%e %b %R" : "%b %e %R";
|
2004-07-15 03:14:46 +00:00
|
|
|
strftime(tmp, sizeof(tmp), fmt, localtime(&tim));
|
|
|
|
fprintf(out, " %s ", tmp);
|
|
|
|
safe_fprintf(out, "%s", archive_entry_pathname(entry));
|
2004-04-05 21:32:18 +00:00
|
|
|
|
|
|
|
/* Extra information for links. */
|
|
|
|
if (archive_entry_hardlink(entry)) /* Hard link */
|
|
|
|
safe_fprintf(out, " link to %s",
|
|
|
|
archive_entry_hardlink(entry));
|
|
|
|
else if (S_ISLNK(st->st_mode)) /* Symbolic link */
|
|
|
|
safe_fprintf(out, " -> %s", archive_entry_symlink(entry));
|
|
|
|
}
|