1 /* File tree walker functions.
2 Copyright (C) 1996,1997,1998,1999,2000,2001 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 Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 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 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
28 #include <sys/param.h>
29 #include <include/sys/stat.h>
31 /* #define NDEBUG 1 */
34 /* Support for the LFS API version. */
37 # define NFTW_NAME nftw
40 # define LXSTAT __lxstat
41 # define XSTAT __xstat
42 # define FTW_FUNC_T __ftw_func_t
43 # define NFTW_FUNC_T __nftw_func_t
60 /* Array with pointers to open directory streams. */
61 struct dir_data **dirstreams;
65 /* Buffer containing name of currently processed object. */
69 /* Passed as fourth argument to `nftw' callback. The `base' member
70 tracks the content of the `dirbuf'. */
73 /* Flags passed to `nftw' function. 0 for `ftw'. */
76 /* Conversion array for flag values. It is the identity mapping for
77 `nftw' calls, otherwise it maps the values to those know by
81 /* Callback function. We always use the `nftw' form. */
84 /* Device of starting point. Needed for FTW_MOUNT. */
87 /* Data structure for keeping fingerprints of already processed
88 object. This is needed when not using FTW_PHYS. */
93 /* Internally we use the FTW_* constants used for `nftw'. When the
94 process called `ftw' we must reduce the flag to the known flags
96 static const int nftw_arr[] =
98 FTW_F, FTW_D, FTW_DNR, FTW_NS, FTW_SL, FTW_DP, FTW_SLN
101 static const int ftw_arr[] =
103 FTW_F, FTW_D, FTW_DNR, FTW_NS, FTW_F, FTW_D, FTW_NS
107 /* Forward declarations of local functions. */
108 static int ftw_dir (struct ftw_data *data, struct STAT *st) internal_function;
112 object_compare (const void *p1, const void *p2)
114 /* We don't need a sophisticated and useful comparison. We are only
115 interested in equality. However, we must be careful not to
116 accidentally compare `holes' in the structure. */
117 const struct known_object *kp1 = p1, *kp2 = p2;
119 cmp1 = (kp1->dev > kp2->dev) - (kp1->dev < kp2->dev);
122 return (kp1->ino > kp2->ino) - (kp1->ino < kp2->ino);
127 add_object (struct ftw_data *data, struct STAT *st)
129 struct known_object *newp = malloc (sizeof (struct known_object));
132 newp->dev = st->st_dev;
133 newp->ino = st->st_ino;
134 return __tsearch (newp, &data->known_objects, object_compare) ? 0 : -1;
139 find_object (struct ftw_data *data, struct STAT *st)
141 struct known_object obj = { dev: st->st_dev, ino: st->st_ino };
142 return __tfind (&obj, &data->known_objects, object_compare) != NULL;
147 open_dir_stream (struct ftw_data *data, struct dir_data *dirp)
151 if (data->dirstreams[data->actdir] != NULL)
153 /* Oh, oh. We must close this stream. Get all remaining
154 entries and store them as a list in the `content' member of
155 the `struct dir_data' variable. */
156 size_t bufsize = 1024;
157 char *buf = malloc (bufsize);
163 DIR *st = data->dirstreams[data->actdir]->stream;
167 while ((d = __readdir64 (st)) != NULL)
169 size_t this_len = _D_EXACT_NAMLEN (d);
170 if (actsize + this_len + 2 >= bufsize)
173 bufsize += MAX (1024, 2 * this_len);
174 newp = realloc (buf, bufsize);
177 /* No more memory. */
178 int save_err = errno;
180 __set_errno (save_err);
187 *((char *) __mempcpy (buf + actsize, d->d_name, this_len))
189 actsize += this_len + 1;
192 /* Terminate the list with an additional NUL byte. */
193 buf[actsize++] = '\0';
195 /* Shrink the buffer to what we actually need. */
196 data->dirstreams[data->actdir]->content = realloc (buf, actsize);
197 if (data->dirstreams[data->actdir]->content == NULL)
199 int save_err = errno;
201 __set_errno (save_err);
207 data->dirstreams[data->actdir]->stream = NULL;
208 data->dirstreams[data->actdir] = NULL;
213 /* Open the new stream. */
216 assert (data->dirstreams[data->actdir] == NULL);
218 dirp->stream = __opendir (data->dirbuf);
219 if (dirp->stream == NULL)
223 dirp->content = NULL;
224 data->dirstreams[data->actdir] = dirp;
226 if (++data->actdir == data->maxdir)
236 process_entry (struct ftw_data *data, struct dir_data *dir, const char *name,
243 if (name[0] == '.' && (name[1] == '\0'
244 || (name[1] == '.' && name[2] == '\0')))
245 /* Don't process the "." and ".." entries. */
248 if (data->dirbufsize < data->ftw.base + namlen + 2)
250 /* Enlarge the buffer. */
253 data->dirbufsize *= 2;
254 newp = realloc (data->dirbuf, data->dirbufsize);
260 *((char *) __mempcpy (data->dirbuf + data->ftw.base, name, namlen)) = '\0';
262 if (((data->flags & FTW_PHYS)
263 ? LXSTAT (_STAT_VER, data->dirbuf, &st)
264 : XSTAT (_STAT_VER, data->dirbuf, &st)) < 0)
266 if (errno != EACCES && errno != ENOENT)
268 else if (!(data->flags & FTW_PHYS)
269 && LXSTAT (_STAT_VER, data->dirbuf, &st) == 0
270 && S_ISLNK (st.st_mode))
277 if (S_ISDIR (st.st_mode))
279 else if (S_ISLNK (st.st_mode))
287 || !(data->flags & FTW_MOUNT) || st.st_dev == data->dev))
291 if ((data->flags & FTW_PHYS)
292 || (!find_object (data, &st)
293 /* Remember the object. */
294 && (result = add_object (data, &st)) == 0))
296 result = ftw_dir (data, &st);
298 if (result == 0 && (data->flags & FTW_CHDIR))
300 /* Change back to current directory. */
302 if (dir->stream != NULL)
303 if (__fchdir (dirfd (dir->stream)) == 0)
308 if (data->ftw.base == 1)
310 if (__chdir ("/") < 0)
315 /* Please note that we overwrite a slash. */
316 data->dirbuf[data->ftw.base - 1] = '\0';
318 if (__chdir (data->dirbuf) < 0)
321 data->dirbuf[data->ftw.base - 1] = '/';
328 result = (*data->func) (data->dirbuf, &st, data->cvt_arr[flag],
338 ftw_dir (struct ftw_data *data, struct STAT *st)
342 int previous_base = data->ftw.base;
346 /* Open the stream for this directory. This might require that
347 another stream has to be closed. */
348 result = open_dir_stream (data, &dir);
352 /* We cannot read the directory. Signal this with a special flag. */
353 result = (*data->func) (data->dirbuf, st, FTW_DNR, &data->ftw);
358 /* First, report the directory (if not depth-first). */
359 if (!(data->flags & FTW_DEPTH))
361 result = (*data->func) (data->dirbuf, st, FTW_D, &data->ftw);
366 /* If necessary, change to this directory. */
367 if (data->flags & FTW_CHDIR)
369 if (__fchdir (dirfd (dir.stream)) < 0)
373 if (__chdir (data->dirbuf) < 0)
382 int save_err = errno;
383 __closedir (dir.stream);
384 __set_errno (save_err);
386 if (data->actdir-- == 0)
387 data->actdir = data->maxdir - 1;
388 data->dirstreams[data->actdir] = NULL;
394 /* Next, update the `struct FTW' information. */
396 startp = strchr (data->dirbuf, '\0');
397 /* There always must be a directory name. */
398 assert (startp != data->dirbuf);
399 if (startp[-1] != '/')
401 data->ftw.base = startp - data->dirbuf;
403 while (dir.stream != NULL && (d = __readdir64 (dir.stream)) != NULL)
405 result = process_entry (data, &dir, d->d_name, _D_EXACT_NAMLEN (d));
410 if (dir.stream != NULL)
412 /* The stream is still open. I.e., we did not need more
413 descriptors. Simply close the stream now. */
414 int save_err = errno;
416 assert (dir.content == NULL);
418 __closedir (dir.stream);
419 __set_errno (save_err);
421 if (data->actdir-- == 0)
422 data->actdir = data->maxdir - 1;
423 data->dirstreams[data->actdir] = NULL;
428 char *runp = dir.content;
430 while (result == 0 && *runp != '\0')
432 char *endp = strchr (runp, '\0');
434 result = process_entry (data, &dir, runp, endp - runp);
441 __set_errno (save_err);
444 /* Prepare the return, revert the `struct FTW' information. */
445 data->dirbuf[data->ftw.base - 1] = '\0';
447 data->ftw.base = previous_base;
449 /* Finally, if we process depth-first report the directory. */
450 if (result == 0 && (data->flags & FTW_DEPTH))
451 result = (*data->func) (data->dirbuf, st, FTW_DP, &data->ftw);
459 ftw_startup (const char *dir, int is_nftw, void *func, int descriptors,
462 struct ftw_data data;
469 /* First make sure the parameters are reasonable. */
472 __set_errno (ENOENT);
476 data.maxdir = descriptors < 1 ? 1 : descriptors;
478 data.dirstreams = (struct dir_data **) alloca (data.maxdir
479 * sizeof (struct dir_data *));
480 memset (data.dirstreams, '\0', data.maxdir * sizeof (struct dir_data *));
483 data.dirbufsize = MAX (2 * strlen (dir), PATH_MAX);
485 data.dirbufsize = 2 * strlen (dir);
487 data.dirbuf = (char *) malloc (data.dirbufsize);
488 if (data.dirbuf == NULL)
490 cp = __stpcpy (data.dirbuf, dir);
491 /* Strip trailing slashes. */
492 while (cp > data.dirbuf + 1 && cp[-1] == '/')
499 while (cp > data.dirbuf && cp[-1] != '/')
501 data.ftw.base = cp - data.dirbuf;
505 /* This assignment might seem to be strange but it is what we want.
506 The trick is that the first three arguments to the `ftw' and
507 `nftw' callback functions are equal. Therefore we can call in
508 every case the callback using the format of the `nftw' version
509 and get the correct result since the stack layout for a function
510 call in C allows this. */
511 data.func = (NFTW_FUNC_T) func;
513 /* Since we internally use the complete set of FTW_* values we need
514 to reduce the value range before calling a `ftw' callback. */
515 data.cvt_arr = is_nftw ? nftw_arr : ftw_arr;
517 /* No object known so far. */
518 data.known_objects = NULL;
520 /* Now go to the directory containing the initial file/directory. */
521 if ((flags & FTW_CHDIR) && data.ftw.base > 0)
523 /* GNU extension ahead. */
524 cwd = __getcwd (NULL, 0);
529 /* Change to the directory the file is in. In data.dirbuf
530 we have a writable copy of the file name. Just NUL
531 terminate it for now and change the directory. */
532 if (data.ftw.base == 1)
533 /* I.e., the file is in the root directory. */
534 result = __chdir ("/");
537 char ch = data.dirbuf[data.ftw.base - 1];
538 data.dirbuf[data.ftw.base - 1] = '\0';
539 result = __chdir (data.dirbuf);
540 data.dirbuf[data.ftw.base - 1] = ch;
545 /* Get stat info for start directory. */
548 if (((flags & FTW_PHYS)
549 ? LXSTAT (_STAT_VER, data.dirbuf, &st)
550 : XSTAT (_STAT_VER, data.dirbuf, &st)) < 0)
553 result = (*data.func) (data.dirbuf, &st, FTW_NS, &data.ftw);
554 else if (!(flags & FTW_PHYS)
556 && LXSTAT (_STAT_VER, dir, &st) == 0
557 && S_ISLNK (st.st_mode))
558 result = (*data.func) (data.dirbuf, &st, data.cvt_arr[FTW_SLN],
561 /* No need to call the callback since we cannot say anything
567 if (S_ISDIR (st.st_mode))
569 /* Remember the device of the initial directory in case
570 FTW_MOUNT is given. */
571 data.dev = st.st_dev;
573 /* We know this directory now. */
574 if (!(flags & FTW_PHYS))
575 result = add_object (&data, &st);
578 result = ftw_dir (&data, &st);
582 int flag = S_ISLNK (st.st_mode) ? FTW_SL : FTW_F;
584 result = (*data.func) (data.dirbuf, &st, data.cvt_arr[flag],
590 /* Return to the start directory (if necessary). */
593 int save_err = errno;
596 __set_errno (save_err);
599 /* Free all memory. */
601 __tdestroy (data.known_objects, free);
603 __set_errno (save_err);
613 FTW_NAME (path, func, descriptors)
618 return ftw_startup (path, 0, func, descriptors, 0);
622 NFTW_NAME (path, func, descriptors, flags)
628 return ftw_startup (path, 1, func, descriptors, flags);