archive_read_disk_entry_from_file.c revision 306321
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: stable/11/contrib/libarchive/libarchive/archive_read_disk_entry_from_file.c 306321 2016-09-25 22:02:27Z mm $"); 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 120static int setup_acls(struct archive_read_disk *, 121 struct archive_entry *, int *fd); 122static int setup_mac_metadata(struct archive_read_disk *, 123 struct archive_entry *, int *fd); 124static int setup_xattrs(struct archive_read_disk *, 125 struct archive_entry *, int *fd); 126static int setup_sparse(struct archive_read_disk *, 127 struct archive_entry *, int *fd); 128 129int 130archive_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 */ 288static int 289setup_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, ©file_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 377cleanup: 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 */ 392static int 393setup_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 405static int translate_acl(struct archive_read_disk *a, 406 struct archive_entry *entry, acl_t acl, int archive_entry_acl_type); 407 408static int 409setup_acls(struct archive_read_disk *a, 410 struct archive_entry *entry, int *fd) 411{ 412 const char *accpath; 413 acl_t acl; 414 int r; 415 416 accpath = archive_entry_sourcepath(entry); 417 if (accpath == NULL) 418 accpath = archive_entry_pathname(entry); 419 420 if (*fd < 0 && a->tree != NULL) { 421 if (a->follow_symlinks || 422 archive_entry_filetype(entry) != AE_IFLNK) 423 *fd = a->open_on_current_dir(a->tree, 424 accpath, O_RDONLY | O_NONBLOCK); 425 if (*fd < 0) { 426 if (a->tree_enter_working_dir(a->tree) != 0) { 427 archive_set_error(&a->archive, errno, 428 "Couldn't access %s", accpath); 429 return (ARCHIVE_FAILED); 430 } 431 } 432 } 433 434 archive_entry_acl_clear(entry); 435 436 acl = NULL; 437 438#ifdef ACL_TYPE_NFS4 439 /* Try NFS4 ACL first. */ 440 if (*fd >= 0) 441#if HAVE_ACL_GET_FD_NP 442 acl = acl_get_fd_np(*fd, ACL_TYPE_NFS4); 443#else 444 acl = acl_get_fd(*fd); 445#endif 446#if HAVE_ACL_GET_LINK_NP 447 else if (!a->follow_symlinks) 448 acl = acl_get_link_np(accpath, ACL_TYPE_NFS4); 449#else 450 else if ((!a->follow_symlinks) 451 && (archive_entry_filetype(entry) == AE_IFLNK)) 452 /* We can't get the ACL of a symlink, so we assume it can't 453 have one. */ 454 acl = NULL; 455#endif 456 else 457 acl = acl_get_file(accpath, ACL_TYPE_NFS4); 458 459#if HAVE_ACL_IS_TRIVIAL_NP 460 if (acl != NULL && acl_is_trivial_np(acl, &r) == 0) { 461 /* Ignore "trivial" ACLs that just mirror the file mode. */ 462 if (r) { 463 acl_free(acl); 464 acl = NULL; 465 /* 466 * Simultaneous NFSv4 and POSIX.1e ACLs for the same 467 * entry are not allowed, so we should return here 468 */ 469 return (ARCHIVE_OK); 470 } 471 } 472#endif 473 if (acl != NULL) { 474 r = translate_acl(a, entry, acl, ARCHIVE_ENTRY_ACL_TYPE_NFS4); 475 acl_free(acl); 476 if (r != ARCHIVE_OK) { 477 archive_set_error(&a->archive, errno, 478 "Couldn't translate NFSv4 ACLs: %s", accpath); 479 } 480 return (r); 481 } 482#endif /* ACL_TYPE_NFS4 */ 483 484 /* Retrieve access ACL from file. */ 485 if (*fd >= 0) 486 acl = acl_get_fd(*fd); 487#if HAVE_ACL_GET_LINK_NP 488 else if (!a->follow_symlinks) 489 acl = acl_get_link_np(accpath, ACL_TYPE_ACCESS); 490#else 491 else if ((!a->follow_symlinks) 492 && (archive_entry_filetype(entry) == AE_IFLNK)) 493 /* We can't get the ACL of a symlink, so we assume it can't 494 have one. */ 495 acl = NULL; 496#endif 497 else 498 acl = acl_get_file(accpath, ACL_TYPE_ACCESS); 499 500#if HAVE_ACL_IS_TRIVIAL_NP 501 /* Ignore "trivial" ACLs that just mirror the file mode. */ 502 if (acl != NULL && acl_is_trivial_np(acl, &r) == 0) { 503 if (r) { 504 acl_free(acl); 505 acl = NULL; 506 } 507 } 508#endif 509 510 if (acl != NULL) { 511 r = translate_acl(a, entry, acl, 512 ARCHIVE_ENTRY_ACL_TYPE_ACCESS); 513 acl_free(acl); 514 acl = NULL; 515 if (r != ARCHIVE_OK) { 516 archive_set_error(&a->archive, errno, 517 "Couldn't translate access ACLs: %s", accpath); 518 return (r); 519 } 520 } 521 522 /* Only directories can have default ACLs. */ 523 if (S_ISDIR(archive_entry_mode(entry))) { 524 acl = acl_get_file(accpath, ACL_TYPE_DEFAULT); 525 if (acl != NULL) { 526 r = translate_acl(a, entry, acl, 527 ARCHIVE_ENTRY_ACL_TYPE_DEFAULT); 528 acl_free(acl); 529 if (r != ARCHIVE_OK) { 530 archive_set_error(&a->archive, errno, 531 "Couldn't translate default ACLs: %s", 532 accpath); 533 return (r); 534 } 535 } 536 } 537 return (ARCHIVE_OK); 538} 539 540/* 541 * Translate system ACL into libarchive internal structure. 542 */ 543 544static struct { 545 int archive_perm; 546 int platform_perm; 547} acl_perm_map[] = { 548 {ARCHIVE_ENTRY_ACL_EXECUTE, ACL_EXECUTE}, 549 {ARCHIVE_ENTRY_ACL_WRITE, ACL_WRITE}, 550 {ARCHIVE_ENTRY_ACL_READ, ACL_READ}, 551#ifdef ACL_TYPE_NFS4 552 {ARCHIVE_ENTRY_ACL_READ_DATA, ACL_READ_DATA}, 553 {ARCHIVE_ENTRY_ACL_LIST_DIRECTORY, ACL_LIST_DIRECTORY}, 554 {ARCHIVE_ENTRY_ACL_WRITE_DATA, ACL_WRITE_DATA}, 555 {ARCHIVE_ENTRY_ACL_ADD_FILE, ACL_ADD_FILE}, 556 {ARCHIVE_ENTRY_ACL_APPEND_DATA, ACL_APPEND_DATA}, 557 {ARCHIVE_ENTRY_ACL_ADD_SUBDIRECTORY, ACL_ADD_SUBDIRECTORY}, 558 {ARCHIVE_ENTRY_ACL_READ_NAMED_ATTRS, ACL_READ_NAMED_ATTRS}, 559 {ARCHIVE_ENTRY_ACL_WRITE_NAMED_ATTRS, ACL_WRITE_NAMED_ATTRS}, 560 {ARCHIVE_ENTRY_ACL_DELETE_CHILD, ACL_DELETE_CHILD}, 561 {ARCHIVE_ENTRY_ACL_READ_ATTRIBUTES, ACL_READ_ATTRIBUTES}, 562 {ARCHIVE_ENTRY_ACL_WRITE_ATTRIBUTES, ACL_WRITE_ATTRIBUTES}, 563 {ARCHIVE_ENTRY_ACL_DELETE, ACL_DELETE}, 564 {ARCHIVE_ENTRY_ACL_READ_ACL, ACL_READ_ACL}, 565 {ARCHIVE_ENTRY_ACL_WRITE_ACL, ACL_WRITE_ACL}, 566 {ARCHIVE_ENTRY_ACL_WRITE_OWNER, ACL_WRITE_OWNER}, 567 {ARCHIVE_ENTRY_ACL_SYNCHRONIZE, ACL_SYNCHRONIZE} 568#endif 569}; 570 571#ifdef ACL_TYPE_NFS4 572static struct { 573 int archive_inherit; 574 int platform_inherit; 575} acl_inherit_map[] = { 576 {ARCHIVE_ENTRY_ACL_ENTRY_FILE_INHERIT, ACL_ENTRY_FILE_INHERIT}, 577 {ARCHIVE_ENTRY_ACL_ENTRY_DIRECTORY_INHERIT, ACL_ENTRY_DIRECTORY_INHERIT}, 578 {ARCHIVE_ENTRY_ACL_ENTRY_NO_PROPAGATE_INHERIT, ACL_ENTRY_NO_PROPAGATE_INHERIT}, 579 {ARCHIVE_ENTRY_ACL_ENTRY_INHERIT_ONLY, ACL_ENTRY_INHERIT_ONLY} 580}; 581#endif 582static int 583translate_acl(struct archive_read_disk *a, 584 struct archive_entry *entry, acl_t acl, int default_entry_acl_type) 585{ 586 acl_tag_t acl_tag; 587#ifdef ACL_TYPE_NFS4 588 acl_entry_type_t acl_type; 589 acl_flagset_t acl_flagset; 590 int brand; 591#endif 592 acl_entry_t acl_entry; 593 acl_permset_t acl_permset; 594 int i, entry_acl_type; 595 int r, s, ae_id, ae_tag, ae_perm; 596 const char *ae_name; 597 598#ifdef ACL_TYPE_NFS4 599 // FreeBSD "brands" ACLs as POSIX.1e or NFSv4 600 // Make sure the "brand" on this ACL is consistent 601 // with the default_entry_acl_type bits provided. 602 if (acl_get_brand_np(acl, &brand) != 0) { 603 archive_set_error(&a->archive, errno, 604 "Failed to read ACL brand"); 605 return (ARCHIVE_WARN); 606 } 607 switch (brand) { 608 case ACL_BRAND_POSIX: 609 switch (default_entry_acl_type) { 610 case ARCHIVE_ENTRY_ACL_TYPE_ACCESS: 611 case ARCHIVE_ENTRY_ACL_TYPE_DEFAULT: 612 break; 613 default: 614 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 615 "Invalid ACL entry type for POSIX.1e ACL"); 616 return (ARCHIVE_WARN); 617 } 618 break; 619 case ACL_BRAND_NFS4: 620 if (default_entry_acl_type & ~ARCHIVE_ENTRY_ACL_TYPE_NFS4) { 621 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 622 "Invalid ACL entry type for NFSv4 ACL"); 623 return (ARCHIVE_WARN); 624 } 625 break; 626 default: 627 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 628 "Unknown ACL brand"); 629 return (ARCHIVE_WARN); 630 break; 631 } 632#endif 633 634 635 s = acl_get_entry(acl, ACL_FIRST_ENTRY, &acl_entry); 636 if (s == -1) { 637 archive_set_error(&a->archive, errno, 638 "Failed to get first ACL entry"); 639 return (ARCHIVE_WARN); 640 } 641 while (s == 1) { 642 ae_id = -1; 643 ae_name = NULL; 644 ae_perm = 0; 645 646 if (acl_get_tag_type(acl_entry, &acl_tag) != 0) { 647 archive_set_error(&a->archive, errno, 648 "Failed to get ACL tag type"); 649 return (ARCHIVE_WARN); 650 } 651 switch (acl_tag) { 652 case ACL_USER: 653 ae_id = (int)*(uid_t *)acl_get_qualifier(acl_entry); 654 ae_name = archive_read_disk_uname(&a->archive, ae_id); 655 ae_tag = ARCHIVE_ENTRY_ACL_USER; 656 break; 657 case ACL_GROUP: 658 ae_id = (int)*(gid_t *)acl_get_qualifier(acl_entry); 659 ae_name = archive_read_disk_gname(&a->archive, ae_id); 660 ae_tag = ARCHIVE_ENTRY_ACL_GROUP; 661 break; 662 case ACL_MASK: 663 ae_tag = ARCHIVE_ENTRY_ACL_MASK; 664 break; 665 case ACL_USER_OBJ: 666 ae_tag = ARCHIVE_ENTRY_ACL_USER_OBJ; 667 break; 668 case ACL_GROUP_OBJ: 669 ae_tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ; 670 break; 671 case ACL_OTHER: 672 ae_tag = ARCHIVE_ENTRY_ACL_OTHER; 673 break; 674#ifdef ACL_TYPE_NFS4 675 case ACL_EVERYONE: 676 ae_tag = ARCHIVE_ENTRY_ACL_EVERYONE; 677 break; 678#endif 679 default: 680 /* Skip types that libarchive can't support. */ 681 s = acl_get_entry(acl, ACL_NEXT_ENTRY, &acl_entry); 682 continue; 683 } 684 685 // XXX acl_type maps to allow/deny/audit/YYYY bits 686 entry_acl_type = default_entry_acl_type; 687#ifdef ACL_TYPE_NFS4 688 if (default_entry_acl_type & ARCHIVE_ENTRY_ACL_TYPE_NFS4) { 689 /* 690 * acl_get_entry_type_np() falis with non-NFSv4 ACLs 691 */ 692 if (acl_get_entry_type_np(acl_entry, &acl_type) != 0) { 693 archive_set_error(&a->archive, errno, "Failed " 694 "to get ACL type from a NFSv4 ACL entry"); 695 return (ARCHIVE_WARN); 696 } 697 switch (acl_type) { 698 case ACL_ENTRY_TYPE_ALLOW: 699 entry_acl_type = ARCHIVE_ENTRY_ACL_TYPE_ALLOW; 700 break; 701 case ACL_ENTRY_TYPE_DENY: 702 entry_acl_type = ARCHIVE_ENTRY_ACL_TYPE_DENY; 703 break; 704 case ACL_ENTRY_TYPE_AUDIT: 705 entry_acl_type = ARCHIVE_ENTRY_ACL_TYPE_AUDIT; 706 break; 707 case ACL_ENTRY_TYPE_ALARM: 708 entry_acl_type = ARCHIVE_ENTRY_ACL_TYPE_ALARM; 709 break; 710 default: 711 archive_set_error(&a->archive, errno, 712 "Invalid NFSv4 ACL entry type"); 713 return (ARCHIVE_WARN); 714 } 715 716 /* 717 * Libarchive stores "flag" (NFSv4 inheritance bits) 718 * in the ae_perm bitmap. 719 * 720 * acl_get_flagset_np() fails with non-NFSv4 ACLs 721 */ 722 if (acl_get_flagset_np(acl_entry, &acl_flagset) != 0) { 723 archive_set_error(&a->archive, errno, 724 "Failed to get flagset from a NFSv4 ACL entry"); 725 return (ARCHIVE_WARN); 726 } 727 for (i = 0; i < (int)(sizeof(acl_inherit_map) / sizeof(acl_inherit_map[0])); ++i) { 728 r = acl_get_flag_np(acl_flagset, 729 acl_inherit_map[i].platform_inherit); 730 if (r == -1) { 731 archive_set_error(&a->archive, errno, 732 "Failed to check flag in a NFSv4 " 733 "ACL flagset"); 734 return (ARCHIVE_WARN); 735 } else if (r) 736 ae_perm |= acl_inherit_map[i].archive_inherit; 737 } 738 } 739#endif 740 741 if (acl_get_permset(acl_entry, &acl_permset) != 0) { 742 archive_set_error(&a->archive, errno, 743 "Failed to get ACL permission set"); 744 return (ARCHIVE_WARN); 745 } 746 for (i = 0; i < (int)(sizeof(acl_perm_map) / sizeof(acl_perm_map[0])); ++i) { 747 /* 748 * acl_get_perm() is spelled differently on different 749 * platforms; see above. 750 */ 751 r = ACL_GET_PERM(acl_permset, acl_perm_map[i].platform_perm); 752 if (r == -1) { 753 archive_set_error(&a->archive, errno, 754 "Failed to check permission in an ACL permission set"); 755 return (ARCHIVE_WARN); 756 } else if (r) 757 ae_perm |= acl_perm_map[i].archive_perm; 758 } 759 760 archive_entry_acl_add_entry(entry, entry_acl_type, 761 ae_perm, ae_tag, 762 ae_id, ae_name); 763 764 s = acl_get_entry(acl, ACL_NEXT_ENTRY, &acl_entry); 765 if (s == -1) { 766 archive_set_error(&a->archive, errno, 767 "Failed to get next ACL entry"); 768 return (ARCHIVE_WARN); 769 } 770 } 771 return (ARCHIVE_OK); 772} 773#else 774static int 775setup_acls(struct archive_read_disk *a, 776 struct archive_entry *entry, int *fd) 777{ 778 (void)a; /* UNUSED */ 779 (void)entry; /* UNUSED */ 780 (void)fd; /* UNUSED */ 781 return (ARCHIVE_OK); 782} 783#endif 784 785#if (HAVE_FGETXATTR && HAVE_FLISTXATTR && HAVE_LISTXATTR && \ 786 HAVE_LLISTXATTR && HAVE_GETXATTR && HAVE_LGETXATTR) || \ 787 (HAVE_FGETEA && HAVE_FLISTEA && HAVE_LISTEA) 788 789/* 790 * Linux and AIX extended attribute support. 791 * 792 * TODO: By using a stack-allocated buffer for the first 793 * call to getxattr(), we might be able to avoid the second 794 * call entirely. We only need the second call if the 795 * stack-allocated buffer is too small. But a modest buffer 796 * of 1024 bytes or so will often be big enough. Same applies 797 * to listxattr(). 798 */ 799 800 801static int 802setup_xattr(struct archive_read_disk *a, 803 struct archive_entry *entry, const char *name, int fd) 804{ 805 ssize_t size; 806 void *value = NULL; 807 const char *accpath; 808 809 accpath = archive_entry_sourcepath(entry); 810 if (accpath == NULL) 811 accpath = archive_entry_pathname(entry); 812 813#if HAVE_FGETXATTR 814 if (fd >= 0) 815 size = fgetxattr(fd, name, NULL, 0); 816 else if (!a->follow_symlinks) 817 size = lgetxattr(accpath, name, NULL, 0); 818 else 819 size = getxattr(accpath, name, NULL, 0); 820#elif HAVE_FGETEA 821 if (fd >= 0) 822 size = fgetea(fd, name, NULL, 0); 823 else if (!a->follow_symlinks) 824 size = lgetea(accpath, name, NULL, 0); 825 else 826 size = getea(accpath, name, NULL, 0); 827#endif 828 829 if (size == -1) { 830 archive_set_error(&a->archive, errno, 831 "Couldn't query extended attribute"); 832 return (ARCHIVE_WARN); 833 } 834 835 if (size > 0 && (value = malloc(size)) == NULL) { 836 archive_set_error(&a->archive, errno, "Out of memory"); 837 return (ARCHIVE_FATAL); 838 } 839 840#if HAVE_FGETXATTR 841 if (fd >= 0) 842 size = fgetxattr(fd, name, value, size); 843 else if (!a->follow_symlinks) 844 size = lgetxattr(accpath, name, value, size); 845 else 846 size = getxattr(accpath, name, value, size); 847#elif HAVE_FGETEA 848 if (fd >= 0) 849 size = fgetea(fd, name, value, size); 850 else if (!a->follow_symlinks) 851 size = lgetea(accpath, name, value, size); 852 else 853 size = getea(accpath, name, value, size); 854#endif 855 856 if (size == -1) { 857 archive_set_error(&a->archive, errno, 858 "Couldn't read extended attribute"); 859 return (ARCHIVE_WARN); 860 } 861 862 archive_entry_xattr_add_entry(entry, name, value, size); 863 864 free(value); 865 return (ARCHIVE_OK); 866} 867 868static int 869setup_xattrs(struct archive_read_disk *a, 870 struct archive_entry *entry, int *fd) 871{ 872 char *list, *p; 873 const char *path; 874 ssize_t list_size; 875 876 path = archive_entry_sourcepath(entry); 877 if (path == NULL) 878 path = archive_entry_pathname(entry); 879 880 if (*fd < 0 && a->tree != NULL) { 881 if (a->follow_symlinks || 882 archive_entry_filetype(entry) != AE_IFLNK) 883 *fd = a->open_on_current_dir(a->tree, path, 884 O_RDONLY | O_NONBLOCK); 885 if (*fd < 0) { 886 if (a->tree_enter_working_dir(a->tree) != 0) { 887 archive_set_error(&a->archive, errno, 888 "Couldn't access %s", path); 889 return (ARCHIVE_FAILED); 890 } 891 } 892 } 893 894#if HAVE_FLISTXATTR 895 if (*fd >= 0) 896 list_size = flistxattr(*fd, NULL, 0); 897 else if (!a->follow_symlinks) 898 list_size = llistxattr(path, NULL, 0); 899 else 900 list_size = listxattr(path, NULL, 0); 901#elif HAVE_FLISTEA 902 if (*fd >= 0) 903 list_size = flistea(*fd, NULL, 0); 904 else if (!a->follow_symlinks) 905 list_size = llistea(path, NULL, 0); 906 else 907 list_size = listea(path, NULL, 0); 908#endif 909 910 if (list_size == -1) { 911 if (errno == ENOTSUP || errno == ENOSYS) 912 return (ARCHIVE_OK); 913 archive_set_error(&a->archive, errno, 914 "Couldn't list extended attributes"); 915 return (ARCHIVE_WARN); 916 } 917 918 if (list_size == 0) 919 return (ARCHIVE_OK); 920 921 if ((list = malloc(list_size)) == NULL) { 922 archive_set_error(&a->archive, errno, "Out of memory"); 923 return (ARCHIVE_FATAL); 924 } 925 926#if HAVE_FLISTXATTR 927 if (*fd >= 0) 928 list_size = flistxattr(*fd, list, list_size); 929 else if (!a->follow_symlinks) 930 list_size = llistxattr(path, list, list_size); 931 else 932 list_size = listxattr(path, list, list_size); 933#elif HAVE_FLISTEA 934 if (*fd >= 0) 935 list_size = flistea(*fd, list, list_size); 936 else if (!a->follow_symlinks) 937 list_size = llistea(path, list, list_size); 938 else 939 list_size = listea(path, list, list_size); 940#endif 941 942 if (list_size == -1) { 943 archive_set_error(&a->archive, errno, 944 "Couldn't retrieve extended attributes"); 945 free(list); 946 return (ARCHIVE_WARN); 947 } 948 949 for (p = list; (p - list) < list_size; p += strlen(p) + 1) { 950 if (strncmp(p, "system.", 7) == 0 || 951 strncmp(p, "xfsroot.", 8) == 0) 952 continue; 953 setup_xattr(a, entry, p, *fd); 954 } 955 956 free(list); 957 return (ARCHIVE_OK); 958} 959 960#elif HAVE_EXTATTR_GET_FILE && HAVE_EXTATTR_LIST_FILE && \ 961 HAVE_DECL_EXTATTR_NAMESPACE_USER 962 963/* 964 * FreeBSD extattr interface. 965 */ 966 967/* TODO: Implement this. Follow the Linux model above, but 968 * with FreeBSD-specific system calls, of course. Be careful 969 * to not include the system extattrs that hold ACLs; we handle 970 * those separately. 971 */ 972static int 973setup_xattr(struct archive_read_disk *a, struct archive_entry *entry, 974 int namespace, const char *name, const char *fullname, int fd); 975 976static int 977setup_xattr(struct archive_read_disk *a, struct archive_entry *entry, 978 int namespace, const char *name, const char *fullname, int fd) 979{ 980 ssize_t size; 981 void *value = NULL; 982 const char *accpath; 983 984 accpath = archive_entry_sourcepath(entry); 985 if (accpath == NULL) 986 accpath = archive_entry_pathname(entry); 987 988 if (fd >= 0) 989 size = extattr_get_fd(fd, namespace, name, NULL, 0); 990 else if (!a->follow_symlinks) 991 size = extattr_get_link(accpath, namespace, name, NULL, 0); 992 else 993 size = extattr_get_file(accpath, namespace, name, NULL, 0); 994 995 if (size == -1) { 996 archive_set_error(&a->archive, errno, 997 "Couldn't query extended attribute"); 998 return (ARCHIVE_WARN); 999 } 1000 1001 if (size > 0 && (value = malloc(size)) == NULL) { 1002 archive_set_error(&a->archive, errno, "Out of memory"); 1003 return (ARCHIVE_FATAL); 1004 } 1005 1006 if (fd >= 0) 1007 size = extattr_get_fd(fd, namespace, name, value, size); 1008 else if (!a->follow_symlinks) 1009 size = extattr_get_link(accpath, namespace, name, value, size); 1010 else 1011 size = extattr_get_file(accpath, namespace, name, value, size); 1012 1013 if (size == -1) { 1014 free(value); 1015 archive_set_error(&a->archive, errno, 1016 "Couldn't read extended attribute"); 1017 return (ARCHIVE_WARN); 1018 } 1019 1020 archive_entry_xattr_add_entry(entry, fullname, value, size); 1021 1022 free(value); 1023 return (ARCHIVE_OK); 1024} 1025 1026static int 1027setup_xattrs(struct archive_read_disk *a, 1028 struct archive_entry *entry, int *fd) 1029{ 1030 char buff[512]; 1031 char *list, *p; 1032 ssize_t list_size; 1033 const char *path; 1034 int namespace = EXTATTR_NAMESPACE_USER; 1035 1036 path = archive_entry_sourcepath(entry); 1037 if (path == NULL) 1038 path = archive_entry_pathname(entry); 1039 1040 if (*fd < 0 && a->tree != NULL) { 1041 if (a->follow_symlinks || 1042 archive_entry_filetype(entry) != AE_IFLNK) 1043 *fd = a->open_on_current_dir(a->tree, path, 1044 O_RDONLY | O_NONBLOCK); 1045 if (*fd < 0) { 1046 if (a->tree_enter_working_dir(a->tree) != 0) { 1047 archive_set_error(&a->archive, errno, 1048 "Couldn't access %s", path); 1049 return (ARCHIVE_FAILED); 1050 } 1051 } 1052 } 1053 1054 if (*fd >= 0) 1055 list_size = extattr_list_fd(*fd, namespace, NULL, 0); 1056 else if (!a->follow_symlinks) 1057 list_size = extattr_list_link(path, namespace, NULL, 0); 1058 else 1059 list_size = extattr_list_file(path, namespace, NULL, 0); 1060 1061 if (list_size == -1 && errno == EOPNOTSUPP) 1062 return (ARCHIVE_OK); 1063 if (list_size == -1) { 1064 archive_set_error(&a->archive, errno, 1065 "Couldn't list extended attributes"); 1066 return (ARCHIVE_WARN); 1067 } 1068 1069 if (list_size == 0) 1070 return (ARCHIVE_OK); 1071 1072 if ((list = malloc(list_size)) == NULL) { 1073 archive_set_error(&a->archive, errno, "Out of memory"); 1074 return (ARCHIVE_FATAL); 1075 } 1076 1077 if (*fd >= 0) 1078 list_size = extattr_list_fd(*fd, namespace, list, list_size); 1079 else if (!a->follow_symlinks) 1080 list_size = extattr_list_link(path, namespace, list, list_size); 1081 else 1082 list_size = extattr_list_file(path, namespace, list, list_size); 1083 1084 if (list_size == -1) { 1085 archive_set_error(&a->archive, errno, 1086 "Couldn't retrieve extended attributes"); 1087 free(list); 1088 return (ARCHIVE_WARN); 1089 } 1090 1091 p = list; 1092 while ((p - list) < list_size) { 1093 size_t len = 255 & (int)*p; 1094 char *name; 1095 1096 strcpy(buff, "user."); 1097 name = buff + strlen(buff); 1098 memcpy(name, p + 1, len); 1099 name[len] = '\0'; 1100 setup_xattr(a, entry, namespace, name, buff, *fd); 1101 p += 1 + len; 1102 } 1103 1104 free(list); 1105 return (ARCHIVE_OK); 1106} 1107 1108#else 1109 1110/* 1111 * Generic (stub) extended attribute support. 1112 */ 1113static int 1114setup_xattrs(struct archive_read_disk *a, 1115 struct archive_entry *entry, int *fd) 1116{ 1117 (void)a; /* UNUSED */ 1118 (void)entry; /* UNUSED */ 1119 (void)fd; /* UNUSED */ 1120 return (ARCHIVE_OK); 1121} 1122 1123#endif 1124 1125#if defined(HAVE_LINUX_FIEMAP_H) 1126 1127/* 1128 * Linux sparse interface. 1129 * 1130 * The FIEMAP ioctl returns an "extent" for each physical allocation 1131 * on disk. We need to process those to generate a more compact list 1132 * of logical file blocks. We also need to be very careful to use 1133 * FIEMAP_FLAG_SYNC here, since there are reports that Linux sometimes 1134 * does not report allocations for newly-written data that hasn't 1135 * been synced to disk. 1136 * 1137 * It's important to return a minimal sparse file list because we want 1138 * to not trigger sparse file extensions if we don't have to, since 1139 * not all readers support them. 1140 */ 1141 1142static int 1143setup_sparse(struct archive_read_disk *a, 1144 struct archive_entry *entry, int *fd) 1145{ 1146 char buff[4096]; 1147 struct fiemap *fm; 1148 struct fiemap_extent *fe; 1149 int64_t size; 1150 int count, do_fiemap, iters; 1151 int exit_sts = ARCHIVE_OK; 1152 1153 if (archive_entry_filetype(entry) != AE_IFREG 1154 || archive_entry_size(entry) <= 0 1155 || archive_entry_hardlink(entry) != NULL) 1156 return (ARCHIVE_OK); 1157 1158 if (*fd < 0) { 1159 const char *path; 1160 1161 path = archive_entry_sourcepath(entry); 1162 if (path == NULL) 1163 path = archive_entry_pathname(entry); 1164 if (a->tree != NULL) 1165 *fd = a->open_on_current_dir(a->tree, path, 1166 O_RDONLY | O_NONBLOCK | O_CLOEXEC); 1167 else 1168 *fd = open(path, O_RDONLY | O_NONBLOCK | O_CLOEXEC); 1169 if (*fd < 0) { 1170 archive_set_error(&a->archive, errno, 1171 "Can't open `%s'", path); 1172 return (ARCHIVE_FAILED); 1173 } 1174 __archive_ensure_cloexec_flag(*fd); 1175 } 1176 1177 /* Initialize buffer to avoid the error valgrind complains about. */ 1178 memset(buff, 0, sizeof(buff)); 1179 count = (sizeof(buff) - sizeof(*fm))/sizeof(*fe); 1180 fm = (struct fiemap *)buff; 1181 fm->fm_start = 0; 1182 fm->fm_length = ~0ULL;; 1183 fm->fm_flags = FIEMAP_FLAG_SYNC; 1184 fm->fm_extent_count = count; 1185 do_fiemap = 1; 1186 size = archive_entry_size(entry); 1187 for (iters = 0; ; ++iters) { 1188 int i, r; 1189 1190 r = ioctl(*fd, FS_IOC_FIEMAP, fm); 1191 if (r < 0) { 1192 /* When something error happens, it is better we 1193 * should return ARCHIVE_OK because an earlier 1194 * version(<2.6.28) cannot perfom FS_IOC_FIEMAP. */ 1195 goto exit_setup_sparse; 1196 } 1197 if (fm->fm_mapped_extents == 0) { 1198 if (iters == 0) { 1199 /* Fully sparse file; insert a zero-length "data" entry */ 1200 archive_entry_sparse_add_entry(entry, 0, 0); 1201 } 1202 break; 1203 } 1204 fe = fm->fm_extents; 1205 for (i = 0; i < (int)fm->fm_mapped_extents; i++, fe++) { 1206 if (!(fe->fe_flags & FIEMAP_EXTENT_UNWRITTEN)) { 1207 /* The fe_length of the last block does not 1208 * adjust itself to its size files. */ 1209 int64_t length = fe->fe_length; 1210 if (fe->fe_logical + length > (uint64_t)size) 1211 length -= fe->fe_logical + length - size; 1212 if (fe->fe_logical == 0 && length == size) { 1213 /* This is not sparse. */ 1214 do_fiemap = 0; 1215 break; 1216 } 1217 if (length > 0) 1218 archive_entry_sparse_add_entry(entry, 1219 fe->fe_logical, length); 1220 } 1221 if (fe->fe_flags & FIEMAP_EXTENT_LAST) 1222 do_fiemap = 0; 1223 } 1224 if (do_fiemap) { 1225 fe = fm->fm_extents + fm->fm_mapped_extents -1; 1226 fm->fm_start = fe->fe_logical + fe->fe_length; 1227 } else 1228 break; 1229 } 1230exit_setup_sparse: 1231 return (exit_sts); 1232} 1233 1234#elif defined(SEEK_HOLE) && defined(SEEK_DATA) && defined(_PC_MIN_HOLE_SIZE) 1235 1236/* 1237 * FreeBSD and Solaris sparse interface. 1238 */ 1239 1240static int 1241setup_sparse(struct archive_read_disk *a, 1242 struct archive_entry *entry, int *fd) 1243{ 1244 int64_t size; 1245 off_t initial_off; /* FreeBSD/Solaris only, so off_t okay here */ 1246 off_t off_s, off_e; /* FreeBSD/Solaris only, so off_t okay here */ 1247 int exit_sts = ARCHIVE_OK; 1248 int check_fully_sparse = 0; 1249 1250 if (archive_entry_filetype(entry) != AE_IFREG 1251 || archive_entry_size(entry) <= 0 1252 || archive_entry_hardlink(entry) != NULL) 1253 return (ARCHIVE_OK); 1254 1255 /* Does filesystem support the reporting of hole ? */ 1256 if (*fd < 0 && a->tree != NULL) { 1257 const char *path; 1258 1259 path = archive_entry_sourcepath(entry); 1260 if (path == NULL) 1261 path = archive_entry_pathname(entry); 1262 *fd = a->open_on_current_dir(a->tree, path, 1263 O_RDONLY | O_NONBLOCK); 1264 if (*fd < 0) { 1265 archive_set_error(&a->archive, errno, 1266 "Can't open `%s'", path); 1267 return (ARCHIVE_FAILED); 1268 } 1269 } 1270 1271 if (*fd >= 0) { 1272 if (fpathconf(*fd, _PC_MIN_HOLE_SIZE) <= 0) 1273 return (ARCHIVE_OK); 1274 initial_off = lseek(*fd, 0, SEEK_CUR); 1275 if (initial_off != 0) 1276 lseek(*fd, 0, SEEK_SET); 1277 } else { 1278 const char *path; 1279 1280 path = archive_entry_sourcepath(entry); 1281 if (path == NULL) 1282 path = archive_entry_pathname(entry); 1283 1284 if (pathconf(path, _PC_MIN_HOLE_SIZE) <= 0) 1285 return (ARCHIVE_OK); 1286 *fd = open(path, O_RDONLY | O_NONBLOCK | O_CLOEXEC); 1287 if (*fd < 0) { 1288 archive_set_error(&a->archive, errno, 1289 "Can't open `%s'", path); 1290 return (ARCHIVE_FAILED); 1291 } 1292 __archive_ensure_cloexec_flag(*fd); 1293 initial_off = 0; 1294 } 1295 1296 off_s = 0; 1297 size = archive_entry_size(entry); 1298 while (off_s < size) { 1299 off_s = lseek(*fd, off_s, SEEK_DATA); 1300 if (off_s == (off_t)-1) { 1301 if (errno == ENXIO) { 1302 /* no more hole */ 1303 if (archive_entry_sparse_count(entry) == 0) { 1304 /* Potentially a fully-sparse file. */ 1305 check_fully_sparse = 1; 1306 } 1307 break; 1308 } 1309 archive_set_error(&a->archive, errno, 1310 "lseek(SEEK_HOLE) failed"); 1311 exit_sts = ARCHIVE_FAILED; 1312 goto exit_setup_sparse; 1313 } 1314 off_e = lseek(*fd, off_s, SEEK_HOLE); 1315 if (off_e == (off_t)-1) { 1316 if (errno == ENXIO) { 1317 off_e = lseek(*fd, 0, SEEK_END); 1318 if (off_e != (off_t)-1) 1319 break;/* no more data */ 1320 } 1321 archive_set_error(&a->archive, errno, 1322 "lseek(SEEK_DATA) failed"); 1323 exit_sts = ARCHIVE_FAILED; 1324 goto exit_setup_sparse; 1325 } 1326 if (off_s == 0 && off_e == size) 1327 break;/* This is not spase. */ 1328 archive_entry_sparse_add_entry(entry, off_s, 1329 off_e - off_s); 1330 off_s = off_e; 1331 } 1332 1333 if (check_fully_sparse) { 1334 if (lseek(*fd, 0, SEEK_HOLE) == 0 && 1335 lseek(*fd, 0, SEEK_END) == size) { 1336 /* Fully sparse file; insert a zero-length "data" entry */ 1337 archive_entry_sparse_add_entry(entry, 0, 0); 1338 } 1339 } 1340exit_setup_sparse: 1341 lseek(*fd, initial_off, SEEK_SET); 1342 return (exit_sts); 1343} 1344 1345#else 1346 1347/* 1348 * Generic (stub) sparse support. 1349 */ 1350static int 1351setup_sparse(struct archive_read_disk *a, 1352 struct archive_entry *entry, int *fd) 1353{ 1354 (void)a; /* UNUSED */ 1355 (void)entry; /* UNUSED */ 1356 (void)fd; /* UNUSED */ 1357 return (ARCHIVE_OK); 1358} 1359 1360#endif 1361 1362#endif /* !defined(_WIN32) || defined(__CYGWIN__) */ 1363 1364