1 | /* $NetBSD: fts.c,v 1.26 2005/10/22 20:55:13 christos Exp $ */ |
---|
2 | |
---|
3 | /*- |
---|
4 | * Copyright (c) 1990, 1993, 1994 |
---|
5 | * The Regents of the University of California. All rights reserved. |
---|
6 | * |
---|
7 | * Redistribution and use in source and binary forms, with or without |
---|
8 | * modification, are permitted provided that the following conditions |
---|
9 | * are met: |
---|
10 | * 1. Redistributions of source code must retain the above copyright |
---|
11 | * notice, this list of conditions and the following disclaimer. |
---|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
13 | * notice, this list of conditions and the following disclaimer in the |
---|
14 | * documentation and/or other materials provided with the distribution. |
---|
15 | * 3. Neither the name of the University nor the names of its contributors |
---|
16 | * may be used to endorse or promote products derived from this software |
---|
17 | * without specific prior written permission. |
---|
18 | * |
---|
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
---|
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
---|
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
29 | * SUCH DAMAGE. |
---|
30 | */ |
---|
31 | |
---|
32 | #ifdef HAVE_CONFIG_H |
---|
33 | #include "config.h" |
---|
34 | #endif |
---|
35 | |
---|
36 | #if HAVE_NBTOOL_CONFIG_H |
---|
37 | #include "nbtool_config.h" |
---|
38 | #endif |
---|
39 | |
---|
40 | #include <sys/cdefs.h> |
---|
41 | #if defined(LIBC_SCCS) && !defined(lint) |
---|
42 | #if 0 |
---|
43 | static char sccsid[] = "@(#)fts.c 8.6 (Berkeley) 8/14/94"; |
---|
44 | #else |
---|
45 | __RCSID("$NetBSD: fts.c,v 1.26 2005/10/22 20:55:13 christos Exp $"); |
---|
46 | #endif |
---|
47 | #endif /* LIBC_SCCS and not lint */ |
---|
48 | |
---|
49 | #include <sys/param.h> |
---|
50 | #include <sys/stat.h> |
---|
51 | |
---|
52 | #include <assert.h> |
---|
53 | #include <dirent.h> |
---|
54 | #include <errno.h> |
---|
55 | #include <fcntl.h> |
---|
56 | #include <fts.h> |
---|
57 | #include <stdlib.h> |
---|
58 | #include <string.h> |
---|
59 | #include <unistd.h> |
---|
60 | |
---|
61 | #define _DIAGASSERT(a) |
---|
62 | #undef FTS_WHITEOUT |
---|
63 | #define dirfd(dp) __dirfd(dp) |
---|
64 | |
---|
65 | #if ! HAVE_NBTOOL_CONFIG_H |
---|
66 | #define HAVE_STRUCT_DIRENT_D_NAMLEN 1 |
---|
67 | #endif |
---|
68 | |
---|
69 | static FTSENT *fts_alloc __P((FTS *, const char *, size_t)); |
---|
70 | static FTSENT *fts_build __P((FTS *, int)); |
---|
71 | static void fts_lfree __P((FTSENT *)); |
---|
72 | static void fts_load __P((FTS *, FTSENT *)); |
---|
73 | static size_t fts_maxarglen __P((char * const *)); |
---|
74 | static size_t fts_pow2 __P((size_t)); |
---|
75 | static int fts_palloc __P((FTS *, size_t)); |
---|
76 | static void fts_padjust __P((FTS *, FTSENT *)); |
---|
77 | static FTSENT *fts_sort __P((FTS *, FTSENT *, size_t)); |
---|
78 | static u_short fts_stat __P((FTS *, FTSENT *, int)); |
---|
79 | static int fts_safe_changedir __P((const FTS *, const FTSENT *, int, |
---|
80 | const char *)); |
---|
81 | |
---|
82 | #define ISDOT(a) (a[0] == '.' && (!a[1] || (a[1] == '.' && !a[2]))) |
---|
83 | |
---|
84 | #define CLR(opt) (sp->fts_options &= ~(opt)) |
---|
85 | #define ISSET(opt) (sp->fts_options & (opt)) |
---|
86 | #define SET(opt) (sp->fts_options |= (opt)) |
---|
87 | |
---|
88 | #define CHDIR(sp, path) (!ISSET(FTS_NOCHDIR) && chdir(path)) |
---|
89 | #define FCHDIR(sp, fd) (!ISSET(FTS_NOCHDIR) && fchdir(fd)) |
---|
90 | |
---|
91 | /* fts_build flags */ |
---|
92 | #define BCHILD 1 /* fts_children */ |
---|
93 | #define BNAMES 2 /* fts_children, names only */ |
---|
94 | #define BREAD 3 /* fts_read */ |
---|
95 | |
---|
96 | #ifndef DTF_HIDEW |
---|
97 | #undef FTS_WHITEOUT |
---|
98 | #endif |
---|
99 | |
---|
100 | FTS * |
---|
101 | fts_open( |
---|
102 | char * const *argv, |
---|
103 | int options, |
---|
104 | int (*compar) (const FTSENT **, const FTSENT **) ) |
---|
105 | { |
---|
106 | FTS *sp; |
---|
107 | FTSENT *p, *root; |
---|
108 | size_t nitems; |
---|
109 | FTSENT *parent, *tmp = NULL; /* pacify gcc */ |
---|
110 | size_t len; |
---|
111 | |
---|
112 | _DIAGASSERT(argv != NULL); |
---|
113 | |
---|
114 | /* Options check. */ |
---|
115 | if (options & ~FTS_OPTIONMASK) { |
---|
116 | errno = EINVAL; |
---|
117 | return (NULL); |
---|
118 | } |
---|
119 | |
---|
120 | /* Allocate/initialize the stream */ |
---|
121 | if ((sp = malloc((u_int)sizeof(FTS))) == NULL) |
---|
122 | return (NULL); |
---|
123 | memset(sp, 0, sizeof(FTS)); |
---|
124 | sp->fts_compar = compar; |
---|
125 | sp->fts_options = options; |
---|
126 | |
---|
127 | /* Logical walks turn on NOCHDIR; symbolic links are too hard. */ |
---|
128 | if (ISSET(FTS_LOGICAL)) |
---|
129 | SET(FTS_NOCHDIR); |
---|
130 | |
---|
131 | /* |
---|
132 | * Start out with 1K of path space, and enough, in any case, |
---|
133 | * to hold the user's paths. |
---|
134 | */ |
---|
135 | if (fts_palloc(sp, MAX(fts_maxarglen(argv), MAXPATHLEN))) |
---|
136 | goto mem1; |
---|
137 | |
---|
138 | /* Allocate/initialize root's parent. */ |
---|
139 | if ((parent = fts_alloc(sp, "", 0)) == NULL) |
---|
140 | goto mem2; |
---|
141 | parent->fts_level = FTS_ROOTPARENTLEVEL; |
---|
142 | |
---|
143 | /* Allocate/initialize root(s). */ |
---|
144 | for (root = NULL, nitems = 0; *argv; ++argv, ++nitems) { |
---|
145 | /* Don't allow zero-length paths. */ |
---|
146 | if ((len = strlen(*argv)) == 0) { |
---|
147 | errno = ENOENT; |
---|
148 | goto mem3; |
---|
149 | } |
---|
150 | |
---|
151 | if ((p = fts_alloc(sp, *argv, len)) == NULL) |
---|
152 | goto mem3; |
---|
153 | p->fts_level = FTS_ROOTLEVEL; |
---|
154 | p->fts_parent = parent; |
---|
155 | p->fts_accpath = p->fts_name; |
---|
156 | p->fts_info = fts_stat(sp, p, ISSET(FTS_COMFOLLOW)); |
---|
157 | |
---|
158 | /* Command-line "." and ".." are real directories. */ |
---|
159 | if (p->fts_info == FTS_DOT) |
---|
160 | p->fts_info = FTS_D; |
---|
161 | |
---|
162 | /* |
---|
163 | * If comparison routine supplied, traverse in sorted |
---|
164 | * order; otherwise traverse in the order specified. |
---|
165 | */ |
---|
166 | if (compar) { |
---|
167 | p->fts_link = root; |
---|
168 | root = p; |
---|
169 | } else { |
---|
170 | p->fts_link = NULL; |
---|
171 | if (root == NULL) |
---|
172 | tmp = root = p; |
---|
173 | else { |
---|
174 | tmp->fts_link = p; |
---|
175 | tmp = p; |
---|
176 | } |
---|
177 | } |
---|
178 | } |
---|
179 | if (compar && nitems > 1) |
---|
180 | root = fts_sort(sp, root, nitems); |
---|
181 | |
---|
182 | /* |
---|
183 | * Allocate a dummy pointer and make fts_read think that we've just |
---|
184 | * finished the node before the root(s); set p->fts_info to FTS_INIT |
---|
185 | * so that everything about the "current" node is ignored. |
---|
186 | */ |
---|
187 | if ((sp->fts_cur = fts_alloc(sp, "", 0)) == NULL) |
---|
188 | goto mem3; |
---|
189 | sp->fts_cur->fts_link = root; |
---|
190 | sp->fts_cur->fts_info = FTS_INIT; |
---|
191 | |
---|
192 | /* |
---|
193 | * If using chdir(2), grab a file descriptor pointing to dot to insure |
---|
194 | * that we can get back here; this could be avoided for some paths, |
---|
195 | * but almost certainly not worth the effort. Slashes, symbolic links, |
---|
196 | * and ".." are all fairly nasty problems. Note, if we can't get the |
---|
197 | * descriptor we run anyway, just more slowly. |
---|
198 | */ |
---|
199 | if (!ISSET(FTS_NOCHDIR)) { |
---|
200 | if ((sp->fts_rfd = open(".", O_RDONLY, 0)) == -1) |
---|
201 | SET(FTS_NOCHDIR); |
---|
202 | else if (fcntl(sp->fts_rfd, F_SETFD, FD_CLOEXEC) == -1) { |
---|
203 | close(sp->fts_rfd); |
---|
204 | SET(FTS_NOCHDIR); |
---|
205 | } |
---|
206 | } |
---|
207 | |
---|
208 | return (sp); |
---|
209 | |
---|
210 | mem3: fts_lfree(root); |
---|
211 | free(parent); |
---|
212 | mem2: free(sp->fts_path); |
---|
213 | mem1: free(sp); |
---|
214 | return (NULL); |
---|
215 | } |
---|
216 | |
---|
217 | static void |
---|
218 | fts_load( |
---|
219 | FTS *sp, |
---|
220 | FTSENT *p) |
---|
221 | { |
---|
222 | size_t len; |
---|
223 | char *cp; |
---|
224 | |
---|
225 | _DIAGASSERT(sp != NULL); |
---|
226 | _DIAGASSERT(p != NULL); |
---|
227 | |
---|
228 | /* |
---|
229 | * Load the stream structure for the next traversal. Since we don't |
---|
230 | * actually enter the directory until after the preorder visit, set |
---|
231 | * the fts_accpath field specially so the chdir gets done to the right |
---|
232 | * place and the user can access the first node. From fts_open it's |
---|
233 | * known that the path will fit. |
---|
234 | */ |
---|
235 | len = p->fts_pathlen = p->fts_namelen; |
---|
236 | memmove(sp->fts_path, p->fts_name, len + 1); |
---|
237 | if ((cp = strrchr(p->fts_name, '/')) && (cp != p->fts_name || cp[1])) { |
---|
238 | len = strlen(++cp); |
---|
239 | memmove(p->fts_name, cp, len + 1); |
---|
240 | p->fts_namelen = len; |
---|
241 | } |
---|
242 | p->fts_accpath = p->fts_path = sp->fts_path; |
---|
243 | sp->fts_dev = p->fts_dev; |
---|
244 | } |
---|
245 | |
---|
246 | int |
---|
247 | fts_close( |
---|
248 | FTS *sp) |
---|
249 | { |
---|
250 | FTSENT *freep, *p; |
---|
251 | int saved_errno = 0; |
---|
252 | |
---|
253 | _DIAGASSERT(sp != NULL); |
---|
254 | |
---|
255 | /* |
---|
256 | * This still works if we haven't read anything -- the dummy structure |
---|
257 | * points to the root list, so we step through to the end of the root |
---|
258 | * list which has a valid parent pointer. |
---|
259 | */ |
---|
260 | if (sp->fts_cur) { |
---|
261 | if (ISSET(FTS_SYMFOLLOW)) |
---|
262 | (void)close(sp->fts_cur->fts_symfd); |
---|
263 | for (p = sp->fts_cur; p->fts_level >= FTS_ROOTLEVEL;) { |
---|
264 | freep = p; |
---|
265 | p = p->fts_link ? p->fts_link : p->fts_parent; |
---|
266 | free(freep); |
---|
267 | } |
---|
268 | free(p); |
---|
269 | } |
---|
270 | |
---|
271 | /* Free up child linked list, sort array, path buffer. */ |
---|
272 | if (sp->fts_child) |
---|
273 | fts_lfree(sp->fts_child); |
---|
274 | if (sp->fts_array) |
---|
275 | free(sp->fts_array); |
---|
276 | free(sp->fts_path); |
---|
277 | |
---|
278 | /* Return to original directory, save errno if necessary. */ |
---|
279 | if (!ISSET(FTS_NOCHDIR)) { |
---|
280 | if (fchdir(sp->fts_rfd)) |
---|
281 | saved_errno = errno; |
---|
282 | (void)close(sp->fts_rfd); |
---|
283 | } |
---|
284 | |
---|
285 | /* Free up the stream pointer. */ |
---|
286 | free(sp); |
---|
287 | /* ISSET() is illegal after this, since the macro touches sp */ |
---|
288 | |
---|
289 | /* Set errno and return. */ |
---|
290 | if (saved_errno) { |
---|
291 | errno = saved_errno; |
---|
292 | return (-1); |
---|
293 | } |
---|
294 | return (0); |
---|
295 | } |
---|
296 | |
---|
297 | /* |
---|
298 | * Special case of "/" at the end of the path so that slashes aren't |
---|
299 | * appended which would cause paths to be written as "....//foo". |
---|
300 | */ |
---|
301 | #define NAPPEND(p) \ |
---|
302 | (p->fts_path[p->fts_pathlen - 1] == '/' \ |
---|
303 | ? p->fts_pathlen - 1 : p->fts_pathlen) |
---|
304 | |
---|
305 | FTSENT * |
---|
306 | fts_read( |
---|
307 | FTS *sp ) |
---|
308 | { |
---|
309 | FTSENT *p, *tmp; |
---|
310 | int instr; |
---|
311 | char *t; |
---|
312 | int saved_errno; |
---|
313 | |
---|
314 | _DIAGASSERT(sp != NULL); |
---|
315 | |
---|
316 | /* If finished or unrecoverable error, return NULL. */ |
---|
317 | if (sp->fts_cur == NULL || ISSET(FTS_STOP)) |
---|
318 | return (NULL); |
---|
319 | |
---|
320 | /* Set current node pointer. */ |
---|
321 | p = sp->fts_cur; |
---|
322 | |
---|
323 | /* Save and zero out user instructions. */ |
---|
324 | instr = p->fts_instr; |
---|
325 | p->fts_instr = FTS_NOINSTR; |
---|
326 | |
---|
327 | /* Any type of file may be re-visited; re-stat and re-turn. */ |
---|
328 | if (instr == FTS_AGAIN) { |
---|
329 | p->fts_info = fts_stat(sp, p, 0); |
---|
330 | return (p); |
---|
331 | } |
---|
332 | |
---|
333 | /* |
---|
334 | * Following a symlink -- SLNONE test allows application to see |
---|
335 | * SLNONE and recover. If indirecting through a symlink, have |
---|
336 | * keep a pointer to current location. If unable to get that |
---|
337 | * pointer, follow fails. |
---|
338 | */ |
---|
339 | if (instr == FTS_FOLLOW && |
---|
340 | (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) { |
---|
341 | p->fts_info = fts_stat(sp, p, 1); |
---|
342 | if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) { |
---|
343 | if ((p->fts_symfd = open(".", O_RDONLY, 0)) == -1) { |
---|
344 | p->fts_errno = errno; |
---|
345 | p->fts_info = FTS_ERR; |
---|
346 | } else if (fcntl(p->fts_symfd, F_SETFD, FD_CLOEXEC) == -1) { |
---|
347 | p->fts_errno = errno; |
---|
348 | p->fts_info = FTS_ERR; |
---|
349 | close(p->fts_symfd); |
---|
350 | } else |
---|
351 | p->fts_flags |= FTS_SYMFOLLOW; |
---|
352 | } |
---|
353 | return (p); |
---|
354 | } |
---|
355 | |
---|
356 | /* Directory in pre-order. */ |
---|
357 | if (p->fts_info == FTS_D) { |
---|
358 | /* If skipped or crossed mount point, do post-order visit. */ |
---|
359 | if (instr == FTS_SKIP || |
---|
360 | (ISSET(FTS_XDEV) && p->fts_dev != sp->fts_dev)) { |
---|
361 | if (p->fts_flags & FTS_SYMFOLLOW) |
---|
362 | (void)close(p->fts_symfd); |
---|
363 | if (sp->fts_child) { |
---|
364 | fts_lfree(sp->fts_child); |
---|
365 | sp->fts_child = NULL; |
---|
366 | } |
---|
367 | p->fts_info = FTS_DP; |
---|
368 | return (p); |
---|
369 | } |
---|
370 | |
---|
371 | /* Rebuild if only read the names and now traversing. */ |
---|
372 | if (sp->fts_child && ISSET(FTS_NAMEONLY)) { |
---|
373 | CLR(FTS_NAMEONLY); |
---|
374 | fts_lfree(sp->fts_child); |
---|
375 | sp->fts_child = NULL; |
---|
376 | } |
---|
377 | |
---|
378 | /* |
---|
379 | * Cd to the subdirectory. |
---|
380 | * |
---|
381 | * If have already read and now fail to chdir, whack the list |
---|
382 | * to make the names come out right, and set the parent errno |
---|
383 | * so the application will eventually get an error condition. |
---|
384 | * Set the FTS_DONTCHDIR flag so that when we logically change |
---|
385 | * directories back to the parent we don't do a chdir. |
---|
386 | * |
---|
387 | * If haven't read do so. If the read fails, fts_build sets |
---|
388 | * FTS_STOP or the fts_info field of the node. |
---|
389 | */ |
---|
390 | if (sp->fts_child) { |
---|
391 | if (fts_safe_changedir(sp, p, -1, p->fts_accpath)) { |
---|
392 | p->fts_errno = errno; |
---|
393 | p->fts_flags |= FTS_DONTCHDIR; |
---|
394 | for (p = sp->fts_child; p; p = p->fts_link) |
---|
395 | p->fts_accpath = |
---|
396 | p->fts_parent->fts_accpath; |
---|
397 | } |
---|
398 | } else if ((sp->fts_child = fts_build(sp, BREAD)) == NULL) { |
---|
399 | if (ISSET(FTS_STOP)) |
---|
400 | return (NULL); |
---|
401 | return (p); |
---|
402 | } |
---|
403 | p = sp->fts_child; |
---|
404 | sp->fts_child = NULL; |
---|
405 | goto name; |
---|
406 | } |
---|
407 | |
---|
408 | /* Move to the next node on this level. */ |
---|
409 | next: tmp = p; |
---|
410 | if ((p = p->fts_link) != NULL) { |
---|
411 | free(tmp); |
---|
412 | |
---|
413 | /* |
---|
414 | * If reached the top, return to the original directory, and |
---|
415 | * load the paths for the next root. |
---|
416 | */ |
---|
417 | if (p->fts_level == FTS_ROOTLEVEL) { |
---|
418 | if (FCHDIR(sp, sp->fts_rfd)) { |
---|
419 | SET(FTS_STOP); |
---|
420 | return (NULL); |
---|
421 | } |
---|
422 | fts_load(sp, p); |
---|
423 | return (sp->fts_cur = p); |
---|
424 | } |
---|
425 | |
---|
426 | /* |
---|
427 | * User may have called fts_set on the node. If skipped, |
---|
428 | * ignore. If followed, get a file descriptor so we can |
---|
429 | * get back if necessary. |
---|
430 | */ |
---|
431 | if (p->fts_instr == FTS_SKIP) |
---|
432 | goto next; |
---|
433 | if (p->fts_instr == FTS_FOLLOW) { |
---|
434 | p->fts_info = fts_stat(sp, p, 1); |
---|
435 | if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) { |
---|
436 | if ((p->fts_symfd = |
---|
437 | open(".", O_RDONLY, 0)) == -1) { |
---|
438 | p->fts_errno = errno; |
---|
439 | p->fts_info = FTS_ERR; |
---|
440 | } else if (fcntl(p->fts_symfd, F_SETFD, FD_CLOEXEC) == -1) { |
---|
441 | p->fts_errno = errno; |
---|
442 | p->fts_info = FTS_ERR; |
---|
443 | close(p->fts_symfd); |
---|
444 | } else |
---|
445 | p->fts_flags |= FTS_SYMFOLLOW; |
---|
446 | } |
---|
447 | p->fts_instr = FTS_NOINSTR; |
---|
448 | } |
---|
449 | |
---|
450 | name: t = sp->fts_path + NAPPEND(p->fts_parent); |
---|
451 | *t++ = '/'; |
---|
452 | memmove(t, p->fts_name, (size_t)(p->fts_namelen + 1)); |
---|
453 | return (sp->fts_cur = p); |
---|
454 | } |
---|
455 | |
---|
456 | /* Move up to the parent node. */ |
---|
457 | p = tmp->fts_parent; |
---|
458 | free(tmp); |
---|
459 | |
---|
460 | if (p->fts_level == FTS_ROOTPARENTLEVEL) { |
---|
461 | /* |
---|
462 | * Done; free everything up and set errno to 0 so the user |
---|
463 | * can distinguish between error and EOF. |
---|
464 | */ |
---|
465 | free(p); |
---|
466 | errno = 0; |
---|
467 | return (sp->fts_cur = NULL); |
---|
468 | } |
---|
469 | |
---|
470 | /* Nul terminate the pathname. */ |
---|
471 | sp->fts_path[p->fts_pathlen] = '\0'; |
---|
472 | |
---|
473 | /* |
---|
474 | * Return to the parent directory. If at a root node or came through |
---|
475 | * a symlink, go back through the file descriptor. Otherwise, cd up |
---|
476 | * one directory. |
---|
477 | */ |
---|
478 | if (p->fts_level == FTS_ROOTLEVEL) { |
---|
479 | if (FCHDIR(sp, sp->fts_rfd)) { |
---|
480 | SET(FTS_STOP); |
---|
481 | return (NULL); |
---|
482 | } |
---|
483 | } else if (p->fts_flags & FTS_SYMFOLLOW) { |
---|
484 | if (FCHDIR(sp, p->fts_symfd)) { |
---|
485 | saved_errno = errno; |
---|
486 | (void)close(p->fts_symfd); |
---|
487 | errno = saved_errno; |
---|
488 | SET(FTS_STOP); |
---|
489 | return (NULL); |
---|
490 | } |
---|
491 | (void)close(p->fts_symfd); |
---|
492 | } else if (!(p->fts_flags & FTS_DONTCHDIR) && |
---|
493 | fts_safe_changedir(sp, p->fts_parent, -1, "..")) { |
---|
494 | SET(FTS_STOP); |
---|
495 | return (NULL); |
---|
496 | } |
---|
497 | p->fts_info = p->fts_errno ? FTS_ERR : FTS_DP; |
---|
498 | return (sp->fts_cur = p); |
---|
499 | } |
---|
500 | |
---|
501 | /* |
---|
502 | * Fts_set takes the stream as an argument although it's not used in this |
---|
503 | * implementation; it would be necessary if anyone wanted to add global |
---|
504 | * semantics to fts using fts_set. An error return is allowed for similar |
---|
505 | * reasons. |
---|
506 | */ |
---|
507 | /* ARGSUSED */ |
---|
508 | int |
---|
509 | fts_set( |
---|
510 | FTS *sp __attribute__((unused)), |
---|
511 | FTSENT *p, |
---|
512 | int instr) |
---|
513 | { |
---|
514 | |
---|
515 | _DIAGASSERT(sp != NULL); |
---|
516 | _DIAGASSERT(p != NULL); |
---|
517 | |
---|
518 | if (instr && instr != FTS_AGAIN && instr != FTS_FOLLOW && |
---|
519 | instr != FTS_NOINSTR && instr != FTS_SKIP) { |
---|
520 | errno = EINVAL; |
---|
521 | return (1); |
---|
522 | } |
---|
523 | p->fts_instr = instr; |
---|
524 | return (0); |
---|
525 | } |
---|
526 | |
---|
527 | FTSENT * |
---|
528 | fts_children( |
---|
529 | FTS *sp, |
---|
530 | int instr ) |
---|
531 | { |
---|
532 | FTSENT *p; |
---|
533 | int fd; |
---|
534 | |
---|
535 | _DIAGASSERT(sp != NULL); |
---|
536 | |
---|
537 | if (instr && instr != FTS_NAMEONLY) { |
---|
538 | errno = EINVAL; |
---|
539 | return (NULL); |
---|
540 | } |
---|
541 | |
---|
542 | /* Set current node pointer. */ |
---|
543 | p = sp->fts_cur; |
---|
544 | |
---|
545 | /* |
---|
546 | * Errno set to 0 so user can distinguish empty directory from |
---|
547 | * an error. |
---|
548 | */ |
---|
549 | errno = 0; |
---|
550 | |
---|
551 | /* Fatal errors stop here. */ |
---|
552 | if (ISSET(FTS_STOP)) |
---|
553 | return (NULL); |
---|
554 | |
---|
555 | /* Return logical hierarchy of user's arguments. */ |
---|
556 | if (p->fts_info == FTS_INIT) |
---|
557 | return (p->fts_link); |
---|
558 | |
---|
559 | /* |
---|
560 | * If not a directory being visited in pre-order, stop here. Could |
---|
561 | * allow FTS_DNR, assuming the user has fixed the problem, but the |
---|
562 | * same effect is available with FTS_AGAIN. |
---|
563 | */ |
---|
564 | if (p->fts_info != FTS_D /* && p->fts_info != FTS_DNR */) |
---|
565 | return (NULL); |
---|
566 | |
---|
567 | /* Free up any previous child list. */ |
---|
568 | if (sp->fts_child) |
---|
569 | fts_lfree(sp->fts_child); |
---|
570 | |
---|
571 | if (instr == FTS_NAMEONLY) { |
---|
572 | SET(FTS_NAMEONLY); |
---|
573 | instr = BNAMES; |
---|
574 | } else |
---|
575 | instr = BCHILD; |
---|
576 | |
---|
577 | /* |
---|
578 | * If using chdir on a relative path and called BEFORE fts_read does |
---|
579 | * its chdir to the root of a traversal, we can lose -- we need to |
---|
580 | * chdir into the subdirectory, and we don't know where the current |
---|
581 | * directory is, so we can't get back so that the upcoming chdir by |
---|
582 | * fts_read will work. |
---|
583 | */ |
---|
584 | if (p->fts_level != FTS_ROOTLEVEL || p->fts_accpath[0] == '/' || |
---|
585 | ISSET(FTS_NOCHDIR)) |
---|
586 | return (sp->fts_child = fts_build(sp, instr)); |
---|
587 | |
---|
588 | if ((fd = open(".", O_RDONLY, 0)) == -1) |
---|
589 | return (sp->fts_child = NULL); |
---|
590 | sp->fts_child = fts_build(sp, instr); |
---|
591 | if (fchdir(fd)) { |
---|
592 | (void)close(fd); |
---|
593 | return (NULL); |
---|
594 | } |
---|
595 | (void)close(fd); |
---|
596 | return (sp->fts_child); |
---|
597 | } |
---|
598 | |
---|
599 | /* |
---|
600 | * This is the tricky part -- do not casually change *anything* in here. The |
---|
601 | * idea is to build the linked list of entries that are used by fts_children |
---|
602 | * and fts_read. There are lots of special cases. |
---|
603 | * |
---|
604 | * The real slowdown in walking the tree is the stat calls. If FTS_NOSTAT is |
---|
605 | * set and it's a physical walk (so that symbolic links can't be directories), |
---|
606 | * we can do things quickly. First, if it's a 4.4BSD file system, the type |
---|
607 | * of the file is in the directory entry. Otherwise, we assume that the number |
---|
608 | * of subdirectories in a node is equal to the number of links to the parent. |
---|
609 | * The former skips all stat calls. The latter skips stat calls in any leaf |
---|
610 | * directories and for any files after the subdirectories in the directory have |
---|
611 | * been found, cutting the stat calls by about 2/3. |
---|
612 | */ |
---|
613 | static FTSENT * |
---|
614 | fts_build( |
---|
615 | FTS *sp, |
---|
616 | int type) |
---|
617 | { |
---|
618 | struct dirent *dp; |
---|
619 | FTSENT *p, *head; |
---|
620 | size_t nitems; |
---|
621 | FTSENT *cur, *tail; |
---|
622 | DIR *dirp; |
---|
623 | int adjust, cderrno, descend, len, level, nlinks, saved_errno, nostat; |
---|
624 | size_t maxlen; |
---|
625 | #ifdef FTS_WHITEOUT |
---|
626 | int oflag; |
---|
627 | #endif |
---|
628 | char *cp = NULL; /* pacify gcc */ |
---|
629 | |
---|
630 | _DIAGASSERT(sp != NULL); |
---|
631 | |
---|
632 | /* Set current node pointer. */ |
---|
633 | cur = sp->fts_cur; |
---|
634 | |
---|
635 | /* |
---|
636 | * Open the directory for reading. If this fails, we're done. |
---|
637 | * If being called from fts_read, set the fts_info field. |
---|
638 | */ |
---|
639 | #ifdef FTS_WHITEOUT |
---|
640 | if (ISSET(FTS_WHITEOUT)) |
---|
641 | oflag = DTF_NODUP|DTF_REWIND; |
---|
642 | else |
---|
643 | oflag = DTF_HIDEW|DTF_NODUP|DTF_REWIND; |
---|
644 | #else |
---|
645 | #define __opendir2(path, flag) opendir(path) |
---|
646 | #endif |
---|
647 | if ((dirp = __opendir2(cur->fts_accpath, oflag)) == NULL) { |
---|
648 | if (type == BREAD) { |
---|
649 | cur->fts_info = FTS_DNR; |
---|
650 | cur->fts_errno = errno; |
---|
651 | } |
---|
652 | return (NULL); |
---|
653 | } |
---|
654 | |
---|
655 | /* |
---|
656 | * Nlinks is the number of possible entries of type directory in the |
---|
657 | * directory if we're cheating on stat calls, 0 if we're not doing |
---|
658 | * any stat calls at all, -1 if we're doing stats on everything. |
---|
659 | */ |
---|
660 | if (type == BNAMES) { |
---|
661 | nlinks = 0; |
---|
662 | nostat = 1; |
---|
663 | } else if (ISSET(FTS_NOSTAT) && ISSET(FTS_PHYSICAL)) { |
---|
664 | nlinks = cur->fts_nlink - (ISSET(FTS_SEEDOT) ? 0 : 2); |
---|
665 | nostat = 1; |
---|
666 | } else { |
---|
667 | nlinks = -1; |
---|
668 | nostat = 0; |
---|
669 | } |
---|
670 | |
---|
671 | #ifdef notdef |
---|
672 | (void)printf("nlinks == %d (cur: %d)\n", nlinks, cur->fts_nlink); |
---|
673 | (void)printf("NOSTAT %d PHYSICAL %d SEEDOT %d\n", |
---|
674 | ISSET(FTS_NOSTAT), ISSET(FTS_PHYSICAL), ISSET(FTS_SEEDOT)); |
---|
675 | #endif |
---|
676 | /* |
---|
677 | * If we're going to need to stat anything or we want to descend |
---|
678 | * and stay in the directory, chdir. If this fails we keep going, |
---|
679 | * but set a flag so we don't chdir after the post-order visit. |
---|
680 | * We won't be able to stat anything, but we can still return the |
---|
681 | * names themselves. Note, that since fts_read won't be able to |
---|
682 | * chdir into the directory, it will have to return different path |
---|
683 | * names than before, i.e. "a/b" instead of "b". Since the node |
---|
684 | * has already been visited in pre-order, have to wait until the |
---|
685 | * post-order visit to return the error. There is a special case |
---|
686 | * here, if there was nothing to stat then it's not an error to |
---|
687 | * not be able to stat. This is all fairly nasty. If a program |
---|
688 | * needed sorted entries or stat information, they had better be |
---|
689 | * checking FTS_NS on the returned nodes. |
---|
690 | */ |
---|
691 | cderrno = 0; |
---|
692 | if (nlinks || type == BREAD) { |
---|
693 | if (fts_safe_changedir(sp, cur, dirfd(dirp), NULL)) { |
---|
694 | if (nlinks && type == BREAD) |
---|
695 | cur->fts_errno = errno; |
---|
696 | cur->fts_flags |= FTS_DONTCHDIR; |
---|
697 | descend = 0; |
---|
698 | cderrno = errno; |
---|
699 | } else |
---|
700 | descend = 1; |
---|
701 | } else |
---|
702 | descend = 0; |
---|
703 | |
---|
704 | /* |
---|
705 | * Figure out the max file name length that can be stored in the |
---|
706 | * current path -- the inner loop allocates more path as necessary. |
---|
707 | * We really wouldn't have to do the maxlen calculations here, we |
---|
708 | * could do them in fts_read before returning the path, but it's a |
---|
709 | * lot easier here since the length is part of the dirent structure. |
---|
710 | * |
---|
711 | * If not changing directories set a pointer so that can just append |
---|
712 | * each new name into the path. |
---|
713 | */ |
---|
714 | len = NAPPEND(cur); |
---|
715 | if (ISSET(FTS_NOCHDIR)) { |
---|
716 | cp = sp->fts_path + len; |
---|
717 | *cp++ = '/'; |
---|
718 | } |
---|
719 | len++; |
---|
720 | maxlen = sp->fts_pathlen - len; |
---|
721 | |
---|
722 | level = cur->fts_level + 1; |
---|
723 | |
---|
724 | /* Read the directory, attaching each entry to the `link' pointer. */ |
---|
725 | adjust = 0; |
---|
726 | for (head = tail = NULL, nitems = 0; (dp = readdir(dirp)) != NULL;) { |
---|
727 | size_t dlen; |
---|
728 | |
---|
729 | if (!ISSET(FTS_SEEDOT) && ISDOT(dp->d_name)) |
---|
730 | continue; |
---|
731 | |
---|
732 | #if HAVE_STRUCT_DIRENT_D_NAMLEN |
---|
733 | dlen = dp->d_namlen; |
---|
734 | #else |
---|
735 | dlen = strlen(dp->d_name); |
---|
736 | #endif |
---|
737 | if ((p = fts_alloc(sp, dp->d_name, dlen)) == NULL) |
---|
738 | goto mem1; |
---|
739 | if (dlen >= maxlen) { /* include space for NUL */ |
---|
740 | if (fts_palloc(sp, len + dlen + 1)) { |
---|
741 | /* |
---|
742 | * No more memory for path or structures. Save |
---|
743 | * errno, free up the current structure and the |
---|
744 | * structures already allocated. |
---|
745 | */ |
---|
746 | mem1: saved_errno = errno; |
---|
747 | if (p) |
---|
748 | free(p); |
---|
749 | fts_lfree(head); |
---|
750 | (void)closedir(dirp); |
---|
751 | errno = saved_errno; |
---|
752 | cur->fts_info = FTS_ERR; |
---|
753 | SET(FTS_STOP); |
---|
754 | return (NULL); |
---|
755 | } |
---|
756 | adjust = 1; |
---|
757 | if (ISSET(FTS_NOCHDIR)) |
---|
758 | cp = sp->fts_path + len; |
---|
759 | maxlen = sp->fts_pathlen - len; |
---|
760 | } |
---|
761 | |
---|
762 | p->fts_pathlen = len + dlen; |
---|
763 | p->fts_parent = sp->fts_cur; |
---|
764 | p->fts_level = level; |
---|
765 | |
---|
766 | #ifdef FTS_WHITEOUT |
---|
767 | if (dp->d_type == DT_WHT) |
---|
768 | p->fts_flags |= FTS_ISW; |
---|
769 | #endif |
---|
770 | |
---|
771 | if (cderrno) { |
---|
772 | if (nlinks) { |
---|
773 | p->fts_info = FTS_NS; |
---|
774 | p->fts_errno = cderrno; |
---|
775 | } else |
---|
776 | p->fts_info = FTS_NSOK; |
---|
777 | p->fts_accpath = cur->fts_accpath; |
---|
778 | } else if (nlinks == 0 |
---|
779 | #ifdef DT_DIR |
---|
780 | || (nostat && |
---|
781 | dp->d_type != DT_DIR && dp->d_type != DT_UNKNOWN) |
---|
782 | #endif |
---|
783 | ) { |
---|
784 | p->fts_accpath = |
---|
785 | ISSET(FTS_NOCHDIR) ? p->fts_path : p->fts_name; |
---|
786 | p->fts_info = FTS_NSOK; |
---|
787 | } else { |
---|
788 | /* Build a file name for fts_stat to stat. */ |
---|
789 | if (ISSET(FTS_NOCHDIR)) { |
---|
790 | p->fts_accpath = p->fts_path; |
---|
791 | memmove(cp, p->fts_name, |
---|
792 | (size_t)(p->fts_namelen + 1)); |
---|
793 | } else |
---|
794 | p->fts_accpath = p->fts_name; |
---|
795 | /* Stat it. */ |
---|
796 | p->fts_info = fts_stat(sp, p, 0); |
---|
797 | |
---|
798 | /* Decrement link count if applicable. */ |
---|
799 | if (nlinks > 0 && (p->fts_info == FTS_D || |
---|
800 | p->fts_info == FTS_DC || p->fts_info == FTS_DOT)) |
---|
801 | --nlinks; |
---|
802 | } |
---|
803 | |
---|
804 | /* We walk in directory order so "ls -f" doesn't get upset. */ |
---|
805 | p->fts_link = NULL; |
---|
806 | if (head == NULL) |
---|
807 | head = tail = p; |
---|
808 | else { |
---|
809 | tail->fts_link = p; |
---|
810 | tail = p; |
---|
811 | } |
---|
812 | ++nitems; |
---|
813 | } |
---|
814 | (void)closedir(dirp); |
---|
815 | |
---|
816 | /* |
---|
817 | * If had to realloc the path, adjust the addresses for the rest |
---|
818 | * of the tree. |
---|
819 | */ |
---|
820 | if (adjust) |
---|
821 | fts_padjust(sp, head); |
---|
822 | |
---|
823 | /* |
---|
824 | * If not changing directories, reset the path back to original |
---|
825 | * state. |
---|
826 | */ |
---|
827 | if (ISSET(FTS_NOCHDIR)) { |
---|
828 | if (cp - 1 > sp->fts_path) |
---|
829 | --cp; |
---|
830 | *cp = '\0'; |
---|
831 | } |
---|
832 | |
---|
833 | /* |
---|
834 | * If descended after called from fts_children or after called from |
---|
835 | * fts_read and nothing found, get back. At the root level we use |
---|
836 | * the saved fd; if one of fts_open()'s arguments is a relative path |
---|
837 | * to an empty directory, we wind up here with no other way back. If |
---|
838 | * can't get back, we're done. |
---|
839 | */ |
---|
840 | if (descend && (type == BCHILD || !nitems) && |
---|
841 | (cur->fts_level == FTS_ROOTLEVEL ? |
---|
842 | FCHDIR(sp, sp->fts_rfd) : |
---|
843 | fts_safe_changedir(sp, cur->fts_parent, -1, ".."))) { |
---|
844 | cur->fts_info = FTS_ERR; |
---|
845 | SET(FTS_STOP); |
---|
846 | return (NULL); |
---|
847 | } |
---|
848 | |
---|
849 | /* If didn't find anything, return NULL. */ |
---|
850 | if (!nitems) { |
---|
851 | if (type == BREAD) |
---|
852 | cur->fts_info = FTS_DP; |
---|
853 | return (NULL); |
---|
854 | } |
---|
855 | |
---|
856 | /* Sort the entries. */ |
---|
857 | if (sp->fts_compar && nitems > 1) |
---|
858 | head = fts_sort(sp, head, nitems); |
---|
859 | return (head); |
---|
860 | } |
---|
861 | |
---|
862 | static u_short |
---|
863 | fts_stat( |
---|
864 | FTS *sp, |
---|
865 | FTSENT *p, |
---|
866 | int follow ) |
---|
867 | { |
---|
868 | FTSENT *t; |
---|
869 | dev_t dev; |
---|
870 | __fts_ino_t ino; |
---|
871 | __fts_stat_t *sbp, sb; |
---|
872 | int saved_errno; |
---|
873 | |
---|
874 | _DIAGASSERT(sp != NULL); |
---|
875 | _DIAGASSERT(p != NULL); |
---|
876 | |
---|
877 | /* If user needs stat info, stat buffer already allocated. */ |
---|
878 | sbp = ISSET(FTS_NOSTAT) ? &sb : p->fts_statp; |
---|
879 | |
---|
880 | #ifdef FTS_WHITEOUT |
---|
881 | /* check for whiteout */ |
---|
882 | if (p->fts_flags & FTS_ISW) { |
---|
883 | if (sbp != &sb) { |
---|
884 | memset(sbp, '\0', sizeof (*sbp)); |
---|
885 | sbp->st_mode = S_IFWHT; |
---|
886 | } |
---|
887 | return (FTS_W); |
---|
888 | } |
---|
889 | #endif |
---|
890 | |
---|
891 | /* |
---|
892 | * If doing a logical walk, or application requested FTS_FOLLOW, do |
---|
893 | * a stat(2). If that fails, check for a non-existent symlink. If |
---|
894 | * fail, set the errno from the stat call. |
---|
895 | */ |
---|
896 | if (ISSET(FTS_LOGICAL) || follow) { |
---|
897 | if (stat(p->fts_accpath, sbp)) { |
---|
898 | saved_errno = errno; |
---|
899 | if (!lstat(p->fts_accpath, sbp)) { |
---|
900 | errno = 0; |
---|
901 | return (FTS_SLNONE); |
---|
902 | } |
---|
903 | p->fts_errno = saved_errno; |
---|
904 | goto err; |
---|
905 | } |
---|
906 | } else if (lstat(p->fts_accpath, sbp)) { |
---|
907 | p->fts_errno = errno; |
---|
908 | err: memset(sbp, 0, sizeof(*sbp)); |
---|
909 | return (FTS_NS); |
---|
910 | } |
---|
911 | |
---|
912 | if (S_ISDIR(sbp->st_mode)) { |
---|
913 | /* |
---|
914 | * Set the device/inode. Used to find cycles and check for |
---|
915 | * crossing mount points. Also remember the link count, used |
---|
916 | * in fts_build to limit the number of stat calls. It is |
---|
917 | * understood that these fields are only referenced if fts_info |
---|
918 | * is set to FTS_D. |
---|
919 | */ |
---|
920 | dev = p->fts_dev = sbp->st_dev; |
---|
921 | ino = p->fts_ino = sbp->st_ino; |
---|
922 | p->fts_nlink = sbp->st_nlink; |
---|
923 | |
---|
924 | if (ISDOT(p->fts_name)) |
---|
925 | return (FTS_DOT); |
---|
926 | |
---|
927 | /* |
---|
928 | * Cycle detection is done by brute force when the directory |
---|
929 | * is first encountered. If the tree gets deep enough or the |
---|
930 | * number of symbolic links to directories is high enough, |
---|
931 | * something faster might be worthwhile. |
---|
932 | */ |
---|
933 | for (t = p->fts_parent; |
---|
934 | t->fts_level >= FTS_ROOTLEVEL; t = t->fts_parent) |
---|
935 | if (ino == t->fts_ino && dev == t->fts_dev) { |
---|
936 | p->fts_cycle = t; |
---|
937 | return (FTS_DC); |
---|
938 | } |
---|
939 | return (FTS_D); |
---|
940 | } |
---|
941 | if (S_ISLNK(sbp->st_mode)) |
---|
942 | return (FTS_SL); |
---|
943 | if (S_ISREG(sbp->st_mode)) |
---|
944 | return (FTS_F); |
---|
945 | return (FTS_DEFAULT); |
---|
946 | } |
---|
947 | |
---|
948 | static FTSENT * |
---|
949 | fts_sort( |
---|
950 | FTS *sp, |
---|
951 | FTSENT *head, |
---|
952 | size_t nitems ) |
---|
953 | { |
---|
954 | FTSENT **ap, *p; |
---|
955 | |
---|
956 | _DIAGASSERT(sp != NULL); |
---|
957 | _DIAGASSERT(head != NULL); |
---|
958 | |
---|
959 | /* |
---|
960 | * Construct an array of pointers to the structures and call qsort(3). |
---|
961 | * Reassemble the array in the order returned by qsort. If unable to |
---|
962 | * sort for memory reasons, return the directory entries in their |
---|
963 | * current order. Allocate enough space for the current needs plus |
---|
964 | * 40 so don't realloc one entry at a time. |
---|
965 | */ |
---|
966 | if (nitems > sp->fts_nitems) { |
---|
967 | FTSENT **new; |
---|
968 | |
---|
969 | new = realloc(sp->fts_array, sizeof(FTSENT *) * (nitems + 40)); |
---|
970 | if (new == 0) |
---|
971 | return (head); |
---|
972 | sp->fts_array = new; |
---|
973 | sp->fts_nitems = nitems + 40; |
---|
974 | } |
---|
975 | for (ap = sp->fts_array, p = head; p; p = p->fts_link) |
---|
976 | *ap++ = p; |
---|
977 | qsort((void *)sp->fts_array, nitems, sizeof(FTSENT *), |
---|
978 | (int (*) __P((const void *, const void *)))sp->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( |
---|
987 | FTS *sp, |
---|
988 | const char *name, |
---|
989 | size_t namelen ) |
---|
990 | { |
---|
991 | FTSENT *p; |
---|
992 | size_t len; |
---|
993 | |
---|
994 | _DIAGASSERT(sp != NULL); |
---|
995 | _DIAGASSERT(name != NULL); |
---|
996 | |
---|
997 | #if defined(ALIGNBYTES) && defined(ALIGN) |
---|
998 | /* |
---|
999 | * The file name is a variable length array and no stat structure is |
---|
1000 | * necessary if the user has set the nostat bit. Allocate the FTSENT |
---|
1001 | * structure, the file name and the stat structure in one chunk, but |
---|
1002 | * be careful that the stat structure is reasonably aligned. Since the |
---|
1003 | * fts_name field is declared to be of size 1, the fts_name pointer is |
---|
1004 | * namelen + 2 before the first possible address of the stat structure. |
---|
1005 | */ |
---|
1006 | len = sizeof(FTSENT) + namelen; |
---|
1007 | if (!ISSET(FTS_NOSTAT)) |
---|
1008 | len += sizeof(*(p->fts_statp)) + ALIGNBYTES; |
---|
1009 | if ((p = malloc(len)) == NULL) |
---|
1010 | return (NULL); |
---|
1011 | |
---|
1012 | if (!ISSET(FTS_NOSTAT)) |
---|
1013 | p->fts_statp = |
---|
1014 | (__fts_stat_t *)ALIGN((u_long)(p->fts_name + namelen + 2)); |
---|
1015 | #else |
---|
1016 | if ((p = malloc(sizeof(FTSENT) + namelen)) == NULL) |
---|
1017 | return (NULL); |
---|
1018 | |
---|
1019 | if (!ISSET(FTS_NOSTAT)) |
---|
1020 | if ((p->fts_statp = malloc(sizeof(*(p->fts_statp)))) == NULL) { |
---|
1021 | free(p); |
---|
1022 | return (NULL); |
---|
1023 | } |
---|
1024 | #endif |
---|
1025 | |
---|
1026 | /* Copy the name plus the trailing NULL. */ |
---|
1027 | memmove(p->fts_name, name, namelen + 1); |
---|
1028 | |
---|
1029 | p->fts_namelen = namelen; |
---|
1030 | p->fts_path = sp->fts_path; |
---|
1031 | p->fts_errno = 0; |
---|
1032 | p->fts_flags = 0; |
---|
1033 | p->fts_instr = FTS_NOINSTR; |
---|
1034 | p->fts_number = 0; |
---|
1035 | p->fts_pointer = NULL; |
---|
1036 | return (p); |
---|
1037 | } |
---|
1038 | |
---|
1039 | static void |
---|
1040 | fts_lfree( |
---|
1041 | FTSENT *head ) |
---|
1042 | { |
---|
1043 | FTSENT *p; |
---|
1044 | |
---|
1045 | /* XXX: head may be NULL ? */ |
---|
1046 | |
---|
1047 | /* Free a linked list of structures. */ |
---|
1048 | while ((p = head) != NULL) { |
---|
1049 | head = head->fts_link; |
---|
1050 | |
---|
1051 | #if !defined(ALIGNBYTES) || !defined(ALIGN) |
---|
1052 | if (p->fts_statp) |
---|
1053 | free(p->fts_statp); |
---|
1054 | #endif |
---|
1055 | free(p); |
---|
1056 | } |
---|
1057 | } |
---|
1058 | |
---|
1059 | static size_t |
---|
1060 | fts_pow2( |
---|
1061 | size_t x ) |
---|
1062 | { |
---|
1063 | |
---|
1064 | x--; |
---|
1065 | x |= x>>1; |
---|
1066 | x |= x>>2; |
---|
1067 | x |= x>>4; |
---|
1068 | x |= x>>8; |
---|
1069 | x |= x>>16; |
---|
1070 | #if LONG_BIT > 32 |
---|
1071 | x |= x>>32; |
---|
1072 | #endif |
---|
1073 | #if LONG_BIT > 64 |
---|
1074 | x |= x>>64; |
---|
1075 | #endif |
---|
1076 | x++; |
---|
1077 | return (x); |
---|
1078 | } |
---|
1079 | |
---|
1080 | /* |
---|
1081 | * Allow essentially unlimited paths; find, rm, ls should all work on any tree. |
---|
1082 | * Most systems will allow creation of paths much longer than MAXPATHLEN, even |
---|
1083 | * though the kernel won't resolve them. Round up the new size to a power of 2, |
---|
1084 | * so we don't realloc the path 2 bytes at a time. |
---|
1085 | */ |
---|
1086 | static int |
---|
1087 | fts_palloc( |
---|
1088 | FTS *sp, |
---|
1089 | size_t size ) |
---|
1090 | { |
---|
1091 | char *new; |
---|
1092 | |
---|
1093 | _DIAGASSERT(sp != NULL); |
---|
1094 | |
---|
1095 | #if 1 |
---|
1096 | /* Protect against fts_pathlen overflow. */ |
---|
1097 | if (size > USHRT_MAX + 1) { |
---|
1098 | errno = ENOMEM; |
---|
1099 | return (1); |
---|
1100 | } |
---|
1101 | #endif |
---|
1102 | size = fts_pow2(size); |
---|
1103 | new = realloc(sp->fts_path, size); |
---|
1104 | if (new == 0) |
---|
1105 | return (1); |
---|
1106 | sp->fts_path = new; |
---|
1107 | sp->fts_pathlen = size; |
---|
1108 | return (0); |
---|
1109 | } |
---|
1110 | |
---|
1111 | /* |
---|
1112 | * When the path is realloc'd, have to fix all of the pointers in structures |
---|
1113 | * already returned. |
---|
1114 | */ |
---|
1115 | static void |
---|
1116 | fts_padjust( |
---|
1117 | FTS *sp, |
---|
1118 | FTSENT *head ) |
---|
1119 | { |
---|
1120 | FTSENT *p; |
---|
1121 | char *addr; |
---|
1122 | |
---|
1123 | _DIAGASSERT(sp != NULL); |
---|
1124 | |
---|
1125 | #define ADJUST(p) do { \ |
---|
1126 | if ((p)->fts_accpath != (p)->fts_name) \ |
---|
1127 | (p)->fts_accpath = \ |
---|
1128 | addr + ((p)->fts_accpath - (p)->fts_path); \ |
---|
1129 | (p)->fts_path = addr; \ |
---|
1130 | } while (/*CONSTCOND*/0) |
---|
1131 | |
---|
1132 | addr = sp->fts_path; |
---|
1133 | |
---|
1134 | /* Adjust the current set of children. */ |
---|
1135 | for (p = sp->fts_child; p; p = p->fts_link) |
---|
1136 | ADJUST(p); |
---|
1137 | |
---|
1138 | /* Adjust the rest of the tree, including the current level. */ |
---|
1139 | for (p = head; p->fts_level >= FTS_ROOTLEVEL;) { |
---|
1140 | ADJUST(p); |
---|
1141 | p = p->fts_link ? p->fts_link : p->fts_parent; |
---|
1142 | } |
---|
1143 | } |
---|
1144 | |
---|
1145 | static size_t |
---|
1146 | fts_maxarglen( |
---|
1147 | char * const *argv ) |
---|
1148 | { |
---|
1149 | size_t len, max; |
---|
1150 | |
---|
1151 | _DIAGASSERT(argv != NULL); |
---|
1152 | |
---|
1153 | for (max = 0; *argv; ++argv) |
---|
1154 | if ((len = strlen(*argv)) > max) |
---|
1155 | max = len; |
---|
1156 | return (max + 1); |
---|
1157 | } |
---|
1158 | |
---|
1159 | /* |
---|
1160 | * Change to dir specified by fd or p->fts_accpath without getting |
---|
1161 | * tricked by someone changing the world out from underneath us. |
---|
1162 | * Assumes p->fts_dev and p->fts_ino are filled in. |
---|
1163 | */ |
---|
1164 | static int |
---|
1165 | fts_safe_changedir( |
---|
1166 | const FTS *sp, |
---|
1167 | const FTSENT *p, |
---|
1168 | int fd, |
---|
1169 | const char *path) |
---|
1170 | { |
---|
1171 | int oldfd = fd, ret = -1; |
---|
1172 | __fts_stat_t sb; |
---|
1173 | |
---|
1174 | if (ISSET(FTS_NOCHDIR)) |
---|
1175 | return 0; |
---|
1176 | |
---|
1177 | if (oldfd < 0 && (fd = open(path, O_RDONLY)) == -1) |
---|
1178 | return -1; |
---|
1179 | |
---|
1180 | if (fstat(fd, &sb) == -1) |
---|
1181 | goto bail; |
---|
1182 | |
---|
1183 | if (sb.st_ino != p->fts_ino || sb.st_dev != p->fts_dev) { |
---|
1184 | errno = ENOENT; |
---|
1185 | goto bail; |
---|
1186 | } |
---|
1187 | |
---|
1188 | ret = fchdir(fd); |
---|
1189 | |
---|
1190 | bail: |
---|
1191 | if (oldfd < 0) { |
---|
1192 | int save_errno = errno; |
---|
1193 | (void)close(fd); |
---|
1194 | errno = save_errno; |
---|
1195 | } |
---|
1196 | return ret; |
---|
1197 | } |
---|