line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Filesys::Df;
|
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
9542
|
use strict;
|
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
293
|
|
4
|
1
|
|
|
1
|
|
5
|
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
119
|
|
5
|
1
|
|
|
1
|
|
6
|
use Carp;
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
1615
|
|
6
|
|
|
|
|
|
|
require Exporter;
|
7
|
|
|
|
|
|
|
require DynaLoader;
|
8
|
|
|
|
|
|
|
require 5.006;
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
@ISA = qw(Exporter DynaLoader);
|
11
|
|
|
|
|
|
|
@EXPORT = qw(df);
|
12
|
|
|
|
|
|
|
$VERSION = '0.92';
|
13
|
|
|
|
|
|
|
bootstrap Filesys::Df $VERSION;
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub df {
|
16
|
2
|
|
|
2
|
0
|
236
|
my ($dir, $block_size) = @_;
|
17
|
2
|
|
|
|
|
3
|
my ($used, $fused);
|
18
|
0
|
|
|
|
|
0
|
my ($per, $fper);
|
19
|
0
|
|
|
|
|
0
|
my ($user_blocks, $user_used);
|
20
|
0
|
|
|
|
|
0
|
my ($user_files, $user_fused);
|
21
|
2
|
|
|
|
|
4
|
my %fs = ();
|
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
|
24
|
2
|
50
|
|
|
|
9
|
(defined($dir)) ||
|
25
|
|
|
|
|
|
|
(croak "Usage: df\(\$dir\) or df\(\$dir\, \$block_size)");
|
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
#### If no requested block size then we will return the values in bytes
|
28
|
2
|
50
|
|
|
|
7
|
($block_size) ||
|
29
|
|
|
|
|
|
|
($block_size = 1024);
|
30
|
|
|
|
|
|
|
|
31
|
2
|
|
|
|
|
4
|
my ($frsize, $blocks, $bfree, $bavail, $files, $ffree, $favail);
|
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
#### If open filehandle call fstatvfs or fstatfs
|
34
|
2
|
100
|
|
|
|
8
|
if(defined(fileno($dir))) {
|
35
|
1
|
|
|
|
|
16
|
($frsize, $blocks, $bfree, $bavail, $files, $ffree, $favail) = _df_fh(fileno($dir));
|
36
|
|
|
|
|
|
|
}
|
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
else {
|
39
|
1
|
|
|
|
|
65
|
($frsize, $blocks, $bfree, $bavail, $files, $ffree, $favail) = _df($dir);
|
40
|
|
|
|
|
|
|
}
|
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
#### Some system or XS failure, something like /proc, or bad $dir
|
44
|
2
|
50
|
33
|
|
|
18
|
if($frsize == 0 || $blocks == 0) {
|
45
|
0
|
|
|
|
|
0
|
return();
|
46
|
|
|
|
|
|
|
}
|
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
#### Change to requested or default block size
|
49
|
2
|
50
|
|
|
|
8
|
if($block_size > $frsize) {
|
|
|
50
|
|
|
|
|
|
50
|
0
|
|
|
|
|
0
|
my $result = $block_size / $frsize;
|
51
|
0
|
|
|
|
|
0
|
$blocks /= $result;
|
52
|
0
|
0
|
|
|
|
0
|
($bfree != 0) &&
|
53
|
|
|
|
|
|
|
($bfree /= $result);
|
54
|
|
|
|
|
|
|
#### Keep bavail -
|
55
|
0
|
0
|
|
|
|
0
|
($bavail < 0) &&
|
56
|
|
|
|
|
|
|
($result *= -1);
|
57
|
|
|
|
|
|
|
|
58
|
0
|
0
|
|
|
|
0
|
($bavail != 0) &&
|
59
|
|
|
|
|
|
|
($bavail /= $result);
|
60
|
|
|
|
|
|
|
}
|
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
elsif($block_size < $frsize) {
|
63
|
2
|
|
|
|
|
4
|
my $result = $frsize / $block_size;
|
64
|
2
|
|
|
|
|
3
|
$blocks *= $result;
|
65
|
2
|
|
|
|
|
3
|
$bfree *= $result;
|
66
|
|
|
|
|
|
|
#### Keep bavail -
|
67
|
2
|
50
|
|
|
|
5
|
($bavail < 0) &&
|
68
|
|
|
|
|
|
|
($result *= -1);
|
69
|
2
|
|
|
|
|
3
|
$bavail *= $result;
|
70
|
|
|
|
|
|
|
}
|
71
|
|
|
|
|
|
|
|
72
|
2
|
|
|
|
|
3
|
$used = $blocks - $bfree;
|
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
#### There is a reserved amount for the su
|
75
|
|
|
|
|
|
|
#### or there are disk quotas
|
76
|
2
|
50
|
|
|
|
10
|
if($bfree > $bavail) {
|
77
|
2
|
|
|
|
|
3
|
$user_blocks = $blocks - ($bfree - $bavail);
|
78
|
2
|
|
|
|
|
3
|
$user_used = $user_blocks - $bavail;
|
79
|
2
|
50
|
|
|
|
6
|
if($bavail < 0) {
|
80
|
|
|
|
|
|
|
#### over 100%
|
81
|
0
|
|
|
|
|
0
|
my $tmp_bavail = $bavail;
|
82
|
0
|
|
|
|
|
0
|
$per = ($tmp_bavail *= -1) / $user_blocks;
|
83
|
|
|
|
|
|
|
}
|
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
else {
|
86
|
2
|
50
|
|
|
|
4
|
if($user_used == 0) {
|
87
|
0
|
|
|
|
|
0
|
$per = 0;
|
88
|
|
|
|
|
|
|
}
|
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
else {
|
91
|
2
|
|
|
|
|
4
|
$per = $user_used / $user_blocks;
|
92
|
|
|
|
|
|
|
}
|
93
|
|
|
|
|
|
|
}
|
94
|
|
|
|
|
|
|
}
|
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
#### No reserved amount or quotas
|
97
|
|
|
|
|
|
|
else {
|
98
|
0
|
0
|
|
|
|
0
|
if($used == 0) {
|
99
|
0
|
|
|
|
|
0
|
$per = 0;
|
100
|
|
|
|
|
|
|
}
|
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
else {
|
103
|
0
|
|
|
|
|
0
|
$per = $used / $blocks;
|
104
|
0
|
|
|
|
|
0
|
$user_blocks = $blocks;
|
105
|
0
|
|
|
|
|
0
|
$user_used = $used;
|
106
|
|
|
|
|
|
|
}
|
107
|
|
|
|
|
|
|
}
|
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
#### round
|
110
|
2
|
|
|
|
|
5
|
$per *= 100;
|
111
|
2
|
|
|
|
|
4
|
$per += .5;
|
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
#### over 100%
|
114
|
2
|
50
|
|
|
|
5
|
($bavail < 0) &&
|
115
|
|
|
|
|
|
|
($per += 100);
|
116
|
|
|
|
|
|
|
|
117
|
2
|
|
|
|
|
6
|
$fs{per} = int($per);
|
118
|
2
|
|
|
|
|
4
|
$fs{blocks} = $blocks;
|
119
|
2
|
|
|
|
|
3
|
$fs{bfree} = $bfree;
|
120
|
2
|
|
|
|
|
3
|
$fs{bavail} = $bavail;
|
121
|
2
|
|
|
|
|
4
|
$fs{used} = $used;
|
122
|
|
|
|
|
|
|
#### These are undocumented but kept for backwards compatibility
|
123
|
2
|
|
|
|
|
84
|
$fs{user_blocks} = $user_blocks;
|
124
|
2
|
|
|
|
|
4
|
$fs{user_bavail} = $bavail;
|
125
|
2
|
|
|
|
|
5
|
$fs{user_used} = $user_used;
|
126
|
2
|
|
|
|
|
4
|
$fs{su_bavail} = $bfree;
|
127
|
2
|
|
|
|
|
3
|
$fs{su_blocks} = $blocks;
|
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
#### Handle inodes if system supports them
|
132
|
2
|
50
|
33
|
|
|
106
|
if(defined $files && $files > 0) {
|
133
|
2
|
|
|
|
|
4
|
$fused = $files - $ffree;
|
134
|
|
|
|
|
|
|
#### There is a reserved amount
|
135
|
2
|
50
|
|
|
|
6
|
if($ffree > $favail) {
|
136
|
0
|
|
|
|
|
0
|
$user_files = $files - ($ffree - $favail);
|
137
|
0
|
|
|
|
|
0
|
$user_fused = $user_files - $favail;
|
138
|
0
|
0
|
|
|
|
0
|
if($favail < 0) {
|
139
|
|
|
|
|
|
|
#### over 100%
|
140
|
0
|
|
|
|
|
0
|
my $tmp_favail = $favail;
|
141
|
0
|
|
|
|
|
0
|
$fper = ($tmp_favail *= -1) / $user_files;
|
142
|
|
|
|
|
|
|
}
|
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
else {
|
145
|
0
|
0
|
|
|
|
0
|
if($user_fused == 0) {
|
146
|
0
|
|
|
|
|
0
|
$fper = 0;
|
147
|
|
|
|
|
|
|
}
|
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
else {
|
150
|
0
|
|
|
|
|
0
|
$fper = $user_fused / $user_files;
|
151
|
|
|
|
|
|
|
}
|
152
|
|
|
|
|
|
|
}
|
153
|
|
|
|
|
|
|
}
|
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
#### su and user amount are the same
|
156
|
|
|
|
|
|
|
else {
|
157
|
2
|
50
|
|
|
|
5
|
if($fused == 0) {
|
158
|
0
|
|
|
|
|
0
|
$fper = 0;
|
159
|
|
|
|
|
|
|
}
|
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
else {
|
162
|
2
|
|
|
|
|
4
|
$fper = $fused / $files;
|
163
|
|
|
|
|
|
|
}
|
164
|
|
|
|
|
|
|
|
165
|
2
|
|
|
|
|
3
|
$user_files = $files;
|
166
|
2
|
|
|
|
|
26
|
$user_fused = $fused;
|
167
|
|
|
|
|
|
|
}
|
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
#### round
|
170
|
2
|
|
|
|
|
4
|
$fper *= 100;
|
171
|
2
|
|
|
|
|
3
|
$fper += .5;
|
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
#### over 100%
|
174
|
2
|
50
|
|
|
|
5
|
($favail < 0) &&
|
175
|
|
|
|
|
|
|
($fper += 100);
|
176
|
|
|
|
|
|
|
|
177
|
2
|
|
|
|
|
3
|
$fs{fper} = int($fper);
|
178
|
2
|
|
|
|
|
5
|
$fs{files} = $files;
|
179
|
2
|
|
|
|
|
3
|
$fs{ffree} = $ffree;
|
180
|
2
|
|
|
|
|
3
|
$fs{favail} = $favail;
|
181
|
2
|
|
|
|
|
2
|
$fs{fused} = $fused;
|
182
|
|
|
|
|
|
|
#### These are undocumented but kept for backwards compatibility
|
183
|
2
|
|
|
|
|
16
|
$fs{user_fused} = $user_fused;
|
184
|
2
|
|
|
|
|
4
|
$fs{user_files} = $user_files;
|
185
|
2
|
|
|
|
|
4
|
$fs{su_favail} = $ffree;
|
186
|
2
|
|
|
|
|
3
|
$fs{su_files} = $files;
|
187
|
2
|
|
|
|
|
3
|
$fs{user_favail} = $favail;
|
188
|
|
|
|
|
|
|
}
|
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
#### No valid inode info. Probably NFS.
|
191
|
|
|
|
|
|
|
#### Instead of undefing, just have the user call exists().
|
192
|
|
|
|
|
|
|
#else {
|
193
|
|
|
|
|
|
|
# $fs{fper} = undef;
|
194
|
|
|
|
|
|
|
# $fs{files} = undef;
|
195
|
|
|
|
|
|
|
# $fs{ffree} = undef;
|
196
|
|
|
|
|
|
|
# $fs{favail} = undef;
|
197
|
|
|
|
|
|
|
# $fs{fused} = undef;
|
198
|
|
|
|
|
|
|
# $fs{user_fused} = undef;
|
199
|
|
|
|
|
|
|
# $fs{user_files} = undef;
|
200
|
|
|
|
|
|
|
#}
|
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
|
203
|
2
|
|
|
|
|
9
|
return(\%fs);
|
204
|
|
|
|
|
|
|
}
|
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
1;
|
207
|
|
|
|
|
|
|
__END__
|