From 546d94c3ec5b0cb06cc284dead0da98ee346fc81 Mon Sep 17 00:00:00 2001 From: =?utf8?q?C=C3=B4me=20Chilliet?= Date: Thu, 27 Apr 2023 09:56:05 +0200 Subject: [PATCH] Fix file_get_content signatures to make it clear it can return false MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit In File::getContent, which must return a string, throw an Exception instead of returning false. Signed-off-by: Côme Chilliet --- lib/private/Files/Filesystem.php | 2 +- lib/private/Files/Node/File.php | 12 +++++++----- lib/private/Files/Storage/Wrapper/Encoding.php | 2 +- lib/private/Files/Storage/Wrapper/Encryption.php | 2 +- lib/private/Files/Storage/Wrapper/Jail.php | 2 +- lib/private/Files/Storage/Wrapper/Wrapper.php | 2 +- lib/private/Files/View.php | 2 +- lib/public/Files/File.php | 1 + lib/public/Files/Storage.php | 2 +- lib/public/Files/Storage/IStorage.php | 2 +- 10 files changed, 16 insertions(+), 13 deletions(-) diff --git a/lib/private/Files/Filesystem.php b/lib/private/Files/Filesystem.php index c50fa1f9de9..2eab4c58a0c 100644 --- a/lib/private/Files/Filesystem.php +++ b/lib/private/Files/Filesystem.php @@ -554,7 +554,7 @@ class Filesystem { } /** - * @return string + * @return string|false */ public static function file_get_contents($path) { return self::$defaultInstance->file_get_contents($path); diff --git a/lib/private/Files/Node/File.php b/lib/private/Files/Node/File.php index 27056efef72..b6cd6571651 100644 --- a/lib/private/Files/Node/File.php +++ b/lib/private/Files/Node/File.php @@ -46,14 +46,16 @@ class File extends Node implements \OCP\Files\File { /** * @return string * @throws NotPermittedException + * @throws GenericFileException * @throws LockedException */ public function getContent() { if ($this->checkPermissions(\OCP\Constants::PERMISSION_READ)) { - /** - * @var \OC\Files\Storage\Storage $storage; - */ - return $this->view->file_get_contents($this->path); + $content = $this->view->file_get_contents($this->path); + if ($content === false) { + throw new GenericFileException(); + } + return $content; } else { throw new NotPermittedException(); } @@ -62,7 +64,7 @@ class File extends Node implements \OCP\Files\File { /** * @param string|resource $data * @throws NotPermittedException - * @throws \OCP\Files\GenericFileException + * @throws GenericFileException * @throws LockedException */ public function putContent($data) { diff --git a/lib/private/Files/Storage/Wrapper/Encoding.php b/lib/private/Files/Storage/Wrapper/Encoding.php index dd699993e84..6633cbf41e3 100644 --- a/lib/private/Files/Storage/Wrapper/Encoding.php +++ b/lib/private/Files/Storage/Wrapper/Encoding.php @@ -300,7 +300,7 @@ class Encoding extends Wrapper { * see https://www.php.net/manual/en/function.file_get_contents.php * * @param string $path - * @return string|bool + * @return string|false */ public function file_get_contents($path) { return $this->storage->file_get_contents($this->findPathToUse($path)); diff --git a/lib/private/Files/Storage/Wrapper/Encryption.php b/lib/private/Files/Storage/Wrapper/Encryption.php index 720111793de..9c0e6c91463 100644 --- a/lib/private/Files/Storage/Wrapper/Encryption.php +++ b/lib/private/Files/Storage/Wrapper/Encryption.php @@ -217,7 +217,7 @@ class Encryption extends Wrapper { * see https://www.php.net/manual/en/function.file_get_contents.php * * @param string $path - * @return string + * @return string|false */ public function file_get_contents($path) { $encryptionModule = $this->getEncryptionModule($path); diff --git a/lib/private/Files/Storage/Wrapper/Jail.php b/lib/private/Files/Storage/Wrapper/Jail.php index 619d8b07137..caf039d4cca 100644 --- a/lib/private/Files/Storage/Wrapper/Jail.php +++ b/lib/private/Files/Storage/Wrapper/Jail.php @@ -248,7 +248,7 @@ class Jail extends Wrapper { * see https://www.php.net/manual/en/function.file_get_contents.php * * @param string $path - * @return string|bool + * @return string|false */ public function file_get_contents($path) { return $this->getWrapperStorage()->file_get_contents($this->getUnjailedPath($path)); diff --git a/lib/private/Files/Storage/Wrapper/Wrapper.php b/lib/private/Files/Storage/Wrapper/Wrapper.php index 28a1e0b1e4d..9f5564b4490 100644 --- a/lib/private/Files/Storage/Wrapper/Wrapper.php +++ b/lib/private/Files/Storage/Wrapper/Wrapper.php @@ -238,7 +238,7 @@ class Wrapper implements \OC\Files\Storage\Storage, ILockingStorage, IWriteStrea * see https://www.php.net/manual/en/function.file_get_contents.php * * @param string $path - * @return string|bool + * @return string|false */ public function file_get_contents($path) { return $this->getWrapperStorage()->file_get_contents($path); diff --git a/lib/private/Files/View.php b/lib/private/Files/View.php index 91e6cc2d60e..5970c213210 100644 --- a/lib/private/Files/View.php +++ b/lib/private/Files/View.php @@ -564,7 +564,7 @@ class View { /** * @param string $path - * @return mixed + * @return string|false * @throws LockedException */ public function file_get_contents($path) { diff --git a/lib/public/Files/File.php b/lib/public/Files/File.php index 93d0ab860c1..7c1e36cb3eb 100644 --- a/lib/public/Files/File.php +++ b/lib/public/Files/File.php @@ -40,6 +40,7 @@ interface File extends Node { * * @return string * @throws NotPermittedException + * @throws GenericFileException * @throws LockedException * @since 6.0.0 */ diff --git a/lib/public/Files/Storage.php b/lib/public/Files/Storage.php index c165266a1b7..31405a26bfa 100644 --- a/lib/public/Files/Storage.php +++ b/lib/public/Files/Storage.php @@ -217,7 +217,7 @@ interface Storage extends IStorage { * see https://www.php.net/manual/en/function.file_get_contents.php * * @param string $path - * @return string|bool + * @return string|false * @since 6.0.0 */ public function file_get_contents($path); diff --git a/lib/public/Files/Storage/IStorage.php b/lib/public/Files/Storage/IStorage.php index 2f38165830b..00e98fdfbb6 100644 --- a/lib/public/Files/Storage/IStorage.php +++ b/lib/public/Files/Storage/IStorage.php @@ -214,7 +214,7 @@ interface IStorage { * see https://www.php.net/manual/en/function.file_get_contents.php * * @param string $path - * @return string|bool + * @return string|false * @since 9.0.0 */ public function file_get_contents($path); -- 2.39.5