(cp ab81e77) fixes #1290 avoid calling external command to calculate directory size

+ add a 5 minutes cache to avoid calculating cache size too often
* tested with anold PHP 5.5 (could work with earlier versions, but I prefer to not take the risk)
This commit is contained in:
plegall
2021-01-21 10:43:47 +01:00
parent 0e7531515c
commit d7cb303258
2 changed files with 37 additions and 8 deletions

View File

@@ -625,6 +625,28 @@ function get_fs_directories($path, $recursive = true)
return $dirs;
}
/**
* returns the size (in bytes) of a filesystem directory
*
* @since 11
* @param string path
* @return int
*/
function get_fs_directory_size($path)
{
$bytestotal = 0;
$path = realpath($path);
if ($path !== false && $path != '' && file_exists($path))
{
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS)) as $object)
{
$bytestotal += $object->getSize();
}
}
return $bytestotal;
}
/**
* save the rank depending on given categories order
*

View File

@@ -388,15 +388,22 @@ if (isset($result[0]['SUM(filesize)']))
$data_storage['Formats'] = $result[0]['SUM(filesize)'];
}
//If the host is not windows, get the cache size
if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN')
// PHP 5.5.19 is the oldest version get_fs_directory_size was tested on
if (version_compare(PHP_VERSION, '5.5.19') >= 0)
{
$f = './_data';
$io = popen ( '/usr/bin/du -sk ' . $f, 'r' );
$size = fgets ($io, 4096);
$size = substr ( $size, 0, strpos ( $size, "\t" ) );
pclose ( $io );
$data_storage['Cache'] = $size;
if (!isset($_SESSION['cachedir_info']) or $_SESSION['cachedir_info']['calculated_on'] < strtotime('5 minutes ago'))
{
$start_time = get_moment();
$_SESSION['cachedir_info'] = array(
'size' => get_fs_directory_size($conf['data_location']),
'calculated_on' => time(),
);
$logger->debug('[admin/intro::'.__LINE__.'] cache size calculated in '.get_elapsed_time($start_time, get_moment()).' ('.$_SESSION['cachedir_info']['size'].' bytes)');
}
$data_storage['Cache'] = $_SESSION['cachedir_info']['size'] / 1000;
}
//Calculate total storage