* sysdeps/unix/sysv/linux/i386/lowlevellock.h (lll_futex_wait,
[glibc.git] / io / ftw.c
blobf24a6b46fe004438e71279559d35927864680913
1 /* File tree walker functions.
2 Copyright (C) 1996-2003, 2004, 2006 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
19 02111-1307 USA. */
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
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
39 #ifdef _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
61 #include <errno.h>
62 #include <fcntl.h>
63 #include <ftw.h>
64 #include <limits.h>
65 #include <search.h>
66 #include <stdlib.h>
67 #include <string.h>
68 #include <unistd.h>
69 #if HAVE_SYS_PARAM_H || defined _LIBC
70 # include <sys/param.h>
71 #endif
72 #ifdef _LIBC
73 # include <include/sys/stat.h>
74 #else
75 # include <sys/stat.h>
76 #endif
78 #if ! _LIBC && !HAVE_DECL_STPCPY && !defined stpcpy
79 char *stpcpy ();
80 #endif
82 #if ! _LIBC && ! defined HAVE_MEMPCPY && ! defined mempcpy
83 /* Be CAREFUL that there are no side effects in N. */
84 # define mempcpy(D, S, N) ((void *) ((char *) memcpy (D, S, N) + (N)))
85 #endif
87 /* #define NDEBUG 1 */
88 #include <assert.h>
90 #ifndef _LIBC
91 # undef __chdir
92 # define __chdir chdir
93 # undef __closedir
94 # define __closedir closedir
95 # undef __fchdir
96 # define __fchdir fchdir
97 # undef __getcwd
98 # define __getcwd(P, N) xgetcwd ()
99 extern char *xgetcwd (void);
100 # undef __mempcpy
101 # define __mempcpy mempcpy
102 # undef __opendir
103 # define __opendir opendir
104 # undef __readdir64
105 # define __readdir64 readdir
106 # undef __stpcpy
107 # define __stpcpy stpcpy
108 # undef __tdestroy
109 # define __tdestroy tdestroy
110 # undef __tfind
111 # define __tfind tfind
112 # undef __tsearch
113 # define __tsearch tsearch
114 # undef internal_function
115 # define internal_function /* empty */
116 # undef dirent64
117 # define dirent64 dirent
118 # undef MAX
119 # define MAX(a, b) ((a) > (b) ? (a) : (b))
120 #endif
122 /* Arrange to make lstat calls go through the wrapper function
123 on systems with an lstat function that does not dereference symlinks
124 that are specified with a trailing slash. */
125 #if ! _LIBC && ! LSTAT_FOLLOWS_SLASHED_SYMLINK
126 int rpl_lstat (const char *, struct stat *);
127 # undef lstat
128 # define lstat(Name, Stat_buf) rpl_lstat(Name, Stat_buf)
129 #endif
131 #ifndef __set_errno
132 # define __set_errno(Val) errno = (Val)
133 #endif
135 /* Support for the LFS API version. */
136 #ifndef FTW_NAME
137 # define FTW_NAME ftw
138 # define NFTW_NAME nftw
139 # define NFTW_OLD_NAME __old_nftw
140 # define NFTW_NEW_NAME __new_nftw
141 # define INO_T ino_t
142 # define STAT stat
143 # ifdef _LIBC
144 # define LXSTAT __lxstat
145 # define XSTAT __xstat
146 # else
147 # define LXSTAT(V,f,sb) lstat (f,sb)
148 # define XSTAT(V,f,sb) stat (f,sb)
149 # endif
150 # define FTW_FUNC_T __ftw_func_t
151 # define NFTW_FUNC_T __nftw_func_t
152 #endif
154 /* We define PATH_MAX if the system does not provide a definition.
155 This does not artificially limit any operation. PATH_MAX is simply
156 used as a guesstimate for the expected maximal path length.
157 Buffers will be enlarged if necessary. */
158 #ifndef PATH_MAX
159 # define PATH_MAX 1024
160 #endif
162 struct dir_data
164 DIR *stream;
165 char *content;
168 struct known_object
170 dev_t dev;
171 INO_T ino;
174 struct ftw_data
176 /* Array with pointers to open directory streams. */
177 struct dir_data **dirstreams;
178 size_t actdir;
179 size_t maxdir;
181 /* Buffer containing name of currently processed object. */
182 char *dirbuf;
183 size_t dirbufsize;
185 /* Passed as fourth argument to `nftw' callback. The `base' member
186 tracks the content of the `dirbuf'. */
187 struct FTW ftw;
189 /* Flags passed to `nftw' function. 0 for `ftw'. */
190 int flags;
192 /* Conversion array for flag values. It is the identity mapping for
193 `nftw' calls, otherwise it maps the values to those known by
194 `ftw'. */
195 const int *cvt_arr;
197 /* Callback function. We always use the `nftw' form. */
198 NFTW_FUNC_T func;
200 /* Device of starting point. Needed for FTW_MOUNT. */
201 dev_t dev;
203 /* Data structure for keeping fingerprints of already processed
204 object. This is needed when not using FTW_PHYS. */
205 void *known_objects;
209 /* Internally we use the FTW_* constants used for `nftw'. When invoked
210 as `ftw', map each flag to the subset of values used by `ftw'. */
211 static const int nftw_arr[] =
213 FTW_F, FTW_D, FTW_DNR, FTW_NS, FTW_SL, FTW_DP, FTW_SLN
216 static const int ftw_arr[] =
218 FTW_F, FTW_D, FTW_DNR, FTW_NS, FTW_F, FTW_D, FTW_NS
222 /* Forward declarations of local functions. */
223 static int ftw_dir (struct ftw_data *data, struct STAT *st,
224 struct dir_data *old_dir) internal_function;
227 static int
228 object_compare (const void *p1, const void *p2)
230 /* We don't need a sophisticated and useful comparison. We are only
231 interested in equality. However, we must be careful not to
232 accidentally compare `holes' in the structure. */
233 const struct known_object *kp1 = p1, *kp2 = p2;
234 int cmp1;
235 cmp1 = (kp1->ino > kp2->ino) - (kp1->ino < kp2->ino);
236 if (cmp1 != 0)
237 return cmp1;
238 return (kp1->dev > kp2->dev) - (kp1->dev < kp2->dev);
242 static inline int
243 add_object (struct ftw_data *data, struct STAT *st)
245 struct known_object *newp = malloc (sizeof (struct known_object));
246 if (newp == NULL)
247 return -1;
248 newp->dev = st->st_dev;
249 newp->ino = st->st_ino;
250 return __tsearch (newp, &data->known_objects, object_compare) ? 0 : -1;
254 static inline int
255 find_object (struct ftw_data *data, struct STAT *st)
257 struct known_object obj;
258 obj.dev = st->st_dev;
259 obj.ino = st->st_ino;
260 return __tfind (&obj, &data->known_objects, object_compare) != NULL;
264 static inline int
265 __attribute ((always_inline))
266 open_dir_stream (struct ftw_data *data, struct dir_data *dirp)
268 int result = 0;
270 if (data->dirstreams[data->actdir] != NULL)
272 /* Oh, oh. We must close this stream. Get all remaining
273 entries and store them as a list in the `content' member of
274 the `struct dir_data' variable. */
275 size_t bufsize = 1024;
276 char *buf = malloc (bufsize);
278 if (buf == NULL)
279 result = -1;
280 else
282 DIR *st = data->dirstreams[data->actdir]->stream;
283 struct dirent64 *d;
284 size_t actsize = 0;
286 while ((d = __readdir64 (st)) != NULL)
288 size_t this_len = NAMLEN (d);
289 if (actsize + this_len + 2 >= bufsize)
291 char *newp;
292 bufsize += MAX (1024, 2 * this_len);
293 newp = (char *) realloc (buf, bufsize);
294 if (newp == NULL)
296 /* No more memory. */
297 int save_err = errno;
298 free (buf);
299 __set_errno (save_err);
300 result = -1;
301 break;
303 buf = newp;
306 *((char *) __mempcpy (buf + actsize, d->d_name, this_len))
307 = '\0';
308 actsize += this_len + 1;
311 /* Terminate the list with an additional NUL byte. */
312 buf[actsize++] = '\0';
314 /* Shrink the buffer to what we actually need. */
315 data->dirstreams[data->actdir]->content = realloc (buf, actsize);
316 if (data->dirstreams[data->actdir]->content == NULL)
318 int save_err = errno;
319 free (buf);
320 __set_errno (save_err);
321 result = -1;
323 else
325 __closedir (st);
326 data->dirstreams[data->actdir]->stream = NULL;
327 data->dirstreams[data->actdir] = NULL;
332 /* Open the new stream. */
333 if (result == 0)
335 const char *name = ((data->flags & FTW_CHDIR)
336 ? data->dirbuf + data->ftw.base: data->dirbuf);
337 assert (data->dirstreams[data->actdir] == NULL);
339 dirp->stream = __opendir (name);
340 if (dirp->stream == NULL)
341 result = -1;
342 else
344 dirp->content = NULL;
345 data->dirstreams[data->actdir] = dirp;
347 if (++data->actdir == data->maxdir)
348 data->actdir = 0;
352 return result;
356 static int
357 internal_function
358 process_entry (struct ftw_data *data, struct dir_data *dir, const char *name,
359 size_t namlen)
361 struct STAT st;
362 int result = 0;
363 int flag = 0;
364 size_t new_buflen;
366 if (name[0] == '.' && (name[1] == '\0'
367 || (name[1] == '.' && name[2] == '\0')))
368 /* Don't process the "." and ".." entries. */
369 return 0;
371 new_buflen = data->ftw.base + namlen + 2;
372 if (data->dirbufsize < new_buflen)
374 /* Enlarge the buffer. */
375 char *newp;
377 data->dirbufsize = 2 * new_buflen;
378 newp = (char *) realloc (data->dirbuf, data->dirbufsize);
379 if (newp == NULL)
380 return -1;
381 data->dirbuf = newp;
384 *((char *) __mempcpy (data->dirbuf + data->ftw.base, name, namlen)) = '\0';
386 if ((data->flags & FTW_CHDIR) == 0)
387 name = data->dirbuf;
389 if (((data->flags & FTW_PHYS)
390 ? LXSTAT (_STAT_VER, name, &st)
391 : XSTAT (_STAT_VER, name, &st)) < 0)
393 if (errno != EACCES && errno != ENOENT)
394 result = -1;
395 else if (!(data->flags & FTW_PHYS)
396 && LXSTAT (_STAT_VER, name, &st) == 0
397 && S_ISLNK (st.st_mode))
398 flag = FTW_SLN;
399 else
400 flag = FTW_NS;
402 else
404 if (S_ISDIR (st.st_mode))
405 flag = FTW_D;
406 else if (S_ISLNK (st.st_mode))
407 flag = FTW_SL;
408 else
409 flag = FTW_F;
412 if (result == 0
413 && (flag == FTW_NS
414 || !(data->flags & FTW_MOUNT) || st.st_dev == data->dev))
416 if (flag == FTW_D)
418 if ((data->flags & FTW_PHYS)
419 || (!find_object (data, &st)
420 /* Remember the object. */
421 && (result = add_object (data, &st)) == 0))
422 result = ftw_dir (data, &st, dir);
424 else
425 result = (*data->func) (data->dirbuf, &st, data->cvt_arr[flag],
426 &data->ftw);
429 if ((data->flags & FTW_ACTIONRETVAL) && result == FTW_SKIP_SUBTREE)
430 result = 0;
432 return result;
436 static int
437 __attribute ((noinline))
438 internal_function
439 ftw_dir (struct ftw_data *data, struct STAT *st, struct dir_data *old_dir)
441 struct dir_data dir;
442 struct dirent64 *d;
443 int previous_base = data->ftw.base;
444 int result;
445 char *startp;
447 /* Open the stream for this directory. This might require that
448 another stream has to be closed. */
449 result = open_dir_stream (data, &dir);
450 if (result != 0)
452 if (errno == EACCES)
453 /* We cannot read the directory. Signal this with a special flag. */
454 result = (*data->func) (data->dirbuf, st, FTW_DNR, &data->ftw);
456 return result;
459 /* First, report the directory (if not depth-first). */
460 if (!(data->flags & FTW_DEPTH))
462 result = (*data->func) (data->dirbuf, st, FTW_D, &data->ftw);
463 if (result != 0)
465 int save_err;
466 fail:
467 save_err = errno;
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;
474 return result;
478 /* If necessary, change to this directory. */
479 if (data->flags & FTW_CHDIR)
481 if (__fchdir (dirfd (dir.stream)) < 0)
483 result = -1;
484 goto fail;
488 /* Next, update the `struct FTW' information. */
489 ++data->ftw.level;
490 startp = strchr (data->dirbuf, '\0');
491 /* There always must be a directory name. */
492 assert (startp != data->dirbuf);
493 if (startp[-1] != '/')
494 *startp++ = '/';
495 data->ftw.base = startp - data->dirbuf;
497 while (dir.stream != NULL && (d = __readdir64 (dir.stream)) != NULL)
499 result = process_entry (data, &dir, d->d_name, NAMLEN (d));
500 if (result != 0)
501 break;
504 if (dir.stream != NULL)
506 /* The stream is still open. I.e., we did not need more
507 descriptors. Simply close the stream now. */
508 int save_err = errno;
510 assert (dir.content == NULL);
512 __closedir (dir.stream);
513 __set_errno (save_err);
515 if (data->actdir-- == 0)
516 data->actdir = data->maxdir - 1;
517 data->dirstreams[data->actdir] = NULL;
519 else
521 int save_err;
522 char *runp = dir.content;
524 while (result == 0 && *runp != '\0')
526 char *endp = strchr (runp, '\0');
528 result = process_entry (data, &dir, runp, endp - runp);
530 runp = endp + 1;
533 save_err = errno;
534 free (dir.content);
535 __set_errno (save_err);
538 if ((data->flags & FTW_ACTIONRETVAL) && result == FTW_SKIP_SIBLINGS)
539 result = 0;
541 /* Prepare the return, revert the `struct FTW' information. */
542 data->dirbuf[data->ftw.base - 1] = '\0';
543 --data->ftw.level;
544 data->ftw.base = previous_base;
546 /* Finally, if we process depth-first report the directory. */
547 if (result == 0 && (data->flags & FTW_DEPTH))
548 result = (*data->func) (data->dirbuf, st, FTW_DP, &data->ftw);
550 if (old_dir
551 && (data->flags & FTW_CHDIR)
552 && (result == 0
553 || ((data->flags & FTW_ACTIONRETVAL)
554 && (result != -1 && result != FTW_STOP))))
556 /* Change back to the parent directory. */
557 int done = 0;
558 if (old_dir->stream != NULL)
559 if (__fchdir (dirfd (old_dir->stream)) == 0)
560 done = 1;
562 if (!done)
564 if (data->ftw.base == 1)
566 if (__chdir ("/") < 0)
567 result = -1;
569 else
570 if (__chdir ("..") < 0)
571 result = -1;
575 return result;
579 static int
580 __attribute ((noinline))
581 internal_function
582 ftw_startup (const char *dir, int is_nftw, void *func, int descriptors,
583 int flags)
585 struct ftw_data data;
586 struct STAT st;
587 int result = 0;
588 int save_err;
589 int cwdfd = -1;
590 char *cwd = NULL;
591 char *cp;
593 /* First make sure the parameters are reasonable. */
594 if (dir[0] == '\0')
596 __set_errno (ENOENT);
597 return -1;
600 data.maxdir = descriptors < 1 ? 1 : descriptors;
601 data.actdir = 0;
602 data.dirstreams = (struct dir_data **) alloca (data.maxdir
603 * sizeof (struct dir_data *));
604 memset (data.dirstreams, '\0', data.maxdir * sizeof (struct dir_data *));
606 /* PATH_MAX is always defined when we get here. */
607 data.dirbufsize = MAX (2 * strlen (dir), PATH_MAX);
608 data.dirbuf = (char *) malloc (data.dirbufsize);
609 if (data.dirbuf == NULL)
610 return -1;
611 cp = __stpcpy (data.dirbuf, dir);
612 /* Strip trailing slashes. */
613 while (cp > data.dirbuf + 1 && cp[-1] == '/')
614 --cp;
615 *cp = '\0';
617 data.ftw.level = 0;
619 /* Find basename. */
620 while (cp > data.dirbuf && cp[-1] != '/')
621 --cp;
622 data.ftw.base = cp - data.dirbuf;
624 data.flags = flags;
626 /* This assignment might seem to be strange but it is what we want.
627 The trick is that the first three arguments to the `ftw' and
628 `nftw' callback functions are equal. Therefore we can call in
629 every case the callback using the format of the `nftw' version
630 and get the correct result since the stack layout for a function
631 call in C allows this. */
632 data.func = (NFTW_FUNC_T) func;
634 /* Since we internally use the complete set of FTW_* values we need
635 to reduce the value range before calling a `ftw' callback. */
636 data.cvt_arr = is_nftw ? nftw_arr : ftw_arr;
638 /* No object known so far. */
639 data.known_objects = NULL;
641 /* Now go to the directory containing the initial file/directory. */
642 if (flags & FTW_CHDIR)
644 /* We have to be able to go back to the current working
645 directory. The best way to do this is to use a file
646 descriptor. */
647 cwdfd = __open (".", O_RDONLY | O_DIRECTORY);
648 if (cwdfd == -1)
650 /* Try getting the directory name. This can be needed if
651 the current directory is executable but not readable. */
652 if (errno == EACCES)
653 /* GNU extension ahead. */
654 cwd = __getcwd (NULL, 0);
656 if (cwd == NULL)
657 goto out_fail;
659 else if (data.maxdir > 1)
660 /* Account for the file descriptor we use here. */
661 --data.maxdir;
663 if (data.ftw.base > 0)
665 /* Change to the directory the file is in. In data.dirbuf
666 we have a writable copy of the file name. Just NUL
667 terminate it for now and change the directory. */
668 if (data.ftw.base == 1)
669 /* I.e., the file is in the root directory. */
670 result = __chdir ("/");
671 else
673 char ch = data.dirbuf[data.ftw.base - 1];
674 data.dirbuf[data.ftw.base - 1] = '\0';
675 result = __chdir (data.dirbuf);
676 data.dirbuf[data.ftw.base - 1] = ch;
681 /* Get stat info for start directory. */
682 if (result == 0)
684 const char *name = ((data.flags & FTW_CHDIR)
685 ? data.dirbuf + data.ftw.base
686 : data.dirbuf);
688 if (((flags & FTW_PHYS)
689 ? LXSTAT (_STAT_VER, name, &st)
690 : XSTAT (_STAT_VER, name, &st)) < 0)
692 if (!(flags & FTW_PHYS)
693 && errno == ENOENT
694 && LXSTAT (_STAT_VER, name, &st) == 0
695 && S_ISLNK (st.st_mode))
696 result = (*data.func) (data.dirbuf, &st, data.cvt_arr[FTW_SLN],
697 &data.ftw);
698 else
699 /* No need to call the callback since we cannot say anything
700 about the object. */
701 result = -1;
703 else
705 if (S_ISDIR (st.st_mode))
707 /* Remember the device of the initial directory in case
708 FTW_MOUNT is given. */
709 data.dev = st.st_dev;
711 /* We know this directory now. */
712 if (!(flags & FTW_PHYS))
713 result = add_object (&data, &st);
715 if (result == 0)
716 result = ftw_dir (&data, &st, NULL);
718 else
720 int flag = S_ISLNK (st.st_mode) ? FTW_SL : FTW_F;
722 result = (*data.func) (data.dirbuf, &st, data.cvt_arr[flag],
723 &data.ftw);
727 if ((flags & FTW_ACTIONRETVAL)
728 && (result == FTW_SKIP_SUBTREE || result == FTW_SKIP_SIBLINGS))
729 result = 0;
732 /* Return to the start directory (if necessary). */
733 if (cwdfd != -1)
735 int save_err = errno;
736 __fchdir (cwdfd);
737 __set_errno (save_err);
739 else if (cwd != NULL)
741 int save_err = errno;
742 __chdir (cwd);
743 free (cwd);
744 __set_errno (save_err);
747 /* Free all memory. */
748 out_fail:
749 save_err = errno;
750 __tdestroy (data.known_objects, free);
751 free (data.dirbuf);
752 __set_errno (save_err);
754 return result;
759 /* Entry points. */
762 FTW_NAME (path, func, descriptors)
763 const char *path;
764 FTW_FUNC_T func;
765 int descriptors;
767 return ftw_startup (path, 0, func, descriptors, 0);
770 #ifndef _LIBC
772 NFTW_NAME (path, func, descriptors, flags)
773 const char *path;
774 NFTW_FUNC_T func;
775 int descriptors;
776 int flags;
778 return ftw_startup (path, 1, func, descriptors, flags);
780 #else
782 # include <shlib-compat.h>
784 int NFTW_NEW_NAME (const char *, NFTW_FUNC_T, int, int);
787 NFTW_NEW_NAME (path, func, descriptors, flags)
788 const char *path;
789 NFTW_FUNC_T func;
790 int descriptors;
791 int flags;
793 if (flags
794 & ~(FTW_PHYS | FTW_MOUNT | FTW_CHDIR | FTW_DEPTH | FTW_ACTIONRETVAL))
796 __set_errno (EINVAL);
797 return -1;
799 return ftw_startup (path, 1, func, descriptors, flags);
802 versioned_symbol (libc, NFTW_NEW_NAME, NFTW_NAME, GLIBC_2_3_3);
804 # if SHLIB_COMPAT(libc, GLIBC_2_1, GLIBC_2_3_3)
806 /* Older nftw* version just ignored all unknown flags. */
808 int NFTW_OLD_NAME (const char *, NFTW_FUNC_T, int, int);
811 attribute_compat_text_section
812 NFTW_OLD_NAME (path, func, descriptors, flags)
813 const char *path;
814 NFTW_FUNC_T func;
815 int descriptors;
816 int flags;
818 flags &= (FTW_PHYS | FTW_MOUNT | FTW_CHDIR | FTW_DEPTH);
819 return ftw_startup (path, 1, func, descriptors, flags);
822 compat_symbol (libc, NFTW_OLD_NAME, NFTW_NAME, GLIBC_2_1);
823 # endif
824 #endif