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