]>
git.ipfire.org Git - thirdparty/glibc.git/blob - io/ftw.c
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
33 #if HAVE_SYS_PARAM_H || defined _LIBC
34 # include <sys/param.h>
37 # include <include/sys/stat.h>
39 # include <sys/stat.h>
42 /* #define NDEBUG 1 */
47 # define __chdir chdir
49 # define __closedir closedir
51 # define __fchdir fchdir
53 # define __getcwd getcwd
55 # define __opendir opendir
57 # define __readdir64 readdir
59 # define __tdestroy tdestroy
61 # define __tfind tfind
63 # define __tsearch tsearch
64 # undef internal_function
65 # define internal_function /* empty */
67 # define dirent64 dirent
69 # define MAX(a, b) ((a) > (b) ? (a) : (b))
73 # define __set_errno(Val) errno = (Val)
76 /* Support for the LFS API version. */
79 # define NFTW_NAME nftw
82 # define LXSTAT __lxstat
83 # define XSTAT __xstat
84 # define FTW_FUNC_T __ftw_func_t
85 # define NFTW_FUNC_T __nftw_func_t
88 /* We define PATH_MAX if the system does not provide a definition.
89 This does not artificially limit any operation. PATH_MAX is simply
90 used as a guesstimate for the expected maximal path length.
91 Buffers will be enlarged if necessary. */
93 # define PATH_MAX 1024
110 /* Array with pointers to open directory streams. */
111 struct dir_data
**dirstreams
;
115 /* Buffer containing name of currently processed object. */
119 /* Passed as fourth argument to `nftw' callback. The `base' member
120 tracks the content of the `dirbuf'. */
123 /* Flags passed to `nftw' function. 0 for `ftw'. */
126 /* Conversion array for flag values. It is the identity mapping for
127 `nftw' calls, otherwise it maps the values to those know by
131 /* Callback function. We always use the `nftw' form. */
134 /* Device of starting point. Needed for FTW_MOUNT. */
137 /* Data structure for keeping fingerprints of already processed
138 object. This is needed when not using FTW_PHYS. */
143 /* Internally we use the FTW_* constants used for `nftw'. When the
144 process called `ftw' we must reduce the flag to the known flags
146 static const int nftw_arr
[] =
148 FTW_F
, FTW_D
, FTW_DNR
, FTW_NS
, FTW_SL
, FTW_DP
, FTW_SLN
151 static const int ftw_arr
[] =
153 FTW_F
, FTW_D
, FTW_DNR
, FTW_NS
, FTW_F
, FTW_D
, FTW_NS
157 /* Forward declarations of local functions. */
158 static int ftw_dir (struct ftw_data
*data
, struct STAT
*st
) internal_function
;
162 object_compare (const void *p1
, const void *p2
)
164 /* We don't need a sophisticated and useful comparison. We are only
165 interested in equality. However, we must be careful not to
166 accidentally compare `holes' in the structure. */
167 const struct known_object
*kp1
= p1
, *kp2
= p2
;
169 cmp1
= (kp1
->ino
> kp2
->ino
) - (kp1
->ino
< kp2
->ino
);
172 return (kp1
->dev
> kp2
->dev
) - (kp1
->dev
< kp2
->dev
);
177 add_object (struct ftw_data
*data
, struct STAT
*st
)
179 struct known_object
*newp
= malloc (sizeof (struct known_object
));
182 newp
->dev
= st
->st_dev
;
183 newp
->ino
= st
->st_ino
;
184 return __tsearch (newp
, &data
->known_objects
, object_compare
) ? 0 : -1;
189 find_object (struct ftw_data
*data
, struct STAT
*st
)
191 struct known_object obj
= { .dev
= st
->st_dev
, .ino
= st
->st_ino
};
192 return __tfind (&obj
, &data
->known_objects
, object_compare
) != NULL
;
197 open_dir_stream (struct ftw_data
*data
, struct dir_data
*dirp
)
201 if (data
->dirstreams
[data
->actdir
] != NULL
)
203 /* Oh, oh. We must close this stream. Get all remaining
204 entries and store them as a list in the `content' member of
205 the `struct dir_data' variable. */
206 size_t bufsize
= 1024;
207 char *buf
= malloc (bufsize
);
213 DIR *st
= data
->dirstreams
[data
->actdir
]->stream
;
217 while ((d
= __readdir64 (st
)) != NULL
)
219 size_t this_len
= _D_EXACT_NAMLEN (d
);
220 if (actsize
+ this_len
+ 2 >= bufsize
)
223 bufsize
+= MAX (1024, 2 * this_len
);
224 newp
= (char *) realloc (buf
, bufsize
);
227 /* No more memory. */
228 int save_err
= errno
;
230 __set_errno (save_err
);
237 *((char *) __mempcpy (buf
+ actsize
, d
->d_name
, this_len
))
239 actsize
+= this_len
+ 1;
242 /* Terminate the list with an additional NUL byte. */
243 buf
[actsize
++] = '\0';
245 /* Shrink the buffer to what we actually need. */
246 data
->dirstreams
[data
->actdir
]->content
= realloc (buf
, actsize
);
247 if (data
->dirstreams
[data
->actdir
]->content
== NULL
)
249 int save_err
= errno
;
251 __set_errno (save_err
);
257 data
->dirstreams
[data
->actdir
]->stream
= NULL
;
258 data
->dirstreams
[data
->actdir
] = NULL
;
263 /* Open the new stream. */
266 const char *name
= ((data
->flags
& FTW_CHDIR
)
267 ? data
->dirbuf
+ data
->ftw
.base
: data
->dirbuf
);
268 assert (data
->dirstreams
[data
->actdir
] == NULL
);
270 dirp
->stream
= __opendir (name
);
271 if (dirp
->stream
== NULL
)
275 dirp
->content
= NULL
;
276 data
->dirstreams
[data
->actdir
] = dirp
;
278 if (++data
->actdir
== data
->maxdir
)
288 process_entry (struct ftw_data
*data
, struct dir_data
*dir
, const char *name
,
296 if (name
[0] == '.' && (name
[1] == '\0'
297 || (name
[1] == '.' && name
[2] == '\0')))
298 /* Don't process the "." and ".." entries. */
301 new_buflen
= data
->ftw
.base
+ namlen
+ 2;
302 if (data
->dirbufsize
< new_buflen
)
304 /* Enlarge the buffer. */
307 data
->dirbufsize
= 2 * new_buflen
;
308 newp
= (char *) realloc (data
->dirbuf
, data
->dirbufsize
);
314 *((char *) __mempcpy (data
->dirbuf
+ data
->ftw
.base
, name
, namlen
)) = '\0';
316 if ((data
->flags
& FTW_CHDIR
) == 0)
319 if (((data
->flags
& FTW_PHYS
)
320 ? LXSTAT (_STAT_VER
, name
, &st
)
321 : XSTAT (_STAT_VER
, name
, &st
)) < 0)
323 if (errno
!= EACCES
&& errno
!= ENOENT
)
325 else if (!(data
->flags
& FTW_PHYS
)
326 && LXSTAT (_STAT_VER
, name
, &st
) == 0
327 && S_ISLNK (st
.st_mode
))
334 if (S_ISDIR (st
.st_mode
))
336 else if (S_ISLNK (st
.st_mode
))
344 || !(data
->flags
& FTW_MOUNT
) || st
.st_dev
== data
->dev
))
348 if ((data
->flags
& FTW_PHYS
)
349 || (!find_object (data
, &st
)
350 /* Remember the object. */
351 && (result
= add_object (data
, &st
)) == 0))
353 result
= ftw_dir (data
, &st
);
355 if (result
== 0 && (data
->flags
& FTW_CHDIR
))
357 /* Change back to current directory. */
359 if (dir
->stream
!= NULL
)
360 if (__fchdir (dirfd (dir
->stream
)) == 0)
365 if (data
->ftw
.base
== 1)
367 if (__chdir ("/") < 0)
371 if (__chdir ("..") < 0)
378 result
= (*data
->func
) (data
->dirbuf
, &st
, data
->cvt_arr
[flag
],
388 ftw_dir (struct ftw_data
*data
, struct STAT
*st
)
392 int previous_base
= data
->ftw
.base
;
396 /* Open the stream for this directory. This might require that
397 another stream has to be closed. */
398 result
= open_dir_stream (data
, &dir
);
402 /* We cannot read the directory. Signal this with a special flag. */
403 result
= (*data
->func
) (data
->dirbuf
, st
, FTW_DNR
, &data
->ftw
);
408 /* First, report the directory (if not depth-first). */
409 if (!(data
->flags
& FTW_DEPTH
))
411 result
= (*data
->func
) (data
->dirbuf
, st
, FTW_D
, &data
->ftw
);
416 /* If necessary, change to this directory. */
417 if (data
->flags
& FTW_CHDIR
)
419 if (__fchdir (dirfd (dir
.stream
)) < 0)
423 if (__chdir (data
->dirbuf
) < 0)
432 int save_err
= errno
;
433 __closedir (dir
.stream
);
434 __set_errno (save_err
);
436 if (data
->actdir
-- == 0)
437 data
->actdir
= data
->maxdir
- 1;
438 data
->dirstreams
[data
->actdir
] = NULL
;
444 /* Next, update the `struct FTW' information. */
446 startp
= strchr (data
->dirbuf
, '\0');
447 /* There always must be a directory name. */
448 assert (startp
!= data
->dirbuf
);
449 if (startp
[-1] != '/')
451 data
->ftw
.base
= startp
- data
->dirbuf
;
453 while (dir
.stream
!= NULL
&& (d
= __readdir64 (dir
.stream
)) != NULL
)
455 result
= process_entry (data
, &dir
, d
->d_name
, _D_EXACT_NAMLEN (d
));
460 if (dir
.stream
!= NULL
)
462 /* The stream is still open. I.e., we did not need more
463 descriptors. Simply close the stream now. */
464 int save_err
= errno
;
466 assert (dir
.content
== NULL
);
468 __closedir (dir
.stream
);
469 __set_errno (save_err
);
471 if (data
->actdir
-- == 0)
472 data
->actdir
= data
->maxdir
- 1;
473 data
->dirstreams
[data
->actdir
] = NULL
;
478 char *runp
= dir
.content
;
480 while (result
== 0 && *runp
!= '\0')
482 char *endp
= strchr (runp
, '\0');
484 result
= process_entry (data
, &dir
, runp
, endp
- runp
);
491 __set_errno (save_err
);
494 /* Prepare the return, revert the `struct FTW' information. */
495 data
->dirbuf
[data
->ftw
.base
- 1] = '\0';
497 data
->ftw
.base
= previous_base
;
499 /* Finally, if we process depth-first report the directory. */
500 if (result
== 0 && (data
->flags
& FTW_DEPTH
))
501 result
= (*data
->func
) (data
->dirbuf
, st
, FTW_DP
, &data
->ftw
);
509 ftw_startup (const char *dir
, int is_nftw
, void *func
, int descriptors
,
512 struct ftw_data data
;
519 /* First make sure the parameters are reasonable. */
522 __set_errno (ENOENT
);
526 data
.maxdir
= descriptors
< 1 ? 1 : descriptors
;
528 data
.dirstreams
= (struct dir_data
**) alloca (data
.maxdir
529 * sizeof (struct dir_data
*));
530 memset (data
.dirstreams
, '\0', data
.maxdir
* sizeof (struct dir_data
*));
532 /* PATH_MAX is always defined when we get here. */
533 data
.dirbufsize
= MAX (2 * strlen (dir
), PATH_MAX
);
534 data
.dirbuf
= (char *) malloc (data
.dirbufsize
);
535 if (data
.dirbuf
== NULL
)
537 cp
= __stpcpy (data
.dirbuf
, dir
);
538 /* Strip trailing slashes. */
539 while (cp
> data
.dirbuf
+ 1 && cp
[-1] == '/')
546 while (cp
> data
.dirbuf
&& cp
[-1] != '/')
548 data
.ftw
.base
= cp
- data
.dirbuf
;
552 /* This assignment might seem to be strange but it is what we want.
553 The trick is that the first three arguments to the `ftw' and
554 `nftw' callback functions are equal. Therefore we can call in
555 every case the callback using the format of the `nftw' version
556 and get the correct result since the stack layout for a function
557 call in C allows this. */
558 data
.func
= (NFTW_FUNC_T
) func
;
560 /* Since we internally use the complete set of FTW_* values we need
561 to reduce the value range before calling a `ftw' callback. */
562 data
.cvt_arr
= is_nftw
? nftw_arr
: ftw_arr
;
564 /* No object known so far. */
565 data
.known_objects
= NULL
;
567 /* Now go to the directory containing the initial file/directory. */
568 if ((flags
& FTW_CHDIR
) && data
.ftw
.base
> 0)
570 /* GNU extension ahead. */
571 cwd
= __getcwd (NULL
, 0);
576 /* Change to the directory the file is in. In data.dirbuf
577 we have a writable copy of the file name. Just NUL
578 terminate it for now and change the directory. */
579 if (data
.ftw
.base
== 1)
580 /* I.e., the file is in the root directory. */
581 result
= __chdir ("/");
584 char ch
= data
.dirbuf
[data
.ftw
.base
- 1];
585 data
.dirbuf
[data
.ftw
.base
- 1] = '\0';
586 result
= __chdir (data
.dirbuf
);
587 data
.dirbuf
[data
.ftw
.base
- 1] = ch
;
592 /* Get stat info for start directory. */
595 const char *name
= ((data
.flags
& FTW_CHDIR
)
596 ? data
.dirbuf
+ data
.ftw
.base
599 if (((flags
& FTW_PHYS
)
600 ? LXSTAT (_STAT_VER
, name
, &st
)
601 : XSTAT (_STAT_VER
, name
, &st
)) < 0)
603 if (!(flags
& FTW_PHYS
)
605 && LXSTAT (_STAT_VER
, name
, &st
) == 0
606 && S_ISLNK (st
.st_mode
))
607 result
= (*data
.func
) (data
.dirbuf
, &st
, data
.cvt_arr
[FTW_SLN
],
610 /* No need to call the callback since we cannot say anything
616 if (S_ISDIR (st
.st_mode
))
618 /* Remember the device of the initial directory in case
619 FTW_MOUNT is given. */
620 data
.dev
= st
.st_dev
;
622 /* We know this directory now. */
623 if (!(flags
& FTW_PHYS
))
624 result
= add_object (&data
, &st
);
627 result
= ftw_dir (&data
, &st
);
631 int flag
= S_ISLNK (st
.st_mode
) ? FTW_SL
: FTW_F
;
633 result
= (*data
.func
) (data
.dirbuf
, &st
, data
.cvt_arr
[flag
],
639 /* Return to the start directory (if necessary). */
642 int save_err
= errno
;
645 __set_errno (save_err
);
648 /* Free all memory. */
650 __tdestroy (data
.known_objects
, free
);
652 __set_errno (save_err
);
662 FTW_NAME (path
, func
, descriptors
)
667 return ftw_startup (path
, 0, func
, descriptors
, 0);
671 NFTW_NAME (path
, func
, descriptors
, flags
)
677 return ftw_startup (path
, 1, func
, descriptors
, flags
);