]> git.ipfire.org Git - thirdparty/glibc.git/blob - io/ftw.c
Update.
[thirdparty/glibc.git] / 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.
5
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.
10
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.
15
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
19 02111-1307 USA. */
20
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #if __GNUC__
26 # define alloca __builtin_alloca
27 #else
28 # if HAVE_ALLOCA_H
29 # include <alloca.h>
30 # else
31 # ifdef _AIX
32 # pragma alloca
33 # else
34 char *alloca ();
35 # endif
36 # endif
37 #endif
38
39 #if defined _LIBC
40 # include <dirent.h>
41 # define NAMLEN(dirent) _D_EXACT_NAMLEN (dirent)
42 #else
43 # if HAVE_DIRENT_H
44 # include <dirent.h>
45 # define NAMLEN(dirent) strlen ((dirent)->d_name)
46 # else
47 # define dirent direct
48 # define NAMLEN(dirent) (dirent)->d_namlen
49 # if HAVE_SYS_NDIR_H
50 # include <sys/ndir.h>
51 # endif
52 # if HAVE_SYS_DIR_H
53 # include <sys/dir.h>
54 # endif
55 # if HAVE_NDIR_H
56 # include <ndir.h>
57 # endif
58 # endif
59 #endif
60
61 #include <errno.h>
62 #include <ftw.h>
63 #include <limits.h>
64 #include <search.h>
65 #include <stdlib.h>
66 #include <string.h>
67 #include <unistd.h>
68 #if HAVE_SYS_PARAM_H || defined _LIBC
69 # include <sys/param.h>
70 #endif
71 #ifdef _LIBC
72 # include <include/sys/stat.h>
73 #else
74 # include <sys/stat.h>
75 #endif
76
77 #if ! _LIBC && !HAVE_DECL_STPCPY && !defined stpcpy
78 char *stpcpy ();
79 #endif
80
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)))
84 #endif
85
86 /* #define NDEBUG 1 */
87 #include <assert.h>
88
89 #ifndef _LIBC
90 # undef __chdir
91 # define __chdir chdir
92 # undef __closedir
93 # define __closedir closedir
94 # undef __fchdir
95 # define __fchdir fchdir
96 # undef __getcwd
97 # define __getcwd(P, N) xgetcwd ()
98 extern char *xgetcwd (void);
99 # undef __mempcpy
100 # define __mempcpy mempcpy
101 # undef __opendir
102 # define __opendir opendir
103 # undef __readdir64
104 # define __readdir64 readdir
105 # undef __stpcpy
106 # define __stpcpy stpcpy
107 # undef __tdestroy
108 # define __tdestroy tdestroy
109 # undef __tfind
110 # define __tfind tfind
111 # undef __tsearch
112 # define __tsearch tsearch
113 # undef internal_function
114 # define internal_function /* empty */
115 # undef dirent64
116 # define dirent64 dirent
117 # undef MAX
118 # define MAX(a, b) ((a) > (b) ? (a) : (b))
119 #endif
120
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 *);
126 # undef lstat
127 # define lstat(Name, Stat_buf) rpl_lstat(Name, Stat_buf)
128 #endif
129
130 #ifndef __set_errno
131 # define __set_errno(Val) errno = (Val)
132 #endif
133
134 /* Support for the LFS API version. */
135 #ifndef FTW_NAME
136 # define FTW_NAME ftw
137 # define NFTW_NAME nftw
138 # define INO_T ino_t
139 # define STAT stat
140 # ifdef _LIBC
141 # define LXSTAT __lxstat
142 # define XSTAT __xstat
143 # else
144 # define LXSTAT(V,f,sb) lstat (f,sb)
145 # define XSTAT(V,f,sb) stat (f,sb)
146 # endif
147 # define FTW_FUNC_T __ftw_func_t
148 # define NFTW_FUNC_T __nftw_func_t
149 #endif
150
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. */
155 #ifndef PATH_MAX
156 # define PATH_MAX 1024
157 #endif
158
159 struct dir_data
160 {
161 DIR *stream;
162 char *content;
163 };
164
165 struct known_object
166 {
167 dev_t dev;
168 INO_T ino;
169 };
170
171 struct ftw_data
172 {
173 /* Array with pointers to open directory streams. */
174 struct dir_data **dirstreams;
175 size_t actdir;
176 size_t maxdir;
177
178 /* Buffer containing name of currently processed object. */
179 char *dirbuf;
180 size_t dirbufsize;
181
182 /* Passed as fourth argument to `nftw' callback. The `base' member
183 tracks the content of the `dirbuf'. */
184 struct FTW ftw;
185
186 /* Flags passed to `nftw' function. 0 for `ftw'. */
187 int flags;
188
189 /* Conversion array for flag values. It is the identity mapping for
190 `nftw' calls, otherwise it maps the values to those known by
191 `ftw'. */
192 const int *cvt_arr;
193
194 /* Callback function. We always use the `nftw' form. */
195 NFTW_FUNC_T func;
196
197 /* Device of starting point. Needed for FTW_MOUNT. */
198 dev_t dev;
199
200 /* Data structure for keeping fingerprints of already processed
201 object. This is needed when not using FTW_PHYS. */
202 void *known_objects;
203 };
204
205
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[] =
209 {
210 FTW_F, FTW_D, FTW_DNR, FTW_NS, FTW_SL, FTW_DP, FTW_SLN
211 };
212
213 static const int ftw_arr[] =
214 {
215 FTW_F, FTW_D, FTW_DNR, FTW_NS, FTW_F, FTW_D, FTW_NS
216 };
217
218
219 /* Forward declarations of local functions. */
220 static int ftw_dir (struct ftw_data *data, struct STAT *st) internal_function;
221
222
223 static int
224 object_compare (const void *p1, const void *p2)
225 {
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;
230 int cmp1;
231 cmp1 = (kp1->ino > kp2->ino) - (kp1->ino < kp2->ino);
232 if (cmp1 != 0)
233 return cmp1;
234 return (kp1->dev > kp2->dev) - (kp1->dev < kp2->dev);
235 }
236
237
238 static inline int
239 add_object (struct ftw_data *data, struct STAT *st)
240 {
241 struct known_object *newp = malloc (sizeof (struct known_object));
242 if (newp == NULL)
243 return -1;
244 newp->dev = st->st_dev;
245 newp->ino = st->st_ino;
246 return __tsearch (newp, &data->known_objects, object_compare) ? 0 : -1;
247 }
248
249
250 static inline int
251 find_object (struct ftw_data *data, struct STAT *st)
252 {
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;
257 }
258
259
260 static inline int
261 __attribute ((always_inline))
262 open_dir_stream (struct ftw_data *data, struct dir_data *dirp)
263 {
264 int result = 0;
265
266 if (data->dirstreams[data->actdir] != NULL)
267 {
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);
273
274 if (buf == NULL)
275 result = -1;
276 else
277 {
278 DIR *st = data->dirstreams[data->actdir]->stream;
279 struct dirent64 *d;
280 size_t actsize = 0;
281
282 while ((d = __readdir64 (st)) != NULL)
283 {
284 size_t this_len = NAMLEN (d);
285 if (actsize + this_len + 2 >= bufsize)
286 {
287 char *newp;
288 bufsize += MAX (1024, 2 * this_len);
289 newp = (char *) realloc (buf, bufsize);
290 if (newp == NULL)
291 {
292 /* No more memory. */
293 int save_err = errno;
294 free (buf);
295 __set_errno (save_err);
296 result = -1;
297 break;
298 }
299 buf = newp;
300 }
301
302 *((char *) __mempcpy (buf + actsize, d->d_name, this_len))
303 = '\0';
304 actsize += this_len + 1;
305 }
306
307 /* Terminate the list with an additional NUL byte. */
308 buf[actsize++] = '\0';
309
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)
313 {
314 int save_err = errno;
315 free (buf);
316 __set_errno (save_err);
317 result = -1;
318 }
319 else
320 {
321 __closedir (st);
322 data->dirstreams[data->actdir]->stream = NULL;
323 data->dirstreams[data->actdir] = NULL;
324 }
325 }
326 }
327
328 /* Open the new stream. */
329 if (result == 0)
330 {
331 const char *name = ((data->flags & FTW_CHDIR)
332 ? data->dirbuf + data->ftw.base: data->dirbuf);
333 assert (data->dirstreams[data->actdir] == NULL);
334
335 dirp->stream = __opendir (name);
336 if (dirp->stream == NULL)
337 result = -1;
338 else
339 {
340 dirp->content = NULL;
341 data->dirstreams[data->actdir] = dirp;
342
343 if (++data->actdir == data->maxdir)
344 data->actdir = 0;
345 }
346 }
347
348 return result;
349 }
350
351
352 static int
353 internal_function
354 process_entry (struct ftw_data *data, struct dir_data *dir, const char *name,
355 size_t namlen)
356 {
357 struct STAT st;
358 int result = 0;
359 int flag = 0;
360 size_t new_buflen;
361
362 if (name[0] == '.' && (name[1] == '\0'
363 || (name[1] == '.' && name[2] == '\0')))
364 /* Don't process the "." and ".." entries. */
365 return 0;
366
367 new_buflen = data->ftw.base + namlen + 2;
368 if (data->dirbufsize < new_buflen)
369 {
370 /* Enlarge the buffer. */
371 char *newp;
372
373 data->dirbufsize = 2 * new_buflen;
374 newp = (char *) realloc (data->dirbuf, data->dirbufsize);
375 if (newp == NULL)
376 return -1;
377 data->dirbuf = newp;
378 }
379
380 *((char *) __mempcpy (data->dirbuf + data->ftw.base, name, namlen)) = '\0';
381
382 if ((data->flags & FTW_CHDIR) == 0)
383 name = data->dirbuf;
384
385 if (((data->flags & FTW_PHYS)
386 ? LXSTAT (_STAT_VER, name, &st)
387 : XSTAT (_STAT_VER, name, &st)) < 0)
388 {
389 if (errno != EACCES && errno != ENOENT)
390 result = -1;
391 else if (!(data->flags & FTW_PHYS)
392 && LXSTAT (_STAT_VER, name, &st) == 0
393 && S_ISLNK (st.st_mode))
394 flag = FTW_SLN;
395 else
396 flag = FTW_NS;
397 }
398 else
399 {
400 if (S_ISDIR (st.st_mode))
401 flag = FTW_D;
402 else if (S_ISLNK (st.st_mode))
403 flag = FTW_SL;
404 else
405 flag = FTW_F;
406 }
407
408 if (result == 0
409 && (flag == FTW_NS
410 || !(data->flags & FTW_MOUNT) || st.st_dev == data->dev))
411 {
412 if (flag == FTW_D)
413 {
414 if ((data->flags & FTW_PHYS)
415 || (!find_object (data, &st)
416 /* Remember the object. */
417 && (result = add_object (data, &st)) == 0))
418 {
419 result = ftw_dir (data, &st);
420
421 if (result == 0 && (data->flags & FTW_CHDIR))
422 {
423 /* Change back to the parent directory. */
424 int done = 0;
425 if (dir->stream != NULL)
426 if (__fchdir (dirfd (dir->stream)) == 0)
427 done = 1;
428
429 if (!done)
430 {
431 if (data->ftw.base == 1)
432 {
433 if (__chdir ("/") < 0)
434 result = -1;
435 }
436 else
437 if (__chdir ("..") < 0)
438 result = -1;
439 }
440 }
441 }
442 }
443 else
444 result = (*data->func) (data->dirbuf, &st, data->cvt_arr[flag],
445 &data->ftw);
446 }
447
448 return result;
449 }
450
451
452 static int
453 internal_function
454 ftw_dir (struct ftw_data *data, struct STAT *st)
455 {
456 struct dir_data dir;
457 struct dirent64 *d;
458 int previous_base = data->ftw.base;
459 int result;
460 char *startp;
461
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);
465 if (result != 0)
466 {
467 if (errno == EACCES)
468 /* We cannot read the directory. Signal this with a special flag. */
469 result = (*data->func) (data->dirbuf, st, FTW_DNR, &data->ftw);
470
471 return result;
472 }
473
474 /* First, report the directory (if not depth-first). */
475 if (!(data->flags & FTW_DEPTH))
476 {
477 result = (*data->func) (data->dirbuf, st, FTW_D, &data->ftw);
478 if (result != 0)
479 return result;
480 }
481
482 /* If necessary, change to this directory. */
483 if (data->flags & FTW_CHDIR)
484 {
485 if (__fchdir (dirfd (dir.stream)) < 0)
486 {
487 int save_err = errno;
488 __closedir (dir.stream);
489 __set_errno (save_err);
490
491 if (data->actdir-- == 0)
492 data->actdir = data->maxdir - 1;
493 data->dirstreams[data->actdir] = NULL;
494
495 return -1;
496 }
497 }
498
499 /* Next, update the `struct FTW' information. */
500 ++data->ftw.level;
501 startp = strchr (data->dirbuf, '\0');
502 /* There always must be a directory name. */
503 assert (startp != data->dirbuf);
504 if (startp[-1] != '/')
505 *startp++ = '/';
506 data->ftw.base = startp - data->dirbuf;
507
508 while (dir.stream != NULL && (d = __readdir64 (dir.stream)) != NULL)
509 {
510 result = process_entry (data, &dir, d->d_name, NAMLEN (d));
511 if (result != 0)
512 break;
513 }
514
515 if (dir.stream != NULL)
516 {
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;
520
521 assert (dir.content == NULL);
522
523 __closedir (dir.stream);
524 __set_errno (save_err);
525
526 if (data->actdir-- == 0)
527 data->actdir = data->maxdir - 1;
528 data->dirstreams[data->actdir] = NULL;
529 }
530 else
531 {
532 int save_err;
533 char *runp = dir.content;
534
535 while (result == 0 && *runp != '\0')
536 {
537 char *endp = strchr (runp, '\0');
538
539 result = process_entry (data, &dir, runp, endp - runp);
540
541 runp = endp + 1;
542 }
543
544 save_err = errno;
545 free (dir.content);
546 __set_errno (save_err);
547 }
548
549 /* Prepare the return, revert the `struct FTW' information. */
550 data->dirbuf[data->ftw.base - 1] = '\0';
551 --data->ftw.level;
552 data->ftw.base = previous_base;
553
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);
557
558 return result;
559 }
560
561
562 static int
563 internal_function
564 ftw_startup (const char *dir, int is_nftw, void *func, int descriptors,
565 int flags)
566 {
567 struct ftw_data data;
568 struct STAT st;
569 int result = 0;
570 int save_err;
571 char *cwd = NULL;
572 char *cp;
573
574 /* First make sure the parameters are reasonable. */
575 if (dir[0] == '\0')
576 {
577 __set_errno (ENOENT);
578 return -1;
579 }
580
581 data.maxdir = descriptors < 1 ? 1 : descriptors;
582 data.actdir = 0;
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 *));
586
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)
591 return -1;
592 cp = __stpcpy (data.dirbuf, dir);
593 /* Strip trailing slashes. */
594 while (cp > data.dirbuf + 1 && cp[-1] == '/')
595 --cp;
596 *cp = '\0';
597
598 data.ftw.level = 0;
599
600 /* Find basename. */
601 while (cp > data.dirbuf && cp[-1] != '/')
602 --cp;
603 data.ftw.base = cp - data.dirbuf;
604
605 data.flags = flags;
606
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;
614
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;
618
619 /* No object known so far. */
620 data.known_objects = NULL;
621
622 /* Now go to the directory containing the initial file/directory. */
623 if (flags & FTW_CHDIR)
624 {
625 /* GNU extension ahead. */
626 cwd = __getcwd (NULL, 0);
627 if (cwd == NULL)
628 result = -1;
629 else if (data.ftw.base > 0)
630 {
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 ("/");
637 else
638 {
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;
643 }
644 }
645 }
646
647 /* Get stat info for start directory. */
648 if (result == 0)
649 {
650 const char *name = ((data.flags & FTW_CHDIR)
651 ? data.dirbuf + data.ftw.base
652 : data.dirbuf);
653
654 if (((flags & FTW_PHYS)
655 ? LXSTAT (_STAT_VER, name, &st)
656 : XSTAT (_STAT_VER, name, &st)) < 0)
657 {
658 if (!(flags & FTW_PHYS)
659 && errno == ENOENT
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],
663 &data.ftw);
664 else
665 /* No need to call the callback since we cannot say anything
666 about the object. */
667 result = -1;
668 }
669 else
670 {
671 if (S_ISDIR (st.st_mode))
672 {
673 /* Remember the device of the initial directory in case
674 FTW_MOUNT is given. */
675 data.dev = st.st_dev;
676
677 /* We know this directory now. */
678 if (!(flags & FTW_PHYS))
679 result = add_object (&data, &st);
680
681 if (result == 0)
682 result = ftw_dir (&data, &st);
683 }
684 else
685 {
686 int flag = S_ISLNK (st.st_mode) ? FTW_SL : FTW_F;
687
688 result = (*data.func) (data.dirbuf, &st, data.cvt_arr[flag],
689 &data.ftw);
690 }
691 }
692 }
693
694 /* Return to the start directory (if necessary). */
695 if (cwd != NULL)
696 {
697 int save_err = errno;
698 __chdir (cwd);
699 free (cwd);
700 __set_errno (save_err);
701 }
702
703 /* Free all memory. */
704 save_err = errno;
705 __tdestroy (data.known_objects, free);
706 free (data.dirbuf);
707 __set_errno (save_err);
708
709 return result;
710 }
711
712
713
714 /* Entry points. */
715
716 int
717 FTW_NAME (path, func, descriptors)
718 const char *path;
719 FTW_FUNC_T func;
720 int descriptors;
721 {
722 return ftw_startup (path, 0, func, descriptors, 0);
723 }
724
725 int
726 NFTW_NAME (path, func, descriptors, flags)
727 const char *path;
728 NFTW_FUNC_T func;
729 int descriptors;
730 int flags;
731 {
732 return ftw_startup (path, 1, func, descriptors, flags);
733 }