Define _POSIX_THREAD_PRIORITY_SCHEDULING.
[glibc/pb-stable.git] / io / ftw.c
blobc7c2038c06edf03157b05f9eef5e006f724332f6
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
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 #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
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
77 #if ! _LIBC && !HAVE_DECL_STPCPY && !defined stpcpy
78 char *stpcpy ();
79 #endif
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
86 /* #define NDEBUG 1 */
87 #include <assert.h>
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
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
130 #ifndef __set_errno
131 # define __set_errno(Val) errno = (Val)
132 #endif
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
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
159 struct dir_data
161 DIR *stream;
162 char *content;
165 struct known_object
167 dev_t dev;
168 INO_T ino;
171 struct ftw_data
173 /* Array with pointers to open directory streams. */
174 struct dir_data **dirstreams;
175 size_t actdir;
176 size_t maxdir;
178 /* Buffer containing name of currently processed object. */
179 char *dirbuf;
180 size_t dirbufsize;
182 /* Passed as fourth argument to `nftw' callback. The `base' member
183 tracks the content of the `dirbuf'. */
184 struct FTW ftw;
186 /* Flags passed to `nftw' function. 0 for `ftw'. */
187 int flags;
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;
194 /* Callback function. We always use the `nftw' form. */
195 NFTW_FUNC_T func;
197 /* Device of starting point. Needed for FTW_MOUNT. */
198 dev_t dev;
200 /* Data structure for keeping fingerprints of already processed
201 object. This is needed when not using FTW_PHYS. */
202 void *known_objects;
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[] =
210 FTW_F, FTW_D, FTW_DNR, FTW_NS, FTW_SL, FTW_DP, FTW_SLN
213 static const int ftw_arr[] =
215 FTW_F, FTW_D, FTW_DNR, FTW_NS, FTW_F, FTW_D, FTW_NS
219 /* Forward declarations of local functions. */
220 static int ftw_dir (struct ftw_data *data, struct STAT *st) internal_function;
223 static int
224 object_compare (const void *p1, const void *p2)
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);
238 static inline int
239 add_object (struct ftw_data *data, struct STAT *st)
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;
250 static inline int
251 find_object (struct ftw_data *data, struct STAT *st)
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;
260 static inline int
261 __attribute ((always_inline))
262 open_dir_stream (struct ftw_data *data, struct dir_data *dirp)
264 int result = 0;
266 if (data->dirstreams[data->actdir] != NULL)
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);
274 if (buf == NULL)
275 result = -1;
276 else
278 DIR *st = data->dirstreams[data->actdir]->stream;
279 struct dirent64 *d;
280 size_t actsize = 0;
282 while ((d = __readdir64 (st)) != NULL)
284 size_t this_len = NAMLEN (d);
285 if (actsize + this_len + 2 >= bufsize)
287 char *newp;
288 bufsize += MAX (1024, 2 * this_len);
289 newp = (char *) realloc (buf, bufsize);
290 if (newp == NULL)
292 /* No more memory. */
293 int save_err = errno;
294 free (buf);
295 __set_errno (save_err);
296 result = -1;
297 break;
299 buf = newp;
302 *((char *) __mempcpy (buf + actsize, d->d_name, this_len))
303 = '\0';
304 actsize += this_len + 1;
307 /* Terminate the list with an additional NUL byte. */
308 buf[actsize++] = '\0';
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)
314 int save_err = errno;
315 free (buf);
316 __set_errno (save_err);
317 result = -1;
319 else
321 __closedir (st);
322 data->dirstreams[data->actdir]->stream = NULL;
323 data->dirstreams[data->actdir] = NULL;
328 /* Open the new stream. */
329 if (result == 0)
331 const char *name = ((data->flags & FTW_CHDIR)
332 ? data->dirbuf + data->ftw.base: data->dirbuf);
333 assert (data->dirstreams[data->actdir] == NULL);
335 dirp->stream = __opendir (name);
336 if (dirp->stream == NULL)
337 result = -1;
338 else
340 dirp->content = NULL;
341 data->dirstreams[data->actdir] = dirp;
343 if (++data->actdir == data->maxdir)
344 data->actdir = 0;
348 return result;
352 static int
353 internal_function
354 process_entry (struct ftw_data *data, struct dir_data *dir, const char *name,
355 size_t namlen)
357 struct STAT st;
358 int result = 0;
359 int flag = 0;
360 size_t new_buflen;
362 if (name[0] == '.' && (name[1] == '\0'
363 || (name[1] == '.' && name[2] == '\0')))
364 /* Don't process the "." and ".." entries. */
365 return 0;
367 new_buflen = data->ftw.base + namlen + 2;
368 if (data->dirbufsize < new_buflen)
370 /* Enlarge the buffer. */
371 char *newp;
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;
380 *((char *) __mempcpy (data->dirbuf + data->ftw.base, name, namlen)) = '\0';
382 if ((data->flags & FTW_CHDIR) == 0)
383 name = data->dirbuf;
385 if (((data->flags & FTW_PHYS)
386 ? LXSTAT (_STAT_VER, name, &st)
387 : XSTAT (_STAT_VER, name, &st)) < 0)
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;
398 else
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;
408 if (result == 0
409 && (flag == FTW_NS
410 || !(data->flags & FTW_MOUNT) || st.st_dev == data->dev))
412 if (flag == FTW_D)
414 if ((data->flags & FTW_PHYS)
415 || (!find_object (data, &st)
416 /* Remember the object. */
417 && (result = add_object (data, &st)) == 0))
419 result = ftw_dir (data, &st);
421 if (result == 0 && (data->flags & FTW_CHDIR))
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;
429 if (!done)
431 if (data->ftw.base == 1)
433 if (__chdir ("/") < 0)
434 result = -1;
436 else
437 if (__chdir ("..") < 0)
438 result = -1;
443 else
444 result = (*data->func) (data->dirbuf, &st, data->cvt_arr[flag],
445 &data->ftw);
448 return result;
452 static int
453 internal_function
454 ftw_dir (struct ftw_data *data, struct STAT *st)
456 struct dir_data dir;
457 struct dirent64 *d;
458 int previous_base = data->ftw.base;
459 int result;
460 char *startp;
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)
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);
471 return result;
474 /* First, report the directory (if not depth-first). */
475 if (!(data->flags & FTW_DEPTH))
477 result = (*data->func) (data->dirbuf, st, FTW_D, &data->ftw);
478 if (result != 0)
479 return result;
482 /* If necessary, change to this directory. */
483 if (data->flags & FTW_CHDIR)
485 if (__fchdir (dirfd (dir.stream)) < 0)
487 int save_err = errno;
488 __closedir (dir.stream);
489 __set_errno (save_err);
491 if (data->actdir-- == 0)
492 data->actdir = data->maxdir - 1;
493 data->dirstreams[data->actdir] = NULL;
495 return -1;
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;
508 while (dir.stream != NULL && (d = __readdir64 (dir.stream)) != NULL)
510 result = process_entry (data, &dir, d->d_name, NAMLEN (d));
511 if (result != 0)
512 break;
515 if (dir.stream != NULL)
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;
521 assert (dir.content == NULL);
523 __closedir (dir.stream);
524 __set_errno (save_err);
526 if (data->actdir-- == 0)
527 data->actdir = data->maxdir - 1;
528 data->dirstreams[data->actdir] = NULL;
530 else
532 int save_err;
533 char *runp = dir.content;
535 while (result == 0 && *runp != '\0')
537 char *endp = strchr (runp, '\0');
539 result = process_entry (data, &dir, runp, endp - runp);
541 runp = endp + 1;
544 save_err = errno;
545 free (dir.content);
546 __set_errno (save_err);
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;
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);
558 return result;
562 static int
563 internal_function
564 ftw_startup (const char *dir, int is_nftw, void *func, int descriptors,
565 int flags)
567 struct ftw_data data;
568 struct STAT st;
569 int result = 0;
570 int save_err;
571 char *cwd = NULL;
572 char *cp;
574 /* First make sure the parameters are reasonable. */
575 if (dir[0] == '\0')
577 __set_errno (ENOENT);
578 return -1;
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 *));
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';
598 data.ftw.level = 0;
600 /* Find basename. */
601 while (cp > data.dirbuf && cp[-1] != '/')
602 --cp;
603 data.ftw.base = cp - data.dirbuf;
605 data.flags = flags;
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;
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;
619 /* No object known so far. */
620 data.known_objects = NULL;
622 /* Now go to the directory containing the initial file/directory. */
623 if (flags & FTW_CHDIR)
625 /* GNU extension ahead. */
626 cwd = __getcwd (NULL, 0);
627 if (cwd == NULL)
628 result = -1;
629 else if (data.ftw.base > 0)
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
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;
647 /* Get stat info for start directory. */
648 if (result == 0)
650 const char *name = ((data.flags & FTW_CHDIR)
651 ? data.dirbuf + data.ftw.base
652 : data.dirbuf);
654 if (((flags & FTW_PHYS)
655 ? LXSTAT (_STAT_VER, name, &st)
656 : XSTAT (_STAT_VER, name, &st)) < 0)
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;
669 else
671 if (S_ISDIR (st.st_mode))
673 /* Remember the device of the initial directory in case
674 FTW_MOUNT is given. */
675 data.dev = st.st_dev;
677 /* We know this directory now. */
678 if (!(flags & FTW_PHYS))
679 result = add_object (&data, &st);
681 if (result == 0)
682 result = ftw_dir (&data, &st);
684 else
686 int flag = S_ISLNK (st.st_mode) ? FTW_SL : FTW_F;
688 result = (*data.func) (data.dirbuf, &st, data.cvt_arr[flag],
689 &data.ftw);
694 /* Return to the start directory (if necessary). */
695 if (cwd != NULL)
697 int save_err = errno;
698 __chdir (cwd);
699 free (cwd);
700 __set_errno (save_err);
703 /* Free all memory. */
704 save_err = errno;
705 __tdestroy (data.known_objects, free);
706 free (data.dirbuf);
707 __set_errno (save_err);
709 return result;
714 /* Entry points. */
717 FTW_NAME (path, func, descriptors)
718 const char *path;
719 FTW_FUNC_T func;
720 int descriptors;
722 return ftw_startup (path, 0, func, descriptors, 0);
726 NFTW_NAME (path, func, descriptors, flags)
727 const char *path;
728 NFTW_FUNC_T func;
729 int descriptors;
730 int flags;
732 return ftw_startup (path, 1, func, descriptors, flags);