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