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