[2989] | 1 | /* $Id: fts-nt.c 3535 2021-12-20 23:32:28Z bird $ */
|
---|
[2985] | 2 | /** @file
|
---|
| 3 | * Source for the NT port of BSD fts.c.
|
---|
| 4 | *
|
---|
| 5 | * @copyright 1990, 1993, 1994 The Regents of the University of California. All rights reserved.
|
---|
| 6 | * @copyright NT modifications Copyright (C) 2016 knut st. osmundsen <bird-klibc-spam-xiv@anduin.net>
|
---|
| 7 | * @licenses BSD3
|
---|
| 8 | *
|
---|
| 9 | *
|
---|
| 10 | * Some hints about how the code works.
|
---|
| 11 | *
|
---|
| 12 | * The input directories & files are entered into a pseudo root directory and
|
---|
| 13 | * processed one after another, depth first.
|
---|
| 14 | *
|
---|
| 15 | * Directories are completely read into memory first and arranged as linked
|
---|
| 16 | * list anchored on FTS::fts_cur. fts_read does a pop-like operation on that
|
---|
| 17 | * list, freeing the nodes after they've been completely processed.
|
---|
| 18 | * Subdirectories are returned twice by fts_read, the first time when it
|
---|
| 19 | * decends into it (FTS_D), and the second time as it ascends from it (FTS_DP).
|
---|
| 20 | *
|
---|
| 21 | * In parallel to fts_read, there's the fts_children API that fetches the
|
---|
| 22 | * directory content in a similar manner, but for the consumption of the API
|
---|
| 23 | * caller rather than FTS itself. The result hangs on FTS::fts_child so it can
|
---|
| 24 | * be freed when the directory changes or used by fts_read when it is called
|
---|
| 25 | * upon to enumerate the directory.
|
---|
| 26 | *
|
---|
| 27 | *
|
---|
| 28 | * The NT port of the code does away with the directory changing in favor of
|
---|
| 29 | * using directory relative opens (present in NT since for ever, just not
|
---|
| 30 | * exposed thru Win32). A new FTSENT member fts_dirfd has been added to make
|
---|
| 31 | * this possible for API users too.
|
---|
| 32 | *
|
---|
| 33 | * Note! When using Win32 APIs with path input relative to the current
|
---|
| 34 | * directory, the internal DOS <-> NT path converter will expand it to a
|
---|
| 35 | * full path and subject it to the 260 char limit.
|
---|
| 36 | *
|
---|
| 37 | * The richer NT directory enumeration API allows us to do away with all the
|
---|
| 38 | * stat() calls, and not have to do link counting and other interesting things
|
---|
| 39 | * to try speed things up. (You typical stat() implementation on windows is
|
---|
| 40 | * actually a directory enum call with the name of the file as filter.)
|
---|
| 41 | */
|
---|
| 42 |
|
---|
[2982] | 43 | /*-
|
---|
| 44 | * Copyright (c) 1990, 1993, 1994
|
---|
| 45 | * The Regents of the University of California. All rights reserved.
|
---|
| 46 | *
|
---|
| 47 | * Redistribution and use in source and binary forms, with or without
|
---|
| 48 | * modification, are permitted provided that the following conditions
|
---|
| 49 | * are met:
|
---|
| 50 | * 1. Redistributions of source code must retain the above copyright
|
---|
| 51 | * notice, this list of conditions and the following disclaimer.
|
---|
| 52 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
| 53 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 54 | * documentation and/or other materials provided with the distribution.
|
---|
| 55 | * 4. Neither the name of the University nor the names of its contributors
|
---|
| 56 | * may be used to endorse or promote products derived from this software
|
---|
| 57 | * without specific prior written permission.
|
---|
| 58 | *
|
---|
| 59 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
---|
| 60 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
| 61 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
| 62 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
---|
| 63 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
---|
| 64 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
---|
| 65 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
| 66 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
---|
| 67 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
---|
| 68 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
| 69 | * SUCH DAMAGE.
|
---|
| 70 | *
|
---|
| 71 | * $OpenBSD: fts.c,v 1.22 1999/10/03 19:22:22 millert Exp $
|
---|
| 72 | */
|
---|
| 73 |
|
---|
| 74 | #if 0
|
---|
| 75 | #if defined(LIBC_SCCS) && !defined(lint)
|
---|
| 76 | static char sccsid[] = "@(#)fts.c 8.6 (Berkeley) 8/14/94";
|
---|
| 77 | #endif /* LIBC_SCCS and not lint */
|
---|
| 78 | #endif
|
---|
| 79 |
|
---|
| 80 | #include <errno.h>
|
---|
[2985] | 81 | #include "fts-nt.h"
|
---|
[2982] | 82 | #include <stdlib.h>
|
---|
| 83 | #include <string.h>
|
---|
[2985] | 84 | #include <assert.h>
|
---|
| 85 | #include "nthlp.h"
|
---|
| 86 | #include "ntdir.h"
|
---|
[3535] | 87 | #include "ntopenat.h" /* for AT_FDCWD */
|
---|
[3001] | 88 | #include <stdio.h>//debug
|
---|
[2982] | 89 |
|
---|
[2998] | 90 | static FTSENT *fts_alloc(FTS *sp, char const *name, size_t namelen, wchar_t const *wcsname, size_t cwcname);
|
---|
| 91 | static FTSENT *fts_alloc_ansi(FTS *sp, char const *name, size_t namelen);
|
---|
| 92 | static FTSENT *fts_alloc_utf16(FTS *sp, wchar_t const *wcsname, size_t cwcname);
|
---|
[3004] | 93 | static void nt_fts_free_alloc_cache(FTS *sp);
|
---|
[2982] | 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 *);
|
---|
[2998] | 98 | static size_t fts_maxarglenw(wchar_t * const *);
|
---|
[2982] | 99 | static void fts_padjust(FTS *, FTSENT *);
|
---|
[2998] | 100 | static void fts_padjustw(FTS *, FTSENT *);
|
---|
| 101 | static int fts_palloc(FTS *, size_t, size_t);
|
---|
[2982] | 102 | static FTSENT *fts_sort(FTS *, FTSENT *, size_t);
|
---|
[2985] | 103 | static int fts_stat(FTS *, FTSENT *, int, HANDLE);
|
---|
[2992] | 104 | static int fts_process_stats(FTSENT *, BirdStat_T const *);
|
---|
[2982] | 105 |
|
---|
| 106 | #define ISDOT(a) (a[0] == '.' && (!a[1] || (a[1] == '.' && !a[2])))
|
---|
| 107 |
|
---|
| 108 | #define CLR(opt) (sp->fts_options &= ~(opt))
|
---|
| 109 | #define ISSET(opt) (sp->fts_options & (opt))
|
---|
| 110 | #define SET(opt) (sp->fts_options |= (opt))
|
---|
| 111 |
|
---|
| 112 | /* fts_build flags */
|
---|
| 113 | #define BCHILD 1 /* fts_children */
|
---|
| 114 | #define BNAMES 2 /* fts_children, names only */
|
---|
| 115 | #define BREAD 3 /* fts_read */
|
---|
| 116 |
|
---|
[2985] | 117 | /* NT needs these: */
|
---|
| 118 | #define MAXPATHLEN 260
|
---|
| 119 | #define MAX(a, b) ( (a) >= (b) ? (a) : (b) )
|
---|
| 120 |
|
---|
[3005] | 121 | /** Enables BirdDir_T reuse. (Saves malloc and free calls.) */
|
---|
| 122 | #define FTS_WITH_DIRHANDLE_REUSE
|
---|
| 123 | /** Enables allocation statistics. */
|
---|
| 124 | //#define FTS_WITH_STATISTICS
|
---|
| 125 | /** Enables FTSENT allocation cache. */
|
---|
[3004] | 126 | #define FTS_WITH_ALLOC_CACHE
|
---|
| 127 | /** Number of size buckets for the FTSENT allocation cache. */
|
---|
[3005] | 128 | #define FTS_NUM_FREE_BUCKETS 64
|
---|
[3004] | 129 | /** Shift for converting size to free bucket index. */
|
---|
| 130 | #define FTS_FREE_BUCKET_SHIFT 4
|
---|
| 131 | /** The FTSENT allocation alignment. */
|
---|
[3005] | 132 | #define FTS_ALIGN_FTSENT (1U << FTS_FREE_BUCKET_SHIFT)
|
---|
[2985] | 133 |
|
---|
[2982] | 134 | /*
|
---|
| 135 | * Internal representation of an FTS, including extra implementation
|
---|
| 136 | * details. The FTS returned from fts_open points to this structure's
|
---|
| 137 | * ftsp_fts member (and can be cast to an _fts_private as required)
|
---|
| 138 | */
|
---|
| 139 | struct _fts_private {
|
---|
| 140 | FTS ftsp_fts;
|
---|
[3005] | 141 | #ifdef FTS_WITH_DIRHANDLE_REUSE
|
---|
| 142 | /** Statically allocate directory handle. */
|
---|
| 143 | BirdDir_T dirhandle;
|
---|
| 144 | #endif
|
---|
[3004] | 145 | #ifdef FTS_WITH_ALLOC_CACHE
|
---|
| 146 | /** Number of free entries in the above buckets. */
|
---|
| 147 | size_t numfree;
|
---|
| 148 | # ifdef FTS_WITH_STATISTICS
|
---|
| 149 | size_t allocs;
|
---|
| 150 | size_t hits;
|
---|
| 151 | size_t misses;
|
---|
| 152 | # endif
|
---|
| 153 | /** Free FTSENT buckets (by size).
|
---|
| 154 | * This is to avoid hitting the heap, which is a little sluggish on windows. */
|
---|
| 155 | struct
|
---|
| 156 | {
|
---|
| 157 | FTSENT *head;
|
---|
| 158 | } freebuckets[FTS_NUM_FREE_BUCKETS];
|
---|
| 159 | #endif
|
---|
[2982] | 160 | };
|
---|
| 161 |
|
---|
| 162 |
|
---|
[2998] | 163 | static FTS * FTSCALL
|
---|
| 164 | nt_fts_open_common(char * const *argv, wchar_t * const *wcsargv, int options,
|
---|
[2982] | 165 | int (*compar)(const FTSENT * const *, const FTSENT * const *))
|
---|
| 166 | {
|
---|
| 167 | struct _fts_private *priv;
|
---|
| 168 | FTS *sp;
|
---|
| 169 | FTSENT *p, *root;
|
---|
| 170 | FTSENT *parent, *tmp;
|
---|
| 171 | size_t len, nitems;
|
---|
| 172 |
|
---|
[2999] | 173 | birdResolveImports();
|
---|
[2998] | 174 |
|
---|
[2982] | 175 | /* Options check. */
|
---|
| 176 | if (options & ~FTS_OPTIONMASK) {
|
---|
| 177 | errno = EINVAL;
|
---|
| 178 | return (NULL);
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | /* fts_open() requires at least one path */
|
---|
[3009] | 182 | if (wcsargv ? *wcsargv == NULL : *argv == NULL) {
|
---|
[2982] | 183 | errno = EINVAL;
|
---|
| 184 | return (NULL);
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | /* Allocate/initialize the stream. */
|
---|
| 188 | if ((priv = calloc(1, sizeof(*priv))) == NULL)
|
---|
| 189 | return (NULL);
|
---|
| 190 | sp = &priv->ftsp_fts;
|
---|
| 191 | sp->fts_compar = compar;
|
---|
| 192 | sp->fts_options = options;
|
---|
[2988] | 193 | SET(FTS_NOCHDIR); /* NT: FTS_NOCHDIR is always on (for external consumes) */
|
---|
[3535] | 194 | sp->fts_cwd_fd = AT_FDCWD;
|
---|
[2982] | 195 |
|
---|
| 196 | /* Shush, GCC. */
|
---|
| 197 | tmp = NULL;
|
---|
| 198 |
|
---|
| 199 | /*
|
---|
| 200 | * Start out with 1K of path space, and enough, in any case,
|
---|
| 201 | * to hold the user's paths.
|
---|
| 202 | */
|
---|
[2998] | 203 | if (fts_palloc(sp, MAX(argv ? fts_maxarglen(argv) : 1, MAXPATHLEN),
|
---|
| 204 | MAX(wcsargv ? fts_maxarglenw(wcsargv) : 1, MAXPATHLEN)) )
|
---|
[2982] | 205 | goto mem1;
|
---|
| 206 |
|
---|
| 207 | /* Allocate/initialize root's parent. */
|
---|
[2998] | 208 | if ((parent = fts_alloc(sp, NULL, 0, NULL, 0)) == NULL)
|
---|
[2982] | 209 | goto mem2;
|
---|
| 210 | parent->fts_level = FTS_ROOTPARENTLEVEL;
|
---|
| 211 |
|
---|
| 212 | /* Allocate/initialize root(s). */
|
---|
[3009] | 213 | for (root = NULL, nitems = 0; wcsargv ? *wcsargv != NULL : *argv != NULL; ++nitems) {
|
---|
[2985] | 214 | /* NT: We need to do some small input transformations to make this and
|
---|
| 215 | the API user code happy. 1. Lone drive letters get a dot
|
---|
| 216 | appended so it won't matter if a slash is appended afterwards.
|
---|
[2990] | 217 | 2. DOS slashes are converted to UNIX ones. */
|
---|
[2998] | 218 | wchar_t *wcslash;
|
---|
| 219 |
|
---|
| 220 | if (wcsargv) {
|
---|
| 221 | len = wcslen(*wcsargv);
|
---|
| 222 | if (len == 2 && wcsargv[0][1] == ':') {
|
---|
| 223 | wchar_t wcsdrive[4];
|
---|
| 224 | wcsdrive[0] = wcsargv[0][0];
|
---|
| 225 | wcsdrive[1] = ':';
|
---|
| 226 | wcsdrive[2] = '.';
|
---|
| 227 | wcsdrive[3] = '\0';
|
---|
| 228 | p = fts_alloc_utf16(sp, wcsdrive, 3);
|
---|
| 229 | } else {
|
---|
| 230 | p = fts_alloc_utf16(sp, *wcsargv, len);
|
---|
| 231 | }
|
---|
[3009] | 232 | wcsargv++;
|
---|
[2985] | 233 | } else {
|
---|
[2998] | 234 | len = strlen(*argv);
|
---|
| 235 | if (len == 2 && argv[0][1] == ':') {
|
---|
| 236 | char szdrive[4];
|
---|
| 237 | szdrive[0] = argv[0][0];
|
---|
| 238 | szdrive[1] = ':';
|
---|
| 239 | szdrive[2] = '.';
|
---|
| 240 | szdrive[3] = '\0';
|
---|
| 241 | p = fts_alloc_ansi(sp, szdrive, 3);
|
---|
| 242 | } else {
|
---|
| 243 | p = fts_alloc_ansi(sp, *argv, len);
|
---|
| 244 | }
|
---|
[3009] | 245 | argv++;
|
---|
[2985] | 246 | }
|
---|
| 247 | if (p != NULL) { /* likely */ } else { goto mem3; }
|
---|
[2998] | 248 |
|
---|
| 249 | wcslash = wcschr(p->fts_wcsname, '\\');
|
---|
| 250 | while (wcslash != NULL) {
|
---|
| 251 | *wcslash++ = '/';
|
---|
| 252 | wcslash = wcschr(p->fts_wcsname, '\\');
|
---|
[2985] | 253 | }
|
---|
[2998] | 254 |
|
---|
| 255 | if (p->fts_name) {
|
---|
| 256 | char *slash = strchr(p->fts_name, '\\');
|
---|
| 257 | while (slash != NULL) {
|
---|
| 258 | *slash++ = '/';
|
---|
| 259 | slash = strchr(p->fts_name, '\\');
|
---|
| 260 | }
|
---|
| 261 | }
|
---|
| 262 |
|
---|
[2982] | 263 | p->fts_level = FTS_ROOTLEVEL;
|
---|
| 264 | p->fts_parent = parent;
|
---|
| 265 | p->fts_accpath = p->fts_name;
|
---|
[2998] | 266 | p->fts_wcsaccpath = p->fts_wcsname;
|
---|
[2985] | 267 | p->fts_info = fts_stat(sp, p, ISSET(FTS_COMFOLLOW), INVALID_HANDLE_VALUE);
|
---|
[2982] | 268 |
|
---|
| 269 | /* Command-line "." and ".." are real directories. */
|
---|
| 270 | if (p->fts_info == FTS_DOT)
|
---|
| 271 | p->fts_info = FTS_D;
|
---|
| 272 |
|
---|
| 273 | /*
|
---|
| 274 | * If comparison routine supplied, traverse in sorted
|
---|
| 275 | * order; otherwise traverse in the order specified.
|
---|
| 276 | */
|
---|
| 277 | if (compar) {
|
---|
| 278 | p->fts_link = root;
|
---|
| 279 | root = p;
|
---|
| 280 | } else {
|
---|
| 281 | p->fts_link = NULL;
|
---|
| 282 | if (root == NULL)
|
---|
| 283 | tmp = root = p;
|
---|
| 284 | else {
|
---|
| 285 | tmp->fts_link = p;
|
---|
| 286 | tmp = p;
|
---|
| 287 | }
|
---|
| 288 | }
|
---|
| 289 | }
|
---|
| 290 | if (compar && nitems > 1)
|
---|
| 291 | root = fts_sort(sp, root, nitems);
|
---|
| 292 |
|
---|
| 293 | /*
|
---|
| 294 | * Allocate a dummy pointer and make fts_read think that we've just
|
---|
| 295 | * finished the node before the root(s); set p->fts_info to FTS_INIT
|
---|
| 296 | * so that everything about the "current" node is ignored.
|
---|
| 297 | */
|
---|
[2998] | 298 | if ((sp->fts_cur = fts_alloc(sp, NULL, 0, NULL, 0)) == NULL)
|
---|
[2982] | 299 | goto mem3;
|
---|
| 300 | sp->fts_cur->fts_link = root;
|
---|
| 301 | sp->fts_cur->fts_info = FTS_INIT;
|
---|
| 302 |
|
---|
| 303 | return (sp);
|
---|
| 304 |
|
---|
[2998] | 305 | mem3:
|
---|
| 306 | fts_lfree(root);
|
---|
[2982] | 307 | free(parent);
|
---|
[2998] | 308 | mem2:
|
---|
| 309 | free(sp->fts_path);
|
---|
| 310 | free(sp->fts_wcspath);
|
---|
| 311 | mem1:
|
---|
| 312 | free(sp);
|
---|
[2982] | 313 | return (NULL);
|
---|
| 314 | }
|
---|
| 315 |
|
---|
[2985] | 316 |
|
---|
[2998] | 317 | FTS * FTSCALL
|
---|
| 318 | nt_fts_open(char * const *argv, int options,
|
---|
| 319 | int (*compar)(const FTSENT * const *, const FTSENT * const *))
|
---|
| 320 | {
|
---|
| 321 | return nt_fts_open_common(argv, NULL, options, compar);
|
---|
| 322 | }
|
---|
| 323 |
|
---|
| 324 |
|
---|
| 325 | FTS * FTSCALL
|
---|
| 326 | nt_fts_openw(wchar_t * const *argv, int options,
|
---|
| 327 | int (*compar)(const FTSENT * const *, const FTSENT * const *))
|
---|
| 328 | {
|
---|
| 329 | return nt_fts_open_common(NULL, argv, options, compar);
|
---|
| 330 | }
|
---|
| 331 |
|
---|
| 332 |
|
---|
| 333 | /**
|
---|
| 334 | * Called by fts_read for FTS_ROOTLEVEL entries only.
|
---|
| 335 | */
|
---|
[2982] | 336 | static void
|
---|
| 337 | fts_load(FTS *sp, FTSENT *p)
|
---|
| 338 | {
|
---|
| 339 | size_t len;
|
---|
[2998] | 340 | wchar_t *pwc;
|
---|
[2982] | 341 |
|
---|
| 342 | /*
|
---|
| 343 | * Load the stream structure for the next traversal. Since we don't
|
---|
| 344 | * actually enter the directory until after the preorder visit, set
|
---|
| 345 | * the fts_accpath field specially so the chdir gets done to the right
|
---|
| 346 | * place and the user can access the first node. From fts_open it's
|
---|
| 347 | * known that the path will fit.
|
---|
| 348 | */
|
---|
[3001] | 349 | if (!(sp->fts_options & FTS_NO_ANSI)) {
|
---|
[2998] | 350 | char *cp;
|
---|
| 351 | len = p->fts_pathlen = p->fts_namelen;
|
---|
| 352 | memmove(sp->fts_path, p->fts_name, len + 1);
|
---|
| 353 | cp = strrchr(p->fts_name, '/');
|
---|
| 354 | if (cp != NULL && (cp != p->fts_name || cp[1])) {
|
---|
| 355 | len = strlen(++cp);
|
---|
| 356 | memmove(p->fts_name, cp, len + 1);
|
---|
| 357 | p->fts_namelen = len;
|
---|
| 358 | }
|
---|
| 359 | p->fts_accpath = p->fts_path = sp->fts_path;
|
---|
[2982] | 360 | }
|
---|
[2998] | 361 |
|
---|
| 362 | len = p->fts_cwcpath = p->fts_cwcname;
|
---|
| 363 | memmove(sp->fts_wcspath, p->fts_wcsname, (len + 1) * sizeof(wchar_t));
|
---|
| 364 | pwc = wcsrchr(p->fts_wcsname, '/');
|
---|
| 365 | if (pwc != NULL && (pwc != p->fts_wcsname || pwc[1])) {
|
---|
| 366 | len = wcslen(++pwc);
|
---|
| 367 | memmove(p->fts_wcsname, pwc, (len + 1) * sizeof(wchar_t));
|
---|
| 368 | p->fts_cwcname = len;
|
---|
| 369 | }
|
---|
| 370 | p->fts_wcsaccpath = p->fts_wcspath = sp->fts_wcspath;
|
---|
| 371 |
|
---|
[2982] | 372 | sp->fts_dev = p->fts_dev;
|
---|
| 373 | }
|
---|
| 374 |
|
---|
[3004] | 375 |
|
---|
[2985] | 376 | int FTSCALL
|
---|
[2992] | 377 | nt_fts_close(FTS *sp)
|
---|
[2982] | 378 | {
|
---|
| 379 | FTSENT *freep, *p;
|
---|
[2985] | 380 | /*int saved_errno;*/
|
---|
[2982] | 381 |
|
---|
| 382 | /*
|
---|
| 383 | * This still works if we haven't read anything -- the dummy structure
|
---|
| 384 | * points to the root list, so we step through to the end of the root
|
---|
| 385 | * list which has a valid parent pointer.
|
---|
| 386 | */
|
---|
| 387 | if (sp->fts_cur) {
|
---|
| 388 | for (p = sp->fts_cur; p->fts_level >= FTS_ROOTLEVEL;) {
|
---|
| 389 | freep = p;
|
---|
| 390 | p = p->fts_link != NULL ? p->fts_link : p->fts_parent;
|
---|
| 391 | free(freep);
|
---|
| 392 | }
|
---|
| 393 | free(p);
|
---|
| 394 | }
|
---|
| 395 |
|
---|
| 396 | /* Free up child linked list, sort array, path buffer. */
|
---|
| 397 | if (sp->fts_child)
|
---|
| 398 | fts_lfree(sp->fts_child);
|
---|
| 399 | if (sp->fts_array)
|
---|
| 400 | free(sp->fts_array);
|
---|
| 401 | free(sp->fts_path);
|
---|
[2998] | 402 | free(sp->fts_wcspath);
|
---|
[3004] | 403 | #ifdef FTS_WITH_ALLOC_CACHE
|
---|
| 404 | # ifdef FTS_WITH_STATISTICS
|
---|
| 405 | {
|
---|
| 406 | struct _fts_private *priv = (struct _fts_private *)sp;
|
---|
| 407 | fprintf(stderr, "numfree=%u allocs=%u hits=%u (%uppt) misses=%u (%uppt) other=%u\n",
|
---|
| 408 | priv->numfree, priv->allocs,
|
---|
| 409 | priv->hits, (unsigned)((double)priv->hits * 1000.0 / priv->allocs),
|
---|
| 410 | priv->misses, (unsigned)((double)priv->misses * 1000.0 / priv->allocs),
|
---|
| 411 | priv->allocs - priv->misses - priv->hits);
|
---|
| 412 | }
|
---|
| 413 | # endif
|
---|
| 414 | #endif
|
---|
| 415 | nt_fts_free_alloc_cache(sp);
|
---|
[3005] | 416 | #ifdef FTS_WITH_DIRHANDLE_REUSE
|
---|
| 417 | birdDirClose(&((struct _fts_private *)sp)->dirhandle);
|
---|
| 418 | #endif
|
---|
[2982] | 419 |
|
---|
| 420 | /* Free up the stream pointer. */
|
---|
| 421 | free(sp);
|
---|
| 422 | return (0);
|
---|
| 423 | }
|
---|
| 424 |
|
---|
[3004] | 425 |
|
---|
| 426 | /**
|
---|
| 427 | * Frees a FTSENT structure by way of the allocation cache.
|
---|
[2982] | 428 | */
|
---|
[2985] | 429 | static void
|
---|
[3004] | 430 | fts_free_entry(FTS *sp, FTSENT *tmp)
|
---|
[2985] | 431 | {
|
---|
[3004] | 432 | if (tmp != NULL) {
|
---|
| 433 | struct _fts_private *priv = (struct _fts_private *)sp;
|
---|
| 434 | #ifdef FTS_WITH_ALLOC_CACHE
|
---|
| 435 | size_t idx;
|
---|
| 436 | #endif
|
---|
| 437 |
|
---|
| 438 | if (tmp->fts_dirfd == INVALID_HANDLE_VALUE) {
|
---|
| 439 | /* There are probably more files than directories out there. */
|
---|
| 440 | } else {
|
---|
[2985] | 441 | birdCloseFile(tmp->fts_dirfd);
|
---|
| 442 | tmp->fts_dirfd = INVALID_HANDLE_VALUE;
|
---|
| 443 | }
|
---|
[3004] | 444 |
|
---|
| 445 | #ifdef FTS_WITH_ALLOC_CACHE
|
---|
| 446 | idx = (tmp->fts_alloc_size - sizeof(FTSENT)) >> FTS_FREE_BUCKET_SHIFT;
|
---|
| 447 | if (idx < FTS_NUM_FREE_BUCKETS) {
|
---|
| 448 | tmp->fts_link = priv->freebuckets[idx].head;
|
---|
| 449 | priv->freebuckets[idx].head = tmp;
|
---|
| 450 | } else {
|
---|
| 451 | tmp->fts_link = priv->freebuckets[FTS_NUM_FREE_BUCKETS - 1].head;
|
---|
| 452 | priv->freebuckets[FTS_NUM_FREE_BUCKETS - 1].head = tmp;
|
---|
| 453 | }
|
---|
| 454 |
|
---|
| 455 | priv->numfree++;
|
---|
| 456 | #else
|
---|
[2985] | 457 | free(tmp);
|
---|
[3004] | 458 | #endif
|
---|
| 459 | }
|
---|
[2985] | 460 | }
|
---|
| 461 |
|
---|
[3004] | 462 |
|
---|
| 463 | /*
|
---|
| 464 | * Special case of "/" at the end of the path so that slashes aren't
|
---|
| 465 | * appended which would cause paths to be written as "....//foo".
|
---|
| 466 | */
|
---|
| 467 | #define NAPPEND(p) ( p->fts_pathlen - (p->fts_path[p->fts_pathlen - 1] == '/') )
|
---|
| 468 | #define NAPPENDW(p) ( p->fts_cwcpath - (p->fts_wcspath[p->fts_cwcpath - 1] == L'/') )
|
---|
| 469 |
|
---|
[2985] | 470 | FTSENT * FTSCALL
|
---|
[2992] | 471 | nt_fts_read(FTS *sp)
|
---|
[2982] | 472 | {
|
---|
| 473 | FTSENT *p, *tmp;
|
---|
| 474 | int instr;
|
---|
[2998] | 475 | wchar_t *pwc;
|
---|
[2982] | 476 |
|
---|
[3005] | 477 | /* Set current node pointer. */
|
---|
| 478 | p = sp->fts_cur;
|
---|
| 479 |
|
---|
[2982] | 480 | /* If finished or unrecoverable error, return NULL. */
|
---|
[3005] | 481 | if (p != NULL && !ISSET(FTS_STOP)) {
|
---|
| 482 | /* likely */
|
---|
| 483 | } else {
|
---|
[2982] | 484 | return (NULL);
|
---|
[3005] | 485 | }
|
---|
[2982] | 486 |
|
---|
| 487 | /* Save and zero out user instructions. */
|
---|
| 488 | instr = p->fts_instr;
|
---|
| 489 | p->fts_instr = FTS_NOINSTR;
|
---|
| 490 |
|
---|
| 491 | /* Any type of file may be re-visited; re-stat and re-turn. */
|
---|
[3005] | 492 | if (instr != FTS_AGAIN) {
|
---|
| 493 | /* likely */
|
---|
| 494 | } else {
|
---|
[2985] | 495 | p->fts_info = fts_stat(sp, p, 0, INVALID_HANDLE_VALUE);
|
---|
[2982] | 496 | return (p);
|
---|
| 497 | }
|
---|
| 498 |
|
---|
| 499 | /*
|
---|
| 500 | * Following a symlink -- SLNONE test allows application to see
|
---|
| 501 | * SLNONE and recover. If indirecting through a symlink, have
|
---|
| 502 | * keep a pointer to current location. If unable to get that
|
---|
| 503 | * pointer, follow fails.
|
---|
[2985] | 504 | *
|
---|
[3004] | 505 | * NT: Since we don't change directory, we just set FTS_SYMFOLLOW
|
---|
| 506 | * here in case a API client checks it.
|
---|
[2982] | 507 | */
|
---|
[3005] | 508 | if ( instr != FTS_FOLLOW
|
---|
| 509 | || (p->fts_info != FTS_SL && p->fts_info != FTS_SLNONE)) {
|
---|
| 510 | /* likely */
|
---|
| 511 | } else {
|
---|
[2985] | 512 | p->fts_info = fts_stat(sp, p, 1, INVALID_HANDLE_VALUE);
|
---|
| 513 | if (p->fts_info == FTS_D /*&& !ISSET(FTS_NOCHDIR)*/) {
|
---|
| 514 | p->fts_flags |= FTS_SYMFOLLOW;
|
---|
[2982] | 515 | }
|
---|
| 516 | return (p);
|
---|
| 517 | }
|
---|
| 518 |
|
---|
| 519 | /* Directory in pre-order. */
|
---|
| 520 | if (p->fts_info == FTS_D) {
|
---|
| 521 | /* If skipped or crossed mount point, do post-order visit. */
|
---|
[3005] | 522 | if ( instr == FTS_SKIP
|
---|
| 523 | || (ISSET(FTS_XDEV) && p->fts_dev != sp->fts_dev)) {
|
---|
[2982] | 524 | if (sp->fts_child) {
|
---|
| 525 | fts_lfree(sp->fts_child);
|
---|
| 526 | sp->fts_child = NULL;
|
---|
| 527 | }
|
---|
| 528 | p->fts_info = FTS_DP;
|
---|
| 529 | return (p);
|
---|
| 530 | }
|
---|
| 531 |
|
---|
| 532 | /* Rebuild if only read the names and now traversing. */
|
---|
| 533 | if (sp->fts_child != NULL && ISSET(FTS_NAMEONLY)) {
|
---|
| 534 | CLR(FTS_NAMEONLY);
|
---|
| 535 | fts_lfree(sp->fts_child);
|
---|
| 536 | sp->fts_child = NULL;
|
---|
| 537 | }
|
---|
| 538 |
|
---|
| 539 | /*
|
---|
| 540 | * Cd to the subdirectory.
|
---|
| 541 | *
|
---|
| 542 | * If have already read and now fail to chdir, whack the list
|
---|
| 543 | * to make the names come out right, and set the parent errno
|
---|
| 544 | * so the application will eventually get an error condition.
|
---|
| 545 | * Set the FTS_DONTCHDIR flag so that when we logically change
|
---|
| 546 | * directories back to the parent we don't do a chdir.
|
---|
| 547 | *
|
---|
| 548 | * If haven't read do so. If the read fails, fts_build sets
|
---|
| 549 | * FTS_STOP or the fts_info field of the node.
|
---|
| 550 | */
|
---|
[3005] | 551 | if (sp->fts_child == NULL) {
|
---|
| 552 | p = fts_build(sp, BREAD);
|
---|
| 553 | if (p != NULL) {
|
---|
| 554 | /* likely */
|
---|
| 555 | } else {
|
---|
| 556 | if (ISSET(FTS_STOP))
|
---|
| 557 | return (NULL);
|
---|
| 558 | return sp->fts_cur;
|
---|
| 559 | }
|
---|
| 560 |
|
---|
| 561 | } else {
|
---|
| 562 | p = sp->fts_child;
|
---|
| 563 | sp->fts_child = NULL;
|
---|
[2982] | 564 | }
|
---|
| 565 | goto name;
|
---|
| 566 | }
|
---|
| 567 |
|
---|
| 568 | /* Move to the next node on this level. */
|
---|
| 569 | next: tmp = p;
|
---|
| 570 | if ((p = p->fts_link) != NULL) {
|
---|
| 571 | /*
|
---|
| 572 | * If reached the top, return to the original directory (or
|
---|
| 573 | * the root of the tree), and load the paths for the next root.
|
---|
| 574 | */
|
---|
[3005] | 575 | if (p->fts_level != FTS_ROOTLEVEL) {
|
---|
| 576 | /* likely */
|
---|
| 577 | } else {
|
---|
[3004] | 578 | fts_free_entry(sp, tmp);
|
---|
[2982] | 579 | fts_load(sp, p);
|
---|
| 580 | return (sp->fts_cur = p);
|
---|
| 581 | }
|
---|
| 582 |
|
---|
| 583 | /*
|
---|
| 584 | * User may have called fts_set on the node. If skipped,
|
---|
| 585 | * ignore. If followed, get a file descriptor so we can
|
---|
| 586 | * get back if necessary.
|
---|
| 587 | */
|
---|
[3005] | 588 | if (p->fts_instr != FTS_SKIP) {
|
---|
| 589 | /* likely */
|
---|
| 590 | } else {
|
---|
[3004] | 591 | fts_free_entry(sp, tmp);
|
---|
[2982] | 592 | goto next;
|
---|
| 593 | }
|
---|
[3005] | 594 | if (p->fts_instr != FTS_FOLLOW) {
|
---|
| 595 | /* likely */
|
---|
| 596 | } else {
|
---|
[2985] | 597 | p->fts_info = fts_stat(sp, p, 1, INVALID_HANDLE_VALUE);
|
---|
[3004] | 598 | /* NT: See above regarding fts_flags. */
|
---|
| 599 | if (p->fts_info == FTS_D) {
|
---|
[2985] | 600 | p->fts_flags |= FTS_SYMFOLLOW;
|
---|
[2982] | 601 | }
|
---|
| 602 | p->fts_instr = FTS_NOINSTR;
|
---|
| 603 | }
|
---|
| 604 |
|
---|
[3004] | 605 | fts_free_entry(sp, tmp);
|
---|
[2982] | 606 |
|
---|
[2998] | 607 | name:
|
---|
| 608 | if (!(sp->fts_options & FTS_NO_ANSI)) {
|
---|
| 609 | char *t = sp->fts_path + NAPPEND(p->fts_parent);
|
---|
| 610 | *t++ = '/';
|
---|
| 611 | memmove(t, p->fts_name, p->fts_namelen + 1);
|
---|
| 612 | }
|
---|
| 613 | pwc = sp->fts_wcspath + NAPPENDW(p->fts_parent);
|
---|
| 614 | *pwc++ = '/';
|
---|
| 615 | memmove(pwc, p->fts_wcsname, (p->fts_cwcname + 1) * sizeof(wchar_t));
|
---|
[2982] | 616 | return (sp->fts_cur = p);
|
---|
| 617 | }
|
---|
| 618 |
|
---|
| 619 | /* Move up to the parent node. */
|
---|
| 620 | p = tmp->fts_parent;
|
---|
| 621 |
|
---|
[3005] | 622 | if (p->fts_level != FTS_ROOTPARENTLEVEL) {
|
---|
| 623 | /* likely */
|
---|
| 624 | } else {
|
---|
[2982] | 625 | /*
|
---|
| 626 | * Done; free everything up and set errno to 0 so the user
|
---|
| 627 | * can distinguish between error and EOF.
|
---|
| 628 | */
|
---|
[3004] | 629 | fts_free_entry(sp, tmp);
|
---|
| 630 | fts_free_entry(sp, p);
|
---|
[2982] | 631 | errno = 0;
|
---|
| 632 | return (sp->fts_cur = NULL);
|
---|
| 633 | }
|
---|
| 634 |
|
---|
| 635 | /* NUL terminate the pathname. */
|
---|
[2998] | 636 | if (!(sp->fts_options & FTS_NO_ANSI))
|
---|
| 637 | sp->fts_path[p->fts_pathlen] = '\0';
|
---|
| 638 | sp->fts_wcspath[ p->fts_cwcpath] = '\0';
|
---|
[2982] | 639 |
|
---|
| 640 | /*
|
---|
| 641 | * Return to the parent directory. If at a root node or came through
|
---|
| 642 | * a symlink, go back through the file descriptor. Otherwise, cd up
|
---|
| 643 | * one directory.
|
---|
[2985] | 644 | *
|
---|
[3004] | 645 | * NT: We're doing no fchdir, but we need to close the directory handle.
|
---|
[2982] | 646 | */
|
---|
[3000] | 647 | if (p->fts_dirfd != INVALID_HANDLE_VALUE) {
|
---|
[2985] | 648 | birdCloseFile(p->fts_dirfd);
|
---|
| 649 | p->fts_dirfd = INVALID_HANDLE_VALUE;
|
---|
[3000] | 650 | }
|
---|
[3004] | 651 | fts_free_entry(sp, tmp);
|
---|
[2982] | 652 | p->fts_info = p->fts_errno ? FTS_ERR : FTS_DP;
|
---|
| 653 | return (sp->fts_cur = p);
|
---|
| 654 | }
|
---|
| 655 |
|
---|
| 656 | /*
|
---|
| 657 | * Fts_set takes the stream as an argument although it's not used in this
|
---|
| 658 | * implementation; it would be necessary if anyone wanted to add global
|
---|
| 659 | * semantics to fts using fts_set. An error return is allowed for similar
|
---|
| 660 | * reasons.
|
---|
| 661 | */
|
---|
| 662 | /* ARGSUSED */
|
---|
[2985] | 663 | int FTSCALL
|
---|
[2992] | 664 | nt_fts_set(FTS *sp, FTSENT *p, int instr)
|
---|
[2982] | 665 | {
|
---|
| 666 | if (instr != 0 && instr != FTS_AGAIN && instr != FTS_FOLLOW &&
|
---|
| 667 | instr != FTS_NOINSTR && instr != FTS_SKIP) {
|
---|
| 668 | errno = EINVAL;
|
---|
| 669 | return (1);
|
---|
| 670 | }
|
---|
| 671 | p->fts_instr = instr;
|
---|
| 672 | return (0);
|
---|
| 673 | }
|
---|
| 674 |
|
---|
[2985] | 675 | FTSENT * FTSCALL
|
---|
[2992] | 676 | nt_fts_children(FTS *sp, int instr)
|
---|
[2982] | 677 | {
|
---|
| 678 | FTSENT *p;
|
---|
| 679 |
|
---|
| 680 | if (instr != 0 && instr != FTS_NAMEONLY) {
|
---|
| 681 | errno = EINVAL;
|
---|
| 682 | return (NULL);
|
---|
| 683 | }
|
---|
| 684 |
|
---|
| 685 | /* Set current node pointer. */
|
---|
| 686 | p = sp->fts_cur;
|
---|
| 687 |
|
---|
| 688 | /*
|
---|
| 689 | * Errno set to 0 so user can distinguish empty directory from
|
---|
| 690 | * an error.
|
---|
| 691 | */
|
---|
| 692 | errno = 0;
|
---|
| 693 |
|
---|
| 694 | /* Fatal errors stop here. */
|
---|
| 695 | if (ISSET(FTS_STOP))
|
---|
| 696 | return (NULL);
|
---|
| 697 |
|
---|
| 698 | /* Return logical hierarchy of user's arguments. */
|
---|
| 699 | if (p->fts_info == FTS_INIT)
|
---|
| 700 | return (p->fts_link);
|
---|
| 701 |
|
---|
| 702 | /*
|
---|
| 703 | * If not a directory being visited in pre-order, stop here. Could
|
---|
| 704 | * allow FTS_DNR, assuming the user has fixed the problem, but the
|
---|
| 705 | * same effect is available with FTS_AGAIN.
|
---|
| 706 | */
|
---|
| 707 | if (p->fts_info != FTS_D /* && p->fts_info != FTS_DNR */)
|
---|
| 708 | return (NULL);
|
---|
| 709 |
|
---|
| 710 | /* Free up any previous child list. */
|
---|
[2990] | 711 | if (sp->fts_child != NULL) {
|
---|
[2982] | 712 | fts_lfree(sp->fts_child);
|
---|
[2990] | 713 | sp->fts_child = NULL; /* (bird - double free for _open(".") failure in original) */
|
---|
| 714 | }
|
---|
[2982] | 715 |
|
---|
[2990] | 716 | /* NT: Some BSD utility sets FTS_NAMEONLY? We don't really need this
|
---|
| 717 | optimization, but since it only hurts that utility, it can stay. */
|
---|
[2982] | 718 | if (instr == FTS_NAMEONLY) {
|
---|
[2990] | 719 | assert(0); /* don't specify FTS_NAMEONLY on NT. */
|
---|
[2982] | 720 | SET(FTS_NAMEONLY);
|
---|
| 721 | instr = BNAMES;
|
---|
| 722 | } else
|
---|
| 723 | instr = BCHILD;
|
---|
| 724 |
|
---|
[2990] | 725 | return (sp->fts_child = fts_build(sp, instr));
|
---|
[2982] | 726 | }
|
---|
| 727 |
|
---|
| 728 | #ifndef fts_get_clientptr
|
---|
| 729 | #error "fts_get_clientptr not defined"
|
---|
| 730 | #endif
|
---|
| 731 |
|
---|
| 732 | void *
|
---|
[2985] | 733 | (FTSCALL fts_get_clientptr)(FTS *sp)
|
---|
[2982] | 734 | {
|
---|
| 735 |
|
---|
| 736 | return (fts_get_clientptr(sp));
|
---|
| 737 | }
|
---|
| 738 |
|
---|
| 739 | #ifndef fts_get_stream
|
---|
| 740 | #error "fts_get_stream not defined"
|
---|
| 741 | #endif
|
---|
| 742 |
|
---|
| 743 | FTS *
|
---|
[2985] | 744 | (FTSCALL fts_get_stream)(FTSENT *p)
|
---|
[2982] | 745 | {
|
---|
| 746 | return (fts_get_stream(p));
|
---|
| 747 | }
|
---|
| 748 |
|
---|
[2985] | 749 | void FTSCALL
|
---|
[2992] | 750 | nt_fts_set_clientptr(FTS *sp, void *clientptr)
|
---|
[2982] | 751 | {
|
---|
| 752 |
|
---|
| 753 | sp->fts_clientptr = clientptr;
|
---|
| 754 | }
|
---|
| 755 |
|
---|
| 756 | /*
|
---|
| 757 | * This is the tricky part -- do not casually change *anything* in here. The
|
---|
| 758 | * idea is to build the linked list of entries that are used by fts_children
|
---|
| 759 | * and fts_read. There are lots of special cases.
|
---|
| 760 | *
|
---|
| 761 | * The real slowdown in walking the tree is the stat calls. If FTS_NOSTAT is
|
---|
| 762 | * set and it's a physical walk (so that symbolic links can't be directories),
|
---|
| 763 | * we can do things quickly. First, if it's a 4.4BSD file system, the type
|
---|
| 764 | * of the file is in the directory entry. Otherwise, we assume that the number
|
---|
| 765 | * of subdirectories in a node is equal to the number of links to the parent.
|
---|
| 766 | * The former skips all stat calls. The latter skips stat calls in any leaf
|
---|
| 767 | * directories and for any files after the subdirectories in the directory have
|
---|
| 768 | * been found, cutting the stat calls by about 2/3.
|
---|
[2985] | 769 | *
|
---|
| 770 | * NT: We do not do any link counting or stat avoiding, which invalidates the
|
---|
| 771 | * above warnings. This function is very simple for us.
|
---|
[2982] | 772 | */
|
---|
| 773 | static FTSENT *
|
---|
| 774 | fts_build(FTS *sp, int type)
|
---|
| 775 | {
|
---|
[3004] | 776 | BirdDirEntryW_T *dp;
|
---|
[3006] | 777 | FTSENT *p, *cur;
|
---|
| 778 | FTSENT * volatile head,* volatile *tailp; /* volatile is to prevent aliasing trouble */
|
---|
[2982] | 779 | DIR *dirp;
|
---|
[2998] | 780 | int saved_errno, doadjust, doadjust_utf16;
|
---|
[2982] | 781 | long level;
|
---|
[2998] | 782 | size_t len, cwcdir, maxlen, cwcmax, nitems;
|
---|
[2985] | 783 | unsigned fDirOpenFlags;
|
---|
[2982] | 784 |
|
---|
| 785 | /* Set current node pointer. */
|
---|
| 786 | cur = sp->fts_cur;
|
---|
| 787 |
|
---|
| 788 | /*
|
---|
| 789 | * Open the directory for reading. If this fails, we're done.
|
---|
| 790 | * If being called from fts_read, set the fts_info field.
|
---|
[2985] | 791 | *
|
---|
| 792 | * NT: We do a two stage open so we can keep the directory handle around
|
---|
| 793 | * after we've enumerated the directory. The dir handle is used by
|
---|
| 794 | * us here and by the API users to more efficiently and safely open
|
---|
| 795 | * members of the directory.
|
---|
[2982] | 796 | */
|
---|
[2985] | 797 | fDirOpenFlags = BIRDDIR_F_EXTRA_INFO | BIRDDIR_F_KEEP_HANDLE;
|
---|
[3000] | 798 | if (cur->fts_dirfd == INVALID_HANDLE_VALUE) {
|
---|
[2985] | 799 | if (cur->fts_parent->fts_dirfd != INVALID_HANDLE_VALUE) {
|
---|
| 800 | /* (This works fine for symlinks too, since we follow them.) */
|
---|
[3001] | 801 | cur->fts_dirfd = birdOpenFileExW(cur->fts_parent->fts_dirfd,
|
---|
| 802 | cur->fts_wcsname,
|
---|
| 803 | FILE_READ_DATA | SYNCHRONIZE,
|
---|
| 804 | FILE_ATTRIBUTE_NORMAL,
|
---|
| 805 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
|
---|
| 806 | FILE_OPEN,
|
---|
| 807 | FILE_DIRECTORY_FILE | FILE_OPEN_FOR_BACKUP_INTENT | FILE_SYNCHRONOUS_IO_NONALERT,
|
---|
| 808 | OBJ_CASE_INSENSITIVE);
|
---|
[2985] | 809 | } else {
|
---|
[3001] | 810 | cur->fts_dirfd = birdOpenFileW(cur->fts_wcsaccpath,
|
---|
| 811 | FILE_READ_DATA | SYNCHRONIZE,
|
---|
| 812 | FILE_ATTRIBUTE_NORMAL,
|
---|
| 813 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
|
---|
| 814 | FILE_OPEN,
|
---|
| 815 | FILE_DIRECTORY_FILE | FILE_OPEN_FOR_BACKUP_INTENT | FILE_SYNCHRONOUS_IO_NONALERT,
|
---|
| 816 | OBJ_CASE_INSENSITIVE);
|
---|
[2985] | 817 | }
|
---|
[3001] | 818 | if (cur->fts_dirfd != INVALID_HANDLE_VALUE) { /* likely */ }
|
---|
| 819 | else goto l_open_err;
|
---|
| 820 |
|
---|
[3000] | 821 | } else {
|
---|
[2985] | 822 | fDirOpenFlags |= BIRDDIR_F_RESTART_SCAN;
|
---|
| 823 | }
|
---|
[3005] | 824 | #ifdef FTS_WITH_DIRHANDLE_REUSE
|
---|
| 825 | dirp = birdDirOpenFromHandleWithReuse(&((struct _fts_private *)sp)->dirhandle, cur->fts_dirfd, NULL,
|
---|
| 826 | fDirOpenFlags | BIRDDIR_F_STATIC_ALLOC);
|
---|
| 827 | #else
|
---|
[2985] | 828 | dirp = birdDirOpenFromHandle(cur->fts_dirfd, NULL, fDirOpenFlags);
|
---|
[3005] | 829 | #endif
|
---|
[2985] | 830 | if (dirp == NULL) {
|
---|
[3001] | 831 | l_open_err:
|
---|
[2982] | 832 | if (type == BREAD) {
|
---|
| 833 | cur->fts_info = FTS_DNR;
|
---|
| 834 | cur->fts_errno = errno;
|
---|
| 835 | }
|
---|
| 836 | return (NULL);
|
---|
| 837 | }
|
---|
| 838 |
|
---|
| 839 | /*
|
---|
| 840 | * Figure out the max file name length that can be stored in the
|
---|
| 841 | * current path -- the inner loop allocates more path as necessary.
|
---|
| 842 | * We really wouldn't have to do the maxlen calculations here, we
|
---|
| 843 | * could do them in fts_read before returning the path, but it's a
|
---|
| 844 | * lot easier here since the length is part of the dirent structure.
|
---|
| 845 | */
|
---|
[3000] | 846 | if (sp->fts_options & FTS_NO_ANSI) {
|
---|
[3004] | 847 | len = 0;
|
---|
| 848 | maxlen = 0x10000;
|
---|
[2998] | 849 | } else {
|
---|
| 850 | len = NAPPEND(cur);
|
---|
| 851 | len++;
|
---|
| 852 | maxlen = sp->fts_pathlen - len;
|
---|
| 853 | }
|
---|
[2982] | 854 |
|
---|
[2998] | 855 | cwcdir = NAPPENDW(cur);
|
---|
| 856 | cwcdir++;
|
---|
| 857 | cwcmax = sp->fts_cwcpath - len;
|
---|
| 858 |
|
---|
[2982] | 859 | level = cur->fts_level + 1;
|
---|
| 860 |
|
---|
| 861 | /* Read the directory, attaching each entry to the `link' pointer. */
|
---|
[2998] | 862 | doadjust = doadjust_utf16 = 0;
|
---|
[3004] | 863 | nitems = 0;
|
---|
| 864 | head = NULL;
|
---|
| 865 | tailp = &head;
|
---|
| 866 | while ((dp = birdDirReadW(dirp)) != NULL) {
|
---|
| 867 | if (ISSET(FTS_SEEDOT) || !ISDOT(dp->d_name)) {
|
---|
| 868 | /* assume dirs have two or more entries */
|
---|
| 869 | } else {
|
---|
[2982] | 870 | continue;
|
---|
[3004] | 871 | }
|
---|
[2982] | 872 |
|
---|
[3004] | 873 | if ((p = fts_alloc_utf16(sp, dp->d_name, dp->d_namlen)) != NULL) {
|
---|
| 874 | /* likely */
|
---|
| 875 | } else {
|
---|
[2982] | 876 | goto mem1;
|
---|
[3004] | 877 | }
|
---|
[2998] | 878 |
|
---|
[3004] | 879 | /* include space for NUL */
|
---|
| 880 | if (p->fts_namelen < maxlen && p->fts_cwcname < cwcmax) {
|
---|
| 881 | /* likely */
|
---|
| 882 | } else {
|
---|
[2998] | 883 | void *oldaddr = sp->fts_path;
|
---|
| 884 | wchar_t *oldwcspath = sp->fts_wcspath;
|
---|
| 885 | if (fts_palloc(sp,
|
---|
[3004] | 886 | p->fts_namelen >= maxlen ? len + p->fts_namelen + 1 : 0,
|
---|
| 887 | p->fts_cwcname >= cwcmax ? cwcdir + p->fts_cwcname + 1 : 0)) {
|
---|
| 888 | mem1:
|
---|
[2982] | 889 | /*
|
---|
| 890 | * No more memory for path or structures. Save
|
---|
| 891 | * errno, free up the current structure and the
|
---|
| 892 | * structures already allocated.
|
---|
| 893 | */
|
---|
[3004] | 894 | saved_errno = errno;
|
---|
[2982] | 895 | if (p)
|
---|
| 896 | free(p);
|
---|
| 897 | fts_lfree(head);
|
---|
[3005] | 898 | #ifndef FTS_WITH_DIRHANDLE_REUSE
|
---|
[2985] | 899 | birdDirClose(dirp);
|
---|
[3005] | 900 | #endif
|
---|
[2985] | 901 | birdCloseFile(cur->fts_dirfd);
|
---|
| 902 | cur->fts_dirfd = INVALID_HANDLE_VALUE;
|
---|
[2982] | 903 | cur->fts_info = FTS_ERR;
|
---|
| 904 | SET(FTS_STOP);
|
---|
| 905 | errno = saved_errno;
|
---|
| 906 | return (NULL);
|
---|
| 907 | }
|
---|
| 908 | /* Did realloc() change the pointer? */
|
---|
[2998] | 909 | doadjust |= oldaddr != sp->fts_path;
|
---|
| 910 | doadjust_utf16 |= oldwcspath != sp->fts_wcspath;
|
---|
[2982] | 911 | maxlen = sp->fts_pathlen - len;
|
---|
[2998] | 912 | cwcmax = sp->fts_cwcpath - cwcdir;
|
---|
[2982] | 913 | }
|
---|
| 914 |
|
---|
| 915 | p->fts_level = level;
|
---|
| 916 | p->fts_parent = sp->fts_cur;
|
---|
[2998] | 917 | p->fts_pathlen = len + p->fts_namelen;
|
---|
| 918 | p->fts_cwcpath = cwcdir + p->fts_cwcname;
|
---|
[2985] | 919 | p->fts_accpath = p->fts_path;
|
---|
[2998] | 920 | p->fts_wcsaccpath = p->fts_wcspath;
|
---|
[2985] | 921 | p->fts_stat = dp->d_stat;
|
---|
| 922 | p->fts_info = fts_process_stats(p, &dp->d_stat);
|
---|
[2982] | 923 |
|
---|
| 924 | /* We walk in directory order so "ls -f" doesn't get upset. */
|
---|
| 925 | p->fts_link = NULL;
|
---|
[3004] | 926 | *tailp = p;
|
---|
| 927 | tailp = &p->fts_link;
|
---|
[2982] | 928 | ++nitems;
|
---|
| 929 | }
|
---|
| 930 |
|
---|
[3005] | 931 | #ifndef FTS_WITH_DIRHANDLE_REUSE
|
---|
[2985] | 932 | birdDirClose(dirp);
|
---|
[3005] | 933 | #endif
|
---|
[2985] | 934 |
|
---|
[2982] | 935 | /*
|
---|
| 936 | * If realloc() changed the address of the path, adjust the
|
---|
| 937 | * addresses for the rest of the tree and the dir list.
|
---|
| 938 | */
|
---|
| 939 | if (doadjust)
|
---|
| 940 | fts_padjust(sp, head);
|
---|
[2998] | 941 | if (doadjust_utf16)
|
---|
| 942 | fts_padjustw(sp, head);
|
---|
[2982] | 943 |
|
---|
| 944 | /* If didn't find anything, return NULL. */
|
---|
| 945 | if (!nitems) {
|
---|
| 946 | if (type == BREAD)
|
---|
| 947 | cur->fts_info = FTS_DP;
|
---|
| 948 | return (NULL);
|
---|
| 949 | }
|
---|
| 950 |
|
---|
| 951 | /* Sort the entries. */
|
---|
| 952 | if (sp->fts_compar && nitems > 1)
|
---|
| 953 | head = fts_sort(sp, head, nitems);
|
---|
| 954 | return (head);
|
---|
| 955 | }
|
---|
| 956 |
|
---|
[2985] | 957 |
|
---|
| 958 | /**
|
---|
| 959 | * @note Only used on NT with input arguments, FTS_AGAIN, and links that needs
|
---|
| 960 | * following. On link information is generally retrieved during directory
|
---|
| 961 | * enumeration on NT, in line with it's DOS/OS2/FAT API heritage.
|
---|
| 962 | */
|
---|
[2982] | 963 | static int
|
---|
[2985] | 964 | fts_stat(FTS *sp, FTSENT *p, int follow, HANDLE dfd)
|
---|
[2982] | 965 | {
|
---|
| 966 | int saved_errno;
|
---|
[3001] | 967 | const wchar_t *wcspath;
|
---|
[2982] | 968 |
|
---|
[2985] | 969 | if (dfd == INVALID_HANDLE_VALUE) {
|
---|
[3001] | 970 | wcspath = p->fts_wcsaccpath;
|
---|
[2985] | 971 | } else {
|
---|
[3001] | 972 | wcspath = p->fts_wcsname;
|
---|
[2982] | 973 | }
|
---|
| 974 |
|
---|
| 975 | /*
|
---|
| 976 | * If doing a logical walk, or application requested FTS_FOLLOW, do
|
---|
| 977 | * a stat(2). If that fails, check for a non-existent symlink. If
|
---|
| 978 | * fail, set the errno from the stat call.
|
---|
| 979 | */
|
---|
| 980 | if (ISSET(FTS_LOGICAL) || follow) {
|
---|
[3001] | 981 | if (birdStatAtW(dfd, wcspath, &p->fts_stat, 1 /*fFollowLink*/)) {
|
---|
[2982] | 982 | saved_errno = errno;
|
---|
[3001] | 983 | if (birdStatAtW(dfd, wcspath, &p->fts_stat, 0 /*fFollowLink*/)) {
|
---|
[2982] | 984 | p->fts_errno = saved_errno;
|
---|
| 985 | goto err;
|
---|
| 986 | }
|
---|
| 987 | errno = 0;
|
---|
[2985] | 988 | if (S_ISLNK(p->fts_stat.st_mode))
|
---|
[2982] | 989 | return (FTS_SLNONE);
|
---|
| 990 | }
|
---|
[3001] | 991 | } else if (birdStatAtW(dfd, wcspath, &p->fts_stat, 0 /*fFollowLink*/)) {
|
---|
[2982] | 992 | p->fts_errno = errno;
|
---|
[2985] | 993 | err: memset(&p->fts_stat, 0, sizeof(struct stat));
|
---|
[2982] | 994 | return (FTS_NS);
|
---|
| 995 | }
|
---|
[2985] | 996 | return fts_process_stats(p, &p->fts_stat);
|
---|
| 997 | }
|
---|
[2982] | 998 |
|
---|
[2985] | 999 | /* Shared between fts_stat and fts_build. */
|
---|
| 1000 | static int
|
---|
| 1001 | fts_process_stats(FTSENT *p, BirdStat_T const *sbp)
|
---|
| 1002 | {
|
---|
[2982] | 1003 | if (S_ISDIR(sbp->st_mode)) {
|
---|
[2985] | 1004 | FTSENT *t;
|
---|
| 1005 | fts_dev_t dev;
|
---|
| 1006 | fts_ino_t ino;
|
---|
| 1007 |
|
---|
[2982] | 1008 | /*
|
---|
| 1009 | * Set the device/inode. Used to find cycles and check for
|
---|
| 1010 | * crossing mount points. Also remember the link count, used
|
---|
| 1011 | * in fts_build to limit the number of stat calls. It is
|
---|
| 1012 | * understood that these fields are only referenced if fts_info
|
---|
| 1013 | * is set to FTS_D.
|
---|
| 1014 | */
|
---|
| 1015 | dev = p->fts_dev = sbp->st_dev;
|
---|
| 1016 | ino = p->fts_ino = sbp->st_ino;
|
---|
| 1017 | p->fts_nlink = sbp->st_nlink;
|
---|
| 1018 |
|
---|
[3004] | 1019 | if (ISDOT(p->fts_wcsname))
|
---|
[2982] | 1020 | return (FTS_DOT);
|
---|
| 1021 |
|
---|
| 1022 | /*
|
---|
| 1023 | * Cycle detection is done by brute force when the directory
|
---|
| 1024 | * is first encountered. If the tree gets deep enough or the
|
---|
| 1025 | * number of symbolic links to directories is high enough,
|
---|
| 1026 | * something faster might be worthwhile.
|
---|
| 1027 | */
|
---|
| 1028 | for (t = p->fts_parent;
|
---|
| 1029 | t->fts_level >= FTS_ROOTLEVEL; t = t->fts_parent)
|
---|
| 1030 | if (ino == t->fts_ino && dev == t->fts_dev) {
|
---|
| 1031 | p->fts_cycle = t;
|
---|
| 1032 | return (FTS_DC);
|
---|
| 1033 | }
|
---|
| 1034 | return (FTS_D);
|
---|
| 1035 | }
|
---|
| 1036 | if (S_ISLNK(sbp->st_mode))
|
---|
| 1037 | return (FTS_SL);
|
---|
| 1038 | if (S_ISREG(sbp->st_mode))
|
---|
| 1039 | return (FTS_F);
|
---|
| 1040 | return (FTS_DEFAULT);
|
---|
| 1041 | }
|
---|
| 1042 |
|
---|
| 1043 | /*
|
---|
| 1044 | * The comparison function takes pointers to pointers to FTSENT structures.
|
---|
| 1045 | * Qsort wants a comparison function that takes pointers to void.
|
---|
| 1046 | * (Both with appropriate levels of const-poisoning, of course!)
|
---|
| 1047 | * Use a trampoline function to deal with the difference.
|
---|
| 1048 | */
|
---|
| 1049 | static int
|
---|
| 1050 | fts_compar(const void *a, const void *b)
|
---|
| 1051 | {
|
---|
| 1052 | FTS *parent;
|
---|
| 1053 |
|
---|
| 1054 | parent = (*(const FTSENT * const *)a)->fts_fts;
|
---|
| 1055 | return (*parent->fts_compar)(a, b);
|
---|
| 1056 | }
|
---|
| 1057 |
|
---|
| 1058 | static FTSENT *
|
---|
| 1059 | fts_sort(FTS *sp, FTSENT *head, size_t nitems)
|
---|
| 1060 | {
|
---|
| 1061 | FTSENT **ap, *p;
|
---|
| 1062 |
|
---|
| 1063 | /*
|
---|
| 1064 | * Construct an array of pointers to the structures and call qsort(3).
|
---|
| 1065 | * Reassemble the array in the order returned by qsort. If unable to
|
---|
| 1066 | * sort for memory reasons, return the directory entries in their
|
---|
| 1067 | * current order. Allocate enough space for the current needs plus
|
---|
| 1068 | * 40 so don't realloc one entry at a time.
|
---|
| 1069 | */
|
---|
| 1070 | if (nitems > sp->fts_nitems) {
|
---|
[2985] | 1071 | void *ptr;
|
---|
[2982] | 1072 | sp->fts_nitems = nitems + 40;
|
---|
[2985] | 1073 | ptr = realloc(sp->fts_array, sp->fts_nitems * sizeof(FTSENT *));
|
---|
| 1074 | if (ptr != NULL) {
|
---|
| 1075 | sp->fts_array = ptr;
|
---|
| 1076 | } else {
|
---|
| 1077 | free(sp->fts_array);
|
---|
| 1078 | sp->fts_array = NULL;
|
---|
[2982] | 1079 | sp->fts_nitems = 0;
|
---|
| 1080 | return (head);
|
---|
| 1081 | }
|
---|
| 1082 | }
|
---|
| 1083 | for (ap = sp->fts_array, p = head; p; p = p->fts_link)
|
---|
| 1084 | *ap++ = p;
|
---|
| 1085 | qsort(sp->fts_array, nitems, sizeof(FTSENT *), fts_compar);
|
---|
| 1086 | for (head = *(ap = sp->fts_array); --nitems; ++ap)
|
---|
| 1087 | ap[0]->fts_link = ap[1];
|
---|
| 1088 | ap[0]->fts_link = NULL;
|
---|
| 1089 | return (head);
|
---|
| 1090 | }
|
---|
| 1091 |
|
---|
| 1092 | static FTSENT *
|
---|
[2998] | 1093 | fts_alloc(FTS *sp, char const *name, size_t namelen, wchar_t const *wcsname, size_t cwcname)
|
---|
[2982] | 1094 | {
|
---|
[3004] | 1095 | struct _fts_private *priv = (struct _fts_private *)sp;
|
---|
[2982] | 1096 | FTSENT *p;
|
---|
| 1097 | size_t len;
|
---|
[3004] | 1098 | #ifdef FTS_WITH_ALLOC_CACHE
|
---|
| 1099 | size_t aligned;
|
---|
| 1100 | size_t idx;
|
---|
| 1101 | #endif
|
---|
[2982] | 1102 |
|
---|
[3004] | 1103 | #if defined(FTS_WITH_STATISTICS) && defined(FTS_WITH_ALLOC_CACHE)
|
---|
| 1104 | priv->allocs++;
|
---|
| 1105 | #endif
|
---|
[2982] | 1106 | /*
|
---|
[2985] | 1107 | * The file name is a variable length array. Allocate the FTSENT
|
---|
| 1108 | * structure and the file name.
|
---|
[2982] | 1109 | */
|
---|
[2998] | 1110 | len = sizeof(FTSENT) + (cwcname + 1) * sizeof(wchar_t);
|
---|
| 1111 | if (!(sp->fts_options & FTS_NO_ANSI))
|
---|
[3000] | 1112 | len += namelen + 1;
|
---|
[3004] | 1113 |
|
---|
| 1114 | /*
|
---|
| 1115 | * To speed things up we cache entries. This code is a little insane,
|
---|
| 1116 | * but that's preferable to slow code.
|
---|
| 1117 | */
|
---|
| 1118 | #ifdef FTS_WITH_ALLOC_CACHE
|
---|
| 1119 | aligned = (len + FTS_ALIGN_FTSENT + 1) & ~(size_t)(FTS_ALIGN_FTSENT - 1);
|
---|
| 1120 | idx = ((aligned - sizeof(FTSENT)) >> FTS_FREE_BUCKET_SHIFT);
|
---|
| 1121 | if ( idx < FTS_NUM_FREE_BUCKETS
|
---|
| 1122 | && (p = priv->freebuckets[idx].head)
|
---|
| 1123 | && p->fts_alloc_size >= len) {
|
---|
| 1124 | priv->freebuckets[idx].head = p->fts_link;
|
---|
| 1125 | priv->numfree--;
|
---|
| 1126 | # ifdef FTS_WITH_STATISTICS
|
---|
| 1127 | priv->hits++;
|
---|
| 1128 | # endif
|
---|
| 1129 |
|
---|
| 1130 | } else {
|
---|
| 1131 | # ifdef FTS_WITH_STATISTICS
|
---|
| 1132 | priv->misses++;
|
---|
| 1133 | # endif
|
---|
| 1134 | p = malloc(aligned);
|
---|
| 1135 | if (p) {
|
---|
| 1136 | p->fts_alloc_size = (unsigned)aligned;
|
---|
| 1137 | } else {
|
---|
| 1138 | nt_fts_free_alloc_cache(sp);
|
---|
| 1139 | p = malloc(len);
|
---|
| 1140 | if (!p)
|
---|
| 1141 | return NULL;
|
---|
| 1142 | p->fts_alloc_size = (unsigned)len;
|
---|
| 1143 | }
|
---|
| 1144 | }
|
---|
| 1145 | #else /* !FTS_WITH_ALLOC_CACHE */
|
---|
[2998] | 1146 | p = malloc(len);
|
---|
| 1147 | if (p) {
|
---|
[3004] | 1148 | p->fts_alloc_size = (unsigned)len;
|
---|
| 1149 | } else {
|
---|
| 1150 | return NULL;
|
---|
| 1151 | }
|
---|
| 1152 | #endif /* !FTS_WITH_ALLOC_CACHE */
|
---|
[2982] | 1153 |
|
---|
[3004] | 1154 | /* Copy the names and guarantee NUL termination. */
|
---|
| 1155 | p->fts_wcsname = (wchar_t *)(p + 1);
|
---|
| 1156 | memcpy(p->fts_wcsname, wcsname, cwcname * sizeof(wchar_t));
|
---|
| 1157 | p->fts_wcsname[cwcname] = '\0';
|
---|
| 1158 | p->fts_cwcname = cwcname;
|
---|
| 1159 | if (!(sp->fts_options & FTS_NO_ANSI)) {
|
---|
| 1160 | p->fts_name = (char *)(p->fts_wcsname + cwcname + 1);
|
---|
| 1161 | memcpy(p->fts_name, name, namelen);
|
---|
| 1162 | p->fts_name[namelen] = '\0';
|
---|
| 1163 | p->fts_namelen = namelen;
|
---|
| 1164 | } else {
|
---|
| 1165 | p->fts_name = NULL;
|
---|
| 1166 | p->fts_namelen = 0;
|
---|
[2998] | 1167 | }
|
---|
[3004] | 1168 |
|
---|
| 1169 | p->fts_path = sp->fts_path;
|
---|
| 1170 | p->fts_wcspath = sp->fts_wcspath;
|
---|
| 1171 | p->fts_statp = &p->fts_stat;
|
---|
| 1172 | p->fts_errno = 0;
|
---|
| 1173 | p->fts_flags = 0;
|
---|
| 1174 | p->fts_instr = FTS_NOINSTR;
|
---|
| 1175 | p->fts_number = 0;
|
---|
| 1176 | p->fts_pointer = NULL;
|
---|
| 1177 | p->fts_fts = sp;
|
---|
| 1178 | p->fts_dirfd = INVALID_HANDLE_VALUE;
|
---|
[2982] | 1179 | return (p);
|
---|
| 1180 | }
|
---|
| 1181 |
|
---|
[2998] | 1182 |
|
---|
| 1183 | /**
|
---|
| 1184 | * Converts the ANSI name to UTF-16 and calls fts_alloc.
|
---|
| 1185 | *
|
---|
| 1186 | * @returns Pointer to allocated and mostly initialized FTSENT structure on
|
---|
| 1187 | * success. NULL on failure, caller needs to record it.
|
---|
| 1188 | * @param sp Pointer to FTS instance.
|
---|
| 1189 | * @param name The ANSI name.
|
---|
| 1190 | * @param namelen The ANSI name length.
|
---|
| 1191 | */
|
---|
| 1192 | static FTSENT *
|
---|
| 1193 | fts_alloc_ansi(FTS *sp, char const *name, size_t namelen)
|
---|
| 1194 | {
|
---|
| 1195 | MY_UNICODE_STRING UniStr;
|
---|
| 1196 | MY_ANSI_STRING AnsiStr;
|
---|
| 1197 | MY_NTSTATUS rcNt;
|
---|
| 1198 | FTSENT *pRet;
|
---|
| 1199 |
|
---|
| 1200 | UniStr.Buffer = NULL;
|
---|
| 1201 | UniStr.MaximumLength = UniStr.Length = 0;
|
---|
| 1202 |
|
---|
| 1203 | AnsiStr.Buffer = (char *)name;
|
---|
| 1204 | AnsiStr.Length = AnsiStr.MaximumLength = (USHORT)namelen;
|
---|
| 1205 |
|
---|
| 1206 | rcNt = g_pfnRtlAnsiStringToUnicodeString(&UniStr, &AnsiStr, TRUE /*fAllocate*/);
|
---|
| 1207 | if (NT_SUCCESS(rcNt)) {
|
---|
| 1208 | pRet = fts_alloc(sp, name, namelen, UniStr.Buffer, UniStr.Length / sizeof(wchar_t));
|
---|
| 1209 | HeapFree(GetProcessHeap(), 0, UniStr.Buffer);
|
---|
| 1210 | } else {
|
---|
| 1211 | pRet = NULL;
|
---|
| 1212 | }
|
---|
| 1213 | return pRet;
|
---|
| 1214 | }
|
---|
| 1215 |
|
---|
| 1216 |
|
---|
| 1217 | /**
|
---|
| 1218 | * Converts the UTF-16 name to ANSI (if necessary) and calls fts_alloc.
|
---|
| 1219 | *
|
---|
| 1220 | * @returns Pointer to allocated and mostly initialized FTSENT structure on
|
---|
| 1221 | * success. NULL on failure, caller needs to record it.
|
---|
[3004] | 1222 | * @param sp Pointer to the FTS instance.
|
---|
[2998] | 1223 | * @param wcsname The UTF-16 name.
|
---|
| 1224 | * @param cwcname The UTF-16 name length.
|
---|
| 1225 | */
|
---|
| 1226 | static FTSENT *
|
---|
| 1227 | fts_alloc_utf16(FTS *sp, wchar_t const *wcsname, size_t cwcname)
|
---|
| 1228 | {
|
---|
[3000] | 1229 | FTSENT *pRet;
|
---|
[2998] | 1230 |
|
---|
[3000] | 1231 | if (sp->fts_options & FTS_NO_ANSI) {
|
---|
[2998] | 1232 | pRet = fts_alloc(sp, NULL, 0, wcsname, cwcname);
|
---|
[3000] | 1233 | } else {
|
---|
[2998] | 1234 | MY_UNICODE_STRING UniStr;
|
---|
| 1235 | MY_ANSI_STRING AnsiStr;
|
---|
| 1236 | MY_NTSTATUS rcNt;
|
---|
| 1237 |
|
---|
| 1238 | UniStr.Buffer = (wchar_t *)wcsname;
|
---|
| 1239 | UniStr.MaximumLength = UniStr.Length = (USHORT)(cwcname * sizeof(wchar_t));
|
---|
| 1240 |
|
---|
| 1241 | AnsiStr.Buffer = NULL;
|
---|
| 1242 | AnsiStr.Length = AnsiStr.MaximumLength = 0;
|
---|
| 1243 |
|
---|
| 1244 | rcNt = g_pfnRtlUnicodeStringToAnsiString(&AnsiStr, &UniStr, TRUE /*fAllocate*/);
|
---|
| 1245 | if (NT_SUCCESS(rcNt)) {
|
---|
| 1246 | pRet = fts_alloc(sp, AnsiStr.Buffer, AnsiStr.Length, wcsname, cwcname);
|
---|
| 1247 | HeapFree(GetProcessHeap(), 0, AnsiStr.Buffer);
|
---|
| 1248 | } else {
|
---|
| 1249 | pRet = NULL;
|
---|
| 1250 | }
|
---|
| 1251 | }
|
---|
[3000] | 1252 | return pRet;
|
---|
[2998] | 1253 | }
|
---|
| 1254 |
|
---|
| 1255 |
|
---|
[3004] | 1256 | /**
|
---|
| 1257 | * Frees up the FTSENT allocation cache.
|
---|
| 1258 | *
|
---|
| 1259 | * Used by nt_fts_close, but also called by fts_alloc on alloc failure.
|
---|
| 1260 | *
|
---|
| 1261 | * @param sp Pointer to the FTS instance.
|
---|
| 1262 | */
|
---|
| 1263 | static void nt_fts_free_alloc_cache(FTS *sp)
|
---|
| 1264 | {
|
---|
| 1265 | #ifdef FTS_WITH_ALLOC_CACHE
|
---|
| 1266 | struct _fts_private *priv = (struct _fts_private *)sp;
|
---|
| 1267 | unsigned i = K_ELEMENTS(priv->freebuckets);
|
---|
| 1268 | while (i-- > 0) {
|
---|
| 1269 | FTSENT *cur = priv->freebuckets[i].head;
|
---|
| 1270 | priv->freebuckets[i].head = NULL;
|
---|
| 1271 | while (cur) {
|
---|
| 1272 | FTSENT *freeit = cur;
|
---|
| 1273 | cur = cur->fts_link;
|
---|
| 1274 | free(freeit);
|
---|
| 1275 | }
|
---|
| 1276 | }
|
---|
| 1277 | priv->numfree = 0;
|
---|
| 1278 | #else
|
---|
| 1279 | (void)sp;
|
---|
| 1280 | #endif
|
---|
| 1281 | }
|
---|
| 1282 |
|
---|
| 1283 |
|
---|
[2982] | 1284 | static void
|
---|
| 1285 | fts_lfree(FTSENT *head)
|
---|
| 1286 | {
|
---|
| 1287 | FTSENT *p;
|
---|
| 1288 |
|
---|
| 1289 | /* Free a linked list of structures. */
|
---|
| 1290 | while ((p = head)) {
|
---|
| 1291 | head = head->fts_link;
|
---|
[2985] | 1292 | assert(p->fts_dirfd == INVALID_HANDLE_VALUE);
|
---|
[2982] | 1293 | free(p);
|
---|
| 1294 | }
|
---|
| 1295 | }
|
---|
| 1296 |
|
---|
| 1297 | /*
|
---|
| 1298 | * Allow essentially unlimited paths; find, rm, ls should all work on any tree.
|
---|
| 1299 | * Most systems will allow creation of paths much longer than MAXPATHLEN, even
|
---|
| 1300 | * though the kernel won't resolve them. Add the size (not just what's needed)
|
---|
| 1301 | * plus 256 bytes so don't realloc the path 2 bytes at a time.
|
---|
| 1302 | */
|
---|
| 1303 | static int
|
---|
[2998] | 1304 | fts_palloc(FTS *sp, size_t more, size_t cwcmore)
|
---|
[2982] | 1305 | {
|
---|
[2985] | 1306 | void *ptr;
|
---|
[2982] | 1307 |
|
---|
[2998] | 1308 | /** @todo Isn't more and cwcmore minimum buffer sizes rather than what needs
|
---|
| 1309 | * to be added to the buffer?? This code makes no sense when looking at
|
---|
| 1310 | * the way the caller checks things out! */
|
---|
| 1311 |
|
---|
[3000] | 1312 | if (more) {
|
---|
[2998] | 1313 | sp->fts_pathlen += more + 256;
|
---|
| 1314 | ptr = realloc(sp->fts_path, sp->fts_pathlen);
|
---|
| 1315 | if (ptr) {
|
---|
| 1316 | sp->fts_path = ptr;
|
---|
| 1317 | } else {
|
---|
| 1318 | free(sp->fts_path);
|
---|
| 1319 | sp->fts_path = NULL;
|
---|
| 1320 | free(sp->fts_wcspath);
|
---|
| 1321 | sp->fts_wcspath = NULL;
|
---|
| 1322 | return 1;
|
---|
| 1323 | }
|
---|
[3000] | 1324 | }
|
---|
[2998] | 1325 |
|
---|
[3000] | 1326 | if (cwcmore) {
|
---|
[2998] | 1327 | sp->fts_cwcpath += cwcmore + 256;
|
---|
| 1328 | ptr = realloc(sp->fts_wcspath, sp->fts_cwcpath);
|
---|
| 1329 | if (ptr) {
|
---|
| 1330 | sp->fts_wcspath = ptr;
|
---|
| 1331 | } else {
|
---|
| 1332 | free(sp->fts_path);
|
---|
| 1333 | sp->fts_path = NULL;
|
---|
| 1334 | free(sp->fts_wcspath);
|
---|
| 1335 | sp->fts_wcspath = NULL;
|
---|
| 1336 | return 1;
|
---|
| 1337 | }
|
---|
[3000] | 1338 | }
|
---|
[2998] | 1339 | return 0;
|
---|
[2982] | 1340 | }
|
---|
| 1341 |
|
---|
| 1342 | /*
|
---|
| 1343 | * When the path is realloc'd, have to fix all of the pointers in structures
|
---|
| 1344 | * already returned.
|
---|
| 1345 | */
|
---|
| 1346 | static void
|
---|
| 1347 | fts_padjust(FTS *sp, FTSENT *head)
|
---|
| 1348 | {
|
---|
| 1349 | FTSENT *p;
|
---|
| 1350 | char *addr = sp->fts_path;
|
---|
| 1351 |
|
---|
| 1352 | #define ADJUST(p) do { \
|
---|
| 1353 | if ((p)->fts_accpath != (p)->fts_name) { \
|
---|
| 1354 | (p)->fts_accpath = \
|
---|
| 1355 | (char *)addr + ((p)->fts_accpath - (p)->fts_path); \
|
---|
| 1356 | } \
|
---|
| 1357 | (p)->fts_path = addr; \
|
---|
| 1358 | } while (0)
|
---|
| 1359 | /* Adjust the current set of children. */
|
---|
| 1360 | for (p = sp->fts_child; p; p = p->fts_link)
|
---|
| 1361 | ADJUST(p);
|
---|
| 1362 |
|
---|
| 1363 | /* Adjust the rest of the tree, including the current level. */
|
---|
| 1364 | for (p = head; p->fts_level >= FTS_ROOTLEVEL;) {
|
---|
| 1365 | ADJUST(p);
|
---|
| 1366 | p = p->fts_link ? p->fts_link : p->fts_parent;
|
---|
| 1367 | }
|
---|
| 1368 | }
|
---|
| 1369 |
|
---|
[2998] | 1370 | /*
|
---|
| 1371 | * When the UTF-16 path is realloc'd, have to fix all of the pointers in
|
---|
| 1372 | * structures already returned.
|
---|
| 1373 | */
|
---|
| 1374 | static void
|
---|
| 1375 | fts_padjustw(FTS *sp, FTSENT *head)
|
---|
| 1376 | {
|
---|
| 1377 | FTSENT *p;
|
---|
| 1378 | wchar_t *addr = sp->fts_wcspath;
|
---|
| 1379 |
|
---|
| 1380 | #define ADJUSTW(p) \
|
---|
| 1381 | do { \
|
---|
| 1382 | if ((p)->fts_wcsaccpath != (p)->fts_wcsname) \
|
---|
| 1383 | (p)->fts_wcsaccpath = addr + ((p)->fts_wcsaccpath - (p)->fts_wcspath); \
|
---|
| 1384 | (p)->fts_wcspath = addr; \
|
---|
| 1385 | } while (0)
|
---|
| 1386 |
|
---|
| 1387 | /* Adjust the current set of children. */
|
---|
| 1388 | for (p = sp->fts_child; p; p = p->fts_link)
|
---|
| 1389 | ADJUSTW(p);
|
---|
| 1390 |
|
---|
| 1391 | /* Adjust the rest of the tree, including the current level. */
|
---|
| 1392 | for (p = head; p->fts_level >= FTS_ROOTLEVEL;) {
|
---|
| 1393 | ADJUSTW(p);
|
---|
| 1394 | p = p->fts_link ? p->fts_link : p->fts_parent;
|
---|
| 1395 | }
|
---|
| 1396 | }
|
---|
| 1397 |
|
---|
[2982] | 1398 | static size_t
|
---|
| 1399 | fts_maxarglen(char * const *argv)
|
---|
| 1400 | {
|
---|
| 1401 | size_t len, max;
|
---|
| 1402 |
|
---|
| 1403 | for (max = 0; *argv; ++argv)
|
---|
| 1404 | if ((len = strlen(*argv)) > max)
|
---|
| 1405 | max = len;
|
---|
| 1406 | return (max + 1);
|
---|
| 1407 | }
|
---|
| 1408 |
|
---|
[2998] | 1409 | /** Returns the max string size (including term). */
|
---|
| 1410 | static size_t
|
---|
| 1411 | fts_maxarglenw(wchar_t * const *argv)
|
---|
| 1412 | {
|
---|
| 1413 | size_t max = 0;
|
---|
| 1414 | for (; *argv; ++argv) {
|
---|
| 1415 | size_t len = wcslen(*argv);
|
---|
| 1416 | if (len > max)
|
---|
| 1417 | max = len;
|
---|
| 1418 | }
|
---|
| 1419 | return max + 1;
|
---|
| 1420 | }
|
---|
| 1421 |
|
---|