1 /* File tree walker functions.
2 Copyright (C) 1996-2015 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
112 # undef internal_function
113 # define internal_function /* empty */
115 # define dirent64 dirent
117 # define MAX(a, b) ((a) > (b) ? (a) : (b))
120 /* Arrange to make lstat calls go through the wrapper function
121 on systems with an lstat function that does not dereference symlinks
122 that are specified with a trailing slash. */
123 #if ! _LIBC && ! LSTAT_FOLLOWS_SLASHED_SYMLINK
124 int rpl_lstat (const char *, struct stat *);
126 # define lstat(Name, Stat_buf) rpl_lstat(Name, Stat_buf)
130 # define __set_errno(Val) errno = (Val)
133 /* Support for the LFS API version. */
135 # define FTW_NAME ftw
136 # define NFTW_NAME nftw
137 # define NFTW_OLD_NAME __old_nftw
138 # define NFTW_NEW_NAME __new_nftw
142 # define LXSTAT __lxstat
143 # define XSTAT __xstat
144 # define FXSTATAT __fxstatat
146 # define LXSTAT(V,f,sb) lstat (f,sb)
147 # define XSTAT(V,f,sb) stat (f,sb)
148 # define FXSTATAT(V,d,f,sb,m) fstatat (d, f, sb, m)
150 # define FTW_FUNC_T __ftw_func_t
151 # define NFTW_FUNC_T __nftw_func_t
154 /* We define PATH_MAX if the system does not provide a definition.
155 This does not artificially limit any operation. PATH_MAX is simply
156 used as a guesstimate for the expected maximal path length.
157 Buffers will be enlarged if necessary. */
159 # define PATH_MAX 1024
177 /* Array with pointers to open directory streams. */
178 struct dir_data **dirstreams;
182 /* Buffer containing name of currently processed object. */
186 /* Passed as fourth argument to `nftw' callback. The `base' member
187 tracks the content of the `dirbuf'. */
190 /* Flags passed to `nftw' function. 0 for `ftw'. */
193 /* Conversion array for flag values. It is the identity mapping for
194 `nftw' calls, otherwise it maps the values to those known by
198 /* Callback function. We always use the `nftw' form. */
201 /* Device of starting point. Needed for FTW_MOUNT. */
204 /* Data structure for keeping fingerprints of already processed
205 object. This is needed when not using FTW_PHYS. */
210 /* Internally we use the FTW_* constants used for `nftw'. When invoked
211 as `ftw', map each flag to the subset of values used by `ftw'. */
212 static const int nftw_arr[] =
214 FTW_F, FTW_D, FTW_DNR, FTW_NS, FTW_SL, FTW_DP, FTW_SLN
217 static const int ftw_arr[] =
219 FTW_F, FTW_D, FTW_DNR, FTW_NS, FTW_F, FTW_D, FTW_NS
223 /* Forward declarations of local functions. */
224 static int ftw_dir (struct ftw_data *data, struct STAT *st,
225 struct dir_data *old_dir) internal_function;
229 object_compare (const void *p1, const void *p2)
231 /* We don't need a sophisticated and useful comparison. We are only
232 interested in equality. However, we must be careful not to
233 accidentally compare `holes' in the structure. */
234 const struct known_object *kp1 = p1, *kp2 = p2;
236 cmp1 = (kp1->ino > kp2->ino) - (kp1->ino < kp2->ino);
239 return (kp1->dev > kp2->dev) - (kp1->dev < kp2->dev);
244 add_object (struct ftw_data *data, struct STAT *st)
246 struct known_object *newp = malloc (sizeof (struct known_object));
249 newp->dev = st->st_dev;
250 newp->ino = st->st_ino;
251 return __tsearch (newp, &data->known_objects, object_compare) ? 0 : -1;
256 find_object (struct ftw_data *data, struct STAT *st)
258 struct known_object obj;
259 obj.dev = st->st_dev;
260 obj.ino = st->st_ino;
261 return __tfind (&obj, &data->known_objects, object_compare) != NULL;
266 __attribute ((always_inline))
267 open_dir_stream (int *dfdp, struct ftw_data *data, struct dir_data *dirp)
271 if (data->dirstreams[data->actdir] != NULL)
273 /* Oh, oh. We must close this stream. Get all remaining
274 entries and store them as a list in the `content' member of
275 the `struct dir_data' variable. */
276 size_t bufsize = 1024;
277 char *buf = malloc (bufsize);
283 DIR *st = data->dirstreams[data->actdir]->stream;
287 while ((d = __readdir64 (st)) != NULL)
289 size_t this_len = NAMLEN (d);
290 if (actsize + this_len + 2 >= bufsize)
293 bufsize += MAX (1024, 2 * this_len);
294 newp = (char *) realloc (buf, bufsize);
297 /* No more memory. */
298 int save_err = errno;
300 __set_errno (save_err);
306 *((char *) __mempcpy (buf + actsize, d->d_name, this_len))
308 actsize += this_len + 1;
311 /* Terminate the list with an additional NUL byte. */
312 buf[actsize++] = '\0';
314 /* Shrink the buffer to what we actually need. */
315 data->dirstreams[data->actdir]->content = realloc (buf, actsize);
316 if (data->dirstreams[data->actdir]->content == NULL)
318 int save_err = errno;
320 __set_errno (save_err);
326 data->dirstreams[data->actdir]->stream = NULL;
327 data->dirstreams[data->actdir]->streamfd = -1;
328 data->dirstreams[data->actdir] = NULL;
333 /* Open the new stream. */
336 assert (data->dirstreams[data->actdir] == NULL);
338 if (dfdp != NULL && *dfdp != -1)
340 int fd = openat64_not_cancel_3 (*dfdp, data->dirbuf + data->ftw.base,
341 O_RDONLY | O_DIRECTORY | O_NDELAY);
343 if (fd != -1 && (dirp->stream = __fdopendir (fd)) == NULL)
344 close_not_cancel_no_status (fd);
350 if (data->flags & FTW_CHDIR)
352 name = data->dirbuf + data->ftw.base;
359 dirp->stream = __opendir (name);
362 if (dirp->stream == NULL)
366 dirp->streamfd = dirfd (dirp->stream);
367 dirp->content = NULL;
368 data->dirstreams[data->actdir] = dirp;
370 if (++data->actdir == data->maxdir)
381 process_entry (struct ftw_data *data, struct dir_data *dir, const char *name,
382 size_t namlen, int d_type)
389 if (name[0] == '.' && (name[1] == '\0'
390 || (name[1] == '.' && name[2] == '\0')))
391 /* Don't process the "." and ".." entries. */
394 new_buflen = data->ftw.base + namlen + 2;
395 if (data->dirbufsize < new_buflen)
397 /* Enlarge the buffer. */
400 data->dirbufsize = 2 * new_buflen;
401 newp = (char *) realloc (data->dirbuf, data->dirbufsize);
407 *((char *) __mempcpy (data->dirbuf + data->ftw.base, name, namlen)) = '\0';
410 if (dir->streamfd != -1)
411 statres = FXSTATAT (_STAT_VER, dir->streamfd, name, &st,
412 (data->flags & FTW_PHYS) ? AT_SYMLINK_NOFOLLOW : 0);
415 if ((data->flags & FTW_CHDIR) == 0)
418 statres = ((data->flags & FTW_PHYS)
419 ? LXSTAT (_STAT_VER, name, &st)
420 : XSTAT (_STAT_VER, name, &st));
425 if (errno != EACCES && errno != ENOENT)
427 else if (data->flags & FTW_PHYS)
429 else if (d_type == DT_LNK)
433 if (dir->streamfd != -1)
434 statres = FXSTATAT (_STAT_VER, dir->streamfd, name, &st,
435 AT_SYMLINK_NOFOLLOW);
437 statres = LXSTAT (_STAT_VER, name, &st);
438 if (statres == 0 && S_ISLNK (st.st_mode))
446 if (S_ISDIR (st.st_mode))
448 else if (S_ISLNK (st.st_mode))
456 || !(data->flags & FTW_MOUNT) || st.st_dev == data->dev))
460 if ((data->flags & FTW_PHYS)
461 || (!find_object (data, &st)
462 /* Remember the object. */
463 && (result = add_object (data, &st)) == 0))
464 result = ftw_dir (data, &st, dir);
467 result = (*data->func) (data->dirbuf, &st, data->cvt_arr[flag],
471 if ((data->flags & FTW_ACTIONRETVAL) && result == FTW_SKIP_SUBTREE)
479 __attribute ((noinline))
481 ftw_dir (struct ftw_data *data, struct STAT *st, struct dir_data *old_dir)
485 int previous_base = data->ftw.base;
489 /* Open the stream for this directory. This might require that
490 another stream has to be closed. */
491 result = open_dir_stream (old_dir == NULL ? NULL : &old_dir->streamfd,
496 /* We cannot read the directory. Signal this with a special flag. */
497 result = (*data->func) (data->dirbuf, st, FTW_DNR, &data->ftw);
502 /* First, report the directory (if not depth-first). */
503 if (!(data->flags & FTW_DEPTH))
505 result = (*data->func) (data->dirbuf, st, FTW_D, &data->ftw);
511 __closedir (dir.stream);
513 __set_errno (save_err);
515 if (data->actdir-- == 0)
516 data->actdir = data->maxdir - 1;
517 data->dirstreams[data->actdir] = NULL;
522 /* If necessary, change to this directory. */
523 if (data->flags & FTW_CHDIR)
525 if (__fchdir (dirfd (dir.stream)) < 0)
532 /* Next, update the `struct FTW' information. */
534 startp = __rawmemchr (data->dirbuf, '\0');
535 /* There always must be a directory name. */
536 assert (startp != data->dirbuf);
537 if (startp[-1] != '/')
539 data->ftw.base = startp - data->dirbuf;
541 while (dir.stream != NULL && (d = __readdir64 (dir.stream)) != NULL)
543 int d_type = DT_UNKNOWN;
544 #ifdef _DIRENT_HAVE_D_TYPE
547 result = process_entry (data, &dir, d->d_name, NAMLEN (d), d_type);
552 if (dir.stream != NULL)
554 /* The stream is still open. I.e., we did not need more
555 descriptors. Simply close the stream now. */
556 int save_err = errno;
558 assert (dir.content == NULL);
560 __closedir (dir.stream);
562 __set_errno (save_err);
564 if (data->actdir-- == 0)
565 data->actdir = data->maxdir - 1;
566 data->dirstreams[data->actdir] = NULL;
571 char *runp = dir.content;
573 while (result == 0 && *runp != '\0')
575 char *endp = strchr (runp, '\0');
577 // XXX Should store the d_type values as well?!
578 result = process_entry (data, &dir, runp, endp - runp, DT_UNKNOWN);
585 __set_errno (save_err);
588 if ((data->flags & FTW_ACTIONRETVAL) && result == FTW_SKIP_SIBLINGS)
591 /* Prepare the return, revert the `struct FTW' information. */
592 data->dirbuf[data->ftw.base - 1] = '\0';
594 data->ftw.base = previous_base;
596 /* Finally, if we process depth-first report the directory. */
597 if (result == 0 && (data->flags & FTW_DEPTH))
598 result = (*data->func) (data->dirbuf, st, FTW_DP, &data->ftw);
601 && (data->flags & FTW_CHDIR)
603 || ((data->flags & FTW_ACTIONRETVAL)
604 && (result != -1 && result != FTW_STOP))))
606 /* Change back to the parent directory. */
608 if (old_dir->stream != NULL)
609 if (__fchdir (dirfd (old_dir->stream)) == 0)
614 if (data->ftw.base == 1)
616 if (__chdir ("/") < 0)
620 if (__chdir ("..") < 0)
630 __attribute ((noinline))
632 ftw_startup (const char *dir, int is_nftw, void *func, int descriptors,
635 struct ftw_data data;
643 /* First make sure the parameters are reasonable. */
646 __set_errno (ENOENT);
650 data.maxdir = descriptors < 1 ? 1 : descriptors;
652 data.dirstreams = (struct dir_data **) alloca (data.maxdir
653 * sizeof (struct dir_data *));
654 memset (data.dirstreams, '\0', data.maxdir * sizeof (struct dir_data *));
656 /* PATH_MAX is always defined when we get here. */
657 data.dirbufsize = MAX (2 * strlen (dir), PATH_MAX);
658 data.dirbuf = (char *) malloc (data.dirbufsize);
659 if (data.dirbuf == NULL)
661 cp = __stpcpy (data.dirbuf, dir);
662 /* Strip trailing slashes. */
663 while (cp > data.dirbuf + 1 && cp[-1] == '/')
670 while (cp > data.dirbuf && cp[-1] != '/')
672 data.ftw.base = cp - data.dirbuf;
676 /* This assignment might seem to be strange but it is what we want.
677 The trick is that the first three arguments to the `ftw' and
678 `nftw' callback functions are equal. Therefore we can call in
679 every case the callback using the format of the `nftw' version
680 and get the correct result since the stack layout for a function
681 call in C allows this. */
682 data.func = (NFTW_FUNC_T) func;
684 /* Since we internally use the complete set of FTW_* values we need
685 to reduce the value range before calling a `ftw' callback. */
686 data.cvt_arr = is_nftw ? nftw_arr : ftw_arr;
688 /* No object known so far. */
689 data.known_objects = NULL;
691 /* Now go to the directory containing the initial file/directory. */
692 if (flags & FTW_CHDIR)
694 /* We have to be able to go back to the current working
695 directory. The best way to do this is to use a file
697 cwdfd = __open (".", O_RDONLY | O_DIRECTORY);
700 /* Try getting the directory name. This can be needed if
701 the current directory is executable but not readable. */
703 /* GNU extension ahead. */
704 cwd = __getcwd (NULL, 0);
709 else if (data.maxdir > 1)
710 /* Account for the file descriptor we use here. */
713 if (data.ftw.base > 0)
715 /* Change to the directory the file is in. In data.dirbuf
716 we have a writable copy of the file name. Just NUL
717 terminate it for now and change the directory. */
718 if (data.ftw.base == 1)
719 /* I.e., the file is in the root directory. */
720 result = __chdir ("/");
723 char ch = data.dirbuf[data.ftw.base - 1];
724 data.dirbuf[data.ftw.base - 1] = '\0';
725 result = __chdir (data.dirbuf);
726 data.dirbuf[data.ftw.base - 1] = ch;
731 /* Get stat info for start directory. */
736 if (data.flags & FTW_CHDIR)
738 name = data.dirbuf + data.ftw.base;
745 if (((flags & FTW_PHYS)
746 ? LXSTAT (_STAT_VER, name, &st)
747 : XSTAT (_STAT_VER, name, &st)) < 0)
749 if (!(flags & FTW_PHYS)
751 && LXSTAT (_STAT_VER, name, &st) == 0
752 && S_ISLNK (st.st_mode))
753 result = (*data.func) (data.dirbuf, &st, data.cvt_arr[FTW_SLN],
756 /* No need to call the callback since we cannot say anything
762 if (S_ISDIR (st.st_mode))
764 /* Remember the device of the initial directory in case
765 FTW_MOUNT is given. */
766 data.dev = st.st_dev;
768 /* We know this directory now. */
769 if (!(flags & FTW_PHYS))
770 result = add_object (&data, &st);
773 result = ftw_dir (&data, &st, NULL);
777 int flag = S_ISLNK (st.st_mode) ? FTW_SL : FTW_F;
779 result = (*data.func) (data.dirbuf, &st, data.cvt_arr[flag],
784 if ((flags & FTW_ACTIONRETVAL)
785 && (result == FTW_SKIP_SUBTREE || result == FTW_SKIP_SIBLINGS))
789 /* Return to the start directory (if necessary). */
792 int save_err = errno;
794 close_not_cancel_no_status (cwdfd);
795 __set_errno (save_err);
797 else if (cwd != NULL)
799 int save_err = errno;
802 __set_errno (save_err);
805 /* Free all memory. */
808 __tdestroy (data.known_objects, free);
810 __set_errno (save_err);
820 FTW_NAME (path, func, descriptors)
825 return ftw_startup (path, 0, func, descriptors, 0);
830 NFTW_NAME (path, func, descriptors, flags)
836 return ftw_startup (path, 1, func, descriptors, flags);
840 # include <shlib-compat.h>
842 int NFTW_NEW_NAME (const char *, NFTW_FUNC_T, int, int);
845 NFTW_NEW_NAME (path, func, descriptors, flags)
852 & ~(FTW_PHYS | FTW_MOUNT | FTW_CHDIR | FTW_DEPTH | FTW_ACTIONRETVAL))
854 __set_errno (EINVAL);
857 return ftw_startup (path, 1, func, descriptors, flags);
860 versioned_symbol (libc, NFTW_NEW_NAME, NFTW_NAME, GLIBC_2_3_3);
862 # if SHLIB_COMPAT(libc, GLIBC_2_1, GLIBC_2_3_3)
864 /* Older nftw* version just ignored all unknown flags. */
866 int NFTW_OLD_NAME (const char *, NFTW_FUNC_T, int, int);
869 attribute_compat_text_section
870 NFTW_OLD_NAME (path, func, descriptors, flags)
876 flags &= (FTW_PHYS | FTW_MOUNT | FTW_CHDIR | FTW_DEPTH);
877 return ftw_startup (path, 1, func, descriptors, flags);
880 compat_symbol (libc, NFTW_OLD_NAME, NFTW_NAME, GLIBC_2_1);