2004-04-05 21:32:18 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2003-2004 Tim Kientzle
|
|
|
|
* 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
|
|
|
|
* notice, this list of conditions and the following disclaimer
|
|
|
|
* in this position and unchanged.
|
|
|
|
* 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$");
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#ifdef HAVE_POSIX_ACL
|
|
|
|
#include <sys/acl.h>
|
|
|
|
#endif
|
|
|
|
#include <archive.h>
|
|
|
|
#include <archive_entry.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <fnmatch.h>
|
|
|
|
#include <fts.h>
|
|
|
|
#include <grp.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2004-05-02 18:10:35 +00:00
|
|
|
#ifdef LINUX
|
|
|
|
#include <ext2fs/ext2_fs.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#endif
|
2004-04-05 21:32:18 +00:00
|
|
|
|
|
|
|
#include "bsdtar.h"
|
|
|
|
|
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
|
|
|
|
|
|
|
/* Initial size of link cache. */
|
2004-05-02 22:58:18 +00:00
|
|
|
#define links_cache_initial_size 1024
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct links_cache {
|
|
|
|
unsigned long number_entries;
|
|
|
|
size_t number_buckets;
|
|
|
|
struct links_entry **buckets;
|
|
|
|
char stop_allocating;
|
|
|
|
};
|
|
|
|
|
2004-04-05 21:32:18 +00:00
|
|
|
struct links_entry {
|
|
|
|
struct links_entry *next;
|
|
|
|
struct links_entry *previous;
|
|
|
|
int links;
|
|
|
|
dev_t dev;
|
|
|
|
ino_t ino;
|
|
|
|
char *name;
|
|
|
|
};
|
|
|
|
|
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 *,
|
|
|
|
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);
|
2004-04-05 21:32:18 +00:00
|
|
|
static void create_cleanup(struct bsdtar *);
|
2004-05-17 05:02:39 +00:00
|
|
|
static void free_cache(struct name_cache *cache);
|
2004-04-05 21:32:18 +00:00
|
|
|
static const char * lookup_gname(struct bsdtar *bsdtar, gid_t gid);
|
2004-05-17 05:02:39 +00:00
|
|
|
static int lookup_gname_helper(struct bsdtar *bsdtar,
|
|
|
|
const char **name, id_t gid);
|
2004-04-15 22:37:54 +00:00
|
|
|
static void lookup_hardlink(struct bsdtar *,
|
|
|
|
struct archive_entry *entry, const struct stat *);
|
2004-04-05 21:32:18 +00:00
|
|
|
static const char * lookup_uname(struct bsdtar *bsdtar, uid_t uid);
|
2004-05-17 05:02:39 +00:00
|
|
|
static int lookup_uname_helper(struct bsdtar *bsdtar,
|
|
|
|
const char **name, id_t uid);
|
2004-04-05 21:32:18 +00:00
|
|
|
static int new_enough(struct bsdtar *, const char *path,
|
|
|
|
time_t mtime_sec, int mtime_nsec);
|
2004-04-15 22:37:54 +00:00
|
|
|
static void setup_acls(struct bsdtar *, struct archive_entry *,
|
2004-04-05 21:32:18 +00:00
|
|
|
const char *path);
|
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 *);
|
|
|
|
static void write_entry(struct bsdtar *, struct archive *,
|
|
|
|
struct stat *, const char *pathname,
|
|
|
|
unsigned pathlen, const char *accpath);
|
2004-04-26 23:39:17 +00:00
|
|
|
static int write_file_data(struct bsdtar *, struct archive *,
|
|
|
|
int fd);
|
2004-04-05 21:32:18 +00:00
|
|
|
static void write_heirarchy(struct bsdtar *, struct archive *,
|
|
|
|
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. */
|
|
|
|
if (bsdtar->create_format == NULL)
|
|
|
|
archive_write_set_format_pax_restricted(a);
|
|
|
|
else {
|
|
|
|
r = archive_write_set_format_by_name(a, bsdtar->create_format);
|
|
|
|
if (r != ARCHIVE_OK) {
|
|
|
|
fprintf(stderr, "Can't use format %s: %s\n",
|
|
|
|
bsdtar->create_format,
|
|
|
|
archive_error_string(a));
|
2004-05-17 05:44:53 +00:00
|
|
|
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
|
|
|
|
|
|
|
switch (bsdtar->create_compression) {
|
2004-06-07 03:38:17 +00:00
|
|
|
case 0:
|
|
|
|
break;
|
2004-04-05 21:32:18 +00:00
|
|
|
case 'j': case 'y':
|
|
|
|
archive_write_set_compression_bzip2(a);
|
|
|
|
break;
|
|
|
|
case 'z':
|
|
|
|
archive_write_set_compression_gzip(a);
|
|
|
|
break;
|
2004-06-07 03:19:57 +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
|
|
|
|
|
|
|
write_archive(a, bsdtar);
|
|
|
|
|
|
|
|
archive_write_finish(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Same as 'c', except we only support tar formats in uncompressed
|
|
|
|
* files on disk.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
tar_mode_r(struct bsdtar *bsdtar)
|
|
|
|
{
|
|
|
|
off_t end_offset;
|
|
|
|
int format;
|
|
|
|
struct archive *a;
|
|
|
|
struct archive_entry *entry;
|
|
|
|
|
|
|
|
/* Sanity-test some arguments and the file. */
|
|
|
|
test_for_append(bsdtar);
|
|
|
|
|
|
|
|
format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
|
|
|
|
|
|
|
|
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);
|
|
|
|
archive_read_open_fd(a, bsdtar->fd, 10240);
|
|
|
|
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);
|
|
|
|
/*
|
|
|
|
* Set format to same one auto-detected above, except use
|
|
|
|
* ustar for appending to GNU tar, since the library doesn't
|
|
|
|
* write GNU tar format.
|
|
|
|
*/
|
|
|
|
if (format == ARCHIVE_FORMAT_TAR_GNUTAR)
|
|
|
|
format = ARCHIVE_FORMAT_TAR_USTAR;
|
|
|
|
archive_write_set_format(a, format);
|
|
|
|
lseek(bsdtar->fd, end_offset, SEEK_SET);
|
|
|
|
archive_write_open_fd(a, bsdtar->fd);
|
|
|
|
|
|
|
|
write_archive(a, bsdtar);
|
|
|
|
|
|
|
|
archive_write_finish(a);
|
|
|
|
close(bsdtar->fd);
|
|
|
|
bsdtar->fd = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tar_mode_u(struct bsdtar *bsdtar)
|
|
|
|
{
|
|
|
|
off_t end_offset;
|
|
|
|
struct archive *a;
|
|
|
|
struct archive_entry *entry;
|
|
|
|
const char *filename;
|
|
|
|
int format;
|
|
|
|
struct archive_dir_entry *p;
|
2004-04-15 22:37:54 +00:00
|
|
|
struct archive_dir archive_dir;
|
|
|
|
|
|
|
|
bsdtar->archive_dir = &archive_dir;
|
|
|
|
memset(&archive_dir, 0, sizeof(archive_dir));
|
2004-04-05 21:32:18 +00:00
|
|
|
|
|
|
|
filename = NULL;
|
|
|
|
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);
|
2004-06-07 03:19:57 +00:00
|
|
|
archive_read_open_fd(a, bsdtar->fd,
|
|
|
|
bsdtar->bytes_per_block != 0 ? bsdtar->bytes_per_block :
|
|
|
|
DEFAULT_BYTES_PER_BLOCK);
|
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);
|
|
|
|
|
|
|
|
write_archive(a, bsdtar);
|
|
|
|
|
|
|
|
archive_write_finish(a);
|
|
|
|
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;
|
|
|
|
char *pending_dir;
|
|
|
|
|
|
|
|
pending_dir = NULL;
|
|
|
|
|
2004-06-27 01:08:54 +00:00
|
|
|
if (bsdtar->start_dir != NULL && chdir(bsdtar->start_dir)) {
|
|
|
|
bsdtar_warnc(bsdtar, errno,
|
2004-05-17 05:44:53 +00:00
|
|
|
"chdir(%s) failed", bsdtar->start_dir);
|
2004-06-27 01:08:54 +00:00
|
|
|
bsdtar->return_value = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
return;
|
|
|
|
}
|
2004-06-26 22:49:51 +00:00
|
|
|
}
|
2004-04-05 21:32:18 +00:00
|
|
|
|
|
|
|
/*-
|
2004-06-26 22:49:51 +00:00
|
|
|
* The logic here for -C <dir> attempts to avoid
|
2004-04-05 21:32:18 +00:00
|
|
|
* chdir() as long as possible. For example:
|
2004-06-26 22:49:51 +00:00
|
|
|
* "-C /foo -C /bar file"
|
2004-04-05 21:32:18 +00:00
|
|
|
* needs chdir("/bar") but not chdir("/foo")
|
2004-06-26 22:49:51 +00:00
|
|
|
* "-C /foo -C bar file"
|
2004-04-05 21:32:18 +00:00
|
|
|
* needs chdir("/foo/bar")
|
2004-06-26 22:49:51 +00:00
|
|
|
* "-C /foo -C bar /file1"
|
2004-04-05 21:32:18 +00:00
|
|
|
* does not need chdir()
|
2004-06-26 22:49:51 +00:00
|
|
|
* "-C /foo -C bar /file1 file2"
|
2004-04-05 21:32:18 +00:00
|
|
|
* needs chdir("/foo/bar") before file2
|
|
|
|
*
|
|
|
|
* The only correct way to handle this is to
|
|
|
|
* record a "pending" chdir request and only
|
|
|
|
* execute the real chdir when a non-absolute
|
|
|
|
* filename is seen on the command line.
|
|
|
|
*
|
|
|
|
* I went to all this work so that programs
|
|
|
|
* that build tar command lines don't have to
|
2004-06-26 22:49:51 +00:00
|
|
|
* worry about -C with non-existent
|
2004-04-05 21:32:18 +00:00
|
|
|
* directories; such requests will only fail
|
|
|
|
* if the directory must be accessed.
|
|
|
|
*/
|
|
|
|
if (pending_dir && *arg == '/') {
|
2004-06-26 22:49:51 +00:00
|
|
|
/* The -C /foo -C /bar case; dump first one. */
|
2004-04-05 21:32:18 +00:00
|
|
|
free(pending_dir);
|
|
|
|
pending_dir = NULL;
|
|
|
|
}
|
|
|
|
if (pending_dir) {
|
2004-06-26 22:49:51 +00:00
|
|
|
/* The -C /foo -C bar case; concatenate */
|
2004-04-05 21:32:18 +00:00
|
|
|
char *old_pending = pending_dir;
|
|
|
|
int old_len = strlen(old_pending);
|
|
|
|
|
|
|
|
pending_dir =
|
|
|
|
malloc(old_len + 1 + strlen(arg));
|
|
|
|
strcpy(pending_dir, old_pending);
|
|
|
|
if (pending_dir[old_len - 1] != '/') {
|
|
|
|
pending_dir[old_len] = '/';
|
|
|
|
old_len ++;
|
|
|
|
}
|
|
|
|
strcpy(pending_dir + old_len, arg);
|
|
|
|
} else {
|
|
|
|
/* Easy case: no previously-saved dir. */
|
|
|
|
pending_dir = strdup(arg);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (pending_dir &&
|
|
|
|
(*arg != '/' || (*arg == '@' && arg[1] != '/'))) {
|
2004-06-26 22:49:51 +00:00
|
|
|
/* Handle a deferred -C */
|
2004-06-27 01:08:54 +00:00
|
|
|
if (chdir(pending_dir)) {
|
|
|
|
bsdtar_warnc(bsdtar, 0,
|
2004-04-05 21:32:18 +00:00
|
|
|
"could not chdir to '%s'\n",
|
|
|
|
pending_dir);
|
2004-06-27 01:08:54 +00:00
|
|
|
bsdtar->return_value = 1;
|
|
|
|
return;
|
|
|
|
}
|
2004-04-05 21:32:18 +00:00
|
|
|
free(pending_dir);
|
|
|
|
pending_dir = NULL;
|
|
|
|
}
|
|
|
|
|
2004-06-27 03:28:13 +00:00
|
|
|
if (*arg == '@') {
|
|
|
|
if (append_archive(bsdtar, a, arg+1))
|
|
|
|
break;
|
|
|
|
} else
|
2004-04-05 21:32:18 +00:00
|
|
|
write_heirarchy(bsdtar, a, arg);
|
|
|
|
}
|
|
|
|
bsdtar->argv++;
|
|
|
|
}
|
|
|
|
|
|
|
|
create_cleanup(bsdtar);
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
if (chdir(line) != 0)
|
|
|
|
bsdtar_errc(bsdtar, 1, errno,
|
|
|
|
"chdir(%s) failed", line);
|
|
|
|
bsdtar->next_line_is_dir = 0;
|
|
|
|
} else if (!bsdtar->option_null && strcmp(line, "-C") == 0)
|
|
|
|
bsdtar->next_line_is_dir = 1;
|
|
|
|
else
|
|
|
|
write_heirarchy(bsdtar, bsdtar->archive, line);
|
|
|
|
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
|
|
|
/*
|
|
|
|
* Copy from specified archive to current archive.
|
|
|
|
* Returns non-zero on fatal error (i.e., output errors). Errors
|
|
|
|
* reading the input archive set bsdtar->return_value, but this
|
|
|
|
* function will still return zero.
|
|
|
|
*/
|
2004-04-05 21:32:18 +00:00
|
|
|
static int
|
|
|
|
append_archive(struct bsdtar *bsdtar, struct archive *a, const char *filename)
|
|
|
|
{
|
|
|
|
struct archive *ina;
|
|
|
|
struct archive_entry *in_entry;
|
|
|
|
int bytes_read, bytes_written;
|
|
|
|
char buff[8192];
|
|
|
|
|
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);
|
|
|
|
}
|
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),
|
|
|
|
archive_entry_mtime(in_entry),
|
|
|
|
archive_entry_mtime_nsec(in_entry)))
|
|
|
|
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));
|
|
|
|
/* XXX handle/report errors XXX */
|
2004-06-27 03:28:13 +00:00
|
|
|
if (archive_write_header(a, in_entry)) {
|
|
|
|
bsdtar_warnc(bsdtar, 0, "%s",
|
|
|
|
archive_error_string(ina));
|
|
|
|
bsdtar->return_value = 1;
|
|
|
|
return (-1);
|
|
|
|
}
|
2004-04-05 21:32:18 +00:00
|
|
|
bytes_read = archive_read_data(ina, buff, sizeof(buff));
|
|
|
|
while (bytes_read > 0) {
|
|
|
|
bytes_written =
|
|
|
|
archive_write_data(a, buff, bytes_read);
|
|
|
|
if (bytes_written < bytes_read) {
|
2004-05-17 05:44:53 +00:00
|
|
|
bsdtar_warnc(bsdtar, archive_errno(a), "%s",
|
2004-04-05 21:32:18 +00:00
|
|
|
archive_error_string(a));
|
2004-06-27 03:28:13 +00:00
|
|
|
return (-1);
|
2004-04-05 21:32:18 +00:00
|
|
|
}
|
|
|
|
bytes_read =
|
|
|
|
archive_read_data(ina, buff, sizeof(buff));
|
|
|
|
}
|
|
|
|
if (bsdtar->verbose)
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
|
|
|
|
}
|
2004-06-27 03:28:13 +00:00
|
|
|
if (archive_errno(ina)) {
|
2004-05-17 05:44:53 +00:00
|
|
|
bsdtar_warnc(bsdtar, 0, "Error reading archive %s: %s",
|
|
|
|
filename, archive_error_string(ina));
|
2004-06-27 03:28:13 +00:00
|
|
|
bsdtar->return_value = 1;
|
|
|
|
}
|
2004-04-05 21:32:18 +00:00
|
|
|
|
|
|
|
return (0); /* TODO: Return non-zero on error */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add the file or dir heirarchy named by 'path' to the archive
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
write_heirarchy(struct bsdtar *bsdtar, struct archive *a, const char *path)
|
|
|
|
{
|
|
|
|
FTS *fts;
|
|
|
|
FTSENT *ftsent;
|
|
|
|
int ftsoptions;
|
|
|
|
char *fts_argv[2];
|
2004-05-02 18:10:35 +00:00
|
|
|
#ifdef LINUX
|
|
|
|
int fd, r;
|
|
|
|
unsigned long fflags;
|
|
|
|
#endif
|
2004-04-05 21:32:18 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Sigh: fts_open modifies it's first parameter, so we have to
|
|
|
|
* copy 'path' to mutable storage.
|
|
|
|
*/
|
|
|
|
fts_argv[0] = strdup(path);
|
|
|
|
fts_argv[1] = NULL;
|
|
|
|
ftsoptions = FTS_PHYSICAL;
|
|
|
|
switch (bsdtar->symlink_mode) {
|
|
|
|
case 'H':
|
|
|
|
ftsoptions |= FTS_COMFOLLOW;
|
|
|
|
break;
|
|
|
|
case 'L':
|
|
|
|
ftsoptions = FTS_COMFOLLOW | FTS_LOGICAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (bsdtar->option_dont_traverse_mounts)
|
|
|
|
ftsoptions |= FTS_XDEV;
|
|
|
|
|
|
|
|
fts = fts_open(fts_argv, ftsoptions, NULL);
|
|
|
|
|
|
|
|
|
|
|
|
if (!fts) {
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((ftsent = fts_read(fts))) {
|
|
|
|
switch (ftsent->fts_info) {
|
|
|
|
case FTS_NS:
|
2004-05-17 05:44:53 +00:00
|
|
|
bsdtar_warnc(bsdtar, ftsent->fts_errno,
|
|
|
|
"%s: Could not stat", ftsent->fts_path);
|
2004-06-07 07:19:04 +00:00
|
|
|
bsdtar->return_value = 1;
|
2004-04-05 21:32:18 +00:00
|
|
|
break;
|
|
|
|
case FTS_ERR:
|
2004-05-17 05:44:53 +00:00
|
|
|
bsdtar_warnc(bsdtar, ftsent->fts_errno, "%s",
|
|
|
|
ftsent->fts_path);
|
2004-06-07 07:19:04 +00:00
|
|
|
bsdtar->return_value = 1;
|
2004-04-05 21:32:18 +00:00
|
|
|
break;
|
|
|
|
case FTS_DNR:
|
2004-05-17 05:44:53 +00:00
|
|
|
bsdtar_warnc(bsdtar, ftsent->fts_errno,
|
2004-04-05 21:32:18 +00:00
|
|
|
"%s: Cannot read directory contents",
|
|
|
|
ftsent->fts_path);
|
2004-06-07 07:19:04 +00:00
|
|
|
bsdtar->return_value = 1;
|
2004-04-05 21:32:18 +00:00
|
|
|
break;
|
|
|
|
case FTS_W: /* Skip Whiteout entries */
|
|
|
|
break;
|
|
|
|
case FTS_DC: /* Directory that causes cycle */
|
|
|
|
/* XXX Does this need special handling ? */
|
|
|
|
break;
|
|
|
|
case FTS_D:
|
|
|
|
/*
|
|
|
|
* If this dir is flagged "nodump" and we're
|
|
|
|
* honoring such flags, tell FTS to skip the
|
|
|
|
* entire tree and don't write the entry for the
|
|
|
|
* directory itself.
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_CHFLAGS
|
|
|
|
if (bsdtar->option_honor_nodump &&
|
|
|
|
(ftsent->fts_statp->st_flags & UF_NODUMP)) {
|
|
|
|
fts_set(fts, ftsent, FTS_SKIP);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-05-02 18:10:35 +00:00
|
|
|
#ifdef LINUX
|
|
|
|
/*
|
|
|
|
* Linux has a nodump flag too but to read it
|
2004-05-03 16:56:42 +00:00
|
|
|
* we have to open() the dir and do an ioctl on it...
|
2004-05-02 18:10:35 +00:00
|
|
|
*/
|
|
|
|
if (bsdtar->option_honor_nodump &&
|
|
|
|
((fd = open(ftsent->fts_name, O_RDONLY|O_NONBLOCK)) >= 0) &&
|
|
|
|
((r = ioctl(fd, EXT2_IOC_GETFLAGS, &fflags)),
|
|
|
|
close(fd), r) >= 0 &&
|
2004-05-03 16:56:42 +00:00
|
|
|
(fflags & EXT2_NODUMP_FL)) {
|
|
|
|
fts_set(fts, ftsent, FTS_SKIP);
|
2004-05-02 18:10:35 +00:00
|
|
|
break;
|
2004-05-03 16:56:42 +00:00
|
|
|
}
|
2004-05-02 18:10:35 +00:00
|
|
|
#endif
|
|
|
|
|
2004-04-05 21:32:18 +00:00
|
|
|
/*
|
|
|
|
* In -u mode, we need to check whether this
|
|
|
|
* is newer than what's already in the archive.
|
|
|
|
*/
|
|
|
|
if (!new_enough(bsdtar, ftsent->fts_path,
|
|
|
|
ftsent->fts_statp->st_mtime,
|
2004-05-02 18:10:35 +00:00
|
|
|
ARCHIVE_STAT_MTIME_NANOS(ftsent->fts_statp)))
|
2004-04-05 21:32:18 +00:00
|
|
|
break;
|
|
|
|
/*
|
|
|
|
* If this dir is excluded by a filename
|
|
|
|
* pattern, tell FTS to skip the entire tree
|
|
|
|
* and don't write the entry for the directory
|
|
|
|
* itself.
|
|
|
|
*/
|
|
|
|
if (excluded(bsdtar, ftsent->fts_path)) {
|
|
|
|
fts_set(fts, ftsent, FTS_SKIP);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the user vetoes the directory, skip
|
|
|
|
* the whole thing.
|
|
|
|
*/
|
|
|
|
if (bsdtar->option_interactive &&
|
|
|
|
!yes("add '%s'", ftsent->fts_path)) {
|
|
|
|
fts_set(fts, ftsent, FTS_SKIP);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we're not recursing, tell FTS to skip the
|
|
|
|
* tree but do fall through and write the entry
|
|
|
|
* for the dir itself.
|
|
|
|
*/
|
|
|
|
if (bsdtar->option_no_subdirs)
|
|
|
|
fts_set(fts, ftsent, FTS_SKIP);
|
|
|
|
write_entry(bsdtar, a, ftsent->fts_statp,
|
|
|
|
ftsent->fts_path, ftsent->fts_pathlen,
|
|
|
|
ftsent->fts_accpath);
|
|
|
|
break;
|
|
|
|
case FTS_F:
|
|
|
|
case FTS_SL:
|
|
|
|
case FTS_SLNONE:
|
|
|
|
case FTS_DEFAULT:
|
|
|
|
/*
|
|
|
|
* Skip this file if it's flagged "nodump" and we're
|
|
|
|
* honoring that flag.
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_CHFLAGS
|
|
|
|
if (bsdtar->option_honor_nodump &&
|
|
|
|
(ftsent->fts_statp->st_flags & UF_NODUMP))
|
|
|
|
break;
|
|
|
|
#endif
|
2004-05-02 18:10:35 +00:00
|
|
|
|
|
|
|
#ifdef LINUX
|
|
|
|
/*
|
|
|
|
* Linux has a nodump flag too but to read it
|
|
|
|
* we have to open() the file and do an ioctl on it...
|
|
|
|
*/
|
|
|
|
if (bsdtar->option_honor_nodump &&
|
|
|
|
S_ISREG(ftsent->fts_statp->st_mode) &&
|
|
|
|
((fd = open(ftsent->fts_name, O_RDONLY|O_NONBLOCK)) >= 0) &&
|
|
|
|
((r = ioctl(fd, EXT2_IOC_GETFLAGS, &fflags)),
|
|
|
|
close(fd), r) >= 0 &&
|
|
|
|
(fflags & EXT2_NODUMP_FL))
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2004-04-05 21:32:18 +00:00
|
|
|
/*
|
|
|
|
* Skip this file if it's excluded by a
|
|
|
|
* filename pattern.
|
|
|
|
*/
|
|
|
|
if (excluded(bsdtar, ftsent->fts_path))
|
|
|
|
break;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* In -u mode, we need to check whether this
|
|
|
|
* is newer than what's already in the archive.
|
|
|
|
*/
|
|
|
|
if (!new_enough(bsdtar, ftsent->fts_path,
|
|
|
|
ftsent->fts_statp->st_mtime,
|
2004-05-02 18:10:35 +00:00
|
|
|
ARCHIVE_STAT_MTIME_NANOS(ftsent->fts_statp)))
|
2004-04-05 21:32:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
if (bsdtar->option_interactive &&
|
|
|
|
!yes("add '%s'", ftsent->fts_path)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
write_entry(bsdtar, a, ftsent->fts_statp,
|
|
|
|
ftsent->fts_path, ftsent->fts_pathlen,
|
|
|
|
ftsent->fts_accpath);
|
|
|
|
break;
|
|
|
|
case FTS_DP:
|
|
|
|
break;
|
|
|
|
default:
|
2004-05-17 05:44:53 +00:00
|
|
|
bsdtar_warnc(bsdtar, 0,
|
|
|
|
"%s: Heirarchy traversal error %d\n",
|
2004-04-05 21:32:18 +00:00
|
|
|
ftsent->fts_path,
|
|
|
|
ftsent->fts_info);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
if (errno)
|
2004-05-17 05:44:53 +00:00
|
|
|
bsdtar_warnc(bsdtar, errno, "%s", path);
|
2004-04-05 21:32:18 +00:00
|
|
|
if (fts_close(fts))
|
2004-05-17 05:44:53 +00:00
|
|
|
bsdtar_warnc(bsdtar, errno, "fts_close failed");
|
2004-04-05 21:32:18 +00:00
|
|
|
free(fts_argv[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add a single filesystem object to the archive.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
write_entry(struct bsdtar *bsdtar, struct archive *a, struct stat *st,
|
|
|
|
const char *pathname, unsigned pathlen, const char *accpath)
|
|
|
|
{
|
|
|
|
struct archive_entry *entry;
|
|
|
|
int e;
|
|
|
|
int fd;
|
2004-05-02 18:10:35 +00:00
|
|
|
#ifdef LINUX
|
|
|
|
int r;
|
|
|
|
unsigned long stflags;
|
|
|
|
#endif
|
2004-04-05 21:32:18 +00:00
|
|
|
static char linkbuffer[PATH_MAX+1];
|
|
|
|
|
|
|
|
(void)pathlen; /* UNUSED */
|
|
|
|
|
|
|
|
fd = -1;
|
|
|
|
entry = archive_entry_new();
|
|
|
|
|
|
|
|
/* Non-regular files get archived with zero size. */
|
|
|
|
if (!S_ISREG(st->st_mode))
|
|
|
|
st->st_size = 0;
|
|
|
|
|
|
|
|
/* Strip redundant "./" from start of filename. */
|
|
|
|
if (pathname && pathname[0] == '.' && pathname[1] == '/') {
|
|
|
|
pathname += 2;
|
|
|
|
if (*pathname == 0) /* This is the "./" directory. */
|
|
|
|
goto cleanup; /* Don't archive it ever. */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Strip leading '/' unless user has asked us not to. */
|
2004-06-27 18:32:14 +00:00
|
|
|
if (pathname && pathname[0] == '/' && !bsdtar->option_absolute_paths) {
|
|
|
|
/* Generate a warning the first time this happens. */
|
|
|
|
if (!bsdtar->warned_lead_slash) {
|
|
|
|
bsdtar_warnc(bsdtar, 0,
|
|
|
|
"Removing leading '/' from member names");
|
|
|
|
bsdtar->warned_lead_slash = 1;
|
|
|
|
}
|
2004-04-05 21:32:18 +00:00
|
|
|
pathname++;
|
2004-06-27 18:32:14 +00:00
|
|
|
}
|
2004-04-05 21:32:18 +00:00
|
|
|
|
2004-04-21 05:16:28 +00:00
|
|
|
archive_entry_set_pathname(entry, pathname);
|
|
|
|
|
|
|
|
if (!S_ISDIR(st->st_mode) && (st->st_nlink > 1))
|
|
|
|
lookup_hardlink(bsdtar, entry, st);
|
|
|
|
|
2004-04-05 21:32:18 +00:00
|
|
|
/* Display entry as we process it. This format is required by SUSv2. */
|
|
|
|
if (bsdtar->verbose)
|
|
|
|
safe_fprintf(stderr, "a %s", pathname);
|
|
|
|
|
|
|
|
/* Read symbolic link information. */
|
|
|
|
if ((st->st_mode & S_IFMT) == S_IFLNK) {
|
|
|
|
int lnklen;
|
|
|
|
|
|
|
|
lnklen = readlink(accpath, linkbuffer, PATH_MAX);
|
|
|
|
if (lnklen < 0) {
|
|
|
|
if (!bsdtar->verbose)
|
2004-05-17 05:44:53 +00:00
|
|
|
bsdtar_warnc(bsdtar, errno,
|
2004-04-05 21:32:18 +00:00
|
|
|
"%s: Couldn't read symbolic link",
|
|
|
|
pathname);
|
|
|
|
else
|
|
|
|
safe_fprintf(stderr,
|
|
|
|
": Couldn't read symbolic link: %s",
|
|
|
|
strerror(errno));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
linkbuffer[lnklen] = 0;
|
|
|
|
archive_entry_set_symlink(entry, linkbuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Look up username and group name. */
|
|
|
|
archive_entry_set_uname(entry, lookup_uname(bsdtar, st->st_uid));
|
|
|
|
archive_entry_set_gname(entry, lookup_gname(bsdtar, st->st_gid));
|
|
|
|
|
|
|
|
#ifdef HAVE_CHFLAGS
|
2004-04-26 23:39:17 +00:00
|
|
|
if (st->st_flags != 0)
|
|
|
|
archive_entry_set_fflags(entry, st->st_flags, 0);
|
2004-04-05 21:32:18 +00:00
|
|
|
#endif
|
|
|
|
|
2004-05-02 18:10:35 +00:00
|
|
|
#ifdef LINUX
|
|
|
|
if ((S_ISREG(st->st_mode) || S_ISDIR(st->st_mode)) &&
|
|
|
|
((fd = open(accpath, O_RDONLY|O_NONBLOCK)) >= 0) &&
|
|
|
|
((r = ioctl(fd, EXT2_IOC_GETFLAGS, &stflags)), close(fd), r) >= 0 &&
|
|
|
|
stflags) {
|
|
|
|
archive_entry_set_fflags(entry, stflags, 0);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-04-21 05:16:28 +00:00
|
|
|
archive_entry_copy_stat(entry, st);
|
2004-04-05 21:32:18 +00:00
|
|
|
setup_acls(bsdtar, entry, accpath);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If it's a regular file (and non-zero in size) make sure we
|
|
|
|
* can open it before we start to write. In particular, note
|
|
|
|
* that we can always archive a zero-length file, even if we
|
|
|
|
* can't read it.
|
|
|
|
*/
|
|
|
|
if (S_ISREG(st->st_mode) && st->st_size > 0) {
|
|
|
|
fd = open(accpath, O_RDONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
if (!bsdtar->verbose)
|
2004-05-17 05:44:53 +00:00
|
|
|
bsdtar_warnc(bsdtar, errno, "%s", pathname);
|
2004-04-05 21:32:18 +00:00
|
|
|
else
|
|
|
|
fprintf(stderr, ": %s", strerror(errno));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
e = archive_write_header(a, entry);
|
|
|
|
if (e != ARCHIVE_OK) {
|
|
|
|
if (!bsdtar->verbose)
|
2004-05-17 05:44:53 +00:00
|
|
|
bsdtar_warnc(bsdtar, 0, "%s: %s", pathname,
|
2004-04-05 21:32:18 +00:00
|
|
|
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 (fd >= 0 && archive_entry_size(entry) > 0)
|
2004-04-26 23:39:17 +00:00
|
|
|
write_file_data(bsdtar, a, fd);
|
2004-04-05 21:32:18 +00:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
if (fd >= 0)
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
if (entry != NULL)
|
|
|
|
archive_entry_free(entry);
|
|
|
|
|
|
|
|
if (bsdtar->verbose)
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Helper function to copy file to archive, with stack-allocated buffer. */
|
|
|
|
static int
|
2004-04-26 23:39:17 +00:00
|
|
|
write_file_data(struct bsdtar *bsdtar, struct archive *a, int fd)
|
2004-04-05 21:32:18 +00:00
|
|
|
{
|
2004-04-26 23:39:17 +00:00
|
|
|
char buff[64*1024];
|
2004-04-05 21:32:18 +00:00
|
|
|
ssize_t bytes_read;
|
|
|
|
ssize_t bytes_written;
|
|
|
|
|
2004-04-26 23:39:17 +00:00
|
|
|
/* XXX TODO: Allocate buffer on heap and store pointer to
|
|
|
|
* it in bsdtar structure; arrange cleanup as well. XXX */
|
|
|
|
(void)bsdtar;
|
|
|
|
|
2004-04-05 21:32:18 +00:00
|
|
|
bytes_read = read(fd, buff, sizeof(buff));
|
|
|
|
while (bytes_read > 0) {
|
|
|
|
bytes_written = archive_write_data(a, buff, bytes_read);
|
|
|
|
|
|
|
|
if (bytes_written == 0 && errno) {
|
|
|
|
return -1; /* Write failed; this is bad */
|
|
|
|
}
|
|
|
|
bytes_read = read(fd, buff, sizeof(buff));
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
create_cleanup(struct bsdtar * bsdtar)
|
|
|
|
{
|
2004-04-15 22:37:54 +00:00
|
|
|
struct links_cache *links_cache;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
|
|
|
|
/* Free inode->pathname map used for hardlink detection. */
|
|
|
|
if (bsdtar->links_cache != NULL) {
|
|
|
|
links_cache = bsdtar->links_cache;
|
|
|
|
if (links_cache->buckets != NULL) {
|
|
|
|
for (i = 0; i < links_cache->number_buckets; i++) {
|
|
|
|
while (links_cache->buckets[i] != NULL) {
|
|
|
|
struct links_entry *lp =
|
|
|
|
links_cache->buckets[i]->next;
|
|
|
|
if (bsdtar->option_warn_links)
|
2004-05-17 05:44:53 +00:00
|
|
|
bsdtar_warnc(bsdtar, 0,
|
2004-04-15 22:37:54 +00:00
|
|
|
"Missing links to %s",
|
|
|
|
links_cache->buckets[i]->name);
|
|
|
|
if (links_cache->buckets[i]->name != NULL)
|
|
|
|
free(links_cache->buckets[i]->name);
|
|
|
|
free(links_cache->buckets[i]);
|
|
|
|
links_cache->buckets[i] = lp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(links_cache->buckets);
|
|
|
|
}
|
|
|
|
free(bsdtar->links_cache);
|
|
|
|
bsdtar->links_cache = NULL;
|
|
|
|
}
|
|
|
|
|
2004-05-17 05:02:39 +00:00
|
|
|
free_cache(bsdtar->uname_cache);
|
|
|
|
bsdtar->uname_cache = NULL;
|
|
|
|
free_cache(bsdtar->gname_cache);
|
|
|
|
bsdtar->gname_cache = NULL;
|
2004-04-05 21:32:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2004-04-15 22:37:54 +00:00
|
|
|
lookup_hardlink(struct bsdtar *bsdtar, struct archive_entry *entry,
|
2004-04-05 21:32:18 +00:00
|
|
|
const struct stat *st)
|
|
|
|
{
|
2004-04-15 22:37:54 +00:00
|
|
|
struct links_cache *links_cache;
|
|
|
|
struct links_entry *le, **new_buckets;
|
|
|
|
int hash;
|
|
|
|
size_t i, new_size;
|
|
|
|
|
|
|
|
/* If necessary, initialize the links cache. */
|
|
|
|
links_cache = bsdtar->links_cache;
|
|
|
|
if (links_cache == NULL) {
|
|
|
|
bsdtar->links_cache = malloc(sizeof(struct links_cache));
|
|
|
|
if (bsdtar->links_cache == NULL)
|
2004-05-17 05:44:53 +00:00
|
|
|
bsdtar_errc(bsdtar, 1, ENOMEM,
|
2004-04-15 22:37:54 +00:00
|
|
|
"No memory for hardlink detection.");
|
|
|
|
links_cache = bsdtar->links_cache;
|
|
|
|
memset(links_cache, 0, sizeof(struct links_cache));
|
|
|
|
links_cache->number_buckets = links_cache_initial_size;
|
|
|
|
links_cache->buckets = malloc(links_cache->number_buckets *
|
|
|
|
sizeof(links_cache->buckets[0]));
|
|
|
|
if (links_cache->buckets == NULL) {
|
2004-05-17 05:44:53 +00:00
|
|
|
bsdtar_errc(bsdtar, 1, ENOMEM,
|
2004-04-15 22:37:54 +00:00
|
|
|
"No memory for hardlink detection.");
|
|
|
|
}
|
|
|
|
for (i = 0; i < links_cache->number_buckets; i++)
|
|
|
|
links_cache->buckets[i] = NULL;
|
|
|
|
}
|
2004-04-05 21:32:18 +00:00
|
|
|
|
2004-04-15 22:37:54 +00:00
|
|
|
/* If the links cache is getting too full, enlarge the hash table. */
|
|
|
|
if (links_cache->number_entries > links_cache->number_buckets * 2 &&
|
|
|
|
!links_cache->stop_allocating)
|
|
|
|
{
|
|
|
|
int count;
|
|
|
|
|
|
|
|
new_size = links_cache->number_buckets * 2;
|
|
|
|
new_buckets = malloc(new_size * sizeof(struct links_entry *));
|
|
|
|
|
|
|
|
count = 0;
|
|
|
|
|
|
|
|
if (new_buckets != NULL) {
|
|
|
|
memset(new_buckets, 0,
|
|
|
|
new_size * sizeof(struct links_entry *));
|
|
|
|
for (i = 0; i < links_cache->number_buckets; i++) {
|
|
|
|
while (links_cache->buckets[i] != NULL) {
|
|
|
|
/* Remove entry from old bucket. */
|
|
|
|
le = links_cache->buckets[i];
|
|
|
|
links_cache->buckets[i] = le->next;
|
|
|
|
|
|
|
|
/* Add entry to new bucket. */
|
|
|
|
hash = (le->dev ^ le->ino) % new_size;
|
|
|
|
|
|
|
|
if (new_buckets[hash] != NULL)
|
|
|
|
new_buckets[hash]->previous =
|
|
|
|
le;
|
|
|
|
le->next = new_buckets[hash];
|
|
|
|
le->previous = NULL;
|
|
|
|
new_buckets[hash] = le;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(links_cache->buckets);
|
|
|
|
links_cache->buckets = new_buckets;
|
|
|
|
links_cache->number_buckets = new_size;
|
|
|
|
} else {
|
|
|
|
links_cache->stop_allocating = 1;
|
2004-05-17 05:44:53 +00:00
|
|
|
bsdtar_warnc(bsdtar, ENOMEM,
|
|
|
|
"No more memory for recording hard links");
|
|
|
|
bsdtar_warnc(bsdtar, 0,
|
|
|
|
"Remaining links will be dumped as full files");
|
2004-04-15 22:37:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Try to locate this entry in the links cache. */
|
|
|
|
hash = ( st->st_dev ^ st->st_ino ) % links_cache->number_buckets;
|
|
|
|
for (le = links_cache->buckets[hash]; le != NULL; le = le->next) {
|
2004-04-05 21:32:18 +00:00
|
|
|
if (le->dev == st->st_dev && le->ino == st->st_ino) {
|
2004-04-13 23:50:48 +00:00
|
|
|
archive_entry_copy_hardlink(entry, le->name);
|
2004-04-05 21:32:18 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Decrement link count each time and release
|
|
|
|
* the entry if it hits zero. This saves
|
|
|
|
* memory and is necessary for proper -l
|
|
|
|
* implementation.
|
|
|
|
*/
|
|
|
|
if (--le->links <= 0) {
|
|
|
|
if (le->previous != NULL)
|
|
|
|
le->previous->next = le->next;
|
|
|
|
if (le->next != NULL)
|
|
|
|
le->next->previous = le->previous;
|
2004-04-13 23:50:48 +00:00
|
|
|
if (le->name != NULL)
|
|
|
|
free(le->name);
|
2004-04-15 22:37:54 +00:00
|
|
|
if (links_cache->buckets[hash] == le)
|
|
|
|
links_cache->buckets[hash] = le->next;
|
|
|
|
links_cache->number_entries--;
|
2004-04-05 21:32:18 +00:00
|
|
|
free(le);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-04-15 22:37:54 +00:00
|
|
|
if (links_cache->stop_allocating)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Add this entry to the links cache. */
|
2004-04-05 21:32:18 +00:00
|
|
|
le = malloc(sizeof(struct links_entry));
|
2004-04-15 22:37:54 +00:00
|
|
|
if (le == NULL) {
|
|
|
|
links_cache->stop_allocating = 1;
|
2004-05-17 05:44:53 +00:00
|
|
|
bsdtar_warnc(bsdtar, ENOMEM,
|
|
|
|
"No more memory for recording hard links");
|
|
|
|
bsdtar_warnc(bsdtar, 0,
|
|
|
|
"Remaining hard links will be dumped as full files");
|
2004-04-15 22:37:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (links_cache->buckets[hash] != NULL)
|
|
|
|
links_cache->buckets[hash]->previous = le;
|
|
|
|
links_cache->number_entries++;
|
|
|
|
le->next = links_cache->buckets[hash];
|
2004-04-05 21:32:18 +00:00
|
|
|
le->previous = NULL;
|
2004-04-15 22:37:54 +00:00
|
|
|
links_cache->buckets[hash] = le;
|
2004-04-05 21:32:18 +00:00
|
|
|
le->dev = st->st_dev;
|
|
|
|
le->ino = st->st_ino;
|
|
|
|
le->links = st->st_nlink - 1;
|
|
|
|
le->name = strdup(archive_entry_pathname(entry));
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_POSIX_ACL
|
2004-04-06 23:08:31 +00:00
|
|
|
void setup_acl(struct bsdtar *bsdtar,
|
|
|
|
struct archive_entry *entry, const char *accpath,
|
|
|
|
int acl_type, int archive_entry_acl_type);
|
|
|
|
|
2004-04-05 21:32:18 +00:00
|
|
|
void
|
|
|
|
setup_acls(struct bsdtar *bsdtar, struct archive_entry *entry,
|
|
|
|
const char *accpath)
|
2004-04-06 23:08:31 +00:00
|
|
|
{
|
|
|
|
archive_entry_acl_clear(entry);
|
|
|
|
|
|
|
|
setup_acl(bsdtar, entry, accpath,
|
|
|
|
ACL_TYPE_ACCESS, ARCHIVE_ENTRY_ACL_TYPE_ACCESS);
|
2004-04-15 22:37:54 +00:00
|
|
|
/* Only directories can have default ACLs. */
|
|
|
|
if (S_ISDIR(archive_entry_mode(entry)))
|
|
|
|
setup_acl(bsdtar, entry, accpath,
|
|
|
|
ACL_TYPE_DEFAULT, ARCHIVE_ENTRY_ACL_TYPE_DEFAULT);
|
2004-04-06 23:08:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
setup_acl(struct bsdtar *bsdtar, struct archive_entry *entry,
|
|
|
|
const char *accpath, int acl_type, int archive_entry_acl_type)
|
2004-04-05 21:32:18 +00:00
|
|
|
{
|
|
|
|
acl_t acl;
|
|
|
|
acl_tag_t acl_tag;
|
|
|
|
acl_entry_t acl_entry;
|
|
|
|
acl_permset_t acl_permset;
|
|
|
|
int s, ae_id, ae_tag, ae_perm;
|
|
|
|
const char *ae_name;
|
|
|
|
|
|
|
|
/* Retrieve access ACL from file. */
|
2004-04-06 23:08:31 +00:00
|
|
|
acl = acl_get_file(accpath, acl_type);
|
2004-04-05 21:32:18 +00:00
|
|
|
if (acl != NULL) {
|
|
|
|
s = acl_get_entry(acl, ACL_FIRST_ENTRY, &acl_entry);
|
|
|
|
while (s == 1) {
|
|
|
|
ae_id = -1;
|
|
|
|
ae_name = NULL;
|
|
|
|
|
|
|
|
acl_get_tag_type(acl_entry, &acl_tag);
|
|
|
|
if (acl_tag == ACL_USER) {
|
|
|
|
ae_id = (int)*(uid_t *)acl_get_qualifier(acl_entry);
|
|
|
|
ae_name = lookup_uname(bsdtar, ae_id);
|
|
|
|
ae_tag = ARCHIVE_ENTRY_ACL_USER;
|
|
|
|
} else if (acl_tag == ACL_GROUP) {
|
|
|
|
ae_id = (int)*(gid_t *)acl_get_qualifier(acl_entry);
|
|
|
|
ae_name = lookup_gname(bsdtar, ae_id);
|
|
|
|
ae_tag = ARCHIVE_ENTRY_ACL_GROUP;
|
|
|
|
} else if (acl_tag == ACL_MASK) {
|
|
|
|
ae_tag = ARCHIVE_ENTRY_ACL_MASK;
|
|
|
|
} else if (acl_tag == ACL_USER_OBJ) {
|
|
|
|
ae_tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
|
|
|
|
} else if (acl_tag == ACL_GROUP_OBJ) {
|
|
|
|
ae_tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
|
|
|
|
} else if (acl_tag == ACL_OTHER) {
|
|
|
|
ae_tag = ARCHIVE_ENTRY_ACL_OTHER;
|
|
|
|
} else {
|
|
|
|
/* Skip types that libarchive can't support. */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
acl_get_permset(acl_entry, &acl_permset);
|
|
|
|
ae_perm = 0;
|
|
|
|
if (acl_get_perm_np(acl_permset, ACL_EXECUTE))
|
|
|
|
ae_perm |= ARCHIVE_ENTRY_ACL_EXECUTE;
|
|
|
|
if (acl_get_perm_np(acl_permset, ACL_READ))
|
|
|
|
ae_perm |= ARCHIVE_ENTRY_ACL_READ;
|
|
|
|
if (acl_get_perm_np(acl_permset, ACL_WRITE))
|
|
|
|
ae_perm |= ARCHIVE_ENTRY_ACL_WRITE;
|
|
|
|
|
|
|
|
archive_entry_acl_add_entry(entry,
|
2004-04-06 23:08:31 +00:00
|
|
|
archive_entry_acl_type, ae_perm, ae_tag,
|
2004-04-05 21:32:18 +00:00
|
|
|
ae_id, ae_name);
|
|
|
|
|
|
|
|
s = acl_get_entry(acl, ACL_NEXT_ENTRY, &acl_entry);
|
|
|
|
}
|
|
|
|
acl_free(acl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
void
|
2004-04-07 17:43:48 +00:00
|
|
|
setup_acls(struct bsdtar *bsdtar, struct archive_entry *entry,
|
|
|
|
const char *accpath)
|
2004-04-05 21:32:18 +00:00
|
|
|
{
|
2004-04-07 17:43:48 +00:00
|
|
|
(void)bsdtar;
|
2004-04-05 21:32:18 +00:00
|
|
|
(void)entry;
|
|
|
|
(void)accpath;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-05-17 05:02:39 +00:00
|
|
|
static void
|
|
|
|
free_cache(struct name_cache *cache)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (cache != NULL) {
|
|
|
|
for(i = 0; i < cache->size; i++) {
|
2004-06-07 03:16:18 +00:00
|
|
|
if (cache->cache[i].name != NULL &&
|
2004-05-17 05:02:39 +00:00
|
|
|
cache->cache[i].name != NO_NAME)
|
|
|
|
free((void *)(uintptr_t)cache->cache[i].name);
|
|
|
|
}
|
|
|
|
free(cache);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-04-05 21:32:18 +00:00
|
|
|
/*
|
2004-05-17 05:02:39 +00:00
|
|
|
* Lookup uid/gid from uname/gname, return NULL if no match.
|
2004-04-05 21:32:18 +00:00
|
|
|
*/
|
2004-05-17 05:02:39 +00:00
|
|
|
static const char *
|
|
|
|
lookup_name(struct bsdtar *bsdtar, struct name_cache **name_cache_variable,
|
|
|
|
int (*lookup_fn)(struct bsdtar *, const char **, id_t), id_t id)
|
2004-04-05 21:32:18 +00:00
|
|
|
{
|
2004-05-17 05:02:39 +00:00
|
|
|
struct name_cache *cache;
|
|
|
|
const char *name;
|
2004-04-14 00:40:54 +00:00
|
|
|
int slot;
|
2004-04-05 21:32:18 +00:00
|
|
|
|
2004-04-14 00:40:54 +00:00
|
|
|
|
2004-05-17 05:02:39 +00:00
|
|
|
if (*name_cache_variable == NULL) {
|
|
|
|
*name_cache_variable = malloc(sizeof(struct name_cache));
|
|
|
|
memset(*name_cache_variable, 0, sizeof(struct name_cache));
|
|
|
|
(*name_cache_variable)->size = name_cache_size;
|
2004-04-15 22:37:54 +00:00
|
|
|
}
|
|
|
|
|
2004-05-17 05:02:39 +00:00
|
|
|
cache = *name_cache_variable;
|
|
|
|
cache->probes++;
|
2004-04-15 22:37:54 +00:00
|
|
|
|
2004-05-17 05:02:39 +00:00
|
|
|
slot = id % cache->size;
|
2004-04-15 22:37:54 +00:00
|
|
|
if (cache->cache[slot].name != NULL) {
|
2004-05-17 05:02:39 +00:00
|
|
|
if (cache->cache[slot].id == id) {
|
|
|
|
cache->hits++;
|
|
|
|
if (cache->cache[slot].name == NO_NAME)
|
|
|
|
return (NULL);
|
2004-04-15 22:37:54 +00:00
|
|
|
return (cache->cache[slot].name);
|
2004-05-17 05:02:39 +00:00
|
|
|
}
|
|
|
|
if (cache->cache[slot].name != NO_NAME)
|
|
|
|
free((void *)(uintptr_t)cache->cache[slot].name);
|
2004-04-15 22:37:54 +00:00
|
|
|
cache->cache[slot].name = NULL;
|
2004-04-14 00:40:54 +00:00
|
|
|
}
|
2004-04-05 21:32:18 +00:00
|
|
|
|
2004-05-17 05:02:39 +00:00
|
|
|
if (lookup_fn(bsdtar, &name, id) == 0) {
|
|
|
|
if (name == NULL || name[0] == '\0') {
|
|
|
|
/* Cache the negative response. */
|
|
|
|
cache->cache[slot].name = NO_NAME;
|
|
|
|
cache->cache[slot].id = id;
|
|
|
|
} else {
|
|
|
|
cache->cache[slot].name = strdup(name);
|
|
|
|
cache->cache[slot].id = id;
|
|
|
|
return (cache->cache[slot].name);
|
|
|
|
}
|
2004-04-14 00:40:54 +00:00
|
|
|
}
|
2004-04-05 21:32:18 +00:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2004-05-17 05:02:39 +00:00
|
|
|
static const char *
|
|
|
|
lookup_uname(struct bsdtar *bsdtar, uid_t uid)
|
2004-04-05 21:32:18 +00:00
|
|
|
{
|
2004-05-17 05:02:39 +00:00
|
|
|
return (lookup_name(bsdtar, &bsdtar->uname_cache,
|
|
|
|
&lookup_uname_helper, (id_t)uid));
|
|
|
|
}
|
2004-04-14 00:40:54 +00:00
|
|
|
|
2004-05-17 05:02:39 +00:00
|
|
|
static int
|
|
|
|
lookup_uname_helper(struct bsdtar *bsdtar, const char **name, id_t id)
|
|
|
|
{
|
|
|
|
struct passwd *pwent;
|
2004-04-14 00:40:54 +00:00
|
|
|
|
2004-05-17 05:02:39 +00:00
|
|
|
(void)bsdtar; /* UNUSED */
|
|
|
|
|
2004-05-17 18:29:12 +00:00
|
|
|
errno = 0;
|
2004-05-17 05:02:39 +00:00
|
|
|
pwent = getpwuid((uid_t)id);
|
|
|
|
if (pwent == NULL) {
|
|
|
|
*name = NULL;
|
|
|
|
if (errno != 0)
|
2004-05-17 05:44:53 +00:00
|
|
|
bsdtar_warnc(bsdtar, errno, "getpwuid(%d) failed", id);
|
2004-05-17 05:02:39 +00:00
|
|
|
return (errno);
|
2004-04-14 00:40:54 +00:00
|
|
|
}
|
2004-04-05 21:32:18 +00:00
|
|
|
|
2004-05-17 05:02:39 +00:00
|
|
|
*name = pwent->pw_name;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
lookup_gname(struct bsdtar *bsdtar, gid_t gid)
|
|
|
|
{
|
|
|
|
return (lookup_name(bsdtar, &bsdtar->gname_cache,
|
|
|
|
&lookup_gname_helper, (id_t)gid));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
lookup_gname_helper(struct bsdtar *bsdtar, const char **name, id_t id)
|
|
|
|
{
|
|
|
|
struct group *grent;
|
|
|
|
|
|
|
|
(void)bsdtar; /* UNUSED */
|
|
|
|
|
2004-05-17 18:29:12 +00:00
|
|
|
errno = 0;
|
2004-05-17 05:02:39 +00:00
|
|
|
grent = getgrgid((gid_t)id);
|
2004-05-18 23:40:25 +00:00
|
|
|
if (grent == NULL) {
|
2004-05-17 05:02:39 +00:00
|
|
|
*name = NULL;
|
|
|
|
if (errno != 0)
|
2004-05-17 05:44:53 +00:00
|
|
|
bsdtar_warnc(bsdtar, errno, "getgrgid(%d) failed", id);
|
2004-05-17 05:02:39 +00:00
|
|
|
return (errno);
|
2004-04-14 00:40:54 +00:00
|
|
|
}
|
2004-05-17 05:02:39 +00:00
|
|
|
|
|
|
|
*name = grent->gr_name;
|
|
|
|
return (0);
|
2004-04-05 21:32:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Test if the specified file is newer than what's already
|
|
|
|
* in the archive.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
new_enough(struct bsdtar *bsdtar, const char *path,
|
|
|
|
time_t mtime_sec, int mtime_nsec)
|
|
|
|
{
|
|
|
|
struct archive_dir_entry *p;
|
|
|
|
|
|
|
|
if (path[0] == '.' && path[1] == '/' && path[2] != '\0')
|
|
|
|
path += 2;
|
|
|
|
|
2004-06-07 03:16:18 +00:00
|
|
|
if (bsdtar->archive_dir == NULL ||
|
2004-04-15 22:37:54 +00:00
|
|
|
bsdtar->archive_dir->head == NULL)
|
2004-04-05 21:32:18 +00:00
|
|
|
return (1);
|
|
|
|
|
2004-04-15 22:37:54 +00:00
|
|
|
for (p = bsdtar->archive_dir->head; p != NULL; p = p->next) {
|
2004-04-05 21:32:18 +00:00
|
|
|
if (strcmp(path, p->name)==0)
|
|
|
|
return (p->mtime_sec < mtime_sec ||
|
|
|
|
(p->mtime_sec == mtime_sec &&
|
|
|
|
p->mtime_nsec < mtime_nsec));
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (path[0] == '.' && path[1] == '/' && path[2] != '\0')
|
|
|
|
path += 2;
|
|
|
|
|
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));
|
|
|
|
p->name = strdup(path);
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (*bsdtar->argv == 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)
|
2004-05-17 05:44:53 +00:00
|
|
|
bsdtar_errc(bsdtar, 1, errno,
|
|
|
|
"Cannot stat %s", bsdtar->filename);
|
2004-04-05 21:32:18 +00:00
|
|
|
|
|
|
|
if (!S_ISREG(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);
|
|
|
|
}
|