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);
137 #define lseek seek_file
141 set_writer_options(struct bsdtar *bsdtar, struct archive *a)
143 const char *writer_options;
146 writer_options = getenv(ENV_WRITER_OPTIONS);
147 if (writer_options != NULL) {
149 /* Set default write options. */
150 p = malloc(sizeof(IGNORE_WRONG_MODULE_NAME)
151 + strlen(writer_options) + 1);
153 lafe_errc(1, errno, "Out of memory");
154 /* Prepend magic code to ignore options for
155 * a format or filters which are not added to
156 * the archive write object. */
157 strncpy(p, IGNORE_WRONG_MODULE_NAME,
158 sizeof(IGNORE_WRONG_MODULE_NAME) -1);
159 strcpy(p + sizeof(IGNORE_WRONG_MODULE_NAME) -1, writer_options);
160 r = archive_write_set_options(a, p);
162 if (r < ARCHIVE_WARN)
163 lafe_errc(1, 0, "%s", archive_error_string(a));
165 archive_clear_error(a);
167 if (ARCHIVE_OK != archive_write_set_options(a, bsdtar->option_options))
168 lafe_errc(1, 0, "%s", archive_error_string(a));
172 set_reader_options(struct bsdtar *bsdtar, struct archive *a)
174 const char *reader_options;
177 (void)bsdtar; /* UNUSED */
179 reader_options = getenv(ENV_READER_OPTIONS);
180 if (reader_options != NULL) {
182 /* Set default write options. */
183 p = malloc(sizeof(IGNORE_WRONG_MODULE_NAME)
184 + strlen(reader_options) + 1);
186 lafe_errc(1, errno, "Out of memory");
187 /* Prepend magic code to ignore options for
188 * a format or filters which are not added to
189 * the archive write object. */
190 strncpy(p, IGNORE_WRONG_MODULE_NAME,
191 sizeof(IGNORE_WRONG_MODULE_NAME) -1);
192 strcpy(p + sizeof(IGNORE_WRONG_MODULE_NAME) -1, reader_options);
193 r = archive_read_set_options(a, p);
195 if (r < ARCHIVE_WARN)
196 lafe_errc(1, 0, "%s", archive_error_string(a));
198 archive_clear_error(a);
203 tar_mode_c(struct bsdtar *bsdtar)
206 const void *filter_name;
209 if (*bsdtar->argv == NULL && bsdtar->names_from_file == NULL)
210 lafe_errc(1, 0, "no files or directories specified");
212 a = archive_write_new();
214 /* Support any format that the library supports. */
215 if (cset_get_format(bsdtar->cset) == NULL) {
216 r = archive_write_set_format_pax_restricted(a);
217 cset_set_format(bsdtar->cset, "pax restricted");
219 r = archive_write_set_format_by_name(a,
220 cset_get_format(bsdtar->cset));
222 if (r != ARCHIVE_OK) {
223 fprintf(stderr, "Can't use format %s: %s\n",
224 cset_get_format(bsdtar->cset),
225 archive_error_string(a));
229 archive_write_set_bytes_per_block(a, bsdtar->bytes_per_block);
230 archive_write_set_bytes_in_last_block(a, bsdtar->bytes_in_last_block);
232 r = cset_write_add_filters(bsdtar->cset, a, &filter_name);
233 if (r < ARCHIVE_WARN) {
234 lafe_errc(1, 0, "Unsupported compression option --%s",
235 (const char *)filter_name);
238 set_writer_options(bsdtar, a);
239 if (bsdtar->passphrase != NULL)
240 r = archive_write_set_passphrase(a, bsdtar->passphrase);
242 r = archive_write_set_passphrase_callback(a, bsdtar,
243 &passphrase_callback);
245 lafe_errc(1, 0, "%s", archive_error_string(a));
246 if (ARCHIVE_OK != archive_write_open_filename(a, bsdtar->filename))
247 lafe_errc(1, 0, "%s", archive_error_string(a));
248 write_archive(a, bsdtar);
252 * Same as 'c', except we only support tar or empty formats in
253 * uncompressed files on disk.
256 tar_mode_r(struct bsdtar *bsdtar)
261 struct archive_entry *entry;
264 /* Sanity-test some arguments and the file. */
265 test_for_append(bsdtar);
267 format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
269 #if defined(__BORLANDC__)
270 bsdtar->fd = open(bsdtar->filename, O_RDWR | O_CREAT | O_BINARY);
272 bsdtar->fd = open(bsdtar->filename, O_RDWR | O_CREAT | O_BINARY, 0666);
276 "Cannot open %s", bsdtar->filename);
278 a = archive_read_new();
279 archive_read_support_filter_all(a);
280 archive_read_support_format_empty(a);
281 archive_read_support_format_tar(a);
282 archive_read_support_format_gnutar(a);
283 set_reader_options(bsdtar, a);
284 r = archive_read_open_fd(a, bsdtar->fd, 10240);
286 lafe_errc(1, archive_errno(a),
287 "Can't read archive %s: %s", bsdtar->filename,
288 archive_error_string(a));
289 while (0 == archive_read_next_header(a, &entry)) {
290 if (archive_filter_code(a, 0) != ARCHIVE_FILTER_NONE) {
291 archive_read_free(a);
294 "Cannot append to compressed archive.");
296 /* Keep going until we hit end-of-archive */
297 format = archive_format(a);
300 end_offset = archive_read_header_position(a);
301 archive_read_free(a);
303 /* Re-open archive for writing */
304 a = archive_write_new();
306 * Set the format to be used for writing. To allow people to
307 * extend empty files, we need to allow them to specify the format,
308 * which opens the possibility that they will specify a format that
309 * doesn't match the existing format. Hence, the following bit
310 * of arcane ugliness.
313 if (cset_get_format(bsdtar->cset) != NULL) {
314 /* If the user requested a format, use that, but ... */
315 archive_write_set_format_by_name(a,
316 cset_get_format(bsdtar->cset));
317 /* ... complain if it's not compatible. */
318 format &= ARCHIVE_FORMAT_BASE_MASK;
319 if (format != (int)(archive_format(a) & ARCHIVE_FORMAT_BASE_MASK)
320 && format != ARCHIVE_FORMAT_EMPTY) {
322 "Format %s is incompatible with the archive %s.",
323 cset_get_format(bsdtar->cset), bsdtar->filename);
327 * Just preserve the current format, with a little care
328 * for formats that libarchive can't write.
330 if (format == ARCHIVE_FORMAT_EMPTY)
331 format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
332 archive_write_set_format(a, format);
334 if (lseek(bsdtar->fd, end_offset, SEEK_SET) < 0)
335 lafe_errc(1, errno, "Could not seek to archive end");
336 set_writer_options(bsdtar, a);
337 if (ARCHIVE_OK != archive_write_open_fd(a, bsdtar->fd))
338 lafe_errc(1, 0, "%s", archive_error_string(a));
340 write_archive(a, bsdtar); /* XXX check return val XXX */
347 tar_mode_u(struct bsdtar *bsdtar)
351 struct archive_entry *entry;
353 struct archive_dir_entry *p;
354 struct archive_dir archive_dir;
356 bsdtar->archive_dir = &archive_dir;
357 memset(&archive_dir, 0, sizeof(archive_dir));
359 format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
361 /* Sanity-test some arguments and the file. */
362 test_for_append(bsdtar);
364 bsdtar->fd = open(bsdtar->filename, O_RDWR | O_BINARY);
367 "Cannot open %s", bsdtar->filename);
369 a = archive_read_new();
370 archive_read_support_filter_all(a);
371 archive_read_support_format_tar(a);
372 archive_read_support_format_gnutar(a);
373 set_reader_options(bsdtar, a);
374 if (archive_read_open_fd(a, bsdtar->fd, bsdtar->bytes_per_block)
377 "Can't open %s: %s", bsdtar->filename,
378 archive_error_string(a));
381 /* Build a list of all entries and their recorded mod times. */
382 while (0 == archive_read_next_header(a, &entry)) {
383 if (archive_filter_code(a, 0) != ARCHIVE_FILTER_NONE) {
384 archive_read_free(a);
387 "Cannot append to compressed archive.");
389 if (archive_match_exclude_entry(bsdtar->matching,
390 ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_OLDER |
391 ARCHIVE_MATCH_EQUAL, entry) != ARCHIVE_OK)
392 lafe_errc(1, 0, "Error : %s",
393 archive_error_string(bsdtar->matching));
394 /* Record the last format determination we see */
395 format = archive_format(a);
396 /* Keep going until we hit end-of-archive */
399 end_offset = archive_read_header_position(a);
400 archive_read_free(a);
402 /* Re-open archive for writing. */
403 a = archive_write_new();
405 * Set format to same one auto-detected above.
407 archive_write_set_format(a, format);
408 archive_write_set_bytes_per_block(a, bsdtar->bytes_per_block);
409 archive_write_set_bytes_in_last_block(a, bsdtar->bytes_in_last_block);
411 if (lseek(bsdtar->fd, end_offset, SEEK_SET) < 0)
412 lafe_errc(1, errno, "Could not seek to archive end");
413 set_writer_options(bsdtar, a);
414 if (ARCHIVE_OK != archive_write_open_fd(a, bsdtar->fd))
415 lafe_errc(1, 0, "%s", archive_error_string(a));
417 write_archive(a, bsdtar);
422 while (bsdtar->archive_dir->head != NULL) {
423 p = bsdtar->archive_dir->head->next;
424 free(bsdtar->archive_dir->head->name);
425 free(bsdtar->archive_dir->head);
426 bsdtar->archive_dir->head = p;
428 bsdtar->archive_dir->tail = NULL;
433 * Write user-specified files/dirs to opened archive.
436 write_archive(struct archive *a, struct bsdtar *bsdtar)
439 struct archive_entry *entry, *sparse_entry;
441 /* Choose a suitable copy buffer size */
442 bsdtar->buff_size = 64 * 1024;
443 while (bsdtar->buff_size < (size_t)bsdtar->bytes_per_block)
444 bsdtar->buff_size *= 2;
445 /* Try to compensate for space we'll lose to alignment. */
446 bsdtar->buff_size += 16 * 1024;
448 /* Allocate a buffer for file data. */
449 if ((bsdtar->buff = malloc(bsdtar->buff_size)) == NULL)
450 lafe_errc(1, 0, "cannot allocate memory");
452 if ((bsdtar->resolver = archive_entry_linkresolver_new()) == NULL)
453 lafe_errc(1, 0, "cannot create link resolver");
454 archive_entry_linkresolver_set_strategy(bsdtar->resolver,
457 /* Create a read_disk object. */
458 if ((bsdtar->diskreader = archive_read_disk_new()) == NULL)
459 lafe_errc(1, 0, "Cannot create read_disk object");
460 /* Tell the read_disk how handle symlink. */
461 switch (bsdtar->symlink_mode) {
463 archive_read_disk_set_symlink_hybrid(bsdtar->diskreader);
466 archive_read_disk_set_symlink_logical(bsdtar->diskreader);
469 archive_read_disk_set_symlink_physical(bsdtar->diskreader);
472 /* Register entry filters. */
473 archive_read_disk_set_matching(bsdtar->diskreader,
474 bsdtar->matching, excluded_callback, bsdtar);
475 archive_read_disk_set_metadata_filter_callback(
476 bsdtar->diskreader, metadata_filter, bsdtar);
477 /* Set the behavior of archive_read_disk. */
478 archive_read_disk_set_behavior(bsdtar->diskreader,
479 bsdtar->readdisk_flags);
480 archive_read_disk_set_standard_lookup(bsdtar->diskreader);
482 if (bsdtar->names_from_file != NULL)
483 archive_names_from_file(bsdtar, a);
485 while (*bsdtar->argv) {
487 if (arg[0] == '-' && arg[1] == 'C') {
494 "Missing argument for -C");
495 bsdtar->return_value = 1;
500 "Meaningless argument for -C: ''");
501 bsdtar->return_value = 1;
505 set_chdir(bsdtar, arg);
507 if (*arg != '/' && (arg[0] != '@' || arg[1] != '/'))
508 do_chdir(bsdtar); /* Handle a deferred -C */
510 if (append_archive_filename(bsdtar, a,
514 write_hierarchy(bsdtar, a, arg);
519 archive_read_disk_set_matching(bsdtar->diskreader, NULL, NULL, NULL);
520 archive_read_disk_set_metadata_filter_callback(
521 bsdtar->diskreader, NULL, NULL);
523 archive_entry_linkify(bsdtar->resolver, &entry, &sparse_entry);
524 while (entry != NULL) {
526 struct archive_entry *entry2;
527 struct archive *disk = bsdtar->diskreader;
530 * This tricky code here is to correctly read the cotents
531 * of the entry because the disk reader bsdtar->diskreader
532 * is pointing at does not have any information about the
533 * entry by this time and using archive_read_data_block()
534 * with the disk reader consequently must fail. And we
535 * have to re-open the entry to read the contents.
537 /* TODO: Work with -C option as well. */
538 r = archive_read_disk_open(disk,
539 archive_entry_sourcepath(entry));
540 if (r != ARCHIVE_OK) {
541 lafe_warnc(archive_errno(disk),
542 "%s", archive_error_string(disk));
543 bsdtar->return_value = 1;
544 archive_entry_free(entry);
549 * Invoke archive_read_next_header2() to work
550 * archive_read_data_block(), which is called via write_file(),
553 entry2 = archive_entry_new();
554 r = archive_read_next_header2(disk, entry2);
555 archive_entry_free(entry2);
556 if (r != ARCHIVE_OK) {
557 lafe_warnc(archive_errno(disk),
558 "%s", archive_error_string(disk));
559 if (r == ARCHIVE_FATAL)
560 bsdtar->return_value = 1;
562 archive_read_close(disk);
563 archive_entry_free(entry);
567 write_file(bsdtar, a, entry);
568 archive_entry_free(entry);
569 archive_read_close(disk);
571 archive_entry_linkify(bsdtar->resolver, &entry, &sparse_entry);
574 if (archive_write_close(a)) {
575 lafe_warnc(0, "%s", archive_error_string(a));
576 bsdtar->return_value = 1;
580 /* Free file data buffer. */
582 archive_entry_linkresolver_free(bsdtar->resolver);
583 bsdtar->resolver = NULL;
584 archive_read_free(bsdtar->diskreader);
585 bsdtar->diskreader = NULL;
587 if (bsdtar->option_totals) {
588 fprintf(stderr, "Total bytes written: %s\n",
589 tar_i64toa(archive_filter_bytes(a, -1)));
592 archive_write_free(a);
596 * Archive names specified in file.
598 * Unless --null was specified, a line containing exactly "-C" will
599 * cause the next line to be a directory to pass to chdir(). If
600 * --null is specified, then a line "-C" is just another filename.
603 archive_names_from_file(struct bsdtar *bsdtar, struct archive *a)
605 struct lafe_line_reader *lr;
608 bsdtar->next_line_is_dir = 0;
610 lr = lafe_line_reader(bsdtar->names_from_file, bsdtar->option_null);
611 while ((line = lafe_line_reader_next(lr)) != NULL) {
612 if (bsdtar->next_line_is_dir) {
614 set_chdir(bsdtar, line);
617 "Meaningless argument for -C: ''");
618 bsdtar->return_value = 1;
620 bsdtar->next_line_is_dir = 0;
621 } else if (!bsdtar->option_null && strcmp(line, "-C") == 0)
622 bsdtar->next_line_is_dir = 1;
625 do_chdir(bsdtar); /* Handle a deferred -C */
626 write_hierarchy(bsdtar, a, line);
629 lafe_line_reader_free(lr);
630 if (bsdtar->next_line_is_dir)
632 "Unexpected end of filename list; "
633 "directory expected after -C");
637 * Copy from specified archive to current archive. Returns non-zero
638 * for write errors (which force us to terminate the entire archiving
639 * operation). If there are errors reading the input archive, we set
640 * bsdtar->return_value but return zero, so the overall archiving
641 * operation will complete and return non-zero.
644 append_archive_filename(struct bsdtar *bsdtar, struct archive *a,
645 const char *raw_filename)
648 const char *filename = raw_filename;
651 if (strcmp(filename, "-") == 0)
652 filename = NULL; /* Library uses NULL for stdio. */
654 ina = archive_read_new();
655 archive_read_support_format_all(ina);
656 archive_read_support_filter_all(ina);
657 set_reader_options(bsdtar, ina);
658 archive_read_set_options(ina, "mtree:checkfs");
659 if (bsdtar->passphrase != NULL)
660 rc = archive_read_add_passphrase(a, bsdtar->passphrase);
662 rc = archive_read_set_passphrase_callback(ina, bsdtar,
663 &passphrase_callback);
664 if (rc != ARCHIVE_OK)
665 lafe_errc(1, 0, "%s", archive_error_string(a));
666 if (archive_read_open_filename(ina, filename,
667 bsdtar->bytes_per_block)) {
668 lafe_warnc(0, "%s", archive_error_string(ina));
669 bsdtar->return_value = 1;
673 rc = append_archive(bsdtar, a, ina);
675 if (rc != ARCHIVE_OK) {
676 lafe_warnc(0, "Error reading archive %s: %s",
677 raw_filename, archive_error_string(ina));
678 bsdtar->return_value = 1;
680 archive_read_free(ina);
686 append_archive(struct bsdtar *bsdtar, struct archive *a, struct archive *ina)
688 struct archive_entry *in_entry;
691 while (ARCHIVE_OK == (e = archive_read_next_header(ina, &in_entry))) {
692 if (archive_match_excluded(bsdtar->matching, in_entry))
694 if (bsdtar->option_interactive &&
695 !yes("copy '%s'", archive_entry_pathname(in_entry)))
697 if (bsdtar->verbose > 1) {
698 safe_fprintf(stderr, "a ");
699 list_item_verbose(bsdtar, stderr, in_entry);
700 } else if (bsdtar->verbose > 0)
701 safe_fprintf(stderr, "a %s",
702 archive_entry_pathname(in_entry));
704 report_write(bsdtar, a, in_entry, 0);
706 e = archive_write_header(a, in_entry);
707 if (e != ARCHIVE_OK) {
708 if (!bsdtar->verbose)
709 lafe_warnc(0, "%s: %s",
710 archive_entry_pathname(in_entry),
711 archive_error_string(a));
713 fprintf(stderr, ": %s", archive_error_string(a));
715 if (e == ARCHIVE_FATAL)
718 if (e >= ARCHIVE_WARN) {
719 if (archive_entry_size(in_entry) == 0)
720 archive_read_data_skip(ina);
721 else if (copy_file_data_block(bsdtar, a, ina, in_entry))
726 fprintf(stderr, "\n");
729 return (e == ARCHIVE_EOF ? ARCHIVE_OK : e);
732 /* Helper function to copy file to archive. */
734 copy_file_data_block(struct bsdtar *bsdtar, struct archive *a,
735 struct archive *in_a, struct archive_entry *entry)
738 ssize_t bytes_written;
739 int64_t offset, progress = 0;
740 char *null_buff = NULL;
744 while ((r = archive_read_data_block(in_a, &buff,
745 &bytes_read, &offset)) == ARCHIVE_OK) {
747 report_write(bsdtar, a, entry, progress);
749 if (offset > progress) {
750 int64_t sparse = offset - progress;
753 if (null_buff == NULL) {
754 null_buff = bsdtar->buff;
755 memset(null_buff, 0, bsdtar->buff_size);
759 if (sparse > (int64_t)bsdtar->buff_size)
760 ns = bsdtar->buff_size;
764 archive_write_data(a, null_buff, ns);
765 if (bytes_written < 0) {
766 /* Write failed; this is bad */
768 archive_error_string(a));
771 if ((size_t)bytes_written < ns) {
772 /* Write was truncated; warn but
775 "%s: Truncated write; file may "
776 "have grown while being archived.",
777 archive_entry_pathname(entry));
780 progress += bytes_written;
781 sparse -= bytes_written;
785 bytes_written = archive_write_data(a, buff, bytes_read);
786 if (bytes_written < 0) {
787 /* Write failed; this is bad */
788 lafe_warnc(0, "%s", archive_error_string(a));
791 if ((size_t)bytes_written < bytes_read) {
792 /* Write was truncated; warn but continue. */
794 "%s: Truncated write; file may have grown "
795 "while being archived.",
796 archive_entry_pathname(entry));
799 progress += bytes_written;
801 if (r < ARCHIVE_WARN) {
802 lafe_warnc(archive_errno(a), "%s", archive_error_string(a));
809 excluded_callback(struct archive *a, void *_data, struct archive_entry *entry)
811 struct bsdtar *bsdtar = (struct bsdtar *)_data;
813 if (bsdtar->option_no_subdirs)
815 if (!archive_read_disk_can_descend(a))
817 if (bsdtar->option_interactive &&
818 !yes("add '%s'", archive_entry_pathname(entry)))
820 archive_read_disk_descend(a);
824 metadata_filter(struct archive *a, void *_data, struct archive_entry *entry)
826 struct bsdtar *bsdtar = (struct bsdtar *)_data;
828 /* XXX TODO: check whether this filesystem is
829 * synthetic and/or local. Add a new
830 * --local-only option to skip non-local
831 * filesystems. Skip synthetic filesystems
834 * The results should be cached, since
835 * tree.c doesn't usually visit a directory
836 * and the directory contents together. A simple
837 * move-to-front list should perform quite well.
839 * Use archive_read_disk_current_filesystem_is_remote().
843 * If the user vetoes this file/directory, skip it.
844 * We want this to be fairly late; if some other
845 * check would veto this file, we shouldn't bother
848 if (bsdtar->option_interactive &&
849 !yes("add '%s'", archive_entry_pathname(entry)))
852 /* Note: if user vetoes, we won't descend. */
853 if (!bsdtar->option_no_subdirs && archive_read_disk_can_descend(a))
854 archive_read_disk_descend(a);
860 * Add the file or dir hierarchy named by 'path' to the archive
863 write_hierarchy(struct bsdtar *bsdtar, struct archive *a, const char *path)
865 struct archive *disk = bsdtar->diskreader;
866 struct archive_entry *entry = NULL, *spare_entry = NULL;
869 r = archive_read_disk_open(disk, path);
870 if (r != ARCHIVE_OK) {
871 lafe_warnc(archive_errno(disk),
872 "%s", archive_error_string(disk));
873 bsdtar->return_value = 1;
876 bsdtar->first_fs = -1;
879 archive_entry_free(entry);
880 entry = archive_entry_new();
881 r = archive_read_next_header2(disk, entry);
882 if (r == ARCHIVE_EOF)
884 else if (r != ARCHIVE_OK) {
885 lafe_warnc(archive_errno(disk),
886 "%s", archive_error_string(disk));
887 if (r == ARCHIVE_FATAL) {
888 bsdtar->return_value = 1;
890 } else if (r < ARCHIVE_WARN)
894 if (bsdtar->uid >= 0) {
895 archive_entry_set_uid(entry, bsdtar->uid);
897 archive_entry_set_uname(entry,
898 archive_read_disk_uname(bsdtar->diskreader,
901 if (bsdtar->gid >= 0) {
902 archive_entry_set_gid(entry, bsdtar->gid);
904 archive_entry_set_gname(entry,
905 archive_read_disk_gname(bsdtar->diskreader,
909 archive_entry_set_uname(entry, bsdtar->uname);
911 archive_entry_set_gname(entry, bsdtar->gname);
914 * Rewrite the pathname to be archived. If rewrite
915 * fails, skip the entry.
917 if (edit_pathname(bsdtar, entry))
920 /* Display entry as we process it. */
921 if (bsdtar->verbose > 1) {
922 safe_fprintf(stderr, "a ");
923 list_item_verbose(bsdtar, stderr, entry);
924 } else if (bsdtar->verbose > 0) {
925 /* This format is required by SUSv2. */
926 safe_fprintf(stderr, "a %s",
927 archive_entry_pathname(entry));
930 /* Non-regular files get archived with zero size. */
931 if (archive_entry_filetype(entry) != AE_IFREG)
932 archive_entry_set_size(entry, 0);
934 archive_entry_linkify(bsdtar->resolver, &entry, &spare_entry);
936 while (entry != NULL) {
937 write_file(bsdtar, a, entry);
938 archive_entry_free(entry);
944 fprintf(stderr, "\n");
946 archive_entry_free(entry);
947 archive_read_close(disk);
951 * Write a single file (or directory or other filesystem object) to
955 write_file(struct bsdtar *bsdtar, struct archive *a,
956 struct archive_entry *entry)
958 write_entry(bsdtar, a, entry);
962 * Write a single entry to the archive.
965 write_entry(struct bsdtar *bsdtar, struct archive *a,
966 struct archive_entry *entry)
970 e = archive_write_header(a, entry);
971 if (e != ARCHIVE_OK) {
972 if (bsdtar->verbose > 1) {
973 safe_fprintf(stderr, "a ");
974 list_item_verbose(bsdtar, stderr, entry);
975 lafe_warnc(0, ": %s", archive_error_string(a));
976 } else if (bsdtar->verbose > 0) {
977 lafe_warnc(0, "%s: %s",
978 archive_entry_pathname(entry),
979 archive_error_string(a));
981 fprintf(stderr, ": %s", archive_error_string(a));
984 if (e == ARCHIVE_FATAL)
988 * If we opened a file earlier, write it out now. Note that
989 * the format handler might have reset the size field to zero
990 * to inform us that the archive body won't get stored. In
991 * that case, just skip the write.
993 if (e >= ARCHIVE_WARN && archive_entry_size(entry) > 0) {
994 if (copy_file_data_block(bsdtar, a, bsdtar->diskreader, entry))
1000 report_write(struct bsdtar *bsdtar, struct archive *a,
1001 struct archive_entry *entry, int64_t progress)
1003 uint64_t comp, uncomp;
1006 if (bsdtar->verbose)
1007 fprintf(stderr, "\n");
1008 comp = archive_filter_bytes(a, -1);
1009 uncomp = archive_filter_bytes(a, 0);
1010 fprintf(stderr, "In: %d files, %s bytes;",
1011 archive_file_count(a), tar_i64toa(uncomp));
1015 compression = (int)((uncomp - comp) * 100 / uncomp);
1017 " Out: %s bytes, compression %d%%\n",
1018 tar_i64toa(comp), compression);
1019 /* Can't have two calls to tar_i64toa() pending, so split the output. */
1020 safe_fprintf(stderr, "Current: %s (%s",
1021 archive_entry_pathname(entry),
1022 tar_i64toa(progress));
1023 fprintf(stderr, "/%s bytes)\n",
1024 tar_i64toa(archive_entry_size(entry)));
1028 test_for_append(struct bsdtar *bsdtar)
1032 if (*bsdtar->argv == NULL && bsdtar->names_from_file == NULL)
1033 lafe_errc(1, 0, "no files or directories specified");
1034 if (bsdtar->filename == NULL)
1035 lafe_errc(1, 0, "Cannot append to stdout.");
1037 if (stat(bsdtar->filename, &s) != 0)
1040 if (!S_ISREG(s.st_mode) && !S_ISBLK(s.st_mode))
1042 "Cannot append to %s: not a regular file.",
1045 /* Is this an appropriate check here on Windows? */
1047 if (GetFileType(handle) != FILE_TYPE_DISK)
1048 lafe_errc(1, 0, "Cannot append");