xref: /dragonfly/contrib/libarchive/tar/write.c (revision dcd37f7d)
1 /*-
2  * Copyright (c) 2003-2007 Tim Kientzle
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include "bsdtar_platform.h"
27 __FBSDID("$FreeBSD: src/usr.bin/tar/write.c,v 1.79 2008/11/27 05:49:52 kientzle Exp $");
28 
29 #ifdef HAVE_SYS_TYPES_H
30 #include <sys/types.h>
31 #endif
32 #ifdef HAVE_SYS_ACL_H
33 #include <sys/acl.h>
34 #endif
35 #ifdef HAVE_SYS_IOCTL_H
36 #include <sys/ioctl.h>
37 #endif
38 #ifdef HAVE_SYS_STAT_H
39 #include <sys/stat.h>
40 #endif
41 #ifdef HAVE_ATTR_XATTR_H
42 #include <attr/xattr.h>
43 #endif
44 #ifdef HAVE_ERRNO_H
45 #include <errno.h>
46 #endif
47 #ifdef HAVE_FCNTL_H
48 #include <fcntl.h>
49 #endif
50 #ifdef HAVE_FNMATCH_H
51 #include <fnmatch.h>
52 #endif
53 #ifdef HAVE_GRP_H
54 #include <grp.h>
55 #endif
56 #ifdef HAVE_LIMITS_H
57 #include <limits.h>
58 #endif
59 #ifdef HAVE_LINUX_FS_H
60 #include <linux/fs.h>	/* for Linux file flags */
61 #endif
62 /*
63  * Some Linux distributions have both linux/ext2_fs.h and ext2fs/ext2_fs.h.
64  * As the include guards don't agree, the order of include is important.
65  */
66 #ifdef HAVE_LINUX_EXT2_FS_H
67 #include <linux/ext2_fs.h>	/* for Linux file flags */
68 #endif
69 #if defined(HAVE_EXT2FS_EXT2_FS_H) && !defined(__CYGWIN__)
70 /* This header exists but is broken on Cygwin. */
71 #include <ext2fs/ext2_fs.h>
72 #endif
73 #ifdef HAVE_PWD_H
74 #include <pwd.h>
75 #endif
76 #include <stdio.h>
77 #ifdef HAVE_STDLIB_H
78 #include <stdlib.h>
79 #endif
80 #ifdef HAVE_STRING_H
81 #include <string.h>
82 #endif
83 #ifdef HAVE_UNISTD_H
84 #include <unistd.h>
85 #endif
86 
87 #include "bsdtar.h"
88 #include "tree.h"
89 
90 /* Size of buffer for holding file data prior to writing. */
91 #define FILEDATABUFLEN	65536
92 
93 /* Fixed size of uname/gname caches. */
94 #define	name_cache_size 101
95 
96 static const char * const NO_NAME = "(noname)";
97 
98 struct archive_dir_entry {
99 	struct archive_dir_entry	*next;
100 	time_t			 mtime_sec;
101 	int			 mtime_nsec;
102 	char			*name;
103 };
104 
105 struct archive_dir {
106 	struct archive_dir_entry *head, *tail;
107 };
108 
109 struct name_cache {
110 	int	probes;
111 	int	hits;
112 	size_t	size;
113 	struct {
114 		id_t id;
115 		const char *name;
116 	} cache[name_cache_size];
117 };
118 
119 static void		 add_dir_list(struct bsdtar *bsdtar, const char *path,
120 			     time_t mtime_sec, int mtime_nsec);
121 static int		 append_archive(struct bsdtar *, struct archive *,
122 			     struct archive *ina);
123 static int		 append_archive_filename(struct bsdtar *,
124 			     struct archive *, const char *fname);
125 static void		 archive_names_from_file(struct bsdtar *bsdtar,
126 			     struct archive *a);
127 static int		 archive_names_from_file_helper(struct bsdtar *bsdtar,
128 			     const char *line);
129 static int		 copy_file_data(struct bsdtar *bsdtar,
130 			     struct archive *a, struct archive *ina);
131 static int		 new_enough(struct bsdtar *, const char *path,
132 			     const struct stat *);
133 static void		 test_for_append(struct bsdtar *);
134 static void		 write_archive(struct archive *, struct bsdtar *);
135 static void		 write_entry_backend(struct bsdtar *, struct archive *,
136 			     struct archive_entry *);
137 static int		 write_file_data(struct bsdtar *, struct archive *,
138 			     struct archive_entry *, int fd);
139 static void		 write_hierarchy(struct bsdtar *, struct archive *,
140 			     const char *);
141 
142 void
143 tar_mode_c(struct bsdtar *bsdtar)
144 {
145 	struct archive *a;
146 	int r;
147 
148 	if (*bsdtar->argv == NULL && bsdtar->names_from_file == NULL)
149 		bsdtar_errc(bsdtar, 1, 0, "no files or directories specified");
150 
151 	a = archive_write_new();
152 
153 	/* Support any format that the library supports. */
154 	if (bsdtar->create_format == NULL) {
155 		r = archive_write_set_format_pax_restricted(a);
156 		bsdtar->create_format = "pax restricted";
157 	} else {
158 		r = archive_write_set_format_by_name(a, bsdtar->create_format);
159 	}
160 	if (r != ARCHIVE_OK) {
161 		fprintf(stderr, "Can't use format %s: %s\n",
162 		    bsdtar->create_format,
163 		    archive_error_string(a));
164 		usage(bsdtar);
165 	}
166 
167 	/*
168 	 * If user explicitly set the block size, then assume they
169 	 * want the last block padded as well.  Otherwise, use the
170 	 * default block size and accept archive_write_open_file()'s
171 	 * default padding decisions.
172 	 */
173 	if (bsdtar->bytes_per_block != 0) {
174 		archive_write_set_bytes_per_block(a, bsdtar->bytes_per_block);
175 		archive_write_set_bytes_in_last_block(a,
176 		    bsdtar->bytes_per_block);
177 	} else
178 		archive_write_set_bytes_per_block(a, DEFAULT_BYTES_PER_BLOCK);
179 
180 	if (bsdtar->compress_program) {
181 		archive_write_set_compression_program(a, bsdtar->compress_program);
182 	} else {
183 		switch (bsdtar->create_compression) {
184 		case 0:
185 			archive_write_set_compression_none(a);
186 			break;
187 #ifdef HAVE_LIBBZ2
188 		case 'j': case 'y':
189 			archive_write_set_compression_bzip2(a);
190 			break;
191 #endif
192 #ifdef HAVE_LIBLZMA
193 		case 'J':
194 			archive_write_set_compression_xz(a);
195 			break;
196 		case OPTION_LZMA:
197 			archive_write_set_compression_lzma(a);
198 			break;
199 #endif
200 #ifdef HAVE_LIBZ
201 		case 'z':
202 			archive_write_set_compression_gzip(a);
203 			break;
204 #endif
205 		case 'Z':
206 			archive_write_set_compression_compress(a);
207 			break;
208 		default:
209 			bsdtar_errc(bsdtar, 1, 0,
210 			    "Unrecognized compression option -%c",
211 			    bsdtar->create_compression);
212 		}
213 	}
214 
215 	if (ARCHIVE_OK != archive_write_set_options(a, bsdtar->option_options))
216 		bsdtar_errc(bsdtar, 1, 0, archive_error_string(a));
217 	if (ARCHIVE_OK != archive_write_open_file(a, bsdtar->filename))
218 		bsdtar_errc(bsdtar, 1, 0, archive_error_string(a));
219 	write_archive(a, bsdtar);
220 }
221 
222 /*
223  * Same as 'c', except we only support tar or empty formats in
224  * uncompressed files on disk.
225  */
226 void
227 tar_mode_r(struct bsdtar *bsdtar)
228 {
229 	off_t	end_offset;
230 	int	format;
231 	struct archive *a;
232 	struct archive_entry *entry;
233 	int	r;
234 
235 	/* Sanity-test some arguments and the file. */
236 	test_for_append(bsdtar);
237 
238 	format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
239 
240 	bsdtar->fd = open(bsdtar->filename, O_RDWR | O_CREAT, 0666);
241 	if (bsdtar->fd < 0)
242 		bsdtar_errc(bsdtar, 1, errno,
243 		    "Cannot open %s", bsdtar->filename);
244 
245 	a = archive_read_new();
246 	archive_read_support_compression_all(a);
247 	archive_read_support_format_tar(a);
248 	archive_read_support_format_gnutar(a);
249 	r = archive_read_open_fd(a, bsdtar->fd, 10240);
250 	if (r != ARCHIVE_OK)
251 		bsdtar_errc(bsdtar, 1, archive_errno(a),
252 		    "Can't read archive %s: %s", bsdtar->filename,
253 		    archive_error_string(a));
254 	while (0 == archive_read_next_header(a, &entry)) {
255 		if (archive_compression(a) != ARCHIVE_COMPRESSION_NONE) {
256 			archive_read_finish(a);
257 			close(bsdtar->fd);
258 			bsdtar_errc(bsdtar, 1, 0,
259 			    "Cannot append to compressed archive.");
260 		}
261 		/* Keep going until we hit end-of-archive */
262 		format = archive_format(a);
263 	}
264 
265 	end_offset = archive_read_header_position(a);
266 	archive_read_finish(a);
267 
268 	/* Re-open archive for writing */
269 	a = archive_write_new();
270 	archive_write_set_compression_none(a);
271 	/*
272 	 * Set the format to be used for writing.  To allow people to
273 	 * extend empty files, we need to allow them to specify the format,
274 	 * which opens the possibility that they will specify a format that
275 	 * doesn't match the existing format.  Hence, the following bit
276 	 * of arcane ugliness.
277 	 */
278 
279 	if (bsdtar->create_format != NULL) {
280 		/* If the user requested a format, use that, but ... */
281 		archive_write_set_format_by_name(a,
282 		    bsdtar->create_format);
283 		/* ... complain if it's not compatible. */
284 		format &= ARCHIVE_FORMAT_BASE_MASK;
285 		if (format != (int)(archive_format(a) & ARCHIVE_FORMAT_BASE_MASK)
286 		    && format != ARCHIVE_FORMAT_EMPTY) {
287 			bsdtar_errc(bsdtar, 1, 0,
288 			    "Format %s is incompatible with the archive %s.",
289 			    bsdtar->create_format, bsdtar->filename);
290 		}
291 	} else {
292 		/*
293 		 * Just preserve the current format, with a little care
294 		 * for formats that libarchive can't write.
295 		 */
296 		if (format == ARCHIVE_FORMAT_TAR_GNUTAR)
297 			/* TODO: When gtar supports pax, use pax restricted. */
298 			format = ARCHIVE_FORMAT_TAR_USTAR;
299 		if (format == ARCHIVE_FORMAT_EMPTY)
300 			format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
301 		archive_write_set_format(a, format);
302 	}
303 	lseek(bsdtar->fd, end_offset, SEEK_SET); /* XXX check return val XXX */
304 	if (ARCHIVE_OK != archive_write_set_options(a, bsdtar->option_options))
305 		bsdtar_errc(bsdtar, 1, 0, archive_error_string(a));
306 	if (ARCHIVE_OK != archive_write_open_fd(a, bsdtar->fd))
307 		bsdtar_errc(bsdtar, 1, 0, archive_error_string(a));
308 
309 	write_archive(a, bsdtar); /* XXX check return val XXX */
310 
311 	close(bsdtar->fd);
312 	bsdtar->fd = -1;
313 }
314 
315 void
316 tar_mode_u(struct bsdtar *bsdtar)
317 {
318 	off_t			 end_offset;
319 	struct archive		*a;
320 	struct archive_entry	*entry;
321 	int			 format;
322 	struct archive_dir_entry	*p;
323 	struct archive_dir	 archive_dir;
324 
325 	bsdtar->archive_dir = &archive_dir;
326 	memset(&archive_dir, 0, sizeof(archive_dir));
327 
328 	format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
329 
330 	/* Sanity-test some arguments and the file. */
331 	test_for_append(bsdtar);
332 
333 	bsdtar->fd = open(bsdtar->filename, O_RDWR);
334 	if (bsdtar->fd < 0)
335 		bsdtar_errc(bsdtar, 1, errno,
336 		    "Cannot open %s", bsdtar->filename);
337 
338 	a = archive_read_new();
339 	archive_read_support_compression_all(a);
340 	archive_read_support_format_tar(a);
341 	archive_read_support_format_gnutar(a);
342 	if (archive_read_open_fd(a, bsdtar->fd,
343 	    bsdtar->bytes_per_block != 0 ? bsdtar->bytes_per_block :
344 		DEFAULT_BYTES_PER_BLOCK) != ARCHIVE_OK) {
345 		bsdtar_errc(bsdtar, 1, 0,
346 		    "Can't open %s: %s", bsdtar->filename,
347 		    archive_error_string(a));
348 	}
349 
350 	/* Build a list of all entries and their recorded mod times. */
351 	while (0 == archive_read_next_header(a, &entry)) {
352 		if (archive_compression(a) != ARCHIVE_COMPRESSION_NONE) {
353 			archive_read_finish(a);
354 			close(bsdtar->fd);
355 			bsdtar_errc(bsdtar, 1, 0,
356 			    "Cannot append to compressed archive.");
357 		}
358 		add_dir_list(bsdtar, archive_entry_pathname(entry),
359 		    archive_entry_mtime(entry),
360 		    archive_entry_mtime_nsec(entry));
361 		/* Record the last format determination we see */
362 		format = archive_format(a);
363 		/* Keep going until we hit end-of-archive */
364 	}
365 
366 	end_offset = archive_read_header_position(a);
367 	archive_read_finish(a);
368 
369 	/* Re-open archive for writing. */
370 	a = archive_write_new();
371 	archive_write_set_compression_none(a);
372 	/*
373 	 * Set format to same one auto-detected above, except that
374 	 * we don't write GNU tar format, so use ustar instead.
375 	 */
376 	if (format == ARCHIVE_FORMAT_TAR_GNUTAR)
377 		format = ARCHIVE_FORMAT_TAR_USTAR;
378 	archive_write_set_format(a, format);
379 	if (bsdtar->bytes_per_block != 0) {
380 		archive_write_set_bytes_per_block(a, bsdtar->bytes_per_block);
381 		archive_write_set_bytes_in_last_block(a,
382 		    bsdtar->bytes_per_block);
383 	} else
384 		archive_write_set_bytes_per_block(a, DEFAULT_BYTES_PER_BLOCK);
385 	lseek(bsdtar->fd, end_offset, SEEK_SET);
386 	ftruncate(bsdtar->fd, end_offset);
387 	if (ARCHIVE_OK != archive_write_set_options(a, bsdtar->option_options))
388 		bsdtar_errc(bsdtar, 1, 0, archive_error_string(a));
389 	if (ARCHIVE_OK != archive_write_open_fd(a, bsdtar->fd))
390 		bsdtar_errc(bsdtar, 1, 0, archive_error_string(a));
391 
392 	write_archive(a, bsdtar);
393 
394 	close(bsdtar->fd);
395 	bsdtar->fd = -1;
396 
397 	while (bsdtar->archive_dir->head != NULL) {
398 		p = bsdtar->archive_dir->head->next;
399 		free(bsdtar->archive_dir->head->name);
400 		free(bsdtar->archive_dir->head);
401 		bsdtar->archive_dir->head = p;
402 	}
403 	bsdtar->archive_dir->tail = NULL;
404 }
405 
406 
407 /*
408  * Write user-specified files/dirs to opened archive.
409  */
410 static void
411 write_archive(struct archive *a, struct bsdtar *bsdtar)
412 {
413 	const char *arg;
414 	struct archive_entry *entry, *sparse_entry;
415 
416 	/* We want to catch SIGINFO and SIGUSR1. */
417 	siginfo_init(bsdtar);
418 
419 	/* Allocate a buffer for file data. */
420 	if ((bsdtar->buff = malloc(FILEDATABUFLEN)) == NULL)
421 		bsdtar_errc(bsdtar, 1, 0, "cannot allocate memory");
422 
423 	if ((bsdtar->resolver = archive_entry_linkresolver_new()) == NULL)
424 		bsdtar_errc(bsdtar, 1, 0, "cannot create link resolver");
425 	archive_entry_linkresolver_set_strategy(bsdtar->resolver,
426 	    archive_format(a));
427 	if ((bsdtar->diskreader = archive_read_disk_new()) == NULL)
428 		bsdtar_errc(bsdtar, 1, 0, "Cannot create read_disk object");
429 	archive_read_disk_set_standard_lookup(bsdtar->diskreader);
430 
431 	if (bsdtar->names_from_file != NULL)
432 		archive_names_from_file(bsdtar, a);
433 
434 	while (*bsdtar->argv) {
435 		arg = *bsdtar->argv;
436 		if (arg[0] == '-' && arg[1] == 'C') {
437 			arg += 2;
438 			if (*arg == '\0') {
439 				bsdtar->argv++;
440 				arg = *bsdtar->argv;
441 				if (arg == NULL) {
442 					bsdtar_warnc(bsdtar, 1, 0,
443 					    "Missing argument for -C");
444 					bsdtar->return_value = 1;
445 					goto cleanup;
446 				}
447 			}
448 			set_chdir(bsdtar, arg);
449 		} else {
450 			if (*arg != '/' && (arg[0] != '@' || arg[1] != '/'))
451 				do_chdir(bsdtar); /* Handle a deferred -C */
452 			if (*arg == '@') {
453 				if (append_archive_filename(bsdtar, a,
454 				    arg + 1) != 0)
455 					break;
456 			} else
457 #if defined(_WIN32) && !defined(__CYGWIN__)
458 				write_hierarchy_win(bsdtar, a, arg,
459 				    write_hierarchy);
460 #else
461 				write_hierarchy(bsdtar, a, arg);
462 #endif
463 		}
464 		bsdtar->argv++;
465 	}
466 
467 	entry = NULL;
468 	archive_entry_linkify(bsdtar->resolver, &entry, &sparse_entry);
469 	while (entry != NULL) {
470 		write_entry_backend(bsdtar, a, entry);
471 		archive_entry_free(entry);
472 		entry = NULL;
473 		archive_entry_linkify(bsdtar->resolver, &entry, &sparse_entry);
474 	}
475 
476 	if (archive_write_close(a)) {
477 		bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(a));
478 		bsdtar->return_value = 1;
479 	}
480 
481 cleanup:
482 	/* Free file data buffer. */
483 	free(bsdtar->buff);
484 	archive_entry_linkresolver_free(bsdtar->resolver);
485 	bsdtar->resolver = NULL;
486 	archive_read_finish(bsdtar->diskreader);
487 	bsdtar->diskreader = NULL;
488 
489 	if (bsdtar->option_totals) {
490 		fprintf(stderr, "Total bytes written: " BSDTAR_FILESIZE_PRINTF "\n",
491 		    (BSDTAR_FILESIZE_TYPE)archive_position_compressed(a));
492 	}
493 
494 	archive_write_finish(a);
495 
496 	/* Restore old SIGINFO + SIGUSR1 handlers. */
497 	siginfo_done(bsdtar);
498 }
499 
500 /*
501  * Archive names specified in file.
502  *
503  * Unless --null was specified, a line containing exactly "-C" will
504  * cause the next line to be a directory to pass to chdir().  If
505  * --null is specified, then a line "-C" is just another filename.
506  */
507 void
508 archive_names_from_file(struct bsdtar *bsdtar, struct archive *a)
509 {
510 	bsdtar->archive = a;
511 
512 	bsdtar->next_line_is_dir = 0;
513 	process_lines(bsdtar, bsdtar->names_from_file,
514 	    archive_names_from_file_helper);
515 	if (bsdtar->next_line_is_dir)
516 		bsdtar_errc(bsdtar, 1, errno,
517 		    "Unexpected end of filename list; "
518 		    "directory expected after -C");
519 }
520 
521 static int
522 archive_names_from_file_helper(struct bsdtar *bsdtar, const char *line)
523 {
524 	if (bsdtar->next_line_is_dir) {
525 		set_chdir(bsdtar, line);
526 		bsdtar->next_line_is_dir = 0;
527 	} else if (!bsdtar->option_null && strcmp(line, "-C") == 0)
528 		bsdtar->next_line_is_dir = 1;
529 	else {
530 		if (*line != '/')
531 			do_chdir(bsdtar); /* Handle a deferred -C */
532 		write_hierarchy(bsdtar, bsdtar->archive, line);
533 	}
534 	return (0);
535 }
536 
537 /*
538  * Copy from specified archive to current archive.  Returns non-zero
539  * for write errors (which force us to terminate the entire archiving
540  * operation).  If there are errors reading the input archive, we set
541  * bsdtar->return_value but return zero, so the overall archiving
542  * operation will complete and return non-zero.
543  */
544 static int
545 append_archive_filename(struct bsdtar *bsdtar, struct archive *a,
546     const char *filename)
547 {
548 	struct archive *ina;
549 	int rc;
550 
551 	if (strcmp(filename, "-") == 0)
552 		filename = NULL; /* Library uses NULL for stdio. */
553 
554 	ina = archive_read_new();
555 	archive_read_support_format_all(ina);
556 	archive_read_support_compression_all(ina);
557 	if (archive_read_open_file(ina, filename, 10240)) {
558 		bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(ina));
559 		bsdtar->return_value = 1;
560 		return (0);
561 	}
562 
563 	rc = append_archive(bsdtar, a, ina);
564 
565 	if (archive_errno(ina)) {
566 		bsdtar_warnc(bsdtar, 0, "Error reading archive %s: %s",
567 		    filename, archive_error_string(ina));
568 		bsdtar->return_value = 1;
569 	}
570 	archive_read_finish(ina);
571 
572 	return (rc);
573 }
574 
575 static int
576 append_archive(struct bsdtar *bsdtar, struct archive *a, struct archive *ina)
577 {
578 	struct archive_entry *in_entry;
579 	int e;
580 
581 	while (0 == archive_read_next_header(ina, &in_entry)) {
582 		if (!new_enough(bsdtar, archive_entry_pathname(in_entry),
583 			archive_entry_stat(in_entry)))
584 			continue;
585 		if (excluded(bsdtar, archive_entry_pathname(in_entry)))
586 			continue;
587 		if (bsdtar->option_interactive &&
588 		    !yes("copy '%s'", archive_entry_pathname(in_entry)))
589 			continue;
590 		if (bsdtar->verbose)
591 			safe_fprintf(stderr, "a %s",
592 			    archive_entry_pathname(in_entry));
593 		siginfo_setinfo(bsdtar, "copying",
594 		    archive_entry_pathname(in_entry),
595 		    archive_entry_size(in_entry));
596 		siginfo_printinfo(bsdtar, 0);
597 
598 		e = archive_write_header(a, in_entry);
599 		if (e != ARCHIVE_OK) {
600 			if (!bsdtar->verbose)
601 				bsdtar_warnc(bsdtar, 0, "%s: %s",
602 				    archive_entry_pathname(in_entry),
603 				    archive_error_string(a));
604 			else
605 				fprintf(stderr, ": %s", archive_error_string(a));
606 		}
607 		if (e == ARCHIVE_FATAL)
608 			exit(1);
609 
610 		if (e >= ARCHIVE_WARN) {
611 			if (archive_entry_size(in_entry) == 0)
612 				archive_read_data_skip(ina);
613 			else if (copy_file_data(bsdtar, a, ina))
614 				exit(1);
615 		}
616 
617 		if (bsdtar->verbose)
618 			fprintf(stderr, "\n");
619 	}
620 
621 	/* Note: If we got here, we saw no write errors, so return success. */
622 	return (0);
623 }
624 
625 /* Helper function to copy data between archives. */
626 static int
627 copy_file_data(struct bsdtar *bsdtar, struct archive *a, struct archive *ina)
628 {
629 	ssize_t	bytes_read;
630 	ssize_t	bytes_written;
631 	off_t	progress = 0;
632 
633 	bytes_read = archive_read_data(ina, bsdtar->buff, FILEDATABUFLEN);
634 	while (bytes_read > 0) {
635 		siginfo_printinfo(bsdtar, progress);
636 
637 		bytes_written = archive_write_data(a, bsdtar->buff,
638 		    bytes_read);
639 		if (bytes_written < bytes_read) {
640 			bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(a));
641 			return (-1);
642 		}
643 		progress += bytes_written;
644 		bytes_read = archive_read_data(ina, bsdtar->buff,
645 		    FILEDATABUFLEN);
646 	}
647 
648 	return (0);
649 }
650 
651 /*
652  * Add the file or dir hierarchy named by 'path' to the archive
653  */
654 static void
655 write_hierarchy(struct bsdtar *bsdtar, struct archive *a, const char *path)
656 {
657 	struct archive_entry *entry = NULL, *spare_entry = NULL;
658 	struct tree *tree;
659 	char symlink_mode = bsdtar->symlink_mode;
660 	dev_t first_dev = 0;
661 	int dev_recorded = 0;
662 	int tree_ret;
663 
664 	tree = tree_open(path);
665 
666 	if (!tree) {
667 		bsdtar_warnc(bsdtar, errno, "%s: Cannot open", path);
668 		bsdtar->return_value = 1;
669 		return;
670 	}
671 
672 	while ((tree_ret = tree_next(tree))) {
673 		int r;
674 		const char *name = tree_current_path(tree);
675 		const struct stat *st = NULL; /* info to use for this entry */
676 		const struct stat *lst = NULL; /* lstat() information */
677 		int descend;
678 
679 		if (tree_ret == TREE_ERROR_FATAL)
680 			bsdtar_errc(bsdtar, 1, tree_errno(tree),
681 			    "%s: Unable to continue traversing directory tree",
682 			    name);
683 		if (tree_ret == TREE_ERROR_DIR) {
684 			bsdtar_warnc(bsdtar, errno,
685 			    "%s: Couldn't visit directory", name);
686 			bsdtar->return_value = 1;
687 		}
688 		if (tree_ret != TREE_REGULAR)
689 			continue;
690 
691 		/*
692 		 * If this file/dir is excluded by a filename
693 		 * pattern, skip it.
694 		 */
695 		if (excluded(bsdtar, name))
696 			continue;
697 
698 		/*
699 		 * Get lstat() info from the tree library.
700 		 */
701 		lst = tree_current_lstat(tree);
702 		if (lst == NULL) {
703 			/* Couldn't lstat(); must not exist. */
704 			bsdtar_warnc(bsdtar, errno, "%s: Cannot stat", name);
705 			/* Return error if files disappear during traverse. */
706 			bsdtar->return_value = 1;
707 			continue;
708 		}
709 
710 		/*
711 		 * Distinguish 'L'/'P'/'H' symlink following.
712 		 */
713 		switch(symlink_mode) {
714 		case 'H':
715 			/* 'H': After the first item, rest like 'P'. */
716 			symlink_mode = 'P';
717 			/* 'H': First item (from command line) like 'L'. */
718 			/* FALLTHROUGH */
719 		case 'L':
720 			/* 'L': Do descend through a symlink to dir. */
721 			descend = tree_current_is_dir(tree);
722 			/* 'L': Follow symlinks to files. */
723 			archive_read_disk_set_symlink_logical(bsdtar->diskreader);
724 			/* 'L': Archive symlinks as targets, if we can. */
725 			st = tree_current_stat(tree);
726 			if (st != NULL)
727 				break;
728 			/* If stat fails, we have a broken symlink;
729 			 * in that case, don't follow the link. */
730 			/* FALLTHROUGH */
731 		default:
732 			/* 'P': Don't descend through a symlink to dir. */
733 			descend = tree_current_is_physical_dir(tree);
734 			/* 'P': Don't follow symlinks to files. */
735 			archive_read_disk_set_symlink_physical(bsdtar->diskreader);
736 			/* 'P': Archive symlinks as symlinks. */
737 			st = lst;
738 			break;
739 		}
740 
741 		/*
742 		 * If user has asked us not to cross mount points,
743 		 * then don't descend into into a dir on a different
744 		 * device.
745 		 */
746 		if (!dev_recorded) {
747 			first_dev = lst->st_dev;
748 			dev_recorded = 1;
749 		}
750 		if (bsdtar->option_dont_traverse_mounts) {
751 			if (lst->st_dev != first_dev)
752 				descend = 0;
753 		}
754 
755 		/*
756 		 * In -u mode, check that the file is newer than what's
757 		 * already in the archive; in all modes, obey --newerXXX flags.
758 		 */
759 		if (!new_enough(bsdtar, name, st))
760 			continue;
761 
762 		archive_entry_free(entry);
763 		entry = archive_entry_new();
764 
765 		archive_entry_set_pathname(entry, name);
766 		archive_entry_copy_sourcepath(entry,
767 		    tree_current_access_path(tree));
768 
769 		/* Populate the archive_entry with metadata from the disk. */
770 		/* XXX TODO: Arrange to open a regular file before
771 		 * calling this so we can pass in an fd and shorten
772 		 * the race to query metadata.  The linkify dance
773 		 * makes this more complex than it might sound. */
774 		r = archive_read_disk_entry_from_file(bsdtar->diskreader,
775 		    entry, -1, st);
776 		if (r != ARCHIVE_OK)
777 			bsdtar_warnc(bsdtar, archive_errno(bsdtar->diskreader),
778 			    archive_error_string(bsdtar->diskreader));
779 		if (r < ARCHIVE_WARN)
780 			continue;
781 
782 		/* XXX TODO: Just use flag data from entry; avoid the
783 		 * duplicate check here. */
784 
785 		/*
786 		 * If this file/dir is flagged "nodump" and we're
787 		 * honoring such flags, skip this file/dir.
788 		 */
789 #ifdef HAVE_STRUCT_STAT_ST_FLAGS
790 		/* BSD systems store flags in struct stat */
791 		if (bsdtar->option_honor_nodump &&
792 		    (lst->st_flags & UF_NODUMP))
793 			continue;
794 #endif
795 
796 #if defined(EXT2_IOC_GETFLAGS) && defined(EXT2_NODUMP_FL)
797 		/* Linux uses ioctl to read flags. */
798 		if (bsdtar->option_honor_nodump) {
799 			int fd = open(name, O_RDONLY | O_NONBLOCK);
800 			if (fd >= 0) {
801 				unsigned long fflags;
802 				int r = ioctl(fd, EXT2_IOC_GETFLAGS, &fflags);
803 				close(fd);
804 				if (r >= 0 && (fflags & EXT2_NODUMP_FL))
805 					continue;
806 			}
807 		}
808 #endif
809 
810 		/*
811 		 * If the user vetoes this file/directory, skip it.
812 		 * We want this to be fairly late; if some other
813 		 * check would veto this file, we shouldn't bother
814 		 * the user with it.
815 		 */
816 		if (bsdtar->option_interactive &&
817 		    !yes("add '%s'", name))
818 			continue;
819 
820 		/* Note: if user vetoes, we won't descend. */
821 		if (descend && !bsdtar->option_no_subdirs)
822 			tree_descend(tree);
823 
824 		/*
825 		 * Rewrite the pathname to be archived.  If rewrite
826 		 * fails, skip the entry.
827 		 */
828 		if (edit_pathname(bsdtar, entry))
829 			continue;
830 
831 		/* Display entry as we process it.
832 		 * This format is required by SUSv2. */
833 		if (bsdtar->verbose)
834 			safe_fprintf(stderr, "a %s",
835 			    archive_entry_pathname(entry));
836 
837 		/* Non-regular files get archived with zero size. */
838 		if (!S_ISREG(st->st_mode))
839 			archive_entry_set_size(entry, 0);
840 
841 		/* Record what we're doing, for SIGINFO / SIGUSR1. */
842 		siginfo_setinfo(bsdtar, "adding",
843 		    archive_entry_pathname(entry), archive_entry_size(entry));
844 		archive_entry_linkify(bsdtar->resolver, &entry, &spare_entry);
845 
846 		/* Handle SIGINFO / SIGUSR1 request if one was made. */
847 		siginfo_printinfo(bsdtar, 0);
848 
849 		while (entry != NULL) {
850 			write_entry_backend(bsdtar, a, entry);
851 			archive_entry_free(entry);
852 			entry = spare_entry;
853 			spare_entry = NULL;
854 		}
855 
856 		if (bsdtar->verbose)
857 			fprintf(stderr, "\n");
858 	}
859 	archive_entry_free(entry);
860 	tree_close(tree);
861 }
862 
863 /*
864  * Backend for write_entry.
865  */
866 static void
867 write_entry_backend(struct bsdtar *bsdtar, struct archive *a,
868     struct archive_entry *entry)
869 {
870 	int fd = -1;
871 	int e;
872 
873 	if (archive_entry_size(entry) > 0) {
874 		const char *pathname = archive_entry_sourcepath(entry);
875 		fd = open(pathname, O_RDONLY);
876 		if (fd == -1) {
877 			if (!bsdtar->verbose)
878 				bsdtar_warnc(bsdtar, errno,
879 				    "%s: could not open file", pathname);
880 			else
881 				fprintf(stderr, ": %s", strerror(errno));
882 			return;
883 		}
884 	}
885 
886 	e = archive_write_header(a, entry);
887 	if (e != ARCHIVE_OK) {
888 		if (!bsdtar->verbose)
889 			bsdtar_warnc(bsdtar, 0, "%s: %s",
890 			    archive_entry_pathname(entry),
891 			    archive_error_string(a));
892 		else
893 			fprintf(stderr, ": %s", archive_error_string(a));
894 	}
895 
896 	if (e == ARCHIVE_FATAL)
897 		exit(1);
898 
899 	/*
900 	 * If we opened a file earlier, write it out now.  Note that
901 	 * the format handler might have reset the size field to zero
902 	 * to inform us that the archive body won't get stored.  In
903 	 * that case, just skip the write.
904 	 */
905 	if (e >= ARCHIVE_WARN && fd >= 0 && archive_entry_size(entry) > 0) {
906 		if (write_file_data(bsdtar, a, entry, fd))
907 			exit(1);
908 	}
909 
910 	/*
911 	 * If we opened a file, close it now even if there was an error
912 	 * which made us decide not to write the archive body.
913 	 */
914 	if (fd >= 0)
915 		close(fd);
916 }
917 
918 
919 /* Helper function to copy file to archive. */
920 static int
921 write_file_data(struct bsdtar *bsdtar, struct archive *a,
922     struct archive_entry *entry, int fd)
923 {
924 	ssize_t	bytes_read;
925 	ssize_t	bytes_written;
926 	off_t	progress = 0;
927 
928 	bytes_read = read(fd, bsdtar->buff, FILEDATABUFLEN);
929 	while (bytes_read > 0) {
930 		siginfo_printinfo(bsdtar, progress);
931 
932 		bytes_written = archive_write_data(a, bsdtar->buff,
933 		    bytes_read);
934 		if (bytes_written < 0) {
935 			/* Write failed; this is bad */
936 			bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(a));
937 			return (-1);
938 		}
939 		if (bytes_written < bytes_read) {
940 			/* Write was truncated; warn but continue. */
941 			bsdtar_warnc(bsdtar, 0,
942 			    "%s: Truncated write; file may have grown while being archived.",
943 			    archive_entry_pathname(entry));
944 			return (0);
945 		}
946 		progress += bytes_written;
947 		bytes_read = read(fd, bsdtar->buff, FILEDATABUFLEN);
948 	}
949 	return 0;
950 }
951 
952 /*
953  * Test if the specified file is new enough to include in the archive.
954  */
955 int
956 new_enough(struct bsdtar *bsdtar, const char *path, const struct stat *st)
957 {
958 	struct archive_dir_entry *p;
959 
960 	/*
961 	 * If this file/dir is excluded by a time comparison, skip it.
962 	 */
963 	if (bsdtar->newer_ctime_sec > 0) {
964 		if (st->st_ctime < bsdtar->newer_ctime_sec)
965 			return (0); /* Too old, skip it. */
966 		if (st->st_ctime == bsdtar->newer_ctime_sec
967 		    && ARCHIVE_STAT_CTIME_NANOS(st)
968 		    <= bsdtar->newer_ctime_nsec)
969 			return (0); /* Too old, skip it. */
970 	}
971 	if (bsdtar->newer_mtime_sec > 0) {
972 		if (st->st_mtime < bsdtar->newer_mtime_sec)
973 			return (0); /* Too old, skip it. */
974 		if (st->st_mtime == bsdtar->newer_mtime_sec
975 		    && ARCHIVE_STAT_MTIME_NANOS(st)
976 		    <= bsdtar->newer_mtime_nsec)
977 			return (0); /* Too old, skip it. */
978 	}
979 
980 	/*
981 	 * In -u mode, we only write an entry if it's newer than
982 	 * what was already in the archive.
983 	 */
984 	if (bsdtar->archive_dir != NULL &&
985 	    bsdtar->archive_dir->head != NULL) {
986 		for (p = bsdtar->archive_dir->head; p != NULL; p = p->next) {
987 			if (pathcmp(path, p->name)==0)
988 				return (p->mtime_sec < st->st_mtime ||
989 				    (p->mtime_sec == st->st_mtime &&
990 					p->mtime_nsec
991 					< ARCHIVE_STAT_MTIME_NANOS(st)));
992 		}
993 	}
994 
995 	/* If the file wasn't rejected, include it. */
996 	return (1);
997 }
998 
999 /*
1000  * Add an entry to the dir list for 'u' mode.
1001  *
1002  * XXX TODO: Make this fast.
1003  */
1004 static void
1005 add_dir_list(struct bsdtar *bsdtar, const char *path,
1006     time_t mtime_sec, int mtime_nsec)
1007 {
1008 	struct archive_dir_entry	*p;
1009 
1010 	/*
1011 	 * Search entire list to see if this file has appeared before.
1012 	 * If it has, override the timestamp data.
1013 	 */
1014 	p = bsdtar->archive_dir->head;
1015 	while (p != NULL) {
1016 		if (strcmp(path, p->name)==0) {
1017 			p->mtime_sec = mtime_sec;
1018 			p->mtime_nsec = mtime_nsec;
1019 			return;
1020 		}
1021 		p = p->next;
1022 	}
1023 
1024 	p = malloc(sizeof(*p));
1025 	if (p == NULL)
1026 		bsdtar_errc(bsdtar, 1, ENOMEM, "Can't read archive directory");
1027 
1028 	p->name = strdup(path);
1029 	if (p->name == NULL)
1030 		bsdtar_errc(bsdtar, 1, ENOMEM, "Can't read archive directory");
1031 	p->mtime_sec = mtime_sec;
1032 	p->mtime_nsec = mtime_nsec;
1033 	p->next = NULL;
1034 	if (bsdtar->archive_dir->tail == NULL) {
1035 		bsdtar->archive_dir->head = bsdtar->archive_dir->tail = p;
1036 	} else {
1037 		bsdtar->archive_dir->tail->next = p;
1038 		bsdtar->archive_dir->tail = p;
1039 	}
1040 }
1041 
1042 void
1043 test_for_append(struct bsdtar *bsdtar)
1044 {
1045 	struct stat s;
1046 
1047 	if (*bsdtar->argv == NULL && bsdtar->names_from_file == NULL)
1048 		bsdtar_errc(bsdtar, 1, 0, "no files or directories specified");
1049 	if (bsdtar->filename == NULL)
1050 		bsdtar_errc(bsdtar, 1, 0, "Cannot append to stdout.");
1051 
1052 	if (bsdtar->create_compression != 0)
1053 		bsdtar_errc(bsdtar, 1, 0,
1054 		    "Cannot append to %s with compression", bsdtar->filename);
1055 
1056 	if (stat(bsdtar->filename, &s) != 0)
1057 		return;
1058 
1059 	if (!S_ISREG(s.st_mode) && !S_ISBLK(s.st_mode))
1060 		bsdtar_errc(bsdtar, 1, 0,
1061 		    "Cannot append to %s: not a regular file.",
1062 		    bsdtar->filename);
1063 }
1064