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.
110 * This is a new directory-walking system that addresses a number
111 * of problems I've had with fts(3). In particular, it has no
112 * pathname-length limits (other than the size of 'int'), handles
113 * deep logical traversals, uses considerably less memory, and has
114 * an opaque interface (easier to modify in the future).
116 * Internally, it keeps a single list of "tree_entry" items that
117 * represent filesystem objects that require further attention.
118 * Non-directories are not kept in memory: they are pulled from
119 * readdir(), returned to the client, then freed as soon as possible.
120 * Any directory entry to be traversed gets pushed onto the stack.
122 * There is surprisingly little information that needs to be kept for
123 * each item on the stack. Just the name, depth (represented here as the
124 * string length of the parent directory's pathname), and some markers
125 * indicating how to get back to the parent (via chdir("..") for a
126 * regular dir or via fchdir(2) for a symlink).
131 * 3) Arbitrary logical traversals by closing/reopening intermediate fds.
134 struct restore_time {
146 struct tree_entry *next;
147 struct tree_entry *parent;
148 struct archive_string name;
149 size_t dirname_length;
154 /* How to return back to the parent of a symlink. */
155 int symlink_parent_fd;
156 /* How to restore time of a directory. */
157 struct restore_time restore_time;
165 #if defined(HAVE_READDIR_R)
174 * Buffer used for reading file contents.
176 /* Exactly allocated memory pointer. */
177 unsigned char *allocation_ptr;
178 /* Pointer adjusted to the filesystem alignment . */
183 /* Definitions for tree_entry.flags bitmap. */
184 #define isDir 1 /* This entry is a regular directory. */
185 #define isDirLink 2 /* This entry is a symbolic link to a directory. */
186 #define needsFirstVisit 4 /* This is an initial entry. */
187 #define needsDescent 8 /* This entry needs to be previsited. */
188 #define needsOpen 16 /* This is a directory that needs to be opened. */
189 #define needsAscent 32 /* This entry needs to be postvisited. */
192 * Local data for this package.
195 struct tree_entry *stack;
196 struct tree_entry *current;
198 #define INVALID_DIR_HANDLE NULL
200 #if defined(HAVE_READDIR_R)
201 struct dirent *dirent;
202 size_t dirent_allocated;
206 /* Error code from last failed operation. */
209 /* Dynamically-sized buffer for holding path */
210 struct archive_string path;
212 /* Last path element */
213 const char *basename;
214 /* Leading dir length */
215 size_t dirname_length;
227 /* How to restore time of a file. */
228 struct restore_time restore_time;
230 struct entry_sparse {
233 } *sparse_list, *current_sparse;
235 int sparse_list_size;
237 char initial_symlink_mode;
239 struct filesystem *current_filesystem;
240 struct filesystem *filesystem_table;
241 int initial_filesystem_id;
242 int current_filesystem_id;
243 int max_filesystem_id;
244 int allocated_filesytem;
248 int64_t entry_remaining_bytes;
250 unsigned char *entry_buff;
251 size_t entry_buff_size;
254 /* Definitions for tree.flags bitmap. */
255 #define hasStat 16 /* The st entry is valid. */
256 #define hasLstat 32 /* The lst entry is valid. */
257 #define onWorkingDir 64 /* We are on the working dir where we are
258 * reading directory entry at this time. */
259 #define needsRestoreTimes 128
260 #define onInitialDir 256 /* We are on the initial dir. */
263 tree_dir_next_posix(struct tree *t);
265 #ifdef HAVE_DIRENT_D_NAMLEN
266 /* BSD extension; avoids need for a strlen() call. */
267 #define D_NAMELEN(dp) (dp)->d_namlen
269 #define D_NAMELEN(dp) (strlen((dp)->d_name))
272 /* Initiate/terminate a tree traversal. */
273 static struct tree *tree_open(const char *, int, int);
274 static struct tree *tree_reopen(struct tree *, const char *, int);
275 static void tree_close(struct tree *);
276 static void tree_free(struct tree *);
277 static void tree_push(struct tree *, const char *, int, int64_t, int64_t,
278 struct restore_time *);
279 static int tree_enter_initial_dir(struct tree *);
280 static int tree_enter_working_dir(struct tree *);
281 static int tree_current_dir_fd(struct tree *);
284 * tree_next() returns Zero if there is no next entry, non-zero if
285 * there is. Note that directories are visited three times.
286 * Directories are always visited first as part of enumerating their
287 * parent; that is a "regular" visit. If tree_descend() is invoked at
288 * that time, the directory is added to a work list and will
289 * subsequently be visited two more times: once just after descending
290 * into the directory ("postdescent") and again just after ascending
291 * back to the parent ("postascent").
293 * TREE_ERROR_DIR is returned if the descent failed (because the
294 * directory couldn't be opened, for instance). This is returned
295 * instead of TREE_POSTDESCENT/TREE_POSTASCENT. TREE_ERROR_DIR is not a
296 * fatal error, but it does imply that the relevant subtree won't be
297 * visited. TREE_ERROR_FATAL is returned for an error that left the
298 * traversal completely hosed. Right now, this is only returned for
299 * chdir() failures during ascent.
301 #define TREE_REGULAR 1
302 #define TREE_POSTDESCENT 2
303 #define TREE_POSTASCENT 3
304 #define TREE_ERROR_DIR -1
305 #define TREE_ERROR_FATAL -2
307 static int tree_next(struct tree *);
310 * Return information about the current entry.
314 * The current full pathname, length of the full pathname, and a name
315 * that can be used to access the file. Because tree does use chdir
316 * extensively, the access path is almost never the same as the full
319 * TODO: On platforms that support it, use openat()-style operations
320 * to eliminate the chdir() operations entirely while still supporting
321 * arbitrarily deep traversals. This makes access_path troublesome to
322 * support, of course, which means we'll need a rich enough interface
323 * that clients can function without it. (In particular, we'll need
324 * tree_current_open() that returns an open file descriptor.)
327 static const char *tree_current_path(struct tree *);
328 static const char *tree_current_access_path(struct tree *);
331 * Request the lstat() or stat() data for the current path. Since the
332 * tree package needs to do some of this anyway, and caches the
333 * results, you should take advantage of it here if you need it rather
334 * than make a redundant stat() or lstat() call of your own.
336 static const struct stat *tree_current_stat(struct tree *);
337 static const struct stat *tree_current_lstat(struct tree *);
338 static int tree_current_is_symblic_link_target(struct tree *);
340 /* The following functions use tricks to avoid a certain number of
341 * stat()/lstat() calls. */
342 /* "is_physical_dir" is equivalent to S_ISDIR(tree_current_lstat()->st_mode) */
343 static int tree_current_is_physical_dir(struct tree *);
344 /* "is_dir" is equivalent to S_ISDIR(tree_current_stat()->st_mode) */
345 static int tree_current_is_dir(struct tree *);
346 static int update_current_filesystem(struct archive_read_disk *a,
348 static int setup_current_filesystem(struct archive_read_disk *);
349 static int tree_target_is_same_as_parent(struct tree *, const struct stat *);
351 static int _archive_read_disk_open(struct archive *, const char *);
352 static int _archive_read_free(struct archive *);
353 static int _archive_read_close(struct archive *);
354 static int _archive_read_data_block(struct archive *,
355 const void **, size_t *, int64_t *);
356 static int _archive_read_next_header2(struct archive *,
357 struct archive_entry *);
358 static const char *trivial_lookup_gname(void *, int64_t gid);
359 static const char *trivial_lookup_uname(void *, int64_t uid);
360 static int setup_sparse(struct archive_read_disk *, struct archive_entry *);
361 static int close_and_restore_time(int fd, struct tree *,
362 struct restore_time *);
363 static int open_on_current_dir(struct tree *, const char *, int);
366 static struct archive_vtable *
367 archive_read_disk_vtable(void)
369 static struct archive_vtable av;
370 static int inited = 0;
373 av.archive_free = _archive_read_free;
374 av.archive_close = _archive_read_close;
375 av.archive_read_data_block = _archive_read_data_block;
376 av.archive_read_next_header2 = _archive_read_next_header2;
383 archive_read_disk_gname(struct archive *_a, int64_t gid)
385 struct archive_read_disk *a = (struct archive_read_disk *)_a;
386 if (ARCHIVE_OK != __archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
387 ARCHIVE_STATE_ANY, "archive_read_disk_gname"))
389 if (a->lookup_gname == NULL)
391 return ((*a->lookup_gname)(a->lookup_gname_data, gid));
395 archive_read_disk_uname(struct archive *_a, int64_t uid)
397 struct archive_read_disk *a = (struct archive_read_disk *)_a;
398 if (ARCHIVE_OK != __archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
399 ARCHIVE_STATE_ANY, "archive_read_disk_uname"))
401 if (a->lookup_uname == NULL)
403 return ((*a->lookup_uname)(a->lookup_uname_data, uid));
407 archive_read_disk_set_gname_lookup(struct archive *_a,
409 const char * (*lookup_gname)(void *private, int64_t gid),
410 void (*cleanup_gname)(void *private))
412 struct archive_read_disk *a = (struct archive_read_disk *)_a;
413 archive_check_magic(&a->archive, ARCHIVE_READ_DISK_MAGIC,
414 ARCHIVE_STATE_ANY, "archive_read_disk_set_gname_lookup");
416 if (a->cleanup_gname != NULL && a->lookup_gname_data != NULL)
417 (a->cleanup_gname)(a->lookup_gname_data);
419 a->lookup_gname = lookup_gname;
420 a->cleanup_gname = cleanup_gname;
421 a->lookup_gname_data = private_data;
426 archive_read_disk_set_uname_lookup(struct archive *_a,
428 const char * (*lookup_uname)(void *private, int64_t uid),
429 void (*cleanup_uname)(void *private))
431 struct archive_read_disk *a = (struct archive_read_disk *)_a;
432 archive_check_magic(&a->archive, ARCHIVE_READ_DISK_MAGIC,
433 ARCHIVE_STATE_ANY, "archive_read_disk_set_uname_lookup");
435 if (a->cleanup_uname != NULL && a->lookup_uname_data != NULL)
436 (a->cleanup_uname)(a->lookup_uname_data);
438 a->lookup_uname = lookup_uname;
439 a->cleanup_uname = cleanup_uname;
440 a->lookup_uname_data = private_data;
445 * Create a new archive_read_disk object and initialize it with global state.
448 archive_read_disk_new(void)
450 struct archive_read_disk *a;
452 a = (struct archive_read_disk *)calloc(1, sizeof(*a));
455 a->archive.magic = ARCHIVE_READ_DISK_MAGIC;
456 a->archive.state = ARCHIVE_STATE_NEW;
457 a->archive.vtable = archive_read_disk_vtable();
458 a->lookup_uname = trivial_lookup_uname;
459 a->lookup_gname = trivial_lookup_gname;
460 a->enable_copyfile = 1;
461 a->traverse_mount_points = 1;
462 a->open_on_current_dir = open_on_current_dir;
463 a->tree_current_dir_fd = tree_current_dir_fd;
464 a->tree_enter_working_dir = tree_enter_working_dir;
465 return (&a->archive);
469 _archive_read_free(struct archive *_a)
471 struct archive_read_disk *a = (struct archive_read_disk *)_a;
476 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
477 ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_free");
479 if (a->archive.state != ARCHIVE_STATE_CLOSED)
480 r = _archive_read_close(&a->archive);
485 if (a->cleanup_gname != NULL && a->lookup_gname_data != NULL)
486 (a->cleanup_gname)(a->lookup_gname_data);
487 if (a->cleanup_uname != NULL && a->lookup_uname_data != NULL)
488 (a->cleanup_uname)(a->lookup_uname_data);
489 archive_string_free(&a->archive.error_string);
490 a->archive.magic = 0;
491 __archive_clean(&a->archive);
497 _archive_read_close(struct archive *_a)
499 struct archive_read_disk *a = (struct archive_read_disk *)_a;
501 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
502 ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_close");
504 if (a->archive.state != ARCHIVE_STATE_FATAL)
505 a->archive.state = ARCHIVE_STATE_CLOSED;
513 setup_symlink_mode(struct archive_read_disk *a, char symlink_mode,
516 a->symlink_mode = symlink_mode;
517 a->follow_symlinks = follow_symlinks;
518 if (a->tree != NULL) {
519 a->tree->initial_symlink_mode = a->symlink_mode;
520 a->tree->symlink_mode = a->symlink_mode;
525 archive_read_disk_set_symlink_logical(struct archive *_a)
527 struct archive_read_disk *a = (struct archive_read_disk *)_a;
528 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
529 ARCHIVE_STATE_ANY, "archive_read_disk_set_symlink_logical");
530 setup_symlink_mode(a, 'L', 1);
535 archive_read_disk_set_symlink_physical(struct archive *_a)
537 struct archive_read_disk *a = (struct archive_read_disk *)_a;
538 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
539 ARCHIVE_STATE_ANY, "archive_read_disk_set_symlink_physical");
540 setup_symlink_mode(a, 'P', 0);
545 archive_read_disk_set_symlink_hybrid(struct archive *_a)
547 struct archive_read_disk *a = (struct archive_read_disk *)_a;
548 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
549 ARCHIVE_STATE_ANY, "archive_read_disk_set_symlink_hybrid");
550 setup_symlink_mode(a, 'H', 1);/* Follow symlinks initially. */
555 archive_read_disk_set_atime_restored(struct archive *_a)
558 static int warning_done = 0;
560 struct archive_read_disk *a = (struct archive_read_disk *)_a;
561 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
562 ARCHIVE_STATE_ANY, "archive_read_disk_restore_atime");
566 a->tree->flags |= needsRestoreTimes;
570 /* Warning was already emitted; suppress further warnings. */
573 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
574 "Cannot restore access time on this system");
576 return (ARCHIVE_WARN);
581 archive_read_disk_set_behavior(struct archive *_a, int flags)
583 struct archive_read_disk *a = (struct archive_read_disk *)_a;
586 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
587 ARCHIVE_STATE_ANY, "archive_read_disk_honor_nodump");
589 if (flags & ARCHIVE_READDISK_RESTORE_ATIME)
590 r = archive_read_disk_set_atime_restored(_a);
594 a->tree->flags &= ~needsRestoreTimes;
596 if (flags & ARCHIVE_READDISK_HONOR_NODUMP)
600 if (flags & ARCHIVE_READDISK_MAC_COPYFILE)
601 a->enable_copyfile = 1;
603 a->enable_copyfile = 0;
604 if (flags & ARCHIVE_READDISK_NO_TRAVERSE_MOUNTS)
605 a->traverse_mount_points = 0;
607 a->traverse_mount_points = 1;
612 * Trivial implementations of gname/uname lookup functions.
613 * These are normally overridden by the client, but these stub
614 * versions ensure that we always have something that works.
617 trivial_lookup_gname(void *private_data, int64_t gid)
619 (void)private_data; /* UNUSED */
620 (void)gid; /* UNUSED */
625 trivial_lookup_uname(void *private_data, int64_t uid)
627 (void)private_data; /* UNUSED */
628 (void)uid; /* UNUSED */
633 * Allocate memory for the reading buffer adjusted to the filesystem
637 setup_suitable_read_buffer(struct archive_read_disk *a)
639 struct tree *t = a->tree;
640 struct filesystem *cf = t->current_filesystem;
644 if (cf->allocation_ptr == NULL) {
645 /* If we couldn't get a filesystem alignment,
646 * we use 4096 as default value but we won't use
647 * O_DIRECT to open() and openat() operations. */
648 long xfer_align = (cf->xfer_align == -1)?4096:cf->xfer_align;
650 if (cf->max_xfer_size != -1)
651 asize = cf->max_xfer_size + xfer_align;
653 long incr = cf->incr_xfer_size;
654 /* Some platform does not set a proper value to
657 incr = cf->min_xfer_size;
658 if (cf->min_xfer_size < 0) {
662 asize = cf->min_xfer_size;
664 /* Increase a buffer size up to 64K bytes in
665 * a proper incremant size. */
666 while (asize < 1024*64)
668 /* Take a margin to adjust to the filesystem
672 cf->allocation_ptr = malloc(asize);
673 if (cf->allocation_ptr == NULL) {
674 archive_set_error(&a->archive, ENOMEM,
675 "Couldn't allocate memory");
676 a->archive.state = ARCHIVE_STATE_FATAL;
677 return (ARCHIVE_FATAL);
681 * Calculate proper address for the filesystem.
683 s = (uintptr_t)cf->allocation_ptr;
689 * Set a read buffer pointer in the proper alignment of
690 * the current filesystem.
692 cf->buff = cf->allocation_ptr + s;
693 cf->buff_size = asize - xfer_align;
699 _archive_read_data_block(struct archive *_a, const void **buff,
700 size_t *size, int64_t *offset)
702 struct archive_read_disk *a = (struct archive_read_disk *)_a;
703 struct tree *t = a->tree;
708 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA,
709 "archive_read_data_block");
711 if (t->entry_eof || t->entry_remaining_bytes <= 0) {
713 goto abort_read_data;
717 * Open the current file.
719 if (t->entry_fd < 0) {
720 int flags = O_RDONLY | O_BINARY;
723 * Eliminate or reduce cache effects if we can.
725 * Carefully consider this to be enabled.
727 #if defined(O_DIRECT) && 0/* Disabled for now */
728 if (t->current_filesystem->xfer_align != -1 &&
732 #if defined(O_NOATIME)
734 * Linux has O_NOATIME flag; use it if we need.
736 if ((t->flags & needsRestoreTimes) != 0 &&
737 t->restore_time.noatime == 0)
741 t->entry_fd = open_on_current_dir(t,
742 tree_current_access_path(t), flags);
743 #if defined(O_NOATIME)
745 * When we did open the file with O_NOATIME flag,
746 * if successful, set 1 to t->restore_time.noatime
747 * not to restore an atime of the file later.
748 * if failed by EPERM, retry it without O_NOATIME flag.
750 if (flags & O_NOATIME) {
751 if (t->entry_fd >= 0)
752 t->restore_time.noatime = 1;
753 else if (errno == EPERM) {
760 if (t->entry_fd < 0) {
761 archive_set_error(&a->archive, errno,
762 "Couldn't open %s", tree_current_path(t));
764 tree_enter_initial_dir(t);
765 goto abort_read_data;
767 tree_enter_initial_dir(t);
771 * Allocate read buffer if not allocated.
773 if (t->current_filesystem->allocation_ptr == NULL) {
774 r = setup_suitable_read_buffer(a);
775 if (r != ARCHIVE_OK) {
776 a->archive.state = ARCHIVE_STATE_FATAL;
777 goto abort_read_data;
780 t->entry_buff = t->current_filesystem->buff;
781 t->entry_buff_size = t->current_filesystem->buff_size;
783 buffbytes = t->entry_buff_size;
784 if (buffbytes > (size_t)t->current_sparse->length)
785 buffbytes = (size_t)t->current_sparse->length;
789 * TODO: Should we consider t->current_filesystem->xfer_align?
791 if (t->current_sparse->offset > t->entry_total) {
792 if (lseek(t->entry_fd,
793 (off_t)t->current_sparse->offset, SEEK_SET) < 0) {
794 archive_set_error(&a->archive, errno, "Seek error");
796 a->archive.state = ARCHIVE_STATE_FATAL;
797 goto abort_read_data;
799 bytes = t->current_sparse->offset - t->entry_total;
800 t->entry_remaining_bytes -= bytes;
801 t->entry_total += bytes;
805 * Read file contents.
808 bytes = read(t->entry_fd, t->entry_buff, buffbytes);
810 archive_set_error(&a->archive, errno, "Read error");
812 a->archive.state = ARCHIVE_STATE_FATAL;
813 goto abort_read_data;
821 goto abort_read_data;
823 *buff = t->entry_buff;
825 *offset = t->entry_total;
826 t->entry_total += bytes;
827 t->entry_remaining_bytes -= bytes;
828 if (t->entry_remaining_bytes == 0) {
829 /* Close the current file descriptor */
830 close_and_restore_time(t->entry_fd, t, &t->restore_time);
834 t->current_sparse->offset += bytes;
835 t->current_sparse->length -= bytes;
836 if (t->current_sparse->length == 0 && !t->entry_eof)
843 *offset = t->entry_total;
844 if (t->entry_fd >= 0) {
845 /* Close the current file descriptor */
846 close_and_restore_time(t->entry_fd, t, &t->restore_time);
853 next_entry(struct archive_read_disk *a, struct tree *t,
854 struct archive_entry *entry)
856 const struct stat *st; /* info to use for this entry */
857 const struct stat *lst;/* lstat() information */
865 switch (tree_next(t)) {
866 case TREE_ERROR_FATAL:
867 archive_set_error(&a->archive, t->tree_errno,
868 "%s: Unable to continue traversing directory tree",
869 tree_current_path(t));
870 a->archive.state = ARCHIVE_STATE_FATAL;
871 tree_enter_initial_dir(t);
872 return (ARCHIVE_FATAL);
874 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
875 "%s: Couldn't visit directory",
876 tree_current_path(t));
877 tree_enter_initial_dir(t);
878 return (ARCHIVE_FAILED);
880 tree_enter_initial_dir(t);
881 return (ARCHIVE_EOF);
882 case TREE_POSTDESCENT:
883 case TREE_POSTASCENT:
886 lst = tree_current_lstat(t);
888 archive_set_error(&a->archive, errno,
890 tree_current_path(t));
891 tree_enter_initial_dir(t);
892 return (ARCHIVE_FAILED);
896 } while (lst == NULL);
899 if (a->enable_copyfile) {
900 /* If we're using copyfile(), ignore "._XXX" files. */
901 const char *bname = strrchr(tree_current_path(t), '/');
903 bname = tree_current_path(t);
906 if (bname[0] == '.' && bname[1] == '_')
907 return (ARCHIVE_RETRY);
911 archive_entry_copy_pathname(entry, tree_current_path(t));
913 * Perform path matching.
916 r = archive_match_path_excluded(a->matching, entry);
918 archive_set_error(&(a->archive), errno,
919 "Faild : %s", archive_error_string(a->matching));
923 if (a->excluded_cb_func)
924 a->excluded_cb_func(&(a->archive),
925 a->excluded_cb_data, entry);
926 return (ARCHIVE_RETRY);
931 * Distinguish 'L'/'P'/'H' symlink following.
933 switch(t->symlink_mode) {
935 /* 'H': After the first item, rest like 'P'. */
936 t->symlink_mode = 'P';
937 /* 'H': First item (from command line) like 'L'. */
940 /* 'L': Do descend through a symlink to dir. */
941 descend = tree_current_is_dir(t);
942 /* 'L': Follow symlinks to files. */
943 a->symlink_mode = 'L';
944 a->follow_symlinks = 1;
945 /* 'L': Archive symlinks as targets, if we can. */
946 st = tree_current_stat(t);
947 if (st != NULL && !tree_target_is_same_as_parent(t, st))
949 /* If stat fails, we have a broken symlink;
950 * in that case, don't follow the link. */
953 /* 'P': Don't descend through a symlink to dir. */
954 descend = tree_current_is_physical_dir(t);
955 /* 'P': Don't follow symlinks to files. */
956 a->symlink_mode = 'P';
957 a->follow_symlinks = 0;
958 /* 'P': Archive symlinks as symlinks. */
963 if (update_current_filesystem(a, st->st_dev) != ARCHIVE_OK) {
964 a->archive.state = ARCHIVE_STATE_FATAL;
965 tree_enter_initial_dir(t);
966 return (ARCHIVE_FATAL);
968 if (t->initial_filesystem_id == -1)
969 t->initial_filesystem_id = t->current_filesystem_id;
970 if (!a->traverse_mount_points) {
971 if (t->initial_filesystem_id != t->current_filesystem_id)
972 return (ARCHIVE_RETRY);
974 t->descend = descend;
978 * If the file is marked with nodump flag, do not return this entry.
980 if (a->honor_nodump) {
981 #if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP)
982 if (st->st_flags & UF_NODUMP)
983 return (ARCHIVE_RETRY);
984 #elif defined(EXT2_IOC_GETFLAGS) && defined(EXT2_NODUMP_FL) &&\
985 defined(HAVE_WORKING_EXT2_IOC_GETFLAGS)
986 if (S_ISREG(st->st_mode) || S_ISDIR(st->st_mode)) {
987 unsigned long stflags;
989 t->entry_fd = open_on_current_dir(t,
990 tree_current_access_path(t), O_RDONLY | O_NONBLOCK);
991 if (t->entry_fd >= 0) {
992 r = ioctl(t->entry_fd, EXT2_IOC_GETFLAGS,
994 if (r == 0 && (stflags & EXT2_NODUMP_FL) != 0)
995 return (ARCHIVE_RETRY);
1001 archive_entry_copy_stat(entry, st);
1003 /* Save the times to be restored. This must be in before
1004 * calling archive_read_disk_descend() or any chance of it,
1005 * especially, invokng a callback. */
1006 t->restore_time.mtime = archive_entry_mtime(entry);
1007 t->restore_time.mtime_nsec = archive_entry_mtime_nsec(entry);
1008 t->restore_time.atime = archive_entry_atime(entry);
1009 t->restore_time.atime_nsec = archive_entry_atime_nsec(entry);
1010 t->restore_time.filetype = archive_entry_filetype(entry);
1011 t->restore_time.noatime = t->current_filesystem->noatime;
1014 * Perform time matching.
1017 r = archive_match_time_excluded(a->matching, entry);
1019 archive_set_error(&(a->archive), errno,
1020 "Faild : %s", archive_error_string(a->matching));
1024 if (a->excluded_cb_func)
1025 a->excluded_cb_func(&(a->archive),
1026 a->excluded_cb_data, entry);
1027 return (ARCHIVE_RETRY);
1031 /* Lookup uname/gname */
1032 name = archive_read_disk_uname(&(a->archive), archive_entry_uid(entry));
1034 archive_entry_copy_uname(entry, name);
1035 name = archive_read_disk_gname(&(a->archive), archive_entry_gid(entry));
1037 archive_entry_copy_gname(entry, name);
1040 * Perform owner matching.
1043 r = archive_match_owner_excluded(a->matching, entry);
1045 archive_set_error(&(a->archive), errno,
1046 "Faild : %s", archive_error_string(a->matching));
1050 if (a->excluded_cb_func)
1051 a->excluded_cb_func(&(a->archive),
1052 a->excluded_cb_data, entry);
1053 return (ARCHIVE_RETRY);
1058 * Invoke a meta data filter callback.
1060 if (a->metadata_filter_func) {
1061 if (!a->metadata_filter_func(&(a->archive),
1062 a->metadata_filter_data, entry))
1063 return (ARCHIVE_RETRY);
1067 * Populate the archive_entry with metadata from the disk.
1069 archive_entry_copy_sourcepath(entry, tree_current_access_path(t));
1070 r = archive_read_disk_entry_from_file(&(a->archive), entry,
1077 _archive_read_next_header2(struct archive *_a, struct archive_entry *entry)
1079 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1083 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
1084 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
1085 "archive_read_next_header2");
1088 if (t->entry_fd >= 0) {
1089 close_and_restore_time(t->entry_fd, t, &t->restore_time);
1094 r = next_entry(a, t, entry);
1095 if (t->entry_fd >= 0) {
1100 if (r == ARCHIVE_RETRY) {
1101 archive_entry_clear(entry);
1107 /* Return to the initial directory. */
1108 tree_enter_initial_dir(t);
1111 * EOF and FATAL are persistent at this layer. By
1112 * modifying the state, we guarantee that future calls to
1113 * read a header or read data will fail.
1117 a->archive.state = ARCHIVE_STATE_EOF;
1121 /* Overwrite the sourcepath based on the initial directory. */
1122 archive_entry_copy_sourcepath(entry, tree_current_path(t));
1124 if (archive_entry_filetype(entry) == AE_IFREG) {
1125 t->nlink = archive_entry_nlink(entry);
1126 t->entry_remaining_bytes = archive_entry_size(entry);
1127 t->entry_eof = (t->entry_remaining_bytes == 0)? 1: 0;
1128 if (!t->entry_eof &&
1129 setup_sparse(a, entry) != ARCHIVE_OK)
1130 return (ARCHIVE_FATAL);
1132 t->entry_remaining_bytes = 0;
1135 a->archive.state = ARCHIVE_STATE_DATA;
1140 a->archive.state = ARCHIVE_STATE_FATAL;
1148 setup_sparse(struct archive_read_disk *a, struct archive_entry *entry)
1150 struct tree *t = a->tree;
1151 int64_t length, offset;
1154 t->sparse_count = archive_entry_sparse_reset(entry);
1155 if (t->sparse_count+1 > t->sparse_list_size) {
1156 free(t->sparse_list);
1157 t->sparse_list_size = t->sparse_count + 1;
1158 t->sparse_list = malloc(sizeof(t->sparse_list[0]) *
1159 t->sparse_list_size);
1160 if (t->sparse_list == NULL) {
1161 t->sparse_list_size = 0;
1162 archive_set_error(&a->archive, ENOMEM,
1163 "Can't allocate data");
1164 a->archive.state = ARCHIVE_STATE_FATAL;
1165 return (ARCHIVE_FATAL);
1168 for (i = 0; i < t->sparse_count; i++) {
1169 archive_entry_sparse_next(entry, &offset, &length);
1170 t->sparse_list[i].offset = offset;
1171 t->sparse_list[i].length = length;
1174 t->sparse_list[i].offset = 0;
1175 t->sparse_list[i].length = archive_entry_size(entry);
1177 t->sparse_list[i].offset = archive_entry_size(entry);
1178 t->sparse_list[i].length = 0;
1180 t->current_sparse = t->sparse_list;
1182 return (ARCHIVE_OK);
1186 archive_read_disk_set_matching(struct archive *_a, struct archive *_ma,
1187 void (*_excluded_func)(struct archive *, void *, struct archive_entry *),
1190 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1191 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
1192 ARCHIVE_STATE_ANY, "archive_read_disk_set_matching");
1194 a->excluded_cb_func = _excluded_func;
1195 a->excluded_cb_data = _client_data;
1196 return (ARCHIVE_OK);
1200 archive_read_disk_set_metadata_filter_callback(struct archive *_a,
1201 int (*_metadata_filter_func)(struct archive *, void *,
1202 struct archive_entry *), void *_client_data)
1204 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1206 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_ANY,
1207 "archive_read_disk_set_metadata_filter_callback");
1209 a->metadata_filter_func = _metadata_filter_func;
1210 a->metadata_filter_data = _client_data;
1211 return (ARCHIVE_OK);
1215 archive_read_disk_can_descend(struct archive *_a)
1217 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1218 struct tree *t = a->tree;
1220 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
1221 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
1222 "archive_read_disk_can_descend");
1224 return (t->visit_type == TREE_REGULAR && t->descend);
1228 * Called by the client to mark the directory just returned from
1229 * tree_next() as needing to be visited.
1232 archive_read_disk_descend(struct archive *_a)
1234 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1235 struct tree *t = a->tree;
1237 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
1238 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
1239 "archive_read_disk_descend");
1241 if (t->visit_type != TREE_REGULAR || !t->descend)
1242 return (ARCHIVE_OK);
1244 if (tree_current_is_physical_dir(t)) {
1245 tree_push(t, t->basename, t->current_filesystem_id,
1246 t->lst.st_dev, t->lst.st_ino, &t->restore_time);
1247 t->stack->flags |= isDir;
1248 } else if (tree_current_is_dir(t)) {
1249 tree_push(t, t->basename, t->current_filesystem_id,
1250 t->st.st_dev, t->st.st_ino, &t->restore_time);
1251 t->stack->flags |= isDirLink;
1254 return (ARCHIVE_OK);
1258 archive_read_disk_open(struct archive *_a, const char *pathname)
1260 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1262 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
1263 ARCHIVE_STATE_NEW | ARCHIVE_STATE_CLOSED,
1264 "archive_read_disk_open");
1265 archive_clear_error(&a->archive);
1267 return (_archive_read_disk_open(_a, pathname));
1271 archive_read_disk_open_w(struct archive *_a, const wchar_t *pathname)
1273 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1274 struct archive_string path;
1277 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
1278 ARCHIVE_STATE_NEW | ARCHIVE_STATE_CLOSED,
1279 "archive_read_disk_open_w");
1280 archive_clear_error(&a->archive);
1282 /* Make a char string from a wchar_t string. */
1283 archive_string_init(&path);
1284 if (archive_string_append_from_wcs(&path, pathname,
1285 wcslen(pathname)) != 0) {
1286 if (errno == ENOMEM)
1287 archive_set_error(&a->archive, ENOMEM,
1288 "Can't allocate memory");
1290 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1291 "Can't convert a path to a char string");
1292 a->archive.state = ARCHIVE_STATE_FATAL;
1293 ret = ARCHIVE_FATAL;
1295 ret = _archive_read_disk_open(_a, path.s);
1297 archive_string_free(&path);
1302 _archive_read_disk_open(struct archive *_a, const char *pathname)
1304 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1306 if (a->tree != NULL)
1307 a->tree = tree_reopen(a->tree, pathname, a->restore_time);
1309 a->tree = tree_open(pathname, a->symlink_mode,
1311 if (a->tree == NULL) {
1312 archive_set_error(&a->archive, ENOMEM,
1313 "Can't allocate tar data");
1314 a->archive.state = ARCHIVE_STATE_FATAL;
1315 return (ARCHIVE_FATAL);
1317 a->archive.state = ARCHIVE_STATE_HEADER;
1319 return (ARCHIVE_OK);
1323 * Return a current filesystem ID which is index of the filesystem entry
1324 * you've visited through archive_read_disk.
1327 archive_read_disk_current_filesystem(struct archive *_a)
1329 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1331 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA,
1332 "archive_read_disk_current_filesystem");
1334 return (a->tree->current_filesystem_id);
1338 update_current_filesystem(struct archive_read_disk *a, int64_t dev)
1340 struct tree *t = a->tree;
1343 if (t->current_filesystem != NULL &&
1344 t->current_filesystem->dev == dev)
1345 return (ARCHIVE_OK);
1347 for (i = 0; i < t->max_filesystem_id; i++) {
1348 if (t->filesystem_table[i].dev == dev) {
1349 /* There is the filesytem ID we've already generated. */
1350 t->current_filesystem_id = i;
1351 t->current_filesystem = &(t->filesystem_table[i]);
1352 return (ARCHIVE_OK);
1357 * This is the new filesytem which we have to generate a new ID for.
1359 fid = t->max_filesystem_id++;
1360 if (t->max_filesystem_id > t->allocated_filesytem) {
1363 s = t->max_filesystem_id * 2;
1364 t->filesystem_table = realloc(t->filesystem_table,
1365 s * sizeof(*t->filesystem_table));
1366 if (t->filesystem_table == NULL) {
1367 archive_set_error(&a->archive, ENOMEM,
1368 "Can't allocate tar data");
1369 return (ARCHIVE_FATAL);
1371 t->allocated_filesytem = s;
1373 t->current_filesystem_id = fid;
1374 t->current_filesystem = &(t->filesystem_table[fid]);
1375 t->current_filesystem->dev = dev;
1376 t->current_filesystem->allocation_ptr = NULL;
1377 t->current_filesystem->buff = NULL;
1379 /* Setup the current filesystem properties which depend on
1380 * platform specific. */
1381 return (setup_current_filesystem(a));
1385 * Returns 1 if current filesystem is generated filesystem, 0 if it is not
1386 * or -1 if it is unknown.
1389 archive_read_disk_current_filesystem_is_synthetic(struct archive *_a)
1391 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1393 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA,
1394 "archive_read_disk_current_filesystem");
1396 return (a->tree->current_filesystem->synthetic);
1400 * Returns 1 if current filesystem is remote filesystem, 0 if it is not
1401 * or -1 if it is unknown.
1404 archive_read_disk_current_filesystem_is_remote(struct archive *_a)
1406 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1408 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA,
1409 "archive_read_disk_current_filesystem");
1411 return (a->tree->current_filesystem->remote);
1414 #if defined(_PC_REC_INCR_XFER_SIZE) && defined(_PC_REC_MAX_XFER_SIZE) &&\
1415 defined(_PC_REC_MIN_XFER_SIZE) && defined(_PC_REC_XFER_ALIGN)
1417 get_xfer_size(struct tree *t, int fd, const char *path)
1419 t->current_filesystem->xfer_align = -1;
1422 t->current_filesystem->incr_xfer_size =
1423 fpathconf(fd, _PC_REC_INCR_XFER_SIZE);
1424 t->current_filesystem->max_xfer_size =
1425 fpathconf(fd, _PC_REC_MAX_XFER_SIZE);
1426 t->current_filesystem->min_xfer_size =
1427 fpathconf(fd, _PC_REC_MIN_XFER_SIZE);
1428 t->current_filesystem->xfer_align =
1429 fpathconf(fd, _PC_REC_XFER_ALIGN);
1430 } else if (path != NULL) {
1431 t->current_filesystem->incr_xfer_size =
1432 pathconf(path, _PC_REC_INCR_XFER_SIZE);
1433 t->current_filesystem->max_xfer_size =
1434 pathconf(path, _PC_REC_MAX_XFER_SIZE);
1435 t->current_filesystem->min_xfer_size =
1436 pathconf(path, _PC_REC_MIN_XFER_SIZE);
1437 t->current_filesystem->xfer_align =
1438 pathconf(path, _PC_REC_XFER_ALIGN);
1440 /* At least we need an alignment size. */
1441 if (t->current_filesystem->xfer_align == -1)
1442 return ((errno == EINVAL)?1:-1);
1448 get_xfer_size(struct tree *t, int fd, const char *path)
1450 (void)t; /* UNUSED */
1451 (void)fd; /* UNUSED */
1452 (void)path; /* UNUSED */
1453 return (1);/* Not supported */
1457 #if defined(HAVE_STATFS) && defined(HAVE_FSTATFS) && defined(MNT_LOCAL) \
1458 && !defined(ST_LOCAL)
1461 * Gather current filesystem properties on FreeBSD, OpenBSD and Mac OS X.
1464 setup_current_filesystem(struct archive_read_disk *a)
1466 struct tree *t = a->tree;
1468 #if defined(HAVE_GETVFSBYNAME) && defined(VFCF_SYNTHETIC)
1469 struct xvfsconf vfc;
1472 #if !defined(HAVE_STRUCT_STATFS_F_NAMEMAX)
1476 t->current_filesystem->synthetic = -1;
1477 t->current_filesystem->remote = -1;
1478 if (tree_current_is_symblic_link_target(t)) {
1479 #if defined(HAVE_OPENAT)
1481 * Get file system statistics on any directory
1484 int fd = openat(tree_current_dir_fd(t),
1485 tree_current_access_path(t), O_RDONLY);
1487 archive_set_error(&a->archive, errno,
1489 return (ARCHIVE_FAILED);
1491 r = fstatfs(fd, &sfs);
1493 xr = get_xfer_size(t, fd, NULL);
1496 if (tree_enter_working_dir(t) != 0) {
1497 archive_set_error(&a->archive, errno, "fchdir failed");
1498 return (ARCHIVE_FAILED);
1500 r = statfs(tree_current_access_path(t), &sfs);
1502 xr = get_xfer_size(t, -1, tree_current_access_path(t));
1505 r = fstatfs(tree_current_dir_fd(t), &sfs);
1507 xr = get_xfer_size(t, tree_current_dir_fd(t), NULL);
1509 if (r == -1 || xr == -1) {
1510 archive_set_error(&a->archive, errno, "statfs failed");
1511 return (ARCHIVE_FAILED);
1512 } else if (xr == 1) {
1513 /* pathconf(_PC_REX_*) operations are not supported. */
1514 t->current_filesystem->xfer_align = sfs.f_bsize;
1515 t->current_filesystem->max_xfer_size = -1;
1516 t->current_filesystem->min_xfer_size = sfs.f_iosize;
1517 t->current_filesystem->incr_xfer_size = sfs.f_iosize;
1519 if (sfs.f_flags & MNT_LOCAL)
1520 t->current_filesystem->remote = 0;
1522 t->current_filesystem->remote = 1;
1524 #if defined(HAVE_GETVFSBYNAME) && defined(VFCF_SYNTHETIC)
1525 r = getvfsbyname(sfs.f_fstypename, &vfc);
1527 archive_set_error(&a->archive, errno, "getvfsbyname failed");
1528 return (ARCHIVE_FAILED);
1530 if (vfc.vfc_flags & VFCF_SYNTHETIC)
1531 t->current_filesystem->synthetic = 1;
1533 t->current_filesystem->synthetic = 0;
1536 #if defined(MNT_NOATIME)
1537 if (sfs.f_flags & MNT_NOATIME)
1538 t->current_filesystem->noatime = 1;
1541 t->current_filesystem->noatime = 0;
1543 #if defined(HAVE_READDIR_R)
1544 /* Set maximum filename length. */
1545 #if defined(HAVE_STRUCT_STATFS_F_NAMEMAX)
1546 t->current_filesystem->name_max = sfs.f_namemax;
1548 /* Mac OS X does not have f_namemax in struct statfs. */
1549 if (tree_current_is_symblic_link_target(t)) {
1550 if (tree_enter_working_dir(t) != 0) {
1551 archive_set_error(&a->archive, errno, "fchdir failed");
1552 return (ARCHIVE_FAILED);
1554 nm = pathconf(tree_current_access_path(t), _PC_NAME_MAX);
1556 nm = fpathconf(tree_current_dir_fd(t), _PC_NAME_MAX);
1558 t->current_filesystem->name_max = NAME_MAX;
1560 t->current_filesystem->name_max = nm;
1562 #endif /* HAVE_READDIR_R */
1563 return (ARCHIVE_OK);
1566 #elif (defined(HAVE_STATVFS) || defined(HAVE_FSTATVFS)) && defined(ST_LOCAL)
1569 * Gather current filesystem properties on NetBSD
1572 setup_current_filesystem(struct archive_read_disk *a)
1574 struct tree *t = a->tree;
1578 t->current_filesystem->synthetic = -1;
1579 if (tree_enter_working_dir(t) != 0) {
1580 archive_set_error(&a->archive, errno, "fchdir failed");
1581 return (ARCHIVE_FAILED);
1583 if (tree_current_is_symblic_link_target(t)) {
1584 r = statvfs(tree_current_access_path(t), &sfs);
1586 xr = get_xfer_size(t, -1, tree_current_access_path(t));
1588 #ifdef HAVE_FSTATVFS
1589 r = fstatvfs(tree_current_dir_fd(t), &sfs);
1591 xr = get_xfer_size(t, tree_current_dir_fd(t), NULL);
1593 r = statvfs(".", &sfs);
1595 xr = get_xfer_size(t, -1, ".");
1598 if (r == -1 || xr == -1) {
1599 t->current_filesystem->remote = -1;
1600 archive_set_error(&a->archive, errno, "statvfs failed");
1601 return (ARCHIVE_FAILED);
1602 } else if (xr == 1) {
1603 /* Usuall come here unless NetBSD supports _PC_REC_XFER_ALIGN
1604 * for pathconf() function. */
1605 t->current_filesystem->xfer_align = sfs.f_frsize;
1606 t->current_filesystem->max_xfer_size = -1;
1607 #if defined(HAVE_STRUCT_STATVFS_F_IOSIZE)
1608 t->current_filesystem->min_xfer_size = sfs.f_iosize;
1609 t->current_filesystem->incr_xfer_size = sfs.f_iosize;
1611 t->current_filesystem->min_xfer_size = sfs.f_bsize;
1612 t->current_filesystem->incr_xfer_size = sfs.f_bsize;
1615 if (sfs.f_flag & ST_LOCAL)
1616 t->current_filesystem->remote = 0;
1618 t->current_filesystem->remote = 1;
1620 #if defined(ST_NOATIME)
1621 if (sfs.f_flag & ST_NOATIME)
1622 t->current_filesystem->noatime = 1;
1625 t->current_filesystem->noatime = 0;
1627 /* Set maximum filename length. */
1628 t->current_filesystem->name_max = sfs.f_namemax;
1629 return (ARCHIVE_OK);
1632 #elif defined(HAVE_SYS_STATFS_H) && defined(HAVE_LINUX_MAGIC_H) &&\
1633 defined(HAVE_STATFS) && defined(HAVE_FSTATFS)
1635 * Note: statfs is deprecated since LSB 3.2
1638 #ifndef CIFS_SUPER_MAGIC
1639 #define CIFS_SUPER_MAGIC 0xFF534D42
1641 #ifndef DEVFS_SUPER_MAGIC
1642 #define DEVFS_SUPER_MAGIC 0x1373
1646 * Gather current filesystem properties on Linux
1649 setup_current_filesystem(struct archive_read_disk *a)
1651 struct tree *t = a->tree;
1653 struct statvfs svfs;
1654 int r, vr = 0, xr = 0;
1656 if (tree_current_is_symblic_link_target(t)) {
1657 #if defined(HAVE_OPENAT)
1659 * Get file system statistics on any directory
1662 int fd = openat(tree_current_dir_fd(t),
1663 tree_current_access_path(t), O_RDONLY);
1665 archive_set_error(&a->archive, errno,
1667 return (ARCHIVE_FAILED);
1669 vr = fstatvfs(fd, &svfs);/* for f_flag, mount flags */
1670 r = fstatfs(fd, &sfs);
1672 xr = get_xfer_size(t, fd, NULL);
1675 if (tree_enter_working_dir(t) != 0) {
1676 archive_set_error(&a->archive, errno, "fchdir failed");
1677 return (ARCHIVE_FAILED);
1679 vr = statvfs(tree_current_access_path(t), &svfs);
1680 r = statfs(tree_current_access_path(t), &sfs);
1682 xr = get_xfer_size(t, -1, tree_current_access_path(t));
1686 vr = fstatvfs(tree_current_dir_fd(t), &svfs);
1687 r = fstatfs(tree_current_dir_fd(t), &sfs);
1689 xr = get_xfer_size(t, tree_current_dir_fd(t), NULL);
1691 if (tree_enter_working_dir(t) != 0) {
1692 archive_set_error(&a->archive, errno, "fchdir failed");
1693 return (ARCHIVE_FAILED);
1695 vr = statvfs(".", &svfs);
1696 r = statfs(".", &sfs);
1698 xr = get_xfer_size(t, -1, ".");
1701 if (r == -1 || xr == -1 || vr == -1) {
1702 t->current_filesystem->synthetic = -1;
1703 t->current_filesystem->remote = -1;
1704 archive_set_error(&a->archive, errno, "statfs failed");
1705 return (ARCHIVE_FAILED);
1706 } else if (xr == 1) {
1707 /* pathconf(_PC_REX_*) operations are not supported. */
1708 t->current_filesystem->xfer_align = svfs.f_frsize;
1709 t->current_filesystem->max_xfer_size = -1;
1710 t->current_filesystem->min_xfer_size = svfs.f_bsize;
1711 t->current_filesystem->incr_xfer_size = svfs.f_bsize;
1713 switch (sfs.f_type) {
1714 case AFS_SUPER_MAGIC:
1715 case CIFS_SUPER_MAGIC:
1716 case CODA_SUPER_MAGIC:
1717 case NCP_SUPER_MAGIC:/* NetWare */
1718 case NFS_SUPER_MAGIC:
1719 case SMB_SUPER_MAGIC:
1720 t->current_filesystem->remote = 1;
1721 t->current_filesystem->synthetic = 0;
1723 case DEVFS_SUPER_MAGIC:
1724 case PROC_SUPER_MAGIC:
1725 case USBDEVICE_SUPER_MAGIC:
1726 t->current_filesystem->remote = 0;
1727 t->current_filesystem->synthetic = 1;
1730 t->current_filesystem->remote = 0;
1731 t->current_filesystem->synthetic = 0;
1735 #if defined(ST_NOATIME)
1736 if (svfs.f_flag & ST_NOATIME)
1737 t->current_filesystem->noatime = 1;
1740 t->current_filesystem->noatime = 0;
1742 #if defined(HAVE_READDIR_R)
1743 /* Set maximum filename length. */
1744 t->current_filesystem->name_max = sfs.f_namelen;
1746 return (ARCHIVE_OK);
1749 #elif defined(HAVE_SYS_STATVFS_H) &&\
1750 (defined(HAVE_STATVFS) || defined(HAVE_FSTATVFS))
1753 * Gather current filesystem properties on other posix platform.
1756 setup_current_filesystem(struct archive_read_disk *a)
1758 struct tree *t = a->tree;
1762 t->current_filesystem->synthetic = -1;/* Not supported */
1763 t->current_filesystem->remote = -1;/* Not supported */
1764 if (tree_current_is_symblic_link_target(t)) {
1765 #if defined(HAVE_OPENAT)
1767 * Get file system statistics on any directory
1770 int fd = openat(tree_current_dir_fd(t),
1771 tree_current_access_path(t), O_RDONLY);
1773 archive_set_error(&a->archive, errno,
1775 return (ARCHIVE_FAILED);
1777 r = fstatvfs(fd, &sfs);
1779 xr = get_xfer_size(t, fd, NULL);
1782 if (tree_enter_working_dir(t) != 0) {
1783 archive_set_error(&a->archive, errno, "fchdir failed");
1784 return (ARCHIVE_FAILED);
1786 r = statvfs(tree_current_access_path(t), &sfs);
1788 xr = get_xfer_size(t, -1, tree_current_access_path(t));
1791 #ifdef HAVE_FSTATVFS
1792 r = fstatvfs(tree_current_dir_fd(t), &sfs);
1794 xr = get_xfer_size(t, tree_current_dir_fd(t), NULL);
1796 if (tree_enter_working_dir(t) != 0) {
1797 archive_set_error(&a->archive, errno, "fchdir failed");
1798 return (ARCHIVE_FAILED);
1800 r = statvfs(".", &sfs);
1802 xr = get_xfer_size(t, -1, ".");
1805 if (r == -1 || xr == -1) {
1806 t->current_filesystem->synthetic = -1;
1807 t->current_filesystem->remote = -1;
1808 archive_set_error(&a->archive, errno, "statvfs failed");
1809 return (ARCHIVE_FAILED);
1810 } else if (xr == 1) {
1811 /* pathconf(_PC_REX_*) operations are not supported. */
1812 t->current_filesystem->xfer_align = sfs.f_frsize;
1813 t->current_filesystem->max_xfer_size = -1;
1814 t->current_filesystem->min_xfer_size = sfs.f_bsize;
1815 t->current_filesystem->incr_xfer_size = sfs.f_bsize;
1818 #if defined(ST_NOATIME)
1819 if (sfs.f_flag & ST_NOATIME)
1820 t->current_filesystem->noatime = 1;
1823 t->current_filesystem->noatime = 0;
1825 #if defined(HAVE_READDIR_R)
1826 /* Set maximum filename length. */
1827 t->current_filesystem->name_max = sfs.f_namemax;
1829 return (ARCHIVE_OK);
1835 * Generic: Gather current filesystem properties.
1836 * TODO: Is this generic function really needed?
1839 setup_current_filesystem(struct archive_read_disk *a)
1841 struct tree *t = a->tree;
1842 #if defined(_PC_NAME_MAX) && defined(HAVE_READDIR_R)
1845 t->current_filesystem->synthetic = -1;/* Not supported */
1846 t->current_filesystem->remote = -1;/* Not supported */
1847 t->current_filesystem->noatime = 0;
1848 (void)get_xfer_size(t, -1, ".");/* Dummy call to avoid build error. */
1849 t->current_filesystem->xfer_align = -1;/* Unknown */
1850 t->current_filesystem->max_xfer_size = -1;
1851 t->current_filesystem->min_xfer_size = -1;
1852 t->current_filesystem->incr_xfer_size = -1;
1854 #if defined(HAVE_READDIR_R)
1855 /* Set maximum filename length. */
1856 # if defined(_PC_NAME_MAX)
1857 if (tree_current_is_symblic_link_target(t)) {
1858 if (tree_enter_working_dir(t) != 0) {
1859 archive_set_error(&a->archive, errno, "fchdir failed");
1860 return (ARCHIVE_FAILED);
1862 nm = pathconf(tree_current_access_path(t), _PC_NAME_MAX);
1864 nm = fpathconf(tree_current_dir_fd(t), _PC_NAME_MAX);
1866 # endif /* _PC_NAME_MAX */
1868 * Some sysmtes (HP-UX or others?) incorrectly defined
1869 * NAME_MAX macro to be a smaller value.
1871 # if defined(NAME_MAX) && NAME_MAX >= 255
1872 t->current_filesystem->name_max = NAME_MAX;
1874 /* No way to get a trusted value of maximum filename
1876 t->current_filesystem->name_max = PATH_MAX;
1877 # endif /* NAME_MAX */
1878 # if defined(_PC_NAME_MAX)
1880 t->current_filesystem->name_max = nm;
1881 # endif /* _PC_NAME_MAX */
1882 #endif /* HAVE_READDIR_R */
1883 return (ARCHIVE_OK);
1889 close_and_restore_time(int fd, struct tree *t, struct restore_time *rt)
1892 (void)a; /* UNUSED */
1895 #if defined(HAVE_FUTIMENS) && !defined(__CYGWIN__)
1896 struct timespec timespecs[2];
1898 struct timeval times[2];
1900 if ((t->flags & needsRestoreTimes) == 0 || rt->noatime) {
1907 #if defined(HAVE_FUTIMENS) && !defined(__CYGWIN__)
1908 timespecs[1].tv_sec = rt->mtime;
1909 timespecs[1].tv_nsec = rt->mtime_nsec;
1911 timespecs[0].tv_sec = rt->atime;
1912 timespecs[0].tv_nsec = rt->atime_nsec;
1913 /* futimens() is defined in POSIX.1-2008. */
1914 if (futimens(fd, timespecs) == 0)
1918 times[1].tv_sec = rt->mtime;
1919 times[1].tv_usec = rt->mtime_nsec / 1000;
1921 times[0].tv_sec = rt->atime;
1922 times[0].tv_usec = rt->atime_nsec / 1000;
1924 #if !defined(HAVE_FUTIMENS) && defined(HAVE_FUTIMES) && !defined(__CYGWIN__)
1925 if (futimes(fd, times) == 0)
1929 #if defined(HAVE_FUTIMESAT)
1930 if (futimesat(tree_current_dir_fd(t), rt->name, times) == 0)
1934 if (lutimes(rt->name, times) != 0)
1936 if (AE_IFLNK != rt->filetype && utimes(rt->name, times) != 0)
1944 open_on_current_dir(struct tree *t, const char *path, int flags)
1947 return (openat(tree_current_dir_fd(t), path, flags));
1949 if (tree_enter_working_dir(t) != 0)
1951 return (open(path, flags));
1956 * Add a directory path to the current stack.
1959 tree_push(struct tree *t, const char *path, int filesystem_id,
1960 int64_t dev, int64_t ino, struct restore_time *rt)
1962 struct tree_entry *te;
1964 te = malloc(sizeof(*te));
1965 memset(te, 0, sizeof(*te));
1966 te->next = t->stack;
1967 te->parent = t->current;
1969 te->depth = te->parent->depth + 1;
1971 archive_string_init(&te->name);
1972 te->symlink_parent_fd = -1;
1973 archive_strcpy(&te->name, path);
1974 te->flags = needsDescent | needsOpen | needsAscent;
1975 te->filesystem_id = filesystem_id;
1978 te->dirname_length = t->dirname_length;
1979 te->restore_time.name = te->name.s;
1981 te->restore_time.mtime = rt->mtime;
1982 te->restore_time.mtime_nsec = rt->mtime_nsec;
1983 te->restore_time.atime = rt->atime;
1984 te->restore_time.atime_nsec = rt->atime_nsec;
1985 te->restore_time.filetype = rt->filetype;
1986 te->restore_time.noatime = rt->noatime;
1991 * Append a name to the current dir path.
1994 tree_append(struct tree *t, const char *name, size_t name_length)
1998 t->path.s[t->dirname_length] = '\0';
1999 t->path.length = t->dirname_length;
2000 /* Strip trailing '/' from name, unless entire name is "/". */
2001 while (name_length > 1 && name[name_length - 1] == '/')
2004 /* Resize pathname buffer as needed. */
2005 size_needed = name_length + t->dirname_length + 2;
2006 archive_string_ensure(&t->path, size_needed);
2007 /* Add a separating '/' if it's needed. */
2008 if (t->dirname_length > 0 && t->path.s[archive_strlen(&t->path)-1] != '/')
2009 archive_strappend_char(&t->path, '/');
2010 t->basename = t->path.s + archive_strlen(&t->path);
2011 archive_strncat(&t->path, name, name_length);
2012 t->restore_time.name = t->basename;
2016 * Open a directory tree for traversal.
2018 static struct tree *
2019 tree_open(const char *path, int symlink_mode, int restore_time)
2023 if ((t = malloc(sizeof(*t))) == NULL)
2025 memset(t, 0, sizeof(*t));
2026 archive_string_init(&t->path);
2027 archive_string_ensure(&t->path, 31);
2028 t->initial_symlink_mode = symlink_mode;
2029 return (tree_reopen(t, path, restore_time));
2032 static struct tree *
2033 tree_reopen(struct tree *t, const char *path, int restore_time)
2035 t->flags = (restore_time)?needsRestoreTimes:0;
2036 t->flags |= onInitialDir;
2039 t->dirname_length = 0;
2043 t->d = INVALID_DIR_HANDLE;
2044 t->symlink_mode = t->initial_symlink_mode;
2045 archive_string_empty(&t->path);
2048 t->entry_remaining_bytes = 0;
2049 t->initial_filesystem_id = -1;
2051 /* First item is set up a lot like a symlink traversal. */
2052 tree_push(t, path, 0, 0, 0, NULL);
2053 t->stack->flags = needsFirstVisit;
2054 t->maxOpenCount = t->openCount = 1;
2055 t->initial_dir_fd = open(".", O_RDONLY);
2056 t->working_dir_fd = dup(t->initial_dir_fd);
2061 tree_descent(struct tree *t)
2063 int flag, new_fd, r = 0;
2065 t->dirname_length = archive_strlen(&t->path);
2067 #if defined(O_DIRECTORY)
2068 flag |= O_DIRECTORY;
2070 new_fd = open_on_current_dir(t, t->stack->name.s, flag);
2072 t->tree_errno = errno;
2076 /* If it is a link, set up fd for the ascent. */
2077 if (t->stack->flags & isDirLink) {
2078 t->stack->symlink_parent_fd = t->working_dir_fd;
2080 if (t->openCount > t->maxOpenCount)
2081 t->maxOpenCount = t->openCount;
2083 close(t->working_dir_fd);
2084 /* Renew the current working directory. */
2085 t->working_dir_fd = new_fd;
2086 t->flags &= ~onWorkingDir;
2092 * We've finished a directory; ascend back to the parent.
2095 tree_ascend(struct tree *t)
2097 struct tree_entry *te;
2098 int new_fd, r = 0, prev_dir_fd;
2101 prev_dir_fd = t->working_dir_fd;
2102 if (te->flags & isDirLink)
2103 new_fd = te->symlink_parent_fd;
2105 new_fd = open_on_current_dir(t, "..", O_RDONLY);
2107 t->tree_errno = errno;
2108 r = TREE_ERROR_FATAL;
2110 /* Renew the current working directory. */
2111 t->working_dir_fd = new_fd;
2112 t->flags &= ~onWorkingDir;
2113 /* Current directory has been changed, we should
2114 * close an fd of previous working directory. */
2115 close_and_restore_time(prev_dir_fd, t, &te->restore_time);
2116 if (te->flags & isDirLink) {
2118 te->symlink_parent_fd = -1;
2126 * Return to the initial directory where tree_open() was performed.
2129 tree_enter_initial_dir(struct tree *t)
2133 if ((t->flags & onInitialDir) == 0) {
2134 r = fchdir(t->initial_dir_fd);
2136 t->flags &= ~onWorkingDir;
2137 t->flags |= onInitialDir;
2144 * Restore working directory of directory traversals.
2147 tree_enter_working_dir(struct tree *t)
2152 * Change the current directory if really needed.
2153 * Sometimes this is unneeded when we did not do
2156 if (t->depth > 0 && (t->flags & onWorkingDir) == 0) {
2157 r = fchdir(t->working_dir_fd);
2159 t->flags &= ~onInitialDir;
2160 t->flags |= onWorkingDir;
2167 tree_current_dir_fd(struct tree *t)
2169 return (t->working_dir_fd);
2173 * Pop the working stack.
2176 tree_pop(struct tree *t)
2178 struct tree_entry *te;
2180 t->path.s[t->dirname_length] = '\0';
2181 t->path.length = t->dirname_length;
2182 if (t->stack == t->current && t->current != NULL)
2183 t->current = t->current->parent;
2185 t->stack = te->next;
2186 t->dirname_length = te->dirname_length;
2187 t->basename = t->path.s + t->dirname_length;
2188 while (t->basename[0] == '/')
2190 archive_string_free(&te->name);
2195 * Get the next item in the tree traversal.
2198 tree_next(struct tree *t)
2202 while (t->stack != NULL) {
2203 /* If there's an open dir, get the next entry from there. */
2204 if (t->d != INVALID_DIR_HANDLE) {
2205 r = tree_dir_next_posix(t);
2211 if (t->stack->flags & needsFirstVisit) {
2212 /* Top stack item needs a regular visit. */
2213 t->current = t->stack;
2214 tree_append(t, t->stack->name.s,
2215 archive_strlen(&(t->stack->name)));
2216 /* t->dirname_length = t->path_length; */
2218 t->stack->flags &= ~needsFirstVisit;
2219 return (t->visit_type = TREE_REGULAR);
2220 } else if (t->stack->flags & needsDescent) {
2221 /* Top stack item is dir to descend into. */
2222 t->current = t->stack;
2223 tree_append(t, t->stack->name.s,
2224 archive_strlen(&(t->stack->name)));
2225 t->stack->flags &= ~needsDescent;
2226 r = tree_descent(t);
2231 t->visit_type = TREE_POSTDESCENT;
2232 return (t->visit_type);
2233 } else if (t->stack->flags & needsOpen) {
2234 t->stack->flags &= ~needsOpen;
2235 r = tree_dir_next_posix(t);
2239 } else if (t->stack->flags & needsAscent) {
2240 /* Top stack item is dir and we're done with it. */
2243 t->visit_type = r != 0 ? r : TREE_POSTASCENT;
2244 return (t->visit_type);
2246 /* Top item on stack is dead. */
2248 t->flags &= ~hasLstat;
2249 t->flags &= ~hasStat;
2252 return (t->visit_type = 0);
2256 tree_dir_next_posix(struct tree *t)
2263 #if defined(HAVE_READDIR_R)
2267 #if defined(HAVE_FDOPENDIR)
2268 if ((t->d = fdopendir(dup(t->working_dir_fd))) == NULL) {
2270 if (tree_enter_working_dir(t) != 0 ||
2271 (t->d = opendir(".")) == NULL) {
2273 r = tree_ascend(t); /* Undo "chdir" */
2275 t->tree_errno = errno;
2276 t->visit_type = r != 0 ? r : TREE_ERROR_DIR;
2277 return (t->visit_type);
2279 #if defined(HAVE_READDIR_R)
2280 dirent_size = offsetof(struct dirent, d_name) +
2281 t->filesystem_table[t->current->filesystem_id].name_max + 1;
2282 if (t->dirent == NULL || t->dirent_allocated < dirent_size) {
2284 t->dirent = malloc(dirent_size);
2285 if (t->dirent == NULL) {
2287 t->d = INVALID_DIR_HANDLE;
2288 (void)tree_ascend(t);
2290 t->tree_errno = ENOMEM;
2291 t->visit_type = TREE_ERROR_DIR;
2292 return (t->visit_type);
2294 t->dirent_allocated = dirent_size;
2296 #endif /* HAVE_READDIR_R */
2299 #if defined(HAVE_READDIR_R)
2300 r = readdir_r(t->d, t->dirent, &t->de);
2301 if (r != 0 || t->de == NULL) {
2304 t->de = readdir(t->d);
2305 if (t->de == NULL) {
2309 t->d = INVALID_DIR_HANDLE;
2312 t->visit_type = TREE_ERROR_DIR;
2313 return (t->visit_type);
2317 name = t->de->d_name;
2318 namelen = D_NAMELEN(t->de);
2319 t->flags &= ~hasLstat;
2320 t->flags &= ~hasStat;
2321 if (name[0] == '.' && name[1] == '\0')
2323 if (name[0] == '.' && name[1] == '.' && name[2] == '\0')
2325 tree_append(t, name, namelen);
2326 return (t->visit_type = TREE_REGULAR);
2332 * Get the stat() data for the entry just returned from tree_next().
2334 static const struct stat *
2335 tree_current_stat(struct tree *t)
2337 if (!(t->flags & hasStat)) {
2339 if (fstatat(tree_current_dir_fd(t),
2340 tree_current_access_path(t), &t->st, 0) != 0)
2342 if (tree_enter_working_dir(t) != 0)
2344 if (stat(tree_current_access_path(t), &t->st) != 0)
2347 t->flags |= hasStat;
2353 * Get the lstat() data for the entry just returned from tree_next().
2355 static const struct stat *
2356 tree_current_lstat(struct tree *t)
2358 if (!(t->flags & hasLstat)) {
2360 if (fstatat(tree_current_dir_fd(t),
2361 tree_current_access_path(t), &t->lst,
2362 AT_SYMLINK_NOFOLLOW) != 0)
2364 if (tree_enter_working_dir(t) != 0)
2366 if (lstat(tree_current_access_path(t), &t->lst) != 0)
2369 t->flags |= hasLstat;
2375 * Test whether current entry is a dir or link to a dir.
2378 tree_current_is_dir(struct tree *t)
2380 const struct stat *st;
2382 * If we already have lstat() info, then try some
2383 * cheap tests to determine if this is a dir.
2385 if (t->flags & hasLstat) {
2386 /* If lstat() says it's a dir, it must be a dir. */
2387 st = tree_current_lstat(t);
2390 if (S_ISDIR(st->st_mode))
2392 /* Not a dir; might be a link to a dir. */
2393 /* If it's not a link, then it's not a link to a dir. */
2394 if (!S_ISLNK(tree_current_lstat(t)->st_mode))
2397 * It's a link, but we don't know what it's a link to,
2398 * so we'll have to use stat().
2402 st = tree_current_stat(t);
2403 /* If we can't stat it, it's not a dir. */
2406 /* Use the definitive test. Hopefully this is cached. */
2407 return (S_ISDIR(st->st_mode));
2411 * Test whether current entry is a physical directory. Usually, we
2412 * already have at least one of stat() or lstat() in memory, so we
2413 * use tricks to try to avoid an extra trip to the disk.
2416 tree_current_is_physical_dir(struct tree *t)
2418 const struct stat *st;
2421 * If stat() says it isn't a dir, then it's not a dir.
2422 * If stat() data is cached, this check is free, so do it first.
2424 if (t->flags & hasStat) {
2425 st = tree_current_stat(t);
2428 if (!S_ISDIR(st->st_mode))
2433 * Either stat() said it was a dir (in which case, we have
2434 * to determine whether it's really a link to a dir) or
2435 * stat() info wasn't available. So we use lstat(), which
2436 * hopefully is already cached.
2439 st = tree_current_lstat(t);
2440 /* If we can't stat it, it's not a dir. */
2443 /* Use the definitive test. Hopefully this is cached. */
2444 return (S_ISDIR(st->st_mode));
2448 * Test whether the same file has been in the tree as its parent.
2451 tree_target_is_same_as_parent(struct tree *t, const struct stat *st)
2453 struct tree_entry *te;
2455 for (te = t->current->parent; te != NULL; te = te->parent) {
2456 if (te->dev == (int64_t)st->st_dev &&
2457 te->ino == (int64_t)st->st_ino)
2464 * Test whether the current file is symbolic link target and
2465 * on the other filesystem.
2468 tree_current_is_symblic_link_target(struct tree *t)
2470 static const struct stat *lst, *st;
2472 lst = tree_current_lstat(t);
2473 st = tree_current_stat(t);
2474 return (st != NULL && lst != NULL &&
2475 (int64_t)st->st_dev == t->current_filesystem->dev &&
2476 st->st_dev != lst->st_dev);
2480 * Return the access path for the entry just returned from tree_next().
2483 tree_current_access_path(struct tree *t)
2485 return (t->basename);
2489 * Return the full path for the entry just returned from tree_next().
2492 tree_current_path(struct tree *t)
2498 * Terminate the traversal.
2501 tree_close(struct tree *t)
2506 if (t->entry_fd >= 0) {
2507 close_and_restore_time(t->entry_fd, t, &t->restore_time);
2510 /* Close the handle of readdir(). */
2511 if (t->d != INVALID_DIR_HANDLE) {
2513 t->d = INVALID_DIR_HANDLE;
2515 /* Release anything remaining in the stack. */
2516 while (t->stack != NULL) {
2517 if (t->stack->flags & isDirLink)
2518 close(t->stack->symlink_parent_fd);
2521 if (t->working_dir_fd >= 0) {
2522 close(t->working_dir_fd);
2523 t->working_dir_fd = -1;
2525 if (t->initial_dir_fd >= 0) {
2526 close(t->initial_dir_fd);
2527 t->initial_dir_fd = -1;
2532 * Release any resources.
2535 tree_free(struct tree *t)
2541 archive_string_free(&t->path);
2542 #if defined(HAVE_READDIR_R)
2545 free(t->sparse_list);
2546 for (i = 0; i < t->max_filesystem_id; i++)
2547 free(t->filesystem_table[i].allocation_ptr);
2548 free(t->filesystem_table);