1 /* File tree walker functions.
2 Copyright (C) 1996, 1997 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. */
28 #include <sys/param.h>
31 /* #define NDEBUG 1 */
49 /* Array with pointers to open directory streams. */
50 struct dir_data
**dirstreams
;
54 /* Buffer containing name of currently processed object. */
58 /* Passed as fourth argument to `nftw' callback. The `base' member
59 tracks the content of the `dirbuf'. */
62 /* Flags passed to `nftw' function. 0 for `ftw'. */
65 /* Conversion array for flag values. It is the identity mapping for
66 `nftw' calls, otherwise it maps the values to those know by
70 /* Callback function. We always use the `nftw' form. */
73 /* Device of starting point. Needed for FTW_MOUNT. */
76 /* Data structure for keeping fingerprints of already processed
77 object. This is needed when not using FTW_PHYS. */
82 /* Internally we use the FTW_* constants used for `nftw'. When the
83 process called `ftw' we must reduce the flag to the known flags
85 static int nftw_arr
[] =
87 FTW_F
, FTW_D
, FTW_DNR
, FTW_NS
, FTW_SL
, FTW_DP
, FTW_SLN
90 static int ftw_arr
[] =
92 FTW_F
, FTW_D
, FTW_DNR
, FTW_NS
, FTW_F
, FTW_D
, FTW_NS
96 /* Forward declarations of local functions. */
97 static int ftw_dir (struct ftw_data
*data
, struct stat
*st
);
101 object_compare (const void *p1
, const void *p2
)
103 /* We don't need a sophisticated and useful comparison. We are only
104 interested in equality. */
105 return memcmp (p1
, p2
, sizeof (struct known_object
));
110 add_object (struct ftw_data
*data
, struct stat
*st
)
112 struct known_object
*newp
= malloc (sizeof (struct known_object
));
115 newp
->dev
= st
->st_dev
;
116 newp
->ino
= st
->st_ino
;
117 return __tsearch (newp
, &data
->known_objects
, object_compare
) ? 0 : -1;
122 find_object (struct ftw_data
*data
, struct stat
*st
)
124 struct known_object obj
= { dev
: st
->st_dev
, ino
: st
->st_ino
};
125 return __tfind (&obj
, &data
->known_objects
, object_compare
) != NULL
;
130 open_dir_stream (struct ftw_data
*data
, struct dir_data
*dirp
)
134 if (data
->dirstreams
[data
->actdir
] != NULL
)
136 /* Oh, oh. We must close this stream. Get all remaining
137 entries and store them as a list in the `content' member of
138 the `struct dir_data' variable. */
139 size_t bufsize
= 1024;
140 char *buf
= malloc (bufsize
);
146 DIR *st
= data
->dirstreams
[data
->actdir
]->stream
;
150 while ((d
= readdir (st
)) != NULL
)
152 size_t this_len
= _D_EXACT_NAMLEN (d
);
153 if (actsize
+ this_len
+ 2 >= bufsize
)
156 bufsize
+= MAX (1024, 2 * this_len
);
157 newp
= realloc (buf
, bufsize
);
160 /* No more memory. */
161 int save_err
= errno
;
163 __set_errno (save_err
);
170 memcpy (buf
+ actsize
, d
->d_name
, this_len
);
172 buf
[actsize
++] = '\0';
175 /* Terminate the list with an additional NUL byte. */
176 buf
[actsize
++] = '\0';
178 /* Shrink the buffer to what we actually need. */
179 data
->dirstreams
[data
->actdir
]->content
= realloc (buf
, actsize
);
180 if (data
->dirstreams
[data
->actdir
]->content
== NULL
)
182 int save_err
= errno
;
184 __set_errno (save_err
);
190 data
->dirstreams
[data
->actdir
]->stream
= NULL
;
191 data
->dirstreams
[data
->actdir
] = NULL
;
196 /* Open the new stream. */
199 assert (data
->dirstreams
[data
->actdir
] == NULL
);
201 dirp
->stream
= opendir (data
->dirbuf
);
202 if (dirp
->stream
== NULL
)
206 dirp
->content
= NULL
;
207 data
->dirstreams
[data
->actdir
] = dirp
;
209 if (++data
->actdir
== data
->maxdir
)
219 process_entry (struct ftw_data
*data
, struct dir_data
*dir
, const char *name
,
226 if (name
[0] == '.' && (name
[1] == '\0'
227 || (name
[1] == '.' && name
[2] == '\0')))
228 /* Don't process the "." and ".." entries. */
231 if (data
->dirbufsize
< data
->ftw
.base
+ namlen
+ 2)
233 /* Enlarge the buffer. */
236 data
->dirbufsize
*= 2;
237 newp
= realloc (data
->dirbuf
, data
->dirbufsize
);
243 memcpy (data
->dirbuf
+ data
->ftw
.base
, name
, namlen
);
244 data
->dirbuf
[data
->ftw
.base
+ namlen
] = '\0';
246 if (((data
->flags
& FTW_PHYS
)
247 ? __lxstat (_STAT_VER
, data
->dirbuf
, &st
)
248 : __xstat (_STAT_VER
, data
->dirbuf
, &st
)) < 0)
250 if (errno
!= EACCES
&& errno
!= ENOENT
)
252 else if (!(data
->flags
& FTW_PHYS
)
253 && __lxstat (_STAT_VER
, data
->dirbuf
, &st
) == 0
254 && S_ISLNK (st
.st_mode
))
261 if (S_ISDIR (st
.st_mode
))
263 else if (S_ISLNK (st
.st_mode
))
271 || !(data
->flags
& FTW_MOUNT
) || st
.st_dev
== data
->dev
))
273 if ((data
->flags
& FTW_PHYS
) || flag
== FTW_NS
274 || (!find_object (data
, &st
)
275 /* Remember the object. */
276 && (result
= add_object (data
, &st
)) == 0))
280 result
= ftw_dir (data
, &st
);
282 if (result
== 0 && (data
->flags
& FTW_CHDIR
))
284 /* Change back to current directory. */
286 if (dir
->stream
!= NULL
)
287 if (__fchdir (dirfd (dir
->stream
)) == 0)
292 if (data
->ftw
.base
== 1)
299 /* Please note that we overwrite a slash. */
300 data
->dirbuf
[data
->ftw
.base
- 1] = '\0';
302 if (chdir (data
->dirbuf
) < 0)
305 data
->dirbuf
[data
->ftw
.base
- 1] = '/';
311 result
= (*data
->func
) (data
->dirbuf
, &st
, data
->cvt_arr
[flag
],
321 ftw_dir (struct ftw_data
*data
, struct stat
*st
)
325 int previous_base
= data
->ftw
.base
;
329 /* Open the stream for this directory. This might require that
330 another stream has to be closed. */
331 result
= open_dir_stream (data
, &dir
);
335 /* We cannot read the directory. Signal this with a special flag. */
336 result
= (*data
->func
) (data
->dirbuf
, st
, FTW_DNR
, &data
->ftw
);
341 /* First, report the directory (if not depth-first). */
342 if (!(data
->flags
& FTW_DEPTH
))
344 result
= (*data
->func
) (data
->dirbuf
, st
, FTW_D
, &data
->ftw
);
349 /* If necessary, change to this directory. */
350 if (data
->flags
& FTW_CHDIR
)
352 if (__fchdir (dirfd (dir
.stream
)) < 0)
356 if (chdir (data
->dirbuf
) < 0)
365 int save_err
= errno
;
366 closedir (dir
.stream
);
367 __set_errno (save_err
);
369 if (data
->actdir
-- == 0)
370 data
->actdir
= data
->maxdir
- 1;
371 data
->dirstreams
[data
->actdir
] = NULL
;
377 /* Next, update the `struct FTW' information. */
379 startp
= strchr (data
->dirbuf
, '\0');
381 data
->ftw
.base
= startp
- data
->dirbuf
;
383 while (dir
.stream
!= NULL
&& (d
= readdir (dir
.stream
)) != NULL
)
385 result
= process_entry (data
, &dir
, d
->d_name
, _D_EXACT_NAMLEN (d
));
390 if (dir
.stream
!= NULL
)
392 /* The stream is still open. I.e., we did not need more
393 descriptors. Simply close the stream now. */
394 int save_err
= errno
;
396 assert (dir
.content
== NULL
);
398 closedir (dir
.stream
);
399 __set_errno (save_err
);
401 if (data
->actdir
-- == 0)
402 data
->actdir
= data
->maxdir
- 1;
403 data
->dirstreams
[data
->actdir
] = NULL
;
408 char *runp
= dir
.content
;
410 assert (result
== 0);
412 while (*runp
!= '\0')
414 char *endp
= strchr (runp
, '\0');
416 result
= process_entry (data
, &dir
, runp
, endp
- runp
);
425 __set_errno (save_err
);
428 /* Prepare the return, revert the `struct FTW' information. */
429 data
->dirbuf
[data
->ftw
.base
- 1] = '\0';
431 data
->ftw
.base
= previous_base
;
433 /* Finally, if we process depth-first report the directory. */
434 if (result
== 0 && (data
->flags
& FTW_DEPTH
))
435 result
= (*data
->func
) (data
->dirbuf
, st
, FTW_DP
, &data
->ftw
);
442 ftw_startup (const char *dir
, int is_nftw
, void *func
, int descriptors
,
445 struct ftw_data data
;
452 /* First make sure the parameters are reasonable. */
455 __set_errno (ENOTDIR
);
459 data
.maxdir
= descriptors
< 1 ? 1 : descriptors
;
461 data
.dirstreams
= (struct dir_data
**) alloca (data
.maxdir
462 * sizeof (struct dir_data
*));
463 memset (data
.dirstreams
, '\0', data
.maxdir
* sizeof (struct dir_data
*));
465 data
.dirbufsize
= MAX (2 * strlen (dir
), PATH_MAX
);
466 data
.dirbuf
= (char *) malloc (data
.dirbufsize
);
467 if (data
.dirbuf
== NULL
)
469 cp
= __stpcpy (data
.dirbuf
, dir
);
470 /* Strip trailing slashes. */
471 while (cp
> data
.dirbuf
+ 1 && cp
[-1] == '/')
478 while (cp
> data
.dirbuf
&& cp
[-1] != '/')
480 data
.ftw
.base
= cp
- data
.dirbuf
;
484 /* This assignment might seem to be strange but it is what we want.
485 The trick is that the first three arguments to the `ftw' and
486 `nftw' callback functions are equal. Therefore we can call in
487 every case the callback using the format of the `nftw' version
488 and get the correct result since the stack layout for a function
489 call in C allows this. */
490 data
.func
= (__nftw_func_t
) func
;
492 /* Since we internally use the complete set of FTW_* values we need
493 to reduce the value range before calling a `ftw' callback. */
494 data
.cvt_arr
= is_nftw
? nftw_arr
: ftw_arr
;
496 /* No object known so far. */
497 data
.known_objects
= NULL
;
499 /* Now go to the directory containing the initial file/directory. */
500 if ((flags
& FTW_CHDIR
) && data
.ftw
.base
> 0)
502 /* GNU extension ahead. */
503 cwd
= getcwd (NULL
, 0);
508 /* Change to the directory the file is in. In data.dirbuf
509 we have a writable copy of the file name. Just NUL
510 terminate it for now and change the directory. */
511 if (data
.ftw
.base
== 1)
512 /* I.e., the file is in the root directory. */
513 result
= chdir ("/");
516 char ch
= data
.dirbuf
[data
.ftw
.base
- 1];
517 data
.dirbuf
[data
.ftw
.base
- 1] = '\0';
518 result
= chdir (data
.dirbuf
);
519 data
.dirbuf
[data
.ftw
.base
- 1] = ch
;
524 /* Get stat info for start directory. */
526 if (((flags
& FTW_PHYS
)
527 ? __lxstat (_STAT_VER
, data
.dirbuf
, &st
)
528 : __xstat (_STAT_VER
, data
.dirbuf
, &st
)) < 0)
531 result
= (*data
.func
) (data
.dirbuf
, &st
, FTW_NS
, &data
.ftw
);
532 else if (!(flags
& FTW_PHYS
)
534 && __lxstat (_STAT_VER
, dir
, &st
) == 0
535 && S_ISLNK (st
.st_mode
))
536 result
= (*data
.func
) (data
.dirbuf
, &st
, data
.cvt_arr
[FTW_SLN
],
539 /* No need to call the callback since we cannot say anything
545 if (S_ISDIR (st
.st_mode
))
547 /* Remember the device of the initial directory in case
548 FTW_MOUNT is given. */
549 data
.dev
= st
.st_dev
;
551 /* We know this directory now. */
552 if (!(flags
& FTW_PHYS
))
553 result
= add_object (&data
, &st
);
556 result
= ftw_dir (&data
, &st
);
560 int flag
= S_ISLNK (st
.st_mode
) ? FTW_SL
: FTW_F
;
562 result
= (*data
.func
) (data
.dirbuf
, &st
, data
.cvt_arr
[flag
],
567 /* Return to the start directory (if necessary). */
570 int save_err
= errno
;
573 __set_errno (save_err
);
576 /* Free all memory. */
578 __tdestroy (data
.known_objects
, free
);
580 __set_errno (save_err
);
590 ftw (path
, func
, descriptors
)
595 return ftw_startup (path
, 0, func
, descriptors
, 0);
599 nftw (path
, func
, descriptors
, flags
)
605 return ftw_startup (path
, 1, func
, descriptors
, flags
);