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 * in this position and unchanged. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 /* This is the tree-walking code for POSIX systems. */ 29 #if !defined(_WIN32) || defined(__CYGWIN__) 30 31 #include "archive_platform.h" 32 __FBSDID("$FreeBSD$"); 33 34 #ifdef HAVE_SYS_PARAM_H 35 #include <sys/param.h> 36 #endif 37 #ifdef HAVE_SYS_STAT_H 38 #include <sys/stat.h> 39 #endif 40 #ifdef HAVE_SYS_STATFS_H 41 #include <sys/statfs.h> 42 #endif 43 #ifdef HAVE_SYS_STATVFS_H 44 #include <sys/statvfs.h> 45 #endif 46 #ifdef HAVE_SYS_TIME_H 47 #include <sys/time.h> 48 #endif 49 #ifdef HAVE_LINUX_MAGIC_H 50 #include <linux/magic.h> 51 #endif 52 #ifdef HAVE_LINUX_FS_H 53 #include <linux/fs.h> 54 #elif HAVE_SYS_MOUNT_H 55 #include <sys/mount.h> 56 #endif 57 /* 58 * Some Linux distributions have both linux/ext2_fs.h and ext2fs/ext2_fs.h. 59 * As the include guards don't agree, the order of include is important. 60 */ 61 #ifdef HAVE_LINUX_EXT2_FS_H 62 #include <linux/ext2_fs.h> /* for Linux file flags */ 63 #endif 64 #if defined(HAVE_EXT2FS_EXT2_FS_H) && !defined(__CYGWIN__) 65 #include <ext2fs/ext2_fs.h> /* Linux file flags, broken on Cygwin */ 66 #endif 67 #ifdef HAVE_DIRECT_H 68 #include <direct.h> 69 #endif 70 #ifdef HAVE_DIRENT_H 71 #include <dirent.h> 72 #endif 73 #ifdef HAVE_ERRNO_H 74 #include <errno.h> 75 #endif 76 #ifdef HAVE_FCNTL_H 77 #include <fcntl.h> 78 #endif 79 #ifdef HAVE_LIMITS_H 80 #include <limits.h> 81 #endif 82 #ifdef HAVE_STDLIB_H 83 #include <stdlib.h> 84 #endif 85 #ifdef HAVE_STRING_H 86 #include <string.h> 87 #endif 88 #ifdef HAVE_UNISTD_H 89 #include <unistd.h> 90 #endif 91 #ifdef HAVE_SYS_IOCTL_H 92 #include <sys/ioctl.h> 93 #endif 94 95 #include "archive.h" 96 #include "archive_string.h" 97 #include "archive_entry.h" 98 #include "archive_private.h" 99 #include "archive_read_disk_private.h" 100 101 #ifndef HAVE_FCHDIR 102 #error fchdir function required. 103 #endif 104 #ifndef O_BINARY 105 #define O_BINARY 0 106 #endif 107 #ifndef O_CLOEXEC 108 #define O_CLOEXEC 0 109 #endif 110 111 #if defined(__hpux) && !defined(HAVE_DIRFD) 112 #define dirfd(x) ((x)->__dd_fd) 113 #define HAVE_DIRFD 114 #endif 115 116 /*- 117 * This is a new directory-walking system that addresses a number 118 * of problems I've had with fts(3). In particular, it has no 119 * pathname-length limits (other than the size of 'int'), handles 120 * deep logical traversals, uses considerably less memory, and has 121 * an opaque interface (easier to modify in the future). 122 * 123 * Internally, it keeps a single list of "tree_entry" items that 124 * represent filesystem objects that require further attention. 125 * Non-directories are not kept in memory: they are pulled from 126 * readdir(), returned to the client, then freed as soon as possible. 127 * Any directory entry to be traversed gets pushed onto the stack. 128 * 129 * There is surprisingly little information that needs to be kept for 130 * each item on the stack. Just the name, depth (represented here as the 131 * string length of the parent directory's pathname), and some markers 132 * indicating how to get back to the parent (via chdir("..") for a 133 * regular dir or via fchdir(2) for a symlink). 134 */ 135 /* 136 * TODO: 137 * 1) Loop checking. 138 * 3) Arbitrary logical traversals by closing/reopening intermediate fds. 139 */ 140 141 struct restore_time { 142 const char *name; 143 time_t mtime; 144 long mtime_nsec; 145 time_t atime; 146 long atime_nsec; 147 mode_t filetype; 148 int noatime; 149 }; 150 151 struct tree_entry { 152 int depth; 153 struct tree_entry *next; 154 struct tree_entry *parent; 155 struct archive_string name; 156 size_t dirname_length; 157 int64_t dev; 158 int64_t ino; 159 int flags; 160 int filesystem_id; 161 /* How to return back to the parent of a symlink. */ 162 int symlink_parent_fd; 163 /* How to restore time of a directory. */ 164 struct restore_time restore_time; 165 }; 166 167 struct filesystem { 168 int64_t dev; 169 int synthetic; 170 int remote; 171 int noatime; 172 #if defined(USE_READDIR_R) 173 size_t name_max; 174 #endif 175 long incr_xfer_size; 176 long max_xfer_size; 177 long min_xfer_size; 178 long xfer_align; 179 180 /* 181 * Buffer used for reading file contents. 182 */ 183 /* Exactly allocated memory pointer. */ 184 unsigned char *allocation_ptr; 185 /* Pointer adjusted to the filesystem alignment . */ 186 unsigned char *buff; 187 size_t buff_size; 188 }; 189 190 /* Definitions for tree_entry.flags bitmap. */ 191 #define isDir 1 /* This entry is a regular directory. */ 192 #define isDirLink 2 /* This entry is a symbolic link to a directory. */ 193 #define needsFirstVisit 4 /* This is an initial entry. */ 194 #define needsDescent 8 /* This entry needs to be previsited. */ 195 #define needsOpen 16 /* This is a directory that needs to be opened. */ 196 #define needsAscent 32 /* This entry needs to be postvisited. */ 197 198 /* 199 * Local data for this package. 200 */ 201 struct tree { 202 struct tree_entry *stack; 203 struct tree_entry *current; 204 DIR *d; 205 #define INVALID_DIR_HANDLE NULL 206 struct dirent *de; 207 #if defined(USE_READDIR_R) 208 struct dirent *dirent; 209 size_t dirent_allocated; 210 #endif 211 int flags; 212 int visit_type; 213 /* Error code from last failed operation. */ 214 int tree_errno; 215 216 /* Dynamically-sized buffer for holding path */ 217 struct archive_string path; 218 219 /* Last path element */ 220 const char *basename; 221 /* Leading dir length */ 222 size_t dirname_length; 223 224 int depth; 225 int openCount; 226 int maxOpenCount; 227 int initial_dir_fd; 228 int working_dir_fd; 229 230 struct stat lst; 231 struct stat st; 232 int descend; 233 int nlink; 234 /* How to restore time of a file. */ 235 struct restore_time restore_time; 236 237 struct entry_sparse { 238 int64_t length; 239 int64_t offset; 240 } *sparse_list, *current_sparse; 241 int sparse_count; 242 int sparse_list_size; 243 244 char initial_symlink_mode; 245 char symlink_mode; 246 struct filesystem *current_filesystem; 247 struct filesystem *filesystem_table; 248 int initial_filesystem_id; 249 int current_filesystem_id; 250 int max_filesystem_id; 251 int allocated_filesystem; 252 253 int entry_fd; 254 int entry_eof; 255 int64_t entry_remaining_bytes; 256 int64_t entry_total; 257 unsigned char *entry_buff; 258 size_t entry_buff_size; 259 }; 260 261 /* Definitions for tree.flags bitmap. */ 262 #define hasStat 16 /* The st entry is valid. */ 263 #define hasLstat 32 /* The lst entry is valid. */ 264 #define onWorkingDir 64 /* We are on the working dir where we are 265 * reading directory entry at this time. */ 266 #define needsRestoreTimes 128 267 #define onInitialDir 256 /* We are on the initial dir. */ 268 269 static int 270 tree_dir_next_posix(struct tree *t); 271 272 #ifdef HAVE_DIRENT_D_NAMLEN 273 /* BSD extension; avoids need for a strlen() call. */ 274 #define D_NAMELEN(dp) (dp)->d_namlen 275 #else 276 #define D_NAMELEN(dp) (strlen((dp)->d_name)) 277 #endif 278 279 /* Initiate/terminate a tree traversal. */ 280 static struct tree *tree_open(const char *, int, int); 281 static struct tree *tree_reopen(struct tree *, const char *, int); 282 static void tree_close(struct tree *); 283 static void tree_free(struct tree *); 284 static void tree_push(struct tree *, const char *, int, int64_t, int64_t, 285 struct restore_time *); 286 static int tree_enter_initial_dir(struct tree *); 287 static int tree_enter_working_dir(struct tree *); 288 static int tree_current_dir_fd(struct tree *); 289 290 /* 291 * tree_next() returns Zero if there is no next entry, non-zero if 292 * there is. Note that directories are visited three times. 293 * Directories are always visited first as part of enumerating their 294 * parent; that is a "regular" visit. If tree_descend() is invoked at 295 * that time, the directory is added to a work list and will 296 * subsequently be visited two more times: once just after descending 297 * into the directory ("postdescent") and again just after ascending 298 * back to the parent ("postascent"). 299 * 300 * TREE_ERROR_DIR is returned if the descent failed (because the 301 * directory couldn't be opened, for instance). This is returned 302 * instead of TREE_POSTDESCENT/TREE_POSTASCENT. TREE_ERROR_DIR is not a 303 * fatal error, but it does imply that the relevant subtree won't be 304 * visited. TREE_ERROR_FATAL is returned for an error that left the 305 * traversal completely hosed. Right now, this is only returned for 306 * chdir() failures during ascent. 307 */ 308 #define TREE_REGULAR 1 309 #define TREE_POSTDESCENT 2 310 #define TREE_POSTASCENT 3 311 #define TREE_ERROR_DIR -1 312 #define TREE_ERROR_FATAL -2 313 314 static int tree_next(struct tree *); 315 316 /* 317 * Return information about the current entry. 318 */ 319 320 /* 321 * The current full pathname, length of the full pathname, and a name 322 * that can be used to access the file. Because tree does use chdir 323 * extensively, the access path is almost never the same as the full 324 * current path. 325 * 326 * TODO: On platforms that support it, use openat()-style operations 327 * to eliminate the chdir() operations entirely while still supporting 328 * arbitrarily deep traversals. This makes access_path troublesome to 329 * support, of course, which means we'll need a rich enough interface 330 * that clients can function without it. (In particular, we'll need 331 * tree_current_open() that returns an open file descriptor.) 332 * 333 */ 334 static const char *tree_current_path(struct tree *); 335 static const char *tree_current_access_path(struct tree *); 336 337 /* 338 * Request the lstat() or stat() data for the current path. Since the 339 * tree package needs to do some of this anyway, and caches the 340 * results, you should take advantage of it here if you need it rather 341 * than make a redundant stat() or lstat() call of your own. 342 */ 343 static const struct stat *tree_current_stat(struct tree *); 344 static const struct stat *tree_current_lstat(struct tree *); 345 static int tree_current_is_symblic_link_target(struct tree *); 346 347 /* The following functions use tricks to avoid a certain number of 348 * stat()/lstat() calls. */ 349 /* "is_physical_dir" is equivalent to S_ISDIR(tree_current_lstat()->st_mode) */ 350 static int tree_current_is_physical_dir(struct tree *); 351 /* "is_dir" is equivalent to S_ISDIR(tree_current_stat()->st_mode) */ 352 static int tree_current_is_dir(struct tree *); 353 static int update_current_filesystem(struct archive_read_disk *a, 354 int64_t dev); 355 static int setup_current_filesystem(struct archive_read_disk *); 356 static int tree_target_is_same_as_parent(struct tree *, const struct stat *); 357 358 static int _archive_read_disk_open(struct archive *, const char *); 359 static int _archive_read_free(struct archive *); 360 static int _archive_read_close(struct archive *); 361 static int _archive_read_data_block(struct archive *, 362 const void **, size_t *, int64_t *); 363 static int _archive_read_next_header(struct archive *, 364 struct archive_entry **); 365 static int _archive_read_next_header2(struct archive *, 366 struct archive_entry *); 367 static const char *trivial_lookup_gname(void *, int64_t gid); 368 static const char *trivial_lookup_uname(void *, int64_t uid); 369 static int setup_sparse(struct archive_read_disk *, struct archive_entry *); 370 static int close_and_restore_time(int fd, struct tree *, 371 struct restore_time *); 372 static int open_on_current_dir(struct tree *, const char *, int); 373 static int tree_dup(int); 374 375 376 static const struct archive_vtable 377 archive_read_disk_vtable = { 378 .archive_free = _archive_read_free, 379 .archive_close = _archive_read_close, 380 .archive_read_data_block = _archive_read_data_block, 381 .archive_read_next_header = _archive_read_next_header, 382 .archive_read_next_header2 = _archive_read_next_header2, 383 }; 384 385 const char * 386 archive_read_disk_gname(struct archive *_a, la_int64_t gid) 387 { 388 struct archive_read_disk *a = (struct archive_read_disk *)_a; 389 if (ARCHIVE_OK != __archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, 390 ARCHIVE_STATE_ANY, "archive_read_disk_gname")) 391 return (NULL); 392 if (a->lookup_gname == NULL) 393 return (NULL); 394 return ((*a->lookup_gname)(a->lookup_gname_data, gid)); 395 } 396 397 const char * 398 archive_read_disk_uname(struct archive *_a, la_int64_t uid) 399 { 400 struct archive_read_disk *a = (struct archive_read_disk *)_a; 401 if (ARCHIVE_OK != __archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, 402 ARCHIVE_STATE_ANY, "archive_read_disk_uname")) 403 return (NULL); 404 if (a->lookup_uname == NULL) 405 return (NULL); 406 return ((*a->lookup_uname)(a->lookup_uname_data, uid)); 407 } 408 409 int 410 archive_read_disk_set_gname_lookup(struct archive *_a, 411 void *private_data, 412 const char * (*lookup_gname)(void *private, la_int64_t gid), 413 void (*cleanup_gname)(void *private)) 414 { 415 struct archive_read_disk *a = (struct archive_read_disk *)_a; 416 archive_check_magic(&a->archive, ARCHIVE_READ_DISK_MAGIC, 417 ARCHIVE_STATE_ANY, "archive_read_disk_set_gname_lookup"); 418 419 if (a->cleanup_gname != NULL && a->lookup_gname_data != NULL) 420 (a->cleanup_gname)(a->lookup_gname_data); 421 422 a->lookup_gname = lookup_gname; 423 a->cleanup_gname = cleanup_gname; 424 a->lookup_gname_data = private_data; 425 return (ARCHIVE_OK); 426 } 427 428 int 429 archive_read_disk_set_uname_lookup(struct archive *_a, 430 void *private_data, 431 const char * (*lookup_uname)(void *private, la_int64_t uid), 432 void (*cleanup_uname)(void *private)) 433 { 434 struct archive_read_disk *a = (struct archive_read_disk *)_a; 435 archive_check_magic(&a->archive, ARCHIVE_READ_DISK_MAGIC, 436 ARCHIVE_STATE_ANY, "archive_read_disk_set_uname_lookup"); 437 438 if (a->cleanup_uname != NULL && a->lookup_uname_data != NULL) 439 (a->cleanup_uname)(a->lookup_uname_data); 440 441 a->lookup_uname = lookup_uname; 442 a->cleanup_uname = cleanup_uname; 443 a->lookup_uname_data = private_data; 444 return (ARCHIVE_OK); 445 } 446 447 /* 448 * Create a new archive_read_disk object and initialize it with global state. 449 */ 450 struct archive * 451 archive_read_disk_new(void) 452 { 453 struct archive_read_disk *a; 454 455 a = (struct archive_read_disk *)calloc(1, sizeof(*a)); 456 if (a == NULL) 457 return (NULL); 458 a->archive.magic = ARCHIVE_READ_DISK_MAGIC; 459 a->archive.state = ARCHIVE_STATE_NEW; 460 a->archive.vtable = &archive_read_disk_vtable; 461 a->entry = archive_entry_new2(&a->archive); 462 a->lookup_uname = trivial_lookup_uname; 463 a->lookup_gname = trivial_lookup_gname; 464 a->flags = ARCHIVE_READDISK_MAC_COPYFILE; 465 a->open_on_current_dir = open_on_current_dir; 466 a->tree_current_dir_fd = tree_current_dir_fd; 467 a->tree_enter_working_dir = tree_enter_working_dir; 468 return (&a->archive); 469 } 470 471 static int 472 _archive_read_free(struct archive *_a) 473 { 474 struct archive_read_disk *a = (struct archive_read_disk *)_a; 475 int r; 476 477 if (_a == NULL) 478 return (ARCHIVE_OK); 479 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, 480 ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_free"); 481 482 if (a->archive.state != ARCHIVE_STATE_CLOSED) 483 r = _archive_read_close(&a->archive); 484 else 485 r = ARCHIVE_OK; 486 487 tree_free(a->tree); 488 if (a->cleanup_gname != NULL && a->lookup_gname_data != NULL) 489 (a->cleanup_gname)(a->lookup_gname_data); 490 if (a->cleanup_uname != NULL && a->lookup_uname_data != NULL) 491 (a->cleanup_uname)(a->lookup_uname_data); 492 archive_string_free(&a->archive.error_string); 493 archive_entry_free(a->entry); 494 a->archive.magic = 0; 495 __archive_clean(&a->archive); 496 free(a); 497 return (r); 498 } 499 500 static int 501 _archive_read_close(struct archive *_a) 502 { 503 struct archive_read_disk *a = (struct archive_read_disk *)_a; 504 505 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, 506 ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_close"); 507 508 if (a->archive.state != ARCHIVE_STATE_FATAL) 509 a->archive.state = ARCHIVE_STATE_CLOSED; 510 511 tree_close(a->tree); 512 513 return (ARCHIVE_OK); 514 } 515 516 static void 517 setup_symlink_mode(struct archive_read_disk *a, char symlink_mode, 518 int follow_symlinks) 519 { 520 a->symlink_mode = symlink_mode; 521 a->follow_symlinks = follow_symlinks; 522 if (a->tree != NULL) { 523 a->tree->initial_symlink_mode = a->symlink_mode; 524 a->tree->symlink_mode = a->symlink_mode; 525 } 526 } 527 528 int 529 archive_read_disk_set_symlink_logical(struct archive *_a) 530 { 531 struct archive_read_disk *a = (struct archive_read_disk *)_a; 532 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, 533 ARCHIVE_STATE_ANY, "archive_read_disk_set_symlink_logical"); 534 setup_symlink_mode(a, 'L', 1); 535 return (ARCHIVE_OK); 536 } 537 538 int 539 archive_read_disk_set_symlink_physical(struct archive *_a) 540 { 541 struct archive_read_disk *a = (struct archive_read_disk *)_a; 542 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, 543 ARCHIVE_STATE_ANY, "archive_read_disk_set_symlink_physical"); 544 setup_symlink_mode(a, 'P', 0); 545 return (ARCHIVE_OK); 546 } 547 548 int 549 archive_read_disk_set_symlink_hybrid(struct archive *_a) 550 { 551 struct archive_read_disk *a = (struct archive_read_disk *)_a; 552 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, 553 ARCHIVE_STATE_ANY, "archive_read_disk_set_symlink_hybrid"); 554 setup_symlink_mode(a, 'H', 1);/* Follow symlinks initially. */ 555 return (ARCHIVE_OK); 556 } 557 558 int 559 archive_read_disk_set_atime_restored(struct archive *_a) 560 { 561 struct archive_read_disk *a = (struct archive_read_disk *)_a; 562 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, 563 ARCHIVE_STATE_ANY, "archive_read_disk_restore_atime"); 564 #ifdef HAVE_UTIMES 565 a->flags |= ARCHIVE_READDISK_RESTORE_ATIME; 566 if (a->tree != NULL) 567 a->tree->flags |= needsRestoreTimes; 568 return (ARCHIVE_OK); 569 #else 570 /* Display warning and unset flag */ 571 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 572 "Cannot restore access time on this system"); 573 a->flags &= ~ARCHIVE_READDISK_RESTORE_ATIME; 574 return (ARCHIVE_WARN); 575 #endif 576 } 577 578 int 579 archive_read_disk_set_behavior(struct archive *_a, int flags) 580 { 581 struct archive_read_disk *a = (struct archive_read_disk *)_a; 582 int r = ARCHIVE_OK; 583 584 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, 585 ARCHIVE_STATE_ANY, "archive_read_disk_honor_nodump"); 586 587 a->flags = flags; 588 589 if (flags & ARCHIVE_READDISK_RESTORE_ATIME) 590 r = archive_read_disk_set_atime_restored(_a); 591 else { 592 if (a->tree != NULL) 593 a->tree->flags &= ~needsRestoreTimes; 594 } 595 return (r); 596 } 597 598 /* 599 * Trivial implementations of gname/uname lookup functions. 600 * These are normally overridden by the client, but these stub 601 * versions ensure that we always have something that works. 602 */ 603 static const char * 604 trivial_lookup_gname(void *private_data, int64_t gid) 605 { 606 (void)private_data; /* UNUSED */ 607 (void)gid; /* UNUSED */ 608 return (NULL); 609 } 610 611 static const char * 612 trivial_lookup_uname(void *private_data, int64_t uid) 613 { 614 (void)private_data; /* UNUSED */ 615 (void)uid; /* UNUSED */ 616 return (NULL); 617 } 618 619 /* 620 * Allocate memory for the reading buffer adjusted to the filesystem 621 * alignment. 622 */ 623 static int 624 setup_suitable_read_buffer(struct archive_read_disk *a) 625 { 626 struct tree *t = a->tree; 627 struct filesystem *cf = t->current_filesystem; 628 size_t asize; 629 size_t s; 630 631 if (cf->allocation_ptr == NULL) { 632 /* If we couldn't get a filesystem alignment, 633 * we use 4096 as default value but we won't use 634 * O_DIRECT to open() and openat() operations. */ 635 long xfer_align = (cf->xfer_align == -1)?4096:cf->xfer_align; 636 637 if (cf->max_xfer_size != -1) 638 asize = cf->max_xfer_size + xfer_align; 639 else { 640 long incr = cf->incr_xfer_size; 641 /* Some platform does not set a proper value to 642 * incr_xfer_size.*/ 643 if (incr < 0) 644 incr = cf->min_xfer_size; 645 if (cf->min_xfer_size < 0) { 646 incr = xfer_align; 647 asize = xfer_align; 648 } else 649 asize = cf->min_xfer_size; 650 651 /* Increase a buffer size up to 64K bytes in 652 * a proper increment size. */ 653 while (asize < 1024*64) 654 asize += incr; 655 /* Take a margin to adjust to the filesystem 656 * alignment. */ 657 asize += xfer_align; 658 } 659 cf->allocation_ptr = malloc(asize); 660 if (cf->allocation_ptr == NULL) { 661 archive_set_error(&a->archive, ENOMEM, 662 "Couldn't allocate memory"); 663 a->archive.state = ARCHIVE_STATE_FATAL; 664 return (ARCHIVE_FATAL); 665 } 666 667 /* 668 * Calculate proper address for the filesystem. 669 */ 670 s = (uintptr_t)cf->allocation_ptr; 671 s %= xfer_align; 672 if (s > 0) 673 s = xfer_align - s; 674 675 /* 676 * Set a read buffer pointer in the proper alignment of 677 * the current filesystem. 678 */ 679 cf->buff = cf->allocation_ptr + s; 680 cf->buff_size = asize - xfer_align; 681 } 682 return (ARCHIVE_OK); 683 } 684 685 static int 686 _archive_read_data_block(struct archive *_a, const void **buff, 687 size_t *size, int64_t *offset) 688 { 689 struct archive_read_disk *a = (struct archive_read_disk *)_a; 690 struct tree *t = a->tree; 691 int r; 692 ssize_t bytes; 693 int64_t sparse_bytes; 694 size_t buffbytes; 695 int empty_sparse_region = 0; 696 697 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA, 698 "archive_read_data_block"); 699 700 if (t->entry_eof || t->entry_remaining_bytes <= 0) { 701 r = ARCHIVE_EOF; 702 goto abort_read_data; 703 } 704 705 /* 706 * Open the current file. 707 */ 708 if (t->entry_fd < 0) { 709 int flags = O_RDONLY | O_BINARY | O_CLOEXEC; 710 711 /* 712 * Eliminate or reduce cache effects if we can. 713 * 714 * Carefully consider this to be enabled. 715 */ 716 #if defined(O_DIRECT) && 0/* Disabled for now */ 717 if (t->current_filesystem->xfer_align != -1 && 718 t->nlink == 1) 719 flags |= O_DIRECT; 720 #endif 721 #if defined(O_NOATIME) 722 /* 723 * Linux has O_NOATIME flag; use it if we need. 724 */ 725 if ((t->flags & needsRestoreTimes) != 0 && 726 t->restore_time.noatime == 0) 727 flags |= O_NOATIME; 728 #endif 729 t->entry_fd = open_on_current_dir(t, 730 tree_current_access_path(t), flags); 731 __archive_ensure_cloexec_flag(t->entry_fd); 732 #if defined(O_NOATIME) 733 /* 734 * When we did open the file with O_NOATIME flag, 735 * if successful, set 1 to t->restore_time.noatime 736 * not to restore an atime of the file later. 737 * if failed by EPERM, retry it without O_NOATIME flag. 738 */ 739 if (flags & O_NOATIME) { 740 if (t->entry_fd >= 0) 741 t->restore_time.noatime = 1; 742 else if (errno == EPERM) 743 flags &= ~O_NOATIME; 744 } 745 #endif 746 if (t->entry_fd < 0) { 747 archive_set_error(&a->archive, errno, 748 "Couldn't open %s", tree_current_path(t)); 749 r = ARCHIVE_FAILED; 750 tree_enter_initial_dir(t); 751 goto abort_read_data; 752 } 753 tree_enter_initial_dir(t); 754 } 755 756 /* 757 * Allocate read buffer if not allocated. 758 */ 759 if (t->current_filesystem->allocation_ptr == NULL) { 760 r = setup_suitable_read_buffer(a); 761 if (r != ARCHIVE_OK) { 762 a->archive.state = ARCHIVE_STATE_FATAL; 763 goto abort_read_data; 764 } 765 } 766 t->entry_buff = t->current_filesystem->buff; 767 t->entry_buff_size = t->current_filesystem->buff_size; 768 769 buffbytes = t->entry_buff_size; 770 if ((int64_t)buffbytes > t->current_sparse->length) 771 buffbytes = t->current_sparse->length; 772 773 if (t->current_sparse->length == 0) 774 empty_sparse_region = 1; 775 776 /* 777 * Skip hole. 778 * TODO: Should we consider t->current_filesystem->xfer_align? 779 */ 780 if (t->current_sparse->offset > t->entry_total) { 781 if (lseek(t->entry_fd, 782 (off_t)t->current_sparse->offset, SEEK_SET) < 0) { 783 archive_set_error(&a->archive, errno, "Seek error"); 784 r = ARCHIVE_FATAL; 785 a->archive.state = ARCHIVE_STATE_FATAL; 786 goto abort_read_data; 787 } 788 sparse_bytes = t->current_sparse->offset - t->entry_total; 789 t->entry_remaining_bytes -= sparse_bytes; 790 t->entry_total += sparse_bytes; 791 } 792 793 /* 794 * Read file contents. 795 */ 796 if (buffbytes > 0) { 797 bytes = read(t->entry_fd, t->entry_buff, buffbytes); 798 if (bytes < 0) { 799 archive_set_error(&a->archive, errno, "Read error"); 800 r = ARCHIVE_FATAL; 801 a->archive.state = ARCHIVE_STATE_FATAL; 802 goto abort_read_data; 803 } 804 } else 805 bytes = 0; 806 /* 807 * Return an EOF unless we've read a leading empty sparse region, which 808 * is used to represent fully-sparse files. 809 */ 810 if (bytes == 0 && !empty_sparse_region) { 811 /* Get EOF */ 812 t->entry_eof = 1; 813 r = ARCHIVE_EOF; 814 goto abort_read_data; 815 } 816 *buff = t->entry_buff; 817 *size = bytes; 818 *offset = t->entry_total; 819 t->entry_total += bytes; 820 t->entry_remaining_bytes -= bytes; 821 if (t->entry_remaining_bytes == 0) { 822 /* Close the current file descriptor */ 823 close_and_restore_time(t->entry_fd, t, &t->restore_time); 824 t->entry_fd = -1; 825 t->entry_eof = 1; 826 } 827 t->current_sparse->offset += bytes; 828 t->current_sparse->length -= bytes; 829 if (t->current_sparse->length == 0 && !t->entry_eof) 830 t->current_sparse++; 831 return (ARCHIVE_OK); 832 833 abort_read_data: 834 *buff = NULL; 835 *size = 0; 836 *offset = t->entry_total; 837 if (t->entry_fd >= 0) { 838 /* Close the current file descriptor */ 839 close_and_restore_time(t->entry_fd, t, &t->restore_time); 840 t->entry_fd = -1; 841 } 842 return (r); 843 } 844 845 static int 846 next_entry(struct archive_read_disk *a, struct tree *t, 847 struct archive_entry *entry) 848 { 849 const struct stat *st; /* info to use for this entry */ 850 const struct stat *lst;/* lstat() information */ 851 const char *name; 852 int delayed, delayed_errno, descend, r; 853 struct archive_string delayed_str; 854 855 delayed = ARCHIVE_OK; 856 delayed_errno = 0; 857 archive_string_init(&delayed_str); 858 859 st = NULL; 860 lst = NULL; 861 t->descend = 0; 862 do { 863 switch (tree_next(t)) { 864 case TREE_ERROR_FATAL: 865 archive_set_error(&a->archive, t->tree_errno, 866 "%s: Unable to continue traversing directory tree", 867 tree_current_path(t)); 868 a->archive.state = ARCHIVE_STATE_FATAL; 869 tree_enter_initial_dir(t); 870 return (ARCHIVE_FATAL); 871 case TREE_ERROR_DIR: 872 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 873 "%s: Couldn't visit directory", 874 tree_current_path(t)); 875 tree_enter_initial_dir(t); 876 return (ARCHIVE_FAILED); 877 case 0: 878 tree_enter_initial_dir(t); 879 return (ARCHIVE_EOF); 880 case TREE_POSTDESCENT: 881 case TREE_POSTASCENT: 882 break; 883 case TREE_REGULAR: 884 lst = tree_current_lstat(t); 885 if (lst == NULL) { 886 if (errno == ENOENT && t->depth > 0) { 887 delayed = ARCHIVE_WARN; 888 delayed_errno = errno; 889 if (delayed_str.length == 0) { 890 archive_string_sprintf(&delayed_str, 891 "%s", tree_current_path(t)); 892 } else { 893 archive_string_sprintf(&delayed_str, 894 " %s", tree_current_path(t)); 895 } 896 } else { 897 archive_set_error(&a->archive, errno, 898 "%s: Cannot stat", 899 tree_current_path(t)); 900 tree_enter_initial_dir(t); 901 return (ARCHIVE_FAILED); 902 } 903 } 904 break; 905 } 906 } while (lst == NULL); 907 908 #ifdef __APPLE__ 909 if (a->flags & ARCHIVE_READDISK_MAC_COPYFILE) { 910 /* If we're using copyfile(), ignore "._XXX" files. */ 911 const char *bname = strrchr(tree_current_path(t), '/'); 912 if (bname == NULL) 913 bname = tree_current_path(t); 914 else 915 ++bname; 916 if (bname[0] == '.' && bname[1] == '_') 917 return (ARCHIVE_RETRY); 918 } 919 #endif 920 921 archive_entry_copy_pathname(entry, tree_current_path(t)); 922 /* 923 * Perform path matching. 924 */ 925 if (a->matching) { 926 r = archive_match_path_excluded(a->matching, entry); 927 if (r < 0) { 928 archive_set_error(&(a->archive), errno, 929 "Failed : %s", archive_error_string(a->matching)); 930 return (r); 931 } 932 if (r) { 933 if (a->excluded_cb_func) 934 a->excluded_cb_func(&(a->archive), 935 a->excluded_cb_data, entry); 936 return (ARCHIVE_RETRY); 937 } 938 } 939 940 /* 941 * Distinguish 'L'/'P'/'H' symlink following. 942 */ 943 switch(t->symlink_mode) { 944 case 'H': 945 /* 'H': After the first item, rest like 'P'. */ 946 t->symlink_mode = 'P'; 947 /* 'H': First item (from command line) like 'L'. */ 948 /* FALLTHROUGH */ 949 case 'L': 950 /* 'L': Do descend through a symlink to dir. */ 951 descend = tree_current_is_dir(t); 952 /* 'L': Follow symlinks to files. */ 953 a->symlink_mode = 'L'; 954 a->follow_symlinks = 1; 955 /* 'L': Archive symlinks as targets, if we can. */ 956 st = tree_current_stat(t); 957 if (st != NULL && !tree_target_is_same_as_parent(t, st)) 958 break; 959 /* If stat fails, we have a broken symlink; 960 * in that case, don't follow the link. */ 961 /* FALLTHROUGH */ 962 default: 963 /* 'P': Don't descend through a symlink to dir. */ 964 descend = tree_current_is_physical_dir(t); 965 /* 'P': Don't follow symlinks to files. */ 966 a->symlink_mode = 'P'; 967 a->follow_symlinks = 0; 968 /* 'P': Archive symlinks as symlinks. */ 969 st = lst; 970 break; 971 } 972 973 if (update_current_filesystem(a, st->st_dev) != ARCHIVE_OK) { 974 a->archive.state = ARCHIVE_STATE_FATAL; 975 tree_enter_initial_dir(t); 976 return (ARCHIVE_FATAL); 977 } 978 if (t->initial_filesystem_id == -1) 979 t->initial_filesystem_id = t->current_filesystem_id; 980 if (a->flags & ARCHIVE_READDISK_NO_TRAVERSE_MOUNTS) { 981 if (t->initial_filesystem_id != t->current_filesystem_id) 982 descend = 0; 983 } 984 t->descend = descend; 985 986 /* 987 * Honor nodump flag. 988 * If the file is marked with nodump flag, do not return this entry. 989 */ 990 if (a->flags & ARCHIVE_READDISK_HONOR_NODUMP) { 991 #if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP) 992 if (st->st_flags & UF_NODUMP) 993 return (ARCHIVE_RETRY); 994 #elif (defined(FS_IOC_GETFLAGS) && defined(FS_NODUMP_FL) && \ 995 defined(HAVE_WORKING_FS_IOC_GETFLAGS)) || \ 996 (defined(EXT2_IOC_GETFLAGS) && defined(EXT2_NODUMP_FL) && \ 997 defined(HAVE_WORKING_EXT2_IOC_GETFLAGS)) 998 if (S_ISREG(st->st_mode) || S_ISDIR(st->st_mode)) { 999 int stflags; 1000 1001 t->entry_fd = open_on_current_dir(t, 1002 tree_current_access_path(t), 1003 O_RDONLY | O_NONBLOCK | O_CLOEXEC); 1004 __archive_ensure_cloexec_flag(t->entry_fd); 1005 if (t->entry_fd >= 0) { 1006 r = ioctl(t->entry_fd, 1007 #ifdef FS_IOC_GETFLAGS 1008 FS_IOC_GETFLAGS, 1009 #else 1010 EXT2_IOC_GETFLAGS, 1011 #endif 1012 &stflags); 1013 #ifdef FS_NODUMP_FL 1014 if (r == 0 && (stflags & FS_NODUMP_FL) != 0) 1015 #else 1016 if (r == 0 && (stflags & EXT2_NODUMP_FL) != 0) 1017 #endif 1018 return (ARCHIVE_RETRY); 1019 } 1020 } 1021 #endif 1022 } 1023 1024 archive_entry_copy_stat(entry, st); 1025 1026 /* Save the times to be restored. This must be in before 1027 * calling archive_read_disk_descend() or any chance of it, 1028 * especially, invoking a callback. */ 1029 t->restore_time.mtime = archive_entry_mtime(entry); 1030 t->restore_time.mtime_nsec = archive_entry_mtime_nsec(entry); 1031 t->restore_time.atime = archive_entry_atime(entry); 1032 t->restore_time.atime_nsec = archive_entry_atime_nsec(entry); 1033 t->restore_time.filetype = archive_entry_filetype(entry); 1034 t->restore_time.noatime = t->current_filesystem->noatime; 1035 1036 /* 1037 * Perform time matching. 1038 */ 1039 if (a->matching) { 1040 r = archive_match_time_excluded(a->matching, entry); 1041 if (r < 0) { 1042 archive_set_error(&(a->archive), errno, 1043 "Failed : %s", archive_error_string(a->matching)); 1044 return (r); 1045 } 1046 if (r) { 1047 if (a->excluded_cb_func) 1048 a->excluded_cb_func(&(a->archive), 1049 a->excluded_cb_data, entry); 1050 return (ARCHIVE_RETRY); 1051 } 1052 } 1053 1054 /* Lookup uname/gname */ 1055 name = archive_read_disk_uname(&(a->archive), archive_entry_uid(entry)); 1056 if (name != NULL) 1057 archive_entry_copy_uname(entry, name); 1058 name = archive_read_disk_gname(&(a->archive), archive_entry_gid(entry)); 1059 if (name != NULL) 1060 archive_entry_copy_gname(entry, name); 1061 1062 /* 1063 * Perform owner matching. 1064 */ 1065 if (a->matching) { 1066 r = archive_match_owner_excluded(a->matching, entry); 1067 if (r < 0) { 1068 archive_set_error(&(a->archive), errno, 1069 "Failed : %s", archive_error_string(a->matching)); 1070 return (r); 1071 } 1072 if (r) { 1073 if (a->excluded_cb_func) 1074 a->excluded_cb_func(&(a->archive), 1075 a->excluded_cb_data, entry); 1076 return (ARCHIVE_RETRY); 1077 } 1078 } 1079 1080 /* 1081 * Invoke a meta data filter callback. 1082 */ 1083 if (a->metadata_filter_func) { 1084 if (!a->metadata_filter_func(&(a->archive), 1085 a->metadata_filter_data, entry)) 1086 return (ARCHIVE_RETRY); 1087 } 1088 1089 /* 1090 * Populate the archive_entry with metadata from the disk. 1091 */ 1092 archive_entry_copy_sourcepath(entry, tree_current_access_path(t)); 1093 r = archive_read_disk_entry_from_file(&(a->archive), entry, 1094 t->entry_fd, st); 1095 1096 if (r == ARCHIVE_OK) { 1097 r = delayed; 1098 if (r != ARCHIVE_OK) { 1099 archive_string_sprintf(&delayed_str, ": %s", 1100 "File removed before we read it"); 1101 archive_set_error(&(a->archive), delayed_errno, 1102 "%s", delayed_str.s); 1103 } 1104 } 1105 archive_string_free(&delayed_str); 1106 1107 return (r); 1108 } 1109 1110 static int 1111 _archive_read_next_header(struct archive *_a, struct archive_entry **entryp) 1112 { 1113 int ret; 1114 struct archive_read_disk *a = (struct archive_read_disk *)_a; 1115 *entryp = NULL; 1116 ret = _archive_read_next_header2(_a, a->entry); 1117 *entryp = a->entry; 1118 return ret; 1119 } 1120 1121 static int 1122 _archive_read_next_header2(struct archive *_a, struct archive_entry *entry) 1123 { 1124 struct archive_read_disk *a = (struct archive_read_disk *)_a; 1125 struct tree *t; 1126 int r; 1127 1128 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, 1129 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA, 1130 "archive_read_next_header2"); 1131 1132 t = a->tree; 1133 if (t->entry_fd >= 0) { 1134 close_and_restore_time(t->entry_fd, t, &t->restore_time); 1135 t->entry_fd = -1; 1136 } 1137 1138 archive_entry_clear(entry); 1139 1140 for (;;) { 1141 r = next_entry(a, t, entry); 1142 if (t->entry_fd >= 0) { 1143 close(t->entry_fd); 1144 t->entry_fd = -1; 1145 } 1146 1147 if (r == ARCHIVE_RETRY) { 1148 archive_entry_clear(entry); 1149 continue; 1150 } 1151 break; 1152 } 1153 1154 /* Return to the initial directory. */ 1155 tree_enter_initial_dir(t); 1156 1157 /* 1158 * EOF and FATAL are persistent at this layer. By 1159 * modifying the state, we guarantee that future calls to 1160 * read a header or read data will fail. 1161 */ 1162 switch (r) { 1163 case ARCHIVE_EOF: 1164 a->archive.state = ARCHIVE_STATE_EOF; 1165 break; 1166 case ARCHIVE_OK: 1167 case ARCHIVE_WARN: 1168 /* Overwrite the sourcepath based on the initial directory. */ 1169 archive_entry_copy_sourcepath(entry, tree_current_path(t)); 1170 t->entry_total = 0; 1171 if (archive_entry_filetype(entry) == AE_IFREG) { 1172 t->nlink = archive_entry_nlink(entry); 1173 t->entry_remaining_bytes = archive_entry_size(entry); 1174 t->entry_eof = (t->entry_remaining_bytes == 0)? 1: 0; 1175 if (!t->entry_eof && 1176 setup_sparse(a, entry) != ARCHIVE_OK) 1177 return (ARCHIVE_FATAL); 1178 } else { 1179 t->entry_remaining_bytes = 0; 1180 t->entry_eof = 1; 1181 } 1182 a->archive.state = ARCHIVE_STATE_DATA; 1183 break; 1184 case ARCHIVE_RETRY: 1185 break; 1186 case ARCHIVE_FATAL: 1187 a->archive.state = ARCHIVE_STATE_FATAL; 1188 break; 1189 } 1190 1191 __archive_reset_read_data(&a->archive); 1192 return (r); 1193 } 1194 1195 static int 1196 setup_sparse(struct archive_read_disk *a, struct archive_entry *entry) 1197 { 1198 struct tree *t = a->tree; 1199 int64_t length, offset; 1200 int i; 1201 1202 t->sparse_count = archive_entry_sparse_reset(entry); 1203 if (t->sparse_count+1 > t->sparse_list_size) { 1204 free(t->sparse_list); 1205 t->sparse_list_size = t->sparse_count + 1; 1206 t->sparse_list = malloc(sizeof(t->sparse_list[0]) * 1207 t->sparse_list_size); 1208 if (t->sparse_list == NULL) { 1209 t->sparse_list_size = 0; 1210 archive_set_error(&a->archive, ENOMEM, 1211 "Can't allocate data"); 1212 a->archive.state = ARCHIVE_STATE_FATAL; 1213 return (ARCHIVE_FATAL); 1214 } 1215 } 1216 for (i = 0; i < t->sparse_count; i++) { 1217 archive_entry_sparse_next(entry, &offset, &length); 1218 t->sparse_list[i].offset = offset; 1219 t->sparse_list[i].length = length; 1220 } 1221 if (i == 0) { 1222 t->sparse_list[i].offset = 0; 1223 t->sparse_list[i].length = archive_entry_size(entry); 1224 } else { 1225 t->sparse_list[i].offset = archive_entry_size(entry); 1226 t->sparse_list[i].length = 0; 1227 } 1228 t->current_sparse = t->sparse_list; 1229 1230 return (ARCHIVE_OK); 1231 } 1232 1233 int 1234 archive_read_disk_set_matching(struct archive *_a, struct archive *_ma, 1235 void (*_excluded_func)(struct archive *, void *, struct archive_entry *), 1236 void *_client_data) 1237 { 1238 struct archive_read_disk *a = (struct archive_read_disk *)_a; 1239 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, 1240 ARCHIVE_STATE_ANY, "archive_read_disk_set_matching"); 1241 a->matching = _ma; 1242 a->excluded_cb_func = _excluded_func; 1243 a->excluded_cb_data = _client_data; 1244 return (ARCHIVE_OK); 1245 } 1246 1247 int 1248 archive_read_disk_set_metadata_filter_callback(struct archive *_a, 1249 int (*_metadata_filter_func)(struct archive *, void *, 1250 struct archive_entry *), void *_client_data) 1251 { 1252 struct archive_read_disk *a = (struct archive_read_disk *)_a; 1253 1254 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_ANY, 1255 "archive_read_disk_set_metadata_filter_callback"); 1256 1257 a->metadata_filter_func = _metadata_filter_func; 1258 a->metadata_filter_data = _client_data; 1259 return (ARCHIVE_OK); 1260 } 1261 1262 int 1263 archive_read_disk_can_descend(struct archive *_a) 1264 { 1265 struct archive_read_disk *a = (struct archive_read_disk *)_a; 1266 struct tree *t = a->tree; 1267 1268 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, 1269 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA, 1270 "archive_read_disk_can_descend"); 1271 1272 return (t->visit_type == TREE_REGULAR && t->descend); 1273 } 1274 1275 /* 1276 * Called by the client to mark the directory just returned from 1277 * tree_next() as needing to be visited. 1278 */ 1279 int 1280 archive_read_disk_descend(struct archive *_a) 1281 { 1282 struct archive_read_disk *a = (struct archive_read_disk *)_a; 1283 struct tree *t = a->tree; 1284 1285 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, 1286 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA, 1287 "archive_read_disk_descend"); 1288 1289 if (!archive_read_disk_can_descend(_a)) 1290 return (ARCHIVE_OK); 1291 1292 /* 1293 * We must not treat the initial specified path as a physical dir, 1294 * because if we do then we will try and ascend out of it by opening 1295 * ".." which is (a) wrong and (b) causes spurious permissions errors 1296 * if ".." is not readable by us. Instead, treat it as if it were a 1297 * symlink. (This uses an extra fd, but it can only happen once at the 1298 * top level of a traverse.) But we can't necessarily assume t->st is 1299 * valid here (though t->lst is), which complicates the logic a 1300 * little. 1301 */ 1302 if (tree_current_is_physical_dir(t)) { 1303 tree_push(t, t->basename, t->current_filesystem_id, 1304 t->lst.st_dev, t->lst.st_ino, &t->restore_time); 1305 if (t->stack->parent->parent != NULL) 1306 t->stack->flags |= isDir; 1307 else 1308 t->stack->flags |= isDirLink; 1309 } else if (tree_current_is_dir(t)) { 1310 tree_push(t, t->basename, t->current_filesystem_id, 1311 t->st.st_dev, t->st.st_ino, &t->restore_time); 1312 t->stack->flags |= isDirLink; 1313 } 1314 t->descend = 0; 1315 return (ARCHIVE_OK); 1316 } 1317 1318 int 1319 archive_read_disk_open(struct archive *_a, const char *pathname) 1320 { 1321 struct archive_read_disk *a = (struct archive_read_disk *)_a; 1322 1323 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, 1324 ARCHIVE_STATE_NEW | ARCHIVE_STATE_CLOSED, 1325 "archive_read_disk_open"); 1326 archive_clear_error(&a->archive); 1327 1328 return (_archive_read_disk_open(_a, pathname)); 1329 } 1330 1331 int 1332 archive_read_disk_open_w(struct archive *_a, const wchar_t *pathname) 1333 { 1334 struct archive_read_disk *a = (struct archive_read_disk *)_a; 1335 struct archive_string path; 1336 int ret; 1337 1338 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, 1339 ARCHIVE_STATE_NEW | ARCHIVE_STATE_CLOSED, 1340 "archive_read_disk_open_w"); 1341 archive_clear_error(&a->archive); 1342 1343 /* Make a char string from a wchar_t string. */ 1344 archive_string_init(&path); 1345 if (archive_string_append_from_wcs(&path, pathname, 1346 wcslen(pathname)) != 0) { 1347 if (errno == ENOMEM) 1348 archive_set_error(&a->archive, ENOMEM, 1349 "Can't allocate memory"); 1350 else 1351 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 1352 "Can't convert a path to a char string"); 1353 a->archive.state = ARCHIVE_STATE_FATAL; 1354 ret = ARCHIVE_FATAL; 1355 } else 1356 ret = _archive_read_disk_open(_a, path.s); 1357 1358 archive_string_free(&path); 1359 return (ret); 1360 } 1361 1362 static int 1363 _archive_read_disk_open(struct archive *_a, const char *pathname) 1364 { 1365 struct archive_read_disk *a = (struct archive_read_disk *)_a; 1366 1367 if (a->tree != NULL) 1368 a->tree = tree_reopen(a->tree, pathname, 1369 a->flags & ARCHIVE_READDISK_RESTORE_ATIME); 1370 else 1371 a->tree = tree_open(pathname, a->symlink_mode, 1372 a->flags & ARCHIVE_READDISK_RESTORE_ATIME); 1373 if (a->tree == NULL) { 1374 archive_set_error(&a->archive, ENOMEM, 1375 "Can't allocate tar data"); 1376 a->archive.state = ARCHIVE_STATE_FATAL; 1377 return (ARCHIVE_FATAL); 1378 } 1379 a->archive.state = ARCHIVE_STATE_HEADER; 1380 1381 return (ARCHIVE_OK); 1382 } 1383 1384 /* 1385 * Return a current filesystem ID which is index of the filesystem entry 1386 * you've visited through archive_read_disk. 1387 */ 1388 int 1389 archive_read_disk_current_filesystem(struct archive *_a) 1390 { 1391 struct archive_read_disk *a = (struct archive_read_disk *)_a; 1392 1393 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA, 1394 "archive_read_disk_current_filesystem"); 1395 1396 return (a->tree->current_filesystem_id); 1397 } 1398 1399 static int 1400 update_current_filesystem(struct archive_read_disk *a, int64_t dev) 1401 { 1402 struct tree *t = a->tree; 1403 int i, fid; 1404 1405 if (t->current_filesystem != NULL && 1406 t->current_filesystem->dev == dev) 1407 return (ARCHIVE_OK); 1408 1409 for (i = 0; i < t->max_filesystem_id; i++) { 1410 if (t->filesystem_table[i].dev == dev) { 1411 /* There is the filesystem ID we've already generated. */ 1412 t->current_filesystem_id = i; 1413 t->current_filesystem = &(t->filesystem_table[i]); 1414 return (ARCHIVE_OK); 1415 } 1416 } 1417 1418 /* 1419 * This is the new filesystem which we have to generate a new ID for. 1420 */ 1421 fid = t->max_filesystem_id++; 1422 if (t->max_filesystem_id > t->allocated_filesystem) { 1423 size_t s; 1424 void *p; 1425 1426 s = t->max_filesystem_id * 2; 1427 p = realloc(t->filesystem_table, 1428 s * sizeof(*t->filesystem_table)); 1429 if (p == NULL) { 1430 archive_set_error(&a->archive, ENOMEM, 1431 "Can't allocate tar data"); 1432 return (ARCHIVE_FATAL); 1433 } 1434 t->filesystem_table = (struct filesystem *)p; 1435 t->allocated_filesystem = s; 1436 } 1437 t->current_filesystem_id = fid; 1438 t->current_filesystem = &(t->filesystem_table[fid]); 1439 t->current_filesystem->dev = dev; 1440 t->current_filesystem->allocation_ptr = NULL; 1441 t->current_filesystem->buff = NULL; 1442 1443 /* Setup the current filesystem properties which depend on 1444 * platform specific. */ 1445 return (setup_current_filesystem(a)); 1446 } 1447 1448 /* 1449 * Returns 1 if current filesystem is generated filesystem, 0 if it is not 1450 * or -1 if it is unknown. 1451 */ 1452 int 1453 archive_read_disk_current_filesystem_is_synthetic(struct archive *_a) 1454 { 1455 struct archive_read_disk *a = (struct archive_read_disk *)_a; 1456 1457 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA, 1458 "archive_read_disk_current_filesystem"); 1459 1460 return (a->tree->current_filesystem->synthetic); 1461 } 1462 1463 /* 1464 * Returns 1 if current filesystem is remote filesystem, 0 if it is not 1465 * or -1 if it is unknown. 1466 */ 1467 int 1468 archive_read_disk_current_filesystem_is_remote(struct archive *_a) 1469 { 1470 struct archive_read_disk *a = (struct archive_read_disk *)_a; 1471 1472 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA, 1473 "archive_read_disk_current_filesystem"); 1474 1475 return (a->tree->current_filesystem->remote); 1476 } 1477 1478 #if defined(_PC_REC_INCR_XFER_SIZE) && defined(_PC_REC_MAX_XFER_SIZE) &&\ 1479 defined(_PC_REC_MIN_XFER_SIZE) && defined(_PC_REC_XFER_ALIGN) 1480 static int 1481 get_xfer_size(struct tree *t, int fd, const char *path) 1482 { 1483 t->current_filesystem->xfer_align = -1; 1484 errno = 0; 1485 if (fd >= 0) { 1486 t->current_filesystem->incr_xfer_size = 1487 fpathconf(fd, _PC_REC_INCR_XFER_SIZE); 1488 t->current_filesystem->max_xfer_size = 1489 fpathconf(fd, _PC_REC_MAX_XFER_SIZE); 1490 t->current_filesystem->min_xfer_size = 1491 fpathconf(fd, _PC_REC_MIN_XFER_SIZE); 1492 t->current_filesystem->xfer_align = 1493 fpathconf(fd, _PC_REC_XFER_ALIGN); 1494 } else if (path != NULL) { 1495 t->current_filesystem->incr_xfer_size = 1496 pathconf(path, _PC_REC_INCR_XFER_SIZE); 1497 t->current_filesystem->max_xfer_size = 1498 pathconf(path, _PC_REC_MAX_XFER_SIZE); 1499 t->current_filesystem->min_xfer_size = 1500 pathconf(path, _PC_REC_MIN_XFER_SIZE); 1501 t->current_filesystem->xfer_align = 1502 pathconf(path, _PC_REC_XFER_ALIGN); 1503 } 1504 /* At least we need an alignment size. */ 1505 if (t->current_filesystem->xfer_align == -1) 1506 return ((errno == EINVAL)?1:-1); 1507 else 1508 return (0); 1509 } 1510 #else 1511 static int 1512 get_xfer_size(struct tree *t, int fd, const char *path) 1513 { 1514 (void)t; /* UNUSED */ 1515 (void)fd; /* UNUSED */ 1516 (void)path; /* UNUSED */ 1517 return (1);/* Not supported */ 1518 } 1519 #endif 1520 1521 #if defined(HAVE_STATVFS) 1522 static inline __LA_UNUSED void 1523 set_statvfs_transfer_size(struct filesystem *fs, const struct statvfs *sfs) 1524 { 1525 fs->xfer_align = sfs->f_frsize > 0 ? (long)sfs->f_frsize : -1; 1526 fs->max_xfer_size = -1; 1527 #if defined(HAVE_STRUCT_STATVFS_F_IOSIZE) 1528 fs->min_xfer_size = sfs->f_iosize > 0 ? (long)sfs->f_iosize : -1; 1529 fs->incr_xfer_size = sfs->f_iosize > 0 ? (long)sfs->f_iosize : -1; 1530 #else 1531 fs->min_xfer_size = sfs->f_bsize > 0 ? (long)sfs->f_bsize : -1; 1532 fs->incr_xfer_size = sfs->f_bsize > 0 ? (long)sfs->f_bsize : -1; 1533 #endif 1534 } 1535 #endif 1536 1537 #if defined(HAVE_STRUCT_STATFS) 1538 static inline __LA_UNUSED void 1539 set_statfs_transfer_size(struct filesystem *fs, const struct statfs *sfs) 1540 { 1541 fs->xfer_align = sfs->f_bsize > 0 ? (long)sfs->f_bsize : -1; 1542 fs->max_xfer_size = -1; 1543 #if defined(HAVE_STRUCT_STATFS_F_IOSIZE) 1544 fs->min_xfer_size = sfs->f_iosize > 0 ? (long)sfs->f_iosize : -1; 1545 fs->incr_xfer_size = sfs->f_iosize > 0 ? (long)sfs->f_iosize : -1; 1546 #else 1547 fs->min_xfer_size = sfs->f_bsize > 0 ? (long)sfs->f_bsize : -1; 1548 fs->incr_xfer_size = sfs->f_bsize > 0 ? (long)sfs->f_bsize : -1; 1549 #endif 1550 } 1551 #endif 1552 1553 #if defined(HAVE_STRUCT_STATFS) && defined(HAVE_STATFS) && \ 1554 defined(HAVE_FSTATFS) && defined(MNT_LOCAL) && !defined(ST_LOCAL) 1555 1556 /* 1557 * Gather current filesystem properties on FreeBSD, OpenBSD and Mac OS X. 1558 */ 1559 static int 1560 setup_current_filesystem(struct archive_read_disk *a) 1561 { 1562 struct tree *t = a->tree; 1563 struct statfs sfs; 1564 #if defined(HAVE_GETVFSBYNAME) && defined(VFCF_SYNTHETIC) 1565 /* TODO: configure should set GETVFSBYNAME_ARG_TYPE to make 1566 * this accurate; some platforms have both and we need the one that's 1567 * used by getvfsbyname() 1568 * 1569 * Then the following would become: 1570 * #if defined(GETVFSBYNAME_ARG_TYPE) 1571 * GETVFSBYNAME_ARG_TYPE vfc; 1572 * #endif 1573 */ 1574 # if defined(HAVE_STRUCT_XVFSCONF) 1575 struct xvfsconf vfc; 1576 # else 1577 struct vfsconf vfc; 1578 # endif 1579 #endif 1580 int r, xr = 0; 1581 #if !defined(HAVE_STRUCT_STATFS_F_NAMEMAX) 1582 long nm; 1583 #endif 1584 1585 t->current_filesystem->synthetic = -1; 1586 t->current_filesystem->remote = -1; 1587 if (tree_current_is_symblic_link_target(t)) { 1588 #if defined(HAVE_OPENAT) 1589 /* 1590 * Get file system statistics on any directory 1591 * where current is. 1592 */ 1593 int fd = openat(tree_current_dir_fd(t), 1594 tree_current_access_path(t), O_RDONLY | O_CLOEXEC); 1595 __archive_ensure_cloexec_flag(fd); 1596 if (fd < 0) { 1597 archive_set_error(&a->archive, errno, 1598 "openat failed"); 1599 return (ARCHIVE_FAILED); 1600 } 1601 r = fstatfs(fd, &sfs); 1602 if (r == 0) 1603 xr = get_xfer_size(t, fd, NULL); 1604 close(fd); 1605 #else 1606 if (tree_enter_working_dir(t) != 0) { 1607 archive_set_error(&a->archive, errno, "fchdir failed"); 1608 return (ARCHIVE_FAILED); 1609 } 1610 r = statfs(tree_current_access_path(t), &sfs); 1611 if (r == 0) 1612 xr = get_xfer_size(t, -1, tree_current_access_path(t)); 1613 #endif 1614 } else { 1615 r = fstatfs(tree_current_dir_fd(t), &sfs); 1616 if (r == 0) 1617 xr = get_xfer_size(t, tree_current_dir_fd(t), NULL); 1618 } 1619 if (r == -1 || xr == -1) { 1620 archive_set_error(&a->archive, errno, "statfs failed"); 1621 return (ARCHIVE_FAILED); 1622 } else if (xr == 1) { 1623 /* pathconf(_PC_REX_*) operations are not supported. */ 1624 set_statfs_transfer_size(t->current_filesystem, &sfs); 1625 } 1626 if (sfs.f_flags & MNT_LOCAL) 1627 t->current_filesystem->remote = 0; 1628 else 1629 t->current_filesystem->remote = 1; 1630 1631 #if defined(HAVE_GETVFSBYNAME) && defined(VFCF_SYNTHETIC) 1632 r = getvfsbyname(sfs.f_fstypename, &vfc); 1633 if (r == -1) { 1634 archive_set_error(&a->archive, errno, "getvfsbyname failed"); 1635 return (ARCHIVE_FAILED); 1636 } 1637 if (vfc.vfc_flags & VFCF_SYNTHETIC) 1638 t->current_filesystem->synthetic = 1; 1639 else 1640 t->current_filesystem->synthetic = 0; 1641 #endif 1642 1643 #if defined(MNT_NOATIME) 1644 if (sfs.f_flags & MNT_NOATIME) 1645 t->current_filesystem->noatime = 1; 1646 else 1647 #endif 1648 t->current_filesystem->noatime = 0; 1649 1650 #if defined(USE_READDIR_R) 1651 /* Set maximum filename length. */ 1652 #if defined(HAVE_STRUCT_STATFS_F_NAMEMAX) 1653 t->current_filesystem->name_max = sfs.f_namemax; 1654 #else 1655 # if defined(_PC_NAME_MAX) 1656 /* Mac OS X does not have f_namemax in struct statfs. */ 1657 if (tree_current_is_symblic_link_target(t)) { 1658 if (tree_enter_working_dir(t) != 0) { 1659 archive_set_error(&a->archive, errno, "fchdir failed"); 1660 return (ARCHIVE_FAILED); 1661 } 1662 nm = pathconf(tree_current_access_path(t), _PC_NAME_MAX); 1663 } else 1664 nm = fpathconf(tree_current_dir_fd(t), _PC_NAME_MAX); 1665 # else 1666 nm = -1; 1667 # endif 1668 if (nm == -1) 1669 t->current_filesystem->name_max = NAME_MAX; 1670 else 1671 t->current_filesystem->name_max = nm; 1672 #endif 1673 if (t->current_filesystem->name_max == 0) { 1674 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 1675 "Cannot determine name_max"); 1676 return (ARCHIVE_FAILED); 1677 } 1678 #endif /* USE_READDIR_R */ 1679 return (ARCHIVE_OK); 1680 } 1681 1682 #elif (defined(HAVE_STATVFS) || defined(HAVE_FSTATVFS)) && defined(ST_LOCAL) 1683 1684 /* 1685 * Gather current filesystem properties on NetBSD 1686 */ 1687 static int 1688 setup_current_filesystem(struct archive_read_disk *a) 1689 { 1690 struct tree *t = a->tree; 1691 struct statvfs svfs; 1692 int r, xr = 0; 1693 1694 t->current_filesystem->synthetic = -1; 1695 if (tree_enter_working_dir(t) != 0) { 1696 archive_set_error(&a->archive, errno, "fchdir failed"); 1697 return (ARCHIVE_FAILED); 1698 } 1699 if (tree_current_is_symblic_link_target(t)) { 1700 r = statvfs(tree_current_access_path(t), &svfs); 1701 if (r == 0) 1702 xr = get_xfer_size(t, -1, tree_current_access_path(t)); 1703 } else { 1704 #ifdef HAVE_FSTATVFS 1705 r = fstatvfs(tree_current_dir_fd(t), &svfs); 1706 if (r == 0) 1707 xr = get_xfer_size(t, tree_current_dir_fd(t), NULL); 1708 #else 1709 r = statvfs(".", &svfs); 1710 if (r == 0) 1711 xr = get_xfer_size(t, -1, "."); 1712 #endif 1713 } 1714 if (r == -1 || xr == -1) { 1715 t->current_filesystem->remote = -1; 1716 archive_set_error(&a->archive, errno, "statvfs failed"); 1717 return (ARCHIVE_FAILED); 1718 } else if (xr == 1) { 1719 /* Usually come here unless NetBSD supports _PC_REC_XFER_ALIGN 1720 * for pathconf() function. */ 1721 set_statvfs_transfer_size(t->current_filesystem, &svfs); 1722 } 1723 if (svfs.f_flag & ST_LOCAL) 1724 t->current_filesystem->remote = 0; 1725 else 1726 t->current_filesystem->remote = 1; 1727 1728 #if defined(ST_NOATIME) 1729 if (svfs.f_flag & ST_NOATIME) 1730 t->current_filesystem->noatime = 1; 1731 else 1732 #endif 1733 t->current_filesystem->noatime = 0; 1734 1735 /* Set maximum filename length. */ 1736 t->current_filesystem->name_max = svfs.f_namemax; 1737 return (ARCHIVE_OK); 1738 } 1739 1740 #elif defined(HAVE_SYS_STATFS_H) && defined(HAVE_LINUX_MAGIC_H) &&\ 1741 defined(HAVE_STATFS) && defined(HAVE_FSTATFS) 1742 /* 1743 * Note: statfs is deprecated since LSB 3.2 1744 */ 1745 1746 #ifndef CIFS_SUPER_MAGIC 1747 #define CIFS_SUPER_MAGIC 0xFF534D42 1748 #endif 1749 #ifndef DEVFS_SUPER_MAGIC 1750 #define DEVFS_SUPER_MAGIC 0x1373 1751 #endif 1752 1753 /* 1754 * Gather current filesystem properties on Linux 1755 */ 1756 static int 1757 setup_current_filesystem(struct archive_read_disk *a) 1758 { 1759 struct tree *t = a->tree; 1760 struct statfs sfs; 1761 #if defined(HAVE_STATVFS) 1762 struct statvfs svfs; 1763 #endif 1764 int r, vr = 0, xr = 0; 1765 1766 if (tree_current_is_symblic_link_target(t)) { 1767 #if defined(HAVE_OPENAT) 1768 /* 1769 * Get file system statistics on any directory 1770 * where current is. 1771 */ 1772 int fd = openat(tree_current_dir_fd(t), 1773 tree_current_access_path(t), O_RDONLY | O_CLOEXEC); 1774 __archive_ensure_cloexec_flag(fd); 1775 if (fd < 0) { 1776 archive_set_error(&a->archive, errno, 1777 "openat failed"); 1778 return (ARCHIVE_FAILED); 1779 } 1780 #if defined(HAVE_FSTATVFS) 1781 vr = fstatvfs(fd, &svfs);/* for f_flag, mount flags */ 1782 #endif 1783 r = fstatfs(fd, &sfs); 1784 if (r == 0) 1785 xr = get_xfer_size(t, fd, NULL); 1786 close(fd); 1787 #else 1788 if (tree_enter_working_dir(t) != 0) { 1789 archive_set_error(&a->archive, errno, "fchdir failed"); 1790 return (ARCHIVE_FAILED); 1791 } 1792 #if defined(HAVE_STATVFS) 1793 vr = statvfs(tree_current_access_path(t), &svfs); 1794 #endif 1795 r = statfs(tree_current_access_path(t), &sfs); 1796 if (r == 0) 1797 xr = get_xfer_size(t, -1, tree_current_access_path(t)); 1798 #endif 1799 } else { 1800 #ifdef HAVE_FSTATFS 1801 #if defined(HAVE_FSTATVFS) 1802 vr = fstatvfs(tree_current_dir_fd(t), &svfs); 1803 #endif 1804 r = fstatfs(tree_current_dir_fd(t), &sfs); 1805 if (r == 0) 1806 xr = get_xfer_size(t, tree_current_dir_fd(t), NULL); 1807 #else 1808 if (tree_enter_working_dir(t) != 0) { 1809 archive_set_error(&a->archive, errno, "fchdir failed"); 1810 return (ARCHIVE_FAILED); 1811 } 1812 #if defined(HAVE_STATVFS) 1813 vr = statvfs(".", &svfs); 1814 #endif 1815 r = statfs(".", &sfs); 1816 if (r == 0) 1817 xr = get_xfer_size(t, -1, "."); 1818 #endif 1819 } 1820 if (r == -1 || xr == -1 || vr == -1) { 1821 t->current_filesystem->synthetic = -1; 1822 t->current_filesystem->remote = -1; 1823 archive_set_error(&a->archive, errno, "statfs failed"); 1824 return (ARCHIVE_FAILED); 1825 } else if (xr == 1) { 1826 /* pathconf(_PC_REX_*) operations are not supported. */ 1827 #if defined(HAVE_STATVFS) 1828 set_statvfs_transfer_size(t->current_filesystem, &svfs); 1829 #else 1830 set_statfs_transfer_size(t->current_filesystem, &sfs); 1831 #endif 1832 } 1833 switch (sfs.f_type) { 1834 case AFS_SUPER_MAGIC: 1835 case CIFS_SUPER_MAGIC: 1836 case CODA_SUPER_MAGIC: 1837 case NCP_SUPER_MAGIC:/* NetWare */ 1838 case NFS_SUPER_MAGIC: 1839 case SMB_SUPER_MAGIC: 1840 t->current_filesystem->remote = 1; 1841 t->current_filesystem->synthetic = 0; 1842 break; 1843 case DEVFS_SUPER_MAGIC: 1844 case PROC_SUPER_MAGIC: 1845 case USBDEVICE_SUPER_MAGIC: 1846 t->current_filesystem->remote = 0; 1847 t->current_filesystem->synthetic = 1; 1848 break; 1849 default: 1850 t->current_filesystem->remote = 0; 1851 t->current_filesystem->synthetic = 0; 1852 break; 1853 } 1854 1855 #if defined(ST_NOATIME) 1856 #if defined(HAVE_STATVFS) 1857 if (svfs.f_flag & ST_NOATIME) 1858 #else 1859 if (sfs.f_flags & ST_NOATIME) 1860 #endif 1861 t->current_filesystem->noatime = 1; 1862 else 1863 #endif 1864 t->current_filesystem->noatime = 0; 1865 1866 #if defined(USE_READDIR_R) 1867 /* Set maximum filename length. */ 1868 #if defined(HAVE_STATVFS) 1869 t->current_filesystem->name_max = svfs.f_namemax; 1870 #else 1871 t->current_filesystem->name_max = sfs.f_namelen; 1872 #endif 1873 if (t->current_filesystem->name_max == 0) { 1874 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 1875 "Cannot determine name_max"); 1876 return (ARCHIVE_FAILED); 1877 } 1878 #endif 1879 return (ARCHIVE_OK); 1880 } 1881 1882 #elif defined(HAVE_SYS_STATVFS_H) &&\ 1883 (defined(HAVE_STATVFS) || defined(HAVE_FSTATVFS)) 1884 1885 /* 1886 * Gather current filesystem properties on other posix platform. 1887 */ 1888 static int 1889 setup_current_filesystem(struct archive_read_disk *a) 1890 { 1891 struct tree *t = a->tree; 1892 struct statvfs svfs; 1893 int r, xr = 0; 1894 1895 t->current_filesystem->synthetic = -1;/* Not supported */ 1896 t->current_filesystem->remote = -1;/* Not supported */ 1897 if (tree_current_is_symblic_link_target(t)) { 1898 #if defined(HAVE_OPENAT) 1899 /* 1900 * Get file system statistics on any directory 1901 * where current is. 1902 */ 1903 int fd = openat(tree_current_dir_fd(t), 1904 tree_current_access_path(t), O_RDONLY | O_CLOEXEC); 1905 __archive_ensure_cloexec_flag(fd); 1906 if (fd < 0) { 1907 archive_set_error(&a->archive, errno, 1908 "openat failed"); 1909 return (ARCHIVE_FAILED); 1910 } 1911 r = fstatvfs(fd, &svfs); 1912 if (r == 0) 1913 xr = get_xfer_size(t, fd, NULL); 1914 close(fd); 1915 #else 1916 if (tree_enter_working_dir(t) != 0) { 1917 archive_set_error(&a->archive, errno, "fchdir failed"); 1918 return (ARCHIVE_FAILED); 1919 } 1920 r = statvfs(tree_current_access_path(t), &svfs); 1921 if (r == 0) 1922 xr = get_xfer_size(t, -1, tree_current_access_path(t)); 1923 #endif 1924 } else { 1925 #ifdef HAVE_FSTATVFS 1926 r = fstatvfs(tree_current_dir_fd(t), &svfs); 1927 if (r == 0) 1928 xr = get_xfer_size(t, tree_current_dir_fd(t), NULL); 1929 #else 1930 if (tree_enter_working_dir(t) != 0) { 1931 archive_set_error(&a->archive, errno, "fchdir failed"); 1932 return (ARCHIVE_FAILED); 1933 } 1934 r = statvfs(".", &svfs); 1935 if (r == 0) 1936 xr = get_xfer_size(t, -1, "."); 1937 #endif 1938 } 1939 if (r == -1 || xr == -1) { 1940 t->current_filesystem->synthetic = -1; 1941 t->current_filesystem->remote = -1; 1942 archive_set_error(&a->archive, errno, "statvfs failed"); 1943 return (ARCHIVE_FAILED); 1944 } else if (xr == 1) { 1945 /* pathconf(_PC_REX_*) operations are not supported. */ 1946 set_statvfs_transfer_size(t->current_filesystem, &svfs); 1947 } 1948 1949 #if defined(ST_NOATIME) 1950 if (svfs.f_flag & ST_NOATIME) 1951 t->current_filesystem->noatime = 1; 1952 else 1953 #endif 1954 t->current_filesystem->noatime = 0; 1955 1956 #if defined(USE_READDIR_R) 1957 /* Set maximum filename length. */ 1958 t->current_filesystem->name_max = svfs.f_namemax; 1959 if (t->current_filesystem->name_max == 0) { 1960 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 1961 "Cannot determine name_max"); 1962 return (ARCHIVE_FAILED); 1963 } 1964 #endif 1965 return (ARCHIVE_OK); 1966 } 1967 1968 #else 1969 1970 /* 1971 * Generic: Gather current filesystem properties. 1972 * TODO: Is this generic function really needed? 1973 */ 1974 static int 1975 setup_current_filesystem(struct archive_read_disk *a) 1976 { 1977 struct tree *t = a->tree; 1978 #if defined(_PC_NAME_MAX) && defined(USE_READDIR_R) 1979 long nm; 1980 #endif 1981 t->current_filesystem->synthetic = -1;/* Not supported */ 1982 t->current_filesystem->remote = -1;/* Not supported */ 1983 t->current_filesystem->noatime = 0; 1984 (void)get_xfer_size(t, -1, ".");/* Dummy call to avoid build error. */ 1985 t->current_filesystem->xfer_align = -1;/* Unknown */ 1986 t->current_filesystem->max_xfer_size = -1; 1987 t->current_filesystem->min_xfer_size = -1; 1988 t->current_filesystem->incr_xfer_size = -1; 1989 1990 #if defined(USE_READDIR_R) 1991 /* Set maximum filename length. */ 1992 # if defined(_PC_NAME_MAX) 1993 if (tree_current_is_symblic_link_target(t)) { 1994 if (tree_enter_working_dir(t) != 0) { 1995 archive_set_error(&a->archive, errno, "fchdir failed"); 1996 return (ARCHIVE_FAILED); 1997 } 1998 nm = pathconf(tree_current_access_path(t), _PC_NAME_MAX); 1999 } else 2000 nm = fpathconf(tree_current_dir_fd(t), _PC_NAME_MAX); 2001 if (nm == -1) 2002 # endif /* _PC_NAME_MAX */ 2003 /* 2004 * Some systems (HP-UX or others?) incorrectly defined 2005 * NAME_MAX macro to be a smaller value. 2006 */ 2007 # if defined(NAME_MAX) && NAME_MAX >= 255 2008 t->current_filesystem->name_max = NAME_MAX; 2009 # else 2010 /* No way to get a trusted value of maximum filename 2011 * length. */ 2012 t->current_filesystem->name_max = PATH_MAX; 2013 # endif /* NAME_MAX */ 2014 # if defined(_PC_NAME_MAX) 2015 else 2016 t->current_filesystem->name_max = nm; 2017 # endif /* _PC_NAME_MAX */ 2018 if (t->current_filesystem->name_max == 0) { 2019 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 2020 "Cannot determine name_max"); 2021 return (ARCHIVE_FAILED); 2022 } 2023 #endif /* USE_READDIR_R */ 2024 return (ARCHIVE_OK); 2025 } 2026 2027 #endif 2028 2029 static int 2030 close_and_restore_time(int fd, struct tree *t, struct restore_time *rt) 2031 { 2032 #ifndef HAVE_UTIMES 2033 (void)t; /* UNUSED */ 2034 (void)rt; /* UNUSED */ 2035 return (close(fd)); 2036 #else 2037 #if defined(HAVE_FUTIMENS) && !defined(__CYGWIN__) 2038 struct timespec timespecs[2]; 2039 #endif 2040 struct timeval times[2]; 2041 2042 if ((t->flags & needsRestoreTimes) == 0 || rt->noatime) { 2043 if (fd >= 0) 2044 return (close(fd)); 2045 else 2046 return (0); 2047 } 2048 2049 #if defined(HAVE_FUTIMENS) && !defined(__CYGWIN__) 2050 timespecs[1].tv_sec = rt->mtime; 2051 timespecs[1].tv_nsec = rt->mtime_nsec; 2052 2053 timespecs[0].tv_sec = rt->atime; 2054 timespecs[0].tv_nsec = rt->atime_nsec; 2055 /* futimens() is defined in POSIX.1-2008. */ 2056 if (futimens(fd, timespecs) == 0) 2057 return (close(fd)); 2058 #endif 2059 2060 times[1].tv_sec = rt->mtime; 2061 times[1].tv_usec = rt->mtime_nsec / 1000; 2062 2063 times[0].tv_sec = rt->atime; 2064 times[0].tv_usec = rt->atime_nsec / 1000; 2065 2066 #if !defined(HAVE_FUTIMENS) && defined(HAVE_FUTIMES) && !defined(__CYGWIN__) 2067 if (futimes(fd, times) == 0) 2068 return (close(fd)); 2069 #endif 2070 close(fd); 2071 #if defined(HAVE_FUTIMESAT) 2072 if (futimesat(tree_current_dir_fd(t), rt->name, times) == 0) 2073 return (0); 2074 #endif 2075 #ifdef HAVE_LUTIMES 2076 if (lutimes(rt->name, times) != 0) 2077 #else 2078 if (AE_IFLNK != rt->filetype && utimes(rt->name, times) != 0) 2079 #endif 2080 return (-1); 2081 #endif 2082 return (0); 2083 } 2084 2085 static int 2086 open_on_current_dir(struct tree *t, const char *path, int flags) 2087 { 2088 #ifdef HAVE_OPENAT 2089 return (openat(tree_current_dir_fd(t), path, flags)); 2090 #else 2091 if (tree_enter_working_dir(t) != 0) 2092 return (-1); 2093 return (open(path, flags)); 2094 #endif 2095 } 2096 2097 static int 2098 tree_dup(int fd) 2099 { 2100 int new_fd; 2101 #ifdef F_DUPFD_CLOEXEC 2102 static volatile int can_dupfd_cloexec = 1; 2103 2104 if (can_dupfd_cloexec) { 2105 new_fd = fcntl(fd, F_DUPFD_CLOEXEC, 0); 2106 if (new_fd != -1) 2107 return (new_fd); 2108 /* Linux 2.6.18 - 2.6.23 declare F_DUPFD_CLOEXEC, 2109 * but it cannot be used. So we have to try dup(). */ 2110 /* We won't try F_DUPFD_CLOEXEC. */ 2111 can_dupfd_cloexec = 0; 2112 } 2113 #endif /* F_DUPFD_CLOEXEC */ 2114 new_fd = dup(fd); 2115 __archive_ensure_cloexec_flag(new_fd); 2116 return (new_fd); 2117 } 2118 2119 /* 2120 * Add a directory path to the current stack. 2121 */ 2122 static void 2123 tree_push(struct tree *t, const char *path, int filesystem_id, 2124 int64_t dev, int64_t ino, struct restore_time *rt) 2125 { 2126 struct tree_entry *te; 2127 2128 te = calloc(1, sizeof(*te)); 2129 if (te == NULL) 2130 __archive_errx(1, "Out of memory"); 2131 te->next = t->stack; 2132 te->parent = t->current; 2133 if (te->parent) 2134 te->depth = te->parent->depth + 1; 2135 t->stack = te; 2136 archive_string_init(&te->name); 2137 te->symlink_parent_fd = -1; 2138 archive_strcpy(&te->name, path); 2139 te->flags = needsDescent | needsOpen | needsAscent; 2140 te->filesystem_id = filesystem_id; 2141 te->dev = dev; 2142 te->ino = ino; 2143 te->dirname_length = t->dirname_length; 2144 te->restore_time.name = te->name.s; 2145 if (rt != NULL) { 2146 te->restore_time.mtime = rt->mtime; 2147 te->restore_time.mtime_nsec = rt->mtime_nsec; 2148 te->restore_time.atime = rt->atime; 2149 te->restore_time.atime_nsec = rt->atime_nsec; 2150 te->restore_time.filetype = rt->filetype; 2151 te->restore_time.noatime = rt->noatime; 2152 } 2153 } 2154 2155 /* 2156 * Append a name to the current dir path. 2157 */ 2158 static void 2159 tree_append(struct tree *t, const char *name, size_t name_length) 2160 { 2161 size_t size_needed; 2162 2163 t->path.s[t->dirname_length] = '\0'; 2164 t->path.length = t->dirname_length; 2165 /* Strip trailing '/' from name, unless entire name is "/". */ 2166 while (name_length > 1 && name[name_length - 1] == '/') 2167 name_length--; 2168 2169 /* Resize pathname buffer as needed. */ 2170 size_needed = name_length + t->dirname_length + 2; 2171 archive_string_ensure(&t->path, size_needed); 2172 /* Add a separating '/' if it's needed. */ 2173 if (t->dirname_length > 0 && t->path.s[archive_strlen(&t->path)-1] != '/') 2174 archive_strappend_char(&t->path, '/'); 2175 t->basename = t->path.s + archive_strlen(&t->path); 2176 archive_strncat(&t->path, name, name_length); 2177 t->restore_time.name = t->basename; 2178 } 2179 2180 /* 2181 * Open a directory tree for traversal. 2182 */ 2183 static struct tree * 2184 tree_open(const char *path, int symlink_mode, int restore_time) 2185 { 2186 struct tree *t; 2187 2188 if ((t = calloc(1, sizeof(*t))) == NULL) 2189 return (NULL); 2190 archive_string_init(&t->path); 2191 archive_string_ensure(&t->path, 31); 2192 t->initial_symlink_mode = symlink_mode; 2193 return (tree_reopen(t, path, restore_time)); 2194 } 2195 2196 static struct tree * 2197 tree_reopen(struct tree *t, const char *path, int restore_time) 2198 { 2199 #if defined(O_PATH) 2200 /* Linux */ 2201 const int o_flag = O_PATH; 2202 #elif defined(O_SEARCH) 2203 /* SunOS */ 2204 const int o_flag = O_SEARCH; 2205 #elif defined(__FreeBSD__) && defined(O_EXEC) 2206 /* FreeBSD */ 2207 const int o_flag = O_EXEC; 2208 #endif 2209 2210 t->flags = (restore_time != 0)?needsRestoreTimes:0; 2211 t->flags |= onInitialDir; 2212 t->visit_type = 0; 2213 t->tree_errno = 0; 2214 t->dirname_length = 0; 2215 t->depth = 0; 2216 t->descend = 0; 2217 t->current = NULL; 2218 t->d = INVALID_DIR_HANDLE; 2219 t->symlink_mode = t->initial_symlink_mode; 2220 archive_string_empty(&t->path); 2221 t->entry_fd = -1; 2222 t->entry_eof = 0; 2223 t->entry_remaining_bytes = 0; 2224 t->initial_filesystem_id = -1; 2225 2226 /* First item is set up a lot like a symlink traversal. */ 2227 tree_push(t, path, 0, 0, 0, NULL); 2228 t->stack->flags = needsFirstVisit; 2229 t->maxOpenCount = t->openCount = 1; 2230 t->initial_dir_fd = open(".", O_RDONLY | O_CLOEXEC); 2231 #if defined(O_PATH) || defined(O_SEARCH) || \ 2232 (defined(__FreeBSD__) && defined(O_EXEC)) 2233 /* 2234 * Most likely reason to fail opening "." is that it's not readable, 2235 * so try again for execute. The consequences of not opening this are 2236 * unhelpful and unnecessary errors later. 2237 */ 2238 if (t->initial_dir_fd < 0) 2239 t->initial_dir_fd = open(".", o_flag | O_CLOEXEC); 2240 #endif 2241 __archive_ensure_cloexec_flag(t->initial_dir_fd); 2242 t->working_dir_fd = tree_dup(t->initial_dir_fd); 2243 return (t); 2244 } 2245 2246 static int 2247 tree_descent(struct tree *t) 2248 { 2249 int flag, new_fd, r = 0; 2250 2251 t->dirname_length = archive_strlen(&t->path); 2252 flag = O_RDONLY | O_CLOEXEC; 2253 #if defined(O_DIRECTORY) 2254 flag |= O_DIRECTORY; 2255 #endif 2256 new_fd = open_on_current_dir(t, t->stack->name.s, flag); 2257 __archive_ensure_cloexec_flag(new_fd); 2258 if (new_fd < 0) { 2259 t->tree_errno = errno; 2260 r = TREE_ERROR_DIR; 2261 } else { 2262 t->depth++; 2263 /* If it is a link, set up fd for the ascent. */ 2264 if (t->stack->flags & isDirLink) { 2265 t->stack->symlink_parent_fd = t->working_dir_fd; 2266 t->openCount++; 2267 if (t->openCount > t->maxOpenCount) 2268 t->maxOpenCount = t->openCount; 2269 } else 2270 close(t->working_dir_fd); 2271 /* Renew the current working directory. */ 2272 t->working_dir_fd = new_fd; 2273 t->flags &= ~onWorkingDir; 2274 } 2275 return (r); 2276 } 2277 2278 /* 2279 * We've finished a directory; ascend back to the parent. 2280 */ 2281 static int 2282 tree_ascend(struct tree *t) 2283 { 2284 struct tree_entry *te; 2285 int new_fd, r = 0, prev_dir_fd; 2286 2287 te = t->stack; 2288 prev_dir_fd = t->working_dir_fd; 2289 if (te->flags & isDirLink) 2290 new_fd = te->symlink_parent_fd; 2291 else { 2292 new_fd = open_on_current_dir(t, "..", O_RDONLY | O_CLOEXEC); 2293 __archive_ensure_cloexec_flag(new_fd); 2294 } 2295 if (new_fd < 0) { 2296 t->tree_errno = errno; 2297 r = TREE_ERROR_FATAL; 2298 } else { 2299 /* Renew the current working directory. */ 2300 t->working_dir_fd = new_fd; 2301 t->flags &= ~onWorkingDir; 2302 /* Current directory has been changed, we should 2303 * close an fd of previous working directory. */ 2304 close_and_restore_time(prev_dir_fd, t, &te->restore_time); 2305 if (te->flags & isDirLink) { 2306 t->openCount--; 2307 te->symlink_parent_fd = -1; 2308 } 2309 t->depth--; 2310 } 2311 return (r); 2312 } 2313 2314 /* 2315 * Return to the initial directory where tree_open() was performed. 2316 */ 2317 static int 2318 tree_enter_initial_dir(struct tree *t) 2319 { 2320 int r = 0; 2321 2322 if ((t->flags & onInitialDir) == 0) { 2323 r = fchdir(t->initial_dir_fd); 2324 if (r == 0) { 2325 t->flags &= ~onWorkingDir; 2326 t->flags |= onInitialDir; 2327 } 2328 } 2329 return (r); 2330 } 2331 2332 /* 2333 * Restore working directory of directory traversals. 2334 */ 2335 static int 2336 tree_enter_working_dir(struct tree *t) 2337 { 2338 int r = 0; 2339 2340 /* 2341 * Change the current directory if really needed. 2342 * Sometimes this is unneeded when we did not do 2343 * descent. 2344 */ 2345 if (t->depth > 0 && (t->flags & onWorkingDir) == 0) { 2346 r = fchdir(t->working_dir_fd); 2347 if (r == 0) { 2348 t->flags &= ~onInitialDir; 2349 t->flags |= onWorkingDir; 2350 } 2351 } 2352 return (r); 2353 } 2354 2355 static int 2356 tree_current_dir_fd(struct tree *t) 2357 { 2358 return (t->working_dir_fd); 2359 } 2360 2361 /* 2362 * Pop the working stack. 2363 */ 2364 static void 2365 tree_pop(struct tree *t) 2366 { 2367 struct tree_entry *te; 2368 2369 t->path.s[t->dirname_length] = '\0'; 2370 t->path.length = t->dirname_length; 2371 if (t->stack == t->current && t->current != NULL) 2372 t->current = t->current->parent; 2373 te = t->stack; 2374 t->stack = te->next; 2375 t->dirname_length = te->dirname_length; 2376 t->basename = t->path.s + t->dirname_length; 2377 while (t->basename[0] == '/') 2378 t->basename++; 2379 archive_string_free(&te->name); 2380 free(te); 2381 } 2382 2383 /* 2384 * Get the next item in the tree traversal. 2385 */ 2386 static int 2387 tree_next(struct tree *t) 2388 { 2389 int r; 2390 2391 while (t->stack != NULL) { 2392 /* If there's an open dir, get the next entry from there. */ 2393 if (t->d != INVALID_DIR_HANDLE) { 2394 r = tree_dir_next_posix(t); 2395 if (r == 0) 2396 continue; 2397 return (r); 2398 } 2399 2400 if (t->stack->flags & needsFirstVisit) { 2401 /* Top stack item needs a regular visit. */ 2402 t->current = t->stack; 2403 tree_append(t, t->stack->name.s, 2404 archive_strlen(&(t->stack->name))); 2405 /* t->dirname_length = t->path_length; */ 2406 /* tree_pop(t); */ 2407 t->stack->flags &= ~needsFirstVisit; 2408 return (t->visit_type = TREE_REGULAR); 2409 } else if (t->stack->flags & needsDescent) { 2410 /* Top stack item is dir to descend into. */ 2411 t->current = t->stack; 2412 tree_append(t, t->stack->name.s, 2413 archive_strlen(&(t->stack->name))); 2414 t->stack->flags &= ~needsDescent; 2415 r = tree_descent(t); 2416 if (r != 0) { 2417 tree_pop(t); 2418 t->visit_type = r; 2419 } else 2420 t->visit_type = TREE_POSTDESCENT; 2421 return (t->visit_type); 2422 } else if (t->stack->flags & needsOpen) { 2423 t->stack->flags &= ~needsOpen; 2424 r = tree_dir_next_posix(t); 2425 if (r == 0) 2426 continue; 2427 return (r); 2428 } else if (t->stack->flags & needsAscent) { 2429 /* Top stack item is dir and we're done with it. */ 2430 r = tree_ascend(t); 2431 tree_pop(t); 2432 t->visit_type = r != 0 ? r : TREE_POSTASCENT; 2433 return (t->visit_type); 2434 } else { 2435 /* Top item on stack is dead. */ 2436 tree_pop(t); 2437 t->flags &= ~hasLstat; 2438 t->flags &= ~hasStat; 2439 } 2440 } 2441 return (t->visit_type = 0); 2442 } 2443 2444 static int 2445 tree_dir_next_posix(struct tree *t) 2446 { 2447 int r; 2448 const char *name; 2449 size_t namelen; 2450 2451 if (t->d == NULL) { 2452 #if defined(USE_READDIR_R) 2453 size_t dirent_size; 2454 #endif 2455 2456 #if defined(HAVE_FDOPENDIR) 2457 t->d = fdopendir(tree_dup(t->working_dir_fd)); 2458 #else /* HAVE_FDOPENDIR */ 2459 if (tree_enter_working_dir(t) == 0) { 2460 t->d = opendir("."); 2461 #ifdef HAVE_DIRFD 2462 __archive_ensure_cloexec_flag(dirfd(t->d)); 2463 #endif 2464 } 2465 #endif /* HAVE_FDOPENDIR */ 2466 if (t->d == NULL) { 2467 r = tree_ascend(t); /* Undo "chdir" */ 2468 tree_pop(t); 2469 t->tree_errno = errno; 2470 t->visit_type = r != 0 ? r : TREE_ERROR_DIR; 2471 return (t->visit_type); 2472 } 2473 #if defined(USE_READDIR_R) 2474 dirent_size = offsetof(struct dirent, d_name) + 2475 t->filesystem_table[t->current->filesystem_id].name_max + 1; 2476 if (t->dirent == NULL || t->dirent_allocated < dirent_size) { 2477 free(t->dirent); 2478 t->dirent = malloc(dirent_size); 2479 if (t->dirent == NULL) { 2480 closedir(t->d); 2481 t->d = INVALID_DIR_HANDLE; 2482 (void)tree_ascend(t); 2483 tree_pop(t); 2484 t->tree_errno = ENOMEM; 2485 t->visit_type = TREE_ERROR_DIR; 2486 return (t->visit_type); 2487 } 2488 t->dirent_allocated = dirent_size; 2489 } 2490 #endif /* USE_READDIR_R */ 2491 } 2492 for (;;) { 2493 errno = 0; 2494 #if defined(USE_READDIR_R) 2495 r = readdir_r(t->d, t->dirent, &t->de); 2496 #ifdef _AIX 2497 /* Note: According to the man page, return value 9 indicates 2498 * that the readdir_r was not successful and the error code 2499 * is set to the global errno variable. And then if the end 2500 * of directory entries was reached, the return value is 9 2501 * and the third parameter is set to NULL and errno is 2502 * unchanged. */ 2503 if (r == 9) 2504 r = errno; 2505 #endif /* _AIX */ 2506 if (r != 0 || t->de == NULL) { 2507 #else 2508 t->de = readdir(t->d); 2509 if (t->de == NULL) { 2510 r = errno; 2511 #endif 2512 closedir(t->d); 2513 t->d = INVALID_DIR_HANDLE; 2514 if (r != 0) { 2515 t->tree_errno = r; 2516 t->visit_type = TREE_ERROR_DIR; 2517 return (t->visit_type); 2518 } else 2519 return (0); 2520 } 2521 name = t->de->d_name; 2522 namelen = D_NAMELEN(t->de); 2523 t->flags &= ~hasLstat; 2524 t->flags &= ~hasStat; 2525 if (name[0] == '.' && name[1] == '\0') 2526 continue; 2527 if (name[0] == '.' && name[1] == '.' && name[2] == '\0') 2528 continue; 2529 tree_append(t, name, namelen); 2530 return (t->visit_type = TREE_REGULAR); 2531 } 2532 } 2533 2534 2535 /* 2536 * Get the stat() data for the entry just returned from tree_next(). 2537 */ 2538 static const struct stat * 2539 tree_current_stat(struct tree *t) 2540 { 2541 if (!(t->flags & hasStat)) { 2542 #ifdef HAVE_FSTATAT 2543 if (fstatat(tree_current_dir_fd(t), 2544 tree_current_access_path(t), &t->st, 0) != 0) 2545 #else 2546 if (tree_enter_working_dir(t) != 0) 2547 return NULL; 2548 if (la_stat(tree_current_access_path(t), &t->st) != 0) 2549 #endif 2550 return NULL; 2551 t->flags |= hasStat; 2552 } 2553 return (&t->st); 2554 } 2555 2556 /* 2557 * Get the lstat() data for the entry just returned from tree_next(). 2558 */ 2559 static const struct stat * 2560 tree_current_lstat(struct tree *t) 2561 { 2562 if (!(t->flags & hasLstat)) { 2563 #ifdef HAVE_FSTATAT 2564 if (fstatat(tree_current_dir_fd(t), 2565 tree_current_access_path(t), &t->lst, 2566 AT_SYMLINK_NOFOLLOW) != 0) 2567 #else 2568 if (tree_enter_working_dir(t) != 0) 2569 return NULL; 2570 #ifdef HAVE_LSTAT 2571 if (lstat(tree_current_access_path(t), &t->lst) != 0) 2572 #else 2573 if (la_stat(tree_current_access_path(t), &t->lst) != 0) 2574 #endif 2575 #endif 2576 return NULL; 2577 t->flags |= hasLstat; 2578 } 2579 return (&t->lst); 2580 } 2581 2582 /* 2583 * Test whether current entry is a dir or link to a dir. 2584 */ 2585 static int 2586 tree_current_is_dir(struct tree *t) 2587 { 2588 const struct stat *st; 2589 /* 2590 * If we already have lstat() info, then try some 2591 * cheap tests to determine if this is a dir. 2592 */ 2593 if (t->flags & hasLstat) { 2594 /* If lstat() says it's a dir, it must be a dir. */ 2595 st = tree_current_lstat(t); 2596 if (st == NULL) 2597 return 0; 2598 if (S_ISDIR(st->st_mode)) 2599 return 1; 2600 /* Not a dir; might be a link to a dir. */ 2601 /* If it's not a link, then it's not a link to a dir. */ 2602 if (!S_ISLNK(st->st_mode)) 2603 return 0; 2604 /* 2605 * It's a link, but we don't know what it's a link to, 2606 * so we'll have to use stat(). 2607 */ 2608 } 2609 2610 st = tree_current_stat(t); 2611 /* If we can't stat it, it's not a dir. */ 2612 if (st == NULL) 2613 return 0; 2614 /* Use the definitive test. Hopefully this is cached. */ 2615 return (S_ISDIR(st->st_mode)); 2616 } 2617 2618 /* 2619 * Test whether current entry is a physical directory. Usually, we 2620 * already have at least one of stat() or lstat() in memory, so we 2621 * use tricks to try to avoid an extra trip to the disk. 2622 */ 2623 static int 2624 tree_current_is_physical_dir(struct tree *t) 2625 { 2626 const struct stat *st; 2627 2628 /* 2629 * If stat() says it isn't a dir, then it's not a dir. 2630 * If stat() data is cached, this check is free, so do it first. 2631 */ 2632 if (t->flags & hasStat) { 2633 st = tree_current_stat(t); 2634 if (st == NULL) 2635 return (0); 2636 if (!S_ISDIR(st->st_mode)) 2637 return (0); 2638 } 2639 2640 /* 2641 * Either stat() said it was a dir (in which case, we have 2642 * to determine whether it's really a link to a dir) or 2643 * stat() info wasn't available. So we use lstat(), which 2644 * hopefully is already cached. 2645 */ 2646 2647 st = tree_current_lstat(t); 2648 /* If we can't stat it, it's not a dir. */ 2649 if (st == NULL) 2650 return 0; 2651 /* Use the definitive test. Hopefully this is cached. */ 2652 return (S_ISDIR(st->st_mode)); 2653 } 2654 2655 /* 2656 * Test whether the same file has been in the tree as its parent. 2657 */ 2658 static int 2659 tree_target_is_same_as_parent(struct tree *t, const struct stat *st) 2660 { 2661 struct tree_entry *te; 2662 2663 for (te = t->current->parent; te != NULL; te = te->parent) { 2664 if (te->dev == (int64_t)st->st_dev && 2665 te->ino == (int64_t)st->st_ino) 2666 return (1); 2667 } 2668 return (0); 2669 } 2670 2671 /* 2672 * Test whether the current file is symbolic link target and 2673 * on the other filesystem. 2674 */ 2675 static int 2676 tree_current_is_symblic_link_target(struct tree *t) 2677 { 2678 static const struct stat *lst, *st; 2679 2680 lst = tree_current_lstat(t); 2681 st = tree_current_stat(t); 2682 return (st != NULL && lst != NULL && 2683 (int64_t)st->st_dev == t->current_filesystem->dev && 2684 st->st_dev != lst->st_dev); 2685 } 2686 2687 /* 2688 * Return the access path for the entry just returned from tree_next(). 2689 */ 2690 static const char * 2691 tree_current_access_path(struct tree *t) 2692 { 2693 return (t->basename); 2694 } 2695 2696 /* 2697 * Return the full path for the entry just returned from tree_next(). 2698 */ 2699 static const char * 2700 tree_current_path(struct tree *t) 2701 { 2702 return (t->path.s); 2703 } 2704 2705 /* 2706 * Terminate the traversal. 2707 */ 2708 static void 2709 tree_close(struct tree *t) 2710 { 2711 2712 if (t == NULL) 2713 return; 2714 if (t->entry_fd >= 0) { 2715 close_and_restore_time(t->entry_fd, t, &t->restore_time); 2716 t->entry_fd = -1; 2717 } 2718 /* Close the handle of readdir(). */ 2719 if (t->d != INVALID_DIR_HANDLE) { 2720 closedir(t->d); 2721 t->d = INVALID_DIR_HANDLE; 2722 } 2723 /* Release anything remaining in the stack. */ 2724 while (t->stack != NULL) { 2725 if (t->stack->flags & isDirLink) 2726 close(t->stack->symlink_parent_fd); 2727 tree_pop(t); 2728 } 2729 if (t->working_dir_fd >= 0) { 2730 close(t->working_dir_fd); 2731 t->working_dir_fd = -1; 2732 } 2733 if (t->initial_dir_fd >= 0) { 2734 close(t->initial_dir_fd); 2735 t->initial_dir_fd = -1; 2736 } 2737 } 2738 2739 /* 2740 * Release any resources. 2741 */ 2742 static void 2743 tree_free(struct tree *t) 2744 { 2745 int i; 2746 2747 if (t == NULL) 2748 return; 2749 archive_string_free(&t->path); 2750 #if defined(USE_READDIR_R) 2751 free(t->dirent); 2752 #endif 2753 free(t->sparse_list); 2754 for (i = 0; i < t->max_filesystem_id; i++) 2755 free(t->filesystem_table[i].allocation_ptr); 2756 free(t->filesystem_table); 2757 free(t); 2758 } 2759 2760 #endif 2761