From 546d94c3ec5b0cb06cc284dead0da98ee346fc81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?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=UTF-8 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 c50fa1f9de98f..2eab4c58a0c6a 100644 --- a/lib/private/Files/Filesystem.php +++ b/lib/private/Files/Filesystem.php @@ -554,7 +554,7 @@ public static function touch($path, $mtime = null) { } /** - * @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 27056efef72b4..b6cd65716510b 100644 --- a/lib/private/Files/Node/File.php +++ b/lib/private/Files/Node/File.php @@ -46,14 +46,16 @@ protected function createNonExistingNode($path) { /** * @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 @@ public function getContent() { /** * @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 dd699993e841b..6633cbf41e340 100644 --- a/lib/private/Files/Storage/Wrapper/Encoding.php +++ b/lib/private/Files/Storage/Wrapper/Encoding.php @@ -300,7 +300,7 @@ public function filemtime($path) { * 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 720111793deac..9c0e6c9146396 100644 --- a/lib/private/Files/Storage/Wrapper/Encryption.php +++ b/lib/private/Files/Storage/Wrapper/Encryption.php @@ -217,7 +217,7 @@ public function getDirectoryContent($directory): \Traversable { * 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 619d8b07137c8..caf039d4cca05 100644 --- a/lib/private/Files/Storage/Wrapper/Jail.php +++ b/lib/private/Files/Storage/Wrapper/Jail.php @@ -248,7 +248,7 @@ public function filemtime($path) { * 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 28a1e0b1e4da5..9f5564b449038 100644 --- a/lib/private/Files/Storage/Wrapper/Wrapper.php +++ b/lib/private/Files/Storage/Wrapper/Wrapper.php @@ -238,7 +238,7 @@ public function filemtime($path) { * 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 91e6cc2d60e0c..5970c213210a7 100644 --- a/lib/private/Files/View.php +++ b/lib/private/Files/View.php @@ -564,7 +564,7 @@ public function touch($path, $mtime = null): bool { /** * @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 93d0ab860c1e2..7c1e36cb3ebde 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 c165266a1b72a..31405a26bfa63 100644 --- a/lib/public/Files/Storage.php +++ b/lib/public/Files/Storage.php @@ -217,7 +217,7 @@ public function filemtime($path); * 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 2f38165830bbe..00e98fdfbb6e6 100644 --- a/lib/public/Files/Storage/IStorage.php +++ b/lib/public/Files/Storage/IStorage.php @@ -214,7 +214,7 @@ public function filemtime($path); * 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);