]> git.ipfire.org Git - thirdparty/glibc.git/blame - io/ftw.c
Update.
[thirdparty/glibc.git] / io / ftw.c
CommitLineData
76b87c03 1/* File tree walker functions.
25f227b9 2 Copyright (C) 1996,1997,1998,1999,2000,2001 Free Software Foundation, Inc.
47707456 3 This file is part of the GNU C Library.
76b87c03 4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
28f540f4 5
47707456
UD
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.
28f540f4 10
47707456
UD
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.
28f540f4 15
47707456
UD
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. */
28f540f4 20
76b87c03 21#include <dirent.h>
28f540f4 22#include <errno.h>
76b87c03
UD
23#include <ftw.h>
24#include <search.h>
28f540f4
RM
25#include <stdlib.h>
26#include <string.h>
76b87c03
UD
27#include <unistd.h>
28#include <sys/param.h>
cc3fa755 29#include <include/sys/stat.h>
28f540f4 30
76b87c03
UD
31/* #define NDEBUG 1 */
32#include <assert.h>
28f540f4 33
dfd2257a
UD
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 DIRENT dirent
50304ef0 41# define READDIR __readdir
dfd2257a
UD
42# define LXSTAT __lxstat
43# define XSTAT __xstat
44# define FTW_FUNC_T __ftw_func_t
45# define NFTW_FUNC_T __nftw_func_t
46#endif
28f540f4 47
76b87c03
UD
48struct dir_data
49{
50 DIR *stream;
51 char *content;
52};
28f540f4 53
d951286f
UD
54struct known_object
55{
56 dev_t dev;
dfd2257a 57 INO_T ino;
d951286f
UD
58};
59
76b87c03 60struct ftw_data
28f540f4 61{
d951286f 62 /* Array with pointers to open directory streams. */
76b87c03
UD
63 struct dir_data **dirstreams;
64 size_t actdir;
65 size_t maxdir;
28f540f4 66
d951286f 67 /* Buffer containing name of currently processed object. */
76b87c03
UD
68 char *dirbuf;
69 size_t dirbufsize;
d951286f
UD
70
71 /* Passed as fourth argument to `nftw' callback. The `base' member
72 tracks the content of the `dirbuf'. */
76b87c03 73 struct FTW ftw;
28f540f4 74
d951286f 75 /* Flags passed to `nftw' function. 0 for `ftw'. */
76b87c03 76 int flags;
28f540f4 77
d951286f
UD
78 /* Conversion array for flag values. It is the identity mapping for
79 `nftw' calls, otherwise it maps the values to those know by
80 `ftw'. */
390500b1 81 const int *cvt_arr;
28f540f4 82
d951286f 83 /* Callback function. We always use the `nftw' form. */
dfd2257a 84 NFTW_FUNC_T func;
28f540f4 85
d951286f 86 /* Device of starting point. Needed for FTW_MOUNT. */
76b87c03 87 dev_t dev;
d951286f
UD
88
89 /* Data structure for keeping fingerprints of already processed
90 object. This is needed when not using FTW_PHYS. */
91 void *known_objects;
76b87c03 92};
28f540f4 93
92777700 94
76b87c03
UD
95/* Internally we use the FTW_* constants used for `nftw'. When the
96 process called `ftw' we must reduce the flag to the known flags
97 for `ftw'. */
390500b1 98static const int nftw_arr[] =
76b87c03
UD
99{
100 FTW_F, FTW_D, FTW_DNR, FTW_NS, FTW_SL, FTW_DP, FTW_SLN
101};
28f540f4 102
390500b1 103static const int ftw_arr[] =
76b87c03
UD
104{
105 FTW_F, FTW_D, FTW_DNR, FTW_NS, FTW_F, FTW_D, FTW_NS
106};
28f540f4 107
76b87c03
UD
108
109/* Forward declarations of local functions. */
dfd2257a 110static int ftw_dir (struct ftw_data *data, struct STAT *st) internal_function;
d951286f
UD
111
112
113static int
114object_compare (const void *p1, const void *p2)
115{
116 /* We don't need a sophisticated and useful comparison. We are only
0413b54c
UD
117 interested in equality. However, we must be careful not to
118 accidentally compare `holes' in the structure. */
119 const struct known_object *kp1 = p1, *kp2 = p2;
120 int cmp1;
ca34d7a7 121 cmp1 = (kp1->dev > kp2->dev) - (kp1->dev < kp2->dev);
0413b54c
UD
122 if (cmp1 != 0)
123 return cmp1;
ca34d7a7 124 return (kp1->ino > kp2->ino) - (kp1->ino < kp2->ino);
d951286f
UD
125}
126
127
128static inline int
dfd2257a 129add_object (struct ftw_data *data, struct STAT *st)
d951286f
UD
130{
131 struct known_object *newp = malloc (sizeof (struct known_object));
132 if (newp == NULL)
133 return -1;
134 newp->dev = st->st_dev;
135 newp->ino = st->st_ino;
136 return __tsearch (newp, &data->known_objects, object_compare) ? 0 : -1;
137}
138
139
140static inline int
dfd2257a 141find_object (struct ftw_data *data, struct STAT *st)
d951286f
UD
142{
143 struct known_object obj = { dev: st->st_dev, ino: st->st_ino };
144 return __tfind (&obj, &data->known_objects, object_compare) != NULL;
145}
76b87c03
UD
146
147
148static inline int
149open_dir_stream (struct ftw_data *data, struct dir_data *dirp)
150{
151 int result = 0;
152
153 if (data->dirstreams[data->actdir] != NULL)
154 {
155 /* Oh, oh. We must close this stream. Get all remaining
156 entries and store them as a list in the `content' member of
157 the `struct dir_data' variable. */
158 size_t bufsize = 1024;
159 char *buf = malloc (bufsize);
160
161 if (buf == NULL)
162 result = -1;
163 else
28f540f4 164 {
76b87c03 165 DIR *st = data->dirstreams[data->actdir]->stream;
dfd2257a 166 struct DIRENT *d;
76b87c03
UD
167 size_t actsize = 0;
168
dfd2257a 169 while ((d = READDIR (st)) != NULL)
76b87c03
UD
170 {
171 size_t this_len = _D_EXACT_NAMLEN (d);
172 if (actsize + this_len + 2 >= bufsize)
173 {
174 char *newp;
175 bufsize += MAX (1024, 2 * this_len);
176 newp = realloc (buf, bufsize);
177 if (newp == NULL)
178 {
179 /* No more memory. */
180 int save_err = errno;
181 free (buf);
182 __set_errno (save_err);
183 result = -1;
184 break;
185 }
186 buf = newp;
187 }
188
86187531
UD
189 *((char *) __mempcpy (buf + actsize, d->d_name, this_len))
190 = '\0';
191 actsize += this_len + 1;
76b87c03 192 }
28f540f4 193
76b87c03
UD
194 /* Terminate the list with an additional NUL byte. */
195 buf[actsize++] = '\0';
28f540f4 196
76b87c03
UD
197 /* Shrink the buffer to what we actually need. */
198 data->dirstreams[data->actdir]->content = realloc (buf, actsize);
199 if (data->dirstreams[data->actdir]->content == NULL)
200 {
201 int save_err = errno;
202 free (buf);
203 __set_errno (save_err);
204 result = -1;
205 }
28f540f4
RM
206 else
207 {
50304ef0 208 __closedir (st);
76b87c03
UD
209 data->dirstreams[data->actdir]->stream = NULL;
210 data->dirstreams[data->actdir] = NULL;
28f540f4
RM
211 }
212 }
76b87c03
UD
213 }
214
215 /* Open the new stream. */
216 if (result == 0)
217 {
218 assert (data->dirstreams[data->actdir] == NULL);
219
50304ef0 220 dirp->stream = __opendir (data->dirbuf);
76b87c03
UD
221 if (dirp->stream == NULL)
222 result = -1;
28f540f4 223 else
76b87c03
UD
224 {
225 dirp->content = NULL;
226 data->dirstreams[data->actdir] = dirp;
227
228 if (++data->actdir == data->maxdir)
229 data->actdir = 0;
230 }
231 }
232
233 return result;
234}
235
236
237static inline int
238process_entry (struct ftw_data *data, struct dir_data *dir, const char *name,
239 size_t namlen)
240{
dfd2257a 241 struct STAT st;
76b87c03 242 int result = 0;
256846bb 243 int flag = 0;
76b87c03
UD
244
245 if (name[0] == '.' && (name[1] == '\0'
246 || (name[1] == '.' && name[2] == '\0')))
247 /* Don't process the "." and ".." entries. */
248 return 0;
249
250 if (data->dirbufsize < data->ftw.base + namlen + 2)
251 {
252 /* Enlarge the buffer. */
253 char *newp;
254
255 data->dirbufsize *= 2;
256 newp = realloc (data->dirbuf, data->dirbufsize);
257 if (newp == NULL)
258 return -1;
259 data->dirbuf = newp;
260 }
28f540f4 261
86187531 262 *((char *) __mempcpy (data->dirbuf + data->ftw.base, name, namlen)) = '\0';
28f540f4 263
b13927da 264 if (((data->flags & FTW_PHYS)
dfd2257a
UD
265 ? LXSTAT (_STAT_VER, data->dirbuf, &st)
266 : XSTAT (_STAT_VER, data->dirbuf, &st)) < 0)
76b87c03
UD
267 {
268 if (errno != EACCES && errno != ENOENT)
269 result = -1;
270 else if (!(data->flags & FTW_PHYS)
dfd2257a 271 && LXSTAT (_STAT_VER, data->dirbuf, &st) == 0
d951286f 272 && S_ISLNK (st.st_mode))
76b87c03
UD
273 flag = FTW_SLN;
274 else
275 flag = FTW_NS;
276 }
277 else
278 {
d951286f 279 if (S_ISDIR (st.st_mode))
76b87c03 280 flag = FTW_D;
d951286f 281 else if (S_ISLNK (st.st_mode))
76b87c03
UD
282 flag = FTW_SL;
283 else
284 flag = FTW_F;
285 }
286
287 if (result == 0
b13927da
UD
288 && (flag == FTW_NS
289 || !(data->flags & FTW_MOUNT) || st.st_dev == data->dev))
76b87c03 290 {
eb7c2001 291 if (flag == FTW_D)
28f540f4 292 {
eb7c2001
UD
293 if ((data->flags & FTW_PHYS)
294 || (!find_object (data, &st)
295 /* Remember the object. */
296 && (result = add_object (data, &st)) == 0))
76b87c03 297 {
d951286f 298 result = ftw_dir (data, &st);
76b87c03 299
d951286f 300 if (result == 0 && (data->flags & FTW_CHDIR))
76b87c03 301 {
d951286f
UD
302 /* Change back to current directory. */
303 int done = 0;
304 if (dir->stream != NULL)
b13927da 305 if (__fchdir (dirfd (dir->stream)) == 0)
d951286f 306 done = 1;
76b87c03 307
d951286f
UD
308 if (!done)
309 {
310 if (data->ftw.base == 1)
311 {
50304ef0 312 if (__chdir ("/") < 0)
d951286f
UD
313 result = -1;
314 }
76b87c03 315 else
d951286f
UD
316 {
317 /* Please note that we overwrite a slash. */
318 data->dirbuf[data->ftw.base - 1] = '\0';
319
50304ef0 320 if (__chdir (data->dirbuf) < 0)
d951286f
UD
321 result = -1;
322
323 data->dirbuf[data->ftw.base - 1] = '/';
324 }
76b87c03
UD
325 }
326 }
28f540f4
RM
327 }
328 }
eb7c2001
UD
329 else
330 result = (*data->func) (data->dirbuf, &st, data->cvt_arr[flag],
331 &data->ftw);
76b87c03
UD
332 }
333
334 return result;
335}
336
337
338static int
dfd2257a
UD
339internal_function
340ftw_dir (struct ftw_data *data, struct STAT *st)
76b87c03
UD
341{
342 struct dir_data dir;
dfd2257a 343 struct DIRENT *d;
76b87c03 344 int previous_base = data->ftw.base;
d951286f 345 int result;
76b87c03
UD
346 char *startp;
347
d951286f
UD
348 /* Open the stream for this directory. This might require that
349 another stream has to be closed. */
350 result = open_dir_stream (data, &dir);
351 if (result != 0)
352 {
353 if (errno == EACCES)
354 /* We cannot read the directory. Signal this with a special flag. */
355 result = (*data->func) (data->dirbuf, st, FTW_DNR, &data->ftw);
356
357 return result;
358 }
359
76b87c03
UD
360 /* First, report the directory (if not depth-first). */
361 if (!(data->flags & FTW_DEPTH))
362 {
d951286f 363 result = (*data->func) (data->dirbuf, st, FTW_D, &data->ftw);
76b87c03
UD
364 if (result != 0)
365 return result;
366 }
28f540f4 367
76b87c03
UD
368 /* If necessary, change to this directory. */
369 if (data->flags & FTW_CHDIR)
370 {
b13927da 371 if (__fchdir (dirfd (dir.stream)) < 0)
28f540f4 372 {
76b87c03 373 if (errno == ENOSYS)
28f540f4 374 {
50304ef0 375 if (__chdir (data->dirbuf) < 0)
76b87c03 376 result = -1;
28f540f4 377 }
76b87c03
UD
378 else
379 result = -1;
28f540f4
RM
380 }
381
76b87c03
UD
382 if (result != 0)
383 {
384 int save_err = errno;
50304ef0 385 __closedir (dir.stream);
76b87c03
UD
386 __set_errno (save_err);
387
388 if (data->actdir-- == 0)
389 data->actdir = data->maxdir - 1;
390 data->dirstreams[data->actdir] = NULL;
391
392 return result;
393 }
28f540f4
RM
394 }
395
76b87c03
UD
396 /* Next, update the `struct FTW' information. */
397 ++data->ftw.level;
398 startp = strchr (data->dirbuf, '\0');
25f227b9
UD
399 /* There always must be a directory name. */
400 assert (startp != data->dirbuf);
401 if (startp != data->dirbuf + 1)
402 *startp++ = '/';
76b87c03 403 data->ftw.base = startp - data->dirbuf;
28f540f4 404
dfd2257a 405 while (dir.stream != NULL && (d = READDIR (dir.stream)) != NULL)
76b87c03
UD
406 {
407 result = process_entry (data, &dir, d->d_name, _D_EXACT_NAMLEN (d));
408 if (result != 0)
409 break;
410 }
28f540f4 411
76b87c03 412 if (dir.stream != NULL)
28f540f4 413 {
76b87c03
UD
414 /* The stream is still open. I.e., we did not need more
415 descriptors. Simply close the stream now. */
416 int save_err = errno;
417
418 assert (dir.content == NULL);
419
50304ef0 420 __closedir (dir.stream);
76b87c03
UD
421 __set_errno (save_err);
422
423 if (data->actdir-- == 0)
424 data->actdir = data->maxdir - 1;
425 data->dirstreams[data->actdir] = NULL;
28f540f4 426 }
76b87c03 427 else
28f540f4 428 {
76b87c03
UD
429 int save_err;
430 char *runp = dir.content;
431
3d73829c 432 while (result == 0 && *runp != '\0')
28f540f4 433 {
76b87c03
UD
434 char *endp = strchr (runp, '\0');
435
436 result = process_entry (data, &dir, runp, endp - runp);
76b87c03
UD
437
438 runp = endp + 1;
28f540f4 439 }
76b87c03
UD
440
441 save_err = errno;
442 free (dir.content);
443 __set_errno (save_err);
28f540f4 444 }
28f540f4 445
76b87c03 446 /* Prepare the return, revert the `struct FTW' information. */
d951286f 447 data->dirbuf[data->ftw.base - 1] = '\0';
76b87c03
UD
448 --data->ftw.level;
449 data->ftw.base = previous_base;
450
451 /* Finally, if we process depth-first report the directory. */
452 if (result == 0 && (data->flags & FTW_DEPTH))
d951286f 453 result = (*data->func) (data->dirbuf, st, FTW_DP, &data->ftw);
28f540f4 454
76b87c03
UD
455 return result;
456}
457
458
459static int
dfd2257a 460internal_function
76b87c03
UD
461ftw_startup (const char *dir, int is_nftw, void *func, int descriptors,
462 int flags)
463{
464 struct ftw_data data;
dfd2257a 465 struct STAT st;
76b87c03
UD
466 int result = 0;
467 int save_err;
d951286f 468 char *cwd = NULL;
76b87c03
UD
469 char *cp;
470
471 /* First make sure the parameters are reasonable. */
472 if (dir[0] == '\0')
473 {
a7f775a5 474 __set_errno (ENOENT);
76b87c03
UD
475 return -1;
476 }
28f540f4 477
76b87c03
UD
478 data.maxdir = descriptors < 1 ? 1 : descriptors;
479 data.actdir = 0;
480 data.dirstreams = (struct dir_data **) alloca (data.maxdir
481 * sizeof (struct dir_data *));
482 memset (data.dirstreams, '\0', data.maxdir * sizeof (struct dir_data *));
483
ae1025be 484#ifdef PATH_MAX
76b87c03 485 data.dirbufsize = MAX (2 * strlen (dir), PATH_MAX);
ae1025be
UD
486#else
487 data.dirbufsize = 2 * strlen (dir);
488#endif
76b87c03
UD
489 data.dirbuf = (char *) malloc (data.dirbufsize);
490 if (data.dirbuf == NULL)
491 return -1;
d951286f 492 cp = __stpcpy (data.dirbuf, dir);
76b87c03
UD
493 /* Strip trailing slashes. */
494 while (cp > data.dirbuf + 1 && cp[-1] == '/')
495 --cp;
496 *cp = '\0';
497
498 data.ftw.level = 0;
499
500 /* Find basename. */
501 while (cp > data.dirbuf && cp[-1] != '/')
502 --cp;
503 data.ftw.base = cp - data.dirbuf;
504
505 data.flags = flags;
506
507 /* This assignment might seem to be strange but it is what we want.
508 The trick is that the first three arguments to the `ftw' and
509 `nftw' callback functions are equal. Therefore we can call in
510 every case the callback using the format of the `nftw' version
511 and get the correct result since the stack layout for a function
512 call in C allows this. */
dfd2257a 513 data.func = (NFTW_FUNC_T) func;
76b87c03
UD
514
515 /* Since we internally use the complete set of FTW_* values we need
516 to reduce the value range before calling a `ftw' callback. */
517 data.cvt_arr = is_nftw ? nftw_arr : ftw_arr;
518
d951286f
UD
519 /* No object known so far. */
520 data.known_objects = NULL;
521
76b87c03
UD
522 /* Now go to the directory containing the initial file/directory. */
523 if ((flags & FTW_CHDIR) && data.ftw.base > 0)
28f540f4 524 {
76b87c03 525 /* GNU extension ahead. */
50304ef0 526 cwd = __getcwd (NULL, 0);
76b87c03
UD
527 if (cwd == NULL)
528 result = -1;
529 else
28f540f4 530 {
76b87c03
UD
531 /* Change to the directory the file is in. In data.dirbuf
532 we have a writable copy of the file name. Just NUL
533 terminate it for now and change the directory. */
534 if (data.ftw.base == 1)
535 /* I.e., the file is in the root directory. */
50304ef0 536 result = __chdir ("/");
76b87c03
UD
537 else
538 {
539 char ch = data.dirbuf[data.ftw.base - 1];
540 data.dirbuf[data.ftw.base - 1] = '\0';
50304ef0 541 result = __chdir (data.dirbuf);
76b87c03
UD
542 data.dirbuf[data.ftw.base - 1] = ch;
543 }
28f540f4
RM
544 }
545 }
546
76b87c03
UD
547 /* Get stat info for start directory. */
548 if (result == 0)
6e4c40ba
UD
549 {
550 if (((flags & FTW_PHYS)
551 ? LXSTAT (_STAT_VER, data.dirbuf, &st)
552 : XSTAT (_STAT_VER, data.dirbuf, &st)) < 0)
553 {
554 if (errno == EACCES)
555 result = (*data.func) (data.dirbuf, &st, FTW_NS, &data.ftw);
556 else if (!(flags & FTW_PHYS)
557 && errno == ENOENT
558 && LXSTAT (_STAT_VER, dir, &st) == 0
559 && S_ISLNK (st.st_mode))
560 result = (*data.func) (data.dirbuf, &st, data.cvt_arr[FTW_SLN],
76b87c03 561 &data.ftw);
6e4c40ba
UD
562 else
563 /* No need to call the callback since we cannot say anything
564 about the object. */
565 result = -1;
566 }
567 else
568 {
569 if (S_ISDIR (st.st_mode))
570 {
571 /* Remember the device of the initial directory in case
572 FTW_MOUNT is given. */
573 data.dev = st.st_dev;
574
575 /* We know this directory now. */
576 if (!(flags & FTW_PHYS))
577 result = add_object (&data, &st);
578
579 if (result == 0)
580 result = ftw_dir (&data, &st);
581 }
582 else
583 {
584 int flag = S_ISLNK (st.st_mode) ? FTW_SL : FTW_F;
585
586 result = (*data.func) (data.dirbuf, &st, data.cvt_arr[flag],
587 &data.ftw);
588 }
589 }
590 }
76b87c03
UD
591
592 /* Return to the start directory (if necessary). */
593 if (cwd != NULL)
594 {
595 int save_err = errno;
50304ef0 596 __chdir (cwd);
76b87c03
UD
597 free (cwd);
598 __set_errno (save_err);
599 }
600
601 /* Free all memory. */
602 save_err = errno;
d951286f 603 __tdestroy (data.known_objects, free);
76b87c03
UD
604 free (data.dirbuf);
605 __set_errno (save_err);
606
607 return result;
608}
609
610
611
612/* Entry points. */
613
614int
dfd2257a 615FTW_NAME (path, func, descriptors)
76b87c03 616 const char *path;
dfd2257a 617 FTW_FUNC_T func;
76b87c03
UD
618 int descriptors;
619{
620 return ftw_startup (path, 0, func, descriptors, 0);
621}
622
623int
dfd2257a 624NFTW_NAME (path, func, descriptors, flags)
76b87c03 625 const char *path;
dfd2257a 626 NFTW_FUNC_T func;
76b87c03
UD
627 int descriptors;
628 int flags;
629{
630 return ftw_startup (path, 1, func, descriptors, flags);
28f540f4 631}