2 * Copyright (c) 2003-2007 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 * 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$");
29 #ifdef HAVE_SYS_TYPES_H
30 #include <sys/types.h>
35 #ifdef HAVE_SYS_IOCTL_H
36 #include <sys/ioctl.h>
38 #ifdef HAVE_SYS_STAT_H
41 #ifdef HAVE_ATTR_XATTR_H
42 #include <attr/xattr.h>
47 #ifdef HAVE_EXT2FS_EXT2_FS_H
48 #include <ext2fs/ext2_fs.h>
62 #ifdef HAVE_LINUX_FS_H
63 #include <linux/fs.h> /* for Linux file flags */
65 #ifdef HAVE_LINUX_EXT2_FS_H
66 #include <linux/ext2_fs.h> /* for Linux file flags */
85 /* Fixed size of uname/gname caches. */
86 #define name_cache_size 101
88 static const char * const NO_NAME = "(noname)";
90 /* Initial size of link cache. */
91 #define links_cache_initial_size 1024
93 struct archive_dir_entry {
94 struct archive_dir_entry *next;
101 struct archive_dir_entry *head, *tail;
105 unsigned long number_entries;
106 size_t number_buckets;
107 struct links_entry **buckets;
111 struct links_entry *next;
112 struct links_entry *previous;
126 } cache[name_cache_size];
129 static void add_dir_list(struct bsdtar *bsdtar, const char *path,
130 time_t mtime_sec, int mtime_nsec);
131 static int append_archive(struct bsdtar *, struct archive *,
133 static void archive_names_from_file(struct bsdtar *bsdtar,
135 static int archive_names_from_file_helper(struct bsdtar *bsdtar,
137 static void create_cleanup(struct bsdtar *);
138 static void free_buckets(struct bsdtar *, struct links_cache *);
139 static void free_cache(struct name_cache *cache);
140 static const char * lookup_gname(struct bsdtar *bsdtar, gid_t gid);
141 static int lookup_gname_helper(struct bsdtar *bsdtar,
142 const char **name, id_t gid);
143 static void lookup_hardlink(struct bsdtar *,
144 struct archive_entry *entry, const struct stat *);
145 static const char * lookup_uname(struct bsdtar *bsdtar, uid_t uid);
146 static int lookup_uname_helper(struct bsdtar *bsdtar,
147 const char **name, id_t uid);
148 static int new_enough(struct bsdtar *, const char *path,
149 const struct stat *);
150 static void setup_acls(struct bsdtar *, struct archive_entry *,
152 static void setup_xattrs(struct bsdtar *, struct archive_entry *,
154 static void test_for_append(struct bsdtar *);
155 static void write_archive(struct archive *, struct bsdtar *);
156 static void write_entry(struct bsdtar *, struct archive *,
157 const struct stat *, const char *pathname,
158 unsigned pathlen, const char *accpath);
159 static int write_file_data(struct bsdtar *, struct archive *,
161 static void write_hierarchy(struct bsdtar *, struct archive *,
165 tar_mode_c(struct bsdtar *bsdtar)
170 if (*bsdtar->argv == NULL && bsdtar->names_from_file == NULL)
171 bsdtar_errc(bsdtar, 1, 0, "no files or directories specified");
173 a = archive_write_new();
175 /* Support any format that the library supports. */
176 if (bsdtar->create_format == NULL) {
177 r = archive_write_set_format_pax_restricted(a);
178 bsdtar->create_format = "pax restricted";
180 r = archive_write_set_format_by_name(a, bsdtar->create_format);
182 if (r != ARCHIVE_OK) {
183 fprintf(stderr, "Can't use format %s: %s\n",
184 bsdtar->create_format,
185 archive_error_string(a));
190 * If user explicitly set the block size, then assume they
191 * want the last block padded as well. Otherwise, use the
192 * default block size and accept archive_write_open_file()'s
193 * default padding decisions.
195 if (bsdtar->bytes_per_block != 0) {
196 archive_write_set_bytes_per_block(a, bsdtar->bytes_per_block);
197 archive_write_set_bytes_in_last_block(a,
198 bsdtar->bytes_per_block);
200 archive_write_set_bytes_per_block(a, DEFAULT_BYTES_PER_BLOCK);
202 switch (bsdtar->create_compression) {
207 archive_write_set_compression_bzip2(a);
212 archive_write_set_compression_gzip(a);
216 bsdtar_errc(bsdtar, 1, 0,
217 "Unrecognized compression option -%c",
218 bsdtar->create_compression);
221 r = archive_write_open_file(a, bsdtar->filename);
223 bsdtar_errc(bsdtar, 1, 0, archive_error_string(a));
225 write_archive(a, bsdtar);
227 if (bsdtar->option_totals) {
228 fprintf(stderr, "Total bytes written: " BSDTAR_FILESIZE_PRINTF "\n",
229 (BSDTAR_FILESIZE_TYPE)archive_position_compressed(a));
232 archive_write_finish(a);
236 * Same as 'c', except we only support tar or empty formats in
237 * uncompressed files on disk.
240 tar_mode_r(struct bsdtar *bsdtar)
245 struct archive_entry *entry;
248 /* Sanity-test some arguments and the file. */
249 test_for_append(bsdtar);
251 format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
253 bsdtar->fd = open(bsdtar->filename, O_RDWR | O_CREAT, 0666);
255 bsdtar_errc(bsdtar, 1, errno,
256 "Cannot open %s", bsdtar->filename);
258 a = archive_read_new();
259 archive_read_support_compression_all(a);
260 archive_read_support_format_tar(a);
261 archive_read_support_format_gnutar(a);
262 r = archive_read_open_fd(a, bsdtar->fd, 10240);
264 bsdtar_errc(bsdtar, 1, archive_errno(a),
265 "Can't read archive %s: %s", bsdtar->filename,
266 archive_error_string(a));
267 while (0 == archive_read_next_header(a, &entry)) {
268 if (archive_compression(a) != ARCHIVE_COMPRESSION_NONE) {
269 archive_read_finish(a);
271 bsdtar_errc(bsdtar, 1, 0,
272 "Cannot append to compressed archive.");
274 /* Keep going until we hit end-of-archive */
275 format = archive_format(a);
278 end_offset = archive_read_header_position(a);
279 archive_read_finish(a);
281 /* Re-open archive for writing */
282 a = archive_write_new();
283 archive_write_set_compression_none(a);
285 * Set the format to be used for writing. To allow people to
286 * extend empty files, we need to allow them to specify the format,
287 * which opens the possibility that they will specify a format that
288 * doesn't match the existing format. Hence, the following bit
289 * of arcane ugliness.
292 if (bsdtar->create_format != NULL) {
293 /* If the user requested a format, use that, but ... */
294 archive_write_set_format_by_name(a,
295 bsdtar->create_format);
296 /* ... complain if it's not compatible. */
297 format &= ARCHIVE_FORMAT_BASE_MASK;
298 if (format != (int)(archive_format(a) & ARCHIVE_FORMAT_BASE_MASK)
299 && format != ARCHIVE_FORMAT_EMPTY) {
300 bsdtar_errc(bsdtar, 1, 0,
301 "Format %s is incompatible with the archive %s.",
302 bsdtar->create_format, bsdtar->filename);
306 * Just preserve the current format, with a little care
307 * for formats that libarchive can't write.
309 if (format == ARCHIVE_FORMAT_TAR_GNUTAR)
310 /* TODO: When gtar supports pax, use pax restricted. */
311 format = ARCHIVE_FORMAT_TAR_USTAR;
312 if (format == ARCHIVE_FORMAT_EMPTY)
313 format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
314 archive_write_set_format(a, format);
316 lseek(bsdtar->fd, end_offset, SEEK_SET); /* XXX check return val XXX */
317 archive_write_open_fd(a, bsdtar->fd); /* XXX check return val XXX */
319 write_archive(a, bsdtar); /* XXX check return val XXX */
321 if (bsdtar->option_totals) {
322 fprintf(stderr, "Total bytes written: " BSDTAR_FILESIZE_PRINTF "\n",
323 (BSDTAR_FILESIZE_TYPE)archive_position_compressed(a));
326 archive_write_finish(a);
332 tar_mode_u(struct bsdtar *bsdtar)
336 struct archive_entry *entry;
338 struct archive_dir_entry *p;
339 struct archive_dir archive_dir;
341 bsdtar->archive_dir = &archive_dir;
342 memset(&archive_dir, 0, sizeof(archive_dir));
344 format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
346 /* Sanity-test some arguments and the file. */
347 test_for_append(bsdtar);
349 bsdtar->fd = open(bsdtar->filename, O_RDWR);
351 bsdtar_errc(bsdtar, 1, errno,
352 "Cannot open %s", bsdtar->filename);
354 a = archive_read_new();
355 archive_read_support_compression_all(a);
356 archive_read_support_format_tar(a);
357 archive_read_support_format_gnutar(a);
358 if (archive_read_open_fd(a, bsdtar->fd,
359 bsdtar->bytes_per_block != 0 ? bsdtar->bytes_per_block :
360 DEFAULT_BYTES_PER_BLOCK) != ARCHIVE_OK) {
361 bsdtar_errc(bsdtar, 1, 0,
362 "Can't open %s: %s", bsdtar->filename,
363 archive_error_string(a));
366 /* Build a list of all entries and their recorded mod times. */
367 while (0 == archive_read_next_header(a, &entry)) {
368 if (archive_compression(a) != ARCHIVE_COMPRESSION_NONE) {
369 archive_read_finish(a);
371 bsdtar_errc(bsdtar, 1, 0,
372 "Cannot append to compressed archive.");
374 add_dir_list(bsdtar, archive_entry_pathname(entry),
375 archive_entry_mtime(entry),
376 archive_entry_mtime_nsec(entry));
377 /* Record the last format determination we see */
378 format = archive_format(a);
379 /* Keep going until we hit end-of-archive */
382 end_offset = archive_read_header_position(a);
383 archive_read_finish(a);
385 /* Re-open archive for writing. */
386 a = archive_write_new();
387 archive_write_set_compression_none(a);
389 * Set format to same one auto-detected above, except that
390 * we don't write GNU tar format, so use ustar instead.
392 if (format == ARCHIVE_FORMAT_TAR_GNUTAR)
393 format = ARCHIVE_FORMAT_TAR_USTAR;
394 archive_write_set_format(a, format);
395 if (bsdtar->bytes_per_block != 0) {
396 archive_write_set_bytes_per_block(a, bsdtar->bytes_per_block);
397 archive_write_set_bytes_in_last_block(a,
398 bsdtar->bytes_per_block);
400 archive_write_set_bytes_per_block(a, DEFAULT_BYTES_PER_BLOCK);
401 lseek(bsdtar->fd, end_offset, SEEK_SET);
402 ftruncate(bsdtar->fd, end_offset);
403 archive_write_open_fd(a, bsdtar->fd);
405 write_archive(a, bsdtar);
407 if (bsdtar->option_totals) {
408 fprintf(stderr, "Total bytes written: " BSDTAR_FILESIZE_PRINTF "\n",
409 (BSDTAR_FILESIZE_TYPE)archive_position_compressed(a));
412 archive_write_finish(a);
416 while (bsdtar->archive_dir->head != NULL) {
417 p = bsdtar->archive_dir->head->next;
418 free(bsdtar->archive_dir->head->name);
419 free(bsdtar->archive_dir->head);
420 bsdtar->archive_dir->head = p;
422 bsdtar->archive_dir->tail = NULL;
427 * Write user-specified files/dirs to opened archive.
430 write_archive(struct archive *a, struct bsdtar *bsdtar)
434 if (bsdtar->names_from_file != NULL)
435 archive_names_from_file(bsdtar, a);
437 while (*bsdtar->argv) {
439 if (arg[0] == '-' && arg[1] == 'C') {
445 bsdtar_warnc(bsdtar, 1, 0,
446 "Missing argument for -C");
447 bsdtar->return_value = 1;
451 set_chdir(bsdtar, arg);
453 if (*arg != '/' || (arg[0] == '@' && arg[1] != '/'))
454 do_chdir(bsdtar); /* Handle a deferred -C */
456 if (append_archive(bsdtar, a, arg + 1) != 0)
459 write_hierarchy(bsdtar, a, arg);
464 create_cleanup(bsdtar);
465 if (archive_write_close(a)) {
466 bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(a));
467 bsdtar->return_value = 1;
472 * Archive names specified in file.
474 * Unless --null was specified, a line containing exactly "-C" will
475 * cause the next line to be a directory to pass to chdir(). If
476 * --null is specified, then a line "-C" is just another filename.
479 archive_names_from_file(struct bsdtar *bsdtar, struct archive *a)
483 bsdtar->next_line_is_dir = 0;
484 process_lines(bsdtar, bsdtar->names_from_file,
485 archive_names_from_file_helper);
486 if (bsdtar->next_line_is_dir)
487 bsdtar_errc(bsdtar, 1, errno,
488 "Unexpected end of filename list; "
489 "directory expected after -C");
493 archive_names_from_file_helper(struct bsdtar *bsdtar, const char *line)
495 if (bsdtar->next_line_is_dir) {
496 set_chdir(bsdtar, line);
497 bsdtar->next_line_is_dir = 0;
498 } else if (!bsdtar->option_null && strcmp(line, "-C") == 0)
499 bsdtar->next_line_is_dir = 1;
502 do_chdir(bsdtar); /* Handle a deferred -C */
503 write_hierarchy(bsdtar, bsdtar->archive, line);
509 * Copy from specified archive to current archive. Returns non-zero
510 * for write errors (which force us to terminate the entire archiving
511 * operation). If there are errors reading the input archive, we set
512 * bsdtar->return_value but return zero, so the overall archiving
513 * operation will complete and return non-zero.
516 append_archive(struct bsdtar *bsdtar, struct archive *a, const char *filename)
519 struct archive_entry *in_entry;
520 int bytes_read, bytes_written;
523 if (strcmp(filename, "-") == 0)
524 filename = NULL; /* Library uses NULL for stdio. */
526 ina = archive_read_new();
527 archive_read_support_format_all(ina);
528 archive_read_support_compression_all(ina);
529 if (archive_read_open_file(ina, filename, 10240)) {
530 bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(ina));
531 bsdtar->return_value = 1;
534 while (0 == archive_read_next_header(ina, &in_entry)) {
535 if (!new_enough(bsdtar, archive_entry_pathname(in_entry),
536 archive_entry_stat(in_entry)))
538 if (excluded(bsdtar, archive_entry_pathname(in_entry)))
540 if (bsdtar->option_interactive &&
541 !yes("copy '%s'", archive_entry_pathname(in_entry)))
544 safe_fprintf(stderr, "a %s",
545 archive_entry_pathname(in_entry));
546 /* XXX handle/report errors XXX */
547 if (archive_write_header(a, in_entry)) {
548 bsdtar_warnc(bsdtar, 0, "%s",
549 archive_error_string(a));
550 bsdtar->return_value = 1;
553 bytes_read = archive_read_data(ina, buff, sizeof(buff));
554 while (bytes_read > 0) {
556 archive_write_data(a, buff, bytes_read);
557 if (bytes_written < bytes_read) {
558 bsdtar_warnc(bsdtar, archive_errno(a), "%s",
559 archive_error_string(a));
563 archive_read_data(ina, buff, sizeof(buff));
566 fprintf(stderr, "\n");
569 if (archive_errno(ina)) {
570 bsdtar_warnc(bsdtar, 0, "Error reading archive %s: %s",
571 filename, archive_error_string(ina));
572 bsdtar->return_value = 1;
574 archive_read_finish(ina);
576 /* Note: If we got here, we saw no write errors, so return success. */
581 * Add the file or dir hierarchy named by 'path' to the archive
584 write_hierarchy(struct bsdtar *bsdtar, struct archive *a, const char *path)
587 char symlink_mode = bsdtar->symlink_mode;
589 int dev_recorded = 0;
593 unsigned long fflags;
596 tree = tree_open(path);
599 bsdtar_warnc(bsdtar, errno, "%s: Cannot open", path);
600 bsdtar->return_value = 1;
604 while ((tree_ret = tree_next(tree))) {
605 const char *name = tree_current_path(tree);
606 const struct stat *st = NULL, *lst = NULL;
609 if (tree_ret == TREE_ERROR_DIR)
610 bsdtar_warnc(bsdtar, errno, "%s: Couldn't visit directory", name);
611 if (tree_ret != TREE_REGULAR)
613 lst = tree_current_lstat(tree);
615 /* Couldn't lstat(); must not exist. */
616 bsdtar_warnc(bsdtar, errno, "%s: Cannot stat", name);
617 bsdtar->return_value = 1;
620 if (S_ISLNK(lst->st_mode))
621 st = tree_current_stat(tree);
622 /* Default: descend into any dir or symlink to dir. */
623 /* We'll adjust this later on. */
625 if ((st != NULL) && S_ISDIR(st->st_mode))
627 if ((lst != NULL) && S_ISDIR(lst->st_mode))
631 * If user has asked us not to cross mount points,
632 * then don't descend into into a dir on a different
636 first_dev = lst->st_dev;
639 if (bsdtar->option_dont_traverse_mounts) {
640 if (lst != NULL && lst->st_dev != first_dev)
645 * If this file/dir is flagged "nodump" and we're
646 * honoring such flags, skip this file/dir.
649 if (bsdtar->option_honor_nodump &&
650 (lst->st_flags & UF_NODUMP))
656 * Linux has a nodump flag too but to read it
657 * we have to open() the file/dir and do an ioctl on it...
659 if (bsdtar->option_honor_nodump &&
660 ((fd = open(name, O_RDONLY|O_NONBLOCK)) >= 0) &&
661 ((r = ioctl(fd, EXT2_IOC_GETFLAGS, &fflags)),
662 close(fd), r) >= 0 &&
663 (fflags & EXT2_NODUMP_FL))
668 * If this file/dir is excluded by a filename
671 if (excluded(bsdtar, name))
675 * If the user vetoes this file/directory, skip it.
677 if (bsdtar->option_interactive &&
678 !yes("add '%s'", name))
682 * If this is a dir, decide whether or not to recurse.
684 if (bsdtar->option_no_subdirs)
688 * Distinguish 'L'/'P'/'H' symlink following.
690 switch(symlink_mode) {
692 /* 'H': After the first item, rest like 'P'. */
694 /* 'H': First item (from command line) like 'L'. */
697 /* 'L': Do descend through a symlink to dir. */
698 /* 'L': Archive symlink to file as file. */
699 lst = tree_current_stat(tree);
700 /* If stat fails, we have a broken symlink;
701 * in that case, archive the link as such. */
703 lst = tree_current_lstat(tree);
706 /* 'P': Don't descend through a symlink to dir. */
707 if (!S_ISDIR(lst->st_mode))
709 /* 'P': Archive symlink to file as symlink. */
710 /* lst = tree_current_lstat(tree); */
718 * Write the entry. Note that write_entry() handles
719 * pathname editing and newness testing.
721 write_entry(bsdtar, a, lst, name,
722 tree_current_pathlen(tree),
723 tree_current_access_path(tree));
729 * Add a single filesystem object to the archive.
732 write_entry(struct bsdtar *bsdtar, struct archive *a, const struct stat *st,
733 const char *pathname, unsigned pathlen, const char *accpath)
735 struct archive_entry *entry;
740 unsigned long stflags;
742 static char linkbuffer[PATH_MAX+1];
744 (void)pathlen; /* UNUSED */
747 entry = archive_entry_new();
749 archive_entry_set_pathname(entry, pathname);
752 * Rewrite the pathname to be archived. If rewrite
753 * fails, skip the entry.
755 if (edit_pathname(bsdtar, entry))
759 * In -u mode, check that the file is newer than what's
760 * already in the archive; in all modes, obey --newerXXX flags.
762 if (!new_enough(bsdtar, archive_entry_pathname(entry), st))
765 if (!S_ISDIR(st->st_mode) && (st->st_nlink > 1))
766 lookup_hardlink(bsdtar, entry, st);
768 /* Display entry as we process it. This format is required by SUSv2. */
770 safe_fprintf(stderr, "a %s", archive_entry_pathname(entry));
772 /* Read symbolic link information. */
773 if ((st->st_mode & S_IFMT) == S_IFLNK) {
776 lnklen = readlink(accpath, linkbuffer, PATH_MAX);
778 if (!bsdtar->verbose)
779 bsdtar_warnc(bsdtar, errno,
780 "%s: Couldn't read symbolic link",
784 ": Couldn't read symbolic link: %s",
788 linkbuffer[lnklen] = 0;
789 archive_entry_set_symlink(entry, linkbuffer);
792 /* Look up username and group name. */
793 archive_entry_set_uname(entry, lookup_uname(bsdtar, st->st_uid));
794 archive_entry_set_gname(entry, lookup_gname(bsdtar, st->st_gid));
797 if (st->st_flags != 0)
798 archive_entry_set_fflags(entry, st->st_flags, 0);
802 if ((S_ISREG(st->st_mode) || S_ISDIR(st->st_mode)) &&
803 ((fd = open(accpath, O_RDONLY|O_NONBLOCK)) >= 0) &&
804 ((r = ioctl(fd, EXT2_IOC_GETFLAGS, &stflags)), close(fd), (fd = -1), r) >= 0 &&
806 archive_entry_set_fflags(entry, stflags, 0);
810 archive_entry_copy_stat(entry, st);
811 setup_acls(bsdtar, entry, accpath);
812 setup_xattrs(bsdtar, entry, accpath);
815 * If it's a regular file (and non-zero in size) make sure we
816 * can open it before we start to write. In particular, note
817 * that we can always archive a zero-length file, even if we
820 if (S_ISREG(st->st_mode) && st->st_size > 0) {
821 fd = open(accpath, O_RDONLY);
823 if (!bsdtar->verbose)
824 bsdtar_warnc(bsdtar, errno, "%s: could not open file", pathname);
826 fprintf(stderr, ": %s", strerror(errno));
831 /* Non-regular files get archived with zero size. */
832 if (!S_ISREG(st->st_mode))
833 archive_entry_set_size(entry, 0);
835 e = archive_write_header(a, entry);
836 if (e != ARCHIVE_OK) {
837 if (!bsdtar->verbose)
838 bsdtar_warnc(bsdtar, 0, "%s: %s", pathname,
839 archive_error_string(a));
841 fprintf(stderr, ": %s", archive_error_string(a));
844 if (e == ARCHIVE_FATAL)
848 * If we opened a file earlier, write it out now. Note that
849 * the format handler might have reset the size field to zero
850 * to inform us that the archive body won't get stored. In
851 * that case, just skip the write.
853 if (e >= ARCHIVE_WARN && fd >= 0 && archive_entry_size(entry) > 0)
854 if (write_file_data(bsdtar, a, fd))
859 fprintf(stderr, "\n");
866 archive_entry_free(entry);
870 /* Helper function to copy file to archive, with stack-allocated buffer. */
872 write_file_data(struct bsdtar *bsdtar, struct archive *a, int fd)
876 ssize_t bytes_written;
878 /* XXX TODO: Allocate buffer on heap and store pointer to
879 * it in bsdtar structure; arrange cleanup as well. XXX */
881 bytes_read = read(fd, buff, sizeof(buff));
882 while (bytes_read > 0) {
883 bytes_written = archive_write_data(a, buff, bytes_read);
884 if (bytes_written < 0) {
885 /* Write failed; this is bad */
886 bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(a));
889 if (bytes_written < bytes_read) {
890 /* Write was truncated; warn but continue. */
891 bsdtar_warnc(bsdtar, 0,
892 "Truncated write; file may have grown while being archived.");
895 bytes_read = read(fd, buff, sizeof(buff));
902 create_cleanup(struct bsdtar *bsdtar)
904 /* Free inode->pathname map used for hardlink detection. */
905 if (bsdtar->links_cache != NULL) {
906 free_buckets(bsdtar, bsdtar->links_cache);
907 free(bsdtar->links_cache);
908 bsdtar->links_cache = NULL;
911 free_cache(bsdtar->uname_cache);
912 bsdtar->uname_cache = NULL;
913 free_cache(bsdtar->gname_cache);
914 bsdtar->gname_cache = NULL;
919 free_buckets(struct bsdtar *bsdtar, struct links_cache *links_cache)
923 if (links_cache->buckets == NULL)
926 for (i = 0; i < links_cache->number_buckets; i++) {
927 while (links_cache->buckets[i] != NULL) {
928 struct links_entry *lp = links_cache->buckets[i]->next;
929 if (bsdtar->option_warn_links)
930 bsdtar_warnc(bsdtar, 0, "Missing links to %s",
931 links_cache->buckets[i]->name);
932 if (links_cache->buckets[i]->name != NULL)
933 free(links_cache->buckets[i]->name);
934 free(links_cache->buckets[i]);
935 links_cache->buckets[i] = lp;
938 free(links_cache->buckets);
939 links_cache->buckets = NULL;
943 lookup_hardlink(struct bsdtar *bsdtar, struct archive_entry *entry,
944 const struct stat *st)
946 struct links_cache *links_cache;
947 struct links_entry *le, **new_buckets;
951 /* If necessary, initialize the links cache. */
952 links_cache = bsdtar->links_cache;
953 if (links_cache == NULL) {
954 bsdtar->links_cache = malloc(sizeof(struct links_cache));
955 if (bsdtar->links_cache == NULL)
956 bsdtar_errc(bsdtar, 1, ENOMEM,
957 "No memory for hardlink detection.");
958 links_cache = bsdtar->links_cache;
959 memset(links_cache, 0, sizeof(struct links_cache));
960 links_cache->number_buckets = links_cache_initial_size;
961 links_cache->buckets = malloc(links_cache->number_buckets *
962 sizeof(links_cache->buckets[0]));
963 if (links_cache->buckets == NULL) {
964 bsdtar_errc(bsdtar, 1, ENOMEM,
965 "No memory for hardlink detection.");
967 for (i = 0; i < links_cache->number_buckets; i++)
968 links_cache->buckets[i] = NULL;
971 /* If the links cache overflowed and got flushed, don't bother. */
972 if (links_cache->buckets == NULL)
975 /* If the links cache is getting too full, enlarge the hash table. */
976 if (links_cache->number_entries > links_cache->number_buckets * 2)
978 new_size = links_cache->number_buckets * 2;
979 new_buckets = malloc(new_size * sizeof(struct links_entry *));
981 if (new_buckets != NULL) {
982 memset(new_buckets, 0,
983 new_size * sizeof(struct links_entry *));
984 for (i = 0; i < links_cache->number_buckets; i++) {
985 while (links_cache->buckets[i] != NULL) {
986 /* Remove entry from old bucket. */
987 le = links_cache->buckets[i];
988 links_cache->buckets[i] = le->next;
990 /* Add entry to new bucket. */
991 hash = (le->dev ^ le->ino) % new_size;
993 if (new_buckets[hash] != NULL)
994 new_buckets[hash]->previous =
996 le->next = new_buckets[hash];
998 new_buckets[hash] = le;
1001 free(links_cache->buckets);
1002 links_cache->buckets = new_buckets;
1003 links_cache->number_buckets = new_size;
1005 free_buckets(bsdtar, links_cache);
1006 bsdtar_warnc(bsdtar, ENOMEM,
1007 "No more memory for recording hard links");
1008 bsdtar_warnc(bsdtar, 0,
1009 "Remaining links will be dumped as full files");
1013 /* Try to locate this entry in the links cache. */
1014 hash = ( st->st_dev ^ st->st_ino ) % links_cache->number_buckets;
1015 for (le = links_cache->buckets[hash]; le != NULL; le = le->next) {
1016 if (le->dev == st->st_dev && le->ino == st->st_ino) {
1017 archive_entry_copy_hardlink(entry, le->name);
1020 * Decrement link count each time and release
1021 * the entry if it hits zero. This saves
1022 * memory and is necessary for proper -l
1025 if (--le->links <= 0) {
1026 if (le->previous != NULL)
1027 le->previous->next = le->next;
1028 if (le->next != NULL)
1029 le->next->previous = le->previous;
1030 if (le->name != NULL)
1032 if (links_cache->buckets[hash] == le)
1033 links_cache->buckets[hash] = le->next;
1034 links_cache->number_entries--;
1042 /* Add this entry to the links cache. */
1043 le = malloc(sizeof(struct links_entry));
1045 le->name = strdup(archive_entry_pathname(entry));
1046 if ((le == NULL) || (le->name == NULL)) {
1047 free_buckets(bsdtar, links_cache);
1048 bsdtar_warnc(bsdtar, ENOMEM,
1049 "No more memory for recording hard links");
1050 bsdtar_warnc(bsdtar, 0,
1051 "Remaining hard links will be dumped as full files");
1056 if (links_cache->buckets[hash] != NULL)
1057 links_cache->buckets[hash]->previous = le;
1058 links_cache->number_entries++;
1059 le->next = links_cache->buckets[hash];
1060 le->previous = NULL;
1061 links_cache->buckets[hash] = le;
1062 le->dev = st->st_dev;
1063 le->ino = st->st_ino;
1064 le->links = st->st_nlink - 1;
1067 #ifdef HAVE_POSIX_ACL
1068 static void setup_acl(struct bsdtar *bsdtar,
1069 struct archive_entry *entry, const char *accpath,
1070 int acl_type, int archive_entry_acl_type);
1073 setup_acls(struct bsdtar *bsdtar, struct archive_entry *entry,
1074 const char *accpath)
1076 archive_entry_acl_clear(entry);
1078 setup_acl(bsdtar, entry, accpath,
1079 ACL_TYPE_ACCESS, ARCHIVE_ENTRY_ACL_TYPE_ACCESS);
1080 /* Only directories can have default ACLs. */
1081 if (S_ISDIR(archive_entry_mode(entry)))
1082 setup_acl(bsdtar, entry, accpath,
1083 ACL_TYPE_DEFAULT, ARCHIVE_ENTRY_ACL_TYPE_DEFAULT);
1087 setup_acl(struct bsdtar *bsdtar, struct archive_entry *entry,
1088 const char *accpath, int acl_type, int archive_entry_acl_type)
1092 acl_entry_t acl_entry;
1093 acl_permset_t acl_permset;
1094 int s, ae_id, ae_tag, ae_perm;
1095 const char *ae_name;
1097 /* Retrieve access ACL from file. */
1098 acl = acl_get_file(accpath, acl_type);
1100 s = acl_get_entry(acl, ACL_FIRST_ENTRY, &acl_entry);
1105 acl_get_tag_type(acl_entry, &acl_tag);
1106 if (acl_tag == ACL_USER) {
1107 ae_id = (int)*(uid_t *)acl_get_qualifier(acl_entry);
1108 ae_name = lookup_uname(bsdtar, ae_id);
1109 ae_tag = ARCHIVE_ENTRY_ACL_USER;
1110 } else if (acl_tag == ACL_GROUP) {
1111 ae_id = (int)*(gid_t *)acl_get_qualifier(acl_entry);
1112 ae_name = lookup_gname(bsdtar, ae_id);
1113 ae_tag = ARCHIVE_ENTRY_ACL_GROUP;
1114 } else if (acl_tag == ACL_MASK) {
1115 ae_tag = ARCHIVE_ENTRY_ACL_MASK;
1116 } else if (acl_tag == ACL_USER_OBJ) {
1117 ae_tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
1118 } else if (acl_tag == ACL_GROUP_OBJ) {
1119 ae_tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
1120 } else if (acl_tag == ACL_OTHER) {
1121 ae_tag = ARCHIVE_ENTRY_ACL_OTHER;
1123 /* Skip types that libarchive can't support. */
1127 acl_get_permset(acl_entry, &acl_permset);
1130 * acl_get_perm() is spelled differently on different
1131 * platforms; see bsdtar_platform.h for details.
1133 if (ACL_GET_PERM(acl_permset, ACL_EXECUTE))
1134 ae_perm |= ARCHIVE_ENTRY_ACL_EXECUTE;
1135 if (ACL_GET_PERM(acl_permset, ACL_READ))
1136 ae_perm |= ARCHIVE_ENTRY_ACL_READ;
1137 if (ACL_GET_PERM(acl_permset, ACL_WRITE))
1138 ae_perm |= ARCHIVE_ENTRY_ACL_WRITE;
1140 archive_entry_acl_add_entry(entry,
1141 archive_entry_acl_type, ae_perm, ae_tag,
1144 s = acl_get_entry(acl, ACL_NEXT_ENTRY, &acl_entry);
1151 setup_acls(struct bsdtar *bsdtar, struct archive_entry *entry,
1152 const char *accpath)
1160 #if HAVE_LISTXATTR && HAVE_LLISTXATTR && HAVE_GETXATTR && HAVE_LGETXATTR
1163 setup_xattr(struct bsdtar *bsdtar, struct archive_entry *entry,
1164 const char *accpath, const char *name)
1168 char symlink_mode = bsdtar->symlink_mode;
1170 if (symlink_mode == 'H')
1171 size = getxattr(accpath, name, NULL, 0);
1173 size = lgetxattr(accpath, name, NULL, 0);
1176 bsdtar_warnc(bsdtar, errno, "Couldn't get extended attribute");
1180 if (size > 0 && (value = malloc(size)) == NULL) {
1181 bsdtar_errc(bsdtar, 1, errno, "Out of memory");
1185 if (symlink_mode == 'H')
1186 size = getxattr(accpath, name, value, size);
1188 size = lgetxattr(accpath, name, value, size);
1191 bsdtar_warnc(bsdtar, errno, "Couldn't get extended attribute");
1195 archive_entry_xattr_add_entry(entry, name, value, size);
1201 * Linux extended attribute support
1204 setup_xattrs(struct bsdtar *bsdtar, struct archive_entry *entry,
1205 const char *accpath)
1209 char symlink_mode = bsdtar->symlink_mode;
1211 if (symlink_mode == 'H')
1212 list_size = listxattr(accpath, NULL, 0);
1214 list_size = llistxattr(accpath, NULL, 0);
1216 if (list_size == -1) {
1217 bsdtar_warnc(bsdtar, errno,
1218 "Couldn't list extended attributes");
1220 } else if (list_size == 0)
1223 if ((list = malloc(list_size)) == NULL) {
1224 bsdtar_errc(bsdtar, 1, errno, "Out of memory");
1228 if (symlink_mode == 'H')
1229 list_size = listxattr(accpath, list, list_size);
1231 list_size = llistxattr(accpath, list, list_size);
1233 if (list_size == -1) {
1234 bsdtar_warnc(bsdtar, errno,
1235 "Couldn't list extended attributes");
1240 for (p = list; (p - list) < list_size; p += strlen(p) + 1) {
1241 if (strncmp(p, "system.", 7) == 0 ||
1242 strncmp(p, "xfsroot.", 8) == 0)
1245 setup_xattr(bsdtar, entry, accpath, p);
1254 * Generic (stub) extended attribute support.
1257 setup_xattrs(struct bsdtar *bsdtar, struct archive_entry *entry,
1258 const char *accpath)
1260 (void)bsdtar; /* UNUSED */
1261 (void)entry; /* UNUSED */
1262 (void)accpath; /* UNUSED */
1268 free_cache(struct name_cache *cache)
1272 if (cache != NULL) {
1273 for (i = 0; i < cache->size; i++) {
1274 if (cache->cache[i].name != NULL &&
1275 cache->cache[i].name != NO_NAME)
1276 free((void *)(uintptr_t)cache->cache[i].name);
1283 * Lookup uid/gid from uname/gname, return NULL if no match.
1286 lookup_name(struct bsdtar *bsdtar, struct name_cache **name_cache_variable,
1287 int (*lookup_fn)(struct bsdtar *, const char **, id_t), id_t id)
1289 struct name_cache *cache;
1294 if (*name_cache_variable == NULL) {
1295 *name_cache_variable = malloc(sizeof(struct name_cache));
1296 if (*name_cache_variable == NULL)
1297 bsdtar_errc(bsdtar, 1, ENOMEM, "No more memory");
1298 memset(*name_cache_variable, 0, sizeof(struct name_cache));
1299 (*name_cache_variable)->size = name_cache_size;
1302 cache = *name_cache_variable;
1305 slot = id % cache->size;
1306 if (cache->cache[slot].name != NULL) {
1307 if (cache->cache[slot].id == id) {
1309 if (cache->cache[slot].name == NO_NAME)
1311 return (cache->cache[slot].name);
1313 if (cache->cache[slot].name != NO_NAME)
1314 free((void *)(uintptr_t)cache->cache[slot].name);
1315 cache->cache[slot].name = NULL;
1318 if (lookup_fn(bsdtar, &name, id) == 0) {
1319 if (name == NULL || name[0] == '\0') {
1320 /* Cache the negative response. */
1321 cache->cache[slot].name = NO_NAME;
1322 cache->cache[slot].id = id;
1324 cache->cache[slot].name = strdup(name);
1325 if (cache->cache[slot].name != NULL) {
1326 cache->cache[slot].id = id;
1327 return (cache->cache[slot].name);
1330 * Conveniently, NULL marks an empty slot, so
1331 * if the strdup() fails, we've just failed to
1332 * cache it. No recovery necessary.
1340 lookup_uname(struct bsdtar *bsdtar, uid_t uid)
1342 return (lookup_name(bsdtar, &bsdtar->uname_cache,
1343 &lookup_uname_helper, (id_t)uid));
1347 lookup_uname_helper(struct bsdtar *bsdtar, const char **name, id_t id)
1349 struct passwd *pwent;
1351 (void)bsdtar; /* UNUSED */
1354 pwent = getpwuid((uid_t)id);
1355 if (pwent == NULL) {
1358 bsdtar_warnc(bsdtar, errno, "getpwuid(%d) failed", id);
1362 *name = pwent->pw_name;
1367 lookup_gname(struct bsdtar *bsdtar, gid_t gid)
1369 return (lookup_name(bsdtar, &bsdtar->gname_cache,
1370 &lookup_gname_helper, (id_t)gid));
1374 lookup_gname_helper(struct bsdtar *bsdtar, const char **name, id_t id)
1376 struct group *grent;
1378 (void)bsdtar; /* UNUSED */
1381 grent = getgrgid((gid_t)id);
1382 if (grent == NULL) {
1385 bsdtar_warnc(bsdtar, errno, "getgrgid(%d) failed", id);
1389 *name = grent->gr_name;
1394 * Test if the specified file is new enough to include in the archive.
1397 new_enough(struct bsdtar *bsdtar, const char *path, const struct stat *st)
1399 struct archive_dir_entry *p;
1402 * If this file/dir is excluded by a time comparison, skip it.
1404 if (bsdtar->newer_ctime_sec > 0) {
1405 if (st->st_ctime < bsdtar->newer_ctime_sec)
1406 return (0); /* Too old, skip it. */
1407 if (st->st_ctime == bsdtar->newer_ctime_sec
1408 && ARCHIVE_STAT_CTIME_NANOS(st)
1409 <= bsdtar->newer_ctime_nsec)
1410 return (0); /* Too old, skip it. */
1412 if (bsdtar->newer_mtime_sec > 0) {
1413 if (st->st_mtime < bsdtar->newer_mtime_sec)
1414 return (0); /* Too old, skip it. */
1415 if (st->st_mtime == bsdtar->newer_mtime_sec
1416 && ARCHIVE_STAT_MTIME_NANOS(st)
1417 <= bsdtar->newer_mtime_nsec)
1418 return (0); /* Too old, skip it. */
1422 * In -u mode, we only write an entry if it's newer than
1423 * what was already in the archive.
1425 if (bsdtar->archive_dir != NULL &&
1426 bsdtar->archive_dir->head != NULL) {
1427 for (p = bsdtar->archive_dir->head; p != NULL; p = p->next) {
1428 if (pathcmp(path, p->name)==0)
1429 return (p->mtime_sec < st->st_mtime ||
1430 (p->mtime_sec == st->st_mtime &&
1432 < ARCHIVE_STAT_MTIME_NANOS(st)));
1436 /* If the file wasn't rejected, include it. */
1441 * Add an entry to the dir list for 'u' mode.
1443 * XXX TODO: Make this fast.
1446 add_dir_list(struct bsdtar *bsdtar, const char *path,
1447 time_t mtime_sec, int mtime_nsec)
1449 struct archive_dir_entry *p;
1452 * Search entire list to see if this file has appeared before.
1453 * If it has, override the timestamp data.
1455 p = bsdtar->archive_dir->head;
1457 if (strcmp(path, p->name)==0) {
1458 p->mtime_sec = mtime_sec;
1459 p->mtime_nsec = mtime_nsec;
1465 p = malloc(sizeof(*p));
1467 bsdtar_errc(bsdtar, 1, ENOMEM, "Can't read archive directory");
1469 p->name = strdup(path);
1470 if (p->name == NULL)
1471 bsdtar_errc(bsdtar, 1, ENOMEM, "Can't read archive directory");
1472 p->mtime_sec = mtime_sec;
1473 p->mtime_nsec = mtime_nsec;
1475 if (bsdtar->archive_dir->tail == NULL) {
1476 bsdtar->archive_dir->head = bsdtar->archive_dir->tail = p;
1478 bsdtar->archive_dir->tail->next = p;
1479 bsdtar->archive_dir->tail = p;
1484 test_for_append(struct bsdtar *bsdtar)
1488 if (*bsdtar->argv == NULL)
1489 bsdtar_errc(bsdtar, 1, 0, "no files or directories specified");
1490 if (bsdtar->filename == NULL)
1491 bsdtar_errc(bsdtar, 1, 0, "Cannot append to stdout.");
1493 if (bsdtar->create_compression != 0)
1494 bsdtar_errc(bsdtar, 1, 0,
1495 "Cannot append to %s with compression", bsdtar->filename);
1497 if (stat(bsdtar->filename, &s) != 0)
1500 if (!S_ISREG(s.st_mode))
1501 bsdtar_errc(bsdtar, 1, 0,
1502 "Cannot append to %s: not a regular file.",