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