bin/rm

filter: Statementstmt 86.7% 131 / 151 Branchbran 63.8% 46 / 72 Conditioncond 77.7% 21 / 27 MC/DCmcdc 46.6% 7 / 15 Subroutinesub 90.0% 27 / 30 Podpod n/a totaltotal 78.6% 232 / 295 CC 48 SCAR 22.8
CC
48
Cov
79%
remove_directory
23.6
remove_file
16.8
preprocess_options
13.1
CRAP 9.8
« bin/nl ↑ Summary bin/od »
1 #!/usr/bin/perl
2  
3 =encoding utf8
4  
5 =begin metadata
6  
7 Name: rm
8 Description: remove directory entries
9 Author: brian d foy, bdfoy@cpan.org
10 License: artistic2
11  
12 =end metadata
13  
14 =cut
15  
16 =head1 NAME
17  
18 rm - remove directory entries
19  
20 =head1 SYNOPSIS
21  
22 rm [-fiPrRv] file ...
23  
24 =head1 DESCRIPTION
25  
26 =head1 OPTIONS
27  
28 =over 4
29  
30 =item * -f - do not prompt the user for each file, and do not consider it an error if a file cannot be removed
31  
32 =item * -i - prompt the user for each file.
33  
34 =item * -P - a no-op, for compatibility. So implementations would overwrite files with random data
35  
36 =item * -r - same as -R
37  
38 =item * -R - remove directories recursively
39  
40 =item * -v - show the name of each file after it is removed
41  
42 =back
43  
44 =head1 AUTHOR
45  
46 Copyright (c) brian d foy, bdfoy@cpan.org
47  
48 The original version of this program was written by Steve Kemp,
49 steve@steve.org.uk, but almost none of that remains.
50  
51 =head1 LICENCE
52  
53 This program is licensed under the Artistic License 2.0.
54  
55 =cut
56  
57 package PerlPowerTools::rm;
58  
59 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 2 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 2 ▶use File::Spec::Functions;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 2 ▶use Storable qw(dclone);
Subroutine BEGIN: called CC=1 SCAR=0.0
64  
65 2 ▶use constant EX_SUCCESS => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 2 ▶use constant EX_FAILURE => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 2 ▶use constant EX_USAGE => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 2 ▶use constant OP_SUCCEEDED => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 2 ▶use constant OP_FAILED => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
70  
71 2 my $Program = basename($0);
72  
73 2 ▶__PACKAGE__->run( args => \@ARGV ) unless caller;
Branch: 1 branch
BranchTrueFalseTotal
unless caller 0 2 2
74  
75 10 ▶sub run {
Subroutine run: called CC=5 SCAR=17.4
76 10 my $class = shift;
77 10 my %args = @_;
78  
79 10 my $args = delete $args{args};
80  
81 # This looks funny because the other args are filehandles, which
82 # we can't dupe. We want to play with the command-line args such
83 # that we don't mess up anything that called us.
84 10 my $self = $class->new( { args => dclone($args), %args } )->process_options;
85  
86 10 ▶ $self->error( "$Program: -P ignored\n" ) if $self->is_overwrite;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_overwrite 1 9 10
87  
88 10 ▶ unless ( () = $self->files ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (() = $self->files) 0 10 10
89 0 ▶ exit(EX_SUCCESS) if $self->is_force;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_force 0 0 0
90 0 $self->error( "$Program: missing argument\n" );
91 0 usage();
92 }
93  
94 10 my $errors = grep { $self->process_file( $_ ) } $self->files;
95 10 ▶ exit( $errors ? EX_FAILURE : EX_SUCCESS );
Branch: 1 branch
BranchTrueFalseTotal
$errors ? : 2 8 10
96 }
97  
98 32 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
99 32 my( $class, $args ) = @_;
100 32 bless {
101 $class->defaults,
102 %$args
103 }, $class;
104 }
105  
106 32 ▶sub defaults {
Subroutine defaults: called CC=1 SCAR=0.0
107 32 my %hash = (
108 args => [],
109 error_fh => \*STDERR,
110 output_fh => \*STDOUT,
111 );
112 }
113  
114 30 ▶sub files { my $self = shift; @{ $self->{files} } }
Subroutine files: called CC=1 SCAR=0.0
115  
116 44 ▶sub is_force { my $self = shift; $self->{options}{f} }
Subroutine is_force: called CC=1 SCAR=0.0
117 21 ▶sub is_interactive { my $self = shift; $self->{options}{i} }
Subroutine is_interactive: called CC=1 SCAR=0.0
118 20 ▶sub is_overwrite { my $self = shift; $self->{options}{P} }
Subroutine is_overwrite: called CC=1 SCAR=0.0
119 14 ▶sub is_recursive { my $self = shift; $self->{options}{R} || $self->{options}{r} }
Subroutine is_recursive: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$self->{"options"}{"R"} or $self->{"options"}{"r"} 3 11 14
120 19 ▶sub is_verbose { my $self = shift; $self->{options}{v} }
Subroutine is_verbose: called CC=1 SCAR=0.0
121  
122 10 ▶sub options { my $self = shift; $self->{options} }
Subroutine options: called CC=1 SCAR=0.0
123  
124 32 ▶sub preprocess_options {
Subroutine preprocess_options: called CC=13 SCAR=25.7
125 32 my( $self ) = @_;
126  
127 32 my @new_args = @{ $self->{args} };
128  
129 32 my %args = map { $new_args[$_], $_ } 0 .. $#new_args;
130  
131 32 my @rest;
132 32 ▶ if( exists $args{'--'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"--"}) 9 23 32
133 9 @rest = @new_args[ $args{'--'} .. $#new_args ];
134 9 @new_args = @new_args[0 .. ($args{'--'} - 1)];
135 }
136 32 foreach (@new_args) {
137 64 ▶ if (m/\A\-\-/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\A\-\-/) 0 64 64
138 0 warn "unknown option: '$_'\n";
139 0 usage();
140 }
141 }
142  
143 # Expand clustering
144 @new_args = map {
145 32 ▶ if( /\A\-(.+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\A\-(.+)/) { } 35 29 64
146 35 my $cluster = $1;
147 35 map { "-$_" } split //, $cluster;
148 }
149 else {
150 29 $_;
151 }
152 } @new_args;
153  
154 # this is rm particular processing: -f and -i turn off each
155 # other, and the last one wins. Figure out which one is last
156 # then filter out all earlier of the other.
157 32 ▶ if( exists $args{'-f'} && exists $args{'-i'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"-f"} and exists $args{"-i"}) 6 26 32
Condition100%3/3
exists $args{"-f"} and exists $args{"-i"}
!ll&&!rl&&r
2336
MC/DC100%2/2
exists $args{"-f"} and exists $args{"-i"}
exists $args{"-f"} exists $args{"-i"}
Truth table100%3/3
A and B
A= exists $args{"-f"}B= exists $args{"-i"}
ABresult
0X0
100
111
158 6 my $last;
159 6 foreach ( reverse @new_args ) {
160 6 ▶ next unless /\A-[fi]\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless /\A-[fi]\z/ 0 6 6
161 6 $last = $_;
162 6 last;
163 }
164  
165 @new_args = map {
166 6 (
167 12 ▶ ( $last eq '-f' and $_ eq '-i') # f wins
Branch: 1 branch
BranchTrueFalseTotal
$last eq '-f' && $_ eq '-i' || $last ne '-f' && $_ eq '-f' ? : 6 6 12
Condition100%9/9
$last eq "-f" and $_ eq "-i" or $last ne "-f" and $_ eq "-f"
l!l&&r!l&&!r
336
$last eq "-f" and $_ eq "-i"
!ll&&!rl&&r
633
$last ne "-f" and $_ eq "-f"
!ll&&!rl&&r
333
MC/DC75%3/4
$last eq "-f" and $_ eq "-i" or $last ne "-f" and $_ eq "-f"
$last eq "-f" $_ eq "-i" $last ne "-f" $_ eq "-f"
Truth table57%4/7
A and B or C and D
A= $last eq "-f"B= $_ eq "-i"C= $last ne "-f"D= $_ eq "-f"
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
168 ||
169 ( $last ne '-f' and $_ eq '-f' ) # i wins
170 ) ? () : $_;
171 } @new_args;
172 }
173  
174 32 $self->{original_args} = $self->{args};
175 32 $self->{args} = $self->{preprocessed_args} = [ @new_args, @rest ];
176  
177 32 return $self;
178 }
179  
180 20 ▶sub process_options {
Subroutine process_options: called CC=3 SCAR=11.0
181 20 my( $self ) = @_;
182  
183 20 $self->preprocess_options;
184  
185 2 ▶ use Getopt::Long qw(:config no_ignore_case);
Subroutine BEGIN: called CC=1 SCAR=0.0
186  
187 20 my %opts;
188 my $ret = Getopt::Long::GetOptionsFromArray(
189 $self->{args},
190 'f' => \$opts{'f'},
191 'i' => \$opts{'i'},
192 'P' => \$opts{'P'},
193 'R' => \$opts{'R'}, # both of these are recursive
194 'r' => \$opts{'r'},
195 20 'v' => \$opts{'v'},
196 );
197 20 ▶ usage() unless $ret;
Branch: 1 branch
BranchTrueFalseTotal
unless $ret 0 20 20
198  
199 20 ▶ $self->{options} = { map { defined $_ ? $_ : 0 } %opts };
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 142 98 240
200 20 $self->{files} = $self->{args};
201  
202 20 return $self;
203 }
204  
205 13 ▶sub process_file {
Subroutine process_file: called CC=6 SCAR=17.9
206 13 my( $self, $filename ) = @_;
207  
208 13 my $method = do {
209 13 ▶ if( -d $filename ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $filename) { } 4 9 13
210 4 ▶ if( ! $self->is_recursive ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_recursive) 2 2 4
211 2 ▶ $self->error( "$Program: '$filename': is a directory\n" ) unless $self->is_force;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_force 1 1 2
212 2 ▶ return $self->is_force ? OP_SUCCEEDED : OP_FAILED;
Branch: 1 branch
BranchTrueFalseTotal
$self->is_force ? : 1 1 2
213 }
214 2 'remove_directory';
215 }
216 else {
217 9 'remove_file';
218 }
219 };
220  
221 11 my $result = $self->$method( $filename );
222 11 ▶ return $self->is_force ? OP_SUCCEEDED : $result;
Branch: 1 branch
BranchTrueFalseTotal
$self->is_force ? : 2 9 11
223 }
224  
225 2 ▶sub remove_directory {
Subroutine remove_directory: called CC=12 SCAR=31.6
226 2 my( $self, $dirname ) = @_;
227  
228 2 my $dh;
229 2 ▶ unless( opendir( $dh, $dirname ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (opendir $dh, $dirname) 0 2 2
230 0 ▶ $self->error( "$Program: cannot open '$dirname': $!\n" ) unless $self->is_force;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_force 0 0 0
231 0 ▶ return $self->is_force ? OP_SUCCEEDED : OP_FAILED;
Branch: 1 branch
BranchTrueFalseTotal
$self->is_force ? : 0 0 0
232 }
233  
234 2 foreach my $file ( readdir($dh) ) {
235 6 ▶ next if $file eq '.' || $file eq '..';
Branch: 1 branch
BranchTrueFalseTotal
if $file eq "." or $file eq ".." 4 2 6
Condition100%3/3
$file eq "." or $file eq ".."
l!l&&r!l&&!r
222
MC/DC100%2/2
$file eq "." or $file eq ".."
$file eq "." $file eq ".."
Truth table100%3/3
A or B
A= $file eq "."B= $file eq ".."
ABresult
1X1
011
000
236 2 my $path = catfile( $dirname, $file );
237  
238 2 ▶ my $method = -d $path ? 'remove_directory' : 'remove_file';
Branch: 1 branch
BranchTrueFalseTotal
-d $path ? : 0 2 2
239 2 my $result = $self->$method($path);
240 }
241  
242 2 closedir $dh;
243  
244 2 ▶ unless( rmdir $dirname ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (rmdir $dirname) 0 2 2
245 0 ▶ $self->error( "$Program: cannot remove directory '$dirname': $!\n" ) unless $self->is_force;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_force 0 0 0
246 0 ▶ return $self->is_force ? OP_SUCCEEDED : OP_FAILED;
Branch: 1 branch
BranchTrueFalseTotal
$self->is_force ? : 0 0 0
247 }
248  
249 2 ▶ $self->message( "$dirname\n" ) if $self->is_verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_verbose 0 2 2
250  
251 2 return OP_SUCCEEDED;
252 }
253  
254 11 ▶sub remove_file {
Subroutine remove_file: called CC=11 SCAR=28.2
255 11 my( $self, $filename ) = @_;
256  
257 # Answering no to skip a file is not an error
258 11 ▶ if( $self->is_interactive ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->is_interactive) { } 0 11 11
elsif (not $self->is_force and -e $filename and not -w $filename) { } 0 15 15
259 0 $self->message( "$filename: ? " );
260 0 ▶ return OP_SUCCEEDED if <STDIN> =~ /^[Nn]/;
Branch: 1 branch
BranchTrueFalseTotal
if readline(STDIN) =~ /^[Nn]/ 0 0 0
261 }
262 ▶ elsif( !$self->is_force && -e $filename && ! -w $filename ) {
Condition83%5/6
not $self->is_force and -e $filename and not -w $filename
!ll&&!rl&&r
470
not $self->is_force and -e $filename
!ll&&!rl&&r
227
MC/DC0%0/3
not $self->is_force and -e $filename and not -w $filename
not $self->is_force -e $filename not -w $filename
Truth table75%3/4
not(A) and B and not(C)
A= not $self->is_forceB= -e $filenameC= not -w $filename
ABCresult
0XX0
10X0
1100
1111
263 0 $self->message( "$filename: Read-only ? " );
264 0 ▶ return OP_SUCCEEDED if <STDIN> =~ /^[Nn]/;
Branch: 1 branch
BranchTrueFalseTotal
if readline(STDIN) =~ /^[Nn]/ 0 0 0
265 }
266  
267 11 ▶ unless( unlink $filename ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (unlink $filename) 4 7 11
268 4 ▶ $self->error( "$Program: cannot remove '$filename': $!\n" ) unless $self->is_force;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_force 2 2 4
269 4 ▶ return $self->is_force ? OP_SUCCEEDED : OP_FAILED;
Branch: 1 branch
BranchTrueFalseTotal
$self->is_force ? : 2 2 4
270 }
271  
272 7 ▶ $self->message( "$filename\n" ) if $self->is_verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_verbose 0 7 7
273  
274 7 return OP_SUCCEEDED;
275 }
276  
277 0 ▶sub usage {
Subroutine usage: not called CC=1 SCAR=6.9
278 0 require Pod::Usage;
279 0 Pod::Usage::pod2usage({
280 -exitval => EX_USAGE,
281 -verbose => 1,
282 });
283 }
284  
285 4 ▶sub error_fh { my $self = shift; $self->{error_fh} }
Subroutine error_fh: called CC=1 SCAR=0.0
286 4 ▶sub error {
Subroutine error: called CC=2 SCAR=7.6
287 4 my $self = shift;
288 4 ▶ print { $self->error_fh || *STDERR } @_;
Condition33%1/3
$self->error_fh || *STDERR
l!l&&r!l&&!r
400
MC/DC0%0/2
$self->error_fh || *STDERR
$self->error_fh *STDERR
Truth table33%1/3
A || B
A= $self->error_fhB= *STDERR
ABresult
1X1
011
000
289 }
290  
291 0 ▶sub output_fh { my $self = shift; $self->{output_fh} }
Subroutine output_fh: not called CC=1 SCAR=6.9
292 0 ▶sub message {
Subroutine message: not called CC=2 SCAR=17.9
293 0 my $self = shift;
294 0 ▶ print { $self->output_fh || *STDOUT } @_;
Condition0%0/3
$self->output_fh || *STDOUT
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->output_fh || *STDOUT
$self->output_fh *STDOUT
Truth table0%0/3
A || B
A= $self->output_fhB= *STDOUT
ABresult
1X1
011
000
295 }
296  
297 2 __PACKAGE__;
« bin/nl ↑ Summary bin/od »