#include "bsdtar_platform.h"
__FBSDID("$FreeBSD: src/usr.bin/tar/write.c,v 1.79 2008/11/27 05:49:52 kientzle Exp $");
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#ifdef HAVE_ATTR_XATTR_H
#include <attr/xattr.h>
#endif
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef HAVE_GRP_H
#include <grp.h>
#endif
#ifdef HAVE_IO_H
#include <io.h>
#endif
#ifdef HAVE_LIMITS_H
#include <limits.h>
#endif
#ifdef HAVE_LINUX_FS_H
#include <linux/fs.h>
#endif
#ifdef HAVE_LINUX_EXT2_FS_H
#include <linux/ext2_fs.h>
#endif
#if defined(HAVE_EXT2FS_EXT2_FS_H) && !defined(__CYGWIN__)
#include <ext2fs/ext2_fs.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_UNISTD_H
#include <unistd.h>
#endif
#ifdef __APPLE__
#include <copyfile.h>
#include <libgen.h>
#include <paths.h>
#endif
#include "bsdtar.h"
#include "err.h"
#include "line_reader.h"
#include "tree.h"
#define FILEDATABUFLEN 65536
#define name_cache_size 101
#ifndef O_BINARY
#define O_BINARY 0
#endif
static const char * const NO_NAME = "(noname)";
struct archive_dir_entry {
struct archive_dir_entry *next;
time_t mtime_sec;
int mtime_nsec;
char *name;
};
struct archive_dir {
struct archive_dir_entry *head, *tail;
};
struct name_cache {
int probes;
int hits;
size_t size;
struct {
id_t id;
const char *name;
} cache[name_cache_size];
};
static void add_dir_list(struct bsdtar *bsdtar, const char *path,
time_t mtime_sec, int mtime_nsec);
static int append_archive(struct bsdtar *, struct archive *,
struct archive *ina);
static int append_archive_filename(struct bsdtar *,
struct archive *, const char *fname);
static void archive_names_from_file(struct bsdtar *bsdtar,
struct archive *a);
static int copy_file_data(struct bsdtar *, struct archive *a,
struct archive *ina, struct archive_entry *);
static int new_enough(struct bsdtar *, const char *path,
const struct stat *);
static void report_write(struct bsdtar *, struct archive *,
struct archive_entry *, int64_t progress);
static void test_for_append(struct bsdtar *);
static void write_archive(struct archive *, struct bsdtar *);
static void write_entry_backend(struct bsdtar *, struct archive *,
struct archive_entry *);
static int write_file_data(struct bsdtar *, struct archive *,
struct archive_entry *, int fd);
static void write_hierarchy(struct bsdtar *, struct archive *,
const char *);
#if defined(_WIN32) && !defined(__CYGWIN__)
static int
seek_file(int fd, int64_t offset, int whence)
{
LARGE_INTEGER distance;
(void)whence;
distance.QuadPart = offset;
return (SetFilePointerEx((HANDLE)_get_osfhandle(fd),
distance, NULL, FILE_BEGIN) ? 1 : -1);
}
#define open _open
#define close _close
#define read _read
#define lseek seek_file
#endif
void
tar_mode_c(struct bsdtar *bsdtar)
{
struct archive *a;
int r;
if (*bsdtar->argv == NULL && bsdtar->names_from_file == NULL)
lafe_errc(1, 0, "no files or directories specified");
a = archive_write_new();
if (bsdtar->create_format == NULL) {
r = archive_write_set_format_pax_restricted(a);
bsdtar->create_format = "pax restricted";
} else {
r = archive_write_set_format_by_name(a, bsdtar->create_format);
}
if (r != ARCHIVE_OK) {
fprintf(stderr, "Can't use format %s: %s\n",
bsdtar->create_format,
archive_error_string(a));
usage();
}
if (bsdtar->bytes_per_block != 0) {
archive_write_set_bytes_per_block(a, bsdtar->bytes_per_block);
archive_write_set_bytes_in_last_block(a,
bsdtar->bytes_per_block);
} else
archive_write_set_bytes_per_block(a, DEFAULT_BYTES_PER_BLOCK);
if (bsdtar->compress_program) {
archive_write_set_compression_program(a, bsdtar->compress_program);
} else {
switch (bsdtar->create_compression) {
case 0:
r = archive_write_set_compression_none(a);
break;
case 'j': case 'y':
r = archive_write_set_compression_bzip2(a);
break;
case 'J':
r = archive_write_set_compression_xz(a);
break;
case OPTION_LZMA:
r = archive_write_set_compression_lzma(a);
break;
case 'z':
r = archive_write_set_compression_gzip(a);
break;
case 'Z':
r = archive_write_set_compression_compress(a);
break;
default:
lafe_errc(1, 0,
"Unrecognized compression option -%c",
bsdtar->create_compression);
}
if (r != ARCHIVE_OK) {
lafe_errc(1, 0,
"Unsupported compression option -%c",
bsdtar->create_compression);
}
}
if (ARCHIVE_OK != archive_write_set_options(a, bsdtar->option_options))
lafe_errc(1, 0, "%s", archive_error_string(a));
if (ARCHIVE_OK != archive_write_open_file(a, bsdtar->filename))
lafe_errc(1, 0, "%s", archive_error_string(a));
write_archive(a, bsdtar);
}
void
tar_mode_r(struct bsdtar *bsdtar)
{
int64_t end_offset;
int format;
struct archive *a;
struct archive_entry *entry;
int r;
test_for_append(bsdtar);
format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
#if defined(__BORLANDC__)
bsdtar->fd = open(bsdtar->filename, O_RDWR | O_CREAT | O_BINARY);
#else
bsdtar->fd = open(bsdtar->filename, O_RDWR | O_CREAT | O_BINARY, 0666);
#endif
if (bsdtar->fd < 0)
lafe_errc(1, errno,
"Cannot open %s", bsdtar->filename);
a = archive_read_new();
archive_read_support_compression_all(a);
archive_read_support_format_tar(a);
archive_read_support_format_gnutar(a);
r = archive_read_open_fd(a, bsdtar->fd, 10240);
if (r != ARCHIVE_OK)
lafe_errc(1, archive_errno(a),
"Can't read archive %s: %s", bsdtar->filename,
archive_error_string(a));
while (0 == archive_read_next_header(a, &entry)) {
if (archive_compression(a) != ARCHIVE_COMPRESSION_NONE) {
archive_read_finish(a);
close(bsdtar->fd);
lafe_errc(1, 0,
"Cannot append to compressed archive.");
}
format = archive_format(a);
}
end_offset = archive_read_header_position(a);
archive_read_finish(a);
a = archive_write_new();
archive_write_set_compression_none(a);
if (bsdtar->create_format != NULL) {
archive_write_set_format_by_name(a,
bsdtar->create_format);
format &= ARCHIVE_FORMAT_BASE_MASK;
if (format != (int)(archive_format(a) & ARCHIVE_FORMAT_BASE_MASK)
&& format != ARCHIVE_FORMAT_EMPTY) {
lafe_errc(1, 0,
"Format %s is incompatible with the archive %s.",
bsdtar->create_format, bsdtar->filename);
}
} else {
if (format == ARCHIVE_FORMAT_TAR_GNUTAR)
format = ARCHIVE_FORMAT_TAR_USTAR;
if (format == ARCHIVE_FORMAT_EMPTY)
format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
archive_write_set_format(a, format);
}
if (lseek(bsdtar->fd, end_offset, SEEK_SET) < 0)
lafe_errc(1, errno, "Could not seek to archive end");
if (ARCHIVE_OK != archive_write_set_options(a, bsdtar->option_options))
lafe_errc(1, 0, "%s", archive_error_string(a));
if (ARCHIVE_OK != archive_write_open_fd(a, bsdtar->fd))
lafe_errc(1, 0, "%s", archive_error_string(a));
write_archive(a, bsdtar);
close(bsdtar->fd);
bsdtar->fd = -1;
}
void
tar_mode_u(struct bsdtar *bsdtar)
{
int64_t end_offset;
struct archive *a;
struct archive_entry *entry;
int format;
struct archive_dir_entry *p;
struct archive_dir archive_dir;
bsdtar->archive_dir = &archive_dir;
memset(&archive_dir, 0, sizeof(archive_dir));
format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
test_for_append(bsdtar);
bsdtar->fd = open(bsdtar->filename, O_RDWR | O_BINARY);
if (bsdtar->fd < 0)
lafe_errc(1, errno,
"Cannot open %s", bsdtar->filename);
a = archive_read_new();
archive_read_support_compression_all(a);
archive_read_support_format_tar(a);
archive_read_support_format_gnutar(a);
if (archive_read_open_fd(a, bsdtar->fd,
bsdtar->bytes_per_block != 0 ? bsdtar->bytes_per_block :
DEFAULT_BYTES_PER_BLOCK) != ARCHIVE_OK) {
lafe_errc(1, 0,
"Can't open %s: %s", bsdtar->filename,
archive_error_string(a));
}
while (0 == archive_read_next_header(a, &entry)) {
if (archive_compression(a) != ARCHIVE_COMPRESSION_NONE) {
archive_read_finish(a);
close(bsdtar->fd);
lafe_errc(1, 0,
"Cannot append to compressed archive.");
}
add_dir_list(bsdtar, archive_entry_pathname(entry),
archive_entry_mtime(entry),
archive_entry_mtime_nsec(entry));
format = archive_format(a);
}
end_offset = archive_read_header_position(a);
archive_read_finish(a);
a = archive_write_new();
archive_write_set_compression_none(a);
if (format == ARCHIVE_FORMAT_TAR_GNUTAR)
format = ARCHIVE_FORMAT_TAR_USTAR;
archive_write_set_format(a, format);
if (bsdtar->bytes_per_block != 0) {
archive_write_set_bytes_per_block(a, bsdtar->bytes_per_block);
archive_write_set_bytes_in_last_block(a,
bsdtar->bytes_per_block);
} else
archive_write_set_bytes_per_block(a, DEFAULT_BYTES_PER_BLOCK);
if (lseek(bsdtar->fd, end_offset, SEEK_SET) < 0)
lafe_errc(1, errno, "Could not seek to archive end");
if (ARCHIVE_OK != archive_write_set_options(a, bsdtar->option_options))
lafe_errc(1, 0, "%s", archive_error_string(a));
if (ARCHIVE_OK != archive_write_open_fd(a, bsdtar->fd))
lafe_errc(1, 0, "%s", archive_error_string(a));
write_archive(a, bsdtar);
close(bsdtar->fd);
bsdtar->fd = -1;
while (bsdtar->archive_dir->head != NULL) {
p = bsdtar->archive_dir->head->next;
free(bsdtar->archive_dir->head->name);
free(bsdtar->archive_dir->head);
bsdtar->archive_dir->head = p;
}
bsdtar->archive_dir->tail = NULL;
}
static void
write_archive(struct archive *a, struct bsdtar *bsdtar)
{
const char *arg;
struct archive_entry *entry, *sparse_entry;
if ((bsdtar->buff = malloc(FILEDATABUFLEN)) == NULL)
lafe_errc(1, 0, "cannot allocate memory");
if ((bsdtar->resolver = archive_entry_linkresolver_new()) == NULL)
lafe_errc(1, 0, "cannot create link resolver");
archive_entry_linkresolver_set_strategy(bsdtar->resolver,
archive_format(a));
if ((bsdtar->diskreader = archive_read_disk_new()) == NULL)
lafe_errc(1, 0, "Cannot create read_disk object");
archive_read_disk_set_standard_lookup(bsdtar->diskreader);
if (bsdtar->names_from_file != NULL)
archive_names_from_file(bsdtar, a);
while (*bsdtar->argv) {
arg = *bsdtar->argv;
if (arg[0] == '-' && arg[1] == 'C') {
arg += 2;
if (*arg == '\0') {
bsdtar->argv++;
arg = *bsdtar->argv;
if (arg == NULL) {
lafe_warnc(0, "%s",
"Missing argument for -C");
bsdtar->return_value = 1;
goto cleanup;
}
}
set_chdir(bsdtar, arg);
} else {
if (*arg != '/' && (arg[0] != '@' || arg[1] != '/'))
do_chdir(bsdtar);
if (*arg == '@') {
if (append_archive_filename(bsdtar, a,
arg + 1) != 0)
break;
} else
write_hierarchy(bsdtar, a, arg);
}
bsdtar->argv++;
}
entry = NULL;
archive_entry_linkify(bsdtar->resolver, &entry, &sparse_entry);
while (entry != NULL) {
write_entry_backend(bsdtar, a, entry);
archive_entry_free(entry);
entry = NULL;
archive_entry_linkify(bsdtar->resolver, &entry, &sparse_entry);
}
if (archive_write_close(a)) {
lafe_warnc(0, "%s", archive_error_string(a));
bsdtar->return_value = 1;
}
cleanup:
free(bsdtar->buff);
archive_entry_linkresolver_free(bsdtar->resolver);
bsdtar->resolver = NULL;
archive_read_finish(bsdtar->diskreader);
bsdtar->diskreader = NULL;
if (bsdtar->option_totals) {
fprintf(stderr, "Total bytes written: %s\n",
tar_i64toa(archive_position_compressed(a)));
}
archive_write_finish(a);
}
static void
archive_names_from_file(struct bsdtar *bsdtar, struct archive *a)
{
struct lafe_line_reader *lr;
const char *line;
bsdtar->next_line_is_dir = 0;
lr = lafe_line_reader(bsdtar->names_from_file, bsdtar->option_null);
while ((line = lafe_line_reader_next(lr)) != NULL) {
if (bsdtar->next_line_is_dir) {
set_chdir(bsdtar, line);
bsdtar->next_line_is_dir = 0;
} else if (!bsdtar->option_null && strcmp(line, "-C") == 0)
bsdtar->next_line_is_dir = 1;
else {
if (*line != '/')
do_chdir(bsdtar);
write_hierarchy(bsdtar, a, line);
}
}
lafe_line_reader_free(lr);
if (bsdtar->next_line_is_dir)
lafe_errc(1, errno,
"Unexpected end of filename list; "
"directory expected after -C");
}
static int
append_archive_filename(struct bsdtar *bsdtar, struct archive *a,
const char *filename)
{
struct archive *ina;
int rc;
if (strcmp(filename, "-") == 0)
filename = NULL;
ina = archive_read_new();
archive_read_support_format_all(ina);
archive_read_support_compression_all(ina);
if (archive_read_open_file(ina, filename, 10240)) {
lafe_warnc(0, "%s", archive_error_string(ina));
bsdtar->return_value = 1;
return (0);
}
rc = append_archive(bsdtar, a, ina);
if (rc != ARCHIVE_OK) {
lafe_warnc(0, "Error reading archive %s: %s",
filename, archive_error_string(ina));
bsdtar->return_value = 1;
}
archive_read_finish(ina);
return (rc);
}
static int
append_archive(struct bsdtar *bsdtar, struct archive *a, struct archive *ina)
{
struct archive_entry *in_entry;
int e;
while (0 == archive_read_next_header(ina, &in_entry)) {
if (!new_enough(bsdtar, archive_entry_pathname(in_entry),
archive_entry_stat(in_entry)))
continue;
if (lafe_excluded(bsdtar->matching, archive_entry_pathname(in_entry)))
continue;
if (bsdtar->option_interactive &&
!yes("copy '%s'", archive_entry_pathname(in_entry)))
continue;
if (bsdtar->verbose)
safe_fprintf(stderr, "a %s",
archive_entry_pathname(in_entry));
if (need_report())
report_write(bsdtar, a, in_entry, 0);
e = archive_write_header(a, in_entry);
if (e != ARCHIVE_OK) {
if (!bsdtar->verbose)
lafe_warnc(0, "%s: %s",
archive_entry_pathname(in_entry),
archive_error_string(a));
else
fprintf(stderr, ": %s", archive_error_string(a));
}
if (e == ARCHIVE_FATAL)
exit(1);
if (e >= ARCHIVE_WARN) {
if (archive_entry_size(in_entry) == 0)
archive_read_data_skip(ina);
else if (copy_file_data(bsdtar, a, ina, in_entry))
exit(1);
}
if (bsdtar->verbose)
fprintf(stderr, "\n");
}
return (0);
}
static int
copy_file_data(struct bsdtar *bsdtar, struct archive *a,
struct archive *ina, struct archive_entry *entry)
{
ssize_t bytes_read;
ssize_t bytes_written;
int64_t progress = 0;
bytes_read = archive_read_data(ina, bsdtar->buff, FILEDATABUFLEN);
while (bytes_read > 0) {
if (need_report())
report_write(bsdtar, a, entry, progress);
bytes_written = archive_write_data(a, bsdtar->buff,
bytes_read);
if (bytes_written < bytes_read) {
lafe_warnc(0, "%s", archive_error_string(a));
return (-1);
}
progress += bytes_written;
bytes_read = archive_read_data(ina, bsdtar->buff,
FILEDATABUFLEN);
}
return (0);
}
static void
write_hierarchy(struct bsdtar *bsdtar, struct archive *a, const char *path)
{
struct archive_entry *entry = NULL, *spare_entry = NULL;
struct tree *tree;
char symlink_mode = bsdtar->symlink_mode;
dev_t first_dev = 0;
int dev_recorded = 0;
int tree_ret;
tree = tree_open(path);
if (!tree) {
lafe_warnc(errno, "%s: Cannot open", path);
bsdtar->return_value = 1;
return;
}
while ((tree_ret = tree_next(tree)) != 0) {
int r;
const char *name = tree_current_path(tree);
const struct stat *st = NULL;
const struct stat *lst = NULL;
int descend;
if (tree_ret == TREE_ERROR_FATAL)
lafe_errc(1, tree_errno(tree),
"%s: Unable to continue traversing directory tree",
name);
if (tree_ret == TREE_ERROR_DIR) {
lafe_warnc(errno,
"%s: Couldn't visit directory", name);
bsdtar->return_value = 1;
}
if (tree_ret != TREE_REGULAR)
continue;
if (lafe_excluded(bsdtar->matching, name))
continue;
lst = tree_current_lstat(tree);
if (lst == NULL) {
lafe_warnc(errno, "%s: Cannot stat", name);
bsdtar->return_value = 1;
continue;
}
switch(symlink_mode) {
case 'H':
symlink_mode = 'P';
case 'L':
descend = tree_current_is_dir(tree);
archive_read_disk_set_symlink_logical(bsdtar->diskreader);
st = tree_current_stat(tree);
if (st != NULL)
break;
default:
descend = tree_current_is_physical_dir(tree);
archive_read_disk_set_symlink_physical(bsdtar->diskreader);
st = lst;
break;
}
if (!dev_recorded) {
first_dev = lst->st_dev;
dev_recorded = 1;
} else if (lst->st_dev == first_dev) {
} else if (descend == 0) {
} else if (bsdtar->option_dont_traverse_mounts) {
descend = 0;
} else {
}
if (!new_enough(bsdtar, name, st))
continue;
archive_entry_free(entry);
entry = archive_entry_new();
archive_entry_set_pathname(entry, name);
archive_entry_copy_sourcepath(entry,
tree_current_access_path(tree));
#if defined(_WIN32) && !defined(__CYGWIN__)
st = NULL;
#endif
r = archive_read_disk_entry_from_file(bsdtar->diskreader,
entry, -1, st);
if (r != ARCHIVE_OK)
lafe_warnc(archive_errno(bsdtar->diskreader),
"%s", archive_error_string(bsdtar->diskreader));
if (r < ARCHIVE_WARN)
continue;
#if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP)
if (bsdtar->option_honor_nodump &&
(lst->st_flags & UF_NODUMP))
continue;
#endif
#if defined(EXT2_IOC_GETFLAGS) && defined(EXT2_NODUMP_FL)
if (bsdtar->option_honor_nodump) {
int fd = open(name, O_RDONLY | O_NONBLOCK | O_BINARY);
if (fd >= 0) {
unsigned long fflags;
int r = ioctl(fd, EXT2_IOC_GETFLAGS, &fflags);
close(fd);
if (r >= 0 && (fflags & EXT2_NODUMP_FL))
continue;
}
}
#endif
if (bsdtar->option_interactive &&
!yes("add '%s'", name))
continue;
if (descend && !bsdtar->option_no_subdirs)
tree_descend(tree);
if (edit_pathname(bsdtar, entry))
continue;
#ifdef __APPLE__
const char *bname = basename((char *)archive_entry_pathname(entry));
const char *apath = archive_entry_sourcepath(entry);
if (!bsdtar->disable_copyfile && strncmp(bname, "._", 2) == 0) {
continue;
}
if (!bsdtar->disable_copyfile && strncmp(bname, "._", 2) != 0) {
if (copyfile(apath, NULL, 0, COPYFILE_CHECK | COPYFILE_NOFOLLOW | COPYFILE_ACL | COPYFILE_XATTR) != 0) {
const char *tempdir = NULL;
char *md_p = NULL;
if (issetugid() == 0)
tempdir = getenv("TMPDIR");
if (tempdir == NULL)
tempdir = _PATH_TMP;
md_p = tempnam(tempdir, "tar.md.");
if (md_p != NULL) {
char *copyfile_fname;
struct stat copyfile_stat;
struct archive_entry *entry_p;
asprintf(©file_fname, "%s/._%s", dirname((char *)name), bname);
copyfile(apath, md_p, 0, COPYFILE_PACK | COPYFILE_NOFOLLOW | COPYFILE_ACL | COPYFILE_XATTR);
stat(md_p, ©file_stat);
entry_p = archive_entry_new();
archive_entry_set_pathname(entry_p, copyfile_fname);
archive_entry_copy_sourcepath(entry_p, md_p);
archive_read_disk_entry_from_file(bsdtar->diskreader, entry_p, -1, ©file_stat);
write_entry_backend(bsdtar, a, entry_p);
archive_entry_free(entry_p);
unlink(md_p);
free(copyfile_fname);
}
free(md_p);
}
}
#endif
if (bsdtar->verbose)
safe_fprintf(stderr, "a %s",
archive_entry_pathname(entry));
if (archive_entry_filetype(entry) != AE_IFREG)
archive_entry_set_size(entry, 0);
archive_entry_linkify(bsdtar->resolver, &entry, &spare_entry);
while (entry != NULL) {
write_entry_backend(bsdtar, a, entry);
archive_entry_free(entry);
entry = spare_entry;
spare_entry = NULL;
}
if (bsdtar->verbose)
fprintf(stderr, "\n");
}
archive_entry_free(entry);
tree_close(tree);
}
static void
write_entry_backend(struct bsdtar *bsdtar, struct archive *a,
struct archive_entry *entry)
{
int fd = -1;
int e;
if (archive_entry_size(entry) > 0) {
const char *pathname = archive_entry_sourcepath(entry);
fd = open(pathname, O_RDONLY | O_BINARY);
if (fd == -1) {
if (!bsdtar->verbose)
lafe_warnc(errno,
"%s: could not open file", pathname);
else
fprintf(stderr, ": %s", strerror(errno));
return;
}
}
e = archive_write_header(a, entry);
if (e != ARCHIVE_OK) {
if (!bsdtar->verbose)
lafe_warnc(0, "%s: %s",
archive_entry_pathname(entry),
archive_error_string(a));
else
fprintf(stderr, ": %s", archive_error_string(a));
}
if (e == ARCHIVE_FATAL)
exit(1);
if (e >= ARCHIVE_WARN && fd >= 0 && archive_entry_size(entry) > 0) {
if (write_file_data(bsdtar, a, entry, fd))
exit(1);
}
if (fd >= 0)
close(fd);
}
static void
report_write(struct bsdtar *bsdtar, struct archive *a,
struct archive_entry *entry, int64_t progress)
{
uint64_t comp, uncomp;
if (bsdtar->verbose)
fprintf(stderr, "\n");
comp = archive_position_compressed(a);
uncomp = archive_position_uncompressed(a);
fprintf(stderr, "In: %d files, %s bytes;",
archive_file_count(a), tar_i64toa(uncomp));
fprintf(stderr,
" Out: %s bytes, compression %d%%\n",
tar_i64toa(comp), (int)((uncomp - comp) * 100 / uncomp));
safe_fprintf(stderr, "Current: %s (%s",
archive_entry_pathname(entry),
tar_i64toa(progress));
fprintf(stderr, "/%s bytes)\n",
tar_i64toa(archive_entry_size(entry)));
}
static int
write_file_data(struct bsdtar *bsdtar, struct archive *a,
struct archive_entry *entry, int fd)
{
ssize_t bytes_read;
ssize_t bytes_written;
int64_t progress = 0;
bytes_read = read(fd, bsdtar->buff, FILEDATABUFLEN);
while (bytes_read > 0) {
if (need_report())
report_write(bsdtar, a, entry, progress);
bytes_written = archive_write_data(a, bsdtar->buff,
bytes_read);
if (bytes_written < 0) {
lafe_warnc(0, "%s", archive_error_string(a));
return (-1);
}
if (bytes_written < bytes_read) {
lafe_warnc(0,
"%s: Truncated write; file may have grown while being archived.",
archive_entry_pathname(entry));
return (0);
}
progress += bytes_written;
bytes_read = read(fd, bsdtar->buff, FILEDATABUFLEN);
}
return 0;
}
static int
new_enough(struct bsdtar *bsdtar, const char *path, const struct stat *st)
{
struct archive_dir_entry *p;
if (bsdtar->newer_ctime_sec > 0) {
if (st->st_ctime < bsdtar->newer_ctime_sec)
return (0);
if (st->st_ctime == bsdtar->newer_ctime_sec
&& ARCHIVE_STAT_CTIME_NANOS(st)
<= bsdtar->newer_ctime_nsec)
return (0);
}
if (bsdtar->newer_mtime_sec > 0) {
if (st->st_mtime < bsdtar->newer_mtime_sec)
return (0);
if (st->st_mtime == bsdtar->newer_mtime_sec
&& ARCHIVE_STAT_MTIME_NANOS(st)
<= bsdtar->newer_mtime_nsec)
return (0);
}
if (bsdtar->archive_dir != NULL &&
bsdtar->archive_dir->head != NULL) {
for (p = bsdtar->archive_dir->head; p != NULL; p = p->next) {
if (pathcmp(path, p->name)==0)
return (p->mtime_sec < st->st_mtime ||
(p->mtime_sec == st->st_mtime &&
p->mtime_nsec
< ARCHIVE_STAT_MTIME_NANOS(st)));
}
}
return (1);
}
static void
add_dir_list(struct bsdtar *bsdtar, const char *path,
time_t mtime_sec, int mtime_nsec)
{
struct archive_dir_entry *p;
p = bsdtar->archive_dir->head;
while (p != NULL) {
if (strcmp(path, p->name)==0) {
p->mtime_sec = mtime_sec;
p->mtime_nsec = mtime_nsec;
return;
}
p = p->next;
}
p = malloc(sizeof(*p));
if (p == NULL)
lafe_errc(1, ENOMEM, "Can't read archive directory");
p->name = strdup(path);
if (p->name == NULL)
lafe_errc(1, ENOMEM, "Can't read archive directory");
p->mtime_sec = mtime_sec;
p->mtime_nsec = mtime_nsec;
p->next = NULL;
if (bsdtar->archive_dir->tail == NULL) {
bsdtar->archive_dir->head = bsdtar->archive_dir->tail = p;
} else {
bsdtar->archive_dir->tail->next = p;
bsdtar->archive_dir->tail = p;
}
}
static void
test_for_append(struct bsdtar *bsdtar)
{
struct stat s;
if (*bsdtar->argv == NULL && bsdtar->names_from_file == NULL)
lafe_errc(1, 0, "no files or directories specified");
if (bsdtar->filename == NULL)
lafe_errc(1, 0, "Cannot append to stdout.");
if (bsdtar->create_compression != 0)
lafe_errc(1, 0,
"Cannot append to %s with compression", bsdtar->filename);
if (stat(bsdtar->filename, &s) != 0)
return;
if (!S_ISREG(s.st_mode) && !S_ISBLK(s.st_mode))
lafe_errc(1, 0,
"Cannot append to %s: not a regular file.",
bsdtar->filename);
}