File Coverage

blib/script/find
Criterion Covered Total %
statement 72 83 86.7
branch 20 44 45.4
condition 3 6 50.0
subroutine 13 13 100.0
pod n/a
total 108 146 73.9


line stmt bran cond sub pod time code
1             #!/usr/local/bin/perl
2              
3             =begin metadata
4              
5             Name: find
6             Description: search directory tree for files matching a pattern
7             Author: Greg Snow, snow@biostat.washington.edu
8             Author: Tom Christiansen, tchrist@perl.com
9             Author: Larry Wall, larry@wall.org
10             License: perl
11              
12             =end metadata
13              
14             =cut
15              
16              
17             # find - actually a pass through front end for find2perl
18              
19 3     3   15945 use strict;
  3         6  
  3         133  
20              
21 3     3   15 use Config qw(%Config);
  3         4  
  3         194  
22 3     3   18 use File::Basename;
  3         4  
  3         396  
23 3     3   1531 use File::Spec::Functions;
  3         2990  
  3         309  
24 3     3   2791 use File::Temp qw/ tempfile /;
  3         81836  
  3         253  
25 3     3   1425 use FindBin;
  3         4292  
  3         248882  
26              
27             sub find_find2perl {
28 3 50   3   20 return $ENV{FIND2PERL} if defined $ENV{FIND2PERL};
29             my @candidates =
30 30         367 grep { -e }
31 30         130 map { catfile( $_, 'find2perl' ) }
32             dirname($Config{perlpath}),
33             split( /$Config{path_sep}/, $ENV{PATH} )
34 3         580 ;
35              
36             push @candidates, catfile( $ENV{PERL_LOCAL_LIB_ROOT}, 'bin', 'find2perl' )
37 3 50       30 if defined $ENV{PERL_LOCAL_LIB_ROOT};
38              
39 3 50       18 return defined $candidates[0] ? $candidates[0] : ();
40             }
41              
42 3         323056 my $find2perl = find_find2perl();
43 3 50       39 die("This program needs the App::find2perl module.\n") unless -e $find2perl;
44              
45             # Temp files to capture find2perl output and error.
46 3         21 my ($out_fh, $out_file) = tempfile();
47 3         2427 my ($err_fh, $err_file) = tempfile();
48              
49             END {
50 3     3   476 unlink $out_file;
51 3         372 unlink $err_file;
52             }
53              
54             # Save STDOUT and STDERR. Redirect to temp files.
55              
56 3 50       1632 open SAVE_OUT, '>&', STDOUT or die "Can't save STDOUT: $!";
57 3 50       12 die unless defined fileno SAVE_OUT;
58              
59 3 50       71 open STDOUT, '>&', $out_fh or die "Can't dup STDOUT to $out_file: $!";
60 3         35 $|++, select $_ for select STDOUT;
61              
62 3 50       75 open SAVE_ERR, '>&', STDERR or die "Can't save STDERR: $!";
63 3 50       12 die unless defined fileno SAVE_ERR;
64              
65 3 50       52 open STDERR, '>&', $err_fh or die "Can't dup STDERR to $err_file: $!";
66 3         21 $|++, select $_ for select STDERR;
67              
68             # Run find2perl to convert find command to Perl script.
69              
70 3         2253550 system $^X, $find2perl, @ARGV;
71              
72             # Check for errors.
73              
74 3         198 my $child_error = $?;
75 3         19 my $acm_min_error;
76              
77 3 50       190 open STDERR, '>&', SAVE_ERR or die "Can't restore STDERR: $!";
78              
79 3 50       72 seek $err_fh, 0, 0 or die "Can't rewind $err_file: $!";
80              
81 3         20 my $error = do { local $/; <$err_fh> };
  3         92  
  3         307  
82              
83 3         148 close $err_fh;
84              
85 3 50       31 if ($error) {
86 0 0       0 if ($error =~ /^Unrecognized switch: -[acm]min\s*$/) {
87             # Tried to use -amin, -cmin or -mmin, which find2perl doesn't
88             # support.
89 0         0 $acm_min_error++;
90              
91             # So change to -atime, -ctime or -mtime, and append an
92             # identifier to its argument (fortunately, find2perl does not
93             # validate the argument).
94 0         0 for my $i (0..$#ARGV) {
95 0 0       0 $ARGV[$i + 1] .= '-ppt-minutes'
96             if $ARGV[$i] =~ s/^(-[acm])min$/${1}time/;
97             }
98              
99 0         0 system $find2perl, @ARGV;
100 0         0 $child_error = $?;
101             } else {
102 0         0 warn $error;
103             }
104             }
105              
106 3 50       28 die "Can't run $find2perl (wait status == $child_error)" if $child_error;
107              
108 3 50 33     208 die "Empty program" unless -s $out_fh && -s $out_file;
109              
110             # Get "find" Perl script from output.
111              
112 3 50       57 seek $out_fh, 0, 0 or die "Can't rewind $out_file: $!";
113              
114 3         14 my $program = do { local $/; <$out_fh> };
  3         13  
  3         103  
115              
116 3         38 close $out_fh;
117              
118 3 50       99 open STDOUT, '>&', SAVE_OUT or die "Can't restore STDOUT: $!";
119              
120             # Convert fake -atime, -ctime and -mtime conditionals, such as:
121             #
122             # (int(-C _) > 60-ppt-minutes) &&
123             # (int(-M _) < 15-ppt-minutes)
124             #
125             # To -amin, -cmin and -mmin conditionals, such as:
126             #
127             # (int(1440 * -C _) > 60) &&
128             # (int(1440 * -M _) < 15)
129             #
130 3 50       18 if ($acm_min_error) {
131 0         0 my $minutes_per_day = 24 * 60;
132 0         0 $program =~ s/(\bint\()(-[AMC] .*)-ppt-minutes/$1$minutes_per_day * $2/g;
133             }
134              
135             # Run "find" Perl script.
136              
137 3 100 66 3   764 eval qq{
  3     3   44  
  3     3   56  
  3     3   371  
  3     42   23  
  3         8  
  3         115  
  3         17  
  3         8  
  3         111  
  3         19  
  3         5  
  3         974  
  42         117  
  42         4149  
138             no strict;
139             local \$^W = 0;
140             $program;
141             };
142              
143 0 0       0 die "Can't compile and execute $find2perl: $@\n" if $@;
144              
145 0         0 exit 0;
146              
147             __END__