OLD | NEW |
(Empty) | |
| 1 /*- |
| 2 * Copyright (c) 1990, 1993, 1994 |
| 3 * The Regents of the University of California. All rights reserved. |
| 4 * |
| 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions |
| 7 * are met: |
| 8 * 1. Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. |
| 10 * 2. Redistributions in binary form must reproduce the above copyright |
| 11 * notice, this list of conditions and the following disclaimer in the |
| 12 * documentation and/or other materials provided with the distribution. |
| 13 * 4. Neither the name of the University nor the names of its contributors |
| 14 * may be used to endorse or promote products derived from this software |
| 15 * without specific prior written permission. |
| 16 * |
| 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 27 * SUCH DAMAGE. |
| 28 * |
| 29 * $OpenBSD: fts.c,v 1.22 1999/10/03 19:22:22 millert Exp $ |
| 30 */ |
| 31 |
| 32 #if 0 |
| 33 #if defined(LIBC_SCCS) && !defined(lint) |
| 34 static char sccsid[] = "@(#)fts.c 8.6 (Berkeley) 8/14/94"; |
| 35 #endif /* LIBC_SCCS and not lint */ |
| 36 #endif |
| 37 |
| 38 #include <sys/cdefs.h> |
| 39 __FBSDID("$FreeBSD: release/10.1.0/lib/libc/gen/fts.c 264172 2014-04-05 \ |
| 40 20:26:17Z jilles $"); |
| 41 |
| 42 #include <sys/param.h> |
| 43 #include <sys/stat.h> |
| 44 |
| 45 #include <errno.h> |
| 46 #include <fcntl.h> |
| 47 #include <fts.h> |
| 48 #include <stdlib.h> |
| 49 #include <string.h> |
| 50 #include <unistd.h> |
| 51 |
| 52 #include <sys/dirent.h> |
| 53 |
| 54 // Pull from freebsd 10.1 sys/mount.h |
| 55 /* |
| 56 * NOTE: When changing statfs structure, mount structure, MNT_* flags or |
| 57 * MNTK_* flags also update DDB show mount command in vfs_subr.c. |
| 58 */ |
| 59 |
| 60 typedef struct fsid { int32_t val[2]; } fsid_t; /* filesystem id type */ |
| 61 |
| 62 /* |
| 63 * filesystem statistics |
| 64 */ |
| 65 #define MFSNAMELEN 16 /* length of type name including null */ |
| 66 #define MNAMELEN 88 /* size of on/from name bufs */ |
| 67 #define STATFS_VERSION 0x20030518 /* current version number */ |
| 68 struct statfs { |
| 69 uint32_t f_version; /* structure version number */ |
| 70 uint32_t f_type; /* type of filesystem */ |
| 71 uint64_t f_flags; /* copy of mount exported flags */ |
| 72 uint64_t f_bsize; /* filesystem fragment size */ |
| 73 uint64_t f_iosize; /* optimal transfer block size */ |
| 74 uint64_t f_blocks; /* total data blocks in filesystem */ |
| 75 uint64_t f_bfree; /* free blocks in filesystem */ |
| 76 int64_t f_bavail; /* free blocks avail to non-superuser */ |
| 77 uint64_t f_files; /* total file nodes in filesystem */ |
| 78 int64_t f_ffree; /* free nodes avail to non-superuser */ |
| 79 uint64_t f_syncwrites; /* count of sync writes since mount */ |
| 80 uint64_t f_asyncwrites; /* count of async writes since mount */ |
| 81 uint64_t f_syncreads; /* count of sync reads since mount */ |
| 82 uint64_t f_asyncreads; /* count of async reads since mount */ |
| 83 uint64_t f_spare[10]; /* unused spare */ |
| 84 uint32_t f_namemax; /* maximum filename length */ |
| 85 uid_t f_owner; /* user that mounted the filesystem */ |
| 86 fsid_t f_fsid; /* filesystem id */ |
| 87 char f_charspare[80]; /* spare string space */ |
| 88 char f_fstypename[MFSNAMELEN]; /* filesystem type name */ |
| 89 char f_mntfromname[MNAMELEN]; /* mounted filesystem */ |
| 90 char f_mntonname[MNAMELEN]; /* directory on which mounted */ |
| 91 }; |
| 92 |
| 93 static FTSENT *fts_alloc(FTS *, char *, size_t); |
| 94 static FTSENT *fts_build(FTS *, int); |
| 95 static void fts_lfree(FTSENT *); |
| 96 static void fts_load(FTS *, FTSENT *); |
| 97 static size_t fts_maxarglen(char * const *); |
| 98 static void fts_padjust(FTS *, FTSENT *); |
| 99 static int fts_palloc(FTS *, size_t); |
| 100 static FTSENT *fts_sort(FTS *, FTSENT *, size_t); |
| 101 static int fts_stat(FTS *, FTSENT *, int); |
| 102 static int fts_safe_changedir(FTS *, FTSENT *, int, char *); |
| 103 static int fts_ufslinks(FTS *, const FTSENT *); |
| 104 |
| 105 #define ISDOT(a) (a[0] == '.' && (!a[1] || (a[1] == '.' && !a[2]))) |
| 106 |
| 107 #define CLR(opt) (sp->fts_options &= ~(opt)) |
| 108 #define ISSET(opt) (sp->fts_options & (opt)) |
| 109 #define SET(opt) (sp->fts_options |= (opt)) |
| 110 |
| 111 #define FCHDIR(sp, fd) (!ISSET(FTS_NOCHDIR) && fchdir(fd)) |
| 112 |
| 113 /* fts_build flags */ |
| 114 #define BCHILD 1 /* fts_children */ |
| 115 #define BNAMES 2 /* fts_children, names only */ |
| 116 #define BREAD 3 /* fts_read */ |
| 117 |
| 118 /* |
| 119 * Internal representation of an FTS, including extra implementation |
| 120 * details. The FTS returned from fts_open points to this structure's |
| 121 * ftsp_fts member (and can be cast to an _fts_private as required) |
| 122 */ |
| 123 struct _fts_private { |
| 124 FTS ftsp_fts; |
| 125 struct statfs ftsp_statfs; |
| 126 dev_t ftsp_dev; |
| 127 int ftsp_linksreliable; |
| 128 }; |
| 129 |
| 130 /* |
| 131 * The "FTS_NOSTAT" option can avoid a lot of calls to stat(2) if it |
| 132 * knows that a directory could not possibly have subdirectories. This |
| 133 * is decided by looking at the link count: a subdirectory would |
| 134 * increment its parent's link count by virtue of its own ".." entry. |
| 135 * This assumption only holds for UFS-like filesystems that implement |
| 136 * links and directories this way, so we must punt for others. |
| 137 */ |
| 138 |
| 139 static const char *ufslike_filesystems[] = { |
| 140 "ufs", |
| 141 "zfs", |
| 142 "nfs", |
| 143 "nfs4", |
| 144 "ext2fs", |
| 145 0 |
| 146 }; |
| 147 |
| 148 FTS * |
| 149 fts_open(argv, options, compar) |
| 150 char * const *argv; |
| 151 int options; |
| 152 int (*compar)(const FTSENT * const *, const FTSENT * const *); |
| 153 { |
| 154 struct _fts_private *priv; |
| 155 FTS *sp; |
| 156 FTSENT *p, *root; |
| 157 FTSENT *parent, *tmp; |
| 158 size_t len, nitems; |
| 159 |
| 160 /* Options check. */ |
| 161 if (options & ~FTS_OPTIONMASK) { |
| 162 errno = EINVAL; |
| 163 return (NULL); |
| 164 } |
| 165 |
| 166 /* fts_open() requires at least one path */ |
| 167 if (*argv == NULL) { |
| 168 errno = EINVAL; |
| 169 return (NULL); |
| 170 } |
| 171 |
| 172 /* Allocate/initialize the stream. */ |
| 173 if ((priv = calloc(1, sizeof(*priv))) == NULL) |
| 174 return (NULL); |
| 175 sp = &priv->ftsp_fts; |
| 176 sp->fts_compar = compar; |
| 177 sp->fts_options = options; |
| 178 |
| 179 /* Shush, GCC. */ |
| 180 tmp = NULL; |
| 181 |
| 182 /* Logical walks turn on NOCHDIR; symbolic links are too hard. */ |
| 183 if (ISSET(FTS_LOGICAL)) |
| 184 SET(FTS_NOCHDIR); |
| 185 |
| 186 /* |
| 187 * Start out with 1K of path space, and enough, in any case, |
| 188 * to hold the user's paths. |
| 189 */ |
| 190 if (fts_palloc(sp, MAX(fts_maxarglen(argv), MAXPATHLEN))) |
| 191 goto mem1; |
| 192 |
| 193 /* Allocate/initialize root's parent. */ |
| 194 if ((parent = fts_alloc(sp, "", 0)) == NULL) |
| 195 goto mem2; |
| 196 parent->fts_level = FTS_ROOTPARENTLEVEL; |
| 197 |
| 198 /* Allocate/initialize root(s). */ |
| 199 for (root = NULL, nitems = 0; *argv != NULL; ++argv, ++nitems) { |
| 200 len = strlen(*argv); |
| 201 |
| 202 p = fts_alloc(sp, *argv, len); |
| 203 p->fts_level = FTS_ROOTLEVEL; |
| 204 p->fts_parent = parent; |
| 205 p->fts_accpath = p->fts_name; |
| 206 p->fts_info = fts_stat(sp, p, ISSET(FTS_COMFOLLOW)); |
| 207 |
| 208 /* Command-line "." and ".." are real directories. */ |
| 209 if (p->fts_info == FTS_DOT) |
| 210 p->fts_info = FTS_D; |
| 211 |
| 212 /* |
| 213 * If comparison routine supplied, traverse in sorted |
| 214 * order; otherwise traverse in the order specified. |
| 215 */ |
| 216 if (compar) { |
| 217 p->fts_link = root; |
| 218 root = p; |
| 219 } else { |
| 220 p->fts_link = NULL; |
| 221 if (root == NULL) |
| 222 tmp = root = p; |
| 223 else { |
| 224 tmp->fts_link = p; |
| 225 tmp = p; |
| 226 } |
| 227 } |
| 228 } |
| 229 if (compar && nitems > 1) |
| 230 root = fts_sort(sp, root, nitems); |
| 231 |
| 232 /* |
| 233 * Allocate a dummy pointer and make fts_read think that we've just |
| 234 * finished the node before the root(s); set p->fts_info to FTS_INIT |
| 235 * so that everything about the "current" node is ignored. |
| 236 */ |
| 237 if ((sp->fts_cur = fts_alloc(sp, "", 0)) == NULL) |
| 238 goto mem3; |
| 239 sp->fts_cur->fts_link = root; |
| 240 sp->fts_cur->fts_info = FTS_INIT; |
| 241 |
| 242 /* |
| 243 * If using chdir(2), grab a file descriptor pointing to dot to ensure |
| 244 * that we can get back here; this could be avoided for some paths, |
| 245 * but almost certainly not worth the effort. Slashes, symbolic links, |
| 246 * and ".." are all fairly nasty problems. Note, if we can't get the |
| 247 * descriptor we run anyway, just more slowly. |
| 248 */ |
| 249 if (!ISSET(FTS_NOCHDIR) && |
| 250 (sp->fts_rfd = open(".", O_RDONLY | O_CLOEXEC, 0)) < 0) |
| 251 SET(FTS_NOCHDIR); |
| 252 |
| 253 return (sp); |
| 254 |
| 255 mem3: fts_lfree(root); |
| 256 free(parent); |
| 257 mem2: free(sp->fts_path); |
| 258 mem1: free(sp); |
| 259 return (NULL); |
| 260 } |
| 261 |
| 262 static void |
| 263 fts_load(FTS *sp, FTSENT *p) |
| 264 { |
| 265 size_t len; |
| 266 char *cp; |
| 267 |
| 268 /* |
| 269 * Load the stream structure for the next traversal. Since we don't |
| 270 * actually enter the directory until after the preorder visit, set |
| 271 * the fts_accpath field specially so the chdir gets done to the right |
| 272 * place and the user can access the first node. From fts_open it's |
| 273 * known that the path will fit. |
| 274 */ |
| 275 len = p->fts_pathlen = p->fts_namelen; |
| 276 memmove(sp->fts_path, p->fts_name, len + 1); |
| 277 if ((cp = strrchr(p->fts_name, '/')) && (cp != p->fts_name || cp[1])) { |
| 278 len = strlen(++cp); |
| 279 memmove(p->fts_name, cp, len + 1); |
| 280 p->fts_namelen = len; |
| 281 } |
| 282 p->fts_accpath = p->fts_path = sp->fts_path; |
| 283 sp->fts_dev = p->fts_dev; |
| 284 } |
| 285 |
| 286 int |
| 287 fts_close(FTS *sp) |
| 288 { |
| 289 FTSENT *freep, *p; |
| 290 int saved_errno; |
| 291 |
| 292 /* |
| 293 * This still works if we haven't read anything -- the dummy structure |
| 294 * points to the root list, so we step through to the end of the root |
| 295 * list which has a valid parent pointer. |
| 296 */ |
| 297 if (sp->fts_cur) { |
| 298 for (p = sp->fts_cur; p->fts_level >= FTS_ROOTLEVEL;) { |
| 299 freep = p; |
| 300 p = p->fts_link != NULL ? p->fts_link : p->fts_parent; |
| 301 free(freep); |
| 302 } |
| 303 free(p); |
| 304 } |
| 305 |
| 306 /* Free up child linked list, sort array, path buffer. */ |
| 307 if (sp->fts_child) |
| 308 fts_lfree(sp->fts_child); |
| 309 if (sp->fts_array) |
| 310 free(sp->fts_array); |
| 311 free(sp->fts_path); |
| 312 |
| 313 /* Return to original directory, save errno if necessary. */ |
| 314 if (!ISSET(FTS_NOCHDIR)) { |
| 315 saved_errno = fchdir(sp->fts_rfd) ? errno : 0; |
| 316 (void)close(sp->fts_rfd); |
| 317 |
| 318 /* Set errno and return. */ |
| 319 if (saved_errno != 0) { |
| 320 /* Free up the stream pointer. */ |
| 321 free(sp); |
| 322 errno = saved_errno; |
| 323 return (-1); |
| 324 } |
| 325 } |
| 326 |
| 327 /* Free up the stream pointer. */ |
| 328 free(sp); |
| 329 return (0); |
| 330 } |
| 331 |
| 332 /* |
| 333 * Special case of "/" at the end of the path so that slashes aren't |
| 334 * appended which would cause paths to be written as "....//foo". |
| 335 */ |
| 336 #define NAPPEND(p) \ |
| 337 (p->fts_path[p->fts_pathlen - 1] == '/' \ |
| 338 ? p->fts_pathlen - 1 : p->fts_pathlen) |
| 339 |
| 340 FTSENT * |
| 341 fts_read(FTS *sp) |
| 342 { |
| 343 FTSENT *p, *tmp; |
| 344 int instr; |
| 345 char *t; |
| 346 int saved_errno; |
| 347 |
| 348 /* If finished or unrecoverable error, return NULL. */ |
| 349 if (sp->fts_cur == NULL || ISSET(FTS_STOP)) |
| 350 return (NULL); |
| 351 |
| 352 /* Set current node pointer. */ |
| 353 p = sp->fts_cur; |
| 354 |
| 355 /* Save and zero out user instructions. */ |
| 356 instr = p->fts_instr; |
| 357 p->fts_instr = FTS_NOINSTR; |
| 358 |
| 359 /* Any type of file may be re-visited; re-stat and re-turn. */ |
| 360 if (instr == FTS_AGAIN) { |
| 361 p->fts_info = fts_stat(sp, p, 0); |
| 362 return (p); |
| 363 } |
| 364 |
| 365 /* |
| 366 * Following a symlink -- SLNONE test allows application to see |
| 367 * SLNONE and recover. If indirecting through a symlink, have |
| 368 * keep a pointer to current location. If unable to get that |
| 369 * pointer, follow fails. |
| 370 */ |
| 371 if (instr == FTS_FOLLOW && |
| 372 (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) { |
| 373 p->fts_info = fts_stat(sp, p, 1); |
| 374 if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) { |
| 375 if ((p->fts_symfd = open(".", O_RDONLY | O_CLOEXEC, |
| 376 0)) < 0) { |
| 377 p->fts_errno = errno; |
| 378 p->fts_info = FTS_ERR; |
| 379 } else |
| 380 p->fts_flags |= FTS_SYMFOLLOW; |
| 381 } |
| 382 return (p); |
| 383 } |
| 384 |
| 385 /* Directory in pre-order. */ |
| 386 if (p->fts_info == FTS_D) { |
| 387 /* If skipped or crossed mount point, do post-order visit. */ |
| 388 if (instr == FTS_SKIP || |
| 389 (ISSET(FTS_XDEV) && p->fts_dev != sp->fts_dev)) { |
| 390 if (p->fts_flags & FTS_SYMFOLLOW) |
| 391 (void)close(p->fts_symfd); |
| 392 if (sp->fts_child) { |
| 393 fts_lfree(sp->fts_child); |
| 394 sp->fts_child = NULL; |
| 395 } |
| 396 p->fts_info = FTS_DP; |
| 397 return (p); |
| 398 } |
| 399 |
| 400 /* Rebuild if only read the names and now traversing. */ |
| 401 if (sp->fts_child != NULL && ISSET(FTS_NAMEONLY)) { |
| 402 CLR(FTS_NAMEONLY); |
| 403 fts_lfree(sp->fts_child); |
| 404 sp->fts_child = NULL; |
| 405 } |
| 406 |
| 407 /* |
| 408 * Cd to the subdirectory. |
| 409 * |
| 410 * If have already read and now fail to chdir, whack the list |
| 411 * to make the names come out right, and set the parent errno |
| 412 * so the application will eventually get an error condition. |
| 413 * Set the FTS_DONTCHDIR flag so that when we logically change |
| 414 * directories back to the parent we don't do a chdir. |
| 415 * |
| 416 * If haven't read do so. If the read fails, fts_build sets |
| 417 * FTS_STOP or the fts_info field of the node. |
| 418 */ |
| 419 if (sp->fts_child != NULL) { |
| 420 if (fts_safe_changedir(sp, p, -1, p->fts_accpath)) { |
| 421 p->fts_errno = errno; |
| 422 p->fts_flags |= FTS_DONTCHDIR; |
| 423 for (p = sp->fts_child; p != NULL; |
| 424 p = p->fts_link) |
| 425 p->fts_accpath = |
| 426 p->fts_parent->fts_accpath; |
| 427 } |
| 428 } else if ((sp->fts_child = fts_build(sp, BREAD)) == NULL) { |
| 429 if (ISSET(FTS_STOP)) |
| 430 return (NULL); |
| 431 return (p); |
| 432 } |
| 433 p = sp->fts_child; |
| 434 sp->fts_child = NULL; |
| 435 goto name; |
| 436 } |
| 437 |
| 438 /* Move to the next node on this level. */ |
| 439 next: tmp = p; |
| 440 if ((p = p->fts_link) != NULL) { |
| 441 /* |
| 442 * If reached the top, return to the original directory (or |
| 443 * the root of the tree), and load the paths for the next root. |
| 444 */ |
| 445 if (p->fts_level == FTS_ROOTLEVEL) { |
| 446 if (FCHDIR(sp, sp->fts_rfd)) { |
| 447 SET(FTS_STOP); |
| 448 return (NULL); |
| 449 } |
| 450 free(tmp); |
| 451 fts_load(sp, p); |
| 452 return (sp->fts_cur = p); |
| 453 } |
| 454 |
| 455 /* |
| 456 * User may have called fts_set on the node. If skipped, |
| 457 * ignore. If followed, get a file descriptor so we can |
| 458 * get back if necessary. |
| 459 */ |
| 460 if (p->fts_instr == FTS_SKIP) { |
| 461 free(tmp); |
| 462 goto next; |
| 463 } |
| 464 if (p->fts_instr == FTS_FOLLOW) { |
| 465 p->fts_info = fts_stat(sp, p, 1); |
| 466 if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) { |
| 467 if ((p->fts_symfd = |
| 468 open(".", O_RDONLY | O_CLOEXEC, 0)) < 0) { |
| 469 p->fts_errno = errno; |
| 470 p->fts_info = FTS_ERR; |
| 471 } else |
| 472 p->fts_flags |= FTS_SYMFOLLOW; |
| 473 } |
| 474 p->fts_instr = FTS_NOINSTR; |
| 475 } |
| 476 |
| 477 free(tmp); |
| 478 |
| 479 name: t = sp->fts_path + NAPPEND(p->fts_parent); |
| 480 *t++ = '/'; |
| 481 memmove(t, p->fts_name, p->fts_namelen + 1); |
| 482 return (sp->fts_cur = p); |
| 483 } |
| 484 |
| 485 /* Move up to the parent node. */ |
| 486 p = tmp->fts_parent; |
| 487 |
| 488 if (p->fts_level == FTS_ROOTPARENTLEVEL) { |
| 489 /* |
| 490 * Done; free everything up and set errno to 0 so the user |
| 491 * can distinguish between error and EOF. |
| 492 */ |
| 493 free(tmp); |
| 494 free(p); |
| 495 errno = 0; |
| 496 return (sp->fts_cur = NULL); |
| 497 } |
| 498 |
| 499 /* NUL terminate the pathname. */ |
| 500 sp->fts_path[p->fts_pathlen] = '\0'; |
| 501 |
| 502 /* |
| 503 * Return to the parent directory. If at a root node or came through |
| 504 * a symlink, go back through the file descriptor. Otherwise, cd up |
| 505 * one directory. |
| 506 */ |
| 507 if (p->fts_level == FTS_ROOTLEVEL) { |
| 508 if (FCHDIR(sp, sp->fts_rfd)) { |
| 509 SET(FTS_STOP); |
| 510 return (NULL); |
| 511 } |
| 512 } else if (p->fts_flags & FTS_SYMFOLLOW) { |
| 513 if (FCHDIR(sp, p->fts_symfd)) { |
| 514 saved_errno = errno; |
| 515 (void)close(p->fts_symfd); |
| 516 errno = saved_errno; |
| 517 SET(FTS_STOP); |
| 518 return (NULL); |
| 519 } |
| 520 (void)close(p->fts_symfd); |
| 521 } else if (!(p->fts_flags & FTS_DONTCHDIR) && |
| 522 fts_safe_changedir(sp, p->fts_parent, -1, "..")) { |
| 523 SET(FTS_STOP); |
| 524 return (NULL); |
| 525 } |
| 526 free(tmp); |
| 527 p->fts_info = p->fts_errno ? FTS_ERR : FTS_DP; |
| 528 return (sp->fts_cur = p); |
| 529 } |
| 530 |
| 531 /* |
| 532 * Fts_set takes the stream as an argument although it's not used in this |
| 533 * implementation; it would be necessary if anyone wanted to add global |
| 534 * semantics to fts using fts_set. An error return is allowed for similar |
| 535 * reasons. |
| 536 */ |
| 537 /* ARGSUSED */ |
| 538 int |
| 539 fts_set(FTS *sp, FTSENT *p, int instr) |
| 540 { |
| 541 if (instr != 0 && instr != FTS_AGAIN && instr != FTS_FOLLOW && |
| 542 instr != FTS_NOINSTR && instr != FTS_SKIP) { |
| 543 errno = EINVAL; |
| 544 return (1); |
| 545 } |
| 546 p->fts_instr = instr; |
| 547 return (0); |
| 548 } |
| 549 |
| 550 FTSENT * |
| 551 fts_children(FTS *sp, int instr) |
| 552 { |
| 553 FTSENT *p; |
| 554 int fd; |
| 555 |
| 556 if (instr != 0 && instr != FTS_NAMEONLY) { |
| 557 errno = EINVAL; |
| 558 return (NULL); |
| 559 } |
| 560 |
| 561 /* Set current node pointer. */ |
| 562 p = sp->fts_cur; |
| 563 |
| 564 /* |
| 565 * Errno set to 0 so user can distinguish empty directory from |
| 566 * an error. |
| 567 */ |
| 568 errno = 0; |
| 569 |
| 570 /* Fatal errors stop here. */ |
| 571 if (ISSET(FTS_STOP)) |
| 572 return (NULL); |
| 573 |
| 574 /* Return logical hierarchy of user's arguments. */ |
| 575 if (p->fts_info == FTS_INIT) |
| 576 return (p->fts_link); |
| 577 |
| 578 /* |
| 579 * If not a directory being visited in pre-order, stop here. Could |
| 580 * allow FTS_DNR, assuming the user has fixed the problem, but the |
| 581 * same effect is available with FTS_AGAIN. |
| 582 */ |
| 583 if (p->fts_info != FTS_D /* && p->fts_info != FTS_DNR */) |
| 584 return (NULL); |
| 585 |
| 586 /* Free up any previous child list. */ |
| 587 if (sp->fts_child != NULL) |
| 588 fts_lfree(sp->fts_child); |
| 589 |
| 590 if (instr == FTS_NAMEONLY) { |
| 591 SET(FTS_NAMEONLY); |
| 592 instr = BNAMES; |
| 593 } else |
| 594 instr = BCHILD; |
| 595 |
| 596 /* |
| 597 * If using chdir on a relative path and called BEFORE fts_read does |
| 598 * its chdir to the root of a traversal, we can lose -- we need to |
| 599 * chdir into the subdirectory, and we don't know where the current |
| 600 * directory is, so we can't get back so that the upcoming chdir by |
| 601 * fts_read will work. |
| 602 */ |
| 603 if (p->fts_level != FTS_ROOTLEVEL || p->fts_accpath[0] == '/' || |
| 604 ISSET(FTS_NOCHDIR)) |
| 605 return (sp->fts_child = fts_build(sp, instr)); |
| 606 |
| 607 if ((fd = open(".", O_RDONLY | O_CLOEXEC, 0)) < 0) |
| 608 return (NULL); |
| 609 sp->fts_child = fts_build(sp, instr); |
| 610 if (fchdir(fd)) { |
| 611 (void)close(fd); |
| 612 return (NULL); |
| 613 } |
| 614 (void)close(fd); |
| 615 return (sp->fts_child); |
| 616 } |
| 617 |
| 618 #ifndef fts_get_clientptr |
| 619 #error "fts_get_clientptr not defined" |
| 620 #endif |
| 621 |
| 622 void * |
| 623 (fts_get_clientptr)(FTS *sp) |
| 624 { |
| 625 |
| 626 return (fts_get_clientptr(sp)); |
| 627 } |
| 628 |
| 629 #ifndef fts_get_stream |
| 630 #error "fts_get_stream not defined" |
| 631 #endif |
| 632 |
| 633 FTS * |
| 634 (fts_get_stream)(FTSENT *p) |
| 635 { |
| 636 return (fts_get_stream(p)); |
| 637 } |
| 638 |
| 639 void |
| 640 fts_set_clientptr(FTS *sp, void *clientptr) |
| 641 { |
| 642 |
| 643 sp->fts_clientptr = clientptr; |
| 644 } |
| 645 |
| 646 /* |
| 647 * This is the tricky part -- do not casually change *anything* in here. The |
| 648 * idea is to build the linked list of entries that are used by fts_children |
| 649 * and fts_read. There are lots of special cases. |
| 650 * |
| 651 * The real slowdown in walking the tree is the stat calls. If FTS_NOSTAT is |
| 652 * set and it's a physical walk (so that symbolic links can't be directories), |
| 653 * we can do things quickly. First, if it's a 4.4BSD file system, the type |
| 654 * of the file is in the directory entry. Otherwise, we assume that the number |
| 655 * of subdirectories in a node is equal to the number of links to the parent. |
| 656 * The former skips all stat calls. The latter skips stat calls in any leaf |
| 657 * directories and for any files after the subdirectories in the directory have |
| 658 * been found, cutting the stat calls by about 2/3. |
| 659 */ |
| 660 static FTSENT * |
| 661 fts_build(FTS *sp, int type) |
| 662 { |
| 663 struct dirent *dp; |
| 664 FTSENT *p, *head; |
| 665 FTSENT *cur, *tail; |
| 666 DIR *dirp; |
| 667 void *oldaddr; |
| 668 char *cp; |
| 669 int cderrno, descend, oflag, saved_errno, nostat, doadjust; |
| 670 long level; |
| 671 long nlinks; /* has to be signed because -1 is a magic value */ |
| 672 size_t dnamlen, len, maxlen, nitems; |
| 673 |
| 674 /* Set current node pointer. */ |
| 675 cur = sp->fts_cur; |
| 676 |
| 677 /* |
| 678 * Open the directory for reading. If this fails, we're done. |
| 679 * If being called from fts_read, set the fts_info field. |
| 680 */ |
| 681 #if defined FTS_WHITEOUT && !defined (__native_client__) |
| 682 if (ISSET(FTS_WHITEOUT)) |
| 683 oflag = DTF_NODUP | DTF_REWIND; |
| 684 else |
| 685 oflag = DTF_HIDEW | DTF_NODUP | DTF_REWIND; |
| 686 #else |
| 687 #define __opendir2(path, flag) opendir(path) |
| 688 #endif |
| 689 if ((dirp = __opendir2(cur->fts_accpath, oflag)) == NULL) { |
| 690 if (type == BREAD) { |
| 691 cur->fts_info = FTS_DNR; |
| 692 cur->fts_errno = errno; |
| 693 } |
| 694 return (NULL); |
| 695 } |
| 696 |
| 697 /* |
| 698 * Nlinks is the number of possible entries of type directory in the |
| 699 * directory if we're cheating on stat calls, 0 if we're not doing |
| 700 * any stat calls at all, -1 if we're doing stats on everything. |
| 701 */ |
| 702 if (type == BNAMES) { |
| 703 nlinks = 0; |
| 704 /* Be quiet about nostat, GCC. */ |
| 705 nostat = 0; |
| 706 } else if (ISSET(FTS_NOSTAT) && ISSET(FTS_PHYSICAL)) { |
| 707 #ifdef __native_client__ |
| 708 nlinks = cur->fts_nlink - (ISSET(FTS_SEEDOT) ? 0 : 2); |
| 709 #else |
| 710 if (fts_ufslinks(sp, cur)) |
| 711 nlinks = cur->fts_nlink - (ISSET(FTS_SEEDOT) ? 0 : 2); |
| 712 else |
| 713 nlinks = -1; |
| 714 #endif |
| 715 nostat = 1; |
| 716 } else { |
| 717 nlinks = -1; |
| 718 nostat = 0; |
| 719 } |
| 720 |
| 721 #ifdef notdef |
| 722 (void)printf("nlinks == %d (cur: %d)\n", nlinks, cur->fts_nlink); |
| 723 (void)printf("NOSTAT %d PHYSICAL %d SEEDOT %d\n", |
| 724 ISSET(FTS_NOSTAT), ISSET(FTS_PHYSICAL), ISSET(FTS_SEEDOT)); |
| 725 #endif |
| 726 /* |
| 727 * If we're going to need to stat anything or we want to descend |
| 728 * and stay in the directory, chdir. If this fails we keep going, |
| 729 * but set a flag so we don't chdir after the post-order visit. |
| 730 * We won't be able to stat anything, but we can still return the |
| 731 * names themselves. Note, that since fts_read won't be able to |
| 732 * chdir into the directory, it will have to return different path |
| 733 * names than before, i.e. "a/b" instead of "b". Since the node |
| 734 * has already been visited in pre-order, have to wait until the |
| 735 * post-order visit to return the error. There is a special case |
| 736 * here, if there was nothing to stat then it's not an error to |
| 737 * not be able to stat. This is all fairly nasty. If a program |
| 738 * needed sorted entries or stat information, they had better be |
| 739 * checking FTS_NS on the returned nodes. |
| 740 */ |
| 741 cderrno = 0; |
| 742 if (nlinks || type == BREAD) { |
| 743 if (fts_safe_changedir(sp, cur, dirfd(dirp), NULL)) { |
| 744 if (nlinks && type == BREAD) |
| 745 cur->fts_errno = errno; |
| 746 cur->fts_flags |= FTS_DONTCHDIR; |
| 747 descend = 0; |
| 748 cderrno = errno; |
| 749 } else |
| 750 descend = 1; |
| 751 } else |
| 752 descend = 0; |
| 753 |
| 754 /* |
| 755 * Figure out the max file name length that can be stored in the |
| 756 * current path -- the inner loop allocates more path as necessary. |
| 757 * We really wouldn't have to do the maxlen calculations here, we |
| 758 * could do them in fts_read before returning the path, but it's a |
| 759 * lot easier here since the length is part of the dirent structure. |
| 760 * |
| 761 * If not changing directories set a pointer so that can just append |
| 762 * each new name into the path. |
| 763 */ |
| 764 len = NAPPEND(cur); |
| 765 if (ISSET(FTS_NOCHDIR)) { |
| 766 cp = sp->fts_path + len; |
| 767 *cp++ = '/'; |
| 768 } else { |
| 769 /* GCC, you're too verbose. */ |
| 770 cp = NULL; |
| 771 } |
| 772 len++; |
| 773 maxlen = sp->fts_pathlen - len; |
| 774 |
| 775 level = cur->fts_level + 1; |
| 776 |
| 777 /* Read the directory, attaching each entry to the `link' pointer. */ |
| 778 doadjust = 0; |
| 779 for (head = tail = NULL, nitems = 0; dirp && (dp = readdir(dirp));) { |
| 780 dnamlen = strlen(dp->d_name); |
| 781 if (!ISSET(FTS_SEEDOT) && ISDOT(dp->d_name)) |
| 782 continue; |
| 783 |
| 784 if ((p = fts_alloc(sp, dp->d_name, dnamlen)) == NULL) |
| 785 goto mem1; |
| 786 if (dnamlen >= maxlen) { /* include space for NUL */ |
| 787 oldaddr = sp->fts_path; |
| 788 if (fts_palloc(sp, dnamlen + len + 1)) { |
| 789 /* |
| 790 * No more memory for path or structures. Save |
| 791 * errno, free up the current structure and the |
| 792 * structures already allocated. |
| 793 */ |
| 794 mem1: saved_errno = errno; |
| 795 if (p) |
| 796 free(p); |
| 797 fts_lfree(head); |
| 798 (void)closedir(dirp); |
| 799 cur->fts_info = FTS_ERR; |
| 800 SET(FTS_STOP); |
| 801 errno = saved_errno; |
| 802 return (NULL); |
| 803 } |
| 804 /* Did realloc() change the pointer? */ |
| 805 if (oldaddr != sp->fts_path) { |
| 806 doadjust = 1; |
| 807 if (ISSET(FTS_NOCHDIR)) |
| 808 cp = sp->fts_path + len; |
| 809 } |
| 810 maxlen = sp->fts_pathlen - len; |
| 811 } |
| 812 |
| 813 p->fts_level = level; |
| 814 p->fts_parent = sp->fts_cur; |
| 815 p->fts_pathlen = len + dnamlen; |
| 816 |
| 817 #if defined (FTS_WHITEOUT) && !(defined __native_client__) |
| 818 if (dp->d_type == DT_WHT) |
| 819 p->fts_flags |= FTS_ISW; |
| 820 #endif |
| 821 |
| 822 if (cderrno) { |
| 823 if (nlinks) { |
| 824 p->fts_info = FTS_NS; |
| 825 p->fts_errno = cderrno; |
| 826 } else |
| 827 p->fts_info = FTS_NSOK; |
| 828 p->fts_accpath = cur->fts_accpath; |
| 829 } else if (nlinks == 0 |
| 830 #ifdef DT_DIR |
| 831 || (nostat && |
| 832 dp->d_type != DT_DIR && dp->d_type != DT_UNKNOWN) |
| 833 #endif |
| 834 ) { |
| 835 p->fts_accpath = |
| 836 ISSET(FTS_NOCHDIR) ? p->fts_path : p->fts_name; |
| 837 p->fts_info = FTS_NSOK; |
| 838 } else { |
| 839 /* Build a file name for fts_stat to stat. */ |
| 840 if (ISSET(FTS_NOCHDIR)) { |
| 841 p->fts_accpath = p->fts_path; |
| 842 memmove(cp, p->fts_name, p->fts_namelen + 1); |
| 843 } else |
| 844 p->fts_accpath = p->fts_name; |
| 845 /* Stat it. */ |
| 846 p->fts_info = fts_stat(sp, p, 0); |
| 847 |
| 848 /* Decrement link count if applicable. */ |
| 849 if (nlinks > 0 && (p->fts_info == FTS_D || |
| 850 p->fts_info == FTS_DC || p->fts_info == FTS_DOT)) |
| 851 --nlinks; |
| 852 } |
| 853 |
| 854 /* We walk in directory order so "ls -f" doesn't get upset. */ |
| 855 p->fts_link = NULL; |
| 856 if (head == NULL) |
| 857 head = tail = p; |
| 858 else { |
| 859 tail->fts_link = p; |
| 860 tail = p; |
| 861 } |
| 862 ++nitems; |
| 863 } |
| 864 if (dirp) |
| 865 (void)closedir(dirp); |
| 866 |
| 867 /* |
| 868 * If realloc() changed the address of the path, adjust the |
| 869 * addresses for the rest of the tree and the dir list. |
| 870 */ |
| 871 if (doadjust) |
| 872 fts_padjust(sp, head); |
| 873 |
| 874 /* |
| 875 * If not changing directories, reset the path back to original |
| 876 * state. |
| 877 */ |
| 878 if (ISSET(FTS_NOCHDIR)) |
| 879 sp->fts_path[cur->fts_pathlen] = '\0'; |
| 880 |
| 881 /* |
| 882 * If descended after called from fts_children or after called from |
| 883 * fts_read and nothing found, get back. At the root level we use |
| 884 * the saved fd; if one of fts_open()'s arguments is a relative path |
| 885 * to an empty directory, we wind up here with no other way back. If |
| 886 * can't get back, we're done. |
| 887 */ |
| 888 if (descend && (type == BCHILD || !nitems) && |
| 889 (cur->fts_level == FTS_ROOTLEVEL ? |
| 890 FCHDIR(sp, sp->fts_rfd) : |
| 891 fts_safe_changedir(sp, cur->fts_parent, -1, ".."))) { |
| 892 cur->fts_info = FTS_ERR; |
| 893 SET(FTS_STOP); |
| 894 return (NULL); |
| 895 } |
| 896 |
| 897 /* If didn't find anything, return NULL. */ |
| 898 if (!nitems) { |
| 899 if (type == BREAD) |
| 900 cur->fts_info = FTS_DP; |
| 901 return (NULL); |
| 902 } |
| 903 |
| 904 /* Sort the entries. */ |
| 905 if (sp->fts_compar && nitems > 1) |
| 906 head = fts_sort(sp, head, nitems); |
| 907 return (head); |
| 908 } |
| 909 |
| 910 static int |
| 911 fts_stat(FTS *sp, FTSENT *p, int follow) |
| 912 { |
| 913 FTSENT *t; |
| 914 dev_t dev; |
| 915 ino_t ino; |
| 916 struct stat *sbp, sb; |
| 917 int saved_errno; |
| 918 |
| 919 /* If user needs stat info, stat buffer already allocated. */ |
| 920 sbp = ISSET(FTS_NOSTAT) ? &sb : p->fts_statp; |
| 921 |
| 922 #if defined (FTS_WHITEOUT) && !defined(__native_client__) |
| 923 /* Check for whiteout. */ |
| 924 if (p->fts_flags & FTS_ISW) { |
| 925 if (sbp != &sb) { |
| 926 memset(sbp, '\0', sizeof(*sbp)); |
| 927 sbp->st_mode = S_IFWHT; |
| 928 } |
| 929 return (FTS_W); |
| 930 } |
| 931 #endif |
| 932 |
| 933 /* |
| 934 * If doing a logical walk, or application requested FTS_FOLLOW, do |
| 935 * a stat(2). If that fails, check for a non-existent symlink. If |
| 936 * fail, set the errno from the stat call. |
| 937 */ |
| 938 if (ISSET(FTS_LOGICAL) || follow) { |
| 939 if (stat(p->fts_accpath, sbp)) { |
| 940 saved_errno = errno; |
| 941 if (!lstat(p->fts_accpath, sbp)) { |
| 942 errno = 0; |
| 943 return (FTS_SLNONE); |
| 944 } |
| 945 p->fts_errno = saved_errno; |
| 946 goto err; |
| 947 } |
| 948 } else if (lstat(p->fts_accpath, sbp)) { |
| 949 p->fts_errno = errno; |
| 950 err: memset(sbp, 0, sizeof(struct stat)); |
| 951 return (FTS_NS); |
| 952 } |
| 953 |
| 954 if (S_ISDIR(sbp->st_mode)) { |
| 955 /* |
| 956 * Set the device/inode. Used to find cycles and check for |
| 957 * crossing mount points. Also remember the link count, used |
| 958 * in fts_build to limit the number of stat calls. It is |
| 959 * understood that these fields are only referenced if fts_info |
| 960 * is set to FTS_D. |
| 961 */ |
| 962 dev = p->fts_dev = sbp->st_dev; |
| 963 ino = p->fts_ino = sbp->st_ino; |
| 964 p->fts_nlink = sbp->st_nlink; |
| 965 |
| 966 if (ISDOT(p->fts_name)) |
| 967 return (FTS_DOT); |
| 968 |
| 969 /* |
| 970 * Cycle detection is done by brute force when the directory |
| 971 * is first encountered. If the tree gets deep enough or the |
| 972 * number of symbolic links to directories is high enough, |
| 973 * something faster might be worthwhile. |
| 974 */ |
| 975 for (t = p->fts_parent; |
| 976 t->fts_level >= FTS_ROOTLEVEL; t = t->fts_parent) |
| 977 if (ino == t->fts_ino && dev == t->fts_dev) { |
| 978 p->fts_cycle = t; |
| 979 return (FTS_DC); |
| 980 } |
| 981 return (FTS_D); |
| 982 } |
| 983 if (S_ISLNK(sbp->st_mode)) |
| 984 return (FTS_SL); |
| 985 if (S_ISREG(sbp->st_mode)) |
| 986 return (FTS_F); |
| 987 return (FTS_DEFAULT); |
| 988 } |
| 989 |
| 990 /* |
| 991 * The comparison function takes pointers to pointers to FTSENT structures. |
| 992 * Qsort wants a comparison function that takes pointers to void. |
| 993 * (Both with appropriate levels of const-poisoning, of course!) |
| 994 * Use a trampoline function to deal with the difference. |
| 995 */ |
| 996 static int |
| 997 fts_compar(const void *a, const void *b) |
| 998 { |
| 999 FTS *parent; |
| 1000 |
| 1001 parent = (*(const FTSENT * const *)a)->fts_fts; |
| 1002 return (*parent->fts_compar)(a, b); |
| 1003 } |
| 1004 |
| 1005 static FTSENT * |
| 1006 fts_sort(FTS *sp, FTSENT *head, size_t nitems) |
| 1007 { |
| 1008 FTSENT **ap, *p; |
| 1009 |
| 1010 /* |
| 1011 * Construct an array of pointers to the structures and call qsort(3). |
| 1012 * Reassemble the array in the order returned by qsort. If unable to |
| 1013 * sort for memory reasons, return the directory entries in their |
| 1014 * current order. Allocate enough space for the current needs plus |
| 1015 * 40 so don't realloc one entry at a time. |
| 1016 */ |
| 1017 if (nitems > sp->fts_nitems) { |
| 1018 sp->fts_nitems = nitems + 40; |
| 1019 if ((sp->fts_array = reallocf(sp->fts_array, |
| 1020 sp->fts_nitems * sizeof(FTSENT *))) == NULL) { |
| 1021 sp->fts_nitems = 0; |
| 1022 return (head); |
| 1023 } |
| 1024 } |
| 1025 for (ap = sp->fts_array, p = head; p; p = p->fts_link) |
| 1026 *ap++ = p; |
| 1027 qsort(sp->fts_array, nitems, sizeof(FTSENT *), fts_compar); |
| 1028 for (head = *(ap = sp->fts_array); --nitems; ++ap) |
| 1029 ap[0]->fts_link = ap[1]; |
| 1030 ap[0]->fts_link = NULL; |
| 1031 return (head); |
| 1032 } |
| 1033 |
| 1034 static FTSENT * |
| 1035 fts_alloc(FTS *sp, char *name, size_t namelen) |
| 1036 { |
| 1037 FTSENT *p; |
| 1038 size_t len; |
| 1039 |
| 1040 struct ftsent_withstat { |
| 1041 FTSENT ent; |
| 1042 struct stat statbuf; |
| 1043 }; |
| 1044 |
| 1045 /* |
| 1046 * The file name is a variable length array and no stat structure is |
| 1047 * necessary if the user has set the nostat bit. Allocate the FTSENT |
| 1048 * structure, the file name and the stat structure in one chunk, but |
| 1049 * be careful that the stat structure is reasonably aligned. |
| 1050 */ |
| 1051 if (ISSET(FTS_NOSTAT)) |
| 1052 len = sizeof(FTSENT) + namelen + 1; |
| 1053 else |
| 1054 len = sizeof(struct ftsent_withstat) + namelen + 1; |
| 1055 |
| 1056 if ((p = malloc(len)) == NULL) |
| 1057 return (NULL); |
| 1058 |
| 1059 if (ISSET(FTS_NOSTAT)) { |
| 1060 p->fts_name = (char *)(p + 1); |
| 1061 p->fts_statp = NULL; |
| 1062 } else { |
| 1063 p->fts_name = (char *)((struct ftsent_withstat *)p + 1); |
| 1064 p->fts_statp = &((struct ftsent_withstat *)p)->statbuf; |
| 1065 } |
| 1066 |
| 1067 /* Copy the name and guarantee NUL termination. */ |
| 1068 memcpy(p->fts_name, name, namelen); |
| 1069 p->fts_name[namelen] = '\0'; |
| 1070 p->fts_namelen = namelen; |
| 1071 p->fts_path = sp->fts_path; |
| 1072 p->fts_errno = 0; |
| 1073 p->fts_flags = 0; |
| 1074 p->fts_instr = FTS_NOINSTR; |
| 1075 p->fts_number = 0; |
| 1076 p->fts_pointer = NULL; |
| 1077 p->fts_fts = sp; |
| 1078 return (p); |
| 1079 } |
| 1080 |
| 1081 static void |
| 1082 fts_lfree(FTSENT *head) |
| 1083 { |
| 1084 FTSENT *p; |
| 1085 |
| 1086 /* Free a linked list of structures. */ |
| 1087 while ((p = head)) { |
| 1088 head = head->fts_link; |
| 1089 free(p); |
| 1090 } |
| 1091 } |
| 1092 |
| 1093 /* |
| 1094 * Allow essentially unlimited paths; find, rm, ls should all work on any tree. |
| 1095 * Most systems will allow creation of paths much longer than MAXPATHLEN, even |
| 1096 * though the kernel won't resolve them. Add the size (not just what's needed) |
| 1097 * plus 256 bytes so don't realloc the path 2 bytes at a time. |
| 1098 */ |
| 1099 static int |
| 1100 fts_palloc(FTS *sp, size_t more) |
| 1101 { |
| 1102 |
| 1103 sp->fts_pathlen += more + 256; |
| 1104 sp->fts_path = reallocf(sp->fts_path, sp->fts_pathlen); |
| 1105 return (sp->fts_path == NULL); |
| 1106 } |
| 1107 |
| 1108 /* |
| 1109 * When the path is realloc'd, have to fix all of the pointers in structures |
| 1110 * already returned. |
| 1111 */ |
| 1112 static void |
| 1113 fts_padjust(FTS *sp, FTSENT *head) |
| 1114 { |
| 1115 FTSENT *p; |
| 1116 char *addr = sp->fts_path; |
| 1117 |
| 1118 #define ADJUST(p) do { \ |
| 1119 if ((p)->fts_accpath != (p)->fts_name) { \ |
| 1120 (p)->fts_accpath = \ |
| 1121 (char *)addr + ((p)->fts_accpath - (p)->fts_path); \ |
| 1122 } \ |
| 1123 (p)->fts_path = addr; \ |
| 1124 } while (0) |
| 1125 /* Adjust the current set of children. */ |
| 1126 for (p = sp->fts_child; p; p = p->fts_link) |
| 1127 ADJUST(p); |
| 1128 |
| 1129 /* Adjust the rest of the tree, including the current level. */ |
| 1130 for (p = head; p->fts_level >= FTS_ROOTLEVEL;) { |
| 1131 ADJUST(p); |
| 1132 p = p->fts_link ? p->fts_link : p->fts_parent; |
| 1133 } |
| 1134 } |
| 1135 |
| 1136 static size_t |
| 1137 fts_maxarglen(argv) |
| 1138 char * const *argv; |
| 1139 { |
| 1140 size_t len, max; |
| 1141 |
| 1142 for (max = 0; *argv; ++argv) |
| 1143 if ((len = strlen(*argv)) > max) |
| 1144 max = len; |
| 1145 return (max + 1); |
| 1146 } |
| 1147 |
| 1148 /* |
| 1149 * Change to dir specified by fd or p->fts_accpath without getting |
| 1150 * tricked by someone changing the world out from underneath us. |
| 1151 * Assumes p->fts_dev and p->fts_ino are filled in. |
| 1152 */ |
| 1153 static int |
| 1154 fts_safe_changedir(FTS *sp, FTSENT *p, int fd, char *path) |
| 1155 { |
| 1156 int ret, oerrno, newfd; |
| 1157 struct stat sb; |
| 1158 |
| 1159 newfd = fd; |
| 1160 if (ISSET(FTS_NOCHDIR)) |
| 1161 return (0); |
| 1162 if (fd < 0 && (newfd = open(path, O_RDONLY | O_DIRECTORY | |
| 1163 O_CLOEXEC, 0)) < 0) |
| 1164 return (-1); |
| 1165 if (fstat(newfd, &sb)) { |
| 1166 ret = -1; |
| 1167 goto bail; |
| 1168 } |
| 1169 if (p->fts_dev != sb.st_dev || p->fts_ino != sb.st_ino) { |
| 1170 errno = ENOENT; /* disinformation */ |
| 1171 ret = -1; |
| 1172 goto bail; |
| 1173 } |
| 1174 ret = fchdir(newfd); |
| 1175 bail: |
| 1176 oerrno = errno; |
| 1177 if (fd < 0) |
| 1178 (void)close(newfd); |
| 1179 errno = oerrno; |
| 1180 return (ret); |
| 1181 } |
| 1182 |
| 1183 // native client does not implement statfs |
| 1184 #ifndef __native_client__ |
| 1185 /* |
| 1186 * Check if the filesystem for "ent" has UFS-style links. |
| 1187 */ |
| 1188 static int |
| 1189 fts_ufslinks(FTS *sp, const FTSENT *ent) |
| 1190 { |
| 1191 struct _fts_private *priv; |
| 1192 const char **cpp; |
| 1193 |
| 1194 priv = (struct _fts_private *)sp; |
| 1195 /* |
| 1196 * If this node's device is different from the previous, grab |
| 1197 * the filesystem information, and decide on the reliability |
| 1198 * of the link information from this filesystem for stat(2) |
| 1199 * avoidance. |
| 1200 */ |
| 1201 if (priv->ftsp_dev != ent->fts_dev) { |
| 1202 if (statfs(ent->fts_path, &priv->ftsp_statfs) != -1) { |
| 1203 priv->ftsp_dev = ent->fts_dev; |
| 1204 priv->ftsp_linksreliable = 0; |
| 1205 for (cpp = ufslike_filesystems; *cpp; cpp++) { |
| 1206 if (strcmp(priv->ftsp_statfs.f_fstypename, |
| 1207 *cpp) == 0) { |
| 1208 priv->ftsp_linksreliable = 1; |
| 1209 break; |
| 1210 } |
| 1211 } |
| 1212 } else { |
| 1213 priv->ftsp_linksreliable = 0; |
| 1214 } |
| 1215 } |
| 1216 return (priv->ftsp_linksreliable); |
| 1217 } |
| 1218 #endif |
OLD | NEW |