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_header(struct archive *,
360 struct archive_entry **);
361 static int _archive_read_next_header2(struct archive *,
362 struct archive_entry *);
363 static const char *trivial_lookup_gname(void *, int64_t gid);
364 static const char *trivial_lookup_uname(void *, int64_t uid);
365 static int setup_sparse(struct archive_read_disk *, struct archive_entry *);
366 static int close_and_restore_time(int fd, struct tree *,
367 struct restore_time *);
368 static int open_on_current_dir(struct tree *, const char *, int);
369 static int tree_dup(int);
372 static struct archive_vtable *
373 archive_read_disk_vtable(void)
375 static struct archive_vtable av;
376 static int inited = 0;
379 av.archive_free = _archive_read_free;
380 av.archive_close = _archive_read_close;
381 av.archive_read_data_block = _archive_read_data_block;
382 av.archive_read_next_header = _archive_read_next_header;
383 av.archive_read_next_header2 = _archive_read_next_header2;
390 archive_read_disk_gname(struct archive *_a, int64_t gid)
392 struct archive_read_disk *a = (struct archive_read_disk *)_a;
393 if (ARCHIVE_OK != __archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
394 ARCHIVE_STATE_ANY, "archive_read_disk_gname"))
396 if (a->lookup_gname == NULL)
398 return ((*a->lookup_gname)(a->lookup_gname_data, gid));
402 archive_read_disk_uname(struct archive *_a, int64_t uid)
404 struct archive_read_disk *a = (struct archive_read_disk *)_a;
405 if (ARCHIVE_OK != __archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
406 ARCHIVE_STATE_ANY, "archive_read_disk_uname"))
408 if (a->lookup_uname == NULL)
410 return ((*a->lookup_uname)(a->lookup_uname_data, uid));
414 archive_read_disk_set_gname_lookup(struct archive *_a,
416 const char * (*lookup_gname)(void *private, int64_t gid),
417 void (*cleanup_gname)(void *private))
419 struct archive_read_disk *a = (struct archive_read_disk *)_a;
420 archive_check_magic(&a->archive, ARCHIVE_READ_DISK_MAGIC,
421 ARCHIVE_STATE_ANY, "archive_read_disk_set_gname_lookup");
423 if (a->cleanup_gname != NULL && a->lookup_gname_data != NULL)
424 (a->cleanup_gname)(a->lookup_gname_data);
426 a->lookup_gname = lookup_gname;
427 a->cleanup_gname = cleanup_gname;
428 a->lookup_gname_data = private_data;
433 archive_read_disk_set_uname_lookup(struct archive *_a,
435 const char * (*lookup_uname)(void *private, int64_t uid),
436 void (*cleanup_uname)(void *private))
438 struct archive_read_disk *a = (struct archive_read_disk *)_a;
439 archive_check_magic(&a->archive, ARCHIVE_READ_DISK_MAGIC,
440 ARCHIVE_STATE_ANY, "archive_read_disk_set_uname_lookup");
442 if (a->cleanup_uname != NULL && a->lookup_uname_data != NULL)
443 (a->cleanup_uname)(a->lookup_uname_data);
445 a->lookup_uname = lookup_uname;
446 a->cleanup_uname = cleanup_uname;
447 a->lookup_uname_data = private_data;
452 * Create a new archive_read_disk object and initialize it with global state.
455 archive_read_disk_new(void)
457 struct archive_read_disk *a;
459 a = (struct archive_read_disk *)calloc(1, sizeof(*a));
462 a->archive.magic = ARCHIVE_READ_DISK_MAGIC;
463 a->archive.state = ARCHIVE_STATE_NEW;
464 a->archive.vtable = archive_read_disk_vtable();
465 a->entry = archive_entry_new2(&a->archive);
466 a->lookup_uname = trivial_lookup_uname;
467 a->lookup_gname = trivial_lookup_gname;
468 a->enable_copyfile = 1;
469 a->traverse_mount_points = 1;
470 a->open_on_current_dir = open_on_current_dir;
471 a->tree_current_dir_fd = tree_current_dir_fd;
472 a->tree_enter_working_dir = tree_enter_working_dir;
473 return (&a->archive);
477 _archive_read_free(struct archive *_a)
479 struct archive_read_disk *a = (struct archive_read_disk *)_a;
484 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
485 ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_free");
487 if (a->archive.state != ARCHIVE_STATE_CLOSED)
488 r = _archive_read_close(&a->archive);
493 if (a->cleanup_gname != NULL && a->lookup_gname_data != NULL)
494 (a->cleanup_gname)(a->lookup_gname_data);
495 if (a->cleanup_uname != NULL && a->lookup_uname_data != NULL)
496 (a->cleanup_uname)(a->lookup_uname_data);
497 archive_string_free(&a->archive.error_string);
498 archive_entry_free(a->entry);
499 a->archive.magic = 0;
500 __archive_clean(&a->archive);
506 _archive_read_close(struct archive *_a)
508 struct archive_read_disk *a = (struct archive_read_disk *)_a;
510 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
511 ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_close");
513 if (a->archive.state != ARCHIVE_STATE_FATAL)
514 a->archive.state = ARCHIVE_STATE_CLOSED;
522 setup_symlink_mode(struct archive_read_disk *a, char symlink_mode,
525 a->symlink_mode = symlink_mode;
526 a->follow_symlinks = follow_symlinks;
527 if (a->tree != NULL) {
528 a->tree->initial_symlink_mode = a->symlink_mode;
529 a->tree->symlink_mode = a->symlink_mode;
534 archive_read_disk_set_symlink_logical(struct archive *_a)
536 struct archive_read_disk *a = (struct archive_read_disk *)_a;
537 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
538 ARCHIVE_STATE_ANY, "archive_read_disk_set_symlink_logical");
539 setup_symlink_mode(a, 'L', 1);
544 archive_read_disk_set_symlink_physical(struct archive *_a)
546 struct archive_read_disk *a = (struct archive_read_disk *)_a;
547 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
548 ARCHIVE_STATE_ANY, "archive_read_disk_set_symlink_physical");
549 setup_symlink_mode(a, 'P', 0);
554 archive_read_disk_set_symlink_hybrid(struct archive *_a)
556 struct archive_read_disk *a = (struct archive_read_disk *)_a;
557 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
558 ARCHIVE_STATE_ANY, "archive_read_disk_set_symlink_hybrid");
559 setup_symlink_mode(a, 'H', 1);/* Follow symlinks initially. */
564 archive_read_disk_set_atime_restored(struct archive *_a)
567 static int warning_done = 0;
569 struct archive_read_disk *a = (struct archive_read_disk *)_a;
570 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
571 ARCHIVE_STATE_ANY, "archive_read_disk_restore_atime");
575 a->tree->flags |= needsRestoreTimes;
579 /* Warning was already emitted; suppress further warnings. */
582 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
583 "Cannot restore access time on this system");
585 return (ARCHIVE_WARN);
590 archive_read_disk_set_behavior(struct archive *_a, int flags)
592 struct archive_read_disk *a = (struct archive_read_disk *)_a;
595 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
596 ARCHIVE_STATE_ANY, "archive_read_disk_honor_nodump");
598 if (flags & ARCHIVE_READDISK_RESTORE_ATIME)
599 r = archive_read_disk_set_atime_restored(_a);
603 a->tree->flags &= ~needsRestoreTimes;
605 if (flags & ARCHIVE_READDISK_HONOR_NODUMP)
609 if (flags & ARCHIVE_READDISK_MAC_COPYFILE)
610 a->enable_copyfile = 1;
612 a->enable_copyfile = 0;
613 if (flags & ARCHIVE_READDISK_NO_TRAVERSE_MOUNTS)
614 a->traverse_mount_points = 0;
616 a->traverse_mount_points = 1;
617 if (flags & ARCHIVE_READDISK_NO_XATTR)
618 a->suppress_xattr = 1;
620 a->suppress_xattr = 0;
625 * Trivial implementations of gname/uname lookup functions.
626 * These are normally overridden by the client, but these stub
627 * versions ensure that we always have something that works.
630 trivial_lookup_gname(void *private_data, int64_t gid)
632 (void)private_data; /* UNUSED */
633 (void)gid; /* UNUSED */
638 trivial_lookup_uname(void *private_data, int64_t uid)
640 (void)private_data; /* UNUSED */
641 (void)uid; /* UNUSED */
646 * Allocate memory for the reading buffer adjusted to the filesystem
650 setup_suitable_read_buffer(struct archive_read_disk *a)
652 struct tree *t = a->tree;
653 struct filesystem *cf = t->current_filesystem;
657 if (cf->allocation_ptr == NULL) {
658 /* If we couldn't get a filesystem alignment,
659 * we use 4096 as default value but we won't use
660 * O_DIRECT to open() and openat() operations. */
661 long xfer_align = (cf->xfer_align == -1)?4096:cf->xfer_align;
663 if (cf->max_xfer_size != -1)
664 asize = cf->max_xfer_size + xfer_align;
666 long incr = cf->incr_xfer_size;
667 /* Some platform does not set a proper value to
670 incr = cf->min_xfer_size;
671 if (cf->min_xfer_size < 0) {
675 asize = cf->min_xfer_size;
677 /* Increase a buffer size up to 64K bytes in
678 * a proper incremant size. */
679 while (asize < 1024*64)
681 /* Take a margin to adjust to the filesystem
685 cf->allocation_ptr = malloc(asize);
686 if (cf->allocation_ptr == NULL) {
687 archive_set_error(&a->archive, ENOMEM,
688 "Couldn't allocate memory");
689 a->archive.state = ARCHIVE_STATE_FATAL;
690 return (ARCHIVE_FATAL);
694 * Calculate proper address for the filesystem.
696 s = (uintptr_t)cf->allocation_ptr;
702 * Set a read buffer pointer in the proper alignment of
703 * the current filesystem.
705 cf->buff = cf->allocation_ptr + s;
706 cf->buff_size = asize - xfer_align;
712 _archive_read_data_block(struct archive *_a, const void **buff,
713 size_t *size, int64_t *offset)
715 struct archive_read_disk *a = (struct archive_read_disk *)_a;
716 struct tree *t = a->tree;
720 int empty_sparse_region = 0;
722 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA,
723 "archive_read_data_block");
725 if (t->entry_eof || t->entry_remaining_bytes <= 0) {
727 goto abort_read_data;
731 * Open the current file.
733 if (t->entry_fd < 0) {
734 int flags = O_RDONLY | O_BINARY | O_CLOEXEC;
737 * Eliminate or reduce cache effects if we can.
739 * Carefully consider this to be enabled.
741 #if defined(O_DIRECT) && 0/* Disabled for now */
742 if (t->current_filesystem->xfer_align != -1 &&
746 #if defined(O_NOATIME)
748 * Linux has O_NOATIME flag; use it if we need.
750 if ((t->flags & needsRestoreTimes) != 0 &&
751 t->restore_time.noatime == 0)
755 t->entry_fd = open_on_current_dir(t,
756 tree_current_access_path(t), flags);
757 __archive_ensure_cloexec_flag(t->entry_fd);
758 #if defined(O_NOATIME)
760 * When we did open the file with O_NOATIME flag,
761 * if successful, set 1 to t->restore_time.noatime
762 * not to restore an atime of the file later.
763 * if failed by EPERM, retry it without O_NOATIME flag.
765 if (flags & O_NOATIME) {
766 if (t->entry_fd >= 0)
767 t->restore_time.noatime = 1;
768 else if (errno == EPERM) {
775 if (t->entry_fd < 0) {
776 archive_set_error(&a->archive, errno,
777 "Couldn't open %s", tree_current_path(t));
779 tree_enter_initial_dir(t);
780 goto abort_read_data;
782 tree_enter_initial_dir(t);
786 * Allocate read buffer if not allocated.
788 if (t->current_filesystem->allocation_ptr == NULL) {
789 r = setup_suitable_read_buffer(a);
790 if (r != ARCHIVE_OK) {
791 a->archive.state = ARCHIVE_STATE_FATAL;
792 goto abort_read_data;
795 t->entry_buff = t->current_filesystem->buff;
796 t->entry_buff_size = t->current_filesystem->buff_size;
798 buffbytes = t->entry_buff_size;
799 if ((int64_t)buffbytes > t->current_sparse->length)
800 buffbytes = t->current_sparse->length;
802 if (t->current_sparse->length == 0)
803 empty_sparse_region = 1;
807 * TODO: Should we consider t->current_filesystem->xfer_align?
809 if (t->current_sparse->offset > t->entry_total) {
810 if (lseek(t->entry_fd,
811 (off_t)t->current_sparse->offset, SEEK_SET) < 0) {
812 archive_set_error(&a->archive, errno, "Seek error");
814 a->archive.state = ARCHIVE_STATE_FATAL;
815 goto abort_read_data;
817 bytes = t->current_sparse->offset - t->entry_total;
818 t->entry_remaining_bytes -= bytes;
819 t->entry_total += bytes;
823 * Read file contents.
826 bytes = read(t->entry_fd, t->entry_buff, buffbytes);
828 archive_set_error(&a->archive, errno, "Read error");
830 a->archive.state = ARCHIVE_STATE_FATAL;
831 goto abort_read_data;
836 * Return an EOF unless we've read a leading empty sparse region, which
837 * is used to represent fully-sparse files.
839 if (bytes == 0 && !empty_sparse_region) {
843 goto abort_read_data;
845 *buff = t->entry_buff;
847 *offset = t->entry_total;
848 t->entry_total += bytes;
849 t->entry_remaining_bytes -= bytes;
850 if (t->entry_remaining_bytes == 0) {
851 /* Close the current file descriptor */
852 close_and_restore_time(t->entry_fd, t, &t->restore_time);
856 t->current_sparse->offset += bytes;
857 t->current_sparse->length -= bytes;
858 if (t->current_sparse->length == 0 && !t->entry_eof)
865 *offset = t->entry_total;
866 if (t->entry_fd >= 0) {
867 /* Close the current file descriptor */
868 close_and_restore_time(t->entry_fd, t, &t->restore_time);
875 next_entry(struct archive_read_disk *a, struct tree *t,
876 struct archive_entry *entry)
878 const struct stat *st; /* info to use for this entry */
879 const struct stat *lst;/* lstat() information */
887 switch (tree_next(t)) {
888 case TREE_ERROR_FATAL:
889 archive_set_error(&a->archive, t->tree_errno,
890 "%s: Unable to continue traversing directory tree",
891 tree_current_path(t));
892 a->archive.state = ARCHIVE_STATE_FATAL;
893 tree_enter_initial_dir(t);
894 return (ARCHIVE_FATAL);
896 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
897 "%s: Couldn't visit directory",
898 tree_current_path(t));
899 tree_enter_initial_dir(t);
900 return (ARCHIVE_FAILED);
902 tree_enter_initial_dir(t);
903 return (ARCHIVE_EOF);
904 case TREE_POSTDESCENT:
905 case TREE_POSTASCENT:
908 lst = tree_current_lstat(t);
910 archive_set_error(&a->archive, errno,
912 tree_current_path(t));
913 tree_enter_initial_dir(t);
914 return (ARCHIVE_FAILED);
918 } while (lst == NULL);
921 if (a->enable_copyfile) {
922 /* If we're using copyfile(), ignore "._XXX" files. */
923 const char *bname = strrchr(tree_current_path(t), '/');
925 bname = tree_current_path(t);
928 if (bname[0] == '.' && bname[1] == '_')
929 return (ARCHIVE_RETRY);
933 archive_entry_copy_pathname(entry, tree_current_path(t));
935 * Perform path matching.
938 r = archive_match_path_excluded(a->matching, entry);
940 archive_set_error(&(a->archive), errno,
941 "Faild : %s", archive_error_string(a->matching));
945 if (a->excluded_cb_func)
946 a->excluded_cb_func(&(a->archive),
947 a->excluded_cb_data, entry);
948 return (ARCHIVE_RETRY);
953 * Distinguish 'L'/'P'/'H' symlink following.
955 switch(t->symlink_mode) {
957 /* 'H': After the first item, rest like 'P'. */
958 t->symlink_mode = 'P';
959 /* 'H': First item (from command line) like 'L'. */
962 /* 'L': Do descend through a symlink to dir. */
963 descend = tree_current_is_dir(t);
964 /* 'L': Follow symlinks to files. */
965 a->symlink_mode = 'L';
966 a->follow_symlinks = 1;
967 /* 'L': Archive symlinks as targets, if we can. */
968 st = tree_current_stat(t);
969 if (st != NULL && !tree_target_is_same_as_parent(t, st))
971 /* If stat fails, we have a broken symlink;
972 * in that case, don't follow the link. */
975 /* 'P': Don't descend through a symlink to dir. */
976 descend = tree_current_is_physical_dir(t);
977 /* 'P': Don't follow symlinks to files. */
978 a->symlink_mode = 'P';
979 a->follow_symlinks = 0;
980 /* 'P': Archive symlinks as symlinks. */
985 if (update_current_filesystem(a, st->st_dev) != ARCHIVE_OK) {
986 a->archive.state = ARCHIVE_STATE_FATAL;
987 tree_enter_initial_dir(t);
988 return (ARCHIVE_FATAL);
990 if (t->initial_filesystem_id == -1)
991 t->initial_filesystem_id = t->current_filesystem_id;
992 if (!a->traverse_mount_points) {
993 if (t->initial_filesystem_id != t->current_filesystem_id)
996 t->descend = descend;
1000 * If the file is marked with nodump flag, do not return this entry.
1002 if (a->honor_nodump) {
1003 #if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP)
1004 if (st->st_flags & UF_NODUMP)
1005 return (ARCHIVE_RETRY);
1006 #elif defined(EXT2_IOC_GETFLAGS) && defined(EXT2_NODUMP_FL) &&\
1007 defined(HAVE_WORKING_EXT2_IOC_GETFLAGS)
1008 if (S_ISREG(st->st_mode) || S_ISDIR(st->st_mode)) {
1011 t->entry_fd = open_on_current_dir(t,
1012 tree_current_access_path(t),
1013 O_RDONLY | O_NONBLOCK | O_CLOEXEC);
1014 __archive_ensure_cloexec_flag(t->entry_fd);
1015 if (t->entry_fd >= 0) {
1016 r = ioctl(t->entry_fd, EXT2_IOC_GETFLAGS,
1018 if (r == 0 && (stflags & EXT2_NODUMP_FL) != 0)
1019 return (ARCHIVE_RETRY);
1025 archive_entry_copy_stat(entry, st);
1027 /* Save the times to be restored. This must be in before
1028 * calling archive_read_disk_descend() or any chance of it,
1029 * especially, invokng a callback. */
1030 t->restore_time.mtime = archive_entry_mtime(entry);
1031 t->restore_time.mtime_nsec = archive_entry_mtime_nsec(entry);
1032 t->restore_time.atime = archive_entry_atime(entry);
1033 t->restore_time.atime_nsec = archive_entry_atime_nsec(entry);
1034 t->restore_time.filetype = archive_entry_filetype(entry);
1035 t->restore_time.noatime = t->current_filesystem->noatime;
1038 * Perform time matching.
1041 r = archive_match_time_excluded(a->matching, entry);
1043 archive_set_error(&(a->archive), errno,
1044 "Faild : %s", archive_error_string(a->matching));
1048 if (a->excluded_cb_func)
1049 a->excluded_cb_func(&(a->archive),
1050 a->excluded_cb_data, entry);
1051 return (ARCHIVE_RETRY);
1055 /* Lookup uname/gname */
1056 name = archive_read_disk_uname(&(a->archive), archive_entry_uid(entry));
1058 archive_entry_copy_uname(entry, name);
1059 name = archive_read_disk_gname(&(a->archive), archive_entry_gid(entry));
1061 archive_entry_copy_gname(entry, name);
1064 * Perform owner matching.
1067 r = archive_match_owner_excluded(a->matching, entry);
1069 archive_set_error(&(a->archive), errno,
1070 "Faild : %s", archive_error_string(a->matching));
1074 if (a->excluded_cb_func)
1075 a->excluded_cb_func(&(a->archive),
1076 a->excluded_cb_data, entry);
1077 return (ARCHIVE_RETRY);
1082 * Invoke a meta data filter callback.
1084 if (a->metadata_filter_func) {
1085 if (!a->metadata_filter_func(&(a->archive),
1086 a->metadata_filter_data, entry))
1087 return (ARCHIVE_RETRY);
1091 * Populate the archive_entry with metadata from the disk.
1093 archive_entry_copy_sourcepath(entry, tree_current_access_path(t));
1094 r = archive_read_disk_entry_from_file(&(a->archive), entry,
1101 _archive_read_next_header(struct archive *_a, struct archive_entry **entryp)
1104 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1106 ret = _archive_read_next_header2(_a, a->entry);
1112 _archive_read_next_header2(struct archive *_a, struct archive_entry *entry)
1114 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1118 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
1119 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
1120 "archive_read_next_header2");
1123 if (t->entry_fd >= 0) {
1124 close_and_restore_time(t->entry_fd, t, &t->restore_time);
1129 r = next_entry(a, t, entry);
1130 if (t->entry_fd >= 0) {
1135 if (r == ARCHIVE_RETRY) {
1136 archive_entry_clear(entry);
1142 /* Return to the initial directory. */
1143 tree_enter_initial_dir(t);
1146 * EOF and FATAL are persistent at this layer. By
1147 * modifying the state, we guarantee that future calls to
1148 * read a header or read data will fail.
1152 a->archive.state = ARCHIVE_STATE_EOF;
1156 /* Overwrite the sourcepath based on the initial directory. */
1157 archive_entry_copy_sourcepath(entry, tree_current_path(t));
1159 if (archive_entry_filetype(entry) == AE_IFREG) {
1160 t->nlink = archive_entry_nlink(entry);
1161 t->entry_remaining_bytes = archive_entry_size(entry);
1162 t->entry_eof = (t->entry_remaining_bytes == 0)? 1: 0;
1163 if (!t->entry_eof &&
1164 setup_sparse(a, entry) != ARCHIVE_OK)
1165 return (ARCHIVE_FATAL);
1167 t->entry_remaining_bytes = 0;
1170 a->archive.state = ARCHIVE_STATE_DATA;
1175 a->archive.state = ARCHIVE_STATE_FATAL;
1179 __archive_reset_read_data(&a->archive);
1184 setup_sparse(struct archive_read_disk *a, struct archive_entry *entry)
1186 struct tree *t = a->tree;
1187 int64_t length, offset;
1190 t->sparse_count = archive_entry_sparse_reset(entry);
1191 if (t->sparse_count+1 > t->sparse_list_size) {
1192 free(t->sparse_list);
1193 t->sparse_list_size = t->sparse_count + 1;
1194 t->sparse_list = malloc(sizeof(t->sparse_list[0]) *
1195 t->sparse_list_size);
1196 if (t->sparse_list == NULL) {
1197 t->sparse_list_size = 0;
1198 archive_set_error(&a->archive, ENOMEM,
1199 "Can't allocate data");
1200 a->archive.state = ARCHIVE_STATE_FATAL;
1201 return (ARCHIVE_FATAL);
1204 for (i = 0; i < t->sparse_count; i++) {
1205 archive_entry_sparse_next(entry, &offset, &length);
1206 t->sparse_list[i].offset = offset;
1207 t->sparse_list[i].length = length;
1210 t->sparse_list[i].offset = 0;
1211 t->sparse_list[i].length = archive_entry_size(entry);
1213 t->sparse_list[i].offset = archive_entry_size(entry);
1214 t->sparse_list[i].length = 0;
1216 t->current_sparse = t->sparse_list;
1218 return (ARCHIVE_OK);
1222 archive_read_disk_set_matching(struct archive *_a, struct archive *_ma,
1223 void (*_excluded_func)(struct archive *, void *, struct archive_entry *),
1226 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1227 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
1228 ARCHIVE_STATE_ANY, "archive_read_disk_set_matching");
1230 a->excluded_cb_func = _excluded_func;
1231 a->excluded_cb_data = _client_data;
1232 return (ARCHIVE_OK);
1236 archive_read_disk_set_metadata_filter_callback(struct archive *_a,
1237 int (*_metadata_filter_func)(struct archive *, void *,
1238 struct archive_entry *), void *_client_data)
1240 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1242 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_ANY,
1243 "archive_read_disk_set_metadata_filter_callback");
1245 a->metadata_filter_func = _metadata_filter_func;
1246 a->metadata_filter_data = _client_data;
1247 return (ARCHIVE_OK);
1251 archive_read_disk_can_descend(struct archive *_a)
1253 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1254 struct tree *t = a->tree;
1256 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
1257 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
1258 "archive_read_disk_can_descend");
1260 return (t->visit_type == TREE_REGULAR && t->descend);
1264 * Called by the client to mark the directory just returned from
1265 * tree_next() as needing to be visited.
1268 archive_read_disk_descend(struct archive *_a)
1270 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1271 struct tree *t = a->tree;
1273 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
1274 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
1275 "archive_read_disk_descend");
1277 if (t->visit_type != TREE_REGULAR || !t->descend)
1278 return (ARCHIVE_OK);
1280 if (tree_current_is_physical_dir(t)) {
1281 tree_push(t, t->basename, t->current_filesystem_id,
1282 t->lst.st_dev, t->lst.st_ino, &t->restore_time);
1283 t->stack->flags |= isDir;
1284 } else if (tree_current_is_dir(t)) {
1285 tree_push(t, t->basename, t->current_filesystem_id,
1286 t->st.st_dev, t->st.st_ino, &t->restore_time);
1287 t->stack->flags |= isDirLink;
1290 return (ARCHIVE_OK);
1294 archive_read_disk_open(struct archive *_a, const char *pathname)
1296 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1298 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
1299 ARCHIVE_STATE_NEW | ARCHIVE_STATE_CLOSED,
1300 "archive_read_disk_open");
1301 archive_clear_error(&a->archive);
1303 return (_archive_read_disk_open(_a, pathname));
1307 archive_read_disk_open_w(struct archive *_a, const wchar_t *pathname)
1309 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1310 struct archive_string path;
1313 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
1314 ARCHIVE_STATE_NEW | ARCHIVE_STATE_CLOSED,
1315 "archive_read_disk_open_w");
1316 archive_clear_error(&a->archive);
1318 /* Make a char string from a wchar_t string. */
1319 archive_string_init(&path);
1320 if (archive_string_append_from_wcs(&path, pathname,
1321 wcslen(pathname)) != 0) {
1322 if (errno == ENOMEM)
1323 archive_set_error(&a->archive, ENOMEM,
1324 "Can't allocate memory");
1326 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1327 "Can't convert a path to a char string");
1328 a->archive.state = ARCHIVE_STATE_FATAL;
1329 ret = ARCHIVE_FATAL;
1331 ret = _archive_read_disk_open(_a, path.s);
1333 archive_string_free(&path);
1338 _archive_read_disk_open(struct archive *_a, const char *pathname)
1340 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1342 if (a->tree != NULL)
1343 a->tree = tree_reopen(a->tree, pathname, a->restore_time);
1345 a->tree = tree_open(pathname, a->symlink_mode,
1347 if (a->tree == NULL) {
1348 archive_set_error(&a->archive, ENOMEM,
1349 "Can't allocate tar data");
1350 a->archive.state = ARCHIVE_STATE_FATAL;
1351 return (ARCHIVE_FATAL);
1353 a->archive.state = ARCHIVE_STATE_HEADER;
1355 return (ARCHIVE_OK);
1359 * Return a current filesystem ID which is index of the filesystem entry
1360 * you've visited through archive_read_disk.
1363 archive_read_disk_current_filesystem(struct archive *_a)
1365 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1367 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA,
1368 "archive_read_disk_current_filesystem");
1370 return (a->tree->current_filesystem_id);
1374 update_current_filesystem(struct archive_read_disk *a, int64_t dev)
1376 struct tree *t = a->tree;
1379 if (t->current_filesystem != NULL &&
1380 t->current_filesystem->dev == dev)
1381 return (ARCHIVE_OK);
1383 for (i = 0; i < t->max_filesystem_id; i++) {
1384 if (t->filesystem_table[i].dev == dev) {
1385 /* There is the filesytem ID we've already generated. */
1386 t->current_filesystem_id = i;
1387 t->current_filesystem = &(t->filesystem_table[i]);
1388 return (ARCHIVE_OK);
1393 * This is the new filesytem which we have to generate a new ID for.
1395 fid = t->max_filesystem_id++;
1396 if (t->max_filesystem_id > t->allocated_filesytem) {
1400 s = t->max_filesystem_id * 2;
1401 p = realloc(t->filesystem_table,
1402 s * sizeof(*t->filesystem_table));
1404 archive_set_error(&a->archive, ENOMEM,
1405 "Can't allocate tar data");
1406 return (ARCHIVE_FATAL);
1408 t->filesystem_table = (struct filesystem *)p;
1409 t->allocated_filesytem = s;
1411 t->current_filesystem_id = fid;
1412 t->current_filesystem = &(t->filesystem_table[fid]);
1413 t->current_filesystem->dev = dev;
1414 t->current_filesystem->allocation_ptr = NULL;
1415 t->current_filesystem->buff = NULL;
1417 /* Setup the current filesystem properties which depend on
1418 * platform specific. */
1419 return (setup_current_filesystem(a));
1423 * Returns 1 if current filesystem is generated filesystem, 0 if it is not
1424 * or -1 if it is unknown.
1427 archive_read_disk_current_filesystem_is_synthetic(struct archive *_a)
1429 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1431 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA,
1432 "archive_read_disk_current_filesystem");
1434 return (a->tree->current_filesystem->synthetic);
1438 * Returns 1 if current filesystem is remote filesystem, 0 if it is not
1439 * or -1 if it is unknown.
1442 archive_read_disk_current_filesystem_is_remote(struct archive *_a)
1444 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1446 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA,
1447 "archive_read_disk_current_filesystem");
1449 return (a->tree->current_filesystem->remote);
1452 #if defined(_PC_REC_INCR_XFER_SIZE) && defined(_PC_REC_MAX_XFER_SIZE) &&\
1453 defined(_PC_REC_MIN_XFER_SIZE) && defined(_PC_REC_XFER_ALIGN)
1455 get_xfer_size(struct tree *t, int fd, const char *path)
1457 t->current_filesystem->xfer_align = -1;
1460 t->current_filesystem->incr_xfer_size =
1461 fpathconf(fd, _PC_REC_INCR_XFER_SIZE);
1462 t->current_filesystem->max_xfer_size =
1463 fpathconf(fd, _PC_REC_MAX_XFER_SIZE);
1464 t->current_filesystem->min_xfer_size =
1465 fpathconf(fd, _PC_REC_MIN_XFER_SIZE);
1466 t->current_filesystem->xfer_align =
1467 fpathconf(fd, _PC_REC_XFER_ALIGN);
1468 } else if (path != NULL) {
1469 t->current_filesystem->incr_xfer_size =
1470 pathconf(path, _PC_REC_INCR_XFER_SIZE);
1471 t->current_filesystem->max_xfer_size =
1472 pathconf(path, _PC_REC_MAX_XFER_SIZE);
1473 t->current_filesystem->min_xfer_size =
1474 pathconf(path, _PC_REC_MIN_XFER_SIZE);
1475 t->current_filesystem->xfer_align =
1476 pathconf(path, _PC_REC_XFER_ALIGN);
1478 /* At least we need an alignment size. */
1479 if (t->current_filesystem->xfer_align == -1)
1480 return ((errno == EINVAL)?1:-1);
1486 get_xfer_size(struct tree *t, int fd, const char *path)
1488 (void)t; /* UNUSED */
1489 (void)fd; /* UNUSED */
1490 (void)path; /* UNUSED */
1491 return (1);/* Not supported */
1495 #if defined(HAVE_STATFS) && defined(HAVE_FSTATFS) && defined(MNT_LOCAL) \
1496 && !defined(ST_LOCAL)
1499 * Gather current filesystem properties on FreeBSD, OpenBSD and Mac OS X.
1502 setup_current_filesystem(struct archive_read_disk *a)
1504 struct tree *t = a->tree;
1506 #if defined(HAVE_GETVFSBYNAME) && defined(VFCF_SYNTHETIC)
1507 # if defined(HAVE_STRUCT_VFSCONF)
1510 struct xvfsconf vfc;
1514 #if !defined(HAVE_STRUCT_STATFS_F_NAMEMAX)
1518 t->current_filesystem->synthetic = -1;
1519 t->current_filesystem->remote = -1;
1520 if (tree_current_is_symblic_link_target(t)) {
1521 #if defined(HAVE_OPENAT)
1523 * Get file system statistics on any directory
1526 int fd = openat(tree_current_dir_fd(t),
1527 tree_current_access_path(t), O_RDONLY | O_CLOEXEC);
1528 __archive_ensure_cloexec_flag(fd);
1530 archive_set_error(&a->archive, errno,
1532 return (ARCHIVE_FAILED);
1534 r = fstatfs(fd, &sfs);
1536 xr = get_xfer_size(t, fd, NULL);
1539 if (tree_enter_working_dir(t) != 0) {
1540 archive_set_error(&a->archive, errno, "fchdir failed");
1541 return (ARCHIVE_FAILED);
1543 r = statfs(tree_current_access_path(t), &sfs);
1545 xr = get_xfer_size(t, -1, tree_current_access_path(t));
1548 r = fstatfs(tree_current_dir_fd(t), &sfs);
1550 xr = get_xfer_size(t, tree_current_dir_fd(t), NULL);
1552 if (r == -1 || xr == -1) {
1553 archive_set_error(&a->archive, errno, "statfs failed");
1554 return (ARCHIVE_FAILED);
1555 } else if (xr == 1) {
1556 /* pathconf(_PC_REX_*) operations are not supported. */
1557 t->current_filesystem->xfer_align = sfs.f_bsize;
1558 t->current_filesystem->max_xfer_size = -1;
1559 t->current_filesystem->min_xfer_size = sfs.f_iosize;
1560 t->current_filesystem->incr_xfer_size = sfs.f_iosize;
1562 if (sfs.f_flags & MNT_LOCAL)
1563 t->current_filesystem->remote = 0;
1565 t->current_filesystem->remote = 1;
1567 #if defined(HAVE_GETVFSBYNAME) && defined(VFCF_SYNTHETIC)
1568 r = getvfsbyname(sfs.f_fstypename, &vfc);
1570 archive_set_error(&a->archive, errno, "getvfsbyname failed");
1571 return (ARCHIVE_FAILED);
1573 if (vfc.vfc_flags & VFCF_SYNTHETIC)
1574 t->current_filesystem->synthetic = 1;
1576 t->current_filesystem->synthetic = 0;
1579 #if defined(MNT_NOATIME)
1580 if (sfs.f_flags & MNT_NOATIME)
1581 t->current_filesystem->noatime = 1;
1584 t->current_filesystem->noatime = 0;
1586 #if defined(HAVE_READDIR_R)
1587 /* Set maximum filename length. */
1588 #if defined(HAVE_STRUCT_STATFS_F_NAMEMAX)
1589 t->current_filesystem->name_max = sfs.f_namemax;
1591 # if defined(_PC_NAME_MAX)
1592 /* Mac OS X does not have f_namemax in struct statfs. */
1593 if (tree_current_is_symblic_link_target(t)) {
1594 if (tree_enter_working_dir(t) != 0) {
1595 archive_set_error(&a->archive, errno, "fchdir failed");
1596 return (ARCHIVE_FAILED);
1598 nm = pathconf(tree_current_access_path(t), _PC_NAME_MAX);
1600 nm = fpathconf(tree_current_dir_fd(t), _PC_NAME_MAX);
1605 t->current_filesystem->name_max = NAME_MAX;
1607 t->current_filesystem->name_max = nm;
1609 #endif /* HAVE_READDIR_R */
1610 return (ARCHIVE_OK);
1613 #elif (defined(HAVE_STATVFS) || defined(HAVE_FSTATVFS)) && defined(ST_LOCAL)
1616 * Gather current filesystem properties on NetBSD
1619 setup_current_filesystem(struct archive_read_disk *a)
1621 struct tree *t = a->tree;
1625 t->current_filesystem->synthetic = -1;
1626 if (tree_enter_working_dir(t) != 0) {
1627 archive_set_error(&a->archive, errno, "fchdir failed");
1628 return (ARCHIVE_FAILED);
1630 if (tree_current_is_symblic_link_target(t)) {
1631 r = statvfs(tree_current_access_path(t), &sfs);
1633 xr = get_xfer_size(t, -1, tree_current_access_path(t));
1635 #ifdef HAVE_FSTATVFS
1636 r = fstatvfs(tree_current_dir_fd(t), &sfs);
1638 xr = get_xfer_size(t, tree_current_dir_fd(t), NULL);
1640 r = statvfs(".", &sfs);
1642 xr = get_xfer_size(t, -1, ".");
1645 if (r == -1 || xr == -1) {
1646 t->current_filesystem->remote = -1;
1647 archive_set_error(&a->archive, errno, "statvfs failed");
1648 return (ARCHIVE_FAILED);
1649 } else if (xr == 1) {
1650 /* Usuall come here unless NetBSD supports _PC_REC_XFER_ALIGN
1651 * for pathconf() function. */
1652 t->current_filesystem->xfer_align = sfs.f_frsize;
1653 t->current_filesystem->max_xfer_size = -1;
1654 #if defined(HAVE_STRUCT_STATVFS_F_IOSIZE)
1655 t->current_filesystem->min_xfer_size = sfs.f_iosize;
1656 t->current_filesystem->incr_xfer_size = sfs.f_iosize;
1658 t->current_filesystem->min_xfer_size = sfs.f_bsize;
1659 t->current_filesystem->incr_xfer_size = sfs.f_bsize;
1662 if (sfs.f_flag & ST_LOCAL)
1663 t->current_filesystem->remote = 0;
1665 t->current_filesystem->remote = 1;
1667 #if defined(ST_NOATIME)
1668 if (sfs.f_flag & ST_NOATIME)
1669 t->current_filesystem->noatime = 1;
1672 t->current_filesystem->noatime = 0;
1674 /* Set maximum filename length. */
1675 t->current_filesystem->name_max = sfs.f_namemax;
1676 return (ARCHIVE_OK);
1679 #elif defined(HAVE_SYS_STATFS_H) && defined(HAVE_LINUX_MAGIC_H) &&\
1680 defined(HAVE_STATFS) && defined(HAVE_FSTATFS)
1682 * Note: statfs is deprecated since LSB 3.2
1685 #ifndef CIFS_SUPER_MAGIC
1686 #define CIFS_SUPER_MAGIC 0xFF534D42
1688 #ifndef DEVFS_SUPER_MAGIC
1689 #define DEVFS_SUPER_MAGIC 0x1373
1693 * Gather current filesystem properties on Linux
1696 setup_current_filesystem(struct archive_read_disk *a)
1698 struct tree *t = a->tree;
1700 #if defined(HAVE_STATVFS)
1701 struct statvfs svfs;
1703 int r, vr = 0, xr = 0;
1705 if (tree_current_is_symblic_link_target(t)) {
1706 #if defined(HAVE_OPENAT)
1708 * Get file system statistics on any directory
1711 int fd = openat(tree_current_dir_fd(t),
1712 tree_current_access_path(t), O_RDONLY | O_CLOEXEC);
1713 __archive_ensure_cloexec_flag(fd);
1715 archive_set_error(&a->archive, errno,
1717 return (ARCHIVE_FAILED);
1719 #if defined(HAVE_FSTATVFS)
1720 vr = fstatvfs(fd, &svfs);/* for f_flag, mount flags */
1722 r = fstatfs(fd, &sfs);
1724 xr = get_xfer_size(t, fd, NULL);
1727 if (tree_enter_working_dir(t) != 0) {
1728 archive_set_error(&a->archive, errno, "fchdir failed");
1729 return (ARCHIVE_FAILED);
1731 #if defined(HAVE_STATVFS)
1732 vr = statvfs(tree_current_access_path(t), &svfs);
1734 r = statfs(tree_current_access_path(t), &sfs);
1736 xr = get_xfer_size(t, -1, tree_current_access_path(t));
1740 #if defined(HAVE_FSTATVFS)
1741 vr = fstatvfs(tree_current_dir_fd(t), &svfs);
1743 r = fstatfs(tree_current_dir_fd(t), &sfs);
1745 xr = get_xfer_size(t, tree_current_dir_fd(t), NULL);
1747 if (tree_enter_working_dir(t) != 0) {
1748 archive_set_error(&a->archive, errno, "fchdir failed");
1749 return (ARCHIVE_FAILED);
1751 #if defined(HAVE_STATVFS)
1752 vr = statvfs(".", &svfs);
1754 r = statfs(".", &sfs);
1756 xr = get_xfer_size(t, -1, ".");
1759 if (r == -1 || xr == -1 || vr == -1) {
1760 t->current_filesystem->synthetic = -1;
1761 t->current_filesystem->remote = -1;
1762 archive_set_error(&a->archive, errno, "statfs failed");
1763 return (ARCHIVE_FAILED);
1764 } else if (xr == 1) {
1765 /* pathconf(_PC_REX_*) operations are not supported. */
1766 #if defined(HAVE_STATVFS)
1767 t->current_filesystem->xfer_align = svfs.f_frsize;
1768 t->current_filesystem->max_xfer_size = -1;
1769 t->current_filesystem->min_xfer_size = svfs.f_bsize;
1770 t->current_filesystem->incr_xfer_size = svfs.f_bsize;
1772 t->current_filesystem->xfer_align = sfs.f_frsize;
1773 t->current_filesystem->max_xfer_size = -1;
1774 t->current_filesystem->min_xfer_size = sfs.f_bsize;
1775 t->current_filesystem->incr_xfer_size = sfs.f_bsize;
1778 switch (sfs.f_type) {
1779 case AFS_SUPER_MAGIC:
1780 case CIFS_SUPER_MAGIC:
1781 case CODA_SUPER_MAGIC:
1782 case NCP_SUPER_MAGIC:/* NetWare */
1783 case NFS_SUPER_MAGIC:
1784 case SMB_SUPER_MAGIC:
1785 t->current_filesystem->remote = 1;
1786 t->current_filesystem->synthetic = 0;
1788 case DEVFS_SUPER_MAGIC:
1789 case PROC_SUPER_MAGIC:
1790 case USBDEVICE_SUPER_MAGIC:
1791 t->current_filesystem->remote = 0;
1792 t->current_filesystem->synthetic = 1;
1795 t->current_filesystem->remote = 0;
1796 t->current_filesystem->synthetic = 0;
1800 #if defined(ST_NOATIME)
1801 #if defined(HAVE_STATVFS)
1802 if (svfs.f_flag & ST_NOATIME)
1804 if (sfs.f_flag & ST_NOATIME)
1806 t->current_filesystem->noatime = 1;
1809 t->current_filesystem->noatime = 0;
1811 #if defined(HAVE_READDIR_R)
1812 /* Set maximum filename length. */
1813 t->current_filesystem->name_max = sfs.f_namelen;
1815 return (ARCHIVE_OK);
1818 #elif defined(HAVE_SYS_STATVFS_H) &&\
1819 (defined(HAVE_STATVFS) || defined(HAVE_FSTATVFS))
1822 * Gather current filesystem properties on other posix platform.
1825 setup_current_filesystem(struct archive_read_disk *a)
1827 struct tree *t = a->tree;
1831 t->current_filesystem->synthetic = -1;/* Not supported */
1832 t->current_filesystem->remote = -1;/* Not supported */
1833 if (tree_current_is_symblic_link_target(t)) {
1834 #if defined(HAVE_OPENAT)
1836 * Get file system statistics on any directory
1839 int fd = openat(tree_current_dir_fd(t),
1840 tree_current_access_path(t), O_RDONLY | O_CLOEXEC);
1841 __archive_ensure_cloexec_flag(fd);
1843 archive_set_error(&a->archive, errno,
1845 return (ARCHIVE_FAILED);
1847 r = fstatvfs(fd, &sfs);
1849 xr = get_xfer_size(t, fd, NULL);
1852 if (tree_enter_working_dir(t) != 0) {
1853 archive_set_error(&a->archive, errno, "fchdir failed");
1854 return (ARCHIVE_FAILED);
1856 r = statvfs(tree_current_access_path(t), &sfs);
1858 xr = get_xfer_size(t, -1, tree_current_access_path(t));
1861 #ifdef HAVE_FSTATVFS
1862 r = fstatvfs(tree_current_dir_fd(t), &sfs);
1864 xr = get_xfer_size(t, tree_current_dir_fd(t), NULL);
1866 if (tree_enter_working_dir(t) != 0) {
1867 archive_set_error(&a->archive, errno, "fchdir failed");
1868 return (ARCHIVE_FAILED);
1870 r = statvfs(".", &sfs);
1872 xr = get_xfer_size(t, -1, ".");
1875 if (r == -1 || xr == -1) {
1876 t->current_filesystem->synthetic = -1;
1877 t->current_filesystem->remote = -1;
1878 archive_set_error(&a->archive, errno, "statvfs failed");
1879 return (ARCHIVE_FAILED);
1880 } else if (xr == 1) {
1881 /* pathconf(_PC_REX_*) operations are not supported. */
1882 t->current_filesystem->xfer_align = sfs.f_frsize;
1883 t->current_filesystem->max_xfer_size = -1;
1884 t->current_filesystem->min_xfer_size = sfs.f_bsize;
1885 t->current_filesystem->incr_xfer_size = sfs.f_bsize;
1888 #if defined(ST_NOATIME)
1889 if (sfs.f_flag & ST_NOATIME)
1890 t->current_filesystem->noatime = 1;
1893 t->current_filesystem->noatime = 0;
1895 #if defined(HAVE_READDIR_R)
1896 /* Set maximum filename length. */
1897 t->current_filesystem->name_max = sfs.f_namemax;
1899 return (ARCHIVE_OK);
1905 * Generic: Gather current filesystem properties.
1906 * TODO: Is this generic function really needed?
1909 setup_current_filesystem(struct archive_read_disk *a)
1911 struct tree *t = a->tree;
1912 #if defined(_PC_NAME_MAX) && defined(HAVE_READDIR_R)
1915 t->current_filesystem->synthetic = -1;/* Not supported */
1916 t->current_filesystem->remote = -1;/* Not supported */
1917 t->current_filesystem->noatime = 0;
1918 (void)get_xfer_size(t, -1, ".");/* Dummy call to avoid build error. */
1919 t->current_filesystem->xfer_align = -1;/* Unknown */
1920 t->current_filesystem->max_xfer_size = -1;
1921 t->current_filesystem->min_xfer_size = -1;
1922 t->current_filesystem->incr_xfer_size = -1;
1924 #if defined(HAVE_READDIR_R)
1925 /* Set maximum filename length. */
1926 # if defined(_PC_NAME_MAX)
1927 if (tree_current_is_symblic_link_target(t)) {
1928 if (tree_enter_working_dir(t) != 0) {
1929 archive_set_error(&a->archive, errno, "fchdir failed");
1930 return (ARCHIVE_FAILED);
1932 nm = pathconf(tree_current_access_path(t), _PC_NAME_MAX);
1934 nm = fpathconf(tree_current_dir_fd(t), _PC_NAME_MAX);
1936 # endif /* _PC_NAME_MAX */
1938 * Some sysmtes (HP-UX or others?) incorrectly defined
1939 * NAME_MAX macro to be a smaller value.
1941 # if defined(NAME_MAX) && NAME_MAX >= 255
1942 t->current_filesystem->name_max = NAME_MAX;
1944 /* No way to get a trusted value of maximum filename
1946 t->current_filesystem->name_max = PATH_MAX;
1947 # endif /* NAME_MAX */
1948 # if defined(_PC_NAME_MAX)
1950 t->current_filesystem->name_max = nm;
1951 # endif /* _PC_NAME_MAX */
1952 #endif /* HAVE_READDIR_R */
1953 return (ARCHIVE_OK);
1959 close_and_restore_time(int fd, struct tree *t, struct restore_time *rt)
1962 (void)t; /* UNUSED */
1963 (void)rt; /* UNUSED */
1966 #if defined(HAVE_FUTIMENS) && !defined(__CYGWIN__)
1967 struct timespec timespecs[2];
1969 struct timeval times[2];
1971 if ((t->flags & needsRestoreTimes) == 0 || rt->noatime) {
1978 #if defined(HAVE_FUTIMENS) && !defined(__CYGWIN__)
1979 timespecs[1].tv_sec = rt->mtime;
1980 timespecs[1].tv_nsec = rt->mtime_nsec;
1982 timespecs[0].tv_sec = rt->atime;
1983 timespecs[0].tv_nsec = rt->atime_nsec;
1984 /* futimens() is defined in POSIX.1-2008. */
1985 if (futimens(fd, timespecs) == 0)
1989 times[1].tv_sec = rt->mtime;
1990 times[1].tv_usec = rt->mtime_nsec / 1000;
1992 times[0].tv_sec = rt->atime;
1993 times[0].tv_usec = rt->atime_nsec / 1000;
1995 #if !defined(HAVE_FUTIMENS) && defined(HAVE_FUTIMES) && !defined(__CYGWIN__)
1996 if (futimes(fd, times) == 0)
2000 #if defined(HAVE_FUTIMESAT)
2001 if (futimesat(tree_current_dir_fd(t), rt->name, times) == 0)
2005 if (lutimes(rt->name, times) != 0)
2007 if (AE_IFLNK != rt->filetype && utimes(rt->name, times) != 0)
2015 open_on_current_dir(struct tree *t, const char *path, int flags)
2018 return (openat(tree_current_dir_fd(t), path, flags));
2020 if (tree_enter_working_dir(t) != 0)
2022 return (open(path, flags));
2030 #ifdef F_DUPFD_CLOEXEC
2031 static volatile int can_dupfd_cloexec = 1;
2033 if (can_dupfd_cloexec) {
2034 new_fd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
2037 /* Linux 2.6.18 - 2.6.23 declare F_DUPFD_CLOEXEC,
2038 * but it cannot be used. So we have to try dup(). */
2039 /* We won't try F_DUPFD_CLOEXEC. */
2040 can_dupfd_cloexec = 0;
2042 #endif /* F_DUPFD_CLOEXEC */
2044 __archive_ensure_cloexec_flag(new_fd);
2049 * Add a directory path to the current stack.
2052 tree_push(struct tree *t, const char *path, int filesystem_id,
2053 int64_t dev, int64_t ino, struct restore_time *rt)
2055 struct tree_entry *te;
2057 te = malloc(sizeof(*te));
2058 memset(te, 0, sizeof(*te));
2059 te->next = t->stack;
2060 te->parent = t->current;
2062 te->depth = te->parent->depth + 1;
2064 archive_string_init(&te->name);
2065 te->symlink_parent_fd = -1;
2066 archive_strcpy(&te->name, path);
2067 te->flags = needsDescent | needsOpen | needsAscent;
2068 te->filesystem_id = filesystem_id;
2071 te->dirname_length = t->dirname_length;
2072 te->restore_time.name = te->name.s;
2074 te->restore_time.mtime = rt->mtime;
2075 te->restore_time.mtime_nsec = rt->mtime_nsec;
2076 te->restore_time.atime = rt->atime;
2077 te->restore_time.atime_nsec = rt->atime_nsec;
2078 te->restore_time.filetype = rt->filetype;
2079 te->restore_time.noatime = rt->noatime;
2084 * Append a name to the current dir path.
2087 tree_append(struct tree *t, const char *name, size_t name_length)
2091 t->path.s[t->dirname_length] = '\0';
2092 t->path.length = t->dirname_length;
2093 /* Strip trailing '/' from name, unless entire name is "/". */
2094 while (name_length > 1 && name[name_length - 1] == '/')
2097 /* Resize pathname buffer as needed. */
2098 size_needed = name_length + t->dirname_length + 2;
2099 archive_string_ensure(&t->path, size_needed);
2100 /* Add a separating '/' if it's needed. */
2101 if (t->dirname_length > 0 && t->path.s[archive_strlen(&t->path)-1] != '/')
2102 archive_strappend_char(&t->path, '/');
2103 t->basename = t->path.s + archive_strlen(&t->path);
2104 archive_strncat(&t->path, name, name_length);
2105 t->restore_time.name = t->basename;
2109 * Open a directory tree for traversal.
2111 static struct tree *
2112 tree_open(const char *path, int symlink_mode, int restore_time)
2116 if ((t = malloc(sizeof(*t))) == NULL)
2118 memset(t, 0, sizeof(*t));
2119 archive_string_init(&t->path);
2120 archive_string_ensure(&t->path, 31);
2121 t->initial_symlink_mode = symlink_mode;
2122 return (tree_reopen(t, path, restore_time));
2125 static struct tree *
2126 tree_reopen(struct tree *t, const char *path, int restore_time)
2128 t->flags = (restore_time)?needsRestoreTimes:0;
2129 t->flags |= onInitialDir;
2132 t->dirname_length = 0;
2136 t->d = INVALID_DIR_HANDLE;
2137 t->symlink_mode = t->initial_symlink_mode;
2138 archive_string_empty(&t->path);
2141 t->entry_remaining_bytes = 0;
2142 t->initial_filesystem_id = -1;
2144 /* First item is set up a lot like a symlink traversal. */
2145 tree_push(t, path, 0, 0, 0, NULL);
2146 t->stack->flags = needsFirstVisit;
2147 t->maxOpenCount = t->openCount = 1;
2148 t->initial_dir_fd = open(".", O_RDONLY | O_CLOEXEC);
2149 __archive_ensure_cloexec_flag(t->initial_dir_fd);
2150 t->working_dir_fd = tree_dup(t->initial_dir_fd);
2155 tree_descent(struct tree *t)
2157 int flag, new_fd, r = 0;
2159 t->dirname_length = archive_strlen(&t->path);
2160 flag = O_RDONLY | O_CLOEXEC;
2161 #if defined(O_DIRECTORY)
2162 flag |= O_DIRECTORY;
2164 new_fd = open_on_current_dir(t, t->stack->name.s, flag);
2165 __archive_ensure_cloexec_flag(new_fd);
2167 t->tree_errno = errno;
2171 /* If it is a link, set up fd for the ascent. */
2172 if (t->stack->flags & isDirLink) {
2173 t->stack->symlink_parent_fd = t->working_dir_fd;
2175 if (t->openCount > t->maxOpenCount)
2176 t->maxOpenCount = t->openCount;
2178 close(t->working_dir_fd);
2179 /* Renew the current working directory. */
2180 t->working_dir_fd = new_fd;
2181 t->flags &= ~onWorkingDir;
2187 * We've finished a directory; ascend back to the parent.
2190 tree_ascend(struct tree *t)
2192 struct tree_entry *te;
2193 int new_fd, r = 0, prev_dir_fd;
2196 prev_dir_fd = t->working_dir_fd;
2197 if (te->flags & isDirLink)
2198 new_fd = te->symlink_parent_fd;
2200 new_fd = open_on_current_dir(t, "..", O_RDONLY | O_CLOEXEC);
2201 __archive_ensure_cloexec_flag(new_fd);
2204 t->tree_errno = errno;
2205 r = TREE_ERROR_FATAL;
2207 /* Renew the current working directory. */
2208 t->working_dir_fd = new_fd;
2209 t->flags &= ~onWorkingDir;
2210 /* Current directory has been changed, we should
2211 * close an fd of previous working directory. */
2212 close_and_restore_time(prev_dir_fd, t, &te->restore_time);
2213 if (te->flags & isDirLink) {
2215 te->symlink_parent_fd = -1;
2223 * Return to the initial directory where tree_open() was performed.
2226 tree_enter_initial_dir(struct tree *t)
2230 if ((t->flags & onInitialDir) == 0) {
2231 r = fchdir(t->initial_dir_fd);
2233 t->flags &= ~onWorkingDir;
2234 t->flags |= onInitialDir;
2241 * Restore working directory of directory traversals.
2244 tree_enter_working_dir(struct tree *t)
2249 * Change the current directory if really needed.
2250 * Sometimes this is unneeded when we did not do
2253 if (t->depth > 0 && (t->flags & onWorkingDir) == 0) {
2254 r = fchdir(t->working_dir_fd);
2256 t->flags &= ~onInitialDir;
2257 t->flags |= onWorkingDir;
2264 tree_current_dir_fd(struct tree *t)
2266 return (t->working_dir_fd);
2270 * Pop the working stack.
2273 tree_pop(struct tree *t)
2275 struct tree_entry *te;
2277 t->path.s[t->dirname_length] = '\0';
2278 t->path.length = t->dirname_length;
2279 if (t->stack == t->current && t->current != NULL)
2280 t->current = t->current->parent;
2282 t->stack = te->next;
2283 t->dirname_length = te->dirname_length;
2284 t->basename = t->path.s + t->dirname_length;
2285 while (t->basename[0] == '/')
2287 archive_string_free(&te->name);
2292 * Get the next item in the tree traversal.
2295 tree_next(struct tree *t)
2299 while (t->stack != NULL) {
2300 /* If there's an open dir, get the next entry from there. */
2301 if (t->d != INVALID_DIR_HANDLE) {
2302 r = tree_dir_next_posix(t);
2308 if (t->stack->flags & needsFirstVisit) {
2309 /* Top stack item needs a regular visit. */
2310 t->current = t->stack;
2311 tree_append(t, t->stack->name.s,
2312 archive_strlen(&(t->stack->name)));
2313 /* t->dirname_length = t->path_length; */
2315 t->stack->flags &= ~needsFirstVisit;
2316 return (t->visit_type = TREE_REGULAR);
2317 } else if (t->stack->flags & needsDescent) {
2318 /* Top stack item is dir to descend into. */
2319 t->current = t->stack;
2320 tree_append(t, t->stack->name.s,
2321 archive_strlen(&(t->stack->name)));
2322 t->stack->flags &= ~needsDescent;
2323 r = tree_descent(t);
2328 t->visit_type = TREE_POSTDESCENT;
2329 return (t->visit_type);
2330 } else if (t->stack->flags & needsOpen) {
2331 t->stack->flags &= ~needsOpen;
2332 r = tree_dir_next_posix(t);
2336 } else if (t->stack->flags & needsAscent) {
2337 /* Top stack item is dir and we're done with it. */
2340 t->visit_type = r != 0 ? r : TREE_POSTASCENT;
2341 return (t->visit_type);
2343 /* Top item on stack is dead. */
2345 t->flags &= ~hasLstat;
2346 t->flags &= ~hasStat;
2349 return (t->visit_type = 0);
2353 tree_dir_next_posix(struct tree *t)
2360 #if defined(HAVE_READDIR_R)
2364 #if defined(HAVE_FDOPENDIR)
2365 t->d = fdopendir(tree_dup(t->working_dir_fd));
2366 #else /* HAVE_FDOPENDIR */
2367 if (tree_enter_working_dir(t) == 0) {
2368 t->d = opendir(".");
2369 #if HAVE_DIRFD || defined(dirfd)
2370 __archive_ensure_cloexec_flag(dirfd(t->d));
2373 #endif /* HAVE_FDOPENDIR */
2375 r = tree_ascend(t); /* Undo "chdir" */
2377 t->tree_errno = errno;
2378 t->visit_type = r != 0 ? r : TREE_ERROR_DIR;
2379 return (t->visit_type);
2381 #if defined(HAVE_READDIR_R)
2382 dirent_size = offsetof(struct dirent, d_name) +
2383 t->filesystem_table[t->current->filesystem_id].name_max + 1;
2384 if (t->dirent == NULL || t->dirent_allocated < dirent_size) {
2386 t->dirent = malloc(dirent_size);
2387 if (t->dirent == NULL) {
2389 t->d = INVALID_DIR_HANDLE;
2390 (void)tree_ascend(t);
2392 t->tree_errno = ENOMEM;
2393 t->visit_type = TREE_ERROR_DIR;
2394 return (t->visit_type);
2396 t->dirent_allocated = dirent_size;
2398 #endif /* HAVE_READDIR_R */
2402 #if defined(HAVE_READDIR_R)
2403 r = readdir_r(t->d, t->dirent, &t->de);
2405 /* Note: According to the man page, return value 9 indicates
2406 * that the readdir_r was not successful and the error code
2407 * is set to the global errno variable. And then if the end
2408 * of directory entries was reached, the return value is 9
2409 * and the third parameter is set to NULL and errno is
2414 if (r != 0 || t->de == NULL) {
2416 t->de = readdir(t->d);
2417 if (t->de == NULL) {
2421 t->d = INVALID_DIR_HANDLE;
2424 t->visit_type = TREE_ERROR_DIR;
2425 return (t->visit_type);
2429 name = t->de->d_name;
2430 namelen = D_NAMELEN(t->de);
2431 t->flags &= ~hasLstat;
2432 t->flags &= ~hasStat;
2433 if (name[0] == '.' && name[1] == '\0')
2435 if (name[0] == '.' && name[1] == '.' && name[2] == '\0')
2437 tree_append(t, name, namelen);
2438 return (t->visit_type = TREE_REGULAR);
2444 * Get the stat() data for the entry just returned from tree_next().
2446 static const struct stat *
2447 tree_current_stat(struct tree *t)
2449 if (!(t->flags & hasStat)) {
2451 if (fstatat(tree_current_dir_fd(t),
2452 tree_current_access_path(t), &t->st, 0) != 0)
2454 if (tree_enter_working_dir(t) != 0)
2456 if (stat(tree_current_access_path(t), &t->st) != 0)
2459 t->flags |= hasStat;
2465 * Get the lstat() data for the entry just returned from tree_next().
2467 static const struct stat *
2468 tree_current_lstat(struct tree *t)
2470 if (!(t->flags & hasLstat)) {
2472 if (fstatat(tree_current_dir_fd(t),
2473 tree_current_access_path(t), &t->lst,
2474 AT_SYMLINK_NOFOLLOW) != 0)
2476 if (tree_enter_working_dir(t) != 0)
2478 if (lstat(tree_current_access_path(t), &t->lst) != 0)
2481 t->flags |= hasLstat;
2487 * Test whether current entry is a dir or link to a dir.
2490 tree_current_is_dir(struct tree *t)
2492 const struct stat *st;
2494 * If we already have lstat() info, then try some
2495 * cheap tests to determine if this is a dir.
2497 if (t->flags & hasLstat) {
2498 /* If lstat() says it's a dir, it must be a dir. */
2499 st = tree_current_lstat(t);
2502 if (S_ISDIR(st->st_mode))
2504 /* Not a dir; might be a link to a dir. */
2505 /* If it's not a link, then it's not a link to a dir. */
2506 if (!S_ISLNK(st->st_mode))
2509 * It's a link, but we don't know what it's a link to,
2510 * so we'll have to use stat().
2514 st = tree_current_stat(t);
2515 /* If we can't stat it, it's not a dir. */
2518 /* Use the definitive test. Hopefully this is cached. */
2519 return (S_ISDIR(st->st_mode));
2523 * Test whether current entry is a physical directory. Usually, we
2524 * already have at least one of stat() or lstat() in memory, so we
2525 * use tricks to try to avoid an extra trip to the disk.
2528 tree_current_is_physical_dir(struct tree *t)
2530 const struct stat *st;
2533 * If stat() says it isn't a dir, then it's not a dir.
2534 * If stat() data is cached, this check is free, so do it first.
2536 if (t->flags & hasStat) {
2537 st = tree_current_stat(t);
2540 if (!S_ISDIR(st->st_mode))
2545 * Either stat() said it was a dir (in which case, we have
2546 * to determine whether it's really a link to a dir) or
2547 * stat() info wasn't available. So we use lstat(), which
2548 * hopefully is already cached.
2551 st = tree_current_lstat(t);
2552 /* If we can't stat it, it's not a dir. */
2555 /* Use the definitive test. Hopefully this is cached. */
2556 return (S_ISDIR(st->st_mode));
2560 * Test whether the same file has been in the tree as its parent.
2563 tree_target_is_same_as_parent(struct tree *t, const struct stat *st)
2565 struct tree_entry *te;
2567 for (te = t->current->parent; te != NULL; te = te->parent) {
2568 if (te->dev == (int64_t)st->st_dev &&
2569 te->ino == (int64_t)st->st_ino)
2576 * Test whether the current file is symbolic link target and
2577 * on the other filesystem.
2580 tree_current_is_symblic_link_target(struct tree *t)
2582 static const struct stat *lst, *st;
2584 lst = tree_current_lstat(t);
2585 st = tree_current_stat(t);
2586 return (st != NULL && lst != NULL &&
2587 (int64_t)st->st_dev == t->current_filesystem->dev &&
2588 st->st_dev != lst->st_dev);
2592 * Return the access path for the entry just returned from tree_next().
2595 tree_current_access_path(struct tree *t)
2597 return (t->basename);
2601 * Return the full path for the entry just returned from tree_next().
2604 tree_current_path(struct tree *t)
2610 * Terminate the traversal.
2613 tree_close(struct tree *t)
2618 if (t->entry_fd >= 0) {
2619 close_and_restore_time(t->entry_fd, t, &t->restore_time);
2622 /* Close the handle of readdir(). */
2623 if (t->d != INVALID_DIR_HANDLE) {
2625 t->d = INVALID_DIR_HANDLE;
2627 /* Release anything remaining in the stack. */
2628 while (t->stack != NULL) {
2629 if (t->stack->flags & isDirLink)
2630 close(t->stack->symlink_parent_fd);
2633 if (t->working_dir_fd >= 0) {
2634 close(t->working_dir_fd);
2635 t->working_dir_fd = -1;
2637 if (t->initial_dir_fd >= 0) {
2638 close(t->initial_dir_fd);
2639 t->initial_dir_fd = -1;
2644 * Release any resources.
2647 tree_free(struct tree *t)
2653 archive_string_free(&t->path);
2654 #if defined(HAVE_READDIR_R)
2657 free(t->sparse_list);
2658 for (i = 0; i < t->max_filesystem_id; i++)
2659 free(t->filesystem_table[i].allocation_ptr);
2660 free(t->filesystem_table);