2 * Copyright (c) 2003-2007 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include "bsdtar_platform.h"
27 __FBSDID("$FreeBSD: src/usr.bin/tar/write.c,v 1.79 2008/11/27 05:49:52 kientzle Exp $");
29 #ifdef HAVE_SYS_TYPES_H
30 #include <sys/types.h>
35 #ifdef HAVE_SYS_IOCTL_H
36 #include <sys/ioctl.h>
38 #ifdef HAVE_SYS_STAT_H
41 #ifdef HAVE_ATTR_XATTR_H
42 #include <attr/xattr.h>
59 #ifdef HAVE_LINUX_FS_H
60 #include <linux/fs.h> /* for Linux file flags */
63 * Some Linux distributions have both linux/ext2_fs.h and ext2fs/ext2_fs.h.
64 * As the include guards don't agree, the order of include is important.
66 #ifdef HAVE_LINUX_EXT2_FS_H
67 #include <linux/ext2_fs.h> /* for Linux file flags */
69 #if defined(HAVE_EXT2FS_EXT2_FS_H) && !defined(__CYGWIN__)
70 /* This header exists but is broken on Cygwin. */
71 #include <ext2fs/ext2_fs.h>
90 /* Size of buffer for holding file data prior to writing. */
91 #define FILEDATABUFLEN 65536
93 /* Fixed size of uname/gname caches. */
94 #define name_cache_size 101
96 static const char * const NO_NAME = "(noname)";
98 struct archive_dir_entry {
99 struct archive_dir_entry *next;
106 struct archive_dir_entry *head, *tail;
116 } cache[name_cache_size];
119 static void add_dir_list(struct bsdtar *bsdtar, const char *path,
120 time_t mtime_sec, int mtime_nsec);
121 static int append_archive(struct bsdtar *, struct archive *,
122 struct archive *ina);
123 static int append_archive_filename(struct bsdtar *,
124 struct archive *, const char *fname);
125 static void archive_names_from_file(struct bsdtar *bsdtar,
127 static int archive_names_from_file_helper(struct bsdtar *bsdtar,
129 static int copy_file_data(struct bsdtar *bsdtar,
130 struct archive *a, struct archive *ina);
131 static int new_enough(struct bsdtar *, const char *path,
132 const struct stat *);
133 static void test_for_append(struct bsdtar *);
134 static void write_archive(struct archive *, struct bsdtar *);
135 static void write_entry_backend(struct bsdtar *, struct archive *,
136 struct archive_entry *);
137 static int write_file_data(struct bsdtar *, struct archive *,
138 struct archive_entry *, int fd);
139 static void write_hierarchy(struct bsdtar *, struct archive *,
143 tar_mode_c(struct bsdtar *bsdtar)
148 if (*bsdtar->argv == NULL && bsdtar->names_from_file == NULL)
149 bsdtar_errc(bsdtar, 1, 0, "no files or directories specified");
151 a = archive_write_new();
153 /* Support any format that the library supports. */
154 if (bsdtar->create_format == NULL) {
155 r = archive_write_set_format_pax_restricted(a);
156 bsdtar->create_format = "pax restricted";
158 r = archive_write_set_format_by_name(a, bsdtar->create_format);
160 if (r != ARCHIVE_OK) {
161 fprintf(stderr, "Can't use format %s: %s\n",
162 bsdtar->create_format,
163 archive_error_string(a));
168 * If user explicitly set the block size, then assume they
169 * want the last block padded as well. Otherwise, use the
170 * default block size and accept archive_write_open_file()'s
171 * default padding decisions.
173 if (bsdtar->bytes_per_block != 0) {
174 archive_write_set_bytes_per_block(a, bsdtar->bytes_per_block);
175 archive_write_set_bytes_in_last_block(a,
176 bsdtar->bytes_per_block);
178 archive_write_set_bytes_per_block(a, DEFAULT_BYTES_PER_BLOCK);
180 if (bsdtar->compress_program) {
181 archive_write_set_compression_program(a, bsdtar->compress_program);
183 switch (bsdtar->create_compression) {
185 archive_write_set_compression_none(a);
189 archive_write_set_compression_bzip2(a);
194 archive_write_set_compression_xz(a);
197 archive_write_set_compression_lzma(a);
202 archive_write_set_compression_gzip(a);
206 archive_write_set_compression_compress(a);
209 bsdtar_errc(bsdtar, 1, 0,
210 "Unrecognized compression option -%c",
211 bsdtar->create_compression);
215 if (ARCHIVE_OK != archive_write_set_options(a, bsdtar->option_options))
216 bsdtar_errc(bsdtar, 1, 0, archive_error_string(a));
217 if (ARCHIVE_OK != archive_write_open_file(a, bsdtar->filename))
218 bsdtar_errc(bsdtar, 1, 0, archive_error_string(a));
219 write_archive(a, bsdtar);
223 * Same as 'c', except we only support tar or empty formats in
224 * uncompressed files on disk.
227 tar_mode_r(struct bsdtar *bsdtar)
232 struct archive_entry *entry;
235 /* Sanity-test some arguments and the file. */
236 test_for_append(bsdtar);
238 format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
240 bsdtar->fd = open(bsdtar->filename, O_RDWR | O_CREAT, 0666);
242 bsdtar_errc(bsdtar, 1, errno,
243 "Cannot open %s", bsdtar->filename);
245 a = archive_read_new();
246 archive_read_support_compression_all(a);
247 archive_read_support_format_tar(a);
248 archive_read_support_format_gnutar(a);
249 r = archive_read_open_fd(a, bsdtar->fd, 10240);
251 bsdtar_errc(bsdtar, 1, archive_errno(a),
252 "Can't read archive %s: %s", bsdtar->filename,
253 archive_error_string(a));
254 while (0 == archive_read_next_header(a, &entry)) {
255 if (archive_compression(a) != ARCHIVE_COMPRESSION_NONE) {
256 archive_read_finish(a);
258 bsdtar_errc(bsdtar, 1, 0,
259 "Cannot append to compressed archive.");
261 /* Keep going until we hit end-of-archive */
262 format = archive_format(a);
265 end_offset = archive_read_header_position(a);
266 archive_read_finish(a);
268 /* Re-open archive for writing */
269 a = archive_write_new();
270 archive_write_set_compression_none(a);
272 * Set the format to be used for writing. To allow people to
273 * extend empty files, we need to allow them to specify the format,
274 * which opens the possibility that they will specify a format that
275 * doesn't match the existing format. Hence, the following bit
276 * of arcane ugliness.
279 if (bsdtar->create_format != NULL) {
280 /* If the user requested a format, use that, but ... */
281 archive_write_set_format_by_name(a,
282 bsdtar->create_format);
283 /* ... complain if it's not compatible. */
284 format &= ARCHIVE_FORMAT_BASE_MASK;
285 if (format != (int)(archive_format(a) & ARCHIVE_FORMAT_BASE_MASK)
286 && format != ARCHIVE_FORMAT_EMPTY) {
287 bsdtar_errc(bsdtar, 1, 0,
288 "Format %s is incompatible with the archive %s.",
289 bsdtar->create_format, bsdtar->filename);
293 * Just preserve the current format, with a little care
294 * for formats that libarchive can't write.
296 if (format == ARCHIVE_FORMAT_TAR_GNUTAR)
297 /* TODO: When gtar supports pax, use pax restricted. */
298 format = ARCHIVE_FORMAT_TAR_USTAR;
299 if (format == ARCHIVE_FORMAT_EMPTY)
300 format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
301 archive_write_set_format(a, format);
303 lseek(bsdtar->fd, end_offset, SEEK_SET); /* XXX check return val XXX */
304 if (ARCHIVE_OK != archive_write_set_options(a, bsdtar->option_options))
305 bsdtar_errc(bsdtar, 1, 0, archive_error_string(a));
306 if (ARCHIVE_OK != archive_write_open_fd(a, bsdtar->fd))
307 bsdtar_errc(bsdtar, 1, 0, archive_error_string(a));
309 write_archive(a, bsdtar); /* XXX check return val XXX */
316 tar_mode_u(struct bsdtar *bsdtar)
320 struct archive_entry *entry;
322 struct archive_dir_entry *p;
323 struct archive_dir archive_dir;
325 bsdtar->archive_dir = &archive_dir;
326 memset(&archive_dir, 0, sizeof(archive_dir));
328 format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
330 /* Sanity-test some arguments and the file. */
331 test_for_append(bsdtar);
333 bsdtar->fd = open(bsdtar->filename, O_RDWR);
335 bsdtar_errc(bsdtar, 1, errno,
336 "Cannot open %s", bsdtar->filename);
338 a = archive_read_new();
339 archive_read_support_compression_all(a);
340 archive_read_support_format_tar(a);
341 archive_read_support_format_gnutar(a);
342 if (archive_read_open_fd(a, bsdtar->fd,
343 bsdtar->bytes_per_block != 0 ? bsdtar->bytes_per_block :
344 DEFAULT_BYTES_PER_BLOCK) != ARCHIVE_OK) {
345 bsdtar_errc(bsdtar, 1, 0,
346 "Can't open %s: %s", bsdtar->filename,
347 archive_error_string(a));
350 /* Build a list of all entries and their recorded mod times. */
351 while (0 == archive_read_next_header(a, &entry)) {
352 if (archive_compression(a) != ARCHIVE_COMPRESSION_NONE) {
353 archive_read_finish(a);
355 bsdtar_errc(bsdtar, 1, 0,
356 "Cannot append to compressed archive.");
358 add_dir_list(bsdtar, archive_entry_pathname(entry),
359 archive_entry_mtime(entry),
360 archive_entry_mtime_nsec(entry));
361 /* Record the last format determination we see */
362 format = archive_format(a);
363 /* Keep going until we hit end-of-archive */
366 end_offset = archive_read_header_position(a);
367 archive_read_finish(a);
369 /* Re-open archive for writing. */
370 a = archive_write_new();
371 archive_write_set_compression_none(a);
373 * Set format to same one auto-detected above, except that
374 * we don't write GNU tar format, so use ustar instead.
376 if (format == ARCHIVE_FORMAT_TAR_GNUTAR)
377 format = ARCHIVE_FORMAT_TAR_USTAR;
378 archive_write_set_format(a, format);
379 if (bsdtar->bytes_per_block != 0) {
380 archive_write_set_bytes_per_block(a, bsdtar->bytes_per_block);
381 archive_write_set_bytes_in_last_block(a,
382 bsdtar->bytes_per_block);
384 archive_write_set_bytes_per_block(a, DEFAULT_BYTES_PER_BLOCK);
385 lseek(bsdtar->fd, end_offset, SEEK_SET);
386 ftruncate(bsdtar->fd, end_offset);
387 if (ARCHIVE_OK != archive_write_set_options(a, bsdtar->option_options))
388 bsdtar_errc(bsdtar, 1, 0, archive_error_string(a));
389 if (ARCHIVE_OK != archive_write_open_fd(a, bsdtar->fd))
390 bsdtar_errc(bsdtar, 1, 0, archive_error_string(a));
392 write_archive(a, bsdtar);
397 while (bsdtar->archive_dir->head != NULL) {
398 p = bsdtar->archive_dir->head->next;
399 free(bsdtar->archive_dir->head->name);
400 free(bsdtar->archive_dir->head);
401 bsdtar->archive_dir->head = p;
403 bsdtar->archive_dir->tail = NULL;
408 * Write user-specified files/dirs to opened archive.
411 write_archive(struct archive *a, struct bsdtar *bsdtar)
414 struct archive_entry *entry, *sparse_entry;
416 /* We want to catch SIGINFO and SIGUSR1. */
417 siginfo_init(bsdtar);
419 /* Allocate a buffer for file data. */
420 if ((bsdtar->buff = malloc(FILEDATABUFLEN)) == NULL)
421 bsdtar_errc(bsdtar, 1, 0, "cannot allocate memory");
423 if ((bsdtar->resolver = archive_entry_linkresolver_new()) == NULL)
424 bsdtar_errc(bsdtar, 1, 0, "cannot create link resolver");
425 archive_entry_linkresolver_set_strategy(bsdtar->resolver,
427 if ((bsdtar->diskreader = archive_read_disk_new()) == NULL)
428 bsdtar_errc(bsdtar, 1, 0, "Cannot create read_disk object");
429 archive_read_disk_set_standard_lookup(bsdtar->diskreader);
431 if (bsdtar->names_from_file != NULL)
432 archive_names_from_file(bsdtar, a);
434 while (*bsdtar->argv) {
436 if (arg[0] == '-' && arg[1] == 'C') {
442 bsdtar_warnc(bsdtar, 1, 0,
443 "Missing argument for -C");
444 bsdtar->return_value = 1;
448 set_chdir(bsdtar, arg);
450 if (*arg != '/' && (arg[0] != '@' || arg[1] != '/'))
451 do_chdir(bsdtar); /* Handle a deferred -C */
453 if (append_archive_filename(bsdtar, a,
457 #if defined(_WIN32) && !defined(__CYGWIN__)
458 write_hierarchy_win(bsdtar, a, arg,
461 write_hierarchy(bsdtar, a, arg);
468 archive_entry_linkify(bsdtar->resolver, &entry, &sparse_entry);
469 while (entry != NULL) {
470 write_entry_backend(bsdtar, a, entry);
471 archive_entry_free(entry);
473 archive_entry_linkify(bsdtar->resolver, &entry, &sparse_entry);
476 if (archive_write_close(a)) {
477 bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(a));
478 bsdtar->return_value = 1;
482 /* Free file data buffer. */
484 archive_entry_linkresolver_free(bsdtar->resolver);
485 bsdtar->resolver = NULL;
486 archive_read_finish(bsdtar->diskreader);
487 bsdtar->diskreader = NULL;
489 if (bsdtar->option_totals) {
490 fprintf(stderr, "Total bytes written: " BSDTAR_FILESIZE_PRINTF "\n",
491 (BSDTAR_FILESIZE_TYPE)archive_position_compressed(a));
494 archive_write_finish(a);
496 /* Restore old SIGINFO + SIGUSR1 handlers. */
497 siginfo_done(bsdtar);
501 * Archive names specified in file.
503 * Unless --null was specified, a line containing exactly "-C" will
504 * cause the next line to be a directory to pass to chdir(). If
505 * --null is specified, then a line "-C" is just another filename.
508 archive_names_from_file(struct bsdtar *bsdtar, struct archive *a)
512 bsdtar->next_line_is_dir = 0;
513 process_lines(bsdtar, bsdtar->names_from_file,
514 archive_names_from_file_helper);
515 if (bsdtar->next_line_is_dir)
516 bsdtar_errc(bsdtar, 1, errno,
517 "Unexpected end of filename list; "
518 "directory expected after -C");
522 archive_names_from_file_helper(struct bsdtar *bsdtar, const char *line)
524 if (bsdtar->next_line_is_dir) {
525 set_chdir(bsdtar, line);
526 bsdtar->next_line_is_dir = 0;
527 } else if (!bsdtar->option_null && strcmp(line, "-C") == 0)
528 bsdtar->next_line_is_dir = 1;
531 do_chdir(bsdtar); /* Handle a deferred -C */
532 write_hierarchy(bsdtar, bsdtar->archive, line);
538 * Copy from specified archive to current archive. Returns non-zero
539 * for write errors (which force us to terminate the entire archiving
540 * operation). If there are errors reading the input archive, we set
541 * bsdtar->return_value but return zero, so the overall archiving
542 * operation will complete and return non-zero.
545 append_archive_filename(struct bsdtar *bsdtar, struct archive *a,
546 const char *filename)
551 if (strcmp(filename, "-") == 0)
552 filename = NULL; /* Library uses NULL for stdio. */
554 ina = archive_read_new();
555 archive_read_support_format_all(ina);
556 archive_read_support_compression_all(ina);
557 if (archive_read_open_file(ina, filename, 10240)) {
558 bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(ina));
559 bsdtar->return_value = 1;
563 rc = append_archive(bsdtar, a, ina);
565 if (archive_errno(ina)) {
566 bsdtar_warnc(bsdtar, 0, "Error reading archive %s: %s",
567 filename, archive_error_string(ina));
568 bsdtar->return_value = 1;
570 archive_read_finish(ina);
576 append_archive(struct bsdtar *bsdtar, struct archive *a, struct archive *ina)
578 struct archive_entry *in_entry;
581 while (0 == archive_read_next_header(ina, &in_entry)) {
582 if (!new_enough(bsdtar, archive_entry_pathname(in_entry),
583 archive_entry_stat(in_entry)))
585 if (excluded(bsdtar, archive_entry_pathname(in_entry)))
587 if (bsdtar->option_interactive &&
588 !yes("copy '%s'", archive_entry_pathname(in_entry)))
591 safe_fprintf(stderr, "a %s",
592 archive_entry_pathname(in_entry));
593 siginfo_setinfo(bsdtar, "copying",
594 archive_entry_pathname(in_entry),
595 archive_entry_size(in_entry));
596 siginfo_printinfo(bsdtar, 0);
598 e = archive_write_header(a, in_entry);
599 if (e != ARCHIVE_OK) {
600 if (!bsdtar->verbose)
601 bsdtar_warnc(bsdtar, 0, "%s: %s",
602 archive_entry_pathname(in_entry),
603 archive_error_string(a));
605 fprintf(stderr, ": %s", archive_error_string(a));
607 if (e == ARCHIVE_FATAL)
610 if (e >= ARCHIVE_WARN) {
611 if (archive_entry_size(in_entry) == 0)
612 archive_read_data_skip(ina);
613 else if (copy_file_data(bsdtar, a, ina))
618 fprintf(stderr, "\n");
621 /* Note: If we got here, we saw no write errors, so return success. */
625 /* Helper function to copy data between archives. */
627 copy_file_data(struct bsdtar *bsdtar, struct archive *a, struct archive *ina)
630 ssize_t bytes_written;
633 bytes_read = archive_read_data(ina, bsdtar->buff, FILEDATABUFLEN);
634 while (bytes_read > 0) {
635 siginfo_printinfo(bsdtar, progress);
637 bytes_written = archive_write_data(a, bsdtar->buff,
639 if (bytes_written < bytes_read) {
640 bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(a));
643 progress += bytes_written;
644 bytes_read = archive_read_data(ina, bsdtar->buff,
652 * Add the file or dir hierarchy named by 'path' to the archive
655 write_hierarchy(struct bsdtar *bsdtar, struct archive *a, const char *path)
657 struct archive_entry *entry = NULL, *spare_entry = NULL;
659 char symlink_mode = bsdtar->symlink_mode;
661 int dev_recorded = 0;
664 tree = tree_open(path);
667 bsdtar_warnc(bsdtar, errno, "%s: Cannot open", path);
668 bsdtar->return_value = 1;
672 while ((tree_ret = tree_next(tree))) {
674 const char *name = tree_current_path(tree);
675 const struct stat *st = NULL; /* info to use for this entry */
676 const struct stat *lst = NULL; /* lstat() information */
679 if (tree_ret == TREE_ERROR_FATAL)
680 bsdtar_errc(bsdtar, 1, tree_errno(tree),
681 "%s: Unable to continue traversing directory tree",
683 if (tree_ret == TREE_ERROR_DIR) {
684 bsdtar_warnc(bsdtar, errno,
685 "%s: Couldn't visit directory", name);
686 bsdtar->return_value = 1;
688 if (tree_ret != TREE_REGULAR)
692 * If this file/dir is excluded by a filename
695 if (excluded(bsdtar, name))
699 * Get lstat() info from the tree library.
701 lst = tree_current_lstat(tree);
703 /* Couldn't lstat(); must not exist. */
704 bsdtar_warnc(bsdtar, errno, "%s: Cannot stat", name);
705 /* Return error if files disappear during traverse. */
706 bsdtar->return_value = 1;
711 * Distinguish 'L'/'P'/'H' symlink following.
713 switch(symlink_mode) {
715 /* 'H': After the first item, rest like 'P'. */
717 /* 'H': First item (from command line) like 'L'. */
720 /* 'L': Do descend through a symlink to dir. */
721 descend = tree_current_is_dir(tree);
722 /* 'L': Follow symlinks to files. */
723 archive_read_disk_set_symlink_logical(bsdtar->diskreader);
724 /* 'L': Archive symlinks as targets, if we can. */
725 st = tree_current_stat(tree);
728 /* If stat fails, we have a broken symlink;
729 * in that case, don't follow the link. */
732 /* 'P': Don't descend through a symlink to dir. */
733 descend = tree_current_is_physical_dir(tree);
734 /* 'P': Don't follow symlinks to files. */
735 archive_read_disk_set_symlink_physical(bsdtar->diskreader);
736 /* 'P': Archive symlinks as symlinks. */
742 * If user has asked us not to cross mount points,
743 * then don't descend into into a dir on a different
747 first_dev = lst->st_dev;
750 if (bsdtar->option_dont_traverse_mounts) {
751 if (lst->st_dev != first_dev)
756 * In -u mode, check that the file is newer than what's
757 * already in the archive; in all modes, obey --newerXXX flags.
759 if (!new_enough(bsdtar, name, st))
762 archive_entry_free(entry);
763 entry = archive_entry_new();
765 archive_entry_set_pathname(entry, name);
766 archive_entry_copy_sourcepath(entry,
767 tree_current_access_path(tree));
769 /* Populate the archive_entry with metadata from the disk. */
770 /* XXX TODO: Arrange to open a regular file before
771 * calling this so we can pass in an fd and shorten
772 * the race to query metadata. The linkify dance
773 * makes this more complex than it might sound. */
774 r = archive_read_disk_entry_from_file(bsdtar->diskreader,
777 bsdtar_warnc(bsdtar, archive_errno(bsdtar->diskreader),
778 archive_error_string(bsdtar->diskreader));
779 if (r < ARCHIVE_WARN)
782 /* XXX TODO: Just use flag data from entry; avoid the
783 * duplicate check here. */
786 * If this file/dir is flagged "nodump" and we're
787 * honoring such flags, skip this file/dir.
789 #ifdef HAVE_STRUCT_STAT_ST_FLAGS
790 /* BSD systems store flags in struct stat */
791 if (bsdtar->option_honor_nodump &&
792 (lst->st_flags & UF_NODUMP))
796 #if defined(EXT2_IOC_GETFLAGS) && defined(EXT2_NODUMP_FL)
797 /* Linux uses ioctl to read flags. */
798 if (bsdtar->option_honor_nodump) {
799 int fd = open(name, O_RDONLY | O_NONBLOCK);
801 unsigned long fflags;
802 int r = ioctl(fd, EXT2_IOC_GETFLAGS, &fflags);
804 if (r >= 0 && (fflags & EXT2_NODUMP_FL))
811 * If the user vetoes this file/directory, skip it.
812 * We want this to be fairly late; if some other
813 * check would veto this file, we shouldn't bother
816 if (bsdtar->option_interactive &&
817 !yes("add '%s'", name))
820 /* Note: if user vetoes, we won't descend. */
821 if (descend && !bsdtar->option_no_subdirs)
825 * Rewrite the pathname to be archived. If rewrite
826 * fails, skip the entry.
828 if (edit_pathname(bsdtar, entry))
831 /* Display entry as we process it.
832 * This format is required by SUSv2. */
834 safe_fprintf(stderr, "a %s",
835 archive_entry_pathname(entry));
837 /* Non-regular files get archived with zero size. */
838 if (!S_ISREG(st->st_mode))
839 archive_entry_set_size(entry, 0);
841 /* Record what we're doing, for SIGINFO / SIGUSR1. */
842 siginfo_setinfo(bsdtar, "adding",
843 archive_entry_pathname(entry), archive_entry_size(entry));
844 archive_entry_linkify(bsdtar->resolver, &entry, &spare_entry);
846 /* Handle SIGINFO / SIGUSR1 request if one was made. */
847 siginfo_printinfo(bsdtar, 0);
849 while (entry != NULL) {
850 write_entry_backend(bsdtar, a, entry);
851 archive_entry_free(entry);
857 fprintf(stderr, "\n");
859 archive_entry_free(entry);
864 * Backend for write_entry.
867 write_entry_backend(struct bsdtar *bsdtar, struct archive *a,
868 struct archive_entry *entry)
873 if (archive_entry_size(entry) > 0) {
874 const char *pathname = archive_entry_sourcepath(entry);
875 fd = open(pathname, O_RDONLY);
877 if (!bsdtar->verbose)
878 bsdtar_warnc(bsdtar, errno,
879 "%s: could not open file", pathname);
881 fprintf(stderr, ": %s", strerror(errno));
886 e = archive_write_header(a, entry);
887 if (e != ARCHIVE_OK) {
888 if (!bsdtar->verbose)
889 bsdtar_warnc(bsdtar, 0, "%s: %s",
890 archive_entry_pathname(entry),
891 archive_error_string(a));
893 fprintf(stderr, ": %s", archive_error_string(a));
896 if (e == ARCHIVE_FATAL)
900 * If we opened a file earlier, write it out now. Note that
901 * the format handler might have reset the size field to zero
902 * to inform us that the archive body won't get stored. In
903 * that case, just skip the write.
905 if (e >= ARCHIVE_WARN && fd >= 0 && archive_entry_size(entry) > 0) {
906 if (write_file_data(bsdtar, a, entry, fd))
911 * If we opened a file, close it now even if there was an error
912 * which made us decide not to write the archive body.
919 /* Helper function to copy file to archive. */
921 write_file_data(struct bsdtar *bsdtar, struct archive *a,
922 struct archive_entry *entry, int fd)
925 ssize_t bytes_written;
928 bytes_read = read(fd, bsdtar->buff, FILEDATABUFLEN);
929 while (bytes_read > 0) {
930 siginfo_printinfo(bsdtar, progress);
932 bytes_written = archive_write_data(a, bsdtar->buff,
934 if (bytes_written < 0) {
935 /* Write failed; this is bad */
936 bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(a));
939 if (bytes_written < bytes_read) {
940 /* Write was truncated; warn but continue. */
941 bsdtar_warnc(bsdtar, 0,
942 "%s: Truncated write; file may have grown while being archived.",
943 archive_entry_pathname(entry));
946 progress += bytes_written;
947 bytes_read = read(fd, bsdtar->buff, FILEDATABUFLEN);
953 * Test if the specified file is new enough to include in the archive.
956 new_enough(struct bsdtar *bsdtar, const char *path, const struct stat *st)
958 struct archive_dir_entry *p;
961 * If this file/dir is excluded by a time comparison, skip it.
963 if (bsdtar->newer_ctime_sec > 0) {
964 if (st->st_ctime < bsdtar->newer_ctime_sec)
965 return (0); /* Too old, skip it. */
966 if (st->st_ctime == bsdtar->newer_ctime_sec
967 && ARCHIVE_STAT_CTIME_NANOS(st)
968 <= bsdtar->newer_ctime_nsec)
969 return (0); /* Too old, skip it. */
971 if (bsdtar->newer_mtime_sec > 0) {
972 if (st->st_mtime < bsdtar->newer_mtime_sec)
973 return (0); /* Too old, skip it. */
974 if (st->st_mtime == bsdtar->newer_mtime_sec
975 && ARCHIVE_STAT_MTIME_NANOS(st)
976 <= bsdtar->newer_mtime_nsec)
977 return (0); /* Too old, skip it. */
981 * In -u mode, we only write an entry if it's newer than
982 * what was already in the archive.
984 if (bsdtar->archive_dir != NULL &&
985 bsdtar->archive_dir->head != NULL) {
986 for (p = bsdtar->archive_dir->head; p != NULL; p = p->next) {
987 if (pathcmp(path, p->name)==0)
988 return (p->mtime_sec < st->st_mtime ||
989 (p->mtime_sec == st->st_mtime &&
991 < ARCHIVE_STAT_MTIME_NANOS(st)));
995 /* If the file wasn't rejected, include it. */
1000 * Add an entry to the dir list for 'u' mode.
1002 * XXX TODO: Make this fast.
1005 add_dir_list(struct bsdtar *bsdtar, const char *path,
1006 time_t mtime_sec, int mtime_nsec)
1008 struct archive_dir_entry *p;
1011 * Search entire list to see if this file has appeared before.
1012 * If it has, override the timestamp data.
1014 p = bsdtar->archive_dir->head;
1016 if (strcmp(path, p->name)==0) {
1017 p->mtime_sec = mtime_sec;
1018 p->mtime_nsec = mtime_nsec;
1024 p = malloc(sizeof(*p));
1026 bsdtar_errc(bsdtar, 1, ENOMEM, "Can't read archive directory");
1028 p->name = strdup(path);
1029 if (p->name == NULL)
1030 bsdtar_errc(bsdtar, 1, ENOMEM, "Can't read archive directory");
1031 p->mtime_sec = mtime_sec;
1032 p->mtime_nsec = mtime_nsec;
1034 if (bsdtar->archive_dir->tail == NULL) {
1035 bsdtar->archive_dir->head = bsdtar->archive_dir->tail = p;
1037 bsdtar->archive_dir->tail->next = p;
1038 bsdtar->archive_dir->tail = p;
1043 test_for_append(struct bsdtar *bsdtar)
1047 if (*bsdtar->argv == NULL && bsdtar->names_from_file == NULL)
1048 bsdtar_errc(bsdtar, 1, 0, "no files or directories specified");
1049 if (bsdtar->filename == NULL)
1050 bsdtar_errc(bsdtar, 1, 0, "Cannot append to stdout.");
1052 if (bsdtar->create_compression != 0)
1053 bsdtar_errc(bsdtar, 1, 0,
1054 "Cannot append to %s with compression", bsdtar->filename);
1056 if (stat(bsdtar->filename, &s) != 0)
1059 if (!S_ISREG(s.st_mode) && !S_ISBLK(s.st_mode))
1060 bsdtar_errc(bsdtar, 1, 0,
1061 "Cannot append to %s: not a regular file.",