forked from laminas/laminas-diagnostics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiskFree.php
More file actions
306 lines (275 loc) · 9.86 KB
/
DiskFree.php
File metadata and controls
306 lines (275 loc) · 9.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<?php
namespace Laminas\Diagnostics\Check;
use InvalidArgumentException;
use Laminas\Diagnostics\Result\Failure;
use Laminas\Diagnostics\Result\Success;
use Laminas\Diagnostics\Result\Warning;
use function array_merge;
use function array_search;
use function array_unique;
use function disk_free_space;
use function implode;
use function in_array;
use function is_float;
use function is_numeric;
use function is_scalar;
use function is_string;
use function preg_match;
use function round;
use function sprintf;
use function strtolower;
use function substr;
/**
* Check if there is enough remaining free disk space.
*
* String to byte size conversion borrowed from Jerity project:
* https://github.com/jerity/jerity/blob/master/src/Util/Number.php
* authors: Dave Ingram <dave@dmi.me.uk>, Nick Pope <nick@nickpope.me.uk>
* license: http://creativecommons.org/licenses/BSD/ CC-BSD
* copyright: Copyright (c) 2010, Dave Ingram, Nick Pope
*/
class DiskFree extends AbstractCheck implements CheckInterface
{
/**
* Minimum number of bytes
*
* @var int
*/
protected $minDiskBytes;
/**
* SI prefix symbols for units of information.
*
* @var array
*/
public static $siPrefixSymbol = ['', 'k', 'M', 'G', 'T', 'P', 'E', 'Z'];
/**
* SI prefix names for units of information.
*
* @var array
*/
public static $siPrefixName = ['', 'kilo', 'mega', 'giga', 'tera', 'peta', 'exa', 'zetta'];
/**
* SI multiplier for units of information SI prefixes.
*
* @var array
*/
public static $siMultiplier = [
0 => 1e0, // 10^0 == 1000^0 (2^00 == 1024^0)
1 => 1e3, // 10^3 == 1000^1 (2^10 == 1024^1)
2 => 1e6, // 10^6 == 1000^2 (2^20 == 1024^2)
3 => 1e9, // 10^9 == 1000^3 (2^30 == 1024^3)
4 => 1e12, // 10^12 == 1000^4 (2^40 == 1024^4)
5 => 1e15, // 10^15 == 1000^5 (2^50 == 1024^5)
6 => 1e18, // 10^18 == 1000^6 (2^60 == 1024^6)
7 => 1e21, // 10^21 == 1000^7 (2^70 == 1024^7)
];
/**
* IEC binary prefix symbols for units of information.
*
* @var array
*/
public static $iecPrefixSymbol = ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei'];
/**
* IEC binary prefix symbols for units of information.
*
* @var array
*/
public static $iecPrefixName = ['', 'kibi', 'mebi', 'gibi', 'tebi', 'pebi', 'exbi'];
/**
* IEC multiplier for units of information IEC binary prefixes.
*
* @var array
*/
public static $iecMultiplier = [
0 => 1, // 2^00 == 1024^0
1 => 1024, // 2^10 == 1024^1
2 => 1048576, // 2^20 == 1024^2
3 => 1073741824, // 2^30 == 1024^3
4 => 1099511627776, // 2^40 == 1024^4
5 => 1125899906842624, // 2^50 == 1024^5
6 => 1152921504606846976, // 2^60 == 1024^6
];
/**
* JEDEC memory standards prefixes for units of information.
*
* @var array
*/
public static $jedecPrefixSymbol = ['', 'K', 'M', 'G'];
/**
* JEDEC memory standards prefixes for units of information.
*
* @var array
*/
public static $jedecPrefixName = ['', 'kilo', 'mega', 'giga'];
/**
* JEDEC multiplier for units of information JEDEC memory standards prefixes.
*
* @var array
*/
public static $jedecMultiplier = [
0 => 1, // 2^00 == 1024^0
1 => 1024, // 2^10 == 1024^1
2 => 1048576, // 2^20 == 1024^2
3 => 1073741824, // 2^30 == 1024^3
];
/**
* The disk path to check.
*
* @internal
*/
public string $path;
/**
* @param int|string $size Minimum disk size in bytes or a valid byte string (IEC, SI or Jedec).
* @param string $path The disk path to check, i.e. '/tmp' or 'C:' (defaults to /)
* @throws InvalidArgumentException
*/
public function __construct($size, $path = '/')
{
if (! is_scalar($size)) {
throw new InvalidArgumentException('Invalid free disk space argument - expecting a positive number');
}
if (! is_string($path)) {
throw new InvalidArgumentException('Invalid disk path argument - expecting a string');
}
if (is_numeric($size)) {
$this->minDiskBytes = (int) $size;
} else {
$this->minDiskBytes = static::stringToBytes($size);
}
if ($size <= 0) {
throw new InvalidArgumentException('Invalid free disk space argument - expecting a positive number');
}
$this->path = $path;
}
/**
* Perform the check
*
* @see \Laminas\Diagnostics\Check\CheckInterface::check()
*
* @return Failure|Success|Warning
*/
public function check()
{
// We are using error suppression because the method will trigger a warning
// in case of non-existent paths and other errors. We are more interested in
// the potential return value of FALSE, which will tell us that free space
// could not be obtained and we do not care about the real cause of this.
$free = @ disk_free_space($this->path);
if ($free === false || ! is_float($free) || $free < 0) {
return new Warning('Unable to determine free disk space at ' . $this->path);
}
$freeHumanReadable = static::bytesToString($free, 2);
$description = sprintf('Remaining space at %s: %s', $this->path, $freeHumanReadable);
if (disk_free_space($this->path) < $this->minDiskBytes) {
return new Failure($description, $free);
}
return new Success($description, $free);
}
/**
* Converts int bytes to a highest, rounded, multiplication that is IEC compliant.
*
* @link https://en.wikipedia.org/wiki/Binary_prefix
*
* @param int $size Number of bytes to convert
* @param int $precision Rounding precision (defaults to 0)
* @return string Highest rounded multiplication with IEC suffix
*/
public static function bytesToString($size, $precision = 0)
{
if ($size >= 1125899906842624) {
$size /= 1125899906842624;
$suffix = 'PiB';
} elseif ($size >= 1099511627776) {
$size /= 1099511627776;
$suffix = 'TiB';
} elseif ($size >= 1073741824) {
$size /= 1073741824;
$suffix = 'GiB';
} elseif ($size >= 1048576) {
$size /= 1048576;
$suffix = 'MiB';
} elseif ($size >= 1024) {
$size /= 1024;
$suffix = 'KiB';
} else {
$suffix = 'B';
}
return round($size, $precision) . ' ' . $suffix;
}
/**
* Parses a string specifying a size of information and converts it to bytes.
* Expects a string with a value followed by a symbol or named unit with an
* optional space in between.
*
* @param string $string The string to parse.
* @param bool $jedec Whether to prefer JEDEC over SI units.
* @throws InvalidArgumentException
* @return int The number of bytes.
*/
public static function stringToBytes($string, $jedec = false)
{
// Prepare regular expression.
$symbol = '(?:([kKMGTPEZ])(i)?)?([Bb])?(?:ps)?';
$name = '(' . implode('|', array_unique(array_merge(
self::$siPrefixName,
self::$iecPrefixName,
self::$jedecPrefixName
))) . ')?(bytes?|bits?)?';
// Attempt to match the string.
if (! preg_match('/^(\d+(?:\.\d+)?) *(?:' . $symbol . '|' . $name . ')$/', $string, $match)) {
throw new InvalidArgumentException('Invalid byte size provided - unable to parse.');
}
// The value in the provided units.
$bytes = $match[1];
if (isset($match[5]) && $match[5] || isset($match[2]) && $match[2]) {
// Check for prefix (by name).
if (isset($match[5]) && $match[5]) {
$k = strtolower($match[5]);
if (in_array($k, self::$iecPrefixName)) {
$a = &self::$iecPrefixName;
$x = &self::$iecMultiplier;
} elseif (in_array($k, self::$jedecPrefixName) && $jedec) {
$a = &self::$jedecPrefixName;
$x = &self::$jedecMultiplier;
} elseif (in_array($k, self::$siPrefixName)) {
$a = &self::$siPrefixName;
$x = &self::$siMultiplier;
}
}
// Check for prefix (by symbol).
if (isset($match[2]) && $match[2]) {
$k = $match[2];
if (isset($match[3]) && $match[3] === 'i') {
$a = &self::$iecPrefixSymbol;
$x = &self::$iecMultiplier;
$k .= $match[3];
} elseif ($jedec) {
$a = &self::$jedecPrefixSymbol;
$x = &self::$jedecMultiplier;
} else {
$a = &self::$siPrefixSymbol;
$x = &self::$siMultiplier;
}
}
// Find the correct multiplier and apply it.
$i = array_search($k, $a, true);
if ($i === false || ! isset($x[$i])) {
throw new InvalidArgumentException(sprintf(
'Invalid multiplier: %s not one of %s.',
$k,
implode(', ', $a)
));
}
$bytes *= $x[$i];
}
// Check whether we were provided with bits or bytes - divide if needed.
if (
isset($match[4]) && $match[4] === 'b' ||
isset($match[6]) && substr(strtolower($match[6]), 0, 3) === 'bit'
) {
$bytes /= 8;
}
// Return the number of bytes.
return $bytes;
}
}