1 /* File tree walker functions.
2 Copyright (C) 1996-2003, 2004, 2006 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
26 # define alloca __builtin_alloca
41 # define NAMLEN(dirent) _D_EXACT_NAMLEN (dirent)
45 # define NAMLEN(dirent) strlen ((dirent)->d_name)
47 # define dirent direct
48 # define NAMLEN(dirent) (dirent)->d_namlen
50 # include <sys/ndir.h>
69 #include <not-cancel.h>
70 #if HAVE_SYS_PARAM_H || defined _LIBC
71 # include <sys/param.h>
74 # include <include/sys/stat.h>
76 # include <sys/stat.h>
79 #if ! _LIBC && !HAVE_DECL_STPCPY && !defined stpcpy
83 #if ! _LIBC && ! defined HAVE_MEMPCPY && ! defined mempcpy
84 /* Be CAREFUL that there are no side effects in N. */
85 # define mempcpy(D, S, N) ((void *) ((char *) memcpy (D, S, N) + (N)))
88 /* #define NDEBUG 1 */
93 # define __chdir chdir
95 # define __closedir closedir
97 # define __fchdir fchdir
99 # define __getcwd(P, N) xgetcwd ()
100 extern char *xgetcwd (void);
102 # define __mempcpy mempcpy
104 # define __opendir opendir
106 # define __readdir64 readdir
108 # define __stpcpy stpcpy
110 # define __tdestroy tdestroy
112 # define __tfind tfind
114 # define __tsearch tsearch
115 # undef internal_function
116 # define internal_function /* empty */
118 # define dirent64 dirent
120 # define MAX(a, b) ((a) > (b) ? (a) : (b))
123 /* Arrange to make lstat calls go through the wrapper function
124 on systems with an lstat function that does not dereference symlinks
125 that are specified with a trailing slash. */
126 #if ! _LIBC && ! LSTAT_FOLLOWS_SLASHED_SYMLINK
127 int rpl_lstat (const char *, struct stat
*);
129 # define lstat(Name, Stat_buf) rpl_lstat(Name, Stat_buf)
133 # define __set_errno(Val) errno = (Val)
136 /* Support for the LFS API version. */
138 # define FTW_NAME ftw
139 # define NFTW_NAME nftw
140 # define NFTW_OLD_NAME __old_nftw
141 # define NFTW_NEW_NAME __new_nftw
145 # define LXSTAT __lxstat
146 # define XSTAT __xstat
147 # define FXSTATAT __fxstatat
149 # define LXSTAT(V,f,sb) lstat (f,sb)
150 # define XSTAT(V,f,sb) stat (f,sb)
151 # define FXSTATAT(V,d,f,sb,m) fstatat (d, f, sb, m)
153 # define FTW_FUNC_T __ftw_func_t
154 # define NFTW_FUNC_T __nftw_func_t
157 /* We define PATH_MAX if the system does not provide a definition.
158 This does not artificially limit any operation. PATH_MAX is simply
159 used as a guesstimate for the expected maximal path length.
160 Buffers will be enlarged if necessary. */
162 # define PATH_MAX 1024
180 /* Array with pointers to open directory streams. */
181 struct dir_data
**dirstreams
;
185 /* Buffer containing name of currently processed object. */
189 /* Passed as fourth argument to `nftw' callback. The `base' member
190 tracks the content of the `dirbuf'. */
193 /* Flags passed to `nftw' function. 0 for `ftw'. */
196 /* Conversion array for flag values. It is the identity mapping for
197 `nftw' calls, otherwise it maps the values to those known by
201 /* Callback function. We always use the `nftw' form. */
204 /* Device of starting point. Needed for FTW_MOUNT. */
207 /* Data structure for keeping fingerprints of already processed
208 object. This is needed when not using FTW_PHYS. */
213 /* Internally we use the FTW_* constants used for `nftw'. When invoked
214 as `ftw', map each flag to the subset of values used by `ftw'. */
215 static const int nftw_arr
[] =
217 FTW_F
, FTW_D
, FTW_DNR
, FTW_NS
, FTW_SL
, FTW_DP
, FTW_SLN
220 static const int ftw_arr
[] =
222 FTW_F
, FTW_D
, FTW_DNR
, FTW_NS
, FTW_F
, FTW_D
, FTW_NS
226 /* Forward declarations of local functions. */
227 static int ftw_dir (struct ftw_data
*data
, struct STAT
*st
,
228 struct dir_data
*old_dir
) internal_function
;
232 object_compare (const void *p1
, const void *p2
)
234 /* We don't need a sophisticated and useful comparison. We are only
235 interested in equality. However, we must be careful not to
236 accidentally compare `holes' in the structure. */
237 const struct known_object
*kp1
= p1
, *kp2
= p2
;
239 cmp1
= (kp1
->ino
> kp2
->ino
) - (kp1
->ino
< kp2
->ino
);
242 return (kp1
->dev
> kp2
->dev
) - (kp1
->dev
< kp2
->dev
);
247 add_object (struct ftw_data
*data
, struct STAT
*st
)
249 struct known_object
*newp
= malloc (sizeof (struct known_object
));
252 newp
->dev
= st
->st_dev
;
253 newp
->ino
= st
->st_ino
;
254 return __tsearch (newp
, &data
->known_objects
, object_compare
) ? 0 : -1;
259 find_object (struct ftw_data
*data
, struct STAT
*st
)
261 struct known_object obj
;
262 obj
.dev
= st
->st_dev
;
263 obj
.ino
= st
->st_ino
;
264 return __tfind (&obj
, &data
->known_objects
, object_compare
) != NULL
;
269 __attribute ((always_inline
))
270 open_dir_stream (int *dfdp
, struct ftw_data
*data
, struct dir_data
*dirp
)
274 if (data
->dirstreams
[data
->actdir
] != NULL
)
276 /* Oh, oh. We must close this stream. Get all remaining
277 entries and store them as a list in the `content' member of
278 the `struct dir_data' variable. */
279 size_t bufsize
= 1024;
280 char *buf
= malloc (bufsize
);
286 DIR *st
= data
->dirstreams
[data
->actdir
]->stream
;
290 while ((d
= __readdir64 (st
)) != NULL
)
292 size_t this_len
= NAMLEN (d
);
293 if (actsize
+ this_len
+ 2 >= bufsize
)
296 bufsize
+= MAX (1024, 2 * this_len
);
297 newp
= (char *) realloc (buf
, bufsize
);
300 /* No more memory. */
301 int save_err
= errno
;
303 __set_errno (save_err
);
309 *((char *) __mempcpy (buf
+ actsize
, d
->d_name
, this_len
))
311 actsize
+= this_len
+ 1;
314 /* Terminate the list with an additional NUL byte. */
315 buf
[actsize
++] = '\0';
317 /* Shrink the buffer to what we actually need. */
318 data
->dirstreams
[data
->actdir
]->content
= realloc (buf
, actsize
);
319 if (data
->dirstreams
[data
->actdir
]->content
== NULL
)
321 int save_err
= errno
;
323 __set_errno (save_err
);
329 data
->dirstreams
[data
->actdir
]->stream
= NULL
;
330 data
->dirstreams
[data
->actdir
]->streamfd
= -1;
331 data
->dirstreams
[data
->actdir
] = NULL
;
336 /* Open the new stream. */
339 assert (data
->dirstreams
[data
->actdir
] == NULL
);
341 if (dfdp
!= NULL
&& *dfdp
!= -1)
343 int fd
= openat64_not_cancel_3 (*dfdp
, data
->dirbuf
+ data
->ftw
.base
,
344 O_RDONLY
| O_DIRECTORY
| O_NDELAY
);
346 if (fd
!= -1 && (dirp
->stream
= __fdopendir (fd
)) == NULL
)
347 close_not_cancel_no_status (fd
);
351 const char *name
= ((data
->flags
& FTW_CHDIR
)
352 ? data
->dirbuf
+ data
->ftw
.base
: data
->dirbuf
);
353 dirp
->stream
= __opendir (name
);
356 if (dirp
->stream
== NULL
)
360 dirp
->streamfd
= dirfd (dirp
->stream
);
361 dirp
->content
= NULL
;
362 data
->dirstreams
[data
->actdir
] = dirp
;
364 if (++data
->actdir
== data
->maxdir
)
375 process_entry (struct ftw_data
*data
, struct dir_data
*dir
, const char *name
,
376 size_t namlen
, int d_type
)
383 if (name
[0] == '.' && (name
[1] == '\0'
384 || (name
[1] == '.' && name
[2] == '\0')))
385 /* Don't process the "." and ".." entries. */
388 new_buflen
= data
->ftw
.base
+ namlen
+ 2;
389 if (data
->dirbufsize
< new_buflen
)
391 /* Enlarge the buffer. */
394 data
->dirbufsize
= 2 * new_buflen
;
395 newp
= (char *) realloc (data
->dirbuf
, data
->dirbufsize
);
401 *((char *) __mempcpy (data
->dirbuf
+ data
->ftw
.base
, name
, namlen
)) = '\0';
404 if (dir
->streamfd
!= -1)
405 statres
= FXSTATAT (_STAT_VER
, dir
->streamfd
, name
, &st
,
406 (data
->flags
& FTW_PHYS
) ? AT_SYMLINK_NOFOLLOW
: 0);
409 if ((data
->flags
& FTW_CHDIR
) == 0)
412 statres
= ((data
->flags
& FTW_PHYS
)
413 ? LXSTAT (_STAT_VER
, name
, &st
)
414 : XSTAT (_STAT_VER
, name
, &st
));
419 if (errno
!= EACCES
&& errno
!= ENOENT
)
421 else if (data
->flags
& FTW_PHYS
)
423 else if (d_type
== DT_LNK
)
427 if (dir
->streamfd
!= -1)
428 statres
= FXSTATAT (_STAT_VER
, dir
->streamfd
, name
, &st
,
429 AT_SYMLINK_NOFOLLOW
);
431 statres
= LXSTAT (_STAT_VER
, name
, &st
);
432 if (statres
== 0 && S_ISLNK (st
.st_mode
))
440 if (S_ISDIR (st
.st_mode
))
442 else if (S_ISLNK (st
.st_mode
))
450 || !(data
->flags
& FTW_MOUNT
) || st
.st_dev
== data
->dev
))
454 if ((data
->flags
& FTW_PHYS
)
455 || (!find_object (data
, &st
)
456 /* Remember the object. */
457 && (result
= add_object (data
, &st
)) == 0))
458 result
= ftw_dir (data
, &st
, dir
);
461 result
= (*data
->func
) (data
->dirbuf
, &st
, data
->cvt_arr
[flag
],
465 if ((data
->flags
& FTW_ACTIONRETVAL
) && result
== FTW_SKIP_SUBTREE
)
473 __attribute ((noinline
))
475 ftw_dir (struct ftw_data
*data
, struct STAT
*st
, struct dir_data
*old_dir
)
479 int previous_base
= data
->ftw
.base
;
483 /* Open the stream for this directory. This might require that
484 another stream has to be closed. */
485 result
= open_dir_stream (old_dir
== NULL
? NULL
: &old_dir
->streamfd
,
490 /* We cannot read the directory. Signal this with a special flag. */
491 result
= (*data
->func
) (data
->dirbuf
, st
, FTW_DNR
, &data
->ftw
);
496 /* First, report the directory (if not depth-first). */
497 if (!(data
->flags
& FTW_DEPTH
))
499 result
= (*data
->func
) (data
->dirbuf
, st
, FTW_D
, &data
->ftw
);
505 __closedir (dir
.stream
);
507 __set_errno (save_err
);
509 if (data
->actdir
-- == 0)
510 data
->actdir
= data
->maxdir
- 1;
511 data
->dirstreams
[data
->actdir
] = NULL
;
516 /* If necessary, change to this directory. */
517 if (data
->flags
& FTW_CHDIR
)
519 if (__fchdir (dirfd (dir
.stream
)) < 0)
526 /* Next, update the `struct FTW' information. */
528 startp
= __rawmemchr (data
->dirbuf
, '\0');
529 /* There always must be a directory name. */
530 assert (startp
!= data
->dirbuf
);
531 if (startp
[-1] != '/')
533 data
->ftw
.base
= startp
- data
->dirbuf
;
535 while (dir
.stream
!= NULL
&& (d
= __readdir64 (dir
.stream
)) != NULL
)
537 result
= process_entry (data
, &dir
, d
->d_name
, NAMLEN (d
), d
->d_type
);
542 if (dir
.stream
!= NULL
)
544 /* The stream is still open. I.e., we did not need more
545 descriptors. Simply close the stream now. */
546 int save_err
= errno
;
548 assert (dir
.content
== NULL
);
550 __closedir (dir
.stream
);
552 __set_errno (save_err
);
554 if (data
->actdir
-- == 0)
555 data
->actdir
= data
->maxdir
- 1;
556 data
->dirstreams
[data
->actdir
] = NULL
;
561 char *runp
= dir
.content
;
563 while (result
== 0 && *runp
!= '\0')
565 char *endp
= strchr (runp
, '\0');
567 // XXX Should store the d_type values as well?!
568 result
= process_entry (data
, &dir
, runp
, endp
- runp
, DT_UNKNOWN
);
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
;
633 /* First make sure the parameters are reasonable. */
636 __set_errno (ENOENT
);
640 data
.maxdir
= descriptors
< 1 ? 1 : descriptors
;
642 data
.dirstreams
= (struct dir_data
**) alloca (data
.maxdir
643 * sizeof (struct dir_data
*));
644 memset (data
.dirstreams
, '\0', data
.maxdir
* sizeof (struct dir_data
*));
646 /* PATH_MAX is always defined when we get here. */
647 data
.dirbufsize
= MAX (2 * strlen (dir
), PATH_MAX
);
648 data
.dirbuf
= (char *) malloc (data
.dirbufsize
);
649 if (data
.dirbuf
== NULL
)
651 cp
= __stpcpy (data
.dirbuf
, dir
);
652 /* Strip trailing slashes. */
653 while (cp
> data
.dirbuf
+ 1 && cp
[-1] == '/')
660 while (cp
> data
.dirbuf
&& cp
[-1] != '/')
662 data
.ftw
.base
= cp
- data
.dirbuf
;
666 /* This assignment might seem to be strange but it is what we want.
667 The trick is that the first three arguments to the `ftw' and
668 `nftw' callback functions are equal. Therefore we can call in
669 every case the callback using the format of the `nftw' version
670 and get the correct result since the stack layout for a function
671 call in C allows this. */
672 data
.func
= (NFTW_FUNC_T
) func
;
674 /* Since we internally use the complete set of FTW_* values we need
675 to reduce the value range before calling a `ftw' callback. */
676 data
.cvt_arr
= is_nftw
? nftw_arr
: ftw_arr
;
678 /* No object known so far. */
679 data
.known_objects
= NULL
;
681 /* Now go to the directory containing the initial file/directory. */
682 if (flags
& FTW_CHDIR
)
684 /* We have to be able to go back to the current working
685 directory. The best way to do this is to use a file
687 cwdfd
= __open (".", O_RDONLY
| O_DIRECTORY
);
690 /* Try getting the directory name. This can be needed if
691 the current directory is executable but not readable. */
693 /* GNU extension ahead. */
694 cwd
= __getcwd (NULL
, 0);
699 else if (data
.maxdir
> 1)
700 /* Account for the file descriptor we use here. */
703 if (data
.ftw
.base
> 0)
705 /* Change to the directory the file is in. In data.dirbuf
706 we have a writable copy of the file name. Just NUL
707 terminate it for now and change the directory. */
708 if (data
.ftw
.base
== 1)
709 /* I.e., the file is in the root directory. */
710 result
= __chdir ("/");
713 char ch
= data
.dirbuf
[data
.ftw
.base
- 1];
714 data
.dirbuf
[data
.ftw
.base
- 1] = '\0';
715 result
= __chdir (data
.dirbuf
);
716 data
.dirbuf
[data
.ftw
.base
- 1] = ch
;
721 /* Get stat info for start directory. */
724 const char *name
= ((data
.flags
& FTW_CHDIR
)
725 ? data
.dirbuf
+ data
.ftw
.base
728 if (((flags
& FTW_PHYS
)
729 ? LXSTAT (_STAT_VER
, name
, &st
)
730 : XSTAT (_STAT_VER
, name
, &st
)) < 0)
732 if (!(flags
& FTW_PHYS
)
734 && LXSTAT (_STAT_VER
, name
, &st
) == 0
735 && S_ISLNK (st
.st_mode
))
736 result
= (*data
.func
) (data
.dirbuf
, &st
, data
.cvt_arr
[FTW_SLN
],
739 /* No need to call the callback since we cannot say anything
745 if (S_ISDIR (st
.st_mode
))
747 /* Remember the device of the initial directory in case
748 FTW_MOUNT is given. */
749 data
.dev
= st
.st_dev
;
751 /* We know this directory now. */
752 if (!(flags
& FTW_PHYS
))
753 result
= add_object (&data
, &st
);
756 result
= ftw_dir (&data
, &st
, NULL
);
760 int flag
= S_ISLNK (st
.st_mode
) ? FTW_SL
: FTW_F
;
762 result
= (*data
.func
) (data
.dirbuf
, &st
, data
.cvt_arr
[flag
],
767 if ((flags
& FTW_ACTIONRETVAL
)
768 && (result
== FTW_SKIP_SUBTREE
|| result
== FTW_SKIP_SIBLINGS
))
772 /* Return to the start directory (if necessary). */
775 int save_err
= errno
;
777 __set_errno (save_err
);
779 else if (cwd
!= NULL
)
781 int save_err
= errno
;
784 __set_errno (save_err
);
787 /* Free all memory. */
790 __tdestroy (data
.known_objects
, free
);
792 __set_errno (save_err
);
802 FTW_NAME (path
, func
, descriptors
)
807 return ftw_startup (path
, 0, func
, descriptors
, 0);
812 NFTW_NAME (path
, func
, descriptors
, flags
)
818 return ftw_startup (path
, 1, func
, descriptors
, flags
);
822 # include <shlib-compat.h>
824 int NFTW_NEW_NAME (const char *, NFTW_FUNC_T
, int, int);
827 NFTW_NEW_NAME (path
, func
, descriptors
, flags
)
834 & ~(FTW_PHYS
| FTW_MOUNT
| FTW_CHDIR
| FTW_DEPTH
| FTW_ACTIONRETVAL
))
836 __set_errno (EINVAL
);
839 return ftw_startup (path
, 1, func
, descriptors
, flags
);
842 versioned_symbol (libc
, NFTW_NEW_NAME
, NFTW_NAME
, GLIBC_2_3_3
);
844 # if SHLIB_COMPAT(libc, GLIBC_2_1, GLIBC_2_3_3)
846 /* Older nftw* version just ignored all unknown flags. */
848 int NFTW_OLD_NAME (const char *, NFTW_FUNC_T
, int, int);
851 attribute_compat_text_section
852 NFTW_OLD_NAME (path
, func
, descriptors
, flags
)
858 flags
&= (FTW_PHYS
| FTW_MOUNT
| FTW_CHDIR
| FTW_DEPTH
);
859 return ftw_startup (path
, 1, func
, descriptors
, flags
);
862 compat_symbol (libc
, NFTW_OLD_NAME
, NFTW_NAME
, GLIBC_2_1
);