]> source.dussan.org Git - nextcloud-server.git/commitdiff
use exceptions for error signaling in writeStream
authorRobin Appelman <robin@icewind.nl>
Wed, 1 Jul 2020 13:37:47 +0000 (15:37 +0200)
committerRobin Appelman <robin@icewind.nl>
Thu, 23 Jul 2020 13:24:52 +0000 (15:24 +0200)
this remove the ambiguity when writing zero length files

Signed-off-by: Robin Appelman <robin@icewind.nl>
lib/private/Files/Storage/Common.php
lib/private/Files/Storage/Local.php
tests/lib/Files/ViewTest.php

index ada037768bd09b838f0fd5c033aacf7e1cd26166..958d09832c4b0235d2006d91c5ff4c19dfde7866 100644 (file)
@@ -53,6 +53,7 @@ use OC\Files\Storage\Wrapper\Jail;
 use OC\Files\Storage\Wrapper\Wrapper;
 use OCP\Files\EmptyFileNameException;
 use OCP\Files\FileNameTooLongException;
+use OCP\Files\GenericFileException;
 use OCP\Files\InvalidCharacterInPathException;
 use OCP\Files\InvalidDirectoryException;
 use OCP\Files\InvalidPathException;
@@ -620,10 +621,14 @@ abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage {
                        }
                } else {
                        $source = $sourceStorage->fopen($sourceInternalPath, 'r');
+                       $result = false;
                        if ($source) {
-                               $result = $this->writeStream($targetInternalPath, $source) > 0;
-                       } else {
-                               $result = false;
+                               try {
+                                       $this->writeStream($targetInternalPath, $source);
+                                       $result = true;
+                               } catch (\Exception $e) {
+                                       \OC::$server->getLogger()->logException($e, ['level' => ILogger::WARN, 'message' => 'Failed to copy stream to storage']);
+                               }
                        }
 
                        if ($result and $preserveMtime) {
@@ -855,10 +860,13 @@ abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage {
        public function writeStream(string $path, $stream, int $size = null): int {
                $target = $this->fopen($path, 'w');
                if (!$target) {
-                       return 0;
+                       throw new GenericFileException("Failed to open $path for writing");
                }
                try {
                        [$count, $result] = \OC_Helper::streamCopy($stream, $target);
+                       if (!$result) {
+                               throw new GenericFileException("Failed to copy stream");
+                       }
                } finally {
                        fclose($target);
                        fclose($stream);
index 4cf3ac4799fa50a43508b68b0dfe194754d1a986..0b636d06bde3d874036e729aa46522e1d46a593a 100644 (file)
@@ -44,6 +44,7 @@ use OC\Files\Filesystem;
 use OC\Files\Storage\Wrapper\Jail;
 use OCP\Constants;
 use OCP\Files\ForbiddenException;
+use OCP\Files\GenericFileException;
 use OCP\Files\Storage\IStorage;
 use OCP\ILogger;
 
@@ -553,6 +554,11 @@ class Local extends \OC\Files\Storage\Common {
        }
 
        public function writeStream(string $path, $stream, int $size = null): int {
-               return (int)file_put_contents($this->getSourcePath($path), $stream);
+               $result = file_put_contents($this->getSourcePath($path), $stream);
+               if ($result === false) {
+                       throw new GenericFileException("Failed write steam to $path");
+               } else {
+                       return $result;
+               }
        }
 }
index ae6c4b22dec565ffa99c1a30f919c0ca68f02e4c..fad2217bfdbed55de632e7a2d32fca12257b26ae 100644 (file)
@@ -13,7 +13,6 @@ use OC\Files\Filesystem;
 use OC\Files\Mount\MountPoint;
 use OC\Files\Storage\Common;
 use OC\Files\Storage\Temporary;
-use OC\Files\Stream\Quota;
 use OC\Files\View;
 use OCP\Constants;
 use OCP\Files\Config\IMountProvider;