]> CyberLeo.Net >> Repos - FreeBSD/releng/10.3.git/blob - contrib/libarchive/libarchive/archive_read_disk_entry_from_file.c
Fix bspatch heap overflow vulnerability. [SA-16:29]
[FreeBSD/releng/10.3.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         r1 = setup_xattrs(a, entry, &fd);
255         if (r1 < r)
256                 r = r1;
257         if (a->enable_copyfile) {
258                 r1 = setup_mac_metadata(a, entry, &fd);
259                 if (r1 < r)
260                         r = r1;
261         }
262         r1 = setup_sparse(a, entry, &fd);
263         if (r1 < r)
264                 r = r1;
265
266         /* If we opened the file earlier in this function, close it. */
267         if (initial_fd != fd)
268                 close(fd);
269         return (r);
270 }
271
272 #if defined(__APPLE__) && defined(HAVE_COPYFILE_H)
273 /*
274  * The Mac OS "copyfile()" API copies the extended metadata for a
275  * file into a separate file in AppleDouble format (see RFC 1740).
276  *
277  * Mac OS tar and cpio implementations store this extended
278  * metadata as a separate entry just before the regular entry
279  * with a "._" prefix added to the filename.
280  *
281  * Note that this is currently done unconditionally; the tar program has
282  * an option to discard this information before the archive is written.
283  *
284  * TODO: If there's a failure, report it and return ARCHIVE_WARN.
285  */
286 static int
287 setup_mac_metadata(struct archive_read_disk *a,
288     struct archive_entry *entry, int *fd)
289 {
290         int tempfd = -1;
291         int copyfile_flags = COPYFILE_NOFOLLOW | COPYFILE_ACL | COPYFILE_XATTR;
292         struct stat copyfile_stat;
293         int ret = ARCHIVE_OK;
294         void *buff = NULL;
295         int have_attrs;
296         const char *name, *tempdir;
297         struct archive_string tempfile;
298
299         (void)fd; /* UNUSED */
300         name = archive_entry_sourcepath(entry);
301         if (name == NULL)
302                 name = archive_entry_pathname(entry);
303         if (name == NULL) {
304                 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
305                     "Can't open file to read extended attributes: No name");
306                 return (ARCHIVE_WARN);
307         }
308
309         if (a->tree != NULL) {
310                 if (a->tree_enter_working_dir(a->tree) != 0) {
311                         archive_set_error(&a->archive, errno,
312                                     "Couldn't change dir");
313                                 return (ARCHIVE_FAILED);
314                 }
315         }
316
317         /* Short-circuit if there's nothing to do. */
318         have_attrs = copyfile(name, NULL, 0, copyfile_flags | COPYFILE_CHECK);
319         if (have_attrs == -1) {
320                 archive_set_error(&a->archive, errno,
321                         "Could not check extended attributes");
322                 return (ARCHIVE_WARN);
323         }
324         if (have_attrs == 0)
325                 return (ARCHIVE_OK);
326
327         tempdir = NULL;
328         if (issetugid() == 0)
329                 tempdir = getenv("TMPDIR");
330         if (tempdir == NULL)
331                 tempdir = _PATH_TMP;
332         archive_string_init(&tempfile);
333         archive_strcpy(&tempfile, tempdir);
334         archive_strcat(&tempfile, "tar.md.XXXXXX");
335         tempfd = mkstemp(tempfile.s);
336         if (tempfd < 0) {
337                 archive_set_error(&a->archive, errno,
338                     "Could not open extended attribute file");
339                 ret = ARCHIVE_WARN;
340                 goto cleanup;
341         }
342         __archive_ensure_cloexec_flag(tempfd);
343
344         /* XXX I wish copyfile() could pack directly to a memory
345          * buffer; that would avoid the temp file here.  For that
346          * matter, it would be nice if fcopyfile() actually worked,
347          * that would reduce the many open/close races here. */
348         if (copyfile(name, tempfile.s, 0, copyfile_flags | COPYFILE_PACK)) {
349                 archive_set_error(&a->archive, errno,
350                     "Could not pack extended attributes");
351                 ret = ARCHIVE_WARN;
352                 goto cleanup;
353         }
354         if (fstat(tempfd, &copyfile_stat)) {
355                 archive_set_error(&a->archive, errno,
356                     "Could not check size of extended attributes");
357                 ret = ARCHIVE_WARN;
358                 goto cleanup;
359         }
360         buff = malloc(copyfile_stat.st_size);
361         if (buff == NULL) {
362                 archive_set_error(&a->archive, errno,
363                     "Could not allocate memory for extended attributes");
364                 ret = ARCHIVE_WARN;
365                 goto cleanup;
366         }
367         if (copyfile_stat.st_size != read(tempfd, buff, copyfile_stat.st_size)) {
368                 archive_set_error(&a->archive, errno,
369                     "Could not read extended attributes into memory");
370                 ret = ARCHIVE_WARN;
371                 goto cleanup;
372         }
373         archive_entry_copy_mac_metadata(entry, buff, copyfile_stat.st_size);
374
375 cleanup:
376         if (tempfd >= 0) {
377                 close(tempfd);
378                 unlink(tempfile.s);
379         }
380         archive_string_free(&tempfile);
381         free(buff);
382         return (ret);
383 }
384
385 #else
386
387 /*
388  * Stub implementation for non-Mac systems.
389  */
390 static int
391 setup_mac_metadata(struct archive_read_disk *a,
392     struct archive_entry *entry, int *fd)
393 {
394         (void)a; /* UNUSED */
395         (void)entry; /* UNUSED */
396         (void)fd; /* UNUSED */
397         return (ARCHIVE_OK);
398 }
399 #endif
400
401
402 #if defined(HAVE_POSIX_ACL) && defined(ACL_TYPE_NFS4)
403 static int translate_acl(struct archive_read_disk *a,
404     struct archive_entry *entry, acl_t acl, int archive_entry_acl_type);
405
406 static int
407 setup_acls(struct archive_read_disk *a,
408     struct archive_entry *entry, int *fd)
409 {
410         const char      *accpath;
411         acl_t            acl;
412         int             r;
413
414         accpath = archive_entry_sourcepath(entry);
415         if (accpath == NULL)
416                 accpath = archive_entry_pathname(entry);
417
418         archive_entry_acl_clear(entry);
419
420         /* Try NFS4 ACL first. */
421         if (*fd >= 0)
422                 acl = acl_get_fd(*fd);
423 #if HAVE_ACL_GET_LINK_NP
424         else if (!a->follow_symlinks)
425                 acl = acl_get_link_np(accpath, ACL_TYPE_NFS4);
426 #else
427         else if ((!a->follow_symlinks)
428             && (archive_entry_filetype(entry) == AE_IFLNK))
429                 /* We can't get the ACL of a symlink, so we assume it can't
430                    have one. */
431                 acl = NULL;
432 #endif
433         else
434                 acl = acl_get_file(accpath, ACL_TYPE_NFS4);
435 #if HAVE_ACL_IS_TRIVIAL_NP
436         /* Ignore "trivial" ACLs that just mirror the file mode. */
437         acl_is_trivial_np(acl, &r);
438         if (r) {
439                 acl_free(acl);
440                 acl = NULL;
441         }
442 #endif
443         if (acl != NULL) {
444                 r = translate_acl(a, entry, acl, ARCHIVE_ENTRY_ACL_TYPE_NFS4);
445                 acl_free(acl);
446                 if (r != ARCHIVE_OK) {
447                         archive_set_error(&a->archive, errno,
448                             "Couldn't translate NFSv4 ACLs: %s", accpath);
449                 }
450                 return (r);
451         }
452
453         /* Retrieve access ACL from file. */
454         if (*fd >= 0)
455                 acl = acl_get_fd(*fd);
456 #if HAVE_ACL_GET_LINK_NP
457         else if (!a->follow_symlinks)
458                 acl = acl_get_link_np(accpath, ACL_TYPE_ACCESS);
459 #else
460         else if ((!a->follow_symlinks)
461             && (archive_entry_filetype(entry) == AE_IFLNK))
462                 /* We can't get the ACL of a symlink, so we assume it can't
463                    have one. */
464                 acl = NULL;
465 #endif
466         else
467                 acl = acl_get_file(accpath, ACL_TYPE_ACCESS);
468         if (acl != NULL) {
469                 r = translate_acl(a, entry, acl,
470                     ARCHIVE_ENTRY_ACL_TYPE_ACCESS);
471                 acl_free(acl);
472                 if (r != ARCHIVE_OK) {
473                         archive_set_error(&a->archive, errno,
474                             "Couldn't translate access ACLs: %s", accpath);
475                         return (r);
476                 }
477         }
478
479         /* Only directories can have default ACLs. */
480         if (S_ISDIR(archive_entry_mode(entry))) {
481                 acl = acl_get_file(accpath, ACL_TYPE_DEFAULT);
482                 if (acl != NULL) {
483                         r = translate_acl(a, entry, acl,
484                             ARCHIVE_ENTRY_ACL_TYPE_DEFAULT);
485                         acl_free(acl);
486                         if (r != ARCHIVE_OK) {
487                                 archive_set_error(&a->archive, errno,
488                                     "Couldn't translate default ACLs: %s",
489                                     accpath);
490                                 return (r);
491                         }
492                 }
493         }
494         return (ARCHIVE_OK);
495 }
496
497 /*
498  * Translate system ACL into libarchive internal structure.
499  */
500
501 static struct {
502         int archive_perm;
503         int platform_perm;
504 } acl_perm_map[] = {
505         {ARCHIVE_ENTRY_ACL_EXECUTE, ACL_EXECUTE},
506         {ARCHIVE_ENTRY_ACL_WRITE, ACL_WRITE},
507         {ARCHIVE_ENTRY_ACL_READ, ACL_READ},
508         {ARCHIVE_ENTRY_ACL_READ_DATA, ACL_READ_DATA},
509         {ARCHIVE_ENTRY_ACL_LIST_DIRECTORY, ACL_LIST_DIRECTORY},
510         {ARCHIVE_ENTRY_ACL_WRITE_DATA, ACL_WRITE_DATA},
511         {ARCHIVE_ENTRY_ACL_ADD_FILE, ACL_ADD_FILE},
512         {ARCHIVE_ENTRY_ACL_APPEND_DATA, ACL_APPEND_DATA},
513         {ARCHIVE_ENTRY_ACL_ADD_SUBDIRECTORY, ACL_ADD_SUBDIRECTORY},
514         {ARCHIVE_ENTRY_ACL_READ_NAMED_ATTRS, ACL_READ_NAMED_ATTRS},
515         {ARCHIVE_ENTRY_ACL_WRITE_NAMED_ATTRS, ACL_WRITE_NAMED_ATTRS},
516         {ARCHIVE_ENTRY_ACL_DELETE_CHILD, ACL_DELETE_CHILD},
517         {ARCHIVE_ENTRY_ACL_READ_ATTRIBUTES, ACL_READ_ATTRIBUTES},
518         {ARCHIVE_ENTRY_ACL_WRITE_ATTRIBUTES, ACL_WRITE_ATTRIBUTES},
519         {ARCHIVE_ENTRY_ACL_DELETE, ACL_DELETE},
520         {ARCHIVE_ENTRY_ACL_READ_ACL, ACL_READ_ACL},
521         {ARCHIVE_ENTRY_ACL_WRITE_ACL, ACL_WRITE_ACL},
522         {ARCHIVE_ENTRY_ACL_WRITE_OWNER, ACL_WRITE_OWNER},
523         {ARCHIVE_ENTRY_ACL_SYNCHRONIZE, ACL_SYNCHRONIZE}
524 };
525
526 static struct {
527         int archive_inherit;
528         int platform_inherit;
529 } acl_inherit_map[] = {
530         {ARCHIVE_ENTRY_ACL_ENTRY_FILE_INHERIT, ACL_ENTRY_FILE_INHERIT},
531         {ARCHIVE_ENTRY_ACL_ENTRY_DIRECTORY_INHERIT, ACL_ENTRY_DIRECTORY_INHERIT},
532         {ARCHIVE_ENTRY_ACL_ENTRY_NO_PROPAGATE_INHERIT, ACL_ENTRY_NO_PROPAGATE_INHERIT},
533         {ARCHIVE_ENTRY_ACL_ENTRY_INHERIT_ONLY, ACL_ENTRY_INHERIT_ONLY}
534 };
535
536 static int
537 translate_acl(struct archive_read_disk *a,
538     struct archive_entry *entry, acl_t acl, int default_entry_acl_type)
539 {
540         acl_tag_t        acl_tag;
541         acl_entry_type_t acl_type;
542         acl_flagset_t    acl_flagset;
543         acl_entry_t      acl_entry;
544         acl_permset_t    acl_permset;
545         int              brand, i, r, entry_acl_type;
546         int              s, ae_id, ae_tag, ae_perm;
547         const char      *ae_name;
548
549         // FreeBSD "brands" ACLs as POSIX.1e or NFSv4
550         // Make sure the "brand" on this ACL is consistent
551         // with the default_entry_acl_type bits provided.
552         if (acl_get_brand_np(acl, &brand) != 0) {
553                 archive_set_error(&a->archive, errno,
554                     "Failed to read ACL brand");
555                 return (ARCHIVE_WARN);
556         }
557         switch (brand) {
558         case ACL_BRAND_POSIX:
559                 switch (default_entry_acl_type) {
560                 case ARCHIVE_ENTRY_ACL_TYPE_ACCESS:
561                 case ARCHIVE_ENTRY_ACL_TYPE_DEFAULT:
562                         break;
563                 default:
564                         archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
565                             "Invalid ACL entry type for POSIX.1e ACL");
566                         return (ARCHIVE_WARN);
567                 }
568                 break;
569         case ACL_BRAND_NFS4:
570                 if (default_entry_acl_type & ~ARCHIVE_ENTRY_ACL_TYPE_NFS4) {
571                         archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
572                             "Invalid ACL entry type for NFSv4 ACL");
573                         return (ARCHIVE_WARN);
574                 }
575                 break;
576         default:
577                 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
578                     "Unknown ACL brand");
579                 return (ARCHIVE_WARN);
580                 break;
581         }
582
583
584         s = acl_get_entry(acl, ACL_FIRST_ENTRY, &acl_entry);
585         if (s == -1) {
586                 archive_set_error(&a->archive, errno,
587                     "Failed to get first ACL entry");
588                 return (ARCHIVE_WARN);
589         }
590         while (s == 1) {
591                 ae_id = -1;
592                 ae_name = NULL;
593                 ae_perm = 0;
594
595                 if (acl_get_tag_type(acl_entry, &acl_tag) != 0) {
596                         archive_set_error(&a->archive, errno,
597                             "Failed to get ACL tag type");
598                         return (ARCHIVE_WARN);
599                 }
600                 switch (acl_tag) {
601                 case ACL_USER:
602                         ae_id = (int)*(uid_t *)acl_get_qualifier(acl_entry);
603                         ae_name = archive_read_disk_uname(&a->archive, ae_id);
604                         ae_tag = ARCHIVE_ENTRY_ACL_USER;
605                         break;
606                 case ACL_GROUP:
607                         ae_id = (int)*(gid_t *)acl_get_qualifier(acl_entry);
608                         ae_name = archive_read_disk_gname(&a->archive, ae_id);
609                         ae_tag = ARCHIVE_ENTRY_ACL_GROUP;
610                         break;
611                 case ACL_MASK:
612                         ae_tag = ARCHIVE_ENTRY_ACL_MASK;
613                         break;
614                 case ACL_USER_OBJ:
615                         ae_tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
616                         break;
617                 case ACL_GROUP_OBJ:
618                         ae_tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
619                         break;
620                 case ACL_OTHER:
621                         ae_tag = ARCHIVE_ENTRY_ACL_OTHER;
622                         break;
623                 case ACL_EVERYONE:
624                         ae_tag = ARCHIVE_ENTRY_ACL_EVERYONE;
625                         break;
626                 default:
627                         /* Skip types that libarchive can't support. */
628                         s = acl_get_entry(acl, ACL_NEXT_ENTRY, &acl_entry);
629                         continue;
630                 }
631
632                 // XXX acl_type maps to allow/deny/audit/YYYY bits
633                 entry_acl_type = default_entry_acl_type;
634                 if (default_entry_acl_type & ARCHIVE_ENTRY_ACL_TYPE_NFS4) {
635                         /*
636                          * acl_get_entry_type_np() falis with non-NFSv4 ACLs
637                          */
638                         if (acl_get_entry_type_np(acl_entry, &acl_type) != 0) {
639                                 archive_set_error(&a->archive, errno, "Failed "
640                                     "to get ACL type from a NFSv4 ACL entry");
641                                 return (ARCHIVE_WARN);
642                         }
643                         switch (acl_type) {
644                         case ACL_ENTRY_TYPE_ALLOW:
645                                 entry_acl_type = ARCHIVE_ENTRY_ACL_TYPE_ALLOW;
646                                 break;
647                         case ACL_ENTRY_TYPE_DENY:
648                                 entry_acl_type = ARCHIVE_ENTRY_ACL_TYPE_DENY;
649                                 break;
650                         case ACL_ENTRY_TYPE_AUDIT:
651                                 entry_acl_type = ARCHIVE_ENTRY_ACL_TYPE_AUDIT;
652                                 break;
653                         case ACL_ENTRY_TYPE_ALARM:
654                                 entry_acl_type = ARCHIVE_ENTRY_ACL_TYPE_ALARM;
655                                 break;
656                         default:
657                                 archive_set_error(&a->archive, errno,
658                                     "Invalid NFSv4 ACL entry type");
659                                 return (ARCHIVE_WARN);
660                         }
661
662                         /*
663                          * Libarchive stores "flag" (NFSv4 inheritance bits)
664                          * in the ae_perm bitmap.
665                          *
666                          * acl_get_flagset_np() fails with non-NFSv4 ACLs
667                          */
668                         if (acl_get_flagset_np(acl_entry, &acl_flagset) != 0) {
669                                 archive_set_error(&a->archive, errno,
670                                     "Failed to get flagset from a NFSv4 ACL entry");
671                                 return (ARCHIVE_WARN);
672                         }
673                         for (i = 0; i < (int)(sizeof(acl_inherit_map) / sizeof(acl_inherit_map[0])); ++i) {
674                                 r = acl_get_flag_np(acl_flagset,
675                                     acl_inherit_map[i].platform_inherit);
676                                 if (r == -1) {
677                                         archive_set_error(&a->archive, errno,
678                                             "Failed to check flag in a NFSv4 "
679                                             "ACL flagset");
680                                         return (ARCHIVE_WARN);
681                                 } else if (r)
682                                         ae_perm |= acl_inherit_map[i].archive_inherit;
683                         }
684                 }
685
686                 if (acl_get_permset(acl_entry, &acl_permset) != 0) {
687                         archive_set_error(&a->archive, errno,
688                             "Failed to get ACL permission set");
689                         return (ARCHIVE_WARN);
690                 }
691                 for (i = 0; i < (int)(sizeof(acl_perm_map) / sizeof(acl_perm_map[0])); ++i) {
692                         /*
693                          * acl_get_perm() is spelled differently on different
694                          * platforms; see above.
695                          */
696                         r = ACL_GET_PERM(acl_permset, acl_perm_map[i].platform_perm);
697                         if (r == -1) {
698                                 archive_set_error(&a->archive, errno,
699                                     "Failed to check permission in an ACL permission set");
700                                 return (ARCHIVE_WARN);
701                         } else if (r)
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                 if (s == -1) {
711                         archive_set_error(&a->archive, errno,
712                             "Failed to get next ACL entry");
713                         return (ARCHIVE_WARN);
714                 }
715         }
716         return (ARCHIVE_OK);
717 }
718 #else
719 static int
720 setup_acls(struct archive_read_disk *a,
721     struct archive_entry *entry, int *fd)
722 {
723         (void)a;      /* UNUSED */
724         (void)entry;  /* UNUSED */
725         (void)fd;     /* UNUSED */
726         return (ARCHIVE_OK);
727 }
728 #endif
729
730 #if (HAVE_FGETXATTR && HAVE_FLISTXATTR && HAVE_LISTXATTR && \
731     HAVE_LLISTXATTR && HAVE_GETXATTR && HAVE_LGETXATTR) || \
732     (HAVE_FGETEA && HAVE_FLISTEA && HAVE_LISTEA)
733
734 /*
735  * Linux and AIX extended attribute support.
736  *
737  * TODO:  By using a stack-allocated buffer for the first
738  * call to getxattr(), we might be able to avoid the second
739  * call entirely.  We only need the second call if the
740  * stack-allocated buffer is too small.  But a modest buffer
741  * of 1024 bytes or so will often be big enough.  Same applies
742  * to listxattr().
743  */
744
745
746 static int
747 setup_xattr(struct archive_read_disk *a,
748     struct archive_entry *entry, const char *name, int fd)
749 {
750         ssize_t size;
751         void *value = NULL;
752         const char *accpath;
753
754         accpath = archive_entry_sourcepath(entry);
755         if (accpath == NULL)
756                 accpath = archive_entry_pathname(entry);
757
758 #if HAVE_FGETXATTR
759         if (fd >= 0)
760                 size = fgetxattr(fd, name, NULL, 0);
761         else if (!a->follow_symlinks)
762                 size = lgetxattr(accpath, name, NULL, 0);
763         else
764                 size = getxattr(accpath, name, NULL, 0);
765 #elif HAVE_FGETEA
766         if (fd >= 0)
767                 size = fgetea(fd, name, NULL, 0);
768         else if (!a->follow_symlinks)
769                 size = lgetea(accpath, name, NULL, 0);
770         else
771                 size = getea(accpath, name, NULL, 0);
772 #endif
773
774         if (size == -1) {
775                 archive_set_error(&a->archive, errno,
776                     "Couldn't query extended attribute");
777                 return (ARCHIVE_WARN);
778         }
779
780         if (size > 0 && (value = malloc(size)) == NULL) {
781                 archive_set_error(&a->archive, errno, "Out of memory");
782                 return (ARCHIVE_FATAL);
783         }
784
785 #if HAVE_FGETXATTR
786         if (fd >= 0)
787                 size = fgetxattr(fd, name, value, size);
788         else if (!a->follow_symlinks)
789                 size = lgetxattr(accpath, name, value, size);
790         else
791                 size = getxattr(accpath, name, value, size);
792 #elif HAVE_FGETEA
793         if (fd >= 0)
794                 size = fgetea(fd, name, value, size);
795         else if (!a->follow_symlinks)
796                 size = lgetea(accpath, name, value, size);
797         else
798                 size = getea(accpath, name, value, size);
799 #endif
800
801         if (size == -1) {
802                 archive_set_error(&a->archive, errno,
803                     "Couldn't read extended attribute");
804                 return (ARCHIVE_WARN);
805         }
806
807         archive_entry_xattr_add_entry(entry, name, value, size);
808
809         free(value);
810         return (ARCHIVE_OK);
811 }
812
813 static int
814 setup_xattrs(struct archive_read_disk *a,
815     struct archive_entry *entry, int *fd)
816 {
817         char *list, *p;
818         const char *path;
819         ssize_t list_size;
820
821         path = archive_entry_sourcepath(entry);
822         if (path == NULL)
823                 path = archive_entry_pathname(entry);
824
825         if (*fd < 0 && a->tree != NULL) {
826                 if (a->follow_symlinks ||
827                     archive_entry_filetype(entry) != AE_IFLNK)
828                         *fd = a->open_on_current_dir(a->tree, path,
829                                 O_RDONLY | O_NONBLOCK);
830                 if (*fd < 0) {
831                         if (a->tree_enter_working_dir(a->tree) != 0) {
832                                 archive_set_error(&a->archive, errno,
833                                     "Couldn't access %s", path);
834                                 return (ARCHIVE_FAILED);
835                         }
836                 }
837         }
838
839 #if HAVE_FLISTXATTR
840         if (*fd >= 0)
841                 list_size = flistxattr(*fd, NULL, 0);
842         else if (!a->follow_symlinks)
843                 list_size = llistxattr(path, NULL, 0);
844         else
845                 list_size = listxattr(path, NULL, 0);
846 #elif HAVE_FLISTEA
847         if (*fd >= 0)
848                 list_size = flistea(*fd, NULL, 0);
849         else if (!a->follow_symlinks)
850                 list_size = llistea(path, NULL, 0);
851         else
852                 list_size = listea(path, NULL, 0);
853 #endif
854
855         if (list_size == -1) {
856                 if (errno == ENOTSUP || errno == ENOSYS)
857                         return (ARCHIVE_OK);
858                 archive_set_error(&a->archive, errno,
859                         "Couldn't list extended attributes");
860                 return (ARCHIVE_WARN);
861         }
862
863         if (list_size == 0)
864                 return (ARCHIVE_OK);
865
866         if ((list = malloc(list_size)) == NULL) {
867                 archive_set_error(&a->archive, errno, "Out of memory");
868                 return (ARCHIVE_FATAL);
869         }
870
871 #if HAVE_FLISTXATTR
872         if (*fd >= 0)
873                 list_size = flistxattr(*fd, list, list_size);
874         else if (!a->follow_symlinks)
875                 list_size = llistxattr(path, list, list_size);
876         else
877                 list_size = listxattr(path, list, list_size);
878 #elif HAVE_FLISTEA
879         if (*fd >= 0)
880                 list_size = flistea(*fd, list, list_size);
881         else if (!a->follow_symlinks)
882                 list_size = llistea(path, list, list_size);
883         else
884                 list_size = listea(path, list, list_size);
885 #endif
886
887         if (list_size == -1) {
888                 archive_set_error(&a->archive, errno,
889                         "Couldn't retrieve extended attributes");
890                 free(list);
891                 return (ARCHIVE_WARN);
892         }
893
894         for (p = list; (p - list) < list_size; p += strlen(p) + 1) {
895                 if (strncmp(p, "system.", 7) == 0 ||
896                                 strncmp(p, "xfsroot.", 8) == 0)
897                         continue;
898                 setup_xattr(a, entry, p, *fd);
899         }
900
901         free(list);
902         return (ARCHIVE_OK);
903 }
904
905 #elif HAVE_EXTATTR_GET_FILE && HAVE_EXTATTR_LIST_FILE && \
906     HAVE_DECL_EXTATTR_NAMESPACE_USER
907
908 /*
909  * FreeBSD extattr interface.
910  */
911
912 /* TODO: Implement this.  Follow the Linux model above, but
913  * with FreeBSD-specific system calls, of course.  Be careful
914  * to not include the system extattrs that hold ACLs; we handle
915  * those separately.
916  */
917 static int
918 setup_xattr(struct archive_read_disk *a, struct archive_entry *entry,
919     int namespace, const char *name, const char *fullname, int fd);
920
921 static int
922 setup_xattr(struct archive_read_disk *a, struct archive_entry *entry,
923     int namespace, const char *name, const char *fullname, int fd)
924 {
925         ssize_t size;
926         void *value = NULL;
927         const char *accpath;
928
929         accpath = archive_entry_sourcepath(entry);
930         if (accpath == NULL)
931                 accpath = archive_entry_pathname(entry);
932
933         if (fd >= 0)
934                 size = extattr_get_fd(fd, namespace, name, NULL, 0);
935         else if (!a->follow_symlinks)
936                 size = extattr_get_link(accpath, namespace, name, NULL, 0);
937         else
938                 size = extattr_get_file(accpath, namespace, name, NULL, 0);
939
940         if (size == -1) {
941                 archive_set_error(&a->archive, errno,
942                     "Couldn't query extended attribute");
943                 return (ARCHIVE_WARN);
944         }
945
946         if (size > 0 && (value = malloc(size)) == NULL) {
947                 archive_set_error(&a->archive, errno, "Out of memory");
948                 return (ARCHIVE_FATAL);
949         }
950
951         if (fd >= 0)
952                 size = extattr_get_fd(fd, namespace, name, value, size);
953         else if (!a->follow_symlinks)
954                 size = extattr_get_link(accpath, namespace, name, value, size);
955         else
956                 size = extattr_get_file(accpath, namespace, name, value, size);
957
958         if (size == -1) {
959                 free(value);
960                 archive_set_error(&a->archive, errno,
961                     "Couldn't read extended attribute");
962                 return (ARCHIVE_WARN);
963         }
964
965         archive_entry_xattr_add_entry(entry, fullname, value, size);
966
967         free(value);
968         return (ARCHIVE_OK);
969 }
970
971 static int
972 setup_xattrs(struct archive_read_disk *a,
973     struct archive_entry *entry, int *fd)
974 {
975         char buff[512];
976         char *list, *p;
977         ssize_t list_size;
978         const char *path;
979         int namespace = EXTATTR_NAMESPACE_USER;
980
981         path = archive_entry_sourcepath(entry);
982         if (path == NULL)
983                 path = archive_entry_pathname(entry);
984
985         if (*fd < 0 && a->tree != NULL) {
986                 if (a->follow_symlinks ||
987                     archive_entry_filetype(entry) != AE_IFLNK)
988                         *fd = a->open_on_current_dir(a->tree, path,
989                                 O_RDONLY | O_NONBLOCK);
990                 if (*fd < 0) {
991                         if (a->tree_enter_working_dir(a->tree) != 0) {
992                                 archive_set_error(&a->archive, errno,
993                                     "Couldn't access %s", path);
994                                 return (ARCHIVE_FAILED);
995                         }
996                 }
997         }
998
999         if (*fd >= 0)
1000                 list_size = extattr_list_fd(*fd, namespace, NULL, 0);
1001         else if (!a->follow_symlinks)
1002                 list_size = extattr_list_link(path, namespace, NULL, 0);
1003         else
1004                 list_size = extattr_list_file(path, namespace, NULL, 0);
1005
1006         if (list_size == -1 && errno == EOPNOTSUPP)
1007                 return (ARCHIVE_OK);
1008         if (list_size == -1) {
1009                 archive_set_error(&a->archive, errno,
1010                         "Couldn't list extended attributes");
1011                 return (ARCHIVE_WARN);
1012         }
1013
1014         if (list_size == 0)
1015                 return (ARCHIVE_OK);
1016
1017         if ((list = malloc(list_size)) == NULL) {
1018                 archive_set_error(&a->archive, errno, "Out of memory");
1019                 return (ARCHIVE_FATAL);
1020         }
1021
1022         if (*fd >= 0)
1023                 list_size = extattr_list_fd(*fd, namespace, list, list_size);
1024         else if (!a->follow_symlinks)
1025                 list_size = extattr_list_link(path, namespace, list, list_size);
1026         else
1027                 list_size = extattr_list_file(path, namespace, list, list_size);
1028
1029         if (list_size == -1) {
1030                 archive_set_error(&a->archive, errno,
1031                         "Couldn't retrieve extended attributes");
1032                 free(list);
1033                 return (ARCHIVE_WARN);
1034         }
1035
1036         p = list;
1037         while ((p - list) < list_size) {
1038                 size_t len = 255 & (int)*p;
1039                 char *name;
1040
1041                 strcpy(buff, "user.");
1042                 name = buff + strlen(buff);
1043                 memcpy(name, p + 1, len);
1044                 name[len] = '\0';
1045                 setup_xattr(a, entry, namespace, name, buff, *fd);
1046                 p += 1 + len;
1047         }
1048
1049         free(list);
1050         return (ARCHIVE_OK);
1051 }
1052
1053 #else
1054
1055 /*
1056  * Generic (stub) extended attribute support.
1057  */
1058 static int
1059 setup_xattrs(struct archive_read_disk *a,
1060     struct archive_entry *entry, int *fd)
1061 {
1062         (void)a;     /* UNUSED */
1063         (void)entry; /* UNUSED */
1064         (void)fd;    /* UNUSED */
1065         return (ARCHIVE_OK);
1066 }
1067
1068 #endif
1069
1070 #if defined(HAVE_LINUX_FIEMAP_H)
1071
1072 /*
1073  * Linux sparse interface.
1074  *
1075  * The FIEMAP ioctl returns an "extent" for each physical allocation
1076  * on disk.  We need to process those to generate a more compact list
1077  * of logical file blocks.  We also need to be very careful to use
1078  * FIEMAP_FLAG_SYNC here, since there are reports that Linux sometimes
1079  * does not report allocations for newly-written data that hasn't
1080  * been synced to disk.
1081  *
1082  * It's important to return a minimal sparse file list because we want
1083  * to not trigger sparse file extensions if we don't have to, since
1084  * not all readers support them.
1085  */
1086
1087 static int
1088 setup_sparse(struct archive_read_disk *a,
1089     struct archive_entry *entry, int *fd)
1090 {
1091         char buff[4096];
1092         struct fiemap *fm;
1093         struct fiemap_extent *fe;
1094         int64_t size;
1095         int count, do_fiemap;
1096         int exit_sts = ARCHIVE_OK;
1097
1098         if (archive_entry_filetype(entry) != AE_IFREG
1099             || archive_entry_size(entry) <= 0
1100             || archive_entry_hardlink(entry) != NULL)
1101                 return (ARCHIVE_OK);
1102
1103         if (*fd < 0) {
1104                 const char *path;
1105
1106                 path = archive_entry_sourcepath(entry);
1107                 if (path == NULL)
1108                         path = archive_entry_pathname(entry);
1109                 if (a->tree != NULL)
1110                         *fd = a->open_on_current_dir(a->tree, path,
1111                                 O_RDONLY | O_NONBLOCK | O_CLOEXEC);
1112                 else
1113                         *fd = open(path, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
1114                 if (*fd < 0) {
1115                         archive_set_error(&a->archive, errno,
1116                             "Can't open `%s'", path);
1117                         return (ARCHIVE_FAILED);
1118                 }
1119                 __archive_ensure_cloexec_flag(*fd);
1120         }
1121
1122         /* Initialize buffer to avoid the error valgrind complains about. */
1123         memset(buff, 0, sizeof(buff));
1124         count = (sizeof(buff) - sizeof(*fm))/sizeof(*fe);
1125         fm = (struct fiemap *)buff;
1126         fm->fm_start = 0;
1127         fm->fm_length = ~0ULL;;
1128         fm->fm_flags = FIEMAP_FLAG_SYNC;
1129         fm->fm_extent_count = count;
1130         do_fiemap = 1;
1131         size = archive_entry_size(entry);
1132         for (;;) {
1133                 int i, r;
1134
1135                 r = ioctl(*fd, FS_IOC_FIEMAP, fm); 
1136                 if (r < 0) {
1137                         /* When something error happens, it is better we
1138                          * should return ARCHIVE_OK because an earlier
1139                          * version(<2.6.28) cannot perfom FS_IOC_FIEMAP. */
1140                         goto exit_setup_sparse;
1141                 }
1142                 if (fm->fm_mapped_extents == 0)
1143                         break;
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
1189         if (archive_entry_filetype(entry) != AE_IFREG
1190             || archive_entry_size(entry) <= 0
1191             || archive_entry_hardlink(entry) != NULL)
1192                 return (ARCHIVE_OK);
1193
1194         /* Does filesystem support the reporting of hole ? */
1195         if (*fd < 0 && a->tree != NULL) {
1196                 const char *path;
1197
1198                 path = archive_entry_sourcepath(entry);
1199                 if (path == NULL)
1200                         path = archive_entry_pathname(entry);
1201                 *fd = a->open_on_current_dir(a->tree, path,
1202                                 O_RDONLY | O_NONBLOCK);
1203                 if (*fd < 0) {
1204                         archive_set_error(&a->archive, errno,
1205                             "Can't open `%s'", path);
1206                         return (ARCHIVE_FAILED);
1207                 }
1208         }
1209
1210         if (*fd >= 0) {
1211                 if (fpathconf(*fd, _PC_MIN_HOLE_SIZE) <= 0)
1212                         return (ARCHIVE_OK);
1213                 initial_off = lseek(*fd, 0, SEEK_CUR);
1214                 if (initial_off != 0)
1215                         lseek(*fd, 0, SEEK_SET);
1216         } else {
1217                 const char *path;
1218
1219                 path = archive_entry_sourcepath(entry);
1220                 if (path == NULL)
1221                         path = archive_entry_pathname(entry);
1222                         
1223                 if (pathconf(path, _PC_MIN_HOLE_SIZE) <= 0)
1224                         return (ARCHIVE_OK);
1225                 *fd = open(path, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
1226                 if (*fd < 0) {
1227                         archive_set_error(&a->archive, errno,
1228                             "Can't open `%s'", path);
1229                         return (ARCHIVE_FAILED);
1230                 }
1231                 __archive_ensure_cloexec_flag(*fd);
1232                 initial_off = 0;
1233         }
1234
1235         off_s = 0;
1236         size = archive_entry_size(entry);
1237         while (off_s < size) {
1238                 off_s = lseek(*fd, off_s, SEEK_DATA);
1239                 if (off_s == (off_t)-1) {
1240                         if (errno == ENXIO)
1241                                 break;/* no more hole */
1242                         archive_set_error(&a->archive, errno,
1243                             "lseek(SEEK_HOLE) failed");
1244                         exit_sts = ARCHIVE_FAILED;
1245                         goto exit_setup_sparse;
1246                 }
1247                 off_e = lseek(*fd, off_s, SEEK_HOLE);
1248                 if (off_e == (off_t)-1) {
1249                         if (errno == ENXIO) {
1250                                 off_e = lseek(*fd, 0, SEEK_END);
1251                                 if (off_e != (off_t)-1)
1252                                         break;/* no more data */
1253                         }
1254                         archive_set_error(&a->archive, errno,
1255                             "lseek(SEEK_DATA) failed");
1256                         exit_sts = ARCHIVE_FAILED;
1257                         goto exit_setup_sparse;
1258                 }
1259                 if (off_s == 0 && off_e == size)
1260                         break;/* This is not spase. */
1261                 archive_entry_sparse_add_entry(entry, off_s,
1262                         off_e - off_s);
1263                 off_s = off_e;
1264         }
1265 exit_setup_sparse:
1266         lseek(*fd, initial_off, SEEK_SET);
1267         return (exit_sts);
1268 }
1269
1270 #else
1271
1272 /*
1273  * Generic (stub) sparse support.
1274  */
1275 static int
1276 setup_sparse(struct archive_read_disk *a,
1277     struct archive_entry *entry, int *fd)
1278 {
1279         (void)a;     /* UNUSED */
1280         (void)entry; /* UNUSED */
1281         (void)fd;    /* UNUSED */
1282         return (ARCHIVE_OK);
1283 }
1284
1285 #endif
1286
1287 #endif /* !defined(_WIN32) || defined(__CYGWIN__) */
1288