2004-04-05 21:32:18 +00:00
|
|
|
/*-
|
2007-01-09 08:12:17 +00:00
|
|
|
* Copyright (c) 2003-2007 Tim Kientzle
|
2004-04-05 21:32:18 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
2007-01-09 08:12:17 +00:00
|
|
|
* notice, this list of conditions and the following disclaimer.
|
2004-04-05 21:32:18 +00:00
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "bsdtar_platform.h"
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2007-03-11 10:36:42 +00:00
|
|
|
#ifdef HAVE_SYS_TYPES_H
|
2004-04-05 21:32:18 +00:00
|
|
|
#include <sys/types.h>
|
2007-03-11 10:36:42 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_ACL_H
|
2004-04-05 21:32:18 +00:00
|
|
|
#include <sys/acl.h>
|
|
|
|
#endif
|
2007-03-11 10:36:42 +00:00
|
|
|
#ifdef HAVE_SYS_IOCTL_H
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_STAT_H
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#endif
|
2006-03-21 17:03:51 +00:00
|
|
|
#ifdef HAVE_ATTR_XATTR_H
|
|
|
|
#include <attr/xattr.h>
|
|
|
|
#endif
|
2007-03-11 10:36:42 +00:00
|
|
|
#ifdef HAVE_ERRNO_H
|
2004-04-05 21:32:18 +00:00
|
|
|
#include <errno.h>
|
2007-03-11 10:36:42 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_FCNTL_H
|
2004-04-05 21:32:18 +00:00
|
|
|
#include <fcntl.h>
|
2007-03-11 10:36:42 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_FNMATCH_H
|
2004-04-05 21:32:18 +00:00
|
|
|
#include <fnmatch.h>
|
2007-03-11 10:36:42 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_GRP_H
|
2004-04-05 21:32:18 +00:00
|
|
|
#include <grp.h>
|
2007-03-11 10:36:42 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_LIMITS_H
|
2004-04-05 21:32:18 +00:00
|
|
|
#include <limits.h>
|
2007-03-11 10:36:42 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_LINUX_FS_H
|
|
|
|
#include <linux/fs.h> /* for Linux file flags */
|
|
|
|
#endif
|
2009-03-08 05:28:52 +00:00
|
|
|
/*
|
|
|
|
* Some Linux distributions have both linux/ext2_fs.h and ext2fs/ext2_fs.h.
|
|
|
|
* As the include guards don't agree, the order of include is important.
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_LINUX_EXT2_FS_H
|
|
|
|
#include <linux/ext2_fs.h> /* for Linux file flags */
|
|
|
|
#endif
|
|
|
|
#if defined(HAVE_EXT2FS_EXT2_FS_H) && !defined(__CYGWIN__)
|
|
|
|
/* This header exists but is broken on Cygwin. */
|
|
|
|
#include <ext2fs/ext2_fs.h>
|
|
|
|
#endif
|
2007-03-11 10:36:42 +00:00
|
|
|
#ifdef HAVE_PWD_H
|
2004-04-05 21:32:18 +00:00
|
|
|
#include <pwd.h>
|
2007-03-11 10:36:42 +00:00
|
|
|
#endif
|
2004-04-05 21:32:18 +00:00
|
|
|
#include <stdio.h>
|
2007-03-11 10:36:42 +00:00
|
|
|
#ifdef HAVE_STDLIB_H
|
2004-04-05 21:32:18 +00:00
|
|
|
#include <stdlib.h>
|
2007-03-11 10:36:42 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRING_H
|
2004-04-05 21:32:18 +00:00
|
|
|
#include <string.h>
|
2007-03-11 10:36:42 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_UNISTD_H
|
2004-04-05 21:32:18 +00:00
|
|
|
#include <unistd.h>
|
2004-05-02 18:10:35 +00:00
|
|
|
#endif
|
2004-04-05 21:32:18 +00:00
|
|
|
|
|
|
|
#include "bsdtar.h"
|
2005-04-24 05:53:37 +00:00
|
|
|
#include "tree.h"
|
2004-04-05 21:32:18 +00:00
|
|
|
|
2008-07-05 08:03:08 +00:00
|
|
|
/* Size of buffer for holding file data prior to writing. */
|
|
|
|
#define FILEDATABUFLEN 65536
|
|
|
|
|
2004-05-17 05:02:39 +00:00
|
|
|
/* Fixed size of uname/gname caches. */
|
|
|
|
#define name_cache_size 101
|
|
|
|
|
|
|
|
static const char * const NO_NAME = "(noname)";
|
2004-04-15 22:37:54 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2004-05-17 05:02:39 +00:00
|
|
|
struct name_cache {
|
|
|
|
int probes;
|
|
|
|
int hits;
|
|
|
|
size_t size;
|
2004-04-15 22:37:54 +00:00
|
|
|
struct {
|
2004-05-17 05:02:39 +00:00
|
|
|
id_t id;
|
|
|
|
const char *name;
|
|
|
|
} cache[name_cache_size];
|
2004-04-15 22:37:54 +00:00
|
|
|
};
|
2004-04-05 21:32:18 +00:00
|
|
|
|
|
|
|
static void add_dir_list(struct bsdtar *bsdtar, const char *path,
|
|
|
|
time_t mtime_sec, int mtime_nsec);
|
2004-05-17 05:02:39 +00:00
|
|
|
static int append_archive(struct bsdtar *, struct archive *,
|
2007-03-31 10:14:03 +00:00
|
|
|
struct archive *ina);
|
|
|
|
static int append_archive_filename(struct bsdtar *,
|
|
|
|
struct archive *, const char *fname);
|
2004-04-15 22:37:54 +00:00
|
|
|
static void archive_names_from_file(struct bsdtar *bsdtar,
|
2004-04-08 06:12:01 +00:00
|
|
|
struct archive *a);
|
2004-06-27 06:29:03 +00:00
|
|
|
static int archive_names_from_file_helper(struct bsdtar *bsdtar,
|
|
|
|
const char *line);
|
2007-04-09 08:22:34 +00:00
|
|
|
static int copy_file_data(struct bsdtar *bsdtar,
|
|
|
|
struct archive *a, struct archive *ina);
|
2004-04-05 21:32:18 +00:00
|
|
|
static int new_enough(struct bsdtar *, const char *path,
|
2005-03-13 18:36:24 +00:00
|
|
|
const struct stat *);
|
2004-04-15 22:37:54 +00:00
|
|
|
static void test_for_append(struct bsdtar *);
|
2004-04-05 21:32:18 +00:00
|
|
|
static void write_archive(struct archive *, struct bsdtar *);
|
2008-05-26 17:10:10 +00:00
|
|
|
static void write_entry_backend(struct bsdtar *, struct archive *,
|
2008-07-05 06:05:34 +00:00
|
|
|
struct archive_entry *);
|
2004-04-26 23:39:17 +00:00
|
|
|
static int write_file_data(struct bsdtar *, struct archive *,
|
2008-06-25 05:01:02 +00:00
|
|
|
struct archive_entry *, int fd);
|
2005-04-17 17:20:54 +00:00
|
|
|
static void write_hierarchy(struct bsdtar *, struct archive *,
|
2004-04-05 21:32:18 +00:00
|
|
|
const char *);
|
|
|
|
|
|
|
|
void
|
|
|
|
tar_mode_c(struct bsdtar *bsdtar)
|
|
|
|
{
|
|
|
|
struct archive *a;
|
|
|
|
int r;
|
|
|
|
|
2004-04-08 06:12:01 +00:00
|
|
|
if (*bsdtar->argv == NULL && bsdtar->names_from_file == NULL)
|
2004-05-17 05:44:53 +00:00
|
|
|
bsdtar_errc(bsdtar, 1, 0, "no files or directories specified");
|
2004-04-05 21:32:18 +00:00
|
|
|
|
|
|
|
a = archive_write_new();
|
|
|
|
|
|
|
|
/* Support any format that the library supports. */
|
2004-07-25 04:15:50 +00:00
|
|
|
if (bsdtar->create_format == NULL) {
|
|
|
|
r = archive_write_set_format_pax_restricted(a);
|
|
|
|
bsdtar->create_format = "pax restricted";
|
|
|
|
} else {
|
2004-04-05 21:32:18 +00:00
|
|
|
r = archive_write_set_format_by_name(a, bsdtar->create_format);
|
2004-07-25 04:15:50 +00:00
|
|
|
}
|
|
|
|
if (r != ARCHIVE_OK) {
|
|
|
|
fprintf(stderr, "Can't use format %s: %s\n",
|
|
|
|
bsdtar->create_format,
|
|
|
|
archive_error_string(a));
|
|
|
|
usage(bsdtar);
|
2004-04-05 21:32:18 +00:00
|
|
|
}
|
|
|
|
|
2004-06-07 03:19:57 +00:00
|
|
|
/*
|
|
|
|
* If user explicitly set the block size, then assume they
|
|
|
|
* want the last block padded as well. Otherwise, use the
|
|
|
|
* default block size and accept archive_write_open_file()'s
|
|
|
|
* default padding decisions.
|
|
|
|
*/
|
|
|
|
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);
|
2004-04-05 21:32:18 +00:00
|
|
|
|
2007-05-29 05:39:10 +00:00
|
|
|
if (bsdtar->compress_program) {
|
|
|
|
archive_write_set_compression_program(a, bsdtar->compress_program);
|
|
|
|
} else {
|
|
|
|
switch (bsdtar->create_compression) {
|
|
|
|
case 0:
|
|
|
|
archive_write_set_compression_none(a);
|
|
|
|
break;
|
2004-08-07 03:24:49 +00:00
|
|
|
#ifdef HAVE_LIBBZ2
|
2007-05-29 05:39:10 +00:00
|
|
|
case 'j': case 'y':
|
|
|
|
archive_write_set_compression_bzip2(a);
|
|
|
|
break;
|
2004-08-07 03:24:49 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_LIBZ
|
2007-05-29 05:39:10 +00:00
|
|
|
case 'z':
|
|
|
|
archive_write_set_compression_gzip(a);
|
|
|
|
break;
|
2004-08-07 03:24:49 +00:00
|
|
|
#endif
|
2008-03-15 02:41:44 +00:00
|
|
|
case 'Z':
|
|
|
|
archive_write_set_compression_compress(a);
|
|
|
|
break;
|
2007-05-29 05:39:10 +00:00
|
|
|
default:
|
|
|
|
bsdtar_errc(bsdtar, 1, 0,
|
|
|
|
"Unrecognized compression option -%c",
|
|
|
|
bsdtar->create_compression);
|
|
|
|
}
|
2004-04-05 21:32:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
r = archive_write_open_file(a, bsdtar->filename);
|
|
|
|
if (r != ARCHIVE_OK)
|
2004-05-17 05:44:53 +00:00
|
|
|
bsdtar_errc(bsdtar, 1, 0, archive_error_string(a));
|
2004-04-05 21:32:18 +00:00
|
|
|
|
2009-03-08 06:19:28 +00:00
|
|
|
if (bsdtar->option_format_options != NULL) {
|
|
|
|
r = archive_write_set_options(a, bsdtar->option_format_options);
|
|
|
|
if (r != ARCHIVE_OK)
|
|
|
|
bsdtar_errc(bsdtar, 1, 0, archive_error_string(a));
|
|
|
|
}
|
|
|
|
|
2004-04-05 21:32:18 +00:00
|
|
|
write_archive(a, bsdtar);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-02-08 07:25:53 +00:00
|
|
|
* Same as 'c', except we only support tar or empty formats in
|
|
|
|
* uncompressed files on disk.
|
2004-04-05 21:32:18 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
tar_mode_r(struct bsdtar *bsdtar)
|
|
|
|
{
|
|
|
|
off_t end_offset;
|
|
|
|
int format;
|
|
|
|
struct archive *a;
|
|
|
|
struct archive_entry *entry;
|
2007-02-08 07:25:53 +00:00
|
|
|
int r;
|
2004-04-05 21:32:18 +00:00
|
|
|
|
|
|
|
/* Sanity-test some arguments and the file. */
|
|
|
|
test_for_append(bsdtar);
|
|
|
|
|
|
|
|
format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
|
|
|
|
|
2007-02-08 07:25:53 +00:00
|
|
|
bsdtar->fd = open(bsdtar->filename, O_RDWR | O_CREAT, 0666);
|
2004-04-05 21:32:18 +00:00
|
|
|
if (bsdtar->fd < 0)
|
2004-05-17 05:44:53 +00:00
|
|
|
bsdtar_errc(bsdtar, 1, errno,
|
|
|
|
"Cannot open %s", bsdtar->filename);
|
2004-04-05 21:32:18 +00:00
|
|
|
|
|
|
|
a = archive_read_new();
|
|
|
|
archive_read_support_compression_all(a);
|
|
|
|
archive_read_support_format_tar(a);
|
|
|
|
archive_read_support_format_gnutar(a);
|
2007-02-08 07:25:53 +00:00
|
|
|
r = archive_read_open_fd(a, bsdtar->fd, 10240);
|
|
|
|
if (r != ARCHIVE_OK)
|
|
|
|
bsdtar_errc(bsdtar, 1, archive_errno(a),
|
|
|
|
"Can't read archive %s: %s", bsdtar->filename,
|
|
|
|
archive_error_string(a));
|
2004-04-05 21:32:18 +00:00
|
|
|
while (0 == archive_read_next_header(a, &entry)) {
|
|
|
|
if (archive_compression(a) != ARCHIVE_COMPRESSION_NONE) {
|
|
|
|
archive_read_finish(a);
|
|
|
|
close(bsdtar->fd);
|
2004-05-17 05:44:53 +00:00
|
|
|
bsdtar_errc(bsdtar, 1, 0,
|
2004-04-05 21:32:18 +00:00
|
|
|
"Cannot append to compressed archive.");
|
|
|
|
}
|
|
|
|
/* Keep going until we hit end-of-archive */
|
|
|
|
format = archive_format(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
end_offset = archive_read_header_position(a);
|
|
|
|
archive_read_finish(a);
|
|
|
|
|
|
|
|
/* Re-open archive for writing */
|
|
|
|
a = archive_write_new();
|
|
|
|
archive_write_set_compression_none(a);
|
|
|
|
/*
|
2007-02-08 07:25:53 +00:00
|
|
|
* Set the format to be used for writing. To allow people to
|
|
|
|
* extend empty files, we need to allow them to specify the format,
|
|
|
|
* which opens the possibility that they will specify a format that
|
|
|
|
* doesn't match the existing format. Hence, the following bit
|
|
|
|
* of arcane ugliness.
|
2004-04-05 21:32:18 +00:00
|
|
|
*/
|
2007-02-08 07:25:53 +00:00
|
|
|
|
|
|
|
if (bsdtar->create_format != NULL) {
|
|
|
|
/* If the user requested a format, use that, but ... */
|
|
|
|
archive_write_set_format_by_name(a,
|
|
|
|
bsdtar->create_format);
|
|
|
|
/* ... complain if it's not compatible. */
|
|
|
|
format &= ARCHIVE_FORMAT_BASE_MASK;
|
|
|
|
if (format != (int)(archive_format(a) & ARCHIVE_FORMAT_BASE_MASK)
|
|
|
|
&& format != ARCHIVE_FORMAT_EMPTY) {
|
|
|
|
bsdtar_errc(bsdtar, 1, 0,
|
|
|
|
"Format %s is incompatible with the archive %s.",
|
|
|
|
bsdtar->create_format, bsdtar->filename);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Just preserve the current format, with a little care
|
|
|
|
* for formats that libarchive can't write.
|
|
|
|
*/
|
|
|
|
if (format == ARCHIVE_FORMAT_TAR_GNUTAR)
|
|
|
|
/* TODO: When gtar supports pax, use pax restricted. */
|
|
|
|
format = ARCHIVE_FORMAT_TAR_USTAR;
|
|
|
|
if (format == ARCHIVE_FORMAT_EMPTY)
|
|
|
|
format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
|
|
|
|
archive_write_set_format(a, format);
|
|
|
|
}
|
2004-07-25 04:15:50 +00:00
|
|
|
lseek(bsdtar->fd, end_offset, SEEK_SET); /* XXX check return val XXX */
|
|
|
|
archive_write_open_fd(a, bsdtar->fd); /* XXX check return val XXX */
|
2009-03-08 06:19:28 +00:00
|
|
|
if (bsdtar->option_format_options != NULL) {
|
|
|
|
r = archive_write_set_options(a, bsdtar->option_format_options);
|
|
|
|
if (r != ARCHIVE_OK)
|
|
|
|
bsdtar_errc(bsdtar, 1, 0, archive_error_string(a));
|
|
|
|
}
|
2004-04-05 21:32:18 +00:00
|
|
|
|
2004-07-25 04:15:50 +00:00
|
|
|
write_archive(a, bsdtar); /* XXX check return val XXX */
|
2004-04-05 21:32:18 +00:00
|
|
|
|
|
|
|
close(bsdtar->fd);
|
|
|
|
bsdtar->fd = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tar_mode_u(struct bsdtar *bsdtar)
|
|
|
|
{
|
|
|
|
off_t end_offset;
|
|
|
|
struct archive *a;
|
|
|
|
struct archive_entry *entry;
|
|
|
|
int format;
|
|
|
|
struct archive_dir_entry *p;
|
2004-04-15 22:37:54 +00:00
|
|
|
struct archive_dir archive_dir;
|
2009-03-08 06:19:28 +00:00
|
|
|
int r;
|
2004-04-15 22:37:54 +00:00
|
|
|
|
|
|
|
bsdtar->archive_dir = &archive_dir;
|
|
|
|
memset(&archive_dir, 0, sizeof(archive_dir));
|
2004-04-05 21:32:18 +00:00
|
|
|
|
|
|
|
format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
|
|
|
|
|
|
|
|
/* Sanity-test some arguments and the file. */
|
|
|
|
test_for_append(bsdtar);
|
|
|
|
|
|
|
|
bsdtar->fd = open(bsdtar->filename, O_RDWR);
|
|
|
|
if (bsdtar->fd < 0)
|
2004-05-17 05:44:53 +00:00
|
|
|
bsdtar_errc(bsdtar, 1, errno,
|
|
|
|
"Cannot open %s", bsdtar->filename);
|
2004-04-05 21:32:18 +00:00
|
|
|
|
|
|
|
a = archive_read_new();
|
|
|
|
archive_read_support_compression_all(a);
|
|
|
|
archive_read_support_format_tar(a);
|
|
|
|
archive_read_support_format_gnutar(a);
|
2005-11-01 06:41:05 +00:00
|
|
|
if (archive_read_open_fd(a, bsdtar->fd,
|
2004-06-07 03:19:57 +00:00
|
|
|
bsdtar->bytes_per_block != 0 ? bsdtar->bytes_per_block :
|
2005-11-01 06:41:05 +00:00
|
|
|
DEFAULT_BYTES_PER_BLOCK) != ARCHIVE_OK) {
|
2005-11-06 22:53:51 +00:00
|
|
|
bsdtar_errc(bsdtar, 1, 0,
|
2005-11-01 06:41:05 +00:00
|
|
|
"Can't open %s: %s", bsdtar->filename,
|
|
|
|
archive_error_string(a));
|
|
|
|
}
|
2004-04-05 21:32:18 +00:00
|
|
|
|
|
|
|
/* Build a list of all entries and their recorded mod times. */
|
|
|
|
while (0 == archive_read_next_header(a, &entry)) {
|
|
|
|
if (archive_compression(a) != ARCHIVE_COMPRESSION_NONE) {
|
|
|
|
archive_read_finish(a);
|
|
|
|
close(bsdtar->fd);
|
2004-05-17 05:44:53 +00:00
|
|
|
bsdtar_errc(bsdtar, 1, 0,
|
2004-04-05 21:32:18 +00:00
|
|
|
"Cannot append to compressed archive.");
|
|
|
|
}
|
|
|
|
add_dir_list(bsdtar, archive_entry_pathname(entry),
|
|
|
|
archive_entry_mtime(entry),
|
|
|
|
archive_entry_mtime_nsec(entry));
|
|
|
|
/* Record the last format determination we see */
|
|
|
|
format = archive_format(a);
|
|
|
|
/* Keep going until we hit end-of-archive */
|
|
|
|
}
|
|
|
|
|
|
|
|
end_offset = archive_read_header_position(a);
|
|
|
|
archive_read_finish(a);
|
|
|
|
|
|
|
|
/* Re-open archive for writing. */
|
|
|
|
a = archive_write_new();
|
|
|
|
archive_write_set_compression_none(a);
|
|
|
|
/*
|
|
|
|
* Set format to same one auto-detected above, except that
|
|
|
|
* we don't write GNU tar format, so use ustar instead.
|
|
|
|
*/
|
|
|
|
if (format == ARCHIVE_FORMAT_TAR_GNUTAR)
|
|
|
|
format = ARCHIVE_FORMAT_TAR_USTAR;
|
|
|
|
archive_write_set_format(a, format);
|
2004-06-07 03:19:57 +00:00
|
|
|
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);
|
2004-04-05 21:32:18 +00:00
|
|
|
lseek(bsdtar->fd, end_offset, SEEK_SET);
|
|
|
|
ftruncate(bsdtar->fd, end_offset);
|
|
|
|
archive_write_open_fd(a, bsdtar->fd);
|
2009-03-08 06:19:28 +00:00
|
|
|
if (bsdtar->option_format_options != NULL) {
|
|
|
|
r = archive_write_set_options(a, bsdtar->option_format_options);
|
|
|
|
if (r != ARCHIVE_OK)
|
|
|
|
bsdtar_errc(bsdtar, 1, 0, archive_error_string(a));
|
|
|
|
}
|
2004-04-05 21:32:18 +00:00
|
|
|
|
|
|
|
write_archive(a, bsdtar);
|
|
|
|
|
|
|
|
close(bsdtar->fd);
|
|
|
|
bsdtar->fd = -1;
|
|
|
|
|
2004-04-15 22:37:54 +00:00
|
|
|
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;
|
2004-04-05 21:32:18 +00:00
|
|
|
}
|
2004-04-15 22:37:54 +00:00
|
|
|
bsdtar->archive_dir->tail = NULL;
|
2004-04-05 21:32:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2004-04-08 06:12:01 +00:00
|
|
|
* Write user-specified files/dirs to opened archive.
|
2004-04-05 21:32:18 +00:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
write_archive(struct archive *a, struct bsdtar *bsdtar)
|
|
|
|
{
|
|
|
|
const char *arg;
|
2008-05-26 17:10:10 +00:00
|
|
|
struct archive_entry *entry, *sparse_entry;
|
|
|
|
|
2008-07-05 08:10:55 +00:00
|
|
|
/* We want to catch SIGINFO and SIGUSR1. */
|
|
|
|
siginfo_init(bsdtar);
|
|
|
|
|
2008-07-05 08:03:08 +00:00
|
|
|
/* Allocate a buffer for file data. */
|
|
|
|
if ((bsdtar->buff = malloc(FILEDATABUFLEN)) == NULL)
|
|
|
|
bsdtar_errc(bsdtar, 1, 0, "cannot allocate memory");
|
|
|
|
|
2008-05-26 17:10:10 +00:00
|
|
|
if ((bsdtar->resolver = archive_entry_linkresolver_new()) == NULL)
|
|
|
|
bsdtar_errc(bsdtar, 1, 0, "cannot create link resolver");
|
|
|
|
archive_entry_linkresolver_set_strategy(bsdtar->resolver,
|
|
|
|
archive_format(a));
|
2009-03-08 05:22:50 +00:00
|
|
|
if ((bsdtar->diskreader = archive_read_disk_new()) == NULL)
|
|
|
|
bsdtar_errc(bsdtar, 1, 0, "Cannot create read_disk object");
|
|
|
|
archive_read_disk_set_standard_lookup(bsdtar->diskreader);
|
2004-04-05 21:32:18 +00:00
|
|
|
|
2004-04-08 06:12:01 +00:00
|
|
|
if (bsdtar->names_from_file != NULL)
|
|
|
|
archive_names_from_file(bsdtar, a);
|
|
|
|
|
2004-04-05 21:32:18 +00:00
|
|
|
while (*bsdtar->argv) {
|
|
|
|
arg = *bsdtar->argv;
|
2004-06-26 22:49:51 +00:00
|
|
|
if (arg[0] == '-' && arg[1] == 'C') {
|
2004-04-05 21:32:18 +00:00
|
|
|
arg += 2;
|
2004-06-26 22:49:51 +00:00
|
|
|
if (*arg == '\0') {
|
|
|
|
bsdtar->argv++;
|
|
|
|
arg = *bsdtar->argv;
|
2004-06-27 01:08:54 +00:00
|
|
|
if (arg == NULL) {
|
|
|
|
bsdtar_warnc(bsdtar, 1, 0,
|
2004-06-26 22:49:51 +00:00
|
|
|
"Missing argument for -C");
|
2004-06-27 01:08:54 +00:00
|
|
|
bsdtar->return_value = 1;
|
2008-07-05 08:10:55 +00:00
|
|
|
goto cleanup;
|
2004-06-27 01:08:54 +00:00
|
|
|
}
|
2004-06-26 22:49:51 +00:00
|
|
|
}
|
2004-08-08 05:50:10 +00:00
|
|
|
set_chdir(bsdtar, arg);
|
2004-04-05 21:32:18 +00:00
|
|
|
} else {
|
2007-03-17 16:17:14 +00:00
|
|
|
if (*arg != '/' && (arg[0] != '@' || arg[1] != '/'))
|
2004-08-08 05:50:10 +00:00
|
|
|
do_chdir(bsdtar); /* Handle a deferred -C */
|
2004-06-27 03:28:13 +00:00
|
|
|
if (*arg == '@') {
|
2007-03-31 10:14:03 +00:00
|
|
|
if (append_archive_filename(bsdtar, a,
|
|
|
|
arg + 1) != 0)
|
2004-06-27 03:28:13 +00:00
|
|
|
break;
|
|
|
|
} else
|
2009-03-08 05:47:21 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
write_hierarchy_win(bsdtar, a, arg,
|
|
|
|
write_hierarchy);
|
|
|
|
#else
|
2005-04-17 17:20:54 +00:00
|
|
|
write_hierarchy(bsdtar, a, arg);
|
2009-03-08 05:47:21 +00:00
|
|
|
#endif
|
2004-04-05 21:32:18 +00:00
|
|
|
}
|
|
|
|
bsdtar->argv++;
|
|
|
|
}
|
|
|
|
|
2008-05-26 17:10:10 +00:00
|
|
|
entry = NULL;
|
|
|
|
archive_entry_linkify(bsdtar->resolver, &entry, &sparse_entry);
|
|
|
|
while (entry != NULL) {
|
2008-07-05 06:05:34 +00:00
|
|
|
write_entry_backend(bsdtar, a, entry);
|
2008-05-26 17:10:10 +00:00
|
|
|
archive_entry_free(entry);
|
|
|
|
entry = NULL;
|
|
|
|
archive_entry_linkify(bsdtar->resolver, &entry, &sparse_entry);
|
|
|
|
}
|
|
|
|
|
2007-01-02 12:24:14 +00:00
|
|
|
if (archive_write_close(a)) {
|
|
|
|
bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(a));
|
|
|
|
bsdtar->return_value = 1;
|
|
|
|
}
|
2008-07-05 08:03:08 +00:00
|
|
|
|
2008-07-05 08:10:55 +00:00
|
|
|
cleanup:
|
2008-07-05 08:03:08 +00:00
|
|
|
/* Free file data buffer. */
|
|
|
|
free(bsdtar->buff);
|
2009-03-08 05:22:50 +00:00
|
|
|
archive_entry_linkresolver_free(bsdtar->resolver);
|
|
|
|
bsdtar->resolver = NULL;
|
|
|
|
archive_read_finish(bsdtar->diskreader);
|
|
|
|
bsdtar->diskreader = NULL;
|
2008-07-05 08:10:55 +00:00
|
|
|
|
|
|
|
if (bsdtar->option_totals) {
|
|
|
|
fprintf(stderr, "Total bytes written: " BSDTAR_FILESIZE_PRINTF "\n",
|
|
|
|
(BSDTAR_FILESIZE_TYPE)archive_position_compressed(a));
|
|
|
|
}
|
|
|
|
|
|
|
|
archive_write_finish(a);
|
|
|
|
|
|
|
|
/* Restore old SIGINFO + SIGUSR1 handlers. */
|
|
|
|
siginfo_done(bsdtar);
|
2004-04-05 21:32:18 +00:00
|
|
|
}
|
|
|
|
|
2004-06-27 06:29:03 +00:00
|
|
|
/*
|
|
|
|
* Archive names specified in file.
|
|
|
|
*
|
|
|
|
* Unless --null was specified, a line containing exactly "-C" will
|
|
|
|
* cause the next line to be a directory to pass to chdir(). If
|
|
|
|
* --null is specified, then a line "-C" is just another filename.
|
|
|
|
*/
|
2004-04-08 06:12:01 +00:00
|
|
|
void
|
|
|
|
archive_names_from_file(struct bsdtar *bsdtar, struct archive *a)
|
|
|
|
{
|
2004-06-27 06:29:03 +00:00
|
|
|
bsdtar->archive = a;
|
2004-04-08 06:12:01 +00:00
|
|
|
|
2004-06-27 06:29:03 +00:00
|
|
|
bsdtar->next_line_is_dir = 0;
|
|
|
|
process_lines(bsdtar, bsdtar->names_from_file,
|
|
|
|
archive_names_from_file_helper);
|
|
|
|
if (bsdtar->next_line_is_dir)
|
|
|
|
bsdtar_errc(bsdtar, 1, errno,
|
|
|
|
"Unexpected end of filename list; "
|
|
|
|
"directory expected after -C");
|
|
|
|
}
|
2004-04-08 06:12:01 +00:00
|
|
|
|
2004-06-27 06:29:03 +00:00
|
|
|
static int
|
|
|
|
archive_names_from_file_helper(struct bsdtar *bsdtar, const char *line)
|
|
|
|
{
|
|
|
|
if (bsdtar->next_line_is_dir) {
|
2004-08-28 05:28:19 +00:00
|
|
|
set_chdir(bsdtar, line);
|
2004-06-27 06:29:03 +00:00
|
|
|
bsdtar->next_line_is_dir = 0;
|
|
|
|
} else if (!bsdtar->option_null && strcmp(line, "-C") == 0)
|
|
|
|
bsdtar->next_line_is_dir = 1;
|
2004-08-28 05:28:19 +00:00
|
|
|
else {
|
|
|
|
if (*line != '/')
|
|
|
|
do_chdir(bsdtar); /* Handle a deferred -C */
|
2005-04-17 17:20:54 +00:00
|
|
|
write_hierarchy(bsdtar, bsdtar->archive, line);
|
2004-08-28 05:28:19 +00:00
|
|
|
}
|
2004-06-27 06:29:03 +00:00
|
|
|
return (0);
|
2004-04-08 06:12:01 +00:00
|
|
|
}
|
2004-04-05 21:32:18 +00:00
|
|
|
|
2004-06-27 03:28:13 +00:00
|
|
|
/*
|
2006-03-21 17:03:51 +00:00
|
|
|
* Copy from specified archive to current archive. Returns non-zero
|
|
|
|
* for write errors (which force us to terminate the entire archiving
|
|
|
|
* operation). If there are errors reading the input archive, we set
|
|
|
|
* bsdtar->return_value but return zero, so the overall archiving
|
|
|
|
* operation will complete and return non-zero.
|
2004-06-27 03:28:13 +00:00
|
|
|
*/
|
2004-04-05 21:32:18 +00:00
|
|
|
static int
|
2007-03-31 10:14:03 +00:00
|
|
|
append_archive_filename(struct bsdtar *bsdtar, struct archive *a,
|
|
|
|
const char *filename)
|
2004-04-05 21:32:18 +00:00
|
|
|
{
|
|
|
|
struct archive *ina;
|
2007-03-31 10:14:03 +00:00
|
|
|
int rc;
|
2004-04-05 21:32:18 +00:00
|
|
|
|
2004-06-27 03:28:13 +00:00
|
|
|
if (strcmp(filename, "-") == 0)
|
|
|
|
filename = NULL; /* Library uses NULL for stdio. */
|
|
|
|
|
2004-04-05 21:32:18 +00:00
|
|
|
ina = archive_read_new();
|
|
|
|
archive_read_support_format_all(ina);
|
|
|
|
archive_read_support_compression_all(ina);
|
2004-06-27 03:28:13 +00:00
|
|
|
if (archive_read_open_file(ina, filename, 10240)) {
|
|
|
|
bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(ina));
|
|
|
|
bsdtar->return_value = 1;
|
|
|
|
return (0);
|
|
|
|
}
|
2007-03-31 10:14:03 +00:00
|
|
|
|
|
|
|
rc = append_archive(bsdtar, a, ina);
|
|
|
|
|
|
|
|
if (archive_errno(ina)) {
|
|
|
|
bsdtar_warnc(bsdtar, 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;
|
2007-04-09 08:22:34 +00:00
|
|
|
int e;
|
2007-03-31 10:14:03 +00:00
|
|
|
|
2004-04-05 21:32:18 +00:00
|
|
|
while (0 == archive_read_next_header(ina, &in_entry)) {
|
|
|
|
if (!new_enough(bsdtar, archive_entry_pathname(in_entry),
|
2005-03-13 18:36:24 +00:00
|
|
|
archive_entry_stat(in_entry)))
|
2004-04-05 21:32:18 +00:00
|
|
|
continue;
|
|
|
|
if (excluded(bsdtar, 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));
|
2008-05-18 06:24:47 +00:00
|
|
|
siginfo_setinfo(bsdtar, "copying",
|
|
|
|
archive_entry_pathname(in_entry),
|
|
|
|
archive_entry_size(in_entry));
|
|
|
|
siginfo_printinfo(bsdtar, 0);
|
2007-04-09 08:22:34 +00:00
|
|
|
|
|
|
|
e = archive_write_header(a, in_entry);
|
|
|
|
if (e != ARCHIVE_OK) {
|
|
|
|
if (!bsdtar->verbose)
|
|
|
|
bsdtar_warnc(bsdtar, 0, "%s: %s",
|
|
|
|
archive_entry_pathname(in_entry),
|
2004-04-05 21:32:18 +00:00
|
|
|
archive_error_string(a));
|
2007-04-09 08:22:34 +00:00
|
|
|
else
|
|
|
|
fprintf(stderr, ": %s", archive_error_string(a));
|
2004-04-05 21:32:18 +00:00
|
|
|
}
|
2007-04-09 08:22:34 +00:00
|
|
|
if (e == ARCHIVE_FATAL)
|
|
|
|
exit(1);
|
|
|
|
|
2008-05-26 17:10:10 +00:00
|
|
|
if (e >= ARCHIVE_WARN) {
|
|
|
|
if (archive_entry_size(in_entry) == 0)
|
|
|
|
archive_read_data_skip(ina);
|
|
|
|
else if (copy_file_data(bsdtar, a, ina))
|
2007-04-09 08:22:34 +00:00
|
|
|
exit(1);
|
2008-05-26 17:10:10 +00:00
|
|
|
}
|
2007-04-09 08:22:34 +00:00
|
|
|
|
2004-04-05 21:32:18 +00:00
|
|
|
if (bsdtar->verbose)
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
|
2006-03-21 17:03:51 +00:00
|
|
|
/* Note: If we got here, we saw no write errors, so return success. */
|
|
|
|
return (0);
|
2004-04-05 21:32:18 +00:00
|
|
|
}
|
|
|
|
|
2007-04-09 08:22:34 +00:00
|
|
|
/* Helper function to copy data between archives. */
|
|
|
|
static int
|
|
|
|
copy_file_data(struct bsdtar *bsdtar, struct archive *a, struct archive *ina)
|
|
|
|
{
|
|
|
|
ssize_t bytes_read;
|
|
|
|
ssize_t bytes_written;
|
2008-05-18 06:24:47 +00:00
|
|
|
off_t progress = 0;
|
2007-04-09 08:22:34 +00:00
|
|
|
|
2008-07-05 08:03:08 +00:00
|
|
|
bytes_read = archive_read_data(ina, bsdtar->buff, FILEDATABUFLEN);
|
2007-04-09 08:22:34 +00:00
|
|
|
while (bytes_read > 0) {
|
2008-05-18 06:24:47 +00:00
|
|
|
siginfo_printinfo(bsdtar, progress);
|
|
|
|
|
2008-07-05 08:03:08 +00:00
|
|
|
bytes_written = archive_write_data(a, bsdtar->buff,
|
|
|
|
bytes_read);
|
2007-04-09 08:22:34 +00:00
|
|
|
if (bytes_written < bytes_read) {
|
|
|
|
bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(a));
|
|
|
|
return (-1);
|
|
|
|
}
|
2008-05-18 06:24:47 +00:00
|
|
|
progress += bytes_written;
|
2008-07-05 08:03:08 +00:00
|
|
|
bytes_read = archive_read_data(ina, bsdtar->buff,
|
|
|
|
FILEDATABUFLEN);
|
2007-04-09 08:22:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2004-04-05 21:32:18 +00:00
|
|
|
/*
|
2005-04-17 17:20:54 +00:00
|
|
|
* Add the file or dir hierarchy named by 'path' to the archive
|
2004-04-05 21:32:18 +00:00
|
|
|
*/
|
|
|
|
static void
|
2005-04-17 17:20:54 +00:00
|
|
|
write_hierarchy(struct bsdtar *bsdtar, struct archive *a, const char *path)
|
2004-04-05 21:32:18 +00:00
|
|
|
{
|
2009-03-08 05:22:50 +00:00
|
|
|
struct archive_entry *entry = NULL, *spare_entry = NULL;
|
2005-04-24 05:53:37 +00:00
|
|
|
struct tree *tree;
|
|
|
|
char symlink_mode = bsdtar->symlink_mode;
|
|
|
|
dev_t first_dev = 0;
|
|
|
|
int dev_recorded = 0;
|
|
|
|
int tree_ret;
|
2004-04-05 21:32:18 +00:00
|
|
|
|
2005-04-24 05:53:37 +00:00
|
|
|
tree = tree_open(path);
|
2004-04-05 21:32:18 +00:00
|
|
|
|
2005-04-24 05:53:37 +00:00
|
|
|
if (!tree) {
|
2004-05-17 05:44:53 +00:00
|
|
|
bsdtar_warnc(bsdtar, errno, "%s: Cannot open", path);
|
2004-06-07 07:19:04 +00:00
|
|
|
bsdtar->return_value = 1;
|
2004-04-05 21:32:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-04-24 05:53:37 +00:00
|
|
|
while ((tree_ret = tree_next(tree))) {
|
2009-03-08 05:22:50 +00:00
|
|
|
int r;
|
2005-04-24 05:53:37 +00:00
|
|
|
const char *name = tree_current_path(tree);
|
2009-03-08 05:22:50 +00:00
|
|
|
const struct stat *st = NULL; /* info to use for this entry */
|
|
|
|
const struct stat *lst = NULL; /* lstat() information */
|
2005-04-24 05:53:37 +00:00
|
|
|
int descend;
|
|
|
|
|
2008-11-27 05:49:52 +00:00
|
|
|
if (tree_ret == TREE_ERROR_FATAL)
|
|
|
|
bsdtar_errc(bsdtar, 1, tree_errno(tree),
|
|
|
|
"%s: Unable to continue traversing directory tree",
|
|
|
|
name);
|
2008-11-27 05:14:36 +00:00
|
|
|
if (tree_ret == TREE_ERROR_DIR) {
|
2008-11-27 05:49:52 +00:00
|
|
|
bsdtar_warnc(bsdtar, errno,
|
|
|
|
"%s: Couldn't visit directory", name);
|
2008-11-27 05:14:36 +00:00
|
|
|
bsdtar->return_value = 1;
|
|
|
|
}
|
2005-04-24 05:53:37 +00:00
|
|
|
if (tree_ret != TREE_REGULAR)
|
|
|
|
continue;
|
2009-03-08 05:22:50 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If this file/dir is excluded by a filename
|
|
|
|
* pattern, skip it.
|
|
|
|
*/
|
|
|
|
if (excluded(bsdtar, name))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get lstat() info from the tree library.
|
|
|
|
*/
|
2005-04-24 05:53:37 +00:00
|
|
|
lst = tree_current_lstat(tree);
|
|
|
|
if (lst == NULL) {
|
|
|
|
/* Couldn't lstat(); must not exist. */
|
2007-03-14 07:30:51 +00:00
|
|
|
bsdtar_warnc(bsdtar, errno, "%s: Cannot stat", name);
|
2009-03-08 05:22:50 +00:00
|
|
|
/* Return error if files disappear during traverse. */
|
|
|
|
bsdtar->return_value = 1;
|
2005-04-24 05:53:37 +00:00
|
|
|
continue;
|
|
|
|
}
|
2009-03-08 05:22:50 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Distinguish 'L'/'P'/'H' symlink following.
|
|
|
|
*/
|
|
|
|
switch(symlink_mode) {
|
|
|
|
case 'H':
|
|
|
|
/* 'H': After the first item, rest like 'P'. */
|
|
|
|
symlink_mode = 'P';
|
|
|
|
/* 'H': First item (from command line) like 'L'. */
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case 'L':
|
|
|
|
/* 'L': Do descend through a symlink to dir. */
|
|
|
|
descend = tree_current_is_dir(tree);
|
|
|
|
/* 'L': Follow symlinks to files. */
|
|
|
|
archive_read_disk_set_symlink_logical(bsdtar->diskreader);
|
|
|
|
/* 'L': Archive symlinks as targets, if we can. */
|
2005-04-24 05:53:37 +00:00
|
|
|
st = tree_current_stat(tree);
|
2009-03-08 05:22:50 +00:00
|
|
|
if (st != NULL)
|
|
|
|
break;
|
|
|
|
/* If stat fails, we have a broken symlink;
|
|
|
|
* in that case, don't follow the link. */
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
default:
|
|
|
|
/* 'P': Don't descend through a symlink to dir. */
|
|
|
|
descend = tree_current_is_physical_dir(tree);
|
|
|
|
/* 'P': Don't follow symlinks to files. */
|
|
|
|
archive_read_disk_set_symlink_physical(bsdtar->diskreader);
|
|
|
|
/* 'P': Archive symlinks as symlinks. */
|
|
|
|
st = lst;
|
|
|
|
break;
|
|
|
|
}
|
2005-04-24 05:53:37 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If user has asked us not to cross mount points,
|
|
|
|
* then don't descend into into a dir on a different
|
|
|
|
* device.
|
|
|
|
*/
|
|
|
|
if (!dev_recorded) {
|
|
|
|
first_dev = lst->st_dev;
|
|
|
|
dev_recorded = 1;
|
|
|
|
}
|
|
|
|
if (bsdtar->option_dont_traverse_mounts) {
|
2009-03-08 05:22:50 +00:00
|
|
|
if (lst->st_dev != first_dev)
|
2005-04-24 05:53:37 +00:00
|
|
|
descend = 0;
|
|
|
|
}
|
|
|
|
|
2009-03-08 05:22:50 +00:00
|
|
|
/*
|
|
|
|
* In -u mode, check that the file is newer than what's
|
|
|
|
* already in the archive; in all modes, obey --newerXXX flags.
|
|
|
|
*/
|
|
|
|
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));
|
|
|
|
|
|
|
|
/* Populate the archive_entry with metadata from the disk. */
|
|
|
|
/* XXX TODO: Arrange to open a regular file before
|
|
|
|
* calling this so we can pass in an fd and shorten
|
|
|
|
* the race to query metadata. The linkify dance
|
|
|
|
* makes this more complex than it might sound. */
|
|
|
|
r = archive_read_disk_entry_from_file(bsdtar->diskreader,
|
|
|
|
entry, -1, st);
|
|
|
|
if (r != ARCHIVE_OK)
|
|
|
|
bsdtar_warnc(bsdtar, archive_errno(bsdtar->diskreader),
|
|
|
|
archive_error_string(bsdtar->diskreader));
|
|
|
|
if (r < ARCHIVE_WARN)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* XXX TODO: Just use flag data from entry; avoid the
|
|
|
|
* duplicate check here. */
|
|
|
|
|
2005-04-24 05:53:37 +00:00
|
|
|
/*
|
|
|
|
* If this file/dir is flagged "nodump" and we're
|
|
|
|
* honoring such flags, skip this file/dir.
|
|
|
|
*/
|
2008-09-14 03:49:00 +00:00
|
|
|
#ifdef HAVE_STRUCT_STAT_ST_FLAGS
|
|
|
|
/* BSD systems store flags in struct stat */
|
2005-04-24 05:53:37 +00:00
|
|
|
if (bsdtar->option_honor_nodump &&
|
|
|
|
(lst->st_flags & UF_NODUMP))
|
|
|
|
continue;
|
2004-04-05 21:32:18 +00:00
|
|
|
#endif
|
|
|
|
|
2008-09-14 03:49:00 +00:00
|
|
|
#if defined(EXT2_IOC_GETFLAGS) && defined(EXT2_NODUMP_FL)
|
|
|
|
/* Linux uses ioctl to read flags. */
|
|
|
|
if (bsdtar->option_honor_nodump) {
|
|
|
|
int fd = open(name, O_RDONLY | O_NONBLOCK);
|
|
|
|
if (fd >= 0) {
|
|
|
|
unsigned long fflags;
|
|
|
|
int r = ioctl(fd, EXT2_IOC_GETFLAGS, &fflags);
|
|
|
|
close(fd);
|
|
|
|
if (r >= 0 && (fflags & EXT2_NODUMP_FL))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2004-04-05 21:32:18 +00:00
|
|
|
#endif
|
2004-05-02 18:10:35 +00:00
|
|
|
|
2005-04-24 05:53:37 +00:00
|
|
|
/*
|
|
|
|
* If the user vetoes this file/directory, skip it.
|
2009-03-08 05:22:50 +00:00
|
|
|
* We want this to be fairly late; if some other
|
|
|
|
* check would veto this file, we shouldn't bother
|
|
|
|
* the user with it.
|
2005-04-24 05:53:37 +00:00
|
|
|
*/
|
|
|
|
if (bsdtar->option_interactive &&
|
|
|
|
!yes("add '%s'", name))
|
|
|
|
continue;
|
2004-04-05 21:32:18 +00:00
|
|
|
|
2009-03-08 05:22:50 +00:00
|
|
|
/* Note: if user vetoes, we won't descend. */
|
|
|
|
if (descend && !bsdtar->option_no_subdirs)
|
|
|
|
tree_descend(tree);
|
2005-04-24 05:53:37 +00:00
|
|
|
|
|
|
|
/*
|
2009-03-08 05:22:50 +00:00
|
|
|
* Rewrite the pathname to be archived. If rewrite
|
|
|
|
* fails, skip the entry.
|
2005-04-24 05:53:37 +00:00
|
|
|
*/
|
2009-03-08 05:22:50 +00:00
|
|
|
if (edit_pathname(bsdtar, entry))
|
|
|
|
continue;
|
2004-04-05 21:32:18 +00:00
|
|
|
|
2009-03-08 05:22:50 +00:00
|
|
|
/* Display entry as we process it.
|
|
|
|
* This format is required by SUSv2. */
|
|
|
|
if (bsdtar->verbose)
|
|
|
|
safe_fprintf(stderr, "a %s",
|
|
|
|
archive_entry_pathname(entry));
|
2005-04-24 05:53:37 +00:00
|
|
|
|
2009-03-08 05:22:50 +00:00
|
|
|
/* Non-regular files get archived with zero size. */
|
|
|
|
if (!S_ISREG(st->st_mode))
|
|
|
|
archive_entry_set_size(entry, 0);
|
|
|
|
|
|
|
|
/* Record what we're doing, for SIGINFO / SIGUSR1. */
|
|
|
|
siginfo_setinfo(bsdtar, "adding",
|
|
|
|
archive_entry_pathname(entry), archive_entry_size(entry));
|
|
|
|
archive_entry_linkify(bsdtar->resolver, &entry, &spare_entry);
|
|
|
|
|
|
|
|
/* Handle SIGINFO / SIGUSR1 request if one was made. */
|
|
|
|
siginfo_printinfo(bsdtar, 0);
|
|
|
|
|
|
|
|
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");
|
2004-04-05 21:32:18 +00:00
|
|
|
}
|
2009-03-08 05:22:50 +00:00
|
|
|
archive_entry_free(entry);
|
2005-04-24 05:53:37 +00:00
|
|
|
tree_close(tree);
|
2004-04-05 21:32:18 +00:00
|
|
|
}
|
|
|
|
|
2008-05-26 17:10:10 +00:00
|
|
|
/*
|
|
|
|
* Backend for write_entry.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
write_entry_backend(struct bsdtar *bsdtar, struct archive *a,
|
2008-07-05 06:05:34 +00:00
|
|
|
struct archive_entry *entry)
|
2008-05-26 17:10:10 +00:00
|
|
|
{
|
2008-07-05 06:05:34 +00:00
|
|
|
int fd = -1;
|
2008-05-26 17:10:10 +00:00
|
|
|
int e;
|
|
|
|
|
2008-07-05 06:05:34 +00:00
|
|
|
if (archive_entry_size(entry) > 0) {
|
2008-05-26 17:10:10 +00:00
|
|
|
const char *pathname = archive_entry_sourcepath(entry);
|
|
|
|
fd = open(pathname, O_RDONLY);
|
|
|
|
if (fd == -1) {
|
|
|
|
if (!bsdtar->verbose)
|
|
|
|
bsdtar_warnc(bsdtar, 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)
|
|
|
|
bsdtar_warnc(bsdtar, 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 we opened a file earlier, write it out now. Note that
|
|
|
|
* the format handler might have reset the size field to zero
|
|
|
|
* to inform us that the archive body won't get stored. In
|
|
|
|
* that case, just skip the write.
|
|
|
|
*/
|
|
|
|
if (e >= ARCHIVE_WARN && fd >= 0 && archive_entry_size(entry) > 0) {
|
2008-06-25 05:01:02 +00:00
|
|
|
if (write_file_data(bsdtar, a, entry, fd))
|
2008-05-26 17:10:10 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2008-07-05 06:05:34 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If we opened a file, close it now even if there was an error
|
|
|
|
* which made us decide not to write the archive body.
|
|
|
|
*/
|
|
|
|
if (fd >= 0)
|
|
|
|
close(fd);
|
2008-05-26 17:10:10 +00:00
|
|
|
}
|
|
|
|
|
2004-04-05 21:32:18 +00:00
|
|
|
|
2008-07-05 08:03:08 +00:00
|
|
|
/* Helper function to copy file to archive. */
|
2004-04-05 21:32:18 +00:00
|
|
|
static int
|
2008-06-25 05:01:02 +00:00
|
|
|
write_file_data(struct bsdtar *bsdtar, struct archive *a,
|
|
|
|
struct archive_entry *entry, int fd)
|
2004-04-05 21:32:18 +00:00
|
|
|
{
|
|
|
|
ssize_t bytes_read;
|
|
|
|
ssize_t bytes_written;
|
2008-05-18 06:24:47 +00:00
|
|
|
off_t progress = 0;
|
2004-04-05 21:32:18 +00:00
|
|
|
|
2008-07-05 08:03:08 +00:00
|
|
|
bytes_read = read(fd, bsdtar->buff, FILEDATABUFLEN);
|
2004-04-05 21:32:18 +00:00
|
|
|
while (bytes_read > 0) {
|
2008-05-18 06:24:47 +00:00
|
|
|
siginfo_printinfo(bsdtar, progress);
|
|
|
|
|
2008-07-05 08:03:08 +00:00
|
|
|
bytes_written = archive_write_data(a, bsdtar->buff,
|
2009-02-08 22:02:46 +00:00
|
|
|
bytes_read);
|
2007-02-14 08:16:08 +00:00
|
|
|
if (bytes_written < 0) {
|
2007-01-02 12:24:14 +00:00
|
|
|
/* Write failed; this is bad */
|
|
|
|
bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(a));
|
|
|
|
return (-1);
|
|
|
|
}
|
2007-02-14 08:16:08 +00:00
|
|
|
if (bytes_written < bytes_read) {
|
|
|
|
/* Write was truncated; warn but continue. */
|
|
|
|
bsdtar_warnc(bsdtar, 0,
|
2008-06-25 05:01:02 +00:00
|
|
|
"%s: Truncated write; file may have grown while being archived.",
|
|
|
|
archive_entry_pathname(entry));
|
2007-02-14 08:16:08 +00:00
|
|
|
return (0);
|
|
|
|
}
|
2008-05-18 06:24:47 +00:00
|
|
|
progress += bytes_written;
|
2008-07-05 08:03:08 +00:00
|
|
|
bytes_read = read(fd, bsdtar->buff, FILEDATABUFLEN);
|
2004-04-05 21:32:18 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-03-14 00:30:35 +00:00
|
|
|
* Test if the specified file is new enough to include in the archive.
|
2004-04-05 21:32:18 +00:00
|
|
|
*/
|
|
|
|
int
|
2005-03-13 18:36:24 +00:00
|
|
|
new_enough(struct bsdtar *bsdtar, const char *path, const struct stat *st)
|
2004-04-05 21:32:18 +00:00
|
|
|
{
|
|
|
|
struct archive_dir_entry *p;
|
|
|
|
|
2005-03-13 18:36:24 +00:00
|
|
|
/*
|
|
|
|
* If this file/dir is excluded by a time comparison, skip it.
|
|
|
|
*/
|
|
|
|
if (bsdtar->newer_ctime_sec > 0) {
|
|
|
|
if (st->st_ctime < bsdtar->newer_ctime_sec)
|
|
|
|
return (0); /* Too old, skip it. */
|
|
|
|
if (st->st_ctime == bsdtar->newer_ctime_sec
|
|
|
|
&& ARCHIVE_STAT_CTIME_NANOS(st)
|
|
|
|
<= bsdtar->newer_ctime_nsec)
|
|
|
|
return (0); /* Too old, skip it. */
|
|
|
|
}
|
|
|
|
if (bsdtar->newer_mtime_sec > 0) {
|
|
|
|
if (st->st_mtime < bsdtar->newer_mtime_sec)
|
|
|
|
return (0); /* Too old, skip it. */
|
|
|
|
if (st->st_mtime == bsdtar->newer_mtime_sec
|
|
|
|
&& ARCHIVE_STAT_MTIME_NANOS(st)
|
|
|
|
<= bsdtar->newer_mtime_nsec)
|
|
|
|
return (0); /* Too old, skip it. */
|
|
|
|
}
|
2004-04-05 21:32:18 +00:00
|
|
|
|
2005-03-13 18:36:24 +00:00
|
|
|
/*
|
|
|
|
* In -u mode, we only write an entry if it's newer than
|
|
|
|
* what was already in the archive.
|
|
|
|
*/
|
|
|
|
if (bsdtar->archive_dir != NULL &&
|
|
|
|
bsdtar->archive_dir->head != NULL) {
|
|
|
|
for (p = bsdtar->archive_dir->head; p != NULL; p = p->next) {
|
2007-03-11 10:36:42 +00:00
|
|
|
if (pathcmp(path, p->name)==0)
|
2005-03-13 18:36:24 +00:00
|
|
|
return (p->mtime_sec < st->st_mtime ||
|
|
|
|
(p->mtime_sec == st->st_mtime &&
|
|
|
|
p->mtime_nsec
|
|
|
|
< ARCHIVE_STAT_MTIME_NANOS(st)));
|
|
|
|
}
|
2004-04-05 21:32:18 +00:00
|
|
|
}
|
2005-03-13 18:36:24 +00:00
|
|
|
|
|
|
|
/* If the file wasn't rejected, include it. */
|
2004-04-05 21:32:18 +00:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add an entry to the dir list for 'u' mode.
|
|
|
|
*
|
|
|
|
* XXX TODO: Make this fast.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
add_dir_list(struct bsdtar *bsdtar, const char *path,
|
|
|
|
time_t mtime_sec, int mtime_nsec)
|
|
|
|
{
|
|
|
|
struct archive_dir_entry *p;
|
|
|
|
|
2004-04-15 22:37:54 +00:00
|
|
|
/*
|
|
|
|
* Search entire list to see if this file has appeared before.
|
|
|
|
* If it has, override the timestamp data.
|
|
|
|
*/
|
|
|
|
p = bsdtar->archive_dir->head;
|
2004-04-05 21:32:18 +00:00
|
|
|
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));
|
2004-07-25 04:15:50 +00:00
|
|
|
if (p == NULL)
|
|
|
|
bsdtar_errc(bsdtar, 1, ENOMEM, "Can't read archive directory");
|
|
|
|
|
2004-04-05 21:32:18 +00:00
|
|
|
p->name = strdup(path);
|
2004-07-25 04:15:50 +00:00
|
|
|
if (p->name == NULL)
|
|
|
|
bsdtar_errc(bsdtar, 1, ENOMEM, "Can't read archive directory");
|
2004-04-05 21:32:18 +00:00
|
|
|
p->mtime_sec = mtime_sec;
|
|
|
|
p->mtime_nsec = mtime_nsec;
|
|
|
|
p->next = NULL;
|
2004-04-15 22:37:54 +00:00
|
|
|
if (bsdtar->archive_dir->tail == NULL) {
|
|
|
|
bsdtar->archive_dir->head = bsdtar->archive_dir->tail = p;
|
2004-04-05 21:32:18 +00:00
|
|
|
} else {
|
2004-04-15 22:37:54 +00:00
|
|
|
bsdtar->archive_dir->tail->next = p;
|
|
|
|
bsdtar->archive_dir->tail = p;
|
2004-04-05 21:32:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
test_for_append(struct bsdtar *bsdtar)
|
|
|
|
{
|
|
|
|
struct stat s;
|
|
|
|
|
2008-05-02 05:14:58 +00:00
|
|
|
if (*bsdtar->argv == NULL && bsdtar->names_from_file == NULL)
|
2004-05-17 05:44:53 +00:00
|
|
|
bsdtar_errc(bsdtar, 1, 0, "no files or directories specified");
|
2004-04-05 21:32:18 +00:00
|
|
|
if (bsdtar->filename == NULL)
|
2004-05-17 05:44:53 +00:00
|
|
|
bsdtar_errc(bsdtar, 1, 0, "Cannot append to stdout.");
|
2004-04-05 21:32:18 +00:00
|
|
|
|
|
|
|
if (bsdtar->create_compression != 0)
|
2004-05-17 05:44:53 +00:00
|
|
|
bsdtar_errc(bsdtar, 1, 0,
|
|
|
|
"Cannot append to %s with compression", bsdtar->filename);
|
2004-04-05 21:32:18 +00:00
|
|
|
|
|
|
|
if (stat(bsdtar->filename, &s) != 0)
|
2007-02-08 07:25:53 +00:00
|
|
|
return;
|
2004-04-05 21:32:18 +00:00
|
|
|
|
2008-02-19 05:27:17 +00:00
|
|
|
if (!S_ISREG(s.st_mode) && !S_ISBLK(s.st_mode))
|
2004-05-17 05:44:53 +00:00
|
|
|
bsdtar_errc(bsdtar, 1, 0,
|
|
|
|
"Cannot append to %s: not a regular file.",
|
2004-04-05 21:32:18 +00:00
|
|
|
bsdtar->filename);
|
|
|
|
}
|