blib/lib/Filesys/DiskUsage.pm

filter: Statementstmt 96.2% 76 / 79 Branchbran 81.2% 39 / 48 Conditioncond 77.7% 14 / 18 MC/DCmcdc 63.6% 7 / 11 Subroutinesub 100% 6 / 6 Podpod 100% 1 / 1 totaltotal 87.7% 143 / 163 CC 34 SCAR 31.0
CC
34
Cov
89%
du
28.8
_convert
7.8
BEGIN
1.0
CRAP 22.2
↑ 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.13';
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 44 ▶sub du {
Subroutine du: called CC=28 SCAR=33.6
Pod documented
188 # options
189 44 my %config = (
190 'blocks' => 0,
191 'count-dirs' => 0,
192 'dereference' => 0,
193 'exclude' => undef,
194 'human-readable' => 0,
195 'Human-readable' => 0,
196 'make-hash' => 0,
197 'max-depth' => -1,
198 'recursive' => 1,
199 'sector-size' => 1,
200 'show-warnings' => 1,
201 'symlink-size' => undef,
202 'truncate-readable' => 2,
203 );
204 44 ▶ if (ref($_[0]) eq 'HASH') {%config = (%config, %{+shift})}
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq "HASH") 38 6 44
205 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
206  
207 44 my $calling_sub = (caller(1))[3];
208 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
209 21 %all = ();
210 }
211 44 my %sizes;
212  
213 # calculate sizes
214 44 for (@_) {
215 124 ▶ next if exists $all{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $all{$_} 1 123 124
216 123 $all{$_} = 0;
217 123 ▶ if (defined $config{exclude} and -f || -d) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config{"exclude"} and -f $_ || -d $_) 39 84 123
Condition66%4/6
defined $config{"exclude"} and -f $_ || -d $_
!ll&&!rl&&r
84039
-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 table75%3/4
A and B or C
A= defined $config{"exclude"}B= -f $_C= -d $_
ABCresult
0XX0
1000
11X1
1011
218 39 my $filename = basename($_);
219 39 ▶ next if $filename =~ /$config{exclude}/;
Branch: 1 branch
BranchTrueFalseTotal
if $filename =~ /$config{"exclude"}/ 13 26 39
220 }
221 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
222 2 ▶ if ($config{'dereference'}) { # we want to follow it
Branch: 1 branch
BranchTrueFalseTotal
if ($config{'dereference'}) { } 1 1 2
223 $sizes{$_} = du( { 'recursive' => $config{'recursive'},
224 'exclude' => $config{'exclude'},
225 'sector-size' => $config{'sector-size'},
226 'blocks' => $config{'blocks'},
227 1 'dereference' => $config{'dereference'},
228 }, readlink($_));
229 }
230 else {
231 1 ▶ $sizes{$_} = $config{'symlink-size'} if defined $config{'symlink-size'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $config{"symlink-size"} 0 1 1
232 1 next;
233 }
234 }
235 elsif (-f) { # is a file
236 75 my @stat = stat(_);
237 75 ▶ if (defined $stat[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $stat[0]) 75 0 75
238 75 ▶ if ($config{blocks}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config{'blocks'}) { } 0 75 75
239 0 $sizes{$_} = $stat[12] * BYTES_PER_BLOCK;
240 } else {
241 75 $sizes{$_} = $config{'sector-size'} - 1 + $stat[7];
242 75 $sizes{$_} -= $sizes{$_} % $config{'sector-size'};
243 }
244 }
245 }
246 elsif (-d) { # is a directory
247 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
248  
249 24 ▶ if (opendir(my $dh, $_)) {
Branch: 2 branches
BranchTrueFalseTotal
if (opendir my $dh, $_) { } 22 2 24
elsif ($config{'show-warnings'}) { } 2 0 2
250 22 my $dir = $_;
251 22 my @files = readdir $dh;
252 22 closedir($dh);
253 22 ▶ $sizes{$_} += (stat $dir)[7] if $config{'count-dirs'};
Branch: 1 branch
BranchTrueFalseTotal
if $config{"count-dirs"} 0 22 22
254  
255 $sizes{$_} += du( { 'recursive' => $config{'recursive'},
256 'max-depth' => $config{'max-depth'} -1,
257 'exclude' => $config{'exclude'},
258 'sector-size' => $config{'sector-size'},
259 'blocks' => $config{'blocks'},
260 'count-dirs' => $config{'count-dirs'},
261 'show-warnings' => $config{'show-warnings'},
262 'dereference' => $config{'dereference'},
263 22 }, map {"$dir/$_"} grep {! /^\.\.?$/} @files);
264 }
265 elsif ( $config{'show-warnings'} ) {
266 # if the user requests to be notified of non openable directories, notify the user
267 2 warn "could not open $_ ($!)\n";
268 }
269  
270 }
271 }
272 }
273  
274 # return sizes
275 44 ▶ if ( $config{'make-hash'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config{'make-hash'}) { } 1 43 44
276 1 for (keys %sizes) {$sizes{$_} = _convert($sizes{$_}, %config)}
277  
278 1 ▶ return wantarray ? %sizes : \%sizes;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
279 }
280 else {
281 43 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 1 42 43
282 1 return map {_convert($_, %config)} @sizes{@_};
283 }
284 else {
285 42 my $total = 0;
286 42 for (values %sizes) {$total += $_}
287  
288 42 return _convert($total, %config);
289 }
290 }
291  
292 }
293  
294 # convert size to human readable format
295 51 ▶sub _convert {
Subroutine _convert: called CC=7 SCAR=20.5
296 51 ▶ defined (my $size = shift) || return undef;
Branch: 1 branch
BranchTrueFalseTotal
defined(my $size = shift()) or return undef 51 0 51
297 51 my $config = {@_};
298 51 ▶ $config->{human} || return $size;
Branch: 1 branch
BranchTrueFalseTotal
$config->{"human"} or return $size 4 47 51
299 4 ▶ my $block = $config->{'Human-readable'} ? 1000 : 1024;
Branch: 1 branch
BranchTrueFalseTotal
$config->{'Human-readable'} ? : 1 3 4
300 4 my @args = qw/B K M G/;
301  
302 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
303 0 shift @args;
304 0 $size /= $block;
305 }
306  
307 4 ▶ if ($config->{'truncate-readable'} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"truncate-readable"} > 0) 2 2 4
308 2 $size = sprintf("%.$config->{'truncate-readable'}f",$size);
309 }
310  
311 4 "$size$args[0]";
312 }
313  
314 =head1 AUTHOR
315  
316 Jose Castro, C<< <cog@cpan.org> >>
317  
318 =head1 COPYRIGHT & LICENSE
319  
320 Copyright 2004 Jose Castro, All Rights Reserved.
321  
322 This program is free software; you can redistribute it and/or modify it
323 under the same terms as Perl itself.
324  
325 =cut
326  
327 5 1; # End of Filesys::DiskUsage
↑ Summary