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