xref: /freebsd/contrib/libarchive/tar/write.c (revision 8aac90f18aef7c9eea906c3ff9a001ca7b94f375)
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 
29 #ifdef HAVE_SYS_TYPES_H
30 #include <sys/types.h>
31 #endif
32 #ifdef HAVE_SYS_STAT_H
33 #include <sys/stat.h>
34 #endif
35 #ifdef HAVE_ATTR_XATTR_H
36 #include <attr/xattr.h>
37 #endif
38 #ifdef HAVE_ERRNO_H
39 #include <errno.h>
40 #endif
41 #ifdef HAVE_FCNTL_H
42 #include <fcntl.h>
43 #endif
44 #ifdef HAVE_GRP_H
45 #include <grp.h>
46 #endif
47 #ifdef HAVE_IO_H
48 #include <io.h>
49 #endif
50 #ifdef HAVE_LIBGEN_H
51 #include <libgen.h>
52 #endif
53 #ifdef HAVE_LIMITS_H
54 #include <limits.h>
55 #endif
56 #ifdef HAVE_PATHS_H
57 #include <paths.h>
58 #endif
59 #ifdef HAVE_PWD_H
60 #include <pwd.h>
61 #endif
62 #ifdef HAVE_STDINT_H
63 #include <stdint.h>
64 #endif
65 #include <stdio.h>
66 #ifdef HAVE_STDLIB_H
67 #include <stdlib.h>
68 #endif
69 #ifdef HAVE_STRING_H
70 #include <string.h>
71 #endif
72 #ifdef HAVE_UNISTD_H
73 #include <unistd.h>
74 #endif
75 
76 #include "bsdtar.h"
77 #include "err.h"
78 #include "line_reader.h"
79 
80 #ifndef O_BINARY
81 #define	O_BINARY 0
82 #endif
83 
84 struct archive_dir_entry {
85 	struct archive_dir_entry	*next;
86 	time_t			 mtime_sec;
87 	int			 mtime_nsec;
88 	char			*name;
89 };
90 
91 struct archive_dir {
92 	struct archive_dir_entry *head, *tail;
93 };
94 
95 static int		 append_archive(struct bsdtar *, struct archive *,
96 			     struct archive *ina);
97 static int		 append_archive_filename(struct bsdtar *,
98 			     struct archive *, const char *fname);
99 static void		 archive_names_from_file(struct bsdtar *bsdtar,
100 			     struct archive *a);
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 *,
117 			     const char *);
118 
119 #if defined(_WIN32) && !defined(__CYGWIN__)
120 /* Not a full lseek() emulation, but enough for our needs here. */
121 static int
122 seek_file(int fd, int64_t offset, int whence)
123 {
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);
129 }
130 #define	open _open
131 #define	close _close
132 #define	read _read
133 #ifdef lseek
134 #undef lseek
135 #endif
136 #define	lseek seek_file
137 #endif
138 
139 static void
140 set_writer_options(struct bsdtar *bsdtar, struct archive *a)
141 {
142 	const char *writer_options;
143 	int r;
144 
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;
149 		char *p;
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);
159 		free(p);
160 		if (r < ARCHIVE_WARN)
161 			lafe_errc(1, 0, "%s", archive_error_string(a));
162 		else
163 			archive_clear_error(a);
164 	}
165 	if (ARCHIVE_OK != archive_write_set_options(a, bsdtar->option_options))
166 		lafe_errc(1, 0, "%s", archive_error_string(a));
167 }
168 
169 static void
170 set_reader_options(struct bsdtar *bsdtar, struct archive *a)
171 {
172 	const char *reader_options;
173 	int r;
174 
175 	(void)bsdtar; /* UNUSED */
176 
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;
181 		char *p;
182 		/* Set default write options. */
183 		if ((p = malloc(module_len + opt_len)) == NULL)
184 		if (p == 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);
192 		free(p);
193 		if (r < ARCHIVE_WARN)
194 			lafe_errc(1, 0, "%s", archive_error_string(a));
195 		else
196 			archive_clear_error(a);
197 	}
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));
202 }
203 
204 void
205 tar_mode_c(struct bsdtar *bsdtar)
206 {
207 	struct archive *a;
208 	const void *filter_name;
209 	int r;
210 
211 	if (*bsdtar->argv == NULL && bsdtar->names_from_file == NULL)
212 		lafe_errc(1, 0, "no files or directories specified");
213 
214 	a = archive_write_new();
215 
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");
220 	} else {
221 		r = archive_write_set_format_by_name(a,
222 			cset_get_format(bsdtar->cset));
223 	}
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));
228 		usage();
229 	}
230 
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);
233 
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);
238 	}
239 
240 	set_writer_options(bsdtar, a);
241 	if (bsdtar->passphrase != NULL)
242 		r = archive_write_set_passphrase(a, bsdtar->passphrase);
243 	else
244 		r = archive_write_set_passphrase_callback(a, bsdtar,
245 			&passphrase_callback);
246 	if (r != ARCHIVE_OK)
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);
251 }
252 
253 /*
254  * Same as 'c', except we only support tar or empty formats in
255  * uncompressed files on disk.
256  */
257 void
258 tar_mode_r(struct bsdtar *bsdtar)
259 {
260 	int64_t	end_offset;
261 	int	format;
262 	struct archive *a;
263 	struct archive_entry *entry;
264 	int	r;
265 
266 	/* Sanity-test some arguments and the file. */
267 	test_for_append(bsdtar);
268 
269 	format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
270 
271 #if defined(__BORLANDC__)
272 	bsdtar->fd = open(bsdtar->filename, O_RDWR | O_CREAT | O_BINARY);
273 #else
274 	bsdtar->fd = open(bsdtar->filename, O_RDWR | O_CREAT | O_BINARY, 0666);
275 #endif
276 	if (bsdtar->fd < 0)
277 		lafe_errc(1, errno,
278 		    "Cannot open %s", bsdtar->filename);
279 
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);
287 	if (r != ARCHIVE_OK)
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);
294 			close(bsdtar->fd);
295 			lafe_errc(1, 0,
296 			    "Cannot append to compressed archive.");
297 		}
298 		/* Keep going until we hit end-of-archive */
299 		format = archive_format(a);
300 	}
301 
302 	end_offset = archive_read_header_position(a);
303 	archive_read_free(a);
304 
305 	/* Re-open archive for writing */
306 	a = archive_write_new();
307 	/*
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.
313 	 */
314 
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) {
323 			lafe_errc(1, 0,
324 			    "Format %s is incompatible with the archive %s.",
325 			    cset_get_format(bsdtar->cset), bsdtar->filename);
326 		}
327 	} else {
328 		/*
329 		 * Just preserve the current format, with a little care
330 		 * for formats that libarchive can't write.
331 		 */
332 		if (format == ARCHIVE_FORMAT_EMPTY)
333 			format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
334 		archive_write_set_format(a, format);
335 	}
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));
341 
342 	write_archive(a, bsdtar); /* XXX check return val XXX */
343 
344 	close(bsdtar->fd);
345 	bsdtar->fd = -1;
346 }
347 
348 void
349 tar_mode_u(struct bsdtar *bsdtar)
350 {
351 	int64_t			 end_offset;
352 	struct archive		*a;
353 	struct archive_entry	*entry;
354 	int			 format;
355 	struct archive_dir_entry	*p;
356 	struct archive_dir	 archive_dir;
357 
358 	bsdtar->archive_dir = &archive_dir;
359 	memset(&archive_dir, 0, sizeof(archive_dir));
360 
361 	format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
362 
363 	/* Sanity-test some arguments and the file. */
364 	test_for_append(bsdtar);
365 
366 	bsdtar->fd = open(bsdtar->filename, O_RDWR | O_BINARY);
367 	if (bsdtar->fd < 0)
368 		lafe_errc(1, errno,
369 		    "Cannot open %s", bsdtar->filename);
370 
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)
377 	    != ARCHIVE_OK) {
378 		lafe_errc(1, 0,
379 		    "Can't open %s: %s", bsdtar->filename,
380 		    archive_error_string(a));
381 	}
382 
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);
387 			close(bsdtar->fd);
388 			lafe_errc(1, 0,
389 			    "Cannot append to compressed archive.");
390 		}
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 */
399 	}
400 
401 	end_offset = archive_read_header_position(a);
402 	archive_read_free(a);
403 
404 	/* Re-open archive for writing. */
405 	a = archive_write_new();
406 	/*
407 	 * Set format to same one auto-detected above.
408 	 */
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);
412 
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));
418 
419 	write_archive(a, bsdtar);
420 
421 	close(bsdtar->fd);
422 	bsdtar->fd = -1;
423 
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;
429 	}
430 	bsdtar->archive_dir->tail = NULL;
431 }
432 
433 
434 /*
435  * Write user-specified files/dirs to opened archive.
436  */
437 static void
438 write_archive(struct archive *a, struct bsdtar *bsdtar)
439 {
440 	const char *arg;
441 	struct archive_entry *entry, *sparse_entry;
442 
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;
449 
450 	/* Allocate a buffer for file data. */
451 	if ((bsdtar->buff = malloc(bsdtar->buff_size)) == NULL)
452 		lafe_errc(1, 0, "cannot allocate memory");
453 
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,
457 	    archive_format(a));
458 
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) {
464 	case 'H':
465 		archive_read_disk_set_symlink_hybrid(bsdtar->diskreader);
466 		break;
467 	case 'L':
468 		archive_read_disk_set_symlink_logical(bsdtar->diskreader);
469 		break;
470 	default:
471 		archive_read_disk_set_symlink_physical(bsdtar->diskreader);
472 		break;
473 	}
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);
483 
484 	if (bsdtar->names_from_file != NULL)
485 		archive_names_from_file(bsdtar, a);
486 
487 	while (*bsdtar->argv) {
488 		arg = *bsdtar->argv;
489 		if (arg[0] == '-' && arg[1] == 'C') {
490 			arg += 2;
491 			if (*arg == '\0') {
492 				bsdtar->argv++;
493 				arg = *bsdtar->argv;
494 				if (arg == NULL) {
495 					lafe_warnc(0, "%s",
496 					    "Missing argument for -C");
497 					bsdtar->return_value = 1;
498 					goto cleanup;
499 				}
500 				if (*arg == '\0') {
501 					lafe_warnc(0,
502 					    "Meaningless argument for -C: ''");
503 					bsdtar->return_value = 1;
504 					goto cleanup;
505 				}
506 			}
507 			set_chdir(bsdtar, arg);
508 		} else {
509 			if (*arg != '/')
510 				do_chdir(bsdtar); /* Handle a deferred -C */
511 			if (*arg == '@') {
512 				if (append_archive_filename(bsdtar, a,
513 				    arg + 1) != 0)
514 					break;
515 			} else
516 				write_hierarchy(bsdtar, a, arg);
517 		}
518 		bsdtar->argv++;
519 	}
520 
521 	archive_read_disk_set_matching(bsdtar->diskreader, NULL, NULL, NULL);
522 	archive_read_disk_set_metadata_filter_callback(
523 	    bsdtar->diskreader, NULL, NULL);
524 	entry = NULL;
525 	archive_entry_linkify(bsdtar->resolver, &entry, &sparse_entry);
526 	while (entry != NULL) {
527 		int r;
528 		struct archive_entry *entry2;
529 		struct archive *disk = bsdtar->diskreader;
530 
531 		/*
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.
538 		 */
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;
546 			goto next_entry;
547 		}
548 
549 		/*
550 		 * Invoke archive_read_next_header2() to work
551 		 * archive_read_data_block(), which is called via write_file(),
552 		 * without failure.
553 		 */
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);
563 			goto next_entry;
564 		}
565 
566 		write_file(bsdtar, a, entry);
567 		archive_read_close(disk);
568 next_entry:
569 		archive_entry_free(entry);
570 		entry = NULL;
571 		archive_entry_linkify(bsdtar->resolver, &entry, &sparse_entry);
572 	}
573 
574 	if (archive_write_close(a)) {
575 		lafe_warnc(0, "%s", archive_error_string(a));
576 		bsdtar->return_value = 1;
577 	}
578 
579 cleanup:
580 	/* Free file data buffer. */
581 	free(bsdtar->buff);
582 	archive_entry_linkresolver_free(bsdtar->resolver);
583 	bsdtar->resolver = NULL;
584 	archive_read_free(bsdtar->diskreader);
585 	bsdtar->diskreader = NULL;
586 
587 	if (bsdtar->flags & OPTFLAG_TOTALS) {
588 		fprintf(stderr, "Total bytes written: %s\n",
589 		    tar_i64toa(archive_filter_bytes(a, -1)));
590 	}
591 
592 	archive_write_free(a);
593 }
594 
595 /*
596  * Archive names specified in file.
597  *
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.
601  */
602 static void
603 archive_names_from_file(struct bsdtar *bsdtar, struct archive *a)
604 {
605 	struct lafe_line_reader *lr;
606 	const char *line;
607 
608 	bsdtar->next_line_is_dir = 0;
609 
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) {
614 			if (*line != '\0')
615 				set_chdir(bsdtar, line);
616 			else {
617 				lafe_warnc(0,
618 				    "Meaningless argument for -C: ''");
619 				bsdtar->return_value = 1;
620 			}
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;
625 		else {
626 			if (*line != '/')
627 				do_chdir(bsdtar); /* Handle a deferred -C */
628 			write_hierarchy(bsdtar, a, line);
629 		}
630 	}
631 	lafe_line_reader_free(lr);
632 	if (bsdtar->next_line_is_dir)
633 		lafe_errc(1, errno,
634 		    "Unexpected end of filename list; "
635 		    "directory expected after -C");
636 }
637 
638 /*
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.
644  */
645 static int
646 append_archive_filename(struct bsdtar *bsdtar, struct archive *a,
647     const char *raw_filename)
648 {
649 	struct archive *ina;
650 	const char *filename = raw_filename;
651 	int rc;
652 
653 	if (strcmp(filename, "-") == 0)
654 		filename = NULL; /* Library uses NULL for stdio. */
655 
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);
663 	else
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;
672 		return (0);
673 	}
674 
675 	rc = append_archive(bsdtar, a, ina);
676 
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;
681 	}
682 	archive_read_free(ina);
683 
684 	return (rc);
685 }
686 
687 static int
688 append_archive(struct bsdtar *bsdtar, struct archive *a, struct archive *ina)
689 {
690 	struct archive_entry *in_entry;
691 	int e;
692 
693 	while (ARCHIVE_OK == (e = archive_read_next_header(ina, &in_entry))) {
694 		if (archive_match_excluded(bsdtar->matching, in_entry))
695 			continue;
696 		if(edit_pathname(bsdtar, in_entry))
697 			continue;
698 		if ((bsdtar->flags & OPTFLAG_INTERACTIVE) &&
699 		    !yes("copy '%s'", archive_entry_pathname(in_entry)))
700 			continue;
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));
707 		if (need_report())
708 			report_write(bsdtar, a, in_entry, 0);
709 
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));
716 			else
717 				fprintf(stderr, ": %s", archive_error_string(a));
718 		}
719 		if (e == ARCHIVE_FATAL)
720 			exit(1);
721 
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))
726 				exit(1);
727 		}
728 
729 		if (bsdtar->verbose)
730 			fprintf(stderr, "\n");
731 	}
732 
733 	return (e == ARCHIVE_EOF ? ARCHIVE_OK : e);
734 }
735 
736 /* Helper function to copy file to archive. */
737 static int
738 copy_file_data_block(struct bsdtar *bsdtar, struct archive *a,
739     struct archive *in_a, struct archive_entry *entry)
740 {
741 	size_t	bytes_read;
742 	ssize_t	bytes_written;
743 	int64_t	offset, progress = 0;
744 	char *null_buff = NULL;
745 	const void *buff;
746 	int r;
747 
748 	while ((r = archive_read_data_block(in_a, &buff,
749 	    &bytes_read, &offset)) == ARCHIVE_OK) {
750 		if (need_report())
751 			report_write(bsdtar, a, entry, progress);
752 
753 		if (offset > progress) {
754 			int64_t sparse = offset - progress;
755 			size_t ns;
756 
757 			if (null_buff == NULL) {
758 				null_buff = bsdtar->buff;
759 				memset(null_buff, 0, bsdtar->buff_size);
760 			}
761 
762 			while (sparse > 0) {
763 				if (sparse > (int64_t)bsdtar->buff_size)
764 					ns = bsdtar->buff_size;
765 				else
766 					ns = (size_t)sparse;
767 				bytes_written =
768 				    archive_write_data(a, null_buff, ns);
769 				if (bytes_written < 0) {
770 					/* Write failed; this is bad */
771 					lafe_warnc(0, "%s",
772 					     archive_error_string(a));
773 					return (-1);
774 				}
775 				if ((size_t)bytes_written < ns) {
776 					/* Write was truncated; warn but
777 					 * continue. */
778 					lafe_warnc(0,
779 					    "%s: Truncated write; file may "
780 					    "have grown while being archived.",
781 					    archive_entry_pathname(entry));
782 					return (0);
783 				}
784 				progress += bytes_written;
785 				sparse -= bytes_written;
786 			}
787 		}
788 
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));
793 			return (-1);
794 		}
795 		if ((size_t)bytes_written < bytes_read) {
796 			/* Write was truncated; warn but continue. */
797 			lafe_warnc(0,
798 			    "%s: Truncated write; file may have grown "
799 			    "while being archived.",
800 			    archive_entry_pathname(entry));
801 			return (0);
802 		}
803 		progress += bytes_written;
804 	}
805 	if (r < ARCHIVE_WARN) {
806 		lafe_warnc(archive_errno(a), "%s", archive_error_string(a));
807 		return (-1);
808 	}
809 	return (0);
810 }
811 
812 static void
813 excluded_callback(struct archive *a, void *_data, struct archive_entry *entry)
814 {
815 	struct bsdtar *bsdtar = (struct bsdtar *)_data;
816 
817 	if (bsdtar->flags & OPTFLAG_NO_SUBDIRS)
818 		return;
819 	if (!archive_read_disk_can_descend(a))
820 		return;
821 	if ((bsdtar->flags & OPTFLAG_INTERACTIVE) &&
822 	    !yes("add '%s'", archive_entry_pathname(entry)))
823 		return;
824 	archive_read_disk_descend(a);
825 }
826 
827 static int
828 metadata_filter(struct archive *a, void *_data, struct archive_entry *entry)
829 {
830 	struct bsdtar *bsdtar = (struct bsdtar *)_data;
831 
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
836 	 * regardless.
837 	 *
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.
842 	 *
843 	 * Use archive_read_disk_current_filesystem_is_remote().
844 	 */
845 
846 	/*
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
850 	 * the user with it.
851 	 */
852 	if ((bsdtar->flags & OPTFLAG_INTERACTIVE) &&
853 	    !yes("add '%s'", archive_entry_pathname(entry)))
854 		return (0);
855 
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);
860 
861 	return (1);
862 }
863 
864 /*
865  * Add the file or dir hierarchy named by 'path' to the archive
866  */
867 static void
868 write_hierarchy(struct bsdtar *bsdtar, struct archive *a, const char *path)
869 {
870 	struct archive *disk = bsdtar->diskreader;
871 	struct archive_entry *entry = NULL, *spare_entry = NULL;
872 	int r;
873 
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;
879 		return;
880 	}
881 	bsdtar->first_fs = -1;
882 
883 	for (;;) {
884 		archive_entry_free(entry);
885 		entry = archive_entry_new();
886 		r = archive_read_next_header2(disk, entry);
887 		if (r == ARCHIVE_EOF)
888 			break;
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);
896 				return;
897 			} else if (r < ARCHIVE_WARN)
898 				continue;
899 		}
900 
901 		if (bsdtar->uid >= 0) {
902 			archive_entry_set_uid(entry, bsdtar->uid);
903 			if (!bsdtar->uname)
904 				archive_entry_set_uname(entry,
905 				    archive_read_disk_uname(bsdtar->diskreader,
906 					bsdtar->uid));
907 		}
908 		if (bsdtar->gid >= 0) {
909 			archive_entry_set_gid(entry, bsdtar->gid);
910 			if (!bsdtar->gname)
911 				archive_entry_set_gname(entry,
912 				    archive_read_disk_gname(bsdtar->diskreader,
913 					bsdtar->gid));
914 		}
915 		if (bsdtar->uname)
916 			archive_entry_set_uname(entry, bsdtar->uname);
917 		if (bsdtar->gname)
918 			archive_entry_set_gname(entry, bsdtar->gname);
919 
920 		/*
921 		 * Rewrite the pathname to be archived.  If rewrite
922 		 * fails, skip the entry.
923 		 */
924 		if (edit_pathname(bsdtar, entry))
925 			continue;
926 
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));
935 		}
936 
937 		/* Non-regular files get archived with zero size. */
938 		if (archive_entry_filetype(entry) != AE_IFREG)
939 			archive_entry_set_size(entry, 0);
940 
941 		archive_entry_linkify(bsdtar->resolver, &entry, &spare_entry);
942 
943 		while (entry != NULL) {
944 			write_file(bsdtar, a, entry);
945 			archive_entry_free(entry);
946 			entry = spare_entry;
947 			spare_entry = NULL;
948 		}
949 
950 		if (bsdtar->verbose)
951 			fprintf(stderr, "\n");
952 	}
953 	archive_entry_free(entry);
954 	archive_read_close(disk);
955 }
956 
957 /*
958  * Write a single file (or directory or other filesystem object) to
959  * the archive.
960  */
961 static void
962 write_file(struct bsdtar *bsdtar, struct archive *a,
963     struct archive_entry *entry)
964 {
965 	write_entry(bsdtar, a, entry);
966 }
967 
968 /*
969  * Write a single entry to the archive.
970  */
971 static void
972 write_entry(struct bsdtar *bsdtar, struct archive *a,
973     struct archive_entry *entry)
974 {
975 	int e;
976 
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));
987 		} else
988 			fprintf(stderr, ": %s", archive_error_string(a));
989 	}
990 
991 	if (e == ARCHIVE_FATAL)
992 		exit(1);
993 
994 	/*
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.
999 	 */
1000 	if (e >= ARCHIVE_WARN && archive_entry_size(entry) > 0) {
1001 		if (copy_file_data_block(bsdtar, a, bsdtar->diskreader, entry))
1002 			exit(1);
1003 	}
1004 }
1005 
1006 static void
1007 report_write(struct bsdtar *bsdtar, struct archive *a,
1008     struct archive_entry *entry, int64_t progress)
1009 {
1010 	uint64_t comp, uncomp;
1011 	int compression;
1012 
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));
1019 	if (comp >= uncomp)
1020 		compression = 0;
1021 	else
1022 		compression = (int)((uncomp - comp) * 100 / uncomp);
1023 	fprintf(stderr,
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)));
1032 }
1033 
1034 static void
1035 test_for_append(struct bsdtar *bsdtar)
1036 {
1037 	struct stat s;
1038 
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.");
1043 
1044 	if (stat(bsdtar->filename, &s) != 0)
1045 		return;
1046 
1047 	if (!S_ISREG(s.st_mode) && !S_ISBLK(s.st_mode))
1048 		lafe_errc(1, 0,
1049 		    "Cannot append to %s: not a regular file.",
1050 		    bsdtar->filename);
1051 
1052 /* Is this an appropriate check here on Windows? */
1053 /*
1054 	if (GetFileType(handle) != FILE_TYPE_DISK)
1055 		lafe_errc(1, 0, "Cannot append");
1056 */
1057 
1058 }
1059