1 /* File tree walker functions.
2 Copyright (C) 1996-2004, 2006-2008, 2010 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 #if HAVE_SYS_PARAM_H || defined _LIBC
70 # include <sys/param.h>
73 # include <include/sys/stat.h>
75 # include <sys/stat.h>
78 #if ! _LIBC && !HAVE_DECL_STPCPY && !defined stpcpy
82 #if ! _LIBC && ! defined HAVE_MEMPCPY && ! defined mempcpy
83 /* Be CAREFUL that there are no side effects in N. */
84 # define mempcpy(D, S, N) ((void *) ((char *) memcpy (D, S, N) + (N)))
87 /* #define NDEBUG 1 */
92 # define __chdir chdir
94 # define __closedir closedir
96 # define __fchdir fchdir
98 # define __getcwd(P, N) xgetcwd ()
99 extern char *xgetcwd (void);
101 # define __mempcpy mempcpy
103 # define __opendir opendir
105 # define __readdir64 readdir
107 # define __stpcpy stpcpy
109 # define __tdestroy tdestroy
111 # define __tfind tfind
113 # define __tsearch tsearch
114 # undef internal_function
115 # define internal_function /* empty */
117 # define dirent64 dirent
119 # define MAX(a, b) ((a) > (b) ? (a) : (b))
122 /* Arrange to make lstat calls go through the wrapper function
123 on systems with an lstat function that does not dereference symlinks
124 that are specified with a trailing slash. */
125 #if ! _LIBC && ! LSTAT_FOLLOWS_SLASHED_SYMLINK
126 int rpl_lstat (const char *, struct stat
*);
128 # define lstat(Name, Stat_buf) rpl_lstat(Name, Stat_buf)
132 # define __set_errno(Val) errno = (Val)
135 /* Support for the LFS API version. */
137 # define FTW_NAME ftw
138 # define NFTW_NAME nftw
139 # define NFTW_OLD_NAME __old_nftw
140 # define NFTW_NEW_NAME __new_nftw
144 # define LXSTAT __lxstat
145 # define XSTAT __xstat
146 # define FXSTATAT __fxstatat
148 # define LXSTAT(V,f,sb) lstat (f,sb)
149 # define XSTAT(V,f,sb) stat (f,sb)
150 # define FXSTATAT(V,d,f,sb,m) fstatat (d, f, sb, m)
152 # define FTW_FUNC_T __ftw_func_t
153 # define NFTW_FUNC_T __nftw_func_t
156 /* We define PATH_MAX if the system does not provide a definition.
157 This does not artificially limit any operation. PATH_MAX is simply
158 used as a guesstimate for the expected maximal path length.
159 Buffers will be enlarged if necessary. */
161 # define PATH_MAX 1024
179 /* Array with pointers to open directory streams. */
180 struct dir_data
**dirstreams
;
184 /* Buffer containing name of currently processed object. */
188 /* Passed as fourth argument to `nftw' callback. The `base' member
189 tracks the content of the `dirbuf'. */
192 /* Flags passed to `nftw' function. 0 for `ftw'. */
195 /* Conversion array for flag values. It is the identity mapping for
196 `nftw' calls, otherwise it maps the values to those known by
200 /* Callback function. We always use the `nftw' form. */
203 /* Device of starting point. Needed for FTW_MOUNT. */
206 /* Data structure for keeping fingerprints of already processed
207 object. This is needed when not using FTW_PHYS. */
212 /* Internally we use the FTW_* constants used for `nftw'. When invoked
213 as `ftw', map each flag to the subset of values used by `ftw'. */
214 static const int nftw_arr
[] =
216 FTW_F
, FTW_D
, FTW_DNR
, FTW_NS
, FTW_SL
, FTW_DP
, FTW_SLN
219 static const int ftw_arr
[] =
221 FTW_F
, FTW_D
, FTW_DNR
, FTW_NS
, FTW_F
, FTW_D
, FTW_NS
225 /* Forward declarations of local functions. */
226 static int ftw_dir (struct ftw_data
*data
, struct STAT
*st
,
227 struct dir_data
*old_dir
) internal_function
;
231 object_compare (const void *p1
, const void *p2
)
233 /* We don't need a sophisticated and useful comparison. We are only
234 interested in equality. However, we must be careful not to
235 accidentally compare `holes' in the structure. */
236 const struct known_object
*kp1
= p1
, *kp2
= p2
;
238 cmp1
= (kp1
->ino
> kp2
->ino
) - (kp1
->ino
< kp2
->ino
);
241 return (kp1
->dev
> kp2
->dev
) - (kp1
->dev
< kp2
->dev
);
246 add_object (struct ftw_data
*data
, struct STAT
*st
)
248 struct known_object
*newp
= malloc (sizeof (struct known_object
));
251 newp
->dev
= st
->st_dev
;
252 newp
->ino
= st
->st_ino
;
253 return __tsearch (newp
, &data
->known_objects
, object_compare
) ? 0 : -1;
258 find_object (struct ftw_data
*data
, struct STAT
*st
)
260 struct known_object obj
;
261 obj
.dev
= st
->st_dev
;
262 obj
.ino
= st
->st_ino
;
263 return __tfind (&obj
, &data
->known_objects
, object_compare
) != NULL
;
268 __attribute ((always_inline
))
269 open_dir_stream (int *dfdp
, struct ftw_data
*data
, struct dir_data
*dirp
)
273 if (data
->dirstreams
[data
->actdir
] != NULL
)
275 /* Oh, oh. We must close this stream. Get all remaining
276 entries and store them as a list in the `content' member of
277 the `struct dir_data' variable. */
278 size_t bufsize
= 1024;
279 char *buf
= malloc (bufsize
);
285 DIR *st
= data
->dirstreams
[data
->actdir
]->stream
;
289 while ((d
= __readdir64 (st
)) != NULL
)
291 size_t this_len
= NAMLEN (d
);
292 if (actsize
+ this_len
+ 2 >= bufsize
)
295 bufsize
+= MAX (1024, 2 * this_len
);
296 newp
= (char *) realloc (buf
, bufsize
);
299 /* No more memory. */
300 int save_err
= errno
;
302 __set_errno (save_err
);
308 *((char *) __mempcpy (buf
+ actsize
, d
->d_name
, this_len
))
310 actsize
+= this_len
+ 1;
313 /* Terminate the list with an additional NUL byte. */
314 buf
[actsize
++] = '\0';
316 /* Shrink the buffer to what we actually need. */
317 data
->dirstreams
[data
->actdir
]->content
= realloc (buf
, actsize
);
318 if (data
->dirstreams
[data
->actdir
]->content
== NULL
)
320 int save_err
= errno
;
322 __set_errno (save_err
);
328 data
->dirstreams
[data
->actdir
]->stream
= NULL
;
329 data
->dirstreams
[data
->actdir
]->streamfd
= -1;
330 data
->dirstreams
[data
->actdir
] = NULL
;
335 /* Open the new stream. */
338 assert (data
->dirstreams
[data
->actdir
] == NULL
);
340 if (dfdp
!= NULL
&& *dfdp
!= -1)
342 int fd
= openat64_not_cancel_3 (*dfdp
, data
->dirbuf
+ data
->ftw
.base
,
343 O_RDONLY
| O_DIRECTORY
| O_NDELAY
);
345 if (fd
!= -1 && (dirp
->stream
= __fdopendir (fd
)) == NULL
)
346 close_not_cancel_no_status (fd
);
352 if (data
->flags
& FTW_CHDIR
)
354 name
= data
->dirbuf
+ data
->ftw
.base
;
361 dirp
->stream
= __opendir (name
);
364 if (dirp
->stream
== NULL
)
368 dirp
->streamfd
= dirfd (dirp
->stream
);
369 dirp
->content
= NULL
;
370 data
->dirstreams
[data
->actdir
] = dirp
;
372 if (++data
->actdir
== data
->maxdir
)
383 process_entry (struct ftw_data
*data
, struct dir_data
*dir
, const char *name
,
384 size_t namlen
, int d_type
)
391 if (name
[0] == '.' && (name
[1] == '\0'
392 || (name
[1] == '.' && name
[2] == '\0')))
393 /* Don't process the "." and ".." entries. */
396 new_buflen
= data
->ftw
.base
+ namlen
+ 2;
397 if (data
->dirbufsize
< new_buflen
)
399 /* Enlarge the buffer. */
402 data
->dirbufsize
= 2 * new_buflen
;
403 newp
= (char *) realloc (data
->dirbuf
, data
->dirbufsize
);
409 *((char *) __mempcpy (data
->dirbuf
+ data
->ftw
.base
, name
, namlen
)) = '\0';
412 if (dir
->streamfd
!= -1)
413 statres
= FXSTATAT (_STAT_VER
, dir
->streamfd
, name
, &st
,
414 (data
->flags
& FTW_PHYS
) ? AT_SYMLINK_NOFOLLOW
: 0);
417 if ((data
->flags
& FTW_CHDIR
) == 0)
420 statres
= ((data
->flags
& FTW_PHYS
)
421 ? LXSTAT (_STAT_VER
, name
, &st
)
422 : XSTAT (_STAT_VER
, name
, &st
));
427 if (errno
!= EACCES
&& errno
!= ENOENT
)
429 else if (data
->flags
& FTW_PHYS
)
431 else if (d_type
== DT_LNK
)
435 if (dir
->streamfd
!= -1)
436 statres
= FXSTATAT (_STAT_VER
, dir
->streamfd
, name
, &st
,
437 AT_SYMLINK_NOFOLLOW
);
439 statres
= LXSTAT (_STAT_VER
, name
, &st
);
440 if (statres
== 0 && S_ISLNK (st
.st_mode
))
448 if (S_ISDIR (st
.st_mode
))
450 else if (S_ISLNK (st
.st_mode
))
458 || !(data
->flags
& FTW_MOUNT
) || st
.st_dev
== data
->dev
))
462 if ((data
->flags
& FTW_PHYS
)
463 || (!find_object (data
, &st
)
464 /* Remember the object. */
465 && (result
= add_object (data
, &st
)) == 0))
466 result
= ftw_dir (data
, &st
, dir
);
469 result
= (*data
->func
) (data
->dirbuf
, &st
, data
->cvt_arr
[flag
],
473 if ((data
->flags
& FTW_ACTIONRETVAL
) && result
== FTW_SKIP_SUBTREE
)
481 __attribute ((noinline
))
483 ftw_dir (struct ftw_data
*data
, struct STAT
*st
, struct dir_data
*old_dir
)
487 int previous_base
= data
->ftw
.base
;
491 /* Open the stream for this directory. This might require that
492 another stream has to be closed. */
493 result
= open_dir_stream (old_dir
== NULL
? NULL
: &old_dir
->streamfd
,
498 /* We cannot read the directory. Signal this with a special flag. */
499 result
= (*data
->func
) (data
->dirbuf
, st
, FTW_DNR
, &data
->ftw
);
504 /* First, report the directory (if not depth-first). */
505 if (!(data
->flags
& FTW_DEPTH
))
507 result
= (*data
->func
) (data
->dirbuf
, st
, FTW_D
, &data
->ftw
);
513 __closedir (dir
.stream
);
515 __set_errno (save_err
);
517 if (data
->actdir
-- == 0)
518 data
->actdir
= data
->maxdir
- 1;
519 data
->dirstreams
[data
->actdir
] = NULL
;
524 /* If necessary, change to this directory. */
525 if (data
->flags
& FTW_CHDIR
)
527 if (__fchdir (dirfd (dir
.stream
)) < 0)
534 /* Next, update the `struct FTW' information. */
536 startp
= __rawmemchr (data
->dirbuf
, '\0');
537 /* There always must be a directory name. */
538 assert (startp
!= data
->dirbuf
);
539 if (startp
[-1] != '/')
541 data
->ftw
.base
= startp
- data
->dirbuf
;
543 while (dir
.stream
!= NULL
&& (d
= __readdir64 (dir
.stream
)) != NULL
)
545 result
= process_entry (data
, &dir
, d
->d_name
, NAMLEN (d
), d
->d_type
);
550 if (dir
.stream
!= NULL
)
552 /* The stream is still open. I.e., we did not need more
553 descriptors. Simply close the stream now. */
554 int save_err
= errno
;
556 assert (dir
.content
== NULL
);
558 __closedir (dir
.stream
);
560 __set_errno (save_err
);
562 if (data
->actdir
-- == 0)
563 data
->actdir
= data
->maxdir
- 1;
564 data
->dirstreams
[data
->actdir
] = NULL
;
569 char *runp
= dir
.content
;
571 while (result
== 0 && *runp
!= '\0')
573 char *endp
= strchr (runp
, '\0');
575 // XXX Should store the d_type values as well?!
576 result
= process_entry (data
, &dir
, runp
, endp
- runp
, DT_UNKNOWN
);
583 __set_errno (save_err
);
586 if ((data
->flags
& FTW_ACTIONRETVAL
) && result
== FTW_SKIP_SIBLINGS
)
589 /* Prepare the return, revert the `struct FTW' information. */
590 data
->dirbuf
[data
->ftw
.base
- 1] = '\0';
592 data
->ftw
.base
= previous_base
;
594 /* Finally, if we process depth-first report the directory. */
595 if (result
== 0 && (data
->flags
& FTW_DEPTH
))
596 result
= (*data
->func
) (data
->dirbuf
, st
, FTW_DP
, &data
->ftw
);
599 && (data
->flags
& FTW_CHDIR
)
601 || ((data
->flags
& FTW_ACTIONRETVAL
)
602 && (result
!= -1 && result
!= FTW_STOP
))))
604 /* Change back to the parent directory. */
606 if (old_dir
->stream
!= NULL
)
607 if (__fchdir (dirfd (old_dir
->stream
)) == 0)
612 if (data
->ftw
.base
== 1)
614 if (__chdir ("/") < 0)
618 if (__chdir ("..") < 0)
628 __attribute ((noinline
))
630 ftw_startup (const char *dir
, int is_nftw
, void *func
, int descriptors
,
633 struct ftw_data data
;
641 /* First make sure the parameters are reasonable. */
644 __set_errno (ENOENT
);
648 data
.maxdir
= descriptors
< 1 ? 1 : descriptors
;
650 data
.dirstreams
= (struct dir_data
**) alloca (data
.maxdir
651 * sizeof (struct dir_data
*));
652 memset (data
.dirstreams
, '\0', data
.maxdir
* sizeof (struct dir_data
*));
654 /* PATH_MAX is always defined when we get here. */
655 data
.dirbufsize
= MAX (2 * strlen (dir
), PATH_MAX
);
656 data
.dirbuf
= (char *) malloc (data
.dirbufsize
);
657 if (data
.dirbuf
== NULL
)
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
)
744 ? LXSTAT (_STAT_VER
, name
, &st
)
745 : XSTAT (_STAT_VER
, name
, &st
)) < 0)
747 if (!(flags
& FTW_PHYS
)
749 && LXSTAT (_STAT_VER
, 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_not_cancel_no_status (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
);
808 __set_errno (save_err
);
818 FTW_NAME (path
, func
, descriptors
)
823 return ftw_startup (path
, 0, func
, descriptors
, 0);
828 NFTW_NAME (path
, func
, descriptors
, flags
)
834 return ftw_startup (path
, 1, func
, descriptors
, flags
);
838 # include <shlib-compat.h>
840 int NFTW_NEW_NAME (const char *, NFTW_FUNC_T
, int, int);
843 NFTW_NEW_NAME (path
, func
, descriptors
, flags
)
850 & ~(FTW_PHYS
| FTW_MOUNT
| FTW_CHDIR
| FTW_DEPTH
| FTW_ACTIONRETVAL
))
852 __set_errno (EINVAL
);
855 return ftw_startup (path
, 1, func
, descriptors
, flags
);
858 versioned_symbol (libc
, NFTW_NEW_NAME
, NFTW_NAME
, GLIBC_2_3_3
);
860 # if SHLIB_COMPAT(libc, GLIBC_2_1, GLIBC_2_3_3)
862 /* Older nftw* version just ignored all unknown flags. */
864 int NFTW_OLD_NAME (const char *, NFTW_FUNC_T
, int, int);
867 attribute_compat_text_section
868 NFTW_OLD_NAME (path
, func
, descriptors
, flags
)
874 flags
&= (FTW_PHYS
| FTW_MOUNT
| FTW_CHDIR
| FTW_DEPTH
);
875 return ftw_startup (path
, 1, func
, descriptors
, flags
);
878 compat_symbol (libc
, NFTW_OLD_NAME
, NFTW_NAME
, GLIBC_2_1
);