From 6e935cc29cb1aba7c004158979c3304ea32119c8 Mon Sep 17 00:00:00 2001 From: Brendan Heywood Date: Thu, 17 Sep 2020 10:57:22 +1000 Subject: [PATCH] MDL-69718 core: Added support for TB and PT to display_size --- lang/en/moodle.php | 2 ++ lib/moodlelib.php | 36 ++++++++++++++++++------------- lib/setuplib.php | 20 ++++++++++-------- lib/tests/moodlelib_test.php | 41 ++++++++++++++++++++++++++++++++++++ lib/tests/setuplib_test.php | 28 ++++++++++++++---------- 5 files changed, 92 insertions(+), 35 deletions(-) diff --git a/lang/en/moodle.php b/lang/en/moodle.php index 2d8bd7267fe4a..68c2723c0614f 100644 --- a/lang/en/moodle.php +++ b/lang/en/moodle.php @@ -1936,6 +1936,8 @@ $string['sizegb'] = 'GB'; $string['sizekb'] = 'KB'; $string['sizemb'] = 'MB'; +$string['sizepb'] = 'PB'; +$string['sizetb'] = 'TB'; $string['skipped'] = 'Skipped'; $string['skiptocategorylisting'] = 'Skip to the category listings'; $string['skiptocourselisting'] = 'Skip to the course listings'; diff --git a/lib/moodlelib.php b/lib/moodlelib.php index c15e8d6b63d48..d1270bceb439a 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -7104,27 +7104,33 @@ function get_directory_size($rootdir, $excludefile='') { */ function display_size($size) { - static $gb, $mb, $kb, $b; + static $units; if ($size === USER_CAN_IGNORE_FILE_SIZE_LIMITS) { return get_string('unlimited'); } - if (empty($gb)) { - $gb = get_string('sizegb'); - $mb = get_string('sizemb'); - $kb = get_string('sizekb'); - $b = get_string('sizeb'); - } - - if ($size >= 1073741824) { - $size = round($size / 1073741824 * 10) / 10 . $gb; - } else if ($size >= 1048576) { - $size = round($size / 1048576 * 10) / 10 . $mb; - } else if ($size >= 1024) { - $size = round($size / 1024 * 10) / 10 . $kb; + if (empty($units)) { + $units[] = get_string('sizeb'); + $units[] = get_string('sizekb'); + $units[] = get_string('sizemb'); + $units[] = get_string('sizegb'); + $units[] = get_string('sizetb'); + $units[] = get_string('sizepb'); + } + + if ($size >= 1024 ** 5) { + $size = round($size / 1024 ** 5 * 10) / 10 . $units[5]; + } else if ($size >= 1024 ** 4) { + $size = round($size / 1024 ** 4 * 10) / 10 . $units[4]; + } else if ($size >= 1024 ** 3) { + $size = round($size / 1024 ** 3 * 10) / 10 . $units[3]; + } else if ($size >= 1024 ** 2) { + $size = round($size / 1024 ** 2 * 10) / 10 . $units[2]; + } else if ($size >= 1024 ** 1) { + $size = round($size / 1024 ** 1 * 10) / 10 . $units[1]; } else { - $size = intval($size) .' '. $b; // File sizes over 2GB can not work in 32bit PHP anyway. + $size = intval($size) .' '. $units[0]; // File sizes over 2GB can not work in 32bit PHP anyway. } return $size; } diff --git a/lib/setuplib.php b/lib/setuplib.php index 5a2b1efc81c50..2133479e41f2d 100644 --- a/lib/setuplib.php +++ b/lib/setuplib.php @@ -1325,17 +1325,19 @@ function get_real_size($size = 0) { } static $binaryprefixes = array( - 'K' => 1024, - 'k' => 1024, - 'M' => 1048576, - 'm' => 1048576, - 'G' => 1073741824, - 'g' => 1073741824, - 'T' => 1099511627776, - 't' => 1099511627776, + 'K' => 1024 ** 1, + 'k' => 1024 ** 1, + 'M' => 1024 ** 2, + 'm' => 1024 ** 2, + 'G' => 1024 ** 3, + 'g' => 1024 ** 3, + 'T' => 1024 ** 4, + 't' => 1024 ** 4, + 'P' => 1024 ** 5, + 'p' => 1024 ** 5, ); - if (preg_match('/^([0-9]+)([KMGT])/i', $size, $matches)) { + if (preg_match('/^([0-9]+)([KMGTP])/i', $size, $matches)) { return $matches[1] * $binaryprefixes[$matches[2]]; } diff --git a/lib/tests/moodlelib_test.php b/lib/tests/moodlelib_test.php index cbb7dc2d92554..0b89adb2dc043 100644 --- a/lib/tests/moodlelib_test.php +++ b/lib/tests/moodlelib_test.php @@ -4776,4 +4776,45 @@ public function test_rename_to_unused_name_failure() { $file = $CFG->dataroot . '/argh.txt'; $this->assertFalse(rename_to_unused_name($file)); } + + /** + * Provider for display_size + * + * @return array of ($size, $expected) + */ + public function display_size_provider() { + + return [ + [0, '0 bytes' ], + [1, '1 bytes' ], + [1023, '1023 bytes' ], + [1024, '1KB' ], + [2222, '2.2KB' ], + [33333, '32.6KB' ], + [444444, '434KB' ], + [5555555, '5.3MB' ], + [66666666, '63.6MB' ], + [777777777, '741.7MB'], + [8888888888, '8.3GB' ], + [99999999999, '93.1GB' ], + [111111111111, '103.5GB'], + [2222222222222, '2TB' ], + [33333333333333, '30.3TB' ], + [444444444444444, '404.2TB'], + [5555555555555555, '4.9PB' ], + [66666666666666666, '59.2PB' ], + [777777777777777777, '690.8PB'], + ]; + } + + /** + * Test display_size + * @dataProvider display_size_provider + * @param int $size the size in bytes + * @param string $expected the expected string. + */ + public function test_display_size($size, $expected) { + $result = display_size($size); + $this->assertEquals($expected, $result); + } } diff --git a/lib/tests/setuplib_test.php b/lib/tests/setuplib_test.php index 3e00f6bc14886..bed8fc7588afe 100644 --- a/lib/tests/setuplib_test.php +++ b/lib/tests/setuplib_test.php @@ -450,17 +450,23 @@ public function get_exception_info($ex) { */ public function data_for_test_get_real_size() { return array( - array('8KB', 8192), - array('8Kb', 8192), - array('8K', 8192), - array('8k', 8192), - array('50MB', 52428800), - array('50Mb', 52428800), - array('50M', 52428800), - array('50m', 52428800), - array('8Gb', 8589934592), - array('8GB', 8589934592), - array('8G', 8589934592), + array('8KB', 8192), + array('8Kb', 8192), + array('8K', 8192), + array('8k', 8192), + array('50MB', 52428800), + array('50Mb', 52428800), + array('50M', 52428800), + array('50m', 52428800), + array('8GB', 8589934592), + array('8Gb', 8589934592), + array('8G', 8589934592), + array('7T', 7696581394432), + array('7TB', 7696581394432), + array('7Tb', 7696581394432), + array('6P', 6755399441055744), + array('6PB', 6755399441055744), + array('6Pb', 6755399441055744), ); }