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