blib/lib/Dir/Purge.pm

filter: Statementstmt 64.5% 80 / 124 Branchbran 38.8% 28 / 72 Conditioncond 16.6% 1 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 83.3% 10 / 12 Podpod 0.0% 0 / 2 totaltotal 54.0% 119 / 220 CC 44 SCAR 42.2
CC
44
Cov
54%
purgedir
145.8
purgedir_by_age
20.0
__ANON__
11.2
CRAP 68.0
↑ Summary
1 # Dir::Purge.pm -- Purge directories
2 # RCS Info : $Id: Purge.pm,v 1.6 2006/09/19 12:24:01 jv Exp $
3 # Author : Johan Vromans
4 # Created On : Wed May 17 12:58:02 2000
5 # Last Modified By: Johan Vromans
6 # Last Modified On: Tue Sep 19 14:23:56 2006
7 # Update Count : 161
8 # Status : Unknown, Use with caution!
9  
10 # Purge directories by strategy.
11 #
12 # This is also an exercise in weird programming techniques.
13  
14 package Dir::Purge;
15  
16 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 ▶use vars qw(@ISA @EXPORT @EXPORT_OK $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 $VERSION = "1.02";
21 1 @ISA = qw(Exporter);
22 1 @EXPORT = qw(&purgedir);
23 1 @EXPORT_OK = qw(&purgedir_by_age);
24  
25 1 my $purge_by_age; # strategy
26  
27 0 ▶sub purgedir_by_age {
Subroutine purgedir_by_age: not called CC=4 SCAR=30.0
Pod undocumented
28 0 my @dirs = @_;
29 0 my $opts;
30 0 ▶ if ( UNIVERSAL::isa ($dirs[0], 'HASH') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($dirs[0], 'HASH')) { } 0 0 0
31 0 $opts = shift (@dirs);
32 0 my $strat = delete $opts->{strategy};
33 0 ▶ if ( defined $strat && $strat ne "by_age" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $strat and $strat ne "by_age") 0 0 0
Condition0%0/3
defined $strat and $strat ne "by_age"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $strat and $strat ne "by_age"
defined $strat $strat ne "by_age"
Truth table0%0/3
A and B
A= defined $stratB= $strat ne "by_age"
ABresult
0X0
100
111
34 0 croak ("Invalid option: 'strategy'");
35 }
36 0 $opts->{strategy} = "by_age";
37 }
38 else {
39 0 $opts = { keep => shift(@dirs), strategy => "by_age" };
40 }
41 0 purgedir ($opts, @dirs);
42 }
43  
44  
45 # Common processing code. It verifies the arguments, directories and
46 # calls $code->(...) to do the actual purging.
47 # Nothing is done if any of the verifications fail.
48  
49 2 ▶sub purgedir {
Subroutine purgedir: called CC=24 SCAR=49.8
Pod undocumented
50  
51 2 my (@dirs) = @_;
52 2 my $error = 0;
53 2 my $code = $purge_by_age; # default: by age
54 2 my $ctl = { tag => "purgedir" };
55 2 my @opts = qw(keep strategy reverse include verbose test debug);
56  
57 # Get the parameters. Only the 'keep' value is mandatory.
58 2 ▶ if ( UNIVERSAL::isa ($dirs[0], 'HASH') ) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($dirs[0], 'HASH')) { } 2 0 2
elsif ($dirs[0] =~ /^-?\d+$/) { } 0 0 0
59 2 my $opts = shift (@dirs);
60 2 @{$ctl}{@opts} = delete @{$opts}{@opts};
61 2 ▶ if ( $ctl->{strategy} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ctl->{"strategy"}) 0 2 2
62 0 ▶ if ( $ctl->{strategy} eq "by_age" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ctl->{'strategy'} eq 'by_age') { } 0 0 0
63 0 $code = $purge_by_age;
64 }
65 else {
66 0 carp ("Unsupported purge strategy: '$ctl->{strategy}'");
67 0 $error++;
68 }
69 }
70 2 foreach (sort keys %$opts) {
71 0 carp ("Unhandled option \"$_\"");
72 0 $error++;
73 }
74 }
75 elsif ( $dirs[0] =~ /^-?\d+$/ ) {
76 0 $ctl->{keep} = shift (@dirs);
77 }
78  
79 2 ▶ unless ( $ctl->{keep} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ctl->{'keep'}) { } 2 0 2
if (do { $ctl->{"keep"} < 0 }) 0 2 2
80 0 croak ("Missing 'keep' value");
81 }
82 0 elsif ( $ctl->{keep} < 0 ) {
83 # Hmm. I would like to deprecate this, but on the other hand,
84 # a negative 'subscript' fits well in Perl.
85 #carp ("Negative 'keep' value is deprecated, ".
86 # "use 'reverse => 1' instead");
87 0 $ctl->{keep} = -$ctl->{keep};
88 0 $ctl->{reverse} = !$ctl->{reverse};
89 }
90  
91 2 ▶ $ctl->{verbose} = 1 unless defined ($ctl->{verbose});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ctl->{"verbose"} 0 2 2
92 2 ▶ $ctl->{verbose} = 9 if $ctl->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $ctl->{"debug"} 0 2 2
93  
94 2 ▶ if ( $ctl->{include} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ctl->{"include"}) 0 2 2
95 0 ▶ if ( !ref($ctl->{include}) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not ref $ctl->{'include'}) { } 0 0 0
elsif (UNIVERSAL::isa($ctl->{'include'}, 'CODE')) { } 0 0 0
elsif (UNIVERSAL::isa($ctl->{'include'}, 'Regexp')) { } 0 0 0
96 0 croak("Invalid value for 'include': " . $ctl->{include});
97 }
98 elsif ( UNIVERSAL::isa($ctl->{include}, 'CODE') ) {
99 # OK
100 }
101 elsif ( UNIVERSAL::isa($ctl->{include}, 'Regexp') ) {
102 0 my $pat = $ctl->{include};
103 0 ▶ $ctl->{include} = sub { $_[0] =~ $pat };
Subroutine __ANON__: not called CC=1 SCAR=6.9
104 }
105 else {
106 0 croak("Invalid value for 'include': " . $ctl->{include});
107 }
108 }
109  
110 # Thouroughly check the directories, and refuse to do anything
111 # in case of problems.
112 2 ▶ warn ("$ctl->{tag}: checking directories\n") if $ctl->{verbose} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $ctl->{"verbose"} > 1 0 2 2
113 2 foreach my $dir ( @dirs ) {
114 # Must be a directory.
115 2 ▶ unless ( -d $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir) 0 2 2
116 0 ▶ carp (-e _ ? "$dir: not a directory" : "$dir: not existing");
Branch: 1 branch
BranchTrueFalseTotal
-e _ ? : 0 0 0
117 0 $error++;
118 0 next;
119 }
120 # We need write access since we are going to delete files.
121 2 ▶ unless ( -w _ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-w _) 0 2 2
122 0 carp ("$dir: no write access");
123 0 $error++;
124 }
125 # We need read access since we are going to get the file list.
126 2 ▶ unless ( -r _ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-r _) 0 2 2
127 0 carp ("$dir: no read access");
128 0 $error++;
129 }
130 # Probably need this as well, don't know.
131 2 ▶ unless ( -x _ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-x _) 0 2 2
132 0 carp ("$dir: no access");
133 0 $error++;
134 }
135 }
136  
137 # If errors, bail out unless testing.
138 2 ▶ if ( $error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 0 2 2
139 0 ▶ if ( $ctl->{test} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ctl->{'test'}) { } 0 0 0
140 0 carp ("$ctl->{tag}: errors detected, continuing");
141 }
142 else {
143 0 croak ("$ctl->{tag}: errors detected, nothing done");
144 }
145 }
146  
147 # Process the directories.
148 2 foreach my $dir ( @dirs ) {
149 2 $code->($ctl, $dir);
150 }
151 };
152  
153 # Everything else is assumed to be small building-block routines to
154 # implement a plethora of purge strategies.
155 # Actually, I cannot think of any right now.
156  
157 # Gather file names and additional info.
158 2 ▶my $gather = sub {
Subroutine __ANON__: called CC=9 SCAR=24.1
159 2 my ($ctl, $dir, $what) = @_;
160  
161 2 local (*DIR);
162 2 ▶ opendir (DIR, $dir)
Branch: 1 branch
BranchTrueFalseTotal
opendir DIR, $dir or croak("dir: $!") 2 0 2
163 or croak ("dir: $!"); # shouldn't happen -- we've checked!
164 2 my @files;
165 2 foreach ( readdir (DIR) ) {
166 16 ▶ next if $ctl->{include} && !$ctl->{include}->($_, $dir);
Branch: 1 branch
BranchTrueFalseTotal
if $ctl->{"include"} and not $ctl->{"include"}->($_, $dir) 0 16 16
Condition33%1/3
$ctl->{"include"} and not $ctl->{"include"}->($_, $dir)
!ll&&!rl&&r
1600
MC/DC0%0/2
$ctl->{"include"} and not $ctl->{"include"}->($_, $dir)
$ctl->{"include"} not $ctl->{"include"}->($_, $dir)
Truth table33%1/3
A and not(B)
A= $ctl->{"include"}B= not $ctl->{"include"}->($_, $dir)
ABresult
0X0
100
111
167 16 ▶ next if /^\./;
Branch: 1 branch
BranchTrueFalseTotal
if /^\./ 4 12 16
168 12 ▶ next unless -f "$dir/$_";
Branch: 1 branch
BranchTrueFalseTotal
unless -f "$dir/$_" 0 12 12
169 12 push (@files, [ "$dir/$_", $what->("$dir/$_") ]);
170 }
171 2 closedir (DIR);
172  
173 warn ("$ctl->{tag}: $dir: ", scalar(@files), " files\n")
174 2 ▶ if $ctl->{verbose} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $ctl->{"verbose"} > 1 0 2 2
175 0 warn ("$ctl->{tag}: $dir: @{[map { $_->[0] } @files]}\n")
176 2 ▶ if $ctl->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $ctl->{"debug"} 0 2 2
177  
178 2 \@files;
179 1 };
180  
181 # Sort the list on the supplied info.
182 2 ▶my $sort = sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
183 2 my ($ctl, $files) = @_;
184  
185 2 my @sorted = map { $_->[0] } sort { $a->[1] <=> $b->[1] } @$files;
186 2 ▶ warn ("$ctl->{tag}: sorted: @sorted\n") if $ctl->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $ctl->{"debug"} 0 2 2
187 2 \@sorted;
188 1 };
189  
190 # Remove the files to keep from the list.
191 2 ▶my $reduce = sub {
Subroutine __ANON__: called CC=2 SCAR=7.4
192 2 my ($ctl, $files) = @_;
193  
194 2 ▶ if ( $ctl->{reverse} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ctl->{'reverse'}) { } 0 2 2
195 # Keep the newest files (tail of the list).
196 0 splice (@$files, @$files-$ctl->{keep}, $ctl->{keep});
197 }
198 else {
199 # Keep the oldest files (head of the list).
200 2 splice (@$files, 0, $ctl->{keep});
201 }
202 2 $files;
203 1 };
204  
205 # Remove the files in the list.
206 2 ▶my $purge = sub {
Subroutine __ANON__: called CC=5 SCAR=16.3
207 2 my ($ctl, $files) = @_;
208  
209 # Remove the selected files.
210 2 foreach ( @$files ) {
211 4 ▶ if ( $ctl->{test} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ctl->{'test'}) { } 2 2 4
212 2 warn ("$ctl->{tag}: candidate: $_\n");
213 }
214 else {
215 2 ▶ warn ("$ctl->{tag}: removing $_\n") if $ctl->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $ctl->{"verbose"} 0 2 2
216 2 ▶ unlink ($_) or carp ("$_: $!");
Branch: 1 branch
BranchTrueFalseTotal
unlink $_ or carp("${_}: $!") 2 0 2
217 }
218 }
219 1 };
220  
221 # Processing routine: purge by file age.
222 2 ▶$purge_by_age = sub {
Subroutine __ANON__: called CC=4 SCAR=16.6
223 2 my ($ctl, $dir) = @_;
224  
225 warn ("$ctl->{tag}: purging directory $dir (by age, keep $ctl->{keep})\n")
226 2 ▶ if $ctl->{verbose} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $ctl->{"verbose"} > 1 0 2 2
227  
228 # Gather, with age info.
229 2 ▶ my $files = $gather->($ctl, $dir, sub { -M _ });
Subroutine __ANON__: called CC=1 SCAR=0.0
230  
231 # Is there anything to do?
232 2 ▶ if ( @$files <= $ctl->{keep} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$files <= $ctl->{"keep"}) 0 2 2
233 0 ▶ warn ("$ctl->{tag}: $dir: below limit\n") if $ctl->{verbose} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $ctl->{"verbose"} > 1 0 0 0
234 0 return;
235 }
236  
237 # Sort, reduce and purge.
238 2 $purge->($ctl, $reduce->($ctl, $sort->($ctl, $files)));
239 1 };
240  
241 1 1;
242  
243 __END__
↑ Summary