]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/libarchive/libarchive/archive_read_disk_entry_from_file.c
MFV r311899:
[FreeBSD/FreeBSD.git] / contrib / libarchive / libarchive / archive_read_disk_entry_from_file.c
1 /*-
2  * Copyright (c) 2003-2009 Tim Kientzle
3  * Copyright (c) 2010-2012 Michihiro NAKAJIMA
4  * Copyright (c) 2016 Martin Matuska
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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.
26  */
27
28 #include "archive_platform.h"
29 __FBSDID("$FreeBSD$");
30
31 /* This is the tree-walking code for POSIX systems. */
32 #if !defined(_WIN32) || defined(__CYGWIN__)
33
34 #ifdef HAVE_SYS_TYPES_H
35 /* Mac OSX requires sys/types.h before sys/acl.h. */
36 #include <sys/types.h>
37 #endif
38 #ifdef HAVE_SYS_ACL_H
39 #include <sys/acl.h>
40 #endif
41 #ifdef HAVE_SYS_EXTATTR_H
42 #include <sys/extattr.h>
43 #endif
44 #ifdef HAVE_SYS_IOCTL_H
45 #include <sys/ioctl.h>
46 #endif
47 #ifdef HAVE_SYS_PARAM_H
48 #include <sys/param.h>
49 #endif
50 #ifdef HAVE_SYS_STAT_H
51 #include <sys/stat.h>
52 #endif
53 #if defined(HAVE_SYS_XATTR_H)
54 #include <sys/xattr.h>
55 #elif defined(HAVE_ATTR_XATTR_H)
56 #include <attr/xattr.h>
57 #endif
58 #ifdef HAVE_SYS_EA_H
59 #include <sys/ea.h>
60 #endif
61 #ifdef HAVE_ACL_LIBACL_H
62 #include <acl/libacl.h>
63 #endif
64 #ifdef HAVE_COPYFILE_H
65 #include <copyfile.h>
66 #endif
67 #ifdef HAVE_ERRNO_H
68 #include <errno.h>
69 #endif
70 #ifdef HAVE_FCNTL_H
71 #include <fcntl.h>
72 #endif
73 #ifdef HAVE_LIMITS_H
74 #include <limits.h>
75 #endif
76 #ifdef HAVE_LINUX_TYPES_H
77 #include <linux/types.h>
78 #endif
79 #ifdef HAVE_LINUX_FIEMAP_H
80 #include <linux/fiemap.h>
81 #endif
82 #ifdef HAVE_LINUX_FS_H
83 #include <linux/fs.h>
84 #endif
85 /*
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.
88  */
89 #ifdef HAVE_LINUX_EXT2_FS_H
90 #include <linux/ext2_fs.h>      /* for Linux file flags */
91 #endif
92 #if defined(HAVE_EXT2FS_EXT2_FS_H) && !defined(__CYGWIN__)
93 #include <ext2fs/ext2_fs.h>     /* Linux file flags, broken on Cygwin */
94 #endif
95 #ifdef HAVE_PATHS_H
96 #include <paths.h>
97 #endif
98 #ifdef HAVE_UNISTD_H
99 #include <unistd.h>
100 #endif
101
102 #include "archive.h"
103 #include "archive_entry.h"
104 #include "archive_private.h"
105 #include "archive_read_disk_private.h"
106
107 #ifndef O_CLOEXEC
108 #define O_CLOEXEC       0
109 #endif
110
111 /*
112  * Linux and FreeBSD plug this obvious hole in POSIX.1e in
113  * different ways.
114  */
115 #if HAVE_ACL_GET_PERM
116 #define ACL_GET_PERM acl_get_perm
117 #elif HAVE_ACL_GET_PERM_NP
118 #define ACL_GET_PERM acl_get_perm_np
119 #endif
120
121 static int setup_acls(struct archive_read_disk *,
122     struct archive_entry *, int *fd);
123 static int setup_mac_metadata(struct archive_read_disk *,
124     struct archive_entry *, int *fd);
125 static int setup_xattrs(struct archive_read_disk *,
126     struct archive_entry *, int *fd);
127 static int setup_sparse(struct archive_read_disk *,
128     struct archive_entry *, int *fd);
129 #if defined(HAVE_LINUX_FIEMAP_H)
130 static int setup_sparse_fiemap(struct archive_read_disk *,
131     struct archive_entry *, int *fd);
132 #endif
133
134 int
135 archive_read_disk_entry_from_file(struct archive *_a,
136     struct archive_entry *entry,
137     int fd,
138     const struct stat *st)
139 {
140         struct archive_read_disk *a = (struct archive_read_disk *)_a;
141         const char *path, *name;
142         struct stat s;
143         int initial_fd = fd;
144         int r, r1;
145
146         archive_clear_error(_a);
147         path = archive_entry_sourcepath(entry);
148         if (path == NULL)
149                 path = archive_entry_pathname(entry);
150
151         if (a->tree == NULL) {
152                 if (st == NULL) {
153 #if HAVE_FSTAT
154                         if (fd >= 0) {
155                                 if (fstat(fd, &s) != 0) {
156                                         archive_set_error(&a->archive, errno,
157                                             "Can't fstat");
158                                         return (ARCHIVE_FAILED);
159                                 }
160                         } else
161 #endif
162 #if HAVE_LSTAT
163                         if (!a->follow_symlinks) {
164                                 if (lstat(path, &s) != 0) {
165                                         archive_set_error(&a->archive, errno,
166                                             "Can't lstat %s", path);
167                                         return (ARCHIVE_FAILED);
168                                 }
169                         } else
170 #endif
171                         if (stat(path, &s) != 0) {
172                                 archive_set_error(&a->archive, errno,
173                                     "Can't stat %s", path);
174                                 return (ARCHIVE_FAILED);
175                         }
176                         st = &s;
177                 }
178                 archive_entry_copy_stat(entry, st);
179         }
180
181         /* Lookup uname/gname */
182         name = archive_read_disk_uname(_a, archive_entry_uid(entry));
183         if (name != NULL)
184                 archive_entry_copy_uname(entry, name);
185         name = archive_read_disk_gname(_a, archive_entry_gid(entry));
186         if (name != NULL)
187                 archive_entry_copy_gname(entry, name);
188
189 #ifdef HAVE_STRUCT_STAT_ST_FLAGS
190         /* On FreeBSD, we get flags for free with the stat. */
191         /* TODO: Does this belong in copy_stat()? */
192         if (st->st_flags != 0)
193                 archive_entry_set_fflags(entry, st->st_flags, 0);
194 #endif
195
196 #if defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS)
197         /* Linux requires an extra ioctl to pull the flags.  Although
198          * this is an extra step, it has a nice side-effect: We get an
199          * open file descriptor which we can use in the subsequent lookups. */
200         if ((S_ISREG(st->st_mode) || S_ISDIR(st->st_mode))) {
201                 if (fd < 0) {
202                         if (a->tree != NULL)
203                                 fd = a->open_on_current_dir(a->tree, path,
204                                         O_RDONLY | O_NONBLOCK | O_CLOEXEC);
205                         else
206                                 fd = open(path, O_RDONLY | O_NONBLOCK |
207                                                 O_CLOEXEC);
208                         __archive_ensure_cloexec_flag(fd);
209                 }
210                 if (fd >= 0) {
211                         int stflags;
212                         r = ioctl(fd, EXT2_IOC_GETFLAGS, &stflags);
213                         if (r == 0 && stflags != 0)
214                                 archive_entry_set_fflags(entry, stflags, 0);
215                 }
216         }
217 #endif
218
219 #if defined(HAVE_READLINK) || defined(HAVE_READLINKAT)
220         if (S_ISLNK(st->st_mode)) {
221                 size_t linkbuffer_len = st->st_size + 1;
222                 char *linkbuffer;
223                 int lnklen;
224
225                 linkbuffer = malloc(linkbuffer_len);
226                 if (linkbuffer == NULL) {
227                         archive_set_error(&a->archive, ENOMEM,
228                             "Couldn't read link data");
229                         return (ARCHIVE_FAILED);
230                 }
231                 if (a->tree != NULL) {
232 #ifdef HAVE_READLINKAT
233                         lnklen = readlinkat(a->tree_current_dir_fd(a->tree),
234                             path, linkbuffer, linkbuffer_len);
235 #else
236                         if (a->tree_enter_working_dir(a->tree) != 0) {
237                                 archive_set_error(&a->archive, errno,
238                                     "Couldn't read link data");
239                                 free(linkbuffer);
240                                 return (ARCHIVE_FAILED);
241                         }
242                         lnklen = readlink(path, linkbuffer, linkbuffer_len);
243 #endif /* HAVE_READLINKAT */
244                 } else
245                         lnklen = readlink(path, linkbuffer, linkbuffer_len);
246                 if (lnklen < 0) {
247                         archive_set_error(&a->archive, errno,
248                             "Couldn't read link data");
249                         free(linkbuffer);
250                         return (ARCHIVE_FAILED);
251                 }
252                 linkbuffer[lnklen] = 0;
253                 archive_entry_set_symlink(entry, linkbuffer);
254                 free(linkbuffer);
255         }
256 #endif /* HAVE_READLINK || HAVE_READLINKAT */
257
258         r = setup_acls(a, entry, &fd);
259         if (!a->suppress_xattr) {
260                 r1 = setup_xattrs(a, entry, &fd);
261                 if (r1 < r)
262                         r = r1;
263         }
264         if (a->enable_copyfile) {
265                 r1 = setup_mac_metadata(a, entry, &fd);
266                 if (r1 < r)
267                         r = r1;
268         }
269         r1 = setup_sparse(a, entry, &fd);
270         if (r1 < r)
271                 r = r1;
272
273         /* If we opened the file earlier in this function, close it. */
274         if (initial_fd != fd)
275                 close(fd);
276         return (r);
277 }
278
279 #if defined(__APPLE__) && defined(HAVE_COPYFILE_H)
280 /*
281  * The Mac OS "copyfile()" API copies the extended metadata for a
282  * file into a separate file in AppleDouble format (see RFC 1740).
283  *
284  * Mac OS tar and cpio implementations store this extended
285  * metadata as a separate entry just before the regular entry
286  * with a "._" prefix added to the filename.
287  *
288  * Note that this is currently done unconditionally; the tar program has
289  * an option to discard this information before the archive is written.
290  *
291  * TODO: If there's a failure, report it and return ARCHIVE_WARN.
292  */
293 static int
294 setup_mac_metadata(struct archive_read_disk *a,
295     struct archive_entry *entry, int *fd)
296 {
297         int tempfd = -1;
298         int copyfile_flags = COPYFILE_NOFOLLOW | COPYFILE_ACL | COPYFILE_XATTR;
299         struct stat copyfile_stat;
300         int ret = ARCHIVE_OK;
301         void *buff = NULL;
302         int have_attrs;
303         const char *name, *tempdir;
304         struct archive_string tempfile;
305
306         (void)fd; /* UNUSED */
307         name = archive_entry_sourcepath(entry);
308         if (name == NULL)
309                 name = archive_entry_pathname(entry);
310         if (name == NULL) {
311                 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
312                     "Can't open file to read extended attributes: No name");
313                 return (ARCHIVE_WARN);
314         }
315
316         if (a->tree != NULL) {
317                 if (a->tree_enter_working_dir(a->tree) != 0) {
318                         archive_set_error(&a->archive, errno,
319                                     "Couldn't change dir");
320                                 return (ARCHIVE_FAILED);
321                 }
322         }
323
324         /* Short-circuit if there's nothing to do. */
325         have_attrs = copyfile(name, NULL, 0, copyfile_flags | COPYFILE_CHECK);
326         if (have_attrs == -1) {
327                 archive_set_error(&a->archive, errno,
328                         "Could not check extended attributes");
329                 return (ARCHIVE_WARN);
330         }
331         if (have_attrs == 0)
332                 return (ARCHIVE_OK);
333
334         tempdir = NULL;
335         if (issetugid() == 0)
336                 tempdir = getenv("TMPDIR");
337         if (tempdir == NULL)
338                 tempdir = _PATH_TMP;
339         archive_string_init(&tempfile);
340         archive_strcpy(&tempfile, tempdir);
341         archive_strcat(&tempfile, "tar.md.XXXXXX");
342         tempfd = mkstemp(tempfile.s);
343         if (tempfd < 0) {
344                 archive_set_error(&a->archive, errno,
345                     "Could not open extended attribute file");
346                 ret = ARCHIVE_WARN;
347                 goto cleanup;
348         }
349         __archive_ensure_cloexec_flag(tempfd);
350
351         /* XXX I wish copyfile() could pack directly to a memory
352          * buffer; that would avoid the temp file here.  For that
353          * matter, it would be nice if fcopyfile() actually worked,
354          * that would reduce the many open/close races here. */
355         if (copyfile(name, tempfile.s, 0, copyfile_flags | COPYFILE_PACK)) {
356                 archive_set_error(&a->archive, errno,
357                     "Could not pack extended attributes");
358                 ret = ARCHIVE_WARN;
359                 goto cleanup;
360         }
361         if (fstat(tempfd, &copyfile_stat)) {
362                 archive_set_error(&a->archive, errno,
363                     "Could not check size of extended attributes");
364                 ret = ARCHIVE_WARN;
365                 goto cleanup;
366         }
367         buff = malloc(copyfile_stat.st_size);
368         if (buff == NULL) {
369                 archive_set_error(&a->archive, errno,
370                     "Could not allocate memory for extended attributes");
371                 ret = ARCHIVE_WARN;
372                 goto cleanup;
373         }
374         if (copyfile_stat.st_size != read(tempfd, buff, copyfile_stat.st_size)) {
375                 archive_set_error(&a->archive, errno,
376                     "Could not read extended attributes into memory");
377                 ret = ARCHIVE_WARN;
378                 goto cleanup;
379         }
380         archive_entry_copy_mac_metadata(entry, buff, copyfile_stat.st_size);
381
382 cleanup:
383         if (tempfd >= 0) {
384                 close(tempfd);
385                 unlink(tempfile.s);
386         }
387         archive_string_free(&tempfile);
388         free(buff);
389         return (ret);
390 }
391
392 #else
393
394 /*
395  * Stub implementation for non-Mac systems.
396  */
397 static int
398 setup_mac_metadata(struct archive_read_disk *a,
399     struct archive_entry *entry, int *fd)
400 {
401         (void)a; /* UNUSED */
402         (void)entry; /* UNUSED */
403         (void)fd; /* UNUSED */
404         return (ARCHIVE_OK);
405 }
406 #endif
407
408
409 #ifdef HAVE_POSIX_ACL
410 static int translate_acl(struct archive_read_disk *a,
411     struct archive_entry *entry, acl_t acl, int archive_entry_acl_type);
412
413 static int
414 setup_acls(struct archive_read_disk *a,
415     struct archive_entry *entry, int *fd)
416 {
417         const char      *accpath;
418         acl_t            acl;
419         int             r;
420
421         accpath = archive_entry_sourcepath(entry);
422         if (accpath == NULL)
423                 accpath = archive_entry_pathname(entry);
424
425         if (*fd < 0 && a->tree != NULL) {
426                 if (a->follow_symlinks ||
427                     archive_entry_filetype(entry) != AE_IFLNK)
428                         *fd = a->open_on_current_dir(a->tree,
429                             accpath, O_RDONLY | O_NONBLOCK);
430                 if (*fd < 0) {
431                         if (a->tree_enter_working_dir(a->tree) != 0) {
432                                 archive_set_error(&a->archive, errno,
433                                     "Couldn't access %s", accpath);
434                                 return (ARCHIVE_FAILED);
435                         }
436                 }
437         }
438
439         archive_entry_acl_clear(entry);
440
441         acl = NULL;
442
443 #ifdef ACL_TYPE_NFS4
444         /* Try NFSv4 ACL first. */
445         if (*fd >= 0)
446 #if HAVE_ACL_GET_FD_NP
447                 acl = acl_get_fd_np(*fd, ACL_TYPE_NFS4);
448 #else
449                 acl = acl_get_fd(*fd);
450 #endif
451 #if HAVE_ACL_GET_LINK_NP
452         else if (!a->follow_symlinks)
453                 acl = acl_get_link_np(accpath, ACL_TYPE_NFS4);
454 #else
455         else if ((!a->follow_symlinks)
456             && (archive_entry_filetype(entry) == AE_IFLNK))
457                 /* We can't get the ACL of a symlink, so we assume it can't
458                    have one. */
459                 acl = NULL;
460 #endif
461         else
462                 acl = acl_get_file(accpath, ACL_TYPE_NFS4);
463
464 #if HAVE_ACL_IS_TRIVIAL_NP
465         if (acl != NULL && acl_is_trivial_np(acl, &r) == 0) {
466                 /* Ignore "trivial" ACLs that just mirror the file mode. */
467                 if (r) {
468                         acl_free(acl);
469                         acl = NULL;
470                         /*
471                          * Simultaneous NFSv4 and POSIX.1e ACLs for the same
472                          * entry are not allowed, so we should return here
473                          */
474                         return (ARCHIVE_OK);
475                 }
476         }
477 #endif
478         if (acl != NULL) {
479                 r = translate_acl(a, entry, acl, ARCHIVE_ENTRY_ACL_TYPE_NFS4);
480                 acl_free(acl);
481                 if (r != ARCHIVE_OK) {
482                         archive_set_error(&a->archive, errno,
483                             "Couldn't translate NFSv4 ACLs: %s", accpath);
484                 }
485                 return (r);
486         }
487 #endif  /* ACL_TYPE_NFS4 */
488
489         /* Retrieve access ACL from file. */
490         if (*fd >= 0)
491                 acl = acl_get_fd(*fd);
492 #if HAVE_ACL_GET_LINK_NP
493         else if (!a->follow_symlinks)
494                 acl = acl_get_link_np(accpath, ACL_TYPE_ACCESS);
495 #else
496         else if ((!a->follow_symlinks)
497             && (archive_entry_filetype(entry) == AE_IFLNK))
498                 /* We can't get the ACL of a symlink, so we assume it can't
499                    have one. */
500                 acl = NULL;
501 #endif
502         else
503                 acl = acl_get_file(accpath, ACL_TYPE_ACCESS);
504
505 #if HAVE_ACL_IS_TRIVIAL_NP
506         /* Ignore "trivial" ACLs that just mirror the file mode. */
507         if (acl != NULL && acl_is_trivial_np(acl, &r) == 0) {
508                 if (r) {
509                         acl_free(acl);
510                         acl = NULL;
511                 }
512         }
513 #endif
514
515         if (acl != NULL) {
516                 r = translate_acl(a, entry, acl,
517                     ARCHIVE_ENTRY_ACL_TYPE_ACCESS);
518                 acl_free(acl);
519                 acl = NULL;
520                 if (r != ARCHIVE_OK) {
521                         archive_set_error(&a->archive, errno,
522                             "Couldn't translate access ACLs: %s", accpath);
523                         return (r);
524                 }
525         }
526
527         /* Only directories can have default ACLs. */
528         if (S_ISDIR(archive_entry_mode(entry))) {
529 #if HAVE_ACL_GET_FD_NP
530                 if (*fd >= 0)
531                         acl = acl_get_fd_np(*fd, ACL_TYPE_DEFAULT);
532                 else
533 #endif
534                 acl = acl_get_file(accpath, ACL_TYPE_DEFAULT);
535                 if (acl != NULL) {
536                         r = translate_acl(a, entry, acl,
537                             ARCHIVE_ENTRY_ACL_TYPE_DEFAULT);
538                         acl_free(acl);
539                         if (r != ARCHIVE_OK) {
540                                 archive_set_error(&a->archive, errno,
541                                     "Couldn't translate default ACLs: %s",
542                                     accpath);
543                                 return (r);
544                         }
545                 }
546         }
547         return (ARCHIVE_OK);
548 }
549
550 /*
551  * Translate system ACL into libarchive internal structure.
552  */
553
554 static struct {
555         int archive_perm;
556         int platform_perm;
557 } acl_perm_map[] = {
558         {ARCHIVE_ENTRY_ACL_EXECUTE, ACL_EXECUTE},
559         {ARCHIVE_ENTRY_ACL_WRITE, ACL_WRITE},
560         {ARCHIVE_ENTRY_ACL_READ, ACL_READ},
561 #ifdef ACL_TYPE_NFS4
562         {ARCHIVE_ENTRY_ACL_READ_DATA, ACL_READ_DATA},
563         {ARCHIVE_ENTRY_ACL_LIST_DIRECTORY, ACL_LIST_DIRECTORY},
564         {ARCHIVE_ENTRY_ACL_WRITE_DATA, ACL_WRITE_DATA},
565         {ARCHIVE_ENTRY_ACL_ADD_FILE, ACL_ADD_FILE},
566         {ARCHIVE_ENTRY_ACL_APPEND_DATA, ACL_APPEND_DATA},
567         {ARCHIVE_ENTRY_ACL_ADD_SUBDIRECTORY, ACL_ADD_SUBDIRECTORY},
568         {ARCHIVE_ENTRY_ACL_READ_NAMED_ATTRS, ACL_READ_NAMED_ATTRS},
569         {ARCHIVE_ENTRY_ACL_WRITE_NAMED_ATTRS, ACL_WRITE_NAMED_ATTRS},
570         {ARCHIVE_ENTRY_ACL_DELETE_CHILD, ACL_DELETE_CHILD},
571         {ARCHIVE_ENTRY_ACL_READ_ATTRIBUTES, ACL_READ_ATTRIBUTES},
572         {ARCHIVE_ENTRY_ACL_WRITE_ATTRIBUTES, ACL_WRITE_ATTRIBUTES},
573         {ARCHIVE_ENTRY_ACL_DELETE, ACL_DELETE},
574         {ARCHIVE_ENTRY_ACL_READ_ACL, ACL_READ_ACL},
575         {ARCHIVE_ENTRY_ACL_WRITE_ACL, ACL_WRITE_ACL},
576         {ARCHIVE_ENTRY_ACL_WRITE_OWNER, ACL_WRITE_OWNER},
577         {ARCHIVE_ENTRY_ACL_SYNCHRONIZE, ACL_SYNCHRONIZE}
578 #endif
579 };
580
581 #ifdef ACL_TYPE_NFS4
582 static struct {
583         int archive_inherit;
584         int platform_inherit;
585 } acl_inherit_map[] = {
586         {ARCHIVE_ENTRY_ACL_ENTRY_FILE_INHERIT, ACL_ENTRY_FILE_INHERIT},
587         {ARCHIVE_ENTRY_ACL_ENTRY_DIRECTORY_INHERIT, ACL_ENTRY_DIRECTORY_INHERIT},
588         {ARCHIVE_ENTRY_ACL_ENTRY_NO_PROPAGATE_INHERIT, ACL_ENTRY_NO_PROPAGATE_INHERIT},
589         {ARCHIVE_ENTRY_ACL_ENTRY_INHERIT_ONLY, ACL_ENTRY_INHERIT_ONLY},
590         {ARCHIVE_ENTRY_ACL_ENTRY_SUCCESSFUL_ACCESS, ACL_ENTRY_SUCCESSFUL_ACCESS},
591         {ARCHIVE_ENTRY_ACL_ENTRY_FAILED_ACCESS, ACL_ENTRY_FAILED_ACCESS},
592         {ARCHIVE_ENTRY_ACL_ENTRY_INHERITED, ACL_ENTRY_INHERITED}
593 };
594 #endif
595 static int
596 translate_acl(struct archive_read_disk *a,
597     struct archive_entry *entry, acl_t acl, int default_entry_acl_type)
598 {
599         acl_tag_t        acl_tag;
600 #ifdef ACL_TYPE_NFS4
601         acl_entry_type_t acl_type;
602         acl_flagset_t    acl_flagset;
603         int brand;
604 #endif
605         acl_entry_t      acl_entry;
606         acl_permset_t    acl_permset;
607         int              i, entry_acl_type;
608         int              r, s, ae_id, ae_tag, ae_perm;
609         const char      *ae_name;
610
611 #ifdef ACL_TYPE_NFS4
612         // FreeBSD "brands" ACLs as POSIX.1e or NFSv4
613         // Make sure the "brand" on this ACL is consistent
614         // with the default_entry_acl_type bits provided.
615         if (acl_get_brand_np(acl, &brand) != 0) {
616                 archive_set_error(&a->archive, errno,
617                     "Failed to read ACL brand");
618                 return (ARCHIVE_WARN);
619         }
620         switch (brand) {
621         case ACL_BRAND_POSIX:
622                 switch (default_entry_acl_type) {
623                 case ARCHIVE_ENTRY_ACL_TYPE_ACCESS:
624                 case ARCHIVE_ENTRY_ACL_TYPE_DEFAULT:
625                         break;
626                 default:
627                         archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
628                             "Invalid ACL entry type for POSIX.1e ACL");
629                         return (ARCHIVE_WARN);
630                 }
631                 break;
632         case ACL_BRAND_NFS4:
633                 if (default_entry_acl_type & ~ARCHIVE_ENTRY_ACL_TYPE_NFS4) {
634                         archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
635                             "Invalid ACL entry type for NFSv4 ACL");
636                         return (ARCHIVE_WARN);
637                 }
638                 break;
639         default:
640                 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
641                     "Unknown ACL brand");
642                 return (ARCHIVE_WARN);
643         }
644 #endif
645
646
647         s = acl_get_entry(acl, ACL_FIRST_ENTRY, &acl_entry);
648         if (s == -1) {
649                 archive_set_error(&a->archive, errno,
650                     "Failed to get first ACL entry");
651                 return (ARCHIVE_WARN);
652         }
653         while (s == 1) {
654                 ae_id = -1;
655                 ae_name = NULL;
656                 ae_perm = 0;
657
658                 if (acl_get_tag_type(acl_entry, &acl_tag) != 0) {
659                         archive_set_error(&a->archive, errno,
660                             "Failed to get ACL tag type");
661                         return (ARCHIVE_WARN);
662                 }
663                 switch (acl_tag) {
664                 case ACL_USER:
665                         ae_id = (int)*(uid_t *)acl_get_qualifier(acl_entry);
666                         ae_name = archive_read_disk_uname(&a->archive, ae_id);
667                         ae_tag = ARCHIVE_ENTRY_ACL_USER;
668                         break;
669                 case ACL_GROUP:
670                         ae_id = (int)*(gid_t *)acl_get_qualifier(acl_entry);
671                         ae_name = archive_read_disk_gname(&a->archive, ae_id);
672                         ae_tag = ARCHIVE_ENTRY_ACL_GROUP;
673                         break;
674                 case ACL_MASK:
675                         ae_tag = ARCHIVE_ENTRY_ACL_MASK;
676                         break;
677                 case ACL_USER_OBJ:
678                         ae_tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
679                         break;
680                 case ACL_GROUP_OBJ:
681                         ae_tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
682                         break;
683                 case ACL_OTHER:
684                         ae_tag = ARCHIVE_ENTRY_ACL_OTHER;
685                         break;
686 #ifdef ACL_TYPE_NFS4
687                 case ACL_EVERYONE:
688                         ae_tag = ARCHIVE_ENTRY_ACL_EVERYONE;
689                         break;
690 #endif
691                 default:
692                         /* Skip types that libarchive can't support. */
693                         s = acl_get_entry(acl, ACL_NEXT_ENTRY, &acl_entry);
694                         continue;
695                 }
696
697                 // XXX acl_type maps to allow/deny/audit/YYYY bits
698                 entry_acl_type = default_entry_acl_type;
699 #ifdef ACL_TYPE_NFS4
700                 if (default_entry_acl_type & ARCHIVE_ENTRY_ACL_TYPE_NFS4) {
701                         /*
702                          * acl_get_entry_type_np() fails with non-NFSv4 ACLs
703                          */
704                         if (acl_get_entry_type_np(acl_entry, &acl_type) != 0) {
705                                 archive_set_error(&a->archive, errno, "Failed "
706                                     "to get ACL type from a NFSv4 ACL entry");
707                                 return (ARCHIVE_WARN);
708                         }
709                         switch (acl_type) {
710                         case ACL_ENTRY_TYPE_ALLOW:
711                                 entry_acl_type = ARCHIVE_ENTRY_ACL_TYPE_ALLOW;
712                                 break;
713                         case ACL_ENTRY_TYPE_DENY:
714                                 entry_acl_type = ARCHIVE_ENTRY_ACL_TYPE_DENY;
715                                 break;
716                         case ACL_ENTRY_TYPE_AUDIT:
717                                 entry_acl_type = ARCHIVE_ENTRY_ACL_TYPE_AUDIT;
718                                 break;
719                         case ACL_ENTRY_TYPE_ALARM:
720                                 entry_acl_type = ARCHIVE_ENTRY_ACL_TYPE_ALARM;
721                                 break;
722                         default:
723                                 archive_set_error(&a->archive, errno,
724                                     "Invalid NFSv4 ACL entry type");
725                                 return (ARCHIVE_WARN);
726                         }
727
728                         /*
729                          * Libarchive stores "flag" (NFSv4 inheritance bits)
730                          * in the ae_perm bitmap.
731                          *
732                          * acl_get_flagset_np() fails with non-NFSv4 ACLs
733                          */
734                         if (acl_get_flagset_np(acl_entry, &acl_flagset) != 0) {
735                                 archive_set_error(&a->archive, errno,
736                                     "Failed to get flagset from a NFSv4 ACL entry");
737                                 return (ARCHIVE_WARN);
738                         }
739                         for (i = 0; i < (int)(sizeof(acl_inherit_map) / sizeof(acl_inherit_map[0])); ++i) {
740                                 r = acl_get_flag_np(acl_flagset,
741                                     acl_inherit_map[i].platform_inherit);
742                                 if (r == -1) {
743                                         archive_set_error(&a->archive, errno,
744                                             "Failed to check flag in a NFSv4 "
745                                             "ACL flagset");
746                                         return (ARCHIVE_WARN);
747                                 } else if (r)
748                                         ae_perm |= acl_inherit_map[i].archive_inherit;
749                         }
750                 }
751 #endif
752
753                 if (acl_get_permset(acl_entry, &acl_permset) != 0) {
754                         archive_set_error(&a->archive, errno,
755                             "Failed to get ACL permission set");
756                         return (ARCHIVE_WARN);
757                 }
758                 for (i = 0; i < (int)(sizeof(acl_perm_map) / sizeof(acl_perm_map[0])); ++i) {
759                         /*
760                          * acl_get_perm() is spelled differently on different
761                          * platforms; see above.
762                          */
763                         r = ACL_GET_PERM(acl_permset, acl_perm_map[i].platform_perm);
764                         if (r == -1) {
765                                 archive_set_error(&a->archive, errno,
766                                     "Failed to check permission in an ACL permission set");
767                                 return (ARCHIVE_WARN);
768                         } else if (r)
769                                 ae_perm |= acl_perm_map[i].archive_perm;
770                 }
771
772                 archive_entry_acl_add_entry(entry, entry_acl_type,
773                                             ae_perm, ae_tag,
774                                             ae_id, ae_name);
775
776                 s = acl_get_entry(acl, ACL_NEXT_ENTRY, &acl_entry);
777                 if (s == -1) {
778                         archive_set_error(&a->archive, errno,
779                             "Failed to get next ACL entry");
780                         return (ARCHIVE_WARN);
781                 }
782         }
783         return (ARCHIVE_OK);
784 }
785 #else
786 static int
787 setup_acls(struct archive_read_disk *a,
788     struct archive_entry *entry, int *fd)
789 {
790         (void)a;      /* UNUSED */
791         (void)entry;  /* UNUSED */
792         (void)fd;     /* UNUSED */
793         return (ARCHIVE_OK);
794 }
795 #endif
796
797 #if (HAVE_FGETXATTR && HAVE_FLISTXATTR && HAVE_LISTXATTR && \
798     HAVE_LLISTXATTR && HAVE_GETXATTR && HAVE_LGETXATTR) || \
799     (HAVE_FGETEA && HAVE_FLISTEA && HAVE_LISTEA)
800
801 /*
802  * Linux and AIX extended attribute support.
803  *
804  * TODO:  By using a stack-allocated buffer for the first
805  * call to getxattr(), we might be able to avoid the second
806  * call entirely.  We only need the second call if the
807  * stack-allocated buffer is too small.  But a modest buffer
808  * of 1024 bytes or so will often be big enough.  Same applies
809  * to listxattr().
810  */
811
812
813 static int
814 setup_xattr(struct archive_read_disk *a,
815     struct archive_entry *entry, const char *name, int fd)
816 {
817         ssize_t size;
818         void *value = NULL;
819         const char *accpath;
820
821         accpath = archive_entry_sourcepath(entry);
822         if (accpath == NULL)
823                 accpath = archive_entry_pathname(entry);
824
825 #if HAVE_FGETXATTR
826         if (fd >= 0)
827                 size = fgetxattr(fd, name, NULL, 0);
828         else if (!a->follow_symlinks)
829                 size = lgetxattr(accpath, name, NULL, 0);
830         else
831                 size = getxattr(accpath, name, NULL, 0);
832 #elif HAVE_FGETEA
833         if (fd >= 0)
834                 size = fgetea(fd, name, NULL, 0);
835         else if (!a->follow_symlinks)
836                 size = lgetea(accpath, name, NULL, 0);
837         else
838                 size = getea(accpath, name, NULL, 0);
839 #endif
840
841         if (size == -1) {
842                 archive_set_error(&a->archive, errno,
843                     "Couldn't query extended attribute");
844                 return (ARCHIVE_WARN);
845         }
846
847         if (size > 0 && (value = malloc(size)) == NULL) {
848                 archive_set_error(&a->archive, errno, "Out of memory");
849                 return (ARCHIVE_FATAL);
850         }
851
852 #if HAVE_FGETXATTR
853         if (fd >= 0)
854                 size = fgetxattr(fd, name, value, size);
855         else if (!a->follow_symlinks)
856                 size = lgetxattr(accpath, name, value, size);
857         else
858                 size = getxattr(accpath, name, value, size);
859 #elif HAVE_FGETEA
860         if (fd >= 0)
861                 size = fgetea(fd, name, value, size);
862         else if (!a->follow_symlinks)
863                 size = lgetea(accpath, name, value, size);
864         else
865                 size = getea(accpath, name, value, size);
866 #endif
867
868         if (size == -1) {
869                 archive_set_error(&a->archive, errno,
870                     "Couldn't read extended attribute");
871                 return (ARCHIVE_WARN);
872         }
873
874         archive_entry_xattr_add_entry(entry, name, value, size);
875
876         free(value);
877         return (ARCHIVE_OK);
878 }
879
880 static int
881 setup_xattrs(struct archive_read_disk *a,
882     struct archive_entry *entry, int *fd)
883 {
884         char *list, *p;
885         const char *path;
886         ssize_t list_size;
887
888         path = archive_entry_sourcepath(entry);
889         if (path == NULL)
890                 path = archive_entry_pathname(entry);
891
892         if (*fd < 0 && a->tree != NULL) {
893                 if (a->follow_symlinks ||
894                     archive_entry_filetype(entry) != AE_IFLNK)
895                         *fd = a->open_on_current_dir(a->tree, path,
896                                 O_RDONLY | O_NONBLOCK);
897                 if (*fd < 0) {
898                         if (a->tree_enter_working_dir(a->tree) != 0) {
899                                 archive_set_error(&a->archive, errno,
900                                     "Couldn't access %s", path);
901                                 return (ARCHIVE_FAILED);
902                         }
903                 }
904         }
905
906 #if HAVE_FLISTXATTR
907         if (*fd >= 0)
908                 list_size = flistxattr(*fd, NULL, 0);
909         else if (!a->follow_symlinks)
910                 list_size = llistxattr(path, NULL, 0);
911         else
912                 list_size = listxattr(path, NULL, 0);
913 #elif HAVE_FLISTEA
914         if (*fd >= 0)
915                 list_size = flistea(*fd, NULL, 0);
916         else if (!a->follow_symlinks)
917                 list_size = llistea(path, NULL, 0);
918         else
919                 list_size = listea(path, NULL, 0);
920 #endif
921
922         if (list_size == -1) {
923                 if (errno == ENOTSUP || errno == ENOSYS)
924                         return (ARCHIVE_OK);
925                 archive_set_error(&a->archive, errno,
926                         "Couldn't list extended attributes");
927                 return (ARCHIVE_WARN);
928         }
929
930         if (list_size == 0)
931                 return (ARCHIVE_OK);
932
933         if ((list = malloc(list_size)) == NULL) {
934                 archive_set_error(&a->archive, errno, "Out of memory");
935                 return (ARCHIVE_FATAL);
936         }
937
938 #if HAVE_FLISTXATTR
939         if (*fd >= 0)
940                 list_size = flistxattr(*fd, list, list_size);
941         else if (!a->follow_symlinks)
942                 list_size = llistxattr(path, list, list_size);
943         else
944                 list_size = listxattr(path, list, list_size);
945 #elif HAVE_FLISTEA
946         if (*fd >= 0)
947                 list_size = flistea(*fd, list, list_size);
948         else if (!a->follow_symlinks)
949                 list_size = llistea(path, list, list_size);
950         else
951                 list_size = listea(path, list, list_size);
952 #endif
953
954         if (list_size == -1) {
955                 archive_set_error(&a->archive, errno,
956                         "Couldn't retrieve extended attributes");
957                 free(list);
958                 return (ARCHIVE_WARN);
959         }
960
961         for (p = list; (p - list) < list_size; p += strlen(p) + 1) {
962                 if (strncmp(p, "system.", 7) == 0 ||
963                                 strncmp(p, "xfsroot.", 8) == 0)
964                         continue;
965                 setup_xattr(a, entry, p, *fd);
966         }
967
968         free(list);
969         return (ARCHIVE_OK);
970 }
971
972 #elif HAVE_EXTATTR_GET_FILE && HAVE_EXTATTR_LIST_FILE && \
973     HAVE_DECL_EXTATTR_NAMESPACE_USER
974
975 /*
976  * FreeBSD extattr interface.
977  */
978
979 /* TODO: Implement this.  Follow the Linux model above, but
980  * with FreeBSD-specific system calls, of course.  Be careful
981  * to not include the system extattrs that hold ACLs; we handle
982  * those separately.
983  */
984 static int
985 setup_xattr(struct archive_read_disk *a, struct archive_entry *entry,
986     int namespace, const char *name, const char *fullname, int fd);
987
988 static int
989 setup_xattr(struct archive_read_disk *a, struct archive_entry *entry,
990     int namespace, const char *name, const char *fullname, int fd)
991 {
992         ssize_t size;
993         void *value = NULL;
994         const char *accpath;
995
996         accpath = archive_entry_sourcepath(entry);
997         if (accpath == NULL)
998                 accpath = archive_entry_pathname(entry);
999
1000         if (fd >= 0)
1001                 size = extattr_get_fd(fd, namespace, name, NULL, 0);
1002         else if (!a->follow_symlinks)
1003                 size = extattr_get_link(accpath, namespace, name, NULL, 0);
1004         else
1005                 size = extattr_get_file(accpath, namespace, name, NULL, 0);
1006
1007         if (size == -1) {
1008                 archive_set_error(&a->archive, errno,
1009                     "Couldn't query extended attribute");
1010                 return (ARCHIVE_WARN);
1011         }
1012
1013         if (size > 0 && (value = malloc(size)) == NULL) {
1014                 archive_set_error(&a->archive, errno, "Out of memory");
1015                 return (ARCHIVE_FATAL);
1016         }
1017
1018         if (fd >= 0)
1019                 size = extattr_get_fd(fd, namespace, name, value, size);
1020         else if (!a->follow_symlinks)
1021                 size = extattr_get_link(accpath, namespace, name, value, size);
1022         else
1023                 size = extattr_get_file(accpath, namespace, name, value, size);
1024
1025         if (size == -1) {
1026                 free(value);
1027                 archive_set_error(&a->archive, errno,
1028                     "Couldn't read extended attribute");
1029                 return (ARCHIVE_WARN);
1030         }
1031
1032         archive_entry_xattr_add_entry(entry, fullname, value, size);
1033
1034         free(value);
1035         return (ARCHIVE_OK);
1036 }
1037
1038 static int
1039 setup_xattrs(struct archive_read_disk *a,
1040     struct archive_entry *entry, int *fd)
1041 {
1042         char buff[512];
1043         char *list, *p;
1044         ssize_t list_size;
1045         const char *path;
1046         int namespace = EXTATTR_NAMESPACE_USER;
1047
1048         path = archive_entry_sourcepath(entry);
1049         if (path == NULL)
1050                 path = archive_entry_pathname(entry);
1051
1052         if (*fd < 0 && a->tree != NULL) {
1053                 if (a->follow_symlinks ||
1054                     archive_entry_filetype(entry) != AE_IFLNK)
1055                         *fd = a->open_on_current_dir(a->tree, path,
1056                                 O_RDONLY | O_NONBLOCK);
1057                 if (*fd < 0) {
1058                         if (a->tree_enter_working_dir(a->tree) != 0) {
1059                                 archive_set_error(&a->archive, errno,
1060                                     "Couldn't access %s", path);
1061                                 return (ARCHIVE_FAILED);
1062                         }
1063                 }
1064         }
1065
1066         if (*fd >= 0)
1067                 list_size = extattr_list_fd(*fd, namespace, NULL, 0);
1068         else if (!a->follow_symlinks)
1069                 list_size = extattr_list_link(path, namespace, NULL, 0);
1070         else
1071                 list_size = extattr_list_file(path, namespace, NULL, 0);
1072
1073         if (list_size == -1 && errno == EOPNOTSUPP)
1074                 return (ARCHIVE_OK);
1075         if (list_size == -1) {
1076                 archive_set_error(&a->archive, errno,
1077                         "Couldn't list extended attributes");
1078                 return (ARCHIVE_WARN);
1079         }
1080
1081         if (list_size == 0)
1082                 return (ARCHIVE_OK);
1083
1084         if ((list = malloc(list_size)) == NULL) {
1085                 archive_set_error(&a->archive, errno, "Out of memory");
1086                 return (ARCHIVE_FATAL);
1087         }
1088
1089         if (*fd >= 0)
1090                 list_size = extattr_list_fd(*fd, namespace, list, list_size);
1091         else if (!a->follow_symlinks)
1092                 list_size = extattr_list_link(path, namespace, list, list_size);
1093         else
1094                 list_size = extattr_list_file(path, namespace, list, list_size);
1095
1096         if (list_size == -1) {
1097                 archive_set_error(&a->archive, errno,
1098                         "Couldn't retrieve extended attributes");
1099                 free(list);
1100                 return (ARCHIVE_WARN);
1101         }
1102
1103         p = list;
1104         while ((p - list) < list_size) {
1105                 size_t len = 255 & (int)*p;
1106                 char *name;
1107
1108                 strcpy(buff, "user.");
1109                 name = buff + strlen(buff);
1110                 memcpy(name, p + 1, len);
1111                 name[len] = '\0';
1112                 setup_xattr(a, entry, namespace, name, buff, *fd);
1113                 p += 1 + len;
1114         }
1115
1116         free(list);
1117         return (ARCHIVE_OK);
1118 }
1119
1120 #else
1121
1122 /*
1123  * Generic (stub) extended attribute support.
1124  */
1125 static int
1126 setup_xattrs(struct archive_read_disk *a,
1127     struct archive_entry *entry, int *fd)
1128 {
1129         (void)a;     /* UNUSED */
1130         (void)entry; /* UNUSED */
1131         (void)fd;    /* UNUSED */
1132         return (ARCHIVE_OK);
1133 }
1134
1135 #endif
1136
1137 #if defined(HAVE_LINUX_FIEMAP_H)
1138
1139 /*
1140  * Linux FIEMAP sparse interface.
1141  *
1142  * The FIEMAP ioctl returns an "extent" for each physical allocation
1143  * on disk.  We need to process those to generate a more compact list
1144  * of logical file blocks.  We also need to be very careful to use
1145  * FIEMAP_FLAG_SYNC here, since there are reports that Linux sometimes
1146  * does not report allocations for newly-written data that hasn't
1147  * been synced to disk.
1148  *
1149  * It's important to return a minimal sparse file list because we want
1150  * to not trigger sparse file extensions if we don't have to, since
1151  * not all readers support them.
1152  */
1153
1154 static int
1155 setup_sparse_fiemap(struct archive_read_disk *a,
1156     struct archive_entry *entry, int *fd)
1157 {
1158         char buff[4096];
1159         struct fiemap *fm;
1160         struct fiemap_extent *fe;
1161         int64_t size;
1162         int count, do_fiemap, iters;
1163         int exit_sts = ARCHIVE_OK;
1164
1165         if (archive_entry_filetype(entry) != AE_IFREG
1166             || archive_entry_size(entry) <= 0
1167             || archive_entry_hardlink(entry) != NULL)
1168                 return (ARCHIVE_OK);
1169
1170         if (*fd < 0) {
1171                 const char *path;
1172
1173                 path = archive_entry_sourcepath(entry);
1174                 if (path == NULL)
1175                         path = archive_entry_pathname(entry);
1176                 if (a->tree != NULL)
1177                         *fd = a->open_on_current_dir(a->tree, path,
1178                                 O_RDONLY | O_NONBLOCK | O_CLOEXEC);
1179                 else
1180                         *fd = open(path, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
1181                 if (*fd < 0) {
1182                         archive_set_error(&a->archive, errno,
1183                             "Can't open `%s'", path);
1184                         return (ARCHIVE_FAILED);
1185                 }
1186                 __archive_ensure_cloexec_flag(*fd);
1187         }
1188
1189         /* Initialize buffer to avoid the error valgrind complains about. */
1190         memset(buff, 0, sizeof(buff));
1191         count = (sizeof(buff) - sizeof(*fm))/sizeof(*fe);
1192         fm = (struct fiemap *)buff;
1193         fm->fm_start = 0;
1194         fm->fm_length = ~0ULL;;
1195         fm->fm_flags = FIEMAP_FLAG_SYNC;
1196         fm->fm_extent_count = count;
1197         do_fiemap = 1;
1198         size = archive_entry_size(entry);
1199         for (iters = 0; ; ++iters) {
1200                 int i, r;
1201
1202                 r = ioctl(*fd, FS_IOC_FIEMAP, fm); 
1203                 if (r < 0) {
1204                         /* When something error happens, it is better we
1205                          * should return ARCHIVE_OK because an earlier
1206                          * version(<2.6.28) cannot perform FS_IOC_FIEMAP. */
1207                         goto exit_setup_sparse_fiemap;
1208                 }
1209                 if (fm->fm_mapped_extents == 0) {
1210                         if (iters == 0) {
1211                                 /* Fully sparse file; insert a zero-length "data" entry */
1212                                 archive_entry_sparse_add_entry(entry, 0, 0);
1213                         }
1214                         break;
1215                 }
1216                 fe = fm->fm_extents;
1217                 for (i = 0; i < (int)fm->fm_mapped_extents; i++, fe++) {
1218                         if (!(fe->fe_flags & FIEMAP_EXTENT_UNWRITTEN)) {
1219                                 /* The fe_length of the last block does not
1220                                  * adjust itself to its size files. */
1221                                 int64_t length = fe->fe_length;
1222                                 if (fe->fe_logical + length > (uint64_t)size)
1223                                         length -= fe->fe_logical + length - size;
1224                                 if (fe->fe_logical == 0 && length == size) {
1225                                         /* This is not sparse. */
1226                                         do_fiemap = 0;
1227                                         break;
1228                                 }
1229                                 if (length > 0)
1230                                         archive_entry_sparse_add_entry(entry,
1231                                             fe->fe_logical, length);
1232                         }
1233                         if (fe->fe_flags & FIEMAP_EXTENT_LAST)
1234                                 do_fiemap = 0;
1235                 }
1236                 if (do_fiemap) {
1237                         fe = fm->fm_extents + fm->fm_mapped_extents -1;
1238                         fm->fm_start = fe->fe_logical + fe->fe_length;
1239                 } else
1240                         break;
1241         }
1242 exit_setup_sparse_fiemap:
1243         return (exit_sts);
1244 }
1245
1246 #if !defined(SEEK_HOLE) || !defined(SEEK_DATA)
1247 static int
1248 setup_sparse(struct archive_read_disk *a,
1249     struct archive_entry *entry, int *fd)
1250 {
1251         return setup_sparse_fiemap(a, entry, fd);
1252 }
1253 #endif
1254 #endif  /* defined(HAVE_LINUX_FIEMAP_H) */
1255
1256 #if defined(SEEK_HOLE) && defined(SEEK_DATA)
1257
1258 /*
1259  * SEEK_HOLE sparse interface (FreeBSD, Linux, Solaris)
1260  */
1261
1262 static int
1263 setup_sparse(struct archive_read_disk *a,
1264     struct archive_entry *entry, int *fd)
1265 {
1266         int64_t size;
1267         off_t initial_off;
1268         off_t off_s, off_e;
1269         int exit_sts = ARCHIVE_OK;
1270         int check_fully_sparse = 0;
1271
1272         if (archive_entry_filetype(entry) != AE_IFREG
1273             || archive_entry_size(entry) <= 0
1274             || archive_entry_hardlink(entry) != NULL)
1275                 return (ARCHIVE_OK);
1276
1277         /* Does filesystem support the reporting of hole ? */
1278         if (*fd < 0 && a->tree != NULL) {
1279                 const char *path;
1280
1281                 path = archive_entry_sourcepath(entry);
1282                 if (path == NULL)
1283                         path = archive_entry_pathname(entry);
1284                 *fd = a->open_on_current_dir(a->tree, path,
1285                                 O_RDONLY | O_NONBLOCK);
1286                 if (*fd < 0) {
1287                         archive_set_error(&a->archive, errno,
1288                             "Can't open `%s'", path);
1289                         return (ARCHIVE_FAILED);
1290                 }
1291         }
1292
1293         if (*fd >= 0) {
1294 #ifdef _PC_MIN_HOLE_SIZE
1295                 if (fpathconf(*fd, _PC_MIN_HOLE_SIZE) <= 0)
1296                         return (ARCHIVE_OK);
1297 #endif
1298                 initial_off = lseek(*fd, 0, SEEK_CUR);
1299                 if (initial_off != 0)
1300                         lseek(*fd, 0, SEEK_SET);
1301         } else {
1302                 const char *path;
1303
1304                 path = archive_entry_sourcepath(entry);
1305                 if (path == NULL)
1306                         path = archive_entry_pathname(entry);
1307                         
1308 #ifdef _PC_MIN_HOLE_SIZE
1309                 if (pathconf(path, _PC_MIN_HOLE_SIZE) <= 0)
1310                         return (ARCHIVE_OK);
1311 #endif
1312                 *fd = open(path, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
1313                 if (*fd < 0) {
1314                         archive_set_error(&a->archive, errno,
1315                             "Can't open `%s'", path);
1316                         return (ARCHIVE_FAILED);
1317                 }
1318                 __archive_ensure_cloexec_flag(*fd);
1319                 initial_off = 0;
1320         }
1321
1322 #ifndef _PC_MIN_HOLE_SIZE
1323         /* Check if the underlying filesystem supports seek hole */
1324         off_s = lseek(*fd, 0, SEEK_HOLE);
1325         if (off_s < 0)
1326 #if defined(HAVE_LINUX_FIEMAP_H)
1327                 return setup_sparse_fiemap(a, entry, fd);
1328 #else
1329                 goto exit_setup_sparse;
1330 #endif
1331         else if (off_s > 0)
1332                 lseek(*fd, 0, SEEK_SET);
1333 #endif
1334
1335         off_s = 0;
1336         size = archive_entry_size(entry);
1337         while (off_s < size) {
1338                 off_s = lseek(*fd, off_s, SEEK_DATA);
1339                 if (off_s == (off_t)-1) {
1340                         if (errno == ENXIO) {
1341                                 /* no more hole */
1342                                 if (archive_entry_sparse_count(entry) == 0) {
1343                                         /* Potentially a fully-sparse file. */
1344                                         check_fully_sparse = 1;
1345                                 }
1346                                 break;
1347                         }
1348                         archive_set_error(&a->archive, errno,
1349                             "lseek(SEEK_HOLE) failed");
1350                         exit_sts = ARCHIVE_FAILED;
1351                         goto exit_setup_sparse;
1352                 }
1353                 off_e = lseek(*fd, off_s, SEEK_HOLE);
1354                 if (off_e == (off_t)-1) {
1355                         if (errno == ENXIO) {
1356                                 off_e = lseek(*fd, 0, SEEK_END);
1357                                 if (off_e != (off_t)-1)
1358                                         break;/* no more data */
1359                         }
1360                         archive_set_error(&a->archive, errno,
1361                             "lseek(SEEK_DATA) failed");
1362                         exit_sts = ARCHIVE_FAILED;
1363                         goto exit_setup_sparse;
1364                 }
1365                 if (off_s == 0 && off_e == size)
1366                         break;/* This is not sparse. */
1367                 archive_entry_sparse_add_entry(entry, off_s,
1368                         off_e - off_s);
1369                 off_s = off_e;
1370         }
1371
1372         if (check_fully_sparse) {
1373                 if (lseek(*fd, 0, SEEK_HOLE) == 0 &&
1374                         lseek(*fd, 0, SEEK_END) == size) {
1375                         /* Fully sparse file; insert a zero-length "data" entry */
1376                         archive_entry_sparse_add_entry(entry, 0, 0);
1377                 }
1378         }
1379 exit_setup_sparse:
1380         lseek(*fd, initial_off, SEEK_SET);
1381         return (exit_sts);
1382 }
1383
1384 #elif !defined(HAVE_LINUX_FIEMAP_H)
1385
1386 /*
1387  * Generic (stub) sparse support.
1388  */
1389 static int
1390 setup_sparse(struct archive_read_disk *a,
1391     struct archive_entry *entry, int *fd)
1392 {
1393         (void)a;     /* UNUSED */
1394         (void)entry; /* UNUSED */
1395         (void)fd;    /* UNUSED */
1396         return (ARCHIVE_OK);
1397 }
1398
1399 #endif
1400
1401 #endif /* !defined(_WIN32) || defined(__CYGWIN__) */
1402