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