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