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 # define __readdir64 readdir64
116 # define __stpcpy stpcpy
118 # define __tdestroy tdestroy
120 # define __tfind tfind
122 # define __tsearch tsearch
123 # undef internal_function
124 # define internal_function /* empty */
126 # define MAX(a, b) ((a) > (b) ? (a) : (b))
129 /* Arrange to make lstat calls go through the wrapper function
130 on systems with an lstat function that does not dereference symlinks
131 that are specified with a trailing slash. */
132 #if !defined _LIBC && !defined LSTAT_FOLLOWS_SLASHED_SYMLINK && !defined __UCLIBC__
133 int rpl_lstat (const char *, struct stat
*);
135 # define lstat(Name, Stat_buf) rpl_lstat(Name, Stat_buf)
139 # define __set_errno(Val) errno = (Val)
142 /* Support for the LFS API version. */
144 # define FTW_NAME ftw
145 # define NFTW_NAME nftw
146 # define NFTW_OLD_NAME __old_nftw
147 # define NFTW_NEW_NAME __new_nftw
151 # define LXSTAT __lxstat
152 # define XSTAT __xstat
154 # define LXSTAT(V,f,sb) lstat (f,sb)
155 # define XSTAT(V,f,sb) stat (f,sb)
157 # define FTW_FUNC_T __ftw_func_t
158 # define NFTW_FUNC_T __nftw_func_t
161 /* We define PATH_MAX if the system does not provide a definition.
162 This does not artificially limit any operation. PATH_MAX is simply
163 used as a guesstimate for the expected maximal path length.
164 Buffers will be enlarged if necessary. */
166 # define PATH_MAX 1024
183 /* Array with pointers to open directory streams. */
184 struct dir_data
**dirstreams
;
188 /* Buffer containing name of currently processed object. */
192 /* Passed as fourth argument to `nftw' callback. The `base' member
193 tracks the content of the `dirbuf'. */
196 /* Flags passed to `nftw' function. 0 for `ftw'. */
199 /* Conversion array for flag values. It is the identity mapping for
200 `nftw' calls, otherwise it maps the values to those known by
204 /* Callback function. We always use the `nftw' form. */
207 /* Device of starting point. Needed for FTW_MOUNT. */
210 /* Data structure for keeping fingerprints of already processed
211 object. This is needed when not using FTW_PHYS. */
216 /* Internally we use the FTW_* constants used for `nftw'. When invoked
217 as `ftw', map each flag to the subset of values used by `ftw'. */
218 static const int nftw_arr
[] =
220 FTW_F
, FTW_D
, FTW_DNR
, FTW_NS
, FTW_SL
, FTW_DP
, FTW_SLN
223 static const int ftw_arr
[] =
225 FTW_F
, FTW_D
, FTW_DNR
, FTW_NS
, FTW_F
, FTW_D
, FTW_NS
229 /* Forward declarations of local functions. */
230 static int ftw_dir (struct ftw_data
*data
, struct STAT
*st
,
231 struct dir_data
*old_dir
) internal_function
;
235 object_compare (const void *p1
, const void *p2
)
237 /* We don't need a sophisticated and useful comparison. We are only
238 interested in equality. However, we must be careful not to
239 accidentally compare `holes' in the structure. */
240 const struct known_object
*kp1
= p1
, *kp2
= p2
;
242 cmp1
= (kp1
->ino
> kp2
->ino
) - (kp1
->ino
< kp2
->ino
);
245 return (kp1
->dev
> kp2
->dev
) - (kp1
->dev
< kp2
->dev
);
249 static __inline__
int
250 add_object (struct ftw_data
*data
, struct STAT
*st
)
252 struct known_object
*newp
= malloc (sizeof (struct known_object
));
255 newp
->dev
= st
->st_dev
;
256 newp
->ino
= st
->st_ino
;
257 return __tsearch (newp
, &data
->known_objects
, object_compare
) ? 0 : -1;
261 static __inline__
int
262 find_object (struct ftw_data
*data
, struct STAT
*st
)
264 struct known_object obj
;
265 obj
.dev
= st
->st_dev
;
266 obj
.ino
= st
->st_ino
;
267 return __tfind (&obj
, &data
->known_objects
, object_compare
) != NULL
;
271 static __inline__
int
272 __attribute ((always_inline
))
273 open_dir_stream (struct ftw_data
*data
, struct dir_data
*dirp
)
277 if (data
->dirstreams
[data
->actdir
] != NULL
)
279 /* Oh, oh. We must close this stream. Get all remaining
280 entries and store them as a list in the `content' member of
281 the `struct dir_data' variable. */
282 size_t bufsize
= 1024;
283 char *buf
= malloc (bufsize
);
289 DIR *st
= data
->dirstreams
[data
->actdir
]->stream
;
293 while ((d
= __readdir64 (st
)) != NULL
)
295 size_t this_len
= NAMLEN (d
);
296 if (actsize
+ this_len
+ 2 >= bufsize
)
299 bufsize
+= MAX (1024, 2 * this_len
);
300 newp
= (char *) realloc (buf
, bufsize
);
303 /* No more memory. */
304 int save_err
= errno
;
306 __set_errno (save_err
);
313 *((char *) __mempcpy (buf
+ actsize
, d
->d_name
, this_len
))
315 actsize
+= this_len
+ 1;
318 /* Terminate the list with an additional NUL byte. */
319 buf
[actsize
++] = '\0';
321 /* Shrink the buffer to what we actually need. */
322 data
->dirstreams
[data
->actdir
]->content
= realloc (buf
, actsize
);
323 if (data
->dirstreams
[data
->actdir
]->content
== NULL
)
325 int save_err
= errno
;
327 __set_errno (save_err
);
333 data
->dirstreams
[data
->actdir
]->stream
= NULL
;
334 data
->dirstreams
[data
->actdir
] = NULL
;
339 /* Open the new stream. */
342 const char *name
= ((data
->flags
& FTW_CHDIR
)
343 ? data
->dirbuf
+ data
->ftw
.base
: data
->dirbuf
);
344 assert (data
->dirstreams
[data
->actdir
] == NULL
);
346 dirp
->stream
= __opendir (name
);
347 if (dirp
->stream
== NULL
)
351 dirp
->content
= NULL
;
352 data
->dirstreams
[data
->actdir
] = dirp
;
354 if (++data
->actdir
== data
->maxdir
)
365 process_entry (struct ftw_data
*data
, struct dir_data
*dir
, const char *name
,
373 if (name
[0] == '.' && (name
[1] == '\0'
374 || (name
[1] == '.' && name
[2] == '\0')))
375 /* Don't process the "." and ".." entries. */
378 new_buflen
= data
->ftw
.base
+ namlen
+ 2;
379 if (data
->dirbufsize
< new_buflen
)
381 /* Enlarge the buffer. */
384 data
->dirbufsize
= 2 * new_buflen
;
385 newp
= (char *) realloc (data
->dirbuf
, data
->dirbufsize
);
391 *((char *) __mempcpy (data
->dirbuf
+ data
->ftw
.base
, name
, namlen
)) = '\0';
393 if ((data
->flags
& FTW_CHDIR
) == 0)
396 if (((data
->flags
& FTW_PHYS
)
397 ? LXSTAT (_STAT_VER
, name
, &st
)
398 : XSTAT (_STAT_VER
, name
, &st
)) < 0)
400 if (errno
!= EACCES
&& errno
!= ENOENT
)
402 else if (!(data
->flags
& FTW_PHYS
)
403 && LXSTAT (_STAT_VER
, name
, &st
) == 0
404 && S_ISLNK (st
.st_mode
))
411 if (S_ISDIR (st
.st_mode
))
413 else if (S_ISLNK (st
.st_mode
))
421 || !(data
->flags
& FTW_MOUNT
) || st
.st_dev
== data
->dev
))
425 if ((data
->flags
& FTW_PHYS
)
426 || (!find_object (data
, &st
)
427 /* Remember the object. */
428 && (result
= add_object (data
, &st
)) == 0))
429 result
= ftw_dir (data
, &st
, dir
);
432 result
= (*data
->func
) (data
->dirbuf
, &st
, data
->cvt_arr
[flag
],
436 if ((data
->flags
& FTW_ACTIONRETVAL
) && result
== FTW_SKIP_SUBTREE
)
444 __attribute ((noinline
))
446 ftw_dir (struct ftw_data
*data
, struct STAT
*st
, struct dir_data
*old_dir
)
450 int previous_base
= data
->ftw
.base
;
454 /* Open the stream for this directory. This might require that
455 another stream has to be closed. */
456 result
= open_dir_stream (data
, &dir
);
460 /* We cannot read the directory. Signal this with a special flag. */
461 result
= (*data
->func
) (data
->dirbuf
, st
, FTW_DNR
, &data
->ftw
);
466 /* First, report the directory (if not depth-first). */
467 if (!(data
->flags
& FTW_DEPTH
))
469 result
= (*data
->func
) (data
->dirbuf
, st
, FTW_D
, &data
->ftw
);
475 __closedir (dir
.stream
);
476 __set_errno (save_err
);
478 if (data
->actdir
-- == 0)
479 data
->actdir
= data
->maxdir
- 1;
480 data
->dirstreams
[data
->actdir
] = NULL
;
485 /* If necessary, change to this directory. */
486 if (data
->flags
& FTW_CHDIR
)
488 if (__fchdir (dirfd (dir
.stream
)) < 0)
495 /* Next, update the `struct FTW' information. */
497 startp
= strchr (data
->dirbuf
, '\0');
498 /* There always must be a directory name. */
499 assert (startp
!= data
->dirbuf
);
500 if (startp
[-1] != '/')
502 data
->ftw
.base
= startp
- data
->dirbuf
;
504 while (dir
.stream
!= NULL
&& (d
= __readdir64 (dir
.stream
)) != NULL
)
506 result
= process_entry (data
, &dir
, d
->d_name
, NAMLEN (d
));
511 if (dir
.stream
!= NULL
)
513 /* The stream is still open. I.e., we did not need more
514 descriptors. Simply close the stream now. */
515 int save_err
= errno
;
517 assert (dir
.content
== NULL
);
519 __closedir (dir
.stream
);
520 __set_errno (save_err
);
522 if (data
->actdir
-- == 0)
523 data
->actdir
= data
->maxdir
- 1;
524 data
->dirstreams
[data
->actdir
] = NULL
;
529 char *runp
= dir
.content
;
531 while (result
== 0 && *runp
!= '\0')
533 char *endp
= strchr (runp
, '\0');
535 result
= process_entry (data
, &dir
, runp
, endp
- runp
);
542 __set_errno (save_err
);
545 if ((data
->flags
& FTW_ACTIONRETVAL
) && result
== FTW_SKIP_SIBLINGS
)
548 /* Prepare the return, revert the `struct FTW' information. */
549 data
->dirbuf
[data
->ftw
.base
- 1] = '\0';
551 data
->ftw
.base
= previous_base
;
553 if ((data
->flags
& FTW_CHDIR
)
555 || ((data
->flags
& FTW_ACTIONRETVAL
)
556 && (result
!= -1 && result
!= FTW_STOP
))))
558 /* Change back to the parent directory. */
560 if (old_dir
&& old_dir
->stream
!= NULL
)
561 if (__fchdir (dirfd (old_dir
->stream
)) == 0)
566 if (data
->ftw
.base
== 1)
568 if (__chdir ("/") < 0)
572 if (__chdir ("..") < 0)
577 /* Finally, if we process depth-first report the directory. */
578 if (result
== 0 && (data
->flags
& FTW_DEPTH
))
579 result
= (*data
->func
) (data
->dirbuf
, st
, FTW_DP
, &data
->ftw
);
586 __attribute ((noinline
))
588 ftw_startup (const char *dir
, int is_nftw
, void *func
, int descriptors
,
591 struct ftw_data data
;
598 /* First make sure the parameters are reasonable. */
601 __set_errno (ENOENT
);
605 data
.maxdir
= descriptors
< 1 ? 1 : descriptors
;
607 data
.dirstreams
= (struct dir_data
**) alloca (data
.maxdir
608 * sizeof (struct dir_data
*));
609 memset (data
.dirstreams
, '\0', data
.maxdir
* sizeof (struct dir_data
*));
611 /* PATH_MAX is always defined when we get here. */
612 data
.dirbufsize
= MAX (2 * strlen (dir
), PATH_MAX
);
613 data
.dirbuf
= (char *) malloc (data
.dirbufsize
);
614 if (data
.dirbuf
== NULL
)
616 cp
= __stpcpy (data
.dirbuf
, dir
);
617 /* Strip trailing slashes. */
618 while (cp
> data
.dirbuf
+ 1 && cp
[-1] == '/')
625 while (cp
> data
.dirbuf
&& cp
[-1] != '/')
627 data
.ftw
.base
= cp
- data
.dirbuf
;
631 /* This assignment might seem to be strange but it is what we want.
632 The trick is that the first three arguments to the `ftw' and
633 `nftw' callback functions are equal. Therefore we can call in
634 every case the callback using the format of the `nftw' version
635 and get the correct result since the stack layout for a function
636 call in C allows this. */
637 data
.func
= (NFTW_FUNC_T
) func
;
639 /* Since we internally use the complete set of FTW_* values we need
640 to reduce the value range before calling a `ftw' callback. */
641 data
.cvt_arr
= is_nftw
? nftw_arr
: ftw_arr
;
643 /* No object known so far. */
644 data
.known_objects
= NULL
;
646 /* Now go to the directory containing the initial file/directory. */
647 if (flags
& FTW_CHDIR
)
649 /* GNU extension ahead. */
650 cwd
= __getcwd (NULL
, 0);
653 else if (data
.ftw
.base
> 0)
655 /* Change to the directory the file is in. In data.dirbuf
656 we have a writable copy of the file name. Just NUL
657 terminate it for now and change the directory. */
658 if (data
.ftw
.base
== 1)
659 /* I.e., the file is in the root directory. */
660 result
= __chdir ("/");
663 char ch
= data
.dirbuf
[data
.ftw
.base
- 1];
664 data
.dirbuf
[data
.ftw
.base
- 1] = '\0';
665 result
= __chdir (data
.dirbuf
);
666 data
.dirbuf
[data
.ftw
.base
- 1] = ch
;
671 /* Get stat info for start directory. */
674 const char *name
= ((data
.flags
& FTW_CHDIR
)
675 ? data
.dirbuf
+ data
.ftw
.base
678 if (((flags
& FTW_PHYS
)
679 ? LXSTAT (_STAT_VER
, name
, &st
)
680 : XSTAT (_STAT_VER
, name
, &st
)) < 0)
682 if (!(flags
& FTW_PHYS
)
684 && LXSTAT (_STAT_VER
, name
, &st
) == 0
685 && S_ISLNK (st
.st_mode
))
686 result
= (*data
.func
) (data
.dirbuf
, &st
, data
.cvt_arr
[FTW_SLN
],
689 /* No need to call the callback since we cannot say anything
695 if (S_ISDIR (st
.st_mode
))
697 /* Remember the device of the initial directory in case
698 FTW_MOUNT is given. */
699 data
.dev
= st
.st_dev
;
701 /* We know this directory now. */
702 if (!(flags
& FTW_PHYS
))
703 result
= add_object (&data
, &st
);
706 result
= ftw_dir (&data
, &st
, NULL
);
710 int flag
= S_ISLNK (st
.st_mode
) ? FTW_SL
: FTW_F
;
712 result
= (*data
.func
) (data
.dirbuf
, &st
, data
.cvt_arr
[flag
],
717 if ((flags
& FTW_ACTIONRETVAL
)
718 && (result
== FTW_SKIP_SUBTREE
|| result
== FTW_SKIP_SIBLINGS
))
722 /* Return to the start directory (if necessary). */
728 __set_errno (save_err
);
731 /* Free all memory. */
733 __tdestroy (data
.known_objects
, free
);
735 __set_errno (save_err
);
743 #ifdef __UCLIBC_HAS_FTW__
745 FTW_NAME (const char *path
, FTW_FUNC_T func
, int descriptors
)
747 return ftw_startup (path
, 0, func
, descriptors
, 0);
751 #ifdef __UCLIBC_HAS_NFTW__
754 NFTW_NAME (const char *path
, NFTW_FUNC_T func
, int descriptors
, int flags
)
756 return ftw_startup (path
, 1, func
, descriptors
, flags
);
760 #include <shlib-compat.h>
762 int NFTW_NEW_NAME (const char *, NFTW_FUNC_T
, int, int);
765 NFTW_NEW_NAME (const char *path
, NFTW_FUNC_T func
, int descriptors
, int flags
)
768 & ~(FTW_PHYS
| FTW_MOUNT
| FTW_CHDIR
| FTW_DEPTH
| FTW_ACTIONRETVAL
))
770 __set_errno (EINVAL
);
773 return ftw_startup (path
, 1, func
, descriptors
, flags
);
776 versioned_symbol (libc
, NFTW_NEW_NAME
, NFTW_NAME
, GLIBC_2_3_3
);
778 #if SHLIB_COMPAT(libc, GLIBC_2_1, GLIBC_2_3_3)
780 /* Older nftw* version just ignored all unknown flags. */
782 int NFTW_OLD_NAME (const char *, NFTW_FUNC_T
, int, int);
785 attribute_compat_text_section
786 NFTW_OLD_NAME (const char *path
, NFTW_FUNC_T func
, int descriptors
, int flags
)
788 flags
&= (FTW_PHYS
| FTW_MOUNT
| FTW_CHDIR
| FTW_DEPTH
);
789 return ftw_startup (path
, 1, func
, descriptors
, flags
);
792 compat_symbol (libc
, NFTW_OLD_NAME
, NFTW_NAME
, GLIBC_2_1
);