Skip to content

Commit

Permalink
Merge pull request #28907 from nextcloud/bug/noid/close-stream-local
Browse files Browse the repository at this point in the history
explicitly close source stream on local / encryption storage
  • Loading branch information
kesselb authored Oct 8, 2021
2 parents 8d2caa0 + be3f4ed commit 9187e98
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
5 changes: 4 additions & 1 deletion lib/private/Files/Storage/Local.php
Original file line number Diff line number Diff line change
Expand Up @@ -568,8 +568,11 @@ public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $t

public function writeStream(string $path, $stream, int $size = null): int {
$result = $this->file_put_contents($path, $stream);
if (is_resource($stream)) {
fclose($stream);
}
if ($result === false) {
throw new GenericFileException("Failed write steam to $path");
throw new GenericFileException("Failed write stream to $path");
} else {
return $result;
}
Expand Down
5 changes: 3 additions & 2 deletions lib/private/Files/Storage/Wrapper/Encryption.php
Original file line number Diff line number Diff line change
Expand Up @@ -926,10 +926,10 @@ protected function getHeader($path) {
}

$result = [];

// first check if it is an encrypted file at all
// We would do query to filecache only if we know that entry in filecache exists

$info = $this->getCache()->get($path);
if (isset($info['encrypted']) && $info['encrypted'] === true) {
$firstBlock = $this->readFirstBlock($path);
Expand Down Expand Up @@ -1033,6 +1033,7 @@ public function writeStream(string $path, $stream, int $size = null): int {
// always fall back to fopen
$target = $this->fopen($path, 'w');
[$count, $result] = \OC_Helper::streamCopy($stream, $target);
fclose($stream);
fclose($target);
return $count;
}
Expand Down
3 changes: 2 additions & 1 deletion tests/lib/Files/Storage/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,7 @@ public function testWriteStream() {

$storage->writeStream('test.txt', $source);
$this->assertTrue($storage->file_exists('test.txt'));
$this->assertEquals(file_get_contents($textFile), $storage->file_get_contents('test.txt'));
$this->assertStringEqualsFile($textFile, $storage->file_get_contents('test.txt'));
$this->assertEquals('resource (closed)', gettype($source));
}
}

0 comments on commit 9187e98

Please sign in to comment.