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