1 /* File tree walker functions.
2 Copyright (C) 1996-2003, 2004 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 /* need errno.h before undefining _LIBC */
29 #define HAVE_DIRENT_H 1
30 #define HAVE_SYS_PARAM_H 1
31 #define HAVE_DECL_STPCPY 1
32 #define HAVE_MEMPCPY 1
37 # define alloca __builtin_alloca
52 # define NAMLEN(dirent) _D_EXACT_NAMLEN (dirent)
56 # define NAMLEN(dirent) strlen ((dirent)->d_name)
58 # define dirent direct
59 # define NAMLEN(dirent) (dirent)->d_namlen
61 # include <sys/ndir.h>
78 #if HAVE_SYS_PARAM_H || defined _LIBC
79 # include <sys/param.h>
83 #if !defined _LIBC && !HAVE_DECL_STPCPY && !defined stpcpy
87 #if !defined _LIBC && ! defined HAVE_MEMPCPY && ! defined mempcpy
88 /* Be CAREFUL that there are no side effects in N. */
89 # define mempcpy(D, S, N) ((void *) ((char *) memcpy (D, S, N) + (N)))
92 /* #define NDEBUG 1 */
97 # define __chdir chdir
99 # define __closedir closedir
101 # define __fchdir fchdir
104 # define __getcwd(P, N) xgetcwd ()
105 extern char *xgetcwd (void);
107 # define __getcwd getcwd
110 # define __mempcpy mempcpy
112 # define __opendir opendir
114 # ifndef __UCLIBC_HAS_LFS__
115 # define __readdir64 readdir
117 # define __readdir64 readdir64
120 # define __stpcpy stpcpy
122 # define __tdestroy tdestroy
124 # define __tfind tfind
126 # define __tsearch tsearch
127 # undef internal_function
128 # define internal_function /* empty */
129 # ifndef __UCLIBC_HAS_LFS__
131 # define dirent64 dirent
134 # define MAX(a, b) ((a) > (b) ? (a) : (b))
137 /* Arrange to make lstat calls go through the wrapper function
138 on systems with an lstat function that does not dereference symlinks
139 that are specified with a trailing slash. */
140 #if !defined _LIBC && !defined LSTAT_FOLLOWS_SLASHED_SYMLINK && !defined __UCLIBC__
141 int rpl_lstat (const char *, struct stat *);
143 # define lstat(Name, Stat_buf) rpl_lstat(Name, Stat_buf)
147 # define __set_errno(Val) errno = (Val)
150 /* Support for the LFS API version. */
152 # define FTW_NAME ftw
153 # define NFTW_NAME nftw
154 # define NFTW_OLD_NAME __old_nftw
155 # define NFTW_NEW_NAME __new_nftw
159 # define LXSTAT __lxstat
160 # define XSTAT __xstat
162 # define LXSTAT(V,f,sb) lstat (f,sb)
163 # define XSTAT(V,f,sb) stat (f,sb)
165 # define FTW_FUNC_T __ftw_func_t
166 # define NFTW_FUNC_T __nftw_func_t
169 /* We define PATH_MAX if the system does not provide a definition.
170 This does not artificially limit any operation. PATH_MAX is simply
171 used as a guesstimate for the expected maximal path length.
172 Buffers will be enlarged if necessary. */
174 # define PATH_MAX 1024
191 /* Array with pointers to open directory streams. */
192 struct dir_data **dirstreams;
196 /* Buffer containing name of currently processed object. */
200 /* Passed as fourth argument to `nftw' callback. The `base' member
201 tracks the content of the `dirbuf'. */
204 /* Flags passed to `nftw' function. 0 for `ftw'. */
207 /* Conversion array for flag values. It is the identity mapping for
208 `nftw' calls, otherwise it maps the values to those known by
212 /* Callback function. We always use the `nftw' form. */
215 /* Device of starting point. Needed for FTW_MOUNT. */
218 /* Data structure for keeping fingerprints of already processed
219 object. This is needed when not using FTW_PHYS. */
224 /* Internally we use the FTW_* constants used for `nftw'. When invoked
225 as `ftw', map each flag to the subset of values used by `ftw'. */
226 static const int nftw_arr[] =
228 FTW_F, FTW_D, FTW_DNR, FTW_NS, FTW_SL, FTW_DP, FTW_SLN
231 static const int ftw_arr[] =
233 FTW_F, FTW_D, FTW_DNR, FTW_NS, FTW_F, FTW_D, FTW_NS
237 /* Forward declarations of local functions. */
238 static int ftw_dir (struct ftw_data *data, struct STAT *st,
239 struct dir_data *old_dir) internal_function;
243 object_compare (const void *p1, const void *p2)
245 /* We don't need a sophisticated and useful comparison. We are only
246 interested in equality. However, we must be careful not to
247 accidentally compare `holes' in the structure. */
248 const struct known_object *kp1 = p1, *kp2 = p2;
250 cmp1 = (kp1->ino > kp2->ino) - (kp1->ino < kp2->ino);
253 return (kp1->dev > kp2->dev) - (kp1->dev < kp2->dev);
257 static __inline__ int
258 add_object (struct ftw_data *data, struct STAT *st)
260 struct known_object *newp = malloc (sizeof (struct known_object));
263 newp->dev = st->st_dev;
264 newp->ino = st->st_ino;
265 return __tsearch (newp, &data->known_objects, object_compare) ? 0 : -1;
269 static __inline__ int
270 find_object (struct ftw_data *data, struct STAT *st)
272 struct known_object obj;
273 obj.dev = st->st_dev;
274 obj.ino = st->st_ino;
275 return __tfind (&obj, &data->known_objects, object_compare) != NULL;
279 static __inline__ int
280 __attribute ((always_inline))
281 open_dir_stream (struct ftw_data *data, struct dir_data *dirp)
285 if (data->dirstreams[data->actdir] != NULL)
287 /* Oh, oh. We must close this stream. Get all remaining
288 entries and store them as a list in the `content' member of
289 the `struct dir_data' variable. */
290 size_t bufsize = 1024;
291 char *buf = malloc (bufsize);
297 DIR *st = data->dirstreams[data->actdir]->stream;
301 while ((d = __readdir64 (st)) != NULL)
303 size_t this_len = NAMLEN (d);
304 if (actsize + this_len + 2 >= bufsize)
307 bufsize += MAX (1024, 2 * this_len);
308 newp = (char *) realloc (buf, bufsize);
311 /* No more memory. */
312 int save_err = errno;
314 __set_errno (save_err);
321 *((char *) __mempcpy (buf + actsize, d->d_name, this_len))
323 actsize += this_len + 1;
326 /* Terminate the list with an additional NUL byte. */
327 buf[actsize++] = '\0';
329 /* Shrink the buffer to what we actually need. */
330 data->dirstreams[data->actdir]->content = realloc (buf, actsize);
331 if (data->dirstreams[data->actdir]->content == NULL)
333 int save_err = errno;
335 __set_errno (save_err);
341 data->dirstreams[data->actdir]->stream = NULL;
342 data->dirstreams[data->actdir] = NULL;
347 /* Open the new stream. */
350 const char *name = ((data->flags & FTW_CHDIR)
351 ? data->dirbuf + data->ftw.base: data->dirbuf);
352 assert (data->dirstreams[data->actdir] == NULL);
354 dirp->stream = __opendir (name);
355 if (dirp->stream == NULL)
359 dirp->content = NULL;
360 data->dirstreams[data->actdir] = dirp;
362 if (++data->actdir == data->maxdir)
373 process_entry (struct ftw_data *data, struct dir_data *dir, const char *name,
381 if (name[0] == '.' && (name[1] == '\0'
382 || (name[1] == '.' && name[2] == '\0')))
383 /* Don't process the "." and ".." entries. */
386 new_buflen = data->ftw.base + namlen + 2;
387 if (data->dirbufsize < new_buflen)
389 /* Enlarge the buffer. */
392 data->dirbufsize = 2 * new_buflen;
393 newp = (char *) realloc (data->dirbuf, data->dirbufsize);
399 *((char *) __mempcpy (data->dirbuf + data->ftw.base, name, namlen)) = '\0';
401 if ((data->flags & FTW_CHDIR) == 0)
404 if (((data->flags & FTW_PHYS)
405 ? LXSTAT (_STAT_VER, name, &st)
406 : XSTAT (_STAT_VER, name, &st)) < 0)
408 if (errno != EACCES && errno != ENOENT)
410 else if (!(data->flags & FTW_PHYS)
411 && LXSTAT (_STAT_VER, name, &st) == 0
412 && S_ISLNK (st.st_mode))
419 if (S_ISDIR (st.st_mode))
421 else if (S_ISLNK (st.st_mode))
429 || !(data->flags & FTW_MOUNT) || st.st_dev == data->dev))
433 if ((data->flags & FTW_PHYS)
434 || (!find_object (data, &st)
435 /* Remember the object. */
436 && (result = add_object (data, &st)) == 0))
437 result = ftw_dir (data, &st, dir);
440 result = (*data->func) (data->dirbuf, &st, data->cvt_arr[flag],
444 if ((data->flags & FTW_ACTIONRETVAL) && result == FTW_SKIP_SUBTREE)
452 __attribute ((noinline))
454 ftw_dir (struct ftw_data *data, struct STAT *st, struct dir_data *old_dir)
458 int previous_base = data->ftw.base;
462 /* Open the stream for this directory. This might require that
463 another stream has to be closed. */
464 result = open_dir_stream (data, &dir);
468 /* We cannot read the directory. Signal this with a special flag. */
469 result = (*data->func) (data->dirbuf, st, FTW_DNR, &data->ftw);
474 /* First, report the directory (if not depth-first). */
475 if (!(data->flags & FTW_DEPTH))
477 result = (*data->func) (data->dirbuf, st, FTW_D, &data->ftw);
483 __closedir (dir.stream);
484 __set_errno (save_err);
486 if (data->actdir-- == 0)
487 data->actdir = data->maxdir - 1;
488 data->dirstreams[data->actdir] = NULL;
493 /* If necessary, change to this directory. */
494 if (data->flags & FTW_CHDIR)
496 if (__fchdir (dirfd (dir.stream)) < 0)
503 /* Next, update the `struct FTW' information. */
505 startp = strchr (data->dirbuf, '\0');
506 /* There always must be a directory name. */
507 assert (startp != data->dirbuf);
508 if (startp[-1] != '/')
510 data->ftw.base = startp - data->dirbuf;
512 while (dir.stream != NULL && (d = __readdir64 (dir.stream)) != NULL)
514 result = process_entry (data, &dir, d->d_name, NAMLEN (d));
519 if (dir.stream != NULL)
521 /* The stream is still open. I.e., we did not need more
522 descriptors. Simply close the stream now. */
523 int save_err = errno;
525 assert (dir.content == NULL);
527 __closedir (dir.stream);
528 __set_errno (save_err);
530 if (data->actdir-- == 0)
531 data->actdir = data->maxdir - 1;
532 data->dirstreams[data->actdir] = NULL;
537 char *runp = dir.content;
539 while (result == 0 && *runp != '\0')
541 char *endp = strchr (runp, '\0');
543 result = process_entry (data, &dir, runp, endp - runp);
550 __set_errno (save_err);
553 if ((data->flags & FTW_ACTIONRETVAL) && result == FTW_SKIP_SIBLINGS)
556 /* Prepare the return, revert the `struct FTW' information. */
557 data->dirbuf[data->ftw.base - 1] = '\0';
559 data->ftw.base = previous_base;
561 /* Finally, if we process depth-first report the directory. */
562 if (result == 0 && (data->flags & FTW_DEPTH))
563 result = (*data->func) (data->dirbuf, st, FTW_DP, &data->ftw);
566 && (data->flags & FTW_CHDIR)
568 || ((data->flags & FTW_ACTIONRETVAL)
569 && (result != -1 && result != FTW_STOP))))
571 /* Change back to the parent directory. */
573 if (old_dir->stream != NULL)
574 if (__fchdir (dirfd (old_dir->stream)) == 0)
579 if (data->ftw.base == 1)
581 if (__chdir ("/") < 0)
585 if (__chdir ("..") < 0)
595 __attribute ((noinline))
597 ftw_startup (const char *dir, int is_nftw, void *func, int descriptors,
600 struct ftw_data data;
607 /* First make sure the parameters are reasonable. */
610 __set_errno (ENOENT);
614 data.maxdir = descriptors < 1 ? 1 : descriptors;
616 data.dirstreams = (struct dir_data **) alloca (data.maxdir
617 * sizeof (struct dir_data *));
618 memset (data.dirstreams, '\0', data.maxdir * sizeof (struct dir_data *));
620 /* PATH_MAX is always defined when we get here. */
621 data.dirbufsize = MAX (2 * strlen (dir), PATH_MAX);
622 data.dirbuf = (char *) malloc (data.dirbufsize);
623 if (data.dirbuf == NULL)
625 cp = __stpcpy (data.dirbuf, dir);
626 /* Strip trailing slashes. */
627 while (cp > data.dirbuf + 1 && cp[-1] == '/')
634 while (cp > data.dirbuf && cp[-1] != '/')
636 data.ftw.base = cp - data.dirbuf;
640 /* This assignment might seem to be strange but it is what we want.
641 The trick is that the first three arguments to the `ftw' and
642 `nftw' callback functions are equal. Therefore we can call in
643 every case the callback using the format of the `nftw' version
644 and get the correct result since the stack layout for a function
645 call in C allows this. */
646 data.func = (NFTW_FUNC_T) func;
648 /* Since we internally use the complete set of FTW_* values we need
649 to reduce the value range before calling a `ftw' callback. */
650 data.cvt_arr = is_nftw ? nftw_arr : ftw_arr;
652 /* No object known so far. */
653 data.known_objects = NULL;
655 /* Now go to the directory containing the initial file/directory. */
656 if (flags & FTW_CHDIR)
658 /* GNU extension ahead. */
659 cwd = __getcwd (NULL, 0);
662 else if (data.ftw.base > 0)
664 /* Change to the directory the file is in. In data.dirbuf
665 we have a writable copy of the file name. Just NUL
666 terminate it for now and change the directory. */
667 if (data.ftw.base == 1)
668 /* I.e., the file is in the root directory. */
669 result = __chdir ("/");
672 char ch = data.dirbuf[data.ftw.base - 1];
673 data.dirbuf[data.ftw.base - 1] = '\0';
674 result = __chdir (data.dirbuf);
675 data.dirbuf[data.ftw.base - 1] = ch;
680 /* Get stat info for start directory. */
683 const char *name = ((data.flags & FTW_CHDIR)
684 ? data.dirbuf + data.ftw.base
687 if (((flags & FTW_PHYS)
688 ? LXSTAT (_STAT_VER, name, &st)
689 : XSTAT (_STAT_VER, name, &st)) < 0)
691 if (!(flags & FTW_PHYS)
693 && LXSTAT (_STAT_VER, name, &st) == 0
694 && S_ISLNK (st.st_mode))
695 result = (*data.func) (data.dirbuf, &st, data.cvt_arr[FTW_SLN],
698 /* No need to call the callback since we cannot say anything
704 if (S_ISDIR (st.st_mode))
706 /* Remember the device of the initial directory in case
707 FTW_MOUNT is given. */
708 data.dev = st.st_dev;
710 /* We know this directory now. */
711 if (!(flags & FTW_PHYS))
712 result = add_object (&data, &st);
715 result = ftw_dir (&data, &st, NULL);
719 int flag = S_ISLNK (st.st_mode) ? FTW_SL : FTW_F;
721 result = (*data.func) (data.dirbuf, &st, data.cvt_arr[flag],
726 if ((flags & FTW_ACTIONRETVAL)
727 && (result == FTW_SKIP_SUBTREE || result == FTW_SKIP_SIBLINGS))
731 /* Return to the start directory (if necessary). */
737 __set_errno (save_err);
740 /* Free all memory. */
742 __tdestroy (data.known_objects, free);
744 __set_errno (save_err);
752 #ifdef __UCLIBC_HAS_FTW__
754 FTW_NAME (const char *path, FTW_FUNC_T func, int descriptors)
756 return ftw_startup (path, 0, func, descriptors, 0);
760 #ifdef __UCLIBC_HAS_NFTW__
763 NFTW_NAME (const char *path, NFTW_FUNC_T func, int descriptors, int flags)
765 return ftw_startup (path, 1, func, descriptors, flags);
769 #include <shlib-compat.h>
771 int NFTW_NEW_NAME (const char *, NFTW_FUNC_T, int, int);
774 NFTW_NEW_NAME (const char *path, NFTW_FUNC_T func, int descriptors, int flags)
777 & ~(FTW_PHYS | FTW_MOUNT | FTW_CHDIR | FTW_DEPTH | FTW_ACTIONRETVAL))
779 __set_errno (EINVAL);
782 return ftw_startup (path, 1, func, descriptors, flags);
785 versioned_symbol (libc, NFTW_NEW_NAME, NFTW_NAME, GLIBC_2_3_3);
787 #if SHLIB_COMPAT(libc, GLIBC_2_1, GLIBC_2_3_3)
789 /* Older nftw* version just ignored all unknown flags. */
791 int NFTW_OLD_NAME (const char *, NFTW_FUNC_T, int, int);
794 attribute_compat_text_section
795 NFTW_OLD_NAME (const char *path, NFTW_FUNC_T func, int descriptors, int flags)
797 flags &= (FTW_PHYS | FTW_MOUNT | FTW_CHDIR | FTW_DEPTH);
798 return ftw_startup (path, 1, func, descriptors, flags);
801 compat_symbol (libc, NFTW_OLD_NAME, NFTW_NAME, GLIBC_2_1);