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 #ifdef HAVE_SYS_XATTR_H
53 #include <sys/xattr.h>
58 #ifdef HAVE_ACL_LIBACL_H
59 #include <acl/libacl.h>
61 #ifdef HAVE_ATTR_XATTR_H
62 #include <attr/xattr.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"
108 * Linux and FreeBSD plug this obvious hole in POSIX.1e in
111 #if HAVE_ACL_GET_PERM
112 #define ACL_GET_PERM acl_get_perm
113 #elif HAVE_ACL_GET_PERM_NP
114 #define ACL_GET_PERM acl_get_perm_np
117 static int setup_acls(struct archive_read_disk *,
118 struct archive_entry *, int *fd);
119 static int setup_mac_metadata(struct archive_read_disk *,
120 struct archive_entry *, int *fd);
121 static int setup_xattrs(struct archive_read_disk *,
122 struct archive_entry *, int *fd);
123 static int setup_sparse(struct archive_read_disk *,
124 struct archive_entry *, int *fd);
127 archive_read_disk_entry_from_file(struct archive *_a,
128 struct archive_entry *entry,
130 const struct stat *st)
132 struct archive_read_disk *a = (struct archive_read_disk *)_a;
133 const char *path, *name;
138 archive_clear_error(_a);
139 path = archive_entry_sourcepath(entry);
141 path = archive_entry_pathname(entry);
143 if (a->tree == NULL) {
147 if (fstat(fd, &s) != 0) {
148 archive_set_error(&a->archive, errno,
150 return (ARCHIVE_FAILED);
155 if (!a->follow_symlinks) {
156 if (lstat(path, &s) != 0) {
157 archive_set_error(&a->archive, errno,
158 "Can't lstat %s", path);
159 return (ARCHIVE_FAILED);
163 if (stat(path, &s) != 0) {
164 archive_set_error(&a->archive, errno,
165 "Can't stat %s", path);
166 return (ARCHIVE_FAILED);
170 archive_entry_copy_stat(entry, st);
173 /* Lookup uname/gname */
174 name = archive_read_disk_uname(_a, archive_entry_uid(entry));
176 archive_entry_copy_uname(entry, name);
177 name = archive_read_disk_gname(_a, archive_entry_gid(entry));
179 archive_entry_copy_gname(entry, name);
181 #ifdef HAVE_STRUCT_STAT_ST_FLAGS
182 /* On FreeBSD, we get flags for free with the stat. */
183 /* TODO: Does this belong in copy_stat()? */
184 if (st->st_flags != 0)
185 archive_entry_set_fflags(entry, st->st_flags, 0);
188 #if defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS)
189 /* Linux requires an extra ioctl to pull the flags. Although
190 * this is an extra step, it has a nice side-effect: We get an
191 * open file descriptor which we can use in the subsequent lookups. */
192 if ((S_ISREG(st->st_mode) || S_ISDIR(st->st_mode))) {
195 fd = a->open_on_current_dir(a->tree, path,
196 O_RDONLY | O_NONBLOCK);
198 fd = open(path, O_RDONLY | O_NONBLOCK);
201 unsigned long stflags;
202 r = ioctl(fd, EXT2_IOC_GETFLAGS, &stflags);
203 if (r == 0 && stflags != 0)
204 archive_entry_set_fflags(entry, stflags, 0);
209 #if defined(HAVE_READLINK) || defined(HAVE_READLINKAT)
210 if (S_ISLNK(st->st_mode)) {
211 size_t linkbuffer_len = st->st_size + 1;
215 linkbuffer = malloc(linkbuffer_len);
216 if (linkbuffer == NULL) {
217 archive_set_error(&a->archive, ENOMEM,
218 "Couldn't read link data");
219 return (ARCHIVE_FAILED);
221 if (a->tree != NULL) {
222 #ifdef HAVE_READLINKAT
223 lnklen = readlinkat(a->tree_current_dir_fd(a->tree),
224 path, linkbuffer, linkbuffer_len);
226 if (a->tree_enter_working_dir(a->tree) != 0) {
227 archive_set_error(&a->archive, errno,
228 "Couldn't read link data");
230 return (ARCHIVE_FAILED);
232 lnklen = readlink(path, linkbuffer, linkbuffer_len);
233 #endif /* HAVE_READLINKAT */
235 lnklen = readlink(path, linkbuffer, linkbuffer_len);
237 archive_set_error(&a->archive, errno,
238 "Couldn't read link data");
240 return (ARCHIVE_FAILED);
242 linkbuffer[lnklen] = 0;
243 archive_entry_set_symlink(entry, linkbuffer);
246 #endif /* HAVE_READLINK || HAVE_READLINKAT */
248 r = setup_acls(a, entry, &fd);
249 r1 = setup_xattrs(a, entry, &fd);
252 if (a->enable_copyfile) {
253 r1 = setup_mac_metadata(a, entry, &fd);
257 r1 = setup_sparse(a, entry, &fd);
261 /* If we opened the file earlier in this function, close it. */
262 if (initial_fd != fd)
267 #if defined(__APPLE__) && defined(HAVE_COPYFILE_H)
269 * The Mac OS "copyfile()" API copies the extended metadata for a
270 * file into a separate file in AppleDouble format (see RFC 1740).
272 * Mac OS tar and cpio implementations store this extended
273 * metadata as a separate entry just before the regular entry
274 * with a "._" prefix added to the filename.
276 * Note that this is currently done unconditionally; the tar program has
277 * an option to discard this information before the archive is written.
279 * TODO: If there's a failure, report it and return ARCHIVE_WARN.
282 setup_mac_metadata(struct archive_read_disk *a,
283 struct archive_entry *entry, int *fd)
286 int copyfile_flags = COPYFILE_NOFOLLOW | COPYFILE_ACL | COPYFILE_XATTR;
287 struct stat copyfile_stat;
288 int ret = ARCHIVE_OK;
291 const char *name, *tempdir, *tempfile = NULL;
293 (void)fd; /* UNUSED */
294 name = archive_entry_sourcepath(entry);
296 name = archive_entry_pathname(entry);
298 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
299 "Can't open file to read extended attributes: No name");
300 return (ARCHIVE_WARN);
303 if (a->tree != NULL) {
304 if (a->tree_enter_working_dir(a->tree) != 0) {
305 archive_set_error(&a->archive, errno,
306 "Couldn't change dir");
307 return (ARCHIVE_FAILED);
311 /* Short-circuit if there's nothing to do. */
312 have_attrs = copyfile(name, NULL, 0, copyfile_flags | COPYFILE_CHECK);
313 if (have_attrs == -1) {
314 archive_set_error(&a->archive, errno,
315 "Could not check extended attributes");
316 return (ARCHIVE_WARN);
322 if (issetugid() == 0)
323 tempdir = getenv("TMPDIR");
326 tempfile = tempnam(tempdir, "tar.md.");
328 /* XXX I wish copyfile() could pack directly to a memory
329 * buffer; that would avoid the temp file here. For that
330 * matter, it would be nice if fcopyfile() actually worked,
331 * that would reduce the many open/close races here. */
332 if (copyfile(name, tempfile, 0, copyfile_flags | COPYFILE_PACK)) {
333 archive_set_error(&a->archive, errno,
334 "Could not pack extended attributes");
338 tempfd = open(tempfile, O_RDONLY);
340 archive_set_error(&a->archive, errno,
341 "Could not open extended attribute file");
345 if (fstat(tempfd, ©file_stat)) {
346 archive_set_error(&a->archive, errno,
347 "Could not check size of extended attributes");
351 buff = malloc(copyfile_stat.st_size);
353 archive_set_error(&a->archive, errno,
354 "Could not allocate memory for extended attributes");
358 if (copyfile_stat.st_size != read(tempfd, buff, copyfile_stat.st_size)) {
359 archive_set_error(&a->archive, errno,
360 "Could not read extended attributes into memory");
364 archive_entry_copy_mac_metadata(entry, buff, copyfile_stat.st_size);
369 if (tempfile != NULL)
377 * Stub implementation for non-Mac systems.
380 setup_mac_metadata(struct archive_read_disk *a,
381 struct archive_entry *entry, int *fd)
383 (void)a; /* UNUSED */
384 (void)entry; /* UNUSED */
385 (void)fd; /* UNUSED */
391 #ifdef HAVE_POSIX_ACL
392 static int translate_acl(struct archive_read_disk *a,
393 struct archive_entry *entry, acl_t acl, int archive_entry_acl_type);
396 setup_acls(struct archive_read_disk *a,
397 struct archive_entry *entry, int *fd)
403 accpath = archive_entry_sourcepath(entry);
405 accpath = archive_entry_pathname(entry);
407 archive_entry_acl_clear(entry);
409 /* Try NFS4 ACL first. */
411 acl = acl_get_fd(*fd);
412 #if HAVE_ACL_GET_LINK_NP
413 else if (!a->follow_symlinks)
414 acl = acl_get_link_np(accpath, ACL_TYPE_NFS4);
416 else if ((!a->follow_symlinks)
417 && (archive_entry_filetype(entry) == AE_IFLNK))
418 /* We can't get the ACL of a symlink, so we assume it can't
423 acl = acl_get_file(accpath, ACL_TYPE_NFS4);
424 #if HAVE_ACL_IS_TRIVIAL_NP
425 /* Ignore "trivial" ACLs that just mirror the file mode. */
426 acl_is_trivial_np(acl, &r);
433 translate_acl(a, entry, acl, ARCHIVE_ENTRY_ACL_TYPE_NFS4);
438 /* Retrieve access ACL from file. */
440 acl = acl_get_fd(*fd);
441 #if HAVE_ACL_GET_LINK_NP
442 else if (!a->follow_symlinks)
443 acl = acl_get_link_np(accpath, ACL_TYPE_ACCESS);
445 else if ((!a->follow_symlinks)
446 && (archive_entry_filetype(entry) == AE_IFLNK))
447 /* We can't get the ACL of a symlink, so we assume it can't
452 acl = acl_get_file(accpath, ACL_TYPE_ACCESS);
454 translate_acl(a, entry, acl,
455 ARCHIVE_ENTRY_ACL_TYPE_ACCESS);
459 /* Only directories can have default ACLs. */
460 if (S_ISDIR(archive_entry_mode(entry))) {
461 acl = acl_get_file(accpath, ACL_TYPE_DEFAULT);
463 translate_acl(a, entry, acl,
464 ARCHIVE_ENTRY_ACL_TYPE_DEFAULT);
472 * Translate system ACL into libarchive internal structure.
479 {ARCHIVE_ENTRY_ACL_EXECUTE, ACL_EXECUTE},
480 {ARCHIVE_ENTRY_ACL_WRITE, ACL_WRITE},
481 {ARCHIVE_ENTRY_ACL_READ, ACL_READ},
482 {ARCHIVE_ENTRY_ACL_READ_DATA, ACL_READ_DATA},
483 {ARCHIVE_ENTRY_ACL_LIST_DIRECTORY, ACL_LIST_DIRECTORY},
484 {ARCHIVE_ENTRY_ACL_WRITE_DATA, ACL_WRITE_DATA},
485 {ARCHIVE_ENTRY_ACL_ADD_FILE, ACL_ADD_FILE},
486 {ARCHIVE_ENTRY_ACL_APPEND_DATA, ACL_APPEND_DATA},
487 {ARCHIVE_ENTRY_ACL_ADD_SUBDIRECTORY, ACL_ADD_SUBDIRECTORY},
488 {ARCHIVE_ENTRY_ACL_READ_NAMED_ATTRS, ACL_READ_NAMED_ATTRS},
489 {ARCHIVE_ENTRY_ACL_WRITE_NAMED_ATTRS, ACL_WRITE_NAMED_ATTRS},
490 {ARCHIVE_ENTRY_ACL_DELETE_CHILD, ACL_DELETE_CHILD},
491 {ARCHIVE_ENTRY_ACL_READ_ATTRIBUTES, ACL_READ_ATTRIBUTES},
492 {ARCHIVE_ENTRY_ACL_WRITE_ATTRIBUTES, ACL_WRITE_ATTRIBUTES},
493 {ARCHIVE_ENTRY_ACL_DELETE, ACL_DELETE},
494 {ARCHIVE_ENTRY_ACL_READ_ACL, ACL_READ_ACL},
495 {ARCHIVE_ENTRY_ACL_WRITE_ACL, ACL_WRITE_ACL},
496 {ARCHIVE_ENTRY_ACL_WRITE_OWNER, ACL_WRITE_OWNER},
497 {ARCHIVE_ENTRY_ACL_SYNCHRONIZE, ACL_SYNCHRONIZE}
502 int platform_inherit;
503 } acl_inherit_map[] = {
504 {ARCHIVE_ENTRY_ACL_ENTRY_FILE_INHERIT, ACL_ENTRY_FILE_INHERIT},
505 {ARCHIVE_ENTRY_ACL_ENTRY_DIRECTORY_INHERIT, ACL_ENTRY_DIRECTORY_INHERIT},
506 {ARCHIVE_ENTRY_ACL_ENTRY_NO_PROPAGATE_INHERIT, ACL_ENTRY_NO_PROPAGATE_INHERIT},
507 {ARCHIVE_ENTRY_ACL_ENTRY_INHERIT_ONLY, ACL_ENTRY_INHERIT_ONLY}
511 translate_acl(struct archive_read_disk *a,
512 struct archive_entry *entry, acl_t acl, int default_entry_acl_type)
515 acl_entry_type_t acl_type;
516 acl_flagset_t acl_flagset;
517 acl_entry_t acl_entry;
518 acl_permset_t acl_permset;
519 int brand, i, r, entry_acl_type;
520 int s, ae_id, ae_tag, ae_perm;
524 // FreeBSD "brands" ACLs as POSIX.1e or NFSv4
525 // Make sure the "brand" on this ACL is consistent
526 // with the default_entry_acl_type bits provided.
527 acl_get_brand_np(acl, &brand);
529 case ACL_BRAND_POSIX:
530 switch (default_entry_acl_type) {
531 case ARCHIVE_ENTRY_ACL_TYPE_ACCESS:
532 case ARCHIVE_ENTRY_ACL_TYPE_DEFAULT:
533 entry_acl_type = default_entry_acl_type;
536 // XXX set warning message?
537 return ARCHIVE_FAILED;
541 if (default_entry_acl_type & ~ARCHIVE_ENTRY_ACL_TYPE_NFS4) {
542 // XXX set warning message?
543 return ARCHIVE_FAILED;
547 // XXX set warning message?
548 return ARCHIVE_FAILED;
553 s = acl_get_entry(acl, ACL_FIRST_ENTRY, &acl_entry);
559 acl_get_tag_type(acl_entry, &acl_tag);
562 ae_id = (int)*(uid_t *)acl_get_qualifier(acl_entry);
563 ae_name = archive_read_disk_uname(&a->archive, ae_id);
564 ae_tag = ARCHIVE_ENTRY_ACL_USER;
567 ae_id = (int)*(gid_t *)acl_get_qualifier(acl_entry);
568 ae_name = archive_read_disk_gname(&a->archive, ae_id);
569 ae_tag = ARCHIVE_ENTRY_ACL_GROUP;
572 ae_tag = ARCHIVE_ENTRY_ACL_MASK;
575 ae_tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
578 ae_tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
581 ae_tag = ARCHIVE_ENTRY_ACL_OTHER;
584 ae_tag = ARCHIVE_ENTRY_ACL_EVERYONE;
587 /* Skip types that libarchive can't support. */
588 s = acl_get_entry(acl, ACL_NEXT_ENTRY, &acl_entry);
592 // XXX acl type maps to allow/deny/audit/YYYY bits
593 // XXX acl_get_entry_type_np on FreeBSD returns EINVAL for
595 entry_acl_type = default_entry_acl_type;
596 r = acl_get_entry_type_np(acl_entry, &acl_type);
599 case ACL_ENTRY_TYPE_ALLOW:
600 entry_acl_type = ARCHIVE_ENTRY_ACL_TYPE_ALLOW;
602 case ACL_ENTRY_TYPE_DENY:
603 entry_acl_type = ARCHIVE_ENTRY_ACL_TYPE_DENY;
605 case ACL_ENTRY_TYPE_AUDIT:
606 entry_acl_type = ARCHIVE_ENTRY_ACL_TYPE_AUDIT;
608 case ACL_ENTRY_TYPE_ALARM:
609 entry_acl_type = ARCHIVE_ENTRY_ACL_TYPE_ALARM;
615 * Libarchive stores "flag" (NFSv4 inheritance bits)
616 * in the ae_perm bitmap.
618 acl_get_flagset_np(acl_entry, &acl_flagset);
619 for (i = 0; i < (int)(sizeof(acl_inherit_map) / sizeof(acl_inherit_map[0])); ++i) {
620 if (acl_get_flag_np(acl_flagset,
621 acl_inherit_map[i].platform_inherit))
622 ae_perm |= acl_inherit_map[i].archive_inherit;
626 acl_get_permset(acl_entry, &acl_permset);
627 for (i = 0; i < (int)(sizeof(acl_perm_map) / sizeof(acl_perm_map[0])); ++i) {
629 * acl_get_perm() is spelled differently on different
630 * platforms; see above.
632 if (ACL_GET_PERM(acl_permset, acl_perm_map[i].platform_perm))
633 ae_perm |= acl_perm_map[i].archive_perm;
636 archive_entry_acl_add_entry(entry, entry_acl_type,
640 s = acl_get_entry(acl, ACL_NEXT_ENTRY, &acl_entry);
646 setup_acls(struct archive_read_disk *a,
647 struct archive_entry *entry, int fd)
649 (void)a; /* UNUSED */
650 (void)entry; /* UNUSED */
651 (void)fd; /* UNUSED */
656 #if (HAVE_FGETXATTR && HAVE_FLISTXATTR && HAVE_LISTXATTR && \
657 HAVE_LLISTXATTR && HAVE_GETXATTR && HAVE_LGETXATTR) || \
658 (HAVE_FGETEA && HAVE_FLISTEA && HAVE_LISTEA)
661 * Linux and AIX extended attribute support.
663 * TODO: By using a stack-allocated buffer for the first
664 * call to getxattr(), we might be able to avoid the second
665 * call entirely. We only need the second call if the
666 * stack-allocated buffer is too small. But a modest buffer
667 * of 1024 bytes or so will often be big enough. Same applies
673 setup_xattr(struct archive_read_disk *a,
674 struct archive_entry *entry, const char *name, int fd)
680 accpath = archive_entry_sourcepath(entry);
682 accpath = archive_entry_pathname(entry);
686 size = fgetxattr(fd, name, NULL, 0);
687 else if (!a->follow_symlinks)
688 size = lgetxattr(accpath, name, NULL, 0);
690 size = getxattr(accpath, name, NULL, 0);
693 size = fgetea(fd, name, NULL, 0);
694 else if (!a->follow_symlinks)
695 size = lgetea(accpath, name, NULL, 0);
697 size = getea(accpath, name, NULL, 0);
701 archive_set_error(&a->archive, errno,
702 "Couldn't query extended attribute");
703 return (ARCHIVE_WARN);
706 if (size > 0 && (value = malloc(size)) == NULL) {
707 archive_set_error(&a->archive, errno, "Out of memory");
708 return (ARCHIVE_FATAL);
713 size = fgetxattr(fd, name, value, size);
714 else if (!a->follow_symlinks)
715 size = lgetxattr(accpath, name, value, size);
717 size = getxattr(accpath, name, value, size);
720 size = fgetea(fd, name, value, size);
721 else if (!a->follow_symlinks)
722 size = lgetea(accpath, name, value, size);
724 size = getea(accpath, name, value, size);
728 archive_set_error(&a->archive, errno,
729 "Couldn't read extended attribute");
730 return (ARCHIVE_WARN);
733 archive_entry_xattr_add_entry(entry, name, value, size);
740 setup_xattrs(struct archive_read_disk *a,
741 struct archive_entry *entry, int *fd)
747 path = archive_entry_sourcepath(entry);
749 path = archive_entry_pathname(entry);
751 if (*fd < 0 && a->tree != NULL) {
752 if (a->follow_symlinks ||
753 archive_entry_filetype(entry) != AE_IFLNK)
754 *fd = a->open_on_current_dir(a->tree, path,
755 O_RDONLY | O_NONBLOCK);
757 if (a->tree_enter_working_dir(a->tree) != 0) {
758 archive_set_error(&a->archive, errno,
759 "Couldn't access %s", path);
760 return (ARCHIVE_FAILED);
767 list_size = flistxattr(*fd, NULL, 0);
768 else if (!a->follow_symlinks)
769 list_size = llistxattr(path, NULL, 0);
771 list_size = listxattr(path, NULL, 0);
774 list_size = flistea(*fd, NULL, 0);
775 else if (!a->follow_symlinks)
776 list_size = llistea(path, NULL, 0);
778 list_size = listea(path, NULL, 0);
781 if (list_size == -1) {
782 if (errno == ENOTSUP || errno == ENOSYS)
784 archive_set_error(&a->archive, errno,
785 "Couldn't list extended attributes");
786 return (ARCHIVE_WARN);
792 if ((list = malloc(list_size)) == NULL) {
793 archive_set_error(&a->archive, errno, "Out of memory");
794 return (ARCHIVE_FATAL);
799 list_size = flistxattr(*fd, list, list_size);
800 else if (!a->follow_symlinks)
801 list_size = llistxattr(path, list, list_size);
803 list_size = listxattr(path, list, list_size);
806 list_size = flistea(*fd, list, list_size);
807 else if (!a->follow_symlinks)
808 list_size = llistea(path, list, list_size);
810 list_size = listea(path, list, list_size);
813 if (list_size == -1) {
814 archive_set_error(&a->archive, errno,
815 "Couldn't retrieve extended attributes");
817 return (ARCHIVE_WARN);
820 for (p = list; (p - list) < list_size; p += strlen(p) + 1) {
821 if (strncmp(p, "system.", 7) == 0 ||
822 strncmp(p, "xfsroot.", 8) == 0)
824 setup_xattr(a, entry, p, *fd);
831 #elif HAVE_EXTATTR_GET_FILE && HAVE_EXTATTR_LIST_FILE && \
832 HAVE_DECL_EXTATTR_NAMESPACE_USER
835 * FreeBSD extattr interface.
838 /* TODO: Implement this. Follow the Linux model above, but
839 * with FreeBSD-specific system calls, of course. Be careful
840 * to not include the system extattrs that hold ACLs; we handle
844 setup_xattr(struct archive_read_disk *a, struct archive_entry *entry,
845 int namespace, const char *name, const char *fullname, int fd);
848 setup_xattr(struct archive_read_disk *a, struct archive_entry *entry,
849 int namespace, const char *name, const char *fullname, int fd)
855 accpath = archive_entry_sourcepath(entry);
857 accpath = archive_entry_pathname(entry);
860 size = extattr_get_fd(fd, namespace, name, NULL, 0);
861 else if (!a->follow_symlinks)
862 size = extattr_get_link(accpath, namespace, name, NULL, 0);
864 size = extattr_get_file(accpath, namespace, name, NULL, 0);
867 archive_set_error(&a->archive, errno,
868 "Couldn't query extended attribute");
869 return (ARCHIVE_WARN);
872 if (size > 0 && (value = malloc(size)) == NULL) {
873 archive_set_error(&a->archive, errno, "Out of memory");
874 return (ARCHIVE_FATAL);
878 size = extattr_get_fd(fd, namespace, name, value, size);
879 else if (!a->follow_symlinks)
880 size = extattr_get_link(accpath, namespace, name, value, size);
882 size = extattr_get_file(accpath, namespace, name, value, size);
886 archive_set_error(&a->archive, errno,
887 "Couldn't read extended attribute");
888 return (ARCHIVE_WARN);
891 archive_entry_xattr_add_entry(entry, fullname, value, size);
898 setup_xattrs(struct archive_read_disk *a,
899 struct archive_entry *entry, int *fd)
905 int namespace = EXTATTR_NAMESPACE_USER;
907 path = archive_entry_sourcepath(entry);
909 path = archive_entry_pathname(entry);
911 if (*fd < 0 && a->tree != NULL) {
912 if (a->follow_symlinks ||
913 archive_entry_filetype(entry) != AE_IFLNK)
914 *fd = a->open_on_current_dir(a->tree, path,
915 O_RDONLY | O_NONBLOCK);
917 if (a->tree_enter_working_dir(a->tree) != 0) {
918 archive_set_error(&a->archive, errno,
919 "Couldn't access %s", path);
920 return (ARCHIVE_FAILED);
926 list_size = extattr_list_fd(*fd, namespace, NULL, 0);
927 else if (!a->follow_symlinks)
928 list_size = extattr_list_link(path, namespace, NULL, 0);
930 list_size = extattr_list_file(path, namespace, NULL, 0);
932 if (list_size == -1 && errno == EOPNOTSUPP)
934 if (list_size == -1) {
935 archive_set_error(&a->archive, errno,
936 "Couldn't list extended attributes");
937 return (ARCHIVE_WARN);
943 if ((list = malloc(list_size)) == NULL) {
944 archive_set_error(&a->archive, errno, "Out of memory");
945 return (ARCHIVE_FATAL);
949 list_size = extattr_list_fd(*fd, namespace, list, list_size);
950 else if (!a->follow_symlinks)
951 list_size = extattr_list_link(path, namespace, list, list_size);
953 list_size = extattr_list_file(path, namespace, list, list_size);
955 if (list_size == -1) {
956 archive_set_error(&a->archive, errno,
957 "Couldn't retrieve extended attributes");
959 return (ARCHIVE_WARN);
963 while ((p - list) < list_size) {
964 size_t len = 255 & (int)*p;
967 strcpy(buff, "user.");
968 name = buff + strlen(buff);
969 memcpy(name, p + 1, len);
971 setup_xattr(a, entry, namespace, name, buff, *fd);
982 * Generic (stub) extended attribute support.
985 setup_xattrs(struct archive_read_disk *a,
986 struct archive_entry *entry, int *fd)
988 (void)a; /* UNUSED */
989 (void)entry; /* UNUSED */
990 (void)fd; /* UNUSED */
996 #if defined(HAVE_LINUX_FIEMAP_H)
999 * Linux sparse interface.
1001 * The FIEMAP ioctl returns an "extent" for each physical allocation
1002 * on disk. We need to process those to generate a more compact list
1003 * of logical file blocks. We also need to be very careful to use
1004 * FIEMAP_FLAG_SYNC here, since there are reports that Linux sometimes
1005 * does not report allocations for newly-written data that hasn't
1006 * been synced to disk.
1008 * It's important to return a minimal sparse file list because we want
1009 * to not trigger sparse file extensions if we don't have to, since
1010 * not all readers support them.
1014 setup_sparse(struct archive_read_disk *a,
1015 struct archive_entry *entry, int *fd)
1019 struct fiemap_extent *fe;
1021 int count, do_fiemap;
1022 int exit_sts = ARCHIVE_OK;
1024 if (archive_entry_filetype(entry) != AE_IFREG
1025 || archive_entry_size(entry) <= 0
1026 || archive_entry_hardlink(entry) != NULL)
1027 return (ARCHIVE_OK);
1032 path = archive_entry_sourcepath(entry);
1034 path = archive_entry_pathname(entry);
1035 if (a->tree != NULL)
1036 *fd = a->open_on_current_dir(a->tree, path,
1037 O_RDONLY | O_NONBLOCK);
1039 *fd = open(path, O_RDONLY | O_NONBLOCK);
1041 archive_set_error(&a->archive, errno,
1042 "Can't open `%s'", path);
1043 return (ARCHIVE_FAILED);
1047 count = (sizeof(buff) - sizeof(*fm))/sizeof(*fe);
1048 fm = (struct fiemap *)buff;
1050 fm->fm_length = ~0ULL;;
1051 fm->fm_flags = FIEMAP_FLAG_SYNC;
1052 fm->fm_extent_count = count;
1054 size = archive_entry_size(entry);
1058 r = ioctl(*fd, FS_IOC_FIEMAP, fm);
1060 /* When something error happens, it is better we
1061 * should return ARCHIVE_OK because an earlier
1062 * version(<2.6.28) cannot perfom FS_IOC_FIEMAP. */
1063 goto exit_setup_sparse;
1065 if (fm->fm_mapped_extents == 0)
1067 fe = fm->fm_extents;
1068 for (i = 0; i < (int)fm->fm_mapped_extents; i++, fe++) {
1069 if (!(fe->fe_flags & FIEMAP_EXTENT_UNWRITTEN)) {
1070 /* The fe_length of the last block does not
1071 * adjust itself to its size files. */
1072 int64_t length = fe->fe_length;
1073 if (fe->fe_logical + length > (uint64_t)size)
1074 length -= fe->fe_logical + length - size;
1075 if (fe->fe_logical == 0 && length == size) {
1076 /* This is not sparse. */
1081 archive_entry_sparse_add_entry(entry,
1082 fe->fe_logical, length);
1084 if (fe->fe_flags & FIEMAP_EXTENT_LAST)
1088 fe = fm->fm_extents + fm->fm_mapped_extents -1;
1089 fm->fm_start = fe->fe_logical + fe->fe_length;
1097 #elif defined(SEEK_HOLE) && defined(SEEK_DATA) && defined(_PC_MIN_HOLE_SIZE)
1100 * FreeBSD and Solaris sparse interface.
1104 setup_sparse(struct archive_read_disk *a,
1105 struct archive_entry *entry, int *fd)
1108 off_t initial_off; /* FreeBSD/Solaris only, so off_t okay here */
1109 off_t off_s, off_e; /* FreeBSD/Solaris only, so off_t okay here */
1110 int exit_sts = ARCHIVE_OK;
1112 if (archive_entry_filetype(entry) != AE_IFREG
1113 || archive_entry_size(entry) <= 0
1114 || archive_entry_hardlink(entry) != NULL)
1115 return (ARCHIVE_OK);
1117 /* Does filesystem support the reporting of hole ? */
1118 if (*fd < 0 && a->tree != NULL) {
1121 path = archive_entry_sourcepath(entry);
1123 path = archive_entry_pathname(entry);
1124 *fd = a->open_on_current_dir(a->tree, path,
1125 O_RDONLY | O_NONBLOCK);
1127 archive_set_error(&a->archive, errno,
1128 "Can't open `%s'", path);
1129 return (ARCHIVE_FAILED);
1134 if (fpathconf(*fd, _PC_MIN_HOLE_SIZE) <= 0)
1135 return (ARCHIVE_OK);
1136 initial_off = lseek(*fd, 0, SEEK_CUR);
1137 if (initial_off != 0)
1138 lseek(*fd, 0, SEEK_SET);
1142 path = archive_entry_sourcepath(entry);
1144 path = archive_entry_pathname(entry);
1146 if (pathconf(path, _PC_MIN_HOLE_SIZE) <= 0)
1147 return (ARCHIVE_OK);
1148 *fd = open(path, O_RDONLY | O_NONBLOCK);
1150 archive_set_error(&a->archive, errno,
1151 "Can't open `%s'", path);
1152 return (ARCHIVE_FAILED);
1158 size = archive_entry_size(entry);
1159 while (off_s < size) {
1160 off_s = lseek(*fd, off_s, SEEK_DATA);
1161 if (off_s == (off_t)-1) {
1163 break;/* no more hole */
1164 archive_set_error(&a->archive, errno,
1165 "lseek(SEEK_HOLE) failed");
1166 exit_sts = ARCHIVE_FAILED;
1167 goto exit_setup_sparse;
1169 off_e = lseek(*fd, off_s, SEEK_HOLE);
1170 if (off_e == (off_t)-1) {
1171 if (errno == ENXIO) {
1172 off_e = lseek(*fd, 0, SEEK_END);
1173 if (off_e != (off_t)-1)
1174 break;/* no more data */
1176 archive_set_error(&a->archive, errno,
1177 "lseek(SEEK_DATA) failed");
1178 exit_sts = ARCHIVE_FAILED;
1179 goto exit_setup_sparse;
1181 if (off_s == 0 && off_e == size)
1182 break;/* This is not spase. */
1183 archive_entry_sparse_add_entry(entry, off_s,
1188 lseek(*fd, initial_off, SEEK_SET);
1195 * Generic (stub) sparse support.
1198 setup_sparse(struct archive_read_disk *a,
1199 struct archive_entry *entry, int *fd)
1201 (void)a; /* UNUSED */
1202 (void)entry; /* UNUSED */
1203 (void)fd; /* UNUSED */
1204 return (ARCHIVE_OK);
1209 #endif /* !defined(_WIN32) || defined(__CYGWIN__) */