1717/**
1818 * Dir sizes performance check.
1919 *
20- * @package tool_heartbeat
21- * @copyright 2023 Brendan Heywood <brendan@catalyst-au.net>
22- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23- *
20+ * @package tool_heartbeat
21+ * @copyright 2023 Brendan Heywood <brendan@catalyst-au.net>
22+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2423 */
2524
2625namespace tool_heartbeat \check ;
3029/**
3130 * Dir sizes performance check.
3231 *
33- * @copyright 2023
34- * @author Brendan Heywood <brendan@catalyst-au.net>
35- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32+ * @copyright 2023
33+ * @author Brendan Heywood <brendan@catalyst-au.net>
34+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3635 */
3736class dirsizes extends check {
3837
@@ -44,11 +43,10 @@ class dirsizes extends check {
4443 public function get_result (): result {
4544 global $ CFG ;
4645
47- $ sizedataroot = get_directory_size ( $ CFG -> dataroot );
46+ $ sizedataroot = $ this -> dirsize ( ' dataroot ' , true );
4847 $ summary = $ sizedataroot ;
4948 $ details = "Shared paths:<br> " ;
5049 $ details .= '$CFG->dataroot = ' . display_size ($ sizedataroot );
51-
5250 $ details .= $ this ->dirsize ('themedir ' );
5351 $ details .= $ this ->dirsize ('tempdir ' );
5452 $ details .= $ this ->dirsize ('cachedir ' );
@@ -61,19 +59,72 @@ public function get_result(): result {
6159 return new result (result::INFO , $ summary , $ details );
6260 }
6361 /**
64- * Get a paths sizet
65- * @param string $cfg the path to check
66- * @return string size for a path as html
62+ * Get a path's size
63+ *
64+ * @param string $cfg the path to check
65+ * @param bool $rawsize return rawsize of directory
66+ * @return string $size for a path as html
6767 */
68- private function dirsize (string $ cfg ) {
68+ private function dirsize (string $ cfg, bool $ rawsize = false ) {
6969 global $ CFG ;
7070 if (!property_exists ($ CFG , $ cfg )) {
7171 return "<br> \$CFG-> $ cfg not in use " ;
7272 }
7373 $ path = $ CFG ->{$ cfg };
74- $ size = get_directory_size ($ path );
74+
75+ // If Totara, use Totara-compatible function.
76+ if (!empty ($ CFG ->totara_version )) {
77+ $ size = $ this ->get_directory_size_totara ($ path );
78+ } else {
79+ $ size = get_directory_size ($ path );
80+ }
81+
82+ if ($ rawsize ) {
83+ return $ size ;
84+ }
7585
7686 return "<br> \$CFG-> {$ cfg } = " . display_size ($ size );
7787 }
7888
89+ /**
90+ * Recursively calculate the size of a directory (Totara-compatible).
91+ *
92+ * This replicates Moodle core's get_directory_size() logic,
93+ * but avoids using it directly for Totara compatibility.
94+ *
95+ * @param string $dir The directory path to measure
96+ * @return int Total size in bytes
97+ */
98+ private function get_directory_size_totara (string $ dir ): int {
99+ if (!is_dir ($ dir )) {
100+ return 0 ;
101+ }
102+
103+ $ size = 0 ;
104+ if (!$ dh = @opendir ($ dir )) {
105+ return 0 ;
106+ }
107+
108+ while (false !== ($ file = readdir ($ dh ))) {
109+ // Skip hidden files and CVS dirs.
110+ if ($ file [0 ] === '. ' || $ file === 'CVS ' ) {
111+ continue ;
112+ }
113+
114+ $ fullfile = $ dir . '/ ' . $ file ;
115+
116+ if (is_dir ($ fullfile )) {
117+ // Recurse into subdirectory.
118+ $ size += $ this ->get_directory_size_totara ($ fullfile );
119+ } else {
120+ $ filesize = filesize ($ fullfile );
121+ if ($ filesize !== false ) {
122+ $ size += $ filesize ;
123+ }
124+ }
125+ }
126+
127+ closedir ($ dh );
128+ return $ size ;
129+ }
79130}
0 commit comments