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.
113 * This is a new directory-walking system that addresses a number
114 * of problems I've had with fts(3). In particular, it has no
115 * pathname-length limits (other than the size of 'int'), handles
116 * deep logical traversals, uses considerably less memory, and has
117 * an opaque interface (easier to modify in the future).
119 * Internally, it keeps a single list of "tree_entry" items that
120 * represent filesystem objects that require further attention.
121 * Non-directories are not kept in memory: they are pulled from
122 * readdir(), returned to the client, then freed as soon as possible.
123 * Any directory entry to be traversed gets pushed onto the stack.
125 * There is surprisingly little information that needs to be kept for
126 * each item on the stack. Just the name, depth (represented here as the
127 * string length of the parent directory's pathname), and some markers
128 * indicating how to get back to the parent (via chdir("..") for a
129 * regular dir or via fchdir(2) for a symlink).
134 * 3) Arbitrary logical traversals by closing/reopening intermediate fds.
137 struct restore_time {
149 struct tree_entry *next;
150 struct tree_entry *parent;
151 struct archive_string name;
152 size_t dirname_length;
157 /* How to return back to the parent of a symlink. */
158 int symlink_parent_fd;
159 /* How to restore time of a directory. */
160 struct restore_time restore_time;
168 #if defined(HAVE_READDIR_R)
177 * Buffer used for reading file contents.
179 /* Exactly allocated memory pointer. */
180 unsigned char *allocation_ptr;
181 /* Pointer adjusted to the filesystem alignment . */
186 /* Definitions for tree_entry.flags bitmap. */
187 #define isDir 1 /* This entry is a regular directory. */
188 #define isDirLink 2 /* This entry is a symbolic link to a directory. */
189 #define needsFirstVisit 4 /* This is an initial entry. */
190 #define needsDescent 8 /* This entry needs to be previsited. */
191 #define needsOpen 16 /* This is a directory that needs to be opened. */
192 #define needsAscent 32 /* This entry needs to be postvisited. */
195 * Local data for this package.
198 struct tree_entry *stack;
199 struct tree_entry *current;
201 #define INVALID_DIR_HANDLE NULL
203 #if defined(HAVE_READDIR_R)
204 struct dirent *dirent;
205 size_t dirent_allocated;
209 /* Error code from last failed operation. */
212 /* Dynamically-sized buffer for holding path */
213 struct archive_string path;
215 /* Last path element */
216 const char *basename;
217 /* Leading dir length */
218 size_t dirname_length;
230 /* How to restore time of a file. */
231 struct restore_time restore_time;
233 struct entry_sparse {
236 } *sparse_list, *current_sparse;
238 int sparse_list_size;
240 char initial_symlink_mode;
242 struct filesystem *current_filesystem;
243 struct filesystem *filesystem_table;
244 int initial_filesystem_id;
245 int current_filesystem_id;
246 int max_filesystem_id;
247 int allocated_filesytem;
251 int64_t entry_remaining_bytes;
253 unsigned char *entry_buff;
254 size_t entry_buff_size;
257 /* Definitions for tree.flags bitmap. */
258 #define hasStat 16 /* The st entry is valid. */
259 #define hasLstat 32 /* The lst entry is valid. */
260 #define onWorkingDir 64 /* We are on the working dir where we are
261 * reading directory entry at this time. */
262 #define needsRestoreTimes 128
263 #define onInitialDir 256 /* We are on the initial dir. */
266 tree_dir_next_posix(struct tree *t);
268 #ifdef HAVE_DIRENT_D_NAMLEN
269 /* BSD extension; avoids need for a strlen() call. */
270 #define D_NAMELEN(dp) (dp)->d_namlen
272 #define D_NAMELEN(dp) (strlen((dp)->d_name))
275 /* Initiate/terminate a tree traversal. */
276 static struct tree *tree_open(const char *, int, int);
277 static struct tree *tree_reopen(struct tree *, const char *, int);
278 static void tree_close(struct tree *);
279 static void tree_free(struct tree *);
280 static void tree_push(struct tree *, const char *, int, int64_t, int64_t,
281 struct restore_time *);
282 static int tree_enter_initial_dir(struct tree *);
283 static int tree_enter_working_dir(struct tree *);
284 static int tree_current_dir_fd(struct tree *);
287 * tree_next() returns Zero if there is no next entry, non-zero if
288 * there is. Note that directories are visited three times.
289 * Directories are always visited first as part of enumerating their
290 * parent; that is a "regular" visit. If tree_descend() is invoked at
291 * that time, the directory is added to a work list and will
292 * subsequently be visited two more times: once just after descending
293 * into the directory ("postdescent") and again just after ascending
294 * back to the parent ("postascent").
296 * TREE_ERROR_DIR is returned if the descent failed (because the
297 * directory couldn't be opened, for instance). This is returned
298 * instead of TREE_POSTDESCENT/TREE_POSTASCENT. TREE_ERROR_DIR is not a
299 * fatal error, but it does imply that the relevant subtree won't be
300 * visited. TREE_ERROR_FATAL is returned for an error that left the
301 * traversal completely hosed. Right now, this is only returned for
302 * chdir() failures during ascent.
304 #define TREE_REGULAR 1
305 #define TREE_POSTDESCENT 2
306 #define TREE_POSTASCENT 3
307 #define TREE_ERROR_DIR -1
308 #define TREE_ERROR_FATAL -2
310 static int tree_next(struct tree *);
313 * Return information about the current entry.
317 * The current full pathname, length of the full pathname, and a name
318 * that can be used to access the file. Because tree does use chdir
319 * extensively, the access path is almost never the same as the full
322 * TODO: On platforms that support it, use openat()-style operations
323 * to eliminate the chdir() operations entirely while still supporting
324 * arbitrarily deep traversals. This makes access_path troublesome to
325 * support, of course, which means we'll need a rich enough interface
326 * that clients can function without it. (In particular, we'll need
327 * tree_current_open() that returns an open file descriptor.)
330 static const char *tree_current_path(struct tree *);
331 static const char *tree_current_access_path(struct tree *);
334 * Request the lstat() or stat() data for the current path. Since the
335 * tree package needs to do some of this anyway, and caches the
336 * results, you should take advantage of it here if you need it rather
337 * than make a redundant stat() or lstat() call of your own.
339 static const struct stat *tree_current_stat(struct tree *);
340 static const struct stat *tree_current_lstat(struct tree *);
341 static int tree_current_is_symblic_link_target(struct tree *);
343 /* The following functions use tricks to avoid a certain number of
344 * stat()/lstat() calls. */
345 /* "is_physical_dir" is equivalent to S_ISDIR(tree_current_lstat()->st_mode) */
346 static int tree_current_is_physical_dir(struct tree *);
347 /* "is_dir" is equivalent to S_ISDIR(tree_current_stat()->st_mode) */
348 static int tree_current_is_dir(struct tree *);
349 static int update_current_filesystem(struct archive_read_disk *a,
351 static int setup_current_filesystem(struct archive_read_disk *);
352 static int tree_target_is_same_as_parent(struct tree *, const struct stat *);
354 static int _archive_read_disk_open(struct archive *, const char *);
355 static int _archive_read_free(struct archive *);
356 static int _archive_read_close(struct archive *);
357 static int _archive_read_data_block(struct archive *,
358 const void **, size_t *, int64_t *);
359 static int _archive_read_next_header2(struct archive *,
360 struct archive_entry *);
361 static const char *trivial_lookup_gname(void *, int64_t gid);
362 static const char *trivial_lookup_uname(void *, int64_t uid);
363 static int setup_sparse(struct archive_read_disk *, struct archive_entry *);
364 static int close_and_restore_time(int fd, struct tree *,
365 struct restore_time *);
366 static int open_on_current_dir(struct tree *, const char *, int);
367 static int tree_dup(int);
370 static struct archive_vtable *
371 archive_read_disk_vtable(void)
373 static struct archive_vtable av;
374 static int inited = 0;
377 av.archive_free = _archive_read_free;
378 av.archive_close = _archive_read_close;
379 av.archive_read_data_block = _archive_read_data_block;
380 av.archive_read_next_header2 = _archive_read_next_header2;
387 archive_read_disk_gname(struct archive *_a, 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, 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, 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, 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->lookup_uname = trivial_lookup_uname;
463 a->lookup_gname = trivial_lookup_gname;
464 a->enable_copyfile = 1;
465 a->traverse_mount_points = 1;
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 a->archive.magic = 0;
495 __archive_clean(&a->archive);
501 _archive_read_close(struct archive *_a)
503 struct archive_read_disk *a = (struct archive_read_disk *)_a;
505 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
506 ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_close");
508 if (a->archive.state != ARCHIVE_STATE_FATAL)
509 a->archive.state = ARCHIVE_STATE_CLOSED;
517 setup_symlink_mode(struct archive_read_disk *a, char symlink_mode,
520 a->symlink_mode = symlink_mode;
521 a->follow_symlinks = follow_symlinks;
522 if (a->tree != NULL) {
523 a->tree->initial_symlink_mode = a->symlink_mode;
524 a->tree->symlink_mode = a->symlink_mode;
529 archive_read_disk_set_symlink_logical(struct archive *_a)
531 struct archive_read_disk *a = (struct archive_read_disk *)_a;
532 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
533 ARCHIVE_STATE_ANY, "archive_read_disk_set_symlink_logical");
534 setup_symlink_mode(a, 'L', 1);
539 archive_read_disk_set_symlink_physical(struct archive *_a)
541 struct archive_read_disk *a = (struct archive_read_disk *)_a;
542 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
543 ARCHIVE_STATE_ANY, "archive_read_disk_set_symlink_physical");
544 setup_symlink_mode(a, 'P', 0);
549 archive_read_disk_set_symlink_hybrid(struct archive *_a)
551 struct archive_read_disk *a = (struct archive_read_disk *)_a;
552 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
553 ARCHIVE_STATE_ANY, "archive_read_disk_set_symlink_hybrid");
554 setup_symlink_mode(a, 'H', 1);/* Follow symlinks initially. */
559 archive_read_disk_set_atime_restored(struct archive *_a)
562 static int warning_done = 0;
564 struct archive_read_disk *a = (struct archive_read_disk *)_a;
565 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
566 ARCHIVE_STATE_ANY, "archive_read_disk_restore_atime");
570 a->tree->flags |= needsRestoreTimes;
574 /* Warning was already emitted; suppress further warnings. */
577 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
578 "Cannot restore access time on this system");
580 return (ARCHIVE_WARN);
585 archive_read_disk_set_behavior(struct archive *_a, int flags)
587 struct archive_read_disk *a = (struct archive_read_disk *)_a;
590 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
591 ARCHIVE_STATE_ANY, "archive_read_disk_honor_nodump");
593 if (flags & ARCHIVE_READDISK_RESTORE_ATIME)
594 r = archive_read_disk_set_atime_restored(_a);
598 a->tree->flags &= ~needsRestoreTimes;
600 if (flags & ARCHIVE_READDISK_HONOR_NODUMP)
604 if (flags & ARCHIVE_READDISK_MAC_COPYFILE)
605 a->enable_copyfile = 1;
607 a->enable_copyfile = 0;
608 if (flags & ARCHIVE_READDISK_NO_TRAVERSE_MOUNTS)
609 a->traverse_mount_points = 0;
611 a->traverse_mount_points = 1;
616 * Trivial implementations of gname/uname lookup functions.
617 * These are normally overridden by the client, but these stub
618 * versions ensure that we always have something that works.
621 trivial_lookup_gname(void *private_data, int64_t gid)
623 (void)private_data; /* UNUSED */
624 (void)gid; /* UNUSED */
629 trivial_lookup_uname(void *private_data, int64_t uid)
631 (void)private_data; /* UNUSED */
632 (void)uid; /* UNUSED */
637 * Allocate memory for the reading buffer adjusted to the filesystem
641 setup_suitable_read_buffer(struct archive_read_disk *a)
643 struct tree *t = a->tree;
644 struct filesystem *cf = t->current_filesystem;
648 if (cf->allocation_ptr == NULL) {
649 /* If we couldn't get a filesystem alignment,
650 * we use 4096 as default value but we won't use
651 * O_DIRECT to open() and openat() operations. */
652 long xfer_align = (cf->xfer_align == -1)?4096:cf->xfer_align;
654 if (cf->max_xfer_size != -1)
655 asize = cf->max_xfer_size + xfer_align;
657 long incr = cf->incr_xfer_size;
658 /* Some platform does not set a proper value to
661 incr = cf->min_xfer_size;
662 if (cf->min_xfer_size < 0) {
666 asize = cf->min_xfer_size;
668 /* Increase a buffer size up to 64K bytes in
669 * a proper incremant size. */
670 while (asize < 1024*64)
672 /* Take a margin to adjust to the filesystem
676 cf->allocation_ptr = malloc(asize);
677 if (cf->allocation_ptr == NULL) {
678 archive_set_error(&a->archive, ENOMEM,
679 "Couldn't allocate memory");
680 a->archive.state = ARCHIVE_STATE_FATAL;
681 return (ARCHIVE_FATAL);
685 * Calculate proper address for the filesystem.
687 s = (uintptr_t)cf->allocation_ptr;
693 * Set a read buffer pointer in the proper alignment of
694 * the current filesystem.
696 cf->buff = cf->allocation_ptr + s;
697 cf->buff_size = asize - xfer_align;
703 _archive_read_data_block(struct archive *_a, const void **buff,
704 size_t *size, int64_t *offset)
706 struct archive_read_disk *a = (struct archive_read_disk *)_a;
707 struct tree *t = a->tree;
712 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA,
713 "archive_read_data_block");
715 if (t->entry_eof || t->entry_remaining_bytes <= 0) {
717 goto abort_read_data;
721 * Open the current file.
723 if (t->entry_fd < 0) {
724 int flags = O_RDONLY | O_BINARY | O_CLOEXEC;
727 * Eliminate or reduce cache effects if we can.
729 * Carefully consider this to be enabled.
731 #if defined(O_DIRECT) && 0/* Disabled for now */
732 if (t->current_filesystem->xfer_align != -1 &&
736 #if defined(O_NOATIME)
738 * Linux has O_NOATIME flag; use it if we need.
740 if ((t->flags & needsRestoreTimes) != 0 &&
741 t->restore_time.noatime == 0)
745 t->entry_fd = open_on_current_dir(t,
746 tree_current_access_path(t), flags);
747 __archive_ensure_cloexec_flag(t->entry_fd);
748 #if defined(O_NOATIME)
750 * When we did open the file with O_NOATIME flag,
751 * if successful, set 1 to t->restore_time.noatime
752 * not to restore an atime of the file later.
753 * if failed by EPERM, retry it without O_NOATIME flag.
755 if (flags & O_NOATIME) {
756 if (t->entry_fd >= 0)
757 t->restore_time.noatime = 1;
758 else if (errno == EPERM) {
765 if (t->entry_fd < 0) {
766 archive_set_error(&a->archive, errno,
767 "Couldn't open %s", tree_current_path(t));
769 tree_enter_initial_dir(t);
770 goto abort_read_data;
772 tree_enter_initial_dir(t);
776 * Allocate read buffer if not allocated.
778 if (t->current_filesystem->allocation_ptr == NULL) {
779 r = setup_suitable_read_buffer(a);
780 if (r != ARCHIVE_OK) {
781 a->archive.state = ARCHIVE_STATE_FATAL;
782 goto abort_read_data;
785 t->entry_buff = t->current_filesystem->buff;
786 t->entry_buff_size = t->current_filesystem->buff_size;
788 buffbytes = t->entry_buff_size;
789 if ((int64_t)buffbytes > t->current_sparse->length)
790 buffbytes = t->current_sparse->length;
794 * TODO: Should we consider t->current_filesystem->xfer_align?
796 if (t->current_sparse->offset > t->entry_total) {
797 if (lseek(t->entry_fd,
798 (off_t)t->current_sparse->offset, SEEK_SET) < 0) {
799 archive_set_error(&a->archive, errno, "Seek error");
801 a->archive.state = ARCHIVE_STATE_FATAL;
802 goto abort_read_data;
804 bytes = t->current_sparse->offset - t->entry_total;
805 t->entry_remaining_bytes -= bytes;
806 t->entry_total += bytes;
810 * Read file contents.
813 bytes = read(t->entry_fd, t->entry_buff, buffbytes);
815 archive_set_error(&a->archive, errno, "Read error");
817 a->archive.state = ARCHIVE_STATE_FATAL;
818 goto abort_read_data;
826 goto abort_read_data;
828 *buff = t->entry_buff;
830 *offset = t->entry_total;
831 t->entry_total += bytes;
832 t->entry_remaining_bytes -= bytes;
833 if (t->entry_remaining_bytes == 0) {
834 /* Close the current file descriptor */
835 close_and_restore_time(t->entry_fd, t, &t->restore_time);
839 t->current_sparse->offset += bytes;
840 t->current_sparse->length -= bytes;
841 if (t->current_sparse->length == 0 && !t->entry_eof)
848 *offset = t->entry_total;
849 if (t->entry_fd >= 0) {
850 /* Close the current file descriptor */
851 close_and_restore_time(t->entry_fd, t, &t->restore_time);
858 next_entry(struct archive_read_disk *a, struct tree *t,
859 struct archive_entry *entry)
861 const struct stat *st; /* info to use for this entry */
862 const struct stat *lst;/* lstat() information */
870 switch (tree_next(t)) {
871 case TREE_ERROR_FATAL:
872 archive_set_error(&a->archive, t->tree_errno,
873 "%s: Unable to continue traversing directory tree",
874 tree_current_path(t));
875 a->archive.state = ARCHIVE_STATE_FATAL;
876 tree_enter_initial_dir(t);
877 return (ARCHIVE_FATAL);
879 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
880 "%s: Couldn't visit directory",
881 tree_current_path(t));
882 tree_enter_initial_dir(t);
883 return (ARCHIVE_FAILED);
885 tree_enter_initial_dir(t);
886 return (ARCHIVE_EOF);
887 case TREE_POSTDESCENT:
888 case TREE_POSTASCENT:
891 lst = tree_current_lstat(t);
893 archive_set_error(&a->archive, errno,
895 tree_current_path(t));
896 tree_enter_initial_dir(t);
897 return (ARCHIVE_FAILED);
901 } while (lst == NULL);
904 if (a->enable_copyfile) {
905 /* If we're using copyfile(), ignore "._XXX" files. */
906 const char *bname = strrchr(tree_current_path(t), '/');
908 bname = tree_current_path(t);
911 if (bname[0] == '.' && bname[1] == '_')
912 return (ARCHIVE_RETRY);
916 archive_entry_copy_pathname(entry, tree_current_path(t));
918 * Perform path matching.
921 r = archive_match_path_excluded(a->matching, entry);
923 archive_set_error(&(a->archive), errno,
924 "Faild : %s", archive_error_string(a->matching));
928 if (a->excluded_cb_func)
929 a->excluded_cb_func(&(a->archive),
930 a->excluded_cb_data, entry);
931 return (ARCHIVE_RETRY);
936 * Distinguish 'L'/'P'/'H' symlink following.
938 switch(t->symlink_mode) {
940 /* 'H': After the first item, rest like 'P'. */
941 t->symlink_mode = 'P';
942 /* 'H': First item (from command line) like 'L'. */
945 /* 'L': Do descend through a symlink to dir. */
946 descend = tree_current_is_dir(t);
947 /* 'L': Follow symlinks to files. */
948 a->symlink_mode = 'L';
949 a->follow_symlinks = 1;
950 /* 'L': Archive symlinks as targets, if we can. */
951 st = tree_current_stat(t);
952 if (st != NULL && !tree_target_is_same_as_parent(t, st))
954 /* If stat fails, we have a broken symlink;
955 * in that case, don't follow the link. */
958 /* 'P': Don't descend through a symlink to dir. */
959 descend = tree_current_is_physical_dir(t);
960 /* 'P': Don't follow symlinks to files. */
961 a->symlink_mode = 'P';
962 a->follow_symlinks = 0;
963 /* 'P': Archive symlinks as symlinks. */
968 if (update_current_filesystem(a, st->st_dev) != ARCHIVE_OK) {
969 a->archive.state = ARCHIVE_STATE_FATAL;
970 tree_enter_initial_dir(t);
971 return (ARCHIVE_FATAL);
973 if (t->initial_filesystem_id == -1)
974 t->initial_filesystem_id = t->current_filesystem_id;
975 if (!a->traverse_mount_points) {
976 if (t->initial_filesystem_id != t->current_filesystem_id)
977 return (ARCHIVE_RETRY);
979 t->descend = descend;
983 * If the file is marked with nodump flag, do not return this entry.
985 if (a->honor_nodump) {
986 #if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP)
987 if (st->st_flags & UF_NODUMP)
988 return (ARCHIVE_RETRY);
989 #elif defined(EXT2_IOC_GETFLAGS) && defined(EXT2_NODUMP_FL) &&\
990 defined(HAVE_WORKING_EXT2_IOC_GETFLAGS)
991 if (S_ISREG(st->st_mode) || S_ISDIR(st->st_mode)) {
994 t->entry_fd = open_on_current_dir(t,
995 tree_current_access_path(t),
996 O_RDONLY | O_NONBLOCK | O_CLOEXEC);
997 __archive_ensure_cloexec_flag(t->entry_fd);
998 if (t->entry_fd >= 0) {
999 r = ioctl(t->entry_fd, EXT2_IOC_GETFLAGS,
1001 if (r == 0 && (stflags & EXT2_NODUMP_FL) != 0)
1002 return (ARCHIVE_RETRY);
1008 archive_entry_copy_stat(entry, st);
1010 /* Save the times to be restored. This must be in before
1011 * calling archive_read_disk_descend() or any chance of it,
1012 * especially, invokng a callback. */
1013 t->restore_time.mtime = archive_entry_mtime(entry);
1014 t->restore_time.mtime_nsec = archive_entry_mtime_nsec(entry);
1015 t->restore_time.atime = archive_entry_atime(entry);
1016 t->restore_time.atime_nsec = archive_entry_atime_nsec(entry);
1017 t->restore_time.filetype = archive_entry_filetype(entry);
1018 t->restore_time.noatime = t->current_filesystem->noatime;
1021 * Perform time matching.
1024 r = archive_match_time_excluded(a->matching, entry);
1026 archive_set_error(&(a->archive), errno,
1027 "Faild : %s", archive_error_string(a->matching));
1031 if (a->excluded_cb_func)
1032 a->excluded_cb_func(&(a->archive),
1033 a->excluded_cb_data, entry);
1034 return (ARCHIVE_RETRY);
1038 /* Lookup uname/gname */
1039 name = archive_read_disk_uname(&(a->archive), archive_entry_uid(entry));
1041 archive_entry_copy_uname(entry, name);
1042 name = archive_read_disk_gname(&(a->archive), archive_entry_gid(entry));
1044 archive_entry_copy_gname(entry, name);
1047 * Perform owner matching.
1050 r = archive_match_owner_excluded(a->matching, entry);
1052 archive_set_error(&(a->archive), errno,
1053 "Faild : %s", archive_error_string(a->matching));
1057 if (a->excluded_cb_func)
1058 a->excluded_cb_func(&(a->archive),
1059 a->excluded_cb_data, entry);
1060 return (ARCHIVE_RETRY);
1065 * Invoke a meta data filter callback.
1067 if (a->metadata_filter_func) {
1068 if (!a->metadata_filter_func(&(a->archive),
1069 a->metadata_filter_data, entry))
1070 return (ARCHIVE_RETRY);
1074 * Populate the archive_entry with metadata from the disk.
1076 archive_entry_copy_sourcepath(entry, tree_current_access_path(t));
1077 r = archive_read_disk_entry_from_file(&(a->archive), entry,
1084 _archive_read_next_header2(struct archive *_a, struct archive_entry *entry)
1086 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1090 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
1091 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
1092 "archive_read_next_header2");
1095 if (t->entry_fd >= 0) {
1096 close_and_restore_time(t->entry_fd, t, &t->restore_time);
1101 r = next_entry(a, t, entry);
1102 if (t->entry_fd >= 0) {
1107 if (r == ARCHIVE_RETRY) {
1108 archive_entry_clear(entry);
1114 /* Return to the initial directory. */
1115 tree_enter_initial_dir(t);
1118 * EOF and FATAL are persistent at this layer. By
1119 * modifying the state, we guarantee that future calls to
1120 * read a header or read data will fail.
1124 a->archive.state = ARCHIVE_STATE_EOF;
1128 /* Overwrite the sourcepath based on the initial directory. */
1129 archive_entry_copy_sourcepath(entry, tree_current_path(t));
1131 if (archive_entry_filetype(entry) == AE_IFREG) {
1132 t->nlink = archive_entry_nlink(entry);
1133 t->entry_remaining_bytes = archive_entry_size(entry);
1134 t->entry_eof = (t->entry_remaining_bytes == 0)? 1: 0;
1135 if (!t->entry_eof &&
1136 setup_sparse(a, entry) != ARCHIVE_OK)
1137 return (ARCHIVE_FATAL);
1139 t->entry_remaining_bytes = 0;
1142 a->archive.state = ARCHIVE_STATE_DATA;
1147 a->archive.state = ARCHIVE_STATE_FATAL;
1155 setup_sparse(struct archive_read_disk *a, struct archive_entry *entry)
1157 struct tree *t = a->tree;
1158 int64_t length, offset;
1161 t->sparse_count = archive_entry_sparse_reset(entry);
1162 if (t->sparse_count+1 > t->sparse_list_size) {
1163 free(t->sparse_list);
1164 t->sparse_list_size = t->sparse_count + 1;
1165 t->sparse_list = malloc(sizeof(t->sparse_list[0]) *
1166 t->sparse_list_size);
1167 if (t->sparse_list == NULL) {
1168 t->sparse_list_size = 0;
1169 archive_set_error(&a->archive, ENOMEM,
1170 "Can't allocate data");
1171 a->archive.state = ARCHIVE_STATE_FATAL;
1172 return (ARCHIVE_FATAL);
1175 for (i = 0; i < t->sparse_count; i++) {
1176 archive_entry_sparse_next(entry, &offset, &length);
1177 t->sparse_list[i].offset = offset;
1178 t->sparse_list[i].length = length;
1181 t->sparse_list[i].offset = 0;
1182 t->sparse_list[i].length = archive_entry_size(entry);
1184 t->sparse_list[i].offset = archive_entry_size(entry);
1185 t->sparse_list[i].length = 0;
1187 t->current_sparse = t->sparse_list;
1189 return (ARCHIVE_OK);
1193 archive_read_disk_set_matching(struct archive *_a, struct archive *_ma,
1194 void (*_excluded_func)(struct archive *, void *, struct archive_entry *),
1197 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1198 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
1199 ARCHIVE_STATE_ANY, "archive_read_disk_set_matching");
1201 a->excluded_cb_func = _excluded_func;
1202 a->excluded_cb_data = _client_data;
1203 return (ARCHIVE_OK);
1207 archive_read_disk_set_metadata_filter_callback(struct archive *_a,
1208 int (*_metadata_filter_func)(struct archive *, void *,
1209 struct archive_entry *), void *_client_data)
1211 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1213 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_ANY,
1214 "archive_read_disk_set_metadata_filter_callback");
1216 a->metadata_filter_func = _metadata_filter_func;
1217 a->metadata_filter_data = _client_data;
1218 return (ARCHIVE_OK);
1222 archive_read_disk_can_descend(struct archive *_a)
1224 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1225 struct tree *t = a->tree;
1227 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
1228 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
1229 "archive_read_disk_can_descend");
1231 return (t->visit_type == TREE_REGULAR && t->descend);
1235 * Called by the client to mark the directory just returned from
1236 * tree_next() as needing to be visited.
1239 archive_read_disk_descend(struct archive *_a)
1241 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1242 struct tree *t = a->tree;
1244 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
1245 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
1246 "archive_read_disk_descend");
1248 if (t->visit_type != TREE_REGULAR || !t->descend)
1249 return (ARCHIVE_OK);
1251 if (tree_current_is_physical_dir(t)) {
1252 tree_push(t, t->basename, t->current_filesystem_id,
1253 t->lst.st_dev, t->lst.st_ino, &t->restore_time);
1254 t->stack->flags |= isDir;
1255 } else if (tree_current_is_dir(t)) {
1256 tree_push(t, t->basename, t->current_filesystem_id,
1257 t->st.st_dev, t->st.st_ino, &t->restore_time);
1258 t->stack->flags |= isDirLink;
1261 return (ARCHIVE_OK);
1265 archive_read_disk_open(struct archive *_a, const char *pathname)
1267 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1269 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
1270 ARCHIVE_STATE_NEW | ARCHIVE_STATE_CLOSED,
1271 "archive_read_disk_open");
1272 archive_clear_error(&a->archive);
1274 return (_archive_read_disk_open(_a, pathname));
1278 archive_read_disk_open_w(struct archive *_a, const wchar_t *pathname)
1280 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1281 struct archive_string path;
1284 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
1285 ARCHIVE_STATE_NEW | ARCHIVE_STATE_CLOSED,
1286 "archive_read_disk_open_w");
1287 archive_clear_error(&a->archive);
1289 /* Make a char string from a wchar_t string. */
1290 archive_string_init(&path);
1291 if (archive_string_append_from_wcs(&path, pathname,
1292 wcslen(pathname)) != 0) {
1293 if (errno == ENOMEM)
1294 archive_set_error(&a->archive, ENOMEM,
1295 "Can't allocate memory");
1297 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1298 "Can't convert a path to a char string");
1299 a->archive.state = ARCHIVE_STATE_FATAL;
1300 ret = ARCHIVE_FATAL;
1302 ret = _archive_read_disk_open(_a, path.s);
1304 archive_string_free(&path);
1309 _archive_read_disk_open(struct archive *_a, const char *pathname)
1311 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1313 if (a->tree != NULL)
1314 a->tree = tree_reopen(a->tree, pathname, a->restore_time);
1316 a->tree = tree_open(pathname, a->symlink_mode,
1318 if (a->tree == NULL) {
1319 archive_set_error(&a->archive, ENOMEM,
1320 "Can't allocate tar data");
1321 a->archive.state = ARCHIVE_STATE_FATAL;
1322 return (ARCHIVE_FATAL);
1324 a->archive.state = ARCHIVE_STATE_HEADER;
1326 return (ARCHIVE_OK);
1330 * Return a current filesystem ID which is index of the filesystem entry
1331 * you've visited through archive_read_disk.
1334 archive_read_disk_current_filesystem(struct archive *_a)
1336 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1338 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA,
1339 "archive_read_disk_current_filesystem");
1341 return (a->tree->current_filesystem_id);
1345 update_current_filesystem(struct archive_read_disk *a, int64_t dev)
1347 struct tree *t = a->tree;
1350 if (t->current_filesystem != NULL &&
1351 t->current_filesystem->dev == dev)
1352 return (ARCHIVE_OK);
1354 for (i = 0; i < t->max_filesystem_id; i++) {
1355 if (t->filesystem_table[i].dev == dev) {
1356 /* There is the filesytem ID we've already generated. */
1357 t->current_filesystem_id = i;
1358 t->current_filesystem = &(t->filesystem_table[i]);
1359 return (ARCHIVE_OK);
1364 * This is the new filesytem which we have to generate a new ID for.
1366 fid = t->max_filesystem_id++;
1367 if (t->max_filesystem_id > t->allocated_filesytem) {
1371 s = t->max_filesystem_id * 2;
1372 p = realloc(t->filesystem_table,
1373 s * sizeof(*t->filesystem_table));
1375 archive_set_error(&a->archive, ENOMEM,
1376 "Can't allocate tar data");
1377 return (ARCHIVE_FATAL);
1379 t->filesystem_table = (struct filesystem *)p;
1380 t->allocated_filesytem = s;
1382 t->current_filesystem_id = fid;
1383 t->current_filesystem = &(t->filesystem_table[fid]);
1384 t->current_filesystem->dev = dev;
1385 t->current_filesystem->allocation_ptr = NULL;
1386 t->current_filesystem->buff = NULL;
1388 /* Setup the current filesystem properties which depend on
1389 * platform specific. */
1390 return (setup_current_filesystem(a));
1394 * Returns 1 if current filesystem is generated filesystem, 0 if it is not
1395 * or -1 if it is unknown.
1398 archive_read_disk_current_filesystem_is_synthetic(struct archive *_a)
1400 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1402 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA,
1403 "archive_read_disk_current_filesystem");
1405 return (a->tree->current_filesystem->synthetic);
1409 * Returns 1 if current filesystem is remote filesystem, 0 if it is not
1410 * or -1 if it is unknown.
1413 archive_read_disk_current_filesystem_is_remote(struct archive *_a)
1415 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1417 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA,
1418 "archive_read_disk_current_filesystem");
1420 return (a->tree->current_filesystem->remote);
1423 #if defined(_PC_REC_INCR_XFER_SIZE) && defined(_PC_REC_MAX_XFER_SIZE) &&\
1424 defined(_PC_REC_MIN_XFER_SIZE) && defined(_PC_REC_XFER_ALIGN)
1426 get_xfer_size(struct tree *t, int fd, const char *path)
1428 t->current_filesystem->xfer_align = -1;
1431 t->current_filesystem->incr_xfer_size =
1432 fpathconf(fd, _PC_REC_INCR_XFER_SIZE);
1433 t->current_filesystem->max_xfer_size =
1434 fpathconf(fd, _PC_REC_MAX_XFER_SIZE);
1435 t->current_filesystem->min_xfer_size =
1436 fpathconf(fd, _PC_REC_MIN_XFER_SIZE);
1437 t->current_filesystem->xfer_align =
1438 fpathconf(fd, _PC_REC_XFER_ALIGN);
1439 } else if (path != NULL) {
1440 t->current_filesystem->incr_xfer_size =
1441 pathconf(path, _PC_REC_INCR_XFER_SIZE);
1442 t->current_filesystem->max_xfer_size =
1443 pathconf(path, _PC_REC_MAX_XFER_SIZE);
1444 t->current_filesystem->min_xfer_size =
1445 pathconf(path, _PC_REC_MIN_XFER_SIZE);
1446 t->current_filesystem->xfer_align =
1447 pathconf(path, _PC_REC_XFER_ALIGN);
1449 /* At least we need an alignment size. */
1450 if (t->current_filesystem->xfer_align == -1)
1451 return ((errno == EINVAL)?1:-1);
1457 get_xfer_size(struct tree *t, int fd, const char *path)
1459 (void)t; /* UNUSED */
1460 (void)fd; /* UNUSED */
1461 (void)path; /* UNUSED */
1462 return (1);/* Not supported */
1466 #if defined(HAVE_STATFS) && defined(HAVE_FSTATFS) && defined(MNT_LOCAL) \
1467 && !defined(ST_LOCAL)
1470 * Gather current filesystem properties on FreeBSD, OpenBSD and Mac OS X.
1473 setup_current_filesystem(struct archive_read_disk *a)
1475 struct tree *t = a->tree;
1477 #if defined(HAVE_GETVFSBYNAME) && defined(VFCF_SYNTHETIC)
1478 struct xvfsconf vfc;
1481 #if !defined(HAVE_STRUCT_STATFS_F_NAMEMAX)
1485 t->current_filesystem->synthetic = -1;
1486 t->current_filesystem->remote = -1;
1487 if (tree_current_is_symblic_link_target(t)) {
1488 #if defined(HAVE_OPENAT)
1490 * Get file system statistics on any directory
1493 int fd = openat(tree_current_dir_fd(t),
1494 tree_current_access_path(t), O_RDONLY | O_CLOEXEC);
1495 __archive_ensure_cloexec_flag(fd);
1497 archive_set_error(&a->archive, errno,
1499 return (ARCHIVE_FAILED);
1501 r = fstatfs(fd, &sfs);
1503 xr = get_xfer_size(t, fd, NULL);
1506 if (tree_enter_working_dir(t) != 0) {
1507 archive_set_error(&a->archive, errno, "fchdir failed");
1508 return (ARCHIVE_FAILED);
1510 r = statfs(tree_current_access_path(t), &sfs);
1512 xr = get_xfer_size(t, -1, tree_current_access_path(t));
1515 r = fstatfs(tree_current_dir_fd(t), &sfs);
1517 xr = get_xfer_size(t, tree_current_dir_fd(t), NULL);
1519 if (r == -1 || xr == -1) {
1520 archive_set_error(&a->archive, errno, "statfs failed");
1521 return (ARCHIVE_FAILED);
1522 } else if (xr == 1) {
1523 /* pathconf(_PC_REX_*) operations are not supported. */
1524 t->current_filesystem->xfer_align = sfs.f_bsize;
1525 t->current_filesystem->max_xfer_size = -1;
1526 t->current_filesystem->min_xfer_size = sfs.f_iosize;
1527 t->current_filesystem->incr_xfer_size = sfs.f_iosize;
1529 if (sfs.f_flags & MNT_LOCAL)
1530 t->current_filesystem->remote = 0;
1532 t->current_filesystem->remote = 1;
1534 #if defined(HAVE_GETVFSBYNAME) && defined(VFCF_SYNTHETIC)
1535 r = getvfsbyname(sfs.f_fstypename, &vfc);
1537 archive_set_error(&a->archive, errno, "getvfsbyname failed");
1538 return (ARCHIVE_FAILED);
1540 if (vfc.vfc_flags & VFCF_SYNTHETIC)
1541 t->current_filesystem->synthetic = 1;
1543 t->current_filesystem->synthetic = 0;
1546 #if defined(MNT_NOATIME)
1547 if (sfs.f_flags & MNT_NOATIME)
1548 t->current_filesystem->noatime = 1;
1551 t->current_filesystem->noatime = 0;
1553 #if defined(HAVE_READDIR_R)
1554 /* Set maximum filename length. */
1555 #if defined(HAVE_STRUCT_STATFS_F_NAMEMAX)
1556 t->current_filesystem->name_max = sfs.f_namemax;
1558 /* Mac OS X does not have f_namemax in struct statfs. */
1559 if (tree_current_is_symblic_link_target(t)) {
1560 if (tree_enter_working_dir(t) != 0) {
1561 archive_set_error(&a->archive, errno, "fchdir failed");
1562 return (ARCHIVE_FAILED);
1564 nm = pathconf(tree_current_access_path(t), _PC_NAME_MAX);
1566 nm = fpathconf(tree_current_dir_fd(t), _PC_NAME_MAX);
1568 t->current_filesystem->name_max = NAME_MAX;
1570 t->current_filesystem->name_max = nm;
1572 #endif /* HAVE_READDIR_R */
1573 return (ARCHIVE_OK);
1576 #elif (defined(HAVE_STATVFS) || defined(HAVE_FSTATVFS)) && defined(ST_LOCAL)
1579 * Gather current filesystem properties on NetBSD
1582 setup_current_filesystem(struct archive_read_disk *a)
1584 struct tree *t = a->tree;
1588 t->current_filesystem->synthetic = -1;
1589 if (tree_enter_working_dir(t) != 0) {
1590 archive_set_error(&a->archive, errno, "fchdir failed");
1591 return (ARCHIVE_FAILED);
1593 if (tree_current_is_symblic_link_target(t)) {
1594 r = statvfs(tree_current_access_path(t), &sfs);
1596 xr = get_xfer_size(t, -1, tree_current_access_path(t));
1598 #ifdef HAVE_FSTATVFS
1599 r = fstatvfs(tree_current_dir_fd(t), &sfs);
1601 xr = get_xfer_size(t, tree_current_dir_fd(t), NULL);
1603 r = statvfs(".", &sfs);
1605 xr = get_xfer_size(t, -1, ".");
1608 if (r == -1 || xr == -1) {
1609 t->current_filesystem->remote = -1;
1610 archive_set_error(&a->archive, errno, "statvfs failed");
1611 return (ARCHIVE_FAILED);
1612 } else if (xr == 1) {
1613 /* Usuall come here unless NetBSD supports _PC_REC_XFER_ALIGN
1614 * for pathconf() function. */
1615 t->current_filesystem->xfer_align = sfs.f_frsize;
1616 t->current_filesystem->max_xfer_size = -1;
1617 #if defined(HAVE_STRUCT_STATVFS_F_IOSIZE)
1618 t->current_filesystem->min_xfer_size = sfs.f_iosize;
1619 t->current_filesystem->incr_xfer_size = sfs.f_iosize;
1621 t->current_filesystem->min_xfer_size = sfs.f_bsize;
1622 t->current_filesystem->incr_xfer_size = sfs.f_bsize;
1625 if (sfs.f_flag & ST_LOCAL)
1626 t->current_filesystem->remote = 0;
1628 t->current_filesystem->remote = 1;
1630 #if defined(ST_NOATIME)
1631 if (sfs.f_flag & ST_NOATIME)
1632 t->current_filesystem->noatime = 1;
1635 t->current_filesystem->noatime = 0;
1637 /* Set maximum filename length. */
1638 t->current_filesystem->name_max = sfs.f_namemax;
1639 return (ARCHIVE_OK);
1642 #elif defined(HAVE_SYS_STATFS_H) && defined(HAVE_LINUX_MAGIC_H) &&\
1643 defined(HAVE_STATFS) && defined(HAVE_FSTATFS)
1645 * Note: statfs is deprecated since LSB 3.2
1648 #ifndef CIFS_SUPER_MAGIC
1649 #define CIFS_SUPER_MAGIC 0xFF534D42
1651 #ifndef DEVFS_SUPER_MAGIC
1652 #define DEVFS_SUPER_MAGIC 0x1373
1656 * Gather current filesystem properties on Linux
1659 setup_current_filesystem(struct archive_read_disk *a)
1661 struct tree *t = a->tree;
1663 struct statvfs svfs;
1664 int r, vr = 0, xr = 0;
1666 if (tree_current_is_symblic_link_target(t)) {
1667 #if defined(HAVE_OPENAT)
1669 * Get file system statistics on any directory
1672 int fd = openat(tree_current_dir_fd(t),
1673 tree_current_access_path(t), O_RDONLY | O_CLOEXEC);
1674 __archive_ensure_cloexec_flag(fd);
1676 archive_set_error(&a->archive, errno,
1678 return (ARCHIVE_FAILED);
1680 vr = fstatvfs(fd, &svfs);/* for f_flag, mount flags */
1681 r = fstatfs(fd, &sfs);
1683 xr = get_xfer_size(t, fd, NULL);
1686 if (tree_enter_working_dir(t) != 0) {
1687 archive_set_error(&a->archive, errno, "fchdir failed");
1688 return (ARCHIVE_FAILED);
1690 vr = statvfs(tree_current_access_path(t), &svfs);
1691 r = statfs(tree_current_access_path(t), &sfs);
1693 xr = get_xfer_size(t, -1, tree_current_access_path(t));
1697 vr = fstatvfs(tree_current_dir_fd(t), &svfs);
1698 r = fstatfs(tree_current_dir_fd(t), &sfs);
1700 xr = get_xfer_size(t, tree_current_dir_fd(t), NULL);
1702 if (tree_enter_working_dir(t) != 0) {
1703 archive_set_error(&a->archive, errno, "fchdir failed");
1704 return (ARCHIVE_FAILED);
1706 vr = statvfs(".", &svfs);
1707 r = statfs(".", &sfs);
1709 xr = get_xfer_size(t, -1, ".");
1712 if (r == -1 || xr == -1 || vr == -1) {
1713 t->current_filesystem->synthetic = -1;
1714 t->current_filesystem->remote = -1;
1715 archive_set_error(&a->archive, errno, "statfs failed");
1716 return (ARCHIVE_FAILED);
1717 } else if (xr == 1) {
1718 /* pathconf(_PC_REX_*) operations are not supported. */
1719 t->current_filesystem->xfer_align = svfs.f_frsize;
1720 t->current_filesystem->max_xfer_size = -1;
1721 t->current_filesystem->min_xfer_size = svfs.f_bsize;
1722 t->current_filesystem->incr_xfer_size = svfs.f_bsize;
1724 switch (sfs.f_type) {
1725 case AFS_SUPER_MAGIC:
1726 case CIFS_SUPER_MAGIC:
1727 case CODA_SUPER_MAGIC:
1728 case NCP_SUPER_MAGIC:/* NetWare */
1729 case NFS_SUPER_MAGIC:
1730 case SMB_SUPER_MAGIC:
1731 t->current_filesystem->remote = 1;
1732 t->current_filesystem->synthetic = 0;
1734 case DEVFS_SUPER_MAGIC:
1735 case PROC_SUPER_MAGIC:
1736 case USBDEVICE_SUPER_MAGIC:
1737 t->current_filesystem->remote = 0;
1738 t->current_filesystem->synthetic = 1;
1741 t->current_filesystem->remote = 0;
1742 t->current_filesystem->synthetic = 0;
1746 #if defined(ST_NOATIME)
1747 if (svfs.f_flag & ST_NOATIME)
1748 t->current_filesystem->noatime = 1;
1751 t->current_filesystem->noatime = 0;
1753 #if defined(HAVE_READDIR_R)
1754 /* Set maximum filename length. */
1755 t->current_filesystem->name_max = sfs.f_namelen;
1757 return (ARCHIVE_OK);
1760 #elif defined(HAVE_SYS_STATVFS_H) &&\
1761 (defined(HAVE_STATVFS) || defined(HAVE_FSTATVFS))
1764 * Gather current filesystem properties on other posix platform.
1767 setup_current_filesystem(struct archive_read_disk *a)
1769 struct tree *t = a->tree;
1773 t->current_filesystem->synthetic = -1;/* Not supported */
1774 t->current_filesystem->remote = -1;/* Not supported */
1775 if (tree_current_is_symblic_link_target(t)) {
1776 #if defined(HAVE_OPENAT)
1778 * Get file system statistics on any directory
1781 int fd = openat(tree_current_dir_fd(t),
1782 tree_current_access_path(t), O_RDONLY | O_CLOEXEC);
1783 __archive_ensure_cloexec_flag(fd);
1785 archive_set_error(&a->archive, errno,
1787 return (ARCHIVE_FAILED);
1789 r = fstatvfs(fd, &sfs);
1791 xr = get_xfer_size(t, fd, NULL);
1794 if (tree_enter_working_dir(t) != 0) {
1795 archive_set_error(&a->archive, errno, "fchdir failed");
1796 return (ARCHIVE_FAILED);
1798 r = statvfs(tree_current_access_path(t), &sfs);
1800 xr = get_xfer_size(t, -1, tree_current_access_path(t));
1803 #ifdef HAVE_FSTATVFS
1804 r = fstatvfs(tree_current_dir_fd(t), &sfs);
1806 xr = get_xfer_size(t, tree_current_dir_fd(t), NULL);
1808 if (tree_enter_working_dir(t) != 0) {
1809 archive_set_error(&a->archive, errno, "fchdir failed");
1810 return (ARCHIVE_FAILED);
1812 r = statvfs(".", &sfs);
1814 xr = get_xfer_size(t, -1, ".");
1817 if (r == -1 || xr == -1) {
1818 t->current_filesystem->synthetic = -1;
1819 t->current_filesystem->remote = -1;
1820 archive_set_error(&a->archive, errno, "statvfs failed");
1821 return (ARCHIVE_FAILED);
1822 } else if (xr == 1) {
1823 /* pathconf(_PC_REX_*) operations are not supported. */
1824 t->current_filesystem->xfer_align = sfs.f_frsize;
1825 t->current_filesystem->max_xfer_size = -1;
1826 t->current_filesystem->min_xfer_size = sfs.f_bsize;
1827 t->current_filesystem->incr_xfer_size = sfs.f_bsize;
1830 #if defined(ST_NOATIME)
1831 if (sfs.f_flag & ST_NOATIME)
1832 t->current_filesystem->noatime = 1;
1835 t->current_filesystem->noatime = 0;
1837 #if defined(HAVE_READDIR_R)
1838 /* Set maximum filename length. */
1839 t->current_filesystem->name_max = sfs.f_namemax;
1841 return (ARCHIVE_OK);
1847 * Generic: Gather current filesystem properties.
1848 * TODO: Is this generic function really needed?
1851 setup_current_filesystem(struct archive_read_disk *a)
1853 struct tree *t = a->tree;
1854 #if defined(_PC_NAME_MAX) && defined(HAVE_READDIR_R)
1857 t->current_filesystem->synthetic = -1;/* Not supported */
1858 t->current_filesystem->remote = -1;/* Not supported */
1859 t->current_filesystem->noatime = 0;
1860 (void)get_xfer_size(t, -1, ".");/* Dummy call to avoid build error. */
1861 t->current_filesystem->xfer_align = -1;/* Unknown */
1862 t->current_filesystem->max_xfer_size = -1;
1863 t->current_filesystem->min_xfer_size = -1;
1864 t->current_filesystem->incr_xfer_size = -1;
1866 #if defined(HAVE_READDIR_R)
1867 /* Set maximum filename length. */
1868 # if defined(_PC_NAME_MAX)
1869 if (tree_current_is_symblic_link_target(t)) {
1870 if (tree_enter_working_dir(t) != 0) {
1871 archive_set_error(&a->archive, errno, "fchdir failed");
1872 return (ARCHIVE_FAILED);
1874 nm = pathconf(tree_current_access_path(t), _PC_NAME_MAX);
1876 nm = fpathconf(tree_current_dir_fd(t), _PC_NAME_MAX);
1878 # endif /* _PC_NAME_MAX */
1880 * Some sysmtes (HP-UX or others?) incorrectly defined
1881 * NAME_MAX macro to be a smaller value.
1883 # if defined(NAME_MAX) && NAME_MAX >= 255
1884 t->current_filesystem->name_max = NAME_MAX;
1886 /* No way to get a trusted value of maximum filename
1888 t->current_filesystem->name_max = PATH_MAX;
1889 # endif /* NAME_MAX */
1890 # if defined(_PC_NAME_MAX)
1892 t->current_filesystem->name_max = nm;
1893 # endif /* _PC_NAME_MAX */
1894 #endif /* HAVE_READDIR_R */
1895 return (ARCHIVE_OK);
1901 close_and_restore_time(int fd, struct tree *t, struct restore_time *rt)
1904 (void)t; /* UNUSED */
1905 (void)rt; /* UNUSED */
1908 #if defined(HAVE_FUTIMENS) && !defined(__CYGWIN__)
1909 struct timespec timespecs[2];
1911 struct timeval times[2];
1913 if ((t->flags & needsRestoreTimes) == 0 || rt->noatime) {
1920 #if defined(HAVE_FUTIMENS) && !defined(__CYGWIN__)
1921 timespecs[1].tv_sec = rt->mtime;
1922 timespecs[1].tv_nsec = rt->mtime_nsec;
1924 timespecs[0].tv_sec = rt->atime;
1925 timespecs[0].tv_nsec = rt->atime_nsec;
1926 /* futimens() is defined in POSIX.1-2008. */
1927 if (futimens(fd, timespecs) == 0)
1931 times[1].tv_sec = rt->mtime;
1932 times[1].tv_usec = rt->mtime_nsec / 1000;
1934 times[0].tv_sec = rt->atime;
1935 times[0].tv_usec = rt->atime_nsec / 1000;
1937 #if !defined(HAVE_FUTIMENS) && defined(HAVE_FUTIMES) && !defined(__CYGWIN__)
1938 if (futimes(fd, times) == 0)
1942 #if defined(HAVE_FUTIMESAT)
1943 if (futimesat(tree_current_dir_fd(t), rt->name, times) == 0)
1947 if (lutimes(rt->name, times) != 0)
1949 if (AE_IFLNK != rt->filetype && utimes(rt->name, times) != 0)
1957 open_on_current_dir(struct tree *t, const char *path, int flags)
1960 return (openat(tree_current_dir_fd(t), path, flags));
1962 if (tree_enter_working_dir(t) != 0)
1964 return (open(path, flags));
1972 #ifdef F_DUPFD_CLOEXEC
1973 static volatile int can_dupfd_cloexec = 1;
1975 if (can_dupfd_cloexec) {
1976 new_fd = fcntl(fd, F_DUPFD_CLOEXEC);
1979 /* Linux 2.6.18 - 2.6.23 declare F_DUPFD_CLOEXEC,
1980 * but it cannot be used. So we have to try dup(). */
1981 /* We won't try F_DUPFD_CLOEXEC. */
1982 can_dupfd_cloexec = 0;
1984 #endif /* F_DUPFD_CLOEXEC */
1986 __archive_ensure_cloexec_flag(new_fd);
1991 * Add a directory path to the current stack.
1994 tree_push(struct tree *t, const char *path, int filesystem_id,
1995 int64_t dev, int64_t ino, struct restore_time *rt)
1997 struct tree_entry *te;
1999 te = malloc(sizeof(*te));
2000 memset(te, 0, sizeof(*te));
2001 te->next = t->stack;
2002 te->parent = t->current;
2004 te->depth = te->parent->depth + 1;
2006 archive_string_init(&te->name);
2007 te->symlink_parent_fd = -1;
2008 archive_strcpy(&te->name, path);
2009 te->flags = needsDescent | needsOpen | needsAscent;
2010 te->filesystem_id = filesystem_id;
2013 te->dirname_length = t->dirname_length;
2014 te->restore_time.name = te->name.s;
2016 te->restore_time.mtime = rt->mtime;
2017 te->restore_time.mtime_nsec = rt->mtime_nsec;
2018 te->restore_time.atime = rt->atime;
2019 te->restore_time.atime_nsec = rt->atime_nsec;
2020 te->restore_time.filetype = rt->filetype;
2021 te->restore_time.noatime = rt->noatime;
2026 * Append a name to the current dir path.
2029 tree_append(struct tree *t, const char *name, size_t name_length)
2033 t->path.s[t->dirname_length] = '\0';
2034 t->path.length = t->dirname_length;
2035 /* Strip trailing '/' from name, unless entire name is "/". */
2036 while (name_length > 1 && name[name_length - 1] == '/')
2039 /* Resize pathname buffer as needed. */
2040 size_needed = name_length + t->dirname_length + 2;
2041 archive_string_ensure(&t->path, size_needed);
2042 /* Add a separating '/' if it's needed. */
2043 if (t->dirname_length > 0 && t->path.s[archive_strlen(&t->path)-1] != '/')
2044 archive_strappend_char(&t->path, '/');
2045 t->basename = t->path.s + archive_strlen(&t->path);
2046 archive_strncat(&t->path, name, name_length);
2047 t->restore_time.name = t->basename;
2051 * Open a directory tree for traversal.
2053 static struct tree *
2054 tree_open(const char *path, int symlink_mode, int restore_time)
2058 if ((t = malloc(sizeof(*t))) == NULL)
2060 memset(t, 0, sizeof(*t));
2061 archive_string_init(&t->path);
2062 archive_string_ensure(&t->path, 31);
2063 t->initial_symlink_mode = symlink_mode;
2064 return (tree_reopen(t, path, restore_time));
2067 static struct tree *
2068 tree_reopen(struct tree *t, const char *path, int restore_time)
2070 t->flags = (restore_time)?needsRestoreTimes:0;
2071 t->flags |= onInitialDir;
2074 t->dirname_length = 0;
2078 t->d = INVALID_DIR_HANDLE;
2079 t->symlink_mode = t->initial_symlink_mode;
2080 archive_string_empty(&t->path);
2083 t->entry_remaining_bytes = 0;
2084 t->initial_filesystem_id = -1;
2086 /* First item is set up a lot like a symlink traversal. */
2087 tree_push(t, path, 0, 0, 0, NULL);
2088 t->stack->flags = needsFirstVisit;
2089 t->maxOpenCount = t->openCount = 1;
2090 t->initial_dir_fd = open(".", O_RDONLY | O_CLOEXEC);
2091 __archive_ensure_cloexec_flag(t->initial_dir_fd);
2092 t->working_dir_fd = tree_dup(t->initial_dir_fd);
2097 tree_descent(struct tree *t)
2099 int flag, new_fd, r = 0;
2101 t->dirname_length = archive_strlen(&t->path);
2102 flag = O_RDONLY | O_CLOEXEC;
2103 #if defined(O_DIRECTORY)
2104 flag |= O_DIRECTORY;
2106 new_fd = open_on_current_dir(t, t->stack->name.s, flag);
2107 __archive_ensure_cloexec_flag(new_fd);
2109 t->tree_errno = errno;
2113 /* If it is a link, set up fd for the ascent. */
2114 if (t->stack->flags & isDirLink) {
2115 t->stack->symlink_parent_fd = t->working_dir_fd;
2117 if (t->openCount > t->maxOpenCount)
2118 t->maxOpenCount = t->openCount;
2120 close(t->working_dir_fd);
2121 /* Renew the current working directory. */
2122 t->working_dir_fd = new_fd;
2123 t->flags &= ~onWorkingDir;
2129 * We've finished a directory; ascend back to the parent.
2132 tree_ascend(struct tree *t)
2134 struct tree_entry *te;
2135 int new_fd, r = 0, prev_dir_fd;
2138 prev_dir_fd = t->working_dir_fd;
2139 if (te->flags & isDirLink)
2140 new_fd = te->symlink_parent_fd;
2142 new_fd = open_on_current_dir(t, "..", O_RDONLY | O_CLOEXEC);
2143 __archive_ensure_cloexec_flag(new_fd);
2146 t->tree_errno = errno;
2147 r = TREE_ERROR_FATAL;
2149 /* Renew the current working directory. */
2150 t->working_dir_fd = new_fd;
2151 t->flags &= ~onWorkingDir;
2152 /* Current directory has been changed, we should
2153 * close an fd of previous working directory. */
2154 close_and_restore_time(prev_dir_fd, t, &te->restore_time);
2155 if (te->flags & isDirLink) {
2157 te->symlink_parent_fd = -1;
2165 * Return to the initial directory where tree_open() was performed.
2168 tree_enter_initial_dir(struct tree *t)
2172 if ((t->flags & onInitialDir) == 0) {
2173 r = fchdir(t->initial_dir_fd);
2175 t->flags &= ~onWorkingDir;
2176 t->flags |= onInitialDir;
2183 * Restore working directory of directory traversals.
2186 tree_enter_working_dir(struct tree *t)
2191 * Change the current directory if really needed.
2192 * Sometimes this is unneeded when we did not do
2195 if (t->depth > 0 && (t->flags & onWorkingDir) == 0) {
2196 r = fchdir(t->working_dir_fd);
2198 t->flags &= ~onInitialDir;
2199 t->flags |= onWorkingDir;
2206 tree_current_dir_fd(struct tree *t)
2208 return (t->working_dir_fd);
2212 * Pop the working stack.
2215 tree_pop(struct tree *t)
2217 struct tree_entry *te;
2219 t->path.s[t->dirname_length] = '\0';
2220 t->path.length = t->dirname_length;
2221 if (t->stack == t->current && t->current != NULL)
2222 t->current = t->current->parent;
2224 t->stack = te->next;
2225 t->dirname_length = te->dirname_length;
2226 t->basename = t->path.s + t->dirname_length;
2227 while (t->basename[0] == '/')
2229 archive_string_free(&te->name);
2234 * Get the next item in the tree traversal.
2237 tree_next(struct tree *t)
2241 while (t->stack != NULL) {
2242 /* If there's an open dir, get the next entry from there. */
2243 if (t->d != INVALID_DIR_HANDLE) {
2244 r = tree_dir_next_posix(t);
2250 if (t->stack->flags & needsFirstVisit) {
2251 /* Top stack item needs a regular visit. */
2252 t->current = t->stack;
2253 tree_append(t, t->stack->name.s,
2254 archive_strlen(&(t->stack->name)));
2255 /* t->dirname_length = t->path_length; */
2257 t->stack->flags &= ~needsFirstVisit;
2258 return (t->visit_type = TREE_REGULAR);
2259 } else if (t->stack->flags & needsDescent) {
2260 /* Top stack item is dir to descend into. */
2261 t->current = t->stack;
2262 tree_append(t, t->stack->name.s,
2263 archive_strlen(&(t->stack->name)));
2264 t->stack->flags &= ~needsDescent;
2265 r = tree_descent(t);
2270 t->visit_type = TREE_POSTDESCENT;
2271 return (t->visit_type);
2272 } else if (t->stack->flags & needsOpen) {
2273 t->stack->flags &= ~needsOpen;
2274 r = tree_dir_next_posix(t);
2278 } else if (t->stack->flags & needsAscent) {
2279 /* Top stack item is dir and we're done with it. */
2282 t->visit_type = r != 0 ? r : TREE_POSTASCENT;
2283 return (t->visit_type);
2285 /* Top item on stack is dead. */
2287 t->flags &= ~hasLstat;
2288 t->flags &= ~hasStat;
2291 return (t->visit_type = 0);
2295 tree_dir_next_posix(struct tree *t)
2302 #if defined(HAVE_READDIR_R)
2306 #if defined(HAVE_FDOPENDIR)
2307 t->d = fdopendir(tree_dup(t->working_dir_fd));
2308 #else /* HAVE_FDOPENDIR */
2309 if (tree_enter_working_dir(t) == 0) {
2310 t->d = opendir(".");
2311 #if HAVE_DIRFD || defined(dirfd)
2312 __archive_ensure_cloexec_flag(dirfd(t->d));
2315 #endif /* HAVE_FDOPENDIR */
2317 r = tree_ascend(t); /* Undo "chdir" */
2319 t->tree_errno = errno;
2320 t->visit_type = r != 0 ? r : TREE_ERROR_DIR;
2321 return (t->visit_type);
2323 #if defined(HAVE_READDIR_R)
2324 dirent_size = offsetof(struct dirent, d_name) +
2325 t->filesystem_table[t->current->filesystem_id].name_max + 1;
2326 if (t->dirent == NULL || t->dirent_allocated < dirent_size) {
2328 t->dirent = malloc(dirent_size);
2329 if (t->dirent == NULL) {
2331 t->d = INVALID_DIR_HANDLE;
2332 (void)tree_ascend(t);
2334 t->tree_errno = ENOMEM;
2335 t->visit_type = TREE_ERROR_DIR;
2336 return (t->visit_type);
2338 t->dirent_allocated = dirent_size;
2340 #endif /* HAVE_READDIR_R */
2344 #if defined(HAVE_READDIR_R)
2345 r = readdir_r(t->d, t->dirent, &t->de);
2347 /* Note: According to the man page, return value 9 indicates
2348 * that the readdir_r was not successful and the error code
2349 * is set to the global errno variable. And then if the end
2350 * of directory entries was reached, the return value is 9
2351 * and the third parameter is set to NULL and errno is
2356 if (r != 0 || t->de == NULL) {
2358 t->de = readdir(t->d);
2359 if (t->de == NULL) {
2363 t->d = INVALID_DIR_HANDLE;
2366 t->visit_type = TREE_ERROR_DIR;
2367 return (t->visit_type);
2371 name = t->de->d_name;
2372 namelen = D_NAMELEN(t->de);
2373 t->flags &= ~hasLstat;
2374 t->flags &= ~hasStat;
2375 if (name[0] == '.' && name[1] == '\0')
2377 if (name[0] == '.' && name[1] == '.' && name[2] == '\0')
2379 tree_append(t, name, namelen);
2380 return (t->visit_type = TREE_REGULAR);
2386 * Get the stat() data for the entry just returned from tree_next().
2388 static const struct stat *
2389 tree_current_stat(struct tree *t)
2391 if (!(t->flags & hasStat)) {
2393 if (fstatat(tree_current_dir_fd(t),
2394 tree_current_access_path(t), &t->st, 0) != 0)
2396 if (tree_enter_working_dir(t) != 0)
2398 if (stat(tree_current_access_path(t), &t->st) != 0)
2401 t->flags |= hasStat;
2407 * Get the lstat() data for the entry just returned from tree_next().
2409 static const struct stat *
2410 tree_current_lstat(struct tree *t)
2412 if (!(t->flags & hasLstat)) {
2414 if (fstatat(tree_current_dir_fd(t),
2415 tree_current_access_path(t), &t->lst,
2416 AT_SYMLINK_NOFOLLOW) != 0)
2418 if (tree_enter_working_dir(t) != 0)
2420 if (lstat(tree_current_access_path(t), &t->lst) != 0)
2423 t->flags |= hasLstat;
2429 * Test whether current entry is a dir or link to a dir.
2432 tree_current_is_dir(struct tree *t)
2434 const struct stat *st;
2436 * If we already have lstat() info, then try some
2437 * cheap tests to determine if this is a dir.
2439 if (t->flags & hasLstat) {
2440 /* If lstat() says it's a dir, it must be a dir. */
2441 st = tree_current_lstat(t);
2444 if (S_ISDIR(st->st_mode))
2446 /* Not a dir; might be a link to a dir. */
2447 /* If it's not a link, then it's not a link to a dir. */
2448 if (!S_ISLNK(st->st_mode))
2451 * It's a link, but we don't know what it's a link to,
2452 * so we'll have to use stat().
2456 st = tree_current_stat(t);
2457 /* If we can't stat it, it's not a dir. */
2460 /* Use the definitive test. Hopefully this is cached. */
2461 return (S_ISDIR(st->st_mode));
2465 * Test whether current entry is a physical directory. Usually, we
2466 * already have at least one of stat() or lstat() in memory, so we
2467 * use tricks to try to avoid an extra trip to the disk.
2470 tree_current_is_physical_dir(struct tree *t)
2472 const struct stat *st;
2475 * If stat() says it isn't a dir, then it's not a dir.
2476 * If stat() data is cached, this check is free, so do it first.
2478 if (t->flags & hasStat) {
2479 st = tree_current_stat(t);
2482 if (!S_ISDIR(st->st_mode))
2487 * Either stat() said it was a dir (in which case, we have
2488 * to determine whether it's really a link to a dir) or
2489 * stat() info wasn't available. So we use lstat(), which
2490 * hopefully is already cached.
2493 st = tree_current_lstat(t);
2494 /* If we can't stat it, it's not a dir. */
2497 /* Use the definitive test. Hopefully this is cached. */
2498 return (S_ISDIR(st->st_mode));
2502 * Test whether the same file has been in the tree as its parent.
2505 tree_target_is_same_as_parent(struct tree *t, const struct stat *st)
2507 struct tree_entry *te;
2509 for (te = t->current->parent; te != NULL; te = te->parent) {
2510 if (te->dev == (int64_t)st->st_dev &&
2511 te->ino == (int64_t)st->st_ino)
2518 * Test whether the current file is symbolic link target and
2519 * on the other filesystem.
2522 tree_current_is_symblic_link_target(struct tree *t)
2524 static const struct stat *lst, *st;
2526 lst = tree_current_lstat(t);
2527 st = tree_current_stat(t);
2528 return (st != NULL && lst != NULL &&
2529 (int64_t)st->st_dev == t->current_filesystem->dev &&
2530 st->st_dev != lst->st_dev);
2534 * Return the access path for the entry just returned from tree_next().
2537 tree_current_access_path(struct tree *t)
2539 return (t->basename);
2543 * Return the full path for the entry just returned from tree_next().
2546 tree_current_path(struct tree *t)
2552 * Terminate the traversal.
2555 tree_close(struct tree *t)
2560 if (t->entry_fd >= 0) {
2561 close_and_restore_time(t->entry_fd, t, &t->restore_time);
2564 /* Close the handle of readdir(). */
2565 if (t->d != INVALID_DIR_HANDLE) {
2567 t->d = INVALID_DIR_HANDLE;
2569 /* Release anything remaining in the stack. */
2570 while (t->stack != NULL) {
2571 if (t->stack->flags & isDirLink)
2572 close(t->stack->symlink_parent_fd);
2575 if (t->working_dir_fd >= 0) {
2576 close(t->working_dir_fd);
2577 t->working_dir_fd = -1;
2579 if (t->initial_dir_fd >= 0) {
2580 close(t->initial_dir_fd);
2581 t->initial_dir_fd = -1;
2586 * Release any resources.
2589 tree_free(struct tree *t)
2595 archive_string_free(&t->path);
2596 #if defined(HAVE_READDIR_R)
2599 free(t->sparse_list);
2600 for (i = 0; i < t->max_filesystem_id; i++)
2601 free(t->filesystem_table[i].allocation_ptr);
2602 free(t->filesystem_table);