VirtualBox

source: kBuild/trunk/src/lib/nt/fts-nt.c@ 3004

Last change on this file since 3004 was 3004, checked in by bird, 8 years ago

fts-nt.c: Wide char support, part 3.

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette