2 * Copyright (c) 2003-2009 Tim Kientzle
3 * Copyright (c) 2010-2012 Michihiro NAKAJIMA
4 * Copyright (c) 2016 Martin Matuska
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "archive_platform.h"
29 __FBSDID("$FreeBSD$");
31 /* This is the tree-walking code for POSIX systems. */
32 #if !defined(_WIN32) || defined(__CYGWIN__)
34 #ifdef HAVE_SYS_TYPES_H
35 /* Mac OSX requires sys/types.h before sys/acl.h. */
36 #include <sys/types.h>
41 #ifdef HAVE_SYS_EXTATTR_H
42 #include <sys/extattr.h>
44 #ifdef HAVE_SYS_IOCTL_H
45 #include <sys/ioctl.h>
47 #ifdef HAVE_SYS_PARAM_H
48 #include <sys/param.h>
50 #ifdef HAVE_SYS_STAT_H
53 #if defined(HAVE_SYS_XATTR_H)
54 #include <sys/xattr.h>
55 #elif defined(HAVE_ATTR_XATTR_H)
56 #include <attr/xattr.h>
61 #ifdef HAVE_ACL_LIBACL_H
62 #include <acl/libacl.h>
64 #ifdef HAVE_COPYFILE_H
76 #ifdef HAVE_LINUX_TYPES_H
77 #include <linux/types.h>
79 #ifdef HAVE_LINUX_FIEMAP_H
80 #include <linux/fiemap.h>
82 #ifdef HAVE_LINUX_FS_H
86 * Some Linux distributions have both linux/ext2_fs.h and ext2fs/ext2_fs.h.
87 * As the include guards don't agree, the order of include is important.
89 #ifdef HAVE_LINUX_EXT2_FS_H
90 #include <linux/ext2_fs.h> /* for Linux file flags */
92 #if defined(HAVE_EXT2FS_EXT2_FS_H) && !defined(__CYGWIN__)
93 #include <ext2fs/ext2_fs.h> /* Linux file flags, broken on Cygwin */
103 #include "archive_entry.h"
104 #include "archive_private.h"
105 #include "archive_read_disk_private.h"
112 * Linux and FreeBSD plug this obvious hole in POSIX.1e in
115 #if HAVE_ACL_GET_PERM
116 #define ACL_GET_PERM acl_get_perm
117 #elif HAVE_ACL_GET_PERM_NP
118 #define ACL_GET_PERM acl_get_perm_np
121 static int setup_acls(struct archive_read_disk *,
122 struct archive_entry *, int *fd);
123 static int setup_mac_metadata(struct archive_read_disk *,
124 struct archive_entry *, int *fd);
125 static int setup_xattrs(struct archive_read_disk *,
126 struct archive_entry *, int *fd);
127 static int setup_sparse(struct archive_read_disk *,
128 struct archive_entry *, int *fd);
129 #if defined(HAVE_LINUX_FIEMAP_H)
130 static int setup_sparse_fiemap(struct archive_read_disk *,
131 struct archive_entry *, int *fd);
135 archive_read_disk_entry_from_file(struct archive *_a,
136 struct archive_entry *entry,
138 const struct stat *st)
140 struct archive_read_disk *a = (struct archive_read_disk *)_a;
141 const char *path, *name;
146 archive_clear_error(_a);
147 path = archive_entry_sourcepath(entry);
149 path = archive_entry_pathname(entry);
151 if (a->tree == NULL) {
155 if (fstat(fd, &s) != 0) {
156 archive_set_error(&a->archive, errno,
158 return (ARCHIVE_FAILED);
163 if (!a->follow_symlinks) {
164 if (lstat(path, &s) != 0) {
165 archive_set_error(&a->archive, errno,
166 "Can't lstat %s", path);
167 return (ARCHIVE_FAILED);
171 if (stat(path, &s) != 0) {
172 archive_set_error(&a->archive, errno,
173 "Can't stat %s", path);
174 return (ARCHIVE_FAILED);
178 archive_entry_copy_stat(entry, st);
181 /* Lookup uname/gname */
182 name = archive_read_disk_uname(_a, archive_entry_uid(entry));
184 archive_entry_copy_uname(entry, name);
185 name = archive_read_disk_gname(_a, archive_entry_gid(entry));
187 archive_entry_copy_gname(entry, name);
189 #ifdef HAVE_STRUCT_STAT_ST_FLAGS
190 /* On FreeBSD, we get flags for free with the stat. */
191 /* TODO: Does this belong in copy_stat()? */
192 if (st->st_flags != 0)
193 archive_entry_set_fflags(entry, st->st_flags, 0);
196 #if defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS)
197 /* Linux requires an extra ioctl to pull the flags. Although
198 * this is an extra step, it has a nice side-effect: We get an
199 * open file descriptor which we can use in the subsequent lookups. */
200 if ((S_ISREG(st->st_mode) || S_ISDIR(st->st_mode))) {
203 fd = a->open_on_current_dir(a->tree, path,
204 O_RDONLY | O_NONBLOCK | O_CLOEXEC);
206 fd = open(path, O_RDONLY | O_NONBLOCK |
208 __archive_ensure_cloexec_flag(fd);
212 r = ioctl(fd, EXT2_IOC_GETFLAGS, &stflags);
213 if (r == 0 && stflags != 0)
214 archive_entry_set_fflags(entry, stflags, 0);
219 #if defined(HAVE_READLINK) || defined(HAVE_READLINKAT)
220 if (S_ISLNK(st->st_mode)) {
221 size_t linkbuffer_len = st->st_size + 1;
225 linkbuffer = malloc(linkbuffer_len);
226 if (linkbuffer == NULL) {
227 archive_set_error(&a->archive, ENOMEM,
228 "Couldn't read link data");
229 return (ARCHIVE_FAILED);
231 if (a->tree != NULL) {
232 #ifdef HAVE_READLINKAT
233 lnklen = readlinkat(a->tree_current_dir_fd(a->tree),
234 path, linkbuffer, linkbuffer_len);
236 if (a->tree_enter_working_dir(a->tree) != 0) {
237 archive_set_error(&a->archive, errno,
238 "Couldn't read link data");
240 return (ARCHIVE_FAILED);
242 lnklen = readlink(path, linkbuffer, linkbuffer_len);
243 #endif /* HAVE_READLINKAT */
245 lnklen = readlink(path, linkbuffer, linkbuffer_len);
247 archive_set_error(&a->archive, errno,
248 "Couldn't read link data");
250 return (ARCHIVE_FAILED);
252 linkbuffer[lnklen] = 0;
253 archive_entry_set_symlink(entry, linkbuffer);
256 #endif /* HAVE_READLINK || HAVE_READLINKAT */
258 r = setup_acls(a, entry, &fd);
259 if (!a->suppress_xattr) {
260 r1 = setup_xattrs(a, entry, &fd);
264 if (a->enable_copyfile) {
265 r1 = setup_mac_metadata(a, entry, &fd);
269 r1 = setup_sparse(a, entry, &fd);
273 /* If we opened the file earlier in this function, close it. */
274 if (initial_fd != fd)
279 #if defined(__APPLE__) && defined(HAVE_COPYFILE_H)
281 * The Mac OS "copyfile()" API copies the extended metadata for a
282 * file into a separate file in AppleDouble format (see RFC 1740).
284 * Mac OS tar and cpio implementations store this extended
285 * metadata as a separate entry just before the regular entry
286 * with a "._" prefix added to the filename.
288 * Note that this is currently done unconditionally; the tar program has
289 * an option to discard this information before the archive is written.
291 * TODO: If there's a failure, report it and return ARCHIVE_WARN.
294 setup_mac_metadata(struct archive_read_disk *a,
295 struct archive_entry *entry, int *fd)
298 int copyfile_flags = COPYFILE_NOFOLLOW | COPYFILE_ACL | COPYFILE_XATTR;
299 struct stat copyfile_stat;
300 int ret = ARCHIVE_OK;
303 const char *name, *tempdir;
304 struct archive_string tempfile;
306 (void)fd; /* UNUSED */
307 name = archive_entry_sourcepath(entry);
309 name = archive_entry_pathname(entry);
311 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
312 "Can't open file to read extended attributes: No name");
313 return (ARCHIVE_WARN);
316 if (a->tree != NULL) {
317 if (a->tree_enter_working_dir(a->tree) != 0) {
318 archive_set_error(&a->archive, errno,
319 "Couldn't change dir");
320 return (ARCHIVE_FAILED);
324 /* Short-circuit if there's nothing to do. */
325 have_attrs = copyfile(name, NULL, 0, copyfile_flags | COPYFILE_CHECK);
326 if (have_attrs == -1) {
327 archive_set_error(&a->archive, errno,
328 "Could not check extended attributes");
329 return (ARCHIVE_WARN);
335 if (issetugid() == 0)
336 tempdir = getenv("TMPDIR");
339 archive_string_init(&tempfile);
340 archive_strcpy(&tempfile, tempdir);
341 archive_strcat(&tempfile, "tar.md.XXXXXX");
342 tempfd = mkstemp(tempfile.s);
344 archive_set_error(&a->archive, errno,
345 "Could not open extended attribute file");
349 __archive_ensure_cloexec_flag(tempfd);
351 /* XXX I wish copyfile() could pack directly to a memory
352 * buffer; that would avoid the temp file here. For that
353 * matter, it would be nice if fcopyfile() actually worked,
354 * that would reduce the many open/close races here. */
355 if (copyfile(name, tempfile.s, 0, copyfile_flags | COPYFILE_PACK)) {
356 archive_set_error(&a->archive, errno,
357 "Could not pack extended attributes");
361 if (fstat(tempfd, ©file_stat)) {
362 archive_set_error(&a->archive, errno,
363 "Could not check size of extended attributes");
367 buff = malloc(copyfile_stat.st_size);
369 archive_set_error(&a->archive, errno,
370 "Could not allocate memory for extended attributes");
374 if (copyfile_stat.st_size != read(tempfd, buff, copyfile_stat.st_size)) {
375 archive_set_error(&a->archive, errno,
376 "Could not read extended attributes into memory");
380 archive_entry_copy_mac_metadata(entry, buff, copyfile_stat.st_size);
387 archive_string_free(&tempfile);
395 * Stub implementation for non-Mac systems.
398 setup_mac_metadata(struct archive_read_disk *a,
399 struct archive_entry *entry, int *fd)
401 (void)a; /* UNUSED */
402 (void)entry; /* UNUSED */
403 (void)fd; /* UNUSED */
409 #ifdef HAVE_POSIX_ACL
410 static int translate_acl(struct archive_read_disk *a,
411 struct archive_entry *entry, acl_t acl, int archive_entry_acl_type);
414 setup_acls(struct archive_read_disk *a,
415 struct archive_entry *entry, int *fd)
421 accpath = archive_entry_sourcepath(entry);
423 accpath = archive_entry_pathname(entry);
425 if (*fd < 0 && a->tree != NULL) {
426 if (a->follow_symlinks ||
427 archive_entry_filetype(entry) != AE_IFLNK)
428 *fd = a->open_on_current_dir(a->tree,
429 accpath, O_RDONLY | O_NONBLOCK);
431 if (a->tree_enter_working_dir(a->tree) != 0) {
432 archive_set_error(&a->archive, errno,
433 "Couldn't access %s", accpath);
434 return (ARCHIVE_FAILED);
439 archive_entry_acl_clear(entry);
444 /* Try NFSv4 ACL first. */
446 #if HAVE_ACL_GET_FD_NP
447 acl = acl_get_fd_np(*fd, ACL_TYPE_NFS4);
449 acl = acl_get_fd(*fd);
451 #if HAVE_ACL_GET_LINK_NP
452 else if (!a->follow_symlinks)
453 acl = acl_get_link_np(accpath, ACL_TYPE_NFS4);
455 else if ((!a->follow_symlinks)
456 && (archive_entry_filetype(entry) == AE_IFLNK))
457 /* We can't get the ACL of a symlink, so we assume it can't
462 acl = acl_get_file(accpath, ACL_TYPE_NFS4);
464 #if HAVE_ACL_IS_TRIVIAL_NP
465 if (acl != NULL && acl_is_trivial_np(acl, &r) == 0) {
466 /* Ignore "trivial" ACLs that just mirror the file mode. */
471 * Simultaneous NFSv4 and POSIX.1e ACLs for the same
472 * entry are not allowed, so we should return here
479 r = translate_acl(a, entry, acl, ARCHIVE_ENTRY_ACL_TYPE_NFS4);
481 if (r != ARCHIVE_OK) {
482 archive_set_error(&a->archive, errno,
483 "Couldn't translate NFSv4 ACLs: %s", accpath);
487 #endif /* ACL_TYPE_NFS4 */
489 /* Retrieve access ACL from file. */
491 acl = acl_get_fd(*fd);
492 #if HAVE_ACL_GET_LINK_NP
493 else if (!a->follow_symlinks)
494 acl = acl_get_link_np(accpath, ACL_TYPE_ACCESS);
496 else if ((!a->follow_symlinks)
497 && (archive_entry_filetype(entry) == AE_IFLNK))
498 /* We can't get the ACL of a symlink, so we assume it can't
503 acl = acl_get_file(accpath, ACL_TYPE_ACCESS);
505 #if HAVE_ACL_IS_TRIVIAL_NP
506 /* Ignore "trivial" ACLs that just mirror the file mode. */
507 if (acl != NULL && acl_is_trivial_np(acl, &r) == 0) {
516 r = translate_acl(a, entry, acl,
517 ARCHIVE_ENTRY_ACL_TYPE_ACCESS);
520 if (r != ARCHIVE_OK) {
521 archive_set_error(&a->archive, errno,
522 "Couldn't translate access ACLs: %s", accpath);
527 /* Only directories can have default ACLs. */
528 if (S_ISDIR(archive_entry_mode(entry))) {
529 #if HAVE_ACL_GET_FD_NP
531 acl = acl_get_fd_np(*fd, ACL_TYPE_DEFAULT);
534 acl = acl_get_file(accpath, ACL_TYPE_DEFAULT);
536 r = translate_acl(a, entry, acl,
537 ARCHIVE_ENTRY_ACL_TYPE_DEFAULT);
539 if (r != ARCHIVE_OK) {
540 archive_set_error(&a->archive, errno,
541 "Couldn't translate default ACLs: %s",
551 * Translate system ACL into libarchive internal structure.
558 {ARCHIVE_ENTRY_ACL_EXECUTE, ACL_EXECUTE},
559 {ARCHIVE_ENTRY_ACL_WRITE, ACL_WRITE},
560 {ARCHIVE_ENTRY_ACL_READ, ACL_READ},
562 {ARCHIVE_ENTRY_ACL_READ_DATA, ACL_READ_DATA},
563 {ARCHIVE_ENTRY_ACL_LIST_DIRECTORY, ACL_LIST_DIRECTORY},
564 {ARCHIVE_ENTRY_ACL_WRITE_DATA, ACL_WRITE_DATA},
565 {ARCHIVE_ENTRY_ACL_ADD_FILE, ACL_ADD_FILE},
566 {ARCHIVE_ENTRY_ACL_APPEND_DATA, ACL_APPEND_DATA},
567 {ARCHIVE_ENTRY_ACL_ADD_SUBDIRECTORY, ACL_ADD_SUBDIRECTORY},
568 {ARCHIVE_ENTRY_ACL_READ_NAMED_ATTRS, ACL_READ_NAMED_ATTRS},
569 {ARCHIVE_ENTRY_ACL_WRITE_NAMED_ATTRS, ACL_WRITE_NAMED_ATTRS},
570 {ARCHIVE_ENTRY_ACL_DELETE_CHILD, ACL_DELETE_CHILD},
571 {ARCHIVE_ENTRY_ACL_READ_ATTRIBUTES, ACL_READ_ATTRIBUTES},
572 {ARCHIVE_ENTRY_ACL_WRITE_ATTRIBUTES, ACL_WRITE_ATTRIBUTES},
573 {ARCHIVE_ENTRY_ACL_DELETE, ACL_DELETE},
574 {ARCHIVE_ENTRY_ACL_READ_ACL, ACL_READ_ACL},
575 {ARCHIVE_ENTRY_ACL_WRITE_ACL, ACL_WRITE_ACL},
576 {ARCHIVE_ENTRY_ACL_WRITE_OWNER, ACL_WRITE_OWNER},
577 {ARCHIVE_ENTRY_ACL_SYNCHRONIZE, ACL_SYNCHRONIZE}
584 int platform_inherit;
585 } acl_inherit_map[] = {
586 {ARCHIVE_ENTRY_ACL_ENTRY_FILE_INHERIT, ACL_ENTRY_FILE_INHERIT},
587 {ARCHIVE_ENTRY_ACL_ENTRY_DIRECTORY_INHERIT, ACL_ENTRY_DIRECTORY_INHERIT},
588 {ARCHIVE_ENTRY_ACL_ENTRY_NO_PROPAGATE_INHERIT, ACL_ENTRY_NO_PROPAGATE_INHERIT},
589 {ARCHIVE_ENTRY_ACL_ENTRY_INHERIT_ONLY, ACL_ENTRY_INHERIT_ONLY},
590 {ARCHIVE_ENTRY_ACL_ENTRY_SUCCESSFUL_ACCESS, ACL_ENTRY_SUCCESSFUL_ACCESS},
591 {ARCHIVE_ENTRY_ACL_ENTRY_FAILED_ACCESS, ACL_ENTRY_FAILED_ACCESS},
592 {ARCHIVE_ENTRY_ACL_ENTRY_INHERITED, ACL_ENTRY_INHERITED}
596 translate_acl(struct archive_read_disk *a,
597 struct archive_entry *entry, acl_t acl, int default_entry_acl_type)
601 acl_entry_type_t acl_type;
602 acl_flagset_t acl_flagset;
605 acl_entry_t acl_entry;
606 acl_permset_t acl_permset;
607 int i, entry_acl_type;
608 int r, s, ae_id, ae_tag, ae_perm;
612 // FreeBSD "brands" ACLs as POSIX.1e or NFSv4
613 // Make sure the "brand" on this ACL is consistent
614 // with the default_entry_acl_type bits provided.
615 if (acl_get_brand_np(acl, &brand) != 0) {
616 archive_set_error(&a->archive, errno,
617 "Failed to read ACL brand");
618 return (ARCHIVE_WARN);
621 case ACL_BRAND_POSIX:
622 switch (default_entry_acl_type) {
623 case ARCHIVE_ENTRY_ACL_TYPE_ACCESS:
624 case ARCHIVE_ENTRY_ACL_TYPE_DEFAULT:
627 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
628 "Invalid ACL entry type for POSIX.1e ACL");
629 return (ARCHIVE_WARN);
633 if (default_entry_acl_type & ~ARCHIVE_ENTRY_ACL_TYPE_NFS4) {
634 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
635 "Invalid ACL entry type for NFSv4 ACL");
636 return (ARCHIVE_WARN);
640 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
641 "Unknown ACL brand");
642 return (ARCHIVE_WARN);
647 s = acl_get_entry(acl, ACL_FIRST_ENTRY, &acl_entry);
649 archive_set_error(&a->archive, errno,
650 "Failed to get first ACL entry");
651 return (ARCHIVE_WARN);
658 if (acl_get_tag_type(acl_entry, &acl_tag) != 0) {
659 archive_set_error(&a->archive, errno,
660 "Failed to get ACL tag type");
661 return (ARCHIVE_WARN);
665 ae_id = (int)*(uid_t *)acl_get_qualifier(acl_entry);
666 ae_name = archive_read_disk_uname(&a->archive, ae_id);
667 ae_tag = ARCHIVE_ENTRY_ACL_USER;
670 ae_id = (int)*(gid_t *)acl_get_qualifier(acl_entry);
671 ae_name = archive_read_disk_gname(&a->archive, ae_id);
672 ae_tag = ARCHIVE_ENTRY_ACL_GROUP;
675 ae_tag = ARCHIVE_ENTRY_ACL_MASK;
678 ae_tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
681 ae_tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
684 ae_tag = ARCHIVE_ENTRY_ACL_OTHER;
688 ae_tag = ARCHIVE_ENTRY_ACL_EVERYONE;
692 /* Skip types that libarchive can't support. */
693 s = acl_get_entry(acl, ACL_NEXT_ENTRY, &acl_entry);
697 // XXX acl_type maps to allow/deny/audit/YYYY bits
698 entry_acl_type = default_entry_acl_type;
700 if (default_entry_acl_type & ARCHIVE_ENTRY_ACL_TYPE_NFS4) {
702 * acl_get_entry_type_np() fails with non-NFSv4 ACLs
704 if (acl_get_entry_type_np(acl_entry, &acl_type) != 0) {
705 archive_set_error(&a->archive, errno, "Failed "
706 "to get ACL type from a NFSv4 ACL entry");
707 return (ARCHIVE_WARN);
710 case ACL_ENTRY_TYPE_ALLOW:
711 entry_acl_type = ARCHIVE_ENTRY_ACL_TYPE_ALLOW;
713 case ACL_ENTRY_TYPE_DENY:
714 entry_acl_type = ARCHIVE_ENTRY_ACL_TYPE_DENY;
716 case ACL_ENTRY_TYPE_AUDIT:
717 entry_acl_type = ARCHIVE_ENTRY_ACL_TYPE_AUDIT;
719 case ACL_ENTRY_TYPE_ALARM:
720 entry_acl_type = ARCHIVE_ENTRY_ACL_TYPE_ALARM;
723 archive_set_error(&a->archive, errno,
724 "Invalid NFSv4 ACL entry type");
725 return (ARCHIVE_WARN);
729 * Libarchive stores "flag" (NFSv4 inheritance bits)
730 * in the ae_perm bitmap.
732 * acl_get_flagset_np() fails with non-NFSv4 ACLs
734 if (acl_get_flagset_np(acl_entry, &acl_flagset) != 0) {
735 archive_set_error(&a->archive, errno,
736 "Failed to get flagset from a NFSv4 ACL entry");
737 return (ARCHIVE_WARN);
739 for (i = 0; i < (int)(sizeof(acl_inherit_map) / sizeof(acl_inherit_map[0])); ++i) {
740 r = acl_get_flag_np(acl_flagset,
741 acl_inherit_map[i].platform_inherit);
743 archive_set_error(&a->archive, errno,
744 "Failed to check flag in a NFSv4 "
746 return (ARCHIVE_WARN);
748 ae_perm |= acl_inherit_map[i].archive_inherit;
753 if (acl_get_permset(acl_entry, &acl_permset) != 0) {
754 archive_set_error(&a->archive, errno,
755 "Failed to get ACL permission set");
756 return (ARCHIVE_WARN);
758 for (i = 0; i < (int)(sizeof(acl_perm_map) / sizeof(acl_perm_map[0])); ++i) {
760 * acl_get_perm() is spelled differently on different
761 * platforms; see above.
763 r = ACL_GET_PERM(acl_permset, acl_perm_map[i].platform_perm);
765 archive_set_error(&a->archive, errno,
766 "Failed to check permission in an ACL permission set");
767 return (ARCHIVE_WARN);
769 ae_perm |= acl_perm_map[i].archive_perm;
772 archive_entry_acl_add_entry(entry, entry_acl_type,
776 s = acl_get_entry(acl, ACL_NEXT_ENTRY, &acl_entry);
778 archive_set_error(&a->archive, errno,
779 "Failed to get next ACL entry");
780 return (ARCHIVE_WARN);
787 setup_acls(struct archive_read_disk *a,
788 struct archive_entry *entry, int *fd)
790 (void)a; /* UNUSED */
791 (void)entry; /* UNUSED */
792 (void)fd; /* UNUSED */
797 #if (HAVE_FGETXATTR && HAVE_FLISTXATTR && HAVE_LISTXATTR && \
798 HAVE_LLISTXATTR && HAVE_GETXATTR && HAVE_LGETXATTR) || \
799 (HAVE_FGETEA && HAVE_FLISTEA && HAVE_LISTEA)
802 * Linux and AIX extended attribute support.
804 * TODO: By using a stack-allocated buffer for the first
805 * call to getxattr(), we might be able to avoid the second
806 * call entirely. We only need the second call if the
807 * stack-allocated buffer is too small. But a modest buffer
808 * of 1024 bytes or so will often be big enough. Same applies
814 setup_xattr(struct archive_read_disk *a,
815 struct archive_entry *entry, const char *name, int fd)
821 accpath = archive_entry_sourcepath(entry);
823 accpath = archive_entry_pathname(entry);
827 size = fgetxattr(fd, name, NULL, 0);
828 else if (!a->follow_symlinks)
829 size = lgetxattr(accpath, name, NULL, 0);
831 size = getxattr(accpath, name, NULL, 0);
834 size = fgetea(fd, name, NULL, 0);
835 else if (!a->follow_symlinks)
836 size = lgetea(accpath, name, NULL, 0);
838 size = getea(accpath, name, NULL, 0);
842 archive_set_error(&a->archive, errno,
843 "Couldn't query extended attribute");
844 return (ARCHIVE_WARN);
847 if (size > 0 && (value = malloc(size)) == NULL) {
848 archive_set_error(&a->archive, errno, "Out of memory");
849 return (ARCHIVE_FATAL);
854 size = fgetxattr(fd, name, value, size);
855 else if (!a->follow_symlinks)
856 size = lgetxattr(accpath, name, value, size);
858 size = getxattr(accpath, name, value, size);
861 size = fgetea(fd, name, value, size);
862 else if (!a->follow_symlinks)
863 size = lgetea(accpath, name, value, size);
865 size = getea(accpath, name, value, size);
869 archive_set_error(&a->archive, errno,
870 "Couldn't read extended attribute");
871 return (ARCHIVE_WARN);
874 archive_entry_xattr_add_entry(entry, name, value, size);
881 setup_xattrs(struct archive_read_disk *a,
882 struct archive_entry *entry, int *fd)
888 path = archive_entry_sourcepath(entry);
890 path = archive_entry_pathname(entry);
892 if (*fd < 0 && a->tree != NULL) {
893 if (a->follow_symlinks ||
894 archive_entry_filetype(entry) != AE_IFLNK)
895 *fd = a->open_on_current_dir(a->tree, path,
896 O_RDONLY | O_NONBLOCK);
898 if (a->tree_enter_working_dir(a->tree) != 0) {
899 archive_set_error(&a->archive, errno,
900 "Couldn't access %s", path);
901 return (ARCHIVE_FAILED);
908 list_size = flistxattr(*fd, NULL, 0);
909 else if (!a->follow_symlinks)
910 list_size = llistxattr(path, NULL, 0);
912 list_size = listxattr(path, NULL, 0);
915 list_size = flistea(*fd, NULL, 0);
916 else if (!a->follow_symlinks)
917 list_size = llistea(path, NULL, 0);
919 list_size = listea(path, NULL, 0);
922 if (list_size == -1) {
923 if (errno == ENOTSUP || errno == ENOSYS)
925 archive_set_error(&a->archive, errno,
926 "Couldn't list extended attributes");
927 return (ARCHIVE_WARN);
933 if ((list = malloc(list_size)) == NULL) {
934 archive_set_error(&a->archive, errno, "Out of memory");
935 return (ARCHIVE_FATAL);
940 list_size = flistxattr(*fd, list, list_size);
941 else if (!a->follow_symlinks)
942 list_size = llistxattr(path, list, list_size);
944 list_size = listxattr(path, list, list_size);
947 list_size = flistea(*fd, list, list_size);
948 else if (!a->follow_symlinks)
949 list_size = llistea(path, list, list_size);
951 list_size = listea(path, list, list_size);
954 if (list_size == -1) {
955 archive_set_error(&a->archive, errno,
956 "Couldn't retrieve extended attributes");
958 return (ARCHIVE_WARN);
961 for (p = list; (p - list) < list_size; p += strlen(p) + 1) {
962 if (strncmp(p, "system.", 7) == 0 ||
963 strncmp(p, "xfsroot.", 8) == 0)
965 setup_xattr(a, entry, p, *fd);
972 #elif HAVE_EXTATTR_GET_FILE && HAVE_EXTATTR_LIST_FILE && \
973 HAVE_DECL_EXTATTR_NAMESPACE_USER
976 * FreeBSD extattr interface.
979 /* TODO: Implement this. Follow the Linux model above, but
980 * with FreeBSD-specific system calls, of course. Be careful
981 * to not include the system extattrs that hold ACLs; we handle
985 setup_xattr(struct archive_read_disk *a, struct archive_entry *entry,
986 int namespace, const char *name, const char *fullname, int fd);
989 setup_xattr(struct archive_read_disk *a, struct archive_entry *entry,
990 int namespace, const char *name, const char *fullname, int fd)
996 accpath = archive_entry_sourcepath(entry);
998 accpath = archive_entry_pathname(entry);
1001 size = extattr_get_fd(fd, namespace, name, NULL, 0);
1002 else if (!a->follow_symlinks)
1003 size = extattr_get_link(accpath, namespace, name, NULL, 0);
1005 size = extattr_get_file(accpath, namespace, name, NULL, 0);
1008 archive_set_error(&a->archive, errno,
1009 "Couldn't query extended attribute");
1010 return (ARCHIVE_WARN);
1013 if (size > 0 && (value = malloc(size)) == NULL) {
1014 archive_set_error(&a->archive, errno, "Out of memory");
1015 return (ARCHIVE_FATAL);
1019 size = extattr_get_fd(fd, namespace, name, value, size);
1020 else if (!a->follow_symlinks)
1021 size = extattr_get_link(accpath, namespace, name, value, size);
1023 size = extattr_get_file(accpath, namespace, name, value, size);
1027 archive_set_error(&a->archive, errno,
1028 "Couldn't read extended attribute");
1029 return (ARCHIVE_WARN);
1032 archive_entry_xattr_add_entry(entry, fullname, value, size);
1035 return (ARCHIVE_OK);
1039 setup_xattrs(struct archive_read_disk *a,
1040 struct archive_entry *entry, int *fd)
1046 int namespace = EXTATTR_NAMESPACE_USER;
1048 path = archive_entry_sourcepath(entry);
1050 path = archive_entry_pathname(entry);
1052 if (*fd < 0 && a->tree != NULL) {
1053 if (a->follow_symlinks ||
1054 archive_entry_filetype(entry) != AE_IFLNK)
1055 *fd = a->open_on_current_dir(a->tree, path,
1056 O_RDONLY | O_NONBLOCK);
1058 if (a->tree_enter_working_dir(a->tree) != 0) {
1059 archive_set_error(&a->archive, errno,
1060 "Couldn't access %s", path);
1061 return (ARCHIVE_FAILED);
1067 list_size = extattr_list_fd(*fd, namespace, NULL, 0);
1068 else if (!a->follow_symlinks)
1069 list_size = extattr_list_link(path, namespace, NULL, 0);
1071 list_size = extattr_list_file(path, namespace, NULL, 0);
1073 if (list_size == -1 && errno == EOPNOTSUPP)
1074 return (ARCHIVE_OK);
1075 if (list_size == -1) {
1076 archive_set_error(&a->archive, errno,
1077 "Couldn't list extended attributes");
1078 return (ARCHIVE_WARN);
1082 return (ARCHIVE_OK);
1084 if ((list = malloc(list_size)) == NULL) {
1085 archive_set_error(&a->archive, errno, "Out of memory");
1086 return (ARCHIVE_FATAL);
1090 list_size = extattr_list_fd(*fd, namespace, list, list_size);
1091 else if (!a->follow_symlinks)
1092 list_size = extattr_list_link(path, namespace, list, list_size);
1094 list_size = extattr_list_file(path, namespace, list, list_size);
1096 if (list_size == -1) {
1097 archive_set_error(&a->archive, errno,
1098 "Couldn't retrieve extended attributes");
1100 return (ARCHIVE_WARN);
1104 while ((p - list) < list_size) {
1105 size_t len = 255 & (int)*p;
1108 strcpy(buff, "user.");
1109 name = buff + strlen(buff);
1110 memcpy(name, p + 1, len);
1112 setup_xattr(a, entry, namespace, name, buff, *fd);
1117 return (ARCHIVE_OK);
1123 * Generic (stub) extended attribute support.
1126 setup_xattrs(struct archive_read_disk *a,
1127 struct archive_entry *entry, int *fd)
1129 (void)a; /* UNUSED */
1130 (void)entry; /* UNUSED */
1131 (void)fd; /* UNUSED */
1132 return (ARCHIVE_OK);
1137 #if defined(HAVE_LINUX_FIEMAP_H)
1140 * Linux FIEMAP sparse interface.
1142 * The FIEMAP ioctl returns an "extent" for each physical allocation
1143 * on disk. We need to process those to generate a more compact list
1144 * of logical file blocks. We also need to be very careful to use
1145 * FIEMAP_FLAG_SYNC here, since there are reports that Linux sometimes
1146 * does not report allocations for newly-written data that hasn't
1147 * been synced to disk.
1149 * It's important to return a minimal sparse file list because we want
1150 * to not trigger sparse file extensions if we don't have to, since
1151 * not all readers support them.
1155 setup_sparse_fiemap(struct archive_read_disk *a,
1156 struct archive_entry *entry, int *fd)
1160 struct fiemap_extent *fe;
1162 int count, do_fiemap, iters;
1163 int exit_sts = ARCHIVE_OK;
1165 if (archive_entry_filetype(entry) != AE_IFREG
1166 || archive_entry_size(entry) <= 0
1167 || archive_entry_hardlink(entry) != NULL)
1168 return (ARCHIVE_OK);
1173 path = archive_entry_sourcepath(entry);
1175 path = archive_entry_pathname(entry);
1176 if (a->tree != NULL)
1177 *fd = a->open_on_current_dir(a->tree, path,
1178 O_RDONLY | O_NONBLOCK | O_CLOEXEC);
1180 *fd = open(path, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
1182 archive_set_error(&a->archive, errno,
1183 "Can't open `%s'", path);
1184 return (ARCHIVE_FAILED);
1186 __archive_ensure_cloexec_flag(*fd);
1189 /* Initialize buffer to avoid the error valgrind complains about. */
1190 memset(buff, 0, sizeof(buff));
1191 count = (sizeof(buff) - sizeof(*fm))/sizeof(*fe);
1192 fm = (struct fiemap *)buff;
1194 fm->fm_length = ~0ULL;;
1195 fm->fm_flags = FIEMAP_FLAG_SYNC;
1196 fm->fm_extent_count = count;
1198 size = archive_entry_size(entry);
1199 for (iters = 0; ; ++iters) {
1202 r = ioctl(*fd, FS_IOC_FIEMAP, fm);
1204 /* When something error happens, it is better we
1205 * should return ARCHIVE_OK because an earlier
1206 * version(<2.6.28) cannot perform FS_IOC_FIEMAP. */
1207 goto exit_setup_sparse_fiemap;
1209 if (fm->fm_mapped_extents == 0) {
1211 /* Fully sparse file; insert a zero-length "data" entry */
1212 archive_entry_sparse_add_entry(entry, 0, 0);
1216 fe = fm->fm_extents;
1217 for (i = 0; i < (int)fm->fm_mapped_extents; i++, fe++) {
1218 if (!(fe->fe_flags & FIEMAP_EXTENT_UNWRITTEN)) {
1219 /* The fe_length of the last block does not
1220 * adjust itself to its size files. */
1221 int64_t length = fe->fe_length;
1222 if (fe->fe_logical + length > (uint64_t)size)
1223 length -= fe->fe_logical + length - size;
1224 if (fe->fe_logical == 0 && length == size) {
1225 /* This is not sparse. */
1230 archive_entry_sparse_add_entry(entry,
1231 fe->fe_logical, length);
1233 if (fe->fe_flags & FIEMAP_EXTENT_LAST)
1237 fe = fm->fm_extents + fm->fm_mapped_extents -1;
1238 fm->fm_start = fe->fe_logical + fe->fe_length;
1242 exit_setup_sparse_fiemap:
1246 #if !defined(SEEK_HOLE) || !defined(SEEK_DATA)
1248 setup_sparse(struct archive_read_disk *a,
1249 struct archive_entry *entry, int *fd)
1251 return setup_sparse_fiemap(a, entry, fd);
1254 #endif /* defined(HAVE_LINUX_FIEMAP_H) */
1256 #if defined(SEEK_HOLE) && defined(SEEK_DATA)
1259 * SEEK_HOLE sparse interface (FreeBSD, Linux, Solaris)
1263 setup_sparse(struct archive_read_disk *a,
1264 struct archive_entry *entry, int *fd)
1269 int exit_sts = ARCHIVE_OK;
1270 int check_fully_sparse = 0;
1272 if (archive_entry_filetype(entry) != AE_IFREG
1273 || archive_entry_size(entry) <= 0
1274 || archive_entry_hardlink(entry) != NULL)
1275 return (ARCHIVE_OK);
1277 /* Does filesystem support the reporting of hole ? */
1278 if (*fd < 0 && a->tree != NULL) {
1281 path = archive_entry_sourcepath(entry);
1283 path = archive_entry_pathname(entry);
1284 *fd = a->open_on_current_dir(a->tree, path,
1285 O_RDONLY | O_NONBLOCK);
1287 archive_set_error(&a->archive, errno,
1288 "Can't open `%s'", path);
1289 return (ARCHIVE_FAILED);
1294 #ifdef _PC_MIN_HOLE_SIZE
1295 if (fpathconf(*fd, _PC_MIN_HOLE_SIZE) <= 0)
1296 return (ARCHIVE_OK);
1298 initial_off = lseek(*fd, 0, SEEK_CUR);
1299 if (initial_off != 0)
1300 lseek(*fd, 0, SEEK_SET);
1304 path = archive_entry_sourcepath(entry);
1306 path = archive_entry_pathname(entry);
1308 #ifdef _PC_MIN_HOLE_SIZE
1309 if (pathconf(path, _PC_MIN_HOLE_SIZE) <= 0)
1310 return (ARCHIVE_OK);
1312 *fd = open(path, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
1314 archive_set_error(&a->archive, errno,
1315 "Can't open `%s'", path);
1316 return (ARCHIVE_FAILED);
1318 __archive_ensure_cloexec_flag(*fd);
1322 #ifndef _PC_MIN_HOLE_SIZE
1323 /* Check if the underlying filesystem supports seek hole */
1324 off_s = lseek(*fd, 0, SEEK_HOLE);
1326 #if defined(HAVE_LINUX_FIEMAP_H)
1327 return setup_sparse_fiemap(a, entry, fd);
1329 goto exit_setup_sparse;
1332 lseek(*fd, 0, SEEK_SET);
1336 size = archive_entry_size(entry);
1337 while (off_s < size) {
1338 off_s = lseek(*fd, off_s, SEEK_DATA);
1339 if (off_s == (off_t)-1) {
1340 if (errno == ENXIO) {
1342 if (archive_entry_sparse_count(entry) == 0) {
1343 /* Potentially a fully-sparse file. */
1344 check_fully_sparse = 1;
1348 archive_set_error(&a->archive, errno,
1349 "lseek(SEEK_HOLE) failed");
1350 exit_sts = ARCHIVE_FAILED;
1351 goto exit_setup_sparse;
1353 off_e = lseek(*fd, off_s, SEEK_HOLE);
1354 if (off_e == (off_t)-1) {
1355 if (errno == ENXIO) {
1356 off_e = lseek(*fd, 0, SEEK_END);
1357 if (off_e != (off_t)-1)
1358 break;/* no more data */
1360 archive_set_error(&a->archive, errno,
1361 "lseek(SEEK_DATA) failed");
1362 exit_sts = ARCHIVE_FAILED;
1363 goto exit_setup_sparse;
1365 if (off_s == 0 && off_e == size)
1366 break;/* This is not sparse. */
1367 archive_entry_sparse_add_entry(entry, off_s,
1372 if (check_fully_sparse) {
1373 if (lseek(*fd, 0, SEEK_HOLE) == 0 &&
1374 lseek(*fd, 0, SEEK_END) == size) {
1375 /* Fully sparse file; insert a zero-length "data" entry */
1376 archive_entry_sparse_add_entry(entry, 0, 0);
1380 lseek(*fd, initial_off, SEEK_SET);
1384 #elif !defined(HAVE_LINUX_FIEMAP_H)
1387 * Generic (stub) sparse support.
1390 setup_sparse(struct archive_read_disk *a,
1391 struct archive_entry *entry, int *fd)
1393 (void)a; /* UNUSED */
1394 (void)entry; /* UNUSED */
1395 (void)fd; /* UNUSED */
1396 return (ARCHIVE_OK);
1401 #endif /* !defined(_WIN32) || defined(__CYGWIN__) */