2 * Copyright (c) 2003-2009 Tim Kientzle
3 * Copyright (c) 2010,2011 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>
81 #include "archive_string.h"
82 #include "archive_entry.h"
83 #include "archive_private.h"
84 #include "archive_read_disk_private.h"
87 #error fchdir function required.
94 * This is a new directory-walking system that addresses a number
95 * of problems I've had with fts(3). In particular, it has no
96 * pathname-length limits (other than the size of 'int'), handles
97 * deep logical traversals, uses considerably less memory, and has
98 * an opaque interface (easier to modify in the future).
100 * Internally, it keeps a single list of "tree_entry" items that
101 * represent filesystem objects that require further attention.
102 * Non-directories are not kept in memory: they are pulled from
103 * readdir(), returned to the client, then freed as soon as possible.
104 * Any directory entry to be traversed gets pushed onto the stack.
106 * There is surprisingly little information that needs to be kept for
107 * each item on the stack. Just the name, depth (represented here as the
108 * string length of the parent directory's pathname), and some markers
109 * indicating how to get back to the parent (via chdir("..") for a
110 * regular dir or via fchdir(2) for a symlink).
115 * 3) Arbitrary logical traversals by closing/reopening intermediate fds.
118 struct restore_time {
130 struct tree_entry *next;
131 struct tree_entry *parent;
132 struct archive_string name;
133 size_t dirname_length;
138 /* How to return back to the parent of a symlink. */
139 int symlink_parent_fd;
140 /* How to restore time of a directory. */
141 struct restore_time restore_time;
149 #if defined(HAVE_READDIR_R)
158 * Buffer used for reading file contents.
160 /* Exactly allocated memory pointer. */
161 unsigned char *allocation_ptr;
162 /* Pointer adjusted to the filesystem alignment . */
167 /* Definitions for tree_entry.flags bitmap. */
168 #define isDir 1 /* This entry is a regular directory. */
169 #define isDirLink 2 /* This entry is a symbolic link to a directory. */
170 #define needsFirstVisit 4 /* This is an initial entry. */
171 #define needsDescent 8 /* This entry needs to be previsited. */
172 #define needsOpen 16 /* This is a directory that needs to be opened. */
173 #define needsAscent 32 /* This entry needs to be postvisited. */
176 * Local data for this package.
179 struct tree_entry *stack;
180 struct tree_entry *current;
182 #define INVALID_DIR_HANDLE NULL
184 #if defined(HAVE_READDIR_R)
185 struct dirent *dirent;
186 size_t dirent_allocated;
190 /* Error code from last failed operation. */
193 /* Dynamically-sized buffer for holding path */
194 struct archive_string path;
196 /* Last path element */
197 const char *basename;
198 /* Leading dir length */
199 size_t dirname_length;
211 /* How to restore time of a file. */
212 struct restore_time restore_time;
214 struct entry_sparse {
217 } *sparse_list, *current_sparse;
219 int sparse_list_size;
221 char initial_symlink_mode;
223 struct filesystem *current_filesystem;
224 struct filesystem *filesystem_table;
225 int current_filesystem_id;
226 int max_filesystem_id;
227 int allocated_filesytem;
231 int64_t entry_remaining_bytes;
233 unsigned char *entry_buff;
234 size_t entry_buff_size;
237 /* Definitions for tree.flags bitmap. */
238 #define hasStat 16 /* The st entry is valid. */
239 #define hasLstat 32 /* The lst entry is valid. */
240 #define onWorkingDir 64 /* We are on the working dir where we are
241 * reading directory entry at this time. */
242 #define needsRestoreTimes 128
245 tree_dir_next_posix(struct tree *t);
247 #ifdef HAVE_DIRENT_D_NAMLEN
248 /* BSD extension; avoids need for a strlen() call. */
249 #define D_NAMELEN(dp) (dp)->d_namlen
251 #define D_NAMELEN(dp) (strlen((dp)->d_name))
254 /* Initiate/terminate a tree traversal. */
255 static struct tree *tree_open(const char *, int, int);
256 static struct tree *tree_reopen(struct tree *, const char *, int);
257 static void tree_close(struct tree *);
258 static void tree_free(struct tree *);
259 static void tree_push(struct tree *, const char *, int, int64_t, int64_t,
260 struct restore_time *);
261 static int tree_enter_initial_dir(struct tree *);
262 static int tree_enter_working_dir(struct tree *);
263 static int tree_current_dir_fd(struct tree *);
266 * tree_next() returns Zero if there is no next entry, non-zero if
267 * there is. Note that directories are visited three times.
268 * Directories are always visited first as part of enumerating their
269 * parent; that is a "regular" visit. If tree_descend() is invoked at
270 * that time, the directory is added to a work list and will
271 * subsequently be visited two more times: once just after descending
272 * into the directory ("postdescent") and again just after ascending
273 * back to the parent ("postascent").
275 * TREE_ERROR_DIR is returned if the descent failed (because the
276 * directory couldn't be opened, for instance). This is returned
277 * instead of TREE_POSTDESCENT/TREE_POSTASCENT. TREE_ERROR_DIR is not a
278 * fatal error, but it does imply that the relevant subtree won't be
279 * visited. TREE_ERROR_FATAL is returned for an error that left the
280 * traversal completely hosed. Right now, this is only returned for
281 * chdir() failures during ascent.
283 #define TREE_REGULAR 1
284 #define TREE_POSTDESCENT 2
285 #define TREE_POSTASCENT 3
286 #define TREE_ERROR_DIR -1
287 #define TREE_ERROR_FATAL -2
289 static int tree_next(struct tree *);
292 * Return information about the current entry.
296 * The current full pathname, length of the full pathname, and a name
297 * that can be used to access the file. Because tree does use chdir
298 * extensively, the access path is almost never the same as the full
301 * TODO: On platforms that support it, use openat()-style operations
302 * to eliminate the chdir() operations entirely while still supporting
303 * arbitrarily deep traversals. This makes access_path troublesome to
304 * support, of course, which means we'll need a rich enough interface
305 * that clients can function without it. (In particular, we'll need
306 * tree_current_open() that returns an open file descriptor.)
309 static const char *tree_current_path(struct tree *);
310 static const char *tree_current_access_path(struct tree *);
313 * Request the lstat() or stat() data for the current path. Since the
314 * tree package needs to do some of this anyway, and caches the
315 * results, you should take advantage of it here if you need it rather
316 * than make a redundant stat() or lstat() call of your own.
318 static const struct stat *tree_current_stat(struct tree *);
319 static const struct stat *tree_current_lstat(struct tree *);
320 static int tree_current_is_symblic_link_target(struct tree *);
322 /* The following functions use tricks to avoid a certain number of
323 * stat()/lstat() calls. */
324 /* "is_physical_dir" is equivalent to S_ISDIR(tree_current_lstat()->st_mode) */
325 static int tree_current_is_physical_dir(struct tree *);
326 /* "is_dir" is equivalent to S_ISDIR(tree_current_stat()->st_mode) */
327 static int tree_current_is_dir(struct tree *);
328 static int update_current_filesystem(struct archive_read_disk *a,
330 static int setup_current_filesystem(struct archive_read_disk *);
331 static int tree_target_is_same_as_parent(struct tree *, const struct stat *);
333 static int _archive_read_disk_open(struct archive *, const char *);
334 static int _archive_read_free(struct archive *);
335 static int _archive_read_close(struct archive *);
336 static int _archive_read_data_block(struct archive *,
337 const void **, size_t *, int64_t *);
338 static int _archive_read_next_header2(struct archive *,
339 struct archive_entry *);
340 static const char *trivial_lookup_gname(void *, int64_t gid);
341 static const char *trivial_lookup_uname(void *, int64_t uid);
342 static int setup_sparse(struct archive_read_disk *, struct archive_entry *);
343 static int close_and_restore_time(int fd, struct tree *,
344 struct restore_time *);
347 static struct archive_vtable *
348 archive_read_disk_vtable(void)
350 static struct archive_vtable av;
351 static int inited = 0;
354 av.archive_free = _archive_read_free;
355 av.archive_close = _archive_read_close;
356 av.archive_read_data_block = _archive_read_data_block;
357 av.archive_read_next_header2 = _archive_read_next_header2;
364 archive_read_disk_gname(struct archive *_a, int64_t gid)
366 struct archive_read_disk *a = (struct archive_read_disk *)_a;
367 if (ARCHIVE_OK != __archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
368 ARCHIVE_STATE_ANY, "archive_read_disk_gname"))
370 if (a->lookup_gname == NULL)
372 return ((*a->lookup_gname)(a->lookup_gname_data, gid));
376 archive_read_disk_uname(struct archive *_a, int64_t uid)
378 struct archive_read_disk *a = (struct archive_read_disk *)_a;
379 if (ARCHIVE_OK != __archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
380 ARCHIVE_STATE_ANY, "archive_read_disk_uname"))
382 if (a->lookup_uname == NULL)
384 return ((*a->lookup_uname)(a->lookup_uname_data, uid));
388 archive_read_disk_set_gname_lookup(struct archive *_a,
390 const char * (*lookup_gname)(void *private, int64_t gid),
391 void (*cleanup_gname)(void *private))
393 struct archive_read_disk *a = (struct archive_read_disk *)_a;
394 archive_check_magic(&a->archive, ARCHIVE_READ_DISK_MAGIC,
395 ARCHIVE_STATE_ANY, "archive_read_disk_set_gname_lookup");
397 if (a->cleanup_gname != NULL && a->lookup_gname_data != NULL)
398 (a->cleanup_gname)(a->lookup_gname_data);
400 a->lookup_gname = lookup_gname;
401 a->cleanup_gname = cleanup_gname;
402 a->lookup_gname_data = private_data;
407 archive_read_disk_set_uname_lookup(struct archive *_a,
409 const char * (*lookup_uname)(void *private, int64_t uid),
410 void (*cleanup_uname)(void *private))
412 struct archive_read_disk *a = (struct archive_read_disk *)_a;
413 archive_check_magic(&a->archive, ARCHIVE_READ_DISK_MAGIC,
414 ARCHIVE_STATE_ANY, "archive_read_disk_set_uname_lookup");
416 if (a->cleanup_uname != NULL && a->lookup_uname_data != NULL)
417 (a->cleanup_uname)(a->lookup_uname_data);
419 a->lookup_uname = lookup_uname;
420 a->cleanup_uname = cleanup_uname;
421 a->lookup_uname_data = private_data;
426 * Create a new archive_read_disk object and initialize it with global state.
429 archive_read_disk_new(void)
431 struct archive_read_disk *a;
433 a = (struct archive_read_disk *)malloc(sizeof(*a));
436 memset(a, 0, sizeof(*a));
437 a->archive.magic = ARCHIVE_READ_DISK_MAGIC;
438 a->archive.state = ARCHIVE_STATE_NEW;
439 a->archive.vtable = archive_read_disk_vtable();
440 a->lookup_uname = trivial_lookup_uname;
441 a->lookup_gname = trivial_lookup_gname;
443 return (&a->archive);
447 _archive_read_free(struct archive *_a)
449 struct archive_read_disk *a = (struct archive_read_disk *)_a;
454 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
455 ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_free");
457 if (a->archive.state != ARCHIVE_STATE_CLOSED)
458 r = _archive_read_close(&a->archive);
463 if (a->cleanup_gname != NULL && a->lookup_gname_data != NULL)
464 (a->cleanup_gname)(a->lookup_gname_data);
465 if (a->cleanup_uname != NULL && a->lookup_uname_data != NULL)
466 (a->cleanup_uname)(a->lookup_uname_data);
467 archive_string_free(&a->archive.error_string);
468 a->archive.magic = 0;
469 __archive_clean(&a->archive);
475 _archive_read_close(struct archive *_a)
477 struct archive_read_disk *a = (struct archive_read_disk *)_a;
479 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
480 ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_close");
482 if (a->archive.state != ARCHIVE_STATE_FATAL)
483 a->archive.state = ARCHIVE_STATE_CLOSED;
491 setup_symlink_mode(struct archive_read_disk *a, char symlink_mode,
494 a->symlink_mode = symlink_mode;
495 a->follow_symlinks = follow_symlinks;
496 if (a->tree != NULL) {
497 a->tree->initial_symlink_mode = a->symlink_mode;
498 a->tree->symlink_mode = a->symlink_mode;
503 archive_read_disk_set_symlink_logical(struct archive *_a)
505 struct archive_read_disk *a = (struct archive_read_disk *)_a;
506 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
507 ARCHIVE_STATE_ANY, "archive_read_disk_set_symlink_logical");
508 setup_symlink_mode(a, 'L', 1);
513 archive_read_disk_set_symlink_physical(struct archive *_a)
515 struct archive_read_disk *a = (struct archive_read_disk *)_a;
516 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
517 ARCHIVE_STATE_ANY, "archive_read_disk_set_symlink_physical");
518 setup_symlink_mode(a, 'P', 0);
523 archive_read_disk_set_symlink_hybrid(struct archive *_a)
525 struct archive_read_disk *a = (struct archive_read_disk *)_a;
526 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
527 ARCHIVE_STATE_ANY, "archive_read_disk_set_symlink_hybrid");
528 setup_symlink_mode(a, 'H', 1);/* Follow symlinks initially. */
533 archive_read_disk_set_atime_restored(struct archive *_a)
536 static int warning_done = 0;
538 struct archive_read_disk *a = (struct archive_read_disk *)_a;
539 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
540 ARCHIVE_STATE_ANY, "archive_read_disk_restore_atime");
544 a->tree->flags |= needsRestoreTimes;
548 /* Warning was already emitted; suppress further warnings. */
551 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
552 "Cannot restore access time on this system");
554 return (ARCHIVE_WARN);
559 * Trivial implementations of gname/uname lookup functions.
560 * These are normally overridden by the client, but these stub
561 * versions ensure that we always have something that works.
564 trivial_lookup_gname(void *private_data, int64_t gid)
566 (void)private_data; /* UNUSED */
567 (void)gid; /* UNUSED */
572 trivial_lookup_uname(void *private_data, int64_t uid)
574 (void)private_data; /* UNUSED */
575 (void)uid; /* UNUSED */
580 * Allocate memory for the reading buffer adjusted to the filesystem
584 setup_suitable_read_buffer(struct archive_read_disk *a)
586 struct tree *t = a->tree;
587 struct filesystem *cf = t->current_filesystem;
591 if (cf->allocation_ptr == NULL) {
592 /* If we couldn't get a filesystem alignment,
593 * we use 4096 as default value but we won't use
594 * O_DIRECT to open() and openat() operations. */
595 long xfer_align = (cf->xfer_align == -1)?4096:cf->xfer_align;
597 if (cf->max_xfer_size != -1)
598 asize = cf->max_xfer_size + xfer_align;
600 long incr = cf->incr_xfer_size;
601 /* Some platform does not set a proper value to
604 incr = cf->min_xfer_size;
605 if (cf->min_xfer_size < 0) {
609 asize = cf->min_xfer_size;
611 /* Increase a buffer size up to 64K bytes in
612 * a proper incremant size. */
613 while (asize < 1024*64)
615 /* Take a margin to adjust to the filesystem
619 cf->allocation_ptr = malloc(asize);
620 if (cf->allocation_ptr == NULL) {
621 archive_set_error(&a->archive, ENOMEM,
622 "Couldn't allocate memory");
623 a->archive.state = ARCHIVE_STATE_FATAL;
624 return (ARCHIVE_FATAL);
628 * Calculate proper address for the filesystem.
630 s = (uintptr_t)cf->allocation_ptr;
636 * Set a read buffer pointer in the proper alignment of
637 * the current filesystem.
639 cf->buff = cf->allocation_ptr + s;
640 cf->buff_size = asize - xfer_align;
646 _archive_read_data_block(struct archive *_a, const void **buff,
647 size_t *size, int64_t *offset)
649 struct archive_read_disk *a = (struct archive_read_disk *)_a;
650 struct tree *t = a->tree;
655 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA,
656 "archive_read_data_block");
658 if (t->entry_eof || t->entry_remaining_bytes <= 0) {
660 goto abort_read_data;
664 * Open the current file.
666 if (t->entry_fd < 0) {
667 int flags = O_RDONLY | O_BINARY;
670 * Eliminate or reduce cache effects if we can.
672 * Carefully consider this to be enabled.
674 #if defined(O_DIRECT) && 0/* Disabled for now */
675 if (t->current_filesystem->xfer_align != -1 &&
679 #if defined(O_NOATIME)
681 * Linux has O_NOATIME flag; use it if we need.
683 if ((t->flags & needsRestoreTimes) != 0 &&
684 t->restore_time.noatime == 0)
689 t->entry_fd = openat(tree_current_dir_fd(t),
690 tree_current_access_path(t), flags);
692 tree_enter_working_dir(t);
693 t->entry_fd = open(tree_current_access_path(t), flags);
695 #if defined(O_NOATIME)
697 * When we did open the file with O_NOATIME flag,
698 * if successful, set 1 to t->restore_time.noatime
699 * not to restore an atime of the file later.
700 * if failed by EPERM, retry it without O_NOATIME flag.
702 if (flags & O_NOATIME) {
703 if (t->entry_fd >= 0)
704 t->restore_time.noatime = 1;
705 else if (errno == EPERM) {
712 if (t->entry_fd < 0) {
713 archive_set_error(&a->archive, errno,
714 "Couldn't open %s", tree_current_path(t));
716 tree_enter_initial_dir(t);
717 goto abort_read_data;
719 tree_enter_initial_dir(t);
723 * Allocate read buffer if not allocated.
725 if (t->current_filesystem->allocation_ptr == NULL) {
726 r = setup_suitable_read_buffer(a);
727 if (r != ARCHIVE_OK) {
728 a->archive.state = ARCHIVE_STATE_FATAL;
729 goto abort_read_data;
732 t->entry_buff = t->current_filesystem->buff;
733 t->entry_buff_size = t->current_filesystem->buff_size;
735 buffbytes = t->entry_buff_size;
736 if (buffbytes > t->current_sparse->length)
737 buffbytes = t->current_sparse->length;
741 * TODO: Should we consider t->current_filesystem->xfer_align?
743 if (t->current_sparse->offset > t->entry_total) {
744 if (lseek(t->entry_fd,
745 (off_t)t->current_sparse->offset, SEEK_SET) < 0) {
746 archive_set_error(&a->archive, errno, "Seek error");
748 a->archive.state = ARCHIVE_STATE_FATAL;
749 goto abort_read_data;
751 bytes = t->current_sparse->offset - t->entry_total;
752 t->entry_remaining_bytes -= bytes;
753 t->entry_total += bytes;
757 * Read file contents.
760 bytes = read(t->entry_fd, t->entry_buff, buffbytes);
762 archive_set_error(&a->archive, errno, "Read error");
764 a->archive.state = ARCHIVE_STATE_FATAL;
765 goto abort_read_data;
773 goto abort_read_data;
775 *buff = t->entry_buff;
777 *offset = t->entry_total;
778 t->entry_total += bytes;
779 t->entry_remaining_bytes -= bytes;
780 if (t->entry_remaining_bytes == 0) {
781 /* Close the current file descriptor */
782 close_and_restore_time(t->entry_fd, t, &t->restore_time);
786 t->current_sparse->offset += bytes;
787 t->current_sparse->length -= bytes;
788 if (t->current_sparse->length == 0 && !t->entry_eof)
795 *offset = t->entry_total;
796 if (t->entry_fd >= 0) {
797 /* Close the current file descriptor */
798 close_and_restore_time(t->entry_fd, t, &t->restore_time);
805 _archive_read_next_header2(struct archive *_a, struct archive_entry *entry)
807 struct archive_read_disk *a = (struct archive_read_disk *)_a;
809 const struct stat *st; /* info to use for this entry */
810 const struct stat *lst;/* lstat() information */
811 int descend, fd = -1, r;
813 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
814 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
815 "archive_read_next_header2");
818 if (t->entry_fd >= 0) {
819 close_and_restore_time(t->entry_fd, t, &t->restore_time);
822 #if !(defined(HAVE_OPENAT) && defined(HAVE_FSTATAT) && defined(HAVE_FDOPENDIR))
823 /* Restore working directory. */
824 tree_enter_working_dir(t);
829 switch (tree_next(t)) {
830 case TREE_ERROR_FATAL:
831 archive_set_error(&a->archive, t->tree_errno,
832 "%s: Unable to continue traversing directory tree",
833 tree_current_path(t));
834 a->archive.state = ARCHIVE_STATE_FATAL;
835 tree_enter_initial_dir(t);
836 return (ARCHIVE_FATAL);
838 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
839 "%s: Couldn't visit directory",
840 tree_current_path(t));
841 tree_enter_initial_dir(t);
842 return (ARCHIVE_FAILED);
844 tree_enter_initial_dir(t);
845 return (ARCHIVE_EOF);
846 case TREE_POSTDESCENT:
847 case TREE_POSTASCENT:
850 lst = tree_current_lstat(t);
852 archive_set_error(&a->archive, errno,
854 tree_current_path(t));
855 tree_enter_initial_dir(t);
856 return (ARCHIVE_FAILED);
860 } while (lst == NULL);
863 * Distinguish 'L'/'P'/'H' symlink following.
865 switch(t->symlink_mode) {
867 /* 'H': After the first item, rest like 'P'. */
868 t->symlink_mode = 'P';
869 /* 'H': First item (from command line) like 'L'. */
872 /* 'L': Do descend through a symlink to dir. */
873 descend = tree_current_is_dir(t);
874 /* 'L': Follow symlinks to files. */
875 a->symlink_mode = 'L';
876 a->follow_symlinks = 1;
877 /* 'L': Archive symlinks as targets, if we can. */
878 st = tree_current_stat(t);
879 if (st != NULL && !tree_target_is_same_as_parent(t, st))
881 /* If stat fails, we have a broken symlink;
882 * in that case, don't follow the link. */
885 /* 'P': Don't descend through a symlink to dir. */
886 descend = tree_current_is_physical_dir(t);
887 /* 'P': Don't follow symlinks to files. */
888 a->symlink_mode = 'P';
889 a->follow_symlinks = 0;
890 /* 'P': Archive symlinks as symlinks. */
895 if (update_current_filesystem(a, st->st_dev) != ARCHIVE_OK) {
896 a->archive.state = ARCHIVE_STATE_FATAL;
897 tree_enter_initial_dir(t);
898 return (ARCHIVE_FATAL);
900 t->descend = descend;
902 archive_entry_set_pathname(entry, tree_current_path(t));
903 archive_entry_copy_sourcepath(entry, tree_current_access_path(t));
904 archive_entry_copy_stat(entry, st);
906 /* Save the times to be restored. */
907 t->restore_time.mtime = archive_entry_mtime(entry);
908 t->restore_time.mtime_nsec = archive_entry_mtime_nsec(entry);
909 t->restore_time.atime = archive_entry_atime(entry);
910 t->restore_time.atime_nsec = archive_entry_atime_nsec(entry);
911 t->restore_time.filetype = archive_entry_filetype(entry);
912 t->restore_time.noatime = t->current_filesystem->noatime;
914 #if defined(HAVE_OPENAT) && defined(HAVE_FSTATAT) && defined(HAVE_FDOPENDIR)
916 * Open the current file to freely gather its metadata anywhere in
918 * Note: A symbolic link file cannot be opened with O_NOFOLLOW.
920 if (a->follow_symlinks || archive_entry_filetype(entry) != AE_IFLNK)
921 fd = openat(tree_current_dir_fd(t), tree_current_access_path(t),
922 O_RDONLY | O_NONBLOCK);
923 /* Restore working directory if openat() operation failed or
924 * the file is a symbolic link. */
926 tree_enter_working_dir(t);
928 /* The current direcotry fd is needed at
929 * archive_read_disk_entry_from_file() function to read link data
930 * with readlinkat(). */
931 a->entry_wd_fd = tree_current_dir_fd(t);
935 * Populate the archive_entry with metadata from the disk.
937 r = archive_read_disk_entry_from_file(&(a->archive), entry, fd, st);
939 /* Close the file descriptor used for reding the current file
940 * metadata at archive_read_disk_entry_from_file(). */
944 /* Return to the initial directory. */
945 tree_enter_initial_dir(t);
946 archive_entry_copy_sourcepath(entry, tree_current_path(t));
949 * EOF and FATAL are persistent at this layer. By
950 * modifying the state, we guarantee that future calls to
951 * read a header or read data will fail.
955 a->archive.state = ARCHIVE_STATE_EOF;
960 if (archive_entry_filetype(entry) == AE_IFREG) {
961 t->nlink = archive_entry_nlink(entry);
962 t->entry_remaining_bytes = archive_entry_size(entry);
963 t->entry_eof = (t->entry_remaining_bytes == 0)? 1: 0;
965 setup_sparse(a, entry) != ARCHIVE_OK)
966 return (ARCHIVE_FATAL);
968 t->entry_remaining_bytes = 0;
971 a->archive.state = ARCHIVE_STATE_DATA;
976 a->archive.state = ARCHIVE_STATE_FATAL;
984 setup_sparse(struct archive_read_disk *a, struct archive_entry *entry)
986 struct tree *t = a->tree;
987 int64_t length, offset;
990 t->sparse_count = archive_entry_sparse_reset(entry);
991 if (t->sparse_count+1 > t->sparse_list_size) {
992 free(t->sparse_list);
993 t->sparse_list_size = t->sparse_count + 1;
994 t->sparse_list = malloc(sizeof(t->sparse_list[0]) *
995 t->sparse_list_size);
996 if (t->sparse_list == NULL) {
997 t->sparse_list_size = 0;
998 archive_set_error(&a->archive, ENOMEM,
999 "Can't allocate data");
1000 a->archive.state = ARCHIVE_STATE_FATAL;
1001 return (ARCHIVE_FATAL);
1004 for (i = 0; i < t->sparse_count; i++) {
1005 archive_entry_sparse_next(entry, &offset, &length);
1006 t->sparse_list[i].offset = offset;
1007 t->sparse_list[i].length = length;
1010 t->sparse_list[i].offset = 0;
1011 t->sparse_list[i].length = archive_entry_size(entry);
1013 t->sparse_list[i].offset = archive_entry_size(entry);
1014 t->sparse_list[i].length = 0;
1016 t->current_sparse = t->sparse_list;
1018 return (ARCHIVE_OK);
1022 * Called by the client to mark the directory just returned from
1023 * tree_next() as needing to be visited.
1026 archive_read_disk_descend(struct archive *_a)
1028 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1029 struct tree *t = a->tree;
1031 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA,
1032 "archive_read_disk_descend");
1034 if (t->visit_type != TREE_REGULAR || !t->descend) {
1035 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1036 "Ignored the request descending the current object");
1037 return (ARCHIVE_WARN);
1040 if (tree_current_is_physical_dir(t)) {
1041 tree_push(t, t->basename, t->current_filesystem_id,
1042 t->lst.st_dev, t->lst.st_ino, &t->restore_time);
1043 t->stack->flags |= isDir;
1044 } else if (tree_current_is_dir(t)) {
1045 tree_push(t, t->basename, t->current_filesystem_id,
1046 t->st.st_dev, t->st.st_ino, &t->restore_time);
1047 t->stack->flags |= isDirLink;
1050 return (ARCHIVE_OK);
1054 archive_read_disk_open(struct archive *_a, const char *pathname)
1056 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1058 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
1059 ARCHIVE_STATE_NEW | ARCHIVE_STATE_CLOSED,
1060 "archive_read_disk_open");
1061 archive_clear_error(&a->archive);
1063 return (_archive_read_disk_open(_a, pathname));
1067 archive_read_disk_open_w(struct archive *_a, const wchar_t *pathname)
1069 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1070 struct archive_string path;
1073 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
1074 ARCHIVE_STATE_NEW | ARCHIVE_STATE_CLOSED,
1075 "archive_read_disk_open_w");
1076 archive_clear_error(&a->archive);
1078 /* Make a char string from a wchar_t string. */
1079 archive_string_init(&path);
1080 if (archive_string_append_from_wcs(&path, pathname,
1081 wcslen(pathname)) != 0) {
1082 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1083 "Can't convert a path to a char string");
1084 a->archive.state = ARCHIVE_STATE_FATAL;
1085 ret = ARCHIVE_FATAL;
1087 ret = _archive_read_disk_open(_a, path.s);
1089 archive_string_free(&path);
1094 _archive_read_disk_open(struct archive *_a, const char *pathname)
1096 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1098 if (a->tree != NULL)
1099 a->tree = tree_reopen(a->tree, pathname, a->restore_time);
1101 a->tree = tree_open(pathname, a->symlink_mode,
1103 if (a->tree == NULL) {
1104 archive_set_error(&a->archive, ENOMEM,
1105 "Can't allocate tar data");
1106 a->archive.state = ARCHIVE_STATE_FATAL;
1107 return (ARCHIVE_FATAL);
1109 a->archive.state = ARCHIVE_STATE_HEADER;
1111 return (ARCHIVE_OK);
1115 * Return a current filesystem ID which is index of the filesystem entry
1116 * you've visited through archive_read_disk.
1119 archive_read_disk_current_filesystem(struct archive *_a)
1121 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1123 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA,
1124 "archive_read_disk_current_filesystem");
1126 return (a->tree->current_filesystem_id);
1130 update_current_filesystem(struct archive_read_disk *a, int64_t dev)
1132 struct tree *t = a->tree;
1135 if (t->current_filesystem != NULL &&
1136 t->current_filesystem->dev == dev)
1137 return (ARCHIVE_OK);
1139 for (i = 0; i < t->max_filesystem_id; i++) {
1140 if (t->filesystem_table[i].dev == dev) {
1141 /* There is the filesytem ID we've already generated. */
1142 t->current_filesystem_id = i;
1143 t->current_filesystem = &(t->filesystem_table[i]);
1144 return (ARCHIVE_OK);
1149 * This is the new filesytem which we have to generate a new ID for.
1151 fid = t->max_filesystem_id++;
1152 if (t->max_filesystem_id > t->allocated_filesytem) {
1155 s = t->max_filesystem_id * 2;
1156 t->filesystem_table = realloc(t->filesystem_table,
1157 s * sizeof(*t->filesystem_table));
1158 if (t->filesystem_table == NULL) {
1159 archive_set_error(&a->archive, ENOMEM,
1160 "Can't allocate tar data");
1161 return (ARCHIVE_FATAL);
1163 t->allocated_filesytem = s;
1165 t->current_filesystem_id = fid;
1166 t->current_filesystem = &(t->filesystem_table[fid]);
1167 t->current_filesystem->dev = dev;
1168 t->current_filesystem->allocation_ptr = NULL;
1169 t->current_filesystem->buff = NULL;
1171 /* Setup the current filesystem properties which depend on
1172 * platform specific. */
1173 return (setup_current_filesystem(a));
1177 * Returns 1 if current filesystem is generated filesystem, 0 if it is not
1178 * or -1 if it is unknown.
1181 archive_read_disk_current_filesystem_is_synthetic(struct archive *_a)
1183 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1185 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA,
1186 "archive_read_disk_current_filesystem");
1188 return (a->tree->current_filesystem->synthetic);
1192 * Returns 1 if current filesystem is remote filesystem, 0 if it is not
1193 * or -1 if it is unknown.
1196 archive_read_disk_current_filesystem_is_remote(struct archive *_a)
1198 struct archive_read_disk *a = (struct archive_read_disk *)_a;
1200 archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA,
1201 "archive_read_disk_current_filesystem");
1203 return (a->tree->current_filesystem->remote);
1206 #if defined(_PC_REC_INCR_XFER_SIZE) && defined(_PC_REC_MAX_XFER_SIZE) &&\
1207 defined(_PC_REC_MIN_XFER_SIZE) && defined(_PC_REC_XFER_ALIGN)
1209 get_xfer_size(struct tree *t, int fd, const char *path)
1211 t->current_filesystem->xfer_align = -1;
1214 t->current_filesystem->incr_xfer_size =
1215 fpathconf(fd, _PC_REC_INCR_XFER_SIZE);
1216 t->current_filesystem->max_xfer_size =
1217 fpathconf(fd, _PC_REC_MAX_XFER_SIZE);
1218 t->current_filesystem->min_xfer_size =
1219 fpathconf(fd, _PC_REC_MIN_XFER_SIZE);
1220 t->current_filesystem->xfer_align =
1221 fpathconf(fd, _PC_REC_XFER_ALIGN);
1222 } else if (path != NULL) {
1223 t->current_filesystem->incr_xfer_size =
1224 pathconf(path, _PC_REC_INCR_XFER_SIZE);
1225 t->current_filesystem->max_xfer_size =
1226 pathconf(path, _PC_REC_MAX_XFER_SIZE);
1227 t->current_filesystem->min_xfer_size =
1228 pathconf(path, _PC_REC_MIN_XFER_SIZE);
1229 t->current_filesystem->xfer_align =
1230 pathconf(path, _PC_REC_XFER_ALIGN);
1232 /* At least we need an alignment size. */
1233 if (t->current_filesystem->xfer_align == -1)
1234 return ((errno == EINVAL)?1:-1);
1240 get_xfer_size(struct tree *t, int fd, const char *path)
1242 (void)t; /* UNUSED */
1243 (void)fd; /* UNUSED */
1244 (void)path; /* UNUSED */
1245 return (1);/* Not supported */
1249 #if defined(HAVE_STATFS) && defined(HAVE_FSTATFS) && defined(MNT_LOCAL) \
1250 && !defined(ST_LOCAL)
1253 * Gather current filesystem properties on FreeBSD, OpenBSD and Mac OS X.
1256 setup_current_filesystem(struct archive_read_disk *a)
1258 struct tree *t = a->tree;
1260 #if defined(HAVE_GETVFSBYNAME) && defined(VFCF_SYNTHETIC)
1261 struct xvfsconf vfc;
1264 #if !defined(HAVE_STRUCT_STATFS_F_NAMEMAX)
1268 t->current_filesystem->synthetic = -1;
1269 t->current_filesystem->remote = -1;
1270 if (tree_current_is_symblic_link_target(t)) {
1271 #if defined(HAVE_OPENAT) && defined(HAVE_FSTATAT) && defined(HAVE_FDOPENDIR)
1273 * Get file system statistics on any directory
1276 int fd = openat(tree_current_dir_fd(t),
1277 tree_current_access_path(t), O_RDONLY);
1279 archive_set_error(&a->archive, errno,
1281 return (ARCHIVE_FAILED);
1283 r = fstatfs(fd, &sfs);
1285 xr = get_xfer_size(t, fd, NULL);
1288 r = statfs(tree_current_access_path(t), &sfs);
1290 xr = get_xfer_size(t, -1, tree_current_access_path(t));
1293 r = fstatfs(tree_current_dir_fd(t), &sfs);
1295 xr = get_xfer_size(t, tree_current_dir_fd(t), NULL);
1297 if (r == -1 || xr == -1) {
1298 archive_set_error(&a->archive, errno, "statfs failed");
1299 return (ARCHIVE_FAILED);
1300 } else if (xr == 1) {
1301 /* pathconf(_PC_REX_*) operations are not supported. */
1302 t->current_filesystem->xfer_align = sfs.f_bsize;
1303 t->current_filesystem->max_xfer_size = -1;
1304 t->current_filesystem->min_xfer_size = sfs.f_iosize;
1305 t->current_filesystem->incr_xfer_size = sfs.f_iosize;
1307 if (sfs.f_flags & MNT_LOCAL)
1308 t->current_filesystem->remote = 0;
1310 t->current_filesystem->remote = 1;
1312 #if defined(HAVE_GETVFSBYNAME) && defined(VFCF_SYNTHETIC)
1313 r = getvfsbyname(sfs.f_fstypename, &vfc);
1315 archive_set_error(&a->archive, errno, "getvfsbyname failed");
1316 return (ARCHIVE_FAILED);
1318 if (vfc.vfc_flags & VFCF_SYNTHETIC)
1319 t->current_filesystem->synthetic = 1;
1321 t->current_filesystem->synthetic = 0;
1324 if (sfs.f_flags & MNT_NOATIME)
1325 t->current_filesystem->noatime = 1;
1327 t->current_filesystem->noatime = 0;
1329 #if defined(HAVE_READDIR_R)
1330 /* Set maximum filename length. */
1331 #if defined(HAVE_STRUCT_STATFS_F_NAMEMAX)
1332 t->current_filesystem->name_max = sfs.f_namemax;
1334 /* Mac OS X does not have f_namemax in struct statfs. */
1335 if (tree_current_is_symblic_link_target(t))
1336 nm = pathconf(tree_current_access_path(t), _PC_NAME_MAX);
1338 nm = fpathconf(tree_current_dir_fd(t), _PC_NAME_MAX);
1340 t->current_filesystem->name_max = NAME_MAX;
1342 t->current_filesystem->name_max = nm;
1344 #endif /* HAVE_READDIR_R */
1345 return (ARCHIVE_OK);
1348 #elif (defined(HAVE_STATVFS) || defined(HAVE_FSTATVFS)) && defined(ST_LOCAL)
1351 * Gather current filesystem properties on NetBSD
1354 setup_current_filesystem(struct archive_read_disk *a)
1356 struct tree *t = a->tree;
1360 t->current_filesystem->synthetic = -1;
1361 if (tree_current_is_symblic_link_target(t)) {
1362 r = statvfs(tree_current_access_path(t), &sfs);
1364 xr = get_xfer_size(t, -1, tree_current_access_path(t));
1366 #ifdef HAVE_FSTATVFS
1367 r = fstatvfs(tree_current_dir_fd(t), &sfs);
1369 xr = get_xfer_size(t, tree_current_dir_fd(t), NULL);
1371 r = statvfs(".", &sfs);
1373 xr = get_xfer_size(t, -1, ".");
1376 if (r == -1 || xr == -1) {
1377 t->current_filesystem->remote = -1;
1378 archive_set_error(&a->archive, errno, "statvfs failed");
1379 return (ARCHIVE_FAILED);
1380 } else if (xr == 1) {
1381 /* Usuall come here unless NetBSD supports _PC_REC_XFER_ALIGN
1382 * for pathconf() function. */
1383 t->current_filesystem->xfer_align = sfs.f_frsize;
1384 t->current_filesystem->max_xfer_size = -1;
1385 t->current_filesystem->min_xfer_size = sfs.f_iosize;
1386 t->current_filesystem->incr_xfer_size = sfs.f_iosize;
1388 if (sfs.f_flag & ST_LOCAL)
1389 t->current_filesystem->remote = 0;
1391 t->current_filesystem->remote = 1;
1393 if (sfs.f_flag & ST_NOATIME)
1394 t->current_filesystem->noatime = 1;
1396 t->current_filesystem->noatime = 0;
1398 /* Set maximum filename length. */
1399 t->current_filesystem->name_max = sfs.f_namemax;
1400 return (ARCHIVE_OK);
1403 #elif defined(HAVE_SYS_STATFS_H) && defined(HAVE_LINUX_MAGIC_H) &&\
1404 defined(HAVE_STATFS) && defined(HAVE_FSTATFS)
1406 * Note: statfs is deprecated since LSB 3.2
1409 #ifndef CIFS_SUPER_MAGIC
1410 #define CIFS_SUPER_MAGIC 0xFF534D42
1412 #ifndef DEVFS_SUPER_MAGIC
1413 #define DEVFS_SUPER_MAGIC 0x1373
1417 * Gather current filesystem properties on Linux
1420 setup_current_filesystem(struct archive_read_disk *a)
1422 struct tree *t = a->tree;
1424 struct statvfs svfs;
1425 int r, vr = 0, xr = 0;
1427 if (tree_current_is_symblic_link_target(t)) {
1428 #if defined(HAVE_OPENAT) && defined(HAVE_FSTATAT) && defined(HAVE_FDOPENDIR)
1430 * Get file system statistics on any directory
1433 int fd = openat(tree_current_dir_fd(t),
1434 tree_current_access_path(t), O_RDONLY);
1436 archive_set_error(&a->archive, errno,
1438 return (ARCHIVE_FAILED);
1440 vr = fstatvfs(fd, &svfs);/* for f_flag, mount flags */
1441 r = fstatfs(fd, &sfs);
1443 xr = get_xfer_size(t, fd, NULL);
1446 vr = statvfs(tree_current_access_path(t), &svfs);
1447 r = statfs(tree_current_access_path(t), &sfs);
1449 xr = get_xfer_size(t, -1, tree_current_access_path(t));
1453 vr = fstatvfs(tree_current_dir_fd(t), &svfs);
1454 r = fstatfs(tree_current_dir_fd(t), &sfs);
1456 xr = get_xfer_size(t, tree_current_dir_fd(t), NULL);
1457 #elif defined(HAVE_OPENAT) && defined(HAVE_FSTATAT) && defined(HAVE_FDOPENDIR)
1458 #error "Unexpected case. Please tell us about this error."
1460 vr = statvfs(".", &svfs);
1461 r = statfs(".", &sfs);
1463 xr = get_xfer_size(t, -1, ".");
1466 if (r == -1 || xr == -1 || vr == -1) {
1467 t->current_filesystem->synthetic = -1;
1468 t->current_filesystem->remote = -1;
1469 archive_set_error(&a->archive, errno, "statfs failed");
1470 return (ARCHIVE_FAILED);
1471 } else if (xr == 1) {
1472 /* pathconf(_PC_REX_*) operations are not supported. */
1473 t->current_filesystem->xfer_align = svfs.f_frsize;
1474 t->current_filesystem->max_xfer_size = -1;
1475 t->current_filesystem->min_xfer_size = svfs.f_bsize;
1476 t->current_filesystem->incr_xfer_size = svfs.f_bsize;
1478 switch (sfs.f_type) {
1479 case AFS_SUPER_MAGIC:
1480 case CIFS_SUPER_MAGIC:
1481 case CODA_SUPER_MAGIC:
1482 case NCP_SUPER_MAGIC:/* NetWare */
1483 case NFS_SUPER_MAGIC:
1484 case SMB_SUPER_MAGIC:
1485 t->current_filesystem->remote = 1;
1486 t->current_filesystem->synthetic = 0;
1488 case DEVFS_SUPER_MAGIC:
1489 case PROC_SUPER_MAGIC:
1490 case USBDEVICE_SUPER_MAGIC:
1491 t->current_filesystem->remote = 0;
1492 t->current_filesystem->synthetic = 1;
1495 t->current_filesystem->remote = 0;
1496 t->current_filesystem->synthetic = 0;
1500 #if defined(ST_NOATIME)
1501 if (svfs.f_flag & ST_NOATIME)
1502 t->current_filesystem->noatime = 1;
1505 t->current_filesystem->noatime = 0;
1507 #if defined(HAVE_READDIR_R)
1508 /* Set maximum filename length. */
1509 t->current_filesystem->name_max = sfs.f_namelen;
1511 return (ARCHIVE_OK);
1514 #elif defined(HAVE_SYS_STATVFS_H) &&\
1515 (defined(HAVE_STATVFS) || defined(HAVE_FSTATVFS))
1518 * Gather current filesystem properties on other posix platform.
1521 setup_current_filesystem(struct archive_read_disk *a)
1523 struct tree *t = a->tree;
1527 t->current_filesystem->synthetic = -1;/* Not supported */
1528 t->current_filesystem->remote = -1;/* Not supported */
1529 if (tree_current_is_symblic_link_target(t)) {
1530 #if defined(HAVE_OPENAT) && defined(HAVE_FSTATAT) && defined(HAVE_FDOPENDIR)
1532 * Get file system statistics on any directory
1535 int fd = openat(tree_current_dir_fd(t),
1536 tree_current_access_path(t), O_RDONLY);
1538 archive_set_error(&a->archive, errno,
1540 return (ARCHIVE_FAILED);
1542 r = fstatvfs(fd, &sfs);
1544 xr = get_xfer_size(t, fd, NULL);
1547 r = statvfs(tree_current_access_path(t), &sfs);
1549 xr = get_xfer_size(t, -1, tree_current_access_path(t));
1552 #ifdef HAVE_FSTATVFS
1553 r = fstatvfs(tree_current_dir_fd(t), &sfs);
1555 xr = get_xfer_size(t, tree_current_dir_fd(t), NULL);
1556 #elif defined(HAVE_OPENAT) && defined(HAVE_FSTATAT) && defined(HAVE_FDOPENDIR)
1557 #error "Unexpected case. Please tell us about this error."
1559 r = statvfs(".", &sfs);
1561 xr = get_xfer_size(t, -1, ".");
1564 if (r == -1 || xr == -1) {
1565 t->current_filesystem->synthetic = -1;
1566 t->current_filesystem->remote = -1;
1567 archive_set_error(&a->archive, errno, "statvfs failed");
1568 return (ARCHIVE_FAILED);
1569 } else if (xr == 1) {
1570 /* pathconf(_PC_REX_*) operations are not supported. */
1571 t->current_filesystem->xfer_align = sfs.f_frsize;
1572 t->current_filesystem->max_xfer_size = -1;
1573 t->current_filesystem->min_xfer_size = sfs.f_bsize;
1574 t->current_filesystem->incr_xfer_size = sfs.f_bsize;
1577 #if defined(ST_NOATIME)
1578 if (sfs.f_flag & ST_NOATIME)
1579 t->current_filesystem->noatime = 1;
1582 t->current_filesystem->noatime = 0;
1584 #if defined(HAVE_READDIR_R)
1585 /* Set maximum filename length. */
1586 t->current_filesystem->name_max = sfs.f_namemax;
1588 return (ARCHIVE_OK);
1594 * Generic: Gather current filesystem properties.
1595 * TODO: Is this generic function really needed?
1598 setup_current_filesystem(struct archive_read_disk *a)
1600 struct tree *t = a->tree;
1601 #if defined(_PC_NAME_MAX) && defined(HAVE_READDIR_R)
1604 t->current_filesystem->synthetic = -1;/* Not supported */
1605 t->current_filesystem->remote = -1;/* Not supported */
1606 t->current_filesystem->noatime = 0;
1607 (void)get_xfer_size(t, -1, ".");/* Dummy call to avoid build error. */
1608 t->current_filesystem->xfer_align = -1;/* Unknown */
1609 t->current_filesystem->max_xfer_size = -1;
1610 t->current_filesystem->min_xfer_size = -1;
1611 t->current_filesystem->incr_xfer_size = -1;
1613 #if defined(HAVE_READDIR_R)
1614 /* Set maximum filename length. */
1615 # if defined(_PC_NAME_MAX)
1616 if (tree_current_is_symblic_link_target(t))
1617 nm = pathconf(tree_current_access_path(t), _PC_NAME_MAX);
1619 nm = fpathconf(tree_current_dir_fd(t), _PC_NAME_MAX);
1621 # endif /* _PC_NAME_MAX */
1623 * Some sysmtes (HP-UX or others?) incorrectly defined
1624 * NAME_MAX macro to be a smaller value.
1626 # if defined(NAME_MAX) && NAME_MAX >= 255
1627 t->current_filesystem->name_max = NAME_MAX;
1629 /* No way to get a trusted value of maximum filename
1631 t->current_filesystem->name_max = PATH_MAX;
1632 # endif /* NAME_MAX */
1633 # if defined(_PC_NAME_MAX)
1635 t->current_filesystem->name_max = nm;
1636 # endif /* _PC_NAME_MAX */
1637 #endif /* HAVE_READDIR_R */
1638 return (ARCHIVE_OK);
1644 close_and_restore_time(int fd, struct tree *t, struct restore_time *rt)
1647 (void)a; /* UNUSED */
1650 #if defined(HAVE_FUTIMENS) && !defined(__CYGWIN__)
1651 struct timespec timespecs[2];
1653 struct timeval times[2];
1655 if ((t->flags & needsRestoreTimes) == 0 || rt->noatime) {
1662 #if defined(HAVE_FUTIMENS) && !defined(__CYGWIN__)
1663 timespecs[1].tv_sec = rt->mtime;
1664 timespecs[1].tv_nsec = rt->mtime_nsec;
1666 timespecs[0].tv_sec = rt->atime;
1667 timespecs[0].tv_nsec = rt->atime_nsec;
1668 /* futimens() is defined in POSIX.1-2008. */
1669 if (futimens(fd, timespecs) == 0)
1673 times[1].tv_sec = rt->mtime;
1674 times[1].tv_usec = rt->mtime_nsec / 1000;
1676 times[0].tv_sec = rt->atime;
1677 times[0].tv_usec = rt->atime_nsec / 1000;
1679 #if !defined(HAVE_FUTIMENS) && defined(HAVE_FUTIMES) && !defined(__CYGWIN__)
1680 if (futimes(fd, times) == 0)
1684 #if defined(HAVE_FUTIMESAT)
1685 if (futimesat(tree_current_dir_fd(t), rt->name, times) == 0)
1689 if (lutimes(rt->name, times) != 0)
1691 if (AE_IFLNK != rt->filetype && utimes(rt->name, times) != 0)
1699 * Add a directory path to the current stack.
1702 tree_push(struct tree *t, const char *path, int filesystem_id,
1703 int64_t dev, int64_t ino, struct restore_time *rt)
1705 struct tree_entry *te;
1707 te = malloc(sizeof(*te));
1708 memset(te, 0, sizeof(*te));
1709 te->next = t->stack;
1710 te->parent = t->current;
1712 te->depth = te->parent->depth + 1;
1714 archive_string_init(&te->name);
1715 te->symlink_parent_fd = -1;
1716 archive_strcpy(&te->name, path);
1717 te->flags = needsDescent | needsOpen | needsAscent;
1718 te->filesystem_id = filesystem_id;
1721 te->dirname_length = t->dirname_length;
1722 te->restore_time.name = te->name.s;
1724 te->restore_time.mtime = rt->mtime;
1725 te->restore_time.mtime_nsec = rt->mtime_nsec;
1726 te->restore_time.atime = rt->atime;
1727 te->restore_time.atime_nsec = rt->atime_nsec;
1728 te->restore_time.filetype = rt->filetype;
1729 te->restore_time.noatime = rt->noatime;
1734 * Append a name to the current dir path.
1737 tree_append(struct tree *t, const char *name, size_t name_length)
1741 t->path.s[t->dirname_length] = '\0';
1742 t->path.length = t->dirname_length;
1743 /* Strip trailing '/' from name, unless entire name is "/". */
1744 while (name_length > 1 && name[name_length - 1] == '/')
1747 /* Resize pathname buffer as needed. */
1748 size_needed = name_length + t->dirname_length + 2;
1749 archive_string_ensure(&t->path, size_needed);
1750 /* Add a separating '/' if it's needed. */
1751 if (t->dirname_length > 0 && t->path.s[archive_strlen(&t->path)-1] != '/')
1752 archive_strappend_char(&t->path, '/');
1753 t->basename = t->path.s + archive_strlen(&t->path);
1754 archive_strncat(&t->path, name, name_length);
1755 t->restore_time.name = t->basename;
1759 * Open a directory tree for traversal.
1761 static struct tree *
1762 tree_open(const char *path, int symlink_mode, int restore_time)
1766 if ((t = malloc(sizeof(*t))) == NULL)
1768 memset(t, 0, sizeof(*t));
1769 archive_string_init(&t->path);
1770 archive_string_ensure(&t->path, 31);
1771 t->initial_symlink_mode = symlink_mode;
1772 return (tree_reopen(t, path, restore_time));
1775 static struct tree *
1776 tree_reopen(struct tree *t, const char *path, int restore_time)
1778 t->flags = (restore_time)?needsRestoreTimes:0;
1781 t->dirname_length = 0;
1785 t->d = INVALID_DIR_HANDLE;
1786 t->symlink_mode = t->initial_symlink_mode;
1787 archive_string_empty(&t->path);
1790 t->entry_remaining_bytes = 0;
1792 /* First item is set up a lot like a symlink traversal. */
1793 tree_push(t, path, 0, 0, 0, NULL);
1794 t->stack->flags = needsFirstVisit;
1795 t->maxOpenCount = t->openCount = 1;
1796 t->initial_dir_fd = open(".", O_RDONLY);
1797 t->working_dir_fd = dup(t->initial_dir_fd);
1802 tree_descent(struct tree *t)
1806 #if defined(HAVE_OPENAT) && defined(HAVE_FSTATAT) && defined(HAVE_FDOPENDIR)
1808 t->dirname_length = archive_strlen(&t->path);
1809 new_fd = openat(t->working_dir_fd, t->stack->name.s, O_RDONLY);
1811 t->tree_errno = errno;
1815 /* If it is a link, set up fd for the ascent. */
1816 if (t->stack->flags & isDirLink) {
1817 t->stack->symlink_parent_fd = t->working_dir_fd;
1819 if (t->openCount > t->maxOpenCount)
1820 t->maxOpenCount = t->openCount;
1822 close(t->working_dir_fd);
1823 t->working_dir_fd = new_fd;
1826 /* If it is a link, set up fd for the ascent. */
1827 if (t->stack->flags & isDirLink)
1828 t->stack->symlink_parent_fd = t->working_dir_fd;
1830 close(t->working_dir_fd);
1833 t->working_dir_fd = -1;
1834 t->dirname_length = archive_strlen(&t->path);
1835 if (chdir(t->stack->name.s) != 0)
1837 t->tree_errno = errno;
1841 t->working_dir_fd = open(".", O_RDONLY);
1843 if (t->openCount > t->maxOpenCount)
1844 t->maxOpenCount = t->openCount;
1851 * We've finished a directory; ascend back to the parent.
1854 tree_ascend(struct tree *t)
1856 struct tree_entry *te;
1857 int r = 0, prev_dir_fd;
1860 prev_dir_fd = t->working_dir_fd;
1861 #if defined(HAVE_OPENAT) && defined(HAVE_FSTATAT) && defined(HAVE_FDOPENDIR)
1862 if (te->flags & isDirLink)
1863 t->working_dir_fd = te->symlink_parent_fd;
1865 int new_fd = openat(t->working_dir_fd, "..", O_RDONLY);
1867 t->tree_errno = errno;
1868 r = TREE_ERROR_FATAL;
1870 t->working_dir_fd = new_fd;
1873 if (te->flags & isDirLink) {
1874 if (fchdir(te->symlink_parent_fd) != 0) {
1875 t->tree_errno = errno;
1876 r = TREE_ERROR_FATAL;
1878 t->working_dir_fd = te->symlink_parent_fd;
1880 if (chdir("..") != 0) {
1881 t->tree_errno = errno;
1882 r = TREE_ERROR_FATAL;
1884 t->working_dir_fd = open(".", O_RDONLY);
1888 /* Current directory has been changed, we should
1889 * close an fd of previous working directory. */
1890 close_and_restore_time(prev_dir_fd, t, &te->restore_time);
1891 if (te->flags & isDirLink) {
1893 te->symlink_parent_fd = -1;
1901 * Return to the initial directory where tree_open() was performed.
1904 tree_enter_initial_dir(struct tree *t)
1908 if (t->flags & onWorkingDir) {
1909 r = fchdir(t->initial_dir_fd);
1911 t->flags &= ~onWorkingDir;
1917 * Restore working directory of directory traversals.
1920 tree_enter_working_dir(struct tree *t)
1925 * Change the current directory if really needed.
1926 * Sometimes this is unneeded when we did not do
1929 if (t->depth > 0 && (t->flags & onWorkingDir) == 0) {
1930 r = fchdir(t->working_dir_fd);
1932 t->flags |= onWorkingDir;
1938 tree_current_dir_fd(struct tree *t)
1940 return (t->working_dir_fd);
1944 * Pop the working stack.
1947 tree_pop(struct tree *t)
1949 struct tree_entry *te;
1951 t->path.s[t->dirname_length] = '\0';
1952 t->path.length = t->dirname_length;
1953 if (t->stack == t->current && t->current != NULL)
1954 t->current = t->current->parent;
1956 t->stack = te->next;
1957 t->dirname_length = te->dirname_length;
1958 t->basename = t->path.s + t->dirname_length;
1959 while (t->basename[0] == '/')
1961 archive_string_free(&te->name);
1966 * Get the next item in the tree traversal.
1969 tree_next(struct tree *t)
1973 while (t->stack != NULL) {
1974 /* If there's an open dir, get the next entry from there. */
1975 if (t->d != INVALID_DIR_HANDLE) {
1976 r = tree_dir_next_posix(t);
1982 if (t->stack->flags & needsFirstVisit) {
1983 /* Top stack item needs a regular visit. */
1984 t->current = t->stack;
1985 tree_append(t, t->stack->name.s,
1986 archive_strlen(&(t->stack->name)));
1987 /* t->dirname_length = t->path_length; */
1989 t->stack->flags &= ~needsFirstVisit;
1990 return (t->visit_type = TREE_REGULAR);
1991 } else if (t->stack->flags & needsDescent) {
1992 /* Top stack item is dir to descend into. */
1993 t->current = t->stack;
1994 tree_append(t, t->stack->name.s,
1995 archive_strlen(&(t->stack->name)));
1996 t->stack->flags &= ~needsDescent;
1997 r = tree_descent(t);
2002 t->visit_type = TREE_POSTDESCENT;
2003 return (t->visit_type);
2004 } else if (t->stack->flags & needsOpen) {
2005 t->stack->flags &= ~needsOpen;
2006 r = tree_dir_next_posix(t);
2010 } else if (t->stack->flags & needsAscent) {
2011 /* Top stack item is dir and we're done with it. */
2014 t->visit_type = r != 0 ? r : TREE_POSTASCENT;
2015 return (t->visit_type);
2017 /* Top item on stack is dead. */
2019 t->flags &= ~hasLstat;
2020 t->flags &= ~hasStat;
2023 return (t->visit_type = 0);
2027 tree_dir_next_posix(struct tree *t)
2034 #if defined(HAVE_READDIR_R)
2038 #if defined(HAVE_FDOPENDIR)
2039 if ((t->d = fdopendir(dup(t->working_dir_fd))) == NULL) {
2041 if ((t->d = opendir(".")) == NULL) {
2043 r = tree_ascend(t); /* Undo "chdir" */
2045 t->tree_errno = errno;
2046 t->visit_type = r != 0 ? r : TREE_ERROR_DIR;
2047 return (t->visit_type);
2049 #if defined(HAVE_READDIR_R)
2050 dirent_size = offsetof(struct dirent, d_name) +
2051 t->filesystem_table[t->current->filesystem_id].name_max + 1;
2052 if (t->dirent == NULL || t->dirent_allocated < dirent_size) {
2054 t->dirent = malloc(dirent_size);
2055 if (t->dirent == NULL) {
2057 t->d = INVALID_DIR_HANDLE;
2058 (void)tree_ascend(t);
2060 t->tree_errno = ENOMEM;
2061 t->visit_type = TREE_ERROR_DIR;
2062 return (t->visit_type);
2064 t->dirent_allocated = dirent_size;
2066 #endif /* HAVE_READDIR_R */
2069 #if defined(HAVE_READDIR_R)
2070 r = readdir_r(t->d, t->dirent, &t->de);
2071 if (r != 0 || t->de == NULL) {
2074 t->de = readdir(t->d);
2075 if (t->de == NULL) {
2079 t->d = INVALID_DIR_HANDLE;
2082 t->visit_type = TREE_ERROR_DIR;
2083 return (t->visit_type);
2087 name = t->de->d_name;
2088 namelen = D_NAMELEN(t->de);
2089 t->flags &= ~hasLstat;
2090 t->flags &= ~hasStat;
2091 if (name[0] == '.' && name[1] == '\0')
2093 if (name[0] == '.' && name[1] == '.' && name[2] == '\0')
2095 tree_append(t, name, namelen);
2096 return (t->visit_type = TREE_REGULAR);
2102 * Get the stat() data for the entry just returned from tree_next().
2104 static const struct stat *
2105 tree_current_stat(struct tree *t)
2107 if (!(t->flags & hasStat)) {
2109 if (fstatat(tree_current_dir_fd(t),
2110 tree_current_access_path(t), &t->st, 0) != 0)
2112 if (stat(tree_current_access_path(t), &t->st) != 0)
2115 t->flags |= hasStat;
2121 * Get the lstat() data for the entry just returned from tree_next().
2123 static const struct stat *
2124 tree_current_lstat(struct tree *t)
2126 if (!(t->flags & hasLstat)) {
2128 if (fstatat(tree_current_dir_fd(t),
2129 tree_current_access_path(t), &t->lst,
2130 AT_SYMLINK_NOFOLLOW) != 0)
2132 if (lstat(tree_current_access_path(t), &t->lst) != 0)
2135 t->flags |= hasLstat;
2141 * Test whether current entry is a dir or link to a dir.
2144 tree_current_is_dir(struct tree *t)
2146 const struct stat *st;
2148 * If we already have lstat() info, then try some
2149 * cheap tests to determine if this is a dir.
2151 if (t->flags & hasLstat) {
2152 /* If lstat() says it's a dir, it must be a dir. */
2153 if (S_ISDIR(tree_current_lstat(t)->st_mode))
2155 /* Not a dir; might be a link to a dir. */
2156 /* If it's not a link, then it's not a link to a dir. */
2157 if (!S_ISLNK(tree_current_lstat(t)->st_mode))
2160 * It's a link, but we don't know what it's a link to,
2161 * so we'll have to use stat().
2165 st = tree_current_stat(t);
2166 /* If we can't stat it, it's not a dir. */
2169 /* Use the definitive test. Hopefully this is cached. */
2170 return (S_ISDIR(st->st_mode));
2174 * Test whether current entry is a physical directory. Usually, we
2175 * already have at least one of stat() or lstat() in memory, so we
2176 * use tricks to try to avoid an extra trip to the disk.
2179 tree_current_is_physical_dir(struct tree *t)
2181 const struct stat *st;
2184 * If stat() says it isn't a dir, then it's not a dir.
2185 * If stat() data is cached, this check is free, so do it first.
2187 if ((t->flags & hasStat)
2188 && (!S_ISDIR(tree_current_stat(t)->st_mode)))
2192 * Either stat() said it was a dir (in which case, we have
2193 * to determine whether it's really a link to a dir) or
2194 * stat() info wasn't available. So we use lstat(), which
2195 * hopefully is already cached.
2198 st = tree_current_lstat(t);
2199 /* If we can't stat it, it's not a dir. */
2202 /* Use the definitive test. Hopefully this is cached. */
2203 return (S_ISDIR(st->st_mode));
2207 * Test whether the same file has been in the tree as its parent.
2210 tree_target_is_same_as_parent(struct tree *t, const struct stat *st)
2212 struct tree_entry *te;
2214 for (te = t->current->parent; te != NULL; te = te->parent) {
2215 if (te->dev == st->st_dev && te->ino == st->st_ino)
2222 * Test whether the current file is symbolic link target and
2223 * on the other filesystem.
2226 tree_current_is_symblic_link_target(struct tree *t)
2228 static const struct stat *lst, *st;
2230 lst = tree_current_lstat(t);
2231 st = tree_current_stat(t);
2232 return (st != NULL && st->st_dev == t->current_filesystem->dev &&
2233 st->st_dev != lst->st_dev);
2237 * Return the access path for the entry just returned from tree_next().
2240 tree_current_access_path(struct tree *t)
2242 return (t->basename);
2246 * Return the full path for the entry just returned from tree_next().
2249 tree_current_path(struct tree *t)
2255 * Terminate the traversal.
2258 tree_close(struct tree *t)
2263 if (t->entry_fd >= 0) {
2264 close_and_restore_time(t->entry_fd, t, &t->restore_time);
2267 /* Close the handle of readdir(). */
2268 if (t->d != INVALID_DIR_HANDLE) {
2270 t->d = INVALID_DIR_HANDLE;
2272 /* Release anything remaining in the stack. */
2273 while (t->stack != NULL) {
2274 if (t->stack->flags & isDirLink)
2275 close(t->stack->symlink_parent_fd);
2278 if (t->working_dir_fd >= 0) {
2279 close(t->working_dir_fd);
2280 t->working_dir_fd = -1;
2282 if (t->initial_dir_fd >= 0) {
2283 close(t->initial_dir_fd);
2284 t->initial_dir_fd = -1;
2289 * Release any resources.
2292 tree_free(struct tree *t)
2298 archive_string_free(&t->path);
2299 #if defined(HAVE_READDIR_R)
2302 free(t->sparse_list);
2303 for (i = 0; i < t->max_filesystem_id; i++)
2304 free(t->filesystem_table[i].allocation_ptr);
2305 free(t->filesystem_table);