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