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