Skip to content

Commit

Permalink
Allow ext storage Local to go unavailable
Browse files Browse the repository at this point in the history
Whenever an external storage of type Local points at a non-existing
directory, process this as a StorageNotAvailable instead of returning
404.

This makes desktop clients ignore the folder instead of deleting it when
it becomes unavailable.

The code change was limited to external storages to avoid issues during
setup and with the default home storage.
Signed-off-by: Vincent Petry <pvince81@yahoo.fr>
  • Loading branch information
PVince81 committed Aug 3, 2023
1 parent 9a7e2b1 commit 521aeb4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
6 changes: 6 additions & 0 deletions apps/files_external/lib/Lib/Backend/Local.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
use OCA\Files_External\Lib\Auth\AuthMechanism;
use OCA\Files_External\Lib\Auth\NullMechanism;
use OCA\Files_External\Lib\DefinitionParameter;
use OCA\Files_External\Lib\StorageConfig;
use OCA\Files_External\Service\BackendService;
use OCP\IL10N;
use OCP\IUser;

class Local extends Backend {
public function __construct(IL10N $l, NullMechanism $legacyAuth) {
Expand All @@ -45,4 +47,8 @@ public function __construct(IL10N $l, NullMechanism $legacyAuth) {
->setLegacyAuthMechanism($legacyAuth)
;
}

public function manipulateStorageConfig(StorageConfig &$storage, IUser $user = null) {

Check notice

Code scanning / Psalm

MissingReturnType Note

Method OCA\Files_External\Lib\Backend\Local::manipulateStorageConfig does not have a return type, expecting void
$storage->setBackendOption('isExternal', true);
}
}
7 changes: 7 additions & 0 deletions lib/private/Files/Storage/Local.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
use OCP\Files\GenericFileException;
use OCP\Files\IMimeTypeDetector;
use OCP\Files\Storage\IStorage;
use OCP\Files\StorageNotAvailableException;
use OCP\IConfig;
use OCP\Util;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -95,6 +96,12 @@ public function __construct($arguments) {

// support Write-Once-Read-Many file systems
$this->unlinkOnTruncate = $this->config->getSystemValueBool('localstorage.unlink_on_truncate', false);

if (isset($arguments['isExternal']) && $arguments['isExternal'] && !$this->stat('')) {
// data dir not accessible or available, can happen when using an external storage of type Local
// on an unmounted system mount point
throw new StorageNotAvailableException('Local storage path does not exist "' . $this->getSourcePath('') . '"');
}
}

public function __destruct() {
Expand Down
11 changes: 11 additions & 0 deletions tests/lib/Files/Storage/LocalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,15 @@ public function testWriteUmaskCopy() {
umask($oldMask);
$this->assertTrue($this->instance->isUpdatable('test.txt'));
}

public function testUnavailableExternal() {
$this->expectException(\OCP\Files\StorageNotAvailableException::class);
$this->instance = new \OC\Files\Storage\Local(['datadir' => $this->tmpDir . '/unexist', 'isExternal' => true]);
}

public function testUnavailableNonExternal() {
$this->instance = new \OC\Files\Storage\Local(['datadir' => $this->tmpDir . '/unexist']);
// no exception thrown
$this->assertNotNull($this->instance);
}
}

0 comments on commit 521aeb4

Please sign in to comment.