mirror of
https://github.com/nextcloud/server.git
synced 2026-05-28 04:32:30 -04:00
Strong type filesize related methods to ease 32bits problem findings
Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
This commit is contained in:
parent
2d8e696c24
commit
ff776a90b1
17 changed files with 24 additions and 46 deletions
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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));
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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 */
|
||||
|
|
|
|||
|
|
@ -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));
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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');
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue