1 /* File tree walker functions.
2 Copyright (C) 1996-2018 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
25 # define alloca __builtin_alloca
40 # define NAMLEN(dirent) _D_EXACT_NAMLEN (dirent)
44 # define NAMLEN(dirent) strlen ((dirent)->d_name)
46 # define dirent direct
47 # define NAMLEN(dirent) (dirent)->d_namlen
49 # include <sys/ndir.h>
68 #include <not-cancel.h>
69 #include <sys/param.h>
71 # include <include/sys/stat.h>
73 # include <sys/stat.h>
76 #if ! _LIBC && !HAVE_DECL_STPCPY && !defined stpcpy
80 #if ! _LIBC && ! defined HAVE_MEMPCPY && ! defined mempcpy
81 /* Be CAREFUL that there are no side effects in N. */
82 # define mempcpy(D, S, N) ((void *) ((char *) memcpy (D, S, N) + (N)))
85 /* #define NDEBUG 1 */
90 # define __chdir chdir
92 # define __closedir closedir
94 # define __fchdir fchdir
96 # define __getcwd(P, N) xgetcwd ()
97 extern char *xgetcwd (void);
99 # define __mempcpy mempcpy
101 # define __opendir opendir
103 # define __readdir64 readdir
105 # define __stpcpy stpcpy
107 # define __tdestroy tdestroy
109 # define __tfind tfind
111 # define __tsearch tsearch
113 # define dirent64 dirent
115 # define MAX(a, b) ((a) > (b) ? (a) : (b))
118 /* Arrange to make lstat calls go through the wrapper function
119 on systems with an lstat function that does not dereference symlinks
120 that are specified with a trailing slash. */
121 #if ! _LIBC && ! LSTAT_FOLLOWS_SLASHED_SYMLINK
122 int rpl_lstat (const char *, struct stat
*);
124 # define lstat(Name, Stat_buf) rpl_lstat(Name, Stat_buf)
128 # define __set_errno(Val) errno = (Val)
131 /* Support for the LFS API version. */
133 # define FTW_NAME ftw
134 # define NFTW_NAME nftw
135 # define NFTW_OLD_NAME __old_nftw
136 # define NFTW_NEW_NAME __new_nftw
140 # define LXSTAT __lxstat
141 # define XSTAT __xstat
142 # define FXSTATAT __fxstatat
144 # define LXSTAT(V,f,sb) lstat (f,sb)
145 # define XSTAT(V,f,sb) stat (f,sb)
146 # define FXSTATAT(V,d,f,sb,m) fstatat (d, f, sb, m)
148 # define FTW_FUNC_T __ftw_func_t
149 # define NFTW_FUNC_T __nftw_func_t
152 /* We define PATH_MAX if the system does not provide a definition.
153 This does not artificially limit any operation. PATH_MAX is simply
154 used as a guesstimate for the expected maximal path length.
155 Buffers will be enlarged if necessary. */
157 # define PATH_MAX 1024
175 /* Array with pointers to open directory streams. */
176 struct dir_data
**dirstreams
;
180 /* Buffer containing name of currently processed object. */
184 /* Passed as fourth argument to `nftw' callback. The `base' member
185 tracks the content of the `dirbuf'. */
188 /* Flags passed to `nftw' function. 0 for `ftw'. */
191 /* Conversion array for flag values. It is the identity mapping for
192 `nftw' calls, otherwise it maps the values to those known by
196 /* Callback function. We always use the `nftw' form. */
199 /* Device of starting point. Needed for FTW_MOUNT. */
202 /* Data structure for keeping fingerprints of already processed
203 object. This is needed when not using FTW_PHYS. */
208 /* Internally we use the FTW_* constants used for `nftw'. When invoked
209 as `ftw', map each flag to the subset of values used by `ftw'. */
210 static const int nftw_arr
[] =
212 FTW_F
, FTW_D
, FTW_DNR
, FTW_NS
, FTW_SL
, FTW_DP
, FTW_SLN
215 static const int ftw_arr
[] =
217 FTW_F
, FTW_D
, FTW_DNR
, FTW_NS
, FTW_F
, FTW_D
, FTW_NS
221 /* Forward declarations of local functions. */
222 static int ftw_dir (struct ftw_data
*data
, struct STAT
*st
,
223 struct dir_data
*old_dir
);
227 object_compare (const void *p1
, const void *p2
)
229 /* We don't need a sophisticated and useful comparison. We are only
230 interested in equality. However, we must be careful not to
231 accidentally compare `holes' in the structure. */
232 const struct known_object
*kp1
= p1
, *kp2
= p2
;
234 cmp1
= (kp1
->ino
> kp2
->ino
) - (kp1
->ino
< kp2
->ino
);
237 return (kp1
->dev
> kp2
->dev
) - (kp1
->dev
< kp2
->dev
);
242 add_object (struct ftw_data
*data
, struct STAT
*st
)
244 struct known_object
*newp
= malloc (sizeof (struct known_object
));
247 newp
->dev
= st
->st_dev
;
248 newp
->ino
= st
->st_ino
;
249 return __tsearch (newp
, &data
->known_objects
, object_compare
) ? 0 : -1;
254 find_object (struct ftw_data
*data
, struct STAT
*st
)
256 struct known_object obj
;
257 obj
.dev
= st
->st_dev
;
258 obj
.ino
= st
->st_ino
;
259 return __tfind (&obj
, &data
->known_objects
, object_compare
) != NULL
;
264 __attribute ((always_inline
))
265 open_dir_stream (int *dfdp
, struct ftw_data
*data
, struct dir_data
*dirp
)
269 if (data
->dirstreams
[data
->actdir
] != NULL
)
271 /* Oh, oh. We must close this stream. Get all remaining
272 entries and store them as a list in the `content' member of
273 the `struct dir_data' variable. */
274 size_t bufsize
= 1024;
275 char *buf
= malloc (bufsize
);
281 DIR *st
= data
->dirstreams
[data
->actdir
]->stream
;
285 while ((d
= __readdir64 (st
)) != NULL
)
287 size_t this_len
= NAMLEN (d
);
288 if (actsize
+ this_len
+ 2 >= bufsize
)
291 bufsize
+= MAX (1024, 2 * this_len
);
292 newp
= (char *) realloc (buf
, bufsize
);
295 /* No more memory. */
296 int save_err
= errno
;
298 __set_errno (save_err
);
304 *((char *) __mempcpy (buf
+ actsize
, d
->d_name
, this_len
))
306 actsize
+= this_len
+ 1;
309 /* Terminate the list with an additional NUL byte. */
310 buf
[actsize
++] = '\0';
312 /* Shrink the buffer to what we actually need. */
313 data
->dirstreams
[data
->actdir
]->content
= realloc (buf
, actsize
);
314 if (data
->dirstreams
[data
->actdir
]->content
== NULL
)
316 int save_err
= errno
;
318 __set_errno (save_err
);
324 data
->dirstreams
[data
->actdir
]->stream
= NULL
;
325 data
->dirstreams
[data
->actdir
]->streamfd
= -1;
326 data
->dirstreams
[data
->actdir
] = NULL
;
331 /* Open the new stream. */
334 assert (data
->dirstreams
[data
->actdir
] == NULL
);
336 if (dfdp
!= NULL
&& *dfdp
!= -1)
338 int fd
= __openat64_nocancel (*dfdp
, data
->dirbuf
+ data
->ftw
.base
,
339 O_RDONLY
| O_DIRECTORY
| O_NDELAY
);
341 if (fd
!= -1 && (dirp
->stream
= __fdopendir (fd
)) == NULL
)
342 __close_nocancel_nostatus (fd
);
348 if (data
->flags
& FTW_CHDIR
)
350 name
= data
->dirbuf
+ data
->ftw
.base
;
357 dirp
->stream
= __opendir (name
);
360 if (dirp
->stream
== NULL
)
364 dirp
->streamfd
= __dirfd (dirp
->stream
);
365 dirp
->content
= NULL
;
366 data
->dirstreams
[data
->actdir
] = dirp
;
368 if (++data
->actdir
== data
->maxdir
)
378 process_entry (struct ftw_data
*data
, struct dir_data
*dir
, const char *name
,
379 size_t namlen
, int d_type
)
386 if (name
[0] == '.' && (name
[1] == '\0'
387 || (name
[1] == '.' && name
[2] == '\0')))
388 /* Don't process the "." and ".." entries. */
391 new_buflen
= data
->ftw
.base
+ namlen
+ 2;
392 if (data
->dirbufsize
< new_buflen
)
394 /* Enlarge the buffer. */
397 data
->dirbufsize
= 2 * new_buflen
;
398 newp
= (char *) realloc (data
->dirbuf
, data
->dirbufsize
);
404 *((char *) __mempcpy (data
->dirbuf
+ data
->ftw
.base
, name
, namlen
)) = '\0';
407 if (dir
->streamfd
!= -1)
408 statres
= FXSTATAT (_STAT_VER
, dir
->streamfd
, name
, &st
,
409 (data
->flags
& FTW_PHYS
) ? AT_SYMLINK_NOFOLLOW
: 0);
412 if ((data
->flags
& FTW_CHDIR
) == 0)
415 statres
= ((data
->flags
& FTW_PHYS
)
416 ? LXSTAT (_STAT_VER
, name
, &st
)
417 : XSTAT (_STAT_VER
, name
, &st
));
422 if (errno
!= EACCES
&& errno
!= ENOENT
)
424 else if (data
->flags
& FTW_PHYS
)
426 else if (d_type
== DT_LNK
)
430 if (dir
->streamfd
!= -1)
431 statres
= FXSTATAT (_STAT_VER
, dir
->streamfd
, name
, &st
,
432 AT_SYMLINK_NOFOLLOW
);
434 statres
= LXSTAT (_STAT_VER
, name
, &st
);
435 if (statres
== 0 && S_ISLNK (st
.st_mode
))
443 if (S_ISDIR (st
.st_mode
))
445 else if (S_ISLNK (st
.st_mode
))
453 || !(data
->flags
& FTW_MOUNT
) || st
.st_dev
== data
->dev
))
457 if ((data
->flags
& FTW_PHYS
)
458 || (!find_object (data
, &st
)
459 /* Remember the object. */
460 && (result
= add_object (data
, &st
)) == 0))
461 result
= ftw_dir (data
, &st
, dir
);
464 result
= (*data
->func
) (data
->dirbuf
, &st
, data
->cvt_arr
[flag
],
468 if ((data
->flags
& FTW_ACTIONRETVAL
) && result
== FTW_SKIP_SUBTREE
)
476 __attribute ((noinline
))
477 ftw_dir (struct ftw_data
*data
, struct STAT
*st
, struct dir_data
*old_dir
)
481 int previous_base
= data
->ftw
.base
;
485 /* Open the stream for this directory. This might require that
486 another stream has to be closed. */
487 result
= open_dir_stream (old_dir
== NULL
? NULL
: &old_dir
->streamfd
,
492 /* We cannot read the directory. Signal this with a special flag. */
493 result
= (*data
->func
) (data
->dirbuf
, st
, FTW_DNR
, &data
->ftw
);
498 /* First, report the directory (if not depth-first). */
499 if (!(data
->flags
& FTW_DEPTH
))
501 result
= (*data
->func
) (data
->dirbuf
, st
, FTW_D
, &data
->ftw
);
507 __closedir (dir
.stream
);
509 __set_errno (save_err
);
511 if (data
->actdir
-- == 0)
512 data
->actdir
= data
->maxdir
- 1;
513 data
->dirstreams
[data
->actdir
] = NULL
;
518 /* If necessary, change to this directory. */
519 if (data
->flags
& FTW_CHDIR
)
521 if (__fchdir (__dirfd (dir
.stream
)) < 0)
528 /* Next, update the `struct FTW' information. */
530 startp
= __rawmemchr (data
->dirbuf
, '\0');
531 /* There always must be a directory name. */
532 assert (startp
!= data
->dirbuf
);
533 if (startp
[-1] != '/')
535 data
->ftw
.base
= startp
- data
->dirbuf
;
537 while (dir
.stream
!= NULL
&& (d
= __readdir64 (dir
.stream
)) != NULL
)
539 int d_type
= DT_UNKNOWN
;
540 #ifdef _DIRENT_HAVE_D_TYPE
543 result
= process_entry (data
, &dir
, d
->d_name
, NAMLEN (d
), d_type
);
548 if (dir
.stream
!= NULL
)
550 /* The stream is still open. I.e., we did not need more
551 descriptors. Simply close the stream now. */
552 int save_err
= errno
;
554 assert (dir
.content
== NULL
);
556 __closedir (dir
.stream
);
558 __set_errno (save_err
);
560 if (data
->actdir
-- == 0)
561 data
->actdir
= data
->maxdir
- 1;
562 data
->dirstreams
[data
->actdir
] = NULL
;
567 char *runp
= dir
.content
;
569 while (result
== 0 && *runp
!= '\0')
571 char *endp
= strchr (runp
, '\0');
573 // XXX Should store the d_type values as well?!
574 result
= process_entry (data
, &dir
, runp
, endp
- runp
, DT_UNKNOWN
);
581 __set_errno (save_err
);
584 if ((data
->flags
& FTW_ACTIONRETVAL
) && result
== FTW_SKIP_SIBLINGS
)
587 /* Prepare the return, revert the `struct FTW' information. */
588 data
->dirbuf
[data
->ftw
.base
- 1] = '\0';
590 data
->ftw
.base
= previous_base
;
592 /* Finally, if we process depth-first report the directory. */
593 if (result
== 0 && (data
->flags
& FTW_DEPTH
))
594 result
= (*data
->func
) (data
->dirbuf
, st
, FTW_DP
, &data
->ftw
);
597 && (data
->flags
& FTW_CHDIR
)
599 || ((data
->flags
& FTW_ACTIONRETVAL
)
600 && (result
!= -1 && result
!= FTW_STOP
))))
602 /* Change back to the parent directory. */
604 if (old_dir
->stream
!= NULL
)
605 if (__fchdir (__dirfd (old_dir
->stream
)) == 0)
610 if (data
->ftw
.base
== 1)
612 if (__chdir ("/") < 0)
616 if (__chdir ("..") < 0)
626 __attribute ((noinline
))
627 ftw_startup (const char *dir
, int is_nftw
, void *func
, int descriptors
,
630 struct ftw_data data
;
638 /* First make sure the parameters are reasonable. */
641 __set_errno (ENOENT
);
645 data
.maxdir
= descriptors
< 1 ? 1 : descriptors
;
647 data
.dirstreams
= (struct dir_data
**) alloca (data
.maxdir
648 * sizeof (struct dir_data
*));
649 memset (data
.dirstreams
, '\0', data
.maxdir
* sizeof (struct dir_data
*));
651 /* PATH_MAX is always defined when we get here. */
652 data
.dirbufsize
= MAX (2 * strlen (dir
), PATH_MAX
);
653 data
.dirbuf
= (char *) malloc (data
.dirbufsize
);
654 if (data
.dirbuf
== NULL
)
656 cp
= __stpcpy (data
.dirbuf
, dir
);
657 /* Strip trailing slashes. */
658 while (cp
> data
.dirbuf
+ 1 && cp
[-1] == '/')
665 while (cp
> data
.dirbuf
&& cp
[-1] != '/')
667 data
.ftw
.base
= cp
- data
.dirbuf
;
671 /* This assignment might seem to be strange but it is what we want.
672 The trick is that the first three arguments to the `ftw' and
673 `nftw' callback functions are equal. Therefore we can call in
674 every case the callback using the format of the `nftw' version
675 and get the correct result since the stack layout for a function
676 call in C allows this. */
677 data
.func
= (NFTW_FUNC_T
) func
;
679 /* Since we internally use the complete set of FTW_* values we need
680 to reduce the value range before calling a `ftw' callback. */
681 data
.cvt_arr
= is_nftw
? nftw_arr
: ftw_arr
;
683 /* No object known so far. */
684 data
.known_objects
= NULL
;
686 /* Now go to the directory containing the initial file/directory. */
687 if (flags
& FTW_CHDIR
)
689 /* We have to be able to go back to the current working
690 directory. The best way to do this is to use a file
692 cwdfd
= __open (".", O_RDONLY
| O_DIRECTORY
);
695 /* Try getting the directory name. This can be needed if
696 the current directory is executable but not readable. */
698 /* GNU extension ahead. */
699 cwd
= __getcwd (NULL
, 0);
704 else if (data
.maxdir
> 1)
705 /* Account for the file descriptor we use here. */
708 if (data
.ftw
.base
> 0)
710 /* Change to the directory the file is in. In data.dirbuf
711 we have a writable copy of the file name. Just NUL
712 terminate it for now and change the directory. */
713 if (data
.ftw
.base
== 1)
714 /* I.e., the file is in the root directory. */
715 result
= __chdir ("/");
718 char ch
= data
.dirbuf
[data
.ftw
.base
- 1];
719 data
.dirbuf
[data
.ftw
.base
- 1] = '\0';
720 result
= __chdir (data
.dirbuf
);
721 data
.dirbuf
[data
.ftw
.base
- 1] = ch
;
726 /* Get stat info for start directory. */
731 if (data
.flags
& FTW_CHDIR
)
733 name
= data
.dirbuf
+ data
.ftw
.base
;
740 if (((flags
& FTW_PHYS
)
741 ? LXSTAT (_STAT_VER
, name
, &st
)
742 : XSTAT (_STAT_VER
, name
, &st
)) < 0)
744 if (!(flags
& FTW_PHYS
)
746 && LXSTAT (_STAT_VER
, name
, &st
) == 0
747 && S_ISLNK (st
.st_mode
))
748 result
= (*data
.func
) (data
.dirbuf
, &st
, data
.cvt_arr
[FTW_SLN
],
751 /* No need to call the callback since we cannot say anything
757 if (S_ISDIR (st
.st_mode
))
759 /* Remember the device of the initial directory in case
760 FTW_MOUNT is given. */
761 data
.dev
= st
.st_dev
;
763 /* We know this directory now. */
764 if (!(flags
& FTW_PHYS
))
765 result
= add_object (&data
, &st
);
768 result
= ftw_dir (&data
, &st
, NULL
);
772 int flag
= S_ISLNK (st
.st_mode
) ? FTW_SL
: FTW_F
;
774 result
= (*data
.func
) (data
.dirbuf
, &st
, data
.cvt_arr
[flag
],
779 if ((flags
& FTW_ACTIONRETVAL
)
780 && (result
== FTW_SKIP_SUBTREE
|| result
== FTW_SKIP_SIBLINGS
))
784 /* Return to the start directory (if necessary). */
787 int save_err
= errno
;
789 __close_nocancel_nostatus (cwdfd
);
790 __set_errno (save_err
);
792 else if (cwd
!= NULL
)
794 int save_err
= errno
;
797 __set_errno (save_err
);
800 /* Free all memory. */
803 __tdestroy (data
.known_objects
, free
);
805 __set_errno (save_err
);
815 FTW_NAME (const char *path
, FTW_FUNC_T func
, int descriptors
)
817 return ftw_startup (path
, 0, func
, descriptors
, 0);
822 NFTW_NAME (const char *path
, NFTW_FUNC_T func
, int descriptors
, int flags
)
824 return ftw_startup (path
, 1, func
, descriptors
, flags
);
828 # include <shlib-compat.h>
830 int NFTW_NEW_NAME (const char *, NFTW_FUNC_T
, int, int);
833 NFTW_NEW_NAME (const char *path
, NFTW_FUNC_T func
, int descriptors
, int flags
)
836 & ~(FTW_PHYS
| FTW_MOUNT
| FTW_CHDIR
| FTW_DEPTH
| FTW_ACTIONRETVAL
))
838 __set_errno (EINVAL
);
841 return ftw_startup (path
, 1, func
, descriptors
, flags
);
844 versioned_symbol (libc
, NFTW_NEW_NAME
, NFTW_NAME
, GLIBC_2_3_3
);
846 # if SHLIB_COMPAT(libc, GLIBC_2_1, GLIBC_2_3_3)
848 /* Older nftw* version just ignored all unknown flags. */
850 int NFTW_OLD_NAME (const char *, NFTW_FUNC_T
, int, int);
853 attribute_compat_text_section
854 NFTW_OLD_NAME (const char *path
, NFTW_FUNC_T func
, int descriptors
, int flags
)
856 flags
&= (FTW_PHYS
| FTW_MOUNT
| FTW_CHDIR
| FTW_DEPTH
);
857 return ftw_startup (path
, 1, func
, descriptors
, flags
);
860 compat_symbol (libc
, NFTW_OLD_NAME
, NFTW_NAME
, GLIBC_2_1
);