Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable27] Do not cast sizes to int in Trashbin class #38717

Merged
merged 2 commits into from
Jun 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 43 additions & 38 deletions apps/files_trashbin/lib/Trashbin.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@
use OCP\Files\Folder;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\IConfig;
use OCP\Lock\ILockingProvider;
use OCP\Lock\LockedException;
use Psr\Log\LoggerInterface;

class Trashbin {

// unit: percentage; 50% of available disk space/quota
public const DEFAULTMAXSIZE = 50;

Expand Down Expand Up @@ -191,7 +191,7 @@ private static function setUpTrash($user) {
* @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);
Expand Down Expand Up @@ -305,10 +305,7 @@ public static function move2trash($file_path, $ownerOnly = false) {
$trashStorage->unlink($trashInternalPath);
}

$config = \OC::$server->getConfig();
$systemTrashbinSize = (int)$config->getAppValue('files_trashbin', 'trashbin_size', '-1');
$userTrashbinSize = (int)$config->getUserValue($owner, 'files_trashbin', 'trashbin_size', '-1');
$configuredTrashbinSize = ($userTrashbinSize < 0) ? $systemTrashbinSize : $userTrashbinSize;
$configuredTrashbinSize = static::getConfiguredTrashbinSize($owner);
if ($configuredTrashbinSize >= 0 && $sourceInfo->getSize() >= $configuredTrashbinSize) {
return false;
}
Expand Down Expand Up @@ -380,13 +377,26 @@ public static function move2trash($file_path, $ownerOnly = false) {
return $moveSuccessful;
}

private static function getConfiguredTrashbinSize(string $user): int|float {
$config = \OC::$server->get(IConfig::class);
$userTrashbinSize = $config->getUserValue($user, 'files_trashbin', 'trashbin_size', '-1');
if (is_numeric($userTrashbinSize) && ($userTrashbinSize > -1)) {
return \OCP\Util::numericToNumber($userTrashbinSize);
}
$systemTrashbinSize = $config->getAppValue('files_trashbin', 'trashbin_size', '-1');
if (is_numeric($systemTrashbinSize)) {
return \OCP\Util::numericToNumber($systemTrashbinSize);
}
return -1;
}

/**
* Move file versions to trash so that they can be restored later
*
* @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)) {
Expand Down Expand Up @@ -647,7 +657,7 @@ protected static function emitTrashbinPostDelete($path) {
* @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();
Expand Down Expand Up @@ -689,14 +699,11 @@ public static function delete($filename, $user, $timestamp = null) {
}

/**
* @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)) {
Expand Down Expand Up @@ -752,26 +759,21 @@ public static function deleteUser($uid) {
/**
* 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) {
$config = \OC::$server->getConfig();
$userTrashbinSize = (int)$config->getUserValue($user, 'files_trashbin', 'trashbin_size', '-1');
if ($userTrashbinSize > -1) {
return $userTrashbinSize - $trashbinSize;
}
$systemTrashbinSize = (int)$config->getAppValue('files_trashbin', 'trashbin_size', '-1');
if ($systemTrashbinSize > -1) {
return $systemTrashbinSize - $trashbinSize;
private static function calculateFreeSpace(int|float $trashbinSize, string $user): int|float {
$configuredTrashbinSize = static::getConfiguredTrashbinSize($user);
if ($configuredTrashbinSize > -1) {
return $configuredTrashbinSize - $trashbinSize;
}

$softQuota = true;
$userObject = \OC::$server->getUserManager()->get($user);
if (is_null($userObject)) {
return 0;
}
$softQuota = true;
$quota = $userObject->getQuota();
if ($quota === null || $quota === 'none') {
$quota = Filesystem::free_space('/');
Expand All @@ -782,6 +784,10 @@ private static function calculateFreeSpace($trashbinSize, $user) {
}
} else {
$quota = \OCP\Util::computerFileSize($quota);
// invalid quota
if ($quota === false) {
$quota = PHP_INT_MAX;
}
}

// calculate available space for trash bin
Expand All @@ -801,7 +807,7 @@ private static function calculateFreeSpace($trashbinSize, $user) {
$availableSpace = $quota;
}

return (int)$availableSpace;
return \OCP\Util::numericToNumber($availableSpace);
}

/**
Expand Down Expand Up @@ -858,10 +864,10 @@ private static function scheduleExpire($user) {
*
* @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');
Expand All @@ -887,7 +893,7 @@ protected static function deleteFiles($files, $user, $availableSpace) {
*
* @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 */
Expand Down Expand Up @@ -927,10 +933,10 @@ public static function deleteExpiredFiles($files, $user) {
* @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);
Expand Down Expand Up @@ -964,9 +970,8 @@ private static function copy_recursive($source, $destination, View $view) {
*
* @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 = [];

Expand Down Expand Up @@ -1061,9 +1066,9 @@ private static function getUniqueFilename($location, $filename, View $view) {
* 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;
Expand Down Expand Up @@ -1092,9 +1097,9 @@ private static function calculateSize($view) {
* 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;
Expand Down