Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/serverinfo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDaniel Kesselberg <mail@danielkesselberg.de>2020-04-17 21:12:59 +0300
committerDaniel Kesselberg <mail@danielkesselberg.de>2020-04-17 23:44:08 +0300
commita7fe6459b5ca51f288872e52d37510c730e45788 (patch)
tree3676d048dff3f897ec4a6353b362cf4713a09e36 /lib
parent310f821ccf433d74dd67c301efe72c1f548da194 (diff)
Refactor getDiskInfo
Add tests for parsing the output Add wrapper to read and mock the command output Signed-off-by: Daniel Kesselberg <mail@danielkesselberg.de>
Diffstat (limited to 'lib')
-rw-r--r--lib/OperatingSystems/DefaultOs.php62
-rw-r--r--lib/Os.php13
2 files changed, 54 insertions, 21 deletions
diff --git a/lib/OperatingSystems/DefaultOs.php b/lib/OperatingSystems/DefaultOs.php
index c232031..b11ebe0 100644
--- a/lib/OperatingSystems/DefaultOs.php
+++ b/lib/OperatingSystems/DefaultOs.php
@@ -210,27 +210,47 @@ class DefaultOs {
}
/**
+ * Get diskInfo will return a list of disks. Used and Available in bytes.
+ *
+ * [
+ * [device => /dev/mapper/homestead--vg-root, fs => ext4, used => 6205468, available => 47321220, percent => 12%, mount => /]
+ * ]
+ *
* @return array
*/
- public function getDiskInfo() {
- $blacklist = ['', 'Type', 'tmpfs', 'devtmpfs'];
- $data = shell_exec('df -TP');
- $lines = preg_split('/[\r\n]+/', $data);
-
- foreach ($lines as $line) {
- $entry = preg_split('/\s+/', trim($line));
- if (isset($entry[1]) && !in_array($entry[1], $blacklist)) {
- $items = [];
- $items['device'] = $entry[0];
- $items['fs'] = $entry[1];
- $items['used'] = $entry[3] * 1024;
- $items['available'] = $entry[4] * 1024;
- $items['percent'] = $entry[5];
- $items['mount'] = $entry[6];
- $result[] = $items;
+ public function getDiskInfo(): array {
+ $data = [];
+
+ try {
+ $disks = $this->executeCommand('df -TP');
+ } catch (\RuntimeException $e) {
+ return $data;
+ }
+
+ $matches = [];
+ $pattern = '/^(?<Filesystem>[\w\/-]+)\s*(?<Type>\w+)\s*(?<Blocks>\d+)\s*(?<Used>\d+)\s*(?<Available>\d+)\s*(?<Capacity>\d+%)\s*(?<Mounted>[\w\/-]+)$/m';
+
+ $result = preg_match_all($pattern, $disks, $matches);
+ if ($result === 0 || $result === false) {
+ return $data;
+ }
+
+ foreach ($matches['Filesystem'] as $i => $filesystem) {
+ if (in_array($matches['Type'][$i], ['tmpfs', 'devtmpfs'], false)) {
+ continue;
}
+
+ $data[] = [
+ 'device' => $filesystem,
+ 'fs' => $matches['Type'][$i],
+ 'used' => (int)$matches['Used'][$i] * 1024,
+ 'available' => (int)$matches['Available'][$i] * 1024,
+ 'percent' => $matches['Capacity'][$i],
+ 'mount' => $matches['Mounted'][$i],
+ ];
}
- return $result;
+
+ return $data;
}
protected function readContent(string $filename): string {
@@ -239,4 +259,12 @@ class DefaultOs {
}
return '';
}
+
+ protected function executeCommand(string $command): string {
+ $output = @shell_exec(escapeshellcmd($command));
+ if ($output === null || $output === '') {
+ throw new \RuntimeException('No output for command: "' . $command . '"');
+ }
+ return $output;
+ }
}
diff --git a/lib/Os.php b/lib/Os.php
index eebbf8c..fd26b6a 100644
--- a/lib/Os.php
+++ b/lib/Os.php
@@ -136,11 +136,16 @@ class Os {
}
/**
- * @return string
+ * Get diskInfo will return a list of disks. Used and Available in bytes.
+ *
+ * [
+ * [device => /dev/mapper/homestead--vg-root, fs => ext4, used => 6205468, available => 47321220, percent => 12%, mount => /]
+ * ]
+ *
+ * @return array
*/
- public function getDiskInfo() {
- $data = $this->backend->getDiskInfo();
- return $data;
+ public function getDiskInfo(): array {
+ return $this->backend->getDiskInfo();
}
/**