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$");
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) {
148 size_t module_len = sizeof(IGNORE_WRONG_MODULE_NAME) - 1;
149 size_t opt_len = strlen(writer_options) + 1;
151 /* Set default write options. */
152 if ((p = malloc(module_len + opt_len)) == NULL)
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 memcpy(p, IGNORE_WRONG_MODULE_NAME, module_len);
158 memcpy(p, writer_options, opt_len);
159 r = archive_write_set_options(a, p);
161 if (r < ARCHIVE_WARN)
162 lafe_errc(1, 0, "%s", archive_error_string(a));
164 archive_clear_error(a);
166 if (ARCHIVE_OK != archive_write_set_options(a, bsdtar->option_options))
167 lafe_errc(1, 0, "%s", archive_error_string(a));
171 set_reader_options(struct bsdtar *bsdtar, struct archive *a)
173 const char *reader_options;
176 (void)bsdtar; /* UNUSED */
178 reader_options = getenv(ENV_READER_OPTIONS);
179 if (reader_options != NULL) {
180 size_t module_len = sizeof(IGNORE_WRONG_MODULE_NAME) - 1;
181 size_t opt_len = strlen(reader_options) + 1;
183 /* Set default write options. */
184 if ((p = malloc(module_len + opt_len)) == NULL)
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 memcpy(p, IGNORE_WRONG_MODULE_NAME, module_len);
191 memcpy(p, reader_options, opt_len);
192 r = archive_read_set_options(a, p);
194 if (r < ARCHIVE_WARN)
195 lafe_errc(1, 0, "%s", archive_error_string(a));
197 archive_clear_error(a);
199 if (bsdtar->flags & OPTFLAG_IGNORE_ZEROS)
200 if (archive_read_set_options(a,
201 "read_concatenated_archives") != ARCHIVE_OK)
202 lafe_errc(1, 0, "%s", archive_error_string(a));
206 tar_mode_c(struct bsdtar *bsdtar)
209 const void *filter_name;
212 if (*bsdtar->argv == NULL && bsdtar->names_from_file == NULL)
213 lafe_errc(1, 0, "no files or directories specified");
215 a = archive_write_new();
217 /* Support any format that the library supports. */
218 if (cset_get_format(bsdtar->cset) == NULL) {
219 r = archive_write_set_format_pax_restricted(a);
220 cset_set_format(bsdtar->cset, "pax restricted");
222 r = archive_write_set_format_by_name(a,
223 cset_get_format(bsdtar->cset));
225 if (r != ARCHIVE_OK) {
226 fprintf(stderr, "Can't use format %s: %s\n",
227 cset_get_format(bsdtar->cset),
228 archive_error_string(a));
232 archive_write_set_bytes_per_block(a, bsdtar->bytes_per_block);
233 archive_write_set_bytes_in_last_block(a, bsdtar->bytes_in_last_block);
235 r = cset_write_add_filters(bsdtar->cset, a, &filter_name);
236 if (r < ARCHIVE_WARN) {
237 lafe_errc(1, 0, "Unsupported compression option --%s",
238 (const char *)filter_name);
241 set_writer_options(bsdtar, a);
242 if (bsdtar->passphrase != NULL)
243 r = archive_write_set_passphrase(a, bsdtar->passphrase);
245 r = archive_write_set_passphrase_callback(a, bsdtar,
246 &passphrase_callback);
248 lafe_errc(1, 0, "%s", archive_error_string(a));
249 if (ARCHIVE_OK != archive_write_open_filename(a, bsdtar->filename))
250 lafe_errc(1, 0, "%s", archive_error_string(a));
251 write_archive(a, bsdtar);
255 * Same as 'c', except we only support tar or empty formats in
256 * uncompressed files on disk.
259 tar_mode_r(struct bsdtar *bsdtar)
264 struct archive_entry *entry;
267 /* Sanity-test some arguments and the file. */
268 test_for_append(bsdtar);
270 format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
272 #if defined(__BORLANDC__)
273 bsdtar->fd = open(bsdtar->filename, O_RDWR | O_CREAT | O_BINARY);
275 bsdtar->fd = open(bsdtar->filename, O_RDWR | O_CREAT | O_BINARY, 0666);
279 "Cannot open %s", bsdtar->filename);
281 a = archive_read_new();
282 archive_read_support_filter_all(a);
283 archive_read_support_format_empty(a);
284 archive_read_support_format_tar(a);
285 archive_read_support_format_gnutar(a);
286 set_reader_options(bsdtar, a);
287 r = archive_read_open_fd(a, bsdtar->fd, 10240);
289 lafe_errc(1, archive_errno(a),
290 "Can't read archive %s: %s", bsdtar->filename,
291 archive_error_string(a));
292 while (0 == archive_read_next_header(a, &entry)) {
293 if (archive_filter_code(a, 0) != ARCHIVE_FILTER_NONE) {
294 archive_read_free(a);
297 "Cannot append to compressed archive.");
299 /* Keep going until we hit end-of-archive */
300 format = archive_format(a);
303 end_offset = archive_read_header_position(a);
304 archive_read_free(a);
306 /* Re-open archive for writing */
307 a = archive_write_new();
309 * Set the format to be used for writing. To allow people to
310 * extend empty files, we need to allow them to specify the format,
311 * which opens the possibility that they will specify a format that
312 * doesn't match the existing format. Hence, the following bit
313 * of arcane ugliness.
316 if (cset_get_format(bsdtar->cset) != NULL) {
317 /* If the user requested a format, use that, but ... */
318 archive_write_set_format_by_name(a,
319 cset_get_format(bsdtar->cset));
320 /* ... complain if it's not compatible. */
321 format &= ARCHIVE_FORMAT_BASE_MASK;
322 if (format != (int)(archive_format(a) & ARCHIVE_FORMAT_BASE_MASK)
323 && format != ARCHIVE_FORMAT_EMPTY) {
325 "Format %s is incompatible with the archive %s.",
326 cset_get_format(bsdtar->cset), bsdtar->filename);
330 * Just preserve the current format, with a little care
331 * for formats that libarchive can't write.
333 if (format == ARCHIVE_FORMAT_EMPTY)
334 format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
335 archive_write_set_format(a, format);
337 if (lseek(bsdtar->fd, end_offset, SEEK_SET) < 0)
338 lafe_errc(1, errno, "Could not seek to archive end");
339 set_writer_options(bsdtar, a);
340 if (ARCHIVE_OK != archive_write_open_fd(a, bsdtar->fd))
341 lafe_errc(1, 0, "%s", archive_error_string(a));
343 write_archive(a, bsdtar); /* XXX check return val XXX */
350 tar_mode_u(struct bsdtar *bsdtar)
354 struct archive_entry *entry;
356 struct archive_dir_entry *p;
357 struct archive_dir archive_dir;
359 bsdtar->archive_dir = &archive_dir;
360 memset(&archive_dir, 0, sizeof(archive_dir));
362 format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
364 /* Sanity-test some arguments and the file. */
365 test_for_append(bsdtar);
367 bsdtar->fd = open(bsdtar->filename, O_RDWR | O_BINARY);
370 "Cannot open %s", bsdtar->filename);
372 a = archive_read_new();
373 archive_read_support_filter_all(a);
374 archive_read_support_format_tar(a);
375 archive_read_support_format_gnutar(a);
376 set_reader_options(bsdtar, a);
377 if (archive_read_open_fd(a, bsdtar->fd, bsdtar->bytes_per_block)
380 "Can't open %s: %s", bsdtar->filename,
381 archive_error_string(a));
384 /* Build a list of all entries and their recorded mod times. */
385 while (0 == archive_read_next_header(a, &entry)) {
386 if (archive_filter_code(a, 0) != ARCHIVE_FILTER_NONE) {
387 archive_read_free(a);
390 "Cannot append to compressed archive.");
392 if (archive_match_exclude_entry(bsdtar->matching,
393 ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_OLDER |
394 ARCHIVE_MATCH_EQUAL, entry) != ARCHIVE_OK)
395 lafe_errc(1, 0, "Error : %s",
396 archive_error_string(bsdtar->matching));
397 /* Record the last format determination we see */
398 format = archive_format(a);
399 /* Keep going until we hit end-of-archive */
402 end_offset = archive_read_header_position(a);
403 archive_read_free(a);
405 /* Re-open archive for writing. */
406 a = archive_write_new();
408 * Set format to same one auto-detected above.
410 archive_write_set_format(a, format);
411 archive_write_set_bytes_per_block(a, bsdtar->bytes_per_block);
412 archive_write_set_bytes_in_last_block(a, bsdtar->bytes_in_last_block);
414 if (lseek(bsdtar->fd, end_offset, SEEK_SET) < 0)
415 lafe_errc(1, errno, "Could not seek to archive end");
416 set_writer_options(bsdtar, a);
417 if (ARCHIVE_OK != archive_write_open_fd(a, bsdtar->fd))
418 lafe_errc(1, 0, "%s", archive_error_string(a));
420 write_archive(a, bsdtar);
425 while (bsdtar->archive_dir->head != NULL) {
426 p = bsdtar->archive_dir->head->next;
427 free(bsdtar->archive_dir->head->name);
428 free(bsdtar->archive_dir->head);
429 bsdtar->archive_dir->head = p;
431 bsdtar->archive_dir->tail = NULL;
436 * Write user-specified files/dirs to opened archive.
439 write_archive(struct archive *a, struct bsdtar *bsdtar)
442 struct archive_entry *entry, *sparse_entry;
444 /* Choose a suitable copy buffer size */
445 bsdtar->buff_size = 64 * 1024;
446 while (bsdtar->buff_size < (size_t)bsdtar->bytes_per_block)
447 bsdtar->buff_size *= 2;
448 /* Try to compensate for space we'll lose to alignment. */
449 bsdtar->buff_size += 16 * 1024;
451 /* Allocate a buffer for file data. */
452 if ((bsdtar->buff = malloc(bsdtar->buff_size)) == NULL)
453 lafe_errc(1, 0, "cannot allocate memory");
455 if ((bsdtar->resolver = archive_entry_linkresolver_new()) == NULL)
456 lafe_errc(1, 0, "cannot create link resolver");
457 archive_entry_linkresolver_set_strategy(bsdtar->resolver,
460 /* Create a read_disk object. */
461 if ((bsdtar->diskreader = archive_read_disk_new()) == NULL)
462 lafe_errc(1, 0, "Cannot create read_disk object");
463 /* Tell the read_disk how handle symlink. */
464 switch (bsdtar->symlink_mode) {
466 archive_read_disk_set_symlink_hybrid(bsdtar->diskreader);
469 archive_read_disk_set_symlink_logical(bsdtar->diskreader);
472 archive_read_disk_set_symlink_physical(bsdtar->diskreader);
475 /* Register entry filters. */
476 archive_read_disk_set_matching(bsdtar->diskreader,
477 bsdtar->matching, excluded_callback, bsdtar);
478 archive_read_disk_set_metadata_filter_callback(
479 bsdtar->diskreader, metadata_filter, bsdtar);
480 /* Set the behavior of archive_read_disk. */
481 archive_read_disk_set_behavior(bsdtar->diskreader,
482 bsdtar->readdisk_flags);
483 archive_read_disk_set_standard_lookup(bsdtar->diskreader);
485 if (bsdtar->names_from_file != NULL)
486 archive_names_from_file(bsdtar, a);
488 while (*bsdtar->argv) {
490 if (arg[0] == '-' && arg[1] == 'C') {
497 "Missing argument for -C");
498 bsdtar->return_value = 1;
503 "Meaningless argument for -C: ''");
504 bsdtar->return_value = 1;
508 set_chdir(bsdtar, arg);
511 do_chdir(bsdtar); /* Handle a deferred -C */
513 if (append_archive_filename(bsdtar, a,
517 write_hierarchy(bsdtar, a, arg);
522 archive_read_disk_set_matching(bsdtar->diskreader, NULL, NULL, NULL);
523 archive_read_disk_set_metadata_filter_callback(
524 bsdtar->diskreader, NULL, NULL);
526 archive_entry_linkify(bsdtar->resolver, &entry, &sparse_entry);
527 while (entry != NULL) {
529 struct archive_entry *entry2;
530 struct archive *disk = bsdtar->diskreader;
533 * This tricky code here is to correctly read the contents
534 * of the entry because the disk reader bsdtar->diskreader
535 * is pointing at does not have any information about the
536 * entry by this time and using archive_read_data_block()
537 * with the disk reader consequently must fail. And we
538 * have to re-open the entry to read the contents.
540 /* TODO: Work with -C option as well. */
541 r = archive_read_disk_open(disk,
542 archive_entry_sourcepath(entry));
543 if (r != ARCHIVE_OK) {
544 lafe_warnc(archive_errno(disk),
545 "%s", archive_error_string(disk));
546 bsdtar->return_value = 1;
551 * Invoke archive_read_next_header2() to work
552 * archive_read_data_block(), which is called via write_file(),
555 entry2 = archive_entry_new();
556 r = archive_read_next_header2(disk, entry2);
557 archive_entry_free(entry2);
558 if (r != ARCHIVE_OK) {
559 lafe_warnc(archive_errno(disk),
560 "%s", archive_error_string(disk));
561 if (r == ARCHIVE_FATAL)
562 bsdtar->return_value = 1;
563 archive_read_close(disk);
567 write_file(bsdtar, a, entry);
568 archive_read_close(disk);
570 archive_entry_free(entry);
572 archive_entry_linkify(bsdtar->resolver, &entry, &sparse_entry);
575 if (archive_write_close(a)) {
576 lafe_warnc(0, "%s", archive_error_string(a));
577 bsdtar->return_value = 1;
581 /* Free file data buffer. */
583 archive_entry_linkresolver_free(bsdtar->resolver);
584 bsdtar->resolver = NULL;
585 archive_read_free(bsdtar->diskreader);
586 bsdtar->diskreader = NULL;
588 if (bsdtar->flags & OPTFLAG_TOTALS) {
589 fprintf(stderr, "Total bytes written: %s\n",
590 tar_i64toa(archive_filter_bytes(a, -1)));
593 archive_write_free(a);
597 * Archive names specified in file.
599 * Unless --null was specified, a line containing exactly "-C" will
600 * cause the next line to be a directory to pass to chdir(). If
601 * --null is specified, then a line "-C" is just another filename.
604 archive_names_from_file(struct bsdtar *bsdtar, struct archive *a)
606 struct lafe_line_reader *lr;
609 bsdtar->next_line_is_dir = 0;
611 lr = lafe_line_reader(bsdtar->names_from_file,
612 (bsdtar->flags & OPTFLAG_NULL));
613 while ((line = lafe_line_reader_next(lr)) != NULL) {
614 if (bsdtar->next_line_is_dir) {
616 set_chdir(bsdtar, line);
619 "Meaningless argument for -C: ''");
620 bsdtar->return_value = 1;
622 bsdtar->next_line_is_dir = 0;
623 } else if (((bsdtar->flags & OPTFLAG_NULL) == 0) &&
624 strcmp(line, "-C") == 0)
625 bsdtar->next_line_is_dir = 1;
628 do_chdir(bsdtar); /* Handle a deferred -C */
629 write_hierarchy(bsdtar, a, line);
632 lafe_line_reader_free(lr);
633 if (bsdtar->next_line_is_dir)
635 "Unexpected end of filename list; "
636 "directory expected after -C");
640 * Copy from specified archive to current archive. Returns non-zero
641 * for write errors (which force us to terminate the entire archiving
642 * operation). If there are errors reading the input archive, we set
643 * bsdtar->return_value but return zero, so the overall archiving
644 * operation will complete and return non-zero.
647 append_archive_filename(struct bsdtar *bsdtar, struct archive *a,
648 const char *raw_filename)
651 const char *filename = raw_filename;
654 if (strcmp(filename, "-") == 0)
655 filename = NULL; /* Library uses NULL for stdio. */
657 ina = archive_read_new();
658 archive_read_support_format_all(ina);
659 archive_read_support_filter_all(ina);
660 set_reader_options(bsdtar, ina);
661 archive_read_set_options(ina, "mtree:checkfs");
662 if (bsdtar->passphrase != NULL)
663 rc = archive_read_add_passphrase(a, bsdtar->passphrase);
665 rc = archive_read_set_passphrase_callback(ina, bsdtar,
666 &passphrase_callback);
667 if (rc != ARCHIVE_OK)
668 lafe_errc(1, 0, "%s", archive_error_string(a));
669 if (archive_read_open_filename(ina, filename,
670 bsdtar->bytes_per_block)) {
671 lafe_warnc(0, "%s", archive_error_string(ina));
672 bsdtar->return_value = 1;
676 rc = append_archive(bsdtar, a, ina);
678 if (rc != ARCHIVE_OK) {
679 lafe_warnc(0, "Error reading archive %s: %s",
680 raw_filename, archive_error_string(ina));
681 bsdtar->return_value = 1;
683 archive_read_free(ina);
689 append_archive(struct bsdtar *bsdtar, struct archive *a, struct archive *ina)
691 struct archive_entry *in_entry;
694 while (ARCHIVE_OK == (e = archive_read_next_header(ina, &in_entry))) {
695 if (archive_match_excluded(bsdtar->matching, in_entry))
697 if ((bsdtar->flags & OPTFLAG_INTERACTIVE) &&
698 !yes("copy '%s'", archive_entry_pathname(in_entry)))
700 if (bsdtar->verbose > 1) {
701 safe_fprintf(stderr, "a ");
702 list_item_verbose(bsdtar, stderr, in_entry);
703 } else if (bsdtar->verbose > 0)
704 safe_fprintf(stderr, "a %s",
705 archive_entry_pathname(in_entry));
707 report_write(bsdtar, a, in_entry, 0);
709 e = archive_write_header(a, in_entry);
710 if (e != ARCHIVE_OK) {
711 if (!bsdtar->verbose)
712 lafe_warnc(0, "%s: %s",
713 archive_entry_pathname(in_entry),
714 archive_error_string(a));
716 fprintf(stderr, ": %s", archive_error_string(a));
718 if (e == ARCHIVE_FATAL)
721 if (e >= ARCHIVE_WARN) {
722 if (archive_entry_size(in_entry) == 0)
723 archive_read_data_skip(ina);
724 else if (copy_file_data_block(bsdtar, a, ina, in_entry))
729 fprintf(stderr, "\n");
732 return (e == ARCHIVE_EOF ? ARCHIVE_OK : e);
735 /* Helper function to copy file to archive. */
737 copy_file_data_block(struct bsdtar *bsdtar, struct archive *a,
738 struct archive *in_a, struct archive_entry *entry)
741 ssize_t bytes_written;
742 int64_t offset, progress = 0;
743 char *null_buff = NULL;
747 while ((r = archive_read_data_block(in_a, &buff,
748 &bytes_read, &offset)) == ARCHIVE_OK) {
750 report_write(bsdtar, a, entry, progress);
752 if (offset > progress) {
753 int64_t sparse = offset - progress;
756 if (null_buff == NULL) {
757 null_buff = bsdtar->buff;
758 memset(null_buff, 0, bsdtar->buff_size);
762 if (sparse > (int64_t)bsdtar->buff_size)
763 ns = bsdtar->buff_size;
767 archive_write_data(a, null_buff, ns);
768 if (bytes_written < 0) {
769 /* Write failed; this is bad */
771 archive_error_string(a));
774 if ((size_t)bytes_written < ns) {
775 /* Write was truncated; warn but
778 "%s: Truncated write; file may "
779 "have grown while being archived.",
780 archive_entry_pathname(entry));
783 progress += bytes_written;
784 sparse -= bytes_written;
788 bytes_written = archive_write_data(a, buff, bytes_read);
789 if (bytes_written < 0) {
790 /* Write failed; this is bad */
791 lafe_warnc(0, "%s", archive_error_string(a));
794 if ((size_t)bytes_written < bytes_read) {
795 /* Write was truncated; warn but continue. */
797 "%s: Truncated write; file may have grown "
798 "while being archived.",
799 archive_entry_pathname(entry));
802 progress += bytes_written;
804 if (r < ARCHIVE_WARN) {
805 lafe_warnc(archive_errno(a), "%s", archive_error_string(a));
812 excluded_callback(struct archive *a, void *_data, struct archive_entry *entry)
814 struct bsdtar *bsdtar = (struct bsdtar *)_data;
816 if (bsdtar->flags & OPTFLAG_NO_SUBDIRS)
818 if (!archive_read_disk_can_descend(a))
820 if ((bsdtar->flags & OPTFLAG_INTERACTIVE) &&
821 !yes("add '%s'", archive_entry_pathname(entry)))
823 archive_read_disk_descend(a);
827 metadata_filter(struct archive *a, void *_data, struct archive_entry *entry)
829 struct bsdtar *bsdtar = (struct bsdtar *)_data;
831 /* XXX TODO: check whether this filesystem is
832 * synthetic and/or local. Add a new
833 * --local-only option to skip non-local
834 * filesystems. Skip synthetic filesystems
837 * The results should be cached, since
838 * tree.c doesn't usually visit a directory
839 * and the directory contents together. A simple
840 * move-to-front list should perform quite well.
842 * Use archive_read_disk_current_filesystem_is_remote().
846 * If the user vetoes this file/directory, skip it.
847 * We want this to be fairly late; if some other
848 * check would veto this file, we shouldn't bother
851 if ((bsdtar->flags & OPTFLAG_INTERACTIVE) &&
852 !yes("add '%s'", archive_entry_pathname(entry)))
855 /* Note: if user vetoes, we won't descend. */
856 if (((bsdtar->flags & OPTFLAG_NO_SUBDIRS) == 0) &&
857 archive_read_disk_can_descend(a))
858 archive_read_disk_descend(a);
864 * Add the file or dir hierarchy named by 'path' to the archive
867 write_hierarchy(struct bsdtar *bsdtar, struct archive *a, const char *path)
869 struct archive *disk = bsdtar->diskreader;
870 struct archive_entry *entry = NULL, *spare_entry = NULL;
873 r = archive_read_disk_open(disk, path);
874 if (r != ARCHIVE_OK) {
875 lafe_warnc(archive_errno(disk),
876 "%s", archive_error_string(disk));
877 bsdtar->return_value = 1;
880 bsdtar->first_fs = -1;
883 archive_entry_free(entry);
884 entry = archive_entry_new();
885 r = archive_read_next_header2(disk, entry);
886 if (r == ARCHIVE_EOF)
888 else if (r != ARCHIVE_OK) {
889 lafe_warnc(archive_errno(disk),
890 "%s", archive_error_string(disk));
891 if (r == ARCHIVE_FATAL || r == ARCHIVE_FAILED) {
892 bsdtar->return_value = 1;
893 archive_entry_free(entry);
894 archive_read_close(disk);
896 } else if (r < ARCHIVE_WARN)
900 if (bsdtar->uid >= 0) {
901 archive_entry_set_uid(entry, bsdtar->uid);
903 archive_entry_set_uname(entry,
904 archive_read_disk_uname(bsdtar->diskreader,
907 if (bsdtar->gid >= 0) {
908 archive_entry_set_gid(entry, bsdtar->gid);
910 archive_entry_set_gname(entry,
911 archive_read_disk_gname(bsdtar->diskreader,
915 archive_entry_set_uname(entry, bsdtar->uname);
917 archive_entry_set_gname(entry, bsdtar->gname);
920 * Rewrite the pathname to be archived. If rewrite
921 * fails, skip the entry.
923 if (edit_pathname(bsdtar, entry))
926 /* Display entry as we process it. */
927 if (bsdtar->verbose > 1) {
928 safe_fprintf(stderr, "a ");
929 list_item_verbose(bsdtar, stderr, entry);
930 } else if (bsdtar->verbose > 0) {
931 /* This format is required by SUSv2. */
932 safe_fprintf(stderr, "a %s",
933 archive_entry_pathname(entry));
936 /* Non-regular files get archived with zero size. */
937 if (archive_entry_filetype(entry) != AE_IFREG)
938 archive_entry_set_size(entry, 0);
940 archive_entry_linkify(bsdtar->resolver, &entry, &spare_entry);
942 while (entry != NULL) {
943 write_file(bsdtar, a, entry);
944 archive_entry_free(entry);
950 fprintf(stderr, "\n");
952 archive_entry_free(entry);
953 archive_read_close(disk);
957 * Write a single file (or directory or other filesystem object) to
961 write_file(struct bsdtar *bsdtar, struct archive *a,
962 struct archive_entry *entry)
964 write_entry(bsdtar, a, entry);
968 * Write a single entry to the archive.
971 write_entry(struct bsdtar *bsdtar, struct archive *a,
972 struct archive_entry *entry)
976 e = archive_write_header(a, entry);
977 if (e != ARCHIVE_OK) {
978 if (bsdtar->verbose > 1) {
979 safe_fprintf(stderr, "a ");
980 list_item_verbose(bsdtar, stderr, entry);
981 lafe_warnc(0, ": %s", archive_error_string(a));
982 } else if (bsdtar->verbose > 0) {
983 lafe_warnc(0, "%s: %s",
984 archive_entry_pathname(entry),
985 archive_error_string(a));
987 fprintf(stderr, ": %s", archive_error_string(a));
990 if (e == ARCHIVE_FATAL)
994 * If we opened a file earlier, write it out now. Note that
995 * the format handler might have reset the size field to zero
996 * to inform us that the archive body won't get stored. In
997 * that case, just skip the write.
999 if (e >= ARCHIVE_WARN && archive_entry_size(entry) > 0) {
1000 if (copy_file_data_block(bsdtar, a, bsdtar->diskreader, entry))
1006 report_write(struct bsdtar *bsdtar, struct archive *a,
1007 struct archive_entry *entry, int64_t progress)
1009 uint64_t comp, uncomp;
1012 if (bsdtar->verbose)
1013 fprintf(stderr, "\n");
1014 comp = archive_filter_bytes(a, -1);
1015 uncomp = archive_filter_bytes(a, 0);
1016 fprintf(stderr, "In: %d files, %s bytes;",
1017 archive_file_count(a), tar_i64toa(uncomp));
1021 compression = (int)((uncomp - comp) * 100 / uncomp);
1023 " Out: %s bytes, compression %d%%\n",
1024 tar_i64toa(comp), compression);
1025 /* Can't have two calls to tar_i64toa() pending, so split the output. */
1026 safe_fprintf(stderr, "Current: %s (%s",
1027 archive_entry_pathname(entry),
1028 tar_i64toa(progress));
1029 fprintf(stderr, "/%s bytes)\n",
1030 tar_i64toa(archive_entry_size(entry)));
1034 test_for_append(struct bsdtar *bsdtar)
1038 if (*bsdtar->argv == NULL && bsdtar->names_from_file == NULL)
1039 lafe_errc(1, 0, "no files or directories specified");
1040 if (bsdtar->filename == NULL)
1041 lafe_errc(1, 0, "Cannot append to stdout.");
1043 if (stat(bsdtar->filename, &s) != 0)
1046 if (!S_ISREG(s.st_mode) && !S_ISBLK(s.st_mode))
1048 "Cannot append to %s: not a regular file.",
1051 /* Is this an appropriate check here on Windows? */
1053 if (GetFileType(handle) != FILE_TYPE_DISK)
1054 lafe_errc(1, 0, "Cannot append");