xref: /freebsd/contrib/libarchive/libarchive/archive_read_disk_posix.c (revision 401026e4825a05abba6f945cf1b74b3328876fa2)
1 /*-
2  * Copyright (c) 2003-2009 Tim Kientzle
3  * Copyright (c) 2010-2012 Michihiro NAKAJIMA
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
15  *
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.
26  */
27 
28 /* This is the tree-walking code for POSIX systems. */
29 #if !defined(_WIN32) || defined(__CYGWIN__)
30 
31 #include "archive_platform.h"
32 
33 #ifdef HAVE_SYS_PARAM_H
34 #include <sys/param.h>
35 #endif
36 #ifdef HAVE_SYS_STAT_H
37 #include <sys/stat.h>
38 #endif
39 #ifdef HAVE_SYS_STATFS_H
40 #include <sys/statfs.h>
41 #endif
42 #ifdef HAVE_SYS_STATVFS_H
43 #include <sys/statvfs.h>
44 #endif
45 #ifdef HAVE_SYS_TIME_H
46 #include <sys/time.h>
47 #endif
48 #ifdef HAVE_LINUX_MAGIC_H
49 #include <linux/magic.h>
50 #endif
51 #ifdef HAVE_LINUX_FS_H
52 #include <linux/fs.h>
53 #elif HAVE_SYS_MOUNT_H
54 #include <sys/mount.h>
55 #endif
56 /*
57  * Some Linux distributions have both linux/ext2_fs.h and ext2fs/ext2_fs.h.
58  * As the include guards don't agree, the order of include is important.
59  */
60 #ifdef HAVE_LINUX_EXT2_FS_H
61 #include <linux/ext2_fs.h>      /* for Linux file flags */
62 #endif
63 #if defined(HAVE_EXT2FS_EXT2_FS_H) && !defined(__CYGWIN__)
64 #include <ext2fs/ext2_fs.h>     /* Linux file flags, broken on Cygwin */
65 #endif
66 #ifdef HAVE_DIRECT_H
67 #include <direct.h>
68 #endif
69 #ifdef HAVE_DIRENT_H
70 #include <dirent.h>
71 #endif
72 #ifdef HAVE_ERRNO_H
73 #include <errno.h>
74 #endif
75 #ifdef HAVE_FCNTL_H
76 #include <fcntl.h>
77 #endif
78 #ifdef HAVE_LIMITS_H
79 #include <limits.h>
80 #endif
81 #ifdef HAVE_STDLIB_H
82 #include <stdlib.h>
83 #endif
84 #ifdef HAVE_STRING_H
85 #include <string.h>
86 #endif
87 #ifdef HAVE_UNISTD_H
88 #include <unistd.h>
89 #endif
90 #ifdef HAVE_SYS_IOCTL_H
91 #include <sys/ioctl.h>
92 #endif
93 
94 #include "archive.h"
95 #include "archive_string.h"
96 #include "archive_entry.h"
97 #include "archive_private.h"
98 #include "archive_read_disk_private.h"
99 
100 #ifndef HAVE_FCHDIR
101 #error fchdir function required.
102 #endif
103 #ifndef O_BINARY
104 #define O_BINARY	0
105 #endif
106 #ifndef O_CLOEXEC
107 #define O_CLOEXEC	0
108 #endif
109 
110 #if defined(__hpux) && !defined(HAVE_DIRFD)
111 #define dirfd(x) ((x)->__dd_fd)
112 #define HAVE_DIRFD
113 #endif
114 
115 /*-
116  * This is a new directory-walking system that addresses a number
117  * of problems I've had with fts(3).  In particular, it has no
118  * pathname-length limits (other than the size of 'int'), handles
119  * deep logical traversals, uses considerably less memory, and has
120  * an opaque interface (easier to modify in the future).
121  *
122  * Internally, it keeps a single list of "tree_entry" items that
123  * represent filesystem objects that require further attention.
124  * Non-directories are not kept in memory: they are pulled from
125  * readdir(), returned to the client, then freed as soon as possible.
126  * Any directory entry to be traversed gets pushed onto the stack.
127  *
128  * There is surprisingly little information that needs to be kept for
129  * each item on the stack.  Just the name, depth (represented here as the
130  * string length of the parent directory's pathname), and some markers
131  * indicating how to get back to the parent (via chdir("..") for a
132  * regular dir or via fchdir(2) for a symlink).
133  */
134 /*
135  * TODO:
136  *    1) Loop checking.
137  *    3) Arbitrary logical traversals by closing/reopening intermediate fds.
138  */
139 
140 struct restore_time {
141 	const char		*name;
142 	time_t			 mtime;
143 	long			 mtime_nsec;
144 	time_t			 atime;
145 	long			 atime_nsec;
146 	mode_t			 filetype;
147 	int			 noatime;
148 };
149 
150 struct tree_entry {
151 	int			 depth;
152 	struct tree_entry	*next;
153 	struct tree_entry	*parent;
154 	struct archive_string	 name;
155 	size_t			 dirname_length;
156 	int64_t			 dev;
157 	int64_t			 ino;
158 	int			 flags;
159 	int			 filesystem_id;
160 	/* How to return back to the parent of a symlink. */
161 	int			 symlink_parent_fd;
162 	/* How to restore time of a directory. */
163 	struct restore_time	 restore_time;
164 };
165 
166 struct filesystem {
167 	int64_t		dev;
168 	int		synthetic;
169 	int		remote;
170 	int		noatime;
171 	long		incr_xfer_size;
172 	long		max_xfer_size;
173 	long		min_xfer_size;
174 	long		xfer_align;
175 
176 	/*
177 	 * Buffer used for reading file contents.
178 	 */
179 	/* Exactly allocated memory pointer. */
180 	unsigned char	*allocation_ptr;
181 	/* Pointer adjusted to the filesystem alignment . */
182 	unsigned char	*buff;
183 	size_t		 buff_size;
184 };
185 
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. */
193 
194 /*
195  * Local data for this package.
196  */
197 struct tree {
198 	struct tree_entry	*stack;
199 	struct tree_entry	*current;
200 	DIR			*d;
201 #define	INVALID_DIR_HANDLE NULL
202 	struct dirent		*de;
203 	int			 flags;
204 	int			 visit_type;
205 	/* Error code from last failed operation. */
206 	int			 tree_errno;
207 
208 	/* Dynamically-sized buffer for holding path */
209 	struct archive_string	 path;
210 
211 	/* Last path element */
212 	const char		*basename;
213 	/* Leading dir length */
214 	size_t			 dirname_length;
215 
216 	int			 depth;
217 	int			 openCount;
218 	int			 maxOpenCount;
219 	int			 initial_dir_fd;
220 	int			 working_dir_fd;
221 
222 	struct stat		 lst;
223 	struct stat		 st;
224 	int			 descend;
225 	int			 nlink;
226 	/* How to restore time of a file. */
227 	struct restore_time	 restore_time;
228 
229 	struct entry_sparse {
230 		int64_t		 length;
231 		int64_t		 offset;
232 	}			*sparse_list, *current_sparse;
233 	int			 sparse_count;
234 	int			 sparse_list_size;
235 
236 	char			 initial_symlink_mode;
237 	char			 symlink_mode;
238 	struct filesystem	*current_filesystem;
239 	struct filesystem	*filesystem_table;
240 	int			 initial_filesystem_id;
241 	int			 current_filesystem_id;
242 	int			 max_filesystem_id;
243 	int			 allocated_filesystem;
244 
245 	int			 entry_fd;
246 	int			 entry_eof;
247 	int64_t			 entry_remaining_bytes;
248 	int64_t			 entry_total;
249 	unsigned char		*entry_buff;
250 	size_t			 entry_buff_size;
251 };
252 
253 /* Definitions for tree.flags bitmap. */
254 #define	hasStat		16 /* The st entry is valid. */
255 #define	hasLstat	32 /* The lst entry is valid. */
256 #define	onWorkingDir	64 /* We are on the working dir where we are
257 			    * reading directory entry at this time. */
258 #define	needsRestoreTimes 128
259 #define	onInitialDir	256 /* We are on the initial dir. */
260 
261 static int
262 tree_dir_next_posix(struct tree *t);
263 
264 #ifdef HAVE_DIRENT_D_NAMLEN
265 /* BSD extension; avoids need for a strlen() call. */
266 #define	D_NAMELEN(dp)	(dp)->d_namlen
267 #else
268 #define	D_NAMELEN(dp)	(strlen((dp)->d_name))
269 #endif
270 
271 /* Initiate/terminate a tree traversal. */
272 static struct tree *tree_open(const char *, char, int);
273 static struct tree *tree_reopen(struct tree *, const char *, int);
274 static void tree_close(struct tree *);
275 static void tree_free(struct tree *);
276 static void tree_push(struct tree *, const char *, int, int64_t, int64_t,
277 		struct restore_time *);
278 static int tree_enter_initial_dir(struct tree *);
279 static int tree_enter_working_dir(struct tree *);
280 static int tree_current_dir_fd(struct tree *);
281 
282 /*
283  * tree_next() returns Zero if there is no next entry, non-zero if
284  * there is.  Note that directories are visited three times.
285  * Directories are always visited first as part of enumerating their
286  * parent; that is a "regular" visit.  If tree_descend() is invoked at
287  * that time, the directory is added to a work list and will
288  * subsequently be visited two more times: once just after descending
289  * into the directory ("postdescent") and again just after ascending
290  * back to the parent ("postascent").
291  *
292  * TREE_ERROR_DIR is returned if the descent failed (because the
293  * directory couldn't be opened, for instance).  This is returned
294  * instead of TREE_POSTDESCENT/TREE_POSTASCENT.  TREE_ERROR_DIR is not a
295  * fatal error, but it does imply that the relevant subtree won't be
296  * visited.  TREE_ERROR_FATAL is returned for an error that left the
297  * traversal completely hosed.  Right now, this is only returned for
298  * chdir() failures during ascent.
299  */
300 #define	TREE_REGULAR		1
301 #define	TREE_POSTDESCENT	2
302 #define	TREE_POSTASCENT		3
303 #define	TREE_ERROR_DIR		-1
304 #define	TREE_ERROR_FATAL	-2
305 
306 static int tree_next(struct tree *);
307 
308 /*
309  * Return information about the current entry.
310  */
311 
312 /*
313  * The current full pathname, length of the full pathname, and a name
314  * that can be used to access the file.  Because tree does use chdir
315  * extensively, the access path is almost never the same as the full
316  * current path.
317  *
318  * TODO: On platforms that support it, use openat()-style operations
319  * to eliminate the chdir() operations entirely while still supporting
320  * arbitrarily deep traversals.  This makes access_path troublesome to
321  * support, of course, which means we'll need a rich enough interface
322  * that clients can function without it.  (In particular, we'll need
323  * tree_current_open() that returns an open file descriptor.)
324  *
325  */
326 static const char *tree_current_path(struct tree *);
327 static const char *tree_current_access_path(struct tree *);
328 
329 /*
330  * Request the lstat() or stat() data for the current path.  Since the
331  * tree package needs to do some of this anyway, and caches the
332  * results, you should take advantage of it here if you need it rather
333  * than make a redundant stat() or lstat() call of your own.
334  */
335 static const struct stat *tree_current_stat(struct tree *);
336 static const struct stat *tree_current_lstat(struct tree *);
337 static int	tree_current_is_symblic_link_target(struct tree *);
338 
339 /* The following functions use tricks to avoid a certain number of
340  * stat()/lstat() calls. */
341 /* "is_physical_dir" is equivalent to S_ISDIR(tree_current_lstat()->st_mode) */
342 static int tree_current_is_physical_dir(struct tree *);
343 /* "is_dir" is equivalent to S_ISDIR(tree_current_stat()->st_mode) */
344 static int tree_current_is_dir(struct tree *);
345 static int update_current_filesystem(struct archive_read_disk *a,
346 		    int64_t dev);
347 static int setup_current_filesystem(struct archive_read_disk *);
348 static int tree_target_is_same_as_parent(struct tree *, const struct stat *);
349 
350 static int	_archive_read_disk_open(struct archive *, const char *);
351 static int	_archive_read_free(struct archive *);
352 static int	_archive_read_close(struct archive *);
353 static int	_archive_read_data_block(struct archive *,
354 		    const void **, size_t *, int64_t *);
355 static int	_archive_read_next_header(struct archive *,
356 		    struct archive_entry **);
357 static int	_archive_read_next_header2(struct archive *,
358 		    struct archive_entry *);
359 static const char *trivial_lookup_gname(void *, int64_t gid);
360 static const char *trivial_lookup_uname(void *, int64_t uid);
361 static int	setup_sparse(struct archive_read_disk *, struct archive_entry *);
362 static int	close_and_restore_time(int fd, struct tree *,
363 		    struct restore_time *);
364 static int	open_on_current_dir(struct tree *, const char *, int);
365 static int	tree_dup(int);
366 
367 
368 static const struct archive_vtable
369 archive_read_disk_vtable = {
370 	.archive_free = _archive_read_free,
371 	.archive_close = _archive_read_close,
372 	.archive_read_data_block = _archive_read_data_block,
373 	.archive_read_next_header = _archive_read_next_header,
374 	.archive_read_next_header2 = _archive_read_next_header2,
375 };
376 
377 const char *
archive_read_disk_gname(struct archive * _a,la_int64_t gid)378 archive_read_disk_gname(struct archive *_a, la_int64_t gid)
379 {
380 	struct archive_read_disk *a = (struct archive_read_disk *)_a;
381 	if (ARCHIVE_OK != __archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
382 		ARCHIVE_STATE_ANY, "archive_read_disk_gname"))
383 		return (NULL);
384 	if (a->lookup_gname == NULL)
385 		return (NULL);
386 	return ((*a->lookup_gname)(a->lookup_gname_data, gid));
387 }
388 
389 const char *
archive_read_disk_uname(struct archive * _a,la_int64_t uid)390 archive_read_disk_uname(struct archive *_a, la_int64_t uid)
391 {
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_uname"))
395 		return (NULL);
396 	if (a->lookup_uname == NULL)
397 		return (NULL);
398 	return ((*a->lookup_uname)(a->lookup_uname_data, uid));
399 }
400 
401 int
archive_read_disk_set_gname_lookup(struct archive * _a,void * private_data,const char * (* lookup_gname)(void * private,la_int64_t gid),void (* cleanup_gname)(void * private))402 archive_read_disk_set_gname_lookup(struct archive *_a,
403     void *private_data,
404     const char * (*lookup_gname)(void *private, la_int64_t gid),
405     void (*cleanup_gname)(void *private))
406 {
407 	struct archive_read_disk *a = (struct archive_read_disk *)_a;
408 	archive_check_magic(&a->archive, ARCHIVE_READ_DISK_MAGIC,
409 	    ARCHIVE_STATE_ANY, "archive_read_disk_set_gname_lookup");
410 
411 	if (a->cleanup_gname != NULL && a->lookup_gname_data != NULL)
412 		(a->cleanup_gname)(a->lookup_gname_data);
413 
414 	a->lookup_gname = lookup_gname;
415 	a->cleanup_gname = cleanup_gname;
416 	a->lookup_gname_data = private_data;
417 	return (ARCHIVE_OK);
418 }
419 
420 int
archive_read_disk_set_uname_lookup(struct archive * _a,void * private_data,const char * (* lookup_uname)(void * private,la_int64_t uid),void (* cleanup_uname)(void * private))421 archive_read_disk_set_uname_lookup(struct archive *_a,
422     void *private_data,
423     const char * (*lookup_uname)(void *private, la_int64_t uid),
424     void (*cleanup_uname)(void *private))
425 {
426 	struct archive_read_disk *a = (struct archive_read_disk *)_a;
427 	archive_check_magic(&a->archive, ARCHIVE_READ_DISK_MAGIC,
428 	    ARCHIVE_STATE_ANY, "archive_read_disk_set_uname_lookup");
429 
430 	if (a->cleanup_uname != NULL && a->lookup_uname_data != NULL)
431 		(a->cleanup_uname)(a->lookup_uname_data);
432 
433 	a->lookup_uname = lookup_uname;
434 	a->cleanup_uname = cleanup_uname;
435 	a->lookup_uname_data = private_data;
436 	return (ARCHIVE_OK);
437 }
438 
439 /*
440  * Create a new archive_read_disk object and initialize it with global state.
441  */
442 struct archive *
archive_read_disk_new(void)443 archive_read_disk_new(void)
444 {
445 	struct archive_read_disk *a;
446 
447 	a = calloc(1, sizeof(*a));
448 	if (a == NULL)
449 		return (NULL);
450 	a->archive.magic = ARCHIVE_READ_DISK_MAGIC;
451 	a->archive.state = ARCHIVE_STATE_NEW;
452 	a->archive.vtable = &archive_read_disk_vtable;
453 	a->entry = archive_entry_new2(&a->archive);
454 	a->lookup_uname = trivial_lookup_uname;
455 	a->lookup_gname = trivial_lookup_gname;
456 	a->flags = ARCHIVE_READDISK_MAC_COPYFILE;
457 	a->open_on_current_dir = open_on_current_dir;
458 	a->tree_current_dir_fd = tree_current_dir_fd;
459 	a->tree_enter_working_dir = tree_enter_working_dir;
460 	return (&a->archive);
461 }
462 
463 static int
_archive_read_free(struct archive * _a)464 _archive_read_free(struct archive *_a)
465 {
466 	struct archive_read_disk *a = (struct archive_read_disk *)_a;
467 	int r;
468 
469 	if (_a == NULL)
470 		return (ARCHIVE_OK);
471 	archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
472 	    ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_free");
473 
474 	if (a->archive.state != ARCHIVE_STATE_CLOSED)
475 		r = _archive_read_close(&a->archive);
476 	else
477 		r = ARCHIVE_OK;
478 
479 	tree_free(a->tree);
480 	if (a->cleanup_gname != NULL && a->lookup_gname_data != NULL)
481 		(a->cleanup_gname)(a->lookup_gname_data);
482 	if (a->cleanup_uname != NULL && a->lookup_uname_data != NULL)
483 		(a->cleanup_uname)(a->lookup_uname_data);
484 	archive_string_free(&a->archive.error_string);
485 	archive_entry_free(a->entry);
486 	a->archive.magic = 0;
487 	__archive_clean(&a->archive);
488 	free(a);
489 	return (r);
490 }
491 
492 static int
_archive_read_close(struct archive * _a)493 _archive_read_close(struct archive *_a)
494 {
495 	struct archive_read_disk *a = (struct archive_read_disk *)_a;
496 
497 	archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
498 	    ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_close");
499 
500 	if (a->archive.state != ARCHIVE_STATE_FATAL)
501 		a->archive.state = ARCHIVE_STATE_CLOSED;
502 
503 	tree_close(a->tree);
504 
505 	return (ARCHIVE_OK);
506 }
507 
508 static void
setup_symlink_mode(struct archive_read_disk * a,char symlink_mode,char follow_symlinks)509 setup_symlink_mode(struct archive_read_disk *a, char symlink_mode,
510     char follow_symlinks)
511 {
512 	a->symlink_mode = symlink_mode;
513 	a->follow_symlinks = follow_symlinks;
514 	if (a->tree != NULL) {
515 		a->tree->initial_symlink_mode = a->symlink_mode;
516 		a->tree->symlink_mode = a->symlink_mode;
517 	}
518 }
519 
520 int
archive_read_disk_set_symlink_logical(struct archive * _a)521 archive_read_disk_set_symlink_logical(struct archive *_a)
522 {
523 	struct archive_read_disk *a = (struct archive_read_disk *)_a;
524 	archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
525 	    ARCHIVE_STATE_ANY, "archive_read_disk_set_symlink_logical");
526 	setup_symlink_mode(a, 'L', 1);
527 	return (ARCHIVE_OK);
528 }
529 
530 int
archive_read_disk_set_symlink_physical(struct archive * _a)531 archive_read_disk_set_symlink_physical(struct archive *_a)
532 {
533 	struct archive_read_disk *a = (struct archive_read_disk *)_a;
534 	archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
535 	    ARCHIVE_STATE_ANY, "archive_read_disk_set_symlink_physical");
536 	setup_symlink_mode(a, 'P', 0);
537 	return (ARCHIVE_OK);
538 }
539 
540 int
archive_read_disk_set_symlink_hybrid(struct archive * _a)541 archive_read_disk_set_symlink_hybrid(struct archive *_a)
542 {
543 	struct archive_read_disk *a = (struct archive_read_disk *)_a;
544 	archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
545 	    ARCHIVE_STATE_ANY, "archive_read_disk_set_symlink_hybrid");
546 	setup_symlink_mode(a, 'H', 1);/* Follow symlinks initially. */
547 	return (ARCHIVE_OK);
548 }
549 
550 int
archive_read_disk_set_atime_restored(struct archive * _a)551 archive_read_disk_set_atime_restored(struct archive *_a)
552 {
553 	struct archive_read_disk *a = (struct archive_read_disk *)_a;
554 	archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
555 	    ARCHIVE_STATE_ANY, "archive_read_disk_restore_atime");
556 #ifdef HAVE_UTIMES
557 	a->flags |= ARCHIVE_READDISK_RESTORE_ATIME;
558 	if (a->tree != NULL)
559 		a->tree->flags |= needsRestoreTimes;
560 	return (ARCHIVE_OK);
561 #else
562 	/* Display warning and unset flag */
563 	archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
564 	    "Cannot restore access time on this system");
565 	a->flags &= ~ARCHIVE_READDISK_RESTORE_ATIME;
566 	return (ARCHIVE_WARN);
567 #endif
568 }
569 
570 int
archive_read_disk_set_behavior(struct archive * _a,int flags)571 archive_read_disk_set_behavior(struct archive *_a, int flags)
572 {
573 	struct archive_read_disk *a = (struct archive_read_disk *)_a;
574 	int r = ARCHIVE_OK;
575 
576 	archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
577 	    ARCHIVE_STATE_ANY, "archive_read_disk_honor_nodump");
578 
579 	a->flags = flags;
580 
581 	if (flags & ARCHIVE_READDISK_RESTORE_ATIME)
582 		r = archive_read_disk_set_atime_restored(_a);
583 	else {
584 		if (a->tree != NULL)
585 			a->tree->flags &= ~needsRestoreTimes;
586 	}
587 	return (r);
588 }
589 
590 /*
591  * Trivial implementations of gname/uname lookup functions.
592  * These are normally overridden by the client, but these stub
593  * versions ensure that we always have something that works.
594  */
595 static const char *
trivial_lookup_gname(void * private_data,int64_t gid)596 trivial_lookup_gname(void *private_data, int64_t gid)
597 {
598 	(void)private_data; /* UNUSED */
599 	(void)gid; /* UNUSED */
600 	return (NULL);
601 }
602 
603 static const char *
trivial_lookup_uname(void * private_data,int64_t uid)604 trivial_lookup_uname(void *private_data, int64_t uid)
605 {
606 	(void)private_data; /* UNUSED */
607 	(void)uid; /* UNUSED */
608 	return (NULL);
609 }
610 
611 /*
612  * Allocate memory for the reading buffer adjusted to the filesystem
613  * alignment.
614  */
615 static int
setup_suitable_read_buffer(struct archive_read_disk * a)616 setup_suitable_read_buffer(struct archive_read_disk *a)
617 {
618 	struct tree *t = a->tree;
619 	struct filesystem *cf = t->current_filesystem;
620 	size_t asize;
621 	size_t s;
622 
623 	if (cf->allocation_ptr == NULL) {
624 		/* If we couldn't get a filesystem alignment,
625 		 * we use 4096 as default value but we won't use
626 		 * O_DIRECT to open() and openat() operations. */
627 		long xfer_align = (cf->xfer_align == -1)?4096:cf->xfer_align;
628 
629 		if (cf->max_xfer_size != -1)
630 			asize = cf->max_xfer_size + xfer_align;
631 		else {
632 			long incr = cf->incr_xfer_size;
633 			/* Some platform does not set a proper value to
634 			 * incr_xfer_size.*/
635 			if (incr < 0)
636 				incr = cf->min_xfer_size;
637 			if (cf->min_xfer_size < 0) {
638 				incr = xfer_align;
639 				asize = xfer_align;
640 			} else
641 				asize = cf->min_xfer_size;
642 
643 			/* Increase a buffer size up to 64K bytes in
644 			 * a proper increment size. */
645 			while (asize < 1024*64)
646 				asize += incr;
647 			/* Take a margin to adjust to the filesystem
648 			 * alignment. */
649 			asize += xfer_align;
650 		}
651 		cf->allocation_ptr = malloc(asize);
652 		if (cf->allocation_ptr == NULL) {
653 			archive_set_error(&a->archive, ENOMEM,
654 			    "Couldn't allocate memory");
655 			a->archive.state = ARCHIVE_STATE_FATAL;
656 			return (ARCHIVE_FATAL);
657 		}
658 
659 		/*
660 		 * Calculate proper address for the filesystem.
661 		 */
662 		s = (uintptr_t)cf->allocation_ptr;
663 		s %= xfer_align;
664 		if (s > 0)
665 			s = xfer_align - s;
666 
667 		/*
668 		 * Set a read buffer pointer in the proper alignment of
669 		 * the current filesystem.
670 		 */
671 		cf->buff = cf->allocation_ptr + s;
672 		cf->buff_size = asize - xfer_align;
673 	}
674 	return (ARCHIVE_OK);
675 }
676 
677 static int
_archive_read_data_block(struct archive * _a,const void ** buff,size_t * size,int64_t * offset)678 _archive_read_data_block(struct archive *_a, const void **buff,
679     size_t *size, int64_t *offset)
680 {
681 	struct archive_read_disk *a = (struct archive_read_disk *)_a;
682 	struct tree *t = a->tree;
683 	int r;
684 	ssize_t bytes;
685 	int64_t sparse_bytes;
686 	size_t buffbytes;
687 	int empty_sparse_region = 0;
688 
689 	archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA,
690 	    "archive_read_data_block");
691 
692 	if (t->entry_eof || t->entry_remaining_bytes <= 0) {
693 		r = ARCHIVE_EOF;
694 		goto abort_read_data;
695 	}
696 
697 	/*
698 	 * Open the current file.
699 	 */
700 	if (t->entry_fd < 0) {
701 		int flags = O_RDONLY | O_BINARY | O_CLOEXEC;
702 
703 		/*
704 		 * Eliminate or reduce cache effects if we can.
705 		 *
706 		 * Carefully consider this to be enabled.
707 		 */
708 #if defined(O_DIRECT) && 0/* Disabled for now */
709 		if (t->current_filesystem->xfer_align != -1 &&
710 		    t->nlink == 1)
711 			flags |= O_DIRECT;
712 #endif
713 #if defined(O_NOATIME)
714 		/*
715 		 * Linux has O_NOATIME flag; use it if we need.
716 		 */
717 		if ((t->flags & needsRestoreTimes) != 0 &&
718 		    t->restore_time.noatime == 0)
719 			flags |= O_NOATIME;
720 #endif
721 		t->entry_fd = open_on_current_dir(t,
722 		    tree_current_access_path(t), flags);
723 		__archive_ensure_cloexec_flag(t->entry_fd);
724 #if defined(O_NOATIME)
725 		/*
726 		 * When we did open the file with O_NOATIME flag,
727 		 * if successful, set 1 to t->restore_time.noatime
728 		 * not to restore an atime of the file later.
729 		 * if failed by EPERM, retry it without O_NOATIME flag.
730 		 */
731 		if (flags & O_NOATIME) {
732 			if (t->entry_fd >= 0)
733 				t->restore_time.noatime = 1;
734 			else if (errno == EPERM)
735 				flags &= ~O_NOATIME;
736 		}
737 #endif
738 		if (t->entry_fd < 0) {
739 			archive_set_error(&a->archive, errno,
740 			    "Couldn't open %s", tree_current_path(t));
741 			r = ARCHIVE_FAILED;
742 			tree_enter_initial_dir(t);
743 			goto abort_read_data;
744 		}
745 		tree_enter_initial_dir(t);
746 	}
747 
748 	/*
749 	 * Allocate read buffer if not allocated.
750 	 */
751 	if (t->current_filesystem->allocation_ptr == NULL) {
752 		r = setup_suitable_read_buffer(a);
753 		if (r != ARCHIVE_OK) {
754 			a->archive.state = ARCHIVE_STATE_FATAL;
755 			goto abort_read_data;
756 		}
757 	}
758 	t->entry_buff = t->current_filesystem->buff;
759 	t->entry_buff_size = t->current_filesystem->buff_size;
760 
761 	buffbytes = t->entry_buff_size;
762 	if ((int64_t)buffbytes > t->current_sparse->length)
763 		buffbytes = t->current_sparse->length;
764 
765 	if (t->current_sparse->length == 0)
766 		empty_sparse_region = 1;
767 
768 	/*
769 	 * Skip hole.
770 	 * TODO: Should we consider t->current_filesystem->xfer_align?
771 	 */
772 	if (t->current_sparse->offset > t->entry_total) {
773 		if (lseek(t->entry_fd,
774 		    (off_t)t->current_sparse->offset, SEEK_SET) !=
775 		    t->current_sparse->offset) {
776 			archive_set_error(&a->archive, errno, "Seek error");
777 			r = ARCHIVE_FATAL;
778 			a->archive.state = ARCHIVE_STATE_FATAL;
779 			goto abort_read_data;
780 		}
781 		sparse_bytes = t->current_sparse->offset - t->entry_total;
782 		t->entry_remaining_bytes -= sparse_bytes;
783 		t->entry_total += sparse_bytes;
784 	}
785 
786 	/*
787 	 * Read file contents.
788 	 */
789 	if (buffbytes > 0) {
790 		bytes = read(t->entry_fd, t->entry_buff, buffbytes);
791 		if (bytes < 0) {
792 			archive_set_error(&a->archive, errno, "Read error");
793 			r = ARCHIVE_FATAL;
794 			a->archive.state = ARCHIVE_STATE_FATAL;
795 			goto abort_read_data;
796 		}
797 	} else
798 		bytes = 0;
799 	/*
800 	 * Return an EOF unless we've read a leading empty sparse region, which
801 	 * is used to represent fully-sparse files.
802 	*/
803 	if (bytes == 0 && !empty_sparse_region) {
804 		/* Get EOF */
805 		t->entry_eof = 1;
806 		r = ARCHIVE_EOF;
807 		goto abort_read_data;
808 	}
809 	*buff = t->entry_buff;
810 	*size = bytes;
811 	*offset = t->entry_total;
812 	t->entry_total += bytes;
813 	t->entry_remaining_bytes -= bytes;
814 	if (t->entry_remaining_bytes == 0) {
815 		/* Close the current file descriptor */
816 		close_and_restore_time(t->entry_fd, t, &t->restore_time);
817 		t->entry_fd = -1;
818 		t->entry_eof = 1;
819 	}
820 	t->current_sparse->offset += bytes;
821 	t->current_sparse->length -= bytes;
822 	if (t->current_sparse->length == 0 && !t->entry_eof)
823 		t->current_sparse++;
824 	return (ARCHIVE_OK);
825 
826 abort_read_data:
827 	*buff = NULL;
828 	*size = 0;
829 	*offset = t->entry_total;
830 	if (t->entry_fd >= 0) {
831 		/* Close the current file descriptor */
832 		close_and_restore_time(t->entry_fd, t, &t->restore_time);
833 		t->entry_fd = -1;
834 	}
835 	return (r);
836 }
837 
838 static int
next_entry(struct archive_read_disk * a,struct tree * t,struct archive_entry * entry)839 next_entry(struct archive_read_disk *a, struct tree *t,
840     struct archive_entry *entry)
841 {
842 	const struct stat *st; /* info to use for this entry */
843 	const struct stat *lst;/* lstat() information */
844 	const char *name;
845 	int delayed, delayed_errno, descend, r;
846 	struct archive_string delayed_str;
847 
848 	delayed = ARCHIVE_OK;
849 	delayed_errno = 0;
850 	archive_string_init(&delayed_str);
851 
852 	st = NULL;
853 	lst = NULL;
854 	t->descend = 0;
855 	do {
856 		switch (tree_next(t)) {
857 		case TREE_ERROR_FATAL:
858 			archive_set_error(&a->archive, t->tree_errno,
859 			    "%s: Unable to continue traversing directory tree",
860 			    tree_current_path(t));
861 			a->archive.state = ARCHIVE_STATE_FATAL;
862 			tree_enter_initial_dir(t);
863 			return (ARCHIVE_FATAL);
864 		case TREE_ERROR_DIR:
865 			archive_set_error(&a->archive, t->tree_errno,
866 			    "%s: Couldn't visit directory",
867 			    tree_current_path(t));
868 			tree_enter_initial_dir(t);
869 			return (ARCHIVE_FAILED);
870 		case 0:
871 			tree_enter_initial_dir(t);
872 			return (ARCHIVE_EOF);
873 		case TREE_POSTDESCENT:
874 		case TREE_POSTASCENT:
875 			break;
876 		case TREE_REGULAR:
877 			lst = tree_current_lstat(t);
878 			if (lst == NULL) {
879 			    if (errno == ENOENT && t->depth > 0) {
880 				delayed = ARCHIVE_WARN;
881 				delayed_errno = errno;
882 				if (delayed_str.length == 0) {
883 					archive_string_sprintf(&delayed_str,
884 					    "%s", tree_current_path(t));
885 				} else {
886 					archive_string_sprintf(&delayed_str,
887 					    " %s", tree_current_path(t));
888 				}
889 			    } else {
890 				archive_set_error(&a->archive, errno,
891 				    "%s: Cannot stat",
892 				    tree_current_path(t));
893 				tree_enter_initial_dir(t);
894 				return (ARCHIVE_FAILED);
895 			    }
896 			}
897 			break;
898 		}
899 	} while (lst == NULL);
900 
901 #ifdef __APPLE__
902 	if (a->flags & ARCHIVE_READDISK_MAC_COPYFILE) {
903 		/* If we're using copyfile(), ignore "._XXX" files. */
904 		const char *bname = strrchr(tree_current_path(t), '/');
905 		if (bname == NULL)
906 			bname = tree_current_path(t);
907 		else
908 			++bname;
909 		if (bname[0] == '.' && bname[1] == '_')
910 			return (ARCHIVE_RETRY);
911 	}
912 #endif
913 
914 	archive_entry_copy_pathname(entry, tree_current_path(t));
915 	/*
916 	 * Perform path matching.
917 	 */
918 	if (a->matching) {
919 		r = archive_match_path_excluded(a->matching, entry);
920 		if (r < 0) {
921 			archive_set_error(&(a->archive), errno,
922 			    "Failed : %s", archive_error_string(a->matching));
923 			return (r);
924 		}
925 		if (r) {
926 			if (a->excluded_cb_func)
927 				a->excluded_cb_func(&(a->archive),
928 				    a->excluded_cb_data, entry);
929 			return (ARCHIVE_RETRY);
930 		}
931 	}
932 
933 	/*
934 	 * Distinguish 'L'/'P'/'H' symlink following.
935 	 */
936 	switch(t->symlink_mode) {
937 	case 'H':
938 		/* 'H': After the first item, rest like 'P'. */
939 		t->symlink_mode = 'P';
940 		/* 'H': First item (from command line) like 'L'. */
941 		/* FALLTHROUGH */
942 	case 'L':
943 		/* 'L': Do descend through a symlink to dir. */
944 		descend = tree_current_is_dir(t);
945 		/* 'L': Follow symlinks to files. */
946 		a->symlink_mode = 'L';
947 		a->follow_symlinks = 1;
948 		/* 'L': Archive symlinks as targets, if we can. */
949 		st = tree_current_stat(t);
950 		if (st != NULL && !tree_target_is_same_as_parent(t, st))
951 			break;
952 		/* If stat fails, we have a broken symlink;
953 		 * in that case, don't follow the link. */
954 		/* FALLTHROUGH */
955 	default:
956 		/* 'P': Don't descend through a symlink to dir. */
957 		descend = tree_current_is_physical_dir(t);
958 		/* 'P': Don't follow symlinks to files. */
959 		a->symlink_mode = 'P';
960 		a->follow_symlinks = 0;
961 		/* 'P': Archive symlinks as symlinks. */
962 		st = lst;
963 		break;
964 	}
965 
966 	if (update_current_filesystem(a, st->st_dev) != ARCHIVE_OK) {
967 		a->archive.state = ARCHIVE_STATE_FATAL;
968 		tree_enter_initial_dir(t);
969 		return (ARCHIVE_FATAL);
970 	}
971 	if (t->initial_filesystem_id == -1)
972 		t->initial_filesystem_id = t->current_filesystem_id;
973 	if (a->flags & ARCHIVE_READDISK_NO_TRAVERSE_MOUNTS) {
974 		if (t->initial_filesystem_id != t->current_filesystem_id)
975 			descend = 0;
976 	}
977 	t->descend = descend;
978 
979 	/*
980 	 * Honor nodump flag.
981 	 * If the file is marked with nodump flag, do not return this entry.
982 	 */
983 	if (a->flags & ARCHIVE_READDISK_HONOR_NODUMP) {
984 #if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP)
985 		if (st->st_flags & UF_NODUMP)
986 			return (ARCHIVE_RETRY);
987 #elif (defined(FS_IOC_GETFLAGS) && defined(FS_NODUMP_FL) && \
988        defined(HAVE_WORKING_FS_IOC_GETFLAGS)) || \
989       (defined(EXT2_IOC_GETFLAGS) && defined(EXT2_NODUMP_FL) && \
990        defined(HAVE_WORKING_EXT2_IOC_GETFLAGS))
991 		if (S_ISREG(st->st_mode) || S_ISDIR(st->st_mode)) {
992 			int stflags;
993 
994 			t->entry_fd = open_on_current_dir(t,
995 			    tree_current_access_path(t),
996 			    O_RDONLY | O_NONBLOCK | O_CLOEXEC);
997 			__archive_ensure_cloexec_flag(t->entry_fd);
998 			if (t->entry_fd >= 0) {
999 				r = ioctl(t->entry_fd,
1000 #ifdef FS_IOC_GETFLAGS
1001 				FS_IOC_GETFLAGS,
1002 #else
1003 				EXT2_IOC_GETFLAGS,
1004 #endif
1005 					&stflags);
1006 #ifdef FS_NODUMP_FL
1007 				if (r == 0 && (stflags & FS_NODUMP_FL) != 0)
1008 #else
1009 				if (r == 0 && (stflags & EXT2_NODUMP_FL) != 0)
1010 #endif
1011 					return (ARCHIVE_RETRY);
1012 			}
1013 		}
1014 #endif
1015 	}
1016 
1017 	archive_entry_copy_stat(entry, st);
1018 
1019 	/* Save the times to be restored. This must be in before
1020 	 * calling archive_read_disk_descend() or any chance of it,
1021 	 * especially, invoking a callback. */
1022 	t->restore_time.mtime = archive_entry_mtime(entry);
1023 	t->restore_time.mtime_nsec = archive_entry_mtime_nsec(entry);
1024 	t->restore_time.atime = archive_entry_atime(entry);
1025 	t->restore_time.atime_nsec = archive_entry_atime_nsec(entry);
1026 	t->restore_time.filetype = archive_entry_filetype(entry);
1027 	t->restore_time.noatime = t->current_filesystem->noatime;
1028 
1029 	/*
1030 	 * Perform time matching.
1031 	 */
1032 	if (a->matching) {
1033 		r = archive_match_time_excluded(a->matching, entry);
1034 		if (r < 0) {
1035 			archive_set_error(&(a->archive), errno,
1036 			    "Failed : %s", archive_error_string(a->matching));
1037 			return (r);
1038 		}
1039 		if (r) {
1040 			if (a->excluded_cb_func)
1041 				a->excluded_cb_func(&(a->archive),
1042 				    a->excluded_cb_data, entry);
1043 			return (ARCHIVE_RETRY);
1044 		}
1045 	}
1046 
1047 	/* Lookup uname/gname */
1048 	name = archive_read_disk_uname(&(a->archive), archive_entry_uid(entry));
1049 	if (name != NULL)
1050 		archive_entry_copy_uname(entry, name);
1051 	name = archive_read_disk_gname(&(a->archive), archive_entry_gid(entry));
1052 	if (name != NULL)
1053 		archive_entry_copy_gname(entry, name);
1054 
1055 	/*
1056 	 * Perform owner matching.
1057 	 */
1058 	if (a->matching) {
1059 		r = archive_match_owner_excluded(a->matching, entry);
1060 		if (r < 0) {
1061 			archive_set_error(&(a->archive), errno,
1062 			    "Failed : %s", archive_error_string(a->matching));
1063 			return (r);
1064 		}
1065 		if (r) {
1066 			if (a->excluded_cb_func)
1067 				a->excluded_cb_func(&(a->archive),
1068 				    a->excluded_cb_data, entry);
1069 			return (ARCHIVE_RETRY);
1070 		}
1071 	}
1072 
1073 	/*
1074 	 * Invoke a meta data filter callback.
1075 	 */
1076 	if (a->metadata_filter_func) {
1077 		if (!a->metadata_filter_func(&(a->archive),
1078 		    a->metadata_filter_data, entry))
1079 			return (ARCHIVE_RETRY);
1080 	}
1081 
1082 	/*
1083 	 * Populate the archive_entry with metadata from the disk.
1084 	 */
1085 	archive_entry_copy_sourcepath(entry, tree_current_access_path(t));
1086 	r = archive_read_disk_entry_from_file(&(a->archive), entry,
1087 		t->entry_fd, st);
1088 
1089 	if (r == ARCHIVE_OK) {
1090 		r = delayed;
1091 		if (r != ARCHIVE_OK) {
1092 			archive_string_sprintf(&delayed_str, ": %s",
1093 			    "File removed before we read it");
1094 			archive_set_error(&(a->archive), delayed_errno,
1095 			    "%s", delayed_str.s);
1096 		}
1097 	}
1098 	archive_string_free(&delayed_str);
1099 
1100 	return (r);
1101 }
1102 
1103 static int
_archive_read_next_header(struct archive * _a,struct archive_entry ** entryp)1104 _archive_read_next_header(struct archive *_a, struct archive_entry **entryp)
1105 {
1106 	int ret;
1107 	struct archive_read_disk *a = (struct archive_read_disk *)_a;
1108 	*entryp = NULL;
1109 	ret = _archive_read_next_header2(_a, a->entry);
1110 	*entryp = a->entry;
1111 	return ret;
1112 }
1113 
1114 static int
_archive_read_next_header2(struct archive * _a,struct archive_entry * entry)1115 _archive_read_next_header2(struct archive *_a, struct archive_entry *entry)
1116 {
1117 	struct archive_read_disk *a = (struct archive_read_disk *)_a;
1118 	struct tree *t;
1119 	int r;
1120 
1121 	archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
1122 	    ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
1123 	    "archive_read_next_header2");
1124 
1125 	t = a->tree;
1126 	if (t->entry_fd >= 0) {
1127 		close_and_restore_time(t->entry_fd, t, &t->restore_time);
1128 		t->entry_fd = -1;
1129 	}
1130 
1131 	archive_entry_clear(entry);
1132 
1133 	for (;;) {
1134 		r = next_entry(a, t, entry);
1135 		if (t->entry_fd >= 0) {
1136 			close(t->entry_fd);
1137 			t->entry_fd = -1;
1138 		}
1139 
1140 		if (r == ARCHIVE_RETRY) {
1141 			archive_entry_clear(entry);
1142 			continue;
1143 		}
1144 		break;
1145 	}
1146 
1147 	/* Return to the initial directory. */
1148 	tree_enter_initial_dir(t);
1149 
1150 	/*
1151 	 * EOF and FATAL are persistent at this layer.  By
1152 	 * modifying the state, we guarantee that future calls to
1153 	 * read a header or read data will fail.
1154 	 */
1155 	switch (r) {
1156 	case ARCHIVE_EOF:
1157 		a->archive.state = ARCHIVE_STATE_EOF;
1158 		break;
1159 	case ARCHIVE_OK:
1160 	case ARCHIVE_WARN:
1161 		/* Overwrite the sourcepath based on the initial directory. */
1162 		archive_entry_copy_sourcepath(entry, tree_current_path(t));
1163 		t->entry_total = 0;
1164 		if (archive_entry_filetype(entry) == AE_IFREG) {
1165 			t->nlink = archive_entry_nlink(entry);
1166 			t->entry_remaining_bytes = archive_entry_size(entry);
1167 			t->entry_eof = (t->entry_remaining_bytes == 0)? 1: 0;
1168 			if (!t->entry_eof &&
1169 			    setup_sparse(a, entry) != ARCHIVE_OK)
1170 				return (ARCHIVE_FATAL);
1171 		} else {
1172 			t->entry_remaining_bytes = 0;
1173 			t->entry_eof = 1;
1174 		}
1175 		a->archive.state = ARCHIVE_STATE_DATA;
1176 		break;
1177 	case ARCHIVE_RETRY:
1178 		break;
1179 	case ARCHIVE_FATAL:
1180 		a->archive.state = ARCHIVE_STATE_FATAL;
1181 		break;
1182 	}
1183 
1184 	__archive_reset_read_data(&a->archive);
1185 	return (r);
1186 }
1187 
1188 static int
setup_sparse(struct archive_read_disk * a,struct archive_entry * entry)1189 setup_sparse(struct archive_read_disk *a, struct archive_entry *entry)
1190 {
1191 	struct tree *t = a->tree;
1192 	int64_t length, offset;
1193 	int i;
1194 
1195 	t->sparse_count = archive_entry_sparse_reset(entry);
1196 	if (t->sparse_count+1 > t->sparse_list_size) {
1197 		free(t->sparse_list);
1198 		t->sparse_list_size = t->sparse_count + 1;
1199 		t->sparse_list = malloc(sizeof(t->sparse_list[0]) *
1200 		    t->sparse_list_size);
1201 		if (t->sparse_list == NULL) {
1202 			t->sparse_list_size = 0;
1203 			archive_set_error(&a->archive, ENOMEM,
1204 			    "Can't allocate data");
1205 			a->archive.state = ARCHIVE_STATE_FATAL;
1206 			return (ARCHIVE_FATAL);
1207 		}
1208 	}
1209 	for (i = 0; i < t->sparse_count; i++) {
1210 		archive_entry_sparse_next(entry, &offset, &length);
1211 		t->sparse_list[i].offset = offset;
1212 		t->sparse_list[i].length = length;
1213 	}
1214 	if (i == 0) {
1215 		t->sparse_list[i].offset = 0;
1216 		t->sparse_list[i].length = archive_entry_size(entry);
1217 	} else {
1218 		t->sparse_list[i].offset = archive_entry_size(entry);
1219 		t->sparse_list[i].length = 0;
1220 	}
1221 	t->current_sparse = t->sparse_list;
1222 
1223 	return (ARCHIVE_OK);
1224 }
1225 
1226 int
archive_read_disk_set_matching(struct archive * _a,struct archive * _ma,void (* _excluded_func)(struct archive *,void *,struct archive_entry *),void * _client_data)1227 archive_read_disk_set_matching(struct archive *_a, struct archive *_ma,
1228     void (*_excluded_func)(struct archive *, void *, struct archive_entry *),
1229     void *_client_data)
1230 {
1231 	struct archive_read_disk *a = (struct archive_read_disk *)_a;
1232 	archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
1233 	    ARCHIVE_STATE_ANY, "archive_read_disk_set_matching");
1234 	a->matching = _ma;
1235 	a->excluded_cb_func = _excluded_func;
1236 	a->excluded_cb_data = _client_data;
1237 	return (ARCHIVE_OK);
1238 }
1239 
1240 int
archive_read_disk_set_metadata_filter_callback(struct archive * _a,int (* _metadata_filter_func)(struct archive *,void *,struct archive_entry *),void * _client_data)1241 archive_read_disk_set_metadata_filter_callback(struct archive *_a,
1242     int (*_metadata_filter_func)(struct archive *, void *,
1243     struct archive_entry *), void *_client_data)
1244 {
1245 	struct archive_read_disk *a = (struct archive_read_disk *)_a;
1246 
1247 	archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_ANY,
1248 	    "archive_read_disk_set_metadata_filter_callback");
1249 
1250 	a->metadata_filter_func = _metadata_filter_func;
1251 	a->metadata_filter_data = _client_data;
1252 	return (ARCHIVE_OK);
1253 }
1254 
1255 int
archive_read_disk_can_descend(struct archive * _a)1256 archive_read_disk_can_descend(struct archive *_a)
1257 {
1258 	struct archive_read_disk *a = (struct archive_read_disk *)_a;
1259 	struct tree *t = a->tree;
1260 
1261 	archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
1262 	    ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
1263 	    "archive_read_disk_can_descend");
1264 
1265 	return (t->visit_type == TREE_REGULAR && t->descend);
1266 }
1267 
1268 /*
1269  * Called by the client to mark the directory just returned from
1270  * tree_next() as needing to be visited.
1271  */
1272 int
archive_read_disk_descend(struct archive * _a)1273 archive_read_disk_descend(struct archive *_a)
1274 {
1275 	struct archive_read_disk *a = (struct archive_read_disk *)_a;
1276 	struct tree *t = a->tree;
1277 
1278 	archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
1279 	    ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
1280 	    "archive_read_disk_descend");
1281 
1282 	if (!archive_read_disk_can_descend(_a))
1283 		return (ARCHIVE_OK);
1284 
1285 	/*
1286 	 * We must not treat the initial specified path as a physical dir,
1287 	 * because if we do then we will try and ascend out of it by opening
1288 	 * ".." which is (a) wrong and (b) causes spurious permissions errors
1289 	 * if ".." is not readable by us. Instead, treat it as if it were a
1290 	 * symlink. (This uses an extra fd, but it can only happen once at the
1291 	 * top level of a traverse.) But we can't necessarily assume t->st is
1292 	 * valid here (though t->lst is), which complicates the logic a
1293 	 * little.
1294 	 */
1295 	if (tree_current_is_physical_dir(t)) {
1296 		tree_push(t, t->basename, t->current_filesystem_id,
1297 		    t->lst.st_dev, t->lst.st_ino, &t->restore_time);
1298 		if (t->stack->parent->parent != NULL)
1299 			t->stack->flags |= isDir;
1300 		else
1301 			t->stack->flags |= isDirLink;
1302 	} else if (tree_current_is_dir(t)) {
1303 		tree_push(t, t->basename, t->current_filesystem_id,
1304 		    t->st.st_dev, t->st.st_ino, &t->restore_time);
1305 		t->stack->flags |= isDirLink;
1306 	}
1307 	t->descend = 0;
1308 	return (ARCHIVE_OK);
1309 }
1310 
1311 int
archive_read_disk_open(struct archive * _a,const char * pathname)1312 archive_read_disk_open(struct archive *_a, const char *pathname)
1313 {
1314 	struct archive_read_disk *a = (struct archive_read_disk *)_a;
1315 
1316 	archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
1317 	    ARCHIVE_STATE_NEW | ARCHIVE_STATE_CLOSED,
1318 	    "archive_read_disk_open");
1319 	archive_clear_error(&a->archive);
1320 
1321 	return (_archive_read_disk_open(_a, pathname));
1322 }
1323 
1324 int
archive_read_disk_open_w(struct archive * _a,const wchar_t * pathname)1325 archive_read_disk_open_w(struct archive *_a, const wchar_t *pathname)
1326 {
1327 	struct archive_read_disk *a = (struct archive_read_disk *)_a;
1328 	struct archive_string path;
1329 	int ret;
1330 
1331 	archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
1332 	    ARCHIVE_STATE_NEW | ARCHIVE_STATE_CLOSED,
1333 	    "archive_read_disk_open_w");
1334 	archive_clear_error(&a->archive);
1335 
1336 	/* Make a char string from a wchar_t string. */
1337 	archive_string_init(&path);
1338 	if (archive_string_append_from_wcs(&path, pathname,
1339 	    wcslen(pathname)) != 0) {
1340 		if (errno == ENOMEM)
1341 			archive_set_error(&a->archive, ENOMEM,
1342 			    "Can't allocate memory");
1343 		else
1344 			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1345 			    "Can't convert a path to a char string");
1346 		a->archive.state = ARCHIVE_STATE_FATAL;
1347 		ret = ARCHIVE_FATAL;
1348 	} else
1349 		ret = _archive_read_disk_open(_a, path.s);
1350 
1351 	archive_string_free(&path);
1352 	return (ret);
1353 }
1354 
1355 static int
_archive_read_disk_open(struct archive * _a,const char * pathname)1356 _archive_read_disk_open(struct archive *_a, const char *pathname)
1357 {
1358 	struct archive_read_disk *a = (struct archive_read_disk *)_a;
1359 
1360 	if (a->tree != NULL)
1361 		a->tree = tree_reopen(a->tree, pathname,
1362 		    a->flags & ARCHIVE_READDISK_RESTORE_ATIME);
1363 	else
1364 		a->tree = tree_open(pathname, a->symlink_mode,
1365 		    a->flags & ARCHIVE_READDISK_RESTORE_ATIME);
1366 	if (a->tree == NULL) {
1367 		archive_set_error(&a->archive, ENOMEM,
1368 		    "Can't allocate tar data");
1369 		a->archive.state = ARCHIVE_STATE_FATAL;
1370 		return (ARCHIVE_FATAL);
1371 	}
1372 	a->archive.state = ARCHIVE_STATE_HEADER;
1373 
1374 	return (ARCHIVE_OK);
1375 }
1376 
1377 /*
1378  * Return a current filesystem ID which is index of the filesystem entry
1379  * you've visited through archive_read_disk.
1380  */
1381 int
archive_read_disk_current_filesystem(struct archive * _a)1382 archive_read_disk_current_filesystem(struct archive *_a)
1383 {
1384 	struct archive_read_disk *a = (struct archive_read_disk *)_a;
1385 
1386 	archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA,
1387 	    "archive_read_disk_current_filesystem");
1388 
1389 	return (a->tree->current_filesystem_id);
1390 }
1391 
1392 static int
update_current_filesystem(struct archive_read_disk * a,int64_t dev)1393 update_current_filesystem(struct archive_read_disk *a, int64_t dev)
1394 {
1395 	struct tree *t = a->tree;
1396 	int i, fid;
1397 
1398 	if (t->current_filesystem != NULL &&
1399 	    t->current_filesystem->dev == dev)
1400 		return (ARCHIVE_OK);
1401 
1402 	for (i = 0; i < t->max_filesystem_id; i++) {
1403 		if (t->filesystem_table[i].dev == dev) {
1404 			/* There is the filesystem ID we've already generated. */
1405 			t->current_filesystem_id = i;
1406 			t->current_filesystem = &(t->filesystem_table[i]);
1407 			return (ARCHIVE_OK);
1408 		}
1409 	}
1410 
1411 	/*
1412 	 * This is the new filesystem which we have to generate a new ID for.
1413 	 */
1414 	fid = t->max_filesystem_id++;
1415 	if (t->max_filesystem_id > t->allocated_filesystem) {
1416 		size_t s;
1417 		void *p;
1418 
1419 		s = t->max_filesystem_id * 2;
1420 		p = realloc(t->filesystem_table,
1421 		        s * sizeof(*t->filesystem_table));
1422 		if (p == NULL) {
1423 			archive_set_error(&a->archive, ENOMEM,
1424 			    "Can't allocate tar data");
1425 			return (ARCHIVE_FATAL);
1426 		}
1427 		t->filesystem_table = (struct filesystem *)p;
1428 		t->allocated_filesystem = s;
1429 	}
1430 	t->current_filesystem_id = fid;
1431 	t->current_filesystem = &(t->filesystem_table[fid]);
1432 	t->current_filesystem->dev = dev;
1433 	t->current_filesystem->allocation_ptr = NULL;
1434 	t->current_filesystem->buff = NULL;
1435 
1436 	/* Setup the current filesystem properties which depend on
1437 	 * platform specific. */
1438 	return (setup_current_filesystem(a));
1439 }
1440 
1441 /*
1442  * Returns 1 if current filesystem is generated filesystem, 0 if it is not
1443  * or -1 if it is unknown.
1444  */
1445 int
archive_read_disk_current_filesystem_is_synthetic(struct archive * _a)1446 archive_read_disk_current_filesystem_is_synthetic(struct archive *_a)
1447 {
1448 	struct archive_read_disk *a = (struct archive_read_disk *)_a;
1449 
1450 	archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA,
1451 	    "archive_read_disk_current_filesystem");
1452 
1453 	return (a->tree->current_filesystem->synthetic);
1454 }
1455 
1456 /*
1457  * Returns 1 if current filesystem is remote filesystem, 0 if it is not
1458  * or -1 if it is unknown.
1459  */
1460 int
archive_read_disk_current_filesystem_is_remote(struct archive * _a)1461 archive_read_disk_current_filesystem_is_remote(struct archive *_a)
1462 {
1463 	struct archive_read_disk *a = (struct archive_read_disk *)_a;
1464 
1465 	archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA,
1466 	    "archive_read_disk_current_filesystem");
1467 
1468 	return (a->tree->current_filesystem->remote);
1469 }
1470 
1471 #if defined(_PC_REC_INCR_XFER_SIZE) && defined(_PC_REC_MAX_XFER_SIZE) &&\
1472 	defined(_PC_REC_MIN_XFER_SIZE) && defined(_PC_REC_XFER_ALIGN)
1473 static int
get_xfer_size(struct tree * t,int fd,const char * path)1474 get_xfer_size(struct tree *t, int fd, const char *path)
1475 {
1476 	t->current_filesystem->xfer_align = -1;
1477 	errno = 0;
1478 	if (fd >= 0) {
1479 		t->current_filesystem->incr_xfer_size =
1480 		    fpathconf(fd, _PC_REC_INCR_XFER_SIZE);
1481 		t->current_filesystem->max_xfer_size =
1482 		    fpathconf(fd, _PC_REC_MAX_XFER_SIZE);
1483 		t->current_filesystem->min_xfer_size =
1484 		    fpathconf(fd, _PC_REC_MIN_XFER_SIZE);
1485 		t->current_filesystem->xfer_align =
1486 		    fpathconf(fd, _PC_REC_XFER_ALIGN);
1487 	} else if (path != NULL) {
1488 		t->current_filesystem->incr_xfer_size =
1489 		    pathconf(path, _PC_REC_INCR_XFER_SIZE);
1490 		t->current_filesystem->max_xfer_size =
1491 		    pathconf(path, _PC_REC_MAX_XFER_SIZE);
1492 		t->current_filesystem->min_xfer_size =
1493 		    pathconf(path, _PC_REC_MIN_XFER_SIZE);
1494 		t->current_filesystem->xfer_align =
1495 		    pathconf(path, _PC_REC_XFER_ALIGN);
1496 	}
1497 	/* At least we need an alignment size. */
1498 	if (t->current_filesystem->xfer_align == -1)
1499 		return ((errno == EINVAL)?1:-1);
1500 	else
1501 		return (0);
1502 }
1503 #else
1504 static int
get_xfer_size(struct tree * t,int fd,const char * path)1505 get_xfer_size(struct tree *t, int fd, const char *path)
1506 {
1507 	(void)t; /* UNUSED */
1508 	(void)fd; /* UNUSED */
1509 	(void)path; /* UNUSED */
1510 	return (1);/* Not supported */
1511 }
1512 #endif
1513 
1514 #if defined(HAVE_STATVFS)
1515 static inline __LA_UNUSED void
set_statvfs_transfer_size(struct filesystem * fs,const struct statvfs * sfs)1516 set_statvfs_transfer_size(struct filesystem *fs, const struct statvfs *sfs)
1517 {
1518 	fs->xfer_align = sfs->f_frsize > 0 ? (long)sfs->f_frsize : -1;
1519 	fs->max_xfer_size = -1;
1520 #if defined(HAVE_STRUCT_STATVFS_F_IOSIZE)
1521 	fs->min_xfer_size = sfs->f_iosize > 0 ? (long)sfs->f_iosize : -1;
1522 	fs->incr_xfer_size = sfs->f_iosize > 0 ? (long)sfs->f_iosize : -1;
1523 #else
1524 	fs->min_xfer_size = sfs->f_bsize > 0 ? (long)sfs->f_bsize : -1;
1525 	fs->incr_xfer_size = sfs->f_bsize > 0 ? (long)sfs->f_bsize : -1;
1526 #endif
1527 }
1528 #endif
1529 
1530 #if defined(HAVE_STRUCT_STATFS)
1531 static inline __LA_UNUSED void
set_statfs_transfer_size(struct filesystem * fs,const struct statfs * sfs)1532 set_statfs_transfer_size(struct filesystem *fs, const struct statfs *sfs)
1533 {
1534 	fs->xfer_align = sfs->f_bsize > 0 ? (long)sfs->f_bsize : -1;
1535 	fs->max_xfer_size = -1;
1536 #if defined(HAVE_STRUCT_STATFS_F_IOSIZE)
1537 	fs->min_xfer_size = sfs->f_iosize > 0 ? (long)sfs->f_iosize : -1;
1538 	fs->incr_xfer_size = sfs->f_iosize > 0 ? (long)sfs->f_iosize : -1;
1539 #else
1540 	fs->min_xfer_size = sfs->f_bsize > 0 ? (long)sfs->f_bsize : -1;
1541 	fs->incr_xfer_size = sfs->f_bsize > 0 ? (long)sfs->f_bsize : -1;
1542 #endif
1543 }
1544 #endif
1545 
1546 #if defined(HAVE_STRUCT_STATFS) && defined(HAVE_STATFS) && \
1547     defined(HAVE_FSTATFS) && defined(MNT_LOCAL) && !defined(ST_LOCAL)
1548 
1549 /*
1550  * Gather current filesystem properties on FreeBSD, OpenBSD and Mac OS X.
1551  */
1552 static int
setup_current_filesystem(struct archive_read_disk * a)1553 setup_current_filesystem(struct archive_read_disk *a)
1554 {
1555 	struct tree *t = a->tree;
1556 	struct statfs sfs;
1557 #if defined(HAVE_GETVFSBYNAME) && defined(VFCF_SYNTHETIC)
1558 /* TODO: configure should set GETVFSBYNAME_ARG_TYPE to make
1559  * this accurate; some platforms have both and we need the one that's
1560  * used by getvfsbyname()
1561  *
1562  * Then the following would become:
1563  *  #if defined(GETVFSBYNAME_ARG_TYPE)
1564  *   GETVFSBYNAME_ARG_TYPE vfc;
1565  *  #endif
1566  */
1567 #  if defined(HAVE_STRUCT_XVFSCONF)
1568 	struct xvfsconf vfc;
1569 #  else
1570 	struct vfsconf vfc;
1571 #  endif
1572 #endif
1573 	int r, xr = 0;
1574 
1575 	t->current_filesystem->synthetic = -1;
1576 	t->current_filesystem->remote = -1;
1577 	if (tree_current_is_symblic_link_target(t)) {
1578 #if defined(HAVE_OPENAT)
1579 		/*
1580 		 * Get file system statistics on any directory
1581 		 * where current is.
1582 		 */
1583 		int fd = openat(tree_current_dir_fd(t),
1584 		    tree_current_access_path(t), O_RDONLY | O_CLOEXEC);
1585 		__archive_ensure_cloexec_flag(fd);
1586 		if (fd < 0) {
1587 			archive_set_error(&a->archive, errno,
1588 			    "openat failed");
1589 			return (ARCHIVE_FAILED);
1590 		}
1591 		r = fstatfs(fd, &sfs);
1592 		if (r == 0)
1593 			xr = get_xfer_size(t, fd, NULL);
1594 		close(fd);
1595 #else
1596 		if (tree_enter_working_dir(t) != 0) {
1597 			archive_set_error(&a->archive, errno, "fchdir failed");
1598 			return (ARCHIVE_FAILED);
1599 		}
1600 		r = statfs(tree_current_access_path(t), &sfs);
1601 		if (r == 0)
1602 			xr = get_xfer_size(t, -1, tree_current_access_path(t));
1603 #endif
1604 	} else {
1605 		r = fstatfs(tree_current_dir_fd(t), &sfs);
1606 		if (r == 0)
1607 			xr = get_xfer_size(t, tree_current_dir_fd(t), NULL);
1608 	}
1609 	if (r == -1 || xr == -1) {
1610 		archive_set_error(&a->archive, errno, "statfs failed");
1611 		return (ARCHIVE_FAILED);
1612 	} else if (xr == 1) {
1613 		/* pathconf(_PC_REX_*) operations are not supported. */
1614 		set_statfs_transfer_size(t->current_filesystem, &sfs);
1615 	}
1616 	if (sfs.f_flags & MNT_LOCAL)
1617 		t->current_filesystem->remote = 0;
1618 	else
1619 		t->current_filesystem->remote = 1;
1620 
1621 #if defined(HAVE_GETVFSBYNAME) && defined(VFCF_SYNTHETIC)
1622 	r = getvfsbyname(sfs.f_fstypename, &vfc);
1623 	if (r == -1) {
1624 		archive_set_error(&a->archive, errno, "getvfsbyname failed");
1625 		return (ARCHIVE_FAILED);
1626 	}
1627 	if (vfc.vfc_flags & VFCF_SYNTHETIC)
1628 		t->current_filesystem->synthetic = 1;
1629 	else
1630 		t->current_filesystem->synthetic = 0;
1631 #endif
1632 
1633 #if defined(MNT_NOATIME)
1634 	if (sfs.f_flags & MNT_NOATIME)
1635 		t->current_filesystem->noatime = 1;
1636 	else
1637 #endif
1638 		t->current_filesystem->noatime = 0;
1639 
1640 	return (ARCHIVE_OK);
1641 }
1642 
1643 #elif (defined(HAVE_STATVFS) || defined(HAVE_FSTATVFS)) && defined(ST_LOCAL)
1644 
1645 /*
1646  * Gather current filesystem properties on NetBSD
1647  */
1648 static int
setup_current_filesystem(struct archive_read_disk * a)1649 setup_current_filesystem(struct archive_read_disk *a)
1650 {
1651 	struct tree *t = a->tree;
1652 	struct statvfs svfs;
1653 	int r, xr = 0;
1654 
1655 	t->current_filesystem->synthetic = -1;
1656 	if (tree_enter_working_dir(t) != 0) {
1657 		archive_set_error(&a->archive, errno, "fchdir failed");
1658 		return (ARCHIVE_FAILED);
1659 	}
1660 	if (tree_current_is_symblic_link_target(t)) {
1661 		r = statvfs(tree_current_access_path(t), &svfs);
1662 		if (r == 0)
1663 			xr = get_xfer_size(t, -1, tree_current_access_path(t));
1664 	} else {
1665 #ifdef HAVE_FSTATVFS
1666 		r = fstatvfs(tree_current_dir_fd(t), &svfs);
1667 		if (r == 0)
1668 			xr = get_xfer_size(t, tree_current_dir_fd(t), NULL);
1669 #else
1670 		r = statvfs(".", &svfs);
1671 		if (r == 0)
1672 			xr = get_xfer_size(t, -1, ".");
1673 #endif
1674 	}
1675 	if (r == -1 || xr == -1) {
1676 		t->current_filesystem->remote = -1;
1677 		archive_set_error(&a->archive, errno, "statvfs failed");
1678 		return (ARCHIVE_FAILED);
1679 	} else if (xr == 1) {
1680 		/* Usually come here unless NetBSD supports _PC_REC_XFER_ALIGN
1681 		 * for pathconf() function. */
1682 		set_statvfs_transfer_size(t->current_filesystem, &svfs);
1683 	}
1684 	if (svfs.f_flag & ST_LOCAL)
1685 		t->current_filesystem->remote = 0;
1686 	else
1687 		t->current_filesystem->remote = 1;
1688 
1689 #if defined(ST_NOATIME)
1690 	if (svfs.f_flag & ST_NOATIME)
1691 		t->current_filesystem->noatime = 1;
1692 	else
1693 #endif
1694 		t->current_filesystem->noatime = 0;
1695 
1696 	/* Set maximum filename length. */
1697 	t->current_filesystem->name_max = svfs.f_namemax;
1698 	return (ARCHIVE_OK);
1699 }
1700 
1701 #elif defined(HAVE_SYS_STATFS_H) && defined(HAVE_LINUX_MAGIC_H) &&\
1702 	defined(HAVE_STATFS) && defined(HAVE_FSTATFS)
1703 /*
1704  * Note: statfs is deprecated since LSB 3.2
1705  */
1706 
1707 #ifndef CIFS_SUPER_MAGIC
1708 #define CIFS_SUPER_MAGIC 0xFF534D42
1709 #endif
1710 #ifndef DEVFS_SUPER_MAGIC
1711 #define DEVFS_SUPER_MAGIC 0x1373
1712 #endif
1713 
1714 /*
1715  * Gather current filesystem properties on Linux
1716  */
1717 static int
setup_current_filesystem(struct archive_read_disk * a)1718 setup_current_filesystem(struct archive_read_disk *a)
1719 {
1720 	struct tree *t = a->tree;
1721 	struct statfs sfs;
1722 #if defined(HAVE_STATVFS)
1723 	struct statvfs svfs;
1724 #endif
1725 	int r, vr = 0, xr = 0;
1726 
1727 	if (tree_current_is_symblic_link_target(t)) {
1728 #if defined(HAVE_OPENAT)
1729 		/*
1730 		 * Get file system statistics on any directory
1731 		 * where current is.
1732 		 */
1733 		int fd = openat(tree_current_dir_fd(t),
1734 		    tree_current_access_path(t), O_RDONLY | O_CLOEXEC);
1735 		__archive_ensure_cloexec_flag(fd);
1736 		if (fd < 0) {
1737 			archive_set_error(&a->archive, errno,
1738 			    "openat failed");
1739 			return (ARCHIVE_FAILED);
1740 		}
1741 #if defined(HAVE_FSTATVFS)
1742 		vr = fstatvfs(fd, &svfs);/* for f_flag, mount flags */
1743 #endif
1744 		r = fstatfs(fd, &sfs);
1745 		if (r == 0)
1746 			xr = get_xfer_size(t, fd, NULL);
1747 		close(fd);
1748 #else
1749 		if (tree_enter_working_dir(t) != 0) {
1750 			archive_set_error(&a->archive, errno, "fchdir failed");
1751 			return (ARCHIVE_FAILED);
1752 		}
1753 #if defined(HAVE_STATVFS)
1754 		vr = statvfs(tree_current_access_path(t), &svfs);
1755 #endif
1756 		r = statfs(tree_current_access_path(t), &sfs);
1757 		if (r == 0)
1758 			xr = get_xfer_size(t, -1, tree_current_access_path(t));
1759 #endif
1760 	} else {
1761 #ifdef HAVE_FSTATFS
1762 #if defined(HAVE_FSTATVFS)
1763 		vr = fstatvfs(tree_current_dir_fd(t), &svfs);
1764 #endif
1765 		r = fstatfs(tree_current_dir_fd(t), &sfs);
1766 		if (r == 0)
1767 			xr = get_xfer_size(t, tree_current_dir_fd(t), NULL);
1768 #else
1769 		if (tree_enter_working_dir(t) != 0) {
1770 			archive_set_error(&a->archive, errno, "fchdir failed");
1771 			return (ARCHIVE_FAILED);
1772 		}
1773 #if defined(HAVE_STATVFS)
1774 		vr = statvfs(".", &svfs);
1775 #endif
1776 		r = statfs(".", &sfs);
1777 		if (r == 0)
1778 			xr = get_xfer_size(t, -1, ".");
1779 #endif
1780 	}
1781 	if (r == -1 || xr == -1 || vr == -1) {
1782 		t->current_filesystem->synthetic = -1;
1783 		t->current_filesystem->remote = -1;
1784 		archive_set_error(&a->archive, errno, "statfs failed");
1785 		return (ARCHIVE_FAILED);
1786 	} else if (xr == 1) {
1787 		/* pathconf(_PC_REX_*) operations are not supported. */
1788 #if defined(HAVE_STATVFS)
1789 		set_statvfs_transfer_size(t->current_filesystem, &svfs);
1790 #else
1791 		set_statfs_transfer_size(t->current_filesystem, &sfs);
1792 #endif
1793 	}
1794 	switch (sfs.f_type) {
1795 	case AFS_SUPER_MAGIC:
1796 	case CIFS_SUPER_MAGIC:
1797 	case CODA_SUPER_MAGIC:
1798 	case NCP_SUPER_MAGIC:/* NetWare */
1799 	case NFS_SUPER_MAGIC:
1800 	case SMB_SUPER_MAGIC:
1801 		t->current_filesystem->remote = 1;
1802 		t->current_filesystem->synthetic = 0;
1803 		break;
1804 	case DEVFS_SUPER_MAGIC:
1805 	case PROC_SUPER_MAGIC:
1806 	case USBDEVICE_SUPER_MAGIC:
1807 		t->current_filesystem->remote = 0;
1808 		t->current_filesystem->synthetic = 1;
1809 		break;
1810 	default:
1811 		t->current_filesystem->remote = 0;
1812 		t->current_filesystem->synthetic = 0;
1813 		break;
1814 	}
1815 
1816 #if defined(ST_NOATIME)
1817 #if defined(HAVE_STATVFS)
1818 	if (svfs.f_flag & ST_NOATIME)
1819 #else
1820 	if (sfs.f_flags & ST_NOATIME)
1821 #endif
1822 		t->current_filesystem->noatime = 1;
1823 	else
1824 #endif
1825 		t->current_filesystem->noatime = 0;
1826 
1827 	return (ARCHIVE_OK);
1828 }
1829 
1830 #elif defined(HAVE_SYS_STATVFS_H) &&\
1831 	(defined(HAVE_STATVFS) || defined(HAVE_FSTATVFS))
1832 
1833 /*
1834  * Gather current filesystem properties on other posix platform.
1835  */
1836 static int
setup_current_filesystem(struct archive_read_disk * a)1837 setup_current_filesystem(struct archive_read_disk *a)
1838 {
1839 	struct tree *t = a->tree;
1840 	struct statvfs svfs;
1841 	int r, xr = 0;
1842 
1843 	t->current_filesystem->synthetic = -1;/* Not supported */
1844 	t->current_filesystem->remote = -1;/* Not supported */
1845 	if (tree_current_is_symblic_link_target(t)) {
1846 #if defined(HAVE_OPENAT)
1847 		/*
1848 		 * Get file system statistics on any directory
1849 		 * where current is.
1850 		 */
1851 		int fd = openat(tree_current_dir_fd(t),
1852 		    tree_current_access_path(t), O_RDONLY | O_CLOEXEC);
1853 		__archive_ensure_cloexec_flag(fd);
1854 		if (fd < 0) {
1855 			archive_set_error(&a->archive, errno,
1856 			    "openat failed");
1857 			return (ARCHIVE_FAILED);
1858 		}
1859 		r = fstatvfs(fd, &svfs);
1860 		if (r == 0)
1861 			xr = get_xfer_size(t, fd, NULL);
1862 		close(fd);
1863 #else
1864 		if (tree_enter_working_dir(t) != 0) {
1865 			archive_set_error(&a->archive, errno, "fchdir failed");
1866 			return (ARCHIVE_FAILED);
1867 		}
1868 		r = statvfs(tree_current_access_path(t), &svfs);
1869 		if (r == 0)
1870 			xr = get_xfer_size(t, -1, tree_current_access_path(t));
1871 #endif
1872 	} else {
1873 #ifdef HAVE_FSTATVFS
1874 		r = fstatvfs(tree_current_dir_fd(t), &svfs);
1875 		if (r == 0)
1876 			xr = get_xfer_size(t, tree_current_dir_fd(t), NULL);
1877 #else
1878 		if (tree_enter_working_dir(t) != 0) {
1879 			archive_set_error(&a->archive, errno, "fchdir failed");
1880 			return (ARCHIVE_FAILED);
1881 		}
1882 		r = statvfs(".", &svfs);
1883 		if (r == 0)
1884 			xr = get_xfer_size(t, -1, ".");
1885 #endif
1886 	}
1887 	if (r == -1 || xr == -1) {
1888 		t->current_filesystem->synthetic = -1;
1889 		t->current_filesystem->remote = -1;
1890 		archive_set_error(&a->archive, errno, "statvfs failed");
1891 		return (ARCHIVE_FAILED);
1892 	} else if (xr == 1) {
1893 		/* pathconf(_PC_REX_*) operations are not supported. */
1894 		set_statvfs_transfer_size(t->current_filesystem, &svfs);
1895 	}
1896 
1897 #if defined(ST_NOATIME)
1898 	if (svfs.f_flag & ST_NOATIME)
1899 		t->current_filesystem->noatime = 1;
1900 	else
1901 #endif
1902 		t->current_filesystem->noatime = 0;
1903 
1904 	return (ARCHIVE_OK);
1905 }
1906 
1907 #else
1908 
1909 /*
1910  * Generic: Gather current filesystem properties.
1911  * TODO: Is this generic function really needed?
1912  */
1913 static int
setup_current_filesystem(struct archive_read_disk * a)1914 setup_current_filesystem(struct archive_read_disk *a)
1915 {
1916 	struct tree *t = a->tree;
1917 	t->current_filesystem->synthetic = -1;/* Not supported */
1918 	t->current_filesystem->remote = -1;/* Not supported */
1919 	t->current_filesystem->noatime = 0;
1920 	(void)get_xfer_size(t, -1, ".");/* Dummy call to avoid build error. */
1921 	t->current_filesystem->xfer_align = -1;/* Unknown */
1922 	t->current_filesystem->max_xfer_size = -1;
1923 	t->current_filesystem->min_xfer_size = -1;
1924 	t->current_filesystem->incr_xfer_size = -1;
1925 
1926 	return (ARCHIVE_OK);
1927 }
1928 
1929 #endif
1930 
1931 static int
close_and_restore_time(int fd,struct tree * t,struct restore_time * rt)1932 close_and_restore_time(int fd, struct tree *t, struct restore_time *rt)
1933 {
1934 #ifndef HAVE_UTIMES
1935 	(void)t; /* UNUSED */
1936 	(void)rt; /* UNUSED */
1937 	return (close(fd));
1938 #else
1939 #if defined(HAVE_FUTIMENS) && !defined(__CYGWIN__)
1940 	struct timespec timespecs[2];
1941 #endif
1942 	struct timeval times[2];
1943 
1944 	if ((t->flags & needsRestoreTimes) == 0 || rt->noatime) {
1945 		if (fd >= 0)
1946 			return (close(fd));
1947 		else
1948 			return (0);
1949 	}
1950 
1951 #if defined(HAVE_FUTIMENS) && !defined(__CYGWIN__)
1952 	timespecs[1].tv_sec = rt->mtime;
1953 	timespecs[1].tv_nsec = rt->mtime_nsec;
1954 
1955 	timespecs[0].tv_sec = rt->atime;
1956 	timespecs[0].tv_nsec = rt->atime_nsec;
1957 	/* futimens() is defined in POSIX.1-2008. */
1958 	if (futimens(fd, timespecs) == 0)
1959 		return (close(fd));
1960 #endif
1961 
1962 	times[1].tv_sec = rt->mtime;
1963 	times[1].tv_usec = rt->mtime_nsec / 1000;
1964 
1965 	times[0].tv_sec = rt->atime;
1966 	times[0].tv_usec = rt->atime_nsec / 1000;
1967 
1968 #if !defined(HAVE_FUTIMENS) && defined(HAVE_FUTIMES) && !defined(__CYGWIN__)
1969 	if (futimes(fd, times) == 0)
1970 		return (close(fd));
1971 #endif
1972 	close(fd);
1973 #if defined(HAVE_FUTIMESAT)
1974 	if (futimesat(tree_current_dir_fd(t), rt->name, times) == 0)
1975 		return (0);
1976 #endif
1977 #ifdef HAVE_LUTIMES
1978 	if (lutimes(rt->name, times) != 0)
1979 #else
1980 	if (AE_IFLNK != rt->filetype && utimes(rt->name, times) != 0)
1981 #endif
1982 		return (-1);
1983 #endif
1984 	return (0);
1985 }
1986 
1987 static int
open_on_current_dir(struct tree * t,const char * path,int flags)1988 open_on_current_dir(struct tree *t, const char *path, int flags)
1989 {
1990 #ifdef HAVE_OPENAT
1991 	return (openat(tree_current_dir_fd(t), path, flags));
1992 #else
1993 	if (tree_enter_working_dir(t) != 0)
1994 		return (-1);
1995 	return (open(path, flags));
1996 #endif
1997 }
1998 
1999 static int
tree_dup(int fd)2000 tree_dup(int fd)
2001 {
2002 	int new_fd;
2003 #ifdef F_DUPFD_CLOEXEC
2004 	static volatile int can_dupfd_cloexec = 1;
2005 
2006 	if (can_dupfd_cloexec) {
2007 		new_fd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
2008 		if (new_fd != -1)
2009 			return (new_fd);
2010 		/* Linux 2.6.18 - 2.6.23 declare F_DUPFD_CLOEXEC,
2011 		 * but it cannot be used. So we have to try dup(). */
2012 		/* We won't try F_DUPFD_CLOEXEC. */
2013 		can_dupfd_cloexec = 0;
2014 	}
2015 #endif /* F_DUPFD_CLOEXEC */
2016 	new_fd = dup(fd);
2017 	if (new_fd != -1) {
2018 		__archive_ensure_cloexec_flag(new_fd);
2019 		return (new_fd);
2020 	}
2021 	return (-1);
2022 }
2023 
2024 /*
2025  * Add a directory path to the current stack.
2026  */
2027 static void
tree_push(struct tree * t,const char * path,int filesystem_id,int64_t dev,int64_t ino,struct restore_time * rt)2028 tree_push(struct tree *t, const char *path, int filesystem_id,
2029     int64_t dev, int64_t ino, struct restore_time *rt)
2030 {
2031 	struct tree_entry *te;
2032 
2033 	te = calloc(1, sizeof(*te));
2034 	if (te == NULL)
2035 		__archive_errx(1, "Out of memory");
2036 	te->next = t->stack;
2037 	te->parent = t->current;
2038 	if (te->parent)
2039 		te->depth = te->parent->depth + 1;
2040 	t->stack = te;
2041 	archive_string_init(&te->name);
2042 	te->symlink_parent_fd = -1;
2043 	archive_strcpy(&te->name, path);
2044 	te->flags = needsDescent | needsOpen | needsAscent;
2045 	te->filesystem_id = filesystem_id;
2046 	te->dev = dev;
2047 	te->ino = ino;
2048 	te->dirname_length = t->dirname_length;
2049 	te->restore_time.name = te->name.s;
2050 	if (rt != NULL) {
2051 		te->restore_time.mtime = rt->mtime;
2052 		te->restore_time.mtime_nsec = rt->mtime_nsec;
2053 		te->restore_time.atime = rt->atime;
2054 		te->restore_time.atime_nsec = rt->atime_nsec;
2055 		te->restore_time.filetype = rt->filetype;
2056 		te->restore_time.noatime = rt->noatime;
2057 	}
2058 }
2059 
2060 /*
2061  * Append a name to the current dir path.
2062  */
2063 static void
tree_append(struct tree * t,const char * name,size_t name_length)2064 tree_append(struct tree *t, const char *name, size_t name_length)
2065 {
2066 	size_t size_needed;
2067 
2068 	t->path.s[t->dirname_length] = '\0';
2069 	t->path.length = t->dirname_length;
2070 	/* Strip trailing '/' from name, unless entire name is "/". */
2071 	while (name_length > 1 && name[name_length - 1] == '/')
2072 		name_length--;
2073 
2074 	/* Resize pathname buffer as needed. */
2075 	size_needed = name_length + t->dirname_length + 2;
2076 	archive_string_ensure(&t->path, size_needed);
2077 	/* Add a separating '/' if it's needed. */
2078 	if (t->dirname_length > 0 && t->path.s[archive_strlen(&t->path)-1] != '/')
2079 		archive_strappend_char(&t->path, '/');
2080 	t->basename = t->path.s + archive_strlen(&t->path);
2081 	archive_strncat(&t->path, name, name_length);
2082 	t->restore_time.name = t->basename;
2083 }
2084 
2085 /*
2086  * Open a directory tree for traversal.
2087  */
2088 static struct tree *
tree_open(const char * path,char symlink_mode,int restore_time)2089 tree_open(const char *path, char symlink_mode, int restore_time)
2090 {
2091 	struct tree *t;
2092 
2093 	if ((t = calloc(1, sizeof(*t))) == NULL)
2094 		return (NULL);
2095 	archive_string_init(&t->path);
2096 	archive_string_ensure(&t->path, 31);
2097 	t->initial_symlink_mode = symlink_mode;
2098 	return (tree_reopen(t, path, restore_time));
2099 }
2100 
2101 static struct tree *
tree_reopen(struct tree * t,const char * path,int restore_time)2102 tree_reopen(struct tree *t, const char *path, int restore_time)
2103 {
2104 #if defined(O_PATH)
2105 	/* Linux */
2106 	const int o_flag = O_PATH;
2107 #elif defined(O_SEARCH)
2108 	/* SunOS */
2109 	const int o_flag = O_SEARCH;
2110 #elif defined(__FreeBSD__) && defined(O_EXEC)
2111 	/* FreeBSD */
2112 	const int o_flag = O_EXEC;
2113 #endif
2114 
2115 	t->flags = (restore_time != 0)?needsRestoreTimes:0;
2116 	t->flags |= onInitialDir;
2117 	t->visit_type = 0;
2118 	t->tree_errno = 0;
2119 	t->dirname_length = 0;
2120 	t->depth = 0;
2121 	t->descend = 0;
2122 	t->current = NULL;
2123 	t->d = INVALID_DIR_HANDLE;
2124 	t->symlink_mode = t->initial_symlink_mode;
2125 	archive_string_empty(&t->path);
2126 	t->entry_fd = -1;
2127 	t->entry_eof = 0;
2128 	t->entry_remaining_bytes = 0;
2129 	t->initial_filesystem_id = -1;
2130 
2131 	/* First item is set up a lot like a symlink traversal. */
2132 	tree_push(t, path, 0, 0, 0, NULL);
2133 	t->stack->flags = needsFirstVisit;
2134 	t->maxOpenCount = t->openCount = 1;
2135 	t->initial_dir_fd = open(".", O_RDONLY | O_CLOEXEC);
2136 #if defined(O_PATH) || defined(O_SEARCH) || \
2137  (defined(__FreeBSD__) && defined(O_EXEC))
2138 	/*
2139 	 * Most likely reason to fail opening "." is that it's not readable,
2140 	 * so try again for execute. The consequences of not opening this are
2141 	 * unhelpful and unnecessary errors later.
2142 	 */
2143 	if (t->initial_dir_fd < 0) {
2144 		t->initial_dir_fd = open(".", o_flag | O_CLOEXEC);
2145 		if (t->initial_dir_fd < 0)
2146 			return NULL;
2147 	}
2148 #endif
2149 	__archive_ensure_cloexec_flag(t->initial_dir_fd);
2150 	t->working_dir_fd = tree_dup(t->initial_dir_fd);
2151 	if (t->working_dir_fd < 0)
2152 		return NULL;
2153 	return (t);
2154 }
2155 
2156 static int
tree_descent(struct tree * t)2157 tree_descent(struct tree *t)
2158 {
2159 	int flag, new_fd, r = 0;
2160 
2161 	t->dirname_length = archive_strlen(&t->path);
2162 	flag = O_RDONLY | O_CLOEXEC;
2163 #if defined(O_DIRECTORY)
2164 	flag |= O_DIRECTORY;
2165 #endif
2166 	new_fd = open_on_current_dir(t, t->stack->name.s, flag);
2167 	__archive_ensure_cloexec_flag(new_fd);
2168 	if (new_fd < 0) {
2169 		t->tree_errno = errno;
2170 		r = TREE_ERROR_DIR;
2171 	} else {
2172 		t->depth++;
2173 		/* If it is a link, set up fd for the ascent. */
2174 		if (t->stack->flags & isDirLink) {
2175 			t->stack->symlink_parent_fd = t->working_dir_fd;
2176 			t->openCount++;
2177 			if (t->openCount > t->maxOpenCount)
2178 				t->maxOpenCount = t->openCount;
2179 		} else
2180 			close(t->working_dir_fd);
2181 		/* Renew the current working directory. */
2182 		t->working_dir_fd = new_fd;
2183 		t->flags &= ~onWorkingDir;
2184 	}
2185 	return (r);
2186 }
2187 
2188 /*
2189  * We've finished a directory; ascend back to the parent.
2190  */
2191 static int
tree_ascend(struct tree * t)2192 tree_ascend(struct tree *t)
2193 {
2194 	struct tree_entry *te;
2195 	int new_fd, r = 0, prev_dir_fd;
2196 
2197 	te = t->stack;
2198 	prev_dir_fd = t->working_dir_fd;
2199 	if (te->flags & isDirLink)
2200 		new_fd = te->symlink_parent_fd;
2201 	else {
2202 		new_fd = open_on_current_dir(t, "..", O_RDONLY | O_CLOEXEC);
2203 		__archive_ensure_cloexec_flag(new_fd);
2204 	}
2205 	if (new_fd < 0) {
2206 		t->tree_errno = errno;
2207 		r = TREE_ERROR_FATAL;
2208 	} else {
2209 		/* Renew the current working directory. */
2210 		t->working_dir_fd = new_fd;
2211 		t->flags &= ~onWorkingDir;
2212 		/* Current directory has been changed, we should
2213 		 * close an fd of previous working directory. */
2214 		close_and_restore_time(prev_dir_fd, t, &te->restore_time);
2215 		if (te->flags & isDirLink) {
2216 			t->openCount--;
2217 			te->symlink_parent_fd = -1;
2218 		}
2219 		t->depth--;
2220 	}
2221 	return (r);
2222 }
2223 
2224 /*
2225  * Return to the initial directory where tree_open() was performed.
2226  */
2227 static int
tree_enter_initial_dir(struct tree * t)2228 tree_enter_initial_dir(struct tree *t)
2229 {
2230 	int r = 0;
2231 
2232 	if ((t->flags & onInitialDir) == 0) {
2233 		r = fchdir(t->initial_dir_fd);
2234 		if (r == 0) {
2235 			t->flags &= ~onWorkingDir;
2236 			t->flags |= onInitialDir;
2237 		}
2238 	}
2239 	return (r);
2240 }
2241 
2242 /*
2243  * Restore working directory of directory traversals.
2244  */
2245 static int
tree_enter_working_dir(struct tree * t)2246 tree_enter_working_dir(struct tree *t)
2247 {
2248 	int r = 0;
2249 
2250 	/*
2251 	 * Change the current directory if really needed.
2252 	 * Sometimes this is unneeded when we did not do
2253 	 * descent.
2254 	 */
2255 	if (t->depth > 0 && (t->flags & onWorkingDir) == 0) {
2256 		r = fchdir(t->working_dir_fd);
2257 		if (r == 0) {
2258 			t->flags &= ~onInitialDir;
2259 			t->flags |= onWorkingDir;
2260 		}
2261 	}
2262 	return (r);
2263 }
2264 
2265 static int
tree_current_dir_fd(struct tree * t)2266 tree_current_dir_fd(struct tree *t)
2267 {
2268 	return (t->working_dir_fd);
2269 }
2270 
2271 /*
2272  * Pop the working stack.
2273  */
2274 static void
tree_pop(struct tree * t)2275 tree_pop(struct tree *t)
2276 {
2277 	struct tree_entry *te;
2278 
2279 	t->path.s[t->dirname_length] = '\0';
2280 	t->path.length = t->dirname_length;
2281 	if (t->stack == t->current && t->current != NULL)
2282 		t->current = t->current->parent;
2283 	te = t->stack;
2284 	t->stack = te->next;
2285 	t->dirname_length = te->dirname_length;
2286 	t->basename = t->path.s + t->dirname_length;
2287 	while (t->basename[0] == '/')
2288 		t->basename++;
2289 	archive_string_free(&te->name);
2290 	free(te);
2291 }
2292 
2293 /*
2294  * Get the next item in the tree traversal.
2295  */
2296 static int
tree_next(struct tree * t)2297 tree_next(struct tree *t)
2298 {
2299 	int r;
2300 
2301 	while (t->stack != NULL) {
2302 		/* If there's an open dir, get the next entry from there. */
2303 		if (t->d != INVALID_DIR_HANDLE) {
2304 			r = tree_dir_next_posix(t);
2305 			if (r == 0)
2306 				continue;
2307 			return (r);
2308 		}
2309 
2310 		if (t->stack->flags & needsFirstVisit) {
2311 			/* Top stack item needs a regular visit. */
2312 			t->current = t->stack;
2313 			tree_append(t, t->stack->name.s,
2314 			    archive_strlen(&(t->stack->name)));
2315 			/* t->dirname_length = t->path_length; */
2316 			/* tree_pop(t); */
2317 			t->stack->flags &= ~needsFirstVisit;
2318 			return (t->visit_type = TREE_REGULAR);
2319 		} else if (t->stack->flags & needsDescent) {
2320 			/* Top stack item is dir to descend into. */
2321 			t->current = t->stack;
2322 			tree_append(t, t->stack->name.s,
2323 			    archive_strlen(&(t->stack->name)));
2324 			t->stack->flags &= ~needsDescent;
2325 			r = tree_descent(t);
2326 			if (r != 0) {
2327 				tree_pop(t);
2328 				t->visit_type = r;
2329 			} else
2330 				t->visit_type = TREE_POSTDESCENT;
2331 			return (t->visit_type);
2332 		} else if (t->stack->flags & needsOpen) {
2333 			t->stack->flags &= ~needsOpen;
2334 			r = tree_dir_next_posix(t);
2335 			if (r == 0)
2336 				continue;
2337 			return (r);
2338 		} else if (t->stack->flags & needsAscent) {
2339 		        /* Top stack item is dir and we're done with it. */
2340 			r = tree_ascend(t);
2341 			tree_pop(t);
2342 			t->visit_type = r != 0 ? r : TREE_POSTASCENT;
2343 			return (t->visit_type);
2344 		} else {
2345 			/* Top item on stack is dead. */
2346 			tree_pop(t);
2347 			t->flags &= ~hasLstat;
2348 			t->flags &= ~hasStat;
2349 		}
2350 	}
2351 	return (t->visit_type = 0);
2352 }
2353 
2354 static int
tree_dir_next_posix(struct tree * t)2355 tree_dir_next_posix(struct tree *t)
2356 {
2357 	int r;
2358 	const char *name;
2359 	size_t namelen;
2360 
2361 	if (t->d == NULL) {
2362 
2363 #if defined(HAVE_FDOPENDIR)
2364 		int fd = tree_dup(t->working_dir_fd);
2365 		if (fd != -1)
2366 			t->d = fdopendir(fd);
2367 #else /* HAVE_FDOPENDIR */
2368 		if (tree_enter_working_dir(t) == 0) {
2369 			t->d = opendir(".");
2370 #ifdef HAVE_DIRFD
2371 			__archive_ensure_cloexec_flag(dirfd(t->d));
2372 #endif
2373 		}
2374 #endif /* HAVE_FDOPENDIR */
2375 		if (t->d == NULL) {
2376 			r = tree_ascend(t); /* Undo "chdir" */
2377 			tree_pop(t);
2378 			t->tree_errno = errno;
2379 			t->visit_type = r != 0 ? r : TREE_ERROR_DIR;
2380 			return (t->visit_type);
2381 		}
2382 	}
2383 	for (;;) {
2384 		errno = 0;
2385 		t->de = readdir(t->d);
2386 		if (t->de == NULL) {
2387 			r = errno;
2388 			closedir(t->d);
2389 			t->d = INVALID_DIR_HANDLE;
2390 			if (r != 0) {
2391 				t->tree_errno = r;
2392 				t->visit_type = TREE_ERROR_DIR;
2393 				return (t->visit_type);
2394 			} else
2395 				return (0);
2396 		}
2397 		name = t->de->d_name;
2398 		namelen = D_NAMELEN(t->de);
2399 		t->flags &= ~hasLstat;
2400 		t->flags &= ~hasStat;
2401 		if (name[0] == '.' && name[1] == '\0')
2402 			continue;
2403 		if (name[0] == '.' && name[1] == '.' && name[2] == '\0')
2404 			continue;
2405 		tree_append(t, name, namelen);
2406 		return (t->visit_type = TREE_REGULAR);
2407 	}
2408 }
2409 
2410 
2411 /*
2412  * Get the stat() data for the entry just returned from tree_next().
2413  */
2414 static const struct stat *
tree_current_stat(struct tree * t)2415 tree_current_stat(struct tree *t)
2416 {
2417 	if (!(t->flags & hasStat)) {
2418 #ifdef HAVE_FSTATAT
2419 		if (fstatat(tree_current_dir_fd(t),
2420 		    tree_current_access_path(t), &t->st, 0) != 0)
2421 #else
2422 		if (tree_enter_working_dir(t) != 0)
2423 			return NULL;
2424 		if (la_stat(tree_current_access_path(t), &t->st) != 0)
2425 #endif
2426 			return NULL;
2427 		t->flags |= hasStat;
2428 	}
2429 	return (&t->st);
2430 }
2431 
2432 /*
2433  * Get the lstat() data for the entry just returned from tree_next().
2434  */
2435 static const struct stat *
tree_current_lstat(struct tree * t)2436 tree_current_lstat(struct tree *t)
2437 {
2438 	if (!(t->flags & hasLstat)) {
2439 #ifdef HAVE_FSTATAT
2440 		if (fstatat(tree_current_dir_fd(t),
2441 		    tree_current_access_path(t), &t->lst,
2442 		    AT_SYMLINK_NOFOLLOW) != 0)
2443 #else
2444 		if (tree_enter_working_dir(t) != 0)
2445 			return NULL;
2446 #ifdef HAVE_LSTAT
2447 		if (lstat(tree_current_access_path(t), &t->lst) != 0)
2448 #else
2449 		if (la_stat(tree_current_access_path(t), &t->lst) != 0)
2450 #endif
2451 #endif
2452 			return NULL;
2453 		t->flags |= hasLstat;
2454 	}
2455 	return (&t->lst);
2456 }
2457 
2458 /*
2459  * Test whether current entry is a dir or link to a dir.
2460  */
2461 static int
tree_current_is_dir(struct tree * t)2462 tree_current_is_dir(struct tree *t)
2463 {
2464 	const struct stat *st;
2465 	/*
2466 	 * If we already have lstat() info, then try some
2467 	 * cheap tests to determine if this is a dir.
2468 	 */
2469 	if (t->flags & hasLstat) {
2470 		/* If lstat() says it's a dir, it must be a dir. */
2471 		st = tree_current_lstat(t);
2472 		if (st == NULL)
2473 			return 0;
2474 		if (S_ISDIR(st->st_mode))
2475 			return 1;
2476 		/* Not a dir; might be a link to a dir. */
2477 		/* If it's not a link, then it's not a link to a dir. */
2478 		if (!S_ISLNK(st->st_mode))
2479 			return 0;
2480 		/*
2481 		 * It's a link, but we don't know what it's a link to,
2482 		 * so we'll have to use stat().
2483 		 */
2484 	}
2485 
2486 	st = tree_current_stat(t);
2487 	/* If we can't stat it, it's not a dir. */
2488 	if (st == NULL)
2489 		return 0;
2490 	/* Use the definitive test.  Hopefully this is cached. */
2491 	return (S_ISDIR(st->st_mode));
2492 }
2493 
2494 /*
2495  * Test whether current entry is a physical directory.  Usually, we
2496  * already have at least one of stat() or lstat() in memory, so we
2497  * use tricks to try to avoid an extra trip to the disk.
2498  */
2499 static int
tree_current_is_physical_dir(struct tree * t)2500 tree_current_is_physical_dir(struct tree *t)
2501 {
2502 	const struct stat *st;
2503 
2504 	/*
2505 	 * If stat() says it isn't a dir, then it's not a dir.
2506 	 * If stat() data is cached, this check is free, so do it first.
2507 	 */
2508 	if (t->flags & hasStat) {
2509 		st = tree_current_stat(t);
2510 		if (st == NULL)
2511 			return (0);
2512 		if (!S_ISDIR(st->st_mode))
2513 			return (0);
2514 	}
2515 
2516 	/*
2517 	 * Either stat() said it was a dir (in which case, we have
2518 	 * to determine whether it's really a link to a dir) or
2519 	 * stat() info wasn't available.  So we use lstat(), which
2520 	 * hopefully is already cached.
2521 	 */
2522 
2523 	st = tree_current_lstat(t);
2524 	/* If we can't stat it, it's not a dir. */
2525 	if (st == NULL)
2526 		return 0;
2527 	/* Use the definitive test.  Hopefully this is cached. */
2528 	return (S_ISDIR(st->st_mode));
2529 }
2530 
2531 /*
2532  * Test whether the same file has been in the tree as its parent.
2533  */
2534 static int
tree_target_is_same_as_parent(struct tree * t,const struct stat * st)2535 tree_target_is_same_as_parent(struct tree *t, const struct stat *st)
2536 {
2537 	struct tree_entry *te;
2538 
2539 	for (te = t->current->parent; te != NULL; te = te->parent) {
2540 		if (te->dev == (int64_t)st->st_dev &&
2541 		    te->ino == (int64_t)st->st_ino)
2542 			return (1);
2543 	}
2544 	return (0);
2545 }
2546 
2547 /*
2548  * Test whether the current file is symbolic link target and
2549  * on the other filesystem.
2550  */
2551 static int
tree_current_is_symblic_link_target(struct tree * t)2552 tree_current_is_symblic_link_target(struct tree *t)
2553 {
2554 	static const struct stat *lst, *st;
2555 
2556 	lst = tree_current_lstat(t);
2557 	st = tree_current_stat(t);
2558 	return (st != NULL && lst != NULL &&
2559 	    (int64_t)st->st_dev == t->current_filesystem->dev &&
2560 	    st->st_dev != lst->st_dev);
2561 }
2562 
2563 /*
2564  * Return the access path for the entry just returned from tree_next().
2565  */
2566 static const char *
tree_current_access_path(struct tree * t)2567 tree_current_access_path(struct tree *t)
2568 {
2569 	return (t->basename);
2570 }
2571 
2572 /*
2573  * Return the full path for the entry just returned from tree_next().
2574  */
2575 static const char *
tree_current_path(struct tree * t)2576 tree_current_path(struct tree *t)
2577 {
2578 	return (t->path.s);
2579 }
2580 
2581 /*
2582  * Terminate the traversal.
2583  */
2584 static void
tree_close(struct tree * t)2585 tree_close(struct tree *t)
2586 {
2587 
2588 	if (t == NULL)
2589 		return;
2590 	if (t->entry_fd >= 0) {
2591 		close_and_restore_time(t->entry_fd, t, &t->restore_time);
2592 		t->entry_fd = -1;
2593 	}
2594 	/* Close the handle of readdir(). */
2595 	if (t->d != INVALID_DIR_HANDLE) {
2596 		closedir(t->d);
2597 		t->d = INVALID_DIR_HANDLE;
2598 	}
2599 	/* Release anything remaining in the stack. */
2600 	while (t->stack != NULL) {
2601 		if (t->stack->flags & isDirLink)
2602 			close(t->stack->symlink_parent_fd);
2603 		tree_pop(t);
2604 	}
2605 	if (t->working_dir_fd >= 0) {
2606 		close(t->working_dir_fd);
2607 		t->working_dir_fd = -1;
2608 	}
2609 	if (t->initial_dir_fd >= 0) {
2610 		close(t->initial_dir_fd);
2611 		t->initial_dir_fd = -1;
2612 	}
2613 }
2614 
2615 /*
2616  * Release any resources.
2617  */
2618 static void
tree_free(struct tree * t)2619 tree_free(struct tree *t)
2620 {
2621 	int i;
2622 
2623 	if (t == NULL)
2624 		return;
2625 	archive_string_free(&t->path);
2626 	free(t->sparse_list);
2627 	for (i = 0; i < t->max_filesystem_id; i++)
2628 		free(t->filesystem_table[i].allocation_ptr);
2629 	free(t->filesystem_table);
2630 	free(t);
2631 }
2632 
2633 #endif
2634