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