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
32 #if HAVE_SYS_PARAM_H || defined _LIBC
33 # include <sys/param.h>
36 # include <include/sys/stat.h>
38 # include <sys/stat.h>
41 /* #define NDEBUG 1 */
46 # define __chdir chdir
48 # define __closedir closedir
50 # define __fchdir fchdir
52 # define __getcwd getcwd
54 # define __opendir opendir
56 # define __readdir64 readdir
58 # define __tdestroy tdestroy
60 # define __tfind tfind
62 # define __tsearch tsearch
63 # undef internal_function
64 # define internal_function /* empty */
66 # define dirent64 dirent
68 # define MAX(a, b) ((a) > (b) ? (a) : (b))
72 # define __set_errno(Val) errno = (Val)
75 /* Support for the LFS API version. */
78 # define NFTW_NAME nftw
81 # define LXSTAT __lxstat
82 # define XSTAT __xstat
83 # define FTW_FUNC_T __ftw_func_t
84 # define NFTW_FUNC_T __nftw_func_t
101 /* Array with pointers to open directory streams. */
102 struct dir_data **dirstreams;
106 /* Buffer containing name of currently processed object. */
110 /* Passed as fourth argument to `nftw' callback. The `base' member
111 tracks the content of the `dirbuf'. */
114 /* Flags passed to `nftw' function. 0 for `ftw'. */
117 /* Conversion array for flag values. It is the identity mapping for
118 `nftw' calls, otherwise it maps the values to those know by
122 /* Callback function. We always use the `nftw' form. */
125 /* Device of starting point. Needed for FTW_MOUNT. */
128 /* Data structure for keeping fingerprints of already processed
129 object. This is needed when not using FTW_PHYS. */
134 /* Internally we use the FTW_* constants used for `nftw'. When the
135 process called `ftw' we must reduce the flag to the known flags
137 static const int nftw_arr[] =
139 FTW_F, FTW_D, FTW_DNR, FTW_NS, FTW_SL, FTW_DP, FTW_SLN
142 static const int ftw_arr[] =
144 FTW_F, FTW_D, FTW_DNR, FTW_NS, FTW_F, FTW_D, FTW_NS
148 /* Forward declarations of local functions. */
149 static int ftw_dir (struct ftw_data *data, struct STAT *st) internal_function;
153 object_compare (const void *p1, const void *p2)
155 /* We don't need a sophisticated and useful comparison. We are only
156 interested in equality. However, we must be careful not to
157 accidentally compare `holes' in the structure. */
158 const struct known_object *kp1 = p1, *kp2 = p2;
160 cmp1 = (kp1->dev > kp2->dev) - (kp1->dev < kp2->dev);
163 return (kp1->ino > kp2->ino) - (kp1->ino < kp2->ino);
168 add_object (struct ftw_data *data, struct STAT *st)
170 struct known_object *newp = malloc (sizeof (struct known_object));
173 newp->dev = st->st_dev;
174 newp->ino = st->st_ino;
175 return __tsearch (newp, &data->known_objects, object_compare) ? 0 : -1;
180 find_object (struct ftw_data *data, struct STAT *st)
182 struct known_object obj = { .dev = st->st_dev, .ino = st->st_ino };
183 return __tfind (&obj, &data->known_objects, object_compare) != NULL;
188 open_dir_stream (struct ftw_data *data, struct dir_data *dirp)
192 if (data->dirstreams[data->actdir] != NULL)
194 /* Oh, oh. We must close this stream. Get all remaining
195 entries and store them as a list in the `content' member of
196 the `struct dir_data' variable. */
197 size_t bufsize = 1024;
198 char *buf = malloc (bufsize);
204 DIR *st = data->dirstreams[data->actdir]->stream;
208 while ((d = __readdir64 (st)) != NULL)
210 size_t this_len = _D_EXACT_NAMLEN (d);
211 if (actsize + this_len + 2 >= bufsize)
214 bufsize += MAX (1024, 2 * this_len);
215 newp = (char *) realloc (buf, bufsize);
218 /* No more memory. */
219 int save_err = errno;
221 __set_errno (save_err);
228 *((char *) __mempcpy (buf + actsize, d->d_name, this_len))
230 actsize += this_len + 1;
233 /* Terminate the list with an additional NUL byte. */
234 buf[actsize++] = '\0';
236 /* Shrink the buffer to what we actually need. */
237 data->dirstreams[data->actdir]->content = realloc (buf, actsize);
238 if (data->dirstreams[data->actdir]->content == NULL)
240 int save_err = errno;
242 __set_errno (save_err);
248 data->dirstreams[data->actdir]->stream = NULL;
249 data->dirstreams[data->actdir] = NULL;
254 /* Open the new stream. */
257 const char *name = ((data->flags & FTW_CHDIR)
258 ? data->dirbuf + data->ftw.base: data->dirbuf);
259 assert (data->dirstreams[data->actdir] == NULL);
261 dirp->stream = __opendir (name);
262 if (dirp->stream == NULL)
266 dirp->content = NULL;
267 data->dirstreams[data->actdir] = dirp;
269 if (++data->actdir == data->maxdir)
279 process_entry (struct ftw_data *data, struct dir_data *dir, const char *name,
286 if (name[0] == '.' && (name[1] == '\0'
287 || (name[1] == '.' && name[2] == '\0')))
288 /* Don't process the "." and ".." entries. */
291 if (data->dirbufsize < data->ftw.base + namlen + 2)
293 /* Enlarge the buffer. */
296 data->dirbufsize *= 2;
297 newp = (char *) realloc (data->dirbuf, data->dirbufsize);
303 *((char *) __mempcpy (data->dirbuf + data->ftw.base, name, namlen)) = '\0';
305 if ((data->flags & FTW_CHDIR) == 0)
308 if (((data->flags & FTW_PHYS)
309 ? LXSTAT (_STAT_VER, name, &st)
310 : XSTAT (_STAT_VER, name, &st)) < 0)
312 if (errno != EACCES && errno != ENOENT)
314 else if (!(data->flags & FTW_PHYS)
315 && LXSTAT (_STAT_VER, name, &st) == 0
316 && S_ISLNK (st.st_mode))
323 if (S_ISDIR (st.st_mode))
325 else if (S_ISLNK (st.st_mode))
333 || !(data->flags & FTW_MOUNT) || st.st_dev == data->dev))
337 if ((data->flags & FTW_PHYS)
338 || (!find_object (data, &st)
339 /* Remember the object. */
340 && (result = add_object (data, &st)) == 0))
342 result = ftw_dir (data, &st);
344 if (result == 0 && (data->flags & FTW_CHDIR))
346 /* Change back to current directory. */
348 if (dir->stream != NULL)
349 if (__fchdir (dirfd (dir->stream)) == 0)
354 if (data->ftw.base == 1)
356 if (__chdir ("/") < 0)
360 if (__chdir ("..") < 0)
367 result = (*data->func) (data->dirbuf, &st, data->cvt_arr[flag],
377 ftw_dir (struct ftw_data *data, struct STAT *st)
381 int previous_base = data->ftw.base;
385 /* Open the stream for this directory. This might require that
386 another stream has to be closed. */
387 result = open_dir_stream (data, &dir);
391 /* We cannot read the directory. Signal this with a special flag. */
392 result = (*data->func) (data->dirbuf, st, FTW_DNR, &data->ftw);
397 /* First, report the directory (if not depth-first). */
398 if (!(data->flags & FTW_DEPTH))
400 result = (*data->func) (data->dirbuf, st, FTW_D, &data->ftw);
405 /* If necessary, change to this directory. */
406 if (data->flags & FTW_CHDIR)
408 if (__fchdir (dirfd (dir.stream)) < 0)
412 if (__chdir (data->dirbuf) < 0)
421 int save_err = errno;
422 __closedir (dir.stream);
423 __set_errno (save_err);
425 if (data->actdir-- == 0)
426 data->actdir = data->maxdir - 1;
427 data->dirstreams[data->actdir] = NULL;
433 /* Next, update the `struct FTW' information. */
435 startp = strchr (data->dirbuf, '\0');
436 /* There always must be a directory name. */
437 assert (startp != data->dirbuf);
438 if (startp[-1] != '/')
440 data->ftw.base = startp - data->dirbuf;
442 while (dir.stream != NULL && (d = __readdir64 (dir.stream)) != NULL)
444 result = process_entry (data, &dir, d->d_name, _D_EXACT_NAMLEN (d));
449 if (dir.stream != NULL)
451 /* The stream is still open. I.e., we did not need more
452 descriptors. Simply close the stream now. */
453 int save_err = errno;
455 assert (dir.content == NULL);
457 __closedir (dir.stream);
458 __set_errno (save_err);
460 if (data->actdir-- == 0)
461 data->actdir = data->maxdir - 1;
462 data->dirstreams[data->actdir] = NULL;
467 char *runp = dir.content;
469 while (result == 0 && *runp != '\0')
471 char *endp = strchr (runp, '\0');
473 result = process_entry (data, &dir, runp, endp - runp);
480 __set_errno (save_err);
483 /* Prepare the return, revert the `struct FTW' information. */
484 data->dirbuf[data->ftw.base - 1] = '\0';
486 data->ftw.base = previous_base;
488 /* Finally, if we process depth-first report the directory. */
489 if (result == 0 && (data->flags & FTW_DEPTH))
490 result = (*data->func) (data->dirbuf, st, FTW_DP, &data->ftw);
498 ftw_startup (const char *dir, int is_nftw, void *func, int descriptors,
501 struct ftw_data data;
508 /* First make sure the parameters are reasonable. */
511 __set_errno (ENOENT);
515 data.maxdir = descriptors < 1 ? 1 : descriptors;
517 data.dirstreams = (struct dir_data **) alloca (data.maxdir
518 * sizeof (struct dir_data *));
519 memset (data.dirstreams, '\0', data.maxdir * sizeof (struct dir_data *));
522 data.dirbufsize = MAX (2 * strlen (dir), PATH_MAX);
524 data.dirbufsize = 2 * strlen (dir);
526 data.dirbuf = (char *) malloc (data.dirbufsize);
527 if (data.dirbuf == NULL)
529 cp = __stpcpy (data.dirbuf, dir);
530 /* Strip trailing slashes. */
531 while (cp > data.dirbuf + 1 && cp[-1] == '/')
538 while (cp > data.dirbuf && cp[-1] != '/')
540 data.ftw.base = cp - data.dirbuf;
544 /* This assignment might seem to be strange but it is what we want.
545 The trick is that the first three arguments to the `ftw' and
546 `nftw' callback functions are equal. Therefore we can call in
547 every case the callback using the format of the `nftw' version
548 and get the correct result since the stack layout for a function
549 call in C allows this. */
550 data.func = (NFTW_FUNC_T) func;
552 /* Since we internally use the complete set of FTW_* values we need
553 to reduce the value range before calling a `ftw' callback. */
554 data.cvt_arr = is_nftw ? nftw_arr : ftw_arr;
556 /* No object known so far. */
557 data.known_objects = NULL;
559 /* Now go to the directory containing the initial file/directory. */
560 if ((flags & FTW_CHDIR) && data.ftw.base > 0)
562 /* GNU extension ahead. */
563 cwd = __getcwd (NULL, 0);
568 /* Change to the directory the file is in. In data.dirbuf
569 we have a writable copy of the file name. Just NUL
570 terminate it for now and change the directory. */
571 if (data.ftw.base == 1)
572 /* I.e., the file is in the root directory. */
573 result = __chdir ("/");
576 char ch = data.dirbuf[data.ftw.base - 1];
577 data.dirbuf[data.ftw.base - 1] = '\0';
578 result = __chdir (data.dirbuf);
579 data.dirbuf[data.ftw.base - 1] = ch;
584 /* Get stat info for start directory. */
587 const char *name = ((data.flags & FTW_CHDIR)
588 ? data.dirbuf + data.ftw.base
591 if (((flags & FTW_PHYS)
592 ? LXSTAT (_STAT_VER, name, &st)
593 : XSTAT (_STAT_VER, name, &st)) < 0)
595 if (!(flags & FTW_PHYS)
597 && LXSTAT (_STAT_VER, name, &st) == 0
598 && S_ISLNK (st.st_mode))
599 result = (*data.func) (data.dirbuf, &st, data.cvt_arr[FTW_SLN],
602 /* No need to call the callback since we cannot say anything
608 if (S_ISDIR (st.st_mode))
610 /* Remember the device of the initial directory in case
611 FTW_MOUNT is given. */
612 data.dev = st.st_dev;
614 /* We know this directory now. */
615 if (!(flags & FTW_PHYS))
616 result = add_object (&data, &st);
619 result = ftw_dir (&data, &st);
623 int flag = S_ISLNK (st.st_mode) ? FTW_SL : FTW_F;
625 result = (*data.func) (data.dirbuf, &st, data.cvt_arr[flag],
631 /* Return to the start directory (if necessary). */
634 int save_err = errno;
637 __set_errno (save_err);
640 /* Free all memory. */
642 __tdestroy (data.known_objects, free);
644 __set_errno (save_err);
654 FTW_NAME (path, func, descriptors)
659 return ftw_startup (path, 0, func, descriptors, 0);
663 NFTW_NAME (path, func, descriptors, flags)
669 return ftw_startup (path, 1, func, descriptors, flags);