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: head/lib/libarchive/archive_read_disk_entry_from_file.c 201084 2009-12-28 02:14:09Z kientzle $");
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)
414 #if HAVE_ACL_IS_TRIVIAL_NP
418 accpath = archive_entry_sourcepath(entry);
420 accpath = archive_entry_pathname(entry);
422 archive_entry_acl_clear(entry);
425 /* Try NFS4 ACL first. */
427 acl = acl_get_fd(*fd);
428 #if HAVE_ACL_GET_LINK_NP
429 else if (!a->follow_symlinks)
430 acl = acl_get_link_np(accpath, ACL_TYPE_NFS4);
432 else if ((!a->follow_symlinks)
433 && (archive_entry_filetype(entry) == AE_IFLNK))
434 /* We can't get the ACL of a symlink, so we assume it can't
439 acl = acl_get_file(accpath, ACL_TYPE_NFS4);
440 #if HAVE_ACL_IS_TRIVIAL_NP
441 /* Ignore "trivial" ACLs that just mirror the file mode. */
442 acl_is_trivial_np(acl, &r);
449 translate_acl(a, entry, acl, ARCHIVE_ENTRY_ACL_TYPE_NFS4);
455 /* Retrieve access ACL from file. */
457 acl = acl_get_fd(*fd);
458 #if HAVE_ACL_GET_LINK_NP
459 else if (!a->follow_symlinks)
460 acl = acl_get_link_np(accpath, ACL_TYPE_ACCESS);
462 else if ((!a->follow_symlinks)
463 && (archive_entry_filetype(entry) == AE_IFLNK))
464 /* We can't get the ACL of a symlink, so we assume it can't
469 acl = acl_get_file(accpath, ACL_TYPE_ACCESS);
471 translate_acl(a, entry, acl,
472 ARCHIVE_ENTRY_ACL_TYPE_ACCESS);
476 /* Only directories can have default ACLs. */
477 if (S_ISDIR(archive_entry_mode(entry))) {
478 acl = acl_get_file(accpath, ACL_TYPE_DEFAULT);
480 translate_acl(a, entry, acl,
481 ARCHIVE_ENTRY_ACL_TYPE_DEFAULT);
489 * Translate system ACL into libarchive internal structure.
496 {ARCHIVE_ENTRY_ACL_EXECUTE, ACL_EXECUTE},
497 {ARCHIVE_ENTRY_ACL_WRITE, ACL_WRITE},
498 {ARCHIVE_ENTRY_ACL_READ, ACL_READ},
500 {ARCHIVE_ENTRY_ACL_READ_DATA, ACL_READ_DATA},
501 {ARCHIVE_ENTRY_ACL_LIST_DIRECTORY, ACL_LIST_DIRECTORY},
502 {ARCHIVE_ENTRY_ACL_WRITE_DATA, ACL_WRITE_DATA},
503 {ARCHIVE_ENTRY_ACL_ADD_FILE, ACL_ADD_FILE},
504 {ARCHIVE_ENTRY_ACL_APPEND_DATA, ACL_APPEND_DATA},
505 {ARCHIVE_ENTRY_ACL_ADD_SUBDIRECTORY, ACL_ADD_SUBDIRECTORY},
506 {ARCHIVE_ENTRY_ACL_READ_NAMED_ATTRS, ACL_READ_NAMED_ATTRS},
507 {ARCHIVE_ENTRY_ACL_WRITE_NAMED_ATTRS, ACL_WRITE_NAMED_ATTRS},
508 {ARCHIVE_ENTRY_ACL_DELETE_CHILD, ACL_DELETE_CHILD},
509 {ARCHIVE_ENTRY_ACL_READ_ATTRIBUTES, ACL_READ_ATTRIBUTES},
510 {ARCHIVE_ENTRY_ACL_WRITE_ATTRIBUTES, ACL_WRITE_ATTRIBUTES},
511 {ARCHIVE_ENTRY_ACL_DELETE, ACL_DELETE},
512 {ARCHIVE_ENTRY_ACL_READ_ACL, ACL_READ_ACL},
513 {ARCHIVE_ENTRY_ACL_WRITE_ACL, ACL_WRITE_ACL},
514 {ARCHIVE_ENTRY_ACL_WRITE_OWNER, ACL_WRITE_OWNER},
515 {ARCHIVE_ENTRY_ACL_SYNCHRONIZE, ACL_SYNCHRONIZE}
522 int platform_inherit;
523 } acl_inherit_map[] = {
524 {ARCHIVE_ENTRY_ACL_ENTRY_FILE_INHERIT, ACL_ENTRY_FILE_INHERIT},
525 {ARCHIVE_ENTRY_ACL_ENTRY_DIRECTORY_INHERIT, ACL_ENTRY_DIRECTORY_INHERIT},
526 {ARCHIVE_ENTRY_ACL_ENTRY_NO_PROPAGATE_INHERIT, ACL_ENTRY_NO_PROPAGATE_INHERIT},
527 {ARCHIVE_ENTRY_ACL_ENTRY_INHERIT_ONLY, ACL_ENTRY_INHERIT_ONLY}
531 translate_acl(struct archive_read_disk *a,
532 struct archive_entry *entry, acl_t acl, int default_entry_acl_type)
536 acl_entry_type_t acl_type;
537 acl_flagset_t acl_flagset;
540 acl_entry_t acl_entry;
541 acl_permset_t acl_permset;
542 int i, entry_acl_type;
543 int s, ae_id, ae_tag, ae_perm;
548 // FreeBSD "brands" ACLs as POSIX.1e or NFSv4
549 // Make sure the "brand" on this ACL is consistent
550 // with the default_entry_acl_type bits provided.
551 acl_get_brand_np(acl, &brand);
553 case ACL_BRAND_POSIX:
554 switch (default_entry_acl_type) {
555 case ARCHIVE_ENTRY_ACL_TYPE_ACCESS:
556 case ARCHIVE_ENTRY_ACL_TYPE_DEFAULT:
559 // XXX set warning message?
560 return ARCHIVE_FAILED;
564 if (default_entry_acl_type & ~ARCHIVE_ENTRY_ACL_TYPE_NFS4) {
565 // XXX set warning message?
566 return ARCHIVE_FAILED;
570 // XXX set warning message?
571 return ARCHIVE_FAILED;
577 s = acl_get_entry(acl, ACL_FIRST_ENTRY, &acl_entry);
583 acl_get_tag_type(acl_entry, &acl_tag);
586 ae_id = (int)*(uid_t *)acl_get_qualifier(acl_entry);
587 ae_name = archive_read_disk_uname(&a->archive, ae_id);
588 ae_tag = ARCHIVE_ENTRY_ACL_USER;
591 ae_id = (int)*(gid_t *)acl_get_qualifier(acl_entry);
592 ae_name = archive_read_disk_gname(&a->archive, ae_id);
593 ae_tag = ARCHIVE_ENTRY_ACL_GROUP;
596 ae_tag = ARCHIVE_ENTRY_ACL_MASK;
599 ae_tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
602 ae_tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
605 ae_tag = ARCHIVE_ENTRY_ACL_OTHER;
609 ae_tag = ARCHIVE_ENTRY_ACL_EVERYONE;
613 /* Skip types that libarchive can't support. */
614 s = acl_get_entry(acl, ACL_NEXT_ENTRY, &acl_entry);
618 // XXX acl type maps to allow/deny/audit/YYYY bits
619 // XXX acl_get_entry_type_np on FreeBSD returns EINVAL for
621 entry_acl_type = default_entry_acl_type;
623 r = acl_get_entry_type_np(acl_entry, &acl_type);
626 case ACL_ENTRY_TYPE_ALLOW:
627 entry_acl_type = ARCHIVE_ENTRY_ACL_TYPE_ALLOW;
629 case ACL_ENTRY_TYPE_DENY:
630 entry_acl_type = ARCHIVE_ENTRY_ACL_TYPE_DENY;
632 case ACL_ENTRY_TYPE_AUDIT:
633 entry_acl_type = ARCHIVE_ENTRY_ACL_TYPE_AUDIT;
635 case ACL_ENTRY_TYPE_ALARM:
636 entry_acl_type = ARCHIVE_ENTRY_ACL_TYPE_ALARM;
642 * Libarchive stores "flag" (NFSv4 inheritance bits)
643 * in the ae_perm bitmap.
645 acl_get_flagset_np(acl_entry, &acl_flagset);
646 for (i = 0; i < (int)(sizeof(acl_inherit_map) / sizeof(acl_inherit_map[0])); ++i) {
647 if (acl_get_flag_np(acl_flagset,
648 acl_inherit_map[i].platform_inherit))
649 ae_perm |= acl_inherit_map[i].archive_inherit;
654 acl_get_permset(acl_entry, &acl_permset);
655 for (i = 0; i < (int)(sizeof(acl_perm_map) / sizeof(acl_perm_map[0])); ++i) {
657 * acl_get_perm() is spelled differently on different
658 * platforms; see above.
660 if (ACL_GET_PERM(acl_permset, acl_perm_map[i].platform_perm))
661 ae_perm |= acl_perm_map[i].archive_perm;
664 archive_entry_acl_add_entry(entry, entry_acl_type,
668 s = acl_get_entry(acl, ACL_NEXT_ENTRY, &acl_entry);
674 setup_acls(struct archive_read_disk *a,
675 struct archive_entry *entry, int *fd)
677 (void)a; /* UNUSED */
678 (void)entry; /* UNUSED */
679 (void)fd; /* UNUSED */
684 #if (HAVE_FGETXATTR && HAVE_FLISTXATTR && HAVE_LISTXATTR && \
685 HAVE_LLISTXATTR && HAVE_GETXATTR && HAVE_LGETXATTR) || \
686 (HAVE_FGETEA && HAVE_FLISTEA && HAVE_LISTEA)
689 * Linux and AIX extended attribute support.
691 * TODO: By using a stack-allocated buffer for the first
692 * call to getxattr(), we might be able to avoid the second
693 * call entirely. We only need the second call if the
694 * stack-allocated buffer is too small. But a modest buffer
695 * of 1024 bytes or so will often be big enough. Same applies
701 setup_xattr(struct archive_read_disk *a,
702 struct archive_entry *entry, const char *name, int fd)
708 accpath = archive_entry_sourcepath(entry);
710 accpath = archive_entry_pathname(entry);
714 size = fgetxattr(fd, name, NULL, 0);
715 else if (!a->follow_symlinks)
716 size = lgetxattr(accpath, name, NULL, 0);
718 size = getxattr(accpath, name, NULL, 0);
721 size = fgetea(fd, name, NULL, 0);
722 else if (!a->follow_symlinks)
723 size = lgetea(accpath, name, NULL, 0);
725 size = getea(accpath, name, NULL, 0);
729 archive_set_error(&a->archive, errno,
730 "Couldn't query extended attribute");
731 return (ARCHIVE_WARN);
734 if (size > 0 && (value = malloc(size)) == NULL) {
735 archive_set_error(&a->archive, errno, "Out of memory");
736 return (ARCHIVE_FATAL);
741 size = fgetxattr(fd, name, value, size);
742 else if (!a->follow_symlinks)
743 size = lgetxattr(accpath, name, value, size);
745 size = getxattr(accpath, name, value, size);
748 size = fgetea(fd, name, value, size);
749 else if (!a->follow_symlinks)
750 size = lgetea(accpath, name, value, size);
752 size = getea(accpath, name, value, size);
756 archive_set_error(&a->archive, errno,
757 "Couldn't read extended attribute");
758 return (ARCHIVE_WARN);
761 archive_entry_xattr_add_entry(entry, name, value, size);
768 setup_xattrs(struct archive_read_disk *a,
769 struct archive_entry *entry, int *fd)
775 path = archive_entry_sourcepath(entry);
777 path = archive_entry_pathname(entry);
779 if (*fd < 0 && a->tree != NULL) {
780 if (a->follow_symlinks ||
781 archive_entry_filetype(entry) != AE_IFLNK)
782 *fd = a->open_on_current_dir(a->tree, path,
783 O_RDONLY | O_NONBLOCK);
785 if (a->tree_enter_working_dir(a->tree) != 0) {
786 archive_set_error(&a->archive, errno,
787 "Couldn't access %s", path);
788 return (ARCHIVE_FAILED);
795 list_size = flistxattr(*fd, NULL, 0);
796 else if (!a->follow_symlinks)
797 list_size = llistxattr(path, NULL, 0);
799 list_size = listxattr(path, NULL, 0);
802 list_size = flistea(*fd, NULL, 0);
803 else if (!a->follow_symlinks)
804 list_size = llistea(path, NULL, 0);
806 list_size = listea(path, NULL, 0);
809 if (list_size == -1) {
810 if (errno == ENOTSUP || errno == ENOSYS)
812 archive_set_error(&a->archive, errno,
813 "Couldn't list extended attributes");
814 return (ARCHIVE_WARN);
820 if ((list = malloc(list_size)) == NULL) {
821 archive_set_error(&a->archive, errno, "Out of memory");
822 return (ARCHIVE_FATAL);
827 list_size = flistxattr(*fd, list, list_size);
828 else if (!a->follow_symlinks)
829 list_size = llistxattr(path, list, list_size);
831 list_size = listxattr(path, list, list_size);
834 list_size = flistea(*fd, list, list_size);
835 else if (!a->follow_symlinks)
836 list_size = llistea(path, list, list_size);
838 list_size = listea(path, list, list_size);
841 if (list_size == -1) {
842 archive_set_error(&a->archive, errno,
843 "Couldn't retrieve extended attributes");
845 return (ARCHIVE_WARN);
848 for (p = list; (p - list) < list_size; p += strlen(p) + 1) {
849 if (strncmp(p, "system.", 7) == 0 ||
850 strncmp(p, "xfsroot.", 8) == 0)
852 setup_xattr(a, entry, p, *fd);
859 #elif HAVE_EXTATTR_GET_FILE && HAVE_EXTATTR_LIST_FILE && \
860 HAVE_DECL_EXTATTR_NAMESPACE_USER
863 * FreeBSD extattr interface.
866 /* TODO: Implement this. Follow the Linux model above, but
867 * with FreeBSD-specific system calls, of course. Be careful
868 * to not include the system extattrs that hold ACLs; we handle
872 setup_xattr(struct archive_read_disk *a, struct archive_entry *entry,
873 int namespace, const char *name, const char *fullname, int fd);
876 setup_xattr(struct archive_read_disk *a, struct archive_entry *entry,
877 int namespace, const char *name, const char *fullname, int fd)
883 accpath = archive_entry_sourcepath(entry);
885 accpath = archive_entry_pathname(entry);
888 size = extattr_get_fd(fd, namespace, name, NULL, 0);
889 else if (!a->follow_symlinks)
890 size = extattr_get_link(accpath, namespace, name, NULL, 0);
892 size = extattr_get_file(accpath, namespace, name, NULL, 0);
895 archive_set_error(&a->archive, errno,
896 "Couldn't query extended attribute");
897 return (ARCHIVE_WARN);
900 if (size > 0 && (value = malloc(size)) == NULL) {
901 archive_set_error(&a->archive, errno, "Out of memory");
902 return (ARCHIVE_FATAL);
906 size = extattr_get_fd(fd, namespace, name, value, size);
907 else if (!a->follow_symlinks)
908 size = extattr_get_link(accpath, namespace, name, value, size);
910 size = extattr_get_file(accpath, namespace, name, value, size);
914 archive_set_error(&a->archive, errno,
915 "Couldn't read extended attribute");
916 return (ARCHIVE_WARN);
919 archive_entry_xattr_add_entry(entry, fullname, value, size);
926 setup_xattrs(struct archive_read_disk *a,
927 struct archive_entry *entry, int *fd)
933 int namespace = EXTATTR_NAMESPACE_USER;
935 path = archive_entry_sourcepath(entry);
937 path = archive_entry_pathname(entry);
939 if (*fd < 0 && a->tree != NULL) {
940 if (a->follow_symlinks ||
941 archive_entry_filetype(entry) != AE_IFLNK)
942 *fd = a->open_on_current_dir(a->tree, path,
943 O_RDONLY | O_NONBLOCK);
945 if (a->tree_enter_working_dir(a->tree) != 0) {
946 archive_set_error(&a->archive, errno,
947 "Couldn't access %s", path);
948 return (ARCHIVE_FAILED);
954 list_size = extattr_list_fd(*fd, namespace, NULL, 0);
955 else if (!a->follow_symlinks)
956 list_size = extattr_list_link(path, namespace, NULL, 0);
958 list_size = extattr_list_file(path, namespace, NULL, 0);
960 if (list_size == -1 && errno == EOPNOTSUPP)
962 if (list_size == -1) {
963 archive_set_error(&a->archive, errno,
964 "Couldn't list extended attributes");
965 return (ARCHIVE_WARN);
971 if ((list = malloc(list_size)) == NULL) {
972 archive_set_error(&a->archive, errno, "Out of memory");
973 return (ARCHIVE_FATAL);
977 list_size = extattr_list_fd(*fd, namespace, list, list_size);
978 else if (!a->follow_symlinks)
979 list_size = extattr_list_link(path, namespace, list, list_size);
981 list_size = extattr_list_file(path, namespace, list, list_size);
983 if (list_size == -1) {
984 archive_set_error(&a->archive, errno,
985 "Couldn't retrieve extended attributes");
987 return (ARCHIVE_WARN);
991 while ((p - list) < list_size) {
992 size_t len = 255 & (int)*p;
995 strcpy(buff, "user.");
996 name = buff + strlen(buff);
997 memcpy(name, p + 1, len);
999 setup_xattr(a, entry, namespace, name, buff, *fd);
1004 return (ARCHIVE_OK);
1010 * Generic (stub) extended attribute support.
1013 setup_xattrs(struct archive_read_disk *a,
1014 struct archive_entry *entry, int *fd)
1016 (void)a; /* UNUSED */
1017 (void)entry; /* UNUSED */
1018 (void)fd; /* UNUSED */
1019 return (ARCHIVE_OK);
1024 #if defined(HAVE_LINUX_FIEMAP_H)
1027 * Linux sparse interface.
1029 * The FIEMAP ioctl returns an "extent" for each physical allocation
1030 * on disk. We need to process those to generate a more compact list
1031 * of logical file blocks. We also need to be very careful to use
1032 * FIEMAP_FLAG_SYNC here, since there are reports that Linux sometimes
1033 * does not report allocations for newly-written data that hasn't
1034 * been synced to disk.
1036 * It's important to return a minimal sparse file list because we want
1037 * to not trigger sparse file extensions if we don't have to, since
1038 * not all readers support them.
1042 setup_sparse(struct archive_read_disk *a,
1043 struct archive_entry *entry, int *fd)
1047 struct fiemap_extent *fe;
1049 int count, do_fiemap, iters;
1050 int exit_sts = ARCHIVE_OK;
1052 if (archive_entry_filetype(entry) != AE_IFREG
1053 || archive_entry_size(entry) <= 0
1054 || archive_entry_hardlink(entry) != NULL)
1055 return (ARCHIVE_OK);
1060 path = archive_entry_sourcepath(entry);
1062 path = archive_entry_pathname(entry);
1063 if (a->tree != NULL)
1064 *fd = a->open_on_current_dir(a->tree, path,
1065 O_RDONLY | O_NONBLOCK | O_CLOEXEC);
1067 *fd = open(path, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
1069 archive_set_error(&a->archive, errno,
1070 "Can't open `%s'", path);
1071 return (ARCHIVE_FAILED);
1073 __archive_ensure_cloexec_flag(*fd);
1076 /* Initialize buffer to avoid the error valgrind complains about. */
1077 memset(buff, 0, sizeof(buff));
1078 count = (sizeof(buff) - sizeof(*fm))/sizeof(*fe);
1079 fm = (struct fiemap *)buff;
1081 fm->fm_length = ~0ULL;;
1082 fm->fm_flags = FIEMAP_FLAG_SYNC;
1083 fm->fm_extent_count = count;
1085 size = archive_entry_size(entry);
1086 for (iters = 0; ; ++iters) {
1089 r = ioctl(*fd, FS_IOC_FIEMAP, fm);
1091 /* When something error happens, it is better we
1092 * should return ARCHIVE_OK because an earlier
1093 * version(<2.6.28) cannot perfom FS_IOC_FIEMAP. */
1094 goto exit_setup_sparse;
1096 if (fm->fm_mapped_extents == 0) {
1098 /* Fully sparse file; insert a zero-length "data" entry */
1099 archive_entry_sparse_add_entry(entry, 0, 0);
1103 fe = fm->fm_extents;
1104 for (i = 0; i < (int)fm->fm_mapped_extents; i++, fe++) {
1105 if (!(fe->fe_flags & FIEMAP_EXTENT_UNWRITTEN)) {
1106 /* The fe_length of the last block does not
1107 * adjust itself to its size files. */
1108 int64_t length = fe->fe_length;
1109 if (fe->fe_logical + length > (uint64_t)size)
1110 length -= fe->fe_logical + length - size;
1111 if (fe->fe_logical == 0 && length == size) {
1112 /* This is not sparse. */
1117 archive_entry_sparse_add_entry(entry,
1118 fe->fe_logical, length);
1120 if (fe->fe_flags & FIEMAP_EXTENT_LAST)
1124 fe = fm->fm_extents + fm->fm_mapped_extents -1;
1125 fm->fm_start = fe->fe_logical + fe->fe_length;
1133 #elif defined(SEEK_HOLE) && defined(SEEK_DATA) && defined(_PC_MIN_HOLE_SIZE)
1136 * FreeBSD and Solaris sparse interface.
1140 setup_sparse(struct archive_read_disk *a,
1141 struct archive_entry *entry, int *fd)
1144 off_t initial_off; /* FreeBSD/Solaris only, so off_t okay here */
1145 off_t off_s, off_e; /* FreeBSD/Solaris only, so off_t okay here */
1146 int exit_sts = ARCHIVE_OK;
1147 int check_fully_sparse = 0;
1149 if (archive_entry_filetype(entry) != AE_IFREG
1150 || archive_entry_size(entry) <= 0
1151 || archive_entry_hardlink(entry) != NULL)
1152 return (ARCHIVE_OK);
1154 /* Does filesystem support the reporting of hole ? */
1155 if (*fd < 0 && a->tree != NULL) {
1158 path = archive_entry_sourcepath(entry);
1160 path = archive_entry_pathname(entry);
1161 *fd = a->open_on_current_dir(a->tree, path,
1162 O_RDONLY | O_NONBLOCK);
1164 archive_set_error(&a->archive, errno,
1165 "Can't open `%s'", path);
1166 return (ARCHIVE_FAILED);
1171 if (fpathconf(*fd, _PC_MIN_HOLE_SIZE) <= 0)
1172 return (ARCHIVE_OK);
1173 initial_off = lseek(*fd, 0, SEEK_CUR);
1174 if (initial_off != 0)
1175 lseek(*fd, 0, SEEK_SET);
1179 path = archive_entry_sourcepath(entry);
1181 path = archive_entry_pathname(entry);
1183 if (pathconf(path, _PC_MIN_HOLE_SIZE) <= 0)
1184 return (ARCHIVE_OK);
1185 *fd = open(path, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
1187 archive_set_error(&a->archive, errno,
1188 "Can't open `%s'", path);
1189 return (ARCHIVE_FAILED);
1191 __archive_ensure_cloexec_flag(*fd);
1196 size = archive_entry_size(entry);
1197 while (off_s < size) {
1198 off_s = lseek(*fd, off_s, SEEK_DATA);
1199 if (off_s == (off_t)-1) {
1200 if (errno == ENXIO) {
1202 if (archive_entry_sparse_count(entry) == 0) {
1203 /* Potentially a fully-sparse file. */
1204 check_fully_sparse = 1;
1208 archive_set_error(&a->archive, errno,
1209 "lseek(SEEK_HOLE) failed");
1210 exit_sts = ARCHIVE_FAILED;
1211 goto exit_setup_sparse;
1213 off_e = lseek(*fd, off_s, SEEK_HOLE);
1214 if (off_e == (off_t)-1) {
1215 if (errno == ENXIO) {
1216 off_e = lseek(*fd, 0, SEEK_END);
1217 if (off_e != (off_t)-1)
1218 break;/* no more data */
1220 archive_set_error(&a->archive, errno,
1221 "lseek(SEEK_DATA) failed");
1222 exit_sts = ARCHIVE_FAILED;
1223 goto exit_setup_sparse;
1225 if (off_s == 0 && off_e == size)
1226 break;/* This is not spase. */
1227 archive_entry_sparse_add_entry(entry, off_s,
1232 if (check_fully_sparse) {
1233 if (lseek(*fd, 0, SEEK_HOLE) == 0 &&
1234 lseek(*fd, 0, SEEK_END) == size) {
1235 /* Fully sparse file; insert a zero-length "data" entry */
1236 archive_entry_sparse_add_entry(entry, 0, 0);
1240 lseek(*fd, initial_off, SEEK_SET);
1247 * Generic (stub) sparse support.
1250 setup_sparse(struct archive_read_disk *a,
1251 struct archive_entry *entry, int *fd)
1253 (void)a; /* UNUSED */
1254 (void)entry; /* UNUSED */
1255 (void)fd; /* UNUSED */
1256 return (ARCHIVE_OK);
1261 #endif /* !defined(_WIN32) || defined(__CYGWIN__) */