Update.
[glibc.git] / io / ftw.c
blob3635cba6819b916ce5b6ce5692b2085588f28ccf
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. */
21 #include <dirent.h>
22 #include <errno.h>
23 #include <ftw.h>
24 #include <search.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <sys/param.h>
29 #include <include/sys/stat.h>
31 /* #define NDEBUG 1 */
32 #include <assert.h>
34 /* Support for the LFS API version. */
35 #ifndef FTW_NAME
36 # define FTW_NAME ftw
37 # define NFTW_NAME nftw
38 # define INO_T ino_t
39 # define STAT stat
40 # define LXSTAT __lxstat
41 # define XSTAT __xstat
42 # define FTW_FUNC_T __ftw_func_t
43 # define NFTW_FUNC_T __nftw_func_t
44 #endif
46 struct dir_data
48 DIR *stream;
49 char *content;
52 struct known_object
54 dev_t dev;
55 INO_T ino;
58 struct ftw_data
60 /* Array with pointers to open directory streams. */
61 struct dir_data **dirstreams;
62 size_t actdir;
63 size_t maxdir;
65 /* Buffer containing name of currently processed object. */
66 char *dirbuf;
67 size_t dirbufsize;
69 /* Passed as fourth argument to `nftw' callback. The `base' member
70 tracks the content of the `dirbuf'. */
71 struct FTW ftw;
73 /* Flags passed to `nftw' function. 0 for `ftw'. */
74 int flags;
76 /* Conversion array for flag values. It is the identity mapping for
77 `nftw' calls, otherwise it maps the values to those know by
78 `ftw'. */
79 const int *cvt_arr;
81 /* Callback function. We always use the `nftw' form. */
82 NFTW_FUNC_T func;
84 /* Device of starting point. Needed for FTW_MOUNT. */
85 dev_t dev;
87 /* Data structure for keeping fingerprints of already processed
88 object. This is needed when not using FTW_PHYS. */
89 void *known_objects;
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
95 for `ftw'. */
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;
111 static int
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;
118 int cmp1;
119 cmp1 = (kp1->dev > kp2->dev) - (kp1->dev < kp2->dev);
120 if (cmp1 != 0)
121 return cmp1;
122 return (kp1->ino > kp2->ino) - (kp1->ino < kp2->ino);
126 static inline int
127 add_object (struct ftw_data *data, struct STAT *st)
129 struct known_object *newp = malloc (sizeof (struct known_object));
130 if (newp == NULL)
131 return -1;
132 newp->dev = st->st_dev;
133 newp->ino = st->st_ino;
134 return __tsearch (newp, &data->known_objects, object_compare) ? 0 : -1;
138 static inline int
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;
146 static inline int
147 open_dir_stream (struct ftw_data *data, struct dir_data *dirp)
149 int result = 0;
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);
159 if (buf == NULL)
160 result = -1;
161 else
163 DIR *st = data->dirstreams[data->actdir]->stream;
164 struct dirent64 *d;
165 size_t actsize = 0;
167 while ((d = __readdir64 (st)) != NULL)
169 size_t this_len = _D_EXACT_NAMLEN (d);
170 if (actsize + this_len + 2 >= bufsize)
172 char *newp;
173 bufsize += MAX (1024, 2 * this_len);
174 newp = realloc (buf, bufsize);
175 if (newp == NULL)
177 /* No more memory. */
178 int save_err = errno;
179 free (buf);
180 __set_errno (save_err);
181 result = -1;
182 break;
184 buf = newp;
187 *((char *) __mempcpy (buf + actsize, d->d_name, this_len))
188 = '\0';
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;
200 free (buf);
201 __set_errno (save_err);
202 result = -1;
204 else
206 __closedir (st);
207 data->dirstreams[data->actdir]->stream = NULL;
208 data->dirstreams[data->actdir] = NULL;
213 /* Open the new stream. */
214 if (result == 0)
216 assert (data->dirstreams[data->actdir] == NULL);
218 dirp->stream = __opendir (data->dirbuf);
219 if (dirp->stream == NULL)
220 result = -1;
221 else
223 dirp->content = NULL;
224 data->dirstreams[data->actdir] = dirp;
226 if (++data->actdir == data->maxdir)
227 data->actdir = 0;
231 return result;
235 static inline int
236 process_entry (struct ftw_data *data, struct dir_data *dir, const char *name,
237 size_t namlen)
239 struct STAT st;
240 int result = 0;
241 int flag = 0;
243 if (name[0] == '.' && (name[1] == '\0'
244 || (name[1] == '.' && name[2] == '\0')))
245 /* Don't process the "." and ".." entries. */
246 return 0;
248 if (data->dirbufsize < data->ftw.base + namlen + 2)
250 /* Enlarge the buffer. */
251 char *newp;
253 data->dirbufsize *= 2;
254 newp = realloc (data->dirbuf, data->dirbufsize);
255 if (newp == NULL)
256 return -1;
257 data->dirbuf = newp;
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)
267 result = -1;
268 else if (!(data->flags & FTW_PHYS)
269 && LXSTAT (_STAT_VER, data->dirbuf, &st) == 0
270 && S_ISLNK (st.st_mode))
271 flag = FTW_SLN;
272 else
273 flag = FTW_NS;
275 else
277 if (S_ISDIR (st.st_mode))
278 flag = FTW_D;
279 else if (S_ISLNK (st.st_mode))
280 flag = FTW_SL;
281 else
282 flag = FTW_F;
285 if (result == 0
286 && (flag == FTW_NS
287 || !(data->flags & FTW_MOUNT) || st.st_dev == data->dev))
289 if (flag == FTW_D)
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. */
301 int done = 0;
302 if (dir->stream != NULL)
303 if (__fchdir (dirfd (dir->stream)) == 0)
304 done = 1;
306 if (!done)
308 if (data->ftw.base == 1)
310 if (__chdir ("/") < 0)
311 result = -1;
313 else
315 /* Please note that we overwrite a slash. */
316 data->dirbuf[data->ftw.base - 1] = '\0';
318 if (__chdir (data->dirbuf) < 0)
319 result = -1;
321 data->dirbuf[data->ftw.base - 1] = '/';
327 else
328 result = (*data->func) (data->dirbuf, &st, data->cvt_arr[flag],
329 &data->ftw);
332 return result;
336 static int
337 internal_function
338 ftw_dir (struct ftw_data *data, struct STAT *st)
340 struct dir_data dir;
341 struct dirent64 *d;
342 int previous_base = data->ftw.base;
343 int result;
344 char *startp;
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);
349 if (result != 0)
351 if (errno == EACCES)
352 /* We cannot read the directory. Signal this with a special flag. */
353 result = (*data->func) (data->dirbuf, st, FTW_DNR, &data->ftw);
355 return result;
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);
362 if (result != 0)
363 return result;
366 /* If necessary, change to this directory. */
367 if (data->flags & FTW_CHDIR)
369 if (__fchdir (dirfd (dir.stream)) < 0)
371 if (errno == ENOSYS)
373 if (__chdir (data->dirbuf) < 0)
374 result = -1;
376 else
377 result = -1;
380 if (result != 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;
390 return result;
394 /* Next, update the `struct FTW' information. */
395 ++data->ftw.level;
396 startp = strchr (data->dirbuf, '\0');
397 /* There always must be a directory name. */
398 assert (startp != data->dirbuf);
399 if (startp[-1] != '/')
400 *startp++ = '/';
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));
406 if (result != 0)
407 break;
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;
425 else
427 int save_err;
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);
436 runp = endp + 1;
439 save_err = errno;
440 free (dir.content);
441 __set_errno (save_err);
444 /* Prepare the return, revert the `struct FTW' information. */
445 data->dirbuf[data->ftw.base - 1] = '\0';
446 --data->ftw.level;
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);
453 return result;
457 static int
458 internal_function
459 ftw_startup (const char *dir, int is_nftw, void *func, int descriptors,
460 int flags)
462 struct ftw_data data;
463 struct STAT st;
464 int result = 0;
465 int save_err;
466 char *cwd = NULL;
467 char *cp;
469 /* First make sure the parameters are reasonable. */
470 if (dir[0] == '\0')
472 __set_errno (ENOENT);
473 return -1;
476 data.maxdir = descriptors < 1 ? 1 : descriptors;
477 data.actdir = 0;
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 *));
482 #ifdef PATH_MAX
483 data.dirbufsize = MAX (2 * strlen (dir), PATH_MAX);
484 #else
485 data.dirbufsize = 2 * strlen (dir);
486 #endif
487 data.dirbuf = (char *) malloc (data.dirbufsize);
488 if (data.dirbuf == NULL)
489 return -1;
490 cp = __stpcpy (data.dirbuf, dir);
491 /* Strip trailing slashes. */
492 while (cp > data.dirbuf + 1 && cp[-1] == '/')
493 --cp;
494 *cp = '\0';
496 data.ftw.level = 0;
498 /* Find basename. */
499 while (cp > data.dirbuf && cp[-1] != '/')
500 --cp;
501 data.ftw.base = cp - data.dirbuf;
503 data.flags = flags;
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);
525 if (cwd == NULL)
526 result = -1;
527 else
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 ("/");
535 else
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. */
546 if (result == 0)
548 if (((flags & FTW_PHYS)
549 ? LXSTAT (_STAT_VER, data.dirbuf, &st)
550 : XSTAT (_STAT_VER, data.dirbuf, &st)) < 0)
552 if (errno == EACCES)
553 result = (*data.func) (data.dirbuf, &st, FTW_NS, &data.ftw);
554 else if (!(flags & FTW_PHYS)
555 && errno == ENOENT
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],
559 &data.ftw);
560 else
561 /* No need to call the callback since we cannot say anything
562 about the object. */
563 result = -1;
565 else
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);
577 if (result == 0)
578 result = ftw_dir (&data, &st);
580 else
582 int flag = S_ISLNK (st.st_mode) ? FTW_SL : FTW_F;
584 result = (*data.func) (data.dirbuf, &st, data.cvt_arr[flag],
585 &data.ftw);
590 /* Return to the start directory (if necessary). */
591 if (cwd != NULL)
593 int save_err = errno;
594 __chdir (cwd);
595 free (cwd);
596 __set_errno (save_err);
599 /* Free all memory. */
600 save_err = errno;
601 __tdestroy (data.known_objects, free);
602 free (data.dirbuf);
603 __set_errno (save_err);
605 return result;
610 /* Entry points. */
613 FTW_NAME (path, func, descriptors)
614 const char *path;
615 FTW_FUNC_T func;
616 int descriptors;
618 return ftw_startup (path, 0, func, descriptors, 0);
622 NFTW_NAME (path, func, descriptors, flags)
623 const char *path;
624 NFTW_FUNC_T func;
625 int descriptors;
626 int flags;
628 return ftw_startup (path, 1, func, descriptors, flags);