Skip to content

Commit b26da95

Browse files
committed
refactor: Call IStorageFactory::addStorageFactory directly
Remove call to deprecated Filesystem api and allow to reuse instead of the mounts points between addStorageFactory. Signed-off-by: Carl Schwan <carlschwan@kde.org>
1 parent d6cf529 commit b26da95

7 files changed

Lines changed: 37 additions & 22 deletions

File tree

lib/private/Files/Mount/Manager.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,6 @@ public function findByStorageId(string $id): array {
207207
return $result;
208208
}
209209

210-
/**
211-
* @return IMountPoint[]
212-
*/
213210
#[\Override]
214211
public function getAll(): array {
215212
return $this->mounts;

lib/private/Files/SetupManager.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
use OCP\Files\Mount\IMountPoint;
5050
use OCP\Files\NotFoundException;
5151
use OCP\Files\Storage\IStorage;
52+
use OCP\Files\Storage\IStorageFactory;
5253
use OCP\Group\Events\UserAddedEvent;
5354
use OCP\Group\Events\UserRemovedEvent;
5455
use OCP\HintException;
@@ -60,6 +61,7 @@
6061
use OCP\IUserManager;
6162
use OCP\IUserSession;
6263
use OCP\Lockdown\ILockdownManager;
64+
use OCP\Server;
6365
use OCP\Share\Events\ShareCreatedEvent;
6466
use Override;
6567
use Psr\Log\LoggerInterface;
@@ -115,6 +117,7 @@ public function __construct(
115117
private IAppManager $appManager,
116118
private FileAccess $fileAccess,
117119
private IAppConfig $appConfig,
120+
private IStorageFactory $storageFactory,
118121
) {
119122
$this->cache = $cacheFactory->createDistributed('setupmanager::');
120123
$this->listeningForProviders = false;
@@ -299,7 +302,7 @@ public function setupForUser(IUser $user): void {
299302
/**
300303
* Part of the user setup that is run only once per user.
301304
*/
302-
private function oneTimeUserSetup(IUser $user) {
305+
private function oneTimeUserSetup(IUser $user): void {
303306
if ($this->isSetupStarted($user)) {
304307
return;
305308
}
@@ -311,20 +314,22 @@ private function oneTimeUserSetup(IUser $user) {
311314

312315
$this->setupBuiltinWrappers();
313316

314-
$prevLogging = Filesystem::logWarningWhenAddingStorageWrapper(false);
315-
316317
// TODO remove hook
318+
$prevLogging = Filesystem::logWarningWhenAddingStorageWrapper(false);
317319
OC_Hook::emit('OC_Filesystem', 'preSetup', ['user' => $user->getUID()]);
320+
Filesystem::logWarningWhenAddingStorageWrapper($prevLogging);
318321

319322
$event = new BeforeFileSystemSetupEvent($user);
320323
$this->eventDispatcher->dispatchTyped($event);
321324

322-
foreach ($event->getStorageWrappers() as $name => $wrapper) {
323-
Filesystem::addStorageWrapper($name, $wrapper['wrapper'], $wrapper['priority']);
325+
$storageWrappers = $event->getStorageWrappers();
326+
if ($storageWrappers !== []) {
327+
$mounts = $this->mountManager->getAll();
328+
foreach ($storageWrappers as $wrapperName => $wrapper) {
329+
$this->storageFactory->addStorageWrapper($wrapperName, $wrapper['callable'], $wrapper['priority'], $mounts);
330+
}
324331
}
325332

326-
Filesystem::logWarningWhenAddingStorageWrapper($prevLogging);
327-
328333
$userDir = '/' . $user->getUID() . '/files';
329334

330335
Filesystem::initInternal($userDir);
@@ -776,7 +781,7 @@ private function listenForNewMountProviders() {
776781
}
777782
}
778783

779-
private function setupListeners() {
784+
private function setupListeners(): void {
780785
// note that this event handling is intentionally pessimistic
781786
// clearing the cache to often is better than not enough
782787

lib/private/Files/SetupManagerFactory.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use OCP\Files\Config\IMountProviderCollection;
1717
use OCP\Files\Config\IUserMountCache;
1818
use OCP\Files\Mount\IMountManager;
19+
use OCP\Files\Storage\IStorageFactory;
1920
use OCP\IAppConfig;
2021
use OCP\ICacheFactory;
2122
use OCP\IConfig;
@@ -42,6 +43,7 @@ public function __construct(
4243
private IAppManager $appManager,
4344
private FileAccess $fileAccess,
4445
private IAppConfig $appConfig,
46+
private IStorageFactory $storageFactory,
4547
) {
4648
$this->setupManager = null;
4749
}
@@ -64,6 +66,7 @@ public function create(IMountManager $mountManager): SetupManager {
6466
$this->appManager,
6567
$this->fileAccess,
6668
$this->appConfig,
69+
$this->storageFactory,
6770
);
6871
}
6972
return $this->setupManager;

lib/private/Files/Storage/StorageFactory.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@
1616
use Psr\Log\LoggerInterface;
1717

1818
class StorageFactory implements IStorageFactory {
19-
/**
20-
* @var array[] [$name=>['priority'=>$priority, 'wrapper'=>$callable] $storageWrappers
21-
*/
22-
private $storageWrappers = [];
19+
/** @var array<string, array{wrapper: callable(string $mountPoint, IStorage $storage): IStorage, priority: int}> $storageWrappers */
20+
private array $storageWrappers = [];
2321

2422
#[\Override]
2523
public function addStorageWrapper(string $wrapperName, callable $callback, int $priority = 50, array $existingMounts = []): bool {

lib/public/Files/Events/BeforeFileSystemSetupEvent.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* @since 31.0.0
2020
*/
2121
class BeforeFileSystemSetupEvent extends Event {
22-
/** @var array<string, array{wrapper: callable(string $mountPoint, IStorage $storage): IStorage, priority: int}> $storageWrappers */
22+
/** @var array<class-string<IStorage>, array{callable: callable(string $mountPoint, IStorage $storage): IStorage, priority: int}> $storageWrappers */
2323
private array $storageWrappers = [];
2424

2525
/**
@@ -42,18 +42,20 @@ public function getUser(): IUser {
4242
* Add a storage wrapper to the file system. This allows apps to inject storage wrappers
4343
* for every mount.
4444
*
45+
* @param class-string<IStorage> $name The identifier of the wrapper.
4546
* @param callable(string $mountPoint, IStorage $storage): IStorage $wrapper
47+
* @param int<0, 100> $priority
4648
* @since 35.0.0
4749
*/
4850
public function addStorageWrapper(string $name, callable $wrapper, int $priority = 50): void {
49-
$this->storageWrappers[$name] = ['wrapper' => $wrapper, 'priority' => $priority];
51+
$this->storageWrappers[$name] = ['callable' => $wrapper, 'priority' => $priority];
5052
}
5153

5254
/**
5355
* Get the storage wrappers.
5456
*
57+
* @return array<class-string<IStorage>, array{callable: callable(string $mountPoint, IStorage $storage): IStorage, priority: int}>
5558
* @since 35.0.0
56-
* @return array<string, array{wrapper: callable(string $mountPoint, IStorage $storage): IStorage, priority: int}>
5759
*/
5860
public function getStorageWrappers(): array {
5961
return $this->storageWrappers;

lib/public/Files/Storage/IStorageFactory.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,36 @@
88

99
namespace OCP\Files\Storage;
1010

11+
use OCP\AppFramework\Attribute\Consumable;
12+
use OCP\Files\Events\BeforeFileSystemSetupEvent;
1113
use OCP\Files\Mount\IMountPoint;
1214

1315
/**
1416
* Creates storage instances and manages and applies storage wrappers
1517
* @since 8.0.0
1618
*/
19+
#[Consumable(since: '8.0.0')]
1720
interface IStorageFactory {
1821
/**
19-
* allow modifier storage behaviour by adding wrappers around storages
22+
* Allow modifier storage behaviour by adding wrappers around storages.
2023
*
21-
* $callback should be a function of type (string $mountPoint, Storage $storage) => Storage
24+
* The BeforeFileSystemSetupEvent should be used in most cases instead to add storage wrappers.
25+
*
26+
* @param non-empty-string $wrapperName
27+
* @param callable(string $mountPoint, IStorage $storage): IStorage $callback
28+
* @param int<0, 100> $priority
29+
* @param IMountPoint[] $existingMounts
2230
*
2331
* @return bool true if the wrapper was added, false if there was already a wrapper with this
2432
* name registered
2533
* @since 8.0.0
34+
* @see BeforeFileSystemSetupEvent
2635
*/
27-
public function addStorageWrapper(string $wrapperName, callable $callback);
36+
public function addStorageWrapper(string $wrapperName, callable $callback, int $priority = 50, array $existingMounts = []): bool;
2837

2938
/**
3039
* @return IStorage
3140
* @since 8.0.0
3241
*/
33-
public function getInstance(IMountPoint $mountPoint, string $class, array $arguments);
42+
public function getInstance(IMountPoint $mountPoint, string $class, array $arguments): IStorage;
3443
}

tests/lib/Files/SetupManagerTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ protected function setUp(): void {
114114
$appManager,
115115
$this->fileAccess,
116116
$this->createMock(IAppConfig::class),
117+
$this->createMock(IStorageFactory::class),
117118
);
118119
}
119120

0 commit comments

Comments
 (0)