blib/lib/Filesys/DiskUsage.pm

filter: Statementstmt 96.1% 75 / 78 Branchbran 82.6% 38 / 46 Conditioncond 83.3% 15 / 18 MC/DCmcdc 63.6% 7 / 11 Subroutinesub 100% 6 / 6 Podpod 100% 1 / 1 totaltotal 88.7% 142 / 160 CC 33 SCAR 30.5
CC
33
Cov
90%
du
27.4
_convert
7.8
BEGIN
1.0
CRAP 21.0
↑ Summary
1 package Filesys::DiskUsage;
2  
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 5 ▶use constant BYTES_PER_BLOCK => 512;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Filesys::DiskUsage - Estimate file space usage (similar to `du`)
13  
14 =cut
15  
16 5 require Exporter;
17  
18 5 our @ISA = qw(Exporter);
19  
20 5 our %EXPORT_TAGS = (
21 'all' => [ qw(
22 du
23 ) ],
24 );
25  
26 5 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
27  
28 5 our @EXPORT = qw(
29 );
30  
31 5 our $VERSION = '0.11';
32  
33 =head1 SYNOPSIS
34  
35 use Filesys::DiskUsage qw/du/;
36  
37 # basic
38 $total = du(qw/file1 file2 directory1/);
39  
40 or
41  
42 # no recursion
43 $total = du( { recursive => 0 } , <*> );
44  
45 or
46  
47 # max-depth is 1
48 $total = du( { 'max-depth' => 1 } , <*> );
49  
50 or
51  
52 # get an array
53 @sizes = du( @files );
54  
55 or
56  
57 # get a hash
58 %sizes = du( { 'make-hash' => 1 }, @files_and_directories );
59  
60 =head1 FUNCTIONS
61  
62 =head2 du
63  
64 Estimate file space usage.
65  
66 Get the size of files:
67  
68 $total = du(qw/file1 file2/);
69  
70 Get the size of directories:
71  
72 $total = du(qw/file1 directory1/);
73  
74 =head3 OPTIONS
75  
76 =over 6
77  
78 =item blocks
79  
80 Return the size based upon the number of blocks that the file occupies,
81 rather than the length of the file. The two values might be different
82 if the file is sparse.
83  
84 This value should match more closely the value returned by the du(1)
85 command.
86  
87 $total = du( { blocks => 1 } , $dir );
88  
89 =item dereference
90  
91 Follow symbolic links. Default is 0. Overrides C<symlink-size>.
92  
93 Get the size of a directory, recursively, following symbolic links:
94  
95 $total = du( { dereference => 1 } , $dir );
96  
97 =item exclude => PATTERN
98  
99 Exclude files that match PATTERN.
100  
101 Get the size of every file except for dot files:
102  
103 $total = du( { exclude => qr/^\./ } , @files );
104  
105 =item human-readable
106  
107 Return sizes in human readable format (e.g., 1K 234M 2G)
108  
109 $total = du ( { 'human-readable' => 1 } , @files );
110  
111 =item Human-readable
112  
113 Return sizes in human readable format, but use powers of 1000 instead
114 of 1024.
115  
116 $total = du ( { 'Human-readable' => 1 } , @files );
117  
118 =item make-hash
119  
120 Return the results in a hash.
121  
122 %sizes = du( { 'make-hash' => 1 } , @files );
123  
124 =item max-depth
125  
126 Sets the max-depth for recursion. A negative number means there is no
127 max-depth. Default is -1.
128  
129 Get the size of every file in the directory and immediate
130 subdirectories:
131  
132 $total = du( { 'max-depth' => 1 } , <*> );
133  
134 =item recursive
135  
136 Sets whether directories are to be explored or not. Set to 0 if you
137 don't want recursion. Default is 1. Overrides C<max-depth>.
138  
139 Get the size of every file in the directory, but not directories:
140  
141 $total = du( { recursive => 0 } , <*> );
142  
143 =item sector-size => NUMBER
144  
145 All file sizes are rounded up to a multiple of this number. Any file
146 that is not an exact multiple of this size will be treated as the next
147 multiple of this number as they would in a sector-based file system. Common
148 values will be 512 or 1024. Default is 1 (no sectors).
149  
150 $total = du( { sector-size => 1024 }, <*> );
151  
152 =item show-warnings => 1 | 0
153  
154 Shows warnings when trying to open a directory that isn't readable.
155  
156 $total = du( { 'show-warnings' => 0 }, <*> );
157  
158 1 by default.
159  
160 =item symlink-size => NUMBER
161  
162 Symlinks are assumed to be this size. Without this option, symlinks are
163 ignored unless dereferenced. Setting this option to 0 will result in the
164 files showing up in the hash, if C<make-hash> is set, with a size of 0.
165 Setting this option to any other number will treat the size of the symlink
166 as this number. This option is ignored if the C<dereference> option is
167 set.
168  
169 $total = du( { symlink-size => 1024, sector-size => 1024 }, <*> );
170  
171 =item truncate-readable => NUMBER
172  
173 Human readable formats decimal places are truncated by the value of
174 this option. A negative number means the result won't be truncated at
175 all. Default if 2.
176  
177 Get the size of a file in human readable format with three decimal
178 places:
179  
180 $size = du( { 'human-readable' => 1 , 'truncate-readable' => 3 } , $file);
181  
182 =back
183  
184 =cut
185  
186 5 my %all;
187 sub du {
188 # options
189 44 ▶ my %config = (
Subroutine du: called CC=27 SCAR=33.1
Pod documented
190 'blocks' => 0,
191 'dereference' => 0,
192 'exclude' => undef,
193 'human-readable' => 0,
194 'Human-readable' => 0,
195 'make-hash' => 0,
196 'max-depth' => -1,
197 'recursive' => 1,
198 'sector-size' => 1,
199 'show-warnings' => 1,
200 'symlink-size' => undef,
201 'truncate-readable' => 2,
202 );
203 44 ▶ if (ref($_[0]) eq 'HASH') {%config = (%config, %{+shift})}
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq "HASH") 38 6 44
204 44 ▶ $config{human} = $config{'human-readable'} || $config{'Human-readable'};
Condition100%3/3
$config{'human-readable'} || $config{'Human-readable'}
l!l&&r!l&&!r
3140
MC/DC100%2/2
$config{'human-readable'} || $config{'Human-readable'}
$config{'human-readable'} $config{'Human-readable'}
Truth table100%3/3
A || B
A= $config{'human-readable'}B= $config{'Human-readable'}
ABresult
1X1
011
000
205  
206 44 my $calling_sub = (caller(1))[3];
207 44 ▶ if (not defined $calling_sub or $calling_sub ne 'Filesys::DiskUsage::du') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $calling_sub or $calling_sub ne "Filesys::DiskUsage::du") 21 23 44
Condition100%3/3
not defined $calling_sub or $calling_sub ne "Filesys::DiskUsage::du"
l!l&&r!l&&!r
19223
MC/DC100%2/2
not defined $calling_sub or $calling_sub ne "Filesys::DiskUsage::du"
not defined $calling_sub $calling_sub ne "Filesys::DiskUsage::du"
Truth table100%3/3
not(A) or B
A= not defined $calling_subB= $calling_sub ne "Filesys::DiskUsage::du"
ABresult
1X1
011
000
208 21 %all = ();
209 }
210 44 my %sizes;
211  
212 # calculate sizes
213 44 for (@_) {
214 124 ▶ next if exists $all{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $all{$_} 1 123 124
215 123 $all{$_} = 0;
216 123 ▶ if (defined $config{exclude} and -f || -d) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config{"exclude"} and -f $_ || -d $_) 39 84 123
Condition83%5/6
defined $config{"exclude"} and -f $_ || -d $_
!ll&&!rl&&r
84831
-f $_ or -d $_
l!l&&r!l&&!r
3090
MC/DC33%1/3
defined $config{"exclude"} and -f $_ || -d $_
defined $config{"exclude"} -f $_ -d $_
Truth table50%2/4
A and B or C
A= defined $config{"exclude"}B= -f $_C= -d $_
ABCresult
0XX0
1000
11X1
1011
217 39 my $filename = basename($_);
218 39 ▶ next if $filename =~ /$config{exclude}/;
Branch: 1 branch
BranchTrueFalseTotal
if $filename =~ /$config{"exclude"}/ 13 26 39
219 }
220 110 ▶ if (-l) { # is symbolic link
Branch: 3 branches
BranchTrueFalseTotal
if (-l $_) { } 2 108 110
elsif (-f $_) { } 75 33 108
elsif (-d $_) { } 33 0 33
221 2 ▶ if ($config{'dereference'}) { # we want to follow it
Branch: 1 branch
BranchTrueFalseTotal
if ($config{'dereference'}) { } 1 1 2
222 $sizes{$_} = du( { 'recursive' => $config{'recursive'},
223 'exclude' => $config{'exclude'},
224 'sector-size' => $config{'sector-size'},
225 'blocks' => $config{'blocks'},
226 1 'dereference' => $config{'dereference'},
227 }, readlink($_));
228 }
229 else {
230 1 ▶ $sizes{$_} = $config{'symlink-size'} if defined $config{'symlink-size'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $config{"symlink-size"} 0 1 1
231 1 next;
232 }
233 }
234 elsif (-f) { # is a file
235 75 my @stat = stat(_);
236 75 ▶ if (defined $stat[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $stat[0]) 75 0 75
237 75 ▶ if ($config{blocks}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config{'blocks'}) { } 0 75 75
238 0 $sizes{$_} = $stat[12] * BYTES_PER_BLOCK;
239 } else {
240 75 $sizes{$_} = $config{'sector-size'} - 1 + $stat[7];
241 75 $sizes{$_} -= $sizes{$_} % $config{'sector-size'};
242 }
243 }
244 }
245 elsif (-d) { # is a directory
246 33 ▶ if ($config{recursive} && $config{'max-depth'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config{"recursive"} and $config{"max-depth"}) 24 9 33
Condition100%3/3
$config{"recursive"} and $config{"max-depth"}
!ll&&!rl&&r
7224
MC/DC100%2/2
$config{"recursive"} and $config{"max-depth"}
$config{"recursive"} $config{"max-depth"}
Truth table100%3/3
A and B
A= $config{"recursive"}B= $config{"max-depth"}
ABresult
0X0
100
111
247  
248 24 ▶ if (opendir(my $dh, $_)) {
Branch: 2 branches
BranchTrueFalseTotal
if (opendir my $dh, $_) { } 22 2 24
elsif ($config{'show-warnings'}) { } 2 0 2
249 22 my $dir = $_;
250 22 my @files = readdir $dh;
251 22 closedir($dh);
252  
253 $sizes{$_} += du( { 'recursive' => $config{'recursive'},
254 'max-depth' => $config{'max-depth'} -1,
255 'exclude' => $config{'exclude'},
256 'sector-size' => $config{'sector-size'},
257 'blocks' => $config{'blocks'},
258 'show-warnings' => $config{'show-warnings'},
259 'dereference' => $config{'dereference'},
260 22 }, map {"$dir/$_"} grep {! /^\.\.?$/} @files);
261 }
262 elsif ( $config{'show-warnings'} ) {
263 # if the user requests to be notified of non openable directories, notify the user
264 2 warn "could not open $_ ($!)\n";
265 }
266  
267 }
268 }
269 }
270  
271 # return sizes
272 44 ▶ if ( $config{'make-hash'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config{'make-hash'}) { } 1 43 44
273 1 for (keys %sizes) {$sizes{$_} = _convert($sizes{$_}, %config)}
274  
275 1 ▶ return wantarray ? %sizes : \%sizes;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
276 }
277 else {
278 43 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 1 42 43
279 1 return map {_convert($_, %config)} @sizes{@_};
280 }
281 else {
282 42 my $total = 0;
283 42 for (values %sizes) {$total += $_}
284  
285 42 return _convert($total, %config);
286 }
287 }
288  
289 }
290  
291 # convert size to human readable format
292 sub _convert {
293 51 ▶ defined (my $size = shift) || return undef;
Subroutine _convert: called CC=7 SCAR=20.5
Branch: 1 branch
BranchTrueFalseTotal
defined(my $size = shift()) or return undef 0 51 51
294 51 my $config = {@_};
295 51 ▶ $config->{human} || return $size;
Branch: 1 branch
BranchTrueFalseTotal
$config->{"human"} or return $size 47 4 51
296 4 ▶ my $block = $config->{'Human-readable'} ? 1000 : 1024;
Branch: 1 branch
BranchTrueFalseTotal
$config->{'Human-readable'} ? : 1 3 4
297 4 my @args = qw/B K M G/;
298  
299 4 ▶ while (@args && $size > $block) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args and $size > $block) 0 4 4
Condition33%1/3
@args and $size > $block
!ll&&!rl&&r
040
MC/DC0%0/2
@args and $size > $block
@args $size > $block
Truth table33%1/3
A and B
A= @argsB= $size > $block
ABresult
0X0
100
111
300 0 shift @args;
301 0 $size /= $block;
302 }
303  
304 4 ▶ if ($config->{'truncate-readable'} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"truncate-readable"} > 0) 2 2 4
305 2 $size = sprintf("%.$config->{'truncate-readable'}f",$size);
306 }
307  
308 4 "$size$args[0]";
309 }
310  
311 =head1 AUTHOR
312  
313 Jose Castro, C<< <cog@cpan.org> >>
314  
315 =head1 COPYRIGHT & LICENSE
316  
317 Copyright 2004 Jose Castro, All Rights Reserved.
318  
319 This program is free software; you can redistribute it and/or modify it
320 under the same terms as Perl itself.
321  
322 =cut
323  
324 5 1; # End of Filesys::DiskUsage
↑ Summary