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