Skip to content

Commit 04c1961

Browse files
author
Alex Damsted
committed
[#225] Fix Totara compatibility bug with the core get_directory_size function
1 parent c358a3e commit 04c1961

1 file changed

Lines changed: 51 additions & 3 deletions

File tree

classes/check/dirsizes.php

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,13 @@ class dirsizes extends check {
4444
public function get_result(): result {
4545
global $CFG;
4646

47-
$sizedataroot = get_directory_size($CFG->dataroot);
47+
// If Totara, use custom Totara function for compatibility.
48+
if (!empty($CFG->totara_version)) {
49+
$sizedataroot = $this->dirsize_totara('dataroot');
50+
} else {
51+
$sizedataroot = get_directory_size($CFG->dataroot);
52+
}
53+
4854
$summary = $sizedataroot;
4955
$details = "Shared paths:<br>";
5056
$details .= '$CFG->dataroot = ' . display_size($sizedataroot);
@@ -61,9 +67,9 @@ public function get_result(): result {
6167
return new result(result::INFO, $summary, $details);
6268
}
6369
/**
64-
* Get a paths sizet
70+
* Get a path's size
6571
* @param string $cfg the path to check
66-
* @return string size for a path as html
72+
* @return string $size for a path as html
6773
*/
6874
private function dirsize(string $cfg) {
6975
global $CFG;
@@ -76,4 +82,46 @@ private function dirsize(string $cfg) {
7682
return "<br>\$CFG->{$cfg} = " . display_size($size);
7783
}
7884

85+
/**
86+
* Get a path's size (compatible with Totara)
87+
* @param string $cfg the path to check
88+
* @return int $size size for a path as an integer
89+
*/
90+
private function dirsize_totara(string $cfg): int {
91+
global $CFG;
92+
if (!property_exists($CFG, $cfg)) {
93+
return 0;
94+
}
95+
$rootdir = $CFG->{$cfg};
96+
97+
if (!is_dir($rootdir)) {
98+
// Must be a directory.
99+
return 0;
100+
}
101+
102+
if (!$dir = @opendir($rootdir)) {
103+
// Can't open it for some reason.
104+
return 0;
105+
}
106+
107+
$size = 0;
108+
109+
while (false !== ($file = readdir($dir))) {
110+
$firstchar = substr($file, 0, 1);
111+
if ($firstchar == '.' or $file == 'CVS') {
112+
continue;
113+
}
114+
$fullfile = $rootdir .'/'. $file;
115+
116+
$filesize = filesize($fullfile);
117+
if ($filesize === false) {
118+
continue;
119+
}
120+
121+
$size += $filesize;
122+
}
123+
closedir($dir);
124+
125+
return $size;
126+
}
79127
}

0 commit comments

Comments
 (0)