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$");
29 #ifdef HAVE_SYS_TYPES_H
30 #include <sys/types.h>
32 #ifdef HAVE_SYS_IOCTL_H
33 #include <sys/ioctl.h>
35 #ifdef HAVE_SYS_STAT_H
38 #ifdef HAVE_ATTR_XATTR_H
39 #include <attr/xattr.h>
56 #ifdef HAVE_LINUX_FS_H
57 #include <linux/fs.h> /* for Linux file flags */
60 * Some Linux distributions have both linux/ext2_fs.h and ext2fs/ext2_fs.h.
61 * As the include guards don't agree, the order of include is important.
63 #ifdef HAVE_LINUX_EXT2_FS_H
64 #include <linux/ext2_fs.h> /* for Linux file flags */
66 #if defined(HAVE_EXT2FS_EXT2_FS_H) && !defined(__CYGWIN__)
67 /* This header exists but is broken on Cygwin. */
68 #include <ext2fs/ext2_fs.h>
89 #include "line_reader.h"
92 /* Size of buffer for holding file data prior to writing. */
93 #define FILEDATABUFLEN 65536
95 /* Fixed size of uname/gname caches. */
96 #define name_cache_size 101
102 static const char * const NO_NAME = "(noname)";
104 struct archive_dir_entry {
105 struct archive_dir_entry *next;
112 struct archive_dir_entry *head, *tail;
122 } cache[name_cache_size];
125 static void add_dir_list(struct bsdtar *bsdtar, const char *path,
126 time_t mtime_sec, int mtime_nsec);
127 static int append_archive(struct bsdtar *, struct archive *,
128 struct archive *ina);
129 static int append_archive_filename(struct bsdtar *,
130 struct archive *, const char *fname);
131 static void archive_names_from_file(struct bsdtar *bsdtar,
133 static int copy_file_data(struct bsdtar *, struct archive *a,
134 struct archive *ina, struct archive_entry *);
135 static int new_enough(struct bsdtar *, const char *path,
136 const struct stat *);
137 static void report_write(struct bsdtar *, struct archive *,
138 struct archive_entry *, int64_t progress);
139 static void test_for_append(struct bsdtar *);
140 static void write_archive(struct archive *, struct bsdtar *);
141 static void write_entry_backend(struct bsdtar *, struct archive *,
142 struct archive_entry *);
143 static int write_file_data(struct bsdtar *, struct archive *,
144 struct archive_entry *, int fd);
145 static void write_hierarchy(struct bsdtar *, struct archive *,
148 #if defined(_WIN32) && !defined(__CYGWIN__)
149 /* Not a full lseek() emulation, but enough for our needs here. */
151 seek_file(int fd, int64_t offset, int whence)
153 LARGE_INTEGER distance;
154 (void)whence; /* UNUSED */
155 distance.QuadPart = offset;
156 return (SetFilePointerEx((HANDLE)_get_osfhandle(fd),
157 distance, NULL, FILE_BEGIN) ? 1 : -1);
162 #define lseek seek_file
166 tar_mode_c(struct bsdtar *bsdtar)
171 if (*bsdtar->argv == NULL && bsdtar->names_from_file == NULL)
172 lafe_errc(1, 0, "no files or directories specified");
174 a = archive_write_new();
176 /* Support any format that the library supports. */
177 if (bsdtar->create_format == NULL) {
178 r = archive_write_set_format_pax_restricted(a);
179 bsdtar->create_format = "pax restricted";
181 r = archive_write_set_format_by_name(a, bsdtar->create_format);
183 if (r != ARCHIVE_OK) {
184 fprintf(stderr, "Can't use format %s: %s\n",
185 bsdtar->create_format,
186 archive_error_string(a));
191 * If user explicitly set the block size, then assume they
192 * want the last block padded as well. Otherwise, use the
193 * default block size and accept archive_write_open_file()'s
194 * default padding decisions.
196 if (bsdtar->bytes_per_block != 0) {
197 archive_write_set_bytes_per_block(a, bsdtar->bytes_per_block);
198 archive_write_set_bytes_in_last_block(a,
199 bsdtar->bytes_per_block);
201 archive_write_set_bytes_per_block(a, DEFAULT_BYTES_PER_BLOCK);
203 if (bsdtar->compress_program) {
204 archive_write_set_compression_program(a, bsdtar->compress_program);
206 switch (bsdtar->create_compression) {
208 r = archive_write_set_compression_none(a);
211 r = archive_write_set_compression_bzip2(a);
214 r = archive_write_set_compression_xz(a);
217 r = archive_write_set_compression_lzma(a);
220 r = archive_write_set_compression_gzip(a);
223 r = archive_write_set_compression_compress(a);
227 "Unrecognized compression option -%c",
228 bsdtar->create_compression);
230 if (r != ARCHIVE_OK) {
232 "Unsupported compression option -%c",
233 bsdtar->create_compression);
237 if (ARCHIVE_OK != archive_write_set_options(a, bsdtar->option_options))
238 lafe_errc(1, 0, "%s", archive_error_string(a));
239 if (ARCHIVE_OK != archive_write_open_file(a, bsdtar->filename))
240 lafe_errc(1, 0, "%s", archive_error_string(a));
241 write_archive(a, bsdtar);
245 * Same as 'c', except we only support tar or empty formats in
246 * uncompressed files on disk.
249 tar_mode_r(struct bsdtar *bsdtar)
254 struct archive_entry *entry;
257 /* Sanity-test some arguments and the file. */
258 test_for_append(bsdtar);
260 format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
262 #if defined(__BORLANDC__)
263 bsdtar->fd = open(bsdtar->filename, O_RDWR | O_CREAT | O_BINARY);
265 bsdtar->fd = open(bsdtar->filename, O_RDWR | O_CREAT | O_BINARY, 0666);
269 "Cannot open %s", bsdtar->filename);
271 a = archive_read_new();
272 archive_read_support_compression_all(a);
273 archive_read_support_format_tar(a);
274 archive_read_support_format_gnutar(a);
275 r = archive_read_open_fd(a, bsdtar->fd, 10240);
277 lafe_errc(1, archive_errno(a),
278 "Can't read archive %s: %s", bsdtar->filename,
279 archive_error_string(a));
280 while (0 == archive_read_next_header(a, &entry)) {
281 if (archive_compression(a) != ARCHIVE_COMPRESSION_NONE) {
282 archive_read_finish(a);
285 "Cannot append to compressed archive.");
287 /* Keep going until we hit end-of-archive */
288 format = archive_format(a);
291 end_offset = archive_read_header_position(a);
292 archive_read_finish(a);
294 /* Re-open archive for writing */
295 a = archive_write_new();
296 archive_write_set_compression_none(a);
298 * Set the format to be used for writing. To allow people to
299 * extend empty files, we need to allow them to specify the format,
300 * which opens the possibility that they will specify a format that
301 * doesn't match the existing format. Hence, the following bit
302 * of arcane ugliness.
305 if (bsdtar->create_format != NULL) {
306 /* If the user requested a format, use that, but ... */
307 archive_write_set_format_by_name(a,
308 bsdtar->create_format);
309 /* ... complain if it's not compatible. */
310 format &= ARCHIVE_FORMAT_BASE_MASK;
311 if (format != (int)(archive_format(a) & ARCHIVE_FORMAT_BASE_MASK)
312 && format != ARCHIVE_FORMAT_EMPTY) {
314 "Format %s is incompatible with the archive %s.",
315 bsdtar->create_format, bsdtar->filename);
319 * Just preserve the current format, with a little care
320 * for formats that libarchive can't write.
322 if (format == ARCHIVE_FORMAT_TAR_GNUTAR)
323 /* TODO: When gtar supports pax, use pax restricted. */
324 format = ARCHIVE_FORMAT_TAR_USTAR;
325 if (format == ARCHIVE_FORMAT_EMPTY)
326 format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
327 archive_write_set_format(a, format);
329 if (lseek(bsdtar->fd, end_offset, SEEK_SET) < 0)
330 lafe_errc(1, errno, "Could not seek to archive end");
331 if (ARCHIVE_OK != archive_write_set_options(a, bsdtar->option_options))
332 lafe_errc(1, 0, "%s", archive_error_string(a));
333 if (ARCHIVE_OK != archive_write_open_fd(a, bsdtar->fd))
334 lafe_errc(1, 0, "%s", archive_error_string(a));
336 write_archive(a, bsdtar); /* XXX check return val XXX */
343 tar_mode_u(struct bsdtar *bsdtar)
347 struct archive_entry *entry;
349 struct archive_dir_entry *p;
350 struct archive_dir archive_dir;
352 bsdtar->archive_dir = &archive_dir;
353 memset(&archive_dir, 0, sizeof(archive_dir));
355 format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
357 /* Sanity-test some arguments and the file. */
358 test_for_append(bsdtar);
360 bsdtar->fd = open(bsdtar->filename, O_RDWR | O_BINARY);
363 "Cannot open %s", bsdtar->filename);
365 a = archive_read_new();
366 archive_read_support_compression_all(a);
367 archive_read_support_format_tar(a);
368 archive_read_support_format_gnutar(a);
369 if (archive_read_open_fd(a, bsdtar->fd,
370 bsdtar->bytes_per_block != 0 ? bsdtar->bytes_per_block :
371 DEFAULT_BYTES_PER_BLOCK) != ARCHIVE_OK) {
373 "Can't open %s: %s", bsdtar->filename,
374 archive_error_string(a));
377 /* Build a list of all entries and their recorded mod times. */
378 while (0 == archive_read_next_header(a, &entry)) {
379 if (archive_compression(a) != ARCHIVE_COMPRESSION_NONE) {
380 archive_read_finish(a);
383 "Cannot append to compressed archive.");
385 add_dir_list(bsdtar, archive_entry_pathname(entry),
386 archive_entry_mtime(entry),
387 archive_entry_mtime_nsec(entry));
388 /* Record the last format determination we see */
389 format = archive_format(a);
390 /* Keep going until we hit end-of-archive */
393 end_offset = archive_read_header_position(a);
394 archive_read_finish(a);
396 /* Re-open archive for writing. */
397 a = archive_write_new();
398 archive_write_set_compression_none(a);
400 * Set format to same one auto-detected above, except that
401 * we don't write GNU tar format, so use ustar instead.
403 if (format == ARCHIVE_FORMAT_TAR_GNUTAR)
404 format = ARCHIVE_FORMAT_TAR_USTAR;
405 archive_write_set_format(a, format);
406 if (bsdtar->bytes_per_block != 0) {
407 archive_write_set_bytes_per_block(a, bsdtar->bytes_per_block);
408 archive_write_set_bytes_in_last_block(a,
409 bsdtar->bytes_per_block);
411 archive_write_set_bytes_per_block(a, DEFAULT_BYTES_PER_BLOCK);
412 if (lseek(bsdtar->fd, end_offset, SEEK_SET) < 0)
413 lafe_errc(1, errno, "Could not seek to archive end");
414 if (ARCHIVE_OK != archive_write_set_options(a, bsdtar->option_options))
415 lafe_errc(1, 0, "%s", archive_error_string(a));
416 if (ARCHIVE_OK != archive_write_open_fd(a, bsdtar->fd))
417 lafe_errc(1, 0, "%s", archive_error_string(a));
419 write_archive(a, bsdtar);
424 while (bsdtar->archive_dir->head != NULL) {
425 p = bsdtar->archive_dir->head->next;
426 free(bsdtar->archive_dir->head->name);
427 free(bsdtar->archive_dir->head);
428 bsdtar->archive_dir->head = p;
430 bsdtar->archive_dir->tail = NULL;
435 * Write user-specified files/dirs to opened archive.
438 write_archive(struct archive *a, struct bsdtar *bsdtar)
441 struct archive_entry *entry, *sparse_entry;
443 /* Allocate a buffer for file data. */
444 if ((bsdtar->buff = malloc(FILEDATABUFLEN)) == NULL)
445 lafe_errc(1, 0, "cannot allocate memory");
447 if ((bsdtar->resolver = archive_entry_linkresolver_new()) == NULL)
448 lafe_errc(1, 0, "cannot create link resolver");
449 archive_entry_linkresolver_set_strategy(bsdtar->resolver,
451 if ((bsdtar->diskreader = archive_read_disk_new()) == NULL)
452 lafe_errc(1, 0, "Cannot create read_disk object");
453 archive_read_disk_set_standard_lookup(bsdtar->diskreader);
455 if (bsdtar->names_from_file != NULL)
456 archive_names_from_file(bsdtar, a);
458 while (*bsdtar->argv) {
460 if (arg[0] == '-' && arg[1] == 'C') {
467 "Missing argument for -C");
468 bsdtar->return_value = 1;
472 set_chdir(bsdtar, arg);
474 if (*arg != '/' && (arg[0] != '@' || arg[1] != '/'))
475 do_chdir(bsdtar); /* Handle a deferred -C */
477 if (append_archive_filename(bsdtar, a,
481 write_hierarchy(bsdtar, a, arg);
487 archive_entry_linkify(bsdtar->resolver, &entry, &sparse_entry);
488 while (entry != NULL) {
489 write_entry_backend(bsdtar, a, entry);
490 archive_entry_free(entry);
492 archive_entry_linkify(bsdtar->resolver, &entry, &sparse_entry);
495 if (archive_write_close(a)) {
496 lafe_warnc(0, "%s", archive_error_string(a));
497 bsdtar->return_value = 1;
501 /* Free file data buffer. */
503 archive_entry_linkresolver_free(bsdtar->resolver);
504 bsdtar->resolver = NULL;
505 archive_read_finish(bsdtar->diskreader);
506 bsdtar->diskreader = NULL;
508 if (bsdtar->option_totals) {
509 fprintf(stderr, "Total bytes written: %s\n",
510 tar_i64toa(archive_position_compressed(a)));
513 archive_write_finish(a);
517 * Archive names specified in file.
519 * Unless --null was specified, a line containing exactly "-C" will
520 * cause the next line to be a directory to pass to chdir(). If
521 * --null is specified, then a line "-C" is just another filename.
524 archive_names_from_file(struct bsdtar *bsdtar, struct archive *a)
526 struct lafe_line_reader *lr;
529 bsdtar->next_line_is_dir = 0;
531 lr = lafe_line_reader(bsdtar->names_from_file, bsdtar->option_null);
532 while ((line = lafe_line_reader_next(lr)) != NULL) {
533 if (bsdtar->next_line_is_dir) {
534 set_chdir(bsdtar, line);
535 bsdtar->next_line_is_dir = 0;
536 } else if (!bsdtar->option_null && strcmp(line, "-C") == 0)
537 bsdtar->next_line_is_dir = 1;
540 do_chdir(bsdtar); /* Handle a deferred -C */
541 write_hierarchy(bsdtar, a, line);
544 lafe_line_reader_free(lr);
545 if (bsdtar->next_line_is_dir)
547 "Unexpected end of filename list; "
548 "directory expected after -C");
552 * Copy from specified archive to current archive. Returns non-zero
553 * for write errors (which force us to terminate the entire archiving
554 * operation). If there are errors reading the input archive, we set
555 * bsdtar->return_value but return zero, so the overall archiving
556 * operation will complete and return non-zero.
559 append_archive_filename(struct bsdtar *bsdtar, struct archive *a,
560 const char *filename)
565 if (strcmp(filename, "-") == 0)
566 filename = NULL; /* Library uses NULL for stdio. */
568 ina = archive_read_new();
569 archive_read_support_format_all(ina);
570 archive_read_support_compression_all(ina);
571 if (archive_read_open_file(ina, filename, 10240)) {
572 lafe_warnc(0, "%s", archive_error_string(ina));
573 bsdtar->return_value = 1;
577 rc = append_archive(bsdtar, a, ina);
579 if (rc != ARCHIVE_OK) {
580 lafe_warnc(0, "Error reading archive %s: %s",
581 filename, archive_error_string(ina));
582 bsdtar->return_value = 1;
584 archive_read_finish(ina);
590 append_archive(struct bsdtar *bsdtar, struct archive *a, struct archive *ina)
592 struct archive_entry *in_entry;
595 while (0 == archive_read_next_header(ina, &in_entry)) {
596 if (!new_enough(bsdtar, archive_entry_pathname(in_entry),
597 archive_entry_stat(in_entry)))
599 if (lafe_excluded(bsdtar->matching, archive_entry_pathname(in_entry)))
601 if (bsdtar->option_interactive &&
602 !yes("copy '%s'", archive_entry_pathname(in_entry)))
605 safe_fprintf(stderr, "a %s",
606 archive_entry_pathname(in_entry));
608 report_write(bsdtar, a, in_entry, 0);
610 e = archive_write_header(a, in_entry);
611 if (e != ARCHIVE_OK) {
612 if (!bsdtar->verbose)
613 lafe_warnc(0, "%s: %s",
614 archive_entry_pathname(in_entry),
615 archive_error_string(a));
617 fprintf(stderr, ": %s", archive_error_string(a));
619 if (e == ARCHIVE_FATAL)
622 if (e >= ARCHIVE_WARN) {
623 if (archive_entry_size(in_entry) == 0)
624 archive_read_data_skip(ina);
625 else if (copy_file_data(bsdtar, a, ina, in_entry))
630 fprintf(stderr, "\n");
633 /* Note: If we got here, we saw no write errors, so return success. */
637 /* Helper function to copy data between archives. */
639 copy_file_data(struct bsdtar *bsdtar, struct archive *a,
640 struct archive *ina, struct archive_entry *entry)
643 ssize_t bytes_written;
644 int64_t progress = 0;
646 bytes_read = archive_read_data(ina, bsdtar->buff, FILEDATABUFLEN);
647 while (bytes_read > 0) {
649 report_write(bsdtar, a, entry, progress);
651 bytes_written = archive_write_data(a, bsdtar->buff,
653 if (bytes_written < bytes_read) {
654 lafe_warnc(0, "%s", archive_error_string(a));
657 progress += bytes_written;
658 bytes_read = archive_read_data(ina, bsdtar->buff,
666 * Add the file or dir hierarchy named by 'path' to the archive
669 write_hierarchy(struct bsdtar *bsdtar, struct archive *a, const char *path)
671 struct archive_entry *entry = NULL, *spare_entry = NULL;
673 char symlink_mode = bsdtar->symlink_mode;
675 int dev_recorded = 0;
678 tree = tree_open(path);
681 lafe_warnc(errno, "%s: Cannot open", path);
682 bsdtar->return_value = 1;
686 while ((tree_ret = tree_next(tree)) != 0) {
688 const char *name = tree_current_path(tree);
689 const struct stat *st = NULL; /* info to use for this entry */
690 const struct stat *lst = NULL; /* lstat() information */
693 if (tree_ret == TREE_ERROR_FATAL)
694 lafe_errc(1, tree_errno(tree),
695 "%s: Unable to continue traversing directory tree",
697 if (tree_ret == TREE_ERROR_DIR) {
699 "%s: Couldn't visit directory", name);
700 bsdtar->return_value = 1;
702 if (tree_ret != TREE_REGULAR)
706 * If this file/dir is excluded by a filename
709 if (lafe_excluded(bsdtar->matching, name))
713 * Get lstat() info from the tree library.
715 lst = tree_current_lstat(tree);
717 /* Couldn't lstat(); must not exist. */
718 lafe_warnc(errno, "%s: Cannot stat", name);
719 /* Return error if files disappear during traverse. */
720 bsdtar->return_value = 1;
725 * Distinguish 'L'/'P'/'H' symlink following.
727 switch(symlink_mode) {
729 /* 'H': After the first item, rest like 'P'. */
731 /* 'H': First item (from command line) like 'L'. */
734 /* 'L': Do descend through a symlink to dir. */
735 descend = tree_current_is_dir(tree);
736 /* 'L': Follow symlinks to files. */
737 archive_read_disk_set_symlink_logical(bsdtar->diskreader);
738 /* 'L': Archive symlinks as targets, if we can. */
739 st = tree_current_stat(tree);
742 /* If stat fails, we have a broken symlink;
743 * in that case, don't follow the link. */
746 /* 'P': Don't descend through a symlink to dir. */
747 descend = tree_current_is_physical_dir(tree);
748 /* 'P': Don't follow symlinks to files. */
749 archive_read_disk_set_symlink_physical(bsdtar->diskreader);
750 /* 'P': Archive symlinks as symlinks. */
755 if (bsdtar->option_no_subdirs)
759 * Are we about to cross to a new filesystem?
762 /* This is the initial file system. */
763 first_dev = lst->st_dev;
765 } else if (lst->st_dev == first_dev) {
766 /* The starting file system is always acceptable. */
767 } else if (descend == 0) {
768 /* We're not descending, so no need to check. */
769 } else if (bsdtar->option_dont_traverse_mounts) {
772 /* We're prepared to cross a mount point. */
774 /* XXX TODO: check whether this filesystem is
775 * synthetic and/or local. Add a new
776 * --local-only option to skip non-local
777 * filesystems. Skip synthetic filesystems
780 * The results should be cached, since
781 * tree.c doesn't usually visit a directory
782 * and the directory contents together. A simple
783 * move-to-front list should perform quite well.
785 * This is going to be heavily OS dependent:
786 * FreeBSD's statfs() in conjunction with getvfsbyname()
787 * provides all of this; NetBSD's statvfs() does
788 * most of it; other systems will vary.
793 * In -u mode, check that the file is newer than what's
794 * already in the archive; in all modes, obey --newerXXX flags.
796 if (!new_enough(bsdtar, name, st)) {
799 if (bsdtar->option_interactive &&
800 !yes("add '%s'", name))
806 archive_entry_free(entry);
807 entry = archive_entry_new();
809 archive_entry_set_pathname(entry, name);
810 archive_entry_copy_sourcepath(entry,
811 tree_current_access_path(tree));
813 /* Populate the archive_entry with metadata from the disk. */
814 /* XXX TODO: Arrange to open a regular file before
815 * calling this so we can pass in an fd and shorten
816 * the race to query metadata. The linkify dance
817 * makes this more complex than it might sound. */
818 #if defined(_WIN32) && !defined(__CYGWIN__)
819 /* TODO: tree.c uses stat(), which is badly broken
820 * on Windows. To fix this, we should
821 * deprecate tree_current_stat() and provide a new
822 * call tree_populate_entry(t, entry). This call
823 * would use stat() internally on POSIX and
824 * GetInfoByFileHandle() internally on Windows.
825 * This would be another step towards a tree-walker
826 * that can be integrated deep into libarchive.
827 * For now, just set st to NULL on Windows;
828 * archive_read_disk_entry_from_file() should
829 * be smart enough to use platform-appropriate
830 * ways to probe file information.
834 r = archive_read_disk_entry_from_file(bsdtar->diskreader,
837 lafe_warnc(archive_errno(bsdtar->diskreader),
838 "%s", archive_error_string(bsdtar->diskreader));
839 if (r < ARCHIVE_WARN)
842 /* XXX TODO: Just use flag data from entry; avoid the
843 * duplicate check here. */
846 * If this file/dir is flagged "nodump" and we're
847 * honoring such flags, skip this file/dir.
849 #if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP)
850 /* BSD systems store flags in struct stat */
851 if (bsdtar->option_honor_nodump &&
852 (lst->st_flags & UF_NODUMP))
856 #if defined(EXT2_IOC_GETFLAGS) && defined(EXT2_NODUMP_FL)
857 /* Linux uses ioctl to read flags. */
858 if (bsdtar->option_honor_nodump) {
859 int fd = open(name, O_RDONLY | O_NONBLOCK | O_BINARY);
861 unsigned long fflags;
862 int r = ioctl(fd, EXT2_IOC_GETFLAGS, &fflags);
864 if (r >= 0 && (fflags & EXT2_NODUMP_FL))
871 * If the user vetoes this file/directory, skip it.
872 * We want this to be fairly late; if some other
873 * check would veto this file, we shouldn't bother
876 if (bsdtar->option_interactive &&
877 !yes("add '%s'", name))
884 * Rewrite the pathname to be archived. If rewrite
885 * fails, skip the entry.
887 if (edit_pathname(bsdtar, entry))
890 /* Display entry as we process it.
891 * This format is required by SUSv2. */
893 safe_fprintf(stderr, "a %s",
894 archive_entry_pathname(entry));
896 /* Non-regular files get archived with zero size. */
897 if (archive_entry_filetype(entry) != AE_IFREG)
898 archive_entry_set_size(entry, 0);
900 archive_entry_linkify(bsdtar->resolver, &entry, &spare_entry);
902 while (entry != NULL) {
903 write_entry_backend(bsdtar, a, entry);
904 archive_entry_free(entry);
910 fprintf(stderr, "\n");
912 archive_entry_free(entry);
917 * Backend for write_entry.
920 write_entry_backend(struct bsdtar *bsdtar, struct archive *a,
921 struct archive_entry *entry)
926 if (archive_entry_size(entry) > 0) {
927 const char *pathname = archive_entry_sourcepath(entry);
928 fd = open(pathname, O_RDONLY | O_BINARY);
930 bsdtar->return_value = 1;
931 if (!bsdtar->verbose)
933 "%s: could not open file", pathname);
935 fprintf(stderr, ": %s", strerror(errno));
940 e = archive_write_header(a, entry);
941 if (e != ARCHIVE_OK) {
942 if (!bsdtar->verbose)
943 lafe_warnc(0, "%s: %s",
944 archive_entry_pathname(entry),
945 archive_error_string(a));
947 fprintf(stderr, ": %s", archive_error_string(a));
950 if (e == ARCHIVE_FATAL)
954 * If we opened a file earlier, write it out now. Note that
955 * the format handler might have reset the size field to zero
956 * to inform us that the archive body won't get stored. In
957 * that case, just skip the write.
959 if (e >= ARCHIVE_WARN && fd >= 0 && archive_entry_size(entry) > 0) {
960 if (write_file_data(bsdtar, a, entry, fd))
965 * If we opened a file, close it now even if there was an error
966 * which made us decide not to write the archive body.
973 report_write(struct bsdtar *bsdtar, struct archive *a,
974 struct archive_entry *entry, int64_t progress)
976 uint64_t comp, uncomp;
980 fprintf(stderr, "\n");
981 comp = archive_position_compressed(a);
982 uncomp = archive_position_uncompressed(a);
983 fprintf(stderr, "In: %d files, %s bytes;",
984 archive_file_count(a), tar_i64toa(uncomp));
988 compression = (int)((uncomp - comp) * 100 / uncomp);
990 " Out: %s bytes, compression %d%%\n",
991 tar_i64toa(comp), compression);
992 /* Can't have two calls to tar_i64toa() pending, so split the output. */
993 safe_fprintf(stderr, "Current: %s (%s",
994 archive_entry_pathname(entry),
995 tar_i64toa(progress));
996 fprintf(stderr, "/%s bytes)\n",
997 tar_i64toa(archive_entry_size(entry)));
1001 /* Helper function to copy file to archive. */
1003 write_file_data(struct bsdtar *bsdtar, struct archive *a,
1004 struct archive_entry *entry, int fd)
1007 ssize_t bytes_written;
1008 int64_t progress = 0;
1010 bytes_read = read(fd, bsdtar->buff, FILEDATABUFLEN);
1011 while (bytes_read > 0) {
1013 report_write(bsdtar, a, entry, progress);
1015 bytes_written = archive_write_data(a, bsdtar->buff,
1017 if (bytes_written < 0) {
1018 /* Write failed; this is bad */
1019 lafe_warnc(0, "%s", archive_error_string(a));
1022 if (bytes_written < bytes_read) {
1023 /* Write was truncated; warn but continue. */
1025 "%s: Truncated write; file may have grown while being archived.",
1026 archive_entry_pathname(entry));
1029 progress += bytes_written;
1030 bytes_read = read(fd, bsdtar->buff, FILEDATABUFLEN);
1032 if (bytes_read < 0) {
1035 archive_entry_pathname(entry));
1036 bsdtar->return_value = 1;
1042 * Test if the specified file is new enough to include in the archive.
1045 new_enough(struct bsdtar *bsdtar, const char *path, const struct stat *st)
1047 struct archive_dir_entry *p;
1050 * If this file/dir is excluded by a time comparison, skip it.
1052 if (bsdtar->newer_ctime_sec > 0) {
1053 if (st->st_ctime < bsdtar->newer_ctime_sec)
1054 return (0); /* Too old, skip it. */
1055 if (st->st_ctime == bsdtar->newer_ctime_sec
1056 && ARCHIVE_STAT_CTIME_NANOS(st)
1057 <= bsdtar->newer_ctime_nsec)
1058 return (0); /* Too old, skip it. */
1060 if (bsdtar->newer_mtime_sec > 0) {
1061 if (st->st_mtime < bsdtar->newer_mtime_sec)
1062 return (0); /* Too old, skip it. */
1063 if (st->st_mtime == bsdtar->newer_mtime_sec
1064 && ARCHIVE_STAT_MTIME_NANOS(st)
1065 <= bsdtar->newer_mtime_nsec)
1066 return (0); /* Too old, skip it. */
1070 * In -u mode, we only write an entry if it's newer than
1071 * what was already in the archive.
1073 if (bsdtar->archive_dir != NULL &&
1074 bsdtar->archive_dir->head != NULL) {
1075 for (p = bsdtar->archive_dir->head; p != NULL; p = p->next) {
1076 if (pathcmp(path, p->name)==0)
1077 return (p->mtime_sec < st->st_mtime ||
1078 (p->mtime_sec == st->st_mtime &&
1080 < ARCHIVE_STAT_MTIME_NANOS(st)));
1084 /* If the file wasn't rejected, include it. */
1089 * Add an entry to the dir list for 'u' mode.
1091 * XXX TODO: Make this fast.
1094 add_dir_list(struct bsdtar *bsdtar, const char *path,
1095 time_t mtime_sec, int mtime_nsec)
1097 struct archive_dir_entry *p;
1100 * Search entire list to see if this file has appeared before.
1101 * If it has, override the timestamp data.
1103 p = bsdtar->archive_dir->head;
1105 if (strcmp(path, p->name)==0) {
1106 p->mtime_sec = mtime_sec;
1107 p->mtime_nsec = mtime_nsec;
1113 p = malloc(sizeof(*p));
1115 lafe_errc(1, ENOMEM, "Can't read archive directory");
1117 p->name = strdup(path);
1118 if (p->name == NULL)
1119 lafe_errc(1, ENOMEM, "Can't read archive directory");
1120 p->mtime_sec = mtime_sec;
1121 p->mtime_nsec = mtime_nsec;
1123 if (bsdtar->archive_dir->tail == NULL) {
1124 bsdtar->archive_dir->head = bsdtar->archive_dir->tail = p;
1126 bsdtar->archive_dir->tail->next = p;
1127 bsdtar->archive_dir->tail = p;
1132 test_for_append(struct bsdtar *bsdtar)
1136 if (*bsdtar->argv == NULL && bsdtar->names_from_file == NULL)
1137 lafe_errc(1, 0, "no files or directories specified");
1138 if (bsdtar->filename == NULL)
1139 lafe_errc(1, 0, "Cannot append to stdout.");
1141 if (bsdtar->create_compression != 0)
1143 "Cannot append to %s with compression", bsdtar->filename);
1145 if (stat(bsdtar->filename, &s) != 0)
1148 if (!S_ISREG(s.st_mode) && !S_ISBLK(s.st_mode))
1150 "Cannot append to %s: not a regular file.",
1153 /* Is this an appropriate check here on Windows? */
1155 if (GetFileType(handle) != FILE_TYPE_DISK)
1156 lafe_errc(1, 0, "Cannot append");