blib/lib/Filesys/DiskFree.pm

filter: Statementstmt 12.4% 16 / 129 Branchbran 0.0% 0 / 78 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 27.7% 5 / 18 Podpod 76.9% 10 / 13 totaltotal 12.0% 31 / 258 CC 44 SCAR 52.4
CC
44
Cov
7%
readline
462.0
set
110.0
extract
72.0
CRAP 189.1
↑ Summary
1 #
2 #
3 #
4 # Copyright (c) 1998 Alan R. Barclay. All rights reserved. This program
5 # is free software; you can redistribute it and/or modify it under
6 # the same terms as Perl itself.
7  
8 package Filesys::DiskFree;
9  
10 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 #qw();
14  
15 1 ▶use vars qw($VERSION $Format %Df);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 $VERSION = 0.06;
18  
19 #
20 # The format table
21 #
22 # Note, the format names are not gauranteed. If I find that there
23 # is a reason to rename one, then they be renamed.
24 #
25  
26 1 %Df = (
27 'linux' => {
28 'blocks' => "df -P",
29 'inodes' => "df -Pi",
30 'format' => "linuxish",
31 },
32 'solaris' => {
33 'blocks' => "df -k",
34 'inodes' => "df -k -o i -F ufs",
35 'format' => "svish",
36 },
37 'bsdos' => {
38 'blocks' => "df -i",
39 'inodes' => "df -i",
40 'format' => 'bsdish',
41 },
42  
43 'irix' => {
44 'blocks' => "df",
45 'inodes' => "df -i",
46 'format' => "irixish",
47 },
48 'hpux' => {
49 'blocks' => "bdf -l -i",
50 'inodes' => "bdf -l -i",
51 'format' => 'hpuxish',
52 },
53 'dec_osf' => {
54 'blocks' => "df",
55 'inodes' => "df -i",
56 'format' => 'svish',
57 },
58 );
59  
60 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
61  
62 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
63 1 $Format = $^O;
64 }
65  
66 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
67 0 my $proto = shift;
68 0 ▶ my $class = ref($proto) || $proto;
Condition0%0/3
ref $proto || $proto
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
69 0 my $self = {
70 FORMAT => $Format,
71 DEVICES => undef,
72 MOUNTS => undef,
73 MODE => 'blocks'
74 };
75  
76 0 bless ($self, $class);
77 0 return $self;
78 }
79  
80 0 ▶sub set(){
Subroutine set: not called CC=10 SCAR=47.0
Pod documented
81 0 my $self=shift;
82 0 my @return;
83  
84 0 ▶ return undef if(defined $self->{'DEVICES'});
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"DEVICES"} 0 0 0
85  
86 0 ▶ if(@_){
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
87 0 ▶ if($_[0] =~ m/format/i){
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /format/i) 0 0 0
88 0 push(@return,$self->{'FORMAT'});
89 0 ▶ $self->{'FORMAT'}=$_[1] if(defined $_[1]);
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[1] 0 0 0
90 }
91  
92 0 ▶ if($_[0] =~ m/mode/i){
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /mode/i) 0 0 0
93 0 push(@return,$self->{'MODE'});
94 0 ▶ $self->{'MODE'}='blocks' if($_[1] =~ m/block/i and defined $_[1]);
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] =~ /block/i and defined $_[1] 0 0 0
Condition0%0/3
$_[1] =~ /block/i and defined $_[1]
!ll&&!rl&&r
000
MC/DC0%0/2
$_[1] =~ /block/i and defined $_[1]
$_[1] =~ /block/i defined $_[1]
Truth table0%0/3
A and B
A= $_[1] =~ /block/iB= defined $_[1]
ABresult
0X0
100
111
95 0 ▶ $self->{'MODE'}='inodes' if($_[1] =~ m/inode/i and defined $_[1]);
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] =~ /inode/i and defined $_[1] 0 0 0
Condition0%0/3
$_[1] =~ /inode/i and defined $_[1]
!ll&&!rl&&r
000
MC/DC0%0/2
$_[1] =~ /inode/i and defined $_[1]
$_[1] =~ /inode/i defined $_[1]
Truth table0%0/3
A and B
A= $_[1] =~ /inode/iB= defined $_[1]
ABresult
0X0
100
111
96 }
97 }
98 0 return @return;
99 }
100  
101 0 ▶sub command () {
Subroutine command: not called CC=1 SCAR=6.9
Pod documented
102 0 my $self=shift;
103 0 return $Df{"\L".$self->{'FORMAT'}."\E"}{$self->{'MODE'}};
104 }
105 0 ▶sub df(){
Subroutine df: not called CC=4 SCAR=30.0
Pod documented
106 0 my $self=shift;
107 0 my $cmd="df";
108
109 $cmd=$self->command() or
110 0 ▶ croak "No df command known for format ".$self->{'FORMAT'};
Branch: 1 branch
BranchTrueFalseTotal
$cmd = $self->command or croak('No df command known for format ' . $self->{"FORMAT"}) 0 0 0
111 0 ▶ open(HANDLE,"$cmd|") or croak("Cannot fork $!");
Branch: 1 branch
BranchTrueFalseTotal
open HANDLE, "$cmd|" or croak("Cannot fork $!") 0 0 0
112 0 return $self->load(\*HANDLE);
113 0 ▶ close(HANDLE) or croak("Cannot df $!");
Branch: 1 branch
BranchTrueFalseTotal
close HANDLE or croak("Cannot df $!") 0 0 0
114 }
115  
116 0 ▶sub load() {
Subroutine load: not called CC=3 SCAR=24.8
Pod documented
117 0 my $self=shift;
118 0 my $handle=shift;
119  
120 0 ▶ if(ref $handle eq "GLOB"){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $handle eq 'GLOB') { } 0 0 0
121 0 ▶ while(<$handle>){
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = CORE::readline $handle)) 0 0 0
122 0 $self->readline($_);
123 }
124 } else {
125 0 map { $self->readline($_) } split(/$\//,$handle);
126 }
127 0 return 'true';
128 }
129  
130 0 ▶sub readline() {
Subroutine readline: not called CC=21 SCAR=61.4
Pod undocumented
131 0 my $self=shift;
132 0 my $line=shift;
133 0 my ($device,$btotal,$bused,$bavail,$iused,$iavail,$mount,
134 $total,$used,$avail);
135  
136 0 chomp($line);
137  
138 0 $_=$Df{"\L".$self->{'FORMAT'}."\E"}{'format'};
139  
140 0 ▶ if(/linuxish/i){
Branch: 5 branches
BranchTrueFalseTotal
if (/linuxish/i) { } 0 0 0
elsif (/svish/i) { } 0 0 0
elsif (/bsdish/) { } 0 0 0
elsif (/irixish/) { } 0 0 0
elsif (/hpuxish/) { } 0 0 0
141 0 ▶ return undef if($line =~ /^Filesystem.*Mounted on/i);
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^Filesystem.*Mounted on/i 0 0 0
142 0 ($device,$total,$used,$avail,undef,$mount)=split(' ',$line);
143 0 ▶ if($self->{'MODE'} eq 'blocks'){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"MODE"} eq "blocks") 0 0 0
144 0 $total *= 1024;
145 0 $used *= 1024;
146 0 $avail *= 1024;
147 }
148 } elsif(/svish/i){
149 0 ▶ return undef if($line =~ /^Filesystem.*Mounted on/i);
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^Filesystem.*Mounted on/i 0 0 0
150 0 ▶ if($self->{'MODE'} eq 'blocks'){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'MODE'} eq 'blocks') { } 0 0 0
151 0 ($device,$total,$used,$avail,undef,$mount)=split(' ',$line);
152 0 $total *= 1024;
153 0 $used *= 1024;
154 0 $avail *= 1024;
155 } else {
156 0 ($device,$used,$avail,undef,$mount)=split(' ',$line);
157 0 $total=$used+$avail;
158 }
159 } elsif(/bsdish/){
160 0 ▶ return undef if($line =~ /^Filesystem.*Mounted on/i);
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^Filesystem.*Mounted on/i 0 0 0
161 0 ($device,$btotal,$bused,$bavail,undef,$iused,$iavail,undef,$mount)=
162 split(' ',$line);
163 0 ▶ if($self->{'MODE'} eq 'blocks'){
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'MODE'} eq 'blocks') { } 0 0 0
elsif ($self->{'MODE'} eq 'inodes') { } 0 0 0
164 0 $total=$btotal*512;
165 0 $used=$bused*512;
166 0 $avail=$bavail*512;
167 } elsif($self->{'MODE'} eq 'inodes'){
168 0 $total=undef;
169 0 $used=$iused*512;
170 0 $avail=$iavail*512;
171 }
172 } elsif(/irixish/){
173 0 ▶ return undef if($line =~ /^Filesystem.*Mounted on/i);
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^Filesystem.*Mounted on/i 0 0 0
174 0 ▶ if($self->{'MODE'} eq 'blocks'){
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'MODE'} eq 'blocks') { } 0 0 0
elsif ($self->{'MODE'} eq 'inodes') { } 0 0 0
175 0 ($device,undef,$btotal,$bused,$bavail,undef,$mount)=split(' ',$line);
176 0 $total=$btotal*512;
177 0 $used=$bused*512;
178 0 $avail=$bavail*512;
179 } elsif($self->{'MODE'} eq 'inodes'){
180 0 ($device,undef,$btotal,$bused,$bavail,undef,$iused,$iavail,undef,$mount)=
181 split(' ',$line);
182 0 ▶ return undef if $iused =~ /[A-Za-z]+/ or $iused == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $iused =~ /[A-Za-z]+/ or $iused == 0 0 0 0
Condition0%0/3
$iused =~ /[A-Za-z]+/ or $iused == 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$iused =~ /[A-Za-z]+/ or $iused == 0
$iused =~ /[A-Za-z]+/ $iused == 0
Truth table0%0/3
A or B
A= $iused =~ /[A-Za-z]+/B= $iused == 0
ABresult
1X1
011
000
183 0 $total = ($iused + $iavail) * 512;
184 0 $used=$iused*512;
185 0 $avail=$iavail*512;
186 }
187 } elsif(/hpuxish/){
188 0 ▶ return undef if($line =~ /^Filesystem.*Mounted on/i);
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^Filesystem.*Mounted on/i 0 0 0
189 0 ($device,$btotal,$bused,$bavail,undef,$iused,$iavail,undef,$mount)=
190 split(' ',$line);
191 0 ▶ if($self->{'MODE'} eq 'blocks'){
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'MODE'} eq 'blocks') { } 0 0 0
elsif ($self->{'MODE'} eq 'inodes') { } 0 0 0
192 0 $total=$btotal*1024;
193 0 $used=$bused*1024;
194 0 $avail=$bavail*1024;
195 } elsif($self->{'MODE'} eq 'inodes'){
196 0 $total=($iused + $iavail);
197 0 $used=$iused;
198 0 $avail=$iavail;
199 }
200 } else {
201 croak "Unknown encoding ".$Df{"\L".$self->{'FORMAT'}."\E"}{'format'}.
202 0 " for format ".$self->{'FORMAT'};
203 }
204 0 $self->{'MOUNTS'}{$mount}=$device;
205 0 $self->{'DEVICES'}{$device}={};
206 0 $self->{'DEVICES'}{$device}{'device'}=$device;
207 0 $self->{'DEVICES'}{$device}{'total'} =$total;
208 0 $self->{'DEVICES'}{$device}{'used'} =$used;
209 0 $self->{'DEVICES'}{$device}{'avail'} =$avail;
210 0 $self->{'DEVICES'}{$device}{'mount'} =$mount;
211 }
212  
213 0 ▶sub device() { return extract(@_,'device'); }
Subroutine device: not called CC=1 SCAR=6.9
Pod documented
214 0 ▶sub total() { return extract(@_,'total'); }
Subroutine total: not called CC=1 SCAR=6.9
Pod documented
215 0 ▶sub used() { return extract(@_,'used'); }
Subroutine used: not called CC=1 SCAR=6.9
Pod documented
216 0 ▶sub avail() { return extract(@_,'avail'); }
Subroutine avail: not called CC=1 SCAR=6.9
Pod documented
217 0 ▶sub mount() { return extract(@_,'mount'); }
Subroutine mount: not called CC=1 SCAR=6.9
Pod documented
218  
219 0 ▶sub extract () {
Subroutine extract: not called CC=8 SCAR=42.8
Pod undocumented
220 0 my $self=shift;
221 0 my $device;
222 0 ▶ if(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
223 0 my $thingy=shift;
224 0 ▶ if(defined($self->{'DEVICES'}{$thingy})){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'DEVICES'}{$thingy}) { } 0 0 0
225 0 $device=$thingy;
226 } else {
227 0 ▶ return undef unless(defined($self->{'MOUNTS'}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"MOUNTS"} 0 0 0
228 0 ▶ while(not defined($self->{'MOUNTS'}{$thingy})){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"MOUNTS"}{$thingy}) 0 0 0
229 0 ▶ return undef if($thingy eq '/');
Branch: 1 branch
BranchTrueFalseTotal
if $thingy eq "/" 0 0 0
230 0 ▶ $thingy =~ s!/[^/]*?$!! unless($thingy =~ s!/$!!);
Branch: 1 branch
BranchTrueFalseTotal
unless $thingy =~ s[/$][] 0 0 0
231 0 ▶ $thingy = "/" unless($thingy =~ "/");
Branch: 1 branch
BranchTrueFalseTotal
unless $thingy =~ m[/] 0 0 0
232 }
233 0 $device=$self->{'MOUNTS'}{$thingy}
234 }
235 0 return $self->{'DEVICES'}{$device}{$_[0]};
236 }
237 0 return undef;
238 }
239  
240 0 ▶sub disks () {
Subroutine disks: not called CC=2 SCAR=17.9
Pod documented
241 0 my $self=shift;
242 0 ▶ return undef unless(defined($self->{'MOUNTS'}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"MOUNTS"} 0 0 0
243 0 return keys %{$self->{'MOUNTS'}};
244 }
245  
246 1 1;
247 __END__
↑ Summary