From ff776a90b133fa113c29511a5a7983a7a1a8b73c Mon Sep 17 00:00:00 2001 From: =?utf8?q?C=C3=B4me=20Chilliet?= Date: Fri, 20 Jan 2023 18:02:45 +0100 Subject: [PATCH] Strong type filesize related methods to ease 32bits problem findings MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- apps/files_external/lib/Lib/Storage/FTP.php | 2 +- lib/private/Archive/Archive.php | 3 +-- lib/private/Archive/TAR.php | 4 +--- lib/private/Archive/ZIP.php | 3 +-- lib/private/Files/Storage/Common.php | 2 +- lib/private/Files/Storage/FailedStorage.php | 2 +- lib/private/Files/Storage/Local.php | 4 ++-- lib/private/Files/Storage/Wrapper/Availability.php | 2 +- lib/private/Files/Storage/Wrapper/Encoding.php | 5 +---- lib/private/Files/Storage/Wrapper/Encryption.php | 5 +---- lib/private/Files/Storage/Wrapper/Jail.php | 5 +---- lib/private/Files/Storage/Wrapper/Wrapper.php | 5 +---- lib/private/Files/View.php | 2 +- lib/private/LargeFileHelper.php | 14 +++++++------- lib/private/Lockdown/Filesystem/NullStorage.php | 2 +- lib/public/Files/Storage.php | 5 +---- lib/public/Files/Storage/IStorage.php | 5 +---- 17 files changed, 24 insertions(+), 46 deletions(-) diff --git a/apps/files_external/lib/Lib/Storage/FTP.php b/apps/files_external/lib/Lib/Storage/FTP.php index 284f438fccd..4a6a0a3a011 100644 --- a/apps/files_external/lib/Lib/Storage/FTP.php +++ b/apps/files_external/lib/Lib/Storage/FTP.php @@ -140,7 +140,7 @@ class FTP extends Common { } } - public function filesize($path) { + public function filesize(string $path): false|int|float { $result = $this->getConnection()->size($this->buildPath($path)); if ($result === -1) { return false; diff --git a/lib/private/Archive/Archive.php b/lib/private/Archive/Archive.php index cef306230fd..ff6456113f8 100644 --- a/lib/private/Archive/Archive.php +++ b/lib/private/Archive/Archive.php @@ -50,9 +50,8 @@ abstract class Archive { /** * get the uncompressed size of a file in the archive - * @return int|false */ - abstract public function filesize(string $path); + abstract public function filesize(string $path): false|int|float; /** * get the last modified time of a file in the archive diff --git a/lib/private/Archive/TAR.php b/lib/private/Archive/TAR.php index 79c09cbe9e2..9dc906384e0 100644 --- a/lib/private/Archive/TAR.php +++ b/lib/private/Archive/TAR.php @@ -165,10 +165,8 @@ class TAR extends Archive { /** * get the uncompressed size of a file in the archive - * - * @return int|false */ - public function filesize(string $path) { + public function filesize(string $path): false|int|float { $stat = $this->getHeader($path); return $stat['size'] ?? false; } diff --git a/lib/private/Archive/ZIP.php b/lib/private/Archive/ZIP.php index 743d313f951..f98009ecc3a 100644 --- a/lib/private/Archive/ZIP.php +++ b/lib/private/Archive/ZIP.php @@ -91,9 +91,8 @@ class ZIP extends Archive { /** * get the uncompressed size of a file in the archive - * @return int|false */ - public function filesize(string $path) { + public function filesize(string $path): false|int|float { $stat = $this->zip->statName($path); return $stat['size'] ?? false; } diff --git a/lib/private/Files/Storage/Common.php b/lib/private/Files/Storage/Common.php index 0c121feb11e..8b46df3df6e 100644 --- a/lib/private/Files/Storage/Common.php +++ b/lib/private/Files/Storage/Common.php @@ -121,7 +121,7 @@ abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage { return $this->filetype($path) === 'file'; } - public function filesize($path) { + public function filesize(string $path): false|int|float { if ($this->is_dir($path)) { return 0; //by definition } else { diff --git a/lib/private/Files/Storage/FailedStorage.php b/lib/private/Files/Storage/FailedStorage.php index d748b3410c3..ae0c43ce1dc 100644 --- a/lib/private/Files/Storage/FailedStorage.php +++ b/lib/private/Files/Storage/FailedStorage.php @@ -80,7 +80,7 @@ class FailedStorage extends Common { throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e); } - public function filesize($path) { + public function filesize(string $path): false|int|float { throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e); } diff --git a/lib/private/Files/Storage/Local.php b/lib/private/Files/Storage/Local.php index 4b5154b207a..0bdbcb42d0f 100644 --- a/lib/private/Files/Storage/Local.php +++ b/lib/private/Files/Storage/Local.php @@ -242,14 +242,14 @@ class Local extends \OC\Files\Storage\Common { return $filetype; } - public function filesize($path) { + public function filesize(string $path): false|int|float { if (!$this->is_file($path)) { return 0; } $fullPath = $this->getSourcePath($path); if (PHP_INT_SIZE === 4) { $helper = new \OC\LargeFileHelper; - return $helper->getFileSize($fullPath); + return $helper->getFileSize($fullPath) ?? false; } return filesize($fullPath); } diff --git a/lib/private/Files/Storage/Wrapper/Availability.php b/lib/private/Files/Storage/Wrapper/Availability.php index a4a6fa0bd16..2d3d33ff024 100644 --- a/lib/private/Files/Storage/Wrapper/Availability.php +++ b/lib/private/Files/Storage/Wrapper/Availability.php @@ -165,7 +165,7 @@ class Availability extends Wrapper { } /** {@inheritdoc} */ - public function filesize($path) { + public function filesize(string $path): false|int|float { $this->checkAvailability(); try { return parent::filesize($path); diff --git a/lib/private/Files/Storage/Wrapper/Encoding.php b/lib/private/Files/Storage/Wrapper/Encoding.php index ed680f5045d..71587748931 100644 --- a/lib/private/Files/Storage/Wrapper/Encoding.php +++ b/lib/private/Files/Storage/Wrapper/Encoding.php @@ -210,11 +210,8 @@ class Encoding extends Wrapper { /** * see https://www.php.net/manual/en/function.filesize.php * The result for filesize when called on a folder is required to be 0 - * - * @param string $path - * @return int|bool */ - public function filesize($path) { + public function filesize(string $path): false|int|float { return $this->storage->filesize($this->findPathToUse($path)); } diff --git a/lib/private/Files/Storage/Wrapper/Encryption.php b/lib/private/Files/Storage/Wrapper/Encryption.php index 0bd799507ff..7c453e95092 100644 --- a/lib/private/Files/Storage/Wrapper/Encryption.php +++ b/lib/private/Files/Storage/Wrapper/Encryption.php @@ -133,11 +133,8 @@ class Encryption extends Wrapper { /** * see https://www.php.net/manual/en/function.filesize.php * The result for filesize when called on a folder is required to be 0 - * - * @param string $path - * @return int */ - public function filesize($path) { + public function filesize(string $path): int|float { $fullPath = $this->getFullPath($path); /** @var CacheEntry $info */ diff --git a/lib/private/Files/Storage/Wrapper/Jail.php b/lib/private/Files/Storage/Wrapper/Jail.php index 9834ae5a954..0f4101aad66 100644 --- a/lib/private/Files/Storage/Wrapper/Jail.php +++ b/lib/private/Files/Storage/Wrapper/Jail.php @@ -158,11 +158,8 @@ class Jail extends Wrapper { /** * see https://www.php.net/manual/en/function.filesize.php * The result for filesize when called on a folder is required to be 0 - * - * @param string $path - * @return int|bool */ - public function filesize($path) { + public function filesize(string $path): false|int|float { return $this->getWrapperStorage()->filesize($this->getUnjailedPath($path)); } diff --git a/lib/private/Files/Storage/Wrapper/Wrapper.php b/lib/private/Files/Storage/Wrapper/Wrapper.php index ed7e137fd88..345bf5fa2da 100644 --- a/lib/private/Files/Storage/Wrapper/Wrapper.php +++ b/lib/private/Files/Storage/Wrapper/Wrapper.php @@ -148,11 +148,8 @@ class Wrapper implements \OC\Files\Storage\Storage, ILockingStorage, IWriteStrea /** * see https://www.php.net/manual/en/function.filesize.php * The result for filesize when called on a folder is required to be 0 - * - * @param string $path - * @return int|bool */ - public function filesize($path) { + public function filesize(string $path): false|int|float { return $this->getWrapperStorage()->filesize($path); } diff --git a/lib/private/Files/View.php b/lib/private/Files/View.php index 8f073da9164..08f83fd755f 100644 --- a/lib/private/Files/View.php +++ b/lib/private/Files/View.php @@ -411,7 +411,7 @@ class View { * @param string $path * @return mixed */ - public function filesize($path) { + public function filesize(string $path) { return $this->basicOperation('filesize', $path); } diff --git a/lib/private/LargeFileHelper.php b/lib/private/LargeFileHelper.php index a9c5a329620..c726737a604 100755 --- a/lib/private/LargeFileHelper.php +++ b/lib/private/LargeFileHelper.php @@ -73,7 +73,7 @@ class LargeFileHelper { * * @return string Unsigned integer base-10 string */ - public function formatUnsignedInteger($number) { + public function formatUnsignedInteger(int|float|string $number): string { if (is_float($number)) { // Undo the effect of the php.ini setting 'precision'. return number_format($number, 0, '', ''); @@ -98,7 +98,7 @@ class LargeFileHelper { * @return null|int|float Number of bytes as number (float or int) or * null on failure. */ - public function getFileSize($filename) { + public function getFileSize(string $filename): null|int|float { $fileSize = $this->getFileSizeViaCurl($filename); if (!is_null($fileSize)) { return $fileSize; @@ -118,7 +118,7 @@ class LargeFileHelper { * @return null|int|float Number of bytes as number (float or int) or * null on failure. */ - public function getFileSizeViaCurl($fileName) { + public function getFileSizeViaCurl(string $fileName): null|int|float { if (\OC::$server->get(IniGetWrapper::class)->getString('open_basedir') === '') { $encodedFileName = rawurlencode($fileName); $ch = curl_init("file:///$encodedFileName"); @@ -146,7 +146,7 @@ class LargeFileHelper { * @return null|int|float Number of bytes as number (float or int) or * null on failure. */ - public function getFileSizeViaExec($filename) { + public function getFileSizeViaExec(string $filename): null|int|float { if (\OCP\Util::isFunctionEnabled('exec')) { $os = strtolower(php_uname('s')); $arg = escapeshellarg($filename); @@ -171,7 +171,7 @@ class LargeFileHelper { * * @return int|float Number of bytes as number (float or int). */ - public function getFileSizeNative($filename) { + public function getFileSizeNative(string $filename): int|float { $result = filesize($filename); if ($result < 0) { // For file sizes between 2 GiB and 4 GiB, filesize() will return a @@ -188,7 +188,7 @@ class LargeFileHelper { * @param string $fullPath * @return int */ - public function getFileMtime($fullPath) { + public function getFileMtime(string $fullPath): int { try { $result = filemtime($fullPath); } catch (\Exception $e) { @@ -205,7 +205,7 @@ class LargeFileHelper { return $result; } - protected function exec($cmd) { + protected function exec(string $cmd): null|int|float { $result = trim(exec($cmd)); return ctype_digit($result) ? 0 + $result : null; } diff --git a/lib/private/Lockdown/Filesystem/NullStorage.php b/lib/private/Lockdown/Filesystem/NullStorage.php index 8427a4658d4..8b5b63bd90a 100644 --- a/lib/private/Lockdown/Filesystem/NullStorage.php +++ b/lib/private/Lockdown/Filesystem/NullStorage.php @@ -65,7 +65,7 @@ class NullStorage extends Common { return ($path === '') ? 'dir' : false; } - public function filesize($path) { + public function filesize(string $path): false|int|float { throw new \OC\ForbiddenException('This request is not allowed to access the filesystem'); } diff --git a/lib/public/Files/Storage.php b/lib/public/Files/Storage.php index a0acb4508d2..0d60746a55e 100644 --- a/lib/public/Files/Storage.php +++ b/lib/public/Files/Storage.php @@ -133,12 +133,9 @@ interface Storage extends IStorage { /** * see https://www.php.net/manual/en/function.filesize.php * The result for filesize when called on a folder is required to be 0 - * - * @param string $path - * @return int|bool * @since 6.0.0 */ - public function filesize($path); + public function filesize(string $path): false|int|float; /** * check if a file can be created in $path diff --git a/lib/public/Files/Storage/IStorage.php b/lib/public/Files/Storage/IStorage.php index eb5522909c6..284cf4e30e7 100644 --- a/lib/public/Files/Storage/IStorage.php +++ b/lib/public/Files/Storage/IStorage.php @@ -130,12 +130,9 @@ interface IStorage { /** * see https://www.php.net/manual/en/function.filesize.php * The result for filesize when called on a folder is required to be 0 - * - * @param string $path - * @return int|bool * @since 9.0.0 */ - public function filesize($path); + public function filesize(string $path): false|int|float; /** * check if a file can be created in $path -- 2.39.5