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) {
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);
202 tar_mode_c(struct bsdtar *bsdtar)
205 const void *filter_name;
208 if (*bsdtar->argv == NULL && bsdtar->names_from_file == NULL)
209 lafe_errc(1, 0, "no files or directories specified");
211 a = archive_write_new();
213 /* Support any format that the library supports. */
214 if (cset_get_format(bsdtar->cset) == NULL) {
215 r = archive_write_set_format_pax_restricted(a);
216 cset_set_format(bsdtar->cset, "pax restricted");
218 r = archive_write_set_format_by_name(a,
219 cset_get_format(bsdtar->cset));
221 if (r != ARCHIVE_OK) {
222 fprintf(stderr, "Can't use format %s: %s\n",
223 cset_get_format(bsdtar->cset),
224 archive_error_string(a));
228 archive_write_set_bytes_per_block(a, bsdtar->bytes_per_block);
229 archive_write_set_bytes_in_last_block(a, bsdtar->bytes_in_last_block);
231 r = cset_write_add_filters(bsdtar->cset, a, &filter_name);
232 if (r < ARCHIVE_WARN) {
233 lafe_errc(1, 0, "Unsupported compression option --%s",
234 (const char *)filter_name);
237 set_writer_options(bsdtar, a);
238 if (bsdtar->passphrase != NULL)
239 r = archive_write_set_passphrase(a, bsdtar->passphrase);
241 r = archive_write_set_passphrase_callback(a, bsdtar,
242 &passphrase_callback);
244 lafe_errc(1, 0, "%s", archive_error_string(a));
245 if (ARCHIVE_OK != archive_write_open_filename(a, bsdtar->filename))
246 lafe_errc(1, 0, "%s", archive_error_string(a));
247 write_archive(a, bsdtar);
251 * Same as 'c', except we only support tar or empty formats in
252 * uncompressed files on disk.
255 tar_mode_r(struct bsdtar *bsdtar)
260 struct archive_entry *entry;
263 /* Sanity-test some arguments and the file. */
264 test_for_append(bsdtar);
266 format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
268 #if defined(__BORLANDC__)
269 bsdtar->fd = open(bsdtar->filename, O_RDWR | O_CREAT | O_BINARY);
271 bsdtar->fd = open(bsdtar->filename, O_RDWR | O_CREAT | O_BINARY, 0666);
275 "Cannot open %s", bsdtar->filename);
277 a = archive_read_new();
278 archive_read_support_filter_all(a);
279 archive_read_support_format_empty(a);
280 archive_read_support_format_tar(a);
281 archive_read_support_format_gnutar(a);
282 set_reader_options(bsdtar, a);
283 r = archive_read_open_fd(a, bsdtar->fd, 10240);
285 lafe_errc(1, archive_errno(a),
286 "Can't read archive %s: %s", bsdtar->filename,
287 archive_error_string(a));
288 while (0 == archive_read_next_header(a, &entry)) {
289 if (archive_filter_code(a, 0) != ARCHIVE_FILTER_NONE) {
290 archive_read_free(a);
293 "Cannot append to compressed archive.");
295 /* Keep going until we hit end-of-archive */
296 format = archive_format(a);
299 end_offset = archive_read_header_position(a);
300 archive_read_free(a);
302 /* Re-open archive for writing */
303 a = archive_write_new();
305 * Set the format to be used for writing. To allow people to
306 * extend empty files, we need to allow them to specify the format,
307 * which opens the possibility that they will specify a format that
308 * doesn't match the existing format. Hence, the following bit
309 * of arcane ugliness.
312 if (cset_get_format(bsdtar->cset) != NULL) {
313 /* If the user requested a format, use that, but ... */
314 archive_write_set_format_by_name(a,
315 cset_get_format(bsdtar->cset));
316 /* ... complain if it's not compatible. */
317 format &= ARCHIVE_FORMAT_BASE_MASK;
318 if (format != (int)(archive_format(a) & ARCHIVE_FORMAT_BASE_MASK)
319 && format != ARCHIVE_FORMAT_EMPTY) {
321 "Format %s is incompatible with the archive %s.",
322 cset_get_format(bsdtar->cset), bsdtar->filename);
326 * Just preserve the current format, with a little care
327 * for formats that libarchive can't write.
329 if (format == ARCHIVE_FORMAT_EMPTY)
330 format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
331 archive_write_set_format(a, format);
333 if (lseek(bsdtar->fd, end_offset, SEEK_SET) < 0)
334 lafe_errc(1, errno, "Could not seek to archive end");
335 set_writer_options(bsdtar, a);
336 if (ARCHIVE_OK != archive_write_open_fd(a, bsdtar->fd))
337 lafe_errc(1, 0, "%s", archive_error_string(a));
339 write_archive(a, bsdtar); /* XXX check return val XXX */
346 tar_mode_u(struct bsdtar *bsdtar)
350 struct archive_entry *entry;
352 struct archive_dir_entry *p;
353 struct archive_dir archive_dir;
355 bsdtar->archive_dir = &archive_dir;
356 memset(&archive_dir, 0, sizeof(archive_dir));
358 format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
360 /* Sanity-test some arguments and the file. */
361 test_for_append(bsdtar);
363 bsdtar->fd = open(bsdtar->filename, O_RDWR | O_BINARY);
366 "Cannot open %s", bsdtar->filename);
368 a = archive_read_new();
369 archive_read_support_filter_all(a);
370 archive_read_support_format_tar(a);
371 archive_read_support_format_gnutar(a);
372 set_reader_options(bsdtar, a);
373 if (archive_read_open_fd(a, bsdtar->fd, bsdtar->bytes_per_block)
376 "Can't open %s: %s", bsdtar->filename,
377 archive_error_string(a));
380 /* Build a list of all entries and their recorded mod times. */
381 while (0 == archive_read_next_header(a, &entry)) {
382 if (archive_filter_code(a, 0) != ARCHIVE_FILTER_NONE) {
383 archive_read_free(a);
386 "Cannot append to compressed archive.");
388 if (archive_match_exclude_entry(bsdtar->matching,
389 ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_OLDER |
390 ARCHIVE_MATCH_EQUAL, entry) != ARCHIVE_OK)
391 lafe_errc(1, 0, "Error : %s",
392 archive_error_string(bsdtar->matching));
393 /* Record the last format determination we see */
394 format = archive_format(a);
395 /* Keep going until we hit end-of-archive */
398 end_offset = archive_read_header_position(a);
399 archive_read_free(a);
401 /* Re-open archive for writing. */
402 a = archive_write_new();
404 * Set format to same one auto-detected above.
406 archive_write_set_format(a, format);
407 archive_write_set_bytes_per_block(a, bsdtar->bytes_per_block);
408 archive_write_set_bytes_in_last_block(a, bsdtar->bytes_in_last_block);
410 if (lseek(bsdtar->fd, end_offset, SEEK_SET) < 0)
411 lafe_errc(1, errno, "Could not seek to archive end");
412 set_writer_options(bsdtar, a);
413 if (ARCHIVE_OK != archive_write_open_fd(a, bsdtar->fd))
414 lafe_errc(1, 0, "%s", archive_error_string(a));
416 write_archive(a, bsdtar);
421 while (bsdtar->archive_dir->head != NULL) {
422 p = bsdtar->archive_dir->head->next;
423 free(bsdtar->archive_dir->head->name);
424 free(bsdtar->archive_dir->head);
425 bsdtar->archive_dir->head = p;
427 bsdtar->archive_dir->tail = NULL;
432 * Write user-specified files/dirs to opened archive.
435 write_archive(struct archive *a, struct bsdtar *bsdtar)
438 struct archive_entry *entry, *sparse_entry;
440 /* Choose a suitable copy buffer size */
441 bsdtar->buff_size = 64 * 1024;
442 while (bsdtar->buff_size < (size_t)bsdtar->bytes_per_block)
443 bsdtar->buff_size *= 2;
444 /* Try to compensate for space we'll lose to alignment. */
445 bsdtar->buff_size += 16 * 1024;
447 /* Allocate a buffer for file data. */
448 if ((bsdtar->buff = malloc(bsdtar->buff_size)) == NULL)
449 lafe_errc(1, 0, "cannot allocate memory");
451 if ((bsdtar->resolver = archive_entry_linkresolver_new()) == NULL)
452 lafe_errc(1, 0, "cannot create link resolver");
453 archive_entry_linkresolver_set_strategy(bsdtar->resolver,
456 /* Create a read_disk object. */
457 if ((bsdtar->diskreader = archive_read_disk_new()) == NULL)
458 lafe_errc(1, 0, "Cannot create read_disk object");
459 /* Tell the read_disk how handle symlink. */
460 switch (bsdtar->symlink_mode) {
462 archive_read_disk_set_symlink_hybrid(bsdtar->diskreader);
465 archive_read_disk_set_symlink_logical(bsdtar->diskreader);
468 archive_read_disk_set_symlink_physical(bsdtar->diskreader);
471 /* Register entry filters. */
472 archive_read_disk_set_matching(bsdtar->diskreader,
473 bsdtar->matching, excluded_callback, bsdtar);
474 archive_read_disk_set_metadata_filter_callback(
475 bsdtar->diskreader, metadata_filter, bsdtar);
476 /* Set the behavior of archive_read_disk. */
477 archive_read_disk_set_behavior(bsdtar->diskreader,
478 bsdtar->readdisk_flags);
479 archive_read_disk_set_standard_lookup(bsdtar->diskreader);
481 if (bsdtar->names_from_file != NULL)
482 archive_names_from_file(bsdtar, a);
484 while (*bsdtar->argv) {
486 if (arg[0] == '-' && arg[1] == 'C') {
493 "Missing argument for -C");
494 bsdtar->return_value = 1;
499 "Meaningless argument for -C: ''");
500 bsdtar->return_value = 1;
504 set_chdir(bsdtar, arg);
506 if (*arg != '/' && (arg[0] != '@' || arg[1] != '/'))
507 do_chdir(bsdtar); /* Handle a deferred -C */
509 if (append_archive_filename(bsdtar, a,
513 write_hierarchy(bsdtar, a, arg);
518 archive_read_disk_set_matching(bsdtar->diskreader, NULL, NULL, NULL);
519 archive_read_disk_set_metadata_filter_callback(
520 bsdtar->diskreader, NULL, NULL);
522 archive_entry_linkify(bsdtar->resolver, &entry, &sparse_entry);
523 while (entry != NULL) {
525 struct archive_entry *entry2;
526 struct archive *disk = bsdtar->diskreader;
529 * This tricky code here is to correctly read the contents
530 * of the entry because the disk reader bsdtar->diskreader
531 * is pointing at does not have any information about the
532 * entry by this time and using archive_read_data_block()
533 * with the disk reader consequently must fail. And we
534 * have to re-open the entry to read the contents.
536 /* TODO: Work with -C option as well. */
537 r = archive_read_disk_open(disk,
538 archive_entry_sourcepath(entry));
539 if (r != ARCHIVE_OK) {
540 lafe_warnc(archive_errno(disk),
541 "%s", archive_error_string(disk));
542 bsdtar->return_value = 1;
543 archive_entry_free(entry);
548 * Invoke archive_read_next_header2() to work
549 * archive_read_data_block(), which is called via write_file(),
552 entry2 = archive_entry_new();
553 r = archive_read_next_header2(disk, entry2);
554 archive_entry_free(entry2);
555 if (r != ARCHIVE_OK) {
556 lafe_warnc(archive_errno(disk),
557 "%s", archive_error_string(disk));
558 if (r == ARCHIVE_FATAL)
559 bsdtar->return_value = 1;
561 archive_read_close(disk);
562 archive_entry_free(entry);
566 write_file(bsdtar, a, entry);
567 archive_entry_free(entry);
568 archive_read_close(disk);
570 archive_entry_linkify(bsdtar->resolver, &entry, &sparse_entry);
573 if (archive_write_close(a)) {
574 lafe_warnc(0, "%s", archive_error_string(a));
575 bsdtar->return_value = 1;
579 /* Free file data buffer. */
581 archive_entry_linkresolver_free(bsdtar->resolver);
582 bsdtar->resolver = NULL;
583 archive_read_free(bsdtar->diskreader);
584 bsdtar->diskreader = NULL;
586 if (bsdtar->option_totals) {
587 fprintf(stderr, "Total bytes written: %s\n",
588 tar_i64toa(archive_filter_bytes(a, -1)));
591 archive_write_free(a);
595 * Archive names specified in file.
597 * Unless --null was specified, a line containing exactly "-C" will
598 * cause the next line to be a directory to pass to chdir(). If
599 * --null is specified, then a line "-C" is just another filename.
602 archive_names_from_file(struct bsdtar *bsdtar, struct archive *a)
604 struct lafe_line_reader *lr;
607 bsdtar->next_line_is_dir = 0;
609 lr = lafe_line_reader(bsdtar->names_from_file, bsdtar->option_null);
610 while ((line = lafe_line_reader_next(lr)) != NULL) {
611 if (bsdtar->next_line_is_dir) {
613 set_chdir(bsdtar, line);
616 "Meaningless argument for -C: ''");
617 bsdtar->return_value = 1;
619 bsdtar->next_line_is_dir = 0;
620 } else if (!bsdtar->option_null && strcmp(line, "-C") == 0)
621 bsdtar->next_line_is_dir = 1;
624 do_chdir(bsdtar); /* Handle a deferred -C */
625 write_hierarchy(bsdtar, a, line);
628 lafe_line_reader_free(lr);
629 if (bsdtar->next_line_is_dir)
631 "Unexpected end of filename list; "
632 "directory expected after -C");
636 * Copy from specified archive to current archive. Returns non-zero
637 * for write errors (which force us to terminate the entire archiving
638 * operation). If there are errors reading the input archive, we set
639 * bsdtar->return_value but return zero, so the overall archiving
640 * operation will complete and return non-zero.
643 append_archive_filename(struct bsdtar *bsdtar, struct archive *a,
644 const char *raw_filename)
647 const char *filename = raw_filename;
650 if (strcmp(filename, "-") == 0)
651 filename = NULL; /* Library uses NULL for stdio. */
653 ina = archive_read_new();
654 archive_read_support_format_all(ina);
655 archive_read_support_filter_all(ina);
656 set_reader_options(bsdtar, ina);
657 archive_read_set_options(ina, "mtree:checkfs");
658 if (bsdtar->passphrase != NULL)
659 rc = archive_read_add_passphrase(a, bsdtar->passphrase);
661 rc = archive_read_set_passphrase_callback(ina, bsdtar,
662 &passphrase_callback);
663 if (rc != ARCHIVE_OK)
664 lafe_errc(1, 0, "%s", archive_error_string(a));
665 if (archive_read_open_filename(ina, filename,
666 bsdtar->bytes_per_block)) {
667 lafe_warnc(0, "%s", archive_error_string(ina));
668 bsdtar->return_value = 1;
672 rc = append_archive(bsdtar, a, ina);
674 if (rc != ARCHIVE_OK) {
675 lafe_warnc(0, "Error reading archive %s: %s",
676 raw_filename, archive_error_string(ina));
677 bsdtar->return_value = 1;
679 archive_read_free(ina);
685 append_archive(struct bsdtar *bsdtar, struct archive *a, struct archive *ina)
687 struct archive_entry *in_entry;
690 while (ARCHIVE_OK == (e = archive_read_next_header(ina, &in_entry))) {
691 if (archive_match_excluded(bsdtar->matching, in_entry))
693 if (bsdtar->option_interactive &&
694 !yes("copy '%s'", archive_entry_pathname(in_entry)))
696 if (bsdtar->verbose > 1) {
697 safe_fprintf(stderr, "a ");
698 list_item_verbose(bsdtar, stderr, in_entry);
699 } else if (bsdtar->verbose > 0)
700 safe_fprintf(stderr, "a %s",
701 archive_entry_pathname(in_entry));
703 report_write(bsdtar, a, in_entry, 0);
705 e = archive_write_header(a, in_entry);
706 if (e != ARCHIVE_OK) {
707 if (!bsdtar->verbose)
708 lafe_warnc(0, "%s: %s",
709 archive_entry_pathname(in_entry),
710 archive_error_string(a));
712 fprintf(stderr, ": %s", archive_error_string(a));
714 if (e == ARCHIVE_FATAL)
717 if (e >= ARCHIVE_WARN) {
718 if (archive_entry_size(in_entry) == 0)
719 archive_read_data_skip(ina);
720 else if (copy_file_data_block(bsdtar, a, ina, in_entry))
725 fprintf(stderr, "\n");
728 return (e == ARCHIVE_EOF ? ARCHIVE_OK : e);
731 /* Helper function to copy file to archive. */
733 copy_file_data_block(struct bsdtar *bsdtar, struct archive *a,
734 struct archive *in_a, struct archive_entry *entry)
737 ssize_t bytes_written;
738 int64_t offset, progress = 0;
739 char *null_buff = NULL;
743 while ((r = archive_read_data_block(in_a, &buff,
744 &bytes_read, &offset)) == ARCHIVE_OK) {
746 report_write(bsdtar, a, entry, progress);
748 if (offset > progress) {
749 int64_t sparse = offset - progress;
752 if (null_buff == NULL) {
753 null_buff = bsdtar->buff;
754 memset(null_buff, 0, bsdtar->buff_size);
758 if (sparse > (int64_t)bsdtar->buff_size)
759 ns = bsdtar->buff_size;
763 archive_write_data(a, null_buff, ns);
764 if (bytes_written < 0) {
765 /* Write failed; this is bad */
767 archive_error_string(a));
770 if ((size_t)bytes_written < ns) {
771 /* Write was truncated; warn but
774 "%s: Truncated write; file may "
775 "have grown while being archived.",
776 archive_entry_pathname(entry));
779 progress += bytes_written;
780 sparse -= bytes_written;
784 bytes_written = archive_write_data(a, buff, bytes_read);
785 if (bytes_written < 0) {
786 /* Write failed; this is bad */
787 lafe_warnc(0, "%s", archive_error_string(a));
790 if ((size_t)bytes_written < bytes_read) {
791 /* Write was truncated; warn but continue. */
793 "%s: Truncated write; file may have grown "
794 "while being archived.",
795 archive_entry_pathname(entry));
798 progress += bytes_written;
800 if (r < ARCHIVE_WARN) {
801 lafe_warnc(archive_errno(a), "%s", archive_error_string(a));
808 excluded_callback(struct archive *a, void *_data, struct archive_entry *entry)
810 struct bsdtar *bsdtar = (struct bsdtar *)_data;
812 if (bsdtar->option_no_subdirs)
814 if (!archive_read_disk_can_descend(a))
816 if (bsdtar->option_interactive &&
817 !yes("add '%s'", archive_entry_pathname(entry)))
819 archive_read_disk_descend(a);
823 metadata_filter(struct archive *a, void *_data, struct archive_entry *entry)
825 struct bsdtar *bsdtar = (struct bsdtar *)_data;
827 /* XXX TODO: check whether this filesystem is
828 * synthetic and/or local. Add a new
829 * --local-only option to skip non-local
830 * filesystems. Skip synthetic filesystems
833 * The results should be cached, since
834 * tree.c doesn't usually visit a directory
835 * and the directory contents together. A simple
836 * move-to-front list should perform quite well.
838 * Use archive_read_disk_current_filesystem_is_remote().
842 * If the user vetoes this file/directory, skip it.
843 * We want this to be fairly late; if some other
844 * check would veto this file, we shouldn't bother
847 if (bsdtar->option_interactive &&
848 !yes("add '%s'", archive_entry_pathname(entry)))
851 /* Note: if user vetoes, we won't descend. */
852 if (!bsdtar->option_no_subdirs && archive_read_disk_can_descend(a))
853 archive_read_disk_descend(a);
859 * Add the file or dir hierarchy named by 'path' to the archive
862 write_hierarchy(struct bsdtar *bsdtar, struct archive *a, const char *path)
864 struct archive *disk = bsdtar->diskreader;
865 struct archive_entry *entry = NULL, *spare_entry = NULL;
868 r = archive_read_disk_open(disk, path);
869 if (r != ARCHIVE_OK) {
870 lafe_warnc(archive_errno(disk),
871 "%s", archive_error_string(disk));
872 bsdtar->return_value = 1;
875 bsdtar->first_fs = -1;
878 archive_entry_free(entry);
879 entry = archive_entry_new();
880 r = archive_read_next_header2(disk, entry);
881 if (r == ARCHIVE_EOF)
883 else if (r != ARCHIVE_OK) {
884 lafe_warnc(archive_errno(disk),
885 "%s", archive_error_string(disk));
886 if (r == ARCHIVE_FATAL || r == ARCHIVE_FAILED) {
887 bsdtar->return_value = 1;
888 archive_entry_free(entry);
889 archive_read_close(disk);
891 } else if (r < ARCHIVE_WARN)
895 if (bsdtar->uid >= 0) {
896 archive_entry_set_uid(entry, bsdtar->uid);
898 archive_entry_set_uname(entry,
899 archive_read_disk_uname(bsdtar->diskreader,
902 if (bsdtar->gid >= 0) {
903 archive_entry_set_gid(entry, bsdtar->gid);
905 archive_entry_set_gname(entry,
906 archive_read_disk_gname(bsdtar->diskreader,
910 archive_entry_set_uname(entry, bsdtar->uname);
912 archive_entry_set_gname(entry, bsdtar->gname);
915 * Rewrite the pathname to be archived. If rewrite
916 * fails, skip the entry.
918 if (edit_pathname(bsdtar, entry))
921 /* Display entry as we process it. */
922 if (bsdtar->verbose > 1) {
923 safe_fprintf(stderr, "a ");
924 list_item_verbose(bsdtar, stderr, entry);
925 } else if (bsdtar->verbose > 0) {
926 /* This format is required by SUSv2. */
927 safe_fprintf(stderr, "a %s",
928 archive_entry_pathname(entry));
931 /* Non-regular files get archived with zero size. */
932 if (archive_entry_filetype(entry) != AE_IFREG)
933 archive_entry_set_size(entry, 0);
935 archive_entry_linkify(bsdtar->resolver, &entry, &spare_entry);
937 while (entry != NULL) {
938 write_file(bsdtar, a, entry);
939 archive_entry_free(entry);
945 fprintf(stderr, "\n");
947 archive_entry_free(entry);
948 archive_read_close(disk);
952 * Write a single file (or directory or other filesystem object) to
956 write_file(struct bsdtar *bsdtar, struct archive *a,
957 struct archive_entry *entry)
959 write_entry(bsdtar, a, entry);
963 * Write a single entry to the archive.
966 write_entry(struct bsdtar *bsdtar, struct archive *a,
967 struct archive_entry *entry)
971 e = archive_write_header(a, entry);
972 if (e != ARCHIVE_OK) {
973 if (bsdtar->verbose > 1) {
974 safe_fprintf(stderr, "a ");
975 list_item_verbose(bsdtar, stderr, entry);
976 lafe_warnc(0, ": %s", archive_error_string(a));
977 } else if (bsdtar->verbose > 0) {
978 lafe_warnc(0, "%s: %s",
979 archive_entry_pathname(entry),
980 archive_error_string(a));
982 fprintf(stderr, ": %s", archive_error_string(a));
985 if (e == ARCHIVE_FATAL)
989 * If we opened a file earlier, write it out now. Note that
990 * the format handler might have reset the size field to zero
991 * to inform us that the archive body won't get stored. In
992 * that case, just skip the write.
994 if (e >= ARCHIVE_WARN && archive_entry_size(entry) > 0) {
995 if (copy_file_data_block(bsdtar, a, bsdtar->diskreader, entry))
1001 report_write(struct bsdtar *bsdtar, struct archive *a,
1002 struct archive_entry *entry, int64_t progress)
1004 uint64_t comp, uncomp;
1007 if (bsdtar->verbose)
1008 fprintf(stderr, "\n");
1009 comp = archive_filter_bytes(a, -1);
1010 uncomp = archive_filter_bytes(a, 0);
1011 fprintf(stderr, "In: %d files, %s bytes;",
1012 archive_file_count(a), tar_i64toa(uncomp));
1016 compression = (int)((uncomp - comp) * 100 / uncomp);
1018 " Out: %s bytes, compression %d%%\n",
1019 tar_i64toa(comp), compression);
1020 /* Can't have two calls to tar_i64toa() pending, so split the output. */
1021 safe_fprintf(stderr, "Current: %s (%s",
1022 archive_entry_pathname(entry),
1023 tar_i64toa(progress));
1024 fprintf(stderr, "/%s bytes)\n",
1025 tar_i64toa(archive_entry_size(entry)));
1029 test_for_append(struct bsdtar *bsdtar)
1033 if (*bsdtar->argv == NULL && bsdtar->names_from_file == NULL)
1034 lafe_errc(1, 0, "no files or directories specified");
1035 if (bsdtar->filename == NULL)
1036 lafe_errc(1, 0, "Cannot append to stdout.");
1038 if (stat(bsdtar->filename, &s) != 0)
1041 if (!S_ISREG(s.st_mode) && !S_ISBLK(s.st_mode))
1043 "Cannot append to %s: not a regular file.",
1046 /* Is this an appropriate check here on Windows? */
1048 if (GetFileType(handle) != FILE_TYPE_DISK)
1049 lafe_errc(1, 0, "Cannot append");