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