#include "bsdtar_platform.h"
__FBSDID("$FreeBSD: src/usr.bin/tar/read.c,v 1.40 2008/08/21 06:41:14 kientzle Exp $");
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#ifdef HAVE_GRP_H
#include <grp.h>
#endif
#ifdef HAVE_LIMITS_H
#include <limits.h>
#endif
#ifdef HAVE_PWD_H
#include <pwd.h>
#endif
#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif
#include <stdio.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_TIME_H
#include <time.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <sys/queue.h>
#include <copyfile.h>
#include <fcntl.h>
#include <libgen.h>
#include <TargetConditionals.h>
#if TARGET_OS_MAC && !TARGET_OS_IPHONE
#define HAVE_QUARANTINE 1
#endif
#ifdef HAVE_QUARANTINE
#include <quarantine.h>
#endif
#include "bsdtar.h"
#include "err.h"
struct progress_data {
struct bsdtar *bsdtar;
struct archive *archive;
struct archive_entry *entry;
};
struct copyfile_list_entry_t {
char *src;
char *dst;
char *tmp;
LIST_ENTRY(copyfile_list_entry_t) link;
};
static void list_item_verbose(struct bsdtar *, FILE *,
struct archive_entry *);
static void read_archive(struct bsdtar *bsdtar, char mode);
void
tar_mode_t(struct bsdtar *bsdtar)
{
read_archive(bsdtar, 't');
if (lafe_unmatched_inclusions_warn(bsdtar->matching, "Not found in archive") != 0)
bsdtar->return_value = 1;
}
void
tar_mode_x(struct bsdtar *bsdtar)
{
read_archive(bsdtar, 'x');
if (lafe_unmatched_inclusions_warn(bsdtar->matching, "Not found in archive") != 0)
bsdtar->return_value = 1;
}
static void
progress_func(void *cookie)
{
struct progress_data *progress_data = cookie;
struct bsdtar *bsdtar = progress_data->bsdtar;
struct archive *a = progress_data->archive;
struct archive_entry *entry = progress_data->entry;
uint64_t comp, uncomp;
if (!need_report())
return;
if (bsdtar->verbose)
fprintf(stderr, "\n");
if (a != NULL) {
comp = archive_position_compressed(a);
uncomp = archive_position_uncompressed(a);
fprintf(stderr,
"In: %s bytes, compression %d%%;",
tar_i64toa(comp), (int)((uncomp - comp) * 100 / uncomp));
fprintf(stderr, " Out: %d files, %s bytes\n",
archive_file_count(a), tar_i64toa(uncomp));
}
if (entry != NULL) {
safe_fprintf(stderr, "Current: %s",
archive_entry_pathname(entry));
fprintf(stderr, " (%s bytes)\n",
tar_i64toa(archive_entry_size(entry)));
}
}
#ifdef HAVE_QUARANTINE
void
_qtnapply(struct bsdtar *bsdtar, qtn_file_t qf, char *path)
{
int stat_ok;
struct stat sb;
int qstatus;
if (qf == NULL)
return;
stat_ok = (stat(path, &sb) == 0);
if (stat_ok) chmod(path, sb.st_mode | S_IWUSR);
qstatus = qtn_file_apply_to_path(qf, path);
if (stat_ok) chmod(path, sb.st_mode);
if (qstatus)
lafe_warnc(0, "qtn_file_apply_to_path(%s): %s", path, qtn_error(qstatus));
}
#endif
static void
read_archive(struct bsdtar *bsdtar, char mode)
{
struct progress_data progress_data;
FILE *out;
struct archive *a;
struct archive_entry *entry;
const struct stat *st;
int r;
#ifdef HAVE_QUARANTINE
qtn_file_t qf = NULL;
#endif
LIST_HEAD(copyfile_list_t, copyfile_list_entry_t) copyfile_list;
struct copyfile_list_entry_t *cle;
LIST_INIT(©file_list);
while (*bsdtar->argv) {
lafe_include(&bsdtar->matching, *bsdtar->argv);
bsdtar->argv++;
}
if (bsdtar->names_from_file != NULL)
lafe_include_from_file(&bsdtar->matching,
bsdtar->names_from_file, bsdtar->option_null);
a = archive_read_new();
if (bsdtar->compress_program != NULL)
archive_read_support_compression_program(a, bsdtar->compress_program);
else
archive_read_support_compression_all(a);
archive_read_support_format_all(a);
if (ARCHIVE_OK != archive_read_set_options(a, bsdtar->option_options))
lafe_errc(1, 0, "%s", archive_error_string(a));
if (archive_read_open_file(a, bsdtar->filename,
bsdtar->bytes_per_block != 0 ? bsdtar->bytes_per_block :
DEFAULT_BYTES_PER_BLOCK))
lafe_errc(1, 0, "Error opening archive: %s",
archive_error_string(a));
do_chdir(bsdtar);
if (mode == 'x') {
progress_data.bsdtar = bsdtar;
progress_data.archive = a;
archive_read_extract_set_progress_callback(a, progress_func,
&progress_data);
}
if (mode == 'x' && bsdtar->option_chroot) {
#if HAVE_CHROOT
if (chroot(".") != 0)
lafe_errc(1, errno, "Can't chroot to \".\"");
#else
lafe_errc(1, 0,
"chroot isn't supported on this platform");
#endif
}
#ifdef HAVE_QUARANTINE
if (mode == 'x' && bsdtar->filename != NULL && !bsdtar->option_stdout) {
if ((qf = qtn_file_alloc()) != NULL) {
int qstatus = qtn_file_init_with_path(qf, bsdtar->filename);
if (qstatus != 0) {
qtn_file_free(qf);
qf = NULL;
}
}
}
#endif
for (;;) {
const char *p;
if (bsdtar->option_fast_read &&
lafe_unmatched_inclusions(bsdtar->matching) == 0)
break;
r = archive_read_next_header(a, &entry);
progress_data.entry = entry;
if (r == ARCHIVE_EOF)
break;
if (r < ARCHIVE_OK)
lafe_warnc(0, "%s", archive_error_string(a));
if (r <= ARCHIVE_WARN)
bsdtar->return_value = 1;
if (r == ARCHIVE_RETRY) {
lafe_warnc(0, "Retrying...");
bsdtar->return_value = 1;
continue;
}
if (r == ARCHIVE_FATAL)
break;
p = archive_entry_pathname(entry);
if (p == NULL || p[0] == '\0') {
lafe_warnc(0, "Archive entry has empty or unreadable filename ... skipping.");
bsdtar->return_value = 1;
continue;
}
if (bsdtar->option_numeric_owner) {
archive_entry_set_uname(entry, NULL);
archive_entry_set_gname(entry, NULL);
}
st = archive_entry_stat(entry);
if (bsdtar->newer_ctime_sec > 0) {
if (st->st_ctime < bsdtar->newer_ctime_sec)
continue;
if (st->st_ctime == bsdtar->newer_ctime_sec
&& ARCHIVE_STAT_CTIME_NANOS(st)
<= bsdtar->newer_ctime_nsec)
continue;
}
if (bsdtar->newer_mtime_sec > 0) {
if (st->st_mtime < bsdtar->newer_mtime_sec)
continue;
if (st->st_mtime == bsdtar->newer_mtime_sec
&& ARCHIVE_STAT_MTIME_NANOS(st)
<= bsdtar->newer_mtime_nsec)
continue;
}
if (lafe_excluded(bsdtar->matching, archive_entry_pathname(entry)))
continue;
if (mode == 't') {
out = bsdtar->option_stdout ? stderr : stdout;
if (bsdtar->verbose < 2)
safe_fprintf(out, "%s",
archive_entry_pathname(entry));
else
list_item_verbose(bsdtar, out, entry);
fflush(out);
r = archive_read_data_skip(a);
if (r == ARCHIVE_WARN) {
fprintf(out, "\n");
lafe_warnc(0, "%s",
archive_error_string(a));
}
if (r == ARCHIVE_RETRY) {
fprintf(out, "\n");
lafe_warnc(0, "%s",
archive_error_string(a));
}
if (r == ARCHIVE_FATAL) {
fprintf(out, "\n");
lafe_warnc(0, "%s",
archive_error_string(a));
bsdtar->return_value = 1;
break;
}
fprintf(out, "\n");
} else {
if (edit_pathname(bsdtar, entry))
continue;
if (bsdtar->option_interactive &&
!yes("extract '%s'", archive_entry_pathname(entry)))
continue;
if (bsdtar->verbose) {
safe_fprintf(stderr, "x %s",
archive_entry_pathname(entry));
fflush(stderr);
}
if (bsdtar->option_stdout)
r = archive_read_data_into_fd(a, 1);
else {
char *bname = basename((char *)archive_entry_pathname(entry));
if (bname != NULL && strncmp(bname, "._", 2) == 0) {
cle = calloc(1, sizeof(struct copyfile_list_entry_t));
cle->src = strdup(archive_entry_pathname(entry));
asprintf(&cle->tmp, "%s.XXXXXX", cle->src);
mktemp(cle->tmp);
asprintf(&cle->dst, "%s/%s", dirname(cle->src), basename(cle->src) + 2);
LIST_INSERT_HEAD(©file_list, cle, link);
archive_entry_set_pathname(entry, cle->tmp);
}
r = archive_read_extract(a, entry,
bsdtar->extract_flags);
#ifdef HAVE_QUARANTINE
if (r == ARCHIVE_OK) {
_qtnapply(bsdtar, qf, (char *)archive_entry_pathname(entry));
}
#endif
}
if (r != ARCHIVE_OK) {
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");
bsdtar->return_value = 1;
}
if (bsdtar->verbose)
fprintf(stderr, "\n");
if (r == ARCHIVE_FATAL)
break;
}
}
r = archive_read_close(a);
if (r != ARCHIVE_OK)
lafe_warnc(0, "%s", archive_error_string(a));
if (r <= ARCHIVE_WARN)
bsdtar->return_value = 1;
LIST_FOREACH(cle, ©file_list, link) {
if (!bsdtar->disable_copyfile && copyfile(cle->tmp, cle->dst, 0, COPYFILE_UNPACK | COPYFILE_NOFOLLOW | COPYFILE_ACL | COPYFILE_XATTR) == 0) {
unlink(cle->tmp);
#ifdef HAVE_QUARANTINE
_qtnapply(bsdtar, qf, cle->dst);
#endif
} else {
if (!bsdtar->disable_copyfile)
lafe_warnc(errno, "copyfile unpack (%s) failed", cle->dst);
rename(cle->tmp, cle->src);
#ifdef HAVE_QUARANTINE
_qtnapply(bsdtar, qf, cle->src);
#endif
}
}
if (bsdtar->verbose > 2)
fprintf(stdout, "Archive Format: %s, Compression: %s\n",
archive_format_name(a), archive_compression_name(a));
archive_read_finish(a);
#ifdef HAVE_QUARANTINE
if (qf != NULL) {
qtn_file_free(qf);
qf = NULL;
}
#endif
}
static void
list_item_verbose(struct bsdtar *bsdtar, FILE *out, struct archive_entry *entry)
{
char tmp[100];
size_t w;
const char *p;
const char *fmt;
time_t tim;
static time_t now;
#ifdef __APPLE__
struct tm *lt;
#endif
if (!bsdtar->u_width) {
bsdtar->u_width = 6;
bsdtar->gs_width = 13;
}
if (!now)
time(&now);
fprintf(out, "%s %d ",
archive_entry_strmode(entry),
archive_entry_nlink(entry));
p = archive_entry_uname(entry);
if ((p == NULL) || (*p == '\0')) {
sprintf(tmp, "%lu ",
(unsigned long)archive_entry_uid(entry));
p = tmp;
}
w = strlen(p);
if (w > bsdtar->u_width)
bsdtar->u_width = w;
fprintf(out, "%-*s ", (int)bsdtar->u_width, p);
p = archive_entry_gname(entry);
if (p != NULL && p[0] != '\0') {
fprintf(out, "%s", p);
w = strlen(p);
} else {
sprintf(tmp, "%lu",
(unsigned long)archive_entry_gid(entry));
w = strlen(tmp);
fprintf(out, "%s", tmp);
}
if (archive_entry_filetype(entry) == AE_IFCHR
|| archive_entry_filetype(entry) == AE_IFBLK) {
sprintf(tmp, "%lu,%lu",
(unsigned long)archive_entry_rdevmajor(entry),
(unsigned long)archive_entry_rdevminor(entry));
} else {
strcpy(tmp, tar_i64toa(archive_entry_size(entry)));
}
if (w + strlen(tmp) >= bsdtar->gs_width)
bsdtar->gs_width = w+strlen(tmp)+1;
fprintf(out, "%*s", (int)(bsdtar->gs_width - w), tmp);
tim = archive_entry_mtime(entry);
#define HALF_YEAR (time_t)365 * 86400 / 2
#if defined(_WIN32) && !defined(__CYGWIN__)
#define DAY_FMT "%d"
#else
#define DAY_FMT "%e"
#endif
if (tim < now - HALF_YEAR || tim > now + HALF_YEAR)
fmt = bsdtar->day_first ? DAY_FMT " %b %Y" : "%b " DAY_FMT " %Y";
else
fmt = bsdtar->day_first ? DAY_FMT " %b %H:%M" : "%b " DAY_FMT " %H:%M";
#ifdef __APPLE__
lt = localtime(&tim);
if (lt == NULL) {
tim = 0;
lt = localtime(&tim);
}
strftime(tmp, sizeof(tmp), fmt, lt);
#else
strftime(tmp, sizeof(tmp), fmt, localtime(&tim));
#endif
fprintf(out, " %s ", tmp);
safe_fprintf(out, "%s", archive_entry_pathname(entry));
if (archive_entry_hardlink(entry))
safe_fprintf(out, " link to %s",
archive_entry_hardlink(entry));
else if (archive_entry_symlink(entry))
safe_fprintf(out, " -> %s", archive_entry_symlink(entry));
}