2 * Copyright (c) 2003-2009 Tim Kientzle
3 * Copyright (c) 2010-2012 Michihiro NAKAJIMA
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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 "archive_platform.h"
28 __FBSDID("$FreeBSD$");
30 /* This is the tree-walking code for POSIX systems. */
31 #if !defined(_WIN32) || defined(__CYGWIN__)
33 #ifdef HAVE_SYS_TYPES_H
34 /* Mac OSX requires sys/types.h before sys/acl.h. */
35 #include <sys/types.h>
40 #ifdef HAVE_SYS_EXTATTR_H
41 #include <sys/extattr.h>
43 #ifdef HAVE_SYS_IOCTL_H
44 #include <sys/ioctl.h>
46 #ifdef HAVE_SYS_PARAM_H
47 #include <sys/param.h>
49 #ifdef HAVE_SYS_STAT_H
52 #if defined(HAVE_SYS_XATTR_H)
53 #include <sys/xattr.h>
54 #elif defined(HAVE_ATTR_XATTR_H)
55 #include <attr/xattr.h>
60 #ifdef HAVE_ACL_LIBACL_H
61 #include <acl/libacl.h>
63 #ifdef HAVE_COPYFILE_H
75 #ifdef HAVE_LINUX_TYPES_H
76 #include <linux/types.h>
78 #ifdef HAVE_LINUX_FIEMAP_H
79 #include <linux/fiemap.h>
81 #ifdef HAVE_LINUX_FS_H
85 * Some Linux distributions have both linux/ext2_fs.h and ext2fs/ext2_fs.h.
86 * As the include guards don't agree, the order of include is important.
88 #ifdef HAVE_LINUX_EXT2_FS_H
89 #include <linux/ext2_fs.h> /* for Linux file flags */
91 #if defined(HAVE_EXT2FS_EXT2_FS_H) && !defined(__CYGWIN__)
92 #include <ext2fs/ext2_fs.h> /* Linux file flags, broken on Cygwin */
102 #include "archive_entry.h"
103 #include "archive_private.h"
104 #include "archive_read_disk_private.h"
111 * Linux and FreeBSD plug this obvious hole in POSIX.1e in
114 #if HAVE_ACL_GET_PERM
115 #define ACL_GET_PERM acl_get_perm
116 #elif HAVE_ACL_GET_PERM_NP
117 #define ACL_GET_PERM acl_get_perm_np
120 static int setup_acls(struct archive_read_disk *,
121 struct archive_entry *, int *fd);
122 static int setup_mac_metadata(struct archive_read_disk *,
123 struct archive_entry *, int *fd);
124 static int setup_xattrs(struct archive_read_disk *,
125 struct archive_entry *, int *fd);
126 static int setup_sparse(struct archive_read_disk *,
127 struct archive_entry *, int *fd);
130 archive_read_disk_entry_from_file(struct archive *_a,
131 struct archive_entry *entry,
133 const struct stat *st)
135 struct archive_read_disk *a = (struct archive_read_disk *)_a;
136 const char *path, *name;
141 archive_clear_error(_a);
142 path = archive_entry_sourcepath(entry);
144 path = archive_entry_pathname(entry);
146 if (a->tree == NULL) {
150 if (fstat(fd, &s) != 0) {
151 archive_set_error(&a->archive, errno,
153 return (ARCHIVE_FAILED);
158 if (!a->follow_symlinks) {
159 if (lstat(path, &s) != 0) {
160 archive_set_error(&a->archive, errno,
161 "Can't lstat %s", path);
162 return (ARCHIVE_FAILED);
166 if (stat(path, &s) != 0) {
167 archive_set_error(&a->archive, errno,
168 "Can't stat %s", path);
169 return (ARCHIVE_FAILED);
173 archive_entry_copy_stat(entry, st);
176 /* Lookup uname/gname */
177 name = archive_read_disk_uname(_a, archive_entry_uid(entry));
179 archive_entry_copy_uname(entry, name);
180 name = archive_read_disk_gname(_a, archive_entry_gid(entry));
182 archive_entry_copy_gname(entry, name);
184 #ifdef HAVE_STRUCT_STAT_ST_FLAGS
185 /* On FreeBSD, we get flags for free with the stat. */
186 /* TODO: Does this belong in copy_stat()? */
187 if (st->st_flags != 0)
188 archive_entry_set_fflags(entry, st->st_flags, 0);
191 #if defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS)
192 /* Linux requires an extra ioctl to pull the flags. Although
193 * this is an extra step, it has a nice side-effect: We get an
194 * open file descriptor which we can use in the subsequent lookups. */
195 if ((S_ISREG(st->st_mode) || S_ISDIR(st->st_mode))) {
198 fd = a->open_on_current_dir(a->tree, path,
199 O_RDONLY | O_NONBLOCK | O_CLOEXEC);
201 fd = open(path, O_RDONLY | O_NONBLOCK |
203 __archive_ensure_cloexec_flag(fd);
207 r = ioctl(fd, EXT2_IOC_GETFLAGS, &stflags);
208 if (r == 0 && stflags != 0)
209 archive_entry_set_fflags(entry, stflags, 0);
214 #if defined(HAVE_READLINK) || defined(HAVE_READLINKAT)
215 if (S_ISLNK(st->st_mode)) {
216 size_t linkbuffer_len = st->st_size + 1;
220 linkbuffer = malloc(linkbuffer_len);
221 if (linkbuffer == NULL) {
222 archive_set_error(&a->archive, ENOMEM,
223 "Couldn't read link data");
224 return (ARCHIVE_FAILED);
226 if (a->tree != NULL) {
227 #ifdef HAVE_READLINKAT
228 lnklen = readlinkat(a->tree_current_dir_fd(a->tree),
229 path, linkbuffer, linkbuffer_len);
231 if (a->tree_enter_working_dir(a->tree) != 0) {
232 archive_set_error(&a->archive, errno,
233 "Couldn't read link data");
235 return (ARCHIVE_FAILED);
237 lnklen = readlink(path, linkbuffer, linkbuffer_len);
238 #endif /* HAVE_READLINKAT */
240 lnklen = readlink(path, linkbuffer, linkbuffer_len);
242 archive_set_error(&a->archive, errno,
243 "Couldn't read link data");
245 return (ARCHIVE_FAILED);
247 linkbuffer[lnklen] = 0;
248 archive_entry_set_symlink(entry, linkbuffer);
251 #endif /* HAVE_READLINK || HAVE_READLINKAT */
253 r = setup_acls(a, entry, &fd);
254 if (!a->suppress_xattr) {
255 r1 = setup_xattrs(a, entry, &fd);
259 if (a->enable_copyfile) {
260 r1 = setup_mac_metadata(a, entry, &fd);
264 r1 = setup_sparse(a, entry, &fd);
268 /* If we opened the file earlier in this function, close it. */
269 if (initial_fd != fd)
274 #if defined(__APPLE__) && defined(HAVE_COPYFILE_H)
276 * The Mac OS "copyfile()" API copies the extended metadata for a
277 * file into a separate file in AppleDouble format (see RFC 1740).
279 * Mac OS tar and cpio implementations store this extended
280 * metadata as a separate entry just before the regular entry
281 * with a "._" prefix added to the filename.
283 * Note that this is currently done unconditionally; the tar program has
284 * an option to discard this information before the archive is written.
286 * TODO: If there's a failure, report it and return ARCHIVE_WARN.
289 setup_mac_metadata(struct archive_read_disk *a,
290 struct archive_entry *entry, int *fd)
293 int copyfile_flags = COPYFILE_NOFOLLOW | COPYFILE_ACL | COPYFILE_XATTR;
294 struct stat copyfile_stat;
295 int ret = ARCHIVE_OK;
298 const char *name, *tempdir;
299 struct archive_string tempfile;
301 (void)fd; /* UNUSED */
302 name = archive_entry_sourcepath(entry);
304 name = archive_entry_pathname(entry);
306 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
307 "Can't open file to read extended attributes: No name");
308 return (ARCHIVE_WARN);
311 if (a->tree != NULL) {
312 if (a->tree_enter_working_dir(a->tree) != 0) {
313 archive_set_error(&a->archive, errno,
314 "Couldn't change dir");
315 return (ARCHIVE_FAILED);
319 /* Short-circuit if there's nothing to do. */
320 have_attrs = copyfile(name, NULL, 0, copyfile_flags | COPYFILE_CHECK);
321 if (have_attrs == -1) {
322 archive_set_error(&a->archive, errno,
323 "Could not check extended attributes");
324 return (ARCHIVE_WARN);
330 if (issetugid() == 0)
331 tempdir = getenv("TMPDIR");
334 archive_string_init(&tempfile);
335 archive_strcpy(&tempfile, tempdir);
336 archive_strcat(&tempfile, "tar.md.XXXXXX");
337 tempfd = mkstemp(tempfile.s);
339 archive_set_error(&a->archive, errno,
340 "Could not open extended attribute file");
344 __archive_ensure_cloexec_flag(tempfd);
346 /* XXX I wish copyfile() could pack directly to a memory
347 * buffer; that would avoid the temp file here. For that
348 * matter, it would be nice if fcopyfile() actually worked,
349 * that would reduce the many open/close races here. */
350 if (copyfile(name, tempfile.s, 0, copyfile_flags | COPYFILE_PACK)) {
351 archive_set_error(&a->archive, errno,
352 "Could not pack extended attributes");
356 if (fstat(tempfd, ©file_stat)) {
357 archive_set_error(&a->archive, errno,
358 "Could not check size of extended attributes");
362 buff = malloc(copyfile_stat.st_size);
364 archive_set_error(&a->archive, errno,
365 "Could not allocate memory for extended attributes");
369 if (copyfile_stat.st_size != read(tempfd, buff, copyfile_stat.st_size)) {
370 archive_set_error(&a->archive, errno,
371 "Could not read extended attributes into memory");
375 archive_entry_copy_mac_metadata(entry, buff, copyfile_stat.st_size);
382 archive_string_free(&tempfile);
390 * Stub implementation for non-Mac systems.
393 setup_mac_metadata(struct archive_read_disk *a,
394 struct archive_entry *entry, int *fd)
396 (void)a; /* UNUSED */
397 (void)entry; /* UNUSED */
398 (void)fd; /* UNUSED */
404 #ifdef HAVE_POSIX_ACL
405 static int translate_acl(struct archive_read_disk *a,
406 struct archive_entry *entry, acl_t acl, int archive_entry_acl_type);
409 setup_acls(struct archive_read_disk *a,
410 struct archive_entry *entry, int *fd)
416 accpath = archive_entry_sourcepath(entry);
418 accpath = archive_entry_pathname(entry);
420 if (*fd < 0 && a->tree != NULL) {
421 if (a->follow_symlinks ||
422 archive_entry_filetype(entry) != AE_IFLNK)
423 *fd = a->open_on_current_dir(a->tree,
424 accpath, O_RDONLY | O_NONBLOCK);
426 if (a->tree_enter_working_dir(a->tree) != 0) {
427 archive_set_error(&a->archive, errno,
428 "Couldn't access %s", accpath);
429 return (ARCHIVE_FAILED);
434 archive_entry_acl_clear(entry);
439 /* Try NFS4 ACL first. */
441 #if HAVE_ACL_GET_FD_NP
442 acl = acl_get_fd_np(*fd, ACL_TYPE_NFS4);
444 acl = acl_get_fd(*fd);
446 #if HAVE_ACL_GET_LINK_NP
447 else if (!a->follow_symlinks)
448 acl = acl_get_link_np(accpath, ACL_TYPE_NFS4);
450 else if ((!a->follow_symlinks)
451 && (archive_entry_filetype(entry) == AE_IFLNK))
452 /* We can't get the ACL of a symlink, so we assume it can't
457 acl = acl_get_file(accpath, ACL_TYPE_NFS4);
459 #if HAVE_ACL_IS_TRIVIAL_NP
460 if (acl != NULL && acl_is_trivial_np(acl, &r) == 0) {
461 /* Ignore "trivial" ACLs that just mirror the file mode. */
466 * Simultaneous NFSv4 and POSIX.1e ACLs for the same
467 * entry are not allowed, so we should return here
474 r = translate_acl(a, entry, acl, ARCHIVE_ENTRY_ACL_TYPE_NFS4);
476 if (r != ARCHIVE_OK) {
477 archive_set_error(&a->archive, errno,
478 "Couldn't translate NFSv4 ACLs: %s", accpath);
482 #endif /* ACL_TYPE_NFS4 */
484 /* Retrieve access ACL from file. */
486 acl = acl_get_fd(*fd);
487 #if HAVE_ACL_GET_LINK_NP
488 else if (!a->follow_symlinks)
489 acl = acl_get_link_np(accpath, ACL_TYPE_ACCESS);
491 else if ((!a->follow_symlinks)
492 && (archive_entry_filetype(entry) == AE_IFLNK))
493 /* We can't get the ACL of a symlink, so we assume it can't
498 acl = acl_get_file(accpath, ACL_TYPE_ACCESS);
500 #if HAVE_ACL_IS_TRIVIAL_NP
501 /* Ignore "trivial" ACLs that just mirror the file mode. */
502 if (acl != NULL && acl_is_trivial_np(acl, &r) == 0) {
511 r = translate_acl(a, entry, acl,
512 ARCHIVE_ENTRY_ACL_TYPE_ACCESS);
515 if (r != ARCHIVE_OK) {
516 archive_set_error(&a->archive, errno,
517 "Couldn't translate access ACLs: %s", accpath);
522 /* Only directories can have default ACLs. */
523 if (S_ISDIR(archive_entry_mode(entry))) {
524 acl = acl_get_file(accpath, ACL_TYPE_DEFAULT);
526 r = translate_acl(a, entry, acl,
527 ARCHIVE_ENTRY_ACL_TYPE_DEFAULT);
529 if (r != ARCHIVE_OK) {
530 archive_set_error(&a->archive, errno,
531 "Couldn't translate default ACLs: %s",
541 * Translate system ACL into libarchive internal structure.
548 {ARCHIVE_ENTRY_ACL_EXECUTE, ACL_EXECUTE},
549 {ARCHIVE_ENTRY_ACL_WRITE, ACL_WRITE},
550 {ARCHIVE_ENTRY_ACL_READ, ACL_READ},
552 {ARCHIVE_ENTRY_ACL_READ_DATA, ACL_READ_DATA},
553 {ARCHIVE_ENTRY_ACL_LIST_DIRECTORY, ACL_LIST_DIRECTORY},
554 {ARCHIVE_ENTRY_ACL_WRITE_DATA, ACL_WRITE_DATA},
555 {ARCHIVE_ENTRY_ACL_ADD_FILE, ACL_ADD_FILE},
556 {ARCHIVE_ENTRY_ACL_APPEND_DATA, ACL_APPEND_DATA},
557 {ARCHIVE_ENTRY_ACL_ADD_SUBDIRECTORY, ACL_ADD_SUBDIRECTORY},
558 {ARCHIVE_ENTRY_ACL_READ_NAMED_ATTRS, ACL_READ_NAMED_ATTRS},
559 {ARCHIVE_ENTRY_ACL_WRITE_NAMED_ATTRS, ACL_WRITE_NAMED_ATTRS},
560 {ARCHIVE_ENTRY_ACL_DELETE_CHILD, ACL_DELETE_CHILD},
561 {ARCHIVE_ENTRY_ACL_READ_ATTRIBUTES, ACL_READ_ATTRIBUTES},
562 {ARCHIVE_ENTRY_ACL_WRITE_ATTRIBUTES, ACL_WRITE_ATTRIBUTES},
563 {ARCHIVE_ENTRY_ACL_DELETE, ACL_DELETE},
564 {ARCHIVE_ENTRY_ACL_READ_ACL, ACL_READ_ACL},
565 {ARCHIVE_ENTRY_ACL_WRITE_ACL, ACL_WRITE_ACL},
566 {ARCHIVE_ENTRY_ACL_WRITE_OWNER, ACL_WRITE_OWNER},
567 {ARCHIVE_ENTRY_ACL_SYNCHRONIZE, ACL_SYNCHRONIZE}
574 int platform_inherit;
575 } acl_inherit_map[] = {
576 {ARCHIVE_ENTRY_ACL_ENTRY_FILE_INHERIT, ACL_ENTRY_FILE_INHERIT},
577 {ARCHIVE_ENTRY_ACL_ENTRY_DIRECTORY_INHERIT, ACL_ENTRY_DIRECTORY_INHERIT},
578 {ARCHIVE_ENTRY_ACL_ENTRY_NO_PROPAGATE_INHERIT, ACL_ENTRY_NO_PROPAGATE_INHERIT},
579 {ARCHIVE_ENTRY_ACL_ENTRY_INHERIT_ONLY, ACL_ENTRY_INHERIT_ONLY}
583 translate_acl(struct archive_read_disk *a,
584 struct archive_entry *entry, acl_t acl, int default_entry_acl_type)
588 acl_entry_type_t acl_type;
589 acl_flagset_t acl_flagset;
592 acl_entry_t acl_entry;
593 acl_permset_t acl_permset;
594 int i, entry_acl_type;
595 int r, s, ae_id, ae_tag, ae_perm;
599 // FreeBSD "brands" ACLs as POSIX.1e or NFSv4
600 // Make sure the "brand" on this ACL is consistent
601 // with the default_entry_acl_type bits provided.
602 if (acl_get_brand_np(acl, &brand) != 0) {
603 archive_set_error(&a->archive, errno,
604 "Failed to read ACL brand");
605 return (ARCHIVE_WARN);
608 case ACL_BRAND_POSIX:
609 switch (default_entry_acl_type) {
610 case ARCHIVE_ENTRY_ACL_TYPE_ACCESS:
611 case ARCHIVE_ENTRY_ACL_TYPE_DEFAULT:
614 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
615 "Invalid ACL entry type for POSIX.1e ACL");
616 return (ARCHIVE_WARN);
620 if (default_entry_acl_type & ~ARCHIVE_ENTRY_ACL_TYPE_NFS4) {
621 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
622 "Invalid ACL entry type for NFSv4 ACL");
623 return (ARCHIVE_WARN);
627 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
628 "Unknown ACL brand");
629 return (ARCHIVE_WARN);
634 s = acl_get_entry(acl, ACL_FIRST_ENTRY, &acl_entry);
636 archive_set_error(&a->archive, errno,
637 "Failed to get first ACL entry");
638 return (ARCHIVE_WARN);
645 if (acl_get_tag_type(acl_entry, &acl_tag) != 0) {
646 archive_set_error(&a->archive, errno,
647 "Failed to get ACL tag type");
648 return (ARCHIVE_WARN);
652 ae_id = (int)*(uid_t *)acl_get_qualifier(acl_entry);
653 ae_name = archive_read_disk_uname(&a->archive, ae_id);
654 ae_tag = ARCHIVE_ENTRY_ACL_USER;
657 ae_id = (int)*(gid_t *)acl_get_qualifier(acl_entry);
658 ae_name = archive_read_disk_gname(&a->archive, ae_id);
659 ae_tag = ARCHIVE_ENTRY_ACL_GROUP;
662 ae_tag = ARCHIVE_ENTRY_ACL_MASK;
665 ae_tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
668 ae_tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
671 ae_tag = ARCHIVE_ENTRY_ACL_OTHER;
675 ae_tag = ARCHIVE_ENTRY_ACL_EVERYONE;
679 /* Skip types that libarchive can't support. */
680 s = acl_get_entry(acl, ACL_NEXT_ENTRY, &acl_entry);
684 // XXX acl_type maps to allow/deny/audit/YYYY bits
685 entry_acl_type = default_entry_acl_type;
687 if (default_entry_acl_type & ARCHIVE_ENTRY_ACL_TYPE_NFS4) {
689 * acl_get_entry_type_np() falis with non-NFSv4 ACLs
691 if (acl_get_entry_type_np(acl_entry, &acl_type) != 0) {
692 archive_set_error(&a->archive, errno, "Failed "
693 "to get ACL type from a NFSv4 ACL entry");
694 return (ARCHIVE_WARN);
697 case ACL_ENTRY_TYPE_ALLOW:
698 entry_acl_type = ARCHIVE_ENTRY_ACL_TYPE_ALLOW;
700 case ACL_ENTRY_TYPE_DENY:
701 entry_acl_type = ARCHIVE_ENTRY_ACL_TYPE_DENY;
703 case ACL_ENTRY_TYPE_AUDIT:
704 entry_acl_type = ARCHIVE_ENTRY_ACL_TYPE_AUDIT;
706 case ACL_ENTRY_TYPE_ALARM:
707 entry_acl_type = ARCHIVE_ENTRY_ACL_TYPE_ALARM;
710 archive_set_error(&a->archive, errno,
711 "Invalid NFSv4 ACL entry type");
712 return (ARCHIVE_WARN);
716 * Libarchive stores "flag" (NFSv4 inheritance bits)
717 * in the ae_perm bitmap.
719 * acl_get_flagset_np() fails with non-NFSv4 ACLs
721 if (acl_get_flagset_np(acl_entry, &acl_flagset) != 0) {
722 archive_set_error(&a->archive, errno,
723 "Failed to get flagset from a NFSv4 ACL entry");
724 return (ARCHIVE_WARN);
726 for (i = 0; i < (int)(sizeof(acl_inherit_map) / sizeof(acl_inherit_map[0])); ++i) {
727 r = acl_get_flag_np(acl_flagset,
728 acl_inherit_map[i].platform_inherit);
730 archive_set_error(&a->archive, errno,
731 "Failed to check flag in a NFSv4 "
733 return (ARCHIVE_WARN);
735 ae_perm |= acl_inherit_map[i].archive_inherit;
740 if (acl_get_permset(acl_entry, &acl_permset) != 0) {
741 archive_set_error(&a->archive, errno,
742 "Failed to get ACL permission set");
743 return (ARCHIVE_WARN);
745 for (i = 0; i < (int)(sizeof(acl_perm_map) / sizeof(acl_perm_map[0])); ++i) {
747 * acl_get_perm() is spelled differently on different
748 * platforms; see above.
750 r = ACL_GET_PERM(acl_permset, acl_perm_map[i].platform_perm);
752 archive_set_error(&a->archive, errno,
753 "Failed to check permission in an ACL permission set");
754 return (ARCHIVE_WARN);
756 ae_perm |= acl_perm_map[i].archive_perm;
759 archive_entry_acl_add_entry(entry, entry_acl_type,
763 s = acl_get_entry(acl, ACL_NEXT_ENTRY, &acl_entry);
765 archive_set_error(&a->archive, errno,
766 "Failed to get next ACL entry");
767 return (ARCHIVE_WARN);
774 setup_acls(struct archive_read_disk *a,
775 struct archive_entry *entry, int *fd)
777 (void)a; /* UNUSED */
778 (void)entry; /* UNUSED */
779 (void)fd; /* UNUSED */
784 #if (HAVE_FGETXATTR && HAVE_FLISTXATTR && HAVE_LISTXATTR && \
785 HAVE_LLISTXATTR && HAVE_GETXATTR && HAVE_LGETXATTR) || \
786 (HAVE_FGETEA && HAVE_FLISTEA && HAVE_LISTEA)
789 * Linux and AIX extended attribute support.
791 * TODO: By using a stack-allocated buffer for the first
792 * call to getxattr(), we might be able to avoid the second
793 * call entirely. We only need the second call if the
794 * stack-allocated buffer is too small. But a modest buffer
795 * of 1024 bytes or so will often be big enough. Same applies
801 setup_xattr(struct archive_read_disk *a,
802 struct archive_entry *entry, const char *name, int fd)
808 accpath = archive_entry_sourcepath(entry);
810 accpath = archive_entry_pathname(entry);
814 size = fgetxattr(fd, name, NULL, 0);
815 else if (!a->follow_symlinks)
816 size = lgetxattr(accpath, name, NULL, 0);
818 size = getxattr(accpath, name, NULL, 0);
821 size = fgetea(fd, name, NULL, 0);
822 else if (!a->follow_symlinks)
823 size = lgetea(accpath, name, NULL, 0);
825 size = getea(accpath, name, NULL, 0);
829 archive_set_error(&a->archive, errno,
830 "Couldn't query extended attribute");
831 return (ARCHIVE_WARN);
834 if (size > 0 && (value = malloc(size)) == NULL) {
835 archive_set_error(&a->archive, errno, "Out of memory");
836 return (ARCHIVE_FATAL);
841 size = fgetxattr(fd, name, value, size);
842 else if (!a->follow_symlinks)
843 size = lgetxattr(accpath, name, value, size);
845 size = getxattr(accpath, name, value, size);
848 size = fgetea(fd, name, value, size);
849 else if (!a->follow_symlinks)
850 size = lgetea(accpath, name, value, size);
852 size = getea(accpath, name, value, size);
856 archive_set_error(&a->archive, errno,
857 "Couldn't read extended attribute");
858 return (ARCHIVE_WARN);
861 archive_entry_xattr_add_entry(entry, name, value, size);
868 setup_xattrs(struct archive_read_disk *a,
869 struct archive_entry *entry, int *fd)
875 path = archive_entry_sourcepath(entry);
877 path = archive_entry_pathname(entry);
879 if (*fd < 0 && a->tree != NULL) {
880 if (a->follow_symlinks ||
881 archive_entry_filetype(entry) != AE_IFLNK)
882 *fd = a->open_on_current_dir(a->tree, path,
883 O_RDONLY | O_NONBLOCK);
885 if (a->tree_enter_working_dir(a->tree) != 0) {
886 archive_set_error(&a->archive, errno,
887 "Couldn't access %s", path);
888 return (ARCHIVE_FAILED);
895 list_size = flistxattr(*fd, NULL, 0);
896 else if (!a->follow_symlinks)
897 list_size = llistxattr(path, NULL, 0);
899 list_size = listxattr(path, NULL, 0);
902 list_size = flistea(*fd, NULL, 0);
903 else if (!a->follow_symlinks)
904 list_size = llistea(path, NULL, 0);
906 list_size = listea(path, NULL, 0);
909 if (list_size == -1) {
910 if (errno == ENOTSUP || errno == ENOSYS)
912 archive_set_error(&a->archive, errno,
913 "Couldn't list extended attributes");
914 return (ARCHIVE_WARN);
920 if ((list = malloc(list_size)) == NULL) {
921 archive_set_error(&a->archive, errno, "Out of memory");
922 return (ARCHIVE_FATAL);
927 list_size = flistxattr(*fd, list, list_size);
928 else if (!a->follow_symlinks)
929 list_size = llistxattr(path, list, list_size);
931 list_size = listxattr(path, list, list_size);
934 list_size = flistea(*fd, list, list_size);
935 else if (!a->follow_symlinks)
936 list_size = llistea(path, list, list_size);
938 list_size = listea(path, list, list_size);
941 if (list_size == -1) {
942 archive_set_error(&a->archive, errno,
943 "Couldn't retrieve extended attributes");
945 return (ARCHIVE_WARN);
948 for (p = list; (p - list) < list_size; p += strlen(p) + 1) {
949 if (strncmp(p, "system.", 7) == 0 ||
950 strncmp(p, "xfsroot.", 8) == 0)
952 setup_xattr(a, entry, p, *fd);
959 #elif HAVE_EXTATTR_GET_FILE && HAVE_EXTATTR_LIST_FILE && \
960 HAVE_DECL_EXTATTR_NAMESPACE_USER
963 * FreeBSD extattr interface.
966 /* TODO: Implement this. Follow the Linux model above, but
967 * with FreeBSD-specific system calls, of course. Be careful
968 * to not include the system extattrs that hold ACLs; we handle
972 setup_xattr(struct archive_read_disk *a, struct archive_entry *entry,
973 int namespace, const char *name, const char *fullname, int fd);
976 setup_xattr(struct archive_read_disk *a, struct archive_entry *entry,
977 int namespace, const char *name, const char *fullname, int fd)
983 accpath = archive_entry_sourcepath(entry);
985 accpath = archive_entry_pathname(entry);
988 size = extattr_get_fd(fd, namespace, name, NULL, 0);
989 else if (!a->follow_symlinks)
990 size = extattr_get_link(accpath, namespace, name, NULL, 0);
992 size = extattr_get_file(accpath, namespace, name, NULL, 0);
995 archive_set_error(&a->archive, errno,
996 "Couldn't query extended attribute");
997 return (ARCHIVE_WARN);
1000 if (size > 0 && (value = malloc(size)) == NULL) {
1001 archive_set_error(&a->archive, errno, "Out of memory");
1002 return (ARCHIVE_FATAL);
1006 size = extattr_get_fd(fd, namespace, name, value, size);
1007 else if (!a->follow_symlinks)
1008 size = extattr_get_link(accpath, namespace, name, value, size);
1010 size = extattr_get_file(accpath, namespace, name, value, size);
1014 archive_set_error(&a->archive, errno,
1015 "Couldn't read extended attribute");
1016 return (ARCHIVE_WARN);
1019 archive_entry_xattr_add_entry(entry, fullname, value, size);
1022 return (ARCHIVE_OK);
1026 setup_xattrs(struct archive_read_disk *a,
1027 struct archive_entry *entry, int *fd)
1033 int namespace = EXTATTR_NAMESPACE_USER;
1035 path = archive_entry_sourcepath(entry);
1037 path = archive_entry_pathname(entry);
1039 if (*fd < 0 && a->tree != NULL) {
1040 if (a->follow_symlinks ||
1041 archive_entry_filetype(entry) != AE_IFLNK)
1042 *fd = a->open_on_current_dir(a->tree, path,
1043 O_RDONLY | O_NONBLOCK);
1045 if (a->tree_enter_working_dir(a->tree) != 0) {
1046 archive_set_error(&a->archive, errno,
1047 "Couldn't access %s", path);
1048 return (ARCHIVE_FAILED);
1054 list_size = extattr_list_fd(*fd, namespace, NULL, 0);
1055 else if (!a->follow_symlinks)
1056 list_size = extattr_list_link(path, namespace, NULL, 0);
1058 list_size = extattr_list_file(path, namespace, NULL, 0);
1060 if (list_size == -1 && errno == EOPNOTSUPP)
1061 return (ARCHIVE_OK);
1062 if (list_size == -1) {
1063 archive_set_error(&a->archive, errno,
1064 "Couldn't list extended attributes");
1065 return (ARCHIVE_WARN);
1069 return (ARCHIVE_OK);
1071 if ((list = malloc(list_size)) == NULL) {
1072 archive_set_error(&a->archive, errno, "Out of memory");
1073 return (ARCHIVE_FATAL);
1077 list_size = extattr_list_fd(*fd, namespace, list, list_size);
1078 else if (!a->follow_symlinks)
1079 list_size = extattr_list_link(path, namespace, list, list_size);
1081 list_size = extattr_list_file(path, namespace, list, list_size);
1083 if (list_size == -1) {
1084 archive_set_error(&a->archive, errno,
1085 "Couldn't retrieve extended attributes");
1087 return (ARCHIVE_WARN);
1091 while ((p - list) < list_size) {
1092 size_t len = 255 & (int)*p;
1095 strcpy(buff, "user.");
1096 name = buff + strlen(buff);
1097 memcpy(name, p + 1, len);
1099 setup_xattr(a, entry, namespace, name, buff, *fd);
1104 return (ARCHIVE_OK);
1110 * Generic (stub) extended attribute support.
1113 setup_xattrs(struct archive_read_disk *a,
1114 struct archive_entry *entry, int *fd)
1116 (void)a; /* UNUSED */
1117 (void)entry; /* UNUSED */
1118 (void)fd; /* UNUSED */
1119 return (ARCHIVE_OK);
1124 #if defined(HAVE_LINUX_FIEMAP_H)
1127 * Linux sparse interface.
1129 * The FIEMAP ioctl returns an "extent" for each physical allocation
1130 * on disk. We need to process those to generate a more compact list
1131 * of logical file blocks. We also need to be very careful to use
1132 * FIEMAP_FLAG_SYNC here, since there are reports that Linux sometimes
1133 * does not report allocations for newly-written data that hasn't
1134 * been synced to disk.
1136 * It's important to return a minimal sparse file list because we want
1137 * to not trigger sparse file extensions if we don't have to, since
1138 * not all readers support them.
1142 setup_sparse(struct archive_read_disk *a,
1143 struct archive_entry *entry, int *fd)
1147 struct fiemap_extent *fe;
1149 int count, do_fiemap, iters;
1150 int exit_sts = ARCHIVE_OK;
1152 if (archive_entry_filetype(entry) != AE_IFREG
1153 || archive_entry_size(entry) <= 0
1154 || archive_entry_hardlink(entry) != NULL)
1155 return (ARCHIVE_OK);
1160 path = archive_entry_sourcepath(entry);
1162 path = archive_entry_pathname(entry);
1163 if (a->tree != NULL)
1164 *fd = a->open_on_current_dir(a->tree, path,
1165 O_RDONLY | O_NONBLOCK | O_CLOEXEC);
1167 *fd = open(path, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
1169 archive_set_error(&a->archive, errno,
1170 "Can't open `%s'", path);
1171 return (ARCHIVE_FAILED);
1173 __archive_ensure_cloexec_flag(*fd);
1176 /* Initialize buffer to avoid the error valgrind complains about. */
1177 memset(buff, 0, sizeof(buff));
1178 count = (sizeof(buff) - sizeof(*fm))/sizeof(*fe);
1179 fm = (struct fiemap *)buff;
1181 fm->fm_length = ~0ULL;;
1182 fm->fm_flags = FIEMAP_FLAG_SYNC;
1183 fm->fm_extent_count = count;
1185 size = archive_entry_size(entry);
1186 for (iters = 0; ; ++iters) {
1189 r = ioctl(*fd, FS_IOC_FIEMAP, fm);
1191 /* When something error happens, it is better we
1192 * should return ARCHIVE_OK because an earlier
1193 * version(<2.6.28) cannot perfom FS_IOC_FIEMAP. */
1194 goto exit_setup_sparse;
1196 if (fm->fm_mapped_extents == 0) {
1198 /* Fully sparse file; insert a zero-length "data" entry */
1199 archive_entry_sparse_add_entry(entry, 0, 0);
1203 fe = fm->fm_extents;
1204 for (i = 0; i < (int)fm->fm_mapped_extents; i++, fe++) {
1205 if (!(fe->fe_flags & FIEMAP_EXTENT_UNWRITTEN)) {
1206 /* The fe_length of the last block does not
1207 * adjust itself to its size files. */
1208 int64_t length = fe->fe_length;
1209 if (fe->fe_logical + length > (uint64_t)size)
1210 length -= fe->fe_logical + length - size;
1211 if (fe->fe_logical == 0 && length == size) {
1212 /* This is not sparse. */
1217 archive_entry_sparse_add_entry(entry,
1218 fe->fe_logical, length);
1220 if (fe->fe_flags & FIEMAP_EXTENT_LAST)
1224 fe = fm->fm_extents + fm->fm_mapped_extents -1;
1225 fm->fm_start = fe->fe_logical + fe->fe_length;
1233 #elif defined(SEEK_HOLE) && defined(SEEK_DATA) && defined(_PC_MIN_HOLE_SIZE)
1236 * FreeBSD and Solaris sparse interface.
1240 setup_sparse(struct archive_read_disk *a,
1241 struct archive_entry *entry, int *fd)
1244 off_t initial_off; /* FreeBSD/Solaris only, so off_t okay here */
1245 off_t off_s, off_e; /* FreeBSD/Solaris only, so off_t okay here */
1246 int exit_sts = ARCHIVE_OK;
1247 int check_fully_sparse = 0;
1249 if (archive_entry_filetype(entry) != AE_IFREG
1250 || archive_entry_size(entry) <= 0
1251 || archive_entry_hardlink(entry) != NULL)
1252 return (ARCHIVE_OK);
1254 /* Does filesystem support the reporting of hole ? */
1255 if (*fd < 0 && a->tree != NULL) {
1258 path = archive_entry_sourcepath(entry);
1260 path = archive_entry_pathname(entry);
1261 *fd = a->open_on_current_dir(a->tree, path,
1262 O_RDONLY | O_NONBLOCK);
1264 archive_set_error(&a->archive, errno,
1265 "Can't open `%s'", path);
1266 return (ARCHIVE_FAILED);
1271 if (fpathconf(*fd, _PC_MIN_HOLE_SIZE) <= 0)
1272 return (ARCHIVE_OK);
1273 initial_off = lseek(*fd, 0, SEEK_CUR);
1274 if (initial_off != 0)
1275 lseek(*fd, 0, SEEK_SET);
1279 path = archive_entry_sourcepath(entry);
1281 path = archive_entry_pathname(entry);
1283 if (pathconf(path, _PC_MIN_HOLE_SIZE) <= 0)
1284 return (ARCHIVE_OK);
1285 *fd = open(path, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
1287 archive_set_error(&a->archive, errno,
1288 "Can't open `%s'", path);
1289 return (ARCHIVE_FAILED);
1291 __archive_ensure_cloexec_flag(*fd);
1296 size = archive_entry_size(entry);
1297 while (off_s < size) {
1298 off_s = lseek(*fd, off_s, SEEK_DATA);
1299 if (off_s == (off_t)-1) {
1300 if (errno == ENXIO) {
1302 if (archive_entry_sparse_count(entry) == 0) {
1303 /* Potentially a fully-sparse file. */
1304 check_fully_sparse = 1;
1308 archive_set_error(&a->archive, errno,
1309 "lseek(SEEK_HOLE) failed");
1310 exit_sts = ARCHIVE_FAILED;
1311 goto exit_setup_sparse;
1313 off_e = lseek(*fd, off_s, SEEK_HOLE);
1314 if (off_e == (off_t)-1) {
1315 if (errno == ENXIO) {
1316 off_e = lseek(*fd, 0, SEEK_END);
1317 if (off_e != (off_t)-1)
1318 break;/* no more data */
1320 archive_set_error(&a->archive, errno,
1321 "lseek(SEEK_DATA) failed");
1322 exit_sts = ARCHIVE_FAILED;
1323 goto exit_setup_sparse;
1325 if (off_s == 0 && off_e == size)
1326 break;/* This is not spase. */
1327 archive_entry_sparse_add_entry(entry, off_s,
1332 if (check_fully_sparse) {
1333 if (lseek(*fd, 0, SEEK_HOLE) == 0 &&
1334 lseek(*fd, 0, SEEK_END) == size) {
1335 /* Fully sparse file; insert a zero-length "data" entry */
1336 archive_entry_sparse_add_entry(entry, 0, 0);
1340 lseek(*fd, initial_off, SEEK_SET);
1347 * Generic (stub) sparse support.
1350 setup_sparse(struct archive_read_disk *a,
1351 struct archive_entry *entry, int *fd)
1353 (void)a; /* UNUSED */
1354 (void)entry; /* UNUSED */
1355 (void)fd; /* UNUSED */
1356 return (ARCHIVE_OK);
1361 #endif /* !defined(_WIN32) || defined(__CYGWIN__) */