Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/private/Files/Node/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use OCP\Constants;
use OCP\Files\GenericFileException;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\Lock\LockedException;

Expand All @@ -35,7 +36,7 @@ public function getContent() {
if ($this->checkPermissions(Constants::PERMISSION_READ)) {
$content = $this->view->file_get_contents($this->path);
if ($content === false) {
throw new GenericFileException();
throw new GenericFileException('file_get_contents failed');
}
return $content;
} else {
Expand Down Expand Up @@ -66,6 +67,7 @@ public function putContent($data) {
/**
* @param string $mode
* @return resource|false
* @throws NotFoundException
* @throws NotPermittedException
* @throws LockedException
*/
Expand Down
14 changes: 6 additions & 8 deletions lib/private/Files/SimpleFS/SimpleFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,27 +54,25 @@ public function getMTime(): int {
/**
* Get the content
*
* @throws GenericFileException
* @throws LockedException
* @throws NotFoundException
* @throws NotPermittedException
*/
#[\Override]
public function getContent(): string {
$result = $this->file->getContent();

if ($result === false) {
public function getContent(): string|bool {
try {
return $this->file->getContent();
} catch (GenericFileException) {
$this->checkFile();
}

return $result;
return false;
}

/**
* Overwrite the file
*
* @param string|resource $data
* @throws GenericFileException
* @throws LockedException
* @throws NotFoundException
* @throws NotPermittedException
Expand All @@ -83,7 +81,7 @@ public function getContent(): string {
public function putContent($data): void {
try {
$this->file->putContent($data);
} catch (NotFoundException $e) {
} catch (GenericFileException) {
$this->checkFile();
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/public/Files/SimpleFS/ISimpleFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function getMTime(): int;
* @throws NotPermittedException
* @since 11.0.0
*/
public function getContent(): string;
public function getContent(): string|bool;

/**
* Overwrite the file
Expand Down
17 changes: 16 additions & 1 deletion tests/lib/Files/SimpleFS/SimpleFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use OC\Files\SimpleFS\SimpleFile;
use OCP\Files\File;
use OCP\Files\Folder;
use OCP\Files\GenericFileException;
use OCP\Files\NotFoundException;

class SimpleFileTest extends \Test\TestCase {
Expand Down Expand Up @@ -92,7 +93,7 @@ public function testGetMimeType(): void {

public function testGetContentInvalidAppData(): void {
$this->file->method('getContent')
->willReturn(false);
->willThrowException(new GenericFileException());
$this->file->method('stat')->willReturn(false);

$parent = $this->createMock(Folder::class);
Expand All @@ -109,6 +110,20 @@ public function testGetContentInvalidAppData(): void {
$this->simpleFile->getContent();
}

public function testGetContentReturnsFalseOnFailure(): void {
$this->file->expects($this->once())
->method('getContent')
->willThrowException(new GenericFileException());

// We don't want to test the `checkFile()`
// method, so, just return an empty array.
$this->file->method('stat')->willReturn([]);

$result = $this->simpleFile->getContent();

$this->assertFalse($result);
}

public function testRead(): void {
$this->file->expects($this->once())
->method('fopen')
Expand Down
Loading