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"
31 /* This is the tree-walking code for POSIX systems. */
32 #if !defined(_WIN32) || defined(__CYGWIN__)
34 #ifdef HAVE_SYS_TYPES_H
35 #include <sys/types.h>
37 #ifdef HAVE_SYS_EXTATTR_H
38 #include <sys/extattr.h>
40 #ifdef HAVE_SYS_IOCTL_H
41 #include <sys/ioctl.h>
43 #ifdef HAVE_SYS_PARAM_H
44 #include <sys/param.h>
46 #ifdef HAVE_SYS_STAT_H
49 #if defined(HAVE_SYS_XATTR_H)
50 #include <sys/xattr.h>
51 #elif defined(HAVE_ATTR_XATTR_H)
52 #include <attr/xattr.h>
57 #ifdef HAVE_COPYFILE_H
69 #ifdef HAVE_LINUX_TYPES_H
70 #include <linux/types.h>
72 #ifdef HAVE_LINUX_FIEMAP_H
73 #include <linux/fiemap.h>
75 #ifdef HAVE_LINUX_FS_H
79 * Some Linux distributions have both linux/ext2_fs.h and ext2fs/ext2_fs.h.
80 * As the include guards don't agree, the order of include is important.
82 #ifdef HAVE_LINUX_EXT2_FS_H
83 #include <linux/ext2_fs.h> /* for Linux file flags */
85 #if defined(HAVE_EXT2FS_EXT2_FS_H) && !defined(__CYGWIN__)
86 #include <ext2fs/ext2_fs.h> /* Linux file flags, broken on Cygwin */
96 #include "archive_entry.h"
97 #include "archive_private.h"
98 #include "archive_read_disk_private.h"
104 static int setup_mac_metadata(struct archive_read_disk *,
105 struct archive_entry *, int *fd);
106 static int setup_xattrs(struct archive_read_disk *,
107 struct archive_entry *, int *fd);
108 static int setup_sparse(struct archive_read_disk *,
109 struct archive_entry *, int *fd);
110 #if defined(HAVE_LINUX_FIEMAP_H)
111 static int setup_sparse_fiemap(struct archive_read_disk *,
112 struct archive_entry *, int *fd);
115 #if !ARCHIVE_ACL_SUPPORT
117 archive_read_disk_entry_setup_acls(struct archive_read_disk *a,
118 struct archive_entry *entry, int *fd)
120 (void)a; /* UNUSED */
121 (void)entry; /* UNUSED */
122 (void)fd; /* UNUSED */
128 * Enter working directory and return working pathname of archive_entry.
129 * If a pointer to an integer is provided and its value is below zero
130 * open a file descriptor on this pathname.
133 archive_read_disk_entry_setup_path(struct archive_read_disk *a,
134 struct archive_entry *entry, int *fd)
138 path = archive_entry_sourcepath(entry);
140 if (path == NULL || (a->tree != NULL &&
141 a->tree_enter_working_dir(a->tree) != 0))
142 path = archive_entry_pathname(entry);
144 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
145 "Couldn't determine path");
146 } else if (fd != NULL && *fd < 0 && a->tree != NULL &&
147 (a->follow_symlinks || archive_entry_filetype(entry) != AE_IFLNK)) {
148 *fd = a->open_on_current_dir(a->tree, path,
149 O_RDONLY | O_NONBLOCK);
155 archive_read_disk_entry_from_file(struct archive *_a,
156 struct archive_entry *entry,
158 const struct stat *st)
160 struct archive_read_disk *a = (struct archive_read_disk *)_a;
161 const char *path, *name;
166 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_ANY,
167 "archive_read_disk_entry_from_file");
169 archive_clear_error(_a);
170 path = archive_entry_sourcepath(entry);
172 path = archive_entry_pathname(entry);
174 if (a->tree == NULL) {
178 if (fstat(fd, &s) != 0) {
179 archive_set_error(&a->archive, errno,
181 return (ARCHIVE_FAILED);
186 if (!a->follow_symlinks) {
187 if (lstat(path, &s) != 0) {
188 archive_set_error(&a->archive, errno,
189 "Can't lstat %s", path);
190 return (ARCHIVE_FAILED);
194 if (la_stat(path, &s) != 0) {
195 archive_set_error(&a->archive, errno,
196 "Can't stat %s", path);
197 return (ARCHIVE_FAILED);
201 archive_entry_copy_stat(entry, st);
204 /* Lookup uname/gname */
205 name = archive_read_disk_uname(_a, archive_entry_uid(entry));
207 archive_entry_copy_uname(entry, name);
208 name = archive_read_disk_gname(_a, archive_entry_gid(entry));
210 archive_entry_copy_gname(entry, name);
212 #ifdef HAVE_STRUCT_STAT_ST_FLAGS
213 /* On FreeBSD, we get flags for free with the stat. */
214 /* TODO: Does this belong in copy_stat()? */
215 if ((a->flags & ARCHIVE_READDISK_NO_FFLAGS) == 0 && st->st_flags != 0)
216 archive_entry_set_fflags(entry, st->st_flags, 0);
219 #if (defined(FS_IOC_GETFLAGS) && defined(HAVE_WORKING_FS_IOC_GETFLAGS)) || \
220 (defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS))
221 /* Linux requires an extra ioctl to pull the flags. Although
222 * this is an extra step, it has a nice side-effect: We get an
223 * open file descriptor which we can use in the subsequent lookups. */
224 if ((a->flags & ARCHIVE_READDISK_NO_FFLAGS) == 0 &&
225 (S_ISREG(st->st_mode) || S_ISDIR(st->st_mode))) {
228 fd = a->open_on_current_dir(a->tree, path,
229 O_RDONLY | O_NONBLOCK | O_CLOEXEC);
231 fd = open(path, O_RDONLY | O_NONBLOCK |
233 __archive_ensure_cloexec_flag(fd);
238 #if defined(FS_IOC_GETFLAGS)
244 if (r == 0 && stflags != 0)
245 archive_entry_set_fflags(entry, stflags, 0);
250 #if defined(HAVE_READLINK) || defined(HAVE_READLINKAT)
251 if (S_ISLNK(st->st_mode)) {
252 size_t linkbuffer_len = st->st_size;
256 linkbuffer = malloc(linkbuffer_len + 1);
257 if (linkbuffer == NULL) {
258 archive_set_error(&a->archive, ENOMEM,
259 "Couldn't read link data");
260 return (ARCHIVE_FAILED);
262 if (a->tree != NULL) {
263 #ifdef HAVE_READLINKAT
264 lnklen = readlinkat(a->tree_current_dir_fd(a->tree),
265 path, linkbuffer, linkbuffer_len);
267 if (a->tree_enter_working_dir(a->tree) != 0) {
268 archive_set_error(&a->archive, errno,
269 "Couldn't read link data");
271 return (ARCHIVE_FAILED);
273 lnklen = readlink(path, linkbuffer, linkbuffer_len);
274 #endif /* HAVE_READLINKAT */
276 lnklen = readlink(path, linkbuffer, linkbuffer_len);
278 archive_set_error(&a->archive, errno,
279 "Couldn't read link data");
281 return (ARCHIVE_FAILED);
283 linkbuffer[lnklen] = '\0';
284 archive_entry_set_symlink(entry, linkbuffer);
287 #endif /* HAVE_READLINK || HAVE_READLINKAT */
290 if ((a->flags & ARCHIVE_READDISK_NO_ACL) == 0)
291 r = archive_read_disk_entry_setup_acls(a, entry, &fd);
292 if ((a->flags & ARCHIVE_READDISK_NO_XATTR) == 0) {
293 r1 = setup_xattrs(a, entry, &fd);
297 if (a->flags & ARCHIVE_READDISK_MAC_COPYFILE) {
298 r1 = setup_mac_metadata(a, entry, &fd);
302 r1 = setup_sparse(a, entry, &fd);
306 /* If we opened the file earlier in this function, close it. */
307 if (initial_fd != fd)
312 #if defined(__APPLE__) && defined(HAVE_COPYFILE_H)
314 * The Mac OS "copyfile()" API copies the extended metadata for a
315 * file into a separate file in AppleDouble format (see RFC 1740).
317 * Mac OS tar and cpio implementations store this extended
318 * metadata as a separate entry just before the regular entry
319 * with a "._" prefix added to the filename.
321 * Note that this is currently done unconditionally; the tar program has
322 * an option to discard this information before the archive is written.
324 * TODO: If there's a failure, report it and return ARCHIVE_WARN.
327 setup_mac_metadata(struct archive_read_disk *a,
328 struct archive_entry *entry, int *fd)
331 int copyfile_flags = COPYFILE_NOFOLLOW | COPYFILE_ACL | COPYFILE_XATTR;
332 struct stat copyfile_stat;
333 int ret = ARCHIVE_OK;
336 const char *name, *tempdir;
337 struct archive_string tempfile;
339 (void)fd; /* UNUSED */
341 name = archive_read_disk_entry_setup_path(a, entry, NULL);
343 return (ARCHIVE_WARN);
345 /* Short-circuit if there's nothing to do. */
346 have_attrs = copyfile(name, NULL, 0, copyfile_flags | COPYFILE_CHECK);
347 if (have_attrs == -1) {
348 archive_set_error(&a->archive, errno,
349 "Could not check extended attributes");
350 return (ARCHIVE_WARN);
356 if (issetugid() == 0)
357 tempdir = getenv("TMPDIR");
360 archive_string_init(&tempfile);
361 archive_strcpy(&tempfile, tempdir);
362 archive_strcat(&tempfile, "tar.md.XXXXXX");
363 tempfd = mkstemp(tempfile.s);
365 archive_set_error(&a->archive, errno,
366 "Could not open extended attribute file");
370 __archive_ensure_cloexec_flag(tempfd);
372 /* XXX I wish copyfile() could pack directly to a memory
373 * buffer; that would avoid the temp file here. For that
374 * matter, it would be nice if fcopyfile() actually worked,
375 * that would reduce the many open/close races here. */
376 if (copyfile(name, tempfile.s, 0, copyfile_flags | COPYFILE_PACK)) {
377 archive_set_error(&a->archive, errno,
378 "Could not pack extended attributes");
382 if (fstat(tempfd, ©file_stat)) {
383 archive_set_error(&a->archive, errno,
384 "Could not check size of extended attributes");
388 buff = malloc(copyfile_stat.st_size);
390 archive_set_error(&a->archive, errno,
391 "Could not allocate memory for extended attributes");
395 if (copyfile_stat.st_size != read(tempfd, buff, copyfile_stat.st_size)) {
396 archive_set_error(&a->archive, errno,
397 "Could not read extended attributes into memory");
401 archive_entry_copy_mac_metadata(entry, buff, copyfile_stat.st_size);
408 archive_string_free(&tempfile);
416 * Stub implementation for non-Mac systems.
419 setup_mac_metadata(struct archive_read_disk *a,
420 struct archive_entry *entry, int *fd)
422 (void)a; /* UNUSED */
423 (void)entry; /* UNUSED */
424 (void)fd; /* UNUSED */
429 #if ARCHIVE_XATTR_LINUX || ARCHIVE_XATTR_DARWIN || ARCHIVE_XATTR_AIX
432 * Linux, Darwin and AIX extended attribute support.
434 * TODO: By using a stack-allocated buffer for the first
435 * call to getxattr(), we might be able to avoid the second
436 * call entirely. We only need the second call if the
437 * stack-allocated buffer is too small. But a modest buffer
438 * of 1024 bytes or so will often be big enough. Same applies
444 setup_xattr(struct archive_read_disk *a,
445 struct archive_entry *entry, const char *name, int fd, const char *accpath)
452 #if ARCHIVE_XATTR_LINUX
453 size = fgetxattr(fd, name, NULL, 0);
454 #elif ARCHIVE_XATTR_DARWIN
455 size = fgetxattr(fd, name, NULL, 0, 0, 0);
456 #elif ARCHIVE_XATTR_AIX
457 size = fgetea(fd, name, NULL, 0);
459 } else if (!a->follow_symlinks) {
460 #if ARCHIVE_XATTR_LINUX
461 size = lgetxattr(accpath, name, NULL, 0);
462 #elif ARCHIVE_XATTR_DARWIN
463 size = getxattr(accpath, name, NULL, 0, 0, XATTR_NOFOLLOW);
464 #elif ARCHIVE_XATTR_AIX
465 size = lgetea(accpath, name, NULL, 0);
468 #if ARCHIVE_XATTR_LINUX
469 size = getxattr(accpath, name, NULL, 0);
470 #elif ARCHIVE_XATTR_DARWIN
471 size = getxattr(accpath, name, NULL, 0, 0, 0);
472 #elif ARCHIVE_XATTR_AIX
473 size = getea(accpath, name, NULL, 0);
478 archive_set_error(&a->archive, errno,
479 "Couldn't query extended attribute");
480 return (ARCHIVE_WARN);
483 if (size > 0 && (value = malloc(size)) == NULL) {
484 archive_set_error(&a->archive, errno, "Out of memory");
485 return (ARCHIVE_FATAL);
490 #if ARCHIVE_XATTR_LINUX
491 size = fgetxattr(fd, name, value, size);
492 #elif ARCHIVE_XATTR_DARWIN
493 size = fgetxattr(fd, name, value, size, 0, 0);
494 #elif ARCHIVE_XATTR_AIX
495 size = fgetea(fd, name, value, size);
497 } else if (!a->follow_symlinks) {
498 #if ARCHIVE_XATTR_LINUX
499 size = lgetxattr(accpath, name, value, size);
500 #elif ARCHIVE_XATTR_DARWIN
501 size = getxattr(accpath, name, value, size, 0, XATTR_NOFOLLOW);
502 #elif ARCHIVE_XATTR_AIX
503 size = lgetea(accpath, name, value, size);
506 #if ARCHIVE_XATTR_LINUX
507 size = getxattr(accpath, name, value, size);
508 #elif ARCHIVE_XATTR_DARWIN
509 size = getxattr(accpath, name, value, size, 0, 0);
510 #elif ARCHIVE_XATTR_AIX
511 size = getea(accpath, name, value, size);
516 archive_set_error(&a->archive, errno,
517 "Couldn't read extended attribute");
518 return (ARCHIVE_WARN);
521 archive_entry_xattr_add_entry(entry, name, value, size);
528 setup_xattrs(struct archive_read_disk *a,
529 struct archive_entry *entry, int *fd)
538 path = archive_read_disk_entry_setup_path(a, entry, fd);
540 return (ARCHIVE_WARN);
544 #if ARCHIVE_XATTR_LINUX
545 list_size = flistxattr(*fd, NULL, 0);
546 #elif ARCHIVE_XATTR_DARWIN
547 list_size = flistxattr(*fd, NULL, 0, 0);
548 #elif ARCHIVE_XATTR_AIX
549 list_size = flistea(*fd, NULL, 0);
551 } else if (!a->follow_symlinks) {
552 #if ARCHIVE_XATTR_LINUX
553 list_size = llistxattr(path, NULL, 0);
554 #elif ARCHIVE_XATTR_DARWIN
555 list_size = listxattr(path, NULL, 0, XATTR_NOFOLLOW);
556 #elif ARCHIVE_XATTR_AIX
557 list_size = llistea(path, NULL, 0);
560 #if ARCHIVE_XATTR_LINUX
561 list_size = listxattr(path, NULL, 0);
562 #elif ARCHIVE_XATTR_DARWIN
563 list_size = listxattr(path, NULL, 0, 0);
564 #elif ARCHIVE_XATTR_AIX
565 list_size = listea(path, NULL, 0);
569 if (list_size == -1) {
570 if (errno == ENOTSUP || errno == ENOSYS)
572 archive_set_error(&a->archive, errno,
573 "Couldn't list extended attributes");
574 return (ARCHIVE_WARN);
580 if ((list = malloc(list_size)) == NULL) {
581 archive_set_error(&a->archive, errno, "Out of memory");
582 return (ARCHIVE_FATAL);
586 #if ARCHIVE_XATTR_LINUX
587 list_size = flistxattr(*fd, list, list_size);
588 #elif ARCHIVE_XATTR_DARWIN
589 list_size = flistxattr(*fd, list, list_size, 0);
590 #elif ARCHIVE_XATTR_AIX
591 list_size = flistea(*fd, list, list_size);
593 } else if (!a->follow_symlinks) {
594 #if ARCHIVE_XATTR_LINUX
595 list_size = llistxattr(path, list, list_size);
596 #elif ARCHIVE_XATTR_DARWIN
597 list_size = listxattr(path, list, list_size, XATTR_NOFOLLOW);
598 #elif ARCHIVE_XATTR_AIX
599 list_size = llistea(path, list, list_size);
602 #if ARCHIVE_XATTR_LINUX
603 list_size = listxattr(path, list, list_size);
604 #elif ARCHIVE_XATTR_DARWIN
605 list_size = listxattr(path, list, list_size, 0);
606 #elif ARCHIVE_XATTR_AIX
607 list_size = listea(path, list, list_size);
611 if (list_size == -1) {
612 archive_set_error(&a->archive, errno,
613 "Couldn't retrieve extended attributes");
615 return (ARCHIVE_WARN);
618 for (p = list; (p - list) < list_size; p += strlen(p) + 1) {
619 #if ARCHIVE_XATTR_LINUX
620 /* Linux: skip POSIX.1e ACL extended attributes */
621 if (strncmp(p, "system.", 7) == 0 &&
622 (strcmp(p + 7, "posix_acl_access") == 0 ||
623 strcmp(p + 7, "posix_acl_default") == 0))
625 if (strncmp(p, "trusted.SGI_", 12) == 0 &&
626 (strcmp(p + 12, "ACL_DEFAULT") == 0 ||
627 strcmp(p + 12, "ACL_FILE") == 0))
630 /* Linux: xfsroot namespace is obsolete and unsupported */
631 if (strncmp(p, "xfsroot.", 8) == 0)
634 setup_xattr(a, entry, p, *fd, path);
641 #elif ARCHIVE_XATTR_FREEBSD
644 * FreeBSD extattr interface.
647 /* TODO: Implement this. Follow the Linux model above, but
648 * with FreeBSD-specific system calls, of course. Be careful
649 * to not include the system extattrs that hold ACLs; we handle
653 setup_xattr(struct archive_read_disk *a, struct archive_entry *entry,
654 int namespace, const char *name, const char *fullname, int fd,
658 setup_xattr(struct archive_read_disk *a, struct archive_entry *entry,
659 int namespace, const char *name, const char *fullname, int fd,
666 size = extattr_get_fd(fd, namespace, name, NULL, 0);
667 else if (!a->follow_symlinks)
668 size = extattr_get_link(accpath, namespace, name, NULL, 0);
670 size = extattr_get_file(accpath, namespace, name, NULL, 0);
673 archive_set_error(&a->archive, errno,
674 "Couldn't query extended attribute");
675 return (ARCHIVE_WARN);
678 if (size > 0 && (value = malloc(size)) == NULL) {
679 archive_set_error(&a->archive, errno, "Out of memory");
680 return (ARCHIVE_FATAL);
684 size = extattr_get_fd(fd, namespace, name, value, size);
685 else if (!a->follow_symlinks)
686 size = extattr_get_link(accpath, namespace, name, value, size);
688 size = extattr_get_file(accpath, namespace, name, value, size);
692 archive_set_error(&a->archive, errno,
693 "Couldn't read extended attribute");
694 return (ARCHIVE_WARN);
697 archive_entry_xattr_add_entry(entry, fullname, value, size);
704 setup_xattrs(struct archive_read_disk *a,
705 struct archive_entry *entry, int *fd)
711 int namespace = EXTATTR_NAMESPACE_USER;
716 path = archive_read_disk_entry_setup_path(a, entry, fd);
718 return (ARCHIVE_WARN);
722 list_size = extattr_list_fd(*fd, namespace, NULL, 0);
723 else if (!a->follow_symlinks)
724 list_size = extattr_list_link(path, namespace, NULL, 0);
726 list_size = extattr_list_file(path, namespace, NULL, 0);
728 if (list_size == -1 && errno == EOPNOTSUPP)
730 if (list_size == -1) {
731 archive_set_error(&a->archive, errno,
732 "Couldn't list extended attributes");
733 return (ARCHIVE_WARN);
739 if ((list = malloc(list_size)) == NULL) {
740 archive_set_error(&a->archive, errno, "Out of memory");
741 return (ARCHIVE_FATAL);
745 list_size = extattr_list_fd(*fd, namespace, list, list_size);
746 else if (!a->follow_symlinks)
747 list_size = extattr_list_link(path, namespace, list, list_size);
749 list_size = extattr_list_file(path, namespace, list, list_size);
751 if (list_size == -1) {
752 archive_set_error(&a->archive, errno,
753 "Couldn't retrieve extended attributes");
755 return (ARCHIVE_WARN);
759 while ((p - list) < list_size) {
760 size_t len = 255 & (int)*p;
763 strcpy(buff, "user.");
764 name = buff + strlen(buff);
765 memcpy(name, p + 1, len);
767 setup_xattr(a, entry, namespace, name, buff, *fd, path);
778 * Generic (stub) extended attribute support.
781 setup_xattrs(struct archive_read_disk *a,
782 struct archive_entry *entry, int *fd)
784 (void)a; /* UNUSED */
785 (void)entry; /* UNUSED */
786 (void)fd; /* UNUSED */
792 #if defined(HAVE_LINUX_FIEMAP_H)
795 * Linux FIEMAP sparse interface.
797 * The FIEMAP ioctl returns an "extent" for each physical allocation
798 * on disk. We need to process those to generate a more compact list
799 * of logical file blocks. We also need to be very careful to use
800 * FIEMAP_FLAG_SYNC here, since there are reports that Linux sometimes
801 * does not report allocations for newly-written data that hasn't
802 * been synced to disk.
804 * It's important to return a minimal sparse file list because we want
805 * to not trigger sparse file extensions if we don't have to, since
806 * not all readers support them.
810 setup_sparse_fiemap(struct archive_read_disk *a,
811 struct archive_entry *entry, int *fd)
815 struct fiemap_extent *fe;
817 int count, do_fiemap, iters;
818 int exit_sts = ARCHIVE_OK;
821 if (archive_entry_filetype(entry) != AE_IFREG
822 || archive_entry_size(entry) <= 0
823 || archive_entry_hardlink(entry) != NULL)
827 path = archive_read_disk_entry_setup_path(a, entry, NULL);
829 return (ARCHIVE_FAILED);
832 *fd = a->open_on_current_dir(a->tree, path,
833 O_RDONLY | O_NONBLOCK | O_CLOEXEC);
835 *fd = open(path, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
837 archive_set_error(&a->archive, errno,
838 "Can't open `%s'", path);
839 return (ARCHIVE_FAILED);
841 __archive_ensure_cloexec_flag(*fd);
844 /* Initialize buffer to avoid the error valgrind complains about. */
845 memset(buff, 0, sizeof(buff));
846 count = (sizeof(buff) - sizeof(*fm))/sizeof(*fe);
847 fm = (struct fiemap *)buff;
849 fm->fm_length = ~0ULL;;
850 fm->fm_flags = FIEMAP_FLAG_SYNC;
851 fm->fm_extent_count = count;
853 size = archive_entry_size(entry);
854 for (iters = 0; ; ++iters) {
857 r = ioctl(*fd, FS_IOC_FIEMAP, fm);
859 /* When something error happens, it is better we
860 * should return ARCHIVE_OK because an earlier
861 * version(<2.6.28) cannot perform FS_IOC_FIEMAP. */
862 goto exit_setup_sparse_fiemap;
864 if (fm->fm_mapped_extents == 0) {
866 /* Fully sparse file; insert a zero-length "data" entry */
867 archive_entry_sparse_add_entry(entry, 0, 0);
872 for (i = 0; i < (int)fm->fm_mapped_extents; i++, fe++) {
873 if (!(fe->fe_flags & FIEMAP_EXTENT_UNWRITTEN)) {
874 /* The fe_length of the last block does not
875 * adjust itself to its size files. */
876 int64_t length = fe->fe_length;
877 if (fe->fe_logical + length > (uint64_t)size)
878 length -= fe->fe_logical + length - size;
879 if (fe->fe_logical == 0 && length == size) {
880 /* This is not sparse. */
885 archive_entry_sparse_add_entry(entry,
886 fe->fe_logical, length);
888 if (fe->fe_flags & FIEMAP_EXTENT_LAST)
892 fe = fm->fm_extents + fm->fm_mapped_extents -1;
893 fm->fm_start = fe->fe_logical + fe->fe_length;
897 exit_setup_sparse_fiemap:
901 #if !defined(SEEK_HOLE) || !defined(SEEK_DATA)
903 setup_sparse(struct archive_read_disk *a,
904 struct archive_entry *entry, int *fd)
906 return setup_sparse_fiemap(a, entry, fd);
909 #endif /* defined(HAVE_LINUX_FIEMAP_H) */
911 #if defined(SEEK_HOLE) && defined(SEEK_DATA)
914 * SEEK_HOLE sparse interface (FreeBSD, Linux, Solaris)
918 setup_sparse(struct archive_read_disk *a,
919 struct archive_entry *entry, int *fd)
924 int exit_sts = ARCHIVE_OK;
925 int check_fully_sparse = 0;
928 if (archive_entry_filetype(entry) != AE_IFREG
929 || archive_entry_size(entry) <= 0
930 || archive_entry_hardlink(entry) != NULL)
933 /* Does filesystem support the reporting of hole ? */
935 path = archive_read_disk_entry_setup_path(a, entry, fd);
940 #ifdef _PC_MIN_HOLE_SIZE
941 if (fpathconf(*fd, _PC_MIN_HOLE_SIZE) <= 0)
944 initial_off = lseek(*fd, 0, SEEK_CUR);
945 if (initial_off != 0)
946 lseek(*fd, 0, SEEK_SET);
949 return (ARCHIVE_FAILED);
950 #ifdef _PC_MIN_HOLE_SIZE
951 if (pathconf(path, _PC_MIN_HOLE_SIZE) <= 0)
954 *fd = open(path, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
956 archive_set_error(&a->archive, errno,
957 "Can't open `%s'", path);
958 return (ARCHIVE_FAILED);
960 __archive_ensure_cloexec_flag(*fd);
964 #ifndef _PC_MIN_HOLE_SIZE
965 /* Check if the underlying filesystem supports seek hole */
966 off_s = lseek(*fd, 0, SEEK_HOLE);
968 #if defined(HAVE_LINUX_FIEMAP_H)
969 return setup_sparse_fiemap(a, entry, fd);
971 goto exit_setup_sparse;
974 lseek(*fd, 0, SEEK_SET);
978 size = archive_entry_size(entry);
979 while (off_s < size) {
980 off_s = lseek(*fd, off_s, SEEK_DATA);
981 if (off_s == (off_t)-1) {
982 if (errno == ENXIO) {
984 if (archive_entry_sparse_count(entry) == 0) {
985 /* Potentially a fully-sparse file. */
986 check_fully_sparse = 1;
990 archive_set_error(&a->archive, errno,
991 "lseek(SEEK_HOLE) failed");
992 exit_sts = ARCHIVE_FAILED;
993 goto exit_setup_sparse;
995 off_e = lseek(*fd, off_s, SEEK_HOLE);
996 if (off_e == (off_t)-1) {
997 if (errno == ENXIO) {
998 off_e = lseek(*fd, 0, SEEK_END);
999 if (off_e != (off_t)-1)
1000 break;/* no more data */
1002 archive_set_error(&a->archive, errno,
1003 "lseek(SEEK_DATA) failed");
1004 exit_sts = ARCHIVE_FAILED;
1005 goto exit_setup_sparse;
1007 if (off_s == 0 && off_e == size)
1008 break;/* This is not sparse. */
1009 archive_entry_sparse_add_entry(entry, off_s,
1014 if (check_fully_sparse) {
1015 if (lseek(*fd, 0, SEEK_HOLE) == 0 &&
1016 lseek(*fd, 0, SEEK_END) == size) {
1017 /* Fully sparse file; insert a zero-length "data" entry */
1018 archive_entry_sparse_add_entry(entry, 0, 0);
1022 lseek(*fd, initial_off, SEEK_SET);
1026 #elif !defined(HAVE_LINUX_FIEMAP_H)
1029 * Generic (stub) sparse support.
1032 setup_sparse(struct archive_read_disk *a,
1033 struct archive_entry *entry, int *fd)
1035 (void)a; /* UNUSED */
1036 (void)entry; /* UNUSED */
1037 (void)fd; /* UNUSED */
1038 return (ARCHIVE_OK);
1043 #endif /* !defined(_WIN32) || defined(__CYGWIN__) */