1 | /* $Id: fts-nt.c 3002 2016-11-05 20:04:45Z 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 FTSENT *fts_build(FTS *, int);
|
---|
93 | static void fts_lfree(FTSENT *);
|
---|
94 | static void fts_load(FTS *, FTSENT *);
|
---|
95 | static size_t fts_maxarglen(char * const *);
|
---|
96 | static size_t fts_maxarglenw(wchar_t * const *);
|
---|
97 | static void fts_padjust(FTS *, FTSENT *);
|
---|
98 | static void fts_padjustw(FTS *, FTSENT *);
|
---|
99 | static int fts_palloc(FTS *, size_t, size_t);
|
---|
100 | static FTSENT *fts_sort(FTS *, FTSENT *, size_t);
|
---|
101 | static int fts_stat(FTS *, FTSENT *, int, HANDLE);
|
---|
102 | static 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 | */
|
---|
126 | struct _fts_private {
|
---|
127 | FTS ftsp_fts;
|
---|
128 | };
|
---|
129 |
|
---|
130 |
|
---|
131 | static FTS * FTSCALL
|
---|
132 | nt_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 |
|
---|
270 | mem3:
|
---|
271 | fts_lfree(root);
|
---|
272 | free(parent);
|
---|
273 | mem2:
|
---|
274 | free(sp->fts_path);
|
---|
275 | free(sp->fts_wcspath);
|
---|
276 | mem1:
|
---|
277 | free(sp);
|
---|
278 | return (NULL);
|
---|
279 | }
|
---|
280 |
|
---|
281 |
|
---|
282 | FTS * FTSCALL
|
---|
283 | nt_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 |
|
---|
290 | FTS * FTSCALL
|
---|
291 | nt_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 | */
|
---|
301 | static void
|
---|
302 | fts_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 |
|
---|
340 | int FTSCALL
|
---|
341 | nt_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 |
|
---|
380 | static void
|
---|
381 | fts_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 |
|
---|
392 | FTSENT * FTSCALL
|
---|
393 | nt_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. */
|
---|
484 | next: 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 |
|
---|
517 | name:
|
---|
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 */
|
---|
574 | int FTSCALL
|
---|
575 | nt_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 |
|
---|
586 | FTSENT * FTSCALL
|
---|
587 | nt_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 |
|
---|
643 | void *
|
---|
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 |
|
---|
654 | FTS *
|
---|
655 | (FTSCALL fts_get_stream)(FTSENT *p)
|
---|
656 | {
|
---|
657 | return (fts_get_stream(p));
|
---|
658 | }
|
---|
659 |
|
---|
660 | void FTSCALL
|
---|
661 | nt_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 | */
|
---|
684 | static FTSENT *
|
---|
685 | fts_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) {
|
---|
737 | l_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 (sp->fts_options & FTS_NO_ANSI) {
|
---|
753 | len = maxlen = 0;
|
---|
754 | } else {
|
---|
755 | len = NAPPEND(cur);
|
---|
756 | len++;
|
---|
757 | maxlen = sp->fts_pathlen - len;
|
---|
758 | }
|
---|
759 |
|
---|
760 | cwcdir = NAPPENDW(cur);
|
---|
761 | cwcdir++;
|
---|
762 | cwcmax = sp->fts_cwcpath - len;
|
---|
763 |
|
---|
764 | level = cur->fts_level + 1;
|
---|
765 |
|
---|
766 | /* Read the directory, attaching each entry to the `link' pointer. */
|
---|
767 | doadjust = doadjust_utf16 = 0;
|
---|
768 | for (head = tail = NULL, nitems = 0; dirp && (dp = birdDirRead(dirp));) {
|
---|
769 | if (!ISSET(FTS_SEEDOT) && ISDOT(dp->d_name))
|
---|
770 | continue;
|
---|
771 |
|
---|
772 | if ((p = fts_alloc_ansi(sp, dp->d_name, dp->d_namlen)) == NULL)
|
---|
773 | goto mem1;
|
---|
774 |
|
---|
775 | if (p->fts_namelen >= maxlen
|
---|
776 | || p->fts_cwcname >= cwcmax) { /* include space for NUL */
|
---|
777 | void *oldaddr = sp->fts_path;
|
---|
778 | wchar_t *oldwcspath = sp->fts_wcspath;
|
---|
779 | if (fts_palloc(sp,
|
---|
780 | p->fts_namelen >= maxlen ? len + p->fts_namelen + 1 : 0,
|
---|
781 | p->fts_cwcname >= cwcmax ? cwcdir + p->fts_cwcname + 1 : 0)) {
|
---|
782 | /*
|
---|
783 | * No more memory for path or structures. Save
|
---|
784 | * errno, free up the current structure and the
|
---|
785 | * structures already allocated.
|
---|
786 | */
|
---|
787 | mem1: saved_errno = errno;
|
---|
788 | if (p)
|
---|
789 | free(p);
|
---|
790 | fts_lfree(head);
|
---|
791 | birdDirClose(dirp);
|
---|
792 | birdCloseFile(cur->fts_dirfd);
|
---|
793 | cur->fts_dirfd = INVALID_HANDLE_VALUE;
|
---|
794 | cur->fts_info = FTS_ERR;
|
---|
795 | SET(FTS_STOP);
|
---|
796 | errno = saved_errno;
|
---|
797 | return (NULL);
|
---|
798 | }
|
---|
799 | /* Did realloc() change the pointer? */
|
---|
800 | doadjust |= oldaddr != sp->fts_path;
|
---|
801 | doadjust_utf16 |= oldwcspath != sp->fts_wcspath;
|
---|
802 | maxlen = sp->fts_pathlen - len;
|
---|
803 | cwcmax = sp->fts_cwcpath - cwcdir;
|
---|
804 | }
|
---|
805 |
|
---|
806 | p->fts_level = level;
|
---|
807 | p->fts_parent = sp->fts_cur;
|
---|
808 | p->fts_pathlen = len + p->fts_namelen;
|
---|
809 | p->fts_cwcpath = cwcdir + p->fts_cwcname;
|
---|
810 | p->fts_accpath = p->fts_path;
|
---|
811 | p->fts_wcsaccpath = p->fts_wcspath;
|
---|
812 | p->fts_stat = dp->d_stat;
|
---|
813 | p->fts_info = fts_process_stats(p, &dp->d_stat);
|
---|
814 |
|
---|
815 | /* We walk in directory order so "ls -f" doesn't get upset. */
|
---|
816 | p->fts_link = NULL;
|
---|
817 | if (head == NULL)
|
---|
818 | head = tail = p;
|
---|
819 | else {
|
---|
820 | tail->fts_link = p;
|
---|
821 | tail = p;
|
---|
822 | }
|
---|
823 | ++nitems;
|
---|
824 | }
|
---|
825 |
|
---|
826 | birdDirClose(dirp);
|
---|
827 |
|
---|
828 | /*
|
---|
829 | * If realloc() changed the address of the path, adjust the
|
---|
830 | * addresses for the rest of the tree and the dir list.
|
---|
831 | */
|
---|
832 | if (doadjust)
|
---|
833 | fts_padjust(sp, head);
|
---|
834 | if (doadjust_utf16)
|
---|
835 | fts_padjustw(sp, head);
|
---|
836 |
|
---|
837 | /* If didn't find anything, return NULL. */
|
---|
838 | if (!nitems) {
|
---|
839 | if (type == BREAD)
|
---|
840 | cur->fts_info = FTS_DP;
|
---|
841 | return (NULL);
|
---|
842 | }
|
---|
843 |
|
---|
844 | /* Sort the entries. */
|
---|
845 | if (sp->fts_compar && nitems > 1)
|
---|
846 | head = fts_sort(sp, head, nitems);
|
---|
847 | return (head);
|
---|
848 | }
|
---|
849 |
|
---|
850 |
|
---|
851 | /**
|
---|
852 | * @note Only used on NT with input arguments, FTS_AGAIN, and links that needs
|
---|
853 | * following. On link information is generally retrieved during directory
|
---|
854 | * enumeration on NT, in line with it's DOS/OS2/FAT API heritage.
|
---|
855 | */
|
---|
856 | static int
|
---|
857 | fts_stat(FTS *sp, FTSENT *p, int follow, HANDLE dfd)
|
---|
858 | {
|
---|
859 | int saved_errno;
|
---|
860 | const wchar_t *wcspath;
|
---|
861 |
|
---|
862 | if (dfd == INVALID_HANDLE_VALUE) {
|
---|
863 | wcspath = p->fts_wcsaccpath;
|
---|
864 | } else {
|
---|
865 | wcspath = p->fts_wcsname;
|
---|
866 | }
|
---|
867 |
|
---|
868 | /*
|
---|
869 | * If doing a logical walk, or application requested FTS_FOLLOW, do
|
---|
870 | * a stat(2). If that fails, check for a non-existent symlink. If
|
---|
871 | * fail, set the errno from the stat call.
|
---|
872 | */
|
---|
873 | if (ISSET(FTS_LOGICAL) || follow) {
|
---|
874 | if (birdStatAtW(dfd, wcspath, &p->fts_stat, 1 /*fFollowLink*/)) {
|
---|
875 | saved_errno = errno;
|
---|
876 | if (birdStatAtW(dfd, wcspath, &p->fts_stat, 0 /*fFollowLink*/)) {
|
---|
877 | p->fts_errno = saved_errno;
|
---|
878 | goto err;
|
---|
879 | }
|
---|
880 | errno = 0;
|
---|
881 | if (S_ISLNK(p->fts_stat.st_mode))
|
---|
882 | return (FTS_SLNONE);
|
---|
883 | }
|
---|
884 | } else if (birdStatAtW(dfd, wcspath, &p->fts_stat, 0 /*fFollowLink*/)) {
|
---|
885 | p->fts_errno = errno;
|
---|
886 | err: memset(&p->fts_stat, 0, sizeof(struct stat));
|
---|
887 | return (FTS_NS);
|
---|
888 | }
|
---|
889 | return fts_process_stats(p, &p->fts_stat);
|
---|
890 | }
|
---|
891 |
|
---|
892 | /* Shared between fts_stat and fts_build. */
|
---|
893 | static int
|
---|
894 | fts_process_stats(FTSENT *p, BirdStat_T const *sbp)
|
---|
895 | {
|
---|
896 | if (S_ISDIR(sbp->st_mode)) {
|
---|
897 | FTSENT *t;
|
---|
898 | fts_dev_t dev;
|
---|
899 | fts_ino_t ino;
|
---|
900 |
|
---|
901 | /*
|
---|
902 | * Set the device/inode. Used to find cycles and check for
|
---|
903 | * crossing mount points. Also remember the link count, used
|
---|
904 | * in fts_build to limit the number of stat calls. It is
|
---|
905 | * understood that these fields are only referenced if fts_info
|
---|
906 | * is set to FTS_D.
|
---|
907 | */
|
---|
908 | dev = p->fts_dev = sbp->st_dev;
|
---|
909 | ino = p->fts_ino = sbp->st_ino;
|
---|
910 | p->fts_nlink = sbp->st_nlink;
|
---|
911 |
|
---|
912 | if (ISDOT(p->fts_name))
|
---|
913 | return (FTS_DOT);
|
---|
914 |
|
---|
915 | /*
|
---|
916 | * Cycle detection is done by brute force when the directory
|
---|
917 | * is first encountered. If the tree gets deep enough or the
|
---|
918 | * number of symbolic links to directories is high enough,
|
---|
919 | * something faster might be worthwhile.
|
---|
920 | */
|
---|
921 | for (t = p->fts_parent;
|
---|
922 | t->fts_level >= FTS_ROOTLEVEL; t = t->fts_parent)
|
---|
923 | if (ino == t->fts_ino && dev == t->fts_dev) {
|
---|
924 | p->fts_cycle = t;
|
---|
925 | return (FTS_DC);
|
---|
926 | }
|
---|
927 | return (FTS_D);
|
---|
928 | }
|
---|
929 | if (S_ISLNK(sbp->st_mode))
|
---|
930 | return (FTS_SL);
|
---|
931 | if (S_ISREG(sbp->st_mode))
|
---|
932 | return (FTS_F);
|
---|
933 | return (FTS_DEFAULT);
|
---|
934 | }
|
---|
935 |
|
---|
936 | /*
|
---|
937 | * The comparison function takes pointers to pointers to FTSENT structures.
|
---|
938 | * Qsort wants a comparison function that takes pointers to void.
|
---|
939 | * (Both with appropriate levels of const-poisoning, of course!)
|
---|
940 | * Use a trampoline function to deal with the difference.
|
---|
941 | */
|
---|
942 | static int
|
---|
943 | fts_compar(const void *a, const void *b)
|
---|
944 | {
|
---|
945 | FTS *parent;
|
---|
946 |
|
---|
947 | parent = (*(const FTSENT * const *)a)->fts_fts;
|
---|
948 | return (*parent->fts_compar)(a, b);
|
---|
949 | }
|
---|
950 |
|
---|
951 | static FTSENT *
|
---|
952 | fts_sort(FTS *sp, FTSENT *head, size_t nitems)
|
---|
953 | {
|
---|
954 | FTSENT **ap, *p;
|
---|
955 |
|
---|
956 | /*
|
---|
957 | * Construct an array of pointers to the structures and call qsort(3).
|
---|
958 | * Reassemble the array in the order returned by qsort. If unable to
|
---|
959 | * sort for memory reasons, return the directory entries in their
|
---|
960 | * current order. Allocate enough space for the current needs plus
|
---|
961 | * 40 so don't realloc one entry at a time.
|
---|
962 | */
|
---|
963 | if (nitems > sp->fts_nitems) {
|
---|
964 | void *ptr;
|
---|
965 | sp->fts_nitems = nitems + 40;
|
---|
966 | ptr = realloc(sp->fts_array, sp->fts_nitems * sizeof(FTSENT *));
|
---|
967 | if (ptr != NULL) {
|
---|
968 | sp->fts_array = ptr;
|
---|
969 | } else {
|
---|
970 | free(sp->fts_array);
|
---|
971 | sp->fts_array = NULL;
|
---|
972 | sp->fts_nitems = 0;
|
---|
973 | return (head);
|
---|
974 | }
|
---|
975 | }
|
---|
976 | for (ap = sp->fts_array, p = head; p; p = p->fts_link)
|
---|
977 | *ap++ = p;
|
---|
978 | qsort(sp->fts_array, nitems, sizeof(FTSENT *), fts_compar);
|
---|
979 | for (head = *(ap = sp->fts_array); --nitems; ++ap)
|
---|
980 | ap[0]->fts_link = ap[1];
|
---|
981 | ap[0]->fts_link = NULL;
|
---|
982 | return (head);
|
---|
983 | }
|
---|
984 |
|
---|
985 | static FTSENT *
|
---|
986 | fts_alloc(FTS *sp, char const *name, size_t namelen, wchar_t const *wcsname, size_t cwcname)
|
---|
987 | {
|
---|
988 | FTSENT *p;
|
---|
989 | size_t len;
|
---|
990 |
|
---|
991 | /*
|
---|
992 | * The file name is a variable length array. Allocate the FTSENT
|
---|
993 | * structure and the file name.
|
---|
994 | */
|
---|
995 | len = sizeof(FTSENT) + (cwcname + 1) * sizeof(wchar_t);
|
---|
996 | if (!(sp->fts_options & FTS_NO_ANSI))
|
---|
997 | len += namelen + 1;
|
---|
998 | p = malloc(len);
|
---|
999 | if (p) {
|
---|
1000 | /* Copy the names and guarantee NUL termination. */
|
---|
1001 | p->fts_wcsname = (wchar_t *)(p + 1);
|
---|
1002 | memcpy(p->fts_wcsname, wcsname, cwcname * sizeof(wchar_t));
|
---|
1003 | p->fts_wcsname[cwcname] = '\0';
|
---|
1004 | p->fts_cwcname = cwcname;
|
---|
1005 | if (!(sp->fts_options & FTS_NO_ANSI)) {
|
---|
1006 | p->fts_name = (char *)(p->fts_wcsname + cwcname + 1);
|
---|
1007 | memcpy(p->fts_name, name, namelen);
|
---|
1008 | p->fts_name[namelen] = '\0';
|
---|
1009 | p->fts_namelen = namelen;
|
---|
1010 | } else {
|
---|
1011 | p->fts_name = NULL;
|
---|
1012 | p->fts_namelen = 0;
|
---|
1013 | }
|
---|
1014 |
|
---|
1015 | p->fts_path = sp->fts_path;
|
---|
1016 | p->fts_wcspath = sp->fts_wcspath;
|
---|
1017 | p->fts_statp = &p->fts_stat;
|
---|
1018 | p->fts_errno = 0;
|
---|
1019 | p->fts_flags = 0;
|
---|
1020 | p->fts_instr = FTS_NOINSTR;
|
---|
1021 | p->fts_number = 0;
|
---|
1022 | p->fts_pointer = NULL;
|
---|
1023 | p->fts_fts = sp;
|
---|
1024 | p->fts_symfd = INVALID_HANDLE_VALUE;
|
---|
1025 | p->fts_dirfd = INVALID_HANDLE_VALUE;
|
---|
1026 | }
|
---|
1027 | return (p);
|
---|
1028 | }
|
---|
1029 |
|
---|
1030 |
|
---|
1031 | /**
|
---|
1032 | * Converts the ANSI name to UTF-16 and calls fts_alloc.
|
---|
1033 | *
|
---|
1034 | * @returns Pointer to allocated and mostly initialized FTSENT structure on
|
---|
1035 | * success. NULL on failure, caller needs to record it.
|
---|
1036 | * @param sp Pointer to FTS instance.
|
---|
1037 | * @param name The ANSI name.
|
---|
1038 | * @param namelen The ANSI name length.
|
---|
1039 | */
|
---|
1040 | static FTSENT *
|
---|
1041 | fts_alloc_ansi(FTS *sp, char const *name, size_t namelen)
|
---|
1042 | {
|
---|
1043 | MY_UNICODE_STRING UniStr;
|
---|
1044 | MY_ANSI_STRING AnsiStr;
|
---|
1045 | MY_NTSTATUS rcNt;
|
---|
1046 | FTSENT *pRet;
|
---|
1047 |
|
---|
1048 | UniStr.Buffer = NULL;
|
---|
1049 | UniStr.MaximumLength = UniStr.Length = 0;
|
---|
1050 |
|
---|
1051 | AnsiStr.Buffer = (char *)name;
|
---|
1052 | AnsiStr.Length = AnsiStr.MaximumLength = (USHORT)namelen;
|
---|
1053 |
|
---|
1054 | rcNt = g_pfnRtlAnsiStringToUnicodeString(&UniStr, &AnsiStr, TRUE /*fAllocate*/);
|
---|
1055 | if (NT_SUCCESS(rcNt)) {
|
---|
1056 | pRet = fts_alloc(sp, name, namelen, UniStr.Buffer, UniStr.Length / sizeof(wchar_t));
|
---|
1057 | HeapFree(GetProcessHeap(), 0, UniStr.Buffer);
|
---|
1058 | } else {
|
---|
1059 | pRet = NULL;
|
---|
1060 | }
|
---|
1061 | return pRet;
|
---|
1062 | }
|
---|
1063 |
|
---|
1064 |
|
---|
1065 | /**
|
---|
1066 | * Converts the UTF-16 name to ANSI (if necessary) and calls fts_alloc.
|
---|
1067 | *
|
---|
1068 | * @returns Pointer to allocated and mostly initialized FTSENT structure on
|
---|
1069 | * success. NULL on failure, caller needs to record it.
|
---|
1070 | * @param sp Pointer to FTS instance.
|
---|
1071 | * @param wcsname The UTF-16 name.
|
---|
1072 | * @param cwcname The UTF-16 name length.
|
---|
1073 | */
|
---|
1074 | static FTSENT *
|
---|
1075 | fts_alloc_utf16(FTS *sp, wchar_t const *wcsname, size_t cwcname)
|
---|
1076 | {
|
---|
1077 | FTSENT *pRet;
|
---|
1078 |
|
---|
1079 | if (sp->fts_options & FTS_NO_ANSI) {
|
---|
1080 | pRet = fts_alloc(sp, NULL, 0, wcsname, cwcname);
|
---|
1081 | } else {
|
---|
1082 | MY_UNICODE_STRING UniStr;
|
---|
1083 | MY_ANSI_STRING AnsiStr;
|
---|
1084 | MY_NTSTATUS rcNt;
|
---|
1085 |
|
---|
1086 | UniStr.Buffer = (wchar_t *)wcsname;
|
---|
1087 | UniStr.MaximumLength = UniStr.Length = (USHORT)(cwcname * sizeof(wchar_t));
|
---|
1088 |
|
---|
1089 | AnsiStr.Buffer = NULL;
|
---|
1090 | AnsiStr.Length = AnsiStr.MaximumLength = 0;
|
---|
1091 |
|
---|
1092 | rcNt = g_pfnRtlUnicodeStringToAnsiString(&AnsiStr, &UniStr, TRUE /*fAllocate*/);
|
---|
1093 | if (NT_SUCCESS(rcNt)) {
|
---|
1094 | pRet = fts_alloc(sp, AnsiStr.Buffer, AnsiStr.Length, wcsname, cwcname);
|
---|
1095 | HeapFree(GetProcessHeap(), 0, AnsiStr.Buffer);
|
---|
1096 | } else {
|
---|
1097 | pRet = NULL;
|
---|
1098 | }
|
---|
1099 | }
|
---|
1100 | return pRet;
|
---|
1101 | }
|
---|
1102 |
|
---|
1103 |
|
---|
1104 | static void
|
---|
1105 | fts_lfree(FTSENT *head)
|
---|
1106 | {
|
---|
1107 | FTSENT *p;
|
---|
1108 |
|
---|
1109 | /* Free a linked list of structures. */
|
---|
1110 | while ((p = head)) {
|
---|
1111 | head = head->fts_link;
|
---|
1112 | assert(p->fts_dirfd == INVALID_HANDLE_VALUE);
|
---|
1113 | free(p);
|
---|
1114 | }
|
---|
1115 | }
|
---|
1116 |
|
---|
1117 | /*
|
---|
1118 | * Allow essentially unlimited paths; find, rm, ls should all work on any tree.
|
---|
1119 | * Most systems will allow creation of paths much longer than MAXPATHLEN, even
|
---|
1120 | * though the kernel won't resolve them. Add the size (not just what's needed)
|
---|
1121 | * plus 256 bytes so don't realloc the path 2 bytes at a time.
|
---|
1122 | */
|
---|
1123 | static int
|
---|
1124 | fts_palloc(FTS *sp, size_t more, size_t cwcmore)
|
---|
1125 | {
|
---|
1126 | void *ptr;
|
---|
1127 |
|
---|
1128 | /** @todo Isn't more and cwcmore minimum buffer sizes rather than what needs
|
---|
1129 | * to be added to the buffer?? This code makes no sense when looking at
|
---|
1130 | * the way the caller checks things out! */
|
---|
1131 |
|
---|
1132 | if (more) {
|
---|
1133 | sp->fts_pathlen += more + 256;
|
---|
1134 | ptr = realloc(sp->fts_path, sp->fts_pathlen);
|
---|
1135 | if (ptr) {
|
---|
1136 | sp->fts_path = ptr;
|
---|
1137 | } else {
|
---|
1138 | free(sp->fts_path);
|
---|
1139 | sp->fts_path = NULL;
|
---|
1140 | free(sp->fts_wcspath);
|
---|
1141 | sp->fts_wcspath = NULL;
|
---|
1142 | return 1;
|
---|
1143 | }
|
---|
1144 | }
|
---|
1145 |
|
---|
1146 | if (cwcmore) {
|
---|
1147 | sp->fts_cwcpath += cwcmore + 256;
|
---|
1148 | ptr = realloc(sp->fts_wcspath, sp->fts_cwcpath);
|
---|
1149 | if (ptr) {
|
---|
1150 | sp->fts_wcspath = ptr;
|
---|
1151 | } else {
|
---|
1152 | free(sp->fts_path);
|
---|
1153 | sp->fts_path = NULL;
|
---|
1154 | free(sp->fts_wcspath);
|
---|
1155 | sp->fts_wcspath = NULL;
|
---|
1156 | return 1;
|
---|
1157 | }
|
---|
1158 | }
|
---|
1159 | return 0;
|
---|
1160 | }
|
---|
1161 |
|
---|
1162 | /*
|
---|
1163 | * When the path is realloc'd, have to fix all of the pointers in structures
|
---|
1164 | * already returned.
|
---|
1165 | */
|
---|
1166 | static void
|
---|
1167 | fts_padjust(FTS *sp, FTSENT *head)
|
---|
1168 | {
|
---|
1169 | FTSENT *p;
|
---|
1170 | char *addr = sp->fts_path;
|
---|
1171 |
|
---|
1172 | #define ADJUST(p) do { \
|
---|
1173 | if ((p)->fts_accpath != (p)->fts_name) { \
|
---|
1174 | (p)->fts_accpath = \
|
---|
1175 | (char *)addr + ((p)->fts_accpath - (p)->fts_path); \
|
---|
1176 | } \
|
---|
1177 | (p)->fts_path = addr; \
|
---|
1178 | } while (0)
|
---|
1179 | /* Adjust the current set of children. */
|
---|
1180 | for (p = sp->fts_child; p; p = p->fts_link)
|
---|
1181 | ADJUST(p);
|
---|
1182 |
|
---|
1183 | /* Adjust the rest of the tree, including the current level. */
|
---|
1184 | for (p = head; p->fts_level >= FTS_ROOTLEVEL;) {
|
---|
1185 | ADJUST(p);
|
---|
1186 | p = p->fts_link ? p->fts_link : p->fts_parent;
|
---|
1187 | }
|
---|
1188 | }
|
---|
1189 |
|
---|
1190 | /*
|
---|
1191 | * When the UTF-16 path is realloc'd, have to fix all of the pointers in
|
---|
1192 | * structures already returned.
|
---|
1193 | */
|
---|
1194 | static void
|
---|
1195 | fts_padjustw(FTS *sp, FTSENT *head)
|
---|
1196 | {
|
---|
1197 | FTSENT *p;
|
---|
1198 | wchar_t *addr = sp->fts_wcspath;
|
---|
1199 |
|
---|
1200 | #define ADJUSTW(p) \
|
---|
1201 | do { \
|
---|
1202 | if ((p)->fts_wcsaccpath != (p)->fts_wcsname) \
|
---|
1203 | (p)->fts_wcsaccpath = addr + ((p)->fts_wcsaccpath - (p)->fts_wcspath); \
|
---|
1204 | (p)->fts_wcspath = addr; \
|
---|
1205 | } while (0)
|
---|
1206 |
|
---|
1207 | /* Adjust the current set of children. */
|
---|
1208 | for (p = sp->fts_child; p; p = p->fts_link)
|
---|
1209 | ADJUSTW(p);
|
---|
1210 |
|
---|
1211 | /* Adjust the rest of the tree, including the current level. */
|
---|
1212 | for (p = head; p->fts_level >= FTS_ROOTLEVEL;) {
|
---|
1213 | ADJUSTW(p);
|
---|
1214 | p = p->fts_link ? p->fts_link : p->fts_parent;
|
---|
1215 | }
|
---|
1216 | }
|
---|
1217 |
|
---|
1218 | static size_t
|
---|
1219 | fts_maxarglen(char * const *argv)
|
---|
1220 | {
|
---|
1221 | size_t len, max;
|
---|
1222 |
|
---|
1223 | for (max = 0; *argv; ++argv)
|
---|
1224 | if ((len = strlen(*argv)) > max)
|
---|
1225 | max = len;
|
---|
1226 | return (max + 1);
|
---|
1227 | }
|
---|
1228 |
|
---|
1229 | /** Returns the max string size (including term). */
|
---|
1230 | static size_t
|
---|
1231 | fts_maxarglenw(wchar_t * const *argv)
|
---|
1232 | {
|
---|
1233 | size_t max = 0;
|
---|
1234 | for (; *argv; ++argv) {
|
---|
1235 | size_t len = wcslen(*argv);
|
---|
1236 | if (len > max)
|
---|
1237 | max = len;
|
---|
1238 | }
|
---|
1239 | return max + 1;
|
---|
1240 | }
|
---|
1241 |
|
---|