VirtualBox

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

Last change on this file since 3001 was 3001, checked in by bird, 9 years ago

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

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