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