App-Pfind

 view release on metacpan or  search on metacpan

lib/App/Pfind.pm  view on Meta::CPAN

  undef $!;
  $wrapped_code->();
  die "Failure in the code given to --${flag}: $!\n" if $!;
}

# Perform a real-stat or just reads the result from the previous stat. Returns
# just the mode part of the stat.
my $last_stated_file = '';
sub cheap_stat {
  my ($relative_file_name, $full_file_name) = @_;
  return (stat(_))[2] if $full_file_name eq $last_stated_file;
  $last_stated_file = $full_file_name;
  # When executing find in follow mode, the current file has already been
  # stat-ed (this is guaranteed by find), so we can re-use the value using `_`.
  return (stat(_))[2] if $options{follow} || $options{follow_fast};
  return (stat($relative_file_name))[2];
}

my %file_mode = (
    f => S_IFREG,   # regular file
    d => S_IFDIR,   # directory
    l => S_IFLNK,   # symbolic link
    b => S_IFBLK,   # block special file
    c => S_IFCHR,   # character special file
    p => S_IFIFO,   # fifo (pipe)
    s => S_IFSOCK,  # socket
    # S_IFWHT and S_ENFMT are not supported (they're Sys-V specific features)
  );

sub should_skip_file {
  my ($relative_file_name, $full_file_name) = @_;
  return 0 unless %{$options{type}};
  my $mode = cheap_stat($relative_file_name, $full_file_name);
  for my $m (keys(%{$options{type}})) {
    if (($mode & $file_mode{$m}) xor $options{type}{$m}) {
      print STDERR "Skipping '$full_file_name' due to '$m' type" if $options{verbose};
      return 1;
    }
  }
  return 0;
}

sub Run {

lib/App/Pfind.pm  view on Meta::CPAN

  if (@{$options{post}}) {
    $wrapped_post = wrap_code_blocks($options{post}, '$internal_pfind_dir', 'post');
  }

  find({
    bydepth => $options{depth_first},
    follow => $options{follow},
    follow_fast => $options{follow_fast},
    no_chdir => !$options{chdir},
    wanted => sub {
      if (not $options{recurse} and cheap_stat($_, $File::Find::name) & S_IFDIR) {
        print "Will not recurse into $File::Find::name" if $options{verbose};
        $File::Find::prune = 1;
      }
      print STDERR "Looking at file: $File::Find::name" if $options{verbose};
      return if should_skip_file($_, $File::Find::name);
      run_wrapped_sub($wrapped_exec, 'exec');
    },
    preprocess => sub {
      print STDERR "Entering: $File::Find::dir" if $options{verbose};
      run_wrapped_sub($wrapped_pre, 'pre-process') if $wrapped_pre;



( run in 1.039 second using v1.01-cache-2.11-cpan-49f99fa48dc )