Import libarchive-3.0.4.
[dragonfly.git] / contrib / libarchive / tar / write.c
1 /*-
2  * Copyright (c) 2003-2007 Tim Kientzle
3  * Copyright (c) 2012 Michihiro NAKAJIMA
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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.
25  */
26
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 $");
29
30 #ifdef HAVE_SYS_TYPES_H
31 #include <sys/types.h>
32 #endif
33 #ifdef HAVE_SYS_STAT_H
34 #include <sys/stat.h>
35 #endif
36 #ifdef HAVE_ATTR_XATTR_H
37 #include <attr/xattr.h>
38 #endif
39 #ifdef HAVE_ERRNO_H
40 #include <errno.h>
41 #endif
42 #ifdef HAVE_FCNTL_H
43 #include <fcntl.h>
44 #endif
45 #ifdef HAVE_GRP_H
46 #include <grp.h>
47 #endif
48 #ifdef HAVE_IO_H
49 #include <io.h>
50 #endif
51 #ifdef HAVE_LIBGEN_H
52 #include <libgen.h>
53 #endif
54 #ifdef HAVE_LIMITS_H
55 #include <limits.h>
56 #endif
57 #ifdef HAVE_PATHS_H
58 #include <paths.h>
59 #endif
60 #ifdef HAVE_PWD_H
61 #include <pwd.h>
62 #endif
63 #ifdef HAVE_STDINT_H
64 #include <stdint.h>
65 #endif
66 #include <stdio.h>
67 #ifdef HAVE_STDLIB_H
68 #include <stdlib.h>
69 #endif
70 #ifdef HAVE_STRING_H
71 #include <string.h>
72 #endif
73 #ifdef HAVE_UNISTD_H
74 #include <unistd.h>
75 #endif
76
77 #include "bsdtar.h"
78 #include "err.h"
79 #include "line_reader.h"
80
81 #ifndef O_BINARY
82 #define O_BINARY 0
83 #endif
84
85 struct archive_dir_entry {
86         struct archive_dir_entry        *next;
87         time_t                   mtime_sec;
88         int                      mtime_nsec;
89         char                    *name;
90 };
91
92 struct archive_dir {
93         struct archive_dir_entry *head, *tail;
94 };
95
96 static int               append_archive(struct bsdtar *, struct archive *,
97                              struct archive *ina);
98 static int               append_archive_filename(struct bsdtar *,
99                              struct archive *, const char *fname);
100 static void              archive_names_from_file(struct bsdtar *bsdtar,
101                              struct archive *a);
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 *,
118                              const char *);
119
120 #if defined(_WIN32) && !defined(__CYGWIN__)
121 /* Not a full lseek() emulation, but enough for our needs here. */
122 static int
123 seek_file(int fd, int64_t offset, int whence)
124 {
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);
130 }
131 #define open _open
132 #define close _close
133 #define read _read
134 #define lseek seek_file
135 #endif
136
137 void
138 tar_mode_c(struct bsdtar *bsdtar)
139 {
140         struct archive *a;
141         int r;
142
143         if (*bsdtar->argv == NULL && bsdtar->names_from_file == NULL)
144                 lafe_errc(1, 0, "no files or directories specified");
145
146         a = archive_write_new();
147
148         /* Support any format that the library supports. */
149         if (bsdtar->create_format == NULL) {
150                 r = archive_write_set_format_pax_restricted(a);
151                 bsdtar->create_format = "pax restricted";
152         } else {
153                 r = archive_write_set_format_by_name(a, bsdtar->create_format);
154         }
155         if (r != ARCHIVE_OK) {
156                 fprintf(stderr, "Can't use format %s: %s\n",
157                     bsdtar->create_format,
158                     archive_error_string(a));
159                 usage();
160         }
161
162         archive_write_set_bytes_per_block(a, bsdtar->bytes_per_block);
163         archive_write_set_bytes_in_last_block(a, bsdtar->bytes_in_last_block);
164
165         if (bsdtar->compress_program) {
166                 archive_write_set_compression_program(a, bsdtar->compress_program);
167         } else {
168                 switch (bsdtar->create_compression) {
169                 case 0:
170                         r = ARCHIVE_OK;
171                         break;
172                 case 'j': case 'y':
173                         r = archive_write_set_compression_bzip2(a);
174                         break;
175                 case 'J':
176                         r = archive_write_set_compression_xz(a);
177                         break;
178                 case OPTION_LZIP:
179                         r = archive_write_set_compression_lzip(a);
180                         break;
181                 case OPTION_LZMA:
182                         r = archive_write_set_compression_lzma(a);
183                         break;
184                 case 'z':
185                         r = archive_write_set_compression_gzip(a);
186                         break;
187                 case 'Z':
188                         r = archive_write_set_compression_compress(a);
189                         break;
190                 default:
191                         lafe_errc(1, 0,
192                             "Unrecognized compression option -%c",
193                             bsdtar->create_compression);
194                 }
195                 if (r != ARCHIVE_OK) {
196                         lafe_errc(1, 0,
197                             "Unsupported compression option -%c",
198                             bsdtar->create_compression);
199                 }
200         }
201
202         if (ARCHIVE_OK != archive_write_set_options(a, bsdtar->option_options))
203                 lafe_errc(1, 0, "%s", archive_error_string(a));
204         if (ARCHIVE_OK != archive_write_open_file(a, bsdtar->filename))
205                 lafe_errc(1, 0, "%s", archive_error_string(a));
206         write_archive(a, bsdtar);
207 }
208
209 /*
210  * Same as 'c', except we only support tar or empty formats in
211  * uncompressed files on disk.
212  */
213 void
214 tar_mode_r(struct bsdtar *bsdtar)
215 {
216         int64_t end_offset;
217         int     format;
218         struct archive *a;
219         struct archive_entry *entry;
220         int     r;
221
222         /* Sanity-test some arguments and the file. */
223         test_for_append(bsdtar);
224
225         format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
226
227 #if defined(__BORLANDC__)
228         bsdtar->fd = open(bsdtar->filename, O_RDWR | O_CREAT | O_BINARY);
229 #else
230         bsdtar->fd = open(bsdtar->filename, O_RDWR | O_CREAT | O_BINARY, 0666);
231 #endif
232         if (bsdtar->fd < 0)
233                 lafe_errc(1, errno,
234                     "Cannot open %s", bsdtar->filename);
235
236         a = archive_read_new();
237         archive_read_support_filter_all(a);
238         archive_read_support_format_tar(a);
239         archive_read_support_format_gnutar(a);
240         r = archive_read_open_fd(a, bsdtar->fd, 10240);
241         if (r != ARCHIVE_OK)
242                 lafe_errc(1, archive_errno(a),
243                     "Can't read archive %s: %s", bsdtar->filename,
244                     archive_error_string(a));
245         while (0 == archive_read_next_header(a, &entry)) {
246                 if (archive_compression(a) != ARCHIVE_COMPRESSION_NONE) {
247                         archive_read_free(a);
248                         close(bsdtar->fd);
249                         lafe_errc(1, 0,
250                             "Cannot append to compressed archive.");
251                 }
252                 /* Keep going until we hit end-of-archive */
253                 format = archive_format(a);
254         }
255
256         end_offset = archive_read_header_position(a);
257         archive_read_free(a);
258
259         /* Re-open archive for writing */
260         a = archive_write_new();
261         /*
262          * Set the format to be used for writing.  To allow people to
263          * extend empty files, we need to allow them to specify the format,
264          * which opens the possibility that they will specify a format that
265          * doesn't match the existing format.  Hence, the following bit
266          * of arcane ugliness.
267          */
268
269         if (bsdtar->create_format != NULL) {
270                 /* If the user requested a format, use that, but ... */
271                 archive_write_set_format_by_name(a,
272                     bsdtar->create_format);
273                 /* ... complain if it's not compatible. */
274                 format &= ARCHIVE_FORMAT_BASE_MASK;
275                 if (format != (int)(archive_format(a) & ARCHIVE_FORMAT_BASE_MASK)
276                     && format != ARCHIVE_FORMAT_EMPTY) {
277                         lafe_errc(1, 0,
278                             "Format %s is incompatible with the archive %s.",
279                             bsdtar->create_format, bsdtar->filename);
280                 }
281         } else {
282                 /*
283                  * Just preserve the current format, with a little care
284                  * for formats that libarchive can't write.
285                  */
286                 if (format == ARCHIVE_FORMAT_EMPTY)
287                         format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
288                 archive_write_set_format(a, format);
289         }
290         if (lseek(bsdtar->fd, end_offset, SEEK_SET) < 0)
291                 lafe_errc(1, errno, "Could not seek to archive end");
292         if (ARCHIVE_OK != archive_write_set_options(a, bsdtar->option_options))
293                 lafe_errc(1, 0, "%s", archive_error_string(a));
294         if (ARCHIVE_OK != archive_write_open_fd(a, bsdtar->fd))
295                 lafe_errc(1, 0, "%s", archive_error_string(a));
296
297         write_archive(a, bsdtar); /* XXX check return val XXX */
298
299         close(bsdtar->fd);
300         bsdtar->fd = -1;
301 }
302
303 void
304 tar_mode_u(struct bsdtar *bsdtar)
305 {
306         int64_t                  end_offset;
307         struct archive          *a;
308         struct archive_entry    *entry;
309         int                      format;
310         struct archive_dir_entry        *p;
311         struct archive_dir       archive_dir;
312
313         bsdtar->archive_dir = &archive_dir;
314         memset(&archive_dir, 0, sizeof(archive_dir));
315
316         format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
317
318         /* Sanity-test some arguments and the file. */
319         test_for_append(bsdtar);
320
321         bsdtar->fd = open(bsdtar->filename, O_RDWR | O_BINARY);
322         if (bsdtar->fd < 0)
323                 lafe_errc(1, errno,
324                     "Cannot open %s", bsdtar->filename);
325
326         a = archive_read_new();
327         archive_read_support_filter_all(a);
328         archive_read_support_format_tar(a);
329         archive_read_support_format_gnutar(a);
330         if (archive_read_open_fd(a, bsdtar->fd, bsdtar->bytes_per_block)
331             != ARCHIVE_OK) {
332                 lafe_errc(1, 0,
333                     "Can't open %s: %s", bsdtar->filename,
334                     archive_error_string(a));
335         }
336
337         /* Build a list of all entries and their recorded mod times. */
338         while (0 == archive_read_next_header(a, &entry)) {
339                 if (archive_compression(a) != ARCHIVE_COMPRESSION_NONE) {
340                         archive_read_free(a);
341                         close(bsdtar->fd);
342                         lafe_errc(1, 0,
343                             "Cannot append to compressed archive.");
344                 }
345                 if (archive_match_exclude_entry(bsdtar->matching,
346                     ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_OLDER |
347                     ARCHIVE_MATCH_EQUAL, entry) != ARCHIVE_OK)
348                         lafe_errc(1, 0, "Error : %s",
349                             archive_error_string(bsdtar->matching));
350                 /* Record the last format determination we see */
351                 format = archive_format(a);
352                 /* Keep going until we hit end-of-archive */
353         }
354
355         end_offset = archive_read_header_position(a);
356         archive_read_free(a);
357
358         /* Re-open archive for writing. */
359         a = archive_write_new();
360         /*
361          * Set format to same one auto-detected above.
362          */
363         archive_write_set_format(a, format);
364         archive_write_set_bytes_per_block(a, bsdtar->bytes_per_block);
365         archive_write_set_bytes_in_last_block(a, bsdtar->bytes_in_last_block);
366
367         if (lseek(bsdtar->fd, end_offset, SEEK_SET) < 0)
368                 lafe_errc(1, errno, "Could not seek to archive end");
369         if (ARCHIVE_OK != archive_write_set_options(a, bsdtar->option_options))
370                 lafe_errc(1, 0, "%s", archive_error_string(a));
371         if (ARCHIVE_OK != archive_write_open_fd(a, bsdtar->fd))
372                 lafe_errc(1, 0, "%s", archive_error_string(a));
373
374         write_archive(a, bsdtar);
375
376         close(bsdtar->fd);
377         bsdtar->fd = -1;
378
379         while (bsdtar->archive_dir->head != NULL) {
380                 p = bsdtar->archive_dir->head->next;
381                 free(bsdtar->archive_dir->head->name);
382                 free(bsdtar->archive_dir->head);
383                 bsdtar->archive_dir->head = p;
384         }
385         bsdtar->archive_dir->tail = NULL;
386 }
387
388
389 /*
390  * Write user-specified files/dirs to opened archive.
391  */
392 static void
393 write_archive(struct archive *a, struct bsdtar *bsdtar)
394 {
395         const char *arg;
396         struct archive_entry *entry, *sparse_entry;
397
398         /* Choose a suitable copy buffer size */
399         bsdtar->buff_size = 64 * 1024;
400         while (bsdtar->buff_size < (size_t)bsdtar->bytes_per_block)
401           bsdtar->buff_size *= 2;
402         /* Try to compensate for space we'll lose to alignment. */
403         bsdtar->buff_size += 16 * 1024;
404
405         /* Allocate a buffer for file data. */
406         if ((bsdtar->buff = malloc(bsdtar->buff_size)) == NULL)
407                 lafe_errc(1, 0, "cannot allocate memory");
408
409         if ((bsdtar->resolver = archive_entry_linkresolver_new()) == NULL)
410                 lafe_errc(1, 0, "cannot create link resolver");
411         archive_entry_linkresolver_set_strategy(bsdtar->resolver,
412             archive_format(a));
413
414         /* Create a read_disk object. */
415         if ((bsdtar->diskreader = archive_read_disk_new()) == NULL)
416                 lafe_errc(1, 0, "Cannot create read_disk object");
417         /* Tell the read_disk how handle symlink. */
418         switch (bsdtar->symlink_mode) {
419         case 'H':
420                 archive_read_disk_set_symlink_hybrid(bsdtar->diskreader);
421                 break;
422         case 'L':
423                 archive_read_disk_set_symlink_logical(bsdtar->diskreader);
424                 break;
425         default:
426                 archive_read_disk_set_symlink_physical(bsdtar->diskreader);
427                 break;
428         }
429         /* Register entry filters. */
430         archive_read_disk_set_matching(bsdtar->diskreader,
431             bsdtar->matching, excluded_callback, bsdtar);
432         archive_read_disk_set_metadata_filter_callback(
433             bsdtar->diskreader, metadata_filter, bsdtar);
434         /* Set the behavior of archive_read_disk. */
435         archive_read_disk_set_behavior(bsdtar->diskreader,
436             bsdtar->readdisk_flags);
437         archive_read_disk_set_standard_lookup(bsdtar->diskreader);
438
439         if (bsdtar->names_from_file != NULL)
440                 archive_names_from_file(bsdtar, a);
441
442         while (*bsdtar->argv) {
443                 arg = *bsdtar->argv;
444                 if (arg[0] == '-' && arg[1] == 'C') {
445                         arg += 2;
446                         if (*arg == '\0') {
447                                 bsdtar->argv++;
448                                 arg = *bsdtar->argv;
449                                 if (arg == NULL) {
450                                         lafe_warnc(0, "%s",
451                                             "Missing argument for -C");
452                                         bsdtar->return_value = 1;
453                                         goto cleanup;
454                                 }
455                                 if (*arg == '\0') {
456                                         lafe_warnc(0,
457                                             "Meaningless argument for -C: ''");
458                                         bsdtar->return_value = 1;
459                                         goto cleanup;
460                                 }
461                         }
462                         set_chdir(bsdtar, arg);
463                 } else {
464                         if (*arg != '/' && (arg[0] != '@' || arg[1] != '/'))
465                                 do_chdir(bsdtar); /* Handle a deferred -C */
466                         if (*arg == '@') {
467                                 if (append_archive_filename(bsdtar, a,
468                                     arg + 1) != 0)
469                                         break;
470                         } else
471                                 write_hierarchy(bsdtar, a, arg);
472                 }
473                 bsdtar->argv++;
474         }
475
476         archive_read_disk_set_matching(bsdtar->diskreader, NULL, NULL, NULL);
477         archive_read_disk_set_metadata_filter_callback(
478             bsdtar->diskreader, NULL, NULL);
479         entry = NULL;
480         archive_entry_linkify(bsdtar->resolver, &entry, &sparse_entry);
481         while (entry != NULL) {
482                 int r;
483                 struct archive_entry *entry2;
484                 struct archive *disk = bsdtar->diskreader;
485
486                 /*
487                  * This tricky code here is to correctly read the cotents
488                  * of the entry because the disk reader bsdtar->diskreader
489                  * is pointing at does not have any information about the
490                  * entry by this time and using archive_read_data_block()
491                  * with the disk reader consequently must fail. And we
492                  * have to re-open the entry to read the contents.
493                  */
494                 /* TODO: Work with -C option as well. */
495                 r = archive_read_disk_open(disk,
496                         archive_entry_sourcepath(entry));
497                 if (r != ARCHIVE_OK) {
498                         lafe_warnc(archive_errno(disk),
499                             "%s", archive_error_string(disk));
500                         bsdtar->return_value = 1;
501                         archive_entry_free(entry);
502                         continue;
503                 }
504
505                 /*
506                  * Invoke archive_read_next_header2() to work
507                  * archive_read_data_block(), which is called via write_file(),
508                  * without failure.
509                  */
510                 entry2 = archive_entry_new();
511                 r = archive_read_next_header2(disk, entry2);
512                 archive_entry_free(entry2);
513                 if (r != ARCHIVE_OK) {
514                         lafe_warnc(archive_errno(disk),
515                             "%s", archive_error_string(disk));
516                         if (r == ARCHIVE_FATAL)
517                                 bsdtar->return_value = 1;
518                         else
519                                 archive_read_close(disk);
520                         archive_entry_free(entry);
521                         continue;
522                 }
523
524                 write_file(bsdtar, a, entry);
525                 archive_entry_free(entry);
526                 archive_read_close(disk);
527                 entry = NULL;
528                 archive_entry_linkify(bsdtar->resolver, &entry, &sparse_entry);
529         }
530
531         if (archive_write_close(a)) {
532                 lafe_warnc(0, "%s", archive_error_string(a));
533                 bsdtar->return_value = 1;
534         }
535
536 cleanup:
537         /* Free file data buffer. */
538         free(bsdtar->buff);
539         archive_entry_linkresolver_free(bsdtar->resolver);
540         bsdtar->resolver = NULL;
541         archive_read_free(bsdtar->diskreader);
542         bsdtar->diskreader = NULL;
543
544         if (bsdtar->option_totals) {
545                 fprintf(stderr, "Total bytes written: %s\n",
546                     tar_i64toa(archive_position_compressed(a)));
547         }
548
549         archive_write_free(a);
550 }
551
552 /*
553  * Archive names specified in file.
554  *
555  * Unless --null was specified, a line containing exactly "-C" will
556  * cause the next line to be a directory to pass to chdir().  If
557  * --null is specified, then a line "-C" is just another filename.
558  */
559 static void
560 archive_names_from_file(struct bsdtar *bsdtar, struct archive *a)
561 {
562         struct lafe_line_reader *lr;
563         const char *line;
564
565         bsdtar->next_line_is_dir = 0;
566
567         lr = lafe_line_reader(bsdtar->names_from_file, bsdtar->option_null);
568         while ((line = lafe_line_reader_next(lr)) != NULL) {
569                 if (bsdtar->next_line_is_dir) {
570                         if (*line != '\0')
571                                 set_chdir(bsdtar, line);
572                         else {
573                                 lafe_warnc(0,
574                                     "Meaningless argument for -C: ''");
575                                 bsdtar->return_value = 1;
576                         }
577                         bsdtar->next_line_is_dir = 0;
578                 } else if (!bsdtar->option_null && strcmp(line, "-C") == 0)
579                         bsdtar->next_line_is_dir = 1;
580                 else {
581                         if (*line != '/')
582                                 do_chdir(bsdtar); /* Handle a deferred -C */
583                         write_hierarchy(bsdtar, a, line);
584                 }
585         }
586         lafe_line_reader_free(lr);
587         if (bsdtar->next_line_is_dir)
588                 lafe_errc(1, errno,
589                     "Unexpected end of filename list; "
590                     "directory expected after -C");
591 }
592
593 /*
594  * Copy from specified archive to current archive.  Returns non-zero
595  * for write errors (which force us to terminate the entire archiving
596  * operation).  If there are errors reading the input archive, we set
597  * bsdtar->return_value but return zero, so the overall archiving
598  * operation will complete and return non-zero.
599  */
600 static int
601 append_archive_filename(struct bsdtar *bsdtar, struct archive *a,
602     const char *raw_filename)
603 {
604         struct archive *ina;
605         const char *filename = raw_filename;
606         int rc;
607
608         if (strcmp(filename, "-") == 0)
609                 filename = NULL; /* Library uses NULL for stdio. */
610
611         ina = archive_read_new();
612         archive_read_support_format_all(ina);
613         archive_read_support_filter_all(ina);
614         if (archive_read_open_file(ina, filename, bsdtar->bytes_per_block)) {
615                 lafe_warnc(0, "%s", archive_error_string(ina));
616                 bsdtar->return_value = 1;
617                 return (0);
618         }
619
620         rc = append_archive(bsdtar, a, ina);
621
622         if (rc != ARCHIVE_OK) {
623                 lafe_warnc(0, "Error reading archive %s: %s",
624                     raw_filename, archive_error_string(ina));
625                 bsdtar->return_value = 1;
626         }
627         archive_read_free(ina);
628
629         return (rc);
630 }
631
632 static int
633 append_archive(struct bsdtar *bsdtar, struct archive *a, struct archive *ina)
634 {
635         struct archive_entry *in_entry;
636         int e;
637
638         while (ARCHIVE_OK == (e = archive_read_next_header(ina, &in_entry))) {
639                 if (archive_match_excluded(bsdtar->matching, in_entry))
640                         continue;
641                 if (bsdtar->option_interactive &&
642                     !yes("copy '%s'", archive_entry_pathname(in_entry)))
643                         continue;
644                 if (bsdtar->verbose)
645                         safe_fprintf(stderr, "a %s",
646                             archive_entry_pathname(in_entry));
647                 if (need_report())
648                         report_write(bsdtar, a, in_entry, 0);
649
650                 e = archive_write_header(a, in_entry);
651                 if (e != ARCHIVE_OK) {
652                         if (!bsdtar->verbose)
653                                 lafe_warnc(0, "%s: %s",
654                                     archive_entry_pathname(in_entry),
655                                     archive_error_string(a));
656                         else
657                                 fprintf(stderr, ": %s", archive_error_string(a));
658                 }
659                 if (e == ARCHIVE_FATAL)
660                         exit(1);
661
662                 if (e >= ARCHIVE_WARN) {
663                         if (archive_entry_size(in_entry) == 0)
664                                 archive_read_data_skip(ina);
665                         else if (copy_file_data_block(bsdtar, a, ina, in_entry))
666                                 exit(1);
667                 }
668
669                 if (bsdtar->verbose)
670                         fprintf(stderr, "\n");
671         }
672
673         return (e == ARCHIVE_EOF ? ARCHIVE_OK : e);
674 }
675
676 /* Helper function to copy file to archive. */
677 static int
678 copy_file_data_block(struct bsdtar *bsdtar, struct archive *a,
679     struct archive *in_a, struct archive_entry *entry)
680 {
681         size_t  bytes_read;
682         ssize_t bytes_written;
683         int64_t offset, progress = 0;
684         char *null_buff = NULL;
685         const void *buff;
686         int r;
687
688         while ((r = archive_read_data_block(in_a, &buff,
689             &bytes_read, &offset)) == ARCHIVE_OK) {
690                 if (need_report())
691                         report_write(bsdtar, a, entry, progress);
692
693                 if (offset > progress) {
694                         int64_t sparse = offset - progress;
695                         size_t ns;
696
697                         if (null_buff == NULL) {
698                                 null_buff = bsdtar->buff;
699                                 memset(null_buff, 0, bsdtar->buff_size);
700                         }
701
702                         while (sparse > 0) {
703                                 if (sparse > (int64_t)bsdtar->buff_size)
704                                         ns = bsdtar->buff_size;
705                                 else
706                                         ns = (size_t)sparse;
707                                 bytes_written =
708                                     archive_write_data(a, null_buff, ns);
709                                 if (bytes_written < 0) {
710                                         /* Write failed; this is bad */
711                                         lafe_warnc(0, "%s",
712                                              archive_error_string(a));
713                                         return (-1);
714                                 }
715                                 if ((size_t)bytes_written < ns) {
716                                         /* Write was truncated; warn but
717                                          * continue. */
718                                         lafe_warnc(0,
719                                             "%s: Truncated write; file may "
720                                             "have grown while being archived.",
721                                             archive_entry_pathname(entry));
722                                         return (0);
723                                 }
724                                 progress += bytes_written;
725                                 sparse -= bytes_written;
726                         }
727                 }
728
729                 bytes_written = archive_write_data(a, buff, bytes_read);
730                 if (bytes_written < 0) {
731                         /* Write failed; this is bad */
732                         lafe_warnc(0, "%s", archive_error_string(a));
733                         return (-1);
734                 }
735                 if ((size_t)bytes_written < bytes_read) {
736                         /* Write was truncated; warn but continue. */
737                         lafe_warnc(0,
738                             "%s: Truncated write; file may have grown "
739                             "while being archived.",
740                             archive_entry_pathname(entry));
741                         return (0);
742                 }
743                 progress += bytes_written;
744         }
745         if (r < ARCHIVE_WARN) {
746                 lafe_warnc(archive_errno(a), "%s", archive_error_string(a));
747                 return (-1);
748         }
749         return (0);
750 }
751
752 static void
753 excluded_callback(struct archive *a, void *_data, struct archive_entry *entry)
754 {
755         struct bsdtar *bsdtar = (struct bsdtar *)_data;
756
757         if (bsdtar->option_no_subdirs)
758                 return;
759         if (!archive_read_disk_can_descend(a))
760                 return;
761         if (bsdtar->option_interactive &&
762             !yes("add '%s'", archive_entry_pathname(entry)))
763                 return;
764         archive_read_disk_descend(a);
765 }
766
767 static int
768 metadata_filter(struct archive *a, void *_data, struct archive_entry *entry)
769 {
770         struct bsdtar *bsdtar = (struct bsdtar *)_data;
771
772         /* XXX TODO: check whether this filesystem is
773          * synthetic and/or local.  Add a new
774          * --local-only option to skip non-local
775          * filesystems.  Skip synthetic filesystems
776          * regardless.
777          *
778          * The results should be cached, since
779          * tree.c doesn't usually visit a directory
780          * and the directory contents together.  A simple
781          * move-to-front list should perform quite well.
782          *
783          * Use archive_read_disk_current_filesystem_is_remote().
784          */
785
786         /*
787          * If the user vetoes this file/directory, skip it.
788          * We want this to be fairly late; if some other
789          * check would veto this file, we shouldn't bother
790          * the user with it.
791          */
792         if (bsdtar->option_interactive &&
793             !yes("add '%s'", archive_entry_pathname(entry)))
794                 return (0);
795
796         /* Note: if user vetoes, we won't descend. */
797         if (!bsdtar->option_no_subdirs && archive_read_disk_can_descend(a))
798                 archive_read_disk_descend(a);
799
800         return (1);
801 }
802
803 /*
804  * Add the file or dir hierarchy named by 'path' to the archive
805  */
806 static void
807 write_hierarchy(struct bsdtar *bsdtar, struct archive *a, const char *path)
808 {
809         struct archive *disk = bsdtar->diskreader;
810         struct archive_entry *entry = NULL, *spare_entry = NULL;
811         int r;
812
813         r = archive_read_disk_open(disk, path);
814         if (r != ARCHIVE_OK) {
815                 lafe_warnc(archive_errno(disk),
816                     "%s", archive_error_string(disk));
817                 bsdtar->return_value = 1;
818                 return;
819         }
820         bsdtar->first_fs = -1;
821
822         for (;;) {
823                 archive_entry_free(entry);
824                 entry = archive_entry_new();
825                 r = archive_read_next_header2(disk, entry);
826                 if (r == ARCHIVE_EOF)
827                         break;
828                 else if (r != ARCHIVE_OK) {
829                         lafe_warnc(archive_errno(disk),
830                             "%s", archive_error_string(disk));
831                         if (r == ARCHIVE_FATAL) {
832                                 bsdtar->return_value = 1;
833                                 return;
834                         } else if (r < ARCHIVE_WARN)
835                                 continue;
836                 }
837
838                 if (bsdtar->uid >= 0) {
839                         archive_entry_set_uid(entry, bsdtar->uid);
840                         if (!bsdtar->uname)
841                                 archive_entry_set_uname(entry,
842                                     archive_read_disk_uname(bsdtar->diskreader,
843                                         bsdtar->uid));
844                 }
845                 if (bsdtar->gid >= 0) {
846                         archive_entry_set_gid(entry, bsdtar->gid);
847                         if (!bsdtar->gname)
848                                 archive_entry_set_gname(entry,
849                                     archive_read_disk_gname(bsdtar->diskreader,
850                                         bsdtar->gid));
851                 }
852                 if (bsdtar->uname)
853                         archive_entry_set_uname(entry, bsdtar->uname);
854                 if (bsdtar->gname)
855                         archive_entry_set_gname(entry, bsdtar->gname);
856
857                 /*
858                  * Rewrite the pathname to be archived.  If rewrite
859                  * fails, skip the entry.
860                  */
861                 if (edit_pathname(bsdtar, entry))
862                         continue;
863
864                 /* Display entry as we process it.
865                  * This format is required by SUSv2. */
866                 if (bsdtar->verbose)
867                         safe_fprintf(stderr, "a %s",
868                             archive_entry_pathname(entry));
869
870                 /* Non-regular files get archived with zero size. */
871                 if (archive_entry_filetype(entry) != AE_IFREG)
872                         archive_entry_set_size(entry, 0);
873
874                 archive_entry_linkify(bsdtar->resolver, &entry, &spare_entry);
875
876                 while (entry != NULL) {
877                         write_file(bsdtar, a, entry);
878                         archive_entry_free(entry);
879                         entry = spare_entry;
880                         spare_entry = NULL;
881                 }
882
883                 if (bsdtar->verbose)
884                         fprintf(stderr, "\n");
885         }
886         archive_entry_free(entry);
887         archive_read_close(disk);
888 }
889
890 /*
891  * Write a single file (or directory or other filesystem object) to
892  * the archive.
893  */
894 static void
895 write_file(struct bsdtar *bsdtar, struct archive *a,
896     struct archive_entry *entry)
897 {
898         write_entry(bsdtar, a, entry);
899 }
900
901 /*
902  * Write a single entry to the archive.
903  */
904 static void
905 write_entry(struct bsdtar *bsdtar, struct archive *a,
906     struct archive_entry *entry)
907 {
908         int e;
909
910         e = archive_write_header(a, entry);
911         if (e != ARCHIVE_OK) {
912                 if (!bsdtar->verbose)
913                         lafe_warnc(0, "%s: %s",
914                             archive_entry_pathname(entry),
915                             archive_error_string(a));
916                 else
917                         fprintf(stderr, ": %s", archive_error_string(a));
918         }
919
920         if (e == ARCHIVE_FATAL)
921                 exit(1);
922
923         /*
924          * If we opened a file earlier, write it out now.  Note that
925          * the format handler might have reset the size field to zero
926          * to inform us that the archive body won't get stored.  In
927          * that case, just skip the write.
928          */
929         if (e >= ARCHIVE_WARN && archive_entry_size(entry) > 0) {
930                 if (copy_file_data_block(bsdtar, a, bsdtar->diskreader, entry))
931                         exit(1);
932         }
933 }
934
935 static void
936 report_write(struct bsdtar *bsdtar, struct archive *a,
937     struct archive_entry *entry, int64_t progress)
938 {
939         uint64_t comp, uncomp;
940         int compression;
941
942         if (bsdtar->verbose)
943                 fprintf(stderr, "\n");
944         comp = archive_position_compressed(a);
945         uncomp = archive_position_uncompressed(a);
946         fprintf(stderr, "In: %d files, %s bytes;",
947             archive_file_count(a), tar_i64toa(uncomp));
948         if (comp > uncomp)
949                 compression = 0;
950         else
951                 compression = (int)((uncomp - comp) * 100 / uncomp);
952         fprintf(stderr,
953             " Out: %s bytes, compression %d%%\n",
954             tar_i64toa(comp), compression);
955         /* Can't have two calls to tar_i64toa() pending, so split the output. */
956         safe_fprintf(stderr, "Current: %s (%s",
957             archive_entry_pathname(entry),
958             tar_i64toa(progress));
959         fprintf(stderr, "/%s bytes)\n",
960             tar_i64toa(archive_entry_size(entry)));
961 }
962
963 static void
964 test_for_append(struct bsdtar *bsdtar)
965 {
966         struct stat s;
967
968         if (*bsdtar->argv == NULL && bsdtar->names_from_file == NULL)
969                 lafe_errc(1, 0, "no files or directories specified");
970         if (bsdtar->filename == NULL)
971                 lafe_errc(1, 0, "Cannot append to stdout.");
972
973         if (bsdtar->create_compression != 0)
974                 lafe_errc(1, 0,
975                     "Cannot append to %s with compression", bsdtar->filename);
976
977         if (stat(bsdtar->filename, &s) != 0)
978                 return;
979
980         if (!S_ISREG(s.st_mode) && !S_ISBLK(s.st_mode))
981                 lafe_errc(1, 0,
982                     "Cannot append to %s: not a regular file.",
983                     bsdtar->filename);
984
985 /* Is this an appropriate check here on Windows? */
986 /*
987         if (GetFileType(handle) != FILE_TYPE_DISK)
988                 lafe_errc(1, 0, "Cannot append");
989 */
990
991 }