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 + 1;
256 linkbuffer = malloc(linkbuffer_len);
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 (strncmp(p, "system.", 7) == 0 ||
620 strncmp(p, "xfsroot.", 8) == 0)
622 setup_xattr(a, entry, p, *fd, path);
629 #elif ARCHIVE_XATTR_FREEBSD
632 * FreeBSD extattr interface.
635 /* TODO: Implement this. Follow the Linux model above, but
636 * with FreeBSD-specific system calls, of course. Be careful
637 * to not include the system extattrs that hold ACLs; we handle
641 setup_xattr(struct archive_read_disk *a, struct archive_entry *entry,
642 int namespace, const char *name, const char *fullname, int fd,
646 setup_xattr(struct archive_read_disk *a, struct archive_entry *entry,
647 int namespace, const char *name, const char *fullname, int fd,
654 size = extattr_get_fd(fd, namespace, name, NULL, 0);
655 else if (!a->follow_symlinks)
656 size = extattr_get_link(accpath, namespace, name, NULL, 0);
658 size = extattr_get_file(accpath, namespace, name, NULL, 0);
661 archive_set_error(&a->archive, errno,
662 "Couldn't query extended attribute");
663 return (ARCHIVE_WARN);
666 if (size > 0 && (value = malloc(size)) == NULL) {
667 archive_set_error(&a->archive, errno, "Out of memory");
668 return (ARCHIVE_FATAL);
672 size = extattr_get_fd(fd, namespace, name, value, size);
673 else if (!a->follow_symlinks)
674 size = extattr_get_link(accpath, namespace, name, value, size);
676 size = extattr_get_file(accpath, namespace, name, value, size);
680 archive_set_error(&a->archive, errno,
681 "Couldn't read extended attribute");
682 return (ARCHIVE_WARN);
685 archive_entry_xattr_add_entry(entry, fullname, value, size);
692 setup_xattrs(struct archive_read_disk *a,
693 struct archive_entry *entry, int *fd)
699 int namespace = EXTATTR_NAMESPACE_USER;
704 path = archive_read_disk_entry_setup_path(a, entry, fd);
706 return (ARCHIVE_WARN);
710 list_size = extattr_list_fd(*fd, namespace, NULL, 0);
711 else if (!a->follow_symlinks)
712 list_size = extattr_list_link(path, namespace, NULL, 0);
714 list_size = extattr_list_file(path, namespace, NULL, 0);
716 if (list_size == -1 && errno == EOPNOTSUPP)
718 if (list_size == -1) {
719 archive_set_error(&a->archive, errno,
720 "Couldn't list extended attributes");
721 return (ARCHIVE_WARN);
727 if ((list = malloc(list_size)) == NULL) {
728 archive_set_error(&a->archive, errno, "Out of memory");
729 return (ARCHIVE_FATAL);
733 list_size = extattr_list_fd(*fd, namespace, list, list_size);
734 else if (!a->follow_symlinks)
735 list_size = extattr_list_link(path, namespace, list, list_size);
737 list_size = extattr_list_file(path, namespace, list, list_size);
739 if (list_size == -1) {
740 archive_set_error(&a->archive, errno,
741 "Couldn't retrieve extended attributes");
743 return (ARCHIVE_WARN);
747 while ((p - list) < list_size) {
748 size_t len = 255 & (int)*p;
751 strcpy(buff, "user.");
752 name = buff + strlen(buff);
753 memcpy(name, p + 1, len);
755 setup_xattr(a, entry, namespace, name, buff, *fd, path);
766 * Generic (stub) extended attribute support.
769 setup_xattrs(struct archive_read_disk *a,
770 struct archive_entry *entry, int *fd)
772 (void)a; /* UNUSED */
773 (void)entry; /* UNUSED */
774 (void)fd; /* UNUSED */
780 #if defined(HAVE_LINUX_FIEMAP_H)
783 * Linux FIEMAP sparse interface.
785 * The FIEMAP ioctl returns an "extent" for each physical allocation
786 * on disk. We need to process those to generate a more compact list
787 * of logical file blocks. We also need to be very careful to use
788 * FIEMAP_FLAG_SYNC here, since there are reports that Linux sometimes
789 * does not report allocations for newly-written data that hasn't
790 * been synced to disk.
792 * It's important to return a minimal sparse file list because we want
793 * to not trigger sparse file extensions if we don't have to, since
794 * not all readers support them.
798 setup_sparse_fiemap(struct archive_read_disk *a,
799 struct archive_entry *entry, int *fd)
803 struct fiemap_extent *fe;
805 int count, do_fiemap, iters;
806 int exit_sts = ARCHIVE_OK;
809 if (archive_entry_filetype(entry) != AE_IFREG
810 || archive_entry_size(entry) <= 0
811 || archive_entry_hardlink(entry) != NULL)
815 path = archive_read_disk_entry_setup_path(a, entry, NULL);
817 return (ARCHIVE_FAILED);
820 *fd = a->open_on_current_dir(a->tree, path,
821 O_RDONLY | O_NONBLOCK | O_CLOEXEC);
823 *fd = open(path, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
825 archive_set_error(&a->archive, errno,
826 "Can't open `%s'", path);
827 return (ARCHIVE_FAILED);
829 __archive_ensure_cloexec_flag(*fd);
832 /* Initialize buffer to avoid the error valgrind complains about. */
833 memset(buff, 0, sizeof(buff));
834 count = (sizeof(buff) - sizeof(*fm))/sizeof(*fe);
835 fm = (struct fiemap *)buff;
837 fm->fm_length = ~0ULL;;
838 fm->fm_flags = FIEMAP_FLAG_SYNC;
839 fm->fm_extent_count = count;
841 size = archive_entry_size(entry);
842 for (iters = 0; ; ++iters) {
845 r = ioctl(*fd, FS_IOC_FIEMAP, fm);
847 /* When something error happens, it is better we
848 * should return ARCHIVE_OK because an earlier
849 * version(<2.6.28) cannot perform FS_IOC_FIEMAP. */
850 goto exit_setup_sparse_fiemap;
852 if (fm->fm_mapped_extents == 0) {
854 /* Fully sparse file; insert a zero-length "data" entry */
855 archive_entry_sparse_add_entry(entry, 0, 0);
860 for (i = 0; i < (int)fm->fm_mapped_extents; i++, fe++) {
861 if (!(fe->fe_flags & FIEMAP_EXTENT_UNWRITTEN)) {
862 /* The fe_length of the last block does not
863 * adjust itself to its size files. */
864 int64_t length = fe->fe_length;
865 if (fe->fe_logical + length > (uint64_t)size)
866 length -= fe->fe_logical + length - size;
867 if (fe->fe_logical == 0 && length == size) {
868 /* This is not sparse. */
873 archive_entry_sparse_add_entry(entry,
874 fe->fe_logical, length);
876 if (fe->fe_flags & FIEMAP_EXTENT_LAST)
880 fe = fm->fm_extents + fm->fm_mapped_extents -1;
881 fm->fm_start = fe->fe_logical + fe->fe_length;
885 exit_setup_sparse_fiemap:
889 #if !defined(SEEK_HOLE) || !defined(SEEK_DATA)
891 setup_sparse(struct archive_read_disk *a,
892 struct archive_entry *entry, int *fd)
894 return setup_sparse_fiemap(a, entry, fd);
897 #endif /* defined(HAVE_LINUX_FIEMAP_H) */
899 #if defined(SEEK_HOLE) && defined(SEEK_DATA)
902 * SEEK_HOLE sparse interface (FreeBSD, Linux, Solaris)
906 setup_sparse(struct archive_read_disk *a,
907 struct archive_entry *entry, int *fd)
912 int exit_sts = ARCHIVE_OK;
913 int check_fully_sparse = 0;
916 if (archive_entry_filetype(entry) != AE_IFREG
917 || archive_entry_size(entry) <= 0
918 || archive_entry_hardlink(entry) != NULL)
921 /* Does filesystem support the reporting of hole ? */
923 path = archive_read_disk_entry_setup_path(a, entry, fd);
928 #ifdef _PC_MIN_HOLE_SIZE
929 if (fpathconf(*fd, _PC_MIN_HOLE_SIZE) <= 0)
932 initial_off = lseek(*fd, 0, SEEK_CUR);
933 if (initial_off != 0)
934 lseek(*fd, 0, SEEK_SET);
937 return (ARCHIVE_FAILED);
938 #ifdef _PC_MIN_HOLE_SIZE
939 if (pathconf(path, _PC_MIN_HOLE_SIZE) <= 0)
942 *fd = open(path, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
944 archive_set_error(&a->archive, errno,
945 "Can't open `%s'", path);
946 return (ARCHIVE_FAILED);
948 __archive_ensure_cloexec_flag(*fd);
952 #ifndef _PC_MIN_HOLE_SIZE
953 /* Check if the underlying filesystem supports seek hole */
954 off_s = lseek(*fd, 0, SEEK_HOLE);
956 #if defined(HAVE_LINUX_FIEMAP_H)
957 return setup_sparse_fiemap(a, entry, fd);
959 goto exit_setup_sparse;
962 lseek(*fd, 0, SEEK_SET);
966 size = archive_entry_size(entry);
967 while (off_s < size) {
968 off_s = lseek(*fd, off_s, SEEK_DATA);
969 if (off_s == (off_t)-1) {
970 if (errno == ENXIO) {
972 if (archive_entry_sparse_count(entry) == 0) {
973 /* Potentially a fully-sparse file. */
974 check_fully_sparse = 1;
978 archive_set_error(&a->archive, errno,
979 "lseek(SEEK_HOLE) failed");
980 exit_sts = ARCHIVE_FAILED;
981 goto exit_setup_sparse;
983 off_e = lseek(*fd, off_s, SEEK_HOLE);
984 if (off_e == (off_t)-1) {
985 if (errno == ENXIO) {
986 off_e = lseek(*fd, 0, SEEK_END);
987 if (off_e != (off_t)-1)
988 break;/* no more data */
990 archive_set_error(&a->archive, errno,
991 "lseek(SEEK_DATA) failed");
992 exit_sts = ARCHIVE_FAILED;
993 goto exit_setup_sparse;
995 if (off_s == 0 && off_e == size)
996 break;/* This is not sparse. */
997 archive_entry_sparse_add_entry(entry, off_s,
1002 if (check_fully_sparse) {
1003 if (lseek(*fd, 0, SEEK_HOLE) == 0 &&
1004 lseek(*fd, 0, SEEK_END) == size) {
1005 /* Fully sparse file; insert a zero-length "data" entry */
1006 archive_entry_sparse_add_entry(entry, 0, 0);
1010 lseek(*fd, initial_off, SEEK_SET);
1014 #elif !defined(HAVE_LINUX_FIEMAP_H)
1017 * Generic (stub) sparse support.
1020 setup_sparse(struct archive_read_disk *a,
1021 struct archive_entry *entry, int *fd)
1023 (void)a; /* UNUSED */
1024 (void)entry; /* UNUSED */
1025 (void)fd; /* UNUSED */
1026 return (ARCHIVE_OK);
1031 #endif /* !defined(_WIN32) || defined(__CYGWIN__) */