📄 find.pm
字号:
{ &$wanted_callback }; # protect against wild "next" } } else { # This dir has subdirectories. $subcount = $nlink - 2; for my $FN (@filenames) { next if $FN =~ $File::Find::skip_pattern; if ($subcount > 0 || $avoid_nlink) { # Seen all the subdirs? # check for directoriness. # stat is faster for a file in the current directory $sub_nlink = (lstat ($no_chdir ? $dir_pref . $FN : $FN))[3]; if (-d _) { --$subcount; $FN =~ s/\.dir\z// if $Is_VMS; push @Stack,[$CdLvl,$dir_name,$FN,$sub_nlink]; } else { $name = $dir_pref . $FN; # $File::Find::name $_= ($no_chdir ? $name : $FN); # $_ { &$wanted_callback }; # protect against wild "next" } } else { $name = $dir_pref . $FN; # $File::Find::name $_= ($no_chdir ? $name : $FN); # $_ { &$wanted_callback }; # protect against wild "next" } } } } continue { while ( defined ($SE = pop @Stack) ) { ($Level, $p_dir, $dir_rel, $nlink) = @$SE; if ($CdLvl > $Level && !$no_chdir) { my $tmp; if ($Is_MacOS) { $tmp = (':' x ($CdLvl-$Level)) . ':'; } else { $tmp = join('/',('..') x ($CdLvl-$Level)); } die "Can't cd to $dir_name" . $tmp unless chdir ($tmp); $CdLvl = $Level; } if ($Is_MacOS) { # $pdir always has a trailing ':', except for the starting dir, # where $dir_rel eq ':' $dir_name = "$p_dir$dir_rel"; $dir_pref = "$dir_name:"; } else { $dir_name = ($p_dir eq '/' ? "/$dir_rel" : "$p_dir/$dir_rel"); $dir_pref = "$dir_name/"; } if ( $nlink == -2 ) { $name = $dir = $p_dir; # $File::Find::name / dir if ($Is_MacOS) { $_ = ':'; # $_ } else { $_ = '.'; } &$post_process; # End-of-directory processing } elsif ( $nlink < 0 ) { # must be finddepth, report dirname now $name = $dir_name; if ($Is_MacOS) { if ($dir_rel eq ':') { # must be the top dir, where we started $name =~ s|:$||; # $File::Find::name $p_dir = "$p_dir:" unless ($p_dir =~ /:$/); } $dir = $p_dir; # $File::Find::dir $_ = ($no_chdir ? $name : $dir_rel); # $_ } else { if ( substr($name,-2) eq '/.' ) { $name =~ s|/\.$||; } $dir = $p_dir; $_ = ($no_chdir ? $dir_name : $dir_rel ); if ( substr($_,-2) eq '/.' ) { s|/\.$||; } } { &$wanted_callback }; # protect against wild "next" } else { push @Stack,[$CdLvl,$p_dir,$dir_rel,-1] if $bydepth; last; } } }}# API:# $wanted# $dir_loc : absolute location of a dir# $p_dir : "parent directory"# preconditions:# chdir (if not no_chdir) to dirsub _find_dir_symlnk($$$) { my ($wanted, $dir_loc, $p_dir) = @_; # $dir_loc is the absolute directory my @Stack; my @filenames; my $new_loc; my $updir_loc = $dir_loc; # untainted parent directory my $SE = []; my $dir_name = $p_dir; my $dir_pref; my $loc_pref; my $dir_rel; my $byd_flag; # flag for pending stack entry if $bydepth my $tainted = 0; my $ok = 1; if ($Is_MacOS) { $dir_pref = ($p_dir =~ /:$/) ? "$p_dir" : "$p_dir:"; $loc_pref = ($dir_loc =~ /:$/) ? "$dir_loc" : "$dir_loc:"; $dir_rel = ':'; # directory name relative to current directory } else { $dir_pref = ( $p_dir eq '/' ? '/' : "$p_dir/" ); $loc_pref = ( $dir_loc eq '/' ? '/' : "$dir_loc/" ); $dir_rel = '.'; # directory name relative to current directory } local ($dir, $name, $fullname, $prune, *DIR); unless ($no_chdir) { # untaint the topdir if (( $untaint ) && (is_tainted($dir_loc) )) { ( $updir_loc ) = $dir_loc =~ m|$untaint_pat|; # parent dir, now untainted # once untainted, $updir_loc is pushed on the stack (as parent directory); # hence, we don't need to untaint the parent directory every time we chdir # to it later unless (defined $updir_loc) { if ($untaint_skip == 0) { die "directory $dir_loc is still tainted"; } else { return; } } } $ok = chdir($updir_loc) unless ($p_dir eq $File::Find::current_dir); unless ($ok) { warn "Can't cd to $updir_loc: $!\n" if $^W; return; } } push @Stack,[$dir_loc,$updir_loc,$p_dir,$dir_rel,-1] if $bydepth; if ($Is_MacOS) { $p_dir = $dir_pref; # ensure trailing ':' } while (defined $SE) { unless ($bydepth) { # change (back) to parent directory (always untainted) unless ($no_chdir) { unless (chdir $updir_loc) { warn "Can't cd to $updir_loc: $!\n" if $^W; next; } } $dir= $p_dir; # $File::Find::dir $name= $dir_name; # $File::Find::name $_= ($no_chdir ? $dir_name : $dir_rel ); # $_ $fullname= $dir_loc; # $File::Find::fullname # prune may happen here $prune= 0; lstat($_); # make sure file tests with '_' work { &$wanted_callback }; # protect against wild "next" next if $prune; } # change to that directory unless ($no_chdir || ($dir_rel eq $File::Find::current_dir)) { $updir_loc = $dir_loc; if ( ($untaint) && (($tainted) || ($tainted = is_tainted($dir_loc) )) ) { # untaint $dir_loc, what will be pushed on the stack as (untainted) parent dir ( $updir_loc ) = $dir_loc =~ m|$untaint_pat|; unless (defined $updir_loc) { if ($untaint_skip == 0) { die "directory $dir_loc is still tainted"; } else { next; } } } unless (chdir $updir_loc) { warn "Can't cd to $updir_loc: $!\n" if $^W; next; } } if ($Is_MacOS) { $dir_name = "$dir_name:" unless ($dir_name =~ /:$/); } $dir = $dir_name; # $File::Find::dir # Get the list of files in the current directory. unless (opendir DIR, ($no_chdir ? $dir_loc : $File::Find::current_dir)) { warn "Can't opendir($dir_loc): $!\n" if $^W; next; } @filenames = readdir DIR; closedir(DIR); for my $FN (@filenames) { next if $FN =~ $File::Find::skip_pattern; # follow symbolic links / do an lstat $new_loc = Follow_SymLink($loc_pref.$FN); # ignore if invalid symlink next unless defined $new_loc; if (-d _) { push @Stack,[$new_loc,$updir_loc,$dir_name,$FN,1]; } else { $fullname = $new_loc; # $File::Find::fullname $name = $dir_pref . $FN; # $File::Find::name $_ = ($no_chdir ? $name : $FN); # $_ { &$wanted_callback }; # protect against wild "next" } } } continue { while (defined($SE = pop @Stack)) { ($dir_loc, $updir_loc, $p_dir, $dir_rel, $byd_flag) = @$SE; if ($Is_MacOS) { # $p_dir always has a trailing ':', except for the starting dir, # where $dir_rel eq ':' $dir_name = "$p_dir$dir_rel"; $dir_pref = "$dir_name:"; $loc_pref = ($dir_loc =~ /:$/) ? $dir_loc : "$dir_loc:"; } else { $dir_name = ($p_dir eq '/' ? "/$dir_rel" : "$p_dir/$dir_rel"); $dir_pref = "$dir_name/"; $loc_pref = "$dir_loc/"; } if ( $byd_flag < 0 ) { # must be finddepth, report dirname now unless ($no_chdir || ($dir_rel eq $File::Find::current_dir)) { unless (chdir $updir_loc) { # $updir_loc (parent dir) is always untainted warn "Can't cd to $updir_loc: $!\n" if $^W; next; } } $fullname = $dir_loc; # $File::Find::fullname $name = $dir_name; # $File::Find::name if ($Is_MacOS) { if ($dir_rel eq ':') { # must be the top dir, where we started $name =~ s|:$||; # $File::Find::name $p_dir = "$p_dir:" unless ($p_dir =~ /:$/); } $dir = $p_dir; # $File::Find::dir $_ = ($no_chdir ? $name : $dir_rel); # $_ } else { if ( substr($name,-2) eq '/.' ) { $name =~ s|/\.$||; # $File::Find::name } $dir = $p_dir; # $File::Find::dir $_ = ($no_chdir ? $dir_name : $dir_rel); # $_ if ( substr($_,-2) eq '/.' ) { s|/\.$||; } } lstat($_); # make sure file tests with '_' work { &$wanted_callback }; # protect against wild "next" } else { push @Stack,[$dir_loc, $updir_loc, $p_dir, $dir_rel,-1] if $bydepth; last; } } }}sub wrap_wanted { my $wanted = shift; if ( ref($wanted) eq 'HASH' ) { if ( $wanted->{follow} || $wanted->{follow_fast}) { $wanted->{follow_skip} = 1 unless defined $wanted->{follow_skip}; } if ( $wanted->{untaint} ) { $wanted->{untaint_pattern} = $File::Find::untaint_pattern unless defined $wanted->{untaint_pattern}; $wanted->{untaint_skip} = 0 unless defined $wanted->{untaint_skip}; } return $wanted; } else { return { wanted => $wanted }; }}sub find { my $wanted = shift; _find_opt(wrap_wanted($wanted), @_);}sub finddepth { my $wanted = wrap_wanted(shift); $wanted->{bydepth} = 1; _find_opt($wanted, @_);}# default$File::Find::skip_pattern = qr/^\.{1,2}\z/;$File::Find::untaint_pattern = qr|^([-+@\w./]+)$|;# These are hard-coded for now, but may move to hint files.if ($^O eq 'VMS') { $Is_VMS = 1; $File::Find::dont_use_nlink = 1;}elsif ($^O eq 'MacOS') { $Is_MacOS = 1; $File::Find::dont_use_nlink = 1; $File::Find::skip_pattern = qr/^Icon\015\z/; $File::Find::untaint_pattern = qr|^(.+)$|;}# this _should_ work properly on all platforms# where File::Find can be expected to work$File::Find::current_dir = File::Spec->curdir || '.';$File::Find::dont_use_nlink = 1 if $^O eq 'os2' || $^O eq 'dos' || $^O eq 'amigaos' || $^O eq 'MSWin32' || $^O eq 'cygwin' || $^O eq 'epoc' || $^O eq 'NetWare' || $^O eq 'msys';# Set dont_use_nlink in your hint file if your system's stat doesn't# report the number of links in a directory as an indication# of the number of files.# See, e.g. hints/machten.sh for MachTen 2.2.unless ($File::Find::dont_use_nlink) { require Config; $File::Find::dont_use_nlink = 1 if ($Config::Config{'dont_use_nlink'});}# We need a function that checks if a scalar is tainted. Either use the # Scalar::Util module's tainted() function or our (slower) pure Perl # fallback is_tainted_pp(){ local $@; eval { require Scalar::Util }; *is_tainted = $@ ? \&is_tainted_pp : \&Scalar::Util::tainted;}1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -