2 * Copyright (c) 2003-2004 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "bsdtar_platform.h"
28 __FBSDID("$FreeBSD$");
31 #include <sys/types.h>
35 #ifdef HAVE_ATTR_XATTR_H
36 #include <attr/xattr.h>
49 #include <ext2fs/ext2_fs.h>
50 #include <sys/ioctl.h>
56 /* Fixed size of uname/gname caches. */
57 #define name_cache_size 101
59 static const char * const NO_NAME = "(noname)";
61 /* Initial size of link cache. */
62 #define links_cache_initial_size 1024
64 struct archive_dir_entry {
65 struct archive_dir_entry *next;
72 struct archive_dir_entry *head, *tail;
76 unsigned long number_entries;
77 size_t number_buckets;
78 struct links_entry **buckets;
82 struct links_entry *next;
83 struct links_entry *previous;
97 } cache[name_cache_size];
100 static void add_dir_list(struct bsdtar *bsdtar, const char *path,
101 time_t mtime_sec, int mtime_nsec);
102 static int append_archive(struct bsdtar *, struct archive *,
104 static void archive_names_from_file(struct bsdtar *bsdtar,
106 static int archive_names_from_file_helper(struct bsdtar *bsdtar,
108 static void create_cleanup(struct bsdtar *);
109 static void free_buckets(struct bsdtar *, struct links_cache *);
110 static void free_cache(struct name_cache *cache);
111 static const char * lookup_gname(struct bsdtar *bsdtar, gid_t gid);
112 static int lookup_gname_helper(struct bsdtar *bsdtar,
113 const char **name, id_t gid);
114 static void lookup_hardlink(struct bsdtar *,
115 struct archive_entry *entry, const struct stat *);
116 static const char * lookup_uname(struct bsdtar *bsdtar, uid_t uid);
117 static int lookup_uname_helper(struct bsdtar *bsdtar,
118 const char **name, id_t uid);
119 static int new_enough(struct bsdtar *, const char *path,
120 const struct stat *);
121 static void setup_acls(struct bsdtar *, struct archive_entry *,
123 static void setup_xattrs(struct bsdtar *, struct archive_entry *,
125 static void test_for_append(struct bsdtar *);
126 static void write_archive(struct archive *, struct bsdtar *);
127 static void write_entry(struct bsdtar *, struct archive *,
128 const struct stat *, const char *pathname,
129 unsigned pathlen, const char *accpath);
130 static int write_file_data(struct bsdtar *, struct archive *,
132 static void write_hierarchy(struct bsdtar *, struct archive *,
136 tar_mode_c(struct bsdtar *bsdtar)
141 if (*bsdtar->argv == NULL && bsdtar->names_from_file == NULL)
142 bsdtar_errc(bsdtar, 1, 0, "no files or directories specified");
144 a = archive_write_new();
146 /* Support any format that the library supports. */
147 if (bsdtar->create_format == NULL) {
148 r = archive_write_set_format_pax_restricted(a);
149 bsdtar->create_format = "pax restricted";
151 r = archive_write_set_format_by_name(a, bsdtar->create_format);
153 if (r != ARCHIVE_OK) {
154 fprintf(stderr, "Can't use format %s: %s\n",
155 bsdtar->create_format,
156 archive_error_string(a));
161 * If user explicitly set the block size, then assume they
162 * want the last block padded as well. Otherwise, use the
163 * default block size and accept archive_write_open_file()'s
164 * default padding decisions.
166 if (bsdtar->bytes_per_block != 0) {
167 archive_write_set_bytes_per_block(a, bsdtar->bytes_per_block);
168 archive_write_set_bytes_in_last_block(a,
169 bsdtar->bytes_per_block);
171 archive_write_set_bytes_per_block(a, DEFAULT_BYTES_PER_BLOCK);
173 switch (bsdtar->create_compression) {
178 archive_write_set_compression_bzip2(a);
183 archive_write_set_compression_gzip(a);
187 bsdtar_errc(bsdtar, 1, 0,
188 "Unrecognized compression option -%c",
189 bsdtar->create_compression);
192 r = archive_write_open_file(a, bsdtar->filename);
194 bsdtar_errc(bsdtar, 1, 0, archive_error_string(a));
196 write_archive(a, bsdtar);
198 if (bsdtar->option_totals) {
199 fprintf(stderr, "Total bytes written: " BSDTAR_FILESIZE_PRINTF "\n",
200 (BSDTAR_FILESIZE_TYPE)archive_position_compressed(a));
203 archive_write_finish(a);
207 * Same as 'c', except we only support tar formats in uncompressed
211 tar_mode_r(struct bsdtar *bsdtar)
216 struct archive_entry *entry;
218 /* Sanity-test some arguments and the file. */
219 test_for_append(bsdtar);
221 format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
223 bsdtar->fd = open(bsdtar->filename, O_RDWR);
225 bsdtar_errc(bsdtar, 1, errno,
226 "Cannot open %s", bsdtar->filename);
228 a = archive_read_new();
229 archive_read_support_compression_all(a);
230 archive_read_support_format_tar(a);
231 archive_read_support_format_gnutar(a);
232 archive_read_open_fd(a, bsdtar->fd, 10240);
233 while (0 == archive_read_next_header(a, &entry)) {
234 if (archive_compression(a) != ARCHIVE_COMPRESSION_NONE) {
235 archive_read_finish(a);
237 bsdtar_errc(bsdtar, 1, 0,
238 "Cannot append to compressed archive.");
240 /* Keep going until we hit end-of-archive */
241 format = archive_format(a);
244 end_offset = archive_read_header_position(a);
245 archive_read_finish(a);
247 /* Re-open archive for writing */
248 a = archive_write_new();
249 archive_write_set_compression_none(a);
251 * Set format to same one auto-detected above, except use
252 * ustar for appending to GNU tar, since the library doesn't
253 * write GNU tar format.
255 if (format == ARCHIVE_FORMAT_TAR_GNUTAR)
256 format = ARCHIVE_FORMAT_TAR_USTAR;
257 archive_write_set_format(a, format);
258 lseek(bsdtar->fd, end_offset, SEEK_SET); /* XXX check return val XXX */
259 archive_write_open_fd(a, bsdtar->fd); /* XXX check return val XXX */
261 write_archive(a, bsdtar); /* XXX check return val XXX */
263 if (bsdtar->option_totals) {
264 fprintf(stderr, "Total bytes written: " BSDTAR_FILESIZE_PRINTF "\n",
265 (BSDTAR_FILESIZE_TYPE)archive_position_compressed(a));
268 archive_write_finish(a);
274 tar_mode_u(struct bsdtar *bsdtar)
278 struct archive_entry *entry;
279 const char *filename;
281 struct archive_dir_entry *p;
282 struct archive_dir archive_dir;
284 bsdtar->archive_dir = &archive_dir;
285 memset(&archive_dir, 0, sizeof(archive_dir));
288 format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
290 /* Sanity-test some arguments and the file. */
291 test_for_append(bsdtar);
293 bsdtar->fd = open(bsdtar->filename, O_RDWR);
295 bsdtar_errc(bsdtar, 1, errno,
296 "Cannot open %s", bsdtar->filename);
298 a = archive_read_new();
299 archive_read_support_compression_all(a);
300 archive_read_support_format_tar(a);
301 archive_read_support_format_gnutar(a);
302 if (archive_read_open_fd(a, bsdtar->fd,
303 bsdtar->bytes_per_block != 0 ? bsdtar->bytes_per_block :
304 DEFAULT_BYTES_PER_BLOCK) != ARCHIVE_OK) {
305 bsdtar_errc(bsdtar, 1, 0,
306 "Can't open %s: %s", bsdtar->filename,
307 archive_error_string(a));
310 /* Build a list of all entries and their recorded mod times. */
311 while (0 == archive_read_next_header(a, &entry)) {
312 if (archive_compression(a) != ARCHIVE_COMPRESSION_NONE) {
313 archive_read_finish(a);
315 bsdtar_errc(bsdtar, 1, 0,
316 "Cannot append to compressed archive.");
318 add_dir_list(bsdtar, archive_entry_pathname(entry),
319 archive_entry_mtime(entry),
320 archive_entry_mtime_nsec(entry));
321 /* Record the last format determination we see */
322 format = archive_format(a);
323 /* Keep going until we hit end-of-archive */
326 end_offset = archive_read_header_position(a);
327 archive_read_finish(a);
329 /* Re-open archive for writing. */
330 a = archive_write_new();
331 archive_write_set_compression_none(a);
333 * Set format to same one auto-detected above, except that
334 * we don't write GNU tar format, so use ustar instead.
336 if (format == ARCHIVE_FORMAT_TAR_GNUTAR)
337 format = ARCHIVE_FORMAT_TAR_USTAR;
338 archive_write_set_format(a, format);
339 if (bsdtar->bytes_per_block != 0) {
340 archive_write_set_bytes_per_block(a, bsdtar->bytes_per_block);
341 archive_write_set_bytes_in_last_block(a,
342 bsdtar->bytes_per_block);
344 archive_write_set_bytes_per_block(a, DEFAULT_BYTES_PER_BLOCK);
345 lseek(bsdtar->fd, end_offset, SEEK_SET);
346 ftruncate(bsdtar->fd, end_offset);
347 archive_write_open_fd(a, bsdtar->fd);
349 write_archive(a, bsdtar);
351 if (bsdtar->option_totals) {
352 fprintf(stderr, "Total bytes written: " BSDTAR_FILESIZE_PRINTF "\n",
353 (BSDTAR_FILESIZE_TYPE)archive_position_compressed(a));
356 archive_write_finish(a);
360 while (bsdtar->archive_dir->head != NULL) {
361 p = bsdtar->archive_dir->head->next;
362 free(bsdtar->archive_dir->head->name);
363 free(bsdtar->archive_dir->head);
364 bsdtar->archive_dir->head = p;
366 bsdtar->archive_dir->tail = NULL;
371 * Write user-specified files/dirs to opened archive.
374 write_archive(struct archive *a, struct bsdtar *bsdtar)
378 if (bsdtar->names_from_file != NULL)
379 archive_names_from_file(bsdtar, a);
381 while (*bsdtar->argv) {
383 if (arg[0] == '-' && arg[1] == 'C') {
389 bsdtar_warnc(bsdtar, 1, 0,
390 "Missing argument for -C");
391 bsdtar->return_value = 1;
395 set_chdir(bsdtar, arg);
397 if (*arg != '/' || (arg[0] == '@' && arg[1] != '/'))
398 do_chdir(bsdtar); /* Handle a deferred -C */
400 if (append_archive(bsdtar, a, arg + 1) != 0)
403 write_hierarchy(bsdtar, a, arg);
408 create_cleanup(bsdtar);
409 archive_write_close(a);
413 * Archive names specified in file.
415 * Unless --null was specified, a line containing exactly "-C" will
416 * cause the next line to be a directory to pass to chdir(). If
417 * --null is specified, then a line "-C" is just another filename.
420 archive_names_from_file(struct bsdtar *bsdtar, struct archive *a)
424 bsdtar->next_line_is_dir = 0;
425 process_lines(bsdtar, bsdtar->names_from_file,
426 archive_names_from_file_helper);
427 if (bsdtar->next_line_is_dir)
428 bsdtar_errc(bsdtar, 1, errno,
429 "Unexpected end of filename list; "
430 "directory expected after -C");
434 archive_names_from_file_helper(struct bsdtar *bsdtar, const char *line)
436 if (bsdtar->next_line_is_dir) {
437 set_chdir(bsdtar, line);
438 bsdtar->next_line_is_dir = 0;
439 } else if (!bsdtar->option_null && strcmp(line, "-C") == 0)
440 bsdtar->next_line_is_dir = 1;
443 do_chdir(bsdtar); /* Handle a deferred -C */
444 write_hierarchy(bsdtar, bsdtar->archive, line);
450 * Copy from specified archive to current archive. Returns non-zero
451 * for write errors (which force us to terminate the entire archiving
452 * operation). If there are errors reading the input archive, we set
453 * bsdtar->return_value but return zero, so the overall archiving
454 * operation will complete and return non-zero.
457 append_archive(struct bsdtar *bsdtar, struct archive *a, const char *filename)
460 struct archive_entry *in_entry;
461 int bytes_read, bytes_written;
464 if (strcmp(filename, "-") == 0)
465 filename = NULL; /* Library uses NULL for stdio. */
467 ina = archive_read_new();
468 archive_read_support_format_all(ina);
469 archive_read_support_compression_all(ina);
470 if (archive_read_open_file(ina, filename, 10240)) {
471 bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(ina));
472 bsdtar->return_value = 1;
475 while (0 == archive_read_next_header(ina, &in_entry)) {
476 if (!new_enough(bsdtar, archive_entry_pathname(in_entry),
477 archive_entry_stat(in_entry)))
479 if (excluded(bsdtar, archive_entry_pathname(in_entry)))
481 if (bsdtar->option_interactive &&
482 !yes("copy '%s'", archive_entry_pathname(in_entry)))
485 safe_fprintf(stderr, "a %s",
486 archive_entry_pathname(in_entry));
487 /* XXX handle/report errors XXX */
488 if (archive_write_header(a, in_entry)) {
489 bsdtar_warnc(bsdtar, 0, "%s",
490 archive_error_string(ina));
491 bsdtar->return_value = 1;
494 bytes_read = archive_read_data(ina, buff, sizeof(buff));
495 while (bytes_read > 0) {
497 archive_write_data(a, buff, bytes_read);
498 if (bytes_written < bytes_read) {
499 bsdtar_warnc(bsdtar, archive_errno(a), "%s",
500 archive_error_string(a));
504 archive_read_data(ina, buff, sizeof(buff));
507 fprintf(stderr, "\n");
510 if (archive_errno(ina)) {
511 bsdtar_warnc(bsdtar, 0, "Error reading archive %s: %s",
512 filename, archive_error_string(ina));
513 bsdtar->return_value = 1;
516 /* Note: If we got here, we saw no write errors, so return success. */
521 * Add the file or dir hierarchy named by 'path' to the archive
524 write_hierarchy(struct bsdtar *bsdtar, struct archive *a, const char *path)
527 char symlink_mode = bsdtar->symlink_mode;
529 int dev_recorded = 0;
533 unsigned long fflags;
536 tree = tree_open(path);
539 bsdtar_warnc(bsdtar, errno, "%s: Cannot open", path);
540 bsdtar->return_value = 1;
544 while ((tree_ret = tree_next(tree))) {
545 const char *name = tree_current_path(tree);
546 const struct stat *st = NULL, *lst = NULL;
549 if (tree_ret == TREE_ERROR_DIR)
550 bsdtar_warnc(bsdtar, errno, "%s: Couldn't visit directory", name);
551 if (tree_ret != TREE_REGULAR)
553 lst = tree_current_lstat(tree);
555 /* Couldn't lstat(); must not exist. */
556 bsdtar_warnc(bsdtar, errno, "%s: Cannot stat", path);
557 bsdtar->return_value = 1;
560 if (S_ISLNK(lst->st_mode))
561 st = tree_current_stat(tree);
562 /* Default: descend into any dir or symlink to dir. */
563 /* We'll adjust this later on. */
565 if ((st != NULL) && S_ISDIR(st->st_mode))
567 if ((lst != NULL) && S_ISDIR(lst->st_mode))
571 * If user has asked us not to cross mount points,
572 * then don't descend into into a dir on a different
576 first_dev = lst->st_dev;
579 if (bsdtar->option_dont_traverse_mounts) {
580 if (lst != NULL && lst->st_dev != first_dev)
585 * If this file/dir is flagged "nodump" and we're
586 * honoring such flags, skip this file/dir.
589 if (bsdtar->option_honor_nodump &&
590 (lst->st_flags & UF_NODUMP))
596 * Linux has a nodump flag too but to read it
597 * we have to open() the file/dir and do an ioctl on it...
599 if (bsdtar->option_honor_nodump &&
600 ((fd = open(name, O_RDONLY|O_NONBLOCK)) >= 0) &&
601 ((r = ioctl(fd, EXT2_IOC_GETFLAGS, &fflags)),
602 close(fd), r) >= 0 &&
603 (fflags & EXT2_NODUMP_FL))
608 * If this file/dir is excluded by a filename
611 if (excluded(bsdtar, name))
615 * If the user vetoes this file/directory, skip it.
617 if (bsdtar->option_interactive &&
618 !yes("add '%s'", name))
622 * If this is a dir, decide whether or not to recurse.
624 if (bsdtar->option_no_subdirs)
628 * Distinguish 'L'/'P'/'H' symlink following.
630 switch(symlink_mode) {
632 /* 'H': First item (from command line) like 'L'. */
633 lst = tree_current_stat(tree);
634 /* 'H': After the first item, rest like 'P'. */
638 /* 'L': Do descend through a symlink to dir. */
639 /* 'L': Archive symlink to file as file. */
640 lst = tree_current_stat(tree);
643 /* 'P': Don't descend through a symlink to dir. */
644 if (!S_ISDIR(lst->st_mode))
646 /* 'P': Archive symlink to file as symlink. */
647 /* lst = tree_current_lstat(tree); */
655 * Write the entry. Note that write_entry() handles
656 * pathname editing and newness testing.
658 write_entry(bsdtar, a, lst, name,
659 tree_current_pathlen(tree),
660 tree_current_access_path(tree));
666 * Add a single filesystem object to the archive.
669 write_entry(struct bsdtar *bsdtar, struct archive *a, const struct stat *st,
670 const char *pathname, unsigned pathlen, const char *accpath)
672 struct archive_entry *entry;
677 unsigned long stflags;
679 static char linkbuffer[PATH_MAX+1];
681 (void)pathlen; /* UNUSED */
684 entry = archive_entry_new();
686 archive_entry_set_pathname(entry, pathname);
689 * Rewrite the pathname to be archived. If rewrite
690 * fails, skip the entry.
692 if (edit_pathname(bsdtar, entry))
696 * In -u mode, check that the file is newer than what's
697 * already in the archive; in all modes, obey --newerXXX flags.
699 if (!new_enough(bsdtar, archive_entry_pathname(entry), st))
702 if (!S_ISDIR(st->st_mode) && (st->st_nlink > 1))
703 lookup_hardlink(bsdtar, entry, st);
705 /* Display entry as we process it. This format is required by SUSv2. */
707 safe_fprintf(stderr, "a %s", archive_entry_pathname(entry));
709 /* Read symbolic link information. */
710 if ((st->st_mode & S_IFMT) == S_IFLNK) {
713 lnklen = readlink(accpath, linkbuffer, PATH_MAX);
715 if (!bsdtar->verbose)
716 bsdtar_warnc(bsdtar, errno,
717 "%s: Couldn't read symbolic link",
721 ": Couldn't read symbolic link: %s",
725 linkbuffer[lnklen] = 0;
726 archive_entry_set_symlink(entry, linkbuffer);
729 /* Look up username and group name. */
730 archive_entry_set_uname(entry, lookup_uname(bsdtar, st->st_uid));
731 archive_entry_set_gname(entry, lookup_gname(bsdtar, st->st_gid));
734 if (st->st_flags != 0)
735 archive_entry_set_fflags(entry, st->st_flags, 0);
739 if ((S_ISREG(st->st_mode) || S_ISDIR(st->st_mode)) &&
740 ((fd = open(accpath, O_RDONLY|O_NONBLOCK)) >= 0) &&
741 ((r = ioctl(fd, EXT2_IOC_GETFLAGS, &stflags)), close(fd), (fd = -1), r) >= 0 &&
743 archive_entry_set_fflags(entry, stflags, 0);
747 archive_entry_copy_stat(entry, st);
748 setup_acls(bsdtar, entry, accpath);
749 setup_xattrs(bsdtar, entry, accpath);
752 * If it's a regular file (and non-zero in size) make sure we
753 * can open it before we start to write. In particular, note
754 * that we can always archive a zero-length file, even if we
757 if (S_ISREG(st->st_mode) && st->st_size > 0) {
758 fd = open(accpath, O_RDONLY);
760 if (!bsdtar->verbose)
761 bsdtar_warnc(bsdtar, errno, "%s: could not open file", pathname);
763 fprintf(stderr, ": %s", strerror(errno));
768 /* Non-regular files get archived with zero size. */
769 if (!S_ISREG(st->st_mode))
770 archive_entry_set_size(entry, 0);
772 e = archive_write_header(a, entry);
773 if (e != ARCHIVE_OK) {
774 if (!bsdtar->verbose)
775 bsdtar_warnc(bsdtar, 0, "%s: %s", pathname,
776 archive_error_string(a));
778 fprintf(stderr, ": %s", archive_error_string(a));
781 if (e == ARCHIVE_FATAL)
785 * If we opened a file earlier, write it out now. Note that
786 * the format handler might have reset the size field to zero
787 * to inform us that the archive body won't get stored. In
788 * that case, just skip the write.
790 if (fd >= 0 && archive_entry_size(entry) > 0)
791 write_file_data(bsdtar, a, fd);
795 fprintf(stderr, "\n");
802 archive_entry_free(entry);
806 /* Helper function to copy file to archive, with stack-allocated buffer. */
808 write_file_data(struct bsdtar *bsdtar, struct archive *a, int fd)
812 ssize_t bytes_written;
814 /* XXX TODO: Allocate buffer on heap and store pointer to
815 * it in bsdtar structure; arrange cleanup as well. XXX */
818 bytes_read = read(fd, buff, sizeof(buff));
819 while (bytes_read > 0) {
820 bytes_written = archive_write_data(a, buff, bytes_read);
821 if (bytes_written <= 0)
822 return (-1); /* Write failed; this is bad */
823 bytes_read = read(fd, buff, sizeof(buff));
830 create_cleanup(struct bsdtar *bsdtar)
832 /* Free inode->pathname map used for hardlink detection. */
833 if (bsdtar->links_cache != NULL) {
834 free_buckets(bsdtar, bsdtar->links_cache);
835 free(bsdtar->links_cache);
836 bsdtar->links_cache = NULL;
839 free_cache(bsdtar->uname_cache);
840 bsdtar->uname_cache = NULL;
841 free_cache(bsdtar->gname_cache);
842 bsdtar->gname_cache = NULL;
847 free_buckets(struct bsdtar *bsdtar, struct links_cache *links_cache)
851 if (links_cache->buckets == NULL)
854 for (i = 0; i < links_cache->number_buckets; i++) {
855 while (links_cache->buckets[i] != NULL) {
856 struct links_entry *lp = links_cache->buckets[i]->next;
857 if (bsdtar->option_warn_links)
858 bsdtar_warnc(bsdtar, 0, "Missing links to %s",
859 links_cache->buckets[i]->name);
860 if (links_cache->buckets[i]->name != NULL)
861 free(links_cache->buckets[i]->name);
862 free(links_cache->buckets[i]);
863 links_cache->buckets[i] = lp;
866 free(links_cache->buckets);
867 links_cache->buckets = NULL;
871 lookup_hardlink(struct bsdtar *bsdtar, struct archive_entry *entry,
872 const struct stat *st)
874 struct links_cache *links_cache;
875 struct links_entry *le, **new_buckets;
879 /* If necessary, initialize the links cache. */
880 links_cache = bsdtar->links_cache;
881 if (links_cache == NULL) {
882 bsdtar->links_cache = malloc(sizeof(struct links_cache));
883 if (bsdtar->links_cache == NULL)
884 bsdtar_errc(bsdtar, 1, ENOMEM,
885 "No memory for hardlink detection.");
886 links_cache = bsdtar->links_cache;
887 memset(links_cache, 0, sizeof(struct links_cache));
888 links_cache->number_buckets = links_cache_initial_size;
889 links_cache->buckets = malloc(links_cache->number_buckets *
890 sizeof(links_cache->buckets[0]));
891 if (links_cache->buckets == NULL) {
892 bsdtar_errc(bsdtar, 1, ENOMEM,
893 "No memory for hardlink detection.");
895 for (i = 0; i < links_cache->number_buckets; i++)
896 links_cache->buckets[i] = NULL;
899 /* If the links cache overflowed and got flushed, don't bother. */
900 if (links_cache->buckets == NULL)
903 /* If the links cache is getting too full, enlarge the hash table. */
904 if (links_cache->number_entries > links_cache->number_buckets * 2)
908 new_size = links_cache->number_buckets * 2;
909 new_buckets = malloc(new_size * sizeof(struct links_entry *));
913 if (new_buckets != NULL) {
914 memset(new_buckets, 0,
915 new_size * sizeof(struct links_entry *));
916 for (i = 0; i < links_cache->number_buckets; i++) {
917 while (links_cache->buckets[i] != NULL) {
918 /* Remove entry from old bucket. */
919 le = links_cache->buckets[i];
920 links_cache->buckets[i] = le->next;
922 /* Add entry to new bucket. */
923 hash = (le->dev ^ le->ino) % new_size;
925 if (new_buckets[hash] != NULL)
926 new_buckets[hash]->previous =
928 le->next = new_buckets[hash];
930 new_buckets[hash] = le;
933 free(links_cache->buckets);
934 links_cache->buckets = new_buckets;
935 links_cache->number_buckets = new_size;
937 free_buckets(bsdtar, links_cache);
938 bsdtar_warnc(bsdtar, ENOMEM,
939 "No more memory for recording hard links");
940 bsdtar_warnc(bsdtar, 0,
941 "Remaining links will be dumped as full files");
945 /* Try to locate this entry in the links cache. */
946 hash = ( st->st_dev ^ st->st_ino ) % links_cache->number_buckets;
947 for (le = links_cache->buckets[hash]; le != NULL; le = le->next) {
948 if (le->dev == st->st_dev && le->ino == st->st_ino) {
949 archive_entry_copy_hardlink(entry, le->name);
952 * Decrement link count each time and release
953 * the entry if it hits zero. This saves
954 * memory and is necessary for proper -l
957 if (--le->links <= 0) {
958 if (le->previous != NULL)
959 le->previous->next = le->next;
960 if (le->next != NULL)
961 le->next->previous = le->previous;
962 if (le->name != NULL)
964 if (links_cache->buckets[hash] == le)
965 links_cache->buckets[hash] = le->next;
966 links_cache->number_entries--;
974 /* Add this entry to the links cache. */
975 le = malloc(sizeof(struct links_entry));
977 le->name = strdup(archive_entry_pathname(entry));
978 if ((le == NULL) || (le->name == NULL)) {
979 free_buckets(bsdtar, links_cache);
980 bsdtar_warnc(bsdtar, ENOMEM,
981 "No more memory for recording hard links");
982 bsdtar_warnc(bsdtar, 0,
983 "Remaining hard links will be dumped as full files");
988 if (links_cache->buckets[hash] != NULL)
989 links_cache->buckets[hash]->previous = le;
990 links_cache->number_entries++;
991 le->next = links_cache->buckets[hash];
993 links_cache->buckets[hash] = le;
994 le->dev = st->st_dev;
995 le->ino = st->st_ino;
996 le->links = st->st_nlink - 1;
999 #ifdef HAVE_POSIX_ACL
1000 static void setup_acl(struct bsdtar *bsdtar,
1001 struct archive_entry *entry, const char *accpath,
1002 int acl_type, int archive_entry_acl_type);
1005 setup_acls(struct bsdtar *bsdtar, struct archive_entry *entry,
1006 const char *accpath)
1008 archive_entry_acl_clear(entry);
1010 setup_acl(bsdtar, entry, accpath,
1011 ACL_TYPE_ACCESS, ARCHIVE_ENTRY_ACL_TYPE_ACCESS);
1012 /* Only directories can have default ACLs. */
1013 if (S_ISDIR(archive_entry_mode(entry)))
1014 setup_acl(bsdtar, entry, accpath,
1015 ACL_TYPE_DEFAULT, ARCHIVE_ENTRY_ACL_TYPE_DEFAULT);
1019 setup_acl(struct bsdtar *bsdtar, struct archive_entry *entry,
1020 const char *accpath, int acl_type, int archive_entry_acl_type)
1024 acl_entry_t acl_entry;
1025 acl_permset_t acl_permset;
1026 int s, ae_id, ae_tag, ae_perm;
1027 const char *ae_name;
1029 /* Retrieve access ACL from file. */
1030 acl = acl_get_file(accpath, acl_type);
1032 s = acl_get_entry(acl, ACL_FIRST_ENTRY, &acl_entry);
1037 acl_get_tag_type(acl_entry, &acl_tag);
1038 if (acl_tag == ACL_USER) {
1039 ae_id = (int)*(uid_t *)acl_get_qualifier(acl_entry);
1040 ae_name = lookup_uname(bsdtar, ae_id);
1041 ae_tag = ARCHIVE_ENTRY_ACL_USER;
1042 } else if (acl_tag == ACL_GROUP) {
1043 ae_id = (int)*(gid_t *)acl_get_qualifier(acl_entry);
1044 ae_name = lookup_gname(bsdtar, ae_id);
1045 ae_tag = ARCHIVE_ENTRY_ACL_GROUP;
1046 } else if (acl_tag == ACL_MASK) {
1047 ae_tag = ARCHIVE_ENTRY_ACL_MASK;
1048 } else if (acl_tag == ACL_USER_OBJ) {
1049 ae_tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
1050 } else if (acl_tag == ACL_GROUP_OBJ) {
1051 ae_tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
1052 } else if (acl_tag == ACL_OTHER) {
1053 ae_tag = ARCHIVE_ENTRY_ACL_OTHER;
1055 /* Skip types that libarchive can't support. */
1059 acl_get_permset(acl_entry, &acl_permset);
1062 * acl_get_perm() is spelled differently on different
1063 * platforms; see bsdtar_platform.h for details.
1065 if (ACL_GET_PERM(acl_permset, ACL_EXECUTE))
1066 ae_perm |= ARCHIVE_ENTRY_ACL_EXECUTE;
1067 if (ACL_GET_PERM(acl_permset, ACL_READ))
1068 ae_perm |= ARCHIVE_ENTRY_ACL_READ;
1069 if (ACL_GET_PERM(acl_permset, ACL_WRITE))
1070 ae_perm |= ARCHIVE_ENTRY_ACL_WRITE;
1072 archive_entry_acl_add_entry(entry,
1073 archive_entry_acl_type, ae_perm, ae_tag,
1076 s = acl_get_entry(acl, ACL_NEXT_ENTRY, &acl_entry);
1083 setup_acls(struct bsdtar *bsdtar, struct archive_entry *entry,
1084 const char *accpath)
1092 #if HAVE_LISTXATTR && HAVE_LLISTXATTR && HAVE_GETXATTR && HAVE_LGETXATTR
1095 setup_xattr(struct bsdtar *bsdtar, struct archive_entry *entry,
1096 const char *accpath, const char *name)
1100 char symlink_mode = bsdtar->symlink_mode;
1102 if (symlink_mode == 'H')
1103 size = getxattr(accpath, name, NULL, 0);
1105 size = lgetxattr(accpath, name, NULL, 0);
1108 bsdtar_warnc(bsdtar, errno, "Couldn't get extended attribute");
1112 if (size > 0 && (value = malloc(size)) == NULL) {
1113 bsdtar_errc(bsdtar, 1, errno, "Out of memory");
1117 if (symlink_mode == 'H')
1118 size = getxattr(accpath, name, value, size);
1120 size = lgetxattr(accpath, name, value, size);
1123 bsdtar_warnc(bsdtar, errno, "Couldn't get extended attribute");
1127 archive_entry_xattr_add_entry(entry, name, value, size);
1133 * Linux extended attribute support
1136 setup_xattrs(struct bsdtar *bsdtar, struct archive_entry *entry,
1137 const char *accpath)
1141 char symlink_mode = bsdtar->symlink_mode;
1143 if (symlink_mode == 'H')
1144 list_size = listxattr(accpath, NULL, 0);
1146 list_size = llistxattr(accpath, NULL, 0);
1148 if (list_size == -1) {
1149 bsdtar_warnc(bsdtar, errno,
1150 "Couldn't list extended attributes");
1152 } else if (list_size == 0)
1155 if ((list = malloc(list_size)) == NULL) {
1156 bsdtar_errc(bsdtar, 1, errno, "Out of memory");
1160 if (symlink_mode == 'H')
1161 list_size = listxattr(accpath, list, list_size);
1163 list_size = llistxattr(accpath, list, list_size);
1165 if (list_size == -1) {
1166 bsdtar_warnc(bsdtar, errno,
1167 "Couldn't list extended attributes");
1172 for (p = list; (p - list) < list_size; p += strlen(p) + 1) {
1173 if (strncmp(p, "system.", 7) == 0 ||
1174 strncmp(p, "xfsroot.", 8) == 0)
1177 setup_xattr(bsdtar, entry, accpath, p);
1186 * Generic (stub) extended attribute support.
1189 setup_xattrs(struct bsdtar *bsdtar, struct archive_entry *entry,
1190 const char *accpath)
1192 (void)bsdtar; /* UNUSED */
1193 (void)entry; /* UNUSED */
1194 (void)accpath; /* UNUSED */
1200 free_cache(struct name_cache *cache)
1204 if (cache != NULL) {
1205 for (i = 0; i < cache->size; i++) {
1206 if (cache->cache[i].name != NULL &&
1207 cache->cache[i].name != NO_NAME)
1208 free((void *)(uintptr_t)cache->cache[i].name);
1215 * Lookup uid/gid from uname/gname, return NULL if no match.
1218 lookup_name(struct bsdtar *bsdtar, struct name_cache **name_cache_variable,
1219 int (*lookup_fn)(struct bsdtar *, const char **, id_t), id_t id)
1221 struct name_cache *cache;
1226 if (*name_cache_variable == NULL) {
1227 *name_cache_variable = malloc(sizeof(struct name_cache));
1228 if (*name_cache_variable == NULL)
1229 bsdtar_errc(bsdtar, 1, ENOMEM, "No more memory");
1230 memset(*name_cache_variable, 0, sizeof(struct name_cache));
1231 (*name_cache_variable)->size = name_cache_size;
1234 cache = *name_cache_variable;
1237 slot = id % cache->size;
1238 if (cache->cache[slot].name != NULL) {
1239 if (cache->cache[slot].id == id) {
1241 if (cache->cache[slot].name == NO_NAME)
1243 return (cache->cache[slot].name);
1245 if (cache->cache[slot].name != NO_NAME)
1246 free((void *)(uintptr_t)cache->cache[slot].name);
1247 cache->cache[slot].name = NULL;
1250 if (lookup_fn(bsdtar, &name, id) == 0) {
1251 if (name == NULL || name[0] == '\0') {
1252 /* Cache the negative response. */
1253 cache->cache[slot].name = NO_NAME;
1254 cache->cache[slot].id = id;
1256 cache->cache[slot].name = strdup(name);
1257 if (cache->cache[slot].name != NULL) {
1258 cache->cache[slot].id = id;
1259 return (cache->cache[slot].name);
1262 * Conveniently, NULL marks an empty slot, so
1263 * if the strdup() fails, we've just failed to
1264 * cache it. No recovery necessary.
1272 lookup_uname(struct bsdtar *bsdtar, uid_t uid)
1274 return (lookup_name(bsdtar, &bsdtar->uname_cache,
1275 &lookup_uname_helper, (id_t)uid));
1279 lookup_uname_helper(struct bsdtar *bsdtar, const char **name, id_t id)
1281 struct passwd *pwent;
1283 (void)bsdtar; /* UNUSED */
1286 pwent = getpwuid((uid_t)id);
1287 if (pwent == NULL) {
1290 bsdtar_warnc(bsdtar, errno, "getpwuid(%d) failed", id);
1294 *name = pwent->pw_name;
1299 lookup_gname(struct bsdtar *bsdtar, gid_t gid)
1301 return (lookup_name(bsdtar, &bsdtar->gname_cache,
1302 &lookup_gname_helper, (id_t)gid));
1306 lookup_gname_helper(struct bsdtar *bsdtar, const char **name, id_t id)
1308 struct group *grent;
1310 (void)bsdtar; /* UNUSED */
1313 grent = getgrgid((gid_t)id);
1314 if (grent == NULL) {
1317 bsdtar_warnc(bsdtar, errno, "getgrgid(%d) failed", id);
1321 *name = grent->gr_name;
1326 * Test if the specified file is new enough to include in the archive.
1329 new_enough(struct bsdtar *bsdtar, const char *path, const struct stat *st)
1331 struct archive_dir_entry *p;
1334 * If this file/dir is excluded by a time comparison, skip it.
1336 if (bsdtar->newer_ctime_sec > 0) {
1337 if (st->st_ctime < bsdtar->newer_ctime_sec)
1338 return (0); /* Too old, skip it. */
1339 if (st->st_ctime == bsdtar->newer_ctime_sec
1340 && ARCHIVE_STAT_CTIME_NANOS(st)
1341 <= bsdtar->newer_ctime_nsec)
1342 return (0); /* Too old, skip it. */
1344 if (bsdtar->newer_mtime_sec > 0) {
1345 if (st->st_mtime < bsdtar->newer_mtime_sec)
1346 return (0); /* Too old, skip it. */
1347 if (st->st_mtime == bsdtar->newer_mtime_sec
1348 && ARCHIVE_STAT_MTIME_NANOS(st)
1349 <= bsdtar->newer_mtime_nsec)
1350 return (0); /* Too old, skip it. */
1354 * In -u mode, we only write an entry if it's newer than
1355 * what was already in the archive.
1357 if (bsdtar->archive_dir != NULL &&
1358 bsdtar->archive_dir->head != NULL) {
1359 for (p = bsdtar->archive_dir->head; p != NULL; p = p->next) {
1360 if (strcmp(path, p->name)==0)
1361 return (p->mtime_sec < st->st_mtime ||
1362 (p->mtime_sec == st->st_mtime &&
1364 < ARCHIVE_STAT_MTIME_NANOS(st)));
1368 /* If the file wasn't rejected, include it. */
1373 * Add an entry to the dir list for 'u' mode.
1375 * XXX TODO: Make this fast.
1378 add_dir_list(struct bsdtar *bsdtar, const char *path,
1379 time_t mtime_sec, int mtime_nsec)
1381 struct archive_dir_entry *p;
1383 if (path[0] == '.' && path[1] == '/' && path[2] != '\0')
1387 * Search entire list to see if this file has appeared before.
1388 * If it has, override the timestamp data.
1390 p = bsdtar->archive_dir->head;
1392 if (strcmp(path, p->name)==0) {
1393 p->mtime_sec = mtime_sec;
1394 p->mtime_nsec = mtime_nsec;
1400 p = malloc(sizeof(*p));
1402 bsdtar_errc(bsdtar, 1, ENOMEM, "Can't read archive directory");
1404 p->name = strdup(path);
1405 if (p->name == NULL)
1406 bsdtar_errc(bsdtar, 1, ENOMEM, "Can't read archive directory");
1407 p->mtime_sec = mtime_sec;
1408 p->mtime_nsec = mtime_nsec;
1410 if (bsdtar->archive_dir->tail == NULL) {
1411 bsdtar->archive_dir->head = bsdtar->archive_dir->tail = p;
1413 bsdtar->archive_dir->tail->next = p;
1414 bsdtar->archive_dir->tail = p;
1419 test_for_append(struct bsdtar *bsdtar)
1423 if (*bsdtar->argv == NULL)
1424 bsdtar_errc(bsdtar, 1, 0, "no files or directories specified");
1425 if (bsdtar->filename == NULL)
1426 bsdtar_errc(bsdtar, 1, 0, "Cannot append to stdout.");
1428 if (bsdtar->create_compression != 0)
1429 bsdtar_errc(bsdtar, 1, 0,
1430 "Cannot append to %s with compression", bsdtar->filename);
1432 if (stat(bsdtar->filename, &s) != 0)
1433 bsdtar_errc(bsdtar, 1, errno,
1434 "Cannot stat %s", bsdtar->filename);
1436 if (!S_ISREG(s.st_mode))
1437 bsdtar_errc(bsdtar, 1, 0,
1438 "Cannot append to %s: not a regular file.",