xref: /freebsd/contrib/libarchive/libarchive/archive_read_disk_posix.c (revision bd66c1b43e33540205dbc1187c2f2a15c58b57ba)
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 #if defined(USE_READDIR_R)
172 	size_t		name_max;
173 #endif
174 	long		incr_xfer_size;
175 	long		max_xfer_size;
176 	long		min_xfer_size;
177 	long		xfer_align;
178 
179 	/*
180 	 * Buffer used for reading file contents.
181 	 */
182 	/* Exactly allocated memory pointer. */
183 	unsigned char	*allocation_ptr;
184 	/* Pointer adjusted to the filesystem alignment . */
185 	unsigned char	*buff;
186 	size_t		 buff_size;
187 };
188 
189 /* Definitions for tree_entry.flags bitmap. */
190 #define	isDir		1  /* This entry is a regular directory. */
191 #define	isDirLink	2  /* This entry is a symbolic link to a directory. */
192 #define	needsFirstVisit	4  /* This is an initial entry. */
193 #define	needsDescent	8  /* This entry needs to be previsited. */
194 #define	needsOpen	16 /* This is a directory that needs to be opened. */
195 #define	needsAscent	32 /* This entry needs to be postvisited. */
196 
197 /*
198  * Local data for this package.
199  */
200 struct tree {
201 	struct tree_entry	*stack;
202 	struct tree_entry	*current;
203 	DIR			*d;
204 #define	INVALID_DIR_HANDLE NULL
205 	struct dirent		*de;
206 #if defined(USE_READDIR_R)
207 	struct dirent		*dirent;
208 	size_t			 dirent_allocated;
209 #endif
210 	int			 flags;
211 	int			 visit_type;
212 	/* Error code from last failed operation. */
213 	int			 tree_errno;
214 
215 	/* Dynamically-sized buffer for holding path */
216 	struct archive_string	 path;
217 
218 	/* Last path element */
219 	const char		*basename;
220 	/* Leading dir length */
221 	size_t			 dirname_length;
222 
223 	int			 depth;
224 	int			 openCount;
225 	int			 maxOpenCount;
226 	int			 initial_dir_fd;
227 	int			 working_dir_fd;
228 
229 	struct stat		 lst;
230 	struct stat		 st;
231 	int			 descend;
232 	int			 nlink;
233 	/* How to restore time of a file. */
234 	struct restore_time	 restore_time;
235 
236 	struct entry_sparse {
237 		int64_t		 length;
238 		int64_t		 offset;
239 	}			*sparse_list, *current_sparse;
240 	int			 sparse_count;
241 	int			 sparse_list_size;
242 
243 	char			 initial_symlink_mode;
244 	char			 symlink_mode;
245 	struct filesystem	*current_filesystem;
246 	struct filesystem	*filesystem_table;
247 	int			 initial_filesystem_id;
248 	int			 current_filesystem_id;
249 	int			 max_filesystem_id;
250 	int			 allocated_filesystem;
251 
252 	int			 entry_fd;
253 	int			 entry_eof;
254 	int64_t			 entry_remaining_bytes;
255 	int64_t			 entry_total;
256 	unsigned char		*entry_buff;
257 	size_t			 entry_buff_size;
258 };
259 
260 /* Definitions for tree.flags bitmap. */
261 #define	hasStat		16 /* The st entry is valid. */
262 #define	hasLstat	32 /* The lst entry is valid. */
263 #define	onWorkingDir	64 /* We are on the working dir where we are
264 			    * reading directory entry at this time. */
265 #define	needsRestoreTimes 128
266 #define	onInitialDir	256 /* We are on the initial dir. */
267 
268 static int
269 tree_dir_next_posix(struct tree *t);
270 
271 #ifdef HAVE_DIRENT_D_NAMLEN
272 /* BSD extension; avoids need for a strlen() call. */
273 #define	D_NAMELEN(dp)	(dp)->d_namlen
274 #else
275 #define	D_NAMELEN(dp)	(strlen((dp)->d_name))
276 #endif
277 
278 /* Initiate/terminate a tree traversal. */
279 static struct tree *tree_open(const char *, int, int);
280 static struct tree *tree_reopen(struct tree *, const char *, int);
281 static void tree_close(struct tree *);
282 static void tree_free(struct tree *);
283 static void tree_push(struct tree *, const char *, int, int64_t, int64_t,
284 		struct restore_time *);
285 static int tree_enter_initial_dir(struct tree *);
286 static int tree_enter_working_dir(struct tree *);
287 static int tree_current_dir_fd(struct tree *);
288 
289 /*
290  * tree_next() returns Zero if there is no next entry, non-zero if
291  * there is.  Note that directories are visited three times.
292  * Directories are always visited first as part of enumerating their
293  * parent; that is a "regular" visit.  If tree_descend() is invoked at
294  * that time, the directory is added to a work list and will
295  * subsequently be visited two more times: once just after descending
296  * into the directory ("postdescent") and again just after ascending
297  * back to the parent ("postascent").
298  *
299  * TREE_ERROR_DIR is returned if the descent failed (because the
300  * directory couldn't be opened, for instance).  This is returned
301  * instead of TREE_POSTDESCENT/TREE_POSTASCENT.  TREE_ERROR_DIR is not a
302  * fatal error, but it does imply that the relevant subtree won't be
303  * visited.  TREE_ERROR_FATAL is returned for an error that left the
304  * traversal completely hosed.  Right now, this is only returned for
305  * chdir() failures during ascent.
306  */
307 #define	TREE_REGULAR		1
308 #define	TREE_POSTDESCENT	2
309 #define	TREE_POSTASCENT		3
310 #define	TREE_ERROR_DIR		-1
311 #define	TREE_ERROR_FATAL	-2
312 
313 static int tree_next(struct tree *);
314 
315 /*
316  * Return information about the current entry.
317  */
318 
319 /*
320  * The current full pathname, length of the full pathname, and a name
321  * that can be used to access the file.  Because tree does use chdir
322  * extensively, the access path is almost never the same as the full
323  * current path.
324  *
325  * TODO: On platforms that support it, use openat()-style operations
326  * to eliminate the chdir() operations entirely while still supporting
327  * arbitrarily deep traversals.  This makes access_path troublesome to
328  * support, of course, which means we'll need a rich enough interface
329  * that clients can function without it.  (In particular, we'll need
330  * tree_current_open() that returns an open file descriptor.)
331  *
332  */
333 static const char *tree_current_path(struct tree *);
334 static const char *tree_current_access_path(struct tree *);
335 
336 /*
337  * Request the lstat() or stat() data for the current path.  Since the
338  * tree package needs to do some of this anyway, and caches the
339  * results, you should take advantage of it here if you need it rather
340  * than make a redundant stat() or lstat() call of your own.
341  */
342 static const struct stat *tree_current_stat(struct tree *);
343 static const struct stat *tree_current_lstat(struct tree *);
344 static int	tree_current_is_symblic_link_target(struct tree *);
345 
346 /* The following functions use tricks to avoid a certain number of
347  * stat()/lstat() calls. */
348 /* "is_physical_dir" is equivalent to S_ISDIR(tree_current_lstat()->st_mode) */
349 static int tree_current_is_physical_dir(struct tree *);
350 /* "is_dir" is equivalent to S_ISDIR(tree_current_stat()->st_mode) */
351 static int tree_current_is_dir(struct tree *);
352 static int update_current_filesystem(struct archive_read_disk *a,
353 		    int64_t dev);
354 static int setup_current_filesystem(struct archive_read_disk *);
355 static int tree_target_is_same_as_parent(struct tree *, const struct stat *);
356 
357 static int	_archive_read_disk_open(struct archive *, const char *);
358 static int	_archive_read_free(struct archive *);
359 static int	_archive_read_close(struct archive *);
360 static int	_archive_read_data_block(struct archive *,
361 		    const void **, size_t *, int64_t *);
362 static int	_archive_read_next_header(struct archive *,
363 		    struct archive_entry **);
364 static int	_archive_read_next_header2(struct archive *,
365 		    struct archive_entry *);
366 static const char *trivial_lookup_gname(void *, int64_t gid);
367 static const char *trivial_lookup_uname(void *, int64_t uid);
368 static int	setup_sparse(struct archive_read_disk *, struct archive_entry *);
369 static int	close_and_restore_time(int fd, struct tree *,
370 		    struct restore_time *);
371 static int	open_on_current_dir(struct tree *, const char *, int);
372 static int	tree_dup(int);
373 
374 
375 static const struct archive_vtable
376 archive_read_disk_vtable = {
377 	.archive_free = _archive_read_free,
378 	.archive_close = _archive_read_close,
379 	.archive_read_data_block = _archive_read_data_block,
380 	.archive_read_next_header = _archive_read_next_header,
381 	.archive_read_next_header2 = _archive_read_next_header2,
382 };
383 
384 const char *
archive_read_disk_gname(struct archive * _a,la_int64_t gid)385 archive_read_disk_gname(struct archive *_a, la_int64_t gid)
386 {
387 	struct archive_read_disk *a = (struct archive_read_disk *)_a;
388 	if (ARCHIVE_OK != __archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
389 		ARCHIVE_STATE_ANY, "archive_read_disk_gname"))
390 		return (NULL);
391 	if (a->lookup_gname == NULL)
392 		return (NULL);
393 	return ((*a->lookup_gname)(a->lookup_gname_data, gid));
394 }
395 
396 const char *
archive_read_disk_uname(struct archive * _a,la_int64_t uid)397 archive_read_disk_uname(struct archive *_a, la_int64_t uid)
398 {
399 	struct archive_read_disk *a = (struct archive_read_disk *)_a;
400 	if (ARCHIVE_OK != __archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
401 		ARCHIVE_STATE_ANY, "archive_read_disk_uname"))
402 		return (NULL);
403 	if (a->lookup_uname == NULL)
404 		return (NULL);
405 	return ((*a->lookup_uname)(a->lookup_uname_data, uid));
406 }
407 
408 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))409 archive_read_disk_set_gname_lookup(struct archive *_a,
410     void *private_data,
411     const char * (*lookup_gname)(void *private, la_int64_t gid),
412     void (*cleanup_gname)(void *private))
413 {
414 	struct archive_read_disk *a = (struct archive_read_disk *)_a;
415 	archive_check_magic(&a->archive, ARCHIVE_READ_DISK_MAGIC,
416 	    ARCHIVE_STATE_ANY, "archive_read_disk_set_gname_lookup");
417 
418 	if (a->cleanup_gname != NULL && a->lookup_gname_data != NULL)
419 		(a->cleanup_gname)(a->lookup_gname_data);
420 
421 	a->lookup_gname = lookup_gname;
422 	a->cleanup_gname = cleanup_gname;
423 	a->lookup_gname_data = private_data;
424 	return (ARCHIVE_OK);
425 }
426 
427 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))428 archive_read_disk_set_uname_lookup(struct archive *_a,
429     void *private_data,
430     const char * (*lookup_uname)(void *private, la_int64_t uid),
431     void (*cleanup_uname)(void *private))
432 {
433 	struct archive_read_disk *a = (struct archive_read_disk *)_a;
434 	archive_check_magic(&a->archive, ARCHIVE_READ_DISK_MAGIC,
435 	    ARCHIVE_STATE_ANY, "archive_read_disk_set_uname_lookup");
436 
437 	if (a->cleanup_uname != NULL && a->lookup_uname_data != NULL)
438 		(a->cleanup_uname)(a->lookup_uname_data);
439 
440 	a->lookup_uname = lookup_uname;
441 	a->cleanup_uname = cleanup_uname;
442 	a->lookup_uname_data = private_data;
443 	return (ARCHIVE_OK);
444 }
445 
446 /*
447  * Create a new archive_read_disk object and initialize it with global state.
448  */
449 struct archive *
archive_read_disk_new(void)450 archive_read_disk_new(void)
451 {
452 	struct archive_read_disk *a;
453 
454 	a = calloc(1, sizeof(*a));
455 	if (a == NULL)
456 		return (NULL);
457 	a->archive.magic = ARCHIVE_READ_DISK_MAGIC;
458 	a->archive.state = ARCHIVE_STATE_NEW;
459 	a->archive.vtable = &archive_read_disk_vtable;
460 	a->entry = archive_entry_new2(&a->archive);
461 	a->lookup_uname = trivial_lookup_uname;
462 	a->lookup_gname = trivial_lookup_gname;
463 	a->flags = ARCHIVE_READDISK_MAC_COPYFILE;
464 	a->open_on_current_dir = open_on_current_dir;
465 	a->tree_current_dir_fd = tree_current_dir_fd;
466 	a->tree_enter_working_dir = tree_enter_working_dir;
467 	return (&a->archive);
468 }
469 
470 static int
_archive_read_free(struct archive * _a)471 _archive_read_free(struct archive *_a)
472 {
473 	struct archive_read_disk *a = (struct archive_read_disk *)_a;
474 	int r;
475 
476 	if (_a == NULL)
477 		return (ARCHIVE_OK);
478 	archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
479 	    ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_free");
480 
481 	if (a->archive.state != ARCHIVE_STATE_CLOSED)
482 		r = _archive_read_close(&a->archive);
483 	else
484 		r = ARCHIVE_OK;
485 
486 	tree_free(a->tree);
487 	if (a->cleanup_gname != NULL && a->lookup_gname_data != NULL)
488 		(a->cleanup_gname)(a->lookup_gname_data);
489 	if (a->cleanup_uname != NULL && a->lookup_uname_data != NULL)
490 		(a->cleanup_uname)(a->lookup_uname_data);
491 	archive_string_free(&a->archive.error_string);
492 	archive_entry_free(a->entry);
493 	a->archive.magic = 0;
494 	__archive_clean(&a->archive);
495 	free(a);
496 	return (r);
497 }
498 
499 static int
_archive_read_close(struct archive * _a)500 _archive_read_close(struct archive *_a)
501 {
502 	struct archive_read_disk *a = (struct archive_read_disk *)_a;
503 
504 	archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
505 	    ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_close");
506 
507 	if (a->archive.state != ARCHIVE_STATE_FATAL)
508 		a->archive.state = ARCHIVE_STATE_CLOSED;
509 
510 	tree_close(a->tree);
511 
512 	return (ARCHIVE_OK);
513 }
514 
515 static void
setup_symlink_mode(struct archive_read_disk * a,char symlink_mode,int follow_symlinks)516 setup_symlink_mode(struct archive_read_disk *a, char symlink_mode,
517     int follow_symlinks)
518 {
519 	a->symlink_mode = symlink_mode;
520 	a->follow_symlinks = follow_symlinks;
521 	if (a->tree != NULL) {
522 		a->tree->initial_symlink_mode = a->symlink_mode;
523 		a->tree->symlink_mode = a->symlink_mode;
524 	}
525 }
526 
527 int
archive_read_disk_set_symlink_logical(struct archive * _a)528 archive_read_disk_set_symlink_logical(struct archive *_a)
529 {
530 	struct archive_read_disk *a = (struct archive_read_disk *)_a;
531 	archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
532 	    ARCHIVE_STATE_ANY, "archive_read_disk_set_symlink_logical");
533 	setup_symlink_mode(a, 'L', 1);
534 	return (ARCHIVE_OK);
535 }
536 
537 int
archive_read_disk_set_symlink_physical(struct archive * _a)538 archive_read_disk_set_symlink_physical(struct archive *_a)
539 {
540 	struct archive_read_disk *a = (struct archive_read_disk *)_a;
541 	archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
542 	    ARCHIVE_STATE_ANY, "archive_read_disk_set_symlink_physical");
543 	setup_symlink_mode(a, 'P', 0);
544 	return (ARCHIVE_OK);
545 }
546 
547 int
archive_read_disk_set_symlink_hybrid(struct archive * _a)548 archive_read_disk_set_symlink_hybrid(struct archive *_a)
549 {
550 	struct archive_read_disk *a = (struct archive_read_disk *)_a;
551 	archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
552 	    ARCHIVE_STATE_ANY, "archive_read_disk_set_symlink_hybrid");
553 	setup_symlink_mode(a, 'H', 1);/* Follow symlinks initially. */
554 	return (ARCHIVE_OK);
555 }
556 
557 int
archive_read_disk_set_atime_restored(struct archive * _a)558 archive_read_disk_set_atime_restored(struct archive *_a)
559 {
560 	struct archive_read_disk *a = (struct archive_read_disk *)_a;
561 	archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
562 	    ARCHIVE_STATE_ANY, "archive_read_disk_restore_atime");
563 #ifdef HAVE_UTIMES
564 	a->flags |= ARCHIVE_READDISK_RESTORE_ATIME;
565 	if (a->tree != NULL)
566 		a->tree->flags |= needsRestoreTimes;
567 	return (ARCHIVE_OK);
568 #else
569 	/* Display warning and unset flag */
570 	archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
571 	    "Cannot restore access time on this system");
572 	a->flags &= ~ARCHIVE_READDISK_RESTORE_ATIME;
573 	return (ARCHIVE_WARN);
574 #endif
575 }
576 
577 int
archive_read_disk_set_behavior(struct archive * _a,int flags)578 archive_read_disk_set_behavior(struct archive *_a, int flags)
579 {
580 	struct archive_read_disk *a = (struct archive_read_disk *)_a;
581 	int r = ARCHIVE_OK;
582 
583 	archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
584 	    ARCHIVE_STATE_ANY, "archive_read_disk_honor_nodump");
585 
586 	a->flags = flags;
587 
588 	if (flags & ARCHIVE_READDISK_RESTORE_ATIME)
589 		r = archive_read_disk_set_atime_restored(_a);
590 	else {
591 		if (a->tree != NULL)
592 			a->tree->flags &= ~needsRestoreTimes;
593 	}
594 	return (r);
595 }
596 
597 /*
598  * Trivial implementations of gname/uname lookup functions.
599  * These are normally overridden by the client, but these stub
600  * versions ensure that we always have something that works.
601  */
602 static const char *
trivial_lookup_gname(void * private_data,int64_t gid)603 trivial_lookup_gname(void *private_data, int64_t gid)
604 {
605 	(void)private_data; /* UNUSED */
606 	(void)gid; /* UNUSED */
607 	return (NULL);
608 }
609 
610 static const char *
trivial_lookup_uname(void * private_data,int64_t uid)611 trivial_lookup_uname(void *private_data, int64_t uid)
612 {
613 	(void)private_data; /* UNUSED */
614 	(void)uid; /* UNUSED */
615 	return (NULL);
616 }
617 
618 /*
619  * Allocate memory for the reading buffer adjusted to the filesystem
620  * alignment.
621  */
622 static int
setup_suitable_read_buffer(struct archive_read_disk * a)623 setup_suitable_read_buffer(struct archive_read_disk *a)
624 {
625 	struct tree *t = a->tree;
626 	struct filesystem *cf = t->current_filesystem;
627 	size_t asize;
628 	size_t s;
629 
630 	if (cf->allocation_ptr == NULL) {
631 		/* If we couldn't get a filesystem alignment,
632 		 * we use 4096 as default value but we won't use
633 		 * O_DIRECT to open() and openat() operations. */
634 		long xfer_align = (cf->xfer_align == -1)?4096:cf->xfer_align;
635 
636 		if (cf->max_xfer_size != -1)
637 			asize = cf->max_xfer_size + xfer_align;
638 		else {
639 			long incr = cf->incr_xfer_size;
640 			/* Some platform does not set a proper value to
641 			 * incr_xfer_size.*/
642 			if (incr < 0)
643 				incr = cf->min_xfer_size;
644 			if (cf->min_xfer_size < 0) {
645 				incr = xfer_align;
646 				asize = xfer_align;
647 			} else
648 				asize = cf->min_xfer_size;
649 
650 			/* Increase a buffer size up to 64K bytes in
651 			 * a proper increment size. */
652 			while (asize < 1024*64)
653 				asize += incr;
654 			/* Take a margin to adjust to the filesystem
655 			 * alignment. */
656 			asize += xfer_align;
657 		}
658 		cf->allocation_ptr = malloc(asize);
659 		if (cf->allocation_ptr == NULL) {
660 			archive_set_error(&a->archive, ENOMEM,
661 			    "Couldn't allocate memory");
662 			a->archive.state = ARCHIVE_STATE_FATAL;
663 			return (ARCHIVE_FATAL);
664 		}
665 
666 		/*
667 		 * Calculate proper address for the filesystem.
668 		 */
669 		s = (uintptr_t)cf->allocation_ptr;
670 		s %= xfer_align;
671 		if (s > 0)
672 			s = xfer_align - s;
673 
674 		/*
675 		 * Set a read buffer pointer in the proper alignment of
676 		 * the current filesystem.
677 		 */
678 		cf->buff = cf->allocation_ptr + s;
679 		cf->buff_size = asize - xfer_align;
680 	}
681 	return (ARCHIVE_OK);
682 }
683 
684 static int
_archive_read_data_block(struct archive * _a,const void ** buff,size_t * size,int64_t * offset)685 _archive_read_data_block(struct archive *_a, const void **buff,
686     size_t *size, int64_t *offset)
687 {
688 	struct archive_read_disk *a = (struct archive_read_disk *)_a;
689 	struct tree *t = a->tree;
690 	int r;
691 	ssize_t bytes;
692 	int64_t sparse_bytes;
693 	size_t buffbytes;
694 	int empty_sparse_region = 0;
695 
696 	archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA,
697 	    "archive_read_data_block");
698 
699 	if (t->entry_eof || t->entry_remaining_bytes <= 0) {
700 		r = ARCHIVE_EOF;
701 		goto abort_read_data;
702 	}
703 
704 	/*
705 	 * Open the current file.
706 	 */
707 	if (t->entry_fd < 0) {
708 		int flags = O_RDONLY | O_BINARY | O_CLOEXEC;
709 
710 		/*
711 		 * Eliminate or reduce cache effects if we can.
712 		 *
713 		 * Carefully consider this to be enabled.
714 		 */
715 #if defined(O_DIRECT) && 0/* Disabled for now */
716 		if (t->current_filesystem->xfer_align != -1 &&
717 		    t->nlink == 1)
718 			flags |= O_DIRECT;
719 #endif
720 #if defined(O_NOATIME)
721 		/*
722 		 * Linux has O_NOATIME flag; use it if we need.
723 		 */
724 		if ((t->flags & needsRestoreTimes) != 0 &&
725 		    t->restore_time.noatime == 0)
726 			flags |= O_NOATIME;
727 #endif
728 		t->entry_fd = open_on_current_dir(t,
729 		    tree_current_access_path(t), flags);
730 		__archive_ensure_cloexec_flag(t->entry_fd);
731 #if defined(O_NOATIME)
732 		/*
733 		 * When we did open the file with O_NOATIME flag,
734 		 * if successful, set 1 to t->restore_time.noatime
735 		 * not to restore an atime of the file later.
736 		 * if failed by EPERM, retry it without O_NOATIME flag.
737 		 */
738 		if (flags & O_NOATIME) {
739 			if (t->entry_fd >= 0)
740 				t->restore_time.noatime = 1;
741 			else if (errno == EPERM)
742 				flags &= ~O_NOATIME;
743 		}
744 #endif
745 		if (t->entry_fd < 0) {
746 			archive_set_error(&a->archive, errno,
747 			    "Couldn't open %s", tree_current_path(t));
748 			r = ARCHIVE_FAILED;
749 			tree_enter_initial_dir(t);
750 			goto abort_read_data;
751 		}
752 		tree_enter_initial_dir(t);
753 	}
754 
755 	/*
756 	 * Allocate read buffer if not allocated.
757 	 */
758 	if (t->current_filesystem->allocation_ptr == NULL) {
759 		r = setup_suitable_read_buffer(a);
760 		if (r != ARCHIVE_OK) {
761 			a->archive.state = ARCHIVE_STATE_FATAL;
762 			goto abort_read_data;
763 		}
764 	}
765 	t->entry_buff = t->current_filesystem->buff;
766 	t->entry_buff_size = t->current_filesystem->buff_size;
767 
768 	buffbytes = t->entry_buff_size;
769 	if ((int64_t)buffbytes > t->current_sparse->length)
770 		buffbytes = t->current_sparse->length;
771 
772 	if (t->current_sparse->length == 0)
773 		empty_sparse_region = 1;
774 
775 	/*
776 	 * Skip hole.
777 	 * TODO: Should we consider t->current_filesystem->xfer_align?
778 	 */
779 	if (t->current_sparse->offset > t->entry_total) {
780 		if (lseek(t->entry_fd,
781 		    (off_t)t->current_sparse->offset, SEEK_SET) < 0) {
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, ARCHIVE_ERRNO_MISC,
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), errno,
928 			    "Failed : %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), errno,
1042 			    "Failed : %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), errno,
1068 			    "Failed : %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 (t->max_filesystem_id > t->allocated_filesystem) {
1422 		size_t s;
1423 		void *p;
1424 
1425 		s = t->max_filesystem_id * 2;
1426 		p = realloc(t->filesystem_table,
1427 		        s * sizeof(*t->filesystem_table));
1428 		if (p == NULL) {
1429 			archive_set_error(&a->archive, ENOMEM,
1430 			    "Can't allocate tar data");
1431 			return (ARCHIVE_FATAL);
1432 		}
1433 		t->filesystem_table = (struct filesystem *)p;
1434 		t->allocated_filesystem = s;
1435 	}
1436 	t->current_filesystem_id = fid;
1437 	t->current_filesystem = &(t->filesystem_table[fid]);
1438 	t->current_filesystem->dev = dev;
1439 	t->current_filesystem->allocation_ptr = NULL;
1440 	t->current_filesystem->buff = NULL;
1441 
1442 	/* Setup the current filesystem properties which depend on
1443 	 * platform specific. */
1444 	return (setup_current_filesystem(a));
1445 }
1446 
1447 /*
1448  * Returns 1 if current filesystem is generated filesystem, 0 if it is not
1449  * or -1 if it is unknown.
1450  */
1451 int
archive_read_disk_current_filesystem_is_synthetic(struct archive * _a)1452 archive_read_disk_current_filesystem_is_synthetic(struct archive *_a)
1453 {
1454 	struct archive_read_disk *a = (struct archive_read_disk *)_a;
1455 
1456 	archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA,
1457 	    "archive_read_disk_current_filesystem");
1458 
1459 	return (a->tree->current_filesystem->synthetic);
1460 }
1461 
1462 /*
1463  * Returns 1 if current filesystem is remote filesystem, 0 if it is not
1464  * or -1 if it is unknown.
1465  */
1466 int
archive_read_disk_current_filesystem_is_remote(struct archive * _a)1467 archive_read_disk_current_filesystem_is_remote(struct archive *_a)
1468 {
1469 	struct archive_read_disk *a = (struct archive_read_disk *)_a;
1470 
1471 	archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA,
1472 	    "archive_read_disk_current_filesystem");
1473 
1474 	return (a->tree->current_filesystem->remote);
1475 }
1476 
1477 #if defined(_PC_REC_INCR_XFER_SIZE) && defined(_PC_REC_MAX_XFER_SIZE) &&\
1478 	defined(_PC_REC_MIN_XFER_SIZE) && defined(_PC_REC_XFER_ALIGN)
1479 static int
get_xfer_size(struct tree * t,int fd,const char * path)1480 get_xfer_size(struct tree *t, int fd, const char *path)
1481 {
1482 	t->current_filesystem->xfer_align = -1;
1483 	errno = 0;
1484 	if (fd >= 0) {
1485 		t->current_filesystem->incr_xfer_size =
1486 		    fpathconf(fd, _PC_REC_INCR_XFER_SIZE);
1487 		t->current_filesystem->max_xfer_size =
1488 		    fpathconf(fd, _PC_REC_MAX_XFER_SIZE);
1489 		t->current_filesystem->min_xfer_size =
1490 		    fpathconf(fd, _PC_REC_MIN_XFER_SIZE);
1491 		t->current_filesystem->xfer_align =
1492 		    fpathconf(fd, _PC_REC_XFER_ALIGN);
1493 	} else if (path != NULL) {
1494 		t->current_filesystem->incr_xfer_size =
1495 		    pathconf(path, _PC_REC_INCR_XFER_SIZE);
1496 		t->current_filesystem->max_xfer_size =
1497 		    pathconf(path, _PC_REC_MAX_XFER_SIZE);
1498 		t->current_filesystem->min_xfer_size =
1499 		    pathconf(path, _PC_REC_MIN_XFER_SIZE);
1500 		t->current_filesystem->xfer_align =
1501 		    pathconf(path, _PC_REC_XFER_ALIGN);
1502 	}
1503 	/* At least we need an alignment size. */
1504 	if (t->current_filesystem->xfer_align == -1)
1505 		return ((errno == EINVAL)?1:-1);
1506 	else
1507 		return (0);
1508 }
1509 #else
1510 static int
get_xfer_size(struct tree * t,int fd,const char * path)1511 get_xfer_size(struct tree *t, int fd, const char *path)
1512 {
1513 	(void)t; /* UNUSED */
1514 	(void)fd; /* UNUSED */
1515 	(void)path; /* UNUSED */
1516 	return (1);/* Not supported */
1517 }
1518 #endif
1519 
1520 #if defined(HAVE_STATVFS)
1521 static inline __LA_UNUSED void
set_statvfs_transfer_size(struct filesystem * fs,const struct statvfs * sfs)1522 set_statvfs_transfer_size(struct filesystem *fs, const struct statvfs *sfs)
1523 {
1524 	fs->xfer_align = sfs->f_frsize > 0 ? (long)sfs->f_frsize : -1;
1525 	fs->max_xfer_size = -1;
1526 #if defined(HAVE_STRUCT_STATVFS_F_IOSIZE)
1527 	fs->min_xfer_size = sfs->f_iosize > 0 ? (long)sfs->f_iosize : -1;
1528 	fs->incr_xfer_size = sfs->f_iosize > 0 ? (long)sfs->f_iosize : -1;
1529 #else
1530 	fs->min_xfer_size = sfs->f_bsize > 0 ? (long)sfs->f_bsize : -1;
1531 	fs->incr_xfer_size = sfs->f_bsize > 0 ? (long)sfs->f_bsize : -1;
1532 #endif
1533 }
1534 #endif
1535 
1536 #if defined(HAVE_STRUCT_STATFS)
1537 static inline __LA_UNUSED void
set_statfs_transfer_size(struct filesystem * fs,const struct statfs * sfs)1538 set_statfs_transfer_size(struct filesystem *fs, const struct statfs *sfs)
1539 {
1540 	fs->xfer_align = sfs->f_bsize > 0 ? (long)sfs->f_bsize : -1;
1541 	fs->max_xfer_size = -1;
1542 #if defined(HAVE_STRUCT_STATFS_F_IOSIZE)
1543 	fs->min_xfer_size = sfs->f_iosize > 0 ? (long)sfs->f_iosize : -1;
1544 	fs->incr_xfer_size = sfs->f_iosize > 0 ? (long)sfs->f_iosize : -1;
1545 #else
1546 	fs->min_xfer_size = sfs->f_bsize > 0 ? (long)sfs->f_bsize : -1;
1547 	fs->incr_xfer_size = sfs->f_bsize > 0 ? (long)sfs->f_bsize : -1;
1548 #endif
1549 }
1550 #endif
1551 
1552 #if defined(HAVE_STRUCT_STATFS) && defined(HAVE_STATFS) && \
1553     defined(HAVE_FSTATFS) && defined(MNT_LOCAL) && !defined(ST_LOCAL)
1554 
1555 /*
1556  * Gather current filesystem properties on FreeBSD, OpenBSD and Mac OS X.
1557  */
1558 static int
setup_current_filesystem(struct archive_read_disk * a)1559 setup_current_filesystem(struct archive_read_disk *a)
1560 {
1561 	struct tree *t = a->tree;
1562 	struct statfs sfs;
1563 #if defined(HAVE_GETVFSBYNAME) && defined(VFCF_SYNTHETIC)
1564 /* TODO: configure should set GETVFSBYNAME_ARG_TYPE to make
1565  * this accurate; some platforms have both and we need the one that's
1566  * used by getvfsbyname()
1567  *
1568  * Then the following would become:
1569  *  #if defined(GETVFSBYNAME_ARG_TYPE)
1570  *   GETVFSBYNAME_ARG_TYPE vfc;
1571  *  #endif
1572  */
1573 #  if defined(HAVE_STRUCT_XVFSCONF)
1574 	struct xvfsconf vfc;
1575 #  else
1576 	struct vfsconf vfc;
1577 #  endif
1578 #endif
1579 	int r, xr = 0;
1580 #if !defined(HAVE_STRUCT_STATFS_F_NAMEMAX)
1581 	long nm;
1582 #endif
1583 
1584 	t->current_filesystem->synthetic = -1;
1585 	t->current_filesystem->remote = -1;
1586 	if (tree_current_is_symblic_link_target(t)) {
1587 #if defined(HAVE_OPENAT)
1588 		/*
1589 		 * Get file system statistics on any directory
1590 		 * where current is.
1591 		 */
1592 		int fd = openat(tree_current_dir_fd(t),
1593 		    tree_current_access_path(t), O_RDONLY | O_CLOEXEC);
1594 		__archive_ensure_cloexec_flag(fd);
1595 		if (fd < 0) {
1596 			archive_set_error(&a->archive, errno,
1597 			    "openat failed");
1598 			return (ARCHIVE_FAILED);
1599 		}
1600 		r = fstatfs(fd, &sfs);
1601 		if (r == 0)
1602 			xr = get_xfer_size(t, fd, NULL);
1603 		close(fd);
1604 #else
1605 		if (tree_enter_working_dir(t) != 0) {
1606 			archive_set_error(&a->archive, errno, "fchdir failed");
1607 			return (ARCHIVE_FAILED);
1608 		}
1609 		r = statfs(tree_current_access_path(t), &sfs);
1610 		if (r == 0)
1611 			xr = get_xfer_size(t, -1, tree_current_access_path(t));
1612 #endif
1613 	} else {
1614 		r = fstatfs(tree_current_dir_fd(t), &sfs);
1615 		if (r == 0)
1616 			xr = get_xfer_size(t, tree_current_dir_fd(t), NULL);
1617 	}
1618 	if (r == -1 || xr == -1) {
1619 		archive_set_error(&a->archive, errno, "statfs failed");
1620 		return (ARCHIVE_FAILED);
1621 	} else if (xr == 1) {
1622 		/* pathconf(_PC_REX_*) operations are not supported. */
1623 		set_statfs_transfer_size(t->current_filesystem, &sfs);
1624 	}
1625 	if (sfs.f_flags & MNT_LOCAL)
1626 		t->current_filesystem->remote = 0;
1627 	else
1628 		t->current_filesystem->remote = 1;
1629 
1630 #if defined(HAVE_GETVFSBYNAME) && defined(VFCF_SYNTHETIC)
1631 	r = getvfsbyname(sfs.f_fstypename, &vfc);
1632 	if (r == -1) {
1633 		archive_set_error(&a->archive, errno, "getvfsbyname failed");
1634 		return (ARCHIVE_FAILED);
1635 	}
1636 	if (vfc.vfc_flags & VFCF_SYNTHETIC)
1637 		t->current_filesystem->synthetic = 1;
1638 	else
1639 		t->current_filesystem->synthetic = 0;
1640 #endif
1641 
1642 #if defined(MNT_NOATIME)
1643 	if (sfs.f_flags & MNT_NOATIME)
1644 		t->current_filesystem->noatime = 1;
1645 	else
1646 #endif
1647 		t->current_filesystem->noatime = 0;
1648 
1649 #if defined(USE_READDIR_R)
1650 	/* Set maximum filename length. */
1651 #if defined(HAVE_STRUCT_STATFS_F_NAMEMAX)
1652 	t->current_filesystem->name_max = sfs.f_namemax;
1653 #else
1654 # if defined(_PC_NAME_MAX)
1655 	/* Mac OS X does not have f_namemax in struct statfs. */
1656 	if (tree_current_is_symblic_link_target(t)) {
1657 		if (tree_enter_working_dir(t) != 0) {
1658 			archive_set_error(&a->archive, errno, "fchdir failed");
1659 			return (ARCHIVE_FAILED);
1660 		}
1661 		nm = pathconf(tree_current_access_path(t), _PC_NAME_MAX);
1662 	} else
1663 		nm = fpathconf(tree_current_dir_fd(t), _PC_NAME_MAX);
1664 # else
1665 	nm = -1;
1666 # endif
1667 	if (nm == -1)
1668 		t->current_filesystem->name_max = NAME_MAX;
1669 	else
1670 		t->current_filesystem->name_max = nm;
1671 #endif
1672 	if (t->current_filesystem->name_max == 0) {
1673 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1674 		    "Cannot determine name_max");
1675 		return (ARCHIVE_FAILED);
1676 	}
1677 #endif /* USE_READDIR_R */
1678 	return (ARCHIVE_OK);
1679 }
1680 
1681 #elif (defined(HAVE_STATVFS) || defined(HAVE_FSTATVFS)) && defined(ST_LOCAL)
1682 
1683 /*
1684  * Gather current filesystem properties on NetBSD
1685  */
1686 static int
setup_current_filesystem(struct archive_read_disk * a)1687 setup_current_filesystem(struct archive_read_disk *a)
1688 {
1689 	struct tree *t = a->tree;
1690 	struct statvfs svfs;
1691 	int r, xr = 0;
1692 
1693 	t->current_filesystem->synthetic = -1;
1694 	if (tree_enter_working_dir(t) != 0) {
1695 		archive_set_error(&a->archive, errno, "fchdir failed");
1696 		return (ARCHIVE_FAILED);
1697 	}
1698 	if (tree_current_is_symblic_link_target(t)) {
1699 		r = statvfs(tree_current_access_path(t), &svfs);
1700 		if (r == 0)
1701 			xr = get_xfer_size(t, -1, tree_current_access_path(t));
1702 	} else {
1703 #ifdef HAVE_FSTATVFS
1704 		r = fstatvfs(tree_current_dir_fd(t), &svfs);
1705 		if (r == 0)
1706 			xr = get_xfer_size(t, tree_current_dir_fd(t), NULL);
1707 #else
1708 		r = statvfs(".", &svfs);
1709 		if (r == 0)
1710 			xr = get_xfer_size(t, -1, ".");
1711 #endif
1712 	}
1713 	if (r == -1 || xr == -1) {
1714 		t->current_filesystem->remote = -1;
1715 		archive_set_error(&a->archive, errno, "statvfs failed");
1716 		return (ARCHIVE_FAILED);
1717 	} else if (xr == 1) {
1718 		/* Usually come here unless NetBSD supports _PC_REC_XFER_ALIGN
1719 		 * for pathconf() function. */
1720 		set_statvfs_transfer_size(t->current_filesystem, &svfs);
1721 	}
1722 	if (svfs.f_flag & ST_LOCAL)
1723 		t->current_filesystem->remote = 0;
1724 	else
1725 		t->current_filesystem->remote = 1;
1726 
1727 #if defined(ST_NOATIME)
1728 	if (svfs.f_flag & ST_NOATIME)
1729 		t->current_filesystem->noatime = 1;
1730 	else
1731 #endif
1732 		t->current_filesystem->noatime = 0;
1733 
1734 	/* Set maximum filename length. */
1735 	t->current_filesystem->name_max = svfs.f_namemax;
1736 	return (ARCHIVE_OK);
1737 }
1738 
1739 #elif defined(HAVE_SYS_STATFS_H) && defined(HAVE_LINUX_MAGIC_H) &&\
1740 	defined(HAVE_STATFS) && defined(HAVE_FSTATFS)
1741 /*
1742  * Note: statfs is deprecated since LSB 3.2
1743  */
1744 
1745 #ifndef CIFS_SUPER_MAGIC
1746 #define CIFS_SUPER_MAGIC 0xFF534D42
1747 #endif
1748 #ifndef DEVFS_SUPER_MAGIC
1749 #define DEVFS_SUPER_MAGIC 0x1373
1750 #endif
1751 
1752 /*
1753  * Gather current filesystem properties on Linux
1754  */
1755 static int
setup_current_filesystem(struct archive_read_disk * a)1756 setup_current_filesystem(struct archive_read_disk *a)
1757 {
1758 	struct tree *t = a->tree;
1759 	struct statfs sfs;
1760 #if defined(HAVE_STATVFS)
1761 	struct statvfs svfs;
1762 #endif
1763 	int r, vr = 0, xr = 0;
1764 
1765 	if (tree_current_is_symblic_link_target(t)) {
1766 #if defined(HAVE_OPENAT)
1767 		/*
1768 		 * Get file system statistics on any directory
1769 		 * where current is.
1770 		 */
1771 		int fd = openat(tree_current_dir_fd(t),
1772 		    tree_current_access_path(t), O_RDONLY | O_CLOEXEC);
1773 		__archive_ensure_cloexec_flag(fd);
1774 		if (fd < 0) {
1775 			archive_set_error(&a->archive, errno,
1776 			    "openat failed");
1777 			return (ARCHIVE_FAILED);
1778 		}
1779 #if defined(HAVE_FSTATVFS)
1780 		vr = fstatvfs(fd, &svfs);/* for f_flag, mount flags */
1781 #endif
1782 		r = fstatfs(fd, &sfs);
1783 		if (r == 0)
1784 			xr = get_xfer_size(t, fd, NULL);
1785 		close(fd);
1786 #else
1787 		if (tree_enter_working_dir(t) != 0) {
1788 			archive_set_error(&a->archive, errno, "fchdir failed");
1789 			return (ARCHIVE_FAILED);
1790 		}
1791 #if defined(HAVE_STATVFS)
1792 		vr = statvfs(tree_current_access_path(t), &svfs);
1793 #endif
1794 		r = statfs(tree_current_access_path(t), &sfs);
1795 		if (r == 0)
1796 			xr = get_xfer_size(t, -1, tree_current_access_path(t));
1797 #endif
1798 	} else {
1799 #ifdef HAVE_FSTATFS
1800 #if defined(HAVE_FSTATVFS)
1801 		vr = fstatvfs(tree_current_dir_fd(t), &svfs);
1802 #endif
1803 		r = fstatfs(tree_current_dir_fd(t), &sfs);
1804 		if (r == 0)
1805 			xr = get_xfer_size(t, tree_current_dir_fd(t), NULL);
1806 #else
1807 		if (tree_enter_working_dir(t) != 0) {
1808 			archive_set_error(&a->archive, errno, "fchdir failed");
1809 			return (ARCHIVE_FAILED);
1810 		}
1811 #if defined(HAVE_STATVFS)
1812 		vr = statvfs(".", &svfs);
1813 #endif
1814 		r = statfs(".", &sfs);
1815 		if (r == 0)
1816 			xr = get_xfer_size(t, -1, ".");
1817 #endif
1818 	}
1819 	if (r == -1 || xr == -1 || vr == -1) {
1820 		t->current_filesystem->synthetic = -1;
1821 		t->current_filesystem->remote = -1;
1822 		archive_set_error(&a->archive, errno, "statfs failed");
1823 		return (ARCHIVE_FAILED);
1824 	} else if (xr == 1) {
1825 		/* pathconf(_PC_REX_*) operations are not supported. */
1826 #if defined(HAVE_STATVFS)
1827 		set_statvfs_transfer_size(t->current_filesystem, &svfs);
1828 #else
1829 		set_statfs_transfer_size(t->current_filesystem, &sfs);
1830 #endif
1831 	}
1832 	switch (sfs.f_type) {
1833 	case AFS_SUPER_MAGIC:
1834 	case CIFS_SUPER_MAGIC:
1835 	case CODA_SUPER_MAGIC:
1836 	case NCP_SUPER_MAGIC:/* NetWare */
1837 	case NFS_SUPER_MAGIC:
1838 	case SMB_SUPER_MAGIC:
1839 		t->current_filesystem->remote = 1;
1840 		t->current_filesystem->synthetic = 0;
1841 		break;
1842 	case DEVFS_SUPER_MAGIC:
1843 	case PROC_SUPER_MAGIC:
1844 	case USBDEVICE_SUPER_MAGIC:
1845 		t->current_filesystem->remote = 0;
1846 		t->current_filesystem->synthetic = 1;
1847 		break;
1848 	default:
1849 		t->current_filesystem->remote = 0;
1850 		t->current_filesystem->synthetic = 0;
1851 		break;
1852 	}
1853 
1854 #if defined(ST_NOATIME)
1855 #if defined(HAVE_STATVFS)
1856 	if (svfs.f_flag & ST_NOATIME)
1857 #else
1858 	if (sfs.f_flags & ST_NOATIME)
1859 #endif
1860 		t->current_filesystem->noatime = 1;
1861 	else
1862 #endif
1863 		t->current_filesystem->noatime = 0;
1864 
1865 #if defined(USE_READDIR_R)
1866 	/* Set maximum filename length. */
1867 #if defined(HAVE_STATVFS)
1868 	t->current_filesystem->name_max = svfs.f_namemax;
1869 #else
1870 	t->current_filesystem->name_max = sfs.f_namelen;
1871 #endif
1872 	if (t->current_filesystem->name_max == 0) {
1873 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1874 		    "Cannot determine name_max");
1875 		return (ARCHIVE_FAILED);
1876 	}
1877 #endif
1878 	return (ARCHIVE_OK);
1879 }
1880 
1881 #elif defined(HAVE_SYS_STATVFS_H) &&\
1882 	(defined(HAVE_STATVFS) || defined(HAVE_FSTATVFS))
1883 
1884 /*
1885  * Gather current filesystem properties on other posix platform.
1886  */
1887 static int
setup_current_filesystem(struct archive_read_disk * a)1888 setup_current_filesystem(struct archive_read_disk *a)
1889 {
1890 	struct tree *t = a->tree;
1891 	struct statvfs svfs;
1892 	int r, xr = 0;
1893 
1894 	t->current_filesystem->synthetic = -1;/* Not supported */
1895 	t->current_filesystem->remote = -1;/* Not supported */
1896 	if (tree_current_is_symblic_link_target(t)) {
1897 #if defined(HAVE_OPENAT)
1898 		/*
1899 		 * Get file system statistics on any directory
1900 		 * where current is.
1901 		 */
1902 		int fd = openat(tree_current_dir_fd(t),
1903 		    tree_current_access_path(t), O_RDONLY | O_CLOEXEC);
1904 		__archive_ensure_cloexec_flag(fd);
1905 		if (fd < 0) {
1906 			archive_set_error(&a->archive, errno,
1907 			    "openat failed");
1908 			return (ARCHIVE_FAILED);
1909 		}
1910 		r = fstatvfs(fd, &svfs);
1911 		if (r == 0)
1912 			xr = get_xfer_size(t, fd, NULL);
1913 		close(fd);
1914 #else
1915 		if (tree_enter_working_dir(t) != 0) {
1916 			archive_set_error(&a->archive, errno, "fchdir failed");
1917 			return (ARCHIVE_FAILED);
1918 		}
1919 		r = statvfs(tree_current_access_path(t), &svfs);
1920 		if (r == 0)
1921 			xr = get_xfer_size(t, -1, tree_current_access_path(t));
1922 #endif
1923 	} else {
1924 #ifdef HAVE_FSTATVFS
1925 		r = fstatvfs(tree_current_dir_fd(t), &svfs);
1926 		if (r == 0)
1927 			xr = get_xfer_size(t, tree_current_dir_fd(t), NULL);
1928 #else
1929 		if (tree_enter_working_dir(t) != 0) {
1930 			archive_set_error(&a->archive, errno, "fchdir failed");
1931 			return (ARCHIVE_FAILED);
1932 		}
1933 		r = statvfs(".", &svfs);
1934 		if (r == 0)
1935 			xr = get_xfer_size(t, -1, ".");
1936 #endif
1937 	}
1938 	if (r == -1 || xr == -1) {
1939 		t->current_filesystem->synthetic = -1;
1940 		t->current_filesystem->remote = -1;
1941 		archive_set_error(&a->archive, errno, "statvfs failed");
1942 		return (ARCHIVE_FAILED);
1943 	} else if (xr == 1) {
1944 		/* pathconf(_PC_REX_*) operations are not supported. */
1945 		set_statvfs_transfer_size(t->current_filesystem, &svfs);
1946 	}
1947 
1948 #if defined(ST_NOATIME)
1949 	if (svfs.f_flag & ST_NOATIME)
1950 		t->current_filesystem->noatime = 1;
1951 	else
1952 #endif
1953 		t->current_filesystem->noatime = 0;
1954 
1955 #if defined(USE_READDIR_R)
1956 	/* Set maximum filename length. */
1957 	t->current_filesystem->name_max = svfs.f_namemax;
1958 	if (t->current_filesystem->name_max == 0) {
1959 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1960 		    "Cannot determine name_max");
1961 		return (ARCHIVE_FAILED);
1962 	}
1963 #endif
1964 	return (ARCHIVE_OK);
1965 }
1966 
1967 #else
1968 
1969 /*
1970  * Generic: Gather current filesystem properties.
1971  * TODO: Is this generic function really needed?
1972  */
1973 static int
setup_current_filesystem(struct archive_read_disk * a)1974 setup_current_filesystem(struct archive_read_disk *a)
1975 {
1976 	struct tree *t = a->tree;
1977 #if defined(_PC_NAME_MAX) && defined(USE_READDIR_R)
1978 	long nm;
1979 #endif
1980 	t->current_filesystem->synthetic = -1;/* Not supported */
1981 	t->current_filesystem->remote = -1;/* Not supported */
1982 	t->current_filesystem->noatime = 0;
1983 	(void)get_xfer_size(t, -1, ".");/* Dummy call to avoid build error. */
1984 	t->current_filesystem->xfer_align = -1;/* Unknown */
1985 	t->current_filesystem->max_xfer_size = -1;
1986 	t->current_filesystem->min_xfer_size = -1;
1987 	t->current_filesystem->incr_xfer_size = -1;
1988 
1989 #if defined(USE_READDIR_R)
1990 	/* Set maximum filename length. */
1991 #  if defined(_PC_NAME_MAX)
1992 	if (tree_current_is_symblic_link_target(t)) {
1993 		if (tree_enter_working_dir(t) != 0) {
1994 			archive_set_error(&a->archive, errno, "fchdir failed");
1995 			return (ARCHIVE_FAILED);
1996 		}
1997 		nm = pathconf(tree_current_access_path(t), _PC_NAME_MAX);
1998 	} else
1999 		nm = fpathconf(tree_current_dir_fd(t), _PC_NAME_MAX);
2000 	if (nm == -1)
2001 #  endif /* _PC_NAME_MAX */
2002 		/*
2003 		 * Some systems (HP-UX or others?) incorrectly defined
2004 		 * NAME_MAX macro to be a smaller value.
2005 		 */
2006 #  if defined(NAME_MAX) && NAME_MAX >= 255
2007 		t->current_filesystem->name_max = NAME_MAX;
2008 #  else
2009 		/* No way to get a trusted value of maximum filename
2010 		 * length. */
2011 		t->current_filesystem->name_max = PATH_MAX;
2012 #  endif /* NAME_MAX */
2013 #  if defined(_PC_NAME_MAX)
2014 	else
2015 		t->current_filesystem->name_max = nm;
2016 #  endif /* _PC_NAME_MAX */
2017 	if (t->current_filesystem->name_max == 0) {
2018 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2019 		    "Cannot determine name_max");
2020 		return (ARCHIVE_FAILED);
2021 	}
2022 #endif /* USE_READDIR_R */
2023 	return (ARCHIVE_OK);
2024 }
2025 
2026 #endif
2027 
2028 static int
close_and_restore_time(int fd,struct tree * t,struct restore_time * rt)2029 close_and_restore_time(int fd, struct tree *t, struct restore_time *rt)
2030 {
2031 #ifndef HAVE_UTIMES
2032 	(void)t; /* UNUSED */
2033 	(void)rt; /* UNUSED */
2034 	return (close(fd));
2035 #else
2036 #if defined(HAVE_FUTIMENS) && !defined(__CYGWIN__)
2037 	struct timespec timespecs[2];
2038 #endif
2039 	struct timeval times[2];
2040 
2041 	if ((t->flags & needsRestoreTimes) == 0 || rt->noatime) {
2042 		if (fd >= 0)
2043 			return (close(fd));
2044 		else
2045 			return (0);
2046 	}
2047 
2048 #if defined(HAVE_FUTIMENS) && !defined(__CYGWIN__)
2049 	timespecs[1].tv_sec = rt->mtime;
2050 	timespecs[1].tv_nsec = rt->mtime_nsec;
2051 
2052 	timespecs[0].tv_sec = rt->atime;
2053 	timespecs[0].tv_nsec = rt->atime_nsec;
2054 	/* futimens() is defined in POSIX.1-2008. */
2055 	if (futimens(fd, timespecs) == 0)
2056 		return (close(fd));
2057 #endif
2058 
2059 	times[1].tv_sec = rt->mtime;
2060 	times[1].tv_usec = rt->mtime_nsec / 1000;
2061 
2062 	times[0].tv_sec = rt->atime;
2063 	times[0].tv_usec = rt->atime_nsec / 1000;
2064 
2065 #if !defined(HAVE_FUTIMENS) && defined(HAVE_FUTIMES) && !defined(__CYGWIN__)
2066 	if (futimes(fd, times) == 0)
2067 		return (close(fd));
2068 #endif
2069 	close(fd);
2070 #if defined(HAVE_FUTIMESAT)
2071 	if (futimesat(tree_current_dir_fd(t), rt->name, times) == 0)
2072 		return (0);
2073 #endif
2074 #ifdef HAVE_LUTIMES
2075 	if (lutimes(rt->name, times) != 0)
2076 #else
2077 	if (AE_IFLNK != rt->filetype && utimes(rt->name, times) != 0)
2078 #endif
2079 		return (-1);
2080 #endif
2081 	return (0);
2082 }
2083 
2084 static int
open_on_current_dir(struct tree * t,const char * path,int flags)2085 open_on_current_dir(struct tree *t, const char *path, int flags)
2086 {
2087 #ifdef HAVE_OPENAT
2088 	return (openat(tree_current_dir_fd(t), path, flags));
2089 #else
2090 	if (tree_enter_working_dir(t) != 0)
2091 		return (-1);
2092 	return (open(path, flags));
2093 #endif
2094 }
2095 
2096 static int
tree_dup(int fd)2097 tree_dup(int fd)
2098 {
2099 	int new_fd;
2100 #ifdef F_DUPFD_CLOEXEC
2101 	static volatile int can_dupfd_cloexec = 1;
2102 
2103 	if (can_dupfd_cloexec) {
2104 		new_fd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
2105 		if (new_fd != -1)
2106 			return (new_fd);
2107 		/* Linux 2.6.18 - 2.6.23 declare F_DUPFD_CLOEXEC,
2108 		 * but it cannot be used. So we have to try dup(). */
2109 		/* We won't try F_DUPFD_CLOEXEC. */
2110 		can_dupfd_cloexec = 0;
2111 	}
2112 #endif /* F_DUPFD_CLOEXEC */
2113 	new_fd = dup(fd);
2114 	__archive_ensure_cloexec_flag(new_fd);
2115 	return (new_fd);
2116 }
2117 
2118 /*
2119  * Add a directory path to the current stack.
2120  */
2121 static void
tree_push(struct tree * t,const char * path,int filesystem_id,int64_t dev,int64_t ino,struct restore_time * rt)2122 tree_push(struct tree *t, const char *path, int filesystem_id,
2123     int64_t dev, int64_t ino, struct restore_time *rt)
2124 {
2125 	struct tree_entry *te;
2126 
2127 	te = calloc(1, sizeof(*te));
2128 	if (te == NULL)
2129 		__archive_errx(1, "Out of memory");
2130 	te->next = t->stack;
2131 	te->parent = t->current;
2132 	if (te->parent)
2133 		te->depth = te->parent->depth + 1;
2134 	t->stack = te;
2135 	archive_string_init(&te->name);
2136 	te->symlink_parent_fd = -1;
2137 	archive_strcpy(&te->name, path);
2138 	te->flags = needsDescent | needsOpen | needsAscent;
2139 	te->filesystem_id = filesystem_id;
2140 	te->dev = dev;
2141 	te->ino = ino;
2142 	te->dirname_length = t->dirname_length;
2143 	te->restore_time.name = te->name.s;
2144 	if (rt != NULL) {
2145 		te->restore_time.mtime = rt->mtime;
2146 		te->restore_time.mtime_nsec = rt->mtime_nsec;
2147 		te->restore_time.atime = rt->atime;
2148 		te->restore_time.atime_nsec = rt->atime_nsec;
2149 		te->restore_time.filetype = rt->filetype;
2150 		te->restore_time.noatime = rt->noatime;
2151 	}
2152 }
2153 
2154 /*
2155  * Append a name to the current dir path.
2156  */
2157 static void
tree_append(struct tree * t,const char * name,size_t name_length)2158 tree_append(struct tree *t, const char *name, size_t name_length)
2159 {
2160 	size_t size_needed;
2161 
2162 	t->path.s[t->dirname_length] = '\0';
2163 	t->path.length = t->dirname_length;
2164 	/* Strip trailing '/' from name, unless entire name is "/". */
2165 	while (name_length > 1 && name[name_length - 1] == '/')
2166 		name_length--;
2167 
2168 	/* Resize pathname buffer as needed. */
2169 	size_needed = name_length + t->dirname_length + 2;
2170 	archive_string_ensure(&t->path, size_needed);
2171 	/* Add a separating '/' if it's needed. */
2172 	if (t->dirname_length > 0 && t->path.s[archive_strlen(&t->path)-1] != '/')
2173 		archive_strappend_char(&t->path, '/');
2174 	t->basename = t->path.s + archive_strlen(&t->path);
2175 	archive_strncat(&t->path, name, name_length);
2176 	t->restore_time.name = t->basename;
2177 }
2178 
2179 /*
2180  * Open a directory tree for traversal.
2181  */
2182 static struct tree *
tree_open(const char * path,int symlink_mode,int restore_time)2183 tree_open(const char *path, int symlink_mode, int restore_time)
2184 {
2185 	struct tree *t;
2186 
2187 	if ((t = calloc(1, sizeof(*t))) == NULL)
2188 		return (NULL);
2189 	archive_string_init(&t->path);
2190 	archive_string_ensure(&t->path, 31);
2191 	t->initial_symlink_mode = symlink_mode;
2192 	return (tree_reopen(t, path, restore_time));
2193 }
2194 
2195 static struct tree *
tree_reopen(struct tree * t,const char * path,int restore_time)2196 tree_reopen(struct tree *t, const char *path, int restore_time)
2197 {
2198 #if defined(O_PATH)
2199 	/* Linux */
2200 	const int o_flag = O_PATH;
2201 #elif defined(O_SEARCH)
2202 	/* SunOS */
2203 	const int o_flag = O_SEARCH;
2204 #elif defined(__FreeBSD__) && defined(O_EXEC)
2205 	/* FreeBSD */
2206 	const int o_flag = O_EXEC;
2207 #endif
2208 
2209 	t->flags = (restore_time != 0)?needsRestoreTimes:0;
2210 	t->flags |= onInitialDir;
2211 	t->visit_type = 0;
2212 	t->tree_errno = 0;
2213 	t->dirname_length = 0;
2214 	t->depth = 0;
2215 	t->descend = 0;
2216 	t->current = NULL;
2217 	t->d = INVALID_DIR_HANDLE;
2218 	t->symlink_mode = t->initial_symlink_mode;
2219 	archive_string_empty(&t->path);
2220 	t->entry_fd = -1;
2221 	t->entry_eof = 0;
2222 	t->entry_remaining_bytes = 0;
2223 	t->initial_filesystem_id = -1;
2224 
2225 	/* First item is set up a lot like a symlink traversal. */
2226 	tree_push(t, path, 0, 0, 0, NULL);
2227 	t->stack->flags = needsFirstVisit;
2228 	t->maxOpenCount = t->openCount = 1;
2229 	t->initial_dir_fd = open(".", O_RDONLY | O_CLOEXEC);
2230 #if defined(O_PATH) || defined(O_SEARCH) || \
2231  (defined(__FreeBSD__) && defined(O_EXEC))
2232 	/*
2233 	 * Most likely reason to fail opening "." is that it's not readable,
2234 	 * so try again for execute. The consequences of not opening this are
2235 	 * unhelpful and unnecessary errors later.
2236 	 */
2237 	if (t->initial_dir_fd < 0)
2238 		t->initial_dir_fd = open(".", o_flag | O_CLOEXEC);
2239 #endif
2240 	__archive_ensure_cloexec_flag(t->initial_dir_fd);
2241 	t->working_dir_fd = tree_dup(t->initial_dir_fd);
2242 	return (t);
2243 }
2244 
2245 static int
tree_descent(struct tree * t)2246 tree_descent(struct tree *t)
2247 {
2248 	int flag, new_fd, r = 0;
2249 
2250 	t->dirname_length = archive_strlen(&t->path);
2251 	flag = O_RDONLY | O_CLOEXEC;
2252 #if defined(O_DIRECTORY)
2253 	flag |= O_DIRECTORY;
2254 #endif
2255 	new_fd = open_on_current_dir(t, t->stack->name.s, flag);
2256 	__archive_ensure_cloexec_flag(new_fd);
2257 	if (new_fd < 0) {
2258 		t->tree_errno = errno;
2259 		r = TREE_ERROR_DIR;
2260 	} else {
2261 		t->depth++;
2262 		/* If it is a link, set up fd for the ascent. */
2263 		if (t->stack->flags & isDirLink) {
2264 			t->stack->symlink_parent_fd = t->working_dir_fd;
2265 			t->openCount++;
2266 			if (t->openCount > t->maxOpenCount)
2267 				t->maxOpenCount = t->openCount;
2268 		} else
2269 			close(t->working_dir_fd);
2270 		/* Renew the current working directory. */
2271 		t->working_dir_fd = new_fd;
2272 		t->flags &= ~onWorkingDir;
2273 	}
2274 	return (r);
2275 }
2276 
2277 /*
2278  * We've finished a directory; ascend back to the parent.
2279  */
2280 static int
tree_ascend(struct tree * t)2281 tree_ascend(struct tree *t)
2282 {
2283 	struct tree_entry *te;
2284 	int new_fd, r = 0, prev_dir_fd;
2285 
2286 	te = t->stack;
2287 	prev_dir_fd = t->working_dir_fd;
2288 	if (te->flags & isDirLink)
2289 		new_fd = te->symlink_parent_fd;
2290 	else {
2291 		new_fd = open_on_current_dir(t, "..", O_RDONLY | O_CLOEXEC);
2292 		__archive_ensure_cloexec_flag(new_fd);
2293 	}
2294 	if (new_fd < 0) {
2295 		t->tree_errno = errno;
2296 		r = TREE_ERROR_FATAL;
2297 	} else {
2298 		/* Renew the current working directory. */
2299 		t->working_dir_fd = new_fd;
2300 		t->flags &= ~onWorkingDir;
2301 		/* Current directory has been changed, we should
2302 		 * close an fd of previous working directory. */
2303 		close_and_restore_time(prev_dir_fd, t, &te->restore_time);
2304 		if (te->flags & isDirLink) {
2305 			t->openCount--;
2306 			te->symlink_parent_fd = -1;
2307 		}
2308 		t->depth--;
2309 	}
2310 	return (r);
2311 }
2312 
2313 /*
2314  * Return to the initial directory where tree_open() was performed.
2315  */
2316 static int
tree_enter_initial_dir(struct tree * t)2317 tree_enter_initial_dir(struct tree *t)
2318 {
2319 	int r = 0;
2320 
2321 	if ((t->flags & onInitialDir) == 0) {
2322 		r = fchdir(t->initial_dir_fd);
2323 		if (r == 0) {
2324 			t->flags &= ~onWorkingDir;
2325 			t->flags |= onInitialDir;
2326 		}
2327 	}
2328 	return (r);
2329 }
2330 
2331 /*
2332  * Restore working directory of directory traversals.
2333  */
2334 static int
tree_enter_working_dir(struct tree * t)2335 tree_enter_working_dir(struct tree *t)
2336 {
2337 	int r = 0;
2338 
2339 	/*
2340 	 * Change the current directory if really needed.
2341 	 * Sometimes this is unneeded when we did not do
2342 	 * descent.
2343 	 */
2344 	if (t->depth > 0 && (t->flags & onWorkingDir) == 0) {
2345 		r = fchdir(t->working_dir_fd);
2346 		if (r == 0) {
2347 			t->flags &= ~onInitialDir;
2348 			t->flags |= onWorkingDir;
2349 		}
2350 	}
2351 	return (r);
2352 }
2353 
2354 static int
tree_current_dir_fd(struct tree * t)2355 tree_current_dir_fd(struct tree *t)
2356 {
2357 	return (t->working_dir_fd);
2358 }
2359 
2360 /*
2361  * Pop the working stack.
2362  */
2363 static void
tree_pop(struct tree * t)2364 tree_pop(struct tree *t)
2365 {
2366 	struct tree_entry *te;
2367 
2368 	t->path.s[t->dirname_length] = '\0';
2369 	t->path.length = t->dirname_length;
2370 	if (t->stack == t->current && t->current != NULL)
2371 		t->current = t->current->parent;
2372 	te = t->stack;
2373 	t->stack = te->next;
2374 	t->dirname_length = te->dirname_length;
2375 	t->basename = t->path.s + t->dirname_length;
2376 	while (t->basename[0] == '/')
2377 		t->basename++;
2378 	archive_string_free(&te->name);
2379 	free(te);
2380 }
2381 
2382 /*
2383  * Get the next item in the tree traversal.
2384  */
2385 static int
tree_next(struct tree * t)2386 tree_next(struct tree *t)
2387 {
2388 	int r;
2389 
2390 	while (t->stack != NULL) {
2391 		/* If there's an open dir, get the next entry from there. */
2392 		if (t->d != INVALID_DIR_HANDLE) {
2393 			r = tree_dir_next_posix(t);
2394 			if (r == 0)
2395 				continue;
2396 			return (r);
2397 		}
2398 
2399 		if (t->stack->flags & needsFirstVisit) {
2400 			/* Top stack item needs a regular visit. */
2401 			t->current = t->stack;
2402 			tree_append(t, t->stack->name.s,
2403 			    archive_strlen(&(t->stack->name)));
2404 			/* t->dirname_length = t->path_length; */
2405 			/* tree_pop(t); */
2406 			t->stack->flags &= ~needsFirstVisit;
2407 			return (t->visit_type = TREE_REGULAR);
2408 		} else if (t->stack->flags & needsDescent) {
2409 			/* Top stack item is dir to descend into. */
2410 			t->current = t->stack;
2411 			tree_append(t, t->stack->name.s,
2412 			    archive_strlen(&(t->stack->name)));
2413 			t->stack->flags &= ~needsDescent;
2414 			r = tree_descent(t);
2415 			if (r != 0) {
2416 				tree_pop(t);
2417 				t->visit_type = r;
2418 			} else
2419 				t->visit_type = TREE_POSTDESCENT;
2420 			return (t->visit_type);
2421 		} else if (t->stack->flags & needsOpen) {
2422 			t->stack->flags &= ~needsOpen;
2423 			r = tree_dir_next_posix(t);
2424 			if (r == 0)
2425 				continue;
2426 			return (r);
2427 		} else if (t->stack->flags & needsAscent) {
2428 		        /* Top stack item is dir and we're done with it. */
2429 			r = tree_ascend(t);
2430 			tree_pop(t);
2431 			t->visit_type = r != 0 ? r : TREE_POSTASCENT;
2432 			return (t->visit_type);
2433 		} else {
2434 			/* Top item on stack is dead. */
2435 			tree_pop(t);
2436 			t->flags &= ~hasLstat;
2437 			t->flags &= ~hasStat;
2438 		}
2439 	}
2440 	return (t->visit_type = 0);
2441 }
2442 
2443 static int
tree_dir_next_posix(struct tree * t)2444 tree_dir_next_posix(struct tree *t)
2445 {
2446 	int r;
2447 	const char *name;
2448 	size_t namelen;
2449 
2450 	if (t->d == NULL) {
2451 #if defined(USE_READDIR_R)
2452 		size_t dirent_size;
2453 #endif
2454 
2455 #if defined(HAVE_FDOPENDIR)
2456 		t->d = fdopendir(tree_dup(t->working_dir_fd));
2457 #else /* HAVE_FDOPENDIR */
2458 		if (tree_enter_working_dir(t) == 0) {
2459 			t->d = opendir(".");
2460 #ifdef HAVE_DIRFD
2461 			__archive_ensure_cloexec_flag(dirfd(t->d));
2462 #endif
2463 		}
2464 #endif /* HAVE_FDOPENDIR */
2465 		if (t->d == NULL) {
2466 			r = tree_ascend(t); /* Undo "chdir" */
2467 			tree_pop(t);
2468 			t->tree_errno = errno;
2469 			t->visit_type = r != 0 ? r : TREE_ERROR_DIR;
2470 			return (t->visit_type);
2471 		}
2472 #if defined(USE_READDIR_R)
2473 		dirent_size = offsetof(struct dirent, d_name) +
2474 		  t->filesystem_table[t->current->filesystem_id].name_max + 1;
2475 		if (t->dirent == NULL || t->dirent_allocated < dirent_size) {
2476 			free(t->dirent);
2477 			t->dirent = malloc(dirent_size);
2478 			if (t->dirent == NULL) {
2479 				closedir(t->d);
2480 				t->d = INVALID_DIR_HANDLE;
2481 				(void)tree_ascend(t);
2482 				tree_pop(t);
2483 				t->tree_errno = ENOMEM;
2484 				t->visit_type = TREE_ERROR_DIR;
2485 				return (t->visit_type);
2486 			}
2487 			t->dirent_allocated = dirent_size;
2488 		}
2489 #endif /* USE_READDIR_R */
2490 	}
2491 	for (;;) {
2492 		errno = 0;
2493 #if defined(USE_READDIR_R)
2494 		r = readdir_r(t->d, t->dirent, &t->de);
2495 #ifdef _AIX
2496 		/* Note: According to the man page, return value 9 indicates
2497 		 * that the readdir_r was not successful and the error code
2498 		 * is set to the global errno variable. And then if the end
2499 		 * of directory entries was reached, the return value is 9
2500 		 * and the third parameter is set to NULL and errno is
2501 		 * unchanged. */
2502 		if (r == 9)
2503 			r = errno;
2504 #endif /* _AIX */
2505 		if (r != 0 || t->de == NULL) {
2506 #else
2507 		t->de = readdir(t->d);
2508 		if (t->de == NULL) {
2509 			r = errno;
2510 #endif
2511 			closedir(t->d);
2512 			t->d = INVALID_DIR_HANDLE;
2513 			if (r != 0) {
2514 				t->tree_errno = r;
2515 				t->visit_type = TREE_ERROR_DIR;
2516 				return (t->visit_type);
2517 			} else
2518 				return (0);
2519 		}
2520 		name = t->de->d_name;
2521 		namelen = D_NAMELEN(t->de);
2522 		t->flags &= ~hasLstat;
2523 		t->flags &= ~hasStat;
2524 		if (name[0] == '.' && name[1] == '\0')
2525 			continue;
2526 		if (name[0] == '.' && name[1] == '.' && name[2] == '\0')
2527 			continue;
2528 		tree_append(t, name, namelen);
2529 		return (t->visit_type = TREE_REGULAR);
2530 	}
2531 }
2532 
2533 
2534 /*
2535  * Get the stat() data for the entry just returned from tree_next().
2536  */
2537 static const struct stat *
2538 tree_current_stat(struct tree *t)
2539 {
2540 	if (!(t->flags & hasStat)) {
2541 #ifdef HAVE_FSTATAT
2542 		if (fstatat(tree_current_dir_fd(t),
2543 		    tree_current_access_path(t), &t->st, 0) != 0)
2544 #else
2545 		if (tree_enter_working_dir(t) != 0)
2546 			return NULL;
2547 		if (la_stat(tree_current_access_path(t), &t->st) != 0)
2548 #endif
2549 			return NULL;
2550 		t->flags |= hasStat;
2551 	}
2552 	return (&t->st);
2553 }
2554 
2555 /*
2556  * Get the lstat() data for the entry just returned from tree_next().
2557  */
2558 static const struct stat *
2559 tree_current_lstat(struct tree *t)
2560 {
2561 	if (!(t->flags & hasLstat)) {
2562 #ifdef HAVE_FSTATAT
2563 		if (fstatat(tree_current_dir_fd(t),
2564 		    tree_current_access_path(t), &t->lst,
2565 		    AT_SYMLINK_NOFOLLOW) != 0)
2566 #else
2567 		if (tree_enter_working_dir(t) != 0)
2568 			return NULL;
2569 #ifdef HAVE_LSTAT
2570 		if (lstat(tree_current_access_path(t), &t->lst) != 0)
2571 #else
2572 		if (la_stat(tree_current_access_path(t), &t->lst) != 0)
2573 #endif
2574 #endif
2575 			return NULL;
2576 		t->flags |= hasLstat;
2577 	}
2578 	return (&t->lst);
2579 }
2580 
2581 /*
2582  * Test whether current entry is a dir or link to a dir.
2583  */
2584 static int
2585 tree_current_is_dir(struct tree *t)
2586 {
2587 	const struct stat *st;
2588 	/*
2589 	 * If we already have lstat() info, then try some
2590 	 * cheap tests to determine if this is a dir.
2591 	 */
2592 	if (t->flags & hasLstat) {
2593 		/* If lstat() says it's a dir, it must be a dir. */
2594 		st = tree_current_lstat(t);
2595 		if (st == NULL)
2596 			return 0;
2597 		if (S_ISDIR(st->st_mode))
2598 			return 1;
2599 		/* Not a dir; might be a link to a dir. */
2600 		/* If it's not a link, then it's not a link to a dir. */
2601 		if (!S_ISLNK(st->st_mode))
2602 			return 0;
2603 		/*
2604 		 * It's a link, but we don't know what it's a link to,
2605 		 * so we'll have to use stat().
2606 		 */
2607 	}
2608 
2609 	st = tree_current_stat(t);
2610 	/* If we can't stat it, it's not a dir. */
2611 	if (st == NULL)
2612 		return 0;
2613 	/* Use the definitive test.  Hopefully this is cached. */
2614 	return (S_ISDIR(st->st_mode));
2615 }
2616 
2617 /*
2618  * Test whether current entry is a physical directory.  Usually, we
2619  * already have at least one of stat() or lstat() in memory, so we
2620  * use tricks to try to avoid an extra trip to the disk.
2621  */
2622 static int
2623 tree_current_is_physical_dir(struct tree *t)
2624 {
2625 	const struct stat *st;
2626 
2627 	/*
2628 	 * If stat() says it isn't a dir, then it's not a dir.
2629 	 * If stat() data is cached, this check is free, so do it first.
2630 	 */
2631 	if (t->flags & hasStat) {
2632 		st = tree_current_stat(t);
2633 		if (st == NULL)
2634 			return (0);
2635 		if (!S_ISDIR(st->st_mode))
2636 			return (0);
2637 	}
2638 
2639 	/*
2640 	 * Either stat() said it was a dir (in which case, we have
2641 	 * to determine whether it's really a link to a dir) or
2642 	 * stat() info wasn't available.  So we use lstat(), which
2643 	 * hopefully is already cached.
2644 	 */
2645 
2646 	st = tree_current_lstat(t);
2647 	/* If we can't stat it, it's not a dir. */
2648 	if (st == NULL)
2649 		return 0;
2650 	/* Use the definitive test.  Hopefully this is cached. */
2651 	return (S_ISDIR(st->st_mode));
2652 }
2653 
2654 /*
2655  * Test whether the same file has been in the tree as its parent.
2656  */
2657 static int
2658 tree_target_is_same_as_parent(struct tree *t, const struct stat *st)
2659 {
2660 	struct tree_entry *te;
2661 
2662 	for (te = t->current->parent; te != NULL; te = te->parent) {
2663 		if (te->dev == (int64_t)st->st_dev &&
2664 		    te->ino == (int64_t)st->st_ino)
2665 			return (1);
2666 	}
2667 	return (0);
2668 }
2669 
2670 /*
2671  * Test whether the current file is symbolic link target and
2672  * on the other filesystem.
2673  */
2674 static int
2675 tree_current_is_symblic_link_target(struct tree *t)
2676 {
2677 	static const struct stat *lst, *st;
2678 
2679 	lst = tree_current_lstat(t);
2680 	st = tree_current_stat(t);
2681 	return (st != NULL && lst != NULL &&
2682 	    (int64_t)st->st_dev == t->current_filesystem->dev &&
2683 	    st->st_dev != lst->st_dev);
2684 }
2685 
2686 /*
2687  * Return the access path for the entry just returned from tree_next().
2688  */
2689 static const char *
2690 tree_current_access_path(struct tree *t)
2691 {
2692 	return (t->basename);
2693 }
2694 
2695 /*
2696  * Return the full path for the entry just returned from tree_next().
2697  */
2698 static const char *
2699 tree_current_path(struct tree *t)
2700 {
2701 	return (t->path.s);
2702 }
2703 
2704 /*
2705  * Terminate the traversal.
2706  */
2707 static void
2708 tree_close(struct tree *t)
2709 {
2710 
2711 	if (t == NULL)
2712 		return;
2713 	if (t->entry_fd >= 0) {
2714 		close_and_restore_time(t->entry_fd, t, &t->restore_time);
2715 		t->entry_fd = -1;
2716 	}
2717 	/* Close the handle of readdir(). */
2718 	if (t->d != INVALID_DIR_HANDLE) {
2719 		closedir(t->d);
2720 		t->d = INVALID_DIR_HANDLE;
2721 	}
2722 	/* Release anything remaining in the stack. */
2723 	while (t->stack != NULL) {
2724 		if (t->stack->flags & isDirLink)
2725 			close(t->stack->symlink_parent_fd);
2726 		tree_pop(t);
2727 	}
2728 	if (t->working_dir_fd >= 0) {
2729 		close(t->working_dir_fd);
2730 		t->working_dir_fd = -1;
2731 	}
2732 	if (t->initial_dir_fd >= 0) {
2733 		close(t->initial_dir_fd);
2734 		t->initial_dir_fd = -1;
2735 	}
2736 }
2737 
2738 /*
2739  * Release any resources.
2740  */
2741 static void
2742 tree_free(struct tree *t)
2743 {
2744 	int i;
2745 
2746 	if (t == NULL)
2747 		return;
2748 	archive_string_free(&t->path);
2749 #if defined(USE_READDIR_R)
2750 	free(t->dirent);
2751 #endif
2752 	free(t->sparse_list);
2753 	for (i = 0; i < t->max_filesystem_id; i++)
2754 		free(t->filesystem_table[i].allocation_ptr);
2755 	free(t->filesystem_table);
2756 	free(t);
2757 }
2758 
2759 #endif
2760