5 use warnings::register;
11 # Modified to ensure sub-directory traversal order is not inverted by stack
12 # push and pops. That is remains in the same order as in the directory file,
13 # or user pre-processing (EG:sorted).
18 File::Find - Traverse a directory tree.
23 find(\&wanted, @directories_to_search);
27 finddepth(\&wanted, @directories_to_search);
31 find({ wanted => \&process, follow => 1 }, '.');
35 These are functions for searching through directory trees doing work
36 on each file found similar to the Unix I<find> command. File::Find
37 exports two functions, C<find> and C<finddepth>. They work similarly
38 but have subtle differences.
44 find(\&wanted, @directories);
45 find(\%options, @directories);
47 C<find()> does a depth-first search over the given C<@directories> in
48 the order they are given. For each file or directory found, it calls
49 the C<&wanted> subroutine. (See below for details on how to use the
50 C<&wanted> function). Additionally, for each directory found, it will
51 C<chdir()> into that directory and continue the search, invoking the
52 C<&wanted> function on each file or subdirectory in the directory.
56 finddepth(\&wanted, @directories);
57 finddepth(\%options, @directories);
59 C<finddepth()> works just like C<find()> except that it invokes the
60 C<&wanted> function for a directory I<after> invoking it for the
61 directory's contents. It does a postorder traversal instead of a
62 preorder traversal, working from the bottom of the directory tree up
63 where C<find()> works from the top of the tree down.
69 The first argument to C<find()> is either a code reference to your
70 C<&wanted> function, or a hash reference describing the operations
71 to be performed for each file. The
72 code reference is described in L<The wanted function> below.
74 Here are the possible keys for the hash:
80 The value should be a code reference. This code reference is
81 described in L<The wanted function> below. The C<&wanted> subroutine is
86 Reports the name of a directory only AFTER all its entries
87 have been reported. Entry point C<finddepth()> is a shortcut for
88 specifying C<< { bydepth => 1 } >> in the first argument of C<find()>.
92 The value should be a code reference. This code reference is used to
93 preprocess the current directory. The name of the currently processed
94 directory is in C<$File::Find::dir>. Your preprocessing function is
95 called after C<readdir()>, but before the loop that calls the C<wanted()>
96 function. It is called with a list of strings (actually file/directory
97 names) and is expected to return a list of strings. The code can be
98 used to sort the file/directory names alphabetically, numerically,
99 or to filter out directory entries based on their name alone. When
100 I<follow> or I<follow_fast> are in effect, C<preprocess> is a no-op.
104 The value should be a code reference. It is invoked just before leaving
105 the currently processed directory. It is called in void context with no
106 arguments. The name of the current directory is in C<$File::Find::dir>. This
107 hook is handy for summarizing a directory, such as calculating its disk
108 usage. When I<follow> or I<follow_fast> are in effect, C<postprocess> is a
113 Causes symbolic links to be followed. Since directory trees with symbolic
114 links (followed) may contain files more than once and may even have
115 cycles, a hash has to be built up with an entry for each file.
116 This might be expensive both in space and time for a large
117 directory tree. See L</follow_fast> and L</follow_skip> below.
118 If either I<follow> or I<follow_fast> is in effect:
124 It is guaranteed that an I<lstat> has been called before the user's
125 C<wanted()> function is called. This enables fast file checks involving S<_>.
126 Note that this guarantee no longer holds if I<follow> or I<follow_fast>
131 There is a variable C<$File::Find::fullname> which holds the absolute
132 pathname of the file with all symbolic links resolved. If the link is
133 a dangling symbolic link, then fullname will be set to C<undef>.
137 This is a no-op on Win32.
141 This is similar to I<follow> except that it may report some files more
142 than once. It does detect cycles, however. Since only symbolic links
143 have to be hashed, this is much cheaper both in space and time. If
144 processing a file more than once (by the user's C<wanted()> function)
145 is worse than just taking time, the option I<follow> should be used.
147 This is also a no-op on Win32.
151 C<follow_skip==1>, which is the default, causes all files which are
152 neither directories nor symbolic links to be ignored if they are about
153 to be processed a second time. If a directory or a symbolic link
154 are about to be processed a second time, File::Find dies.
156 C<follow_skip==0> causes File::Find to die if any file is about to be
157 processed a second time.
159 C<follow_skip==2> causes File::Find to ignore any duplicate files and
160 directories but to proceed normally otherwise.
162 =item C<dangling_symlinks>
164 If true and a code reference, will be called with the symbolic link
165 name and the directory it lives in as arguments. Otherwise, if true
166 and warnings are on, warning "symbolic_link_name is a dangling
167 symbolic link\n" will be issued. If false, the dangling symbolic link
168 will be silently ignored.
172 Does not C<chdir()> to each directory as it recurses. The C<wanted()>
173 function will need to be aware of this, of course. In this case,
174 C<$_> will be the same as C<$File::Find::name>.
178 If find is used in taint-mode (-T command line switch or if EUID != UID
179 or if EGID != GID) then internally directory names have to be untainted
180 before they can be chdir'ed to. Therefore they are checked against a regular
181 expression I<untaint_pattern>. Note that all names passed to the user's
182 I<wanted()> function are still tainted. If this option is used while
183 not in taint-mode, C<untaint> is a no-op.
185 =item C<untaint_pattern>
187 See above. This should be set using the C<qr> quoting operator.
188 The default is set to C<qr|^([-+@\w./]+)$|>.
189 Note that the parentheses are vital.
191 =item C<untaint_skip>
193 If set, a directory which fails the I<untaint_pattern> is skipped,
194 including all its sub-directories. The default is to 'die' in such a case.
198 =head2 The wanted function
200 The C<wanted()> function does whatever verifications you want on
201 each file and directory. Note that despite its name, the C<wanted()>
202 function is a generic callback function, and does B<not> tell
203 File::Find if a file is "wanted" or not. In fact, its return value
206 The wanted function takes no arguments but rather does its work
207 through a collection of variables.
211 =item C<$File::Find::dir> is the current directory name,
213 =item C<$_> is the current filename within that directory
215 =item C<$File::Find::name> is the complete pathname to the file.
219 The above variables have all been localized and may be changed without
220 affecting data outside of the wanted function.
222 For example, when examining the file F</some/path/foo.ext> you will have:
224 $File::Find::dir = /some/path/
226 $File::Find::name = /some/path/foo.ext
228 You are chdir()'d to C<$File::Find::dir> when the function is called,
229 unless C<no_chdir> was specified. Note that when changing to
230 directories is in effect the root directory (F</>) is a somewhat
231 special case inasmuch as the concatenation of C<$File::Find::dir>,
232 C<'/'> and C<$_> is not literally equal to C<$File::Find::name>. The
233 table below summarizes all variants:
235 $File::Find::name $File::Find::dir $_
237 no_chdir=>0 /etc / etc
245 When C<follow> or C<follow_fast> are in effect, there is
246 also a C<$File::Find::fullname>. The function may set
247 C<$File::Find::prune> to prune the tree unless C<bydepth> was
248 specified. Unless C<follow> or C<follow_fast> is specified, for
249 compatibility reasons (find.pl, find2perl) there are in addition the
250 following globals available: C<$File::Find::topdir>,
251 C<$File::Find::topdev>, C<$File::Find::topino>,
252 C<$File::Find::topmode> and C<$File::Find::topnlink>.
254 This library is useful for the C<find2perl> tool, which when fed,
256 find2perl / -name .nfs\* -mtime +7 \
257 -exec rm -f {} \; -o -fstype nfs -prune
259 produces something like:
263 (($dev, $ino, $mode, $nlink, $uid, $gid) = lstat($_)) &&
267 ($nlink || (($dev, $ino, $mode, $nlink, $uid, $gid) = lstat($_))) &&
269 ($File::Find::prune = 1);
272 Notice the C<_> in the above C<int(-M _)>: the C<_> is a magical
273 filehandle that caches the information from the preceding
274 C<stat()>, C<lstat()>, or filetest.
276 Here's another interesting wanted function. It will find all symbolic
277 links that don't resolve:
280 -l && !-e && print "bogus link: $File::Find::name\n";
283 See also the script C<pfind> on CPAN for a nice application of this
288 If you run your program with the C<-w> switch, or if you use the
289 C<warnings> pragma, File::Find will report warnings for several weird
290 situations. You can disable these warnings by putting the statement
292 no warnings 'File::Find';
294 in the appropriate scope. See L<perllexwarn> for more info about lexical
301 =item $dont_use_nlink
303 You can set the variable C<$File::Find::dont_use_nlink> to 1, if you want to
304 force File::Find to always stat directories. This was used for file systems
305 that do not have an C<nlink> count matching the number of sub-directories.
306 Examples are ISO-9660 (CD-ROM), AFS, HPFS (OS/2 file system), FAT (DOS file
307 system) and a couple of others.
309 You shouldn't need to set this variable, since File::Find should now detect
310 such file systems on-the-fly and switch itself to using stat. This works even
311 for parts of your file system, like a mounted CD-ROM.
313 If you do set C<$File::Find::dont_use_nlink> to 1, you will notice slow-downs.
317 Be aware that the option to follow symbolic links can be dangerous.
318 Depending on the structure of the directory tree (including symbolic
319 links to directories) you might traverse a given (physical) directory
320 more than once (only if C<follow_fast> is in effect).
321 Furthermore, deleting or changing files in a symbolically linked directory
322 might cause very unpleasant surprises, since you delete or change files
323 in an unknown directory.
327 =head1 BUGS AND CAVEATS
329 Despite the name of the C<finddepth()> function, both C<find()> and
330 C<finddepth()> perform a depth-first search of the directory
335 File::Find used to produce incorrect results if called recursively.
336 During the development of perl 5.8 this bug was fixed.
337 The first fixed version of File::Find was 1.01.
345 our @ISA = qw(Exporter);
346 our @EXPORT = qw(find finddepth);
353 require File::Basename;
356 # Should ideally be my() not our() but local() currently
357 # refuses to operate on lexicals
360 our ($wanted_callback, $avoid_nlink, $bydepth, $no_chdir, $follow,
361 $follow_skip, $full_check, $untaint, $untaint_skip, $untaint_pat,
362 $pre_process, $post_process, $dangling_symlinks);
367 return substr($cdir,0,rindex($cdir,'/')) if $fn eq $File::Find::current_dir;
369 $cdir = substr($cdir,0,rindex($cdir,'/')+1);
373 my $abs_name= $cdir . $fn;
375 if (substr($fn,0,3) eq '../') {
376 1 while $abs_name =~ s!/[^/]*/\.\./+!/!;
382 sub PathCombine($$) {
383 my ($Base,$Name) = @_;
386 if (substr($Name,0,1) eq '/') {
390 $AbsName= contract_name($Base,$Name);
393 # (simple) check for recursion
394 my $newlen= length($AbsName);
395 if ($newlen <= length($Base)) {
396 if (($newlen == length($Base) || substr($Base,$newlen,1) eq '/')
397 && $AbsName eq substr($Base,0,$newlen))
405 sub Follow_SymLink($) {
408 my ($NewName,$DEV, $INO);
409 ($DEV, $INO)= lstat $AbsName;
412 if ($SLnkSeen{$DEV, $INO}++) {
413 if ($follow_skip < 2) {
414 die "$AbsName is encountered a second time";
420 $NewName= PathCombine($AbsName, readlink($AbsName));
421 unless(defined $NewName) {
422 if ($follow_skip < 2) {
423 die "$AbsName is a recursive symbolic link";
432 ($DEV, $INO) = lstat($AbsName);
433 return undef unless defined $DEV; # dangling symbolic link
436 if ($full_check && defined $DEV && $SLnkSeen{$DEV, $INO}++) {
437 if ( ($follow_skip < 1) || ((-d _) && ($follow_skip < 2)) ) {
438 die "$AbsName encountered a second time";
448 our($dir, $name, $fullname, $prune);
449 sub _find_dir_symlnk($$$);
452 # check whether or not a scalar variable is tainted
453 # (code straight from the Camel, 3rd ed., page 561)
456 my $nada = substr($arg, 0, 0); # zero-length
458 eval { eval "# $nada" };
459 return length($@) != 0;
464 die "invalid top directory" unless defined $_[0];
466 # This function must local()ize everything because callbacks may
467 # call find() or finddepth()
470 local ($wanted_callback, $avoid_nlink, $bydepth, $no_chdir, $follow,
471 $follow_skip, $full_check, $untaint, $untaint_skip, $untaint_pat,
472 $pre_process, $post_process, $dangling_symlinks);
473 local($dir, $name, $fullname, $prune);
476 my $cwd = $wanted->{bydepth} ? Cwd::fastcwd() : Cwd::getcwd();
478 # VMS returns this by default in VMS format which just doesn't
479 # work for the rest of this module.
480 $cwd = VMS::Filespec::unixpath($cwd);
482 # Apparently this is not expected to have a trailing space.
483 # To attempt to make VMS/UNIX conversions mostly reversable,
484 # a trailing slash is needed. The run-time functions ignore the
485 # resulting double slash, but it causes the perl tests to fail.
488 # This comes up in upper case now, but should be lower.
489 # In the future this could be exact case, no need to change.
491 my $cwd_untainted = $cwd;
493 $wanted_callback = $wanted->{wanted};
494 $bydepth = $wanted->{bydepth};
495 $pre_process = $wanted->{preprocess};
496 $post_process = $wanted->{postprocess};
497 $no_chdir = $wanted->{no_chdir};
498 $full_check = $Is_Win32 ? 0 : $wanted->{follow};
499 $follow = $Is_Win32 ? 0 :
500 $full_check || $wanted->{follow_fast};
501 $follow_skip = $wanted->{follow_skip};
502 $untaint = $wanted->{untaint};
503 $untaint_pat = $wanted->{untaint_pattern};
504 $untaint_skip = $wanted->{untaint_skip};
505 $dangling_symlinks = $wanted->{dangling_symlinks};
507 # for compatibility reasons (find.pl, find2perl)
508 local our ($topdir, $topdev, $topino, $topmode, $topnlink);
510 # a symbolic link to a directory doesn't increase the link count
511 $avoid_nlink = $follow || $File::Find::dont_use_nlink;
513 my ($abs_dir, $Is_Dir);
516 foreach my $TOP (@_) {
519 ($topdev,$topino,$topmode,$topnlink) = $follow ? stat $top_item : lstat $top_item;
522 $top_item =~ s|[/\\]\z||
523 unless $top_item =~ m{^(?:\w:)?[/\\]$};
526 $top_item =~ s|/\z|| unless $top_item eq '/';
533 if (substr($top_item,0,1) eq '/') {
534 $abs_dir = $top_item;
536 elsif ($top_item eq $File::Find::current_dir) {
539 else { # care about any ../
540 $top_item =~ s/\.dir\z//i if $Is_VMS;
541 $abs_dir = contract_name("$cwd/",$top_item);
543 $abs_dir= Follow_SymLink($abs_dir);
544 unless (defined $abs_dir) {
545 if ($dangling_symlinks) {
546 if (ref $dangling_symlinks eq 'CODE') {
547 $dangling_symlinks->($top_item, $cwd);
549 warnings::warnif "$top_item is a dangling symbolic link\n";
556 $top_item =~ s/\.dir\z//i if $Is_VMS;
557 _find_dir_symlnk($wanted, $abs_dir, $top_item);
563 unless (defined $topnlink) {
564 warnings::warnif "Can't stat $top_item: $!\n";
568 $top_item =~ s/\.dir\z//i if $Is_VMS;
569 _find_dir($wanted, $top_item, $topnlink);
578 unless (($_,$dir) = File::Basename::fileparse($abs_dir)) {
579 ($dir,$_) = ('./', $top_item);
583 if (( $untaint ) && (is_tainted($dir) )) {
584 ( $abs_dir ) = $dir =~ m|$untaint_pat|;
585 unless (defined $abs_dir) {
586 if ($untaint_skip == 0) {
587 die "directory $dir is still tainted";
595 unless ($no_chdir || chdir $abs_dir) {
596 warnings::warnif "Couldn't chdir $abs_dir: $!\n";
600 $name = $abs_dir . $_; # $File::Find::name
601 $_ = $name if $no_chdir;
603 { $wanted_callback->() }; # protect against wild "next"
607 unless ( $no_chdir ) {
608 if ( ($check_t_cwd) && (($untaint) && (is_tainted($cwd) )) ) {
609 ( $cwd_untainted ) = $cwd =~ m|$untaint_pat|;
610 unless (defined $cwd_untainted) {
611 die "insecure cwd in find(depth)";
615 unless (chdir $cwd_untainted) {
616 die "Can't cd to $cwd: $!\n";
624 # $p_dir : "parent directory"
625 # $nlink : what came back from the stat
627 # chdir (if not no_chdir) to dir
630 my ($wanted, $p_dir, $nlink) = @_;
631 my ($CdLvl,$Level) = (0,0);
634 my ($subcount,$sub_nlink);
636 my $dir_name= $p_dir;
638 my $dir_rel = $File::Find::current_dir;
644 = ($p_dir =~ m{^(?:\w:[/\\]?|[/\\])$} ? $p_dir : "$p_dir/" );
647 # VMS is returning trailing .dir on directories
648 # and trailing . on files and symbolic links
652 $p_dir =~ s/\.(dir)?$//i unless $p_dir eq '.';
654 $dir_pref = ($p_dir =~ m/[\]>]+$/ ? $p_dir : "$p_dir/" );
657 $dir_pref= ( $p_dir eq '/' ? '/' : "$p_dir/" );
660 local ($dir, $name, $prune, *DIR);
662 unless ( $no_chdir || ($p_dir eq $File::Find::current_dir)) {
664 if (( $untaint ) && (is_tainted($p_dir) )) {
665 ( $udir ) = $p_dir =~ m|$untaint_pat|;
666 unless (defined $udir) {
667 if ($untaint_skip == 0) {
668 die "directory $p_dir is still tainted";
675 unless (chdir ($Is_VMS && $udir !~ /[\/\[<]+/ ? "./$udir" : $udir)) {
676 warnings::warnif "Can't cd to $udir: $!\n";
681 # push the starting directory
682 push @Stack,[$CdLvl,$p_dir,$dir_rel,-1] if $bydepth;
684 while (defined $SE) {
686 $dir= $p_dir; # $File::Find::dir
687 $name= $dir_name; # $File::Find::name
688 $_= ($no_chdir ? $dir_name : $dir_rel ); # $_
689 # prune may happen here
691 { $wanted_callback->() }; # protect against wild "next"
695 # change to that directory
696 unless ($no_chdir || ($dir_rel eq $File::Find::current_dir)) {
698 if ( ($untaint) && (($tainted) || ($tainted = is_tainted($dir_rel) )) ) {
699 ( $udir ) = $dir_rel =~ m|$untaint_pat|;
700 unless (defined $udir) {
701 if ($untaint_skip == 0) {
702 die "directory (" . ($p_dir ne '/' ? $p_dir : '') . "/) $dir_rel is still tainted";
703 } else { # $untaint_skip == 1
708 unless (chdir ($Is_VMS && $udir !~ /[\/\[<]+/ ? "./$udir" : $udir)) {
709 warnings::warnif "Can't cd to (" .
710 ($p_dir ne '/' ? $p_dir : '') . "/) $udir: $!\n";
716 $dir= $dir_name; # $File::Find::dir
718 # Get the list of files in the current directory.
719 unless (opendir DIR, ($no_chdir ? $dir_name : $File::Find::current_dir)) {
720 warnings::warnif "Can't opendir($dir_name): $!\n";
723 @filenames = readdir DIR;
725 @filenames = $pre_process->(@filenames) if $pre_process;
726 push @Stack,[$CdLvl,$dir_name,"",-2] if $post_process;
728 # default: use whatever was specified
729 # (if $nlink >= 2, and $avoid_nlink == 0, this will switch back)
730 $no_nlink = $avoid_nlink;
731 # if dir has wrong nlink count, force switch to slower stat method
732 $no_nlink = 1 if ($nlink < 2);
734 if ($nlink == 2 && !$no_nlink) {
735 # This dir has no subdirectories.
736 for my $FN (@filenames) {
738 # Big hammer here - Compensate for VMS trailing . and .dir
739 # No win situation until this is changed, but this
740 # will handle the majority of the cases with breaking the fewest
743 $FN =~ s#\.$## if ($FN ne '.');
745 next if $FN =~ $File::Find::skip_pattern;
747 $name = $dir_pref . $FN; # $File::Find::name
748 $_ = ($no_chdir ? $name : $FN); # $_
749 { $wanted_callback->() }; # protect against wild "next"
754 # This dir has subdirectories.
755 $subcount = $nlink - 2;
757 # HACK: insert directories at this position. so as to preserve
758 # the user pre-processed ordering of files.
759 # EG: directory traversal is in user sorted order, not at random.
760 my $stack_top = @Stack;
762 for my $FN (@filenames) {
763 next if $FN =~ $File::Find::skip_pattern;
764 if ($subcount > 0 || $no_nlink) {
765 # Seen all the subdirs?
766 # check for directoriness.
767 # stat is faster for a file in the current directory
768 $sub_nlink = (lstat ($no_chdir ? $dir_pref . $FN : $FN))[3];
772 $FN =~ s/\.dir\z//i if $Is_VMS;
773 # HACK: replace push to preserve dir traversal order
774 #push @Stack,[$CdLvl,$dir_name,$FN,$sub_nlink];
775 splice @Stack, $stack_top, 0,
776 [$CdLvl,$dir_name,$FN,$sub_nlink];
779 $name = $dir_pref . $FN; # $File::Find::name
780 $_= ($no_chdir ? $name : $FN); # $_
781 { $wanted_callback->() }; # protect against wild "next"
785 $name = $dir_pref . $FN; # $File::Find::name
786 $_= ($no_chdir ? $name : $FN); # $_
787 { $wanted_callback->() }; # protect against wild "next"
793 while ( defined ($SE = pop @Stack) ) {
794 ($Level, $p_dir, $dir_rel, $nlink) = @$SE;
795 if ($CdLvl > $Level && !$no_chdir) {
798 $tmp = '[' . ('-' x ($CdLvl-$Level)) . ']';
801 $tmp = join('/',('..') x ($CdLvl-$Level));
803 die "Can't cd to $tmp from $dir_name"
809 $dir_name = ($p_dir =~ m{^(?:\w:[/\\]?|[/\\])$}
810 ? "$p_dir$dir_rel" : "$p_dir/$dir_rel");
811 $dir_pref = "$dir_name/";
813 elsif ($^O eq 'VMS') {
814 if ($p_dir =~ m/[\]>]+$/) {
816 $dir_name =~ s/([\]>]+)$/.$dir_rel$1/;
817 $dir_pref = $dir_name;
820 $dir_name = "$p_dir/$dir_rel";
821 $dir_pref = "$dir_name/";
825 $dir_name = ($p_dir eq '/' ? "/$dir_rel" : "$p_dir/$dir_rel");
826 $dir_pref = "$dir_name/";
829 if ( $nlink == -2 ) {
830 $name = $dir = $p_dir; # $File::Find::name / dir
831 $_ = $File::Find::current_dir;
832 $post_process->(); # End-of-directory processing
834 elsif ( $nlink < 0 ) { # must be finddepth, report dirname now
836 if ( substr($name,-2) eq '/.' ) {
837 substr($name, length($name) == 2 ? -1 : -2) = '';
840 $_ = ($no_chdir ? $dir_name : $dir_rel );
841 if ( substr($_,-2) eq '/.' ) {
842 substr($_, length($_) == 2 ? -1 : -2) = '';
844 { $wanted_callback->() }; # protect against wild "next"
847 push @Stack,[$CdLvl,$p_dir,$dir_rel,-1] if $bydepth;
857 # $dir_loc : absolute location of a dir
858 # $p_dir : "parent directory"
860 # chdir (if not no_chdir) to dir
862 sub _find_dir_symlnk($$$) {
863 my ($wanted, $dir_loc, $p_dir) = @_; # $dir_loc is the absolute directory
867 my $updir_loc = $dir_loc; # untainted parent directory
869 my $dir_name = $p_dir;
872 my $dir_rel = $File::Find::current_dir;
873 my $byd_flag; # flag for pending stack entry if $bydepth
877 $dir_pref = ( $p_dir eq '/' ? '/' : "$p_dir/" );
878 $loc_pref = ( $dir_loc eq '/' ? '/' : "$dir_loc/" );
880 local ($dir, $name, $fullname, $prune, *DIR);
884 if (( $untaint ) && (is_tainted($dir_loc) )) {
885 ( $updir_loc ) = $dir_loc =~ m|$untaint_pat|; # parent dir, now untainted
886 # once untainted, $updir_loc is pushed on the stack (as parent directory);
887 # hence, we don't need to untaint the parent directory every time we chdir
889 unless (defined $updir_loc) {
890 if ($untaint_skip == 0) {
891 die "directory $dir_loc is still tainted";
898 $ok = chdir($updir_loc) unless ($p_dir eq $File::Find::current_dir);
900 warnings::warnif "Can't cd to $updir_loc: $!\n";
905 push @Stack,[$dir_loc,$updir_loc,$p_dir,$dir_rel,-1] if $bydepth;
907 while (defined $SE) {
910 # change (back) to parent directory (always untainted)
912 unless (chdir $updir_loc) {
913 warnings::warnif "Can't cd to $updir_loc: $!\n";
917 $dir= $p_dir; # $File::Find::dir
918 $name= $dir_name; # $File::Find::name
919 $_= ($no_chdir ? $dir_name : $dir_rel ); # $_
920 $fullname= $dir_loc; # $File::Find::fullname
921 # prune may happen here
923 lstat($_); # make sure file tests with '_' work
924 { $wanted_callback->() }; # protect against wild "next"
928 # change to that directory
929 unless ($no_chdir || ($dir_rel eq $File::Find::current_dir)) {
930 $updir_loc = $dir_loc;
931 if ( ($untaint) && (($tainted) || ($tainted = is_tainted($dir_loc) )) ) {
932 # untaint $dir_loc, what will be pushed on the stack as (untainted) parent dir
933 ( $updir_loc ) = $dir_loc =~ m|$untaint_pat|;
934 unless (defined $updir_loc) {
935 if ($untaint_skip == 0) {
936 die "directory $dir_loc is still tainted";
943 unless (chdir $updir_loc) {
944 warnings::warnif "Can't cd to $updir_loc: $!\n";
949 $dir = $dir_name; # $File::Find::dir
951 # Get the list of files in the current directory.
952 unless (opendir DIR, ($no_chdir ? $dir_loc : $File::Find::current_dir)) {
953 warnings::warnif "Can't opendir($dir_loc): $!\n";
956 @filenames = readdir DIR;
959 for my $FN (@filenames) {
961 # Big hammer here - Compensate for VMS trailing . and .dir
962 # No win situation until this is changed, but this
963 # will handle the majority of the cases with breaking the fewest.
966 $FN =~ s#\.$## if ($FN ne '.');
968 next if $FN =~ $File::Find::skip_pattern;
970 # follow symbolic links / do an lstat
971 $new_loc = Follow_SymLink($loc_pref.$FN);
973 # ignore if invalid symlink
974 unless (defined $new_loc) {
975 if (!defined -l _ && $dangling_symlinks) {
976 if (ref $dangling_symlinks eq 'CODE') {
977 $dangling_symlinks->($FN, $dir_pref);
979 warnings::warnif "$dir_pref$FN is a dangling symbolic link\n";
984 $name = $dir_pref . $FN;
985 $_ = ($no_chdir ? $name : $FN);
986 { $wanted_callback->() };
993 $FN =~ s#\.$## if ($FN ne '.');
994 $new_loc =~ s/\.dir\z//i;
995 $new_loc =~ s#\.$## if ($new_loc ne '.');
997 push @Stack,[$new_loc,$updir_loc,$dir_name,$FN,1];
1000 $fullname = $new_loc; # $File::Find::fullname
1001 $name = $dir_pref . $FN; # $File::Find::name
1002 $_ = ($no_chdir ? $name : $FN); # $_
1003 { $wanted_callback->() }; # protect against wild "next"
1009 while (defined($SE = pop @Stack)) {
1010 ($dir_loc, $updir_loc, $p_dir, $dir_rel, $byd_flag) = @$SE;
1011 $dir_name = ($p_dir eq '/' ? "/$dir_rel" : "$p_dir/$dir_rel");
1012 $dir_pref = "$dir_name/";
1013 $loc_pref = "$dir_loc/";
1014 if ( $byd_flag < 0 ) { # must be finddepth, report dirname now
1015 unless ($no_chdir || ($dir_rel eq $File::Find::current_dir)) {
1016 unless (chdir $updir_loc) { # $updir_loc (parent dir) is always untainted
1017 warnings::warnif "Can't cd to $updir_loc: $!\n";
1021 $fullname = $dir_loc; # $File::Find::fullname
1022 $name = $dir_name; # $File::Find::name
1023 if ( substr($name,-2) eq '/.' ) {
1024 substr($name, length($name) == 2 ? -1 : -2) = ''; # $File::Find::name
1026 $dir = $p_dir; # $File::Find::dir
1027 $_ = ($no_chdir ? $dir_name : $dir_rel); # $_
1028 if ( substr($_,-2) eq '/.' ) {
1029 substr($_, length($_) == 2 ? -1 : -2) = '';
1032 lstat($_); # make sure file tests with '_' work
1033 { $wanted_callback->() }; # protect against wild "next"
1036 push @Stack,[$dir_loc, $updir_loc, $p_dir, $dir_rel,-1] if $bydepth;
1046 if ( ref($wanted) eq 'HASH' ) {
1047 unless( exists $wanted->{wanted} and ref( $wanted->{wanted} ) eq 'CODE' ) {
1048 die 'no &wanted subroutine given';
1050 if ( $wanted->{follow} || $wanted->{follow_fast}) {
1051 $wanted->{follow_skip} = 1 unless defined $wanted->{follow_skip};
1053 if ( $wanted->{untaint} ) {
1054 $wanted->{untaint_pattern} = $File::Find::untaint_pattern
1055 unless defined $wanted->{untaint_pattern};
1056 $wanted->{untaint_skip} = 0 unless defined $wanted->{untaint_skip};
1060 elsif( ref( $wanted ) eq 'CODE' ) {
1061 return { wanted => $wanted };
1064 die 'no &wanted subroutine given';
1070 _find_opt(wrap_wanted($wanted), @_);
1074 my $wanted = wrap_wanted(shift);
1075 $wanted->{bydepth} = 1;
1076 _find_opt($wanted, @_);
1080 $File::Find::skip_pattern = qr/^\.{1,2}\z/;
1081 $File::Find::untaint_pattern = qr|^([-+@\w./]+)$|;
1083 # These are hard-coded for now, but may move to hint files.
1086 $File::Find::dont_use_nlink = 1;
1088 elsif ($^O eq 'MSWin32') {
1092 # this _should_ work properly on all platforms
1093 # where File::Find can be expected to work
1094 $File::Find::current_dir = File::Spec->curdir || '.';
1096 $File::Find::dont_use_nlink = 1
1097 if $^O eq 'os2' || $^O eq 'dos' || $^O eq 'amigaos' || $Is_Win32 ||
1098 $^O eq 'interix' || $^O eq 'cygwin' || $^O eq 'epoc' || $^O eq 'qnx' ||
1101 # Set dont_use_nlink in your hint file if your system's stat doesn't
1102 # report the number of links in a directory as an indication
1103 # of the number of files.
1104 # See, e.g. hints/machten.sh for MachTen 2.2.
1105 unless ($File::Find::dont_use_nlink) {
1107 $File::Find::dont_use_nlink = 1 if ($Config::Config{'dont_use_nlink'});
1110 # We need a function that checks if a scalar is tainted. Either use the
1111 # Scalar::Util module's tainted() function or our (slower) pure Perl
1112 # fallback is_tainted_pp()
1115 eval { require Scalar::Util };
1116 *is_tainted = $@ ? \&is_tainted_pp : \&Scalar::Util::tainted;