| 1 | /* $NetBSD: __fts13.c,v 1.44 2005/01/19 00:59:48 mycroft Exp $ */
|
|---|
| 2 |
|
|---|
| 3 | /*-
|
|---|
| 4 | * Copyright (c) 1990, 1993, 1994
|
|---|
| 5 | * The Regents of the University of California. All rights reserved.
|
|---|
| 6 | *
|
|---|
| 7 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 8 | * modification, are permitted provided that the following conditions
|
|---|
| 9 | * are met:
|
|---|
| 10 | * 1. Redistributions of source code must retain the above copyright
|
|---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 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 | * 3. Neither the name of the University nor the names of its contributors
|
|---|
| 16 | * may be used to endorse or promote products derived from this software
|
|---|
| 17 | * without specific prior written permission.
|
|---|
| 18 | *
|
|---|
| 19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|---|
| 20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|---|
| 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|---|
| 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|---|
| 23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|---|
| 24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|---|
| 25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|---|
| 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|---|
| 27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|---|
| 28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|---|
| 29 | * SUCH DAMAGE.
|
|---|
| 30 | */
|
|---|
| 31 |
|
|---|
| 32 | #if HAVE_NBTOOL_CONFIG_H
|
|---|
| 33 | #include "nbtool_config.h"
|
|---|
| 34 | #endif
|
|---|
| 35 |
|
|---|
| 36 | /*#include <sys/cdefs.h>*/
|
|---|
| 37 | #if defined(LIBC_SCCS) && !defined(lint)
|
|---|
| 38 | #if 0
|
|---|
| 39 | static char sccsid[] = "@(#)fts.c 8.6 (Berkeley) 8/14/94";
|
|---|
| 40 | #else
|
|---|
| 41 | __RCSID("$NetBSD: __fts13.c,v 1.44 2005/01/19 00:59:48 mycroft Exp $");
|
|---|
| 42 | #endif
|
|---|
| 43 | #endif /* LIBC_SCCS and not lint */
|
|---|
| 44 |
|
|---|
| 45 | #include "config.h"
|
|---|
| 46 | /*#include "namespace.h"*/
|
|---|
| 47 | #ifndef _MSC_VER
|
|---|
| 48 | #include <sys/param.h>
|
|---|
| 49 | #endif
|
|---|
| 50 | #include <sys/stat.h>
|
|---|
| 51 |
|
|---|
| 52 | #include <assert.h>
|
|---|
| 53 | #include <dirent.h>
|
|---|
| 54 | #include <errno.h>
|
|---|
| 55 | #include <fcntl.h>
|
|---|
| 56 | #include "ftsfake.h"
|
|---|
| 57 | #include <stdlib.h>
|
|---|
| 58 | #include <string.h>
|
|---|
| 59 | #include <unistd.h>
|
|---|
| 60 | #ifdef HAVE_ALLOCA_H
|
|---|
| 61 | # include <alloca.h>
|
|---|
| 62 | #endif
|
|---|
| 63 |
|
|---|
| 64 | #ifdef __sun__
|
|---|
| 65 | # include "solfakes.h"
|
|---|
| 66 | # ifndef __XOPEN_OR_POSIX
|
|---|
| 67 | # define dirfd(dir) ((dir)->dd_fd)
|
|---|
| 68 | #else
|
|---|
| 69 | # define dirfd(dir) ((dir)->d_fd)
|
|---|
| 70 | #endif
|
|---|
| 71 | #endif
|
|---|
| 72 | #ifdef _MSC_VER
|
|---|
| 73 | # include "mscfakes.h"
|
|---|
| 74 | # define dirfd(dir) -1
|
|---|
| 75 | #endif
|
|---|
| 76 |
|
|---|
| 77 | #if ! HAVE_NBTOOL_CONFIG_H
|
|---|
| 78 | # if !defined(__sun__) && !defined(__gnu_linux__) && !defined(__HAIKU__)
|
|---|
| 79 | # define HAVE_STRUCT_DIRENT_D_NAMLEN 1
|
|---|
| 80 | # endif
|
|---|
| 81 | #endif
|
|---|
| 82 |
|
|---|
| 83 | #if 0
|
|---|
| 84 | #ifdef __weak_alias
|
|---|
| 85 | #ifdef __LIBC12_SOURCE__
|
|---|
| 86 | __weak_alias(fts_children,_fts_children)
|
|---|
| 87 | __weak_alias(fts_close,_fts_close)
|
|---|
| 88 | __weak_alias(fts_open,_fts_open)
|
|---|
| 89 | __weak_alias(fts_read,_fts_read)
|
|---|
| 90 | __weak_alias(fts_set,_fts_set)
|
|---|
| 91 | #endif /* __LIBC12_SOURCE__ */
|
|---|
| 92 | #endif /* __weak_alias */
|
|---|
| 93 | #endif
|
|---|
| 94 |
|
|---|
| 95 | #ifdef __LIBC12_SOURCE__
|
|---|
| 96 | #define STAT stat12
|
|---|
| 97 | #else
|
|---|
| 98 | #define STAT stat
|
|---|
| 99 | #endif
|
|---|
| 100 |
|
|---|
| 101 | #ifdef __LIBC12_SOURCE__
|
|---|
| 102 | __warn_references(fts_children,
|
|---|
| 103 | "warning: reference to compatibility fts_children();"
|
|---|
| 104 | " include <fts.h> for correct reference")
|
|---|
| 105 | __warn_references(fts_close,
|
|---|
| 106 | "warning: reference to compatibility fts_close();"
|
|---|
| 107 | " include <fts.h> for correct reference")
|
|---|
| 108 | __warn_references(fts_open,
|
|---|
| 109 | "warning: reference to compatibility fts_open();"
|
|---|
| 110 | " include <fts.h> for correct reference")
|
|---|
| 111 | __warn_references(fts_read,
|
|---|
| 112 | "warning: reference to compatibility fts_read();"
|
|---|
| 113 | " include <fts.h> for correct reference")
|
|---|
| 114 | __warn_references(fts_set,
|
|---|
| 115 | "warning: reference to compatibility fts_set();"
|
|---|
| 116 | " include <fts.h> for correct reference")
|
|---|
| 117 | #endif
|
|---|
| 118 |
|
|---|
| 119 | static FTSENT *fts_alloc(FTS *, const char *, size_t);
|
|---|
| 120 | static FTSENT *fts_build(FTS *, int);
|
|---|
| 121 | static void fts_lfree(FTSENT *);
|
|---|
| 122 | static void fts_load(FTS *, FTSENT *);
|
|---|
| 123 | static size_t fts_maxarglen(char * const *);
|
|---|
| 124 | static size_t fts_pow2(size_t);
|
|---|
| 125 | static int fts_palloc(FTS *, size_t);
|
|---|
| 126 | static void fts_padjust(FTS *, FTSENT *);
|
|---|
| 127 | static FTSENT *fts_sort(FTS *, FTSENT *, size_t);
|
|---|
| 128 | static u_short fts_stat(FTS *, FTSENT *, int);
|
|---|
| 129 | #ifdef _MSC_VER
|
|---|
| 130 | static u_short fts_stat_dirent(FTS *sp, FTSENT *p, int follow, struct dirent *pDirEnt);
|
|---|
| 131 | #endif
|
|---|
| 132 | static int fts_safe_changedir(const FTS *, const FTSENT *, int,
|
|---|
| 133 | const char *);
|
|---|
| 134 |
|
|---|
| 135 | #ifdef _MSC_VER
|
|---|
| 136 | # undef HAVE_FCHDIR
|
|---|
| 137 | #endif
|
|---|
| 138 |
|
|---|
| 139 | #if defined(__EMX__) || defined(_MSC_VER)
|
|---|
| 140 | # define NEED_STRRSLASH
|
|---|
| 141 | # define IS_SLASH(ch) ( (ch) == '/' || (ch) == '\\' )
|
|---|
| 142 | #else
|
|---|
| 143 | # define HAVE_FCHDIR
|
|---|
| 144 | # define IS_SLASH(ch) ( (ch) == '/' )
|
|---|
| 145 | #endif
|
|---|
| 146 |
|
|---|
| 147 | #define ISDOT(a) (a[0] == '.' && (!a[1] || (a[1] == '.' && !a[2])))
|
|---|
| 148 |
|
|---|
| 149 | #define CLR(opt) (sp->fts_options &= ~(opt))
|
|---|
| 150 | #define ISSET(opt) (sp->fts_options & (opt))
|
|---|
| 151 | #define SET(opt) (sp->fts_options |= (opt))
|
|---|
| 152 |
|
|---|
| 153 | #define CHDIR(sp, path) (!ISSET(FTS_NOCHDIR) && chdir(path))
|
|---|
| 154 | #ifdef HAVE_FCHDIR
|
|---|
| 155 | #define FCHDIR(sp, fd) (!ISSET(FTS_NOCHDIR) && fchdir(fd))
|
|---|
| 156 | #else
|
|---|
| 157 | #define FCHDIR(sp, rdir) CHDIR(sp, rdir)
|
|---|
| 158 | #endif
|
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 | /* fts_build flags */
|
|---|
| 162 | #define BCHILD 1 /* fts_children */
|
|---|
| 163 | #define BNAMES 2 /* fts_children, names only */
|
|---|
| 164 | #define BREAD 3 /* fts_read */
|
|---|
| 165 |
|
|---|
| 166 | #ifndef DTF_HIDEW
|
|---|
| 167 | #undef FTS_WHITEOUT
|
|---|
| 168 | #endif
|
|---|
| 169 |
|
|---|
| 170 | #ifndef _DIAGASSERT
|
|---|
| 171 | #define _DIAGASSERT assert
|
|---|
| 172 | #endif
|
|---|
| 173 |
|
|---|
| 174 |
|
|---|
| 175 | FTS *
|
|---|
| 176 | fts_open(argv, options, compar)
|
|---|
| 177 | char * const *argv;
|
|---|
| 178 | int options;
|
|---|
| 179 | int (*compar)(const FTSENT **, const FTSENT **);
|
|---|
| 180 | {
|
|---|
| 181 | FTS *sp;
|
|---|
| 182 | FTSENT *p, *root;
|
|---|
| 183 | size_t nitems;
|
|---|
| 184 | FTSENT *parent, *tmp = NULL; /* pacify gcc */
|
|---|
| 185 | size_t len;
|
|---|
| 186 |
|
|---|
| 187 | _DIAGASSERT(argv != NULL);
|
|---|
| 188 |
|
|---|
| 189 | /* Options check. */
|
|---|
| 190 | if (options & ~FTS_OPTIONMASK) {
|
|---|
| 191 | errno = EINVAL;
|
|---|
| 192 | return (NULL);
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | /* Allocate/initialize the stream */
|
|---|
| 196 | if ((sp = malloc((u_int)sizeof(FTS))) == NULL)
|
|---|
| 197 | return (NULL);
|
|---|
| 198 | memset(sp, 0, sizeof(FTS));
|
|---|
| 199 | sp->fts_compar = compar;
|
|---|
| 200 | sp->fts_options = options;
|
|---|
| 201 |
|
|---|
| 202 | /* Logical walks turn on NOCHDIR; symbolic links are too hard. */
|
|---|
| 203 | if (ISSET(FTS_LOGICAL))
|
|---|
| 204 | SET(FTS_NOCHDIR);
|
|---|
| 205 |
|
|---|
| 206 | /*
|
|---|
| 207 | * Start out with 1K of path space, and enough, in any case,
|
|---|
| 208 | * to hold the user's paths.
|
|---|
| 209 | */
|
|---|
| 210 | if (fts_palloc(sp, MAX(fts_maxarglen(argv), MAXPATHLEN)))
|
|---|
| 211 | goto mem1;
|
|---|
| 212 |
|
|---|
| 213 | /* Allocate/initialize root's parent. */
|
|---|
| 214 | if ((parent = fts_alloc(sp, "", 0)) == NULL)
|
|---|
| 215 | goto mem2;
|
|---|
| 216 | parent->fts_level = FTS_ROOTPARENTLEVEL;
|
|---|
| 217 |
|
|---|
| 218 | /* Allocate/initialize root(s). */
|
|---|
| 219 | for (root = NULL, nitems = 0; *argv; ++argv, ++nitems) {
|
|---|
| 220 | /* Don't allow zero-length paths. */
|
|---|
| 221 | if ((len = strlen(*argv)) == 0) {
|
|---|
| 222 | errno = ENOENT;
|
|---|
| 223 | goto mem3;
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | if ((p = fts_alloc(sp, *argv, len)) == NULL)
|
|---|
| 227 | goto mem3;
|
|---|
| 228 | p->fts_level = FTS_ROOTLEVEL;
|
|---|
| 229 | p->fts_parent = parent;
|
|---|
| 230 | p->fts_accpath = p->fts_name;
|
|---|
| 231 | p->fts_info = fts_stat(sp, p, ISSET(FTS_COMFOLLOW));
|
|---|
| 232 |
|
|---|
| 233 | /* Command-line "." and ".." are real directories. */
|
|---|
| 234 | if (p->fts_info == FTS_DOT)
|
|---|
| 235 | p->fts_info = FTS_D;
|
|---|
| 236 |
|
|---|
| 237 | /*
|
|---|
| 238 | * If comparison routine supplied, traverse in sorted
|
|---|
| 239 | * order; otherwise traverse in the order specified.
|
|---|
| 240 | */
|
|---|
| 241 | if (compar) {
|
|---|
| 242 | p->fts_link = root;
|
|---|
| 243 | root = p;
|
|---|
| 244 | } else {
|
|---|
| 245 | p->fts_link = NULL;
|
|---|
| 246 | if (root == NULL)
|
|---|
| 247 | tmp = root = p;
|
|---|
| 248 | else {
|
|---|
| 249 | tmp->fts_link = p;
|
|---|
| 250 | tmp = p;
|
|---|
| 251 | }
|
|---|
| 252 | }
|
|---|
| 253 | }
|
|---|
| 254 | if (compar && nitems > 1)
|
|---|
| 255 | root = fts_sort(sp, root, nitems);
|
|---|
| 256 |
|
|---|
| 257 | /*
|
|---|
| 258 | * Allocate a dummy pointer and make fts_read think that we've just
|
|---|
| 259 | * finished the node before the root(s); set p->fts_info to FTS_INIT
|
|---|
| 260 | * so that everything about the "current" node is ignored.
|
|---|
| 261 | */
|
|---|
| 262 | if ((sp->fts_cur = fts_alloc(sp, "", 0)) == NULL)
|
|---|
| 263 | goto mem3;
|
|---|
| 264 | sp->fts_cur->fts_link = root;
|
|---|
| 265 | sp->fts_cur->fts_info = FTS_INIT;
|
|---|
| 266 |
|
|---|
| 267 | /*
|
|---|
| 268 | * If using chdir(2), grab a file descriptor pointing to dot to insure
|
|---|
| 269 | * that we can get back here; this could be avoided for some paths,
|
|---|
| 270 | * but almost certainly not worth the effort. Slashes, symbolic links,
|
|---|
| 271 | * and ".." are all fairly nasty problems. Note, if we can't get the
|
|---|
| 272 | * descriptor we run anyway, just more slowly.
|
|---|
| 273 | */
|
|---|
| 274 | if (!ISSET(FTS_NOCHDIR)) {
|
|---|
| 275 | #ifdef HAVE_FCHDIR
|
|---|
| 276 | if ((sp->fts_rfd = open(".", O_RDONLY, 0)) == -1)
|
|---|
| 277 | SET(FTS_NOCHDIR);
|
|---|
| 278 | else if (fcntl(sp->fts_rfd, F_SETFD, FD_CLOEXEC) == -1) {
|
|---|
| 279 | close(sp->fts_rfd);
|
|---|
| 280 | SET(FTS_NOCHDIR);
|
|---|
| 281 | }
|
|---|
| 282 | #else
|
|---|
| 283 | if ((sp->fts_rdir = getcwd(NULL, 0)) != NULL)
|
|---|
| 284 | SET(FTS_NOCHDIR);
|
|---|
| 285 | #endif
|
|---|
| 286 | }
|
|---|
| 287 |
|
|---|
| 288 | return (sp);
|
|---|
| 289 |
|
|---|
| 290 | mem3: fts_lfree(root);
|
|---|
| 291 | free(parent);
|
|---|
| 292 | mem2: free(sp->fts_path);
|
|---|
| 293 | mem1: free(sp);
|
|---|
| 294 | return (NULL);
|
|---|
| 295 | }
|
|---|
| 296 |
|
|---|
| 297 | #ifdef NEED_STRRSLASH
|
|---|
| 298 | static char *strrslash(register char *psz)
|
|---|
| 299 | {
|
|---|
| 300 | register char ch;
|
|---|
| 301 | char *pszLast = NULL;
|
|---|
| 302 | for (; (ch = *psz); psz++)
|
|---|
| 303 | switch (ch)
|
|---|
| 304 | {
|
|---|
| 305 | case '/':
|
|---|
| 306 | case '\\':
|
|---|
| 307 | case ':':
|
|---|
| 308 | pszLast = psz;
|
|---|
| 309 | break;
|
|---|
| 310 | }
|
|---|
| 311 | return pszLast;
|
|---|
| 312 | }
|
|---|
| 313 | #endif
|
|---|
| 314 |
|
|---|
| 315 | static void
|
|---|
| 316 | fts_load(sp, p)
|
|---|
| 317 | FTS *sp;
|
|---|
| 318 | FTSENT *p;
|
|---|
| 319 | {
|
|---|
| 320 | size_t len;
|
|---|
| 321 | char *cp;
|
|---|
| 322 |
|
|---|
| 323 | _DIAGASSERT(sp != NULL);
|
|---|
| 324 | _DIAGASSERT(p != NULL);
|
|---|
| 325 |
|
|---|
| 326 | /*
|
|---|
| 327 | * Load the stream structure for the next traversal. Since we don't
|
|---|
| 328 | * actually enter the directory until after the preorder visit, set
|
|---|
| 329 | * the fts_accpath field specially so the chdir gets done to the right
|
|---|
| 330 | * place and the user can access the first node. From fts_open it's
|
|---|
| 331 | * known that the path will fit.
|
|---|
| 332 | */
|
|---|
| 333 | len = p->fts_pathlen = p->fts_namelen;
|
|---|
| 334 | memmove(sp->fts_path, p->fts_name, len + 1);
|
|---|
| 335 | #ifdef NEED_STRRSLASH
|
|---|
| 336 | if ((cp = strrslash(p->fts_name)) && (cp != p->fts_name || cp[1])) {
|
|---|
| 337 | #else
|
|---|
| 338 | if ((cp = strrchr(p->fts_name, '/')) && (cp != p->fts_name || cp[1])) {
|
|---|
| 339 | #endif
|
|---|
| 340 | len = strlen(++cp);
|
|---|
| 341 | memmove(p->fts_name, cp, len + 1);
|
|---|
| 342 | p->fts_namelen = len;
|
|---|
| 343 | }
|
|---|
| 344 | p->fts_accpath = p->fts_path = sp->fts_path;
|
|---|
| 345 | sp->fts_dev = p->fts_dev;
|
|---|
| 346 | }
|
|---|
| 347 |
|
|---|
| 348 | int
|
|---|
| 349 | fts_close(sp)
|
|---|
| 350 | FTS *sp;
|
|---|
| 351 | {
|
|---|
| 352 | FTSENT *freep, *p;
|
|---|
| 353 | int saved_errno = 0;
|
|---|
| 354 |
|
|---|
| 355 | _DIAGASSERT(sp != NULL);
|
|---|
| 356 |
|
|---|
| 357 | /*
|
|---|
| 358 | * This still works if we haven't read anything -- the dummy structure
|
|---|
| 359 | * points to the root list, so we step through to the end of the root
|
|---|
| 360 | * list which has a valid parent pointer.
|
|---|
| 361 | */
|
|---|
| 362 | if (sp->fts_cur) {
|
|---|
| 363 | #ifndef _MSC_VER
|
|---|
| 364 | if (ISSET(FTS_SYMFOLLOW))
|
|---|
| 365 | (void)close(sp->fts_cur->fts_symfd);
|
|---|
| 366 | #endif
|
|---|
| 367 | for (p = sp->fts_cur; p->fts_level >= FTS_ROOTLEVEL;) {
|
|---|
| 368 | freep = p;
|
|---|
| 369 | p = p->fts_link ? p->fts_link : p->fts_parent;
|
|---|
| 370 | free(freep);
|
|---|
| 371 | }
|
|---|
| 372 | free(p);
|
|---|
| 373 | }
|
|---|
| 374 |
|
|---|
| 375 | /* Free up child linked list, sort array, path buffer. */
|
|---|
| 376 | if (sp->fts_child)
|
|---|
| 377 | fts_lfree(sp->fts_child);
|
|---|
| 378 | if (sp->fts_array)
|
|---|
| 379 | free(sp->fts_array);
|
|---|
| 380 | free(sp->fts_path);
|
|---|
| 381 |
|
|---|
| 382 | /* Return to original directory, save errno if necessary. */
|
|---|
| 383 | if (!ISSET(FTS_NOCHDIR)) {
|
|---|
| 384 | #ifdef HAVE_FCHDIR
|
|---|
| 385 | if (fchdir(sp->fts_rfd))
|
|---|
| 386 | saved_errno = errno;
|
|---|
| 387 | (void)close(sp->fts_rfd);
|
|---|
| 388 | #else
|
|---|
| 389 | if (chdir(sp->fts_rdir))
|
|---|
| 390 | saved_errno = errno;
|
|---|
| 391 | free(sp->fts_rdir);
|
|---|
| 392 | sp->fts_rdir = NULL;
|
|---|
| 393 | #endif
|
|---|
| 394 | }
|
|---|
| 395 |
|
|---|
| 396 | /* Free up the stream pointer. */
|
|---|
| 397 | free(sp);
|
|---|
| 398 | /* ISSET() is illegal after this, since the macro touches sp */
|
|---|
| 399 |
|
|---|
| 400 | /* Set errno and return. */
|
|---|
| 401 | if (saved_errno) {
|
|---|
| 402 | errno = saved_errno;
|
|---|
| 403 | return (-1);
|
|---|
| 404 | }
|
|---|
| 405 | return (0);
|
|---|
| 406 | }
|
|---|
| 407 |
|
|---|
| 408 | /*
|
|---|
| 409 | * Special case a root of "/" so that slashes aren't appended which would
|
|---|
| 410 | * cause paths to be written as "//foo".
|
|---|
| 411 | */
|
|---|
| 412 | #define NAPPEND(p) \
|
|---|
| 413 | (p->fts_level == FTS_ROOTLEVEL && p->fts_pathlen == 1 && \
|
|---|
| 414 | IS_SLASH(p->fts_path[0]) ? 0 : p->fts_pathlen)
|
|---|
| 415 |
|
|---|
| 416 | FTSENT *
|
|---|
| 417 | fts_read(sp)
|
|---|
| 418 | FTS *sp;
|
|---|
| 419 | {
|
|---|
| 420 | FTSENT *p, *tmp;
|
|---|
| 421 | int instr;
|
|---|
| 422 | char *t;
|
|---|
| 423 | int saved_errno;
|
|---|
| 424 |
|
|---|
| 425 | _DIAGASSERT(sp != NULL);
|
|---|
| 426 |
|
|---|
| 427 | /* If finished or unrecoverable error, return NULL. */
|
|---|
| 428 | if (sp->fts_cur == NULL || ISSET(FTS_STOP))
|
|---|
| 429 | return (NULL);
|
|---|
| 430 |
|
|---|
| 431 | /* Set current node pointer. */
|
|---|
| 432 | p = sp->fts_cur;
|
|---|
| 433 |
|
|---|
| 434 | /* Save and zero out user instructions. */
|
|---|
| 435 | instr = p->fts_instr;
|
|---|
| 436 | p->fts_instr = FTS_NOINSTR;
|
|---|
| 437 |
|
|---|
| 438 | /* Any type of file may be re-visited; re-stat and re-turn. */
|
|---|
| 439 | if (instr == FTS_AGAIN) {
|
|---|
| 440 | p->fts_info = fts_stat(sp, p, 0);
|
|---|
| 441 | return (p);
|
|---|
| 442 | }
|
|---|
| 443 |
|
|---|
| 444 | /*
|
|---|
| 445 | * Following a symlink -- SLNONE test allows application to see
|
|---|
| 446 | * SLNONE and recover. If indirecting through a symlink, have
|
|---|
| 447 | * keep a pointer to current location. If unable to get that
|
|---|
| 448 | * pointer, follow fails.
|
|---|
| 449 | */
|
|---|
| 450 | if (instr == FTS_FOLLOW &&
|
|---|
| 451 | (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) {
|
|---|
| 452 | p->fts_info = fts_stat(sp, p, 1);
|
|---|
| 453 | if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) {
|
|---|
| 454 | #ifdef HAVE_FCHDIR
|
|---|
| 455 | if ((p->fts_symfd = open(".", O_RDONLY, 0)) == -1) {
|
|---|
| 456 | p->fts_errno = errno;
|
|---|
| 457 | p->fts_info = FTS_ERR;
|
|---|
| 458 | } else if (fcntl(p->fts_symfd, F_SETFD, FD_CLOEXEC) == -1) {
|
|---|
| 459 | p->fts_errno = errno;
|
|---|
| 460 | p->fts_info = FTS_ERR;
|
|---|
| 461 | close(p->fts_symfd);
|
|---|
| 462 | } else
|
|---|
| 463 | p->fts_flags |= FTS_SYMFOLLOW;
|
|---|
| 464 | #endif
|
|---|
| 465 | }
|
|---|
| 466 | return (p);
|
|---|
| 467 | }
|
|---|
| 468 |
|
|---|
| 469 | /* Directory in pre-order. */
|
|---|
| 470 | if (p->fts_info == FTS_D) {
|
|---|
| 471 | /* If skipped or crossed mount point, do post-order visit. */
|
|---|
| 472 | if (instr == FTS_SKIP ||
|
|---|
| 473 | (ISSET(FTS_XDEV) && p->fts_dev != sp->fts_dev)) {
|
|---|
| 474 | #ifdef HAVE_FCHDIR
|
|---|
| 475 | if (p->fts_flags & FTS_SYMFOLLOW)
|
|---|
| 476 | (void)close(p->fts_symfd);
|
|---|
| 477 | #endif
|
|---|
| 478 | if (sp->fts_child) {
|
|---|
| 479 | fts_lfree(sp->fts_child);
|
|---|
| 480 | sp->fts_child = NULL;
|
|---|
| 481 | }
|
|---|
| 482 | p->fts_info = FTS_DP;
|
|---|
| 483 | return (p);
|
|---|
| 484 | }
|
|---|
| 485 |
|
|---|
| 486 | /* Rebuild if only read the names and now traversing. */
|
|---|
| 487 | if (sp->fts_child && ISSET(FTS_NAMEONLY)) {
|
|---|
| 488 | CLR(FTS_NAMEONLY);
|
|---|
| 489 | fts_lfree(sp->fts_child);
|
|---|
| 490 | sp->fts_child = NULL;
|
|---|
| 491 | }
|
|---|
| 492 |
|
|---|
| 493 | /*
|
|---|
| 494 | * Cd to the subdirectory.
|
|---|
| 495 | *
|
|---|
| 496 | * If have already read and now fail to chdir, whack the list
|
|---|
| 497 | * to make the names come out right, and set the parent errno
|
|---|
| 498 | * so the application will eventually get an error condition.
|
|---|
| 499 | * Set the FTS_DONTCHDIR flag so that when we logically change
|
|---|
| 500 | * directories back to the parent we don't do a chdir.
|
|---|
| 501 | *
|
|---|
| 502 | * If haven't read do so. If the read fails, fts_build sets
|
|---|
| 503 | * FTS_STOP or the fts_info field of the node.
|
|---|
| 504 | */
|
|---|
| 505 | if (sp->fts_child) {
|
|---|
| 506 | if (fts_safe_changedir(sp, p, -1, p->fts_accpath)) {
|
|---|
| 507 | p->fts_errno = errno;
|
|---|
| 508 | p->fts_flags |= FTS_DONTCHDIR;
|
|---|
| 509 | for (p = sp->fts_child; p; p = p->fts_link)
|
|---|
| 510 | p->fts_accpath =
|
|---|
| 511 | p->fts_parent->fts_accpath;
|
|---|
| 512 | }
|
|---|
| 513 | } else if ((sp->fts_child = fts_build(sp, BREAD)) == NULL) {
|
|---|
| 514 | if (ISSET(FTS_STOP))
|
|---|
| 515 | return (NULL);
|
|---|
| 516 | return (p);
|
|---|
| 517 | }
|
|---|
| 518 | p = sp->fts_child;
|
|---|
| 519 | sp->fts_child = NULL;
|
|---|
| 520 | goto name;
|
|---|
| 521 | }
|
|---|
| 522 |
|
|---|
| 523 | /* Move to the next node on this level. */
|
|---|
| 524 | next: tmp = p;
|
|---|
| 525 | if ((p = p->fts_link) != NULL) {
|
|---|
| 526 | free(tmp);
|
|---|
| 527 |
|
|---|
| 528 | /*
|
|---|
| 529 | * If reached the top, return to the original directory, and
|
|---|
| 530 | * load the paths for the next root.
|
|---|
| 531 | */
|
|---|
| 532 | if (p->fts_level == FTS_ROOTLEVEL) {
|
|---|
| 533 | #ifdef HAVE_FCHDIR
|
|---|
| 534 | if (FCHDIR(sp, sp->fts_rfd)) {
|
|---|
| 535 | #else
|
|---|
| 536 | if (CHDIR(sp, sp->fts_rdir)) {
|
|---|
| 537 | #endif
|
|---|
| 538 | SET(FTS_STOP);
|
|---|
| 539 | return (NULL);
|
|---|
| 540 | }
|
|---|
| 541 | fts_load(sp, p);
|
|---|
| 542 | return (sp->fts_cur = p);
|
|---|
| 543 | }
|
|---|
| 544 |
|
|---|
| 545 | /*
|
|---|
| 546 | * User may have called fts_set on the node. If skipped,
|
|---|
| 547 | * ignore. If followed, get a file descriptor so we can
|
|---|
| 548 | * get back if necessary.
|
|---|
| 549 | */
|
|---|
| 550 | if (p->fts_instr == FTS_SKIP)
|
|---|
| 551 | goto next;
|
|---|
| 552 | if (p->fts_instr == FTS_FOLLOW) {
|
|---|
| 553 | p->fts_info = fts_stat(sp, p, 1);
|
|---|
| 554 | if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) {
|
|---|
| 555 | #ifdef HAVE_FCHDIR
|
|---|
| 556 | if ((p->fts_symfd =
|
|---|
| 557 | open(".", O_RDONLY, 0)) == -1) {
|
|---|
| 558 | p->fts_errno = errno;
|
|---|
| 559 | p->fts_info = FTS_ERR;
|
|---|
| 560 | } else if (fcntl(p->fts_symfd, F_SETFD, FD_CLOEXEC) == -1) {
|
|---|
| 561 | p->fts_errno = errno;
|
|---|
| 562 | p->fts_info = FTS_ERR;
|
|---|
| 563 | close(p->fts_symfd);
|
|---|
| 564 | } else
|
|---|
| 565 | p->fts_flags |= FTS_SYMFOLLOW;
|
|---|
| 566 | #endif
|
|---|
| 567 | }
|
|---|
| 568 | p->fts_instr = FTS_NOINSTR;
|
|---|
| 569 | }
|
|---|
| 570 |
|
|---|
| 571 | name: t = sp->fts_path + NAPPEND(p->fts_parent);
|
|---|
| 572 | *t++ = '/';
|
|---|
| 573 | memmove(t, p->fts_name, (size_t)(p->fts_namelen + 1));
|
|---|
| 574 | return (sp->fts_cur = p);
|
|---|
| 575 | }
|
|---|
| 576 |
|
|---|
| 577 | /* Move up to the parent node. */
|
|---|
| 578 | p = tmp->fts_parent;
|
|---|
| 579 | free(tmp);
|
|---|
| 580 |
|
|---|
| 581 | if (p->fts_level == FTS_ROOTPARENTLEVEL) {
|
|---|
| 582 | /*
|
|---|
| 583 | * Done; free everything up and set errno to 0 so the user
|
|---|
| 584 | * can distinguish between error and EOF.
|
|---|
| 585 | */
|
|---|
| 586 | free(p);
|
|---|
| 587 | errno = 0;
|
|---|
| 588 | return (sp->fts_cur = NULL);
|
|---|
| 589 | }
|
|---|
| 590 |
|
|---|
| 591 | /* Nul terminate the pathname. */
|
|---|
| 592 | sp->fts_path[p->fts_pathlen] = '\0';
|
|---|
| 593 |
|
|---|
| 594 | /*
|
|---|
| 595 | * Return to the parent directory. If at a root node or came through
|
|---|
| 596 | * a symlink, go back through the file descriptor. Otherwise, cd up
|
|---|
| 597 | * one directory.
|
|---|
| 598 | */
|
|---|
| 599 | if (p->fts_level == FTS_ROOTLEVEL) {
|
|---|
| 600 | #ifdef HAVE_FCHDIR
|
|---|
| 601 | if (FCHDIR(sp, sp->fts_rfd)) {
|
|---|
| 602 | #else
|
|---|
| 603 | if (CHDIR(sp, sp->fts_rdir)) {
|
|---|
| 604 | #endif
|
|---|
| 605 | SET(FTS_STOP);
|
|---|
| 606 | return (NULL);
|
|---|
| 607 | }
|
|---|
| 608 | #ifdef HAVE_FCHDIR
|
|---|
| 609 | } else if (p->fts_flags & FTS_SYMFOLLOW) {
|
|---|
| 610 | if (FCHDIR(sp, p->fts_symfd)) {
|
|---|
| 611 | saved_errno = errno;
|
|---|
| 612 | (void)close(p->fts_symfd);
|
|---|
| 613 | errno = saved_errno;
|
|---|
| 614 | SET(FTS_STOP);
|
|---|
| 615 | return (NULL);
|
|---|
| 616 | }
|
|---|
| 617 | (void)close(p->fts_symfd);
|
|---|
| 618 | #else
|
|---|
| 619 | (void)saved_errno;
|
|---|
| 620 | #endif
|
|---|
| 621 | } else if (!(p->fts_flags & FTS_DONTCHDIR) &&
|
|---|
| 622 | fts_safe_changedir(sp, p->fts_parent, -1, "..")) {
|
|---|
| 623 | SET(FTS_STOP);
|
|---|
| 624 | return (NULL);
|
|---|
| 625 | }
|
|---|
| 626 | p->fts_info = p->fts_errno ? FTS_ERR : FTS_DP;
|
|---|
| 627 | return (sp->fts_cur = p);
|
|---|
| 628 | }
|
|---|
| 629 |
|
|---|
| 630 | /*
|
|---|
| 631 | * Fts_set takes the stream as an argument although it's not used in this
|
|---|
| 632 | * implementation; it would be necessary if anyone wanted to add global
|
|---|
| 633 | * semantics to fts using fts_set. An error return is allowed for similar
|
|---|
| 634 | * reasons.
|
|---|
| 635 | */
|
|---|
| 636 | /* ARGSUSED */
|
|---|
| 637 | int
|
|---|
| 638 | fts_set(sp, p, instr)
|
|---|
| 639 | FTS *sp;
|
|---|
| 640 | FTSENT *p;
|
|---|
| 641 | int instr;
|
|---|
| 642 | {
|
|---|
| 643 |
|
|---|
| 644 | _DIAGASSERT(sp != NULL);
|
|---|
| 645 | _DIAGASSERT(p != NULL);
|
|---|
| 646 |
|
|---|
| 647 | if (instr && instr != FTS_AGAIN && instr != FTS_FOLLOW &&
|
|---|
| 648 | instr != FTS_NOINSTR && instr != FTS_SKIP) {
|
|---|
| 649 | errno = EINVAL;
|
|---|
| 650 | return (1);
|
|---|
| 651 | }
|
|---|
| 652 | p->fts_instr = instr;
|
|---|
| 653 | return (0);
|
|---|
| 654 | }
|
|---|
| 655 |
|
|---|
| 656 | FTSENT *
|
|---|
| 657 | fts_children(sp, instr)
|
|---|
| 658 | FTS *sp;
|
|---|
| 659 | int instr;
|
|---|
| 660 | {
|
|---|
| 661 | FTSENT *p;
|
|---|
| 662 | #ifdef HAVE_FCHDIR
|
|---|
| 663 | int fd;
|
|---|
| 664 | #else
|
|---|
| 665 | char *pszRoot;
|
|---|
| 666 | int rc;
|
|---|
| 667 | #endif
|
|---|
| 668 |
|
|---|
| 669 | _DIAGASSERT(sp != NULL);
|
|---|
| 670 |
|
|---|
| 671 | if (instr && instr != FTS_NAMEONLY) {
|
|---|
| 672 | errno = EINVAL;
|
|---|
| 673 | return (NULL);
|
|---|
| 674 | }
|
|---|
| 675 |
|
|---|
| 676 | /* Set current node pointer. */
|
|---|
| 677 | p = sp->fts_cur;
|
|---|
| 678 |
|
|---|
| 679 | /*
|
|---|
| 680 | * Errno set to 0 so user can distinguish empty directory from
|
|---|
| 681 | * an error.
|
|---|
| 682 | */
|
|---|
| 683 | errno = 0;
|
|---|
| 684 |
|
|---|
| 685 | /* Fatal errors stop here. */
|
|---|
| 686 | if (ISSET(FTS_STOP))
|
|---|
| 687 | return (NULL);
|
|---|
| 688 |
|
|---|
| 689 | /* Return logical hierarchy of user's arguments. */
|
|---|
| 690 | if (p->fts_info == FTS_INIT)
|
|---|
| 691 | return (p->fts_link);
|
|---|
| 692 |
|
|---|
| 693 | /*
|
|---|
| 694 | * If not a directory being visited in pre-order, stop here. Could
|
|---|
| 695 | * allow FTS_DNR, assuming the user has fixed the problem, but the
|
|---|
| 696 | * same effect is available with FTS_AGAIN.
|
|---|
| 697 | */
|
|---|
| 698 | if (p->fts_info != FTS_D /* && p->fts_info != FTS_DNR */)
|
|---|
| 699 | return (NULL);
|
|---|
| 700 |
|
|---|
| 701 | /* Free up any previous child list. */
|
|---|
| 702 | if (sp->fts_child)
|
|---|
| 703 | fts_lfree(sp->fts_child);
|
|---|
| 704 |
|
|---|
| 705 | if (instr == FTS_NAMEONLY) {
|
|---|
| 706 | SET(FTS_NAMEONLY);
|
|---|
| 707 | instr = BNAMES;
|
|---|
| 708 | } else
|
|---|
| 709 | instr = BCHILD;
|
|---|
| 710 |
|
|---|
| 711 | /*
|
|---|
| 712 | * If using chdir on a relative path and called BEFORE fts_read does
|
|---|
| 713 | * its chdir to the root of a traversal, we can lose -- we need to
|
|---|
| 714 | * chdir into the subdirectory, and we don't know where the current
|
|---|
| 715 | * directory is, so we can't get back so that the upcoming chdir by
|
|---|
| 716 | * fts_read will work.
|
|---|
| 717 | */
|
|---|
| 718 | if (p->fts_level != FTS_ROOTLEVEL || IS_SLASH(p->fts_accpath[0]) ||
|
|---|
| 719 | ISSET(FTS_NOCHDIR))
|
|---|
| 720 | return (sp->fts_child = fts_build(sp, instr));
|
|---|
| 721 |
|
|---|
| 722 | #ifdef HAVE_FCHDIR
|
|---|
| 723 | if ((fd = open(".", O_RDONLY, 0)) == -1)
|
|---|
| 724 | #else
|
|---|
| 725 | if ((pszRoot = getcwd(NULL, 0)) == NULL)
|
|---|
| 726 | #endif
|
|---|
| 727 | return (sp->fts_child = NULL);
|
|---|
| 728 | sp->fts_child = fts_build(sp, instr);
|
|---|
| 729 | #ifdef HAVE_FCHDIR
|
|---|
| 730 | if (fchdir(fd)) {
|
|---|
| 731 | (void)close(fd);
|
|---|
| 732 | return (NULL);
|
|---|
| 733 | }
|
|---|
| 734 | (void)close(fd);
|
|---|
| 735 | #else
|
|---|
| 736 | rc = chdir(pszRoot);
|
|---|
| 737 | free(pszRoot);
|
|---|
| 738 | if (rc)
|
|---|
| 739 | return (NULL);
|
|---|
| 740 | #endif
|
|---|
| 741 |
|
|---|
| 742 | return (sp->fts_child);
|
|---|
| 743 | }
|
|---|
| 744 |
|
|---|
| 745 | /*
|
|---|
| 746 | * This is the tricky part -- do not casually change *anything* in here. The
|
|---|
| 747 | * idea is to build the linked list of entries that are used by fts_children
|
|---|
| 748 | * and fts_read. There are lots of special cases.
|
|---|
| 749 | *
|
|---|
| 750 | * The real slowdown in walking the tree is the stat calls. If FTS_NOSTAT is
|
|---|
| 751 | * set and it's a physical walk (so that symbolic links can't be directories),
|
|---|
| 752 | * we can do things quickly. First, if it's a 4.4BSD file system, the type
|
|---|
| 753 | * of the file is in the directory entry. Otherwise, we assume that the number
|
|---|
| 754 | * of subdirectories in a node is equal to the number of links to the parent.
|
|---|
| 755 | * The former skips all stat calls. The latter skips stat calls in any leaf
|
|---|
| 756 | * directories and for any files after the subdirectories in the directory have
|
|---|
| 757 | * been found, cutting the stat calls by about 2/3.
|
|---|
| 758 | */
|
|---|
| 759 | static FTSENT *
|
|---|
| 760 | fts_build(sp, type)
|
|---|
| 761 | FTS *sp;
|
|---|
| 762 | int type;
|
|---|
| 763 | {
|
|---|
| 764 | struct dirent *dp;
|
|---|
| 765 | FTSENT *p, *head;
|
|---|
| 766 | size_t nitems;
|
|---|
| 767 | FTSENT *cur, *tail;
|
|---|
| 768 | DIR *dirp;
|
|---|
| 769 | int adjust, cderrno, descend, len, level, nlinks, saved_errno, nostat;
|
|---|
| 770 | size_t maxlen;
|
|---|
| 771 | #ifdef FTS_WHITEOUT
|
|---|
| 772 | int oflag;
|
|---|
| 773 | #endif
|
|---|
| 774 | char *cp = NULL; /* pacify gcc */
|
|---|
| 775 |
|
|---|
| 776 | _DIAGASSERT(sp != NULL);
|
|---|
| 777 |
|
|---|
| 778 | /* Set current node pointer. */
|
|---|
| 779 | cur = sp->fts_cur;
|
|---|
| 780 |
|
|---|
| 781 | /*
|
|---|
| 782 | * Open the directory for reading. If this fails, we're done.
|
|---|
| 783 | * If being called from fts_read, set the fts_info field.
|
|---|
| 784 | */
|
|---|
| 785 | #if defined(FTS_WHITEOUT) && !defined(__OS2__)
|
|---|
| 786 | if (ISSET(FTS_WHITEOUT))
|
|---|
| 787 | oflag = DTF_NODUP|DTF_REWIND;
|
|---|
| 788 | else
|
|---|
| 789 | oflag = DTF_HIDEW|DTF_NODUP|DTF_REWIND;
|
|---|
| 790 | #elif defined(_MSC_VER)
|
|---|
| 791 | # define __opendir2(path, flag) birdDirOpenExtraInfo(path)
|
|---|
| 792 | #else
|
|---|
| 793 | #define __opendir2(path, flag) opendir(path)
|
|---|
| 794 | #endif
|
|---|
| 795 | if ((dirp = __opendir2(cur->fts_accpath, oflag)) == NULL) {
|
|---|
| 796 | if (type == BREAD) {
|
|---|
| 797 | cur->fts_info = FTS_DNR;
|
|---|
| 798 | cur->fts_errno = errno;
|
|---|
| 799 | }
|
|---|
| 800 | return (NULL);
|
|---|
| 801 | }
|
|---|
| 802 |
|
|---|
| 803 | /*
|
|---|
| 804 | * Nlinks is the number of possible entries of type directory in the
|
|---|
| 805 | * directory if we're cheating on stat calls, 0 if we're not doing
|
|---|
| 806 | * any stat calls at all, -1 if we're doing stats on everything.
|
|---|
| 807 | */
|
|---|
| 808 | if (type == BNAMES) {
|
|---|
| 809 | nlinks = 0;
|
|---|
| 810 | nostat = 1;
|
|---|
| 811 | } else if (ISSET(FTS_NOSTAT) && ISSET(FTS_PHYSICAL)) {
|
|---|
| 812 | nlinks = cur->fts_nlink - (ISSET(FTS_SEEDOT) ? 0 : 2);
|
|---|
| 813 | nostat = 1;
|
|---|
| 814 | } else {
|
|---|
| 815 | nlinks = -1;
|
|---|
| 816 | nostat = 0;
|
|---|
| 817 | }
|
|---|
| 818 |
|
|---|
| 819 | #ifdef notdef
|
|---|
| 820 | (void)printf("nlinks == %d (cur: %d)\n", nlinks, cur->fts_nlink);
|
|---|
| 821 | (void)printf("NOSTAT %d PHYSICAL %d SEEDOT %d\n",
|
|---|
| 822 | ISSET(FTS_NOSTAT), ISSET(FTS_PHYSICAL), ISSET(FTS_SEEDOT));
|
|---|
| 823 | #endif
|
|---|
| 824 | /*
|
|---|
| 825 | * If we're going to need to stat anything or we want to descend
|
|---|
| 826 | * and stay in the directory, chdir. If this fails we keep going,
|
|---|
| 827 | * but set a flag so we don't chdir after the post-order visit.
|
|---|
| 828 | * We won't be able to stat anything, but we can still return the
|
|---|
| 829 | * names themselves. Note, that since fts_read won't be able to
|
|---|
| 830 | * chdir into the directory, it will have to return different path
|
|---|
| 831 | * names than before, i.e. "a/b" instead of "b". Since the node
|
|---|
| 832 | * has already been visited in pre-order, have to wait until the
|
|---|
| 833 | * post-order visit to return the error. There is a special case
|
|---|
| 834 | * here, if there was nothing to stat then it's not an error to
|
|---|
| 835 | * not be able to stat. This is all fairly nasty. If a program
|
|---|
| 836 | * needed sorted entries or stat information, they had better be
|
|---|
| 837 | * checking FTS_NS on the returned nodes.
|
|---|
| 838 | */
|
|---|
| 839 | cderrno = 0;
|
|---|
| 840 | if (nlinks || type == BREAD) {
|
|---|
| 841 | #ifdef HAVE_FCHDIR
|
|---|
| 842 | if (fts_safe_changedir(sp, cur, dirfd(dirp), NULL)) {
|
|---|
| 843 | #else
|
|---|
| 844 | if (fts_safe_changedir(sp, cur, dirfd(dirp), cur->fts_accpath)) {
|
|---|
| 845 | #endif
|
|---|
| 846 | if (nlinks && type == BREAD)
|
|---|
| 847 | cur->fts_errno = errno;
|
|---|
| 848 | cur->fts_flags |= FTS_DONTCHDIR;
|
|---|
| 849 | descend = 0;
|
|---|
| 850 | cderrno = errno;
|
|---|
| 851 | } else
|
|---|
| 852 | descend = 1;
|
|---|
| 853 | } else
|
|---|
| 854 | descend = 0;
|
|---|
| 855 |
|
|---|
| 856 | /*
|
|---|
| 857 | * Figure out the max file name length that can be stored in the
|
|---|
| 858 | * current path -- the inner loop allocates more path as necessary.
|
|---|
| 859 | * We really wouldn't have to do the maxlen calculations here, we
|
|---|
| 860 | * could do them in fts_read before returning the path, but it's a
|
|---|
| 861 | * lot easier here since the length is part of the dirent structure.
|
|---|
| 862 | *
|
|---|
| 863 | * If not changing directories set a pointer so that can just append
|
|---|
| 864 | * each new name into the path.
|
|---|
| 865 | */
|
|---|
| 866 | len = NAPPEND(cur);
|
|---|
| 867 | if (ISSET(FTS_NOCHDIR)) {
|
|---|
| 868 | cp = sp->fts_path + len;
|
|---|
| 869 | *cp++ = '/';
|
|---|
| 870 | }
|
|---|
| 871 | len++;
|
|---|
| 872 | maxlen = sp->fts_pathlen - len;
|
|---|
| 873 |
|
|---|
| 874 | level = cur->fts_level + 1;
|
|---|
| 875 |
|
|---|
| 876 | /* Read the directory, attaching each entry to the `link' pointer. */
|
|---|
| 877 | adjust = 0;
|
|---|
| 878 | for (head = tail = NULL, nitems = 0; (dp = readdir(dirp)) != NULL;) {
|
|---|
| 879 | size_t dlen;
|
|---|
| 880 |
|
|---|
| 881 | if (!ISSET(FTS_SEEDOT) && ISDOT(dp->d_name))
|
|---|
| 882 | continue;
|
|---|
| 883 |
|
|---|
| 884 | #if HAVE_STRUCT_DIRENT_D_NAMLEN
|
|---|
| 885 | dlen = dp->d_namlen;
|
|---|
| 886 | #else
|
|---|
| 887 | dlen = strlen(dp->d_name);
|
|---|
| 888 | #endif
|
|---|
| 889 | if ((p = fts_alloc(sp, dp->d_name, dlen)) == NULL)
|
|---|
| 890 | goto mem1;
|
|---|
| 891 | if (dlen >= maxlen) { /* include space for NUL */
|
|---|
| 892 | if (fts_palloc(sp, len + dlen + 1)) {
|
|---|
| 893 | /*
|
|---|
| 894 | * No more memory for path or structures. Save
|
|---|
| 895 | * errno, free up the current structure and the
|
|---|
| 896 | * structures already allocated.
|
|---|
| 897 | */
|
|---|
| 898 | mem1: saved_errno = errno;
|
|---|
| 899 | if (p)
|
|---|
| 900 | free(p);
|
|---|
| 901 | fts_lfree(head);
|
|---|
| 902 | (void)closedir(dirp);
|
|---|
| 903 | errno = saved_errno;
|
|---|
| 904 | cur->fts_info = FTS_ERR;
|
|---|
| 905 | SET(FTS_STOP);
|
|---|
| 906 | return (NULL);
|
|---|
| 907 | }
|
|---|
| 908 | adjust = 1;
|
|---|
| 909 | if (ISSET(FTS_NOCHDIR))
|
|---|
| 910 | cp = sp->fts_path + len;
|
|---|
| 911 | maxlen = sp->fts_pathlen - len;
|
|---|
| 912 | }
|
|---|
| 913 |
|
|---|
| 914 | p->fts_pathlen = len + dlen;
|
|---|
| 915 | p->fts_parent = sp->fts_cur;
|
|---|
| 916 | p->fts_level = level;
|
|---|
| 917 |
|
|---|
| 918 | #ifdef FTS_WHITEOUT
|
|---|
| 919 | if (dp->d_type == DT_WHT)
|
|---|
| 920 | p->fts_flags |= FTS_ISW;
|
|---|
| 921 | #endif
|
|---|
| 922 |
|
|---|
| 923 | if (cderrno) {
|
|---|
| 924 | if (nlinks) {
|
|---|
| 925 | p->fts_info = FTS_NS;
|
|---|
| 926 | p->fts_errno = cderrno;
|
|---|
| 927 | } else
|
|---|
| 928 | p->fts_info = FTS_NSOK;
|
|---|
| 929 | p->fts_accpath = cur->fts_accpath;
|
|---|
| 930 | } else if (nlinks == 0
|
|---|
| 931 | #ifdef DT_DIR
|
|---|
| 932 | || (nostat &&
|
|---|
| 933 | dp->d_type != DT_DIR && dp->d_type != DT_UNKNOWN)
|
|---|
| 934 | #endif
|
|---|
| 935 | ) {
|
|---|
| 936 | p->fts_accpath =
|
|---|
| 937 | ISSET(FTS_NOCHDIR) ? p->fts_path : p->fts_name;
|
|---|
| 938 | p->fts_info = FTS_NSOK;
|
|---|
| 939 | } else {
|
|---|
| 940 | /* Build a file name for fts_stat to stat. */
|
|---|
| 941 | if (ISSET(FTS_NOCHDIR)) {
|
|---|
| 942 | p->fts_accpath = p->fts_path;
|
|---|
| 943 | memmove(cp, p->fts_name,
|
|---|
| 944 | (size_t)(p->fts_namelen + 1));
|
|---|
| 945 | } else
|
|---|
| 946 | p->fts_accpath = p->fts_name;
|
|---|
| 947 |
|
|---|
| 948 | /* Stat it. */
|
|---|
| 949 | #ifdef _MSC_VER
|
|---|
| 950 | p->fts_info = fts_stat_dirent(sp, p, 0, dp);
|
|---|
| 951 | #else
|
|---|
| 952 | p->fts_info = fts_stat(sp, p, 0);
|
|---|
| 953 | #endif
|
|---|
| 954 |
|
|---|
| 955 | /* Decrement link count if applicable. */
|
|---|
| 956 | if (nlinks > 0 && (p->fts_info == FTS_D ||
|
|---|
| 957 | p->fts_info == FTS_DC || p->fts_info == FTS_DOT))
|
|---|
| 958 | --nlinks;
|
|---|
| 959 | }
|
|---|
| 960 |
|
|---|
| 961 | /* We walk in directory order so "ls -f" doesn't get upset. */
|
|---|
| 962 | p->fts_link = NULL;
|
|---|
| 963 | if (head == NULL)
|
|---|
| 964 | head = tail = p;
|
|---|
| 965 | else {
|
|---|
| 966 | tail->fts_link = p;
|
|---|
| 967 | tail = p;
|
|---|
| 968 | }
|
|---|
| 969 | ++nitems;
|
|---|
| 970 | }
|
|---|
| 971 | (void)closedir(dirp);
|
|---|
| 972 |
|
|---|
| 973 | /*
|
|---|
| 974 | * If had to realloc the path, adjust the addresses for the rest
|
|---|
| 975 | * of the tree.
|
|---|
| 976 | */
|
|---|
| 977 | if (adjust)
|
|---|
| 978 | fts_padjust(sp, head);
|
|---|
| 979 |
|
|---|
| 980 | /*
|
|---|
| 981 | * If not changing directories, reset the path back to original
|
|---|
| 982 | * state.
|
|---|
| 983 | */
|
|---|
| 984 | if (ISSET(FTS_NOCHDIR)) {
|
|---|
| 985 | if (cp - 1 > sp->fts_path)
|
|---|
| 986 | --cp;
|
|---|
| 987 | *cp = '\0';
|
|---|
| 988 | }
|
|---|
| 989 |
|
|---|
| 990 | /*
|
|---|
| 991 | * If descended after called from fts_children or after called from
|
|---|
| 992 | * fts_read and nothing found, get back. At the root level we use
|
|---|
| 993 | * the saved fd; if one of fts_open()'s arguments is a relative path
|
|---|
| 994 | * to an empty directory, we wind up here with no other way back. If
|
|---|
| 995 | * can't get back, we're done.
|
|---|
| 996 | */
|
|---|
| 997 | if (descend && (type == BCHILD || !nitems) &&
|
|---|
| 998 | (cur->fts_level == FTS_ROOTLEVEL ?
|
|---|
| 999 | #ifdef HAVE_FCHDIR
|
|---|
| 1000 | FCHDIR(sp, sp->fts_rfd) :
|
|---|
| 1001 | #else
|
|---|
| 1002 | CHDIR(sp, sp->fts_rdir) :
|
|---|
| 1003 | #endif
|
|---|
| 1004 | fts_safe_changedir(sp, cur->fts_parent, -1, ".."))) {
|
|---|
| 1005 | cur->fts_info = FTS_ERR;
|
|---|
| 1006 | SET(FTS_STOP);
|
|---|
| 1007 | return (NULL);
|
|---|
| 1008 | }
|
|---|
| 1009 |
|
|---|
| 1010 | /* If didn't find anything, return NULL. */
|
|---|
| 1011 | if (!nitems) {
|
|---|
| 1012 | if (type == BREAD)
|
|---|
| 1013 | cur->fts_info = FTS_DP;
|
|---|
| 1014 | return (NULL);
|
|---|
| 1015 | }
|
|---|
| 1016 |
|
|---|
| 1017 | /* Sort the entries. */
|
|---|
| 1018 | if (sp->fts_compar && nitems > 1)
|
|---|
| 1019 | head = fts_sort(sp, head, nitems);
|
|---|
| 1020 | return (head);
|
|---|
| 1021 | }
|
|---|
| 1022 |
|
|---|
| 1023 | #ifdef _MSC_VER
|
|---|
| 1024 | /** Special version of fts_stat that takes the information from the directory
|
|---|
| 1025 | * entry returned by readdir().
|
|---|
| 1026 | *
|
|---|
| 1027 | * Directory listing returns all the stat information on systems likes
|
|---|
| 1028 | * Windows and OS/2. */
|
|---|
| 1029 | static u_short
|
|---|
| 1030 | fts_stat_dirent(FTS *sp, FTSENT *p, int follow, struct dirent *pDirEnt)
|
|---|
| 1031 | {
|
|---|
| 1032 | FTSENT *t;
|
|---|
| 1033 | dev_t dev;
|
|---|
| 1034 | ino_t ino;
|
|---|
| 1035 | struct STAT *sbp, sb;
|
|---|
| 1036 | int saved_errno;
|
|---|
| 1037 |
|
|---|
| 1038 | _DIAGASSERT(sp != NULL);
|
|---|
| 1039 | _DIAGASSERT(p != NULL);
|
|---|
| 1040 |
|
|---|
| 1041 | /* If user needs stat info, stat buffer already allocated. */
|
|---|
| 1042 | sbp = ISSET(FTS_NOSTAT) ? &sb : p->fts_statp;
|
|---|
| 1043 |
|
|---|
| 1044 | /*
|
|---|
| 1045 | * Copy over the stat info from the direntry.
|
|---|
| 1046 | */
|
|---|
| 1047 | *sbp = pDirEnt->d_stat;
|
|---|
| 1048 |
|
|---|
| 1049 | /*
|
|---|
| 1050 | * If doing a logical walk, or application requested FTS_FOLLOW, do
|
|---|
| 1051 | * a stat(2) on symlinks. If that fails, assume non-existent
|
|---|
| 1052 | * symlink and set the errno from the stat call.
|
|---|
| 1053 | */
|
|---|
| 1054 | if (S_ISLNK(sbp->st_mode) && (ISSET(FTS_LOGICAL) || follow)) {
|
|---|
| 1055 | if (stat(p->fts_accpath, sbp)) {
|
|---|
| 1056 | saved_errno = errno;
|
|---|
| 1057 | errno = 0;
|
|---|
| 1058 | return (FTS_SLNONE);
|
|---|
| 1059 | }
|
|---|
| 1060 | }
|
|---|
| 1061 |
|
|---|
| 1062 | if (S_ISDIR(sbp->st_mode)) {
|
|---|
| 1063 | /*
|
|---|
| 1064 | * Set the device/inode. Used to find cycles and check for
|
|---|
| 1065 | * crossing mount points. Also remember the link count, used
|
|---|
| 1066 | * in fts_build to limit the number of stat calls. It is
|
|---|
| 1067 | * understood that these fields are only referenced if fts_info
|
|---|
| 1068 | * is set to FTS_D.
|
|---|
| 1069 | */
|
|---|
| 1070 | dev = p->fts_dev = sbp->st_dev;
|
|---|
| 1071 | ino = p->fts_ino = sbp->st_ino;
|
|---|
| 1072 | p->fts_nlink = sbp->st_nlink;
|
|---|
| 1073 |
|
|---|
| 1074 | if (ISDOT(p->fts_name))
|
|---|
| 1075 | return (FTS_DOT);
|
|---|
| 1076 |
|
|---|
| 1077 | /*
|
|---|
| 1078 | * Cycle detection is done by brute force when the directory
|
|---|
| 1079 | * is first encountered. If the tree gets deep enough or the
|
|---|
| 1080 | * number of symbolic links to directories is high enough,
|
|---|
| 1081 | * something faster might be worthwhile.
|
|---|
| 1082 | */
|
|---|
| 1083 |
|
|---|
| 1084 | #ifdef _MSC_VER
|
|---|
| 1085 | if (ino && dev) /** @todo ino emulation on windows... */
|
|---|
| 1086 | #endif
|
|---|
| 1087 | for (t = p->fts_parent;
|
|---|
| 1088 | t->fts_level >= FTS_ROOTLEVEL; t = t->fts_parent)
|
|---|
| 1089 | if (ino == t->fts_ino && dev == t->fts_dev) {
|
|---|
| 1090 | p->fts_cycle = t;
|
|---|
| 1091 | return (FTS_DC);
|
|---|
| 1092 | }
|
|---|
| 1093 | return (FTS_D);
|
|---|
| 1094 | }
|
|---|
| 1095 | if (S_ISLNK(sbp->st_mode))
|
|---|
| 1096 | return (FTS_SL);
|
|---|
| 1097 | if (S_ISREG(sbp->st_mode))
|
|---|
| 1098 | return (FTS_F);
|
|---|
| 1099 | return (FTS_DEFAULT);
|
|---|
| 1100 | }
|
|---|
| 1101 |
|
|---|
| 1102 | #endif /* fts_stat_dirent */
|
|---|
| 1103 |
|
|---|
| 1104 | static u_short
|
|---|
| 1105 | fts_stat(sp, p, follow)
|
|---|
| 1106 | FTS *sp;
|
|---|
| 1107 | FTSENT *p;
|
|---|
| 1108 | int follow;
|
|---|
| 1109 | {
|
|---|
| 1110 | FTSENT *t;
|
|---|
| 1111 | dev_t dev;
|
|---|
| 1112 | ino_t ino;
|
|---|
| 1113 | struct STAT *sbp, sb;
|
|---|
| 1114 | int saved_errno;
|
|---|
| 1115 |
|
|---|
| 1116 | _DIAGASSERT(sp != NULL);
|
|---|
| 1117 | _DIAGASSERT(p != NULL);
|
|---|
| 1118 |
|
|---|
| 1119 | /* If user needs stat info, stat buffer already allocated. */
|
|---|
| 1120 | sbp = ISSET(FTS_NOSTAT) ? &sb : p->fts_statp;
|
|---|
| 1121 |
|
|---|
| 1122 | #ifdef FTS_WHITEOUT
|
|---|
| 1123 | /* check for whiteout */
|
|---|
| 1124 | if (p->fts_flags & FTS_ISW) {
|
|---|
| 1125 | if (sbp != &sb) {
|
|---|
| 1126 | memset(sbp, '\0', sizeof (*sbp));
|
|---|
| 1127 | sbp->st_mode = S_IFWHT;
|
|---|
| 1128 | }
|
|---|
| 1129 | return (FTS_W);
|
|---|
| 1130 | }
|
|---|
| 1131 | #endif
|
|---|
| 1132 |
|
|---|
| 1133 | /*
|
|---|
| 1134 | * If doing a logical walk, or application requested FTS_FOLLOW, do
|
|---|
| 1135 | * a stat(2). If that fails, check for a non-existent symlink. If
|
|---|
| 1136 | * fail, set the errno from the stat call.
|
|---|
| 1137 | */
|
|---|
| 1138 | if (ISSET(FTS_LOGICAL) || follow) {
|
|---|
| 1139 | if (stat(p->fts_accpath, sbp)) {
|
|---|
| 1140 | saved_errno = errno;
|
|---|
| 1141 | if (!lstat(p->fts_accpath, sbp)) {
|
|---|
| 1142 | errno = 0;
|
|---|
| 1143 | return (FTS_SLNONE);
|
|---|
| 1144 | }
|
|---|
| 1145 | p->fts_errno = saved_errno;
|
|---|
| 1146 | goto err;
|
|---|
| 1147 | }
|
|---|
| 1148 | } else if (lstat(p->fts_accpath, sbp)) {
|
|---|
| 1149 | p->fts_errno = errno;
|
|---|
| 1150 | err: memset(sbp, 0, sizeof(struct STAT));
|
|---|
| 1151 | return (FTS_NS);
|
|---|
| 1152 | }
|
|---|
| 1153 |
|
|---|
| 1154 | if (S_ISDIR(sbp->st_mode)) {
|
|---|
| 1155 | /*
|
|---|
| 1156 | * Set the device/inode. Used to find cycles and check for
|
|---|
| 1157 | * crossing mount points. Also remember the link count, used
|
|---|
| 1158 | * in fts_build to limit the number of stat calls. It is
|
|---|
| 1159 | * understood that these fields are only referenced if fts_info
|
|---|
| 1160 | * is set to FTS_D.
|
|---|
| 1161 | */
|
|---|
| 1162 | dev = p->fts_dev = sbp->st_dev;
|
|---|
| 1163 | ino = p->fts_ino = sbp->st_ino;
|
|---|
| 1164 | p->fts_nlink = sbp->st_nlink;
|
|---|
| 1165 |
|
|---|
| 1166 | if (ISDOT(p->fts_name))
|
|---|
| 1167 | return (FTS_DOT);
|
|---|
| 1168 |
|
|---|
| 1169 | /*
|
|---|
| 1170 | * Cycle detection is done by brute force when the directory
|
|---|
| 1171 | * is first encountered. If the tree gets deep enough or the
|
|---|
| 1172 | * number of symbolic links to directories is high enough,
|
|---|
| 1173 | * something faster might be worthwhile.
|
|---|
| 1174 | */
|
|---|
| 1175 |
|
|---|
| 1176 | #ifdef _MSC_VER
|
|---|
| 1177 | if (ino && dev) /** @todo ino emulation on windows... */
|
|---|
| 1178 | #endif
|
|---|
| 1179 | for (t = p->fts_parent;
|
|---|
| 1180 | t->fts_level >= FTS_ROOTLEVEL; t = t->fts_parent)
|
|---|
| 1181 | if (ino == t->fts_ino && dev == t->fts_dev) {
|
|---|
| 1182 | p->fts_cycle = t;
|
|---|
| 1183 | return (FTS_DC);
|
|---|
| 1184 | }
|
|---|
| 1185 | return (FTS_D);
|
|---|
| 1186 | }
|
|---|
| 1187 | if (S_ISLNK(sbp->st_mode))
|
|---|
| 1188 | return (FTS_SL);
|
|---|
| 1189 | if (S_ISREG(sbp->st_mode))
|
|---|
| 1190 | return (FTS_F);
|
|---|
| 1191 | return (FTS_DEFAULT);
|
|---|
| 1192 | }
|
|---|
| 1193 |
|
|---|
| 1194 | static FTSENT *
|
|---|
| 1195 | fts_sort(sp, head, nitems)
|
|---|
| 1196 | FTS *sp;
|
|---|
| 1197 | FTSENT *head;
|
|---|
| 1198 | size_t nitems;
|
|---|
| 1199 | {
|
|---|
| 1200 | FTSENT **ap, *p;
|
|---|
| 1201 |
|
|---|
| 1202 | _DIAGASSERT(sp != NULL);
|
|---|
| 1203 | _DIAGASSERT(head != NULL);
|
|---|
| 1204 |
|
|---|
| 1205 | /*
|
|---|
| 1206 | * Construct an array of pointers to the structures and call qsort(3).
|
|---|
| 1207 | * Reassemble the array in the order returned by qsort. If unable to
|
|---|
| 1208 | * sort for memory reasons, return the directory entries in their
|
|---|
| 1209 | * current order. Allocate enough space for the current needs plus
|
|---|
| 1210 | * 40 so don't realloc one entry at a time.
|
|---|
| 1211 | */
|
|---|
| 1212 | if (nitems > sp->fts_nitems) {
|
|---|
| 1213 | FTSENT **new;
|
|---|
| 1214 |
|
|---|
| 1215 | new = realloc(sp->fts_array, sizeof(FTSENT *) * (nitems + 40));
|
|---|
| 1216 | if (new == 0)
|
|---|
| 1217 | return (head);
|
|---|
| 1218 | sp->fts_array = new;
|
|---|
| 1219 | sp->fts_nitems = nitems + 40;
|
|---|
| 1220 | }
|
|---|
| 1221 | for (ap = sp->fts_array, p = head; p; p = p->fts_link)
|
|---|
| 1222 | *ap++ = p;
|
|---|
| 1223 | qsort((void *)sp->fts_array, nitems, sizeof(FTSENT *),
|
|---|
| 1224 | (int (*)(const void *, const void *))sp->fts_compar);
|
|---|
| 1225 | for (head = *(ap = sp->fts_array); --nitems; ++ap)
|
|---|
| 1226 | ap[0]->fts_link = ap[1];
|
|---|
| 1227 | ap[0]->fts_link = NULL;
|
|---|
| 1228 | return (head);
|
|---|
| 1229 | }
|
|---|
| 1230 |
|
|---|
| 1231 | static FTSENT *
|
|---|
| 1232 | fts_alloc(sp, name, namelen)
|
|---|
| 1233 | FTS *sp;
|
|---|
| 1234 | const char *name;
|
|---|
| 1235 | size_t namelen;
|
|---|
| 1236 | {
|
|---|
| 1237 | FTSENT *p;
|
|---|
| 1238 | size_t len;
|
|---|
| 1239 |
|
|---|
| 1240 | _DIAGASSERT(sp != NULL);
|
|---|
| 1241 | _DIAGASSERT(name != NULL);
|
|---|
| 1242 |
|
|---|
| 1243 | #if defined(ALIGNBYTES) && defined(ALIGN)
|
|---|
| 1244 | /*
|
|---|
| 1245 | * The file name is a variable length array and no stat structure is
|
|---|
| 1246 | * necessary if the user has set the nostat bit. Allocate the FTSENT
|
|---|
| 1247 | * structure, the file name and the stat structure in one chunk, but
|
|---|
| 1248 | * be careful that the stat structure is reasonably aligned. Since the
|
|---|
| 1249 | * fts_name field is declared to be of size 1, the fts_name pointer is
|
|---|
| 1250 | * namelen + 2 before the first possible address of the stat structure.
|
|---|
| 1251 | */
|
|---|
| 1252 | len = sizeof(FTSENT) + namelen;
|
|---|
| 1253 | if (!ISSET(FTS_NOSTAT))
|
|---|
| 1254 | len += sizeof(struct STAT) + ALIGNBYTES;
|
|---|
| 1255 | if ((p = malloc(len)) == NULL)
|
|---|
| 1256 | return (NULL);
|
|---|
| 1257 |
|
|---|
| 1258 | if (!ISSET(FTS_NOSTAT))
|
|---|
| 1259 | p->fts_statp =
|
|---|
| 1260 | (struct STAT *)ALIGN((u_long)(p->fts_name + namelen + 2));
|
|---|
| 1261 | #else
|
|---|
| 1262 | (void)len;
|
|---|
| 1263 | if ((p = malloc(sizeof(FTSENT) + namelen)) == NULL)
|
|---|
| 1264 | return (NULL);
|
|---|
| 1265 |
|
|---|
| 1266 | if (!ISSET(FTS_NOSTAT))
|
|---|
| 1267 | if ((p->fts_statp = malloc(sizeof(struct STAT))) == NULL) {
|
|---|
| 1268 | free(p);
|
|---|
| 1269 | return (NULL);
|
|---|
| 1270 | }
|
|---|
| 1271 | #endif
|
|---|
| 1272 |
|
|---|
| 1273 | /* Copy the name plus the trailing NULL. */
|
|---|
| 1274 | memmove(p->fts_name, name, namelen + 1);
|
|---|
| 1275 |
|
|---|
| 1276 | p->fts_namelen = namelen;
|
|---|
| 1277 | p->fts_path = sp->fts_path;
|
|---|
| 1278 | p->fts_errno = 0;
|
|---|
| 1279 | p->fts_flags = 0;
|
|---|
| 1280 | p->fts_instr = FTS_NOINSTR;
|
|---|
| 1281 | p->fts_number = 0;
|
|---|
| 1282 | p->fts_pointer = NULL;
|
|---|
| 1283 | return (p);
|
|---|
| 1284 | }
|
|---|
| 1285 |
|
|---|
| 1286 | static void
|
|---|
| 1287 | fts_lfree(head)
|
|---|
| 1288 | FTSENT *head;
|
|---|
| 1289 | {
|
|---|
| 1290 | FTSENT *p;
|
|---|
| 1291 |
|
|---|
| 1292 | /* XXX: head may be NULL ? */
|
|---|
| 1293 |
|
|---|
| 1294 | /* Free a linked list of structures. */
|
|---|
| 1295 | while ((p = head) != NULL) {
|
|---|
| 1296 | head = head->fts_link;
|
|---|
| 1297 |
|
|---|
| 1298 | #if !defined(ALIGNBYTES) || !defined(ALIGN)
|
|---|
| 1299 | if (p->fts_statp)
|
|---|
| 1300 | free(p->fts_statp);
|
|---|
| 1301 | #endif
|
|---|
| 1302 | free(p);
|
|---|
| 1303 | }
|
|---|
| 1304 | }
|
|---|
| 1305 |
|
|---|
| 1306 | static size_t
|
|---|
| 1307 | fts_pow2(x)
|
|---|
| 1308 | size_t x;
|
|---|
| 1309 | {
|
|---|
| 1310 |
|
|---|
| 1311 | x--;
|
|---|
| 1312 | x |= x>>1;
|
|---|
| 1313 | x |= x>>2;
|
|---|
| 1314 | x |= x>>4;
|
|---|
| 1315 | x |= x>>8;
|
|---|
| 1316 | x |= x>>16;
|
|---|
| 1317 | #if LONG_BIT > 32
|
|---|
| 1318 | x |= x>>32;
|
|---|
| 1319 | #endif
|
|---|
| 1320 | #if LONG_BIT > 64
|
|---|
| 1321 | x |= x>>64;
|
|---|
| 1322 | #endif
|
|---|
| 1323 | x++;
|
|---|
| 1324 | return (x);
|
|---|
| 1325 | }
|
|---|
| 1326 |
|
|---|
| 1327 | /*
|
|---|
| 1328 | * Allow essentially unlimited paths; find, rm, ls should all work on any tree.
|
|---|
| 1329 | * Most systems will allow creation of paths much longer than MAXPATHLEN, even
|
|---|
| 1330 | * though the kernel won't resolve them. Round up the new size to a power of 2,
|
|---|
| 1331 | * so we don't realloc the path 2 bytes at a time.
|
|---|
| 1332 | */
|
|---|
| 1333 | static int
|
|---|
| 1334 | fts_palloc(sp, size)
|
|---|
| 1335 | FTS *sp;
|
|---|
| 1336 | size_t size;
|
|---|
| 1337 | {
|
|---|
| 1338 | char *new;
|
|---|
| 1339 |
|
|---|
| 1340 | _DIAGASSERT(sp != NULL);
|
|---|
| 1341 |
|
|---|
| 1342 | #if 1
|
|---|
| 1343 | /* Protect against fts_pathlen overflow. */
|
|---|
| 1344 | if (size > USHRT_MAX + 1) {
|
|---|
| 1345 | errno = ENOMEM;
|
|---|
| 1346 | return (1);
|
|---|
| 1347 | }
|
|---|
| 1348 | #endif
|
|---|
| 1349 | size = fts_pow2(size);
|
|---|
| 1350 | new = realloc(sp->fts_path, size);
|
|---|
| 1351 | if (new == 0)
|
|---|
| 1352 | return (1);
|
|---|
| 1353 | sp->fts_path = new;
|
|---|
| 1354 | sp->fts_pathlen = size;
|
|---|
| 1355 | return (0);
|
|---|
| 1356 | }
|
|---|
| 1357 |
|
|---|
| 1358 | /*
|
|---|
| 1359 | * When the path is realloc'd, have to fix all of the pointers in structures
|
|---|
| 1360 | * already returned.
|
|---|
| 1361 | */
|
|---|
| 1362 | static void
|
|---|
| 1363 | fts_padjust(sp, head)
|
|---|
| 1364 | FTS *sp;
|
|---|
| 1365 | FTSENT *head;
|
|---|
| 1366 | {
|
|---|
| 1367 | FTSENT *p;
|
|---|
| 1368 | char *addr;
|
|---|
| 1369 |
|
|---|
| 1370 | _DIAGASSERT(sp != NULL);
|
|---|
| 1371 |
|
|---|
| 1372 | #define ADJUST(p) do { \
|
|---|
| 1373 | if ((p)->fts_accpath != (p)->fts_name) \
|
|---|
| 1374 | (p)->fts_accpath = \
|
|---|
| 1375 | addr + ((p)->fts_accpath - (p)->fts_path); \
|
|---|
| 1376 | (p)->fts_path = addr; \
|
|---|
| 1377 | } while (/*CONSTCOND*/0)
|
|---|
| 1378 |
|
|---|
| 1379 | addr = sp->fts_path;
|
|---|
| 1380 |
|
|---|
| 1381 | /* Adjust the current set of children. */
|
|---|
| 1382 | for (p = sp->fts_child; p; p = p->fts_link)
|
|---|
| 1383 | ADJUST(p);
|
|---|
| 1384 |
|
|---|
| 1385 | /* Adjust the rest of the tree, including the current level. */
|
|---|
| 1386 | for (p = head; p->fts_level >= FTS_ROOTLEVEL;) {
|
|---|
| 1387 | ADJUST(p);
|
|---|
| 1388 | p = p->fts_link ? p->fts_link : p->fts_parent;
|
|---|
| 1389 | }
|
|---|
| 1390 | }
|
|---|
| 1391 |
|
|---|
| 1392 | static size_t
|
|---|
| 1393 | fts_maxarglen(argv)
|
|---|
| 1394 | char * const *argv;
|
|---|
| 1395 | {
|
|---|
| 1396 | size_t len, max;
|
|---|
| 1397 |
|
|---|
| 1398 | _DIAGASSERT(argv != NULL);
|
|---|
| 1399 |
|
|---|
| 1400 | for (max = 0; *argv; ++argv)
|
|---|
| 1401 | if ((len = strlen(*argv)) > max)
|
|---|
| 1402 | max = len;
|
|---|
| 1403 | return (max + 1);
|
|---|
| 1404 | }
|
|---|
| 1405 |
|
|---|
| 1406 | /*
|
|---|
| 1407 | * Change to dir specified by fd or p->fts_accpath without getting
|
|---|
| 1408 | * tricked by someone changing the world out from underneath us.
|
|---|
| 1409 | * Assumes p->fts_dev and p->fts_ino are filled in.
|
|---|
| 1410 | */
|
|---|
| 1411 | static int
|
|---|
| 1412 | fts_safe_changedir(sp, p, fd, path)
|
|---|
| 1413 | const FTS *sp;
|
|---|
| 1414 | const FTSENT *p;
|
|---|
| 1415 | int fd;
|
|---|
| 1416 | const char *path;
|
|---|
| 1417 | {
|
|---|
| 1418 | int oldfd = fd, ret = -1;
|
|---|
| 1419 | struct STAT sb;
|
|---|
| 1420 |
|
|---|
| 1421 | if (ISSET(FTS_NOCHDIR))
|
|---|
| 1422 | return 0;
|
|---|
| 1423 |
|
|---|
| 1424 | #ifdef HAVE_FCHDIR
|
|---|
| 1425 | if (oldfd < 0 && (fd = open(path, O_RDONLY)) == -1)
|
|---|
| 1426 | return -1;
|
|---|
| 1427 |
|
|---|
| 1428 | if (fstat(fd, &sb) == -1)
|
|---|
| 1429 | goto bail;
|
|---|
| 1430 | #else
|
|---|
| 1431 | if (stat(path, &sb))
|
|---|
| 1432 | goto bail;
|
|---|
| 1433 | #endif
|
|---|
| 1434 |
|
|---|
| 1435 | if (sb.st_ino != p->fts_ino || sb.st_dev != p->fts_dev) {
|
|---|
| 1436 | errno = ENOENT;
|
|---|
| 1437 | goto bail;
|
|---|
| 1438 | }
|
|---|
| 1439 |
|
|---|
| 1440 | #ifdef HAVE_FCHDIR
|
|---|
| 1441 | ret = fchdir(fd);
|
|---|
| 1442 | #else
|
|---|
| 1443 | ret = chdir(path);
|
|---|
| 1444 | #endif
|
|---|
| 1445 |
|
|---|
| 1446 | bail:
|
|---|
| 1447 | #ifdef HAVE_FCHDIR
|
|---|
| 1448 | if (oldfd < 0) {
|
|---|
| 1449 | int save_errno = errno;
|
|---|
| 1450 | (void)close(fd);
|
|---|
| 1451 | errno = save_errno;
|
|---|
| 1452 | }
|
|---|
| 1453 | #endif
|
|---|
| 1454 | return ret;
|
|---|
| 1455 | }
|
|---|