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"
29 #ifdef HAVE_SYS_TYPES_H
30 #include <sys/types.h>
32 #ifdef HAVE_SYS_STAT_H
35 #ifdef HAVE_ATTR_XATTR_H
36 #include <attr/xattr.h>
78 #include "line_reader.h"
84 struct archive_dir_entry
{
85 struct archive_dir_entry
*next
;
92 struct archive_dir_entry
*head
, *tail
;
95 static int append_archive(struct bsdtar
*, struct archive
*,
97 static int append_archive_filename(struct bsdtar
*,
98 struct archive
*, const char *fname
);
99 static void archive_names_from_file(struct bsdtar
*bsdtar
,
101 static int copy_file_data_block(struct bsdtar
*,
102 struct archive
*a
, struct archive
*,
103 struct archive_entry
*);
104 static void excluded_callback(struct archive
*, void *,
105 struct archive_entry
*);
106 static void report_write(struct bsdtar
*, struct archive
*,
107 struct archive_entry
*, int64_t progress
);
108 static void test_for_append(struct bsdtar
*);
109 static int metadata_filter(struct archive
*, void *,
110 struct archive_entry
*);
111 static void write_archive(struct archive
*, struct bsdtar
*);
112 static void write_entry(struct bsdtar
*, struct archive
*,
113 struct archive_entry
*);
114 static void write_file(struct bsdtar
*, struct archive
*,
115 struct archive_entry
*);
116 static void write_hierarchy(struct bsdtar
*, struct archive
*,
119 #if defined(_WIN32) && !defined(__CYGWIN__)
120 /* Not a full lseek() emulation, but enough for our needs here. */
122 seek_file(int fd
, int64_t offset
, int whence
)
124 LARGE_INTEGER distance
;
125 (void)whence
; /* UNUSED */
126 distance
.QuadPart
= offset
;
127 return (SetFilePointerEx((HANDLE
)_get_osfhandle(fd
),
128 distance
, NULL
, FILE_BEGIN
) ? 1 : -1);
136 #define lseek seek_file
140 set_writer_options(struct bsdtar
*bsdtar
, struct archive
*a
)
142 const char *writer_options
;
145 writer_options
= getenv(ENV_WRITER_OPTIONS
);
146 if (writer_options
!= NULL
) {
147 size_t module_len
= sizeof(IGNORE_WRONG_MODULE_NAME
) - 1;
148 size_t opt_len
= strlen(writer_options
) + 1;
150 /* Set default write options. */
151 if ((p
= malloc(module_len
+ opt_len
)) == NULL
)
152 lafe_errc(1, errno
, "Out of memory");
153 /* Prepend magic code to ignore options for
154 * a format or filters which are not added to
155 * the archive write object. */
156 memcpy(p
, IGNORE_WRONG_MODULE_NAME
, module_len
);
157 memcpy(p
, writer_options
, opt_len
);
158 r
= archive_write_set_options(a
, p
);
160 if (r
< ARCHIVE_WARN
)
161 lafe_errc(1, 0, "%s", archive_error_string(a
));
163 archive_clear_error(a
);
165 if (ARCHIVE_OK
!= archive_write_set_options(a
, bsdtar
->option_options
))
166 lafe_errc(1, 0, "%s", archive_error_string(a
));
170 set_reader_options(struct bsdtar
*bsdtar
, struct archive
*a
)
172 const char *reader_options
;
175 (void)bsdtar
; /* UNUSED */
177 reader_options
= getenv(ENV_READER_OPTIONS
);
178 if (reader_options
!= NULL
) {
179 size_t module_len
= sizeof(IGNORE_WRONG_MODULE_NAME
) - 1;
180 size_t opt_len
= strlen(reader_options
) + 1;
182 /* Set default write options. */
183 if ((p
= malloc(module_len
+ opt_len
)) == NULL
)
185 lafe_errc(1, errno
, "Out of memory");
186 /* Prepend magic code to ignore options for
187 * a format or filters which are not added to
188 * the archive write object. */
189 memcpy(p
, IGNORE_WRONG_MODULE_NAME
, module_len
);
190 memcpy(p
, reader_options
, opt_len
);
191 r
= archive_read_set_options(a
, p
);
193 if (r
< ARCHIVE_WARN
)
194 lafe_errc(1, 0, "%s", archive_error_string(a
));
196 archive_clear_error(a
);
198 if (bsdtar
->flags
& OPTFLAG_IGNORE_ZEROS
)
199 if (archive_read_set_options(a
,
200 "read_concatenated_archives") != ARCHIVE_OK
)
201 lafe_errc(1, 0, "%s", archive_error_string(a
));
205 tar_mode_c(struct bsdtar
*bsdtar
)
208 const void *filter_name
;
211 if (*bsdtar
->argv
== NULL
&& bsdtar
->names_from_file
== NULL
)
212 lafe_errc(1, 0, "no files or directories specified");
214 a
= archive_write_new();
216 /* Support any format that the library supports. */
217 if (cset_get_format(bsdtar
->cset
) == NULL
) {
218 r
= archive_write_set_format_pax_restricted(a
);
219 cset_set_format(bsdtar
->cset
, "pax restricted");
221 r
= archive_write_set_format_by_name(a
,
222 cset_get_format(bsdtar
->cset
));
224 if (r
!= ARCHIVE_OK
) {
225 fprintf(stderr
, "Can't use format %s: %s\n",
226 cset_get_format(bsdtar
->cset
),
227 archive_error_string(a
));
231 archive_write_set_bytes_per_block(a
, bsdtar
->bytes_per_block
);
232 archive_write_set_bytes_in_last_block(a
, bsdtar
->bytes_in_last_block
);
234 r
= cset_write_add_filters(bsdtar
->cset
, a
, &filter_name
);
235 if (r
< ARCHIVE_WARN
) {
236 lafe_errc(1, 0, "Unsupported compression option --%s",
237 (const char *)filter_name
);
240 set_writer_options(bsdtar
, a
);
241 if (bsdtar
->passphrase
!= NULL
)
242 r
= archive_write_set_passphrase(a
, bsdtar
->passphrase
);
244 r
= archive_write_set_passphrase_callback(a
, bsdtar
,
245 &passphrase_callback
);
247 lafe_errc(1, 0, "%s", archive_error_string(a
));
248 if (ARCHIVE_OK
!= archive_write_open_filename(a
, bsdtar
->filename
))
249 lafe_errc(1, 0, "%s", archive_error_string(a
));
250 write_archive(a
, bsdtar
);
254 * Same as 'c', except we only support tar or empty formats in
255 * uncompressed files on disk.
258 tar_mode_r(struct bsdtar
*bsdtar
)
263 struct archive_entry
*entry
;
266 /* Sanity-test some arguments and the file. */
267 test_for_append(bsdtar
);
269 format
= ARCHIVE_FORMAT_TAR_PAX_RESTRICTED
;
271 #if defined(__BORLANDC__)
272 bsdtar
->fd
= open(bsdtar
->filename
, O_RDWR
| O_CREAT
| O_BINARY
);
274 bsdtar
->fd
= open(bsdtar
->filename
, O_RDWR
| O_CREAT
| O_BINARY
, 0666);
278 "Cannot open %s", bsdtar
->filename
);
280 a
= archive_read_new();
281 archive_read_support_filter_all(a
);
282 archive_read_support_format_empty(a
);
283 archive_read_support_format_tar(a
);
284 archive_read_support_format_gnutar(a
);
285 set_reader_options(bsdtar
, a
);
286 r
= archive_read_open_fd(a
, bsdtar
->fd
, 10240);
288 lafe_errc(1, archive_errno(a
),
289 "Can't read archive %s: %s", bsdtar
->filename
,
290 archive_error_string(a
));
291 while (0 == archive_read_next_header(a
, &entry
)) {
292 if (archive_filter_code(a
, 0) != ARCHIVE_FILTER_NONE
) {
293 archive_read_free(a
);
296 "Cannot append to compressed archive.");
298 /* Keep going until we hit end-of-archive */
299 format
= archive_format(a
);
302 end_offset
= archive_read_header_position(a
);
303 archive_read_free(a
);
305 /* Re-open archive for writing */
306 a
= archive_write_new();
308 * Set the format to be used for writing. To allow people to
309 * extend empty files, we need to allow them to specify the format,
310 * which opens the possibility that they will specify a format that
311 * doesn't match the existing format. Hence, the following bit
312 * of arcane ugliness.
315 if (cset_get_format(bsdtar
->cset
) != NULL
) {
316 /* If the user requested a format, use that, but ... */
317 archive_write_set_format_by_name(a
,
318 cset_get_format(bsdtar
->cset
));
319 /* ... complain if it's not compatible. */
320 format
&= ARCHIVE_FORMAT_BASE_MASK
;
321 if (format
!= (int)(archive_format(a
) & ARCHIVE_FORMAT_BASE_MASK
)
322 && format
!= ARCHIVE_FORMAT_EMPTY
) {
324 "Format %s is incompatible with the archive %s.",
325 cset_get_format(bsdtar
->cset
), bsdtar
->filename
);
329 * Just preserve the current format, with a little care
330 * for formats that libarchive can't write.
332 if (format
== ARCHIVE_FORMAT_EMPTY
)
333 format
= ARCHIVE_FORMAT_TAR_PAX_RESTRICTED
;
334 archive_write_set_format(a
, format
);
336 if (lseek(bsdtar
->fd
, end_offset
, SEEK_SET
) < 0)
337 lafe_errc(1, errno
, "Could not seek to archive end");
338 set_writer_options(bsdtar
, a
);
339 if (ARCHIVE_OK
!= archive_write_open_fd(a
, bsdtar
->fd
))
340 lafe_errc(1, 0, "%s", archive_error_string(a
));
342 write_archive(a
, bsdtar
); /* XXX check return val XXX */
349 tar_mode_u(struct bsdtar
*bsdtar
)
353 struct archive_entry
*entry
;
355 struct archive_dir_entry
*p
;
356 struct archive_dir archive_dir
;
358 bsdtar
->archive_dir
= &archive_dir
;
359 memset(&archive_dir
, 0, sizeof(archive_dir
));
361 format
= ARCHIVE_FORMAT_TAR_PAX_RESTRICTED
;
363 /* Sanity-test some arguments and the file. */
364 test_for_append(bsdtar
);
366 bsdtar
->fd
= open(bsdtar
->filename
, O_RDWR
| O_BINARY
);
369 "Cannot open %s", bsdtar
->filename
);
371 a
= archive_read_new();
372 archive_read_support_filter_all(a
);
373 archive_read_support_format_tar(a
);
374 archive_read_support_format_gnutar(a
);
375 set_reader_options(bsdtar
, a
);
376 if (archive_read_open_fd(a
, bsdtar
->fd
, bsdtar
->bytes_per_block
)
379 "Can't open %s: %s", bsdtar
->filename
,
380 archive_error_string(a
));
383 /* Build a list of all entries and their recorded mod times. */
384 while (0 == archive_read_next_header(a
, &entry
)) {
385 if (archive_filter_code(a
, 0) != ARCHIVE_FILTER_NONE
) {
386 archive_read_free(a
);
389 "Cannot append to compressed archive.");
391 if (archive_match_exclude_entry(bsdtar
->matching
,
392 ARCHIVE_MATCH_MTIME
| ARCHIVE_MATCH_OLDER
|
393 ARCHIVE_MATCH_EQUAL
, entry
) != ARCHIVE_OK
)
394 lafe_errc(1, 0, "Error : %s",
395 archive_error_string(bsdtar
->matching
));
396 /* Record the last format determination we see */
397 format
= archive_format(a
);
398 /* Keep going until we hit end-of-archive */
401 end_offset
= archive_read_header_position(a
);
402 archive_read_free(a
);
404 /* Re-open archive for writing. */
405 a
= archive_write_new();
407 * Set format to same one auto-detected above.
409 archive_write_set_format(a
, format
);
410 archive_write_set_bytes_per_block(a
, bsdtar
->bytes_per_block
);
411 archive_write_set_bytes_in_last_block(a
, bsdtar
->bytes_in_last_block
);
413 if (lseek(bsdtar
->fd
, end_offset
, SEEK_SET
) < 0)
414 lafe_errc(1, errno
, "Could not seek to archive end");
415 set_writer_options(bsdtar
, a
);
416 if (ARCHIVE_OK
!= archive_write_open_fd(a
, bsdtar
->fd
))
417 lafe_errc(1, 0, "%s", archive_error_string(a
));
419 write_archive(a
, bsdtar
);
424 while (bsdtar
->archive_dir
->head
!= NULL
) {
425 p
= bsdtar
->archive_dir
->head
->next
;
426 free(bsdtar
->archive_dir
->head
->name
);
427 free(bsdtar
->archive_dir
->head
);
428 bsdtar
->archive_dir
->head
= p
;
430 bsdtar
->archive_dir
->tail
= NULL
;
435 * Write user-specified files/dirs to opened archive.
438 write_archive(struct archive
*a
, struct bsdtar
*bsdtar
)
441 struct archive_entry
*entry
, *sparse_entry
;
443 /* Choose a suitable copy buffer size */
444 bsdtar
->buff_size
= 64 * 1024;
445 while (bsdtar
->buff_size
< (size_t)bsdtar
->bytes_per_block
)
446 bsdtar
->buff_size
*= 2;
447 /* Try to compensate for space we'll lose to alignment. */
448 bsdtar
->buff_size
+= 16 * 1024;
450 /* Allocate a buffer for file data. */
451 if ((bsdtar
->buff
= malloc(bsdtar
->buff_size
)) == NULL
)
452 lafe_errc(1, 0, "cannot allocate memory");
454 if ((bsdtar
->resolver
= archive_entry_linkresolver_new()) == NULL
)
455 lafe_errc(1, 0, "cannot create link resolver");
456 archive_entry_linkresolver_set_strategy(bsdtar
->resolver
,
459 /* Create a read_disk object. */
460 if ((bsdtar
->diskreader
= archive_read_disk_new()) == NULL
)
461 lafe_errc(1, 0, "Cannot create read_disk object");
462 /* Tell the read_disk how handle symlink. */
463 switch (bsdtar
->symlink_mode
) {
465 archive_read_disk_set_symlink_hybrid(bsdtar
->diskreader
);
468 archive_read_disk_set_symlink_logical(bsdtar
->diskreader
);
471 archive_read_disk_set_symlink_physical(bsdtar
->diskreader
);
474 /* Register entry filters. */
475 archive_read_disk_set_matching(bsdtar
->diskreader
,
476 bsdtar
->matching
, excluded_callback
, bsdtar
);
477 archive_read_disk_set_metadata_filter_callback(
478 bsdtar
->diskreader
, metadata_filter
, bsdtar
);
479 /* Set the behavior of archive_read_disk. */
480 archive_read_disk_set_behavior(bsdtar
->diskreader
,
481 bsdtar
->readdisk_flags
);
482 archive_read_disk_set_standard_lookup(bsdtar
->diskreader
);
484 if (bsdtar
->names_from_file
!= NULL
)
485 archive_names_from_file(bsdtar
, a
);
487 while (*bsdtar
->argv
) {
489 if (arg
[0] == '-' && arg
[1] == 'C') {
496 "Missing argument for -C");
497 bsdtar
->return_value
= 1;
502 "Meaningless argument for -C: ''");
503 bsdtar
->return_value
= 1;
507 set_chdir(bsdtar
, arg
);
510 do_chdir(bsdtar
); /* Handle a deferred -C */
512 if (append_archive_filename(bsdtar
, a
,
516 write_hierarchy(bsdtar
, a
, arg
);
521 archive_read_disk_set_matching(bsdtar
->diskreader
, NULL
, NULL
, NULL
);
522 archive_read_disk_set_metadata_filter_callback(
523 bsdtar
->diskreader
, NULL
, NULL
);
525 archive_entry_linkify(bsdtar
->resolver
, &entry
, &sparse_entry
);
526 while (entry
!= NULL
) {
528 struct archive_entry
*entry2
;
529 struct archive
*disk
= bsdtar
->diskreader
;
532 * This tricky code here is to correctly read the contents
533 * of the entry because the disk reader bsdtar->diskreader
534 * is pointing at does not have any information about the
535 * entry by this time and using archive_read_data_block()
536 * with the disk reader consequently must fail. And we
537 * have to re-open the entry to read the contents.
539 /* TODO: Work with -C option as well. */
540 r
= archive_read_disk_open(disk
,
541 archive_entry_sourcepath(entry
));
542 if (r
!= ARCHIVE_OK
) {
543 lafe_warnc(archive_errno(disk
),
544 "%s", archive_error_string(disk
));
545 bsdtar
->return_value
= 1;
550 * Invoke archive_read_next_header2() to work
551 * archive_read_data_block(), which is called via write_file(),
554 entry2
= archive_entry_new();
555 r
= archive_read_next_header2(disk
, entry2
);
556 archive_entry_free(entry2
);
557 if (r
!= ARCHIVE_OK
) {
558 lafe_warnc(archive_errno(disk
),
559 "%s", archive_error_string(disk
));
560 if (r
== ARCHIVE_FATAL
)
561 bsdtar
->return_value
= 1;
562 archive_read_close(disk
);
566 write_file(bsdtar
, a
, entry
);
567 archive_read_close(disk
);
569 archive_entry_free(entry
);
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
->flags
& OPTFLAG_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
,
611 (bsdtar
->flags
& OPTFLAG_NULL
));
612 while ((line
= lafe_line_reader_next(lr
)) != NULL
) {
613 if (bsdtar
->next_line_is_dir
) {
615 set_chdir(bsdtar
, line
);
618 "Meaningless argument for -C: ''");
619 bsdtar
->return_value
= 1;
621 bsdtar
->next_line_is_dir
= 0;
622 } else if (((bsdtar
->flags
& OPTFLAG_NULL
) == 0) &&
623 strcmp(line
, "-C") == 0)
624 bsdtar
->next_line_is_dir
= 1;
627 do_chdir(bsdtar
); /* Handle a deferred -C */
628 write_hierarchy(bsdtar
, a
, line
);
631 lafe_line_reader_free(lr
);
632 if (bsdtar
->next_line_is_dir
)
634 "Unexpected end of filename list; "
635 "directory expected after -C");
639 * Copy from specified archive to current archive. Returns non-zero
640 * for write errors (which force us to terminate the entire archiving
641 * operation). If there are errors reading the input archive, we set
642 * bsdtar->return_value but return zero, so the overall archiving
643 * operation will complete and return non-zero.
646 append_archive_filename(struct bsdtar
*bsdtar
, struct archive
*a
,
647 const char *raw_filename
)
650 const char *filename
= raw_filename
;
653 if (strcmp(filename
, "-") == 0)
654 filename
= NULL
; /* Library uses NULL for stdio. */
656 ina
= archive_read_new();
657 archive_read_support_format_all(ina
);
658 archive_read_support_filter_all(ina
);
659 set_reader_options(bsdtar
, ina
);
660 archive_read_set_options(ina
, "mtree:checkfs");
661 if (bsdtar
->passphrase
!= NULL
)
662 rc
= archive_read_add_passphrase(a
, bsdtar
->passphrase
);
664 rc
= archive_read_set_passphrase_callback(ina
, bsdtar
,
665 &passphrase_callback
);
666 if (rc
!= ARCHIVE_OK
)
667 lafe_errc(1, 0, "%s", archive_error_string(a
));
668 if (archive_read_open_filename(ina
, filename
,
669 bsdtar
->bytes_per_block
)) {
670 lafe_warnc(0, "%s", archive_error_string(ina
));
671 bsdtar
->return_value
= 1;
675 rc
= append_archive(bsdtar
, a
, ina
);
677 if (rc
!= ARCHIVE_OK
) {
678 lafe_warnc(0, "Error reading archive %s: %s",
679 raw_filename
, archive_error_string(ina
));
680 bsdtar
->return_value
= 1;
682 archive_read_free(ina
);
688 append_archive(struct bsdtar
*bsdtar
, struct archive
*a
, struct archive
*ina
)
690 struct archive_entry
*in_entry
;
693 while (ARCHIVE_OK
== (e
= archive_read_next_header(ina
, &in_entry
))) {
694 if (archive_match_excluded(bsdtar
->matching
, in_entry
))
696 if(edit_pathname(bsdtar
, in_entry
))
698 if ((bsdtar
->flags
& OPTFLAG_INTERACTIVE
) &&
699 !yes("copy '%s'", archive_entry_pathname(in_entry
)))
701 if (bsdtar
->verbose
> 1) {
702 safe_fprintf(stderr
, "a ");
703 list_item_verbose(bsdtar
, stderr
, in_entry
);
704 } else if (bsdtar
->verbose
> 0)
705 safe_fprintf(stderr
, "a %s",
706 archive_entry_pathname(in_entry
));
708 report_write(bsdtar
, a
, in_entry
, 0);
710 e
= archive_write_header(a
, in_entry
);
711 if (e
!= ARCHIVE_OK
) {
712 if (!bsdtar
->verbose
)
713 lafe_warnc(0, "%s: %s",
714 archive_entry_pathname(in_entry
),
715 archive_error_string(a
));
717 fprintf(stderr
, ": %s", archive_error_string(a
));
719 if (e
== ARCHIVE_FATAL
)
722 if (e
>= ARCHIVE_WARN
) {
723 if (archive_entry_size(in_entry
) == 0)
724 archive_read_data_skip(ina
);
725 else if (copy_file_data_block(bsdtar
, a
, ina
, in_entry
))
730 fprintf(stderr
, "\n");
733 return (e
== ARCHIVE_EOF
? ARCHIVE_OK
: e
);
736 /* Helper function to copy file to archive. */
738 copy_file_data_block(struct bsdtar
*bsdtar
, struct archive
*a
,
739 struct archive
*in_a
, struct archive_entry
*entry
)
742 ssize_t bytes_written
;
743 int64_t offset
, progress
= 0;
744 char *null_buff
= NULL
;
748 while ((r
= archive_read_data_block(in_a
, &buff
,
749 &bytes_read
, &offset
)) == ARCHIVE_OK
) {
751 report_write(bsdtar
, a
, entry
, progress
);
753 if (offset
> progress
) {
754 int64_t sparse
= offset
- progress
;
757 if (null_buff
== NULL
) {
758 null_buff
= bsdtar
->buff
;
759 memset(null_buff
, 0, bsdtar
->buff_size
);
763 if (sparse
> (int64_t)bsdtar
->buff_size
)
764 ns
= bsdtar
->buff_size
;
768 archive_write_data(a
, null_buff
, ns
);
769 if (bytes_written
< 0) {
770 /* Write failed; this is bad */
772 archive_error_string(a
));
775 if ((size_t)bytes_written
< ns
) {
776 /* Write was truncated; warn but
779 "%s: Truncated write; file may "
780 "have grown while being archived.",
781 archive_entry_pathname(entry
));
784 progress
+= bytes_written
;
785 sparse
-= bytes_written
;
789 bytes_written
= archive_write_data(a
, buff
, bytes_read
);
790 if (bytes_written
< 0) {
791 /* Write failed; this is bad */
792 lafe_warnc(0, "%s", archive_error_string(a
));
795 if ((size_t)bytes_written
< bytes_read
) {
796 /* Write was truncated; warn but continue. */
798 "%s: Truncated write; file may have grown "
799 "while being archived.",
800 archive_entry_pathname(entry
));
803 progress
+= bytes_written
;
805 if (r
< ARCHIVE_WARN
) {
806 lafe_warnc(archive_errno(a
), "%s", archive_error_string(a
));
813 excluded_callback(struct archive
*a
, void *_data
, struct archive_entry
*entry
)
815 struct bsdtar
*bsdtar
= (struct bsdtar
*)_data
;
817 if (bsdtar
->flags
& OPTFLAG_NO_SUBDIRS
)
819 if (!archive_read_disk_can_descend(a
))
821 if ((bsdtar
->flags
& OPTFLAG_INTERACTIVE
) &&
822 !yes("add '%s'", archive_entry_pathname(entry
)))
824 archive_read_disk_descend(a
);
828 metadata_filter(struct archive
*a
, void *_data
, struct archive_entry
*entry
)
830 struct bsdtar
*bsdtar
= (struct bsdtar
*)_data
;
832 /* XXX TODO: check whether this filesystem is
833 * synthetic and/or local. Add a new
834 * --local-only option to skip non-local
835 * filesystems. Skip synthetic filesystems
838 * The results should be cached, since
839 * tree.c doesn't usually visit a directory
840 * and the directory contents together. A simple
841 * move-to-front list should perform quite well.
843 * Use archive_read_disk_current_filesystem_is_remote().
847 * If the user vetoes this file/directory, skip it.
848 * We want this to be fairly late; if some other
849 * check would veto this file, we shouldn't bother
852 if ((bsdtar
->flags
& OPTFLAG_INTERACTIVE
) &&
853 !yes("add '%s'", archive_entry_pathname(entry
)))
856 /* Note: if user vetoes, we won't descend. */
857 if (((bsdtar
->flags
& OPTFLAG_NO_SUBDIRS
) == 0) &&
858 archive_read_disk_can_descend(a
))
859 archive_read_disk_descend(a
);
865 * Add the file or dir hierarchy named by 'path' to the archive
868 write_hierarchy(struct bsdtar
*bsdtar
, struct archive
*a
, const char *path
)
870 struct archive
*disk
= bsdtar
->diskreader
;
871 struct archive_entry
*entry
= NULL
, *spare_entry
= NULL
;
874 r
= archive_read_disk_open(disk
, path
);
875 if (r
!= ARCHIVE_OK
) {
876 lafe_warnc(archive_errno(disk
),
877 "%s", archive_error_string(disk
));
878 bsdtar
->return_value
= 1;
881 bsdtar
->first_fs
= -1;
884 archive_entry_free(entry
);
885 entry
= archive_entry_new();
886 r
= archive_read_next_header2(disk
, entry
);
887 if (r
== ARCHIVE_EOF
)
889 else if (r
!= ARCHIVE_OK
) {
890 lafe_warnc(archive_errno(disk
),
891 "%s", archive_error_string(disk
));
892 if (r
== ARCHIVE_FATAL
|| r
== ARCHIVE_FAILED
) {
893 bsdtar
->return_value
= 1;
894 archive_entry_free(entry
);
895 archive_read_close(disk
);
897 } else if (r
< ARCHIVE_WARN
)
901 if (bsdtar
->uid
>= 0) {
902 archive_entry_set_uid(entry
, bsdtar
->uid
);
904 archive_entry_set_uname(entry
,
905 archive_read_disk_uname(bsdtar
->diskreader
,
908 if (bsdtar
->gid
>= 0) {
909 archive_entry_set_gid(entry
, bsdtar
->gid
);
911 archive_entry_set_gname(entry
,
912 archive_read_disk_gname(bsdtar
->diskreader
,
916 archive_entry_set_uname(entry
, bsdtar
->uname
);
918 archive_entry_set_gname(entry
, bsdtar
->gname
);
921 * Rewrite the pathname to be archived. If rewrite
922 * fails, skip the entry.
924 if (edit_pathname(bsdtar
, entry
))
927 /* Display entry as we process it. */
928 if (bsdtar
->verbose
> 1) {
929 safe_fprintf(stderr
, "a ");
930 list_item_verbose(bsdtar
, stderr
, entry
);
931 } else if (bsdtar
->verbose
> 0) {
932 /* This format is required by SUSv2. */
933 safe_fprintf(stderr
, "a %s",
934 archive_entry_pathname(entry
));
937 /* Non-regular files get archived with zero size. */
938 if (archive_entry_filetype(entry
) != AE_IFREG
)
939 archive_entry_set_size(entry
, 0);
941 archive_entry_linkify(bsdtar
->resolver
, &entry
, &spare_entry
);
943 while (entry
!= NULL
) {
944 write_file(bsdtar
, a
, entry
);
945 archive_entry_free(entry
);
951 fprintf(stderr
, "\n");
953 archive_entry_free(entry
);
954 archive_read_close(disk
);
958 * Write a single file (or directory or other filesystem object) to
962 write_file(struct bsdtar
*bsdtar
, struct archive
*a
,
963 struct archive_entry
*entry
)
965 write_entry(bsdtar
, a
, entry
);
969 * Write a single entry to the archive.
972 write_entry(struct bsdtar
*bsdtar
, struct archive
*a
,
973 struct archive_entry
*entry
)
977 e
= archive_write_header(a
, entry
);
978 if (e
!= ARCHIVE_OK
) {
979 if (bsdtar
->verbose
> 1) {
980 safe_fprintf(stderr
, "a ");
981 list_item_verbose(bsdtar
, stderr
, entry
);
982 lafe_warnc(0, ": %s", archive_error_string(a
));
983 } else if (bsdtar
->verbose
> 0) {
984 lafe_warnc(0, "%s: %s",
985 archive_entry_pathname(entry
),
986 archive_error_string(a
));
988 fprintf(stderr
, ": %s", archive_error_string(a
));
991 if (e
== ARCHIVE_FATAL
)
995 * If we opened a file earlier, write it out now. Note that
996 * the format handler might have reset the size field to zero
997 * to inform us that the archive body won't get stored. In
998 * that case, just skip the write.
1000 if (e
>= ARCHIVE_WARN
&& archive_entry_size(entry
) > 0) {
1001 if (copy_file_data_block(bsdtar
, a
, bsdtar
->diskreader
, entry
))
1007 report_write(struct bsdtar
*bsdtar
, struct archive
*a
,
1008 struct archive_entry
*entry
, int64_t progress
)
1010 uint64_t comp
, uncomp
;
1013 if (bsdtar
->verbose
)
1014 fprintf(stderr
, "\n");
1015 comp
= archive_filter_bytes(a
, -1);
1016 uncomp
= archive_filter_bytes(a
, 0);
1017 fprintf(stderr
, "In: %d files, %s bytes;",
1018 archive_file_count(a
), tar_i64toa(uncomp
));
1022 compression
= (int)((uncomp
- comp
) * 100 / uncomp
);
1024 " Out: %s bytes, compression %d%%\n",
1025 tar_i64toa(comp
), compression
);
1026 /* Can't have two calls to tar_i64toa() pending, so split the output. */
1027 safe_fprintf(stderr
, "Current: %s (%s",
1028 archive_entry_pathname(entry
),
1029 tar_i64toa(progress
));
1030 fprintf(stderr
, "/%s bytes)\n",
1031 tar_i64toa(archive_entry_size(entry
)));
1035 test_for_append(struct bsdtar
*bsdtar
)
1039 if (*bsdtar
->argv
== NULL
&& bsdtar
->names_from_file
== NULL
)
1040 lafe_errc(1, 0, "no files or directories specified");
1041 if (bsdtar
->filename
== NULL
)
1042 lafe_errc(1, 0, "Cannot append to stdout.");
1044 if (stat(bsdtar
->filename
, &s
) != 0)
1047 if (!S_ISREG(s
.st_mode
) && !S_ISBLK(s
.st_mode
))
1049 "Cannot append to %s: not a regular file.",
1052 /* Is this an appropriate check here on Windows? */
1054 if (GetFileType(handle) != FILE_TYPE_DISK)
1055 lafe_errc(1, 0, "Cannot append");