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(USE_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(USE_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_filesystem;
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, la_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, la_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, la_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, la_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->flags = ARCHIVE_READDISK_MAC_COPYFILE;
469 a->open_on_current_dir = open_on_current_dir;
470 a->tree_current_dir_fd = tree_current_dir_fd;
471 a->tree_enter_working_dir = tree_enter_working_dir;
472 return (&a->archive);
476 _archive_read_free(struct archive *_a)
478 struct archive_read_disk *a = (struct archive_read_disk *)_a;
483 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
484 ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_free");
486 if (a->archive.state != ARCHIVE_STATE_CLOSED)
487 r = _archive_read_close(&a->archive);
492 if (a->cleanup_gname != NULL && a->lookup_gname_data != NULL)
493 (a->cleanup_gname)(a->lookup_gname_data);
494 if (a->cleanup_uname != NULL && a->lookup_uname_data != NULL)
495 (a->cleanup_uname)(a->lookup_uname_data);
496 archive_string_free(&a->archive.error_string);
497 archive_entry_free(a->entry);
498 a->archive.magic = 0;
499 __archive_clean(&a->archive);
505 _archive_read_close(struct archive *_a)
507 struct archive_read_disk *a = (struct archive_read_disk *)_a;
509 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
510 ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_close");
512 if (a->archive.state != ARCHIVE_STATE_FATAL)
513 a->archive.state = ARCHIVE_STATE_CLOSED;
521 setup_symlink_mode(struct archive_read_disk *a, char symlink_mode,
524 a->symlink_mode = symlink_mode;
525 a->follow_symlinks = follow_symlinks;
526 if (a->tree != NULL) {
527 a->tree->initial_symlink_mode = a->symlink_mode;
528 a->tree->symlink_mode = a->symlink_mode;
533 archive_read_disk_set_symlink_logical(struct archive *_a)
535 struct archive_read_disk *a = (struct archive_read_disk *)_a;
536 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
537 ARCHIVE_STATE_ANY, "archive_read_disk_set_symlink_logical");
538 setup_symlink_mode(a, 'L', 1);
543 archive_read_disk_set_symlink_physical(struct archive *_a)
545 struct archive_read_disk *a = (struct archive_read_disk *)_a;
546 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
547 ARCHIVE_STATE_ANY, "archive_read_disk_set_symlink_physical");
548 setup_symlink_mode(a, 'P', 0);
553 archive_read_disk_set_symlink_hybrid(struct archive *_a)
555 struct archive_read_disk *a = (struct archive_read_disk *)_a;
556 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
557 ARCHIVE_STATE_ANY, "archive_read_disk_set_symlink_hybrid");
558 setup_symlink_mode(a, 'H', 1);/* Follow symlinks initially. */
563 archive_read_disk_set_atime_restored(struct archive *_a)
565 struct archive_read_disk *a = (struct archive_read_disk *)_a;
566 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
567 ARCHIVE_STATE_ANY, "archive_read_disk_restore_atime");
569 a->flags |= ARCHIVE_READDISK_RESTORE_ATIME;
571 a->tree->flags |= needsRestoreTimes;
574 /* Display warning and unset flag */
575 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
576 "Cannot restore access time on this system");
577 a->flags &= ~ARCHIVE_READDISK_RESTORE_ATIME;
578 return (ARCHIVE_WARN);
583 archive_read_disk_set_behavior(struct archive *_a, int flags)
585 struct archive_read_disk *a = (struct archive_read_disk *)_a;
588 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
589 ARCHIVE_STATE_ANY, "archive_read_disk_honor_nodump");
593 if (flags & ARCHIVE_READDISK_RESTORE_ATIME)
594 r = archive_read_disk_set_atime_restored(_a);
597 a->tree->flags &= ~needsRestoreTimes;
603 * Trivial implementations of gname/uname lookup functions.
604 * These are normally overridden by the client, but these stub
605 * versions ensure that we always have something that works.
608 trivial_lookup_gname(void *private_data, int64_t gid)
610 (void)private_data; /* UNUSED */
611 (void)gid; /* UNUSED */
616 trivial_lookup_uname(void *private_data, int64_t uid)
618 (void)private_data; /* UNUSED */
619 (void)uid; /* UNUSED */
624 * Allocate memory for the reading buffer adjusted to the filesystem
628 setup_suitable_read_buffer(struct archive_read_disk *a)
630 struct tree *t = a->tree;
631 struct filesystem *cf = t->current_filesystem;
635 if (cf->allocation_ptr == NULL) {
636 /* If we couldn't get a filesystem alignment,
637 * we use 4096 as default value but we won't use
638 * O_DIRECT to open() and openat() operations. */
639 long xfer_align = (cf->xfer_align == -1)?4096:cf->xfer_align;
641 if (cf->max_xfer_size != -1)
642 asize = cf->max_xfer_size + xfer_align;
644 long incr = cf->incr_xfer_size;
645 /* Some platform does not set a proper value to
648 incr = cf->min_xfer_size;
649 if (cf->min_xfer_size < 0) {
653 asize = cf->min_xfer_size;
655 /* Increase a buffer size up to 64K bytes in
656 * a proper increment size. */
657 while (asize < 1024*64)
659 /* Take a margin to adjust to the filesystem
663 cf->allocation_ptr = malloc(asize);
664 if (cf->allocation_ptr == NULL) {
665 archive_set_error(&a->archive, ENOMEM,
666 "Couldn't allocate memory");
667 a->archive.state = ARCHIVE_STATE_FATAL;
668 return (ARCHIVE_FATAL);
672 * Calculate proper address for the filesystem.
674 s = (uintptr_t)cf->allocation_ptr;
680 * Set a read buffer pointer in the proper alignment of
681 * the current filesystem.
683 cf->buff = cf->allocation_ptr + s;
684 cf->buff_size = asize - xfer_align;
690 _archive_read_data_block(struct archive *_a, const void **buff,
691 size_t *size, int64_t *offset)
693 struct archive_read_disk *a = (struct archive_read_disk *)_a;
694 struct tree *t = a->tree;
698 int empty_sparse_region = 0;
700 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA,
701 "archive_read_data_block");
703 if (t->entry_eof || t->entry_remaining_bytes <= 0) {
705 goto abort_read_data;
709 * Open the current file.
711 if (t->entry_fd < 0) {
712 int flags = O_RDONLY | O_BINARY | O_CLOEXEC;
715 * Eliminate or reduce cache effects if we can.
717 * Carefully consider this to be enabled.
719 #if defined(O_DIRECT) && 0/* Disabled for now */
720 if (t->current_filesystem->xfer_align != -1 &&
724 #if defined(O_NOATIME)
726 * Linux has O_NOATIME flag; use it if we need.
728 if ((t->flags & needsRestoreTimes) != 0 &&
729 t->restore_time.noatime == 0)
733 t->entry_fd = open_on_current_dir(t,
734 tree_current_access_path(t), flags);
735 __archive_ensure_cloexec_flag(t->entry_fd);
736 #if defined(O_NOATIME)
738 * When we did open the file with O_NOATIME flag,
739 * if successful, set 1 to t->restore_time.noatime
740 * not to restore an atime of the file later.
741 * if failed by EPERM, retry it without O_NOATIME flag.
743 if (flags & O_NOATIME) {
744 if (t->entry_fd >= 0)
745 t->restore_time.noatime = 1;
746 else if (errno == EPERM) {
753 if (t->entry_fd < 0) {
754 archive_set_error(&a->archive, errno,
755 "Couldn't open %s", tree_current_path(t));
757 tree_enter_initial_dir(t);
758 goto abort_read_data;
760 tree_enter_initial_dir(t);
764 * Allocate read buffer if not allocated.
766 if (t->current_filesystem->allocation_ptr == NULL) {
767 r = setup_suitable_read_buffer(a);
768 if (r != ARCHIVE_OK) {
769 a->archive.state = ARCHIVE_STATE_FATAL;
770 goto abort_read_data;
773 t->entry_buff = t->current_filesystem->buff;
774 t->entry_buff_size = t->current_filesystem->buff_size;
776 buffbytes = t->entry_buff_size;
777 if ((int64_t)buffbytes > t->current_sparse->length)
778 buffbytes = t->current_sparse->length;
780 if (t->current_sparse->length == 0)
781 empty_sparse_region = 1;
785 * TODO: Should we consider t->current_filesystem->xfer_align?
787 if (t->current_sparse->offset > t->entry_total) {
788 if (lseek(t->entry_fd,
789 (off_t)t->current_sparse->offset, SEEK_SET) < 0) {
790 archive_set_error(&a->archive, errno, "Seek error");
792 a->archive.state = ARCHIVE_STATE_FATAL;
793 goto abort_read_data;
795 bytes = t->current_sparse->offset - t->entry_total;
796 t->entry_remaining_bytes -= bytes;
797 t->entry_total += bytes;
801 * Read file contents.
804 bytes = read(t->entry_fd, t->entry_buff, buffbytes);
806 archive_set_error(&a->archive, errno, "Read error");
808 a->archive.state = ARCHIVE_STATE_FATAL;
809 goto abort_read_data;
814 * Return an EOF unless we've read a leading empty sparse region, which
815 * is used to represent fully-sparse files.
817 if (bytes == 0 && !empty_sparse_region) {
821 goto abort_read_data;
823 *buff = t->entry_buff;
825 *offset = t->entry_total;
826 t->entry_total += bytes;
827 t->entry_remaining_bytes -= bytes;
828 if (t->entry_remaining_bytes == 0) {
829 /* Close the current file descriptor */
830 close_and_restore_time(t->entry_fd, t, &t->restore_time);
834 t->current_sparse->offset += bytes;
835 t->current_sparse->length -= bytes;
836 if (t->current_sparse->length == 0 && !t->entry_eof)
843 *offset = t->entry_total;
844 if (t->entry_fd >= 0) {
845 /* Close the current file descriptor */
846 close_and_restore_time(t->entry_fd, t, &t->restore_time);
853 next_entry(struct archive_read_disk *a, struct tree *t,
854 struct archive_entry *entry)
856 const struct stat *st; /* info to use for this entry */
857 const struct stat *lst;/* lstat() information */
859 int delayed, delayed_errno, descend, r;
860 struct archive_string delayed_str;
862 delayed = ARCHIVE_OK;
864 archive_string_init(&delayed_str);
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 if (errno == ENOENT && t->depth > 0) {
894 delayed = ARCHIVE_WARN;
895 delayed_errno = errno;
896 if (delayed_str.length == 0) {
897 archive_string_sprintf(&delayed_str,
898 "%s", tree_current_path(t));
900 archive_string_sprintf(&delayed_str,
901 " %s", tree_current_path(t));
904 archive_set_error(&a->archive, errno,
906 tree_current_path(t));
907 tree_enter_initial_dir(t);
908 return (ARCHIVE_FAILED);
913 } while (lst == NULL);
916 if (a->flags & ARCHIVE_READDISK_MAC_COPYFILE) {
917 /* If we're using copyfile(), ignore "._XXX" files. */
918 const char *bname = strrchr(tree_current_path(t), '/');
920 bname = tree_current_path(t);
923 if (bname[0] == '.' && bname[1] == '_')
924 return (ARCHIVE_RETRY);
928 archive_entry_copy_pathname(entry, tree_current_path(t));
930 * Perform path matching.
933 r = archive_match_path_excluded(a->matching, entry);
935 archive_set_error(&(a->archive), errno,
936 "Failed : %s", archive_error_string(a->matching));
940 if (a->excluded_cb_func)
941 a->excluded_cb_func(&(a->archive),
942 a->excluded_cb_data, entry);
943 return (ARCHIVE_RETRY);
948 * Distinguish 'L'/'P'/'H' symlink following.
950 switch(t->symlink_mode) {
952 /* 'H': After the first item, rest like 'P'. */
953 t->symlink_mode = 'P';
954 /* 'H': First item (from command line) like 'L'. */
957 /* 'L': Do descend through a symlink to dir. */
958 descend = tree_current_is_dir(t);
959 /* 'L': Follow symlinks to files. */
960 a->symlink_mode = 'L';
961 a->follow_symlinks = 1;
962 /* 'L': Archive symlinks as targets, if we can. */
963 st = tree_current_stat(t);
964 if (st != NULL && !tree_target_is_same_as_parent(t, st))
966 /* If stat fails, we have a broken symlink;
967 * in that case, don't follow the link. */
970 /* 'P': Don't descend through a symlink to dir. */
971 descend = tree_current_is_physical_dir(t);
972 /* 'P': Don't follow symlinks to files. */
973 a->symlink_mode = 'P';
974 a->follow_symlinks = 0;
975 /* 'P': Archive symlinks as symlinks. */
980 if (update_current_filesystem(a, st->st_dev) != ARCHIVE_OK) {
981 a->archive.state = ARCHIVE_STATE_FATAL;
982 tree_enter_initial_dir(t);
983 return (ARCHIVE_FATAL);
985 if (t->initial_filesystem_id == -1)
986 t->initial_filesystem_id = t->current_filesystem_id;
987 if (a->flags & ARCHIVE_READDISK_NO_TRAVERSE_MOUNTS) {
988 if (t->initial_filesystem_id != t->current_filesystem_id)
991 t->descend = descend;
995 * If the file is marked with nodump flag, do not return this entry.
997 if (a->flags & ARCHIVE_READDISK_HONOR_NODUMP) {
998 #if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP)
999 if (st->st_flags & UF_NODUMP)
1000 return (ARCHIVE_RETRY);
1001 #elif (defined(FS_IOC_GETFLAGS) && defined(FS_NODUMP_FL) && \
1002 defined(HAVE_WORKING_FS_IOC_GETFLAGS)) || \
1003 (defined(EXT2_IOC_GETFLAGS) && defined(EXT2_NODUMP_FL) && \
1004 defined(HAVE_WORKING_EXT2_IOC_GETFLAGS))
1005 if (S_ISREG(st->st_mode) || S_ISDIR(st->st_mode)) {
1008 t->entry_fd = open_on_current_dir(t,
1009 tree_current_access_path(t),
1010 O_RDONLY | O_NONBLOCK | O_CLOEXEC);
1011 __archive_ensure_cloexec_flag(t->entry_fd);
1012 if (t->entry_fd >= 0) {
1013 r = ioctl(t->entry_fd,
1014 #ifdef FS_IOC_GETFLAGS
1021 if (r == 0 && (stflags & FS_NODUMP_FL) != 0)
1023 if (r == 0 && (stflags & EXT2_NODUMP_FL) != 0)
1025 return (ARCHIVE_RETRY);
1031 archive_entry_copy_stat(entry, st);
1033 /* Save the times to be restored. This must be in before
1034 * calling archive_read_disk_descend() or any chance of it,
1035 * especially, invoking a callback. */
1036 t->restore_time.mtime = archive_entry_mtime(entry);
1037 t->restore_time.mtime_nsec = archive_entry_mtime_nsec(entry);
1038 t->restore_time.atime = archive_entry_atime(entry);
1039 t->restore_time.atime_nsec = archive_entry_atime_nsec(entry);
1040 t->restore_time.filetype = archive_entry_filetype(entry);
1041 t->restore_time.noatime = t->current_filesystem->noatime;
1044 * Perform time matching.
1047 r = archive_match_time_excluded(a->matching, entry);
1049 archive_set_error(&(a->archive), errno,
1050 "Failed : %s", archive_error_string(a->matching));
1054 if (a->excluded_cb_func)
1055 a->excluded_cb_func(&(a->archive),
1056 a->excluded_cb_data, entry);
1057 return (ARCHIVE_RETRY);
1061 /* Lookup uname/gname */
1062 name = archive_read_disk_uname(&(a->archive), archive_entry_uid(entry));
1064 archive_entry_copy_uname(entry, name);
1065 name = archive_read_disk_gname(&(a->archive), archive_entry_gid(entry));
1067 archive_entry_copy_gname(entry, name);
1070 * Perform owner matching.
1073 r = archive_match_owner_excluded(a->matching, entry);
1075 archive_set_error(&(a->archive), errno,
1076 "Failed : %s", archive_error_string(a->matching));
1080 if (a->excluded_cb_func)
1081 a->excluded_cb_func(&(a->archive),
1082 a->excluded_cb_data, entry);
1083 return (ARCHIVE_RETRY);
1088 * Invoke a meta data filter callback.
1090 if (a->metadata_filter_func) {
1091 if (!a->metadata_filter_func(&(a->archive),
1092 a->metadata_filter_data, entry))
1093 return (ARCHIVE_RETRY);
1097 * Populate the archive_entry with metadata from the disk.
1099 archive_entry_copy_sourcepath(entry, tree_current_access_path(t));
1100 r = archive_read_disk_entry_from_file(&(a->archive), entry,
1103 if (r == ARCHIVE_OK) {
1105 if (r != ARCHIVE_OK) {
1106 archive_string_sprintf(&delayed_str, ": %s",
1107 "File removed before we read it");
1108 archive_set_error(&(a->archive), delayed_errno,
1109 "%s", delayed_str.s);
1112 if (!archive_string_empty(&delayed_str))
1113 archive_string_free(&delayed_str);
1119 _archive_read_next_header(struct archive *_a, struct archive_entry **entryp)
1122 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1124 ret = _archive_read_next_header2(_a, a->entry);
1130 _archive_read_next_header2(struct archive *_a, struct archive_entry *entry)
1132 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1136 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
1137 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
1138 "archive_read_next_header2");
1141 if (t->entry_fd >= 0) {
1142 close_and_restore_time(t->entry_fd, t, &t->restore_time);
1146 archive_entry_clear(entry);
1149 r = next_entry(a, t, entry);
1150 if (t->entry_fd >= 0) {
1155 if (r == ARCHIVE_RETRY) {
1156 archive_entry_clear(entry);
1162 /* Return to the initial directory. */
1163 tree_enter_initial_dir(t);
1166 * EOF and FATAL are persistent at this layer. By
1167 * modifying the state, we guarantee that future calls to
1168 * read a header or read data will fail.
1172 a->archive.state = ARCHIVE_STATE_EOF;
1176 /* Overwrite the sourcepath based on the initial directory. */
1177 archive_entry_copy_sourcepath(entry, tree_current_path(t));
1179 if (archive_entry_filetype(entry) == AE_IFREG) {
1180 t->nlink = archive_entry_nlink(entry);
1181 t->entry_remaining_bytes = archive_entry_size(entry);
1182 t->entry_eof = (t->entry_remaining_bytes == 0)? 1: 0;
1183 if (!t->entry_eof &&
1184 setup_sparse(a, entry) != ARCHIVE_OK)
1185 return (ARCHIVE_FATAL);
1187 t->entry_remaining_bytes = 0;
1190 a->archive.state = ARCHIVE_STATE_DATA;
1195 a->archive.state = ARCHIVE_STATE_FATAL;
1199 __archive_reset_read_data(&a->archive);
1204 setup_sparse(struct archive_read_disk *a, struct archive_entry *entry)
1206 struct tree *t = a->tree;
1207 int64_t length, offset;
1210 t->sparse_count = archive_entry_sparse_reset(entry);
1211 if (t->sparse_count+1 > t->sparse_list_size) {
1212 free(t->sparse_list);
1213 t->sparse_list_size = t->sparse_count + 1;
1214 t->sparse_list = malloc(sizeof(t->sparse_list[0]) *
1215 t->sparse_list_size);
1216 if (t->sparse_list == NULL) {
1217 t->sparse_list_size = 0;
1218 archive_set_error(&a->archive, ENOMEM,
1219 "Can't allocate data");
1220 a->archive.state = ARCHIVE_STATE_FATAL;
1221 return (ARCHIVE_FATAL);
1224 for (i = 0; i < t->sparse_count; i++) {
1225 archive_entry_sparse_next(entry, &offset, &length);
1226 t->sparse_list[i].offset = offset;
1227 t->sparse_list[i].length = length;
1230 t->sparse_list[i].offset = 0;
1231 t->sparse_list[i].length = archive_entry_size(entry);
1233 t->sparse_list[i].offset = archive_entry_size(entry);
1234 t->sparse_list[i].length = 0;
1236 t->current_sparse = t->sparse_list;
1238 return (ARCHIVE_OK);
1242 archive_read_disk_set_matching(struct archive *_a, struct archive *_ma,
1243 void (*_excluded_func)(struct archive *, void *, struct archive_entry *),
1246 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1247 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
1248 ARCHIVE_STATE_ANY, "archive_read_disk_set_matching");
1250 a->excluded_cb_func = _excluded_func;
1251 a->excluded_cb_data = _client_data;
1252 return (ARCHIVE_OK);
1256 archive_read_disk_set_metadata_filter_callback(struct archive *_a,
1257 int (*_metadata_filter_func)(struct archive *, void *,
1258 struct archive_entry *), void *_client_data)
1260 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1262 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_ANY,
1263 "archive_read_disk_set_metadata_filter_callback");
1265 a->metadata_filter_func = _metadata_filter_func;
1266 a->metadata_filter_data = _client_data;
1267 return (ARCHIVE_OK);
1271 archive_read_disk_can_descend(struct archive *_a)
1273 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1274 struct tree *t = a->tree;
1276 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
1277 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
1278 "archive_read_disk_can_descend");
1280 return (t->visit_type == TREE_REGULAR && t->descend);
1284 * Called by the client to mark the directory just returned from
1285 * tree_next() as needing to be visited.
1288 archive_read_disk_descend(struct archive *_a)
1290 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1291 struct tree *t = a->tree;
1293 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
1294 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
1295 "archive_read_disk_descend");
1297 if (t->visit_type != TREE_REGULAR || !t->descend)
1298 return (ARCHIVE_OK);
1301 * We must not treat the initial specified path as a physical dir,
1302 * because if we do then we will try and ascend out of it by opening
1303 * ".." which is (a) wrong and (b) causes spurious permissions errors
1304 * if ".." is not readable by us. Instead, treat it as if it were a
1305 * symlink. (This uses an extra fd, but it can only happen once at the
1306 * top level of a traverse.) But we can't necessarily assume t->st is
1307 * valid here (though t->lst is), which complicates the logic a
1310 if (tree_current_is_physical_dir(t)) {
1311 tree_push(t, t->basename, t->current_filesystem_id,
1312 t->lst.st_dev, t->lst.st_ino, &t->restore_time);
1313 if (t->stack->parent->parent != NULL)
1314 t->stack->flags |= isDir;
1316 t->stack->flags |= isDirLink;
1317 } else if (tree_current_is_dir(t)) {
1318 tree_push(t, t->basename, t->current_filesystem_id,
1319 t->st.st_dev, t->st.st_ino, &t->restore_time);
1320 t->stack->flags |= isDirLink;
1323 return (ARCHIVE_OK);
1327 archive_read_disk_open(struct archive *_a, const char *pathname)
1329 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1331 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
1332 ARCHIVE_STATE_NEW | ARCHIVE_STATE_CLOSED,
1333 "archive_read_disk_open");
1334 archive_clear_error(&a->archive);
1336 return (_archive_read_disk_open(_a, pathname));
1340 archive_read_disk_open_w(struct archive *_a, const wchar_t *pathname)
1342 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1343 struct archive_string path;
1346 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
1347 ARCHIVE_STATE_NEW | ARCHIVE_STATE_CLOSED,
1348 "archive_read_disk_open_w");
1349 archive_clear_error(&a->archive);
1351 /* Make a char string from a wchar_t string. */
1352 archive_string_init(&path);
1353 if (archive_string_append_from_wcs(&path, pathname,
1354 wcslen(pathname)) != 0) {
1355 if (errno == ENOMEM)
1356 archive_set_error(&a->archive, ENOMEM,
1357 "Can't allocate memory");
1359 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1360 "Can't convert a path to a char string");
1361 a->archive.state = ARCHIVE_STATE_FATAL;
1362 ret = ARCHIVE_FATAL;
1364 ret = _archive_read_disk_open(_a, path.s);
1366 archive_string_free(&path);
1371 _archive_read_disk_open(struct archive *_a, const char *pathname)
1373 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1375 if (a->tree != NULL)
1376 a->tree = tree_reopen(a->tree, pathname,
1377 a->flags & ARCHIVE_READDISK_RESTORE_ATIME);
1379 a->tree = tree_open(pathname, a->symlink_mode,
1380 a->flags & ARCHIVE_READDISK_RESTORE_ATIME);
1381 if (a->tree == NULL) {
1382 archive_set_error(&a->archive, ENOMEM,
1383 "Can't allocate tar data");
1384 a->archive.state = ARCHIVE_STATE_FATAL;
1385 return (ARCHIVE_FATAL);
1387 a->archive.state = ARCHIVE_STATE_HEADER;
1389 return (ARCHIVE_OK);
1393 * Return a current filesystem ID which is index of the filesystem entry
1394 * you've visited through archive_read_disk.
1397 archive_read_disk_current_filesystem(struct archive *_a)
1399 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1401 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA,
1402 "archive_read_disk_current_filesystem");
1404 return (a->tree->current_filesystem_id);
1408 update_current_filesystem(struct archive_read_disk *a, int64_t dev)
1410 struct tree *t = a->tree;
1413 if (t->current_filesystem != NULL &&
1414 t->current_filesystem->dev == dev)
1415 return (ARCHIVE_OK);
1417 for (i = 0; i < t->max_filesystem_id; i++) {
1418 if (t->filesystem_table[i].dev == dev) {
1419 /* There is the filesystem ID we've already generated. */
1420 t->current_filesystem_id = i;
1421 t->current_filesystem = &(t->filesystem_table[i]);
1422 return (ARCHIVE_OK);
1427 * This is the new filesystem which we have to generate a new ID for.
1429 fid = t->max_filesystem_id++;
1430 if (t->max_filesystem_id > t->allocated_filesystem) {
1434 s = t->max_filesystem_id * 2;
1435 p = realloc(t->filesystem_table,
1436 s * sizeof(*t->filesystem_table));
1438 archive_set_error(&a->archive, ENOMEM,
1439 "Can't allocate tar data");
1440 return (ARCHIVE_FATAL);
1442 t->filesystem_table = (struct filesystem *)p;
1443 t->allocated_filesystem = s;
1445 t->current_filesystem_id = fid;
1446 t->current_filesystem = &(t->filesystem_table[fid]);
1447 t->current_filesystem->dev = dev;
1448 t->current_filesystem->allocation_ptr = NULL;
1449 t->current_filesystem->buff = NULL;
1451 /* Setup the current filesystem properties which depend on
1452 * platform specific. */
1453 return (setup_current_filesystem(a));
1457 * Returns 1 if current filesystem is generated filesystem, 0 if it is not
1458 * or -1 if it is unknown.
1461 archive_read_disk_current_filesystem_is_synthetic(struct archive *_a)
1463 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1465 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA,
1466 "archive_read_disk_current_filesystem");
1468 return (a->tree->current_filesystem->synthetic);
1472 * Returns 1 if current filesystem is remote filesystem, 0 if it is not
1473 * or -1 if it is unknown.
1476 archive_read_disk_current_filesystem_is_remote(struct archive *_a)
1478 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1480 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA,
1481 "archive_read_disk_current_filesystem");
1483 return (a->tree->current_filesystem->remote);
1486 #if defined(_PC_REC_INCR_XFER_SIZE) && defined(_PC_REC_MAX_XFER_SIZE) &&\
1487 defined(_PC_REC_MIN_XFER_SIZE) && defined(_PC_REC_XFER_ALIGN)
1489 get_xfer_size(struct tree *t, int fd, const char *path)
1491 t->current_filesystem->xfer_align = -1;
1494 t->current_filesystem->incr_xfer_size =
1495 fpathconf(fd, _PC_REC_INCR_XFER_SIZE);
1496 t->current_filesystem->max_xfer_size =
1497 fpathconf(fd, _PC_REC_MAX_XFER_SIZE);
1498 t->current_filesystem->min_xfer_size =
1499 fpathconf(fd, _PC_REC_MIN_XFER_SIZE);
1500 t->current_filesystem->xfer_align =
1501 fpathconf(fd, _PC_REC_XFER_ALIGN);
1502 } else if (path != NULL) {
1503 t->current_filesystem->incr_xfer_size =
1504 pathconf(path, _PC_REC_INCR_XFER_SIZE);
1505 t->current_filesystem->max_xfer_size =
1506 pathconf(path, _PC_REC_MAX_XFER_SIZE);
1507 t->current_filesystem->min_xfer_size =
1508 pathconf(path, _PC_REC_MIN_XFER_SIZE);
1509 t->current_filesystem->xfer_align =
1510 pathconf(path, _PC_REC_XFER_ALIGN);
1512 /* At least we need an alignment size. */
1513 if (t->current_filesystem->xfer_align == -1)
1514 return ((errno == EINVAL)?1:-1);
1520 get_xfer_size(struct tree *t, int fd, const char *path)
1522 (void)t; /* UNUSED */
1523 (void)fd; /* UNUSED */
1524 (void)path; /* UNUSED */
1525 return (1);/* Not supported */
1529 #if defined(HAVE_STATFS) && defined(HAVE_FSTATFS) && defined(MNT_LOCAL) \
1530 && !defined(ST_LOCAL)
1533 * Gather current filesystem properties on FreeBSD, OpenBSD and Mac OS X.
1536 setup_current_filesystem(struct archive_read_disk *a)
1538 struct tree *t = a->tree;
1540 #if defined(HAVE_GETVFSBYNAME) && defined(VFCF_SYNTHETIC)
1541 /* TODO: configure should set GETVFSBYNAME_ARG_TYPE to make
1542 * this accurate; some platforms have both and we need the one that's
1543 * used by getvfsbyname()
1545 * Then the following would become:
1546 * #if defined(GETVFSBYNAME_ARG_TYPE)
1547 * GETVFSBYNAME_ARG_TYPE vfc;
1550 # if defined(HAVE_STRUCT_XVFSCONF)
1551 struct xvfsconf vfc;
1557 #if !defined(HAVE_STRUCT_STATFS_F_NAMEMAX)
1561 t->current_filesystem->synthetic = -1;
1562 t->current_filesystem->remote = -1;
1563 if (tree_current_is_symblic_link_target(t)) {
1564 #if defined(HAVE_OPENAT)
1566 * Get file system statistics on any directory
1569 int fd = openat(tree_current_dir_fd(t),
1570 tree_current_access_path(t), O_RDONLY | O_CLOEXEC);
1571 __archive_ensure_cloexec_flag(fd);
1573 archive_set_error(&a->archive, errno,
1575 return (ARCHIVE_FAILED);
1577 r = fstatfs(fd, &sfs);
1579 xr = get_xfer_size(t, fd, NULL);
1582 if (tree_enter_working_dir(t) != 0) {
1583 archive_set_error(&a->archive, errno, "fchdir failed");
1584 return (ARCHIVE_FAILED);
1586 r = statfs(tree_current_access_path(t), &sfs);
1588 xr = get_xfer_size(t, -1, tree_current_access_path(t));
1591 r = fstatfs(tree_current_dir_fd(t), &sfs);
1593 xr = get_xfer_size(t, tree_current_dir_fd(t), NULL);
1595 if (r == -1 || xr == -1) {
1596 archive_set_error(&a->archive, errno, "statfs failed");
1597 return (ARCHIVE_FAILED);
1598 } else if (xr == 1) {
1599 /* pathconf(_PC_REX_*) operations are not supported. */
1600 t->current_filesystem->xfer_align = sfs.f_bsize;
1601 t->current_filesystem->max_xfer_size = -1;
1602 t->current_filesystem->min_xfer_size = sfs.f_iosize;
1603 t->current_filesystem->incr_xfer_size = sfs.f_iosize;
1605 if (sfs.f_flags & MNT_LOCAL)
1606 t->current_filesystem->remote = 0;
1608 t->current_filesystem->remote = 1;
1610 #if defined(HAVE_GETVFSBYNAME) && defined(VFCF_SYNTHETIC)
1611 r = getvfsbyname(sfs.f_fstypename, &vfc);
1613 archive_set_error(&a->archive, errno, "getvfsbyname failed");
1614 return (ARCHIVE_FAILED);
1616 if (vfc.vfc_flags & VFCF_SYNTHETIC)
1617 t->current_filesystem->synthetic = 1;
1619 t->current_filesystem->synthetic = 0;
1622 #if defined(MNT_NOATIME)
1623 if (sfs.f_flags & MNT_NOATIME)
1624 t->current_filesystem->noatime = 1;
1627 t->current_filesystem->noatime = 0;
1629 #if defined(USE_READDIR_R)
1630 /* Set maximum filename length. */
1631 #if defined(HAVE_STRUCT_STATFS_F_NAMEMAX)
1632 t->current_filesystem->name_max = sfs.f_namemax;
1634 # if defined(_PC_NAME_MAX)
1635 /* Mac OS X does not have f_namemax in struct statfs. */
1636 if (tree_current_is_symblic_link_target(t)) {
1637 if (tree_enter_working_dir(t) != 0) {
1638 archive_set_error(&a->archive, errno, "fchdir failed");
1639 return (ARCHIVE_FAILED);
1641 nm = pathconf(tree_current_access_path(t), _PC_NAME_MAX);
1643 nm = fpathconf(tree_current_dir_fd(t), _PC_NAME_MAX);
1648 t->current_filesystem->name_max = NAME_MAX;
1650 t->current_filesystem->name_max = nm;
1652 #endif /* USE_READDIR_R */
1653 return (ARCHIVE_OK);
1656 #elif (defined(HAVE_STATVFS) || defined(HAVE_FSTATVFS)) && defined(ST_LOCAL)
1659 * Gather current filesystem properties on NetBSD
1662 setup_current_filesystem(struct archive_read_disk *a)
1664 struct tree *t = a->tree;
1668 t->current_filesystem->synthetic = -1;
1669 if (tree_enter_working_dir(t) != 0) {
1670 archive_set_error(&a->archive, errno, "fchdir failed");
1671 return (ARCHIVE_FAILED);
1673 if (tree_current_is_symblic_link_target(t)) {
1674 r = statvfs(tree_current_access_path(t), &sfs);
1676 xr = get_xfer_size(t, -1, tree_current_access_path(t));
1678 #ifdef HAVE_FSTATVFS
1679 r = fstatvfs(tree_current_dir_fd(t), &sfs);
1681 xr = get_xfer_size(t, tree_current_dir_fd(t), NULL);
1683 r = statvfs(".", &sfs);
1685 xr = get_xfer_size(t, -1, ".");
1688 if (r == -1 || xr == -1) {
1689 t->current_filesystem->remote = -1;
1690 archive_set_error(&a->archive, errno, "statvfs failed");
1691 return (ARCHIVE_FAILED);
1692 } else if (xr == 1) {
1693 /* Usually come here unless NetBSD supports _PC_REC_XFER_ALIGN
1694 * for pathconf() function. */
1695 t->current_filesystem->xfer_align = sfs.f_frsize;
1696 t->current_filesystem->max_xfer_size = -1;
1697 #if defined(HAVE_STRUCT_STATVFS_F_IOSIZE)
1698 t->current_filesystem->min_xfer_size = sfs.f_iosize;
1699 t->current_filesystem->incr_xfer_size = sfs.f_iosize;
1701 t->current_filesystem->min_xfer_size = sfs.f_bsize;
1702 t->current_filesystem->incr_xfer_size = sfs.f_bsize;
1705 if (sfs.f_flag & ST_LOCAL)
1706 t->current_filesystem->remote = 0;
1708 t->current_filesystem->remote = 1;
1710 #if defined(ST_NOATIME)
1711 if (sfs.f_flag & ST_NOATIME)
1712 t->current_filesystem->noatime = 1;
1715 t->current_filesystem->noatime = 0;
1717 /* Set maximum filename length. */
1718 t->current_filesystem->name_max = sfs.f_namemax;
1719 return (ARCHIVE_OK);
1722 #elif defined(HAVE_SYS_STATFS_H) && defined(HAVE_LINUX_MAGIC_H) &&\
1723 defined(HAVE_STATFS) && defined(HAVE_FSTATFS)
1725 * Note: statfs is deprecated since LSB 3.2
1728 #ifndef CIFS_SUPER_MAGIC
1729 #define CIFS_SUPER_MAGIC 0xFF534D42
1731 #ifndef DEVFS_SUPER_MAGIC
1732 #define DEVFS_SUPER_MAGIC 0x1373
1736 * Gather current filesystem properties on Linux
1739 setup_current_filesystem(struct archive_read_disk *a)
1741 struct tree *t = a->tree;
1743 #if defined(HAVE_STATVFS)
1744 struct statvfs svfs;
1746 int r, vr = 0, xr = 0;
1748 if (tree_current_is_symblic_link_target(t)) {
1749 #if defined(HAVE_OPENAT)
1751 * Get file system statistics on any directory
1754 int fd = openat(tree_current_dir_fd(t),
1755 tree_current_access_path(t), O_RDONLY | O_CLOEXEC);
1756 __archive_ensure_cloexec_flag(fd);
1758 archive_set_error(&a->archive, errno,
1760 return (ARCHIVE_FAILED);
1762 #if defined(HAVE_FSTATVFS)
1763 vr = fstatvfs(fd, &svfs);/* for f_flag, mount flags */
1765 r = fstatfs(fd, &sfs);
1767 xr = get_xfer_size(t, fd, NULL);
1770 if (tree_enter_working_dir(t) != 0) {
1771 archive_set_error(&a->archive, errno, "fchdir failed");
1772 return (ARCHIVE_FAILED);
1774 #if defined(HAVE_STATVFS)
1775 vr = statvfs(tree_current_access_path(t), &svfs);
1777 r = statfs(tree_current_access_path(t), &sfs);
1779 xr = get_xfer_size(t, -1, tree_current_access_path(t));
1783 #if defined(HAVE_FSTATVFS)
1784 vr = fstatvfs(tree_current_dir_fd(t), &svfs);
1786 r = fstatfs(tree_current_dir_fd(t), &sfs);
1788 xr = get_xfer_size(t, tree_current_dir_fd(t), NULL);
1790 if (tree_enter_working_dir(t) != 0) {
1791 archive_set_error(&a->archive, errno, "fchdir failed");
1792 return (ARCHIVE_FAILED);
1794 #if defined(HAVE_STATVFS)
1795 vr = statvfs(".", &svfs);
1797 r = statfs(".", &sfs);
1799 xr = get_xfer_size(t, -1, ".");
1802 if (r == -1 || xr == -1 || vr == -1) {
1803 t->current_filesystem->synthetic = -1;
1804 t->current_filesystem->remote = -1;
1805 archive_set_error(&a->archive, errno, "statfs failed");
1806 return (ARCHIVE_FAILED);
1807 } else if (xr == 1) {
1808 /* pathconf(_PC_REX_*) operations are not supported. */
1809 #if defined(HAVE_STATVFS)
1810 t->current_filesystem->xfer_align = svfs.f_frsize;
1811 t->current_filesystem->max_xfer_size = -1;
1812 t->current_filesystem->min_xfer_size = svfs.f_bsize;
1813 t->current_filesystem->incr_xfer_size = svfs.f_bsize;
1815 t->current_filesystem->xfer_align = sfs.f_frsize;
1816 t->current_filesystem->max_xfer_size = -1;
1817 t->current_filesystem->min_xfer_size = sfs.f_bsize;
1818 t->current_filesystem->incr_xfer_size = sfs.f_bsize;
1821 switch (sfs.f_type) {
1822 case AFS_SUPER_MAGIC:
1823 case CIFS_SUPER_MAGIC:
1824 case CODA_SUPER_MAGIC:
1825 case NCP_SUPER_MAGIC:/* NetWare */
1826 case NFS_SUPER_MAGIC:
1827 case SMB_SUPER_MAGIC:
1828 t->current_filesystem->remote = 1;
1829 t->current_filesystem->synthetic = 0;
1831 case DEVFS_SUPER_MAGIC:
1832 case PROC_SUPER_MAGIC:
1833 case USBDEVICE_SUPER_MAGIC:
1834 t->current_filesystem->remote = 0;
1835 t->current_filesystem->synthetic = 1;
1838 t->current_filesystem->remote = 0;
1839 t->current_filesystem->synthetic = 0;
1843 #if defined(ST_NOATIME)
1844 #if defined(HAVE_STATVFS)
1845 if (svfs.f_flag & ST_NOATIME)
1847 if (sfs.f_flag & ST_NOATIME)
1849 t->current_filesystem->noatime = 1;
1852 t->current_filesystem->noatime = 0;
1854 #if defined(USE_READDIR_R)
1855 /* Set maximum filename length. */
1856 t->current_filesystem->name_max = sfs.f_namelen;
1858 return (ARCHIVE_OK);
1861 #elif defined(HAVE_SYS_STATVFS_H) &&\
1862 (defined(HAVE_STATVFS) || defined(HAVE_FSTATVFS))
1865 * Gather current filesystem properties on other posix platform.
1868 setup_current_filesystem(struct archive_read_disk *a)
1870 struct tree *t = a->tree;
1874 t->current_filesystem->synthetic = -1;/* Not supported */
1875 t->current_filesystem->remote = -1;/* Not supported */
1876 if (tree_current_is_symblic_link_target(t)) {
1877 #if defined(HAVE_OPENAT)
1879 * Get file system statistics on any directory
1882 int fd = openat(tree_current_dir_fd(t),
1883 tree_current_access_path(t), O_RDONLY | O_CLOEXEC);
1884 __archive_ensure_cloexec_flag(fd);
1886 archive_set_error(&a->archive, errno,
1888 return (ARCHIVE_FAILED);
1890 r = fstatvfs(fd, &sfs);
1892 xr = get_xfer_size(t, fd, NULL);
1895 if (tree_enter_working_dir(t) != 0) {
1896 archive_set_error(&a->archive, errno, "fchdir failed");
1897 return (ARCHIVE_FAILED);
1899 r = statvfs(tree_current_access_path(t), &sfs);
1901 xr = get_xfer_size(t, -1, tree_current_access_path(t));
1904 #ifdef HAVE_FSTATVFS
1905 r = fstatvfs(tree_current_dir_fd(t), &sfs);
1907 xr = get_xfer_size(t, tree_current_dir_fd(t), NULL);
1909 if (tree_enter_working_dir(t) != 0) {
1910 archive_set_error(&a->archive, errno, "fchdir failed");
1911 return (ARCHIVE_FAILED);
1913 r = statvfs(".", &sfs);
1915 xr = get_xfer_size(t, -1, ".");
1918 if (r == -1 || xr == -1) {
1919 t->current_filesystem->synthetic = -1;
1920 t->current_filesystem->remote = -1;
1921 archive_set_error(&a->archive, errno, "statvfs failed");
1922 return (ARCHIVE_FAILED);
1923 } else if (xr == 1) {
1924 /* pathconf(_PC_REX_*) operations are not supported. */
1925 t->current_filesystem->xfer_align = sfs.f_frsize;
1926 t->current_filesystem->max_xfer_size = -1;
1927 t->current_filesystem->min_xfer_size = sfs.f_bsize;
1928 t->current_filesystem->incr_xfer_size = sfs.f_bsize;
1931 #if defined(ST_NOATIME)
1932 if (sfs.f_flag & ST_NOATIME)
1933 t->current_filesystem->noatime = 1;
1936 t->current_filesystem->noatime = 0;
1938 #if defined(USE_READDIR_R)
1939 /* Set maximum filename length. */
1940 t->current_filesystem->name_max = sfs.f_namemax;
1942 return (ARCHIVE_OK);
1948 * Generic: Gather current filesystem properties.
1949 * TODO: Is this generic function really needed?
1952 setup_current_filesystem(struct archive_read_disk *a)
1954 struct tree *t = a->tree;
1955 #if defined(_PC_NAME_MAX) && defined(USE_READDIR_R)
1958 t->current_filesystem->synthetic = -1;/* Not supported */
1959 t->current_filesystem->remote = -1;/* Not supported */
1960 t->current_filesystem->noatime = 0;
1961 (void)get_xfer_size(t, -1, ".");/* Dummy call to avoid build error. */
1962 t->current_filesystem->xfer_align = -1;/* Unknown */
1963 t->current_filesystem->max_xfer_size = -1;
1964 t->current_filesystem->min_xfer_size = -1;
1965 t->current_filesystem->incr_xfer_size = -1;
1967 #if defined(USE_READDIR_R)
1968 /* Set maximum filename length. */
1969 # if defined(_PC_NAME_MAX)
1970 if (tree_current_is_symblic_link_target(t)) {
1971 if (tree_enter_working_dir(t) != 0) {
1972 archive_set_error(&a->archive, errno, "fchdir failed");
1973 return (ARCHIVE_FAILED);
1975 nm = pathconf(tree_current_access_path(t), _PC_NAME_MAX);
1977 nm = fpathconf(tree_current_dir_fd(t), _PC_NAME_MAX);
1979 # endif /* _PC_NAME_MAX */
1981 * Some systems (HP-UX or others?) incorrectly defined
1982 * NAME_MAX macro to be a smaller value.
1984 # if defined(NAME_MAX) && NAME_MAX >= 255
1985 t->current_filesystem->name_max = NAME_MAX;
1987 /* No way to get a trusted value of maximum filename
1989 t->current_filesystem->name_max = PATH_MAX;
1990 # endif /* NAME_MAX */
1991 # if defined(_PC_NAME_MAX)
1993 t->current_filesystem->name_max = nm;
1994 # endif /* _PC_NAME_MAX */
1995 #endif /* USE_READDIR_R */
1996 return (ARCHIVE_OK);
2002 close_and_restore_time(int fd, struct tree *t, struct restore_time *rt)
2005 (void)t; /* UNUSED */
2006 (void)rt; /* UNUSED */
2009 #if defined(HAVE_FUTIMENS) && !defined(__CYGWIN__)
2010 struct timespec timespecs[2];
2012 struct timeval times[2];
2014 if ((t->flags & needsRestoreTimes) == 0 || rt->noatime) {
2021 #if defined(HAVE_FUTIMENS) && !defined(__CYGWIN__)
2022 timespecs[1].tv_sec = rt->mtime;
2023 timespecs[1].tv_nsec = rt->mtime_nsec;
2025 timespecs[0].tv_sec = rt->atime;
2026 timespecs[0].tv_nsec = rt->atime_nsec;
2027 /* futimens() is defined in POSIX.1-2008. */
2028 if (futimens(fd, timespecs) == 0)
2032 times[1].tv_sec = rt->mtime;
2033 times[1].tv_usec = rt->mtime_nsec / 1000;
2035 times[0].tv_sec = rt->atime;
2036 times[0].tv_usec = rt->atime_nsec / 1000;
2038 #if !defined(HAVE_FUTIMENS) && defined(HAVE_FUTIMES) && !defined(__CYGWIN__)
2039 if (futimes(fd, times) == 0)
2043 #if defined(HAVE_FUTIMESAT)
2044 if (futimesat(tree_current_dir_fd(t), rt->name, times) == 0)
2048 if (lutimes(rt->name, times) != 0)
2050 if (AE_IFLNK != rt->filetype && utimes(rt->name, times) != 0)
2058 open_on_current_dir(struct tree *t, const char *path, int flags)
2061 return (openat(tree_current_dir_fd(t), path, flags));
2063 if (tree_enter_working_dir(t) != 0)
2065 return (open(path, flags));
2073 #ifdef F_DUPFD_CLOEXEC
2074 static volatile int can_dupfd_cloexec = 1;
2076 if (can_dupfd_cloexec) {
2077 new_fd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
2080 /* Linux 2.6.18 - 2.6.23 declare F_DUPFD_CLOEXEC,
2081 * but it cannot be used. So we have to try dup(). */
2082 /* We won't try F_DUPFD_CLOEXEC. */
2083 can_dupfd_cloexec = 0;
2085 #endif /* F_DUPFD_CLOEXEC */
2087 __archive_ensure_cloexec_flag(new_fd);
2092 * Add a directory path to the current stack.
2095 tree_push(struct tree *t, const char *path, int filesystem_id,
2096 int64_t dev, int64_t ino, struct restore_time *rt)
2098 struct tree_entry *te;
2100 te = calloc(1, sizeof(*te));
2101 te->next = t->stack;
2102 te->parent = t->current;
2104 te->depth = te->parent->depth + 1;
2106 archive_string_init(&te->name);
2107 te->symlink_parent_fd = -1;
2108 archive_strcpy(&te->name, path);
2109 te->flags = needsDescent | needsOpen | needsAscent;
2110 te->filesystem_id = filesystem_id;
2113 te->dirname_length = t->dirname_length;
2114 te->restore_time.name = te->name.s;
2116 te->restore_time.mtime = rt->mtime;
2117 te->restore_time.mtime_nsec = rt->mtime_nsec;
2118 te->restore_time.atime = rt->atime;
2119 te->restore_time.atime_nsec = rt->atime_nsec;
2120 te->restore_time.filetype = rt->filetype;
2121 te->restore_time.noatime = rt->noatime;
2126 * Append a name to the current dir path.
2129 tree_append(struct tree *t, const char *name, size_t name_length)
2133 t->path.s[t->dirname_length] = '\0';
2134 t->path.length = t->dirname_length;
2135 /* Strip trailing '/' from name, unless entire name is "/". */
2136 while (name_length > 1 && name[name_length - 1] == '/')
2139 /* Resize pathname buffer as needed. */
2140 size_needed = name_length + t->dirname_length + 2;
2141 archive_string_ensure(&t->path, size_needed);
2142 /* Add a separating '/' if it's needed. */
2143 if (t->dirname_length > 0 && t->path.s[archive_strlen(&t->path)-1] != '/')
2144 archive_strappend_char(&t->path, '/');
2145 t->basename = t->path.s + archive_strlen(&t->path);
2146 archive_strncat(&t->path, name, name_length);
2147 t->restore_time.name = t->basename;
2151 * Open a directory tree for traversal.
2153 static struct tree *
2154 tree_open(const char *path, int symlink_mode, int restore_time)
2158 if ((t = calloc(1, sizeof(*t))) == NULL)
2160 archive_string_init(&t->path);
2161 archive_string_ensure(&t->path, 31);
2162 t->initial_symlink_mode = symlink_mode;
2163 return (tree_reopen(t, path, restore_time));
2166 static struct tree *
2167 tree_reopen(struct tree *t, const char *path, int restore_time)
2171 const int o_flag = O_PATH;
2172 #elif defined(O_SEARCH)
2174 const int o_flag = O_SEARCH;
2175 #elif defined(O_EXEC)
2177 const int o_flag = O_EXEC;
2180 t->flags = (restore_time != 0)?needsRestoreTimes:0;
2181 t->flags |= onInitialDir;
2184 t->dirname_length = 0;
2188 t->d = INVALID_DIR_HANDLE;
2189 t->symlink_mode = t->initial_symlink_mode;
2190 archive_string_empty(&t->path);
2193 t->entry_remaining_bytes = 0;
2194 t->initial_filesystem_id = -1;
2196 /* First item is set up a lot like a symlink traversal. */
2197 tree_push(t, path, 0, 0, 0, NULL);
2198 t->stack->flags = needsFirstVisit;
2199 t->maxOpenCount = t->openCount = 1;
2200 t->initial_dir_fd = open(".", O_RDONLY | O_CLOEXEC);
2201 #if defined(O_PATH) || defined(O_SEARCH) || defined(O_EXEC)
2203 * Most likely reason to fail opening "." is that it's not readable,
2204 * so try again for execute. The consequences of not opening this are
2205 * unhelpful and unnecessary errors later.
2207 if (t->initial_dir_fd < 0)
2208 t->initial_dir_fd = open(".", o_flag | O_CLOEXEC);
2210 __archive_ensure_cloexec_flag(t->initial_dir_fd);
2211 t->working_dir_fd = tree_dup(t->initial_dir_fd);
2216 tree_descent(struct tree *t)
2218 int flag, new_fd, r = 0;
2220 t->dirname_length = archive_strlen(&t->path);
2221 flag = O_RDONLY | O_CLOEXEC;
2222 #if defined(O_DIRECTORY)
2223 flag |= O_DIRECTORY;
2225 new_fd = open_on_current_dir(t, t->stack->name.s, flag);
2226 __archive_ensure_cloexec_flag(new_fd);
2228 t->tree_errno = errno;
2232 /* If it is a link, set up fd for the ascent. */
2233 if (t->stack->flags & isDirLink) {
2234 t->stack->symlink_parent_fd = t->working_dir_fd;
2236 if (t->openCount > t->maxOpenCount)
2237 t->maxOpenCount = t->openCount;
2239 close(t->working_dir_fd);
2240 /* Renew the current working directory. */
2241 t->working_dir_fd = new_fd;
2242 t->flags &= ~onWorkingDir;
2248 * We've finished a directory; ascend back to the parent.
2251 tree_ascend(struct tree *t)
2253 struct tree_entry *te;
2254 int new_fd, r = 0, prev_dir_fd;
2257 prev_dir_fd = t->working_dir_fd;
2258 if (te->flags & isDirLink)
2259 new_fd = te->symlink_parent_fd;
2261 new_fd = open_on_current_dir(t, "..", O_RDONLY | O_CLOEXEC);
2262 __archive_ensure_cloexec_flag(new_fd);
2265 t->tree_errno = errno;
2266 r = TREE_ERROR_FATAL;
2268 /* Renew the current working directory. */
2269 t->working_dir_fd = new_fd;
2270 t->flags &= ~onWorkingDir;
2271 /* Current directory has been changed, we should
2272 * close an fd of previous working directory. */
2273 close_and_restore_time(prev_dir_fd, t, &te->restore_time);
2274 if (te->flags & isDirLink) {
2276 te->symlink_parent_fd = -1;
2284 * Return to the initial directory where tree_open() was performed.
2287 tree_enter_initial_dir(struct tree *t)
2291 if ((t->flags & onInitialDir) == 0) {
2292 r = fchdir(t->initial_dir_fd);
2294 t->flags &= ~onWorkingDir;
2295 t->flags |= onInitialDir;
2302 * Restore working directory of directory traversals.
2305 tree_enter_working_dir(struct tree *t)
2310 * Change the current directory if really needed.
2311 * Sometimes this is unneeded when we did not do
2314 if (t->depth > 0 && (t->flags & onWorkingDir) == 0) {
2315 r = fchdir(t->working_dir_fd);
2317 t->flags &= ~onInitialDir;
2318 t->flags |= onWorkingDir;
2325 tree_current_dir_fd(struct tree *t)
2327 return (t->working_dir_fd);
2331 * Pop the working stack.
2334 tree_pop(struct tree *t)
2336 struct tree_entry *te;
2338 t->path.s[t->dirname_length] = '\0';
2339 t->path.length = t->dirname_length;
2340 if (t->stack == t->current && t->current != NULL)
2341 t->current = t->current->parent;
2343 t->stack = te->next;
2344 t->dirname_length = te->dirname_length;
2345 t->basename = t->path.s + t->dirname_length;
2346 while (t->basename[0] == '/')
2348 archive_string_free(&te->name);
2353 * Get the next item in the tree traversal.
2356 tree_next(struct tree *t)
2360 while (t->stack != NULL) {
2361 /* If there's an open dir, get the next entry from there. */
2362 if (t->d != INVALID_DIR_HANDLE) {
2363 r = tree_dir_next_posix(t);
2369 if (t->stack->flags & needsFirstVisit) {
2370 /* Top stack item needs a regular visit. */
2371 t->current = t->stack;
2372 tree_append(t, t->stack->name.s,
2373 archive_strlen(&(t->stack->name)));
2374 /* t->dirname_length = t->path_length; */
2376 t->stack->flags &= ~needsFirstVisit;
2377 return (t->visit_type = TREE_REGULAR);
2378 } else if (t->stack->flags & needsDescent) {
2379 /* Top stack item is dir to descend into. */
2380 t->current = t->stack;
2381 tree_append(t, t->stack->name.s,
2382 archive_strlen(&(t->stack->name)));
2383 t->stack->flags &= ~needsDescent;
2384 r = tree_descent(t);
2389 t->visit_type = TREE_POSTDESCENT;
2390 return (t->visit_type);
2391 } else if (t->stack->flags & needsOpen) {
2392 t->stack->flags &= ~needsOpen;
2393 r = tree_dir_next_posix(t);
2397 } else if (t->stack->flags & needsAscent) {
2398 /* Top stack item is dir and we're done with it. */
2401 t->visit_type = r != 0 ? r : TREE_POSTASCENT;
2402 return (t->visit_type);
2404 /* Top item on stack is dead. */
2406 t->flags &= ~hasLstat;
2407 t->flags &= ~hasStat;
2410 return (t->visit_type = 0);
2414 tree_dir_next_posix(struct tree *t)
2421 #if defined(USE_READDIR_R)
2425 #if defined(HAVE_FDOPENDIR)
2426 t->d = fdopendir(tree_dup(t->working_dir_fd));
2427 #else /* HAVE_FDOPENDIR */
2428 if (tree_enter_working_dir(t) == 0) {
2429 t->d = opendir(".");
2430 #if HAVE_DIRFD || defined(dirfd)
2431 __archive_ensure_cloexec_flag(dirfd(t->d));
2434 #endif /* HAVE_FDOPENDIR */
2436 r = tree_ascend(t); /* Undo "chdir" */
2438 t->tree_errno = errno;
2439 t->visit_type = r != 0 ? r : TREE_ERROR_DIR;
2440 return (t->visit_type);
2442 #if defined(USE_READDIR_R)
2443 dirent_size = offsetof(struct dirent, d_name) +
2444 t->filesystem_table[t->current->filesystem_id].name_max + 1;
2445 if (t->dirent == NULL || t->dirent_allocated < dirent_size) {
2447 t->dirent = malloc(dirent_size);
2448 if (t->dirent == NULL) {
2450 t->d = INVALID_DIR_HANDLE;
2451 (void)tree_ascend(t);
2453 t->tree_errno = ENOMEM;
2454 t->visit_type = TREE_ERROR_DIR;
2455 return (t->visit_type);
2457 t->dirent_allocated = dirent_size;
2459 #endif /* USE_READDIR_R */
2463 #if defined(USE_READDIR_R)
2464 r = readdir_r(t->d, t->dirent, &t->de);
2466 /* Note: According to the man page, return value 9 indicates
2467 * that the readdir_r was not successful and the error code
2468 * is set to the global errno variable. And then if the end
2469 * of directory entries was reached, the return value is 9
2470 * and the third parameter is set to NULL and errno is
2475 if (r != 0 || t->de == NULL) {
2477 t->de = readdir(t->d);
2478 if (t->de == NULL) {
2482 t->d = INVALID_DIR_HANDLE;
2485 t->visit_type = TREE_ERROR_DIR;
2486 return (t->visit_type);
2490 name = t->de->d_name;
2491 namelen = D_NAMELEN(t->de);
2492 t->flags &= ~hasLstat;
2493 t->flags &= ~hasStat;
2494 if (name[0] == '.' && name[1] == '\0')
2496 if (name[0] == '.' && name[1] == '.' && name[2] == '\0')
2498 tree_append(t, name, namelen);
2499 return (t->visit_type = TREE_REGULAR);
2505 * Get the stat() data for the entry just returned from tree_next().
2507 static const struct stat *
2508 tree_current_stat(struct tree *t)
2510 if (!(t->flags & hasStat)) {
2512 if (fstatat(tree_current_dir_fd(t),
2513 tree_current_access_path(t), &t->st, 0) != 0)
2515 if (tree_enter_working_dir(t) != 0)
2517 if (la_stat(tree_current_access_path(t), &t->st) != 0)
2520 t->flags |= hasStat;
2526 * Get the lstat() data for the entry just returned from tree_next().
2528 static const struct stat *
2529 tree_current_lstat(struct tree *t)
2531 if (!(t->flags & hasLstat)) {
2533 if (fstatat(tree_current_dir_fd(t),
2534 tree_current_access_path(t), &t->lst,
2535 AT_SYMLINK_NOFOLLOW) != 0)
2537 if (tree_enter_working_dir(t) != 0)
2539 if (lstat(tree_current_access_path(t), &t->lst) != 0)
2542 t->flags |= hasLstat;
2548 * Test whether current entry is a dir or link to a dir.
2551 tree_current_is_dir(struct tree *t)
2553 const struct stat *st;
2555 * If we already have lstat() info, then try some
2556 * cheap tests to determine if this is a dir.
2558 if (t->flags & hasLstat) {
2559 /* If lstat() says it's a dir, it must be a dir. */
2560 st = tree_current_lstat(t);
2563 if (S_ISDIR(st->st_mode))
2565 /* Not a dir; might be a link to a dir. */
2566 /* If it's not a link, then it's not a link to a dir. */
2567 if (!S_ISLNK(st->st_mode))
2570 * It's a link, but we don't know what it's a link to,
2571 * so we'll have to use stat().
2575 st = tree_current_stat(t);
2576 /* If we can't stat it, it's not a dir. */
2579 /* Use the definitive test. Hopefully this is cached. */
2580 return (S_ISDIR(st->st_mode));
2584 * Test whether current entry is a physical directory. Usually, we
2585 * already have at least one of stat() or lstat() in memory, so we
2586 * use tricks to try to avoid an extra trip to the disk.
2589 tree_current_is_physical_dir(struct tree *t)
2591 const struct stat *st;
2594 * If stat() says it isn't a dir, then it's not a dir.
2595 * If stat() data is cached, this check is free, so do it first.
2597 if (t->flags & hasStat) {
2598 st = tree_current_stat(t);
2601 if (!S_ISDIR(st->st_mode))
2606 * Either stat() said it was a dir (in which case, we have
2607 * to determine whether it's really a link to a dir) or
2608 * stat() info wasn't available. So we use lstat(), which
2609 * hopefully is already cached.
2612 st = tree_current_lstat(t);
2613 /* If we can't stat it, it's not a dir. */
2616 /* Use the definitive test. Hopefully this is cached. */
2617 return (S_ISDIR(st->st_mode));
2621 * Test whether the same file has been in the tree as its parent.
2624 tree_target_is_same_as_parent(struct tree *t, const struct stat *st)
2626 struct tree_entry *te;
2628 for (te = t->current->parent; te != NULL; te = te->parent) {
2629 if (te->dev == (int64_t)st->st_dev &&
2630 te->ino == (int64_t)st->st_ino)
2637 * Test whether the current file is symbolic link target and
2638 * on the other filesystem.
2641 tree_current_is_symblic_link_target(struct tree *t)
2643 static const struct stat *lst, *st;
2645 lst = tree_current_lstat(t);
2646 st = tree_current_stat(t);
2647 return (st != NULL && lst != NULL &&
2648 (int64_t)st->st_dev == t->current_filesystem->dev &&
2649 st->st_dev != lst->st_dev);
2653 * Return the access path for the entry just returned from tree_next().
2656 tree_current_access_path(struct tree *t)
2658 return (t->basename);
2662 * Return the full path for the entry just returned from tree_next().
2665 tree_current_path(struct tree *t)
2671 * Terminate the traversal.
2674 tree_close(struct tree *t)
2679 if (t->entry_fd >= 0) {
2680 close_and_restore_time(t->entry_fd, t, &t->restore_time);
2683 /* Close the handle of readdir(). */
2684 if (t->d != INVALID_DIR_HANDLE) {
2686 t->d = INVALID_DIR_HANDLE;
2688 /* Release anything remaining in the stack. */
2689 while (t->stack != NULL) {
2690 if (t->stack->flags & isDirLink)
2691 close(t->stack->symlink_parent_fd);
2694 if (t->working_dir_fd >= 0) {
2695 close(t->working_dir_fd);
2696 t->working_dir_fd = -1;
2698 if (t->initial_dir_fd >= 0) {
2699 close(t->initial_dir_fd);
2700 t->initial_dir_fd = -1;
2705 * Release any resources.
2708 tree_free(struct tree *t)
2714 archive_string_free(&t->path);
2715 #if defined(USE_READDIR_R)
2718 free(t->sparse_list);
2719 for (i = 0; i < t->max_filesystem_id; i++)
2720 free(t->filesystem_table[i].allocation_ptr);
2721 free(t->filesystem_table);