blib/lib/App/FileCleanerByDiskUage.pm

filter: Statementstmt 82.5% 123 / 149 Branchbran 71.1% 64 / 90 Conditioncond 75.0% 27 / 36 MC/DCmcdc 59.0% 13 / 22 Subroutinesub 100% 8 / 8 Podpod 100% 1 / 1 totaltotal 77.1% 236 / 306 CC 59 SCAR 43.9
CC
59
Cov
78%
clean
95.9
__ANON__
5.0
BEGIN
1.0
CRAP 80.4
↑ Summary
1 package App::FileCleanerByDiskUage;
2  
3 3 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use File::Find ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Filesys::Df qw( df );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Net::Server::Daemonize qw(check_pid_file create_pid_file unlink_pid_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 App::FileCleanerByDiskUage - Removes files based on disk space usage till it drops below the specified amount.
13  
14 =head1 VERSION
15  
16 Version 0.5.0
17  
18 =cut
19  
20 3 our $VERSION = '0.5.0';
21  
22 =head1 SYNOPSIS
23  
24 use App::FileCleanerByDiskUage;
25  
26 # remove files under /var/log/suricata/pcap when disk usage is over 90%
27 # If over 90% make sure there are atleast 32 files and if there are atleast 32, remove them based
28 # on age till we drop below 90%. The newest 32 will be ignored regardless of disk usage.
29 my $removed=App::FileCleanerByDiskUage->clean(path=>'/var/log/suricata/pcap/', du=>90, min_files=>32);
30 if (defined( $removed->{errors}[0] )){
31 die('One or more file could not be removed... '.join(' ', @{ $removed->{errors} }));
32 }
33 my $int=0;
34 while (defined( $removed->{unlined}[$int] )){
35 print 'Removed ' . $removed->{unlinked}[$int]{name} . "\n";
36  
37 $int++;
38 }
39  
40 This works via doing the following.
41  
42 1: Check if disk usage is above the specified threshold. If not it ends here.
43  
44 2: Search for files under the specified path.
45  
46 3: If the number of found files is less than the number of files to keep regardless
47 of disk size it ends here. So if min_files is set to 32 and there are only 3 files,
48 then it would just return.
49  
50 4: Get the stats for all the found files.
51  
52 5: If min_files is specified, remove that many of the files from the list, starting
53 with the newest.
54  
55 6: Removes the oldest file.
56  
57 7: Check disk usage again and if it is less it ends here.
58  
59 8: Go back to 6.
60  
61 =head1 Functions
62  
63 =head2 clean
64  
65 This performs the cleaning actions. As long as the path exists and .path and .du
66 are defined this will not die. But if any of those are undef or do not exist it will
67 die.
68  
69 The following hash values are taken by it.
70  
71 Minimum Required Vars: path, du
72  
73 - path :: The path to look for files under. May be a array of paths. Only the first is used
74 for getting the disk usage though, so this should not have paths in it that are on
75 a different partition.
76 Default :: undef
77  
78 - du :: Disk usage to remove files down till.
79 Default :: undef
80  
81 - min_files :: Minimum number of files to keep, regardless of disk usage.
82 Default :: undef
83  
84 - ignore :: A regexp to use for ignoring files. So lets say you want to ignore,
85 files matching /\.pcap$/, it would be '\.pcap$'.
86 Default :: undef
87  
88 - dry_run :: Do not actually remove anything. Just check to see if the file writable by the
89 current user.
90  
91 -use_pid :: Create a PID to make sure multiple instances can't run at once.
92 Default :: undef
93  
94 -pid_dir :: Create a PID to make sure multiple instances can't run at once.
95 Default :: /var/run
96  
97 - pid_name :: Append this to the the name of the pid file created. If specified with a
98 value of 'foo' then the file would be 'file_cleaner_by_du-foo.pid'.
99 Default :: undef
100  
101 The returned value is a hash ref.
102  
103 - dry_run :: Boolean for fir it was a dry run or not.
104  
105 - found_files :: Array of hashes of data for all files found. This will only be defined if du is above
106 threshold for removing files. If it is below it, the function will return instead of taking
107 the time required to run a search.
108  
109 - found_files_count :: A count of files found.
110  
111 - path :: The value of path that it was called with. This will always be a array, regardless of if a array or
112 scalar was passed as internally converts a scalars into a array containing just a single item.
113  
114 - missing_paths :: Paths that were passed to it, but don't exist.
115  
116 - unlinked :: Array of hashes of data for files that have been removed.
117  
118 - unlinked_count :: A count of how many files were unlinked
119  
120 - unlink_errors :: Array of strings containing error descriptions.
121  
122 - unlink_failed :: Array of hashes of data for files that could not removed. The corresponding
123 index in .errors will be the error in question. So $results->{unlink_failed}[0]
124 would be $results->{unlink_errors}[0]
125  
126 - unlink_fialed_count :: A count of how many files unlinking failed for.
127  
128 The files hash is composed as below.
129  
130 - name :: Name of the file, including it's path.
131  
132 # following are provided via the Perl function stat
133 - dev
134 - ino
135 - mode
136 - nlink
137 - uid
138 - gid
139 - rdev
140 - size
141 - atime
142 - mtime
143 - ctime
144 - blksize
145 - blocks
146  
147 =cut
148  
149 17 ▶sub clean {
Subroutine clean: called CC=55 SCAR=45.6
Pod documented
150 17 my ( $empty, %opts ) = @_;
151  
152 17 my $pid_file;
153 17 ▶ if ( $opts{use_pid} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"use_pid"}) 0 17 17
154 0 ▶ if ( !defined( $opts{pid_dir} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $opts{'pid_dir'}) { } 0 0 0
155 0 $opts{pid_dir} = '/var/run';
156 } else {
157 0 ▶ if ( !-d $opts{pid_dir} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $opts{"pid_dir"}) 0 0 0
158 0 die( $opts{pid_dir} . ' does not exist or is not a dir' );
159 }
160 }
161  
162 0 ▶ if ( !defined( $opts{pid_name} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $opts{'pid_name'}) { } 0 0 0
163 0 $pid_file = $opts{pid_dir} . '/file_cleaner_by_du.pid';
164 } else {
165 0 ▶ if ( $opts{pid_name} =~ /\// || $opts{pid_name} =~ /\\/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"pid_name"} =~ m[/] or $opts{"pid_name"} =~ /\\/) 0 0 0
Condition0%0/3
$opts{"pid_name"} =~ m[/] or $opts{"pid_name"} =~ /\\/
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{"pid_name"} =~ m[/] or $opts{"pid_name"} =~ /\\/
$opts{"pid_name"} =~ m[/] $opts{"pid_name"} =~ /\\/
Truth table0%0/3
A or B
A= $opts{"pid_name"} =~ m[/]B= $opts{"pid_name"} =~ /\\/
ABresult
1X1
011
000
166 0 die( 'PID name of "' . $opts{pid_name} . '" can not contain either / or \\' );
167 }
168 0 $pid_file = $opts{pid_dir} . '/file_cleaner_by_du-' . $opts{pid_name} . '.pid';
169 }
170  
171 0 check_pid_file($pid_file);
172 0 create_pid_file($pid_file);
173 } ## end if ( $opts{use_pid} )
174  
175 17 my @missing_paths;
176 my @paths;
177  
178 17 my $du_path;
179 # file paths should end with / or otherwise if the path is a symlink File::Find will not descend into it
180 # so fix that up while we are doing the path check
181 17 ▶ if ( !defined( $opts{path} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $opts{'path'}) { } 1 16 17
elsif (ref $opts{'path'} ne 'ARRAY' and not -d $opts{'path'}) { } 0 16 16
elsif (ref $opts{'path'} eq 'ARRAY') { } 1 15 16
182 1 ▶ if ( $opts{use_pid} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"use_pid"}) 0 1 1
183 0 unlink_pid_file($pid_file);
184 }
185 1 die('$opts{path} is not defined');
186 ▶ } elsif ( ref( $opts{path} ) ne 'ARRAY' && !-d $opts{path} ) {
Condition66%2/3
ref $opts{"path"} ne "ARRAY" and not -d $opts{"path"}
!ll&&!rl&&r
1150
MC/DC0%0/2
ref $opts{"path"} ne "ARRAY" and not -d $opts{"path"}
ref $opts{"path"} ne "ARRAY" not -d $opts{"path"}
Truth table66%2/3
A and not(B)
A= ref $opts{"path"} ne "ARRAY"B= not -d $opts{"path"}
ABresult
0X0
100
111
187 0 push( @missing_paths, $opts{path} );
188 } elsif ( ref( $opts{path} ) eq 'ARRAY' ) {
189 1 ▶ if ( !defined( $opts{path}[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"path"}[0]) 0 1 1
190 0 ▶ if ( $opts{use_pid} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"use_pid"}) 0 0 0
191 0 unlink_pid_file($pid_file);
192 }
193 0 die('$opts{path}[0] is not defined');
194 }
195 1 my $int = 0;
196 1 ▶ while ( defined( $opts{path}[$int] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"path"}[$int]) 2 1 3
197 2 $opts{path}[$int] = $opts{path}[$int] . '/';
198 2 $opts{path}[$int] =~ s/\/+$/\//;
199 2 ▶ if ( !-d $opts{path}[$int] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not -d $opts{'path'}[$int]) { } 1 1 2
200 1 push( @missing_paths, $opts{path}[$int] );
201 } else {
202 1 push( @paths, $opts{path}[$int] );
203 }
204 2 $int++;
205 } ## end while ( defined( $opts{path}[$int] ) )
206 1 $du_path = $paths[0];
207 } else {
208 15 $opts{path} = $opts{path} . '/';
209 15 $opts{path} =~ s/\/+$/\//;
210 15 $du_path = $opts{path};
211 15 push( @paths, $opts{path} );
212 }
213  
214 16 ▶ if ( !defined( $paths[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $paths[0]) 0 16 16
215 0 die('All specified paths are missing or not a directory');
216 }
217  
218 16 ▶ if ( !defined( $opts{du} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $opts{'du'}) { } 1 15 16
elsif (not $opts{'du'} =~ /^\d+$/) { } 1 14 15
219 1 ▶ if ( $opts{use_pid} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"use_pid"}) 0 1 1
220 0 unlink_pid_file($pid_file);
221 }
222 1 die('$opts{du} is not defined');
223 } elsif ( $opts{du} !~ /^\d+$/ ) {
224 1 ▶ if ( $opts{use_pid} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"use_pid"}) 0 1 1
225 0 unlink_pid_file($pid_file);
226 }
227 1 die( '$opts{du} is set to "' . $opts{du} . '" whish is not numeric' );
228 }
229  
230 # if we have a min_files specified, make sure the value is numeric
231 14 ▶ if ( defined( $opts{min_files} ) && $opts{min_files} !~ /^\d+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"min_files"} and not $opts{"min_files"} =~ /^\d+$/) 1 13 14
Condition100%3/3
defined $opts{"min_files"} and not $opts{"min_files"} =~ /^\d+$/
!ll&&!rl&&r
1031
MC/DC100%2/2
defined $opts{"min_files"} and not $opts{"min_files"} =~ /^\d+$/
defined $opts{"min_files"} not $opts{"min_files"} =~ /^\d+$/
Truth table100%3/3
A and not(B)
A= defined $opts{"min_files"}B= not $opts{"min_files"} =~ /^\d+$/
ABresult
0X0
100
111
232 1 ▶ if ( $opts{use_pid} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"use_pid"}) 0 1 1
233 0 unlink_pid_file($pid_file);
234 }
235 1 die( '$opts{min_files} is set to "' . $opts{min_files} . '" whish is not numeric matching /^\d+$/' );
236 }
237  
238 13 ▶ if ( !$opts{dry_run} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $opts{'dry_run'}) { } 10 3 13
239 10 $opts{dry_run} = 0,;
240 } else {
241 3 $opts{dry_run} = 1,;
242 }
243  
244 # df() is normally Filesys::Df::df, but may be overridden internally via the
245 # _df option so the removal loop's disk-usage logic can be tested without a
246 # real filesystem. _resync bounds how many files may be removed between real
247 # df() checks (see the removal loop below).
248 13 ▶ my $df_func = ( ref( $opts{_df} ) eq 'CODE' ) ? $opts{_df} : \&df;
Branch: 1 branch
BranchTrueFalseTotal
ref $opts{'_df'} eq 'CODE' ? : 3 10 13
249 13 ▶ my $resync = ( defined( $opts{_resync} ) && $opts{_resync} =~ /^\d+$/ && $opts{_resync} > 0 ) ? $opts{_resync} : 64;
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{'_resync'} && $opts{'_resync'} =~ /^\d+$/ && $opts{'_resync'} > 0 ? : 1 12 13
Condition66%4/6
defined $opts{"_resync"} and $opts{"_resync"} =~ /^\d+$/ and $opts{"_resync"} > 0
!ll&&!rl&&r
1201
defined $opts{"_resync"} and $opts{"_resync"} =~ /^\d+$/
!ll&&!rl&&r
1201
MC/DC33%1/3
defined $opts{"_resync"} and $opts{"_resync"} =~ /^\d+$/ and $opts{"_resync"} > 0
defined $opts{"_resync"} $opts{"_resync"} =~ /^\d+$/ $opts{"_resync"} > 0
Truth table50%2/4
A and B and C
A= defined $opts{"_resync"}B= $opts{"_resync"} =~ /^\d+$/C= $opts{"_resync"} > 0
ABCresult
0XX0
10X0
1100
1111
250  
251 13 my $df = $df_func->($du_path);
252  
253 # the results to be returned
254 my $results = {
255 unlinked => [],
256 unlink_failed => [],
257 unlink_errors => [],
258 found_files => [],
259 found_files_count => 0,
260 unlinked_count => 0,
261 unlink_failed_count => 0,
262 du_target => $opts{du},
263 du_starting => $df->{per},
264 du_ending => $df->{per},
265 min_files => 0,
266 dry_run => $opts{dry_run},
267 13 path => \@paths,
268 missing_paths => \@missing_paths,
269 };
270  
271 13 ▶ if ( !defined( $paths[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $paths[0]) 0 13 13
272 0 ▶ if ( $opts{use_pid} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"use_pid"}) 0 0 0
273 0 unlink_pid_file($pid_file);
274 }
275 0 return $results;
276 }
277  
278 13 ▶ if ( $df->{per} < $opts{du} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($df->{"per"} < $opts{"du"}) 2 11 13
279 2 ▶ if ( $opts{use_pid} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"use_pid"}) 0 2 2
280 0 unlink_pid_file($pid_file);
281 }
282 2 return $results;
283 }
284  
285 # compile the ignore regexp once, if specified, and reuse it below
286 11 ▶ my $ignore_re = defined( $opts{ignore} ) ? qr/$opts{ignore}/ : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{'ignore'} ? : 1 10 11
287  
288 # Recursively find regular files under the requested paths, statting each
289 # one inline during the traversal. Doing the stat here means a single stat
290 # syscall per file (the traversal and the mtime lookup share it) and avoids
291 # building a separate array of path strings alongside the file info.
292 11 my @files_info;
293 File::Find::find(
294 336 ▶ sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
295 # $_ is the basename (we are chdir'd into the containing dir),
296 # $File::Find::name is the full path. stat($_) populates the "_"
297 # handle so the -f test below reuses it rather than statting again.
298 336 my @stat = stat($_);
299 336 ▶ return unless @stat; # skip on stat failure (races, broken symlinks)
Branch: 1 branch
BranchTrueFalseTotal
unless @stat 0 336 336
300 336 ▶ return unless -f _; # regular files only, matching the old ->file rule
Branch: 1 branch
BranchTrueFalseTotal
unless -f _ 12 324 336
301 324 ▶ return if defined($ignore_re) && $_ =~ $ignore_re; # ignore by basename
Branch: 1 branch
BranchTrueFalseTotal
if defined $ignore_re and $_ =~ /$ignore_re/ 2 322 324
Condition100%3/3
defined $ignore_re and $_ =~ /$ignore_re/
!ll&&!rl&&r
32022
MC/DC100%2/2
defined $ignore_re and $_ =~ /$ignore_re/
defined $ignore_re $_ =~ /$ignore_re/
Truth table100%3/3
A and B
A= defined $ignore_reB= $_ =~ /$ignore_re/
ABresult
0X0
100
111
302 # blocks ($stat[12], 512-byte units) is the space actually freed by
303 # unlinking, used by the removal loop to estimate disk usage between
304 # df() calls. apparent size would over-count sparse/small files.
305 322 push( @files_info, { name => $File::Find::name, mtime => $stat[9], blocks => $stat[12] } );
306 },
307 @paths
308 11 );
309 11 $results->{found_files_count} = scalar(@files_info);
310  
311 # if we have a min number of files specified, make sure we found more than
312 # that many. min_files elements at indexes 0 .. min_files-1, so index
313 # min_files existing means there is at least one file eligible for removal.
314 11 ▶ if ( $opts{min_files} && !defined( $files_info[ $opts{min_files} ] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"min_files"} and not defined $files_info[$opts{"min_files"}]) 1 10 11
Condition100%3/3
$opts{"min_files"} and not defined $files_info[$opts{"min_files"}]
!ll&&!rl&&r
821
MC/DC100%2/2
$opts{"min_files"} and not defined $files_info[$opts{"min_files"}]
$opts{"min_files"} not defined $files_info[$opts{"min_files"}]
Truth table100%3/3
A and not(B)
A= $opts{"min_files"}B= not defined $files_info[$opts{"min_files"}]
ABresult
0X0
100
111
315 1 $results->{min_files} = $opts{min_files};
316 1 ▶ if ( $opts{use_pid} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"use_pid"}) 0 1 1
317 0 unlink_pid_file($pid_file);
318 }
319 1 return $results;
320 }
321  
322 # sort files oldest to newest based on mtime, numerically
323 10 @files_info = sort { $a->{mtime} <=> $b->{mtime} } @files_info;
324 # save the full, sorted list into the results; the unlink loop below is
325 # bounded by an index so it never touches this array, meaning we don't
326 # need a defensive copy here
327 10 $results->{found_files} = \@files_info;
328  
329 # the newest min_files files are kept regardless of disk usage. As the list
330 # is sorted oldest to newest, those are the last min_files entries, so we
331 # simply stop the removal loop before reaching them rather than removing
332 # them from the array.
333 10 my $min_files = 0;
334 10 ▶ if ( defined( $opts{min_files} ) && $opts{min_files} > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"min_files"} and $opts{"min_files"} > 0) 2 8 10
Condition66%2/3
defined $opts{"min_files"} and $opts{"min_files"} > 0
!ll&&!rl&&r
802
MC/DC50%1/2
defined $opts{"min_files"} and $opts{"min_files"} > 0
defined $opts{"min_files"} $opts{"min_files"} > 0
Truth table66%2/3
A and B
A= defined $opts{"min_files"}B= $opts{"min_files"} > 0
ABresult
0X0
100
111
335 2 $min_files = $opts{min_files};
336 2 $results->{min_files} = $min_files;
337 }
338 # index of the last (oldest end) file eligible for removal
339 10 my $last_removable = $#files_info - $min_files;
340  
341 # go through files and remove the oldest till we drop below the threshold.
342 #
343 # Rather than calling df() after every single unlink (a statvfs syscall each
344 # time, which dominates the loop on high latency filesystems), we estimate
345 # how much space we still need to free from the block counts we already have
346 # and only consult the real df() when the estimate says we should be close.
347 # The real df() remains the authoritative stop condition, so this never
348 # under removes; the $resync cap bounds how far a bad estimate (concurrent
349 # writers, files held open elsewhere) can run us past the target.
350 10 my $per = $df->{per};
351  
352 # bytes the user may occupy, used to translate a percentage into bytes. The
353 # byte mode df() (block size of 1) reports used/bavail in bytes.
354 10 my $df_bytes = $df_func->( $du_path, 1 );
355 10 ▶ my $user_total = ( $df_bytes->{used} || 0 ) + ( $df_bytes->{bavail} || 0 );
Condition75%3/4
$df_bytes->{'used'} || 0
l!l
100
$df_bytes->{'bavail'} || 0
l!l
73
MC/DC0%0/1
$df_bytes->{'used'} || 0
$df_bytes->{'used'}
MC/DC100%1/1
$df_bytes->{'bavail'} || 0
$df_bytes->{'bavail'}
Truth table50%1/2
A
A= $df_bytes->{'used'}
Aresult
11
00
Truth table100%2/2
A
A= $df_bytes->{'bavail'}
Aresult
11
00
356 # estimated bytes still to free to reach the target, and bytes freed since
357 # the last real df() check
358 10 my $need = ( ( $per - $opts{du} ) / 100 ) * $user_total;
359 10 my $freed = 0;
360  
361 10 my $int = 0;
362 10 my $since_resync = 0;
363 10 ▶ while ( $per >= $opts{du} && $int <= $last_removable ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($per >= $opts{"du"} and $int <= $last_removable) 230 10 240
Condition100%3/3
$per >= $opts{"du"} and $int <= $last_removable
!ll&&!rl&&r
28230
MC/DC100%2/2
$per >= $opts{"du"} and $int <= $last_removable
$per >= $opts{"du"} $int <= $last_removable
Truth table100%3/3
A and B
A= $per >= $opts{"du"}B= $int <= $last_removable
ABresult
0X0
100
111
364 230 my $file = $files_info[$int];
365 230 eval {
366 230 ▶ if ( $opts{dry_run} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'dry_run'}) { } 6 224 230
367 # dry run: never remove anything, just verify the file would be
368 # removable by checking it is writable by the current user
369 6 ▶ if ( !-w $file->{name} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-w $file->{"name"}) 1 5 6
370 1 die('file is not writable');
371 }
372 } else {
373 224 ▶ unlink( $file->{name} ) or die($!);
Branch: 1 branch
BranchTrueFalseTotal
unlink $file->{"name"} or die $! 224 0 224
374 }
375  
376 };
377 230 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 1 229 230
378 1 push( @{ $results->{unlink_errors} }, 'Failed to remove "' . $file->{name} . '"... ' . $@ );
379 1 push( @{ $results->{unlink_failed} }, $file );
380 } else {
381 229 push( @{ $results->{unlinked} }, $file );
382 # a failed unlink frees nothing, so only count successful removals
383 229 ▶ $freed += ( $file->{blocks} || 0 ) * 512 unless $opts{dry_run};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"dry_run"} 224 5 229
Condition50%1/2
$file->{'blocks'} || 0
l!l
2240
MC/DC0%0/1
$file->{'blocks'} || 0
$file->{'blocks'}
Truth table50%1/2
A
A= $file->{'blocks'}
Aresult
11
00
384 }
385  
386 230 $int++;
387 230 $since_resync++;
388  
389 # a dry run never changes disk usage, so re-checking df() would loop
390 # forever on the same $per; the index bound above is what stops it.
391 230 ▶ next if $opts{dry_run};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"dry_run"} 6 224 230
392  
393 # consult the real df() only once we estimate we have freed enough, or
394 # once $resync files have gone by, whichever comes first
395 224 ▶ if ( $freed >= $need || $since_resync >= $resync ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($freed >= $need or $since_resync >= $resync) 72 152 224
Condition100%3/3
$freed >= $need or $since_resync >= $resync
l!l&&r!l&&!r
1359152
MC/DC100%2/2
$freed >= $need or $since_resync >= $resync
$freed >= $need $since_resync >= $resync
Truth table100%3/3
A or B
A= $freed >= $needB= $since_resync >= $resync
ABresult
1X1
011
000
396 72 $df = $df_func->($du_path);
397 72 $per = $df->{per};
398 72 $need = ( ( $per - $opts{du} ) / 100 ) * $user_total;
399 72 $freed = 0;
400 72 $since_resync = 0;
401 }
402 } ## end while ( $per >= $opts{du} && $int <= $last_removable )
403  
404 # make sure du_ending reflects real disk usage, not the last estimate
405 10 $df = $df_func->($du_path);
406 10 $results->{du_ending} = $df->{per};
407 10 ▶ if ( defined( $results->{unlinked}[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $results->{"unlinked"}[0]) 9 1 10
408 9 $results->{unlinked_count} = $#{ $results->{unlinked} } + 1;
409 }
410 10 ▶ if ( defined( $results->{unlink_failed}[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $results->{"unlink_failed"}[0]) 1 9 10
411 1 $results->{unlink_failed_count} = $#{ $results->{unlink_failed} } + 1;
412 }
413  
414 10 ▶ if ( $opts{use_pid} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"use_pid"}) 0 10 10
415 0 unlink_pid_file($pid_file);
416 }
417 10 return $results;
418 } ## end sub clean
419  
420 =head1 AUTHOR
421  
422 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
423  
424 =head1 BUGS
425  
426 Please report any bugs or feature requests to C<bug-app-filecleanerbydiskuage at rt.cpan.org>, or through
427 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=App-FileCleanerByDiskUage>. I will be notified, and then you'll
428 automatically be notified of progress on your bug as I make changes.
429  
430  
431  
432  
433 =head1 SUPPORT
434  
435 You can find documentation for this module with the perldoc command.
436  
437 perldoc App::FileCleanerByDiskUage
438  
439  
440 You can also look for information at:
441  
442 =over 4
443  
444 =item * RT: CPAN's request tracker (report bugs here)
445  
446 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=App-FileCleanerByDiskUage>
447  
448 =item * CPAN Ratings
449  
450 L<https://cpanratings.perl.org/d/App-FileCleanerByDiskUage>
451  
452 =item * Search CPAN
453  
454 L<https://metacpan.org/release/App-FileCleanerByDiskUage>
455  
456 =back
457  
458  
459 =head1 ACKNOWLEDGEMENTS
460  
461  
462 =head1 LICENSE AND COPYRIGHT
463  
464 This software is Copyright (c) 2023 by Zane C. Bowers-Hadley.
465  
466 This is free software, licensed under:
467  
468 The GNU Lesser General Public License, Version 3, June 2007
469  
470  
471 =cut
472  
473 3 1; # End of App::FileCleanerByDiskUage
↑ Summary