2 * Copyright (c) 2003-2009 Tim Kientzle
3 * Copyright (c) 2010-2012 Michihiro NAKAJIMA
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
28 /* This is the tree-walking code for POSIX systems. */
29 #if !defined(_WIN32) || defined(__CYGWIN__)
31 #include "archive_platform.h"
32 __FBSDID("$FreeBSD$");
34 #ifdef HAVE_SYS_PARAM_H
35 #include <sys/param.h>
37 #ifdef HAVE_SYS_MOUNT_H
38 #include <sys/mount.h>
40 #ifdef HAVE_SYS_STAT_H
43 #ifdef HAVE_SYS_STATFS_H
44 #include <sys/statfs.h>
46 #ifdef HAVE_SYS_STATVFS_H
47 #include <sys/statvfs.h>
49 #ifdef HAVE_SYS_TIME_H
52 #ifdef HAVE_LINUX_MAGIC_H
53 #include <linux/magic.h>
55 #ifdef HAVE_LINUX_FS_H
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.
62 #ifdef HAVE_LINUX_EXT2_FS_H
63 #include <linux/ext2_fs.h> /* for Linux file flags */
65 #if defined(HAVE_EXT2FS_EXT2_FS_H) && !defined(__CYGWIN__)
66 #include <ext2fs/ext2_fs.h> /* Linux file flags, broken on Cygwin */
92 #ifdef HAVE_SYS_IOCTL_H
93 #include <sys/ioctl.h>
97 #include "archive_string.h"
98 #include "archive_entry.h"
99 #include "archive_private.h"
100 #include "archive_read_disk_private.h"
103 #error fchdir function required.
112 #if defined(__hpux) && !defined(HAVE_DIRFD)
113 #define dirfd(x) ((x)->__dd_fd)
118 * This is a new directory-walking system that addresses a number
119 * of problems I've had with fts(3). In particular, it has no
120 * pathname-length limits (other than the size of 'int'), handles
121 * deep logical traversals, uses considerably less memory, and has
122 * an opaque interface (easier to modify in the future).
124 * Internally, it keeps a single list of "tree_entry" items that
125 * represent filesystem objects that require further attention.
126 * Non-directories are not kept in memory: they are pulled from
127 * readdir(), returned to the client, then freed as soon as possible.
128 * Any directory entry to be traversed gets pushed onto the stack.
130 * There is surprisingly little information that needs to be kept for
131 * each item on the stack. Just the name, depth (represented here as the
132 * string length of the parent directory's pathname), and some markers
133 * indicating how to get back to the parent (via chdir("..") for a
134 * regular dir or via fchdir(2) for a symlink).
139 * 3) Arbitrary logical traversals by closing/reopening intermediate fds.
142 struct restore_time {
154 struct tree_entry *next;
155 struct tree_entry *parent;
156 struct archive_string name;
157 size_t dirname_length;
162 /* How to return back to the parent of a symlink. */
163 int symlink_parent_fd;
164 /* How to restore time of a directory. */
165 struct restore_time restore_time;
173 #if defined(USE_READDIR_R)
182 * Buffer used for reading file contents.
184 /* Exactly allocated memory pointer. */
185 unsigned char *allocation_ptr;
186 /* Pointer adjusted to the filesystem alignment . */
191 /* Definitions for tree_entry.flags bitmap. */
192 #define isDir 1 /* This entry is a regular directory. */
193 #define isDirLink 2 /* This entry is a symbolic link to a directory. */
194 #define needsFirstVisit 4 /* This is an initial entry. */
195 #define needsDescent 8 /* This entry needs to be previsited. */
196 #define needsOpen 16 /* This is a directory that needs to be opened. */
197 #define needsAscent 32 /* This entry needs to be postvisited. */
200 * Local data for this package.
203 struct tree_entry *stack;
204 struct tree_entry *current;
206 #define INVALID_DIR_HANDLE NULL
208 #if defined(USE_READDIR_R)
209 struct dirent *dirent;
210 size_t dirent_allocated;
214 /* Error code from last failed operation. */
217 /* Dynamically-sized buffer for holding path */
218 struct archive_string path;
220 /* Last path element */
221 const char *basename;
222 /* Leading dir length */
223 size_t dirname_length;
235 /* How to restore time of a file. */
236 struct restore_time restore_time;
238 struct entry_sparse {
241 } *sparse_list, *current_sparse;
243 int sparse_list_size;
245 char initial_symlink_mode;
247 struct filesystem *current_filesystem;
248 struct filesystem *filesystem_table;
249 int initial_filesystem_id;
250 int current_filesystem_id;
251 int max_filesystem_id;
252 int allocated_filesystem;
256 int64_t entry_remaining_bytes;
258 unsigned char *entry_buff;
259 size_t entry_buff_size;
262 /* Definitions for tree.flags bitmap. */
263 #define hasStat 16 /* The st entry is valid. */
264 #define hasLstat 32 /* The lst entry is valid. */
265 #define onWorkingDir 64 /* We are on the working dir where we are
266 * reading directory entry at this time. */
267 #define needsRestoreTimes 128
268 #define onInitialDir 256 /* We are on the initial dir. */
271 tree_dir_next_posix(struct tree *t);
273 #ifdef HAVE_DIRENT_D_NAMLEN
274 /* BSD extension; avoids need for a strlen() call. */
275 #define D_NAMELEN(dp) (dp)->d_namlen
277 #define D_NAMELEN(dp) (strlen((dp)->d_name))
280 /* Initiate/terminate a tree traversal. */
281 static struct tree *tree_open(const char *, int, int);
282 static struct tree *tree_reopen(struct tree *, const char *, int);
283 static void tree_close(struct tree *);
284 static void tree_free(struct tree *);
285 static void tree_push(struct tree *, const char *, int, int64_t, int64_t,
286 struct restore_time *);
287 static int tree_enter_initial_dir(struct tree *);
288 static int tree_enter_working_dir(struct tree *);
289 static int tree_current_dir_fd(struct tree *);
292 * tree_next() returns Zero if there is no next entry, non-zero if
293 * there is. Note that directories are visited three times.
294 * Directories are always visited first as part of enumerating their
295 * parent; that is a "regular" visit. If tree_descend() is invoked at
296 * that time, the directory is added to a work list and will
297 * subsequently be visited two more times: once just after descending
298 * into the directory ("postdescent") and again just after ascending
299 * back to the parent ("postascent").
301 * TREE_ERROR_DIR is returned if the descent failed (because the
302 * directory couldn't be opened, for instance). This is returned
303 * instead of TREE_POSTDESCENT/TREE_POSTASCENT. TREE_ERROR_DIR is not a
304 * fatal error, but it does imply that the relevant subtree won't be
305 * visited. TREE_ERROR_FATAL is returned for an error that left the
306 * traversal completely hosed. Right now, this is only returned for
307 * chdir() failures during ascent.
309 #define TREE_REGULAR 1
310 #define TREE_POSTDESCENT 2
311 #define TREE_POSTASCENT 3
312 #define TREE_ERROR_DIR -1
313 #define TREE_ERROR_FATAL -2
315 static int tree_next(struct tree *);
318 * Return information about the current entry.
322 * The current full pathname, length of the full pathname, and a name
323 * that can be used to access the file. Because tree does use chdir
324 * extensively, the access path is almost never the same as the full
327 * TODO: On platforms that support it, use openat()-style operations
328 * to eliminate the chdir() operations entirely while still supporting
329 * arbitrarily deep traversals. This makes access_path troublesome to
330 * support, of course, which means we'll need a rich enough interface
331 * that clients can function without it. (In particular, we'll need
332 * tree_current_open() that returns an open file descriptor.)
335 static const char *tree_current_path(struct tree *);
336 static const char *tree_current_access_path(struct tree *);
339 * Request the lstat() or stat() data for the current path. Since the
340 * tree package needs to do some of this anyway, and caches the
341 * results, you should take advantage of it here if you need it rather
342 * than make a redundant stat() or lstat() call of your own.
344 static const struct stat *tree_current_stat(struct tree *);
345 static const struct stat *tree_current_lstat(struct tree *);
346 static int tree_current_is_symblic_link_target(struct tree *);
348 /* The following functions use tricks to avoid a certain number of
349 * stat()/lstat() calls. */
350 /* "is_physical_dir" is equivalent to S_ISDIR(tree_current_lstat()->st_mode) */
351 static int tree_current_is_physical_dir(struct tree *);
352 /* "is_dir" is equivalent to S_ISDIR(tree_current_stat()->st_mode) */
353 static int tree_current_is_dir(struct tree *);
354 static int update_current_filesystem(struct archive_read_disk *a,
356 static int setup_current_filesystem(struct archive_read_disk *);
357 static int tree_target_is_same_as_parent(struct tree *, const struct stat *);
359 static int _archive_read_disk_open(struct archive *, const char *);
360 static int _archive_read_free(struct archive *);
361 static int _archive_read_close(struct archive *);
362 static int _archive_read_data_block(struct archive *,
363 const void **, size_t *, int64_t *);
364 static int _archive_read_next_header(struct archive *,
365 struct archive_entry **);
366 static int _archive_read_next_header2(struct archive *,
367 struct archive_entry *);
368 static const char *trivial_lookup_gname(void *, int64_t gid);
369 static const char *trivial_lookup_uname(void *, int64_t uid);
370 static int setup_sparse(struct archive_read_disk *, struct archive_entry *);
371 static int close_and_restore_time(int fd, struct tree *,
372 struct restore_time *);
373 static int open_on_current_dir(struct tree *, const char *, int);
374 static int tree_dup(int);
377 static const struct archive_vtable
378 archive_read_disk_vtable = {
379 .archive_free = _archive_read_free,
380 .archive_close = _archive_read_close,
381 .archive_read_data_block = _archive_read_data_block,
382 .archive_read_next_header = _archive_read_next_header,
383 .archive_read_next_header2 = _archive_read_next_header2,
387 archive_read_disk_gname(struct archive *_a, la_int64_t gid)
389 struct archive_read_disk *a = (struct archive_read_disk *)_a;
390 if (ARCHIVE_OK != __archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
391 ARCHIVE_STATE_ANY, "archive_read_disk_gname"))
393 if (a->lookup_gname == NULL)
395 return ((*a->lookup_gname)(a->lookup_gname_data, gid));
399 archive_read_disk_uname(struct archive *_a, la_int64_t uid)
401 struct archive_read_disk *a = (struct archive_read_disk *)_a;
402 if (ARCHIVE_OK != __archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
403 ARCHIVE_STATE_ANY, "archive_read_disk_uname"))
405 if (a->lookup_uname == NULL)
407 return ((*a->lookup_uname)(a->lookup_uname_data, uid));
411 archive_read_disk_set_gname_lookup(struct archive *_a,
413 const char * (*lookup_gname)(void *private, la_int64_t gid),
414 void (*cleanup_gname)(void *private))
416 struct archive_read_disk *a = (struct archive_read_disk *)_a;
417 archive_check_magic(&a->archive, ARCHIVE_READ_DISK_MAGIC,
418 ARCHIVE_STATE_ANY, "archive_read_disk_set_gname_lookup");
420 if (a->cleanup_gname != NULL && a->lookup_gname_data != NULL)
421 (a->cleanup_gname)(a->lookup_gname_data);
423 a->lookup_gname = lookup_gname;
424 a->cleanup_gname = cleanup_gname;
425 a->lookup_gname_data = private_data;
430 archive_read_disk_set_uname_lookup(struct archive *_a,
432 const char * (*lookup_uname)(void *private, la_int64_t uid),
433 void (*cleanup_uname)(void *private))
435 struct archive_read_disk *a = (struct archive_read_disk *)_a;
436 archive_check_magic(&a->archive, ARCHIVE_READ_DISK_MAGIC,
437 ARCHIVE_STATE_ANY, "archive_read_disk_set_uname_lookup");
439 if (a->cleanup_uname != NULL && a->lookup_uname_data != NULL)
440 (a->cleanup_uname)(a->lookup_uname_data);
442 a->lookup_uname = lookup_uname;
443 a->cleanup_uname = cleanup_uname;
444 a->lookup_uname_data = private_data;
449 * Create a new archive_read_disk object and initialize it with global state.
452 archive_read_disk_new(void)
454 struct archive_read_disk *a;
456 a = (struct archive_read_disk *)calloc(1, sizeof(*a));
459 a->archive.magic = ARCHIVE_READ_DISK_MAGIC;
460 a->archive.state = ARCHIVE_STATE_NEW;
461 a->archive.vtable = &archive_read_disk_vtable;
462 a->entry = archive_entry_new2(&a->archive);
463 a->lookup_uname = trivial_lookup_uname;
464 a->lookup_gname = trivial_lookup_gname;
465 a->flags = ARCHIVE_READDISK_MAC_COPYFILE;
466 a->open_on_current_dir = open_on_current_dir;
467 a->tree_current_dir_fd = tree_current_dir_fd;
468 a->tree_enter_working_dir = tree_enter_working_dir;
469 return (&a->archive);
473 _archive_read_free(struct archive *_a)
475 struct archive_read_disk *a = (struct archive_read_disk *)_a;
480 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
481 ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_free");
483 if (a->archive.state != ARCHIVE_STATE_CLOSED)
484 r = _archive_read_close(&a->archive);
489 if (a->cleanup_gname != NULL && a->lookup_gname_data != NULL)
490 (a->cleanup_gname)(a->lookup_gname_data);
491 if (a->cleanup_uname != NULL && a->lookup_uname_data != NULL)
492 (a->cleanup_uname)(a->lookup_uname_data);
493 archive_string_free(&a->archive.error_string);
494 archive_entry_free(a->entry);
495 a->archive.magic = 0;
496 __archive_clean(&a->archive);
502 _archive_read_close(struct archive *_a)
504 struct archive_read_disk *a = (struct archive_read_disk *)_a;
506 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
507 ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_close");
509 if (a->archive.state != ARCHIVE_STATE_FATAL)
510 a->archive.state = ARCHIVE_STATE_CLOSED;
518 setup_symlink_mode(struct archive_read_disk *a, char symlink_mode,
521 a->symlink_mode = symlink_mode;
522 a->follow_symlinks = follow_symlinks;
523 if (a->tree != NULL) {
524 a->tree->initial_symlink_mode = a->symlink_mode;
525 a->tree->symlink_mode = a->symlink_mode;
530 archive_read_disk_set_symlink_logical(struct archive *_a)
532 struct archive_read_disk *a = (struct archive_read_disk *)_a;
533 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
534 ARCHIVE_STATE_ANY, "archive_read_disk_set_symlink_logical");
535 setup_symlink_mode(a, 'L', 1);
540 archive_read_disk_set_symlink_physical(struct archive *_a)
542 struct archive_read_disk *a = (struct archive_read_disk *)_a;
543 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
544 ARCHIVE_STATE_ANY, "archive_read_disk_set_symlink_physical");
545 setup_symlink_mode(a, 'P', 0);
550 archive_read_disk_set_symlink_hybrid(struct archive *_a)
552 struct archive_read_disk *a = (struct archive_read_disk *)_a;
553 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
554 ARCHIVE_STATE_ANY, "archive_read_disk_set_symlink_hybrid");
555 setup_symlink_mode(a, 'H', 1);/* Follow symlinks initially. */
560 archive_read_disk_set_atime_restored(struct archive *_a)
562 struct archive_read_disk *a = (struct archive_read_disk *)_a;
563 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
564 ARCHIVE_STATE_ANY, "archive_read_disk_restore_atime");
566 a->flags |= ARCHIVE_READDISK_RESTORE_ATIME;
568 a->tree->flags |= needsRestoreTimes;
571 /* Display warning and unset flag */
572 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
573 "Cannot restore access time on this system");
574 a->flags &= ~ARCHIVE_READDISK_RESTORE_ATIME;
575 return (ARCHIVE_WARN);
580 archive_read_disk_set_behavior(struct archive *_a, int flags)
582 struct archive_read_disk *a = (struct archive_read_disk *)_a;
585 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
586 ARCHIVE_STATE_ANY, "archive_read_disk_honor_nodump");
590 if (flags & ARCHIVE_READDISK_RESTORE_ATIME)
591 r = archive_read_disk_set_atime_restored(_a);
594 a->tree->flags &= ~needsRestoreTimes;
600 * Trivial implementations of gname/uname lookup functions.
601 * These are normally overridden by the client, but these stub
602 * versions ensure that we always have something that works.
605 trivial_lookup_gname(void *private_data, int64_t gid)
607 (void)private_data; /* UNUSED */
608 (void)gid; /* UNUSED */
613 trivial_lookup_uname(void *private_data, int64_t uid)
615 (void)private_data; /* UNUSED */
616 (void)uid; /* UNUSED */
621 * Allocate memory for the reading buffer adjusted to the filesystem
625 setup_suitable_read_buffer(struct archive_read_disk *a)
627 struct tree *t = a->tree;
628 struct filesystem *cf = t->current_filesystem;
632 if (cf->allocation_ptr == NULL) {
633 /* If we couldn't get a filesystem alignment,
634 * we use 4096 as default value but we won't use
635 * O_DIRECT to open() and openat() operations. */
636 long xfer_align = (cf->xfer_align == -1)?4096:cf->xfer_align;
638 if (cf->max_xfer_size != -1)
639 asize = cf->max_xfer_size + xfer_align;
641 long incr = cf->incr_xfer_size;
642 /* Some platform does not set a proper value to
645 incr = cf->min_xfer_size;
646 if (cf->min_xfer_size < 0) {
650 asize = cf->min_xfer_size;
652 /* Increase a buffer size up to 64K bytes in
653 * a proper increment size. */
654 while (asize < 1024*64)
656 /* Take a margin to adjust to the filesystem
660 cf->allocation_ptr = malloc(asize);
661 if (cf->allocation_ptr == NULL) {
662 archive_set_error(&a->archive, ENOMEM,
663 "Couldn't allocate memory");
664 a->archive.state = ARCHIVE_STATE_FATAL;
665 return (ARCHIVE_FATAL);
669 * Calculate proper address for the filesystem.
671 s = (uintptr_t)cf->allocation_ptr;
677 * Set a read buffer pointer in the proper alignment of
678 * the current filesystem.
680 cf->buff = cf->allocation_ptr + s;
681 cf->buff_size = asize - xfer_align;
687 _archive_read_data_block(struct archive *_a, const void **buff,
688 size_t *size, int64_t *offset)
690 struct archive_read_disk *a = (struct archive_read_disk *)_a;
691 struct tree *t = a->tree;
694 int64_t sparse_bytes;
696 int empty_sparse_region = 0;
698 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA,
699 "archive_read_data_block");
701 if (t->entry_eof || t->entry_remaining_bytes <= 0) {
703 goto abort_read_data;
707 * Open the current file.
709 if (t->entry_fd < 0) {
710 int flags = O_RDONLY | O_BINARY | O_CLOEXEC;
713 * Eliminate or reduce cache effects if we can.
715 * Carefully consider this to be enabled.
717 #if defined(O_DIRECT) && 0/* Disabled for now */
718 if (t->current_filesystem->xfer_align != -1 &&
722 #if defined(O_NOATIME)
724 * Linux has O_NOATIME flag; use it if we need.
726 if ((t->flags & needsRestoreTimes) != 0 &&
727 t->restore_time.noatime == 0)
730 t->entry_fd = open_on_current_dir(t,
731 tree_current_access_path(t), flags);
732 __archive_ensure_cloexec_flag(t->entry_fd);
733 #if defined(O_NOATIME)
735 * When we did open the file with O_NOATIME flag,
736 * if successful, set 1 to t->restore_time.noatime
737 * not to restore an atime of the file later.
738 * if failed by EPERM, retry it without O_NOATIME flag.
740 if (flags & O_NOATIME) {
741 if (t->entry_fd >= 0)
742 t->restore_time.noatime = 1;
743 else if (errno == EPERM)
747 if (t->entry_fd < 0) {
748 archive_set_error(&a->archive, errno,
749 "Couldn't open %s", tree_current_path(t));
751 tree_enter_initial_dir(t);
752 goto abort_read_data;
754 tree_enter_initial_dir(t);
758 * Allocate read buffer if not allocated.
760 if (t->current_filesystem->allocation_ptr == NULL) {
761 r = setup_suitable_read_buffer(a);
762 if (r != ARCHIVE_OK) {
763 a->archive.state = ARCHIVE_STATE_FATAL;
764 goto abort_read_data;
767 t->entry_buff = t->current_filesystem->buff;
768 t->entry_buff_size = t->current_filesystem->buff_size;
770 buffbytes = t->entry_buff_size;
771 if ((int64_t)buffbytes > t->current_sparse->length)
772 buffbytes = t->current_sparse->length;
774 if (t->current_sparse->length == 0)
775 empty_sparse_region = 1;
779 * TODO: Should we consider t->current_filesystem->xfer_align?
781 if (t->current_sparse->offset > t->entry_total) {
782 if (lseek(t->entry_fd,
783 (off_t)t->current_sparse->offset, SEEK_SET) < 0) {
784 archive_set_error(&a->archive, errno, "Seek error");
786 a->archive.state = ARCHIVE_STATE_FATAL;
787 goto abort_read_data;
789 sparse_bytes = t->current_sparse->offset - t->entry_total;
790 t->entry_remaining_bytes -= sparse_bytes;
791 t->entry_total += sparse_bytes;
795 * Read file contents.
798 bytes = read(t->entry_fd, t->entry_buff, buffbytes);
800 archive_set_error(&a->archive, errno, "Read error");
802 a->archive.state = ARCHIVE_STATE_FATAL;
803 goto abort_read_data;
808 * Return an EOF unless we've read a leading empty sparse region, which
809 * is used to represent fully-sparse files.
811 if (bytes == 0 && !empty_sparse_region) {
815 goto abort_read_data;
817 *buff = t->entry_buff;
819 *offset = t->entry_total;
820 t->entry_total += bytes;
821 t->entry_remaining_bytes -= bytes;
822 if (t->entry_remaining_bytes == 0) {
823 /* Close the current file descriptor */
824 close_and_restore_time(t->entry_fd, t, &t->restore_time);
828 t->current_sparse->offset += bytes;
829 t->current_sparse->length -= bytes;
830 if (t->current_sparse->length == 0 && !t->entry_eof)
837 *offset = t->entry_total;
838 if (t->entry_fd >= 0) {
839 /* Close the current file descriptor */
840 close_and_restore_time(t->entry_fd, t, &t->restore_time);
847 next_entry(struct archive_read_disk *a, struct tree *t,
848 struct archive_entry *entry)
850 const struct stat *st; /* info to use for this entry */
851 const struct stat *lst;/* lstat() information */
853 int delayed, delayed_errno, descend, r;
854 struct archive_string delayed_str;
856 delayed = ARCHIVE_OK;
858 archive_string_init(&delayed_str);
864 switch (tree_next(t)) {
865 case TREE_ERROR_FATAL:
866 archive_set_error(&a->archive, t->tree_errno,
867 "%s: Unable to continue traversing directory tree",
868 tree_current_path(t));
869 a->archive.state = ARCHIVE_STATE_FATAL;
870 tree_enter_initial_dir(t);
871 return (ARCHIVE_FATAL);
873 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
874 "%s: Couldn't visit directory",
875 tree_current_path(t));
876 tree_enter_initial_dir(t);
877 return (ARCHIVE_FAILED);
879 tree_enter_initial_dir(t);
880 return (ARCHIVE_EOF);
881 case TREE_POSTDESCENT:
882 case TREE_POSTASCENT:
885 lst = tree_current_lstat(t);
887 if (errno == ENOENT && t->depth > 0) {
888 delayed = ARCHIVE_WARN;
889 delayed_errno = errno;
890 if (delayed_str.length == 0) {
891 archive_string_sprintf(&delayed_str,
892 "%s", tree_current_path(t));
894 archive_string_sprintf(&delayed_str,
895 " %s", tree_current_path(t));
898 archive_set_error(&a->archive, errno,
900 tree_current_path(t));
901 tree_enter_initial_dir(t);
902 return (ARCHIVE_FAILED);
907 } while (lst == NULL);
910 if (a->flags & ARCHIVE_READDISK_MAC_COPYFILE) {
911 /* If we're using copyfile(), ignore "._XXX" files. */
912 const char *bname = strrchr(tree_current_path(t), '/');
914 bname = tree_current_path(t);
917 if (bname[0] == '.' && bname[1] == '_')
918 return (ARCHIVE_RETRY);
922 archive_entry_copy_pathname(entry, tree_current_path(t));
924 * Perform path matching.
927 r = archive_match_path_excluded(a->matching, entry);
929 archive_set_error(&(a->archive), errno,
930 "Failed : %s", archive_error_string(a->matching));
934 if (a->excluded_cb_func)
935 a->excluded_cb_func(&(a->archive),
936 a->excluded_cb_data, entry);
937 return (ARCHIVE_RETRY);
942 * Distinguish 'L'/'P'/'H' symlink following.
944 switch(t->symlink_mode) {
946 /* 'H': After the first item, rest like 'P'. */
947 t->symlink_mode = 'P';
948 /* 'H': First item (from command line) like 'L'. */
951 /* 'L': Do descend through a symlink to dir. */
952 descend = tree_current_is_dir(t);
953 /* 'L': Follow symlinks to files. */
954 a->symlink_mode = 'L';
955 a->follow_symlinks = 1;
956 /* 'L': Archive symlinks as targets, if we can. */
957 st = tree_current_stat(t);
958 if (st != NULL && !tree_target_is_same_as_parent(t, st))
960 /* If stat fails, we have a broken symlink;
961 * in that case, don't follow the link. */
964 /* 'P': Don't descend through a symlink to dir. */
965 descend = tree_current_is_physical_dir(t);
966 /* 'P': Don't follow symlinks to files. */
967 a->symlink_mode = 'P';
968 a->follow_symlinks = 0;
969 /* 'P': Archive symlinks as symlinks. */
974 if (update_current_filesystem(a, st->st_dev) != ARCHIVE_OK) {
975 a->archive.state = ARCHIVE_STATE_FATAL;
976 tree_enter_initial_dir(t);
977 return (ARCHIVE_FATAL);
979 if (t->initial_filesystem_id == -1)
980 t->initial_filesystem_id = t->current_filesystem_id;
981 if (a->flags & ARCHIVE_READDISK_NO_TRAVERSE_MOUNTS) {
982 if (t->initial_filesystem_id != t->current_filesystem_id)
985 t->descend = descend;
989 * If the file is marked with nodump flag, do not return this entry.
991 if (a->flags & ARCHIVE_READDISK_HONOR_NODUMP) {
992 #if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP)
993 if (st->st_flags & UF_NODUMP)
994 return (ARCHIVE_RETRY);
995 #elif (defined(FS_IOC_GETFLAGS) && defined(FS_NODUMP_FL) && \
996 defined(HAVE_WORKING_FS_IOC_GETFLAGS)) || \
997 (defined(EXT2_IOC_GETFLAGS) && defined(EXT2_NODUMP_FL) && \
998 defined(HAVE_WORKING_EXT2_IOC_GETFLAGS))
999 if (S_ISREG(st->st_mode) || S_ISDIR(st->st_mode)) {
1002 t->entry_fd = open_on_current_dir(t,
1003 tree_current_access_path(t),
1004 O_RDONLY | O_NONBLOCK | O_CLOEXEC);
1005 __archive_ensure_cloexec_flag(t->entry_fd);
1006 if (t->entry_fd >= 0) {
1007 r = ioctl(t->entry_fd,
1008 #ifdef FS_IOC_GETFLAGS
1015 if (r == 0 && (stflags & FS_NODUMP_FL) != 0)
1017 if (r == 0 && (stflags & EXT2_NODUMP_FL) != 0)
1019 return (ARCHIVE_RETRY);
1025 archive_entry_copy_stat(entry, st);
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, invoking 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;
1038 * Perform time matching.
1041 r = archive_match_time_excluded(a->matching, entry);
1043 archive_set_error(&(a->archive), errno,
1044 "Failed : %s", archive_error_string(a->matching));
1048 if (a->excluded_cb_func)
1049 a->excluded_cb_func(&(a->archive),
1050 a->excluded_cb_data, entry);
1051 return (ARCHIVE_RETRY);
1055 /* Lookup uname/gname */
1056 name = archive_read_disk_uname(&(a->archive), archive_entry_uid(entry));
1058 archive_entry_copy_uname(entry, name);
1059 name = archive_read_disk_gname(&(a->archive), archive_entry_gid(entry));
1061 archive_entry_copy_gname(entry, name);
1064 * Perform owner matching.
1067 r = archive_match_owner_excluded(a->matching, entry);
1069 archive_set_error(&(a->archive), errno,
1070 "Failed : %s", archive_error_string(a->matching));
1074 if (a->excluded_cb_func)
1075 a->excluded_cb_func(&(a->archive),
1076 a->excluded_cb_data, entry);
1077 return (ARCHIVE_RETRY);
1082 * Invoke a meta data filter callback.
1084 if (a->metadata_filter_func) {
1085 if (!a->metadata_filter_func(&(a->archive),
1086 a->metadata_filter_data, entry))
1087 return (ARCHIVE_RETRY);
1091 * Populate the archive_entry with metadata from the disk.
1093 archive_entry_copy_sourcepath(entry, tree_current_access_path(t));
1094 r = archive_read_disk_entry_from_file(&(a->archive), entry,
1097 if (r == ARCHIVE_OK) {
1099 if (r != ARCHIVE_OK) {
1100 archive_string_sprintf(&delayed_str, ": %s",
1101 "File removed before we read it");
1102 archive_set_error(&(a->archive), delayed_errno,
1103 "%s", delayed_str.s);
1106 archive_string_free(&delayed_str);
1112 _archive_read_next_header(struct archive *_a, struct archive_entry **entryp)
1115 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1117 ret = _archive_read_next_header2(_a, a->entry);
1123 _archive_read_next_header2(struct archive *_a, struct archive_entry *entry)
1125 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1129 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
1130 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
1131 "archive_read_next_header2");
1134 if (t->entry_fd >= 0) {
1135 close_and_restore_time(t->entry_fd, t, &t->restore_time);
1139 archive_entry_clear(entry);
1142 r = next_entry(a, t, entry);
1143 if (t->entry_fd >= 0) {
1148 if (r == ARCHIVE_RETRY) {
1149 archive_entry_clear(entry);
1155 /* Return to the initial directory. */
1156 tree_enter_initial_dir(t);
1159 * EOF and FATAL are persistent at this layer. By
1160 * modifying the state, we guarantee that future calls to
1161 * read a header or read data will fail.
1165 a->archive.state = ARCHIVE_STATE_EOF;
1169 /* Overwrite the sourcepath based on the initial directory. */
1170 archive_entry_copy_sourcepath(entry, tree_current_path(t));
1172 if (archive_entry_filetype(entry) == AE_IFREG) {
1173 t->nlink = archive_entry_nlink(entry);
1174 t->entry_remaining_bytes = archive_entry_size(entry);
1175 t->entry_eof = (t->entry_remaining_bytes == 0)? 1: 0;
1176 if (!t->entry_eof &&
1177 setup_sparse(a, entry) != ARCHIVE_OK)
1178 return (ARCHIVE_FATAL);
1180 t->entry_remaining_bytes = 0;
1183 a->archive.state = ARCHIVE_STATE_DATA;
1188 a->archive.state = ARCHIVE_STATE_FATAL;
1192 __archive_reset_read_data(&a->archive);
1197 setup_sparse(struct archive_read_disk *a, struct archive_entry *entry)
1199 struct tree *t = a->tree;
1200 int64_t length, offset;
1203 t->sparse_count = archive_entry_sparse_reset(entry);
1204 if (t->sparse_count+1 > t->sparse_list_size) {
1205 free(t->sparse_list);
1206 t->sparse_list_size = t->sparse_count + 1;
1207 t->sparse_list = malloc(sizeof(t->sparse_list[0]) *
1208 t->sparse_list_size);
1209 if (t->sparse_list == NULL) {
1210 t->sparse_list_size = 0;
1211 archive_set_error(&a->archive, ENOMEM,
1212 "Can't allocate data");
1213 a->archive.state = ARCHIVE_STATE_FATAL;
1214 return (ARCHIVE_FATAL);
1217 for (i = 0; i < t->sparse_count; i++) {
1218 archive_entry_sparse_next(entry, &offset, &length);
1219 t->sparse_list[i].offset = offset;
1220 t->sparse_list[i].length = length;
1223 t->sparse_list[i].offset = 0;
1224 t->sparse_list[i].length = archive_entry_size(entry);
1226 t->sparse_list[i].offset = archive_entry_size(entry);
1227 t->sparse_list[i].length = 0;
1229 t->current_sparse = t->sparse_list;
1231 return (ARCHIVE_OK);
1235 archive_read_disk_set_matching(struct archive *_a, struct archive *_ma,
1236 void (*_excluded_func)(struct archive *, void *, struct archive_entry *),
1239 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1240 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
1241 ARCHIVE_STATE_ANY, "archive_read_disk_set_matching");
1243 a->excluded_cb_func = _excluded_func;
1244 a->excluded_cb_data = _client_data;
1245 return (ARCHIVE_OK);
1249 archive_read_disk_set_metadata_filter_callback(struct archive *_a,
1250 int (*_metadata_filter_func)(struct archive *, void *,
1251 struct archive_entry *), void *_client_data)
1253 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1255 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_ANY,
1256 "archive_read_disk_set_metadata_filter_callback");
1258 a->metadata_filter_func = _metadata_filter_func;
1259 a->metadata_filter_data = _client_data;
1260 return (ARCHIVE_OK);
1264 archive_read_disk_can_descend(struct archive *_a)
1266 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1267 struct tree *t = a->tree;
1269 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
1270 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
1271 "archive_read_disk_can_descend");
1273 return (t->visit_type == TREE_REGULAR && t->descend);
1277 * Called by the client to mark the directory just returned from
1278 * tree_next() as needing to be visited.
1281 archive_read_disk_descend(struct archive *_a)
1283 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1284 struct tree *t = a->tree;
1286 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
1287 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
1288 "archive_read_disk_descend");
1290 if (!archive_read_disk_can_descend(_a))
1291 return (ARCHIVE_OK);
1294 * We must not treat the initial specified path as a physical dir,
1295 * because if we do then we will try and ascend out of it by opening
1296 * ".." which is (a) wrong and (b) causes spurious permissions errors
1297 * if ".." is not readable by us. Instead, treat it as if it were a
1298 * symlink. (This uses an extra fd, but it can only happen once at the
1299 * top level of a traverse.) But we can't necessarily assume t->st is
1300 * valid here (though t->lst is), which complicates the logic a
1303 if (tree_current_is_physical_dir(t)) {
1304 tree_push(t, t->basename, t->current_filesystem_id,
1305 t->lst.st_dev, t->lst.st_ino, &t->restore_time);
1306 if (t->stack->parent->parent != NULL)
1307 t->stack->flags |= isDir;
1309 t->stack->flags |= isDirLink;
1310 } else if (tree_current_is_dir(t)) {
1311 tree_push(t, t->basename, t->current_filesystem_id,
1312 t->st.st_dev, t->st.st_ino, &t->restore_time);
1313 t->stack->flags |= isDirLink;
1316 return (ARCHIVE_OK);
1320 archive_read_disk_open(struct archive *_a, const char *pathname)
1322 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1324 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
1325 ARCHIVE_STATE_NEW | ARCHIVE_STATE_CLOSED,
1326 "archive_read_disk_open");
1327 archive_clear_error(&a->archive);
1329 return (_archive_read_disk_open(_a, pathname));
1333 archive_read_disk_open_w(struct archive *_a, const wchar_t *pathname)
1335 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1336 struct archive_string path;
1339 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
1340 ARCHIVE_STATE_NEW | ARCHIVE_STATE_CLOSED,
1341 "archive_read_disk_open_w");
1342 archive_clear_error(&a->archive);
1344 /* Make a char string from a wchar_t string. */
1345 archive_string_init(&path);
1346 if (archive_string_append_from_wcs(&path, pathname,
1347 wcslen(pathname)) != 0) {
1348 if (errno == ENOMEM)
1349 archive_set_error(&a->archive, ENOMEM,
1350 "Can't allocate memory");
1352 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1353 "Can't convert a path to a char string");
1354 a->archive.state = ARCHIVE_STATE_FATAL;
1355 ret = ARCHIVE_FATAL;
1357 ret = _archive_read_disk_open(_a, path.s);
1359 archive_string_free(&path);
1364 _archive_read_disk_open(struct archive *_a, const char *pathname)
1366 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1368 if (a->tree != NULL)
1369 a->tree = tree_reopen(a->tree, pathname,
1370 a->flags & ARCHIVE_READDISK_RESTORE_ATIME);
1372 a->tree = tree_open(pathname, a->symlink_mode,
1373 a->flags & ARCHIVE_READDISK_RESTORE_ATIME);
1374 if (a->tree == NULL) {
1375 archive_set_error(&a->archive, ENOMEM,
1376 "Can't allocate tar data");
1377 a->archive.state = ARCHIVE_STATE_FATAL;
1378 return (ARCHIVE_FATAL);
1380 a->archive.state = ARCHIVE_STATE_HEADER;
1382 return (ARCHIVE_OK);
1386 * Return a current filesystem ID which is index of the filesystem entry
1387 * you've visited through archive_read_disk.
1390 archive_read_disk_current_filesystem(struct archive *_a)
1392 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1394 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA,
1395 "archive_read_disk_current_filesystem");
1397 return (a->tree->current_filesystem_id);
1401 update_current_filesystem(struct archive_read_disk *a, int64_t dev)
1403 struct tree *t = a->tree;
1406 if (t->current_filesystem != NULL &&
1407 t->current_filesystem->dev == dev)
1408 return (ARCHIVE_OK);
1410 for (i = 0; i < t->max_filesystem_id; i++) {
1411 if (t->filesystem_table[i].dev == dev) {
1412 /* There is the filesystem ID we've already generated. */
1413 t->current_filesystem_id = i;
1414 t->current_filesystem = &(t->filesystem_table[i]);
1415 return (ARCHIVE_OK);
1420 * This is the new filesystem which we have to generate a new ID for.
1422 fid = t->max_filesystem_id++;
1423 if (t->max_filesystem_id > t->allocated_filesystem) {
1427 s = t->max_filesystem_id * 2;
1428 p = realloc(t->filesystem_table,
1429 s * sizeof(*t->filesystem_table));
1431 archive_set_error(&a->archive, ENOMEM,
1432 "Can't allocate tar data");
1433 return (ARCHIVE_FATAL);
1435 t->filesystem_table = (struct filesystem *)p;
1436 t->allocated_filesystem = s;
1438 t->current_filesystem_id = fid;
1439 t->current_filesystem = &(t->filesystem_table[fid]);
1440 t->current_filesystem->dev = dev;
1441 t->current_filesystem->allocation_ptr = NULL;
1442 t->current_filesystem->buff = NULL;
1444 /* Setup the current filesystem properties which depend on
1445 * platform specific. */
1446 return (setup_current_filesystem(a));
1450 * Returns 1 if current filesystem is generated filesystem, 0 if it is not
1451 * or -1 if it is unknown.
1454 archive_read_disk_current_filesystem_is_synthetic(struct archive *_a)
1456 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1458 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA,
1459 "archive_read_disk_current_filesystem");
1461 return (a->tree->current_filesystem->synthetic);
1465 * Returns 1 if current filesystem is remote filesystem, 0 if it is not
1466 * or -1 if it is unknown.
1469 archive_read_disk_current_filesystem_is_remote(struct archive *_a)
1471 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1473 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA,
1474 "archive_read_disk_current_filesystem");
1476 return (a->tree->current_filesystem->remote);
1479 #if defined(_PC_REC_INCR_XFER_SIZE) && defined(_PC_REC_MAX_XFER_SIZE) &&\
1480 defined(_PC_REC_MIN_XFER_SIZE) && defined(_PC_REC_XFER_ALIGN)
1482 get_xfer_size(struct tree *t, int fd, const char *path)
1484 t->current_filesystem->xfer_align = -1;
1487 t->current_filesystem->incr_xfer_size =
1488 fpathconf(fd, _PC_REC_INCR_XFER_SIZE);
1489 t->current_filesystem->max_xfer_size =
1490 fpathconf(fd, _PC_REC_MAX_XFER_SIZE);
1491 t->current_filesystem->min_xfer_size =
1492 fpathconf(fd, _PC_REC_MIN_XFER_SIZE);
1493 t->current_filesystem->xfer_align =
1494 fpathconf(fd, _PC_REC_XFER_ALIGN);
1495 } else if (path != NULL) {
1496 t->current_filesystem->incr_xfer_size =
1497 pathconf(path, _PC_REC_INCR_XFER_SIZE);
1498 t->current_filesystem->max_xfer_size =
1499 pathconf(path, _PC_REC_MAX_XFER_SIZE);
1500 t->current_filesystem->min_xfer_size =
1501 pathconf(path, _PC_REC_MIN_XFER_SIZE);
1502 t->current_filesystem->xfer_align =
1503 pathconf(path, _PC_REC_XFER_ALIGN);
1505 /* At least we need an alignment size. */
1506 if (t->current_filesystem->xfer_align == -1)
1507 return ((errno == EINVAL)?1:-1);
1513 get_xfer_size(struct tree *t, int fd, const char *path)
1515 (void)t; /* UNUSED */
1516 (void)fd; /* UNUSED */
1517 (void)path; /* UNUSED */
1518 return (1);/* Not supported */
1522 #if defined(HAVE_STATVFS)
1523 static inline __LA_UNUSED void
1524 set_statvfs_transfer_size(struct filesystem *fs, const struct statvfs *sfs)
1526 fs->xfer_align = sfs->f_frsize > 0 ? (long)sfs->f_frsize : -1;
1527 fs->max_xfer_size = -1;
1528 #if defined(HAVE_STRUCT_STATVFS_F_IOSIZE)
1529 fs->min_xfer_size = sfs->f_iosize > 0 ? (long)sfs->f_iosize : -1;
1530 fs->incr_xfer_size = sfs->f_iosize > 0 ? (long)sfs->f_iosize : -1;
1532 fs->min_xfer_size = sfs->f_bsize > 0 ? (long)sfs->f_bsize : -1;
1533 fs->incr_xfer_size = sfs->f_bsize > 0 ? (long)sfs->f_bsize : -1;
1538 #if defined(HAVE_STRUCT_STATFS)
1539 static inline __LA_UNUSED void
1540 set_statfs_transfer_size(struct filesystem *fs, const struct statfs *sfs)
1542 fs->xfer_align = sfs->f_bsize > 0 ? (long)sfs->f_bsize : -1;
1543 fs->max_xfer_size = -1;
1544 #if defined(HAVE_STRUCT_STATFS_F_IOSIZE)
1545 fs->min_xfer_size = sfs->f_iosize > 0 ? (long)sfs->f_iosize : -1;
1546 fs->incr_xfer_size = sfs->f_iosize > 0 ? (long)sfs->f_iosize : -1;
1548 fs->min_xfer_size = sfs->f_bsize > 0 ? (long)sfs->f_bsize : -1;
1549 fs->incr_xfer_size = sfs->f_bsize > 0 ? (long)sfs->f_bsize : -1;
1554 #if defined(HAVE_STRUCT_STATFS) && defined(HAVE_STATFS) && \
1555 defined(HAVE_FSTATFS) && defined(MNT_LOCAL) && !defined(ST_LOCAL)
1558 * Gather current filesystem properties on FreeBSD, OpenBSD and Mac OS X.
1561 setup_current_filesystem(struct archive_read_disk *a)
1563 struct tree *t = a->tree;
1565 #if defined(HAVE_GETVFSBYNAME) && defined(VFCF_SYNTHETIC)
1566 /* TODO: configure should set GETVFSBYNAME_ARG_TYPE to make
1567 * this accurate; some platforms have both and we need the one that's
1568 * used by getvfsbyname()
1570 * Then the following would become:
1571 * #if defined(GETVFSBYNAME_ARG_TYPE)
1572 * GETVFSBYNAME_ARG_TYPE vfc;
1575 # if defined(HAVE_STRUCT_XVFSCONF)
1576 struct xvfsconf vfc;
1582 #if !defined(HAVE_STRUCT_STATFS_F_NAMEMAX)
1586 t->current_filesystem->synthetic = -1;
1587 t->current_filesystem->remote = -1;
1588 if (tree_current_is_symblic_link_target(t)) {
1589 #if defined(HAVE_OPENAT)
1591 * Get file system statistics on any directory
1594 int fd = openat(tree_current_dir_fd(t),
1595 tree_current_access_path(t), O_RDONLY | O_CLOEXEC);
1596 __archive_ensure_cloexec_flag(fd);
1598 archive_set_error(&a->archive, errno,
1600 return (ARCHIVE_FAILED);
1602 r = fstatfs(fd, &sfs);
1604 xr = get_xfer_size(t, fd, NULL);
1607 if (tree_enter_working_dir(t) != 0) {
1608 archive_set_error(&a->archive, errno, "fchdir failed");
1609 return (ARCHIVE_FAILED);
1611 r = statfs(tree_current_access_path(t), &sfs);
1613 xr = get_xfer_size(t, -1, tree_current_access_path(t));
1616 r = fstatfs(tree_current_dir_fd(t), &sfs);
1618 xr = get_xfer_size(t, tree_current_dir_fd(t), NULL);
1620 if (r == -1 || xr == -1) {
1621 archive_set_error(&a->archive, errno, "statfs failed");
1622 return (ARCHIVE_FAILED);
1623 } else if (xr == 1) {
1624 /* pathconf(_PC_REX_*) operations are not supported. */
1625 set_statfs_transfer_size(t->current_filesystem, &sfs);
1627 if (sfs.f_flags & MNT_LOCAL)
1628 t->current_filesystem->remote = 0;
1630 t->current_filesystem->remote = 1;
1632 #if defined(HAVE_GETVFSBYNAME) && defined(VFCF_SYNTHETIC)
1633 r = getvfsbyname(sfs.f_fstypename, &vfc);
1635 archive_set_error(&a->archive, errno, "getvfsbyname failed");
1636 return (ARCHIVE_FAILED);
1638 if (vfc.vfc_flags & VFCF_SYNTHETIC)
1639 t->current_filesystem->synthetic = 1;
1641 t->current_filesystem->synthetic = 0;
1644 #if defined(MNT_NOATIME)
1645 if (sfs.f_flags & MNT_NOATIME)
1646 t->current_filesystem->noatime = 1;
1649 t->current_filesystem->noatime = 0;
1651 #if defined(USE_READDIR_R)
1652 /* Set maximum filename length. */
1653 #if defined(HAVE_STRUCT_STATFS_F_NAMEMAX)
1654 t->current_filesystem->name_max = sfs.f_namemax;
1656 # if defined(_PC_NAME_MAX)
1657 /* Mac OS X does not have f_namemax in struct statfs. */
1658 if (tree_current_is_symblic_link_target(t)) {
1659 if (tree_enter_working_dir(t) != 0) {
1660 archive_set_error(&a->archive, errno, "fchdir failed");
1661 return (ARCHIVE_FAILED);
1663 nm = pathconf(tree_current_access_path(t), _PC_NAME_MAX);
1665 nm = fpathconf(tree_current_dir_fd(t), _PC_NAME_MAX);
1670 t->current_filesystem->name_max = NAME_MAX;
1672 t->current_filesystem->name_max = nm;
1674 #endif /* USE_READDIR_R */
1675 return (ARCHIVE_OK);
1678 #elif (defined(HAVE_STATVFS) || defined(HAVE_FSTATVFS)) && defined(ST_LOCAL)
1681 * Gather current filesystem properties on NetBSD
1684 setup_current_filesystem(struct archive_read_disk *a)
1686 struct tree *t = a->tree;
1687 struct statvfs svfs;
1690 t->current_filesystem->synthetic = -1;
1691 if (tree_enter_working_dir(t) != 0) {
1692 archive_set_error(&a->archive, errno, "fchdir failed");
1693 return (ARCHIVE_FAILED);
1695 if (tree_current_is_symblic_link_target(t)) {
1696 r = statvfs(tree_current_access_path(t), &svfs);
1698 xr = get_xfer_size(t, -1, tree_current_access_path(t));
1700 #ifdef HAVE_FSTATVFS
1701 r = fstatvfs(tree_current_dir_fd(t), &svfs);
1703 xr = get_xfer_size(t, tree_current_dir_fd(t), NULL);
1705 r = statvfs(".", &svfs);
1707 xr = get_xfer_size(t, -1, ".");
1710 if (r == -1 || xr == -1) {
1711 t->current_filesystem->remote = -1;
1712 archive_set_error(&a->archive, errno, "statvfs failed");
1713 return (ARCHIVE_FAILED);
1714 } else if (xr == 1) {
1715 /* Usually come here unless NetBSD supports _PC_REC_XFER_ALIGN
1716 * for pathconf() function. */
1717 set_statvfs_transfer_size(t->current_filesystem, &svfs);
1719 if (svfs.f_flag & ST_LOCAL)
1720 t->current_filesystem->remote = 0;
1722 t->current_filesystem->remote = 1;
1724 #if defined(ST_NOATIME)
1725 if (svfs.f_flag & ST_NOATIME)
1726 t->current_filesystem->noatime = 1;
1729 t->current_filesystem->noatime = 0;
1731 /* Set maximum filename length. */
1732 t->current_filesystem->name_max = svfs.f_namemax;
1733 return (ARCHIVE_OK);
1736 #elif defined(HAVE_SYS_STATFS_H) && defined(HAVE_LINUX_MAGIC_H) &&\
1737 defined(HAVE_STATFS) && defined(HAVE_FSTATFS)
1739 * Note: statfs is deprecated since LSB 3.2
1742 #ifndef CIFS_SUPER_MAGIC
1743 #define CIFS_SUPER_MAGIC 0xFF534D42
1745 #ifndef DEVFS_SUPER_MAGIC
1746 #define DEVFS_SUPER_MAGIC 0x1373
1750 * Gather current filesystem properties on Linux
1753 setup_current_filesystem(struct archive_read_disk *a)
1755 struct tree *t = a->tree;
1757 #if defined(HAVE_STATVFS)
1758 struct statvfs svfs;
1760 int r, vr = 0, xr = 0;
1762 if (tree_current_is_symblic_link_target(t)) {
1763 #if defined(HAVE_OPENAT)
1765 * Get file system statistics on any directory
1768 int fd = openat(tree_current_dir_fd(t),
1769 tree_current_access_path(t), O_RDONLY | O_CLOEXEC);
1770 __archive_ensure_cloexec_flag(fd);
1772 archive_set_error(&a->archive, errno,
1774 return (ARCHIVE_FAILED);
1776 #if defined(HAVE_FSTATVFS)
1777 vr = fstatvfs(fd, &svfs);/* for f_flag, mount flags */
1779 r = fstatfs(fd, &sfs);
1781 xr = get_xfer_size(t, fd, NULL);
1784 if (tree_enter_working_dir(t) != 0) {
1785 archive_set_error(&a->archive, errno, "fchdir failed");
1786 return (ARCHIVE_FAILED);
1788 #if defined(HAVE_STATVFS)
1789 vr = statvfs(tree_current_access_path(t), &svfs);
1791 r = statfs(tree_current_access_path(t), &sfs);
1793 xr = get_xfer_size(t, -1, tree_current_access_path(t));
1797 #if defined(HAVE_FSTATVFS)
1798 vr = fstatvfs(tree_current_dir_fd(t), &svfs);
1800 r = fstatfs(tree_current_dir_fd(t), &sfs);
1802 xr = get_xfer_size(t, tree_current_dir_fd(t), NULL);
1804 if (tree_enter_working_dir(t) != 0) {
1805 archive_set_error(&a->archive, errno, "fchdir failed");
1806 return (ARCHIVE_FAILED);
1808 #if defined(HAVE_STATVFS)
1809 vr = statvfs(".", &svfs);
1811 r = statfs(".", &sfs);
1813 xr = get_xfer_size(t, -1, ".");
1816 if (r == -1 || xr == -1 || vr == -1) {
1817 t->current_filesystem->synthetic = -1;
1818 t->current_filesystem->remote = -1;
1819 archive_set_error(&a->archive, errno, "statfs failed");
1820 return (ARCHIVE_FAILED);
1821 } else if (xr == 1) {
1822 /* pathconf(_PC_REX_*) operations are not supported. */
1823 #if defined(HAVE_STATVFS)
1824 set_statvfs_transfer_size(t->current_filesystem, &svfs);
1826 set_statfs_transfer_size(t->current_filesystem, &sfs);
1829 switch (sfs.f_type) {
1830 case AFS_SUPER_MAGIC:
1831 case CIFS_SUPER_MAGIC:
1832 case CODA_SUPER_MAGIC:
1833 case NCP_SUPER_MAGIC:/* NetWare */
1834 case NFS_SUPER_MAGIC:
1835 case SMB_SUPER_MAGIC:
1836 t->current_filesystem->remote = 1;
1837 t->current_filesystem->synthetic = 0;
1839 case DEVFS_SUPER_MAGIC:
1840 case PROC_SUPER_MAGIC:
1841 case USBDEVICE_SUPER_MAGIC:
1842 t->current_filesystem->remote = 0;
1843 t->current_filesystem->synthetic = 1;
1846 t->current_filesystem->remote = 0;
1847 t->current_filesystem->synthetic = 0;
1851 #if defined(ST_NOATIME)
1852 #if defined(HAVE_STATVFS)
1853 if (svfs.f_flag & ST_NOATIME)
1855 if (sfs.f_flags & ST_NOATIME)
1857 t->current_filesystem->noatime = 1;
1860 t->current_filesystem->noatime = 0;
1862 #if defined(USE_READDIR_R)
1863 /* Set maximum filename length. */
1864 t->current_filesystem->name_max = sfs.f_namelen;
1866 return (ARCHIVE_OK);
1869 #elif defined(HAVE_SYS_STATVFS_H) &&\
1870 (defined(HAVE_STATVFS) || defined(HAVE_FSTATVFS))
1873 * Gather current filesystem properties on other posix platform.
1876 setup_current_filesystem(struct archive_read_disk *a)
1878 struct tree *t = a->tree;
1879 struct statvfs svfs;
1882 t->current_filesystem->synthetic = -1;/* Not supported */
1883 t->current_filesystem->remote = -1;/* Not supported */
1884 if (tree_current_is_symblic_link_target(t)) {
1885 #if defined(HAVE_OPENAT)
1887 * Get file system statistics on any directory
1890 int fd = openat(tree_current_dir_fd(t),
1891 tree_current_access_path(t), O_RDONLY | O_CLOEXEC);
1892 __archive_ensure_cloexec_flag(fd);
1894 archive_set_error(&a->archive, errno,
1896 return (ARCHIVE_FAILED);
1898 r = fstatvfs(fd, &svfs);
1900 xr = get_xfer_size(t, fd, NULL);
1903 if (tree_enter_working_dir(t) != 0) {
1904 archive_set_error(&a->archive, errno, "fchdir failed");
1905 return (ARCHIVE_FAILED);
1907 r = statvfs(tree_current_access_path(t), &svfs);
1909 xr = get_xfer_size(t, -1, tree_current_access_path(t));
1912 #ifdef HAVE_FSTATVFS
1913 r = fstatvfs(tree_current_dir_fd(t), &svfs);
1915 xr = get_xfer_size(t, tree_current_dir_fd(t), NULL);
1917 if (tree_enter_working_dir(t) != 0) {
1918 archive_set_error(&a->archive, errno, "fchdir failed");
1919 return (ARCHIVE_FAILED);
1921 r = statvfs(".", &svfs);
1923 xr = get_xfer_size(t, -1, ".");
1926 if (r == -1 || xr == -1) {
1927 t->current_filesystem->synthetic = -1;
1928 t->current_filesystem->remote = -1;
1929 archive_set_error(&a->archive, errno, "statvfs failed");
1930 return (ARCHIVE_FAILED);
1931 } else if (xr == 1) {
1932 /* pathconf(_PC_REX_*) operations are not supported. */
1933 set_statvfs_transfer_size(t->current_filesystem, &svfs);
1936 #if defined(ST_NOATIME)
1937 if (svfs.f_flag & ST_NOATIME)
1938 t->current_filesystem->noatime = 1;
1941 t->current_filesystem->noatime = 0;
1943 #if defined(USE_READDIR_R)
1944 /* Set maximum filename length. */
1945 t->current_filesystem->name_max = svfs.f_namemax;
1947 return (ARCHIVE_OK);
1953 * Generic: Gather current filesystem properties.
1954 * TODO: Is this generic function really needed?
1957 setup_current_filesystem(struct archive_read_disk *a)
1959 struct tree *t = a->tree;
1960 #if defined(_PC_NAME_MAX) && defined(USE_READDIR_R)
1963 t->current_filesystem->synthetic = -1;/* Not supported */
1964 t->current_filesystem->remote = -1;/* Not supported */
1965 t->current_filesystem->noatime = 0;
1966 (void)get_xfer_size(t, -1, ".");/* Dummy call to avoid build error. */
1967 t->current_filesystem->xfer_align = -1;/* Unknown */
1968 t->current_filesystem->max_xfer_size = -1;
1969 t->current_filesystem->min_xfer_size = -1;
1970 t->current_filesystem->incr_xfer_size = -1;
1972 #if defined(USE_READDIR_R)
1973 /* Set maximum filename length. */
1974 # if defined(_PC_NAME_MAX)
1975 if (tree_current_is_symblic_link_target(t)) {
1976 if (tree_enter_working_dir(t) != 0) {
1977 archive_set_error(&a->archive, errno, "fchdir failed");
1978 return (ARCHIVE_FAILED);
1980 nm = pathconf(tree_current_access_path(t), _PC_NAME_MAX);
1982 nm = fpathconf(tree_current_dir_fd(t), _PC_NAME_MAX);
1984 # endif /* _PC_NAME_MAX */
1986 * Some systems (HP-UX or others?) incorrectly defined
1987 * NAME_MAX macro to be a smaller value.
1989 # if defined(NAME_MAX) && NAME_MAX >= 255
1990 t->current_filesystem->name_max = NAME_MAX;
1992 /* No way to get a trusted value of maximum filename
1994 t->current_filesystem->name_max = PATH_MAX;
1995 # endif /* NAME_MAX */
1996 # if defined(_PC_NAME_MAX)
1998 t->current_filesystem->name_max = nm;
1999 # endif /* _PC_NAME_MAX */
2000 #endif /* USE_READDIR_R */
2001 return (ARCHIVE_OK);
2007 close_and_restore_time(int fd, struct tree *t, struct restore_time *rt)
2010 (void)t; /* UNUSED */
2011 (void)rt; /* UNUSED */
2014 #if defined(HAVE_FUTIMENS) && !defined(__CYGWIN__)
2015 struct timespec timespecs[2];
2017 struct timeval times[2];
2019 if ((t->flags & needsRestoreTimes) == 0 || rt->noatime) {
2026 #if defined(HAVE_FUTIMENS) && !defined(__CYGWIN__)
2027 if (rt->mtime == (time_t)-1) {
2028 timespecs[1].tv_sec = 0;
2029 timespecs[1].tv_nsec = UTIME_OMIT;
2031 timespecs[1].tv_sec = rt->mtime;
2032 timespecs[1].tv_nsec = rt->mtime_nsec;
2035 if (rt->atime == (time_t)-1) {
2036 timespecs[0].tv_sec = 0;
2037 timespecs[0].tv_nsec = UTIME_OMIT;
2039 timespecs[0].tv_sec = rt->atime;
2040 timespecs[0].tv_nsec = rt->atime_nsec;
2042 /* futimens() is defined in POSIX.1-2008. */
2043 if (futimens(fd, timespecs) == 0)
2047 times[1].tv_sec = rt->mtime;
2048 times[1].tv_usec = rt->mtime_nsec / 1000;
2050 times[0].tv_sec = rt->atime;
2051 times[0].tv_usec = rt->atime_nsec / 1000;
2053 #if !defined(HAVE_FUTIMENS) && defined(HAVE_FUTIMES) && !defined(__CYGWIN__)
2054 if (futimes(fd, times) == 0)
2058 #if defined(HAVE_FUTIMESAT)
2059 if (futimesat(tree_current_dir_fd(t), rt->name, times) == 0)
2063 if (lutimes(rt->name, times) != 0)
2065 if (AE_IFLNK != rt->filetype && utimes(rt->name, times) != 0)
2073 open_on_current_dir(struct tree *t, const char *path, int flags)
2076 return (openat(tree_current_dir_fd(t), path, flags));
2078 if (tree_enter_working_dir(t) != 0)
2080 return (open(path, flags));
2088 #ifdef F_DUPFD_CLOEXEC
2089 static volatile int can_dupfd_cloexec = 1;
2091 if (can_dupfd_cloexec) {
2092 new_fd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
2095 /* Linux 2.6.18 - 2.6.23 declare F_DUPFD_CLOEXEC,
2096 * but it cannot be used. So we have to try dup(). */
2097 /* We won't try F_DUPFD_CLOEXEC. */
2098 can_dupfd_cloexec = 0;
2100 #endif /* F_DUPFD_CLOEXEC */
2102 __archive_ensure_cloexec_flag(new_fd);
2107 * Add a directory path to the current stack.
2110 tree_push(struct tree *t, const char *path, int filesystem_id,
2111 int64_t dev, int64_t ino, struct restore_time *rt)
2113 struct tree_entry *te;
2115 te = calloc(1, sizeof(*te));
2116 te->next = t->stack;
2117 te->parent = t->current;
2119 te->depth = te->parent->depth + 1;
2121 archive_string_init(&te->name);
2122 te->symlink_parent_fd = -1;
2123 archive_strcpy(&te->name, path);
2124 te->flags = needsDescent | needsOpen | needsAscent;
2125 te->filesystem_id = filesystem_id;
2128 te->dirname_length = t->dirname_length;
2129 te->restore_time.name = te->name.s;
2131 te->restore_time.mtime = rt->mtime;
2132 te->restore_time.mtime_nsec = rt->mtime_nsec;
2133 te->restore_time.atime = rt->atime;
2134 te->restore_time.atime_nsec = rt->atime_nsec;
2135 te->restore_time.filetype = rt->filetype;
2136 te->restore_time.noatime = rt->noatime;
2141 * Append a name to the current dir path.
2144 tree_append(struct tree *t, const char *name, size_t name_length)
2148 t->path.s[t->dirname_length] = '\0';
2149 t->path.length = t->dirname_length;
2150 /* Strip trailing '/' from name, unless entire name is "/". */
2151 while (name_length > 1 && name[name_length - 1] == '/')
2154 /* Resize pathname buffer as needed. */
2155 size_needed = name_length + t->dirname_length + 2;
2156 archive_string_ensure(&t->path, size_needed);
2157 /* Add a separating '/' if it's needed. */
2158 if (t->dirname_length > 0 && t->path.s[archive_strlen(&t->path)-1] != '/')
2159 archive_strappend_char(&t->path, '/');
2160 t->basename = t->path.s + archive_strlen(&t->path);
2161 archive_strncat(&t->path, name, name_length);
2162 t->restore_time.name = t->basename;
2166 * Open a directory tree for traversal.
2168 static struct tree *
2169 tree_open(const char *path, int symlink_mode, int restore_time)
2173 if ((t = calloc(1, sizeof(*t))) == NULL)
2175 archive_string_init(&t->path);
2176 archive_string_ensure(&t->path, 31);
2177 t->initial_symlink_mode = symlink_mode;
2178 return (tree_reopen(t, path, restore_time));
2181 static struct tree *
2182 tree_reopen(struct tree *t, const char *path, int restore_time)
2186 const int o_flag = O_PATH;
2187 #elif defined(O_SEARCH)
2189 const int o_flag = O_SEARCH;
2190 #elif defined(__FreeBSD__) && defined(O_EXEC)
2192 const int o_flag = O_EXEC;
2195 t->flags = (restore_time != 0)?needsRestoreTimes:0;
2196 t->flags |= onInitialDir;
2199 t->dirname_length = 0;
2203 t->d = INVALID_DIR_HANDLE;
2204 t->symlink_mode = t->initial_symlink_mode;
2205 archive_string_empty(&t->path);
2208 t->entry_remaining_bytes = 0;
2209 t->initial_filesystem_id = -1;
2211 /* First item is set up a lot like a symlink traversal. */
2212 tree_push(t, path, 0, 0, 0, NULL);
2213 t->stack->flags = needsFirstVisit;
2214 t->maxOpenCount = t->openCount = 1;
2215 t->initial_dir_fd = open(".", O_RDONLY | O_CLOEXEC);
2216 #if defined(O_PATH) || defined(O_SEARCH) || \
2217 (defined(__FreeBSD__) && defined(O_EXEC))
2219 * Most likely reason to fail opening "." is that it's not readable,
2220 * so try again for execute. The consequences of not opening this are
2221 * unhelpful and unnecessary errors later.
2223 if (t->initial_dir_fd < 0)
2224 t->initial_dir_fd = open(".", o_flag | O_CLOEXEC);
2226 __archive_ensure_cloexec_flag(t->initial_dir_fd);
2227 t->working_dir_fd = tree_dup(t->initial_dir_fd);
2232 tree_descent(struct tree *t)
2234 int flag, new_fd, r = 0;
2236 t->dirname_length = archive_strlen(&t->path);
2237 flag = O_RDONLY | O_CLOEXEC;
2238 #if defined(O_DIRECTORY)
2239 flag |= O_DIRECTORY;
2241 new_fd = open_on_current_dir(t, t->stack->name.s, flag);
2242 __archive_ensure_cloexec_flag(new_fd);
2244 t->tree_errno = errno;
2248 /* If it is a link, set up fd for the ascent. */
2249 if (t->stack->flags & isDirLink) {
2250 t->stack->symlink_parent_fd = t->working_dir_fd;
2252 if (t->openCount > t->maxOpenCount)
2253 t->maxOpenCount = t->openCount;
2255 close(t->working_dir_fd);
2256 /* Renew the current working directory. */
2257 t->working_dir_fd = new_fd;
2258 t->flags &= ~onWorkingDir;
2264 * We've finished a directory; ascend back to the parent.
2267 tree_ascend(struct tree *t)
2269 struct tree_entry *te;
2270 int new_fd, r = 0, prev_dir_fd;
2273 prev_dir_fd = t->working_dir_fd;
2274 if (te->flags & isDirLink)
2275 new_fd = te->symlink_parent_fd;
2277 new_fd = open_on_current_dir(t, "..", O_RDONLY | O_CLOEXEC);
2278 __archive_ensure_cloexec_flag(new_fd);
2281 t->tree_errno = errno;
2282 r = TREE_ERROR_FATAL;
2284 /* Renew the current working directory. */
2285 t->working_dir_fd = new_fd;
2286 t->flags &= ~onWorkingDir;
2287 /* Current directory has been changed, we should
2288 * close an fd of previous working directory. */
2289 close_and_restore_time(prev_dir_fd, t, &te->restore_time);
2290 if (te->flags & isDirLink) {
2292 te->symlink_parent_fd = -1;
2300 * Return to the initial directory where tree_open() was performed.
2303 tree_enter_initial_dir(struct tree *t)
2307 if ((t->flags & onInitialDir) == 0) {
2308 r = fchdir(t->initial_dir_fd);
2310 t->flags &= ~onWorkingDir;
2311 t->flags |= onInitialDir;
2318 * Restore working directory of directory traversals.
2321 tree_enter_working_dir(struct tree *t)
2326 * Change the current directory if really needed.
2327 * Sometimes this is unneeded when we did not do
2330 if (t->depth > 0 && (t->flags & onWorkingDir) == 0) {
2331 r = fchdir(t->working_dir_fd);
2333 t->flags &= ~onInitialDir;
2334 t->flags |= onWorkingDir;
2341 tree_current_dir_fd(struct tree *t)
2343 return (t->working_dir_fd);
2347 * Pop the working stack.
2350 tree_pop(struct tree *t)
2352 struct tree_entry *te;
2354 t->path.s[t->dirname_length] = '\0';
2355 t->path.length = t->dirname_length;
2356 if (t->stack == t->current && t->current != NULL)
2357 t->current = t->current->parent;
2359 t->stack = te->next;
2360 t->dirname_length = te->dirname_length;
2361 t->basename = t->path.s + t->dirname_length;
2362 while (t->basename[0] == '/')
2364 archive_string_free(&te->name);
2369 * Get the next item in the tree traversal.
2372 tree_next(struct tree *t)
2376 while (t->stack != NULL) {
2377 /* If there's an open dir, get the next entry from there. */
2378 if (t->d != INVALID_DIR_HANDLE) {
2379 r = tree_dir_next_posix(t);
2385 if (t->stack->flags & needsFirstVisit) {
2386 /* Top stack item needs a regular visit. */
2387 t->current = t->stack;
2388 tree_append(t, t->stack->name.s,
2389 archive_strlen(&(t->stack->name)));
2390 /* t->dirname_length = t->path_length; */
2392 t->stack->flags &= ~needsFirstVisit;
2393 return (t->visit_type = TREE_REGULAR);
2394 } else if (t->stack->flags & needsDescent) {
2395 /* Top stack item is dir to descend into. */
2396 t->current = t->stack;
2397 tree_append(t, t->stack->name.s,
2398 archive_strlen(&(t->stack->name)));
2399 t->stack->flags &= ~needsDescent;
2400 r = tree_descent(t);
2405 t->visit_type = TREE_POSTDESCENT;
2406 return (t->visit_type);
2407 } else if (t->stack->flags & needsOpen) {
2408 t->stack->flags &= ~needsOpen;
2409 r = tree_dir_next_posix(t);
2413 } else if (t->stack->flags & needsAscent) {
2414 /* Top stack item is dir and we're done with it. */
2417 t->visit_type = r != 0 ? r : TREE_POSTASCENT;
2418 return (t->visit_type);
2420 /* Top item on stack is dead. */
2422 t->flags &= ~hasLstat;
2423 t->flags &= ~hasStat;
2426 return (t->visit_type = 0);
2430 tree_dir_next_posix(struct tree *t)
2437 #if defined(USE_READDIR_R)
2441 #if defined(HAVE_FDOPENDIR)
2442 t->d = fdopendir(tree_dup(t->working_dir_fd));
2443 #else /* HAVE_FDOPENDIR */
2444 if (tree_enter_working_dir(t) == 0) {
2445 t->d = opendir(".");
2447 __archive_ensure_cloexec_flag(dirfd(t->d));
2450 #endif /* HAVE_FDOPENDIR */
2452 r = tree_ascend(t); /* Undo "chdir" */
2454 t->tree_errno = errno;
2455 t->visit_type = r != 0 ? r : TREE_ERROR_DIR;
2456 return (t->visit_type);
2458 #if defined(USE_READDIR_R)
2459 dirent_size = offsetof(struct dirent, d_name) +
2460 t->filesystem_table[t->current->filesystem_id].name_max + 1;
2461 if (t->dirent == NULL || t->dirent_allocated < dirent_size) {
2463 t->dirent = malloc(dirent_size);
2464 if (t->dirent == NULL) {
2466 t->d = INVALID_DIR_HANDLE;
2467 (void)tree_ascend(t);
2469 t->tree_errno = ENOMEM;
2470 t->visit_type = TREE_ERROR_DIR;
2471 return (t->visit_type);
2473 t->dirent_allocated = dirent_size;
2475 #endif /* USE_READDIR_R */
2479 #if defined(USE_READDIR_R)
2480 r = readdir_r(t->d, t->dirent, &t->de);
2482 /* Note: According to the man page, return value 9 indicates
2483 * that the readdir_r was not successful and the error code
2484 * is set to the global errno variable. And then if the end
2485 * of directory entries was reached, the return value is 9
2486 * and the third parameter is set to NULL and errno is
2491 if (r != 0 || t->de == NULL) {
2493 t->de = readdir(t->d);
2494 if (t->de == NULL) {
2498 t->d = INVALID_DIR_HANDLE;
2501 t->visit_type = TREE_ERROR_DIR;
2502 return (t->visit_type);
2506 name = t->de->d_name;
2507 namelen = D_NAMELEN(t->de);
2508 t->flags &= ~hasLstat;
2509 t->flags &= ~hasStat;
2510 if (name[0] == '.' && name[1] == '\0')
2512 if (name[0] == '.' && name[1] == '.' && name[2] == '\0')
2514 tree_append(t, name, namelen);
2515 return (t->visit_type = TREE_REGULAR);
2521 * Get the stat() data for the entry just returned from tree_next().
2523 static const struct stat *
2524 tree_current_stat(struct tree *t)
2526 if (!(t->flags & hasStat)) {
2528 if (fstatat(tree_current_dir_fd(t),
2529 tree_current_access_path(t), &t->st, 0) != 0)
2531 if (tree_enter_working_dir(t) != 0)
2533 if (la_stat(tree_current_access_path(t), &t->st) != 0)
2536 t->flags |= hasStat;
2542 * Get the lstat() data for the entry just returned from tree_next().
2544 static const struct stat *
2545 tree_current_lstat(struct tree *t)
2547 if (!(t->flags & hasLstat)) {
2549 if (fstatat(tree_current_dir_fd(t),
2550 tree_current_access_path(t), &t->lst,
2551 AT_SYMLINK_NOFOLLOW) != 0)
2553 if (tree_enter_working_dir(t) != 0)
2555 if (lstat(tree_current_access_path(t), &t->lst) != 0)
2558 t->flags |= hasLstat;
2564 * Test whether current entry is a dir or link to a dir.
2567 tree_current_is_dir(struct tree *t)
2569 const struct stat *st;
2571 * If we already have lstat() info, then try some
2572 * cheap tests to determine if this is a dir.
2574 if (t->flags & hasLstat) {
2575 /* If lstat() says it's a dir, it must be a dir. */
2576 st = tree_current_lstat(t);
2579 if (S_ISDIR(st->st_mode))
2581 /* Not a dir; might be a link to a dir. */
2582 /* If it's not a link, then it's not a link to a dir. */
2583 if (!S_ISLNK(st->st_mode))
2586 * It's a link, but we don't know what it's a link to,
2587 * so we'll have to use stat().
2591 st = tree_current_stat(t);
2592 /* If we can't stat it, it's not a dir. */
2595 /* Use the definitive test. Hopefully this is cached. */
2596 return (S_ISDIR(st->st_mode));
2600 * Test whether current entry is a physical directory. Usually, we
2601 * already have at least one of stat() or lstat() in memory, so we
2602 * use tricks to try to avoid an extra trip to the disk.
2605 tree_current_is_physical_dir(struct tree *t)
2607 const struct stat *st;
2610 * If stat() says it isn't a dir, then it's not a dir.
2611 * If stat() data is cached, this check is free, so do it first.
2613 if (t->flags & hasStat) {
2614 st = tree_current_stat(t);
2617 if (!S_ISDIR(st->st_mode))
2622 * Either stat() said it was a dir (in which case, we have
2623 * to determine whether it's really a link to a dir) or
2624 * stat() info wasn't available. So we use lstat(), which
2625 * hopefully is already cached.
2628 st = tree_current_lstat(t);
2629 /* If we can't stat it, it's not a dir. */
2632 /* Use the definitive test. Hopefully this is cached. */
2633 return (S_ISDIR(st->st_mode));
2637 * Test whether the same file has been in the tree as its parent.
2640 tree_target_is_same_as_parent(struct tree *t, const struct stat *st)
2642 struct tree_entry *te;
2644 for (te = t->current->parent; te != NULL; te = te->parent) {
2645 if (te->dev == (int64_t)st->st_dev &&
2646 te->ino == (int64_t)st->st_ino)
2653 * Test whether the current file is symbolic link target and
2654 * on the other filesystem.
2657 tree_current_is_symblic_link_target(struct tree *t)
2659 static const struct stat *lst, *st;
2661 lst = tree_current_lstat(t);
2662 st = tree_current_stat(t);
2663 return (st != NULL && lst != NULL &&
2664 (int64_t)st->st_dev == t->current_filesystem->dev &&
2665 st->st_dev != lst->st_dev);
2669 * Return the access path for the entry just returned from tree_next().
2672 tree_current_access_path(struct tree *t)
2674 return (t->basename);
2678 * Return the full path for the entry just returned from tree_next().
2681 tree_current_path(struct tree *t)
2687 * Terminate the traversal.
2690 tree_close(struct tree *t)
2695 if (t->entry_fd >= 0) {
2696 close_and_restore_time(t->entry_fd, t, &t->restore_time);
2699 /* Close the handle of readdir(). */
2700 if (t->d != INVALID_DIR_HANDLE) {
2702 t->d = INVALID_DIR_HANDLE;
2704 /* Release anything remaining in the stack. */
2705 while (t->stack != NULL) {
2706 if (t->stack->flags & isDirLink)
2707 close(t->stack->symlink_parent_fd);
2710 if (t->working_dir_fd >= 0) {
2711 close(t->working_dir_fd);
2712 t->working_dir_fd = -1;
2714 if (t->initial_dir_fd >= 0) {
2715 close(t->initial_dir_fd);
2716 t->initial_dir_fd = -1;
2721 * Release any resources.
2724 tree_free(struct tree *t)
2730 archive_string_free(&t->path);
2731 #if defined(USE_READDIR_R)
2734 free(t->sparse_list);
2735 for (i = 0; i < t->max_filesystem_id; i++)
2736 free(t->filesystem_table[i].allocation_ptr);
2737 free(t->filesystem_table);