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
38+
3939 /**
4040 * Get Result.
4141 *
@@ -44,10 +44,10 @@ class dirsizes extends check {
4444 public function get_result (): result {
4545 global $ CFG ;
4646
47- $ sizedataroot = get_directory_size ( $ CFG -> dataroot );
48- $ summary = $ sizedataroot ;
47+ $ sizedataroot = $ this -> dirsize ( ' dataroot ' );
48+ $ summary = $ this -> dirsize_totara ( ' dataroot ' ) ;
4949 $ details = "Shared paths:<br> " ;
50- $ details .= ' $CFG->dataroot = ' . display_size ( $ sizedataroot) ;
50+ $ details .= $ sizedataroot ;
5151
5252 $ details .= $ this ->dirsize ('themedir ' );
5353 $ details .= $ this ->dirsize ('tempdir ' );
@@ -61,19 +61,83 @@ public function get_result(): result {
6161 return new result (result::INFO , $ summary , $ details );
6262 }
6363 /**
64- * Get a paths sizet
65- * @param string $cfg the path to check
66- * @return string size for a path as html
64+ * Get a path's size
65+ *
66+ * @param string $cfg the path to check
67+ * @return string $size for a path as html
6768 */
6869 private function dirsize (string $ cfg ) {
6970 global $ CFG ;
7071 if (!property_exists ($ CFG , $ cfg )) {
7172 return "<br> \$CFG-> $ cfg not in use " ;
7273 }
73- $ 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 ->dirsize_totara ($ cfg );
78+ } else {
79+ $ path = $ CFG ->{$ cfg };
80+ $ size = get_directory_size ($ path );
81+ }
7582
7683 return "<br> \$CFG-> {$ cfg } = " . display_size ($ size );
7784 }
7885
86+ /**
87+ * Get a path's size (compatible with Totara)
88+ *
89+ * @param string $cfg the path to check
90+ * @return int $size size for a path as an integer
91+ */
92+ private function dirsize_totara (string $ cfg ): int {
93+ global $ CFG ;
94+ if (!property_exists ($ CFG , $ cfg )) {
95+ return 0 ;
96+ }
97+ $ rootdir = $ CFG ->{$ cfg };
98+
99+ return $ this ->dirsize_totara_helper ($ rootdir );
100+ }
101+
102+ /**
103+ * Recursively calculate the size of a directory (Totara-compatible).
104+ *
105+ * This replicates Moodle core's get_directory_size() logic,
106+ * but avoids using it directly for Totara compatibility.
107+ *
108+ * @param string $dir The directory path to measure
109+ * @return int Total size in bytes
110+ */
111+ private function dirsize_totara_helper (string $ dir ): int {
112+ if (!is_dir ($ dir )) {
113+ return 0 ;
114+ }
115+
116+ $ size = 0 ;
117+ if (!$ dh = @opendir ($ dir )) {
118+ return 0 ;
119+ }
120+
121+ while (false !== ($ file = readdir ($ dh ))) {
122+ // Skip hidden files and CVS dirs.
123+ if ($ file [0 ] === '. ' || $ file === 'CVS ' ) {
124+ continue ;
125+ }
126+
127+ $ fullfile = $ dir . '/ ' . $ file ;
128+
129+ if (is_dir ($ fullfile )) {
130+ // Recurse into subdirectory.
131+ $ size += $ this ->dirsize_totara_helper ($ fullfile );
132+ } else {
133+ $ filesize = filesize ($ fullfile );
134+ if ($ filesize !== false ) {
135+ $ size += $ filesize ;
136+ }
137+ }
138+ }
139+
140+ closedir ($ dh );
141+ return $ size ;
142+ }
79143}
0 commit comments