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>
29 #include <include/sys/stat.h>
31 /* #define NDEBUG 1 */
34 /* Support for the LFS API version. */
37 # define NFTW_NAME nftw
40 # define DIRENT dirent
41 # define READDIR readdir
42 # define LXSTAT __lxstat
43 # define XSTAT __xstat
44 # define FTW_FUNC_T __ftw_func_t
45 # define NFTW_FUNC_T __nftw_func_t
62 /* Array with pointers to open directory streams. */
63 struct dir_data
**dirstreams
;
67 /* Buffer containing name of currently processed object. */
71 /* Passed as fourth argument to `nftw' callback. The `base' member
72 tracks the content of the `dirbuf'. */
75 /* Flags passed to `nftw' function. 0 for `ftw'. */
78 /* Conversion array for flag values. It is the identity mapping for
79 `nftw' calls, otherwise it maps the values to those know by
83 /* Callback function. We always use the `nftw' form. */
86 /* Device of starting point. Needed for FTW_MOUNT. */
89 /* Data structure for keeping fingerprints of already processed
90 object. This is needed when not using FTW_PHYS. */
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
98 static int nftw_arr
[] =
100 FTW_F
, FTW_D
, FTW_DNR
, FTW_NS
, FTW_SL
, FTW_DP
, FTW_SLN
103 static int ftw_arr
[] =
105 FTW_F
, FTW_D
, FTW_DNR
, FTW_NS
, FTW_F
, FTW_D
, FTW_NS
109 /* Forward declarations of local functions. */
110 static int ftw_dir (struct ftw_data
*data
, struct STAT
*st
) internal_function
;
114 object_compare (const void *p1
, const void *p2
)
116 /* We don't need a sophisticated and useful comparison. We are only
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
;
121 cmp1
= (kp1
->dev
> kp2
->dev
) - (kp1
->dev
< kp2
->dev
);
124 return (kp1
->ino
> kp2
->ino
) - (kp1
->ino
< kp2
->ino
);
129 add_object (struct ftw_data
*data
, struct STAT
*st
)
131 struct known_object
*newp
= malloc (sizeof (struct known_object
));
134 newp
->dev
= st
->st_dev
;
135 newp
->ino
= st
->st_ino
;
136 return __tsearch (newp
, &data
->known_objects
, object_compare
) ? 0 : -1;
141 find_object (struct ftw_data
*data
, struct STAT
*st
)
143 struct known_object obj
= { dev
: st
->st_dev
, ino
: st
->st_ino
};
144 return __tfind (&obj
, &data
->known_objects
, object_compare
) != NULL
;
149 open_dir_stream (struct ftw_data
*data
, struct dir_data
*dirp
)
153 if (data
->dirstreams
[data
->actdir
] != NULL
)
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
);
165 DIR *st
= data
->dirstreams
[data
->actdir
]->stream
;
169 while ((d
= READDIR (st
)) != NULL
)
171 size_t this_len
= _D_EXACT_NAMLEN (d
);
172 if (actsize
+ this_len
+ 2 >= bufsize
)
175 bufsize
+= MAX (1024, 2 * this_len
);
176 newp
= realloc (buf
, bufsize
);
179 /* No more memory. */
180 int save_err
= errno
;
182 __set_errno (save_err
);
189 *((char *) __mempcpy (buf
+ actsize
, d
->d_name
, this_len
))
191 actsize
+= this_len
+ 1;
194 /* Terminate the list with an additional NUL byte. */
195 buf
[actsize
++] = '\0';
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
)
201 int save_err
= errno
;
203 __set_errno (save_err
);
209 data
->dirstreams
[data
->actdir
]->stream
= NULL
;
210 data
->dirstreams
[data
->actdir
] = NULL
;
215 /* Open the new stream. */
218 assert (data
->dirstreams
[data
->actdir
] == NULL
);
220 dirp
->stream
= opendir (data
->dirbuf
);
221 if (dirp
->stream
== NULL
)
225 dirp
->content
= NULL
;
226 data
->dirstreams
[data
->actdir
] = dirp
;
228 if (++data
->actdir
== data
->maxdir
)
238 process_entry (struct ftw_data
*data
, struct dir_data
*dir
, const char *name
,
245 if (name
[0] == '.' && (name
[1] == '\0'
246 || (name
[1] == '.' && name
[2] == '\0')))
247 /* Don't process the "." and ".." entries. */
250 if (data
->dirbufsize
< data
->ftw
.base
+ namlen
+ 2)
252 /* Enlarge the buffer. */
255 data
->dirbufsize
*= 2;
256 newp
= realloc (data
->dirbuf
, data
->dirbufsize
);
262 *((char *) __mempcpy (data
->dirbuf
+ data
->ftw
.base
, name
, namlen
)) = '\0';
264 if (((data
->flags
& FTW_PHYS
)
265 ? LXSTAT (_STAT_VER
, data
->dirbuf
, &st
)
266 : XSTAT (_STAT_VER
, data
->dirbuf
, &st
)) < 0)
268 if (errno
!= EACCES
&& errno
!= ENOENT
)
270 else if (!(data
->flags
& FTW_PHYS
)
271 && LXSTAT (_STAT_VER
, data
->dirbuf
, &st
) == 0
272 && S_ISLNK (st
.st_mode
))
279 if (S_ISDIR (st
.st_mode
))
281 else if (S_ISLNK (st
.st_mode
))
289 || !(data
->flags
& FTW_MOUNT
) || st
.st_dev
== data
->dev
))
291 if ((data
->flags
& FTW_PHYS
) || flag
== FTW_NS
292 || (!find_object (data
, &st
)
293 /* Remember the object. */
294 && (result
= add_object (data
, &st
)) == 0))
298 result
= ftw_dir (data
, &st
);
300 if (result
== 0 && (data
->flags
& FTW_CHDIR
))
302 /* Change back to current directory. */
304 if (dir
->stream
!= NULL
)
305 if (__fchdir (dirfd (dir
->stream
)) == 0)
310 if (data
->ftw
.base
== 1)
317 /* Please note that we overwrite a slash. */
318 data
->dirbuf
[data
->ftw
.base
- 1] = '\0';
320 if (chdir (data
->dirbuf
) < 0)
323 data
->dirbuf
[data
->ftw
.base
- 1] = '/';
329 result
= (*data
->func
) (data
->dirbuf
, &st
, data
->cvt_arr
[flag
],
340 ftw_dir (struct ftw_data
*data
, struct STAT
*st
)
344 int previous_base
= data
->ftw
.base
;
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
);
354 /* We cannot read the directory. Signal this with a special flag. */
355 result
= (*data
->func
) (data
->dirbuf
, st
, FTW_DNR
, &data
->ftw
);
360 /* First, report the directory (if not depth-first). */
361 if (!(data
->flags
& FTW_DEPTH
))
363 result
= (*data
->func
) (data
->dirbuf
, st
, FTW_D
, &data
->ftw
);
368 /* If necessary, change to this directory. */
369 if (data
->flags
& FTW_CHDIR
)
371 if (__fchdir (dirfd (dir
.stream
)) < 0)
375 if (chdir (data
->dirbuf
) < 0)
384 int save_err
= errno
;
385 closedir (dir
.stream
);
386 __set_errno (save_err
);
388 if (data
->actdir
-- == 0)
389 data
->actdir
= data
->maxdir
- 1;
390 data
->dirstreams
[data
->actdir
] = NULL
;
396 /* Next, update the `struct FTW' information. */
398 startp
= strchr (data
->dirbuf
, '\0');
400 data
->ftw
.base
= startp
- data
->dirbuf
;
402 while (dir
.stream
!= NULL
&& (d
= READDIR (dir
.stream
)) != NULL
)
404 result
= process_entry (data
, &dir
, d
->d_name
, _D_EXACT_NAMLEN (d
));
409 if (dir
.stream
!= NULL
)
411 /* The stream is still open. I.e., we did not need more
412 descriptors. Simply close the stream now. */
413 int save_err
= errno
;
415 assert (dir
.content
== NULL
);
417 closedir (dir
.stream
);
418 __set_errno (save_err
);
420 if (data
->actdir
-- == 0)
421 data
->actdir
= data
->maxdir
- 1;
422 data
->dirstreams
[data
->actdir
] = NULL
;
427 char *runp
= dir
.content
;
429 assert (result
== 0);
431 while (*runp
!= '\0')
433 char *endp
= strchr (runp
, '\0');
435 result
= process_entry (data
, &dir
, runp
, endp
- runp
);
444 __set_errno (save_err
);
447 /* Prepare the return, revert the `struct FTW' information. */
448 data
->dirbuf
[data
->ftw
.base
- 1] = '\0';
450 data
->ftw
.base
= previous_base
;
452 /* Finally, if we process depth-first report the directory. */
453 if (result
== 0 && (data
->flags
& FTW_DEPTH
))
454 result
= (*data
->func
) (data
->dirbuf
, st
, FTW_DP
, &data
->ftw
);
462 ftw_startup (const char *dir
, int is_nftw
, void *func
, int descriptors
,
465 struct ftw_data data
;
472 /* First make sure the parameters are reasonable. */
475 __set_errno (ENOTDIR
);
479 data
.maxdir
= descriptors
< 1 ? 1 : descriptors
;
481 data
.dirstreams
= (struct dir_data
**) alloca (data
.maxdir
482 * sizeof (struct dir_data
*));
483 memset (data
.dirstreams
, '\0', data
.maxdir
* sizeof (struct dir_data
*));
486 data
.dirbufsize
= MAX (2 * strlen (dir
), PATH_MAX
);
488 data
.dirbufsize
= 2 * strlen (dir
);
490 data
.dirbuf
= (char *) malloc (data
.dirbufsize
);
491 if (data
.dirbuf
== NULL
)
493 cp
= __stpcpy (data
.dirbuf
, dir
);
494 /* Strip trailing slashes. */
495 while (cp
> data
.dirbuf
+ 1 && cp
[-1] == '/')
502 while (cp
> data
.dirbuf
&& cp
[-1] != '/')
504 data
.ftw
.base
= cp
- data
.dirbuf
;
508 /* This assignment might seem to be strange but it is what we want.
509 The trick is that the first three arguments to the `ftw' and
510 `nftw' callback functions are equal. Therefore we can call in
511 every case the callback using the format of the `nftw' version
512 and get the correct result since the stack layout for a function
513 call in C allows this. */
514 data
.func
= (NFTW_FUNC_T
) func
;
516 /* Since we internally use the complete set of FTW_* values we need
517 to reduce the value range before calling a `ftw' callback. */
518 data
.cvt_arr
= is_nftw
? nftw_arr
: ftw_arr
;
520 /* No object known so far. */
521 data
.known_objects
= NULL
;
523 /* Now go to the directory containing the initial file/directory. */
524 if ((flags
& FTW_CHDIR
) && data
.ftw
.base
> 0)
526 /* GNU extension ahead. */
527 cwd
= getcwd (NULL
, 0);
532 /* Change to the directory the file is in. In data.dirbuf
533 we have a writable copy of the file name. Just NUL
534 terminate it for now and change the directory. */
535 if (data
.ftw
.base
== 1)
536 /* I.e., the file is in the root directory. */
537 result
= chdir ("/");
540 char ch
= data
.dirbuf
[data
.ftw
.base
- 1];
541 data
.dirbuf
[data
.ftw
.base
- 1] = '\0';
542 result
= chdir (data
.dirbuf
);
543 data
.dirbuf
[data
.ftw
.base
- 1] = ch
;
548 /* Get stat info for start directory. */
550 if (((flags
& FTW_PHYS
)
551 ? LXSTAT (_STAT_VER
, data
.dirbuf
, &st
)
552 : XSTAT (_STAT_VER
, data
.dirbuf
, &st
)) < 0)
555 result
= (*data
.func
) (data
.dirbuf
, &st
, FTW_NS
, &data
.ftw
);
556 else if (!(flags
& FTW_PHYS
)
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
],
563 /* No need to call the callback since we cannot say anything
569 if (S_ISDIR (st
.st_mode
))
571 /* Remember the device of the initial directory in case
572 FTW_MOUNT is given. */
573 data
.dev
= st
.st_dev
;
575 /* We know this directory now. */
576 if (!(flags
& FTW_PHYS
))
577 result
= add_object (&data
, &st
);
580 result
= ftw_dir (&data
, &st
);
584 int flag
= S_ISLNK (st
.st_mode
) ? FTW_SL
: FTW_F
;
586 result
= (*data
.func
) (data
.dirbuf
, &st
, data
.cvt_arr
[flag
],
591 /* Return to the start directory (if necessary). */
594 int save_err
= errno
;
597 __set_errno (save_err
);
600 /* Free all memory. */
602 __tdestroy (data
.known_objects
, free
);
604 __set_errno (save_err
);
614 FTW_NAME (path
, func
, descriptors
)
619 return ftw_startup (path
, 0, func
, descriptors
, 0);
623 NFTW_NAME (path
, func
, descriptors
, flags
)
629 return ftw_startup (path
, 1, func
, descriptors
, flags
);