Skip to content

Commit 9fe6afe

Browse files
committed
use exceptions for error signaling in writeStream
this remove the ambiguity when writing zero length files Signed-off-by: Robin Appelman <robin@icewind.nl>
1 parent cc7cf73 commit 9fe6afe

2 files changed

Lines changed: 19 additions & 5 deletions

File tree

lib/private/Files/Storage/Common.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
use OC\Files\Storage\Wrapper\Wrapper;
5454
use OCP\Files\EmptyFileNameException;
5555
use OCP\Files\FileNameTooLongException;
56+
use OCP\Files\GenericFileException;
5657
use OCP\Files\InvalidCharacterInPathException;
5758
use OCP\Files\InvalidDirectoryException;
5859
use OCP\Files\InvalidPathException;
@@ -620,10 +621,14 @@ public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $t
620621
}
621622
} else {
622623
$source = $sourceStorage->fopen($sourceInternalPath, 'r');
624+
$result = false;
623625
if ($source) {
624-
$result = $this->writeStream($targetInternalPath, $source) > 0;
625-
} else {
626-
$result = false;
626+
try {
627+
$this->writeStream($targetInternalPath, $source);
628+
$result = true;
629+
} catch (\Exception $e) {
630+
\OC::$server->getLogger()->logException($e, ['level' => ILogger::WARN, 'message' => 'Failed to copy stream to storage']);
631+
}
627632
}
628633

629634
if ($result and $preserveMtime) {
@@ -855,10 +860,13 @@ public function needsPartFile() {
855860
public function writeStream(string $path, $stream, int $size = null): int {
856861
$target = $this->fopen($path, 'w');
857862
if (!$target) {
858-
return 0;
863+
throw new GenericFileException("Failed to open $path for writing");
859864
}
860865
try {
861866
[$count, $result] = \OC_Helper::streamCopy($stream, $target);
867+
if (!$result) {
868+
throw new GenericFileException("Failed to copy stream");
869+
}
862870
} finally {
863871
fclose($target);
864872
fclose($stream);

lib/private/Files/Storage/Local.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
use OC\Files\Storage\Wrapper\Jail;
4545
use OCP\Constants;
4646
use OCP\Files\ForbiddenException;
47+
use OCP\Files\GenericFileException;
4748
use OCP\Files\Storage\IStorage;
4849
use OCP\ILogger;
4950

@@ -553,6 +554,11 @@ public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $t
553554
}
554555

555556
public function writeStream(string $path, $stream, int $size = null): int {
556-
return (int)file_put_contents($this->getSourcePath($path), $stream);
557+
$result = file_put_contents($this->getSourcePath($path), $stream);
558+
if ($result === false) {
559+
throw new GenericFileException("Failed write steam to $path");
560+
} else {
561+
return $result;
562+
}
557563
}
558564
}

0 commit comments

Comments
 (0)