script/dusage

filter: Statementstmt 85.2% 202 / 237 Branchbran 50.9% 110 / 216 Conditioncond 47.4% 28 / 59 MC/DCmcdc 8.5% 3 / 35 Subroutinesub 90.0% 9 / 10 Podpod n/a totaltotal 63.2% 352 / 557 CC 221 SCAR 43.0
CC
221
Cov
66%
parse_ctl
135.2
parse_ctl
135.2
report_and_update
83.1
CRAP 73.8
↑ Summary blib/lib/App/Dusage.pm »
1 #!/usr/bin/perl -w
2  
3 # dusage.pl -- gather disk usage statistics
4 # Author : Johan Vromans
5 # Created On : Sun Jul 1 21:49:37 1990
6 # Last Modified By: Johan Vromans
7 # Last Modified On: Tue Jun 15 08:32:04 2021
8 # Update Count : 212
9 # Status : OK
10 #
11 # This program requires Perl version 5.10.1, or higher.
12  
13 ################ Common stuff ################
14  
15 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 8 my $my_name = qw( dusage );
18 8 our $VERSION = "2.02";
19  
20 ################ Command line parameters ################
21  
22 3 ▶use Getopt::Long 2.13;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 2 my $verbose = 0; # verbose processing
25 3 my $noupdate = 1; # do not update the control file
26 2 my $retain = 0; # retain emtpy entries
27 7 my $gather = 0; # gather new data
28 8 my $follow = 0; # follow symlinks
29 7 my $allfiles = 0; # also report file stats
30 8 my $allstats = 0; # provide all stats
31  
32 7 my $root; # root of all eveil
33 my $prefix; # root prefix for reporting
34 7 my $data; # the data, or how to get it
35 7 my $table;
36  
37 8 our $runtype; # file or directory
38  
39 # Development options (not shown with -help).
40 8 my $debug = 0; # debugging
41 8 my $trace = 0; # trace (show process)
42 3 my $test = 0; # test (no actual processing)
43  
44 3 ▶unless ( caller ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (caller) 1 7 8
45 0 app_options();
46  
47 # Options post-processing.
48 0 ▶ $trace |= ($debug || $test);
Condition33%1/3
$debug || $test
l!l&&r!l&&!r
001
MC/DC0%0/2
$debug || $test
$debug $test
Truth table33%1/3
A || B
A= $debugB= $test
ABresult
1X1
011
000
49 }
50  
51 ################ Presets ################
52  
53 7 ▶my $TMPDIR = $ENV{TMPDIR} || '/usr/tmp';
Condition66%2/3
$ENV{'TMPDIR'} || '/usr/tmp'
l!l
021
MC/DC0%0/1
$ENV{'TMPDIR'} || '/usr/tmp'
$ENV{'TMPDIR'}
Truth table50%1/2
A
A= $ENV{'TMPDIR'}
Aresult
11
00
54  
55 ################ The Process ################
56  
57 7 my @targets = (); # directories to process, and more
58 7 my %newblocks = (); # du values
59 7 my %oldblocks = (); # previous values
60 7 my @excludes = (); # excluded entries
61 7 my %testglob;
62  
63 7 ▶unless ( caller ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (caller) 0 8 8
64 5 parse_ctl(); # read the control file
65 5 gather(); # gather new info
66 5 report_and_update(); # write report and update control file
67 }
68  
69 ################ Subroutines ################
70  
71 1 ▶sub parse_ctl {
Subroutine parse_ctl: called CC=27 SCAR=49.1
72  
73 # Parsing the control file.
74 #
75 # This file contains the names of the (sub)directories to tally,
76 # and the values dereived from previous runs.
77 # The names of the directories are relative to the $root.
78 # The name may contain '*' or '?' characters, and will be globbed if so.
79 # An entry starting with ! is excluded.
80 #
81 # To add a new dir, just add the name. The special name '.' may
82 # be used to denote the $root directory. If used, '-p' must be
83 # specified.
84 #
85 # Upon completion:
86 # - %oldblocks is filled with the previous values,
87 # colon separated, for each directory.
88 # - @targets contains a list of names to be looked for. These include
89 # break indications and globs info, which will be stripped from
90 # the actual search list.
91  
92 1 my $tb; # ctl file entry
93  
94 1 ▶ open( my $ctl, "<", $table )
Branch: 2 branches
BranchTrueFalseTotal
open my $ctl, "<", $table or die "Cannot open control file ${table}: $!\n" 1 0 1
open my $ctl, "<", $table or die "Cannot open control file ${table}: $!\n" 0 1 1
95 or die ("Cannot open control file $table: $!\n");
96  
97 1 ▶ while ( $tb = <$ctl> ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined($tb = readline $ctl)) 4 1 5
if (defined($tb = readline $ctl)) 0 0 0
98  
99 # For testing. Please ignore.
100 4 ▶ if ( $tb =~ /^# glob\s+(.*?)\s+->\s+(.+)/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($tb =~ /^# glob\s+(.*?)\s+->\s+(.+)/) 2 2 4
if ($tb =~ /^# glob\s+(.*?)\s+->\s+(.+)/) 5 0 5
101 2 $testglob{$1} = $2;
102 }
103  
104 4 ▶ next if $tb =~ /^#/;
Branch: 2 branches
BranchTrueFalseTotal
if $tb =~ /^#/ 2 2 4
if $tb =~ /^#/ 0 6 6
105 2 ▶ next unless $tb =~ /\S/;
Branch: 2 branches
BranchTrueFalseTotal
unless $tb =~ /\S/ 0 2 2
unless $tb =~ /\S/ 5 1 6
106  
107 # syntax: <dir><TAB><size>:<size>:....
108 # possible <dir>
109  
110 2 ▶ if ( $tb =~ /^-(?!\t)(.*)/ ) { # break
Branch: 2 branches
BranchTrueFalseTotal
if ($tb =~ /^-(?!\t)(.*)/) 0 2 2
if ($tb =~ /^-(?!\t)(.*)/) 5 0 5
111 0 push (@targets, "-$1");
112 0 ▶ print STDERR ("tb: *break* $1\n") if $debug;
Branch: 2 branches
BranchTrueFalseTotal
if $debug 0 0 0
if $debug 6 0 6
113 0 next;
114 }
115  
116 2 ▶ if ( $tb =~ /^!(.*)/ ) { # exclude
Branch: 2 branches
BranchTrueFalseTotal
if ($tb =~ /^!(.*)/) 0 2 2
if ($tb =~ /^!(.*)/) 1 5 6
117 0 push (@excludes, $1);
118 0 push (@targets, "!".$1);
119 0 ▶ print STDERR ("tb: *excl* $1\n") if $debug;
Branch: 2 branches
BranchTrueFalseTotal
if $debug 0 0 0
if $debug 0 6 6
120 0 next;
121 }
122  
123 2 my @blocks;
124 my $name;
125 2 ▶ if ( $tb =~ /^(.+)\t([\d:]+)/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($tb =~ /^(.+)\t([\d:]+)/) { } 0 2 2
if ($tb =~ /^(.+)\t([\d:]+)/) { } 0 0 0
126 0 $name = $1;
127 0 @blocks = split (/:/, $2 . "::::::::", -1);
128 0 $#blocks = 7;
129 }
130 else {
131 2 chomp ($name = $tb);
132 2 @blocks = ("") x 8;
133 }
134  
135 2 ▶ if ( $name eq "." ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($name eq '.') { } 0 2 2
if ($name eq '.') { } 1 0 1
136 0 ▶ if ( $root eq "" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($root eq "") 0 0 0
if ($root eq "") 0 0 0
137 0 warn ("Warning: \".\" in control file w/o \"-p path\" - ignored\n");
138 0 next;
139 }
140 0 $name = $root;
141 }
142 else {
143 2 ▶ $name = $prefix . $name unless ord($name) == ord ("/");
Branch: 2 branches
BranchTrueFalseTotal
unless ord $name == 47 2 0 2
unless ord $name == 47 0 0 0
144 }
145  
146 # Check for globs ...
147 # if ( ($gather|$debug|%testglob) && $name =~ /\*|\?/ ) {
148 2 ▶ if ( $name =~ /\*|\?/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($name =~ /\*|\?/) 2 0 2
if ($name =~ /\*|\?/) 0 0 0
149 2 ▶ print STDERR ("glob: $name\n") if $debug;
Branch: 2 branches
BranchTrueFalseTotal
if $debug 0 2 2
if $debug 0 0 0
150 my @glob = $testglob{$name}
151 2 ▶ ? split( ' ', $testglob{$name} )
Branch: 2 branches
BranchTrueFalseTotal
$testglob{$name} ? : 2 0 2
$testglob{$name} ? : 0 0 0
152 2 ▶ : glob($name);
Subroutine BEGIN: called CC=1 SCAR=0.0
153 2 foreach my $n ( @glob ) {
154 5 ▶ next unless $allfiles || -d $n;
Branch: 1 branch
BranchTrueFalseTotal
unless $allfiles or -d $n 0 5 5
Condition33%2/6
$allfiles or -d $n
l!l&&r!l&&!r
050
$allfiles or -d $n
l!l&&r!l&&!r
001
MC/DC0%0/2
$allfiles or -d $n
$allfiles -d $n
MC/DC0%0/2
$allfiles or -d $n
$allfiles -d $n
Truth table33%1/3
A or B
A= $allfilesB= -d $n
ABresult
1X1
011
000
Truth table33%1/3
A or B
A= $allfilesB= -d $n
ABresult
1X1
011
000
155 # Globs never overwrite existing entries
156 5 ▶ unless ( defined $oldblocks{$n} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $oldblocks{$n}) 5 0 5
157 5 $oldblocks{$n} = ":::::::";
158 5 push (@targets, " $n");
159 }
160 5 ▶ print STDERR ("glob: -> $n\n") if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 5 5
161 }
162 # Put on the globs list, and terminate this entry
163 2 push (@targets, "*$name");
164 2 next;
165 }
166  
167 0 push (@targets, " $name");
168  
169 # Entry may be rewritten (in case of globs)
170 0 $oldblocks{$name} = join (":", @blocks[0..7]);
171  
172 0 ▶ print STDERR ("tb: $name\t$oldblocks{$name}\n") if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
173 }
174  
175 1 ▶ if ( @excludes ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@excludes) 0 1 1
176 0 foreach my $excl ( @excludes ) {
177 0 ▶ my $try = ord($excl) == ord("/") ? " $excl" : " $prefix$excl";
Branch: 1 branch
BranchTrueFalseTotal
ord $excl == 47 ? : 0 0 0
178 0 @targets = grep ($_ ne $try, @targets);
179 }
180 0 ▶ print STDERR ("targets after exclusion: @targets\n") if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
181 }
182  
183 1 close ($ctl);
184 }
185  
186 2 ▶sub gather {
Subroutine gather: called CC=24 SCAR=35.0
187  
188 # Build a targets match string, and an optimized list of
189 # directories to search. For example, if /foo and /foo/bar are
190 # both in the list, only /foo is used since du will produce the
191 # statistics for /foo/bar as well.
192  
193 2 my %targets = ();
194 2 my @list = ();
195 # Get all entries, and change the / to nul chars.
196 14 ▶ my @a = map { s;/;\0;g ? ($_) : ($_) }
Branch: 1 branch
BranchTrueFalseTotal
s[/][\000]g ? : 2 4 6
197 # Only dirs unless $allfiles
198 7 ▶ grep { $allfiles || -d }
Condition33%2/6
$allfiles || -d $_
l!l&&r!l&&!r
0100
$allfiles || -d $_
l!l&&r!l&&!r
100
MC/DC0%0/2
$allfiles || -d $_
$allfiles -d $_
MC/DC0%0/2
$allfiles || -d $_
$allfiles -d $_
Truth table33%1/3
A || B
A= $allfilesB= -d $_
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= $allfilesB= -d $_
ABresult
1X1
011
000
199 # And only the file/dir info entries
200 2 ▶ map { /^ (.*)/ ? $1 : () } @targets;
Branch: 1 branch
BranchTrueFalseTotal
/^ (.*)/ ? : 14 3 17
201  
202 8 my $prev = "\0\0\0";
203 8 foreach my $name ( sort (@a) ) {
204 # If $prev is a complete prefix of $name, we've already got a
205 # better one in the tables.
206 5 ▶ unless ( index ($name, $prev) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (index($name, $prev) == 0) 6 8 14
207 # New test arg -- including the trailing nul.
208 4 $prev = $name . "\0";
209 # Back to normal.
210 4 $name =~ s;\0;/;g;
211 # Register.
212 11 push (@list, $name);
213 4 $targets{$name}++;
214 }
215  
216 }
217  
218 1 ▶ if ( $debug ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 2 8 10
219 0 print STDERR ("dirs: ", join(" ",sort(keys(%targets))),"\n",
220 "list: @list\n");
221 }
222  
223 1 my $fh = do { local(*FH); *FH };
224 6 my $out = do { local(*FH); *FH };
225 3 ▶ if ( !$gather && defined $data ) { # we have a data file
Branch: 1 branch
BranchTrueFalseTotal
if (not $gather and defined $data) { } 1 7 8
Condition33%2/6
not $gather and defined $data
!ll&&!rl&&r
001
not $gather and defined $data
!ll&&!rl&&r
010
MC/DC0%0/2
not $gather and defined $data
not $gather defined $data
MC/DC0%0/2
not $gather and defined $data
not $gather defined $data
Truth table33%1/3
not(A) and B
A= not $gatherB= defined $data
ABresult
0X0
100
111
Truth table33%1/3
not(A) and B
A= not $gatherB= defined $data
ABresult
0X0
100
111
226 3 ▶ print STDERR ("Using data from $data\n" ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 8 8
227 8 ▶ open( $fh, "<", $data )
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $data or die "Cannot get data from ${data}: $!\n" 1 0 1
228 or die ("Cannot get data from $data: $!\n");
229 1 undef $data;
230 1 $gather++;
231 }
232 else {
233 0 my @du = ("du");
234 0 ▶ push (@du, "-a") if $allfiles;
Branch: 1 branch
BranchTrueFalseTotal
if $allfiles 0 7 7
235 7 ▶ push( @du, "-L" ) if $follow;
Branch: 1 branch
BranchTrueFalseTotal
if $follow 0 0 0
236 7 push (@du, "--", @list);
237 2 ▶ print STDERR ("Gather data from @du\n" ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 5 2 7
238 2 ▶ my $ret = open( $fh, "-|" ) || exec @du;
Condition0%0/2
open($fh, '-|') || exec(@du)
l!l
00
MC/DC0%0/1
open($fh, '-|') || exec(@du)
open($fh, '-|')
Truth table0%0/2
A
A= open($fh, '-|')
Aresult
11
00
239 2 ▶ die ("Cannot get input from -| @du\n") unless $ret;
Branch: 1 branch
BranchTrueFalseTotal
unless $ret 0 7 7
240 5 ▶ if ( defined $data ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data) 0 0 0
241 5 ▶ open($out, ">", $data) or die ("Cannot create $data: $!\n");
Branch: 1 branch
BranchTrueFalseTotal
open $out, ">", $data or die "Cannot create ${data}: $!\n" 7 0 7
242 }
243 }
244  
245 # Process the data. If a name is found in the target list,
246 # %newblocks will be set to the new blocks value.
247 1 %targets = map { $_ => 1 } @targets;
248 6 my %excludes = map { $prefix.$_ => 1 } @excludes;
249 3 my $du;
250 6 ▶ while ( defined ($du = <$fh>) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($du = readline $fh)) 20 6 26
251 23 ▶ print $out $du if defined $data;
Branch: 1 branch
BranchTrueFalseTotal
if defined $data 0 20 20
252 23 chomp ($du);
253 19 my ($blocks, $name) = split (/\t/, $du);
254 18 ▶ if ( exists ($targets{" ".$name}) && !exists ($excludes{$name}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $targets{' ' . $name} and not exists $excludes{$name}) 7 13 20
Condition66%2/3
exists $targets{' ' . $name} and not exists $excludes{$name}
!ll&&!rl&&r
1305
MC/DC50%1/2
exists $targets{' ' . $name} and not exists $excludes{$name}
exists $targets{' ' . $name} not exists $excludes{$name}
Truth table66%2/3
A and not(B)
A= exists $targets{' ' . $name}B= not exists $excludes{$name}
ABresult
0X0
100
111
255 # Tally and remove entry from search list.
256 5 $newblocks{$name} = $blocks;
257 5 ▶ print STDERR ("du: $name $blocks\n") if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 10 10
258 5 delete ($targets{" ".$name});
259 }
260 }
261 2 close ($fh);
262 1 ▶ close ($out) if defined $data;
Branch: 1 branch
BranchTrueFalseTotal
if defined $data 0 6 6
263 }
264  
265 # Variables used in the formats.
266 7 our $date; # date
267 2 our $name; # name
268 2 our $subtitle; # subtitle
269 7 our @a;
270 7 our $d_day; # day delta
271 3 our $d_week; # week delta
272 2 our $blocks;
273  
274 2 ▶sub report_and_update {
Subroutine report_and_update: called CC=39 SCAR=44.2
275 2 ▶ my $rep = shift || \*STDOUT;
Condition66%2/3
shift() || \*STDOUT
l!l
150
MC/DC100%1/1
shift() || \*STDOUT
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
276 2 select($rep);
277  
278 2 my $ctl;
279  
280 # Prepare update of the control file
281 6 ▶ unless ( $noupdate ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($noupdate) 2 9 11
282 6 ▶ unless ( open( $ctl, ">", $table ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $ctl, ">", $table) 5 8 13
283 7 warn ("Warning: cannot update control file $table [$!] - continuing\n");
284 1 $noupdate = 1;
285 }
286 }
287  
288 # For testing. Please ignore.
289 2 ▶ if ( !$noupdate && %testglob ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $noupdate and %testglob) 5 2 7
Condition33%1/3
not $noupdate and %testglob
!ll&&!rl&&r
002
MC/DC0%0/2
not $noupdate and %testglob
not $noupdate %testglob
Truth table33%1/3
not(A) and B
A= not $noupdateB= %testglob
ABresult
0X0
100
111
290 6 foreach my $k ( sort keys %testglob ) {
291 6 print $ctl "# glob $k -> $testglob{$k}\n";
292 }
293 }
294  
295 5 ▶ if ( $allstats ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($allstats) { } 0 2 2
296 4 $^ = "all_hdr";
297 4 $~ = "all_out";
298 }
299 else {
300 2 $^ = "std_hdr";
301 1 $~ = "std_out";
302 }
303  
304 2 $date = localtime;
305 2 $subtitle = "";
306  
307 # In one pass the report is generated, and the control file rewritten.
308  
309 2 foreach my $nam ( @targets ) {
310  
311 8 ▶ if ( $nam =~ /^-(.*)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nam =~ /^-(.*)/) 1 7 8
312 1 $subtitle = $1;
313 1 ▶ print $ctl ($nam, "\n") unless $noupdate;
Branch: 1 branch
BranchTrueFalseTotal
unless $noupdate 0 1 1
314 1 ▶ print STDERR ("tb: $nam\n") if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 1 0 1
315 1 $- = 0; # force page feed
316 1 next;
317 }
318  
319 8 ▶ if ($nam =~ /^\*\Q$prefix\E(.*)/o ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nam =~ /^\*\Q$prefix\E(.*)/o) 2 5 7
320 3 ▶ print $ctl ("$1\n") unless $noupdate;
Branch: 1 branch
BranchTrueFalseTotal
unless $noupdate 2 0 2
321 2 ▶ print STDERR ("tb: $1\n") if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 2 2
322 2 next;
323 }
324  
325 5 ▶ if ( $nam =~ /^ (.*)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nam =~ /^ (.*)/) { } 5 0 5
326 5 $nam = $1
327 }
328 else {
329 0 ▶ print $ctl $nam, "\n" unless $noupdate;
Branch: 1 branch
BranchTrueFalseTotal
unless $noupdate 0 0 0
330 0 ▶ print STDERR ("tb: $nam\n") if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
331 0 next;
332 }
333  
334 warn("Oops1 $nam\n"), next
335 5 ▶ unless $nam =~ /\*/ || defined $oldblocks{$nam};
Branch: 1 branch
BranchTrueFalseTotal
unless $nam =~ /\*/ or defined $oldblocks{$nam} 18 6 24
Condition33%1/3
$nam =~ /\*/ or defined $oldblocks{$nam}
l!l&&r!l&&!r
050
MC/DC0%0/2
$nam =~ /\*/ or defined $oldblocks{$nam}
$nam =~ /\*/ defined $oldblocks{$nam}
Truth table33%1/3
A or B
A= $nam =~ /\*/B= defined $oldblocks{$nam}
ABresult
1X1
011
000
336 warn("Oops2 $nam\n"), next
337 5 ▶ unless $nam =~ /\*/ || defined $newblocks{$nam};
Branch: 1 branch
BranchTrueFalseTotal
unless $nam =~ /\*/ or defined $newblocks{$nam} 0 23 23
Condition100%3/3
$nam =~ /\*/ or defined $newblocks{$nam}
l!l&&r!l&&!r
1355
MC/DC0%0/2
$nam =~ /\*/ or defined $newblocks{$nam}
$nam =~ /\*/ defined $newblocks{$nam}
Truth table33%1/3
A or B
A= $nam =~ /\*/B= defined $newblocks{$nam}
ABresult
1X1
011
000
338  
339 6 @a = split (/:/, $oldblocks{$nam} . ":::::::", -1);
340 12 $#a = 7;
341 6 ▶ unshift (@a, $newblocks{$nam}) if $gather;
Branch: 1 branch
BranchTrueFalseTotal
if $gather 10 13 23
342 5 ▶ $nam = "." if $nam eq $root;
Branch: 1 branch
BranchTrueFalseTotal
if $nam eq $root 0 10 10
343 6 ▶ $nam = $1 if $nam =~ /^\Q$prefix\E(.*)/o;
Branch: 1 branch
BranchTrueFalseTotal
if $nam =~ /^\Q$prefix\E(.*)/o 5 1 6
344 6 ▶ warn ("Warning: ", scalar(@a), " entries for $nam\n")
Branch: 1 branch
BranchTrueFalseTotal
if $debug and @main::a != 9 0 5 5
Condition33%1/3
$debug and @main::a != 9
!ll&&!rl&&r
500
MC/DC0%0/2
$debug and @main::a != 9
$debug @main::a != 9
Truth table33%1/3
A and B
A= $debugB= @main::a != 9
ABresult
0X0
100
111
345 if $debug && @a != 9;
346  
347 # check for valid data
348 23 my $try = join (":", @a[0..7]);
349 23 ▶ if ( $try eq ":::::::" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($try eq ":::::::") 0 5 5
350 18 ▶ if ($retain) {
Branch: 1 branch
BranchTrueFalseTotal
if ($retain) { } 0 0 0
351 18 @a = ("") x 8;
352 }
353 else {
354 # Discard.
355 5 ▶ print STDERR ("--: $nam\n") if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
356 5 next;
357 }
358 }
359  
360 10 my $line = "$nam\t$try\n";
361 6 ▶ print $ctl ($line) unless $noupdate;
Branch: 1 branch
BranchTrueFalseTotal
unless $noupdate 5 0 5
362 6 ▶ print STDERR ("tb: $line") if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 5 5
363  
364 5 $blocks = $a[0];
365 5 ▶ unless ( $allstats ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($allstats) 5 0 5
366 5 $d_day = $d_week = "";
367 5 ▶ if ( $blocks ne "" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::blocks ne "") 5 0 5
368 5 ▶ if ( $a[1] ne "" ) { # daily delta
Branch: 1 branch
BranchTrueFalseTotal
if ($main::a[1] ne "") 0 5 5
369 0 $d_day = $blocks - $a[1];
370 0 ▶ $d_day = "+" . $d_day if $d_day > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $main::d_day > 0 0 0 0
371 }
372 5 ▶ if ( $a[7] ne "" ) { # weekly delta
Branch: 1 branch
BranchTrueFalseTotal
if ($main::a[7] ne "") 0 5 5
373 0 $d_week = $blocks - $a[7];
374 0 ▶ $d_week = "+" . $d_week if $d_week > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $main::d_week > 0 0 0 0
375 }
376 }
377 }
378  
379 # Using a outer my variable that is aliased in a loop within a
380 # subroutine still doesn't work...
381 5 $name = $nam;
382 5 write($rep);
383 }
384  
385 # Close control file, if opened
386 1 ▶ close ($ctl) unless $noupdate;
Branch: 1 branch
BranchTrueFalseTotal
unless $noupdate 1 0 1
387 }
388  
389 ################ Option Processing ################
390  
391 2 ▶sub app_options {
Subroutine app_options: called CC=24 SCAR=37.4
392 2 my $help = 0; # handled locally
393 2 my $ident = 0; # handled locally
394 2 my $man = 0; # handled locally
395  
396 1 ▶ my $pod2usage = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
397 # Load Pod::Usage only if needed.
398 1 require Pod::Usage;
399 8 Pod::Usage->import;
400 3 &pod2usage;
401 2 };
402  
403 2 Getopt::Long::Configure qw(bundling);
404 GetOptions(
405 'allstats|a' => \$allstats,
406 'allfiles|f' => \$allfiles,
407 'gather|g' => \$gather,
408 'follow|L' => \$follow,
409 'retain|r' => \$retain,
410 3 ▶ 'update!' => sub { $noupdate = !$_[1] },
Subroutine __ANON__: not called CC=1 SCAR=0.0
411 4 ▶ 'u' => sub { $noupdate = !$_[1] },
Subroutine __ANON__: called CC=1 SCAR=0.0
412 1 ▶ 'data|i=s' => \$data,
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("allstats|a", \$allstats, "allfiles|f", \$allfiles, "gather|g", \$gather, "follow|L", \$follow, "retain|r", \$retain, "update!", sub { $noupdate = !$_[1]; } , "u", sub { $noupdate = !$_[1]; } , "data|i=s", \$data, "dir|p=s", \$root, "verbose|v", \$verbose, "trace", \$trace, "help|h|?", \$help, "man", \$man, "debug", \$debug) or &$pod2usage(2) 2 0 2
413 'dir|p=s' => \$root,
414 'verbose|v' => \$verbose,
415 'trace' => \$trace,
416 'help|h|?' => \$help,
417 'man' => \$man,
418 'debug' => \$debug,
419 ) or $pod2usage->(2);
420  
421 1 ▶ if ( $ident or $help or $man ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ident or $help or $man) 0 2 2
Condition50%3/6
$ident or $help or $man
l!l&&r!l&&!r
101
$ident or $help
l!l&&r!l&&!r
002
MC/DC0%0/3
$ident or $help or $man
$ident $help $man
Truth table25%1/4
A or B or C
A= $identB= $helpC= $man
ABCresult
1XX1
01X1
0011
0000
422 1 print STDERR ("This is $my_name $VERSION\n");
423 }
424 2 ▶ if ( $man or $help ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($man or $help) 1 1 2
Condition66%2/3
$man or $help
l!l&&r!l&&!r
051
MC/DC50%1/2
$man or $help
$man $help
Truth table66%2/3
A or B
A= $manB= $help
ABresult
1X1
011
000
425 2 ▶ $pod2usage->(1) if $help;
Branch: 1 branch
BranchTrueFalseTotal
if $help 0 1 1
426 1 ▶ $pod2usage->(VERBOSE => 2) if $man;
Branch: 1 branch
BranchTrueFalseTotal
if $man 0 7 7
427 }
428 1 ▶ if ( @ARGV > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ARGV > 1) 0 1 1
429 0 $pod2usage->(2);
430 }
431  
432 2 ▶ if ( defined $root ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $root) { } 0 1 1
433 1 $root =~ s;/+$;;;
434 1 $prefix = $root . "/";
435 1 ▶ $root = "/" if $root eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $root eq "" 2 5 7
436 }
437 else {
438 2 $prefix = $root = "";
439 }
440  
441 8 ▶ $table = @ARGV ? shift(@ARGV) : $prefix . ".du.ctl";
Branch: 1 branch
BranchTrueFalseTotal
@ARGV ? : 3 0 3
442 1 ▶ $runtype = $allfiles ? "file" : "directory";
Branch: 1 branch
BranchTrueFalseTotal
$allfiles ? : 0 3 3
443 1 ▶ $noupdate |= !$gather && ! $data && ! -s $data;
Condition66%4/6
$gather || $data || -s $data
l!l&&r!l&&!r
150
$gather or $data
l!l&&r!l&&!r
510
MC/DC0%0/3
$gather || $data || -s $data
$gather $data -s $data
Truth table25%1/4
A or B || C
A= $gatherB= $dataC= -s $data
ABCresult
1XX1
01X1
0011
0000
444  
445 1 ▶ if ( $debug ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 5 1 6
446 0 ▶ print STDERR
Branch: 7 branches
BranchTrueFalseTotal
$debug ? : 0 0 0
$noupdate ? : 0 0 0
$retain ? : 0 5 5
$gather ? : 0 5 5
$allstats ? : 5 0 5
$data ? : 0 5 5
$gather ? : 5 0 5
447 ("$my_name $VERSION\n",
448 "Options:",
449 $debug ? " debug" : "" , # silly, isn't it...
450 $noupdate ? " no" : " " , "update",
451 $retain ? " " : " no", "retain",
452 $gather ? " " : " no", "gather",
453 $allstats ? " " : " no", "allstats",
454 "\n",
455 "Root = \"$root\", prefix = \"$prefix\"\n",
456 "Control file = \"$table\"\n",
457 $data ? (($gather ? "Output" : "Input") ." data = \"$data\"\n") : "",
458 "Run type = \"$runtype\"\n",
459 "\n");
460 }
461 }
462  
463 # Formats.
464  
465 format std_hdr =
466 Disk usage statistics@<<<<<<<<<<<<<<<<<<<<<@<<<<<<<<<<<<<<<
467 $subtitle, $date
468  
469 blocks +day +week @<<<<<<<<<<<<<<<
470 $runtype
471 -------- ------- ------- --------------------------------
472 .
473  
474 format std_out =
475 @>>>>>>> @>>>>>>> @>>>>>>> ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
476 $blocks, $d_day, $d_week, $name
477 .
478  
479 format all_hdr =
480 Disk usage statistics@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<
481 $subtitle, $date
482  
483 --0-- --1-- --2-- --3-- --4-- --5-- --6-- --7-- @<<<<<<<<<<<<<<<
484 $runtype
485 ------- ------- ------- ------- ------- ------- ------- ------- --------------------------------
486 .
487 format all_out =
488 @>>>>>> @>>>>>>> @>>>>>>> @>>>>>>> @>>>>>>> @>>>>>>> @>>>>>>> @>>>>>>> ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<..
489 @a, $name
490 .
491  
492 2 1;
493  
494 # Documentation appended from App::Dusage.
495  
496 =head1 NAME
497  
498 dusage - provide disk usage statistics
499  
500 =head1 SYNOPSIS
501  
502 dusage [options] ctlfile
503  
504 -a --allstats provide all statis
505 -f --allfiles also report file statistics
506 -g --gather gather new data
507 -i input --data=input input data as obtained by 'du dir'
508 or output with -g
509 -p dir --dir=dir path to which files in the ctlfile are relative
510 -r --retain do not discard entries which do not have data
511 -u --update update the control file with new values
512 -L resolve symlinks
513 -h --help this help message
514 --man show complete documentation
515 --debug provide debugging info
516  
517 ctlfile file which controls which dirs to report
518 default is dir/.du.ctl
519  
520 =head1 DESCRIPTION
521  
522 Ever wondered why your free disk space gradually decreases? This
523 program may provide you with some useful clues.
524  
525 B<dusage> is a Perl program which produces disk usage statistics.
526 These statistics include the number of blocks that files or
527 directories occupy, the increment since the previous run (which is
528 assumed to be the day before if run daily), and the increment since 7
529 runs ago (which could be interpreted as a week, if run daily).
530  
531 B<dusage> is driven by a control file that describes the names of the
532 files (directories) to be reported. It also contains the results of
533 previous runs.
534  
535 When B<dusage> is run, it reads the control file, optionally gathers
536 new disk usage values by calling the B<du> program, prints the report,
537 and optionally updates the control file with the new information.
538  
539 Filenames in the control file may have wildcards. In this case, the
540 wildcards are expanded, and all entries reported. Both the expanded
541 names as the wildcard info are maintained in the control file. New
542 files in these directories will automatically show up, deleted files
543 will disappear when they have run out of data in the control file (but
544 see the B<-r> option).
545  
546 Wildcard expansion only adds filenames that are not already on the list.
547  
548 The control file may also contain filenames preceded with an
549 exclamation mark C<!>; these entries are skipped. This is meaningful
550 in conjunction with wildcards, to exclude entries which result from a
551 wildcard expansion.
552  
553 The control file may have lines starting with a dash C<-> that is
554 I<not> followed by a C<Tab>, which will cause the report to start a
555 new page here. Any text following the dash is placed in the page
556 header, immediately following the text ``Disk usage statistics''.
557  
558 The available command line options are:
559  
560 =over 4
561  
562 =item B<-a> B<--allstats>
563  
564 Reports the statistics for this and all previous runs, as opposed to
565 the normal case, which is to generate the statistics for this run, and
566 the differences between the previous and 7th previous run.
567  
568 =item B<-f> B<--allfiles>
569  
570 Reports file statistics also. Default is to only report directories.
571  
572 =item B<-g> B<--gather>
573  
574 Gathers new data by calling the B<du> program. See also the C<-i>
575 (B<--data>) option below.
576  
577 =item B<-i> I<file> or <--data> I<file>
578  
579 With B<-g> (B<--gather>), write the obtained raw info (the output of the B<du> program) to this file for subsequent use.
580  
581 Without B<-g> (B<--gather>), a data file written in a previous run is reused.
582  
583 =item B<-p> I<dir> or B<--dir> I<dir>
584  
585 All filenames in the control file are interpreted relative to this
586 directory.
587  
588 =item B<-L> B<--follow>
589  
590 Follow symbolic links.
591  
592 =item B<-r> B<--retain>
593  
594 Normally, entries that do not have any data anymore are discarded.
595 If this option is used, these entries will be retained in the control file.
596  
597 =item B<-u> B<--update>
598  
599 Update the control file with new values. Only effective if B<-g>
600 (B<--gather>) is also supplied.
601  
602 =item B<-h> B<--help> B<-?>
603  
604 Provides a help message. No work is done.
605  
606 =item B<--man>
607  
608 Provides the complete documentation. No work is done.
609  
610 =item B<--debug>
611  
612 Turns on debugging, which yields lots of trace information.
613  
614 =back
615  
616 The default name for the control file is
617 I<.du.ctl>, optionally preceded by the name supplied with the
618 B<-p> (B<--dir>) option.
619  
620 =head1 EXAMPLES
621  
622 Given the following control file:
623  
624 - for manual pages
625 maildir
626 maildir/*
627 !maildir/unimportant
628 src
629  
630 This will generate the following (example) report when running the
631 command ``dusage -gu controlfile'':
632  
633 Disk usage statistics for manual pages Wed Nov 23 22:15:14 2000
634  
635 blocks +day +week directory
636 ------- ------- ------- --------------------------------
637 6518 maildir
638 2 maildir/dirent
639 498 src
640  
641 After updating the control file, it will contain:
642  
643 - for manual pages
644 maildir 6518::::::
645 maildir/dirent 2::::::
646 maildir/*
647 !maildir/unimportant
648 src 498::::::
649  
650 The names in the control file are separated by the values with a C<Tab>;
651 the values are separated by colons. Also, the entries found by
652 expanding the wildcard are added. If the wildcard expansion had
653 generated a name ``maildir/unimportant'' it would have been skipped.
654  
655 When the program is rerun after one day, it could print the following
656 report:
657  
658 Disk usage statistics for manual pages Thu Nov 23 17:25:44 2000
659  
660 blocks +day +week directory
661 ------- ------- ------- --------------------------------
662 6524 +6 maildir
663 2 0 maildir/dirent
664 486 -12 src
665  
666 The control file will contain:
667  
668 - for manual pages
669 maildir 6524:6518:::::
670 maildir/dirent 2:2:::::
671 maildir/*
672 !maildir/unimportant
673 src 486:498:::::
674  
675 It takes very little fantasy to imagine what will happen on subsequent
676 runs...
677  
678 When the contents of the control file are to be changed, e.g. to add
679 new filenames, a normal text editor can be used. Just add or remove
680 lines, and they will be taken into account automatically.
681  
682 When run without B<-g> (B<--gather>) option, it reproduces the report
683 from the previous run.
684  
685 When multiple runs are required, save the output of the B<du> program
686 in a file, and pass this file to B<dusage> using the B<-i> (B<--data>)
687 option.
688  
689 Running the same control file with differing values of the B<-f>
690 (B<--allfiles>) or B<-r> (B<--retain>) options may cause strange
691 results.
692  
693 =head1 CAVEATS
694  
695 The program will screw up when you have filenames with newlines in
696 them. This must be considered an early warning that there may be
697 some serious troubles ahead.
698  
699 =head1 COMPATIBILITY NOTICE
700  
701 This program is rewritten for Perl 5.005 and later. However, it is
702 still fully backward compatible with its 1990 predecessor.
703  
704 =head1 AUTHOR
705  
706 Johan Vromans, C<< <JV at cpan.org> >>
707  
708 =head1 SUPPORT AND DOCUMENTATION
709  
710 Development of this module takes place on GitHub:
711 https://github.com/sciurius/perl-App-Dusage.
712  
713 You can find documentation for this module with the perldoc command.
714  
715 perldoc App::Dusage
716  
717 Please report any bugs or feature requests using the issue tracker on
718 GitHub.
719  
720 =head1 LICENSE
721  
722 Copyright (C) 1990, 2000, 2021, Johan Vromans
723  
724 This module is free software. You can redistribute it and/or
725 modify it under the terms of the Artistic License 2.0.
726  
727 This program is distributed in the hope that it will be useful,
728 but without any warranty; without even the implied warranty of
729 merchantability or fitness for a particular purpose.
730  
731 =cut
732  
733 2 1;
↑ Summary blib/lib/App/Dusage.pm »