Do not cast sizes to int in Trashbin class

This helps with 32bits support

Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
This commit is contained in:
Côme Chilliet 2023-06-08 13:07:57 +02:00
parent 9ebeb5e736
commit b234b9637e
No known key found for this signature in database
GPG key ID: A3E2F658B28C760A

View file

@ -64,7 +64,6 @@ use OCP\Lock\LockedException;
use Psr\Log\LoggerInterface;
class Trashbin {
// unit: percentage; 50% of available disk space/quota
public const DEFAULTMAXSIZE = 50;
@ -191,7 +190,7 @@ class Trashbin {
* @param string $owner
* @param string $targetPath
* @param $user
* @param integer $timestamp
* @param int $timestamp
*/
private static function copyFilesToUser($sourcePath, $owner, $targetPath, $user, $timestamp) {
self::setUpTrash($owner);
@ -306,8 +305,8 @@ class Trashbin {
}
$config = \OC::$server->getConfig();
$systemTrashbinSize = (int)$config->getAppValue('files_trashbin', 'trashbin_size', '-1');
$userTrashbinSize = (int)$config->getUserValue($owner, 'files_trashbin', 'trashbin_size', '-1');
$systemTrashbinSize = \OCP\Util::numericToNumber($config->getAppValue('files_trashbin', 'trashbin_size', '-1'));
$userTrashbinSize = \OCP\Util::numericToNumber($config->getUserValue($owner, 'files_trashbin', 'trashbin_size', '-1'));
$configuredTrashbinSize = ($userTrashbinSize < 0) ? $systemTrashbinSize : $userTrashbinSize;
if ($configuredTrashbinSize >= 0 && $sourceInfo->getSize() >= $configuredTrashbinSize) {
return false;
@ -386,7 +385,7 @@ class Trashbin {
* @param string $filename of deleted file
* @param string $owner owner user id
* @param string $ownerPath path relative to the owner's home storage
* @param integer $timestamp when the file was deleted
* @param int $timestamp when the file was deleted
*/
private static function retainVersions($filename, $owner, $ownerPath, $timestamp) {
if (\OCP\Server::get(IAppManager::class)->isEnabledForUser('files_versions') && !empty($ownerPath)) {
@ -647,7 +646,7 @@ class Trashbin {
* @param string $user
* @param int $timestamp of deletion time
*
* @return int size of deleted files
* @return int|float size of deleted files
*/
public static function delete($filename, $user, $timestamp = null) {
$userRoot = \OC::$server->getUserFolder($user)->getParent();
@ -689,14 +688,11 @@ class Trashbin {
}
/**
* @param View $view
* @param string $file
* @param string $filename
* @param integer|null $timestamp
* @param string $user
* @return int
* @param ?int $timestamp
*/
private static function deleteVersions(View $view, $file, $filename, $timestamp, $user) {
private static function deleteVersions(View $view, $file, $filename, $timestamp, string $user): int|float {
$size = 0;
if (\OCP\Server::get(IAppManager::class)->isEnabledForUser('files_versions')) {
if ($view->is_dir('files_trashbin/versions/' . $file)) {
@ -752,17 +748,17 @@ class Trashbin {
/**
* calculate remaining free space for trash bin
*
* @param integer $trashbinSize current size of the trash bin
* @param int|float $trashbinSize current size of the trash bin
* @param string $user
* @return int available free space for trash bin
* @return int|float available free space for trash bin
*/
private static function calculateFreeSpace($trashbinSize, $user) {
private static function calculateFreeSpace(int|float $trashbinSize, string $user): int|float {
$config = \OC::$server->getConfig();
$userTrashbinSize = (int)$config->getUserValue($user, 'files_trashbin', 'trashbin_size', '-1');
$userTrashbinSize = \OCP\Util::numericToNumber($config->getUserValue($user, 'files_trashbin', 'trashbin_size', '-1'));
if ($userTrashbinSize > -1) {
return $userTrashbinSize - $trashbinSize;
}
$systemTrashbinSize = (int)$config->getAppValue('files_trashbin', 'trashbin_size', '-1');
$systemTrashbinSize = \OCP\Util::numericToNumber($config->getAppValue('files_trashbin', 'trashbin_size', '-1'));
if ($systemTrashbinSize > -1) {
return $systemTrashbinSize - $trashbinSize;
}
@ -801,7 +797,7 @@ class Trashbin {
$availableSpace = $quota;
}
return (int)$availableSpace;
return \OCP\Util::numericToNumber($availableSpace);
}
/**
@ -858,10 +854,10 @@ class Trashbin {
*
* @param array $files
* @param string $user
* @param int $availableSpace available disc space
* @return int size of deleted files
* @param int|float $availableSpace available disc space
* @return int|float size of deleted files
*/
protected static function deleteFiles($files, $user, $availableSpace) {
protected static function deleteFiles(array $files, string $user, int|float $availableSpace): int|float {
/** @var Application $application */
$application = \OC::$server->query(Application::class);
$expiration = $application->getContainer()->query('Expiration');
@ -887,7 +883,7 @@ class Trashbin {
*
* @param array $files list of files sorted by mtime
* @param string $user
* @return integer[] size of deleted files and number of deleted files
* @return array{int|float, int} size of deleted files and number of deleted files
*/
public static function deleteExpiredFiles($files, $user) {
/** @var Expiration $expiration */
@ -927,10 +923,10 @@ class Trashbin {
* @param string $source source path, relative to the users files directory
* @param string $destination destination path relative to the users root directory
* @param View $view file view for the users root directory
* @return int
* @return int|float
* @throws Exceptions\CopyRecursiveException
*/
private static function copy_recursive($source, $destination, View $view) {
private static function copy_recursive($source, $destination, View $view): int|float {
$size = 0;
if ($view->is_dir($source)) {
$view->mkdir($destination);
@ -964,9 +960,8 @@ class Trashbin {
*
* @param string $filename name of the file which should be restored
* @param int $timestamp timestamp when the file was deleted
* @return array
*/
private static function getVersionsFromTrash($filename, $timestamp, $user) {
private static function getVersionsFromTrash($filename, $timestamp, string $user): array {
$view = new View('/' . $user . '/files_trashbin/versions');
$versions = [];
@ -1061,9 +1056,9 @@ class Trashbin {
* get the size from a given root folder
*
* @param View $view file view on the root folder
* @return integer size of the folder
* @return int|float size of the folder
*/
private static function calculateSize($view) {
private static function calculateSize(View $view): int|float {
$root = \OC::$server->getConfig()->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . $view->getAbsolutePath('');
if (!file_exists($root)) {
return 0;
@ -1092,9 +1087,9 @@ class Trashbin {
* get current size of trash bin from a given user
*
* @param string $user user who owns the trash bin
* @return integer trash bin size
* @return int|float trash bin size
*/
private static function getTrashbinSize($user) {
private static function getTrashbinSize(string $user): int|float {
$view = new View('/' . $user);
$fileInfo = $view->getFileInfo('/files_trashbin');
return isset($fileInfo['size']) ? $fileInfo['size'] : 0;