#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 MAJOR_IN_MKDEV
#include <sys/mkdev.h>
#elif defined(MAJOR_IN_SYSMACROS)
#include <sys/sysmacros.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
#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_EMBEDDED
#define HAVE_QUARANTINE 1
#endif
#ifdef HAVE_QUARANTINE
#include <quarantine.h>
#endif
#include "bsdtar.h"
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');
unmatched_inclusions_warn(bsdtar, "Not found in archive");
}
void
tar_mode_x(struct bsdtar *bsdtar)
{
siginfo_init(bsdtar);
read_archive(bsdtar, 'x');
unmatched_inclusions_warn(bsdtar, "Not found in archive");
siginfo_done(bsdtar);
}
static void
progress_func(void * cookie)
{
struct bsdtar * bsdtar = cookie;
siginfo_printinfo(bsdtar, 0);
}
#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)
bsdtar_warnc(bsdtar, 0, "qtn_file_apply_to_path(%s): %s", path, qtn_error(qstatus));
}
#endif
static void
read_archive(struct bsdtar *bsdtar, char mode)
{
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) {
include(bsdtar, *bsdtar->argv);
bsdtar->argv++;
}
if (bsdtar->names_from_file != NULL)
include_from_file(bsdtar, bsdtar->names_from_file);
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_read_open_file(a, bsdtar->filename,
bsdtar->bytes_per_block != 0 ? bsdtar->bytes_per_block :
DEFAULT_BYTES_PER_BLOCK))
bsdtar_errc(bsdtar, 1, 0, "Error opening archive: %s",
archive_error_string(a));
do_chdir(bsdtar);
if (mode == 'x') {
archive_read_extract_set_progress_callback(a, progress_func,
bsdtar);
}
if (mode == 'x' && bsdtar->option_chroot) {
#if HAVE_CHROOT
if (chroot(".") != 0)
bsdtar_errc(bsdtar, 1, errno, "Can't chroot to \".\"");
#else
bsdtar_errc(bsdtar, 1, 0,
"chroot isn't supported on this platform");
#endif
}
#ifdef HAVE_QUARANTINE
if (mode == 'x' && !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 (;;) {
if (bsdtar->option_fast_read &&
unmatched_inclusions(bsdtar) == 0)
break;
r = archive_read_next_header(a, &entry);
if (r == ARCHIVE_EOF)
break;
if (r < ARCHIVE_OK)
bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(a));
if (r <= ARCHIVE_WARN)
bsdtar->return_value = 1;
if (r == ARCHIVE_RETRY) {
bsdtar_warnc(bsdtar, 0, "Retrying...");
continue;
}
if (r == ARCHIVE_FATAL)
break;
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 (excluded(bsdtar, 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");
bsdtar_warnc(bsdtar, 0, "%s",
archive_error_string(a));
}
if (r == ARCHIVE_RETRY) {
fprintf(out, "\n");
bsdtar_warnc(bsdtar, 0, "%s",
archive_error_string(a));
}
if (r == ARCHIVE_FATAL) {
fprintf(out, "\n");
bsdtar_warnc(bsdtar, 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);
}
siginfo_setinfo(bsdtar, "extracting",
archive_entry_pathname(entry), 0);
siginfo_printinfo(bsdtar, 0);
if (bsdtar->option_stdout)
r = archive_read_data_into_fd(a, 1);
else {
if (strncmp(basename((char *)archive_entry_pathname(entry)), "._", 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, 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;
}
}
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)
bsdtar_warnc(bsdtar, 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)
{
const struct stat *st;
char tmp[100];
size_t w;
const char *p;
const char *fmt;
time_t tim;
static time_t now;
st = archive_entry_stat(entry);
if (!bsdtar->u_width) {
bsdtar->u_width = 6;
bsdtar->gs_width = 13;
}
if (!now)
time(&now);
fprintf(out, "%s %d ",
archive_entry_strmode(entry),
(int)(st->st_nlink));
p = archive_entry_uname(entry);
if ((p == NULL) || (*p == '\0')) {
sprintf(tmp, "%lu ", (unsigned long)st->st_uid);
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)st->st_gid);
w = strlen(tmp);
fprintf(out, "%s", tmp);
}
if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
sprintf(tmp, "%lu,%lu",
(unsigned long)major(st->st_rdev),
(unsigned long)minor(st->st_rdev));
} else {
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);
tim = (time_t)st->st_mtime;
if (abs(tim - now) > (365/2)*86400)
fmt = bsdtar->day_first ? "%e %b %Y" : "%b %e %Y";
else
fmt = bsdtar->day_first ? "%e %b %H:%M" : "%b %e %H:%M";
strftime(tmp, sizeof(tmp), fmt, localtime(&tim));
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 (S_ISLNK(st->st_mode))
safe_fprintf(out, " -> %s", archive_entry_symlink(entry));
}