1 /* File tree walker functions.
2 Copyright (C) 1996-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
24 # define alloca __builtin_alloca
39 # define NAMLEN(dirent) _D_EXACT_NAMLEN (dirent)
43 # define NAMLEN(dirent) strlen ((dirent)->d_name)
45 # define dirent direct
46 # define NAMLEN(dirent) (dirent)->d_namlen
48 # include <sys/ndir.h>
67 #include <not-cancel.h>
68 #include <sys/param.h>
70 # include <include/sys/stat.h>
72 # include <sys/stat.h>
75 #if ! _LIBC && !HAVE_DECL_STPCPY && !defined stpcpy
79 #if ! _LIBC && ! defined HAVE_MEMPCPY && ! defined mempcpy
80 /* Be CAREFUL that there are no side effects in N. */
81 # define mempcpy(D, S, N) ((void *) ((char *) memcpy (D, S, N) + (N)))
84 /* #define NDEBUG 1 */
89 # define __chdir chdir
91 # define __closedir closedir
93 # define __fchdir fchdir
95 # define __getcwd(P, N) xgetcwd ()
96 extern char *xgetcwd (void);
98 # define __mempcpy mempcpy
100 # define __opendir opendir
102 # define __readdir64 readdir
104 # define __stpcpy stpcpy
106 # define __tdestroy tdestroy
108 # define __tfind tfind
110 # define __tsearch tsearch
112 # define dirent64 dirent
114 # define MAX(a, b) ((a) > (b) ? (a) : (b))
117 /* Arrange to make lstat calls go through the wrapper function
118 on systems with an lstat function that does not dereference symlinks
119 that are specified with a trailing slash. */
120 #if ! _LIBC && ! LSTAT_FOLLOWS_SLASHED_SYMLINK
121 int rpl_lstat (const char *, struct stat
*);
123 # define lstat(Name, Stat_buf) rpl_lstat(Name, Stat_buf)
127 # define __set_errno(Val) errno = (Val)
130 /* Support for the LFS API version. */
132 # define FTW_NAME ftw
133 # define NFTW_NAME nftw
134 # define NFTW_OLD_NAME __old_nftw
135 # define NFTW_NEW_NAME __new_nftw
137 # define STRUCT_STAT stat
139 # define LSTAT __lstat
141 # define FSTATAT __fstatat
145 # define FSTATAT fstatat
147 # define FTW_FUNC_T __ftw_func_t
148 # define NFTW_FUNC_T __nftw_func_t
151 /* We define PATH_MAX if the system does not provide a definition.
152 This does not artificially limit any operation. PATH_MAX is simply
153 used as a guesstimate for the expected maximal path length.
154 Buffers will be enlarged if necessary. */
156 # define PATH_MAX 1024
174 /* Array with pointers to open directory streams. */
175 struct dir_data
**dirstreams
;
179 /* Buffer containing name of currently processed object. */
183 /* Passed as fourth argument to `nftw' callback. The `base' member
184 tracks the content of the `dirbuf'. */
187 /* Flags passed to `nftw' function. 0 for `ftw'. */
190 /* Conversion array for flag values. It is the identity mapping for
191 `nftw' calls, otherwise it maps the values to those known by
195 /* Callback function. We always use the `nftw' form. */
198 /* Device of starting point. Needed for FTW_MOUNT. */
201 /* Data structure for keeping fingerprints of already processed
202 object. This is needed when not using FTW_PHYS. */
207 ftw_allocate (struct ftw_data
*data
, size_t newsize
)
209 void *newp
= realloc (data
->dirstreams
, data
->maxdir
210 * sizeof (struct dir_data
*)
214 data
->dirstreams
= newp
;
215 data
->dirbufsize
= newsize
;
216 data
->dirbuf
= (char *) data
->dirstreams
217 + data
->maxdir
* sizeof (struct dir_data
*);
221 /* Internally we use the FTW_* constants used for `nftw'. When invoked
222 as `ftw', map each flag to the subset of values used by `ftw'. */
223 static const int nftw_arr
[] =
225 FTW_F
, FTW_D
, FTW_DNR
, FTW_NS
, FTW_SL
, FTW_DP
, FTW_SLN
228 static const int ftw_arr
[] =
230 FTW_F
, FTW_D
, FTW_DNR
, FTW_NS
, FTW_F
, FTW_D
, FTW_NS
234 /* Forward declarations of local functions. */
235 static int ftw_dir (struct ftw_data
*data
, struct STRUCT_STAT
*st
,
236 struct dir_data
*old_dir
);
240 object_compare (const void *p1
, const void *p2
)
242 /* We don't need a sophisticated and useful comparison. We are only
243 interested in equality. However, we must be careful not to
244 accidentally compare `holes' in the structure. */
245 const struct known_object
*kp1
= p1
, *kp2
= p2
;
247 cmp1
= (kp1
->ino
> kp2
->ino
) - (kp1
->ino
< kp2
->ino
);
250 return (kp1
->dev
> kp2
->dev
) - (kp1
->dev
< kp2
->dev
);
255 add_object (struct ftw_data
*data
, struct STRUCT_STAT
*st
)
257 struct known_object
*newp
= malloc (sizeof (struct known_object
));
260 newp
->dev
= st
->st_dev
;
261 newp
->ino
= st
->st_ino
;
262 return __tsearch (newp
, &data
->known_objects
, object_compare
) ? 0 : -1;
267 find_object (struct ftw_data
*data
, struct STRUCT_STAT
*st
)
269 struct known_object obj
;
270 obj
.dev
= st
->st_dev
;
271 obj
.ino
= st
->st_ino
;
272 return __tfind (&obj
, &data
->known_objects
, object_compare
) != NULL
;
277 __attribute ((always_inline
))
278 open_dir_stream (int *dfdp
, struct ftw_data
*data
, struct dir_data
*dirp
)
282 if (data
->dirstreams
[data
->actdir
] != NULL
)
284 /* Oh, oh. We must close this stream. Get all remaining
285 entries and store them as a list in the `content' member of
286 the `struct dir_data' variable. */
287 size_t bufsize
= 1024;
288 char *buf
= malloc (bufsize
);
294 DIR *st
= data
->dirstreams
[data
->actdir
]->stream
;
298 while ((d
= __readdir64 (st
)) != NULL
)
300 size_t this_len
= NAMLEN (d
);
301 if (actsize
+ this_len
+ 2 >= bufsize
)
304 bufsize
+= MAX (1024, 2 * this_len
);
305 newp
= (char *) realloc (buf
, bufsize
);
308 /* No more memory. */
309 int save_err
= errno
;
311 __set_errno (save_err
);
317 *((char *) __mempcpy (buf
+ actsize
, d
->d_name
, this_len
))
319 actsize
+= this_len
+ 1;
322 /* Terminate the list with an additional NUL byte. */
323 buf
[actsize
++] = '\0';
325 /* Shrink the buffer to what we actually need. */
326 void *content
= realloc (buf
, actsize
);
327 data
->dirstreams
[data
->actdir
]->content
= content
;
330 int save_err
= errno
;
332 __set_errno (save_err
);
338 data
->dirstreams
[data
->actdir
]->stream
= NULL
;
339 data
->dirstreams
[data
->actdir
]->streamfd
= -1;
340 data
->dirstreams
[data
->actdir
] = NULL
;
345 /* Open the new stream. */
348 assert (data
->dirstreams
[data
->actdir
] == NULL
);
350 if (dfdp
!= NULL
&& *dfdp
!= -1)
352 int fd
= __openat64_nocancel (*dfdp
, data
->dirbuf
+ data
->ftw
.base
,
353 O_RDONLY
| O_DIRECTORY
| O_NDELAY
);
355 if (fd
!= -1 && (dirp
->stream
= __fdopendir (fd
)) == NULL
)
356 __close_nocancel_nostatus (fd
);
362 if (data
->flags
& FTW_CHDIR
)
364 name
= data
->dirbuf
+ data
->ftw
.base
;
371 dirp
->stream
= __opendir (name
);
374 if (dirp
->stream
== NULL
)
378 dirp
->streamfd
= __dirfd (dirp
->stream
);
379 dirp
->content
= NULL
;
380 data
->dirstreams
[data
->actdir
] = dirp
;
382 if (++data
->actdir
== data
->maxdir
)
392 process_entry (struct ftw_data
*data
, struct dir_data
*dir
, const char *name
,
393 size_t namlen
, int d_type
)
395 struct STRUCT_STAT st
;
400 if (name
[0] == '.' && (name
[1] == '\0'
401 || (name
[1] == '.' && name
[2] == '\0')))
402 /* Don't process the "." and ".." entries. */
405 new_buflen
= data
->ftw
.base
+ namlen
+ 2;
406 if (data
->dirbufsize
< new_buflen
407 && !ftw_allocate (data
, 2 * new_buflen
))
410 *((char *) __mempcpy (data
->dirbuf
+ data
->ftw
.base
, name
, namlen
)) = '\0';
413 if (dir
->streamfd
!= -1)
414 statres
= FSTATAT (dir
->streamfd
, name
, &st
,
415 (data
->flags
& FTW_PHYS
) ? AT_SYMLINK_NOFOLLOW
: 0);
418 if ((data
->flags
& FTW_CHDIR
) == 0)
421 statres
= ((data
->flags
& FTW_PHYS
)
428 if (errno
!= EACCES
&& errno
!= ENOENT
)
430 else if (data
->flags
& FTW_PHYS
)
434 /* Old code left ST undefined for dangling DT_LNK without
435 FTW_PHYS set; a clarification at the POSIX level suggests
436 it should contain information about the link (ala lstat).
437 We do our best to fill in what data we can. */
438 if (dir
->streamfd
!= -1)
439 statres
= FSTATAT (dir
->streamfd
, name
, &st
,
440 AT_SYMLINK_NOFOLLOW
);
442 statres
= LSTAT (name
, &st
);
443 if (statres
== 0 && S_ISLNK (st
.st_mode
))
451 if (S_ISDIR (st
.st_mode
))
453 else if (S_ISLNK (st
.st_mode
))
461 || !(data
->flags
& FTW_MOUNT
) || st
.st_dev
== data
->dev
))
465 if ((data
->flags
& FTW_PHYS
)
466 || (!find_object (data
, &st
)
467 /* Remember the object. */
468 && (result
= add_object (data
, &st
)) == 0))
469 result
= ftw_dir (data
, &st
, dir
);
472 result
= (*data
->func
) (data
->dirbuf
, &st
, data
->cvt_arr
[flag
],
476 if ((data
->flags
& FTW_ACTIONRETVAL
) && result
== FTW_SKIP_SUBTREE
)
484 __attribute ((noinline
))
485 ftw_dir (struct ftw_data
*data
, struct STRUCT_STAT
*st
, struct dir_data
*old_dir
)
489 int previous_base
= data
->ftw
.base
;
493 /* Open the stream for this directory. This might require that
494 another stream has to be closed. */
495 result
= open_dir_stream (old_dir
== NULL
? NULL
: &old_dir
->streamfd
,
500 /* We cannot read the directory. Signal this with a special flag. */
501 result
= (*data
->func
) (data
->dirbuf
, st
, FTW_DNR
, &data
->ftw
);
506 /* First, report the directory (if not depth-first). */
507 if (!(data
->flags
& FTW_DEPTH
))
509 result
= (*data
->func
) (data
->dirbuf
, st
, FTW_D
, &data
->ftw
);
515 __closedir (dir
.stream
);
517 __set_errno (save_err
);
519 if (data
->actdir
-- == 0)
520 data
->actdir
= data
->maxdir
- 1;
521 data
->dirstreams
[data
->actdir
] = NULL
;
526 /* If necessary, change to this directory. */
527 if (data
->flags
& FTW_CHDIR
)
529 if (__fchdir (__dirfd (dir
.stream
)) < 0)
536 /* Next, update the `struct FTW' information. */
538 startp
= strchr (data
->dirbuf
, '\0');
539 /* There always must be a directory name. */
540 assert (startp
!= data
->dirbuf
);
541 if (startp
[-1] != '/')
543 data
->ftw
.base
= startp
- data
->dirbuf
;
545 while (dir
.stream
!= NULL
&& (d
= __readdir64 (dir
.stream
)) != NULL
)
547 int d_type
= DT_UNKNOWN
;
548 #ifdef _DIRENT_HAVE_D_TYPE
551 result
= process_entry (data
, &dir
, d
->d_name
, NAMLEN (d
), d_type
);
556 if (dir
.stream
!= NULL
)
558 /* The stream is still open. I.e., we did not need more
559 descriptors. Simply close the stream now. */
560 int save_err
= errno
;
562 assert (dir
.content
== NULL
);
564 __closedir (dir
.stream
);
566 __set_errno (save_err
);
568 if (data
->actdir
-- == 0)
569 data
->actdir
= data
->maxdir
- 1;
570 data
->dirstreams
[data
->actdir
] = NULL
;
575 char *runp
= dir
.content
;
577 while (result
== 0 && *runp
!= '\0')
579 char *endp
= strchr (runp
, '\0');
581 // XXX Should store the d_type values as well?!
582 result
= process_entry (data
, &dir
, runp
, endp
- runp
, DT_UNKNOWN
);
589 __set_errno (save_err
);
592 if ((data
->flags
& FTW_ACTIONRETVAL
) && result
== FTW_SKIP_SIBLINGS
)
595 /* Prepare the return, revert the `struct FTW' information. */
596 data
->dirbuf
[data
->ftw
.base
- 1] = '\0';
598 data
->ftw
.base
= previous_base
;
600 /* Finally, if we process depth-first report the directory. */
601 if (result
== 0 && (data
->flags
& FTW_DEPTH
))
602 result
= (*data
->func
) (data
->dirbuf
, st
, FTW_DP
, &data
->ftw
);
605 && (data
->flags
& FTW_CHDIR
)
607 || ((data
->flags
& FTW_ACTIONRETVAL
)
608 && (result
!= -1 && result
!= FTW_STOP
))))
610 /* Change back to the parent directory. */
612 if (old_dir
->stream
!= NULL
)
613 if (__fchdir (__dirfd (old_dir
->stream
)) == 0)
618 if (data
->ftw
.base
== 1)
620 if (__chdir ("/") < 0)
624 if (__chdir ("..") < 0)
634 __attribute ((noinline
))
635 ftw_startup (const char *dir
, int is_nftw
, void *func
, int descriptors
,
638 struct ftw_data data
= { .dirstreams
= NULL
};
639 struct STRUCT_STAT st
;
646 /* First make sure the parameters are reasonable. */
649 __set_errno (ENOENT
);
653 data
.maxdir
= descriptors
< 1 ? 1 : descriptors
;
655 /* PATH_MAX is always defined when we get here. */
656 if (!ftw_allocate (&data
, MAX (2 * strlen (dir
), PATH_MAX
)))
658 memset (data
.dirstreams
, '\0', data
.maxdir
* sizeof (struct dir_data
*));
659 cp
= __stpcpy (data
.dirbuf
, dir
);
660 /* Strip trailing slashes. */
661 while (cp
> data
.dirbuf
+ 1 && cp
[-1] == '/')
668 while (cp
> data
.dirbuf
&& cp
[-1] != '/')
670 data
.ftw
.base
= cp
- data
.dirbuf
;
674 /* This assignment might seem to be strange but it is what we want.
675 The trick is that the first three arguments to the `ftw' and
676 `nftw' callback functions are equal. Therefore we can call in
677 every case the callback using the format of the `nftw' version
678 and get the correct result since the stack layout for a function
679 call in C allows this. */
680 data
.func
= (NFTW_FUNC_T
) func
;
682 /* Since we internally use the complete set of FTW_* values we need
683 to reduce the value range before calling a `ftw' callback. */
684 data
.cvt_arr
= is_nftw
? nftw_arr
: ftw_arr
;
686 /* No object known so far. */
687 data
.known_objects
= NULL
;
689 /* Now go to the directory containing the initial file/directory. */
690 if (flags
& FTW_CHDIR
)
692 /* We have to be able to go back to the current working
693 directory. The best way to do this is to use a file
695 cwdfd
= __open (".", O_RDONLY
| O_DIRECTORY
);
698 /* Try getting the directory name. This can be needed if
699 the current directory is executable but not readable. */
701 /* GNU extension ahead. */
702 cwd
= __getcwd (NULL
, 0);
707 else if (data
.maxdir
> 1)
708 /* Account for the file descriptor we use here. */
711 if (data
.ftw
.base
> 0)
713 /* Change to the directory the file is in. In data.dirbuf
714 we have a writable copy of the file name. Just NUL
715 terminate it for now and change the directory. */
716 if (data
.ftw
.base
== 1)
717 /* I.e., the file is in the root directory. */
718 result
= __chdir ("/");
721 char ch
= data
.dirbuf
[data
.ftw
.base
- 1];
722 data
.dirbuf
[data
.ftw
.base
- 1] = '\0';
723 result
= __chdir (data
.dirbuf
);
724 data
.dirbuf
[data
.ftw
.base
- 1] = ch
;
729 /* Get stat info for start directory. */
734 if (data
.flags
& FTW_CHDIR
)
736 name
= data
.dirbuf
+ data
.ftw
.base
;
743 if (((flags
& FTW_PHYS
)
745 : STAT (name
, &st
)) < 0)
747 if (!(flags
& FTW_PHYS
)
749 && LSTAT (name
, &st
) == 0
750 && S_ISLNK (st
.st_mode
))
751 result
= (*data
.func
) (data
.dirbuf
, &st
, data
.cvt_arr
[FTW_SLN
],
754 /* No need to call the callback since we cannot say anything
760 if (S_ISDIR (st
.st_mode
))
762 /* Remember the device of the initial directory in case
763 FTW_MOUNT is given. */
764 data
.dev
= st
.st_dev
;
766 /* We know this directory now. */
767 if (!(flags
& FTW_PHYS
))
768 result
= add_object (&data
, &st
);
771 result
= ftw_dir (&data
, &st
, NULL
);
775 int flag
= S_ISLNK (st
.st_mode
) ? FTW_SL
: FTW_F
;
777 result
= (*data
.func
) (data
.dirbuf
, &st
, data
.cvt_arr
[flag
],
782 if ((flags
& FTW_ACTIONRETVAL
)
783 && (result
== FTW_SKIP_SUBTREE
|| result
== FTW_SKIP_SIBLINGS
))
787 /* Return to the start directory (if necessary). */
790 int save_err
= errno
;
792 __close_nocancel_nostatus (cwdfd
);
793 __set_errno (save_err
);
795 else if (cwd
!= NULL
)
797 int save_err
= errno
;
800 __set_errno (save_err
);
803 /* Free all memory. */
806 __tdestroy (data
.known_objects
, free
);
807 free (data
.dirstreams
);
808 __set_errno (save_err
);
818 FTW_NAME (const char *path
, FTW_FUNC_T func
, int descriptors
)
820 return ftw_startup (path
, 0, func
, descriptors
, 0);
823 #ifndef NFTW_OLD_NAME
825 NFTW_NAME (const char *path
, NFTW_FUNC_T func
, int descriptors
, int flags
)
827 return ftw_startup (path
, 1, func
, descriptors
, flags
);
831 # include <shlib-compat.h>
833 int NFTW_NEW_NAME (const char *, NFTW_FUNC_T
, int, int);
836 NFTW_NEW_NAME (const char *path
, NFTW_FUNC_T func
, int descriptors
, int flags
)
839 & ~(FTW_PHYS
| FTW_MOUNT
| FTW_CHDIR
| FTW_DEPTH
| FTW_ACTIONRETVAL
))
841 __set_errno (EINVAL
);
844 return ftw_startup (path
, 1, func
, descriptors
, flags
);
846 versioned_symbol (libc
, NFTW_NEW_NAME
, NFTW_NAME
, GLIBC_2_3_3
);
848 # if SHLIB_COMPAT(libc, GLIBC_2_1, GLIBC_2_3_3)
850 /* Older nftw* version just ignored all unknown flags. */
852 int NFTW_OLD_NAME (const char *, NFTW_FUNC_T
, int, int);
855 attribute_compat_text_section
856 NFTW_OLD_NAME (const char *path
, NFTW_FUNC_T func
, int descriptors
, int flags
)
858 flags
&= (FTW_PHYS
| FTW_MOUNT
| FTW_CHDIR
| FTW_DEPTH
);
859 return ftw_startup (path
, 1, func
, descriptors
, flags
);
862 compat_symbol (libc
, NFTW_OLD_NAME
, NFTW_NAME
, GLIBC_2_1
);
864 #endif /* NFTW_OLD_NAME */