Import libarchive-2.5.4b.
[dragonfly.git] / contrib / libarchive-2 / tar / write.c
blob432612ac7a0ff71c0d60b09e480bf727da4b9c98
1 /*-
2 * Copyright (c) 2003-2007 Tim Kientzle
3 * All rights reserved.
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.
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.
26 #include "bsdtar_platform.h"
27 __FBSDID("$FreeBSD: src/usr.bin/tar/write.c,v 1.69 2008/05/23 05:07:22 cperciva Exp $");
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_EXT2FS_EXT2_FS_H
48 #include <ext2fs/ext2_fs.h>
49 #endif
50 #ifdef HAVE_FCNTL_H
51 #include <fcntl.h>
52 #endif
53 #ifdef HAVE_FNMATCH_H
54 #include <fnmatch.h>
55 #endif
56 #ifdef HAVE_GRP_H
57 #include <grp.h>
58 #endif
59 #ifdef HAVE_LIMITS_H
60 #include <limits.h>
61 #endif
62 #ifdef HAVE_LINUX_FS_H
63 #include <linux/fs.h> /* for Linux file flags */
64 #endif
65 #ifdef HAVE_LINUX_EXT2_FS_H
66 #include <linux/ext2_fs.h> /* for Linux file flags */
67 #endif
68 #ifdef HAVE_PWD_H
69 #include <pwd.h>
70 #endif
71 #include <stdio.h>
72 #ifdef HAVE_STDLIB_H
73 #include <stdlib.h>
74 #endif
75 #ifdef HAVE_STRING_H
76 #include <string.h>
77 #endif
78 #ifdef HAVE_UNISTD_H
79 #include <unistd.h>
80 #endif
82 #include "bsdtar.h"
83 #include "tree.h"
85 /* Fixed size of uname/gname caches. */
86 #define name_cache_size 101
88 static const char * const NO_NAME = "(noname)";
90 struct archive_dir_entry {
91 struct archive_dir_entry *next;
92 time_t mtime_sec;
93 int mtime_nsec;
94 char *name;
97 struct archive_dir {
98 struct archive_dir_entry *head, *tail;
101 struct name_cache {
102 int probes;
103 int hits;
104 size_t size;
105 struct {
106 id_t id;
107 const char *name;
108 } cache[name_cache_size];
111 static void add_dir_list(struct bsdtar *bsdtar, const char *path,
112 time_t mtime_sec, int mtime_nsec);
113 static int append_archive(struct bsdtar *, struct archive *,
114 struct archive *ina);
115 static int append_archive_filename(struct bsdtar *,
116 struct archive *, const char *fname);
117 static void archive_names_from_file(struct bsdtar *bsdtar,
118 struct archive *a);
119 static int archive_names_from_file_helper(struct bsdtar *bsdtar,
120 const char *line);
121 static int copy_file_data(struct bsdtar *bsdtar,
122 struct archive *a, struct archive *ina);
123 static void create_cleanup(struct bsdtar *);
124 static void free_cache(struct name_cache *cache);
125 static const char * lookup_gname(struct bsdtar *bsdtar, gid_t gid);
126 static int lookup_gname_helper(struct bsdtar *bsdtar,
127 const char **name, id_t gid);
128 static const char * lookup_uname(struct bsdtar *bsdtar, uid_t uid);
129 static int lookup_uname_helper(struct bsdtar *bsdtar,
130 const char **name, id_t uid);
131 static int new_enough(struct bsdtar *, const char *path,
132 const struct stat *);
133 static void setup_acls(struct bsdtar *, struct archive_entry *,
134 const char *path);
135 static void setup_xattrs(struct bsdtar *, struct archive_entry *,
136 const char *path);
137 static void test_for_append(struct bsdtar *);
138 static void write_archive(struct archive *, struct bsdtar *);
139 static void write_entry(struct bsdtar *, struct archive *,
140 const struct stat *, const char *pathname,
141 const char *accpath);
142 static void write_entry_backend(struct bsdtar *, struct archive *,
143 struct archive_entry *, int);
144 static int write_file_data(struct bsdtar *, struct archive *,
145 int fd);
146 static void write_hierarchy(struct bsdtar *, struct archive *,
147 const char *);
149 void
150 tar_mode_c(struct bsdtar *bsdtar)
152 struct archive *a;
153 int r;
155 if (*bsdtar->argv == NULL && bsdtar->names_from_file == NULL)
156 bsdtar_errc(bsdtar, 1, 0, "no files or directories specified");
158 /* We want to catch SIGINFO and SIGUSR1. */
159 siginfo_init(bsdtar);
161 a = archive_write_new();
163 /* Support any format that the library supports. */
164 if (bsdtar->create_format == NULL) {
165 r = archive_write_set_format_pax_restricted(a);
166 bsdtar->create_format = "pax restricted";
167 } else {
168 r = archive_write_set_format_by_name(a, bsdtar->create_format);
170 if (r != ARCHIVE_OK) {
171 fprintf(stderr, "Can't use format %s: %s\n",
172 bsdtar->create_format,
173 archive_error_string(a));
174 usage(bsdtar);
178 * If user explicitly set the block size, then assume they
179 * want the last block padded as well. Otherwise, use the
180 * default block size and accept archive_write_open_file()'s
181 * default padding decisions.
183 if (bsdtar->bytes_per_block != 0) {
184 archive_write_set_bytes_per_block(a, bsdtar->bytes_per_block);
185 archive_write_set_bytes_in_last_block(a,
186 bsdtar->bytes_per_block);
187 } else
188 archive_write_set_bytes_per_block(a, DEFAULT_BYTES_PER_BLOCK);
190 if (bsdtar->compress_program) {
191 archive_write_set_compression_program(a, bsdtar->compress_program);
192 } else {
193 switch (bsdtar->create_compression) {
194 case 0:
195 archive_write_set_compression_none(a);
196 break;
197 #ifdef HAVE_LIBBZ2
198 case 'j': case 'y':
199 archive_write_set_compression_bzip2(a);
200 break;
201 #endif
202 #ifdef HAVE_LIBZ
203 case 'z':
204 archive_write_set_compression_gzip(a);
205 break;
206 #endif
207 case 'Z':
208 archive_write_set_compression_compress(a);
209 break;
210 default:
211 bsdtar_errc(bsdtar, 1, 0,
212 "Unrecognized compression option -%c",
213 bsdtar->create_compression);
217 r = archive_write_open_file(a, bsdtar->filename);
218 if (r != ARCHIVE_OK)
219 bsdtar_errc(bsdtar, 1, 0, archive_error_string(a));
221 write_archive(a, bsdtar);
223 if (bsdtar->option_totals) {
224 fprintf(stderr, "Total bytes written: " BSDTAR_FILESIZE_PRINTF "\n",
225 (BSDTAR_FILESIZE_TYPE)archive_position_compressed(a));
228 archive_write_finish(a);
230 /* Restore old SIGINFO + SIGUSR1 handlers. */
231 siginfo_done(bsdtar);
235 * Same as 'c', except we only support tar or empty formats in
236 * uncompressed files on disk.
238 void
239 tar_mode_r(struct bsdtar *bsdtar)
241 off_t end_offset;
242 int format;
243 struct archive *a;
244 struct archive_entry *entry;
245 int r;
247 /* Sanity-test some arguments and the file. */
248 test_for_append(bsdtar);
250 /* We want to catch SIGINFO and SIGUSR1. */
251 siginfo_init(bsdtar);
253 format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
255 bsdtar->fd = open(bsdtar->filename, O_RDWR | O_CREAT, 0666);
256 if (bsdtar->fd < 0)
257 bsdtar_errc(bsdtar, 1, errno,
258 "Cannot open %s", bsdtar->filename);
260 a = archive_read_new();
261 archive_read_support_compression_all(a);
262 archive_read_support_format_tar(a);
263 archive_read_support_format_gnutar(a);
264 r = archive_read_open_fd(a, bsdtar->fd, 10240);
265 if (r != ARCHIVE_OK)
266 bsdtar_errc(bsdtar, 1, archive_errno(a),
267 "Can't read archive %s: %s", bsdtar->filename,
268 archive_error_string(a));
269 while (0 == archive_read_next_header(a, &entry)) {
270 if (archive_compression(a) != ARCHIVE_COMPRESSION_NONE) {
271 archive_read_finish(a);
272 close(bsdtar->fd);
273 bsdtar_errc(bsdtar, 1, 0,
274 "Cannot append to compressed archive.");
276 /* Keep going until we hit end-of-archive */
277 format = archive_format(a);
280 end_offset = archive_read_header_position(a);
281 archive_read_finish(a);
283 /* Re-open archive for writing */
284 a = archive_write_new();
285 archive_write_set_compression_none(a);
287 * Set the format to be used for writing. To allow people to
288 * extend empty files, we need to allow them to specify the format,
289 * which opens the possibility that they will specify a format that
290 * doesn't match the existing format. Hence, the following bit
291 * of arcane ugliness.
294 if (bsdtar->create_format != NULL) {
295 /* If the user requested a format, use that, but ... */
296 archive_write_set_format_by_name(a,
297 bsdtar->create_format);
298 /* ... complain if it's not compatible. */
299 format &= ARCHIVE_FORMAT_BASE_MASK;
300 if (format != (int)(archive_format(a) & ARCHIVE_FORMAT_BASE_MASK)
301 && format != ARCHIVE_FORMAT_EMPTY) {
302 bsdtar_errc(bsdtar, 1, 0,
303 "Format %s is incompatible with the archive %s.",
304 bsdtar->create_format, bsdtar->filename);
306 } else {
308 * Just preserve the current format, with a little care
309 * for formats that libarchive can't write.
311 if (format == ARCHIVE_FORMAT_TAR_GNUTAR)
312 /* TODO: When gtar supports pax, use pax restricted. */
313 format = ARCHIVE_FORMAT_TAR_USTAR;
314 if (format == ARCHIVE_FORMAT_EMPTY)
315 format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
316 archive_write_set_format(a, format);
318 lseek(bsdtar->fd, end_offset, SEEK_SET); /* XXX check return val XXX */
319 archive_write_open_fd(a, bsdtar->fd); /* XXX check return val XXX */
321 write_archive(a, bsdtar); /* XXX check return val XXX */
323 if (bsdtar->option_totals) {
324 fprintf(stderr, "Total bytes written: " BSDTAR_FILESIZE_PRINTF "\n",
325 (BSDTAR_FILESIZE_TYPE)archive_position_compressed(a));
328 archive_write_finish(a);
329 close(bsdtar->fd);
330 bsdtar->fd = -1;
333 void
334 tar_mode_u(struct bsdtar *bsdtar)
336 off_t end_offset;
337 struct archive *a;
338 struct archive_entry *entry;
339 int format;
340 struct archive_dir_entry *p;
341 struct archive_dir archive_dir;
343 bsdtar->archive_dir = &archive_dir;
344 memset(&archive_dir, 0, sizeof(archive_dir));
346 format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
348 /* Sanity-test some arguments and the file. */
349 test_for_append(bsdtar);
351 /* We want to catch SIGINFO and SIGUSR1. */
352 siginfo_init(bsdtar);
354 bsdtar->fd = open(bsdtar->filename, O_RDWR);
355 if (bsdtar->fd < 0)
356 bsdtar_errc(bsdtar, 1, errno,
357 "Cannot open %s", bsdtar->filename);
359 a = archive_read_new();
360 archive_read_support_compression_all(a);
361 archive_read_support_format_tar(a);
362 archive_read_support_format_gnutar(a);
363 if (archive_read_open_fd(a, bsdtar->fd,
364 bsdtar->bytes_per_block != 0 ? bsdtar->bytes_per_block :
365 DEFAULT_BYTES_PER_BLOCK) != ARCHIVE_OK) {
366 bsdtar_errc(bsdtar, 1, 0,
367 "Can't open %s: %s", bsdtar->filename,
368 archive_error_string(a));
371 /* Build a list of all entries and their recorded mod times. */
372 while (0 == archive_read_next_header(a, &entry)) {
373 if (archive_compression(a) != ARCHIVE_COMPRESSION_NONE) {
374 archive_read_finish(a);
375 close(bsdtar->fd);
376 bsdtar_errc(bsdtar, 1, 0,
377 "Cannot append to compressed archive.");
379 add_dir_list(bsdtar, archive_entry_pathname(entry),
380 archive_entry_mtime(entry),
381 archive_entry_mtime_nsec(entry));
382 /* Record the last format determination we see */
383 format = archive_format(a);
384 /* Keep going until we hit end-of-archive */
387 end_offset = archive_read_header_position(a);
388 archive_read_finish(a);
390 /* Re-open archive for writing. */
391 a = archive_write_new();
392 archive_write_set_compression_none(a);
394 * Set format to same one auto-detected above, except that
395 * we don't write GNU tar format, so use ustar instead.
397 if (format == ARCHIVE_FORMAT_TAR_GNUTAR)
398 format = ARCHIVE_FORMAT_TAR_USTAR;
399 archive_write_set_format(a, format);
400 if (bsdtar->bytes_per_block != 0) {
401 archive_write_set_bytes_per_block(a, bsdtar->bytes_per_block);
402 archive_write_set_bytes_in_last_block(a,
403 bsdtar->bytes_per_block);
404 } else
405 archive_write_set_bytes_per_block(a, DEFAULT_BYTES_PER_BLOCK);
406 lseek(bsdtar->fd, end_offset, SEEK_SET);
407 ftruncate(bsdtar->fd, end_offset);
408 archive_write_open_fd(a, bsdtar->fd);
410 write_archive(a, bsdtar);
412 if (bsdtar->option_totals) {
413 fprintf(stderr, "Total bytes written: " BSDTAR_FILESIZE_PRINTF "\n",
414 (BSDTAR_FILESIZE_TYPE)archive_position_compressed(a));
417 archive_write_finish(a);
418 close(bsdtar->fd);
419 bsdtar->fd = -1;
421 while (bsdtar->archive_dir->head != NULL) {
422 p = bsdtar->archive_dir->head->next;
423 free(bsdtar->archive_dir->head->name);
424 free(bsdtar->archive_dir->head);
425 bsdtar->archive_dir->head = p;
427 bsdtar->archive_dir->tail = NULL;
432 * Write user-specified files/dirs to opened archive.
434 static void
435 write_archive(struct archive *a, struct bsdtar *bsdtar)
437 const char *arg;
438 struct archive_entry *entry, *sparse_entry;
440 if ((bsdtar->resolver = archive_entry_linkresolver_new()) == NULL)
441 bsdtar_errc(bsdtar, 1, 0, "cannot create link resolver");
442 archive_entry_linkresolver_set_strategy(bsdtar->resolver,
443 archive_format(a));
445 if (bsdtar->names_from_file != NULL)
446 archive_names_from_file(bsdtar, a);
448 while (*bsdtar->argv) {
449 arg = *bsdtar->argv;
450 if (arg[0] == '-' && arg[1] == 'C') {
451 arg += 2;
452 if (*arg == '\0') {
453 bsdtar->argv++;
454 arg = *bsdtar->argv;
455 if (arg == NULL) {
456 bsdtar_warnc(bsdtar, 1, 0,
457 "Missing argument for -C");
458 bsdtar->return_value = 1;
459 return;
462 set_chdir(bsdtar, arg);
463 } else {
464 if (*arg != '/' && (arg[0] != '@' || arg[1] != '/'))
465 do_chdir(bsdtar); /* Handle a deferred -C */
466 if (*arg == '@') {
467 if (append_archive_filename(bsdtar, a,
468 arg + 1) != 0)
469 break;
470 } else
471 write_hierarchy(bsdtar, a, arg);
473 bsdtar->argv++;
476 entry = NULL;
477 archive_entry_linkify(bsdtar->resolver, &entry, &sparse_entry);
478 while (entry != NULL) {
479 int fd = -1;
480 write_entry_backend(bsdtar, a, entry, fd);
481 archive_entry_free(entry);
482 entry = NULL;
483 archive_entry_linkify(bsdtar->resolver, &entry, &sparse_entry);
486 create_cleanup(bsdtar);
487 if (archive_write_close(a)) {
488 bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(a));
489 bsdtar->return_value = 1;
494 * Archive names specified in file.
496 * Unless --null was specified, a line containing exactly "-C" will
497 * cause the next line to be a directory to pass to chdir(). If
498 * --null is specified, then a line "-C" is just another filename.
500 void
501 archive_names_from_file(struct bsdtar *bsdtar, struct archive *a)
503 bsdtar->archive = a;
505 bsdtar->next_line_is_dir = 0;
506 process_lines(bsdtar, bsdtar->names_from_file,
507 archive_names_from_file_helper);
508 if (bsdtar->next_line_is_dir)
509 bsdtar_errc(bsdtar, 1, errno,
510 "Unexpected end of filename list; "
511 "directory expected after -C");
514 static int
515 archive_names_from_file_helper(struct bsdtar *bsdtar, const char *line)
517 if (bsdtar->next_line_is_dir) {
518 set_chdir(bsdtar, line);
519 bsdtar->next_line_is_dir = 0;
520 } else if (!bsdtar->option_null && strcmp(line, "-C") == 0)
521 bsdtar->next_line_is_dir = 1;
522 else {
523 if (*line != '/')
524 do_chdir(bsdtar); /* Handle a deferred -C */
525 write_hierarchy(bsdtar, bsdtar->archive, line);
527 return (0);
531 * Copy from specified archive to current archive. Returns non-zero
532 * for write errors (which force us to terminate the entire archiving
533 * operation). If there are errors reading the input archive, we set
534 * bsdtar->return_value but return zero, so the overall archiving
535 * operation will complete and return non-zero.
537 static int
538 append_archive_filename(struct bsdtar *bsdtar, struct archive *a,
539 const char *filename)
541 struct archive *ina;
542 int rc;
544 if (strcmp(filename, "-") == 0)
545 filename = NULL; /* Library uses NULL for stdio. */
547 ina = archive_read_new();
548 archive_read_support_format_all(ina);
549 archive_read_support_compression_all(ina);
550 if (archive_read_open_file(ina, filename, 10240)) {
551 bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(ina));
552 bsdtar->return_value = 1;
553 return (0);
556 rc = append_archive(bsdtar, a, ina);
558 if (archive_errno(ina)) {
559 bsdtar_warnc(bsdtar, 0, "Error reading archive %s: %s",
560 filename, archive_error_string(ina));
561 bsdtar->return_value = 1;
563 archive_read_finish(ina);
565 return (rc);
568 static int
569 append_archive(struct bsdtar *bsdtar, struct archive *a, struct archive *ina)
571 struct archive_entry *in_entry;
572 int e;
574 while (0 == archive_read_next_header(ina, &in_entry)) {
575 if (!new_enough(bsdtar, archive_entry_pathname(in_entry),
576 archive_entry_stat(in_entry)))
577 continue;
578 if (excluded(bsdtar, archive_entry_pathname(in_entry)))
579 continue;
580 if (bsdtar->option_interactive &&
581 !yes("copy '%s'", archive_entry_pathname(in_entry)))
582 continue;
583 if (bsdtar->verbose)
584 safe_fprintf(stderr, "a %s",
585 archive_entry_pathname(in_entry));
586 siginfo_setinfo(bsdtar, "copying",
587 archive_entry_pathname(in_entry),
588 archive_entry_size(in_entry));
589 siginfo_printinfo(bsdtar, 0);
591 e = archive_write_header(a, in_entry);
592 if (e != ARCHIVE_OK) {
593 if (!bsdtar->verbose)
594 bsdtar_warnc(bsdtar, 0, "%s: %s",
595 archive_entry_pathname(in_entry),
596 archive_error_string(a));
597 else
598 fprintf(stderr, ": %s", archive_error_string(a));
600 if (e == ARCHIVE_FATAL)
601 exit(1);
603 if (e >= ARCHIVE_WARN) {
604 if (archive_entry_size(in_entry) == 0)
605 archive_read_data_skip(ina);
606 else if (copy_file_data(bsdtar, a, ina))
607 exit(1);
610 if (bsdtar->verbose)
611 fprintf(stderr, "\n");
614 /* Note: If we got here, we saw no write errors, so return success. */
615 return (0);
618 /* Helper function to copy data between archives. */
619 static int
620 copy_file_data(struct bsdtar *bsdtar, struct archive *a, struct archive *ina)
622 char buff[64*1024];
623 ssize_t bytes_read;
624 ssize_t bytes_written;
625 off_t progress = 0;
627 bytes_read = archive_read_data(ina, buff, sizeof(buff));
628 while (bytes_read > 0) {
629 siginfo_printinfo(bsdtar, progress);
631 bytes_written = archive_write_data(a, buff, bytes_read);
632 if (bytes_written < bytes_read) {
633 bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(a));
634 return (-1);
636 progress += bytes_written;
637 bytes_read = archive_read_data(ina, buff, sizeof(buff));
640 return (0);
644 * Add the file or dir hierarchy named by 'path' to the archive
646 static void
647 write_hierarchy(struct bsdtar *bsdtar, struct archive *a, const char *path)
649 struct tree *tree;
650 char symlink_mode = bsdtar->symlink_mode;
651 dev_t first_dev = 0;
652 int dev_recorded = 0;
653 int tree_ret;
654 #ifdef __linux
655 int fd, r;
656 unsigned long fflags;
657 #endif
659 tree = tree_open(path);
661 if (!tree) {
662 bsdtar_warnc(bsdtar, errno, "%s: Cannot open", path);
663 bsdtar->return_value = 1;
664 return;
667 while ((tree_ret = tree_next(tree))) {
668 const char *name = tree_current_path(tree);
669 const struct stat *st = NULL, *lst = NULL;
670 int descend;
672 if (tree_ret == TREE_ERROR_DIR)
673 bsdtar_warnc(bsdtar, errno, "%s: Couldn't visit directory", name);
674 if (tree_ret != TREE_REGULAR)
675 continue;
676 lst = tree_current_lstat(tree);
677 if (lst == NULL) {
678 /* Couldn't lstat(); must not exist. */
679 bsdtar_warnc(bsdtar, errno, "%s: Cannot stat", name);
682 * Report an error via the exit code if the failed
683 * path is a prefix of what the user provided via
684 * the command line. (Testing for string equality
685 * here won't work due to trailing '/' characters.)
687 if (memcmp(name, path, strlen(name)) == 0)
688 bsdtar->return_value = 1;
690 continue;
692 if (S_ISLNK(lst->st_mode))
693 st = tree_current_stat(tree);
694 /* Default: descend into any dir or symlink to dir. */
695 /* We'll adjust this later on. */
696 descend = 0;
697 if ((st != NULL) && S_ISDIR(st->st_mode))
698 descend = 1;
699 if ((lst != NULL) && S_ISDIR(lst->st_mode))
700 descend = 1;
703 * If user has asked us not to cross mount points,
704 * then don't descend into into a dir on a different
705 * device.
707 if (!dev_recorded) {
708 first_dev = lst->st_dev;
709 dev_recorded = 1;
711 if (bsdtar->option_dont_traverse_mounts) {
712 if (lst != NULL && lst->st_dev != first_dev)
713 descend = 0;
717 * If this file/dir is flagged "nodump" and we're
718 * honoring such flags, skip this file/dir.
720 #ifdef HAVE_CHFLAGS
721 if (bsdtar->option_honor_nodump &&
722 (lst->st_flags & UF_NODUMP))
723 continue;
724 #endif
726 #ifdef __linux
728 * Linux has a nodump flag too but to read it
729 * we have to open() the file/dir and do an ioctl on it...
731 if (bsdtar->option_honor_nodump &&
732 ((fd = open(name, O_RDONLY|O_NONBLOCK)) >= 0) &&
733 ((r = ioctl(fd, EXT2_IOC_GETFLAGS, &fflags)),
734 close(fd), r) >= 0 &&
735 (fflags & EXT2_NODUMP_FL))
736 continue;
737 #endif
740 * If this file/dir is excluded by a filename
741 * pattern, skip it.
743 if (excluded(bsdtar, name))
744 continue;
747 * If the user vetoes this file/directory, skip it.
749 if (bsdtar->option_interactive &&
750 !yes("add '%s'", name))
751 continue;
754 * If this is a dir, decide whether or not to recurse.
756 if (bsdtar->option_no_subdirs)
757 descend = 0;
760 * Distinguish 'L'/'P'/'H' symlink following.
762 switch(symlink_mode) {
763 case 'H':
764 /* 'H': After the first item, rest like 'P'. */
765 symlink_mode = 'P';
766 /* 'H': First item (from command line) like 'L'. */
767 /* FALLTHROUGH */
768 case 'L':
769 /* 'L': Do descend through a symlink to dir. */
770 /* 'L': Archive symlink to file as file. */
771 lst = tree_current_stat(tree);
772 /* If stat fails, we have a broken symlink;
773 * in that case, archive the link as such. */
774 if (lst == NULL)
775 lst = tree_current_lstat(tree);
776 break;
777 default:
778 /* 'P': Don't descend through a symlink to dir. */
779 if (!S_ISDIR(lst->st_mode))
780 descend = 0;
781 /* 'P': Archive symlink to file as symlink. */
782 /* lst = tree_current_lstat(tree); */
783 break;
786 if (descend)
787 tree_descend(tree);
790 * Write the entry. Note that write_entry() handles
791 * pathname editing and newness testing.
793 write_entry(bsdtar, a, lst, name,
794 tree_current_access_path(tree));
796 tree_close(tree);
800 * Backend for write_entry.
802 static void
803 write_entry_backend(struct bsdtar *bsdtar, struct archive *a,
804 struct archive_entry *entry, int fd)
806 int e;
808 if (fd == -1 && archive_entry_size(entry) > 0) {
809 const char *pathname = archive_entry_sourcepath(entry);
810 fd = open(pathname, O_RDONLY);
811 if (fd == -1) {
812 if (!bsdtar->verbose)
813 bsdtar_warnc(bsdtar, errno,
814 "%s: could not open file", pathname);
815 else
816 fprintf(stderr, ": %s", strerror(errno));
817 return;
821 e = archive_write_header(a, entry);
822 if (e != ARCHIVE_OK) {
823 if (!bsdtar->verbose)
824 bsdtar_warnc(bsdtar, 0, "%s: %s",
825 archive_entry_pathname(entry),
826 archive_error_string(a));
827 else
828 fprintf(stderr, ": %s", archive_error_string(a));
831 if (e == ARCHIVE_FATAL)
832 exit(1);
835 * If we opened a file earlier, write it out now. Note that
836 * the format handler might have reset the size field to zero
837 * to inform us that the archive body won't get stored. In
838 * that case, just skip the write.
840 if (e >= ARCHIVE_WARN && fd >= 0 && archive_entry_size(entry) > 0) {
841 if (write_file_data(bsdtar, a, fd))
842 exit(1);
843 close(fd);
848 * Add a single filesystem object to the archive.
850 static void
851 write_entry(struct bsdtar *bsdtar, struct archive *a, const struct stat *st,
852 const char *pathname, const char *accpath)
854 struct archive_entry *entry, *sparse_entry;
855 int fd;
856 #ifdef __linux
857 int r;
858 unsigned long stflags;
859 #endif
860 static char linkbuffer[PATH_MAX+1];
862 fd = -1;
863 entry = archive_entry_new();
865 archive_entry_set_pathname(entry, pathname);
866 archive_entry_copy_sourcepath(entry, accpath);
869 * Rewrite the pathname to be archived. If rewrite
870 * fails, skip the entry.
872 if (edit_pathname(bsdtar, entry))
873 goto abort;
876 * In -u mode, check that the file is newer than what's
877 * already in the archive; in all modes, obey --newerXXX flags.
879 if (!new_enough(bsdtar, archive_entry_pathname(entry), st))
880 goto abort;
882 /* Display entry as we process it. This format is required by SUSv2. */
883 if (bsdtar->verbose)
884 safe_fprintf(stderr, "a %s", archive_entry_pathname(entry));
886 /* Read symbolic link information. */
887 if ((st->st_mode & S_IFMT) == S_IFLNK) {
888 int lnklen;
890 lnklen = readlink(accpath, linkbuffer, PATH_MAX);
891 if (lnklen < 0) {
892 if (!bsdtar->verbose)
893 bsdtar_warnc(bsdtar, errno,
894 "%s: Couldn't read symbolic link",
895 pathname);
896 else
897 safe_fprintf(stderr,
898 ": Couldn't read symbolic link: %s",
899 strerror(errno));
900 goto cleanup;
902 linkbuffer[lnklen] = 0;
903 archive_entry_set_symlink(entry, linkbuffer);
906 /* Look up username and group name. */
907 archive_entry_set_uname(entry, lookup_uname(bsdtar, st->st_uid));
908 archive_entry_set_gname(entry, lookup_gname(bsdtar, st->st_gid));
910 #ifdef HAVE_CHFLAGS
911 if (st->st_flags != 0)
912 archive_entry_set_fflags(entry, st->st_flags, 0);
913 #endif
915 #ifdef __linux
916 if ((S_ISREG(st->st_mode) || S_ISDIR(st->st_mode)) &&
917 ((fd = open(accpath, O_RDONLY|O_NONBLOCK)) >= 0) &&
918 ((r = ioctl(fd, EXT2_IOC_GETFLAGS, &stflags)), close(fd), (fd = -1), r) >= 0 &&
919 stflags) {
920 archive_entry_set_fflags(entry, stflags, 0);
922 #endif
924 archive_entry_copy_stat(entry, st);
925 setup_acls(bsdtar, entry, accpath);
926 setup_xattrs(bsdtar, entry, accpath);
928 /* Non-regular files get archived with zero size. */
929 if (!S_ISREG(st->st_mode))
930 archive_entry_set_size(entry, 0);
932 /* Record what we're doing, for the benefit of SIGINFO / SIGUSR1. */
933 siginfo_setinfo(bsdtar, "adding", archive_entry_pathname(entry),
934 archive_entry_size(entry));
935 archive_entry_linkify(bsdtar->resolver, &entry, &sparse_entry);
937 /* Handle SIGINFO / SIGUSR1 request if one was made. */
938 siginfo_printinfo(bsdtar, 0);
940 while (entry != NULL) {
941 write_entry_backend(bsdtar, a, entry, fd);
942 fd = -1;
943 archive_entry_free(entry);
944 entry = sparse_entry;
945 sparse_entry = NULL;
948 cleanup:
949 if (bsdtar->verbose)
950 fprintf(stderr, "\n");
952 abort:
953 if (fd >= 0)
954 close(fd);
956 archive_entry_free(entry);
960 /* Helper function to copy file to archive, with stack-allocated buffer. */
961 static int
962 write_file_data(struct bsdtar *bsdtar, struct archive *a, int fd)
964 char buff[64*1024];
965 ssize_t bytes_read;
966 ssize_t bytes_written;
967 off_t progress = 0;
969 /* XXX TODO: Allocate buffer on heap and store pointer to
970 * it in bsdtar structure; arrange cleanup as well. XXX */
972 bytes_read = read(fd, buff, sizeof(buff));
973 while (bytes_read > 0) {
974 siginfo_printinfo(bsdtar, progress);
976 bytes_written = archive_write_data(a, buff, bytes_read);
977 if (bytes_written < 0) {
978 /* Write failed; this is bad */
979 bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(a));
980 return (-1);
982 if (bytes_written < bytes_read) {
983 /* Write was truncated; warn but continue. */
984 bsdtar_warnc(bsdtar, 0,
985 "Truncated write; file may have grown while being archived.");
986 return (0);
988 progress += bytes_written;
989 bytes_read = read(fd, buff, sizeof(buff));
991 return 0;
995 static void
996 create_cleanup(struct bsdtar *bsdtar)
998 free_cache(bsdtar->uname_cache);
999 bsdtar->uname_cache = NULL;
1000 free_cache(bsdtar->gname_cache);
1001 bsdtar->gname_cache = NULL;
1004 #ifdef HAVE_POSIX_ACL
1005 static void setup_acl(struct bsdtar *bsdtar,
1006 struct archive_entry *entry, const char *accpath,
1007 int acl_type, int archive_entry_acl_type);
1009 static void
1010 setup_acls(struct bsdtar *bsdtar, struct archive_entry *entry,
1011 const char *accpath)
1013 archive_entry_acl_clear(entry);
1015 setup_acl(bsdtar, entry, accpath,
1016 ACL_TYPE_ACCESS, ARCHIVE_ENTRY_ACL_TYPE_ACCESS);
1017 /* Only directories can have default ACLs. */
1018 if (S_ISDIR(archive_entry_mode(entry)))
1019 setup_acl(bsdtar, entry, accpath,
1020 ACL_TYPE_DEFAULT, ARCHIVE_ENTRY_ACL_TYPE_DEFAULT);
1023 static void
1024 setup_acl(struct bsdtar *bsdtar, struct archive_entry *entry,
1025 const char *accpath, int acl_type, int archive_entry_acl_type)
1027 acl_t acl;
1028 acl_tag_t acl_tag;
1029 acl_entry_t acl_entry;
1030 acl_permset_t acl_permset;
1031 int s, ae_id, ae_tag, ae_perm;
1032 const char *ae_name;
1034 /* Retrieve access ACL from file. */
1035 acl = acl_get_file(accpath, acl_type);
1036 if (acl != NULL) {
1037 s = acl_get_entry(acl, ACL_FIRST_ENTRY, &acl_entry);
1038 while (s == 1) {
1039 ae_id = -1;
1040 ae_name = NULL;
1042 acl_get_tag_type(acl_entry, &acl_tag);
1043 if (acl_tag == ACL_USER) {
1044 ae_id = (int)*(uid_t *)acl_get_qualifier(acl_entry);
1045 ae_name = lookup_uname(bsdtar, ae_id);
1046 ae_tag = ARCHIVE_ENTRY_ACL_USER;
1047 } else if (acl_tag == ACL_GROUP) {
1048 ae_id = (int)*(gid_t *)acl_get_qualifier(acl_entry);
1049 ae_name = lookup_gname(bsdtar, ae_id);
1050 ae_tag = ARCHIVE_ENTRY_ACL_GROUP;
1051 } else if (acl_tag == ACL_MASK) {
1052 ae_tag = ARCHIVE_ENTRY_ACL_MASK;
1053 } else if (acl_tag == ACL_USER_OBJ) {
1054 ae_tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
1055 } else if (acl_tag == ACL_GROUP_OBJ) {
1056 ae_tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
1057 } else if (acl_tag == ACL_OTHER) {
1058 ae_tag = ARCHIVE_ENTRY_ACL_OTHER;
1059 } else {
1060 /* Skip types that libarchive can't support. */
1061 continue;
1064 acl_get_permset(acl_entry, &acl_permset);
1065 ae_perm = 0;
1067 * acl_get_perm() is spelled differently on different
1068 * platforms; see bsdtar_platform.h for details.
1070 if (ACL_GET_PERM(acl_permset, ACL_EXECUTE))
1071 ae_perm |= ARCHIVE_ENTRY_ACL_EXECUTE;
1072 if (ACL_GET_PERM(acl_permset, ACL_READ))
1073 ae_perm |= ARCHIVE_ENTRY_ACL_READ;
1074 if (ACL_GET_PERM(acl_permset, ACL_WRITE))
1075 ae_perm |= ARCHIVE_ENTRY_ACL_WRITE;
1077 archive_entry_acl_add_entry(entry,
1078 archive_entry_acl_type, ae_perm, ae_tag,
1079 ae_id, ae_name);
1081 s = acl_get_entry(acl, ACL_NEXT_ENTRY, &acl_entry);
1083 acl_free(acl);
1086 #else
1087 static void
1088 setup_acls(struct bsdtar *bsdtar, struct archive_entry *entry,
1089 const char *accpath)
1091 (void)bsdtar;
1092 (void)entry;
1093 (void)accpath;
1095 #endif
1097 #if HAVE_LISTXATTR && HAVE_LLISTXATTR && HAVE_GETXATTR && HAVE_LGETXATTR
1099 static void
1100 setup_xattr(struct bsdtar *bsdtar, struct archive_entry *entry,
1101 const char *accpath, const char *name)
1103 size_t size;
1104 void *value = NULL;
1105 char symlink_mode = bsdtar->symlink_mode;
1107 if (symlink_mode == 'H')
1108 size = getxattr(accpath, name, NULL, 0);
1109 else
1110 size = lgetxattr(accpath, name, NULL, 0);
1112 if (size == -1) {
1113 bsdtar_warnc(bsdtar, errno, "Couldn't get extended attribute");
1114 return;
1117 if (size > 0 && (value = malloc(size)) == NULL) {
1118 bsdtar_errc(bsdtar, 1, errno, "Out of memory");
1119 return;
1122 if (symlink_mode == 'H')
1123 size = getxattr(accpath, name, value, size);
1124 else
1125 size = lgetxattr(accpath, name, value, size);
1127 if (size == -1) {
1128 bsdtar_warnc(bsdtar, errno, "Couldn't get extended attribute");
1129 return;
1132 archive_entry_xattr_add_entry(entry, name, value, size);
1134 free(value);
1138 * Linux extended attribute support
1140 static void
1141 setup_xattrs(struct bsdtar *bsdtar, struct archive_entry *entry,
1142 const char *accpath)
1144 char *list, *p;
1145 size_t list_size;
1146 char symlink_mode = bsdtar->symlink_mode;
1148 if (symlink_mode == 'H')
1149 list_size = listxattr(accpath, NULL, 0);
1150 else
1151 list_size = llistxattr(accpath, NULL, 0);
1153 if (list_size == -1) {
1154 bsdtar_warnc(bsdtar, errno,
1155 "Couldn't list extended attributes");
1156 return;
1157 } else if (list_size == 0)
1158 return;
1160 if ((list = malloc(list_size)) == NULL) {
1161 bsdtar_errc(bsdtar, 1, errno, "Out of memory");
1162 return;
1165 if (symlink_mode == 'H')
1166 list_size = listxattr(accpath, list, list_size);
1167 else
1168 list_size = llistxattr(accpath, list, list_size);
1170 if (list_size == -1) {
1171 bsdtar_warnc(bsdtar, errno,
1172 "Couldn't list extended attributes");
1173 free(list);
1174 return;
1177 for (p = list; (p - list) < list_size; p += strlen(p) + 1) {
1178 if (strncmp(p, "system.", 7) == 0 ||
1179 strncmp(p, "xfsroot.", 8) == 0)
1180 continue;
1182 setup_xattr(bsdtar, entry, accpath, p);
1185 free(list);
1188 #else
1191 * Generic (stub) extended attribute support.
1193 static void
1194 setup_xattrs(struct bsdtar *bsdtar, struct archive_entry *entry,
1195 const char *accpath)
1197 (void)bsdtar; /* UNUSED */
1198 (void)entry; /* UNUSED */
1199 (void)accpath; /* UNUSED */
1202 #endif
1204 static void
1205 free_cache(struct name_cache *cache)
1207 size_t i;
1209 if (cache != NULL) {
1210 for (i = 0; i < cache->size; i++) {
1211 if (cache->cache[i].name != NULL &&
1212 cache->cache[i].name != NO_NAME)
1213 free((void *)(uintptr_t)cache->cache[i].name);
1215 free(cache);
1220 * Lookup uid/gid from uname/gname, return NULL if no match.
1222 static const char *
1223 lookup_name(struct bsdtar *bsdtar, struct name_cache **name_cache_variable,
1224 int (*lookup_fn)(struct bsdtar *, const char **, id_t), id_t id)
1226 struct name_cache *cache;
1227 const char *name;
1228 int slot;
1231 if (*name_cache_variable == NULL) {
1232 *name_cache_variable = malloc(sizeof(struct name_cache));
1233 if (*name_cache_variable == NULL)
1234 bsdtar_errc(bsdtar, 1, ENOMEM, "No more memory");
1235 memset(*name_cache_variable, 0, sizeof(struct name_cache));
1236 (*name_cache_variable)->size = name_cache_size;
1239 cache = *name_cache_variable;
1240 cache->probes++;
1242 slot = id % cache->size;
1243 if (cache->cache[slot].name != NULL) {
1244 if (cache->cache[slot].id == id) {
1245 cache->hits++;
1246 if (cache->cache[slot].name == NO_NAME)
1247 return (NULL);
1248 return (cache->cache[slot].name);
1250 if (cache->cache[slot].name != NO_NAME)
1251 free((void *)(uintptr_t)cache->cache[slot].name);
1252 cache->cache[slot].name = NULL;
1255 if (lookup_fn(bsdtar, &name, id) == 0) {
1256 if (name == NULL || name[0] == '\0') {
1257 /* Cache the negative response. */
1258 cache->cache[slot].name = NO_NAME;
1259 cache->cache[slot].id = id;
1260 } else {
1261 cache->cache[slot].name = strdup(name);
1262 if (cache->cache[slot].name != NULL) {
1263 cache->cache[slot].id = id;
1264 return (cache->cache[slot].name);
1267 * Conveniently, NULL marks an empty slot, so
1268 * if the strdup() fails, we've just failed to
1269 * cache it. No recovery necessary.
1273 return (NULL);
1276 static const char *
1277 lookup_uname(struct bsdtar *bsdtar, uid_t uid)
1279 return (lookup_name(bsdtar, &bsdtar->uname_cache,
1280 &lookup_uname_helper, (id_t)uid));
1283 static int
1284 lookup_uname_helper(struct bsdtar *bsdtar, const char **name, id_t id)
1286 struct passwd *pwent;
1288 (void)bsdtar; /* UNUSED */
1290 errno = 0;
1291 pwent = getpwuid((uid_t)id);
1292 if (pwent == NULL) {
1293 *name = NULL;
1294 if (errno != 0)
1295 bsdtar_warnc(bsdtar, errno, "getpwuid(%d) failed", id);
1296 return (errno);
1299 *name = pwent->pw_name;
1300 return (0);
1303 static const char *
1304 lookup_gname(struct bsdtar *bsdtar, gid_t gid)
1306 return (lookup_name(bsdtar, &bsdtar->gname_cache,
1307 &lookup_gname_helper, (id_t)gid));
1310 static int
1311 lookup_gname_helper(struct bsdtar *bsdtar, const char **name, id_t id)
1313 struct group *grent;
1315 (void)bsdtar; /* UNUSED */
1317 errno = 0;
1318 grent = getgrgid((gid_t)id);
1319 if (grent == NULL) {
1320 *name = NULL;
1321 if (errno != 0)
1322 bsdtar_warnc(bsdtar, errno, "getgrgid(%d) failed", id);
1323 return (errno);
1326 *name = grent->gr_name;
1327 return (0);
1331 * Test if the specified file is new enough to include in the archive.
1334 new_enough(struct bsdtar *bsdtar, const char *path, const struct stat *st)
1336 struct archive_dir_entry *p;
1339 * If this file/dir is excluded by a time comparison, skip it.
1341 if (bsdtar->newer_ctime_sec > 0) {
1342 if (st->st_ctime < bsdtar->newer_ctime_sec)
1343 return (0); /* Too old, skip it. */
1344 if (st->st_ctime == bsdtar->newer_ctime_sec
1345 && ARCHIVE_STAT_CTIME_NANOS(st)
1346 <= bsdtar->newer_ctime_nsec)
1347 return (0); /* Too old, skip it. */
1349 if (bsdtar->newer_mtime_sec > 0) {
1350 if (st->st_mtime < bsdtar->newer_mtime_sec)
1351 return (0); /* Too old, skip it. */
1352 if (st->st_mtime == bsdtar->newer_mtime_sec
1353 && ARCHIVE_STAT_MTIME_NANOS(st)
1354 <= bsdtar->newer_mtime_nsec)
1355 return (0); /* Too old, skip it. */
1359 * In -u mode, we only write an entry if it's newer than
1360 * what was already in the archive.
1362 if (bsdtar->archive_dir != NULL &&
1363 bsdtar->archive_dir->head != NULL) {
1364 for (p = bsdtar->archive_dir->head; p != NULL; p = p->next) {
1365 if (pathcmp(path, p->name)==0)
1366 return (p->mtime_sec < st->st_mtime ||
1367 (p->mtime_sec == st->st_mtime &&
1368 p->mtime_nsec
1369 < ARCHIVE_STAT_MTIME_NANOS(st)));
1373 /* If the file wasn't rejected, include it. */
1374 return (1);
1378 * Add an entry to the dir list for 'u' mode.
1380 * XXX TODO: Make this fast.
1382 static void
1383 add_dir_list(struct bsdtar *bsdtar, const char *path,
1384 time_t mtime_sec, int mtime_nsec)
1386 struct archive_dir_entry *p;
1389 * Search entire list to see if this file has appeared before.
1390 * If it has, override the timestamp data.
1392 p = bsdtar->archive_dir->head;
1393 while (p != NULL) {
1394 if (strcmp(path, p->name)==0) {
1395 p->mtime_sec = mtime_sec;
1396 p->mtime_nsec = mtime_nsec;
1397 return;
1399 p = p->next;
1402 p = malloc(sizeof(*p));
1403 if (p == NULL)
1404 bsdtar_errc(bsdtar, 1, ENOMEM, "Can't read archive directory");
1406 p->name = strdup(path);
1407 if (p->name == NULL)
1408 bsdtar_errc(bsdtar, 1, ENOMEM, "Can't read archive directory");
1409 p->mtime_sec = mtime_sec;
1410 p->mtime_nsec = mtime_nsec;
1411 p->next = NULL;
1412 if (bsdtar->archive_dir->tail == NULL) {
1413 bsdtar->archive_dir->head = bsdtar->archive_dir->tail = p;
1414 } else {
1415 bsdtar->archive_dir->tail->next = p;
1416 bsdtar->archive_dir->tail = p;
1420 void
1421 test_for_append(struct bsdtar *bsdtar)
1423 struct stat s;
1425 if (*bsdtar->argv == NULL && bsdtar->names_from_file == NULL)
1426 bsdtar_errc(bsdtar, 1, 0, "no files or directories specified");
1427 if (bsdtar->filename == NULL)
1428 bsdtar_errc(bsdtar, 1, 0, "Cannot append to stdout.");
1430 if (bsdtar->create_compression != 0)
1431 bsdtar_errc(bsdtar, 1, 0,
1432 "Cannot append to %s with compression", bsdtar->filename);
1434 if (stat(bsdtar->filename, &s) != 0)
1435 return;
1437 if (!S_ISREG(s.st_mode) && !S_ISBLK(s.st_mode))
1438 bsdtar_errc(bsdtar, 1, 0,
1439 "Cannot append to %s: not a regular file.",
1440 bsdtar->filename);