Blame tar/write.c

Packit Service 1d0348
/*-
Packit Service 1d0348
 * Copyright (c) 2003-2007 Tim Kientzle
Packit Service 1d0348
 * Copyright (c) 2012 Michihiro NAKAJIMA
Packit Service 1d0348
 * All rights reserved.
Packit Service 1d0348
 *
Packit Service 1d0348
 * Redistribution and use in source and binary forms, with or without
Packit Service 1d0348
 * modification, are permitted provided that the following conditions
Packit Service 1d0348
 * are met:
Packit Service 1d0348
 * 1. Redistributions of source code must retain the above copyright
Packit Service 1d0348
 *    notice, this list of conditions and the following disclaimer.
Packit Service 1d0348
 * 2. Redistributions in binary form must reproduce the above copyright
Packit Service 1d0348
 *    notice, this list of conditions and the following disclaimer in the
Packit Service 1d0348
 *    documentation and/or other materials provided with the distribution.
Packit Service 1d0348
 *
Packit Service 1d0348
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
Packit Service 1d0348
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
Packit Service 1d0348
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
Packit Service 1d0348
 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
Packit Service 1d0348
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
Packit Service 1d0348
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit Service 1d0348
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit Service 1d0348
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit Service 1d0348
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
Packit Service 1d0348
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit Service 1d0348
 */
Packit Service 1d0348
Packit Service 1d0348
#include "bsdtar_platform.h"
Packit Service 1d0348
__FBSDID("$FreeBSD: src/usr.bin/tar/write.c,v 1.79 2008/11/27 05:49:52 kientzle Exp $");
Packit Service 1d0348
Packit Service 1d0348
#ifdef HAVE_SYS_TYPES_H
Packit Service 1d0348
#include <sys/types.h>
Packit Service 1d0348
#endif
Packit Service 1d0348
#ifdef HAVE_SYS_STAT_H
Packit Service 1d0348
#include <sys/stat.h>
Packit Service 1d0348
#endif
Packit Service 1d0348
#ifdef HAVE_ATTR_XATTR_H
Packit Service 1d0348
#include <attr/xattr.h>
Packit Service 1d0348
#endif
Packit Service 1d0348
#ifdef HAVE_ERRNO_H
Packit Service 1d0348
#include <errno.h>
Packit Service 1d0348
#endif
Packit Service 1d0348
#ifdef HAVE_FCNTL_H
Packit Service 1d0348
#include <fcntl.h>
Packit Service 1d0348
#endif
Packit Service 1d0348
#ifdef HAVE_GRP_H
Packit Service 1d0348
#include <grp.h>
Packit Service 1d0348
#endif
Packit Service 1d0348
#ifdef HAVE_IO_H
Packit Service 1d0348
#include <io.h>
Packit Service 1d0348
#endif
Packit Service 1d0348
#ifdef HAVE_LIBGEN_H
Packit Service 1d0348
#include <libgen.h>
Packit Service 1d0348
#endif
Packit Service 1d0348
#ifdef HAVE_LIMITS_H
Packit Service 1d0348
#include <limits.h>
Packit Service 1d0348
#endif
Packit Service 1d0348
#ifdef HAVE_PATHS_H
Packit Service 1d0348
#include <paths.h>
Packit Service 1d0348
#endif
Packit Service 1d0348
#ifdef HAVE_PWD_H
Packit Service 1d0348
#include <pwd.h>
Packit Service 1d0348
#endif
Packit Service 1d0348
#ifdef HAVE_STDINT_H
Packit Service 1d0348
#include <stdint.h>
Packit Service 1d0348
#endif
Packit Service 1d0348
#include <stdio.h>
Packit Service 1d0348
#ifdef HAVE_STDLIB_H
Packit Service 1d0348
#include <stdlib.h>
Packit Service 1d0348
#endif
Packit Service 1d0348
#ifdef HAVE_STRING_H
Packit Service 1d0348
#include <string.h>
Packit Service 1d0348
#endif
Packit Service 1d0348
#ifdef HAVE_UNISTD_H
Packit Service 1d0348
#include <unistd.h>
Packit Service 1d0348
#endif
Packit Service 1d0348
Packit Service 1d0348
#include "bsdtar.h"
Packit Service 1d0348
#include "err.h"
Packit Service 1d0348
#include "line_reader.h"
Packit Service 1d0348
Packit Service 1d0348
#ifndef O_BINARY
Packit Service 1d0348
#define	O_BINARY 0
Packit Service 1d0348
#endif
Packit Service 1d0348
Packit Service 1d0348
struct archive_dir_entry {
Packit Service 1d0348
	struct archive_dir_entry	*next;
Packit Service 1d0348
	time_t			 mtime_sec;
Packit Service 1d0348
	int			 mtime_nsec;
Packit Service 1d0348
	char			*name;
Packit Service 1d0348
};
Packit Service 1d0348
Packit Service 1d0348
struct archive_dir {
Packit Service 1d0348
	struct archive_dir_entry *head, *tail;
Packit Service 1d0348
};
Packit Service 1d0348
Packit Service 1d0348
static int		 append_archive(struct bsdtar *, struct archive *,
Packit Service 1d0348
			     struct archive *ina);
Packit Service 1d0348
static int		 append_archive_filename(struct bsdtar *,
Packit Service 1d0348
			     struct archive *, const char *fname);
Packit Service 1d0348
static void		 archive_names_from_file(struct bsdtar *bsdtar,
Packit Service 1d0348
			     struct archive *a);
Packit Service 1d0348
static int		 copy_file_data_block(struct bsdtar *,
Packit Service 1d0348
			     struct archive *a, struct archive *,
Packit Service 1d0348
			     struct archive_entry *);
Packit Service 1d0348
static void		 excluded_callback(struct archive *, void *,
Packit Service 1d0348
			     struct archive_entry *);
Packit Service 1d0348
static void		 report_write(struct bsdtar *, struct archive *,
Packit Service 1d0348
			     struct archive_entry *, int64_t progress);
Packit Service 1d0348
static void		 test_for_append(struct bsdtar *);
Packit Service 1d0348
static int		 metadata_filter(struct archive *, void *,
Packit Service 1d0348
			     struct archive_entry *);
Packit Service 1d0348
static void		 write_archive(struct archive *, struct bsdtar *);
Packit Service 1d0348
static void		 write_entry(struct bsdtar *, struct archive *,
Packit Service 1d0348
			     struct archive_entry *);
Packit Service 1d0348
static void		 write_file(struct bsdtar *, struct archive *,
Packit Service 1d0348
			     struct archive_entry *);
Packit Service 1d0348
static void		 write_hierarchy(struct bsdtar *, struct archive *,
Packit Service 1d0348
			     const char *);
Packit Service 1d0348
Packit Service 1d0348
#if defined(_WIN32) && !defined(__CYGWIN__)
Packit Service 1d0348
/* Not a full lseek() emulation, but enough for our needs here. */
Packit Service 1d0348
static int
Packit Service 1d0348
seek_file(int fd, int64_t offset, int whence)
Packit Service 1d0348
{
Packit Service 1d0348
	LARGE_INTEGER distance;
Packit Service 1d0348
	(void)whence; /* UNUSED */
Packit Service 1d0348
	distance.QuadPart = offset;
Packit Service 1d0348
	return (SetFilePointerEx((HANDLE)_get_osfhandle(fd),
Packit Service 1d0348
		distance, NULL, FILE_BEGIN) ? 1 : -1);
Packit Service 1d0348
}
Packit Service 1d0348
#define	open _open
Packit Service 1d0348
#define	close _close
Packit Service 1d0348
#define	read _read
Packit Service 1d0348
#ifdef lseek
Packit Service 1d0348
#undef lseek
Packit Service 1d0348
#endif
Packit Service 1d0348
#define	lseek seek_file
Packit Service 1d0348
#endif
Packit Service 1d0348
Packit Service 1d0348
static void
Packit Service 1d0348
set_writer_options(struct bsdtar *bsdtar, struct archive *a)
Packit Service 1d0348
{
Packit Service 1d0348
	const char *writer_options;
Packit Service 1d0348
	int r;
Packit Service 1d0348
Packit Service 1d0348
	writer_options = getenv(ENV_WRITER_OPTIONS);
Packit Service 1d0348
	if (writer_options != NULL) {
Packit Service 1d0348
		size_t module_len = sizeof(IGNORE_WRONG_MODULE_NAME) - 1;
Packit Service 1d0348
		size_t opt_len = strlen(writer_options) + 1;
Packit Service 1d0348
		char *p;
Packit Service 1d0348
		/* Set default write options. */
Packit Service 1d0348
		if ((p = malloc(module_len + opt_len)) == NULL)
Packit Service 1d0348
			lafe_errc(1, errno, "Out of memory");
Packit Service 1d0348
		/* Prepend magic code to ignore options for
Packit Service 1d0348
		 * a format or filters which are not added to
Packit Service 1d0348
		 * the archive write object. */
Packit Service 1d0348
		memcpy(p, IGNORE_WRONG_MODULE_NAME, module_len);
Packit Service 1d0348
		memcpy(p, writer_options, opt_len);
Packit Service 1d0348
		r = archive_write_set_options(a, p);
Packit Service 1d0348
		free(p);
Packit Service 1d0348
		if (r < ARCHIVE_WARN)
Packit Service 1d0348
			lafe_errc(1, 0, "%s", archive_error_string(a));
Packit Service 1d0348
		else
Packit Service 1d0348
			archive_clear_error(a);
Packit Service 1d0348
	}
Packit Service 1d0348
	if (ARCHIVE_OK != archive_write_set_options(a, bsdtar->option_options))
Packit Service 1d0348
		lafe_errc(1, 0, "%s", archive_error_string(a));
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static void
Packit Service 1d0348
set_reader_options(struct bsdtar *bsdtar, struct archive *a)
Packit Service 1d0348
{
Packit Service 1d0348
	const char *reader_options;
Packit Service 1d0348
	int r;
Packit Service 1d0348
Packit Service 1d0348
	(void)bsdtar; /* UNUSED */
Packit Service 1d0348
Packit Service 1d0348
	reader_options = getenv(ENV_READER_OPTIONS);
Packit Service 1d0348
	if (reader_options != NULL) {
Packit Service 1d0348
		size_t module_len = sizeof(IGNORE_WRONG_MODULE_NAME) - 1;
Packit Service 1d0348
		size_t opt_len = strlen(reader_options) + 1;
Packit Service 1d0348
		char *p;
Packit Service 1d0348
		/* Set default write options. */
Packit Service 1d0348
		if ((p = malloc(module_len + opt_len)) == NULL)
Packit Service 1d0348
		if (p == NULL)
Packit Service 1d0348
			lafe_errc(1, errno, "Out of memory");
Packit Service 1d0348
		/* Prepend magic code to ignore options for
Packit Service 1d0348
		 * a format or filters which are not added to
Packit Service 1d0348
		 * the archive write object. */
Packit Service 1d0348
		memcpy(p, IGNORE_WRONG_MODULE_NAME, module_len);
Packit Service 1d0348
		memcpy(p, reader_options, opt_len);
Packit Service 1d0348
		r = archive_read_set_options(a, p);
Packit Service 1d0348
		free(p);
Packit Service 1d0348
		if (r < ARCHIVE_WARN)
Packit Service 1d0348
			lafe_errc(1, 0, "%s", archive_error_string(a));
Packit Service 1d0348
		else
Packit Service 1d0348
			archive_clear_error(a);
Packit Service 1d0348
	}
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
void
Packit Service 1d0348
tar_mode_c(struct bsdtar *bsdtar)
Packit Service 1d0348
{
Packit Service 1d0348
	struct archive *a;
Packit Service 1d0348
	const void *filter_name;
Packit Service 1d0348
	int r;
Packit Service 1d0348
Packit Service 1d0348
	if (*bsdtar->argv == NULL && bsdtar->names_from_file == NULL)
Packit Service 1d0348
		lafe_errc(1, 0, "no files or directories specified");
Packit Service 1d0348
Packit Service 1d0348
	a = archive_write_new();
Packit Service 1d0348
Packit Service 1d0348
	/* Support any format that the library supports. */
Packit Service 1d0348
	if (cset_get_format(bsdtar->cset) == NULL) {
Packit Service 1d0348
		r = archive_write_set_format_pax_restricted(a);
Packit Service 1d0348
		cset_set_format(bsdtar->cset, "pax restricted");
Packit Service 1d0348
	} else {
Packit Service 1d0348
		r = archive_write_set_format_by_name(a,
Packit Service 1d0348
			cset_get_format(bsdtar->cset));
Packit Service 1d0348
	}
Packit Service 1d0348
	if (r != ARCHIVE_OK) {
Packit Service 1d0348
		fprintf(stderr, "Can't use format %s: %s\n",
Packit Service 1d0348
		    cset_get_format(bsdtar->cset),
Packit Service 1d0348
		    archive_error_string(a));
Packit Service 1d0348
		usage();
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	archive_write_set_bytes_per_block(a, bsdtar->bytes_per_block);
Packit Service 1d0348
	archive_write_set_bytes_in_last_block(a, bsdtar->bytes_in_last_block);
Packit Service 1d0348
Packit Service 1d0348
	r = cset_write_add_filters(bsdtar->cset, a, &filter_name);
Packit Service 1d0348
	if (r < ARCHIVE_WARN) {
Packit Service 1d0348
		lafe_errc(1, 0, "Unsupported compression option --%s",
Packit Service 1d0348
		    (const char *)filter_name);
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	set_writer_options(bsdtar, a);
Packit Service 1d0348
	if (bsdtar->passphrase != NULL)
Packit Service 1d0348
		r = archive_write_set_passphrase(a, bsdtar->passphrase);
Packit Service 1d0348
	else
Packit Service 1d0348
		r = archive_write_set_passphrase_callback(a, bsdtar,
Packit Service 1d0348
			&passphrase_callback);
Packit Service 1d0348
	if (r != ARCHIVE_OK)
Packit Service 1d0348
		lafe_errc(1, 0, "%s", archive_error_string(a));
Packit Service 1d0348
	if (ARCHIVE_OK != archive_write_open_filename(a, bsdtar->filename))
Packit Service 1d0348
		lafe_errc(1, 0, "%s", archive_error_string(a));
Packit Service 1d0348
	write_archive(a, bsdtar);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
/*
Packit Service 1d0348
 * Same as 'c', except we only support tar or empty formats in
Packit Service 1d0348
 * uncompressed files on disk.
Packit Service 1d0348
 */
Packit Service 1d0348
void
Packit Service 1d0348
tar_mode_r(struct bsdtar *bsdtar)
Packit Service 1d0348
{
Packit Service 1d0348
	int64_t	end_offset;
Packit Service 1d0348
	int	format;
Packit Service 1d0348
	struct archive *a;
Packit Service 1d0348
	struct archive_entry *entry;
Packit Service 1d0348
	int	r;
Packit Service 1d0348
Packit Service 1d0348
	/* Sanity-test some arguments and the file. */
Packit Service 1d0348
	test_for_append(bsdtar);
Packit Service 1d0348
Packit Service 1d0348
	format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
Packit Service 1d0348
Packit Service 1d0348
#if defined(__BORLANDC__)
Packit Service 1d0348
	bsdtar->fd = open(bsdtar->filename, O_RDWR | O_CREAT | O_BINARY);
Packit Service 1d0348
#else
Packit Service 1d0348
	bsdtar->fd = open(bsdtar->filename, O_RDWR | O_CREAT | O_BINARY, 0666);
Packit Service 1d0348
#endif
Packit Service 1d0348
	if (bsdtar->fd < 0)
Packit Service 1d0348
		lafe_errc(1, errno,
Packit Service 1d0348
		    "Cannot open %s", bsdtar->filename);
Packit Service 1d0348
Packit Service 1d0348
	a = archive_read_new();
Packit Service 1d0348
	archive_read_support_filter_all(a);
Packit Service 1d0348
	archive_read_support_format_empty(a);
Packit Service 1d0348
	archive_read_support_format_tar(a);
Packit Service 1d0348
	archive_read_support_format_gnutar(a);
Packit Service 1d0348
	set_reader_options(bsdtar, a);
Packit Service 1d0348
	r = archive_read_open_fd(a, bsdtar->fd, 10240);
Packit Service 1d0348
	if (r != ARCHIVE_OK)
Packit Service 1d0348
		lafe_errc(1, archive_errno(a),
Packit Service 1d0348
		    "Can't read archive %s: %s", bsdtar->filename,
Packit Service 1d0348
		    archive_error_string(a));
Packit Service 1d0348
	while (0 == archive_read_next_header(a, &entry)) {
Packit Service 1d0348
		if (archive_filter_code(a, 0) != ARCHIVE_FILTER_NONE) {
Packit Service 1d0348
			archive_read_free(a);
Packit Service 1d0348
			close(bsdtar->fd);
Packit Service 1d0348
			lafe_errc(1, 0,
Packit Service 1d0348
			    "Cannot append to compressed archive.");
Packit Service 1d0348
		}
Packit Service 1d0348
		/* Keep going until we hit end-of-archive */
Packit Service 1d0348
		format = archive_format(a);
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	end_offset = archive_read_header_position(a);
Packit Service 1d0348
	archive_read_free(a);
Packit Service 1d0348
Packit Service 1d0348
	/* Re-open archive for writing */
Packit Service 1d0348
	a = archive_write_new();
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * Set the format to be used for writing.  To allow people to
Packit Service 1d0348
	 * extend empty files, we need to allow them to specify the format,
Packit Service 1d0348
	 * which opens the possibility that they will specify a format that
Packit Service 1d0348
	 * doesn't match the existing format.  Hence, the following bit
Packit Service 1d0348
	 * of arcane ugliness.
Packit Service 1d0348
	 */
Packit Service 1d0348
Packit Service 1d0348
	if (cset_get_format(bsdtar->cset) != NULL) {
Packit Service 1d0348
		/* If the user requested a format, use that, but ... */
Packit Service 1d0348
		archive_write_set_format_by_name(a,
Packit Service 1d0348
		    cset_get_format(bsdtar->cset));
Packit Service 1d0348
		/* ... complain if it's not compatible. */
Packit Service 1d0348
		format &= ARCHIVE_FORMAT_BASE_MASK;
Packit Service 1d0348
		if (format != (int)(archive_format(a) & ARCHIVE_FORMAT_BASE_MASK)
Packit Service 1d0348
		    && format != ARCHIVE_FORMAT_EMPTY) {
Packit Service 1d0348
			lafe_errc(1, 0,
Packit Service 1d0348
			    "Format %s is incompatible with the archive %s.",
Packit Service 1d0348
			    cset_get_format(bsdtar->cset), bsdtar->filename);
Packit Service 1d0348
		}
Packit Service 1d0348
	} else {
Packit Service 1d0348
		/*
Packit Service 1d0348
		 * Just preserve the current format, with a little care
Packit Service 1d0348
		 * for formats that libarchive can't write.
Packit Service 1d0348
		 */
Packit Service 1d0348
		if (format == ARCHIVE_FORMAT_EMPTY)
Packit Service 1d0348
			format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
Packit Service 1d0348
		archive_write_set_format(a, format);
Packit Service 1d0348
	}
Packit Service 1d0348
	if (lseek(bsdtar->fd, end_offset, SEEK_SET) < 0)
Packit Service 1d0348
		lafe_errc(1, errno, "Could not seek to archive end");
Packit Service 1d0348
	set_writer_options(bsdtar, a);
Packit Service 1d0348
	if (ARCHIVE_OK != archive_write_open_fd(a, bsdtar->fd))
Packit Service 1d0348
		lafe_errc(1, 0, "%s", archive_error_string(a));
Packit Service 1d0348
Packit Service 1d0348
	write_archive(a, bsdtar); /* XXX check return val XXX */
Packit Service 1d0348
Packit Service 1d0348
	close(bsdtar->fd);
Packit Service 1d0348
	bsdtar->fd = -1;
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
void
Packit Service 1d0348
tar_mode_u(struct bsdtar *bsdtar)
Packit Service 1d0348
{
Packit Service 1d0348
	int64_t			 end_offset;
Packit Service 1d0348
	struct archive		*a;
Packit Service 1d0348
	struct archive_entry	*entry;
Packit Service 1d0348
	int			 format;
Packit Service 1d0348
	struct archive_dir_entry	*p;
Packit Service 1d0348
	struct archive_dir	 archive_dir;
Packit Service 1d0348
Packit Service 1d0348
	bsdtar->archive_dir = &archive_dir;
Packit Service 1d0348
	memset(&archive_dir, 0, sizeof(archive_dir));
Packit Service 1d0348
Packit Service 1d0348
	format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
Packit Service 1d0348
Packit Service 1d0348
	/* Sanity-test some arguments and the file. */
Packit Service 1d0348
	test_for_append(bsdtar);
Packit Service 1d0348
Packit Service 1d0348
	bsdtar->fd = open(bsdtar->filename, O_RDWR | O_BINARY);
Packit Service 1d0348
	if (bsdtar->fd < 0)
Packit Service 1d0348
		lafe_errc(1, errno,
Packit Service 1d0348
		    "Cannot open %s", bsdtar->filename);
Packit Service 1d0348
Packit Service 1d0348
	a = archive_read_new();
Packit Service 1d0348
	archive_read_support_filter_all(a);
Packit Service 1d0348
	archive_read_support_format_tar(a);
Packit Service 1d0348
	archive_read_support_format_gnutar(a);
Packit Service 1d0348
	set_reader_options(bsdtar, a);
Packit Service 1d0348
	if (archive_read_open_fd(a, bsdtar->fd, bsdtar->bytes_per_block)
Packit Service 1d0348
	    != ARCHIVE_OK) {
Packit Service 1d0348
		lafe_errc(1, 0,
Packit Service 1d0348
		    "Can't open %s: %s", bsdtar->filename,
Packit Service 1d0348
		    archive_error_string(a));
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	/* Build a list of all entries and their recorded mod times. */
Packit Service 1d0348
	while (0 == archive_read_next_header(a, &entry)) {
Packit Service 1d0348
		if (archive_filter_code(a, 0) != ARCHIVE_FILTER_NONE) {
Packit Service 1d0348
			archive_read_free(a);
Packit Service 1d0348
			close(bsdtar->fd);
Packit Service 1d0348
			lafe_errc(1, 0,
Packit Service 1d0348
			    "Cannot append to compressed archive.");
Packit Service 1d0348
		}
Packit Service 1d0348
		if (archive_match_exclude_entry(bsdtar->matching,
Packit Service 1d0348
		    ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_OLDER |
Packit Service 1d0348
		    ARCHIVE_MATCH_EQUAL, entry) != ARCHIVE_OK)
Packit Service 1d0348
			lafe_errc(1, 0, "Error : %s",
Packit Service 1d0348
			    archive_error_string(bsdtar->matching));
Packit Service 1d0348
		/* Record the last format determination we see */
Packit Service 1d0348
		format = archive_format(a);
Packit Service 1d0348
		/* Keep going until we hit end-of-archive */
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	end_offset = archive_read_header_position(a);
Packit Service 1d0348
	archive_read_free(a);
Packit Service 1d0348
Packit Service 1d0348
	/* Re-open archive for writing. */
Packit Service 1d0348
	a = archive_write_new();
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * Set format to same one auto-detected above.
Packit Service 1d0348
	 */
Packit Service 1d0348
	archive_write_set_format(a, format);
Packit Service 1d0348
	archive_write_set_bytes_per_block(a, bsdtar->bytes_per_block);
Packit Service 1d0348
	archive_write_set_bytes_in_last_block(a, bsdtar->bytes_in_last_block);
Packit Service 1d0348
Packit Service 1d0348
	if (lseek(bsdtar->fd, end_offset, SEEK_SET) < 0)
Packit Service 1d0348
		lafe_errc(1, errno, "Could not seek to archive end");
Packit Service 1d0348
	set_writer_options(bsdtar, a);
Packit Service 1d0348
	if (ARCHIVE_OK != archive_write_open_fd(a, bsdtar->fd))
Packit Service 1d0348
		lafe_errc(1, 0, "%s", archive_error_string(a));
Packit Service 1d0348
Packit Service 1d0348
	write_archive(a, bsdtar);
Packit Service 1d0348
Packit Service 1d0348
	close(bsdtar->fd);
Packit Service 1d0348
	bsdtar->fd = -1;
Packit Service 1d0348
Packit Service 1d0348
	while (bsdtar->archive_dir->head != NULL) {
Packit Service 1d0348
		p = bsdtar->archive_dir->head->next;
Packit Service 1d0348
		free(bsdtar->archive_dir->head->name);
Packit Service 1d0348
		free(bsdtar->archive_dir->head);
Packit Service 1d0348
		bsdtar->archive_dir->head = p;
Packit Service 1d0348
	}
Packit Service 1d0348
	bsdtar->archive_dir->tail = NULL;
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
Packit Service 1d0348
/*
Packit Service 1d0348
 * Write user-specified files/dirs to opened archive.
Packit Service 1d0348
 */
Packit Service 1d0348
static void
Packit Service 1d0348
write_archive(struct archive *a, struct bsdtar *bsdtar)
Packit Service 1d0348
{
Packit Service 1d0348
	const char *arg;
Packit Service 1d0348
	struct archive_entry *entry, *sparse_entry;
Packit Service 1d0348
Packit Service 1d0348
	/* Choose a suitable copy buffer size */
Packit Service 1d0348
	bsdtar->buff_size = 64 * 1024;
Packit Service 1d0348
	while (bsdtar->buff_size < (size_t)bsdtar->bytes_per_block)
Packit Service 1d0348
	  bsdtar->buff_size *= 2;
Packit Service 1d0348
	/* Try to compensate for space we'll lose to alignment. */
Packit Service 1d0348
	bsdtar->buff_size += 16 * 1024;
Packit Service 1d0348
Packit Service 1d0348
	/* Allocate a buffer for file data. */
Packit Service 1d0348
	if ((bsdtar->buff = malloc(bsdtar->buff_size)) == NULL)
Packit Service 1d0348
		lafe_errc(1, 0, "cannot allocate memory");
Packit Service 1d0348
Packit Service 1d0348
	if ((bsdtar->resolver = archive_entry_linkresolver_new()) == NULL)
Packit Service 1d0348
		lafe_errc(1, 0, "cannot create link resolver");
Packit Service 1d0348
	archive_entry_linkresolver_set_strategy(bsdtar->resolver,
Packit Service 1d0348
	    archive_format(a));
Packit Service 1d0348
Packit Service 1d0348
	/* Create a read_disk object. */
Packit Service 1d0348
	if ((bsdtar->diskreader = archive_read_disk_new()) == NULL)
Packit Service 1d0348
		lafe_errc(1, 0, "Cannot create read_disk object");
Packit Service 1d0348
	/* Tell the read_disk how handle symlink. */
Packit Service 1d0348
	switch (bsdtar->symlink_mode) {
Packit Service 1d0348
	case 'H':
Packit Service 1d0348
		archive_read_disk_set_symlink_hybrid(bsdtar->diskreader);
Packit Service 1d0348
		break;
Packit Service 1d0348
	case 'L':
Packit Service 1d0348
		archive_read_disk_set_symlink_logical(bsdtar->diskreader);
Packit Service 1d0348
		break;
Packit Service 1d0348
	default:
Packit Service 1d0348
		archive_read_disk_set_symlink_physical(bsdtar->diskreader);
Packit Service 1d0348
		break;
Packit Service 1d0348
	}
Packit Service 1d0348
	/* Register entry filters. */
Packit Service 1d0348
	archive_read_disk_set_matching(bsdtar->diskreader,
Packit Service 1d0348
	    bsdtar->matching, excluded_callback, bsdtar);
Packit Service 1d0348
	archive_read_disk_set_metadata_filter_callback(
Packit Service 1d0348
	    bsdtar->diskreader, metadata_filter, bsdtar);
Packit Service 1d0348
	/* Set the behavior of archive_read_disk. */
Packit Service 1d0348
	archive_read_disk_set_behavior(bsdtar->diskreader,
Packit Service 1d0348
	    bsdtar->readdisk_flags);
Packit Service 1d0348
	archive_read_disk_set_standard_lookup(bsdtar->diskreader);
Packit Service 1d0348
Packit Service 1d0348
	if (bsdtar->names_from_file != NULL)
Packit Service 1d0348
		archive_names_from_file(bsdtar, a);
Packit Service 1d0348
Packit Service 1d0348
	while (*bsdtar->argv) {
Packit Service 1d0348
		arg = *bsdtar->argv;
Packit Service 1d0348
		if (arg[0] == '-' && arg[1] == 'C') {
Packit Service 1d0348
			arg += 2;
Packit Service 1d0348
			if (*arg == '\0') {
Packit Service 1d0348
				bsdtar->argv++;
Packit Service 1d0348
				arg = *bsdtar->argv;
Packit Service 1d0348
				if (arg == NULL) {
Packit Service 1d0348
					lafe_warnc(0, "%s",
Packit Service 1d0348
					    "Missing argument for -C");
Packit Service 1d0348
					bsdtar->return_value = 1;
Packit Service 1d0348
					goto cleanup;
Packit Service 1d0348
				}
Packit Service 1d0348
				if (*arg == '\0') {
Packit Service 1d0348
					lafe_warnc(0,
Packit Service 1d0348
					    "Meaningless argument for -C: ''");
Packit Service 1d0348
					bsdtar->return_value = 1;
Packit Service 1d0348
					goto cleanup;
Packit Service 1d0348
				}
Packit Service 1d0348
			}
Packit Service 1d0348
			set_chdir(bsdtar, arg);
Packit Service 1d0348
		} else {
Packit Service 1d0348
			if (*arg != '/')
Packit Service 1d0348
				do_chdir(bsdtar); /* Handle a deferred -C */
Packit Service 1d0348
			if (*arg == '@') {
Packit Service 1d0348
				if (append_archive_filename(bsdtar, a,
Packit Service 1d0348
				    arg + 1) != 0)
Packit Service 1d0348
					break;
Packit Service 1d0348
			} else
Packit Service 1d0348
				write_hierarchy(bsdtar, a, arg);
Packit Service 1d0348
		}
Packit Service 1d0348
		bsdtar->argv++;
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	archive_read_disk_set_matching(bsdtar->diskreader, NULL, NULL, NULL);
Packit Service 1d0348
	archive_read_disk_set_metadata_filter_callback(
Packit Service 1d0348
	    bsdtar->diskreader, NULL, NULL);
Packit Service 1d0348
	entry = NULL;
Packit Service 1d0348
	archive_entry_linkify(bsdtar->resolver, &entry, &sparse_entry);
Packit Service 1d0348
	while (entry != NULL) {
Packit Service 1d0348
		int r;
Packit Service 1d0348
		struct archive_entry *entry2;
Packit Service 1d0348
		struct archive *disk = bsdtar->diskreader;
Packit Service 1d0348
Packit Service 1d0348
		/*
Packit Service 1d0348
		 * This tricky code here is to correctly read the contents
Packit Service 1d0348
		 * of the entry because the disk reader bsdtar->diskreader
Packit Service 1d0348
		 * is pointing at does not have any information about the
Packit Service 1d0348
		 * entry by this time and using archive_read_data_block()
Packit Service 1d0348
		 * with the disk reader consequently must fail. And we
Packit Service 1d0348
		 * have to re-open the entry to read the contents.
Packit Service 1d0348
		 */
Packit Service 1d0348
		/* TODO: Work with -C option as well. */
Packit Service 1d0348
		r = archive_read_disk_open(disk,
Packit Service 1d0348
			archive_entry_sourcepath(entry));
Packit Service 1d0348
		if (r != ARCHIVE_OK) {
Packit Service 1d0348
			lafe_warnc(archive_errno(disk),
Packit Service 1d0348
			    "%s", archive_error_string(disk));
Packit Service 1d0348
			bsdtar->return_value = 1;
Packit Service 2fc294
			goto next_entry;
Packit Service 1d0348
		}
Packit Service 1d0348
Packit Service 1d0348
		/*
Packit Service 1d0348
		 * Invoke archive_read_next_header2() to work
Packit Service 1d0348
		 * archive_read_data_block(), which is called via write_file(),
Packit Service 1d0348
		 * without failure.
Packit Service 1d0348
		 */
Packit Service 1d0348
		entry2 = archive_entry_new();
Packit Service 1d0348
		r = archive_read_next_header2(disk, entry2);
Packit Service 1d0348
		archive_entry_free(entry2);
Packit Service 1d0348
		if (r != ARCHIVE_OK) {
Packit Service 1d0348
			lafe_warnc(archive_errno(disk),
Packit Service 1d0348
			    "%s", archive_error_string(disk));
Packit Service 1d0348
			if (r == ARCHIVE_FATAL)
Packit Service 1d0348
				bsdtar->return_value = 1;
Packit Service 2fc294
			archive_read_close(disk);
Packit Service 2fc294
			goto next_entry;
Packit Service 1d0348
		}
Packit Service 1d0348
Packit Service 1d0348
		write_file(bsdtar, a, entry);
Packit Service 1d0348
		archive_read_close(disk);
Packit Service 2fc294
next_entry:
Packit Service 2fc294
		archive_entry_free(entry);
Packit Service 1d0348
		entry = NULL;
Packit Service 1d0348
		archive_entry_linkify(bsdtar->resolver, &entry, &sparse_entry);
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	if (archive_write_close(a)) {
Packit Service 1d0348
		lafe_warnc(0, "%s", archive_error_string(a));
Packit Service 1d0348
		bsdtar->return_value = 1;
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
cleanup:
Packit Service 1d0348
	/* Free file data buffer. */
Packit Service 1d0348
	free(bsdtar->buff);
Packit Service 1d0348
	archive_entry_linkresolver_free(bsdtar->resolver);
Packit Service 1d0348
	bsdtar->resolver = NULL;
Packit Service 1d0348
	archive_read_free(bsdtar->diskreader);
Packit Service 1d0348
	bsdtar->diskreader = NULL;
Packit Service 1d0348
Packit Service 1d0348
	if (bsdtar->flags & OPTFLAG_TOTALS) {
Packit Service 1d0348
		fprintf(stderr, "Total bytes written: %s\n",
Packit Service 1d0348
		    tar_i64toa(archive_filter_bytes(a, -1)));
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	archive_write_free(a);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
/*
Packit Service 1d0348
 * Archive names specified in file.
Packit Service 1d0348
 *
Packit Service 1d0348
 * Unless --null was specified, a line containing exactly "-C" will
Packit Service 1d0348
 * cause the next line to be a directory to pass to chdir().  If
Packit Service 1d0348
 * --null is specified, then a line "-C" is just another filename.
Packit Service 1d0348
 */
Packit Service 1d0348
static void
Packit Service 1d0348
archive_names_from_file(struct bsdtar *bsdtar, struct archive *a)
Packit Service 1d0348
{
Packit Service 1d0348
	struct lafe_line_reader *lr;
Packit Service 1d0348
	const char *line;
Packit Service 1d0348
Packit Service 1d0348
	bsdtar->next_line_is_dir = 0;
Packit Service 1d0348
Packit Service 1d0348
	lr = lafe_line_reader(bsdtar->names_from_file,
Packit Service 1d0348
	    (bsdtar->flags & OPTFLAG_NULL));
Packit Service 1d0348
	while ((line = lafe_line_reader_next(lr)) != NULL) {
Packit Service 1d0348
		if (bsdtar->next_line_is_dir) {
Packit Service 1d0348
			if (*line != '\0')
Packit Service 1d0348
				set_chdir(bsdtar, line);
Packit Service 1d0348
			else {
Packit Service 1d0348
				lafe_warnc(0,
Packit Service 1d0348
				    "Meaningless argument for -C: ''");
Packit Service 1d0348
				bsdtar->return_value = 1;
Packit Service 1d0348
			}
Packit Service 1d0348
			bsdtar->next_line_is_dir = 0;
Packit Service 1d0348
		} else if (((bsdtar->flags & OPTFLAG_NULL) == 0) &&
Packit Service 1d0348
		    strcmp(line, "-C") == 0)
Packit Service 1d0348
			bsdtar->next_line_is_dir = 1;
Packit Service 1d0348
		else {
Packit Service 1d0348
			if (*line != '/')
Packit Service 1d0348
				do_chdir(bsdtar); /* Handle a deferred -C */
Packit Service 1d0348
			write_hierarchy(bsdtar, a, line);
Packit Service 1d0348
		}
Packit Service 1d0348
	}
Packit Service 1d0348
	lafe_line_reader_free(lr);
Packit Service 1d0348
	if (bsdtar->next_line_is_dir)
Packit Service 1d0348
		lafe_errc(1, errno,
Packit Service 1d0348
		    "Unexpected end of filename list; "
Packit Service 1d0348
		    "directory expected after -C");
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
/*
Packit Service 1d0348
 * Copy from specified archive to current archive.  Returns non-zero
Packit Service 1d0348
 * for write errors (which force us to terminate the entire archiving
Packit Service 1d0348
 * operation).  If there are errors reading the input archive, we set
Packit Service 1d0348
 * bsdtar->return_value but return zero, so the overall archiving
Packit Service 1d0348
 * operation will complete and return non-zero.
Packit Service 1d0348
 */
Packit Service 1d0348
static int
Packit Service 1d0348
append_archive_filename(struct bsdtar *bsdtar, struct archive *a,
Packit Service 1d0348
    const char *raw_filename)
Packit Service 1d0348
{
Packit Service 1d0348
	struct archive *ina;
Packit Service 1d0348
	const char *filename = raw_filename;
Packit Service 1d0348
	int rc;
Packit Service 1d0348
Packit Service 1d0348
	if (strcmp(filename, "-") == 0)
Packit Service 1d0348
		filename = NULL; /* Library uses NULL for stdio. */
Packit Service 1d0348
Packit Service 1d0348
	ina = archive_read_new();
Packit Service 1d0348
	archive_read_support_format_all(ina);
Packit Service 1d0348
	archive_read_support_filter_all(ina);
Packit Service 1d0348
	set_reader_options(bsdtar, ina);
Packit Service 1d0348
	archive_read_set_options(ina, "mtree:checkfs");
Packit Service 1d0348
	if (bsdtar->passphrase != NULL)
Packit Service 1d0348
		rc = archive_read_add_passphrase(a, bsdtar->passphrase);
Packit Service 1d0348
	else
Packit Service 1d0348
		rc = archive_read_set_passphrase_callback(ina, bsdtar,
Packit Service 1d0348
			&passphrase_callback);
Packit Service 1d0348
	if (rc != ARCHIVE_OK)
Packit Service 1d0348
		lafe_errc(1, 0, "%s", archive_error_string(a));
Packit Service 1d0348
	if (archive_read_open_filename(ina, filename,
Packit Service 1d0348
					bsdtar->bytes_per_block)) {
Packit Service 1d0348
		lafe_warnc(0, "%s", archive_error_string(ina));
Packit Service 1d0348
		bsdtar->return_value = 1;
Packit Service 1d0348
		return (0);
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	rc = append_archive(bsdtar, a, ina);
Packit Service 1d0348
Packit Service 1d0348
	if (rc != ARCHIVE_OK) {
Packit Service 1d0348
		lafe_warnc(0, "Error reading archive %s: %s",
Packit Service 1d0348
		    raw_filename, archive_error_string(ina));
Packit Service 1d0348
		bsdtar->return_value = 1;
Packit Service 1d0348
	}
Packit Service 1d0348
	archive_read_free(ina);
Packit Service 1d0348
Packit Service 1d0348
	return (rc);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static int
Packit Service 1d0348
append_archive(struct bsdtar *bsdtar, struct archive *a, struct archive *ina)
Packit Service 1d0348
{
Packit Service 1d0348
	struct archive_entry *in_entry;
Packit Service 1d0348
	int e;
Packit Service 1d0348
Packit Service 1d0348
	while (ARCHIVE_OK == (e = archive_read_next_header(ina, &in_entry))) {
Packit Service 1d0348
		if (archive_match_excluded(bsdtar->matching, in_entry))
Packit Service 1d0348
			continue;
Packit Service 1d0348
		if ((bsdtar->flags & OPTFLAG_INTERACTIVE) &&
Packit Service 1d0348
		    !yes("copy '%s'", archive_entry_pathname(in_entry)))
Packit Service 1d0348
			continue;
Packit Service 1d0348
		if (bsdtar->verbose > 1) {
Packit Service 1d0348
			safe_fprintf(stderr, "a ");
Packit Service 1d0348
			list_item_verbose(bsdtar, stderr, in_entry);
Packit Service 1d0348
		} else if (bsdtar->verbose > 0)
Packit Service 1d0348
			safe_fprintf(stderr, "a %s",
Packit Service 1d0348
			    archive_entry_pathname(in_entry));
Packit Service 1d0348
		if (need_report())
Packit Service 1d0348
			report_write(bsdtar, a, in_entry, 0);
Packit Service 1d0348
Packit Service 1d0348
		e = archive_write_header(a, in_entry);
Packit Service 1d0348
		if (e != ARCHIVE_OK) {
Packit Service 1d0348
			if (!bsdtar->verbose)
Packit Service 1d0348
				lafe_warnc(0, "%s: %s",
Packit Service 1d0348
				    archive_entry_pathname(in_entry),
Packit Service 1d0348
				    archive_error_string(a));
Packit Service 1d0348
			else
Packit Service 1d0348
				fprintf(stderr, ": %s", archive_error_string(a));
Packit Service 1d0348
		}
Packit Service 1d0348
		if (e == ARCHIVE_FATAL)
Packit Service 1d0348
			exit(1);
Packit Service 1d0348
Packit Service 1d0348
		if (e >= ARCHIVE_WARN) {
Packit Service 1d0348
			if (archive_entry_size(in_entry) == 0)
Packit Service 1d0348
				archive_read_data_skip(ina);
Packit Service 1d0348
			else if (copy_file_data_block(bsdtar, a, ina, in_entry))
Packit Service 1d0348
				exit(1);
Packit Service 1d0348
		}
Packit Service 1d0348
Packit Service 1d0348
		if (bsdtar->verbose)
Packit Service 1d0348
			fprintf(stderr, "\n");
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	return (e == ARCHIVE_EOF ? ARCHIVE_OK : e);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
/* Helper function to copy file to archive. */
Packit Service 1d0348
static int
Packit Service 1d0348
copy_file_data_block(struct bsdtar *bsdtar, struct archive *a,
Packit Service 1d0348
    struct archive *in_a, struct archive_entry *entry)
Packit Service 1d0348
{
Packit Service 1d0348
	size_t	bytes_read;
Packit Service 1d0348
	ssize_t	bytes_written;
Packit Service 1d0348
	int64_t	offset, progress = 0;
Packit Service 1d0348
	char *null_buff = NULL;
Packit Service 1d0348
	const void *buff;
Packit Service 1d0348
	int r;
Packit Service 1d0348
Packit Service 1d0348
	while ((r = archive_read_data_block(in_a, &buff,
Packit Service 1d0348
	    &bytes_read, &offset)) == ARCHIVE_OK) {
Packit Service 1d0348
		if (need_report())
Packit Service 1d0348
			report_write(bsdtar, a, entry, progress);
Packit Service 1d0348
Packit Service 1d0348
		if (offset > progress) {
Packit Service 1d0348
			int64_t sparse = offset - progress;
Packit Service 1d0348
			size_t ns;
Packit Service 1d0348
Packit Service 1d0348
			if (null_buff == NULL) {
Packit Service 1d0348
				null_buff = bsdtar->buff;
Packit Service 1d0348
				memset(null_buff, 0, bsdtar->buff_size);
Packit Service 1d0348
			}
Packit Service 1d0348
Packit Service 1d0348
			while (sparse > 0) {
Packit Service 1d0348
				if (sparse > (int64_t)bsdtar->buff_size)
Packit Service 1d0348
					ns = bsdtar->buff_size;
Packit Service 1d0348
				else
Packit Service 1d0348
					ns = (size_t)sparse;
Packit Service 1d0348
				bytes_written =
Packit Service 1d0348
				    archive_write_data(a, null_buff, ns);
Packit Service 1d0348
				if (bytes_written < 0) {
Packit Service 1d0348
					/* Write failed; this is bad */
Packit Service 1d0348
					lafe_warnc(0, "%s",
Packit Service 1d0348
					     archive_error_string(a));
Packit Service 1d0348
					return (-1);
Packit Service 1d0348
				}
Packit Service 1d0348
				if ((size_t)bytes_written < ns) {
Packit Service 1d0348
					/* Write was truncated; warn but
Packit Service 1d0348
					 * continue. */
Packit Service 1d0348
					lafe_warnc(0,
Packit Service 1d0348
					    "%s: Truncated write; file may "
Packit Service 1d0348
					    "have grown while being archived.",
Packit Service 1d0348
					    archive_entry_pathname(entry));
Packit Service 1d0348
					return (0);
Packit Service 1d0348
				}
Packit Service 1d0348
				progress += bytes_written;
Packit Service 1d0348
				sparse -= bytes_written;
Packit Service 1d0348
			}
Packit Service 1d0348
		}
Packit Service 1d0348
Packit Service 1d0348
		bytes_written = archive_write_data(a, buff, bytes_read);
Packit Service 1d0348
		if (bytes_written < 0) {
Packit Service 1d0348
			/* Write failed; this is bad */
Packit Service 1d0348
			lafe_warnc(0, "%s", archive_error_string(a));
Packit Service 1d0348
			return (-1);
Packit Service 1d0348
		}
Packit Service 1d0348
		if ((size_t)bytes_written < bytes_read) {
Packit Service 1d0348
			/* Write was truncated; warn but continue. */
Packit Service 1d0348
			lafe_warnc(0,
Packit Service 1d0348
			    "%s: Truncated write; file may have grown "
Packit Service 1d0348
			    "while being archived.",
Packit Service 1d0348
			    archive_entry_pathname(entry));
Packit Service 1d0348
			return (0);
Packit Service 1d0348
		}
Packit Service 1d0348
		progress += bytes_written;
Packit Service 1d0348
	}
Packit Service 1d0348
	if (r < ARCHIVE_WARN) {
Packit Service 1d0348
		lafe_warnc(archive_errno(a), "%s", archive_error_string(a));
Packit Service 1d0348
		return (-1);
Packit Service 1d0348
	}
Packit Service 1d0348
	return (0);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static void
Packit Service 1d0348
excluded_callback(struct archive *a, void *_data, struct archive_entry *entry)
Packit Service 1d0348
{
Packit Service 1d0348
	struct bsdtar *bsdtar = (struct bsdtar *)_data;
Packit Service 1d0348
Packit Service 1d0348
	if (bsdtar->flags & OPTFLAG_NO_SUBDIRS)
Packit Service 1d0348
		return;
Packit Service 1d0348
	if (!archive_read_disk_can_descend(a))
Packit Service 1d0348
		return;
Packit Service 1d0348
	if ((bsdtar->flags & OPTFLAG_INTERACTIVE) &&
Packit Service 1d0348
	    !yes("add '%s'", archive_entry_pathname(entry)))
Packit Service 1d0348
		return;
Packit Service 1d0348
	archive_read_disk_descend(a);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static int
Packit Service 1d0348
metadata_filter(struct archive *a, void *_data, struct archive_entry *entry)
Packit Service 1d0348
{
Packit Service 1d0348
	struct bsdtar *bsdtar = (struct bsdtar *)_data;
Packit Service 1d0348
Packit Service 1d0348
	/* XXX TODO: check whether this filesystem is
Packit Service 1d0348
	 * synthetic and/or local.  Add a new
Packit Service 1d0348
	 * --local-only option to skip non-local
Packit Service 1d0348
	 * filesystems.  Skip synthetic filesystems
Packit Service 1d0348
	 * regardless.
Packit Service 1d0348
	 *
Packit Service 1d0348
	 * The results should be cached, since
Packit Service 1d0348
	 * tree.c doesn't usually visit a directory
Packit Service 1d0348
	 * and the directory contents together.  A simple
Packit Service 1d0348
	 * move-to-front list should perform quite well.
Packit Service 1d0348
	 *
Packit Service 1d0348
	 * Use archive_read_disk_current_filesystem_is_remote().
Packit Service 1d0348
	 */
Packit Service 1d0348
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * If the user vetoes this file/directory, skip it.
Packit Service 1d0348
	 * We want this to be fairly late; if some other
Packit Service 1d0348
	 * check would veto this file, we shouldn't bother
Packit Service 1d0348
	 * the user with it.
Packit Service 1d0348
	 */
Packit Service 1d0348
	if ((bsdtar->flags & OPTFLAG_INTERACTIVE) &&
Packit Service 1d0348
	    !yes("add '%s'", archive_entry_pathname(entry)))
Packit Service 1d0348
		return (0);
Packit Service 1d0348
Packit Service 1d0348
	/* Note: if user vetoes, we won't descend. */
Packit Service 1d0348
	if (((bsdtar->flags & OPTFLAG_NO_SUBDIRS) == 0) &&
Packit Service 1d0348
	    archive_read_disk_can_descend(a))
Packit Service 1d0348
		archive_read_disk_descend(a);
Packit Service 1d0348
Packit Service 1d0348
	return (1);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
/*
Packit Service 1d0348
 * Add the file or dir hierarchy named by 'path' to the archive
Packit Service 1d0348
 */
Packit Service 1d0348
static void
Packit Service 1d0348
write_hierarchy(struct bsdtar *bsdtar, struct archive *a, const char *path)
Packit Service 1d0348
{
Packit Service 1d0348
	struct archive *disk = bsdtar->diskreader;
Packit Service 1d0348
	struct archive_entry *entry = NULL, *spare_entry = NULL;
Packit Service 1d0348
	int r;
Packit Service 1d0348
Packit Service 1d0348
	r = archive_read_disk_open(disk, path);
Packit Service 1d0348
	if (r != ARCHIVE_OK) {
Packit Service 1d0348
		lafe_warnc(archive_errno(disk),
Packit Service 1d0348
		    "%s", archive_error_string(disk));
Packit Service 1d0348
		bsdtar->return_value = 1;
Packit Service 1d0348
		return;
Packit Service 1d0348
	}
Packit Service 1d0348
	bsdtar->first_fs = -1;
Packit Service 1d0348
Packit Service 1d0348
	for (;;) {
Packit Service 1d0348
		archive_entry_free(entry);
Packit Service 1d0348
		entry = archive_entry_new();
Packit Service 1d0348
		r = archive_read_next_header2(disk, entry);
Packit Service 1d0348
		if (r == ARCHIVE_EOF)
Packit Service 1d0348
			break;
Packit Service 1d0348
		else if (r != ARCHIVE_OK) {
Packit Service 1d0348
			lafe_warnc(archive_errno(disk),
Packit Service 1d0348
			    "%s", archive_error_string(disk));
Packit Service 1d0348
			if (r == ARCHIVE_FATAL || r == ARCHIVE_FAILED) {
Packit Service 1d0348
				bsdtar->return_value = 1;
Packit Service 1d0348
				archive_entry_free(entry);
Packit Service 1d0348
				archive_read_close(disk);
Packit Service 1d0348
				return;
Packit Service 1d0348
			} else if (r < ARCHIVE_WARN)
Packit Service 1d0348
				continue;
Packit Service 1d0348
		}
Packit Service 1d0348
Packit Service 1d0348
		if (bsdtar->uid >= 0) {
Packit Service 1d0348
			archive_entry_set_uid(entry, bsdtar->uid);
Packit Service 1d0348
			if (!bsdtar->uname)
Packit Service 1d0348
				archive_entry_set_uname(entry,
Packit Service 1d0348
				    archive_read_disk_uname(bsdtar->diskreader,
Packit Service 1d0348
					bsdtar->uid));
Packit Service 1d0348
		}
Packit Service 1d0348
		if (bsdtar->gid >= 0) {
Packit Service 1d0348
			archive_entry_set_gid(entry, bsdtar->gid);
Packit Service 1d0348
			if (!bsdtar->gname)
Packit Service 1d0348
				archive_entry_set_gname(entry,
Packit Service 1d0348
				    archive_read_disk_gname(bsdtar->diskreader,
Packit Service 1d0348
					bsdtar->gid));
Packit Service 1d0348
		}
Packit Service 1d0348
		if (bsdtar->uname)
Packit Service 1d0348
			archive_entry_set_uname(entry, bsdtar->uname);
Packit Service 1d0348
		if (bsdtar->gname)
Packit Service 1d0348
			archive_entry_set_gname(entry, bsdtar->gname);
Packit Service 1d0348
Packit Service 1d0348
		/*
Packit Service 1d0348
		 * Rewrite the pathname to be archived.  If rewrite
Packit Service 1d0348
		 * fails, skip the entry.
Packit Service 1d0348
		 */
Packit Service 1d0348
		if (edit_pathname(bsdtar, entry))
Packit Service 1d0348
			continue;
Packit Service 1d0348
Packit Service 1d0348
		/* Display entry as we process it. */
Packit Service 1d0348
		if (bsdtar->verbose > 1) {
Packit Service 1d0348
			safe_fprintf(stderr, "a ");
Packit Service 1d0348
			list_item_verbose(bsdtar, stderr, entry);
Packit Service 1d0348
		} else if (bsdtar->verbose > 0) {
Packit Service 1d0348
		/* This format is required by SUSv2. */
Packit Service 1d0348
			safe_fprintf(stderr, "a %s",
Packit Service 1d0348
			    archive_entry_pathname(entry));
Packit Service 1d0348
		}
Packit Service 1d0348
Packit Service 1d0348
		/* Non-regular files get archived with zero size. */
Packit Service 1d0348
		if (archive_entry_filetype(entry) != AE_IFREG)
Packit Service 1d0348
			archive_entry_set_size(entry, 0);
Packit Service 1d0348
Packit Service 1d0348
		archive_entry_linkify(bsdtar->resolver, &entry, &spare_entry);
Packit Service 1d0348
Packit Service 1d0348
		while (entry != NULL) {
Packit Service 1d0348
			write_file(bsdtar, a, entry);
Packit Service 1d0348
			archive_entry_free(entry);
Packit Service 1d0348
			entry = spare_entry;
Packit Service 1d0348
			spare_entry = NULL;
Packit Service 1d0348
		}
Packit Service 1d0348
Packit Service 1d0348
		if (bsdtar->verbose)
Packit Service 1d0348
			fprintf(stderr, "\n");
Packit Service 1d0348
	}
Packit Service 1d0348
	archive_entry_free(entry);
Packit Service 1d0348
	archive_read_close(disk);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
/*
Packit Service 1d0348
 * Write a single file (or directory or other filesystem object) to
Packit Service 1d0348
 * the archive.
Packit Service 1d0348
 */
Packit Service 1d0348
static void
Packit Service 1d0348
write_file(struct bsdtar *bsdtar, struct archive *a,
Packit Service 1d0348
    struct archive_entry *entry)
Packit Service 1d0348
{
Packit Service 1d0348
	write_entry(bsdtar, a, entry);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
/*
Packit Service 1d0348
 * Write a single entry to the archive.
Packit Service 1d0348
 */
Packit Service 1d0348
static void
Packit Service 1d0348
write_entry(struct bsdtar *bsdtar, struct archive *a,
Packit Service 1d0348
    struct archive_entry *entry)
Packit Service 1d0348
{
Packit Service 1d0348
	int e;
Packit Service 1d0348
Packit Service 1d0348
	e = archive_write_header(a, entry);
Packit Service 1d0348
	if (e != ARCHIVE_OK) {
Packit Service 1d0348
		if (bsdtar->verbose > 1) {
Packit Service 1d0348
			safe_fprintf(stderr, "a ");
Packit Service 1d0348
			list_item_verbose(bsdtar, stderr, entry);
Packit Service 1d0348
			lafe_warnc(0, ": %s", archive_error_string(a));
Packit Service 1d0348
		} else if (bsdtar->verbose > 0) {
Packit Service 1d0348
			lafe_warnc(0, "%s: %s",
Packit Service 1d0348
			    archive_entry_pathname(entry),
Packit Service 1d0348
			    archive_error_string(a));
Packit Service 1d0348
		} else
Packit Service 1d0348
			fprintf(stderr, ": %s", archive_error_string(a));
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	if (e == ARCHIVE_FATAL)
Packit Service 1d0348
		exit(1);
Packit Service 1d0348
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * If we opened a file earlier, write it out now.  Note that
Packit Service 1d0348
	 * the format handler might have reset the size field to zero
Packit Service 1d0348
	 * to inform us that the archive body won't get stored.  In
Packit Service 1d0348
	 * that case, just skip the write.
Packit Service 1d0348
	 */
Packit Service 1d0348
	if (e >= ARCHIVE_WARN && archive_entry_size(entry) > 0) {
Packit Service 1d0348
		if (copy_file_data_block(bsdtar, a, bsdtar->diskreader, entry))
Packit Service 1d0348
			exit(1);
Packit Service 1d0348
	}
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static void
Packit Service 1d0348
report_write(struct bsdtar *bsdtar, struct archive *a,
Packit Service 1d0348
    struct archive_entry *entry, int64_t progress)
Packit Service 1d0348
{
Packit Service 1d0348
	uint64_t comp, uncomp;
Packit Service 1d0348
	int compression;
Packit Service 1d0348
Packit Service 1d0348
	if (bsdtar->verbose)
Packit Service 1d0348
		fprintf(stderr, "\n");
Packit Service 1d0348
	comp = archive_filter_bytes(a, -1);
Packit Service 1d0348
	uncomp = archive_filter_bytes(a, 0);
Packit Service 1d0348
	fprintf(stderr, "In: %d files, %s bytes;",
Packit Service 1d0348
	    archive_file_count(a), tar_i64toa(uncomp));
Packit Service 1d0348
	if (comp >= uncomp)
Packit Service 1d0348
		compression = 0;
Packit Service 1d0348
	else
Packit Service 1d0348
		compression = (int)((uncomp - comp) * 100 / uncomp);
Packit Service 1d0348
	fprintf(stderr,
Packit Service 1d0348
	    " Out: %s bytes, compression %d%%\n",
Packit Service 1d0348
	    tar_i64toa(comp), compression);
Packit Service 1d0348
	/* Can't have two calls to tar_i64toa() pending, so split the output. */
Packit Service 1d0348
	safe_fprintf(stderr, "Current: %s (%s",
Packit Service 1d0348
	    archive_entry_pathname(entry),
Packit Service 1d0348
	    tar_i64toa(progress));
Packit Service 1d0348
	fprintf(stderr, "/%s bytes)\n",
Packit Service 1d0348
	    tar_i64toa(archive_entry_size(entry)));
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static void
Packit Service 1d0348
test_for_append(struct bsdtar *bsdtar)
Packit Service 1d0348
{
Packit Service 1d0348
	struct stat s;
Packit Service 1d0348
Packit Service 1d0348
	if (*bsdtar->argv == NULL && bsdtar->names_from_file == NULL)
Packit Service 1d0348
		lafe_errc(1, 0, "no files or directories specified");
Packit Service 1d0348
	if (bsdtar->filename == NULL)
Packit Service 1d0348
		lafe_errc(1, 0, "Cannot append to stdout.");
Packit Service 1d0348
Packit Service 1d0348
	if (stat(bsdtar->filename, &s) != 0)
Packit Service 1d0348
		return;
Packit Service 1d0348
Packit Service 1d0348
	if (!S_ISREG(s.st_mode) && !S_ISBLK(s.st_mode))
Packit Service 1d0348
		lafe_errc(1, 0,
Packit Service 1d0348
		    "Cannot append to %s: not a regular file.",
Packit Service 1d0348
		    bsdtar->filename);
Packit Service 1d0348
Packit Service 1d0348
/* Is this an appropriate check here on Windows? */
Packit Service 1d0348
/*
Packit Service 1d0348
	if (GetFileType(handle) != FILE_TYPE_DISK)
Packit Service 1d0348
		lafe_errc(1, 0, "Cannot append");
Packit Service 1d0348
*/
Packit Service 1d0348
Packit Service 1d0348
}