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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
28 #define HAVE_DIRENT_H 1
29 #define HAVE_SYS_PARAM_H 1
30 #define HAVE_DECL_STPCPY 1
31 #define HAVE_MEMPCPY 1
35 # define alloca __builtin_alloca
50 # define NAMLEN(dirent) _D_EXACT_NAMLEN (dirent)
54 # define NAMLEN(dirent) strlen ((dirent)->d_name)
56 # define dirent direct
57 # define NAMLEN(dirent) (dirent)->d_namlen
59 # include <sys/ndir.h>
77 #if HAVE_SYS_PARAM_H || defined _LIBC
78 # include <sys/param.h>
81 # include <include/sys/stat.h>
83 # include <sys/stat.h>
86 /* Experimentally off - libc_hidden_proto(memset) */
87 /* Experimentally off - libc_hidden_proto(strchr) */
88 /* Experimentally off - libc_hidden_proto(strlen) */
89 /* libc_hidden_proto(dirfd) */
90 /* libc_hidden_proto(tsearch) */
91 /* libc_hidden_proto(tfind) */
92 /* libc_hidden_proto(tdestroy) */
93 /* libc_hidden_proto(getcwd) */
94 /* libc_hidden_proto(chdir) */
95 /* libc_hidden_proto(fchdir) */
96 /* Experimentally off - libc_hidden_proto(mempcpy) */
97 /* libc_hidden_proto(opendir) */
98 #ifdef __UCLIBC_HAS_LFS__
99 /* libc_hidden_proto(readdir64) */
100 /* libc_hidden_proto(lstat64) */
101 /* libc_hidden_proto(stat64) */
103 /* libc_hidden_proto(closedir) */
104 /* Experimentally off - libc_hidden_proto(stpcpy) */
105 /* libc_hidden_proto(lstat) */
106 /* libc_hidden_proto(stat) */
108 #if ! _LIBC && !HAVE_DECL_STPCPY && !defined stpcpy
112 #if ! _LIBC && ! defined HAVE_MEMPCPY && ! defined mempcpy
113 /* Be CAREFUL that there are no side effects in N. */
114 # define mempcpy(D, S, N) ((void *) ((char *) memcpy (D, S, N) + (N)))
117 /* #define NDEBUG 1 */
122 # define __chdir chdir
124 # define __closedir closedir
126 # define __fchdir fchdir
129 # define __getcwd(P, N) xgetcwd ()
130 extern char *xgetcwd (void);
132 # define __getcwd getcwd
135 # define __mempcpy mempcpy
137 # define __opendir opendir
139 # ifndef __UCLIBC_HAS_LFS__
140 # define __readdir64 readdir
142 # define __readdir64 readdir64
145 # define __stpcpy stpcpy
147 # define __tdestroy tdestroy
149 # define __tfind tfind
151 # define __tsearch tsearch
152 # undef internal_function
153 # define internal_function /* empty */
154 # ifndef __UCLIBC_HAS_LFS__
156 # define dirent64 dirent
159 # define MAX(a, b) ((a) > (b) ? (a) : (b))
162 /* Arrange to make lstat calls go through the wrapper function
163 on systems with an lstat function that does not dereference symlinks
164 that are specified with a trailing slash. */
165 #if ! _LIBC && ! LSTAT_FOLLOWS_SLASHED_SYMLINK && !defined __UCLIBC__
166 int rpl_lstat (const char *, struct stat *);
168 # define lstat(Name, Stat_buf) rpl_lstat(Name, Stat_buf)
172 # define __set_errno(Val) errno = (Val)
175 /* Support for the LFS API version. */
177 # define FTW_NAME ftw
178 # define NFTW_NAME nftw
179 # define NFTW_OLD_NAME __old_nftw
180 # define NFTW_NEW_NAME __new_nftw
184 # define LXSTAT __lxstat
185 # define XSTAT __xstat
187 # define LXSTAT(V,f,sb) lstat (f,sb)
188 # define XSTAT(V,f,sb) stat (f,sb)
190 # define FTW_FUNC_T __ftw_func_t
191 # define NFTW_FUNC_T __nftw_func_t
194 /* We define PATH_MAX if the system does not provide a definition.
195 This does not artificially limit any operation. PATH_MAX is simply
196 used as a guesstimate for the expected maximal path length.
197 Buffers will be enlarged if necessary. */
199 # define PATH_MAX 1024
216 /* Array with pointers to open directory streams. */
217 struct dir_data **dirstreams;
221 /* Buffer containing name of currently processed object. */
225 /* Passed as fourth argument to `nftw' callback. The `base' member
226 tracks the content of the `dirbuf'. */
229 /* Flags passed to `nftw' function. 0 for `ftw'. */
232 /* Conversion array for flag values. It is the identity mapping for
233 `nftw' calls, otherwise it maps the values to those known by
237 /* Callback function. We always use the `nftw' form. */
240 /* Device of starting point. Needed for FTW_MOUNT. */
243 /* Data structure for keeping fingerprints of already processed
244 object. This is needed when not using FTW_PHYS. */
249 /* Internally we use the FTW_* constants used for `nftw'. When invoked
250 as `ftw', map each flag to the subset of values used by `ftw'. */
251 static const int nftw_arr[] =
253 FTW_F, FTW_D, FTW_DNR, FTW_NS, FTW_SL, FTW_DP, FTW_SLN
256 static const int ftw_arr[] =
258 FTW_F, FTW_D, FTW_DNR, FTW_NS, FTW_F, FTW_D, FTW_NS
262 /* Forward declarations of local functions. */
263 static int ftw_dir (struct ftw_data *data, struct STAT *st,
264 struct dir_data *old_dir) internal_function;
268 object_compare (const void *p1, const void *p2)
270 /* We don't need a sophisticated and useful comparison. We are only
271 interested in equality. However, we must be careful not to
272 accidentally compare `holes' in the structure. */
273 const struct known_object *kp1 = p1, *kp2 = p2;
275 cmp1 = (kp1->ino > kp2->ino) - (kp1->ino < kp2->ino);
278 return (kp1->dev > kp2->dev) - (kp1->dev < kp2->dev);
282 static __inline__ int
283 add_object (struct ftw_data *data, struct STAT *st)
285 struct known_object *newp = malloc (sizeof (struct known_object));
288 newp->dev = st->st_dev;
289 newp->ino = st->st_ino;
290 return __tsearch (newp, &data->known_objects, object_compare) ? 0 : -1;
294 static __inline__ int
295 find_object (struct ftw_data *data, struct STAT *st)
297 struct known_object obj;
298 obj.dev = st->st_dev;
299 obj.ino = st->st_ino;
300 return __tfind (&obj, &data->known_objects, object_compare) != NULL;
304 static __inline__ int
305 __attribute ((always_inline))
306 open_dir_stream (struct ftw_data *data, struct dir_data *dirp)
310 if (data->dirstreams[data->actdir] != NULL)
312 /* Oh, oh. We must close this stream. Get all remaining
313 entries and store them as a list in the `content' member of
314 the `struct dir_data' variable. */
315 size_t bufsize = 1024;
316 char *buf = malloc (bufsize);
322 DIR *st = data->dirstreams[data->actdir]->stream;
326 while ((d = __readdir64 (st)) != NULL)
328 size_t this_len = NAMLEN (d);
329 if (actsize + this_len + 2 >= bufsize)
332 bufsize += MAX (1024, 2 * this_len);
333 newp = (char *) realloc (buf, bufsize);
336 /* No more memory. */
337 int save_err = errno;
339 __set_errno (save_err);
346 *((char *) __mempcpy (buf + actsize, d->d_name, this_len))
348 actsize += this_len + 1;
351 /* Terminate the list with an additional NUL byte. */
352 buf[actsize++] = '\0';
354 /* Shrink the buffer to what we actually need. */
355 data->dirstreams[data->actdir]->content = realloc (buf, actsize);
356 if (data->dirstreams[data->actdir]->content == NULL)
358 int save_err = errno;
360 __set_errno (save_err);
366 data->dirstreams[data->actdir]->stream = NULL;
367 data->dirstreams[data->actdir] = NULL;
372 /* Open the new stream. */
375 const char *name = ((data->flags & FTW_CHDIR)
376 ? data->dirbuf + data->ftw.base: data->dirbuf);
377 assert (data->dirstreams[data->actdir] == NULL);
379 dirp->stream = __opendir (name);
380 if (dirp->stream == NULL)
384 dirp->content = NULL;
385 data->dirstreams[data->actdir] = dirp;
387 if (++data->actdir == data->maxdir)
398 process_entry (struct ftw_data *data, struct dir_data *dir, const char *name,
406 if (name[0] == '.' && (name[1] == '\0'
407 || (name[1] == '.' && name[2] == '\0')))
408 /* Don't process the "." and ".." entries. */
411 new_buflen = data->ftw.base + namlen + 2;
412 if (data->dirbufsize < new_buflen)
414 /* Enlarge the buffer. */
417 data->dirbufsize = 2 * new_buflen;
418 newp = (char *) realloc (data->dirbuf, data->dirbufsize);
424 *((char *) __mempcpy (data->dirbuf + data->ftw.base, name, namlen)) = '\0';
426 if ((data->flags & FTW_CHDIR) == 0)
429 if (((data->flags & FTW_PHYS)
430 ? LXSTAT (_STAT_VER, name, &st)
431 : XSTAT (_STAT_VER, name, &st)) < 0)
433 if (errno != EACCES && errno != ENOENT)
435 else if (!(data->flags & FTW_PHYS)
436 && LXSTAT (_STAT_VER, name, &st) == 0
437 && S_ISLNK (st.st_mode))
444 if (S_ISDIR (st.st_mode))
446 else if (S_ISLNK (st.st_mode))
454 || !(data->flags & FTW_MOUNT) || st.st_dev == data->dev))
458 if ((data->flags & FTW_PHYS)
459 || (!find_object (data, &st)
460 /* Remember the object. */
461 && (result = add_object (data, &st)) == 0))
462 result = ftw_dir (data, &st, dir);
465 result = (*data->func) (data->dirbuf, &st, data->cvt_arr[flag],
469 if ((data->flags & FTW_ACTIONRETVAL) && result == FTW_SKIP_SUBTREE)
477 __attribute ((noinline))
479 ftw_dir (struct ftw_data *data, struct STAT *st, struct dir_data *old_dir)
483 int previous_base = data->ftw.base;
487 /* Open the stream for this directory. This might require that
488 another stream has to be closed. */
489 result = open_dir_stream (data, &dir);
493 /* We cannot read the directory. Signal this with a special flag. */
494 result = (*data->func) (data->dirbuf, st, FTW_DNR, &data->ftw);
499 /* First, report the directory (if not depth-first). */
500 if (!(data->flags & FTW_DEPTH))
502 result = (*data->func) (data->dirbuf, st, FTW_D, &data->ftw);
508 __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 = strchr (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 result = process_entry (data, &dir, d->d_name, NAMLEN (d));
544 if (dir.stream != NULL)
546 /* The stream is still open. I.e., we did not need more
547 descriptors. Simply close the stream now. */
548 int save_err = errno;
550 assert (dir.content == NULL);
552 __closedir (dir.stream);
553 __set_errno (save_err);
555 if (data->actdir-- == 0)
556 data->actdir = data->maxdir - 1;
557 data->dirstreams[data->actdir] = NULL;
562 char *runp = dir.content;
564 while (result == 0 && *runp != '\0')
566 char *endp = strchr (runp, '\0');
568 result = process_entry (data, &dir, runp, endp - runp);
575 __set_errno (save_err);
578 if ((data->flags & FTW_ACTIONRETVAL) && result == FTW_SKIP_SIBLINGS)
581 /* Prepare the return, revert the `struct FTW' information. */
582 data->dirbuf[data->ftw.base - 1] = '\0';
584 data->ftw.base = previous_base;
586 /* Finally, if we process depth-first report the directory. */
587 if (result == 0 && (data->flags & FTW_DEPTH))
588 result = (*data->func) (data->dirbuf, st, FTW_DP, &data->ftw);
591 && (data->flags & FTW_CHDIR)
593 || ((data->flags & FTW_ACTIONRETVAL)
594 && (result != -1 && result != FTW_STOP))))
596 /* Change back to the parent directory. */
598 if (old_dir->stream != NULL)
599 if (__fchdir (dirfd (old_dir->stream)) == 0)
604 if (data->ftw.base == 1)
606 if (__chdir ("/") < 0)
610 if (__chdir ("..") < 0)
620 __attribute ((noinline))
622 ftw_startup (const char *dir, int is_nftw, void *func, int descriptors,
625 struct ftw_data data;
632 /* First make sure the parameters are reasonable. */
635 __set_errno (ENOENT);
639 data.maxdir = descriptors < 1 ? 1 : descriptors;
641 data.dirstreams = (struct dir_data **) alloca (data.maxdir
642 * sizeof (struct dir_data *));
643 memset (data.dirstreams, '\0', data.maxdir * sizeof (struct dir_data *));
645 /* PATH_MAX is always defined when we get here. */
646 data.dirbufsize = MAX (2 * strlen (dir), PATH_MAX);
647 data.dirbuf = (char *) malloc (data.dirbufsize);
648 if (data.dirbuf == NULL)
650 cp = __stpcpy (data.dirbuf, dir);
651 /* Strip trailing slashes. */
652 while (cp > data.dirbuf + 1 && cp[-1] == '/')
659 while (cp > data.dirbuf && cp[-1] != '/')
661 data.ftw.base = cp - data.dirbuf;
665 /* This assignment might seem to be strange but it is what we want.
666 The trick is that the first three arguments to the `ftw' and
667 `nftw' callback functions are equal. Therefore we can call in
668 every case the callback using the format of the `nftw' version
669 and get the correct result since the stack layout for a function
670 call in C allows this. */
671 data.func = (NFTW_FUNC_T) func;
673 /* Since we internally use the complete set of FTW_* values we need
674 to reduce the value range before calling a `ftw' callback. */
675 data.cvt_arr = is_nftw ? nftw_arr : ftw_arr;
677 /* No object known so far. */
678 data.known_objects = NULL;
680 /* Now go to the directory containing the initial file/directory. */
681 if (flags & FTW_CHDIR)
683 /* GNU extension ahead. */
684 cwd = __getcwd (NULL, 0);
687 else if (data.ftw.base > 0)
689 /* Change to the directory the file is in. In data.dirbuf
690 we have a writable copy of the file name. Just NUL
691 terminate it for now and change the directory. */
692 if (data.ftw.base == 1)
693 /* I.e., the file is in the root directory. */
694 result = __chdir ("/");
697 char ch = data.dirbuf[data.ftw.base - 1];
698 data.dirbuf[data.ftw.base - 1] = '\0';
699 result = __chdir (data.dirbuf);
700 data.dirbuf[data.ftw.base - 1] = ch;
705 /* Get stat info for start directory. */
708 const char *name = ((data.flags & FTW_CHDIR)
709 ? data.dirbuf + data.ftw.base
712 if (((flags & FTW_PHYS)
713 ? LXSTAT (_STAT_VER, name, &st)
714 : XSTAT (_STAT_VER, name, &st)) < 0)
716 if (!(flags & FTW_PHYS)
718 && LXSTAT (_STAT_VER, name, &st) == 0
719 && S_ISLNK (st.st_mode))
720 result = (*data.func) (data.dirbuf, &st, data.cvt_arr[FTW_SLN],
723 /* No need to call the callback since we cannot say anything
729 if (S_ISDIR (st.st_mode))
731 /* Remember the device of the initial directory in case
732 FTW_MOUNT is given. */
733 data.dev = st.st_dev;
735 /* We know this directory now. */
736 if (!(flags & FTW_PHYS))
737 result = add_object (&data, &st);
740 result = ftw_dir (&data, &st, NULL);
744 int flag = S_ISLNK (st.st_mode) ? FTW_SL : FTW_F;
746 result = (*data.func) (data.dirbuf, &st, data.cvt_arr[flag],
751 if ((flags & FTW_ACTIONRETVAL)
752 && (result == FTW_SKIP_SUBTREE || result == FTW_SKIP_SIBLINGS))
756 /* Return to the start directory (if necessary). */
762 __set_errno (save_err);
765 /* Free all memory. */
767 __tdestroy (data.known_objects, free);
769 __set_errno (save_err);
779 FTW_NAME (const char *path, FTW_FUNC_T func, int descriptors)
781 return ftw_startup (path, 0, func, descriptors, 0);
786 NFTW_NAME (const char *path, NFTW_FUNC_T func, int descriptors, int flags)
788 return ftw_startup (path, 1, func, descriptors, flags);
792 #include <shlib-compat.h>
794 int NFTW_NEW_NAME (const char *, NFTW_FUNC_T, int, int);
797 NFTW_NEW_NAME (const char *path, NFTW_FUNC_T func, int descriptors, int flags)
800 & ~(FTW_PHYS | FTW_MOUNT | FTW_CHDIR | FTW_DEPTH | FTW_ACTIONRETVAL))
802 __set_errno (EINVAL);
805 return ftw_startup (path, 1, func, descriptors, flags);
808 versioned_symbol (libc, NFTW_NEW_NAME, NFTW_NAME, GLIBC_2_3_3);
810 #if SHLIB_COMPAT(libc, GLIBC_2_1, GLIBC_2_3_3)
812 /* Older nftw* version just ignored all unknown flags. */
814 int NFTW_OLD_NAME (const char *, NFTW_FUNC_T, int, int);
817 attribute_compat_text_section
818 NFTW_OLD_NAME (const char *path, NFTW_FUNC_T func, int descriptors, int flags)
820 flags &= (FTW_PHYS | FTW_MOUNT | FTW_CHDIR | FTW_DEPTH);
821 return ftw_startup (path, 1, func, descriptors, flags);
824 compat_symbol (libc, NFTW_OLD_NAME, NFTW_NAME, GLIBC_2_1);