1 /* File tree walker functions.
2 Copyright (C) 1996-2001, 2002, 2003 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>
68 #if HAVE_SYS_PARAM_H || defined _LIBC
69 # include <sys/param.h>
72 # include <include/sys/stat.h>
74 # include <sys/stat.h>
77 #if ! _LIBC && !HAVE_DECL_STPCPY && !defined stpcpy
81 #if ! _LIBC && ! defined HAVE_MEMPCPY && ! defined mempcpy
82 /* Be CAREFUL that there are no side effects in N. */
83 # define mempcpy(D, S, N) ((void *) ((char *) memcpy (D, S, N) + (N)))
86 /* #define NDEBUG 1 */
91 # define __chdir chdir
93 # define __closedir closedir
95 # define __fchdir fchdir
97 # define __getcwd(P, N) xgetcwd ()
98 extern char *xgetcwd (void);
100 # define __mempcpy mempcpy
102 # define __opendir opendir
104 # define __readdir64 readdir
106 # define __stpcpy stpcpy
108 # define __tdestroy tdestroy
110 # define __tfind tfind
112 # define __tsearch tsearch
113 # undef internal_function
114 # define internal_function /* empty */
116 # define dirent64 dirent
118 # define MAX(a, b) ((a) > (b) ? (a) : (b))
121 /* Arrange to make lstat calls go through the wrapper function
122 on systems with an lstat function that does not dereference symlinks
123 that are specified with a trailing slash. */
124 #if ! _LIBC && ! LSTAT_FOLLOWS_SLASHED_SYMLINK
125 int rpl_lstat (const char *, struct stat
*);
127 # define lstat(Name, Stat_buf) rpl_lstat(Name, Stat_buf)
131 # define __set_errno(Val) errno = (Val)
134 /* Support for the LFS API version. */
136 # define FTW_NAME ftw
137 # define NFTW_NAME nftw
141 # define LXSTAT __lxstat
142 # define XSTAT __xstat
144 # define LXSTAT(V,f,sb) lstat (f,sb)
145 # define XSTAT(V,f,sb) stat (f,sb)
147 # define FTW_FUNC_T __ftw_func_t
148 # define NFTW_FUNC_T __nftw_func_t
151 /* We define PATH_MAX if the system does not provide a definition.
152 This does not artificially limit any operation. PATH_MAX is simply
153 used as a guesstimate for the expected maximal path length.
154 Buffers will be enlarged if necessary. */
156 # define PATH_MAX 1024
173 /* Array with pointers to open directory streams. */
174 struct dir_data
**dirstreams
;
178 /* Buffer containing name of currently processed object. */
182 /* Passed as fourth argument to `nftw' callback. The `base' member
183 tracks the content of the `dirbuf'. */
186 /* Flags passed to `nftw' function. 0 for `ftw'. */
189 /* Conversion array for flag values. It is the identity mapping for
190 `nftw' calls, otherwise it maps the values to those known by
194 /* Callback function. We always use the `nftw' form. */
197 /* Device of starting point. Needed for FTW_MOUNT. */
200 /* Data structure for keeping fingerprints of already processed
201 object. This is needed when not using FTW_PHYS. */
206 /* Internally we use the FTW_* constants used for `nftw'. When invoked
207 as `ftw', map each flag to the subset of values used by `ftw'. */
208 static const int nftw_arr
[] =
210 FTW_F
, FTW_D
, FTW_DNR
, FTW_NS
, FTW_SL
, FTW_DP
, FTW_SLN
213 static const int ftw_arr
[] =
215 FTW_F
, FTW_D
, FTW_DNR
, FTW_NS
, FTW_F
, FTW_D
, FTW_NS
219 /* Forward declarations of local functions. */
220 static int ftw_dir (struct ftw_data
*data
, struct STAT
*st
) internal_function
;
224 object_compare (const void *p1
, const void *p2
)
226 /* We don't need a sophisticated and useful comparison. We are only
227 interested in equality. However, we must be careful not to
228 accidentally compare `holes' in the structure. */
229 const struct known_object
*kp1
= p1
, *kp2
= p2
;
231 cmp1
= (kp1
->ino
> kp2
->ino
) - (kp1
->ino
< kp2
->ino
);
234 return (kp1
->dev
> kp2
->dev
) - (kp1
->dev
< kp2
->dev
);
239 add_object (struct ftw_data
*data
, struct STAT
*st
)
241 struct known_object
*newp
= malloc (sizeof (struct known_object
));
244 newp
->dev
= st
->st_dev
;
245 newp
->ino
= st
->st_ino
;
246 return __tsearch (newp
, &data
->known_objects
, object_compare
) ? 0 : -1;
251 find_object (struct ftw_data
*data
, struct STAT
*st
)
253 struct known_object obj
;
254 obj
.dev
= st
->st_dev
;
255 obj
.ino
= st
->st_ino
;
256 return __tfind (&obj
, &data
->known_objects
, object_compare
) != NULL
;
261 __attribute ((always_inline
))
262 open_dir_stream (struct ftw_data
*data
, struct dir_data
*dirp
)
266 if (data
->dirstreams
[data
->actdir
] != NULL
)
268 /* Oh, oh. We must close this stream. Get all remaining
269 entries and store them as a list in the `content' member of
270 the `struct dir_data' variable. */
271 size_t bufsize
= 1024;
272 char *buf
= malloc (bufsize
);
278 DIR *st
= data
->dirstreams
[data
->actdir
]->stream
;
282 while ((d
= __readdir64 (st
)) != NULL
)
284 size_t this_len
= NAMLEN (d
);
285 if (actsize
+ this_len
+ 2 >= bufsize
)
288 bufsize
+= MAX (1024, 2 * this_len
);
289 newp
= (char *) realloc (buf
, bufsize
);
292 /* No more memory. */
293 int save_err
= errno
;
295 __set_errno (save_err
);
302 *((char *) __mempcpy (buf
+ actsize
, d
->d_name
, this_len
))
304 actsize
+= this_len
+ 1;
307 /* Terminate the list with an additional NUL byte. */
308 buf
[actsize
++] = '\0';
310 /* Shrink the buffer to what we actually need. */
311 data
->dirstreams
[data
->actdir
]->content
= realloc (buf
, actsize
);
312 if (data
->dirstreams
[data
->actdir
]->content
== NULL
)
314 int save_err
= errno
;
316 __set_errno (save_err
);
322 data
->dirstreams
[data
->actdir
]->stream
= NULL
;
323 data
->dirstreams
[data
->actdir
] = NULL
;
328 /* Open the new stream. */
331 const char *name
= ((data
->flags
& FTW_CHDIR
)
332 ? data
->dirbuf
+ data
->ftw
.base
: data
->dirbuf
);
333 assert (data
->dirstreams
[data
->actdir
] == NULL
);
335 dirp
->stream
= __opendir (name
);
336 if (dirp
->stream
== NULL
)
340 dirp
->content
= NULL
;
341 data
->dirstreams
[data
->actdir
] = dirp
;
343 if (++data
->actdir
== data
->maxdir
)
354 process_entry (struct ftw_data
*data
, struct dir_data
*dir
, const char *name
,
362 if (name
[0] == '.' && (name
[1] == '\0'
363 || (name
[1] == '.' && name
[2] == '\0')))
364 /* Don't process the "." and ".." entries. */
367 new_buflen
= data
->ftw
.base
+ namlen
+ 2;
368 if (data
->dirbufsize
< new_buflen
)
370 /* Enlarge the buffer. */
373 data
->dirbufsize
= 2 * new_buflen
;
374 newp
= (char *) realloc (data
->dirbuf
, data
->dirbufsize
);
380 *((char *) __mempcpy (data
->dirbuf
+ data
->ftw
.base
, name
, namlen
)) = '\0';
382 if ((data
->flags
& FTW_CHDIR
) == 0)
385 if (((data
->flags
& FTW_PHYS
)
386 ? LXSTAT (_STAT_VER
, name
, &st
)
387 : XSTAT (_STAT_VER
, name
, &st
)) < 0)
389 if (errno
!= EACCES
&& errno
!= ENOENT
)
391 else if (!(data
->flags
& FTW_PHYS
)
392 && LXSTAT (_STAT_VER
, name
, &st
) == 0
393 && S_ISLNK (st
.st_mode
))
400 if (S_ISDIR (st
.st_mode
))
402 else if (S_ISLNK (st
.st_mode
))
410 || !(data
->flags
& FTW_MOUNT
) || st
.st_dev
== data
->dev
))
414 if ((data
->flags
& FTW_PHYS
)
415 || (!find_object (data
, &st
)
416 /* Remember the object. */
417 && (result
= add_object (data
, &st
)) == 0))
419 result
= ftw_dir (data
, &st
);
421 if (result
== 0 && (data
->flags
& FTW_CHDIR
))
423 /* Change back to the parent directory. */
425 if (dir
->stream
!= NULL
)
426 if (__fchdir (dirfd (dir
->stream
)) == 0)
431 if (data
->ftw
.base
== 1)
433 if (__chdir ("/") < 0)
437 if (__chdir ("..") < 0)
444 result
= (*data
->func
) (data
->dirbuf
, &st
, data
->cvt_arr
[flag
],
454 ftw_dir (struct ftw_data
*data
, struct STAT
*st
)
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
);
482 /* If necessary, change to this directory. */
483 if (data
->flags
& FTW_CHDIR
)
485 if (__fchdir (dirfd (dir
.stream
)) < 0)
487 int save_err
= errno
;
488 __closedir (dir
.stream
);
489 __set_errno (save_err
);
491 if (data
->actdir
-- == 0)
492 data
->actdir
= data
->maxdir
- 1;
493 data
->dirstreams
[data
->actdir
] = NULL
;
499 /* Next, update the `struct FTW' information. */
501 startp
= strchr (data
->dirbuf
, '\0');
502 /* There always must be a directory name. */
503 assert (startp
!= data
->dirbuf
);
504 if (startp
[-1] != '/')
506 data
->ftw
.base
= startp
- data
->dirbuf
;
508 while (dir
.stream
!= NULL
&& (d
= __readdir64 (dir
.stream
)) != NULL
)
510 result
= process_entry (data
, &dir
, d
->d_name
, NAMLEN (d
));
515 if (dir
.stream
!= NULL
)
517 /* The stream is still open. I.e., we did not need more
518 descriptors. Simply close the stream now. */
519 int save_err
= errno
;
521 assert (dir
.content
== NULL
);
523 __closedir (dir
.stream
);
524 __set_errno (save_err
);
526 if (data
->actdir
-- == 0)
527 data
->actdir
= data
->maxdir
- 1;
528 data
->dirstreams
[data
->actdir
] = NULL
;
533 char *runp
= dir
.content
;
535 while (result
== 0 && *runp
!= '\0')
537 char *endp
= strchr (runp
, '\0');
539 result
= process_entry (data
, &dir
, runp
, endp
- runp
);
546 __set_errno (save_err
);
549 /* Prepare the return, revert the `struct FTW' information. */
550 data
->dirbuf
[data
->ftw
.base
- 1] = '\0';
552 data
->ftw
.base
= previous_base
;
554 /* Finally, if we process depth-first report the directory. */
555 if (result
== 0 && (data
->flags
& FTW_DEPTH
))
556 result
= (*data
->func
) (data
->dirbuf
, st
, FTW_DP
, &data
->ftw
);
564 ftw_startup (const char *dir
, int is_nftw
, void *func
, int descriptors
,
567 struct ftw_data data
;
574 /* First make sure the parameters are reasonable. */
577 __set_errno (ENOENT
);
581 data
.maxdir
= descriptors
< 1 ? 1 : descriptors
;
583 data
.dirstreams
= (struct dir_data
**) alloca (data
.maxdir
584 * sizeof (struct dir_data
*));
585 memset (data
.dirstreams
, '\0', data
.maxdir
* sizeof (struct dir_data
*));
587 /* PATH_MAX is always defined when we get here. */
588 data
.dirbufsize
= MAX (2 * strlen (dir
), PATH_MAX
);
589 data
.dirbuf
= (char *) malloc (data
.dirbufsize
);
590 if (data
.dirbuf
== NULL
)
592 cp
= __stpcpy (data
.dirbuf
, dir
);
593 /* Strip trailing slashes. */
594 while (cp
> data
.dirbuf
+ 1 && cp
[-1] == '/')
601 while (cp
> data
.dirbuf
&& cp
[-1] != '/')
603 data
.ftw
.base
= cp
- data
.dirbuf
;
607 /* This assignment might seem to be strange but it is what we want.
608 The trick is that the first three arguments to the `ftw' and
609 `nftw' callback functions are equal. Therefore we can call in
610 every case the callback using the format of the `nftw' version
611 and get the correct result since the stack layout for a function
612 call in C allows this. */
613 data
.func
= (NFTW_FUNC_T
) func
;
615 /* Since we internally use the complete set of FTW_* values we need
616 to reduce the value range before calling a `ftw' callback. */
617 data
.cvt_arr
= is_nftw
? nftw_arr
: ftw_arr
;
619 /* No object known so far. */
620 data
.known_objects
= NULL
;
622 /* Now go to the directory containing the initial file/directory. */
623 if (flags
& FTW_CHDIR
)
625 /* GNU extension ahead. */
626 cwd
= __getcwd (NULL
, 0);
629 else if (data
.ftw
.base
> 0)
631 /* Change to the directory the file is in. In data.dirbuf
632 we have a writable copy of the file name. Just NUL
633 terminate it for now and change the directory. */
634 if (data
.ftw
.base
== 1)
635 /* I.e., the file is in the root directory. */
636 result
= __chdir ("/");
639 char ch
= data
.dirbuf
[data
.ftw
.base
- 1];
640 data
.dirbuf
[data
.ftw
.base
- 1] = '\0';
641 result
= __chdir (data
.dirbuf
);
642 data
.dirbuf
[data
.ftw
.base
- 1] = ch
;
647 /* Get stat info for start directory. */
650 const char *name
= ((data
.flags
& FTW_CHDIR
)
651 ? data
.dirbuf
+ data
.ftw
.base
654 if (((flags
& FTW_PHYS
)
655 ? LXSTAT (_STAT_VER
, name
, &st
)
656 : XSTAT (_STAT_VER
, name
, &st
)) < 0)
658 if (!(flags
& FTW_PHYS
)
660 && LXSTAT (_STAT_VER
, name
, &st
) == 0
661 && S_ISLNK (st
.st_mode
))
662 result
= (*data
.func
) (data
.dirbuf
, &st
, data
.cvt_arr
[FTW_SLN
],
665 /* No need to call the callback since we cannot say anything
671 if (S_ISDIR (st
.st_mode
))
673 /* Remember the device of the initial directory in case
674 FTW_MOUNT is given. */
675 data
.dev
= st
.st_dev
;
677 /* We know this directory now. */
678 if (!(flags
& FTW_PHYS
))
679 result
= add_object (&data
, &st
);
682 result
= ftw_dir (&data
, &st
);
686 int flag
= S_ISLNK (st
.st_mode
) ? FTW_SL
: FTW_F
;
688 result
= (*data
.func
) (data
.dirbuf
, &st
, data
.cvt_arr
[flag
],
694 /* Return to the start directory (if necessary). */
697 int save_err
= errno
;
700 __set_errno (save_err
);
703 /* Free all memory. */
705 __tdestroy (data
.known_objects
, free
);
707 __set_errno (save_err
);
717 FTW_NAME (path
, func
, descriptors
)
722 return ftw_startup (path
, 0, func
, descriptors
, 0);
726 NFTW_NAME (path
, func
, descriptors
, flags
)
732 return ftw_startup (path
, 1, func
, descriptors
, flags
);