2 * Copyright (c) 2003-2007 Tim Kientzle
3 * Copyright (c) 2012 Michihiro NAKAJIMA
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "bsdtar_platform.h"
28 __FBSDID("$FreeBSD: src/usr.bin/tar/write.c,v 1.79 2008/11/27 05:49:52 kientzle Exp $");
30 #ifdef HAVE_SYS_TYPES_H
31 #include <sys/types.h>
33 #ifdef HAVE_SYS_STAT_H
36 #ifdef HAVE_ATTR_XATTR_H
37 #include <attr/xattr.h>
79 #include "line_reader.h"
85 struct archive_dir_entry {
86 struct archive_dir_entry *next;
93 struct archive_dir_entry *head, *tail;
96 static int append_archive(struct bsdtar *, struct archive *,
98 static int append_archive_filename(struct bsdtar *,
99 struct archive *, const char *fname);
100 static void archive_names_from_file(struct bsdtar *bsdtar,
102 static int copy_file_data_block(struct bsdtar *,
103 struct archive *a, struct archive *,
104 struct archive_entry *);
105 static void excluded_callback(struct archive *, void *,
106 struct archive_entry *);
107 static void report_write(struct bsdtar *, struct archive *,
108 struct archive_entry *, int64_t progress);
109 static void test_for_append(struct bsdtar *);
110 static int metadata_filter(struct archive *, void *,
111 struct archive_entry *);
112 static void write_archive(struct archive *, struct bsdtar *);
113 static void write_entry(struct bsdtar *, struct archive *,
114 struct archive_entry *);
115 static void write_file(struct bsdtar *, struct archive *,
116 struct archive_entry *);
117 static void write_hierarchy(struct bsdtar *, struct archive *,
120 #if defined(_WIN32) && !defined(__CYGWIN__)
121 /* Not a full lseek() emulation, but enough for our needs here. */
123 seek_file(int fd, int64_t offset, int whence)
125 LARGE_INTEGER distance;
126 (void)whence; /* UNUSED */
127 distance.QuadPart = offset;
128 return (SetFilePointerEx((HANDLE)_get_osfhandle(fd),
129 distance, NULL, FILE_BEGIN) ? 1 : -1);
134 #define lseek seek_file
138 tar_mode_c(struct bsdtar *bsdtar)
143 if (*bsdtar->argv == NULL && bsdtar->names_from_file == NULL)
144 lafe_errc(1, 0, "no files or directories specified");
146 a = archive_write_new();
148 /* Support any format that the library supports. */
149 if (bsdtar->create_format == NULL) {
150 r = archive_write_set_format_pax_restricted(a);
151 bsdtar->create_format = "pax restricted";
153 r = archive_write_set_format_by_name(a, bsdtar->create_format);
155 if (r != ARCHIVE_OK) {
156 fprintf(stderr, "Can't use format %s: %s\n",
157 bsdtar->create_format,
158 archive_error_string(a));
162 archive_write_set_bytes_per_block(a, bsdtar->bytes_per_block);
163 archive_write_set_bytes_in_last_block(a, bsdtar->bytes_in_last_block);
165 if (bsdtar->compress_program) {
166 archive_write_set_compression_program(a, bsdtar->compress_program);
168 switch (bsdtar->create_compression) {
173 r = archive_write_set_compression_bzip2(a);
176 r = archive_write_set_compression_xz(a);
179 r = archive_write_set_compression_lzip(a);
182 r = archive_write_set_compression_lzma(a);
185 r = archive_write_set_compression_gzip(a);
188 r = archive_write_set_compression_compress(a);
192 "Unrecognized compression option -%c",
193 bsdtar->create_compression);
195 if (r != ARCHIVE_OK) {
197 "Unsupported compression option -%c",
198 bsdtar->create_compression);
202 if (ARCHIVE_OK != archive_write_set_options(a, bsdtar->option_options))
203 lafe_errc(1, 0, "%s", archive_error_string(a));
204 if (ARCHIVE_OK != archive_write_open_file(a, bsdtar->filename))
205 lafe_errc(1, 0, "%s", archive_error_string(a));
206 write_archive(a, bsdtar);
210 * Same as 'c', except we only support tar or empty formats in
211 * uncompressed files on disk.
214 tar_mode_r(struct bsdtar *bsdtar)
219 struct archive_entry *entry;
222 /* Sanity-test some arguments and the file. */
223 test_for_append(bsdtar);
225 format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
227 #if defined(__BORLANDC__)
228 bsdtar->fd = open(bsdtar->filename, O_RDWR | O_CREAT | O_BINARY);
230 bsdtar->fd = open(bsdtar->filename, O_RDWR | O_CREAT | O_BINARY, 0666);
234 "Cannot open %s", bsdtar->filename);
236 a = archive_read_new();
237 archive_read_support_filter_all(a);
238 archive_read_support_format_tar(a);
239 archive_read_support_format_gnutar(a);
240 r = archive_read_open_fd(a, bsdtar->fd, 10240);
242 lafe_errc(1, archive_errno(a),
243 "Can't read archive %s: %s", bsdtar->filename,
244 archive_error_string(a));
245 while (0 == archive_read_next_header(a, &entry)) {
246 if (archive_compression(a) != ARCHIVE_COMPRESSION_NONE) {
247 archive_read_free(a);
250 "Cannot append to compressed archive.");
252 /* Keep going until we hit end-of-archive */
253 format = archive_format(a);
256 end_offset = archive_read_header_position(a);
257 archive_read_free(a);
259 /* Re-open archive for writing */
260 a = archive_write_new();
262 * Set the format to be used for writing. To allow people to
263 * extend empty files, we need to allow them to specify the format,
264 * which opens the possibility that they will specify a format that
265 * doesn't match the existing format. Hence, the following bit
266 * of arcane ugliness.
269 if (bsdtar->create_format != NULL) {
270 /* If the user requested a format, use that, but ... */
271 archive_write_set_format_by_name(a,
272 bsdtar->create_format);
273 /* ... complain if it's not compatible. */
274 format &= ARCHIVE_FORMAT_BASE_MASK;
275 if (format != (int)(archive_format(a) & ARCHIVE_FORMAT_BASE_MASK)
276 && format != ARCHIVE_FORMAT_EMPTY) {
278 "Format %s is incompatible with the archive %s.",
279 bsdtar->create_format, bsdtar->filename);
283 * Just preserve the current format, with a little care
284 * for formats that libarchive can't write.
286 if (format == ARCHIVE_FORMAT_EMPTY)
287 format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
288 archive_write_set_format(a, format);
290 if (lseek(bsdtar->fd, end_offset, SEEK_SET) < 0)
291 lafe_errc(1, errno, "Could not seek to archive end");
292 if (ARCHIVE_OK != archive_write_set_options(a, bsdtar->option_options))
293 lafe_errc(1, 0, "%s", archive_error_string(a));
294 if (ARCHIVE_OK != archive_write_open_fd(a, bsdtar->fd))
295 lafe_errc(1, 0, "%s", archive_error_string(a));
297 write_archive(a, bsdtar); /* XXX check return val XXX */
304 tar_mode_u(struct bsdtar *bsdtar)
308 struct archive_entry *entry;
310 struct archive_dir_entry *p;
311 struct archive_dir archive_dir;
313 bsdtar->archive_dir = &archive_dir;
314 memset(&archive_dir, 0, sizeof(archive_dir));
316 format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
318 /* Sanity-test some arguments and the file. */
319 test_for_append(bsdtar);
321 bsdtar->fd = open(bsdtar->filename, O_RDWR | O_BINARY);
324 "Cannot open %s", bsdtar->filename);
326 a = archive_read_new();
327 archive_read_support_filter_all(a);
328 archive_read_support_format_tar(a);
329 archive_read_support_format_gnutar(a);
330 if (archive_read_open_fd(a, bsdtar->fd, bsdtar->bytes_per_block)
333 "Can't open %s: %s", bsdtar->filename,
334 archive_error_string(a));
337 /* Build a list of all entries and their recorded mod times. */
338 while (0 == archive_read_next_header(a, &entry)) {
339 if (archive_compression(a) != ARCHIVE_COMPRESSION_NONE) {
340 archive_read_free(a);
343 "Cannot append to compressed archive.");
345 if (archive_match_exclude_entry(bsdtar->matching,
346 ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_OLDER |
347 ARCHIVE_MATCH_EQUAL, entry) != ARCHIVE_OK)
348 lafe_errc(1, 0, "Error : %s",
349 archive_error_string(bsdtar->matching));
350 /* Record the last format determination we see */
351 format = archive_format(a);
352 /* Keep going until we hit end-of-archive */
355 end_offset = archive_read_header_position(a);
356 archive_read_free(a);
358 /* Re-open archive for writing. */
359 a = archive_write_new();
361 * Set format to same one auto-detected above.
363 archive_write_set_format(a, format);
364 archive_write_set_bytes_per_block(a, bsdtar->bytes_per_block);
365 archive_write_set_bytes_in_last_block(a, bsdtar->bytes_in_last_block);
367 if (lseek(bsdtar->fd, end_offset, SEEK_SET) < 0)
368 lafe_errc(1, errno, "Could not seek to archive end");
369 if (ARCHIVE_OK != archive_write_set_options(a, bsdtar->option_options))
370 lafe_errc(1, 0, "%s", archive_error_string(a));
371 if (ARCHIVE_OK != archive_write_open_fd(a, bsdtar->fd))
372 lafe_errc(1, 0, "%s", archive_error_string(a));
374 write_archive(a, bsdtar);
379 while (bsdtar->archive_dir->head != NULL) {
380 p = bsdtar->archive_dir->head->next;
381 free(bsdtar->archive_dir->head->name);
382 free(bsdtar->archive_dir->head);
383 bsdtar->archive_dir->head = p;
385 bsdtar->archive_dir->tail = NULL;
390 * Write user-specified files/dirs to opened archive.
393 write_archive(struct archive *a, struct bsdtar *bsdtar)
396 struct archive_entry *entry, *sparse_entry;
398 /* Choose a suitable copy buffer size */
399 bsdtar->buff_size = 64 * 1024;
400 while (bsdtar->buff_size < (size_t)bsdtar->bytes_per_block)
401 bsdtar->buff_size *= 2;
402 /* Try to compensate for space we'll lose to alignment. */
403 bsdtar->buff_size += 16 * 1024;
405 /* Allocate a buffer for file data. */
406 if ((bsdtar->buff = malloc(bsdtar->buff_size)) == NULL)
407 lafe_errc(1, 0, "cannot allocate memory");
409 if ((bsdtar->resolver = archive_entry_linkresolver_new()) == NULL)
410 lafe_errc(1, 0, "cannot create link resolver");
411 archive_entry_linkresolver_set_strategy(bsdtar->resolver,
414 /* Create a read_disk object. */
415 if ((bsdtar->diskreader = archive_read_disk_new()) == NULL)
416 lafe_errc(1, 0, "Cannot create read_disk object");
417 /* Tell the read_disk how handle symlink. */
418 switch (bsdtar->symlink_mode) {
420 archive_read_disk_set_symlink_hybrid(bsdtar->diskreader);
423 archive_read_disk_set_symlink_logical(bsdtar->diskreader);
426 archive_read_disk_set_symlink_physical(bsdtar->diskreader);
429 /* Register entry filters. */
430 archive_read_disk_set_matching(bsdtar->diskreader,
431 bsdtar->matching, excluded_callback, bsdtar);
432 archive_read_disk_set_metadata_filter_callback(
433 bsdtar->diskreader, metadata_filter, bsdtar);
434 /* Set the behavior of archive_read_disk. */
435 archive_read_disk_set_behavior(bsdtar->diskreader,
436 bsdtar->readdisk_flags);
437 archive_read_disk_set_standard_lookup(bsdtar->diskreader);
439 if (bsdtar->names_from_file != NULL)
440 archive_names_from_file(bsdtar, a);
442 while (*bsdtar->argv) {
444 if (arg[0] == '-' && arg[1] == 'C') {
451 "Missing argument for -C");
452 bsdtar->return_value = 1;
457 "Meaningless argument for -C: ''");
458 bsdtar->return_value = 1;
462 set_chdir(bsdtar, arg);
464 if (*arg != '/' && (arg[0] != '@' || arg[1] != '/'))
465 do_chdir(bsdtar); /* Handle a deferred -C */
467 if (append_archive_filename(bsdtar, a,
471 write_hierarchy(bsdtar, a, arg);
476 archive_read_disk_set_matching(bsdtar->diskreader, NULL, NULL, NULL);
477 archive_read_disk_set_metadata_filter_callback(
478 bsdtar->diskreader, NULL, NULL);
480 archive_entry_linkify(bsdtar->resolver, &entry, &sparse_entry);
481 while (entry != NULL) {
483 struct archive_entry *entry2;
484 struct archive *disk = bsdtar->diskreader;
487 * This tricky code here is to correctly read the cotents
488 * of the entry because the disk reader bsdtar->diskreader
489 * is pointing at does not have any information about the
490 * entry by this time and using archive_read_data_block()
491 * with the disk reader consequently must fail. And we
492 * have to re-open the entry to read the contents.
494 /* TODO: Work with -C option as well. */
495 r = archive_read_disk_open(disk,
496 archive_entry_sourcepath(entry));
497 if (r != ARCHIVE_OK) {
498 lafe_warnc(archive_errno(disk),
499 "%s", archive_error_string(disk));
500 bsdtar->return_value = 1;
501 archive_entry_free(entry);
506 * Invoke archive_read_next_header2() to work
507 * archive_read_data_block(), which is called via write_file(),
510 entry2 = archive_entry_new();
511 r = archive_read_next_header2(disk, entry2);
512 archive_entry_free(entry2);
513 if (r != ARCHIVE_OK) {
514 lafe_warnc(archive_errno(disk),
515 "%s", archive_error_string(disk));
516 if (r == ARCHIVE_FATAL)
517 bsdtar->return_value = 1;
519 archive_read_close(disk);
520 archive_entry_free(entry);
524 write_file(bsdtar, a, entry);
525 archive_entry_free(entry);
526 archive_read_close(disk);
528 archive_entry_linkify(bsdtar->resolver, &entry, &sparse_entry);
531 if (archive_write_close(a)) {
532 lafe_warnc(0, "%s", archive_error_string(a));
533 bsdtar->return_value = 1;
537 /* Free file data buffer. */
539 archive_entry_linkresolver_free(bsdtar->resolver);
540 bsdtar->resolver = NULL;
541 archive_read_free(bsdtar->diskreader);
542 bsdtar->diskreader = NULL;
544 if (bsdtar->option_totals) {
545 fprintf(stderr, "Total bytes written: %s\n",
546 tar_i64toa(archive_position_compressed(a)));
549 archive_write_free(a);
553 * Archive names specified in file.
555 * Unless --null was specified, a line containing exactly "-C" will
556 * cause the next line to be a directory to pass to chdir(). If
557 * --null is specified, then a line "-C" is just another filename.
560 archive_names_from_file(struct bsdtar *bsdtar, struct archive *a)
562 struct lafe_line_reader *lr;
565 bsdtar->next_line_is_dir = 0;
567 lr = lafe_line_reader(bsdtar->names_from_file, bsdtar->option_null);
568 while ((line = lafe_line_reader_next(lr)) != NULL) {
569 if (bsdtar->next_line_is_dir) {
571 set_chdir(bsdtar, line);
574 "Meaningless argument for -C: ''");
575 bsdtar->return_value = 1;
577 bsdtar->next_line_is_dir = 0;
578 } else if (!bsdtar->option_null && strcmp(line, "-C") == 0)
579 bsdtar->next_line_is_dir = 1;
582 do_chdir(bsdtar); /* Handle a deferred -C */
583 write_hierarchy(bsdtar, a, line);
586 lafe_line_reader_free(lr);
587 if (bsdtar->next_line_is_dir)
589 "Unexpected end of filename list; "
590 "directory expected after -C");
594 * Copy from specified archive to current archive. Returns non-zero
595 * for write errors (which force us to terminate the entire archiving
596 * operation). If there are errors reading the input archive, we set
597 * bsdtar->return_value but return zero, so the overall archiving
598 * operation will complete and return non-zero.
601 append_archive_filename(struct bsdtar *bsdtar, struct archive *a,
602 const char *raw_filename)
605 const char *filename = raw_filename;
608 if (strcmp(filename, "-") == 0)
609 filename = NULL; /* Library uses NULL for stdio. */
611 ina = archive_read_new();
612 archive_read_support_format_all(ina);
613 archive_read_support_filter_all(ina);
614 if (archive_read_open_file(ina, filename, bsdtar->bytes_per_block)) {
615 lafe_warnc(0, "%s", archive_error_string(ina));
616 bsdtar->return_value = 1;
620 rc = append_archive(bsdtar, a, ina);
622 if (rc != ARCHIVE_OK) {
623 lafe_warnc(0, "Error reading archive %s: %s",
624 raw_filename, archive_error_string(ina));
625 bsdtar->return_value = 1;
627 archive_read_free(ina);
633 append_archive(struct bsdtar *bsdtar, struct archive *a, struct archive *ina)
635 struct archive_entry *in_entry;
638 while (ARCHIVE_OK == (e = archive_read_next_header(ina, &in_entry))) {
639 if (archive_match_excluded(bsdtar->matching, in_entry))
641 if (bsdtar->option_interactive &&
642 !yes("copy '%s'", archive_entry_pathname(in_entry)))
645 safe_fprintf(stderr, "a %s",
646 archive_entry_pathname(in_entry));
648 report_write(bsdtar, a, in_entry, 0);
650 e = archive_write_header(a, in_entry);
651 if (e != ARCHIVE_OK) {
652 if (!bsdtar->verbose)
653 lafe_warnc(0, "%s: %s",
654 archive_entry_pathname(in_entry),
655 archive_error_string(a));
657 fprintf(stderr, ": %s", archive_error_string(a));
659 if (e == ARCHIVE_FATAL)
662 if (e >= ARCHIVE_WARN) {
663 if (archive_entry_size(in_entry) == 0)
664 archive_read_data_skip(ina);
665 else if (copy_file_data_block(bsdtar, a, ina, in_entry))
670 fprintf(stderr, "\n");
673 return (e == ARCHIVE_EOF ? ARCHIVE_OK : e);
676 /* Helper function to copy file to archive. */
678 copy_file_data_block(struct bsdtar *bsdtar, struct archive *a,
679 struct archive *in_a, struct archive_entry *entry)
682 ssize_t bytes_written;
683 int64_t offset, progress = 0;
684 char *null_buff = NULL;
688 while ((r = archive_read_data_block(in_a, &buff,
689 &bytes_read, &offset)) == ARCHIVE_OK) {
691 report_write(bsdtar, a, entry, progress);
693 if (offset > progress) {
694 int64_t sparse = offset - progress;
697 if (null_buff == NULL) {
698 null_buff = bsdtar->buff;
699 memset(null_buff, 0, bsdtar->buff_size);
703 if (sparse > (int64_t)bsdtar->buff_size)
704 ns = bsdtar->buff_size;
708 archive_write_data(a, null_buff, ns);
709 if (bytes_written < 0) {
710 /* Write failed; this is bad */
712 archive_error_string(a));
715 if ((size_t)bytes_written < ns) {
716 /* Write was truncated; warn but
719 "%s: Truncated write; file may "
720 "have grown while being archived.",
721 archive_entry_pathname(entry));
724 progress += bytes_written;
725 sparse -= bytes_written;
729 bytes_written = archive_write_data(a, buff, bytes_read);
730 if (bytes_written < 0) {
731 /* Write failed; this is bad */
732 lafe_warnc(0, "%s", archive_error_string(a));
735 if ((size_t)bytes_written < bytes_read) {
736 /* Write was truncated; warn but continue. */
738 "%s: Truncated write; file may have grown "
739 "while being archived.",
740 archive_entry_pathname(entry));
743 progress += bytes_written;
745 if (r < ARCHIVE_WARN) {
746 lafe_warnc(archive_errno(a), "%s", archive_error_string(a));
753 excluded_callback(struct archive *a, void *_data, struct archive_entry *entry)
755 struct bsdtar *bsdtar = (struct bsdtar *)_data;
757 if (bsdtar->option_no_subdirs)
759 if (!archive_read_disk_can_descend(a))
761 if (bsdtar->option_interactive &&
762 !yes("add '%s'", archive_entry_pathname(entry)))
764 archive_read_disk_descend(a);
768 metadata_filter(struct archive *a, void *_data, struct archive_entry *entry)
770 struct bsdtar *bsdtar = (struct bsdtar *)_data;
772 /* XXX TODO: check whether this filesystem is
773 * synthetic and/or local. Add a new
774 * --local-only option to skip non-local
775 * filesystems. Skip synthetic filesystems
778 * The results should be cached, since
779 * tree.c doesn't usually visit a directory
780 * and the directory contents together. A simple
781 * move-to-front list should perform quite well.
783 * Use archive_read_disk_current_filesystem_is_remote().
787 * If the user vetoes this file/directory, skip it.
788 * We want this to be fairly late; if some other
789 * check would veto this file, we shouldn't bother
792 if (bsdtar->option_interactive &&
793 !yes("add '%s'", archive_entry_pathname(entry)))
796 /* Note: if user vetoes, we won't descend. */
797 if (!bsdtar->option_no_subdirs && archive_read_disk_can_descend(a))
798 archive_read_disk_descend(a);
804 * Add the file or dir hierarchy named by 'path' to the archive
807 write_hierarchy(struct bsdtar *bsdtar, struct archive *a, const char *path)
809 struct archive *disk = bsdtar->diskreader;
810 struct archive_entry *entry = NULL, *spare_entry = NULL;
813 r = archive_read_disk_open(disk, path);
814 if (r != ARCHIVE_OK) {
815 lafe_warnc(archive_errno(disk),
816 "%s", archive_error_string(disk));
817 bsdtar->return_value = 1;
820 bsdtar->first_fs = -1;
823 archive_entry_free(entry);
824 entry = archive_entry_new();
825 r = archive_read_next_header2(disk, entry);
826 if (r == ARCHIVE_EOF)
828 else if (r != ARCHIVE_OK) {
829 lafe_warnc(archive_errno(disk),
830 "%s", archive_error_string(disk));
831 if (r == ARCHIVE_FATAL) {
832 bsdtar->return_value = 1;
834 } else if (r < ARCHIVE_WARN)
838 if (bsdtar->uid >= 0) {
839 archive_entry_set_uid(entry, bsdtar->uid);
841 archive_entry_set_uname(entry,
842 archive_read_disk_uname(bsdtar->diskreader,
845 if (bsdtar->gid >= 0) {
846 archive_entry_set_gid(entry, bsdtar->gid);
848 archive_entry_set_gname(entry,
849 archive_read_disk_gname(bsdtar->diskreader,
853 archive_entry_set_uname(entry, bsdtar->uname);
855 archive_entry_set_gname(entry, bsdtar->gname);
858 * Rewrite the pathname to be archived. If rewrite
859 * fails, skip the entry.
861 if (edit_pathname(bsdtar, entry))
864 /* Display entry as we process it.
865 * This format is required by SUSv2. */
867 safe_fprintf(stderr, "a %s",
868 archive_entry_pathname(entry));
870 /* Non-regular files get archived with zero size. */
871 if (archive_entry_filetype(entry) != AE_IFREG)
872 archive_entry_set_size(entry, 0);
874 archive_entry_linkify(bsdtar->resolver, &entry, &spare_entry);
876 while (entry != NULL) {
877 write_file(bsdtar, a, entry);
878 archive_entry_free(entry);
884 fprintf(stderr, "\n");
886 archive_entry_free(entry);
887 archive_read_close(disk);
891 * Write a single file (or directory or other filesystem object) to
895 write_file(struct bsdtar *bsdtar, struct archive *a,
896 struct archive_entry *entry)
898 write_entry(bsdtar, a, entry);
902 * Write a single entry to the archive.
905 write_entry(struct bsdtar *bsdtar, struct archive *a,
906 struct archive_entry *entry)
910 e = archive_write_header(a, entry);
911 if (e != ARCHIVE_OK) {
912 if (!bsdtar->verbose)
913 lafe_warnc(0, "%s: %s",
914 archive_entry_pathname(entry),
915 archive_error_string(a));
917 fprintf(stderr, ": %s", archive_error_string(a));
920 if (e == ARCHIVE_FATAL)
924 * If we opened a file earlier, write it out now. Note that
925 * the format handler might have reset the size field to zero
926 * to inform us that the archive body won't get stored. In
927 * that case, just skip the write.
929 if (e >= ARCHIVE_WARN && archive_entry_size(entry) > 0) {
930 if (copy_file_data_block(bsdtar, a, bsdtar->diskreader, entry))
936 report_write(struct bsdtar *bsdtar, struct archive *a,
937 struct archive_entry *entry, int64_t progress)
939 uint64_t comp, uncomp;
943 fprintf(stderr, "\n");
944 comp = archive_position_compressed(a);
945 uncomp = archive_position_uncompressed(a);
946 fprintf(stderr, "In: %d files, %s bytes;",
947 archive_file_count(a), tar_i64toa(uncomp));
951 compression = (int)((uncomp - comp) * 100 / uncomp);
953 " Out: %s bytes, compression %d%%\n",
954 tar_i64toa(comp), compression);
955 /* Can't have two calls to tar_i64toa() pending, so split the output. */
956 safe_fprintf(stderr, "Current: %s (%s",
957 archive_entry_pathname(entry),
958 tar_i64toa(progress));
959 fprintf(stderr, "/%s bytes)\n",
960 tar_i64toa(archive_entry_size(entry)));
964 test_for_append(struct bsdtar *bsdtar)
968 if (*bsdtar->argv == NULL && bsdtar->names_from_file == NULL)
969 lafe_errc(1, 0, "no files or directories specified");
970 if (bsdtar->filename == NULL)
971 lafe_errc(1, 0, "Cannot append to stdout.");
973 if (bsdtar->create_compression != 0)
975 "Cannot append to %s with compression", bsdtar->filename);
977 if (stat(bsdtar->filename, &s) != 0)
980 if (!S_ISREG(s.st_mode) && !S_ISBLK(s.st_mode))
982 "Cannot append to %s: not a regular file.",
985 /* Is this an appropriate check here on Windows? */
987 if (GetFileType(handle) != FILE_TYPE_DISK)
988 lafe_errc(1, 0, "Cannot append");