Skip to content

Commit df9d3c9

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 df9d3c9

7 files changed

Lines changed: 55 additions & 41 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: 31 additions & 27 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;
@@ -115,6 +116,7 @@ public function __construct(
115116
private IAppManager $appManager,
116117
private FileAccess $fileAccess,
117118
private IAppConfig $appConfig,
119+
private IStorageFactory $storageFactory,
118120
) {
119121
$this->cache = $cacheFactory->createDistributed('setupmanager::');
120122
$this->listeningForProviders = false;
@@ -154,28 +156,30 @@ private function isPathSetup(string $path): bool {
154156
return false;
155157
}
156158

157-
private function setupBuiltinWrappers(): void {
159+
/**
160+
* @param IMountPoint[] $existingMounts
161+
*/
162+
private function setupBuiltinWrappers(array $existingMounts): void {
158163
if ($this->setupBuiltinWrappersDone) {
159164
return;
160165
}
161166
$this->setupBuiltinWrappersDone = true;
162167

163168
// load all filesystem apps before, so no setup-hook gets lost
164169
$this->appManager->loadApps(['filesystem']);
165-
$prevLogging = Filesystem::logWarningWhenAddingStorageWrapper(false);
166170

167-
Filesystem::addStorageWrapper('mount_options', function ($mountPoint, IStorage $storage, IMountPoint $mount) {
171+
$this->storageFactory->addStorageWrapper('mount_options', function (string $mountPoint, IStorage $storage, IMountPoint $mount): IStorage {
168172
if ($storage->instanceOfStorage(Common::class)) {
169173
$options = array_merge($mount->getOptions(), ['mount_point' => $mountPoint]);
170174
$storage->setMountOptions($options);
171175
}
172176
return $storage;
173-
});
177+
}, 50, $existingMounts);
174178

175179
$reSharingEnabled = $this->appConfig->getValueBool('core', 'shareapi_allow_resharing', true);
176180
$user = $this->userSession->getUser();
177181
$sharingEnabledForUser = $user ? !$this->shareDisableChecker->sharingDisabledForUser($user->getUID()) : true;
178-
Filesystem::addStorageWrapper(
182+
$this->storageFactory->addStorageWrapper(
179183
'sharing_mask',
180184
function ($mountPoint, IStorage $storage, IMountPoint $mount) use ($reSharingEnabled, $sharingEnabledForUser) {
181185
$sharingEnabledForMount = $mount->getOption('enable_sharing', true);
@@ -187,27 +191,26 @@ function ($mountPoint, IStorage $storage, IMountPoint $mount) use ($reSharingEna
187191
]);
188192
}
189193
return $storage;
190-
}
191-
);
194+
}, 50, $existingMounts);
192195

193196
// install storage availability wrapper, before most other wrappers
194-
Filesystem::addStorageWrapper('oc_availability', function ($mountPoint, IStorage $storage, IMountPoint $mount) {
197+
$this->storageFactory->addStorageWrapper(Availability::class, function (string $mountPoint, IStorage $storage, IMountPoint $mount) {
195198
$externalMount = $mount instanceof ExternalMountPoint || $mount instanceof Mount;
196199
if ($externalMount && !$storage->isLocal()) {
197200
return new Availability(['storage' => $storage]);
198201
}
199202
return $storage;
200-
});
203+
}, 50, $existingMounts);
201204

202-
Filesystem::addStorageWrapper('oc_encoding', function ($mountPoint, IStorage $storage, IMountPoint $mount) {
205+
$this->storageFactory->addStorageWrapper(Encoding::class, function (string $mountPoint, IStorage $storage, IMountPoint $mount) {
203206
if ($mount->getOption('encoding_compatibility', false) && !$mount instanceof SharedMount) {
204207
return new Encoding(['storage' => $storage]);
205208
}
206209
return $storage;
207-
});
210+
}, 50, $existingMounts);
208211

209212
$quotaIncludeExternal = $this->config->getSystemValue('quota_include_external_storage', false);
210-
Filesystem::addStorageWrapper('oc_quota', function ($mountPoint, $storage, IMountPoint $mount) use ($quotaIncludeExternal) {
213+
$this->storageFactory->addStorageWrapper(Quota::class, function ($mountPoint, $storage, IMountPoint $mount) use ($quotaIncludeExternal) {
211214
// set up quota for home storages, even for other users
212215
// which can happen when using sharing
213216
if ($mount instanceof HomeMountPoint) {
@@ -218,9 +221,9 @@ function ($mountPoint, IStorage $storage, IMountPoint $mount) use ($reSharingEna
218221
}
219222

220223
return $storage;
221-
});
224+
}, 50, $existingMounts);
222225

223-
Filesystem::addStorageWrapper('readonly', function ($mountPoint, IStorage $storage, IMountPoint $mount) {
226+
$this->storageFactory->addStorageWrapper('readonly', function (string $mountPoint, IStorage $storage, IMountPoint $mount) {
224227
/*
225228
* Do not allow any operations that modify the storage
226229
*/
@@ -235,9 +238,7 @@ function ($mountPoint, IStorage $storage, IMountPoint $mount) use ($reSharingEna
235238
]);
236239
}
237240
return $storage;
238-
});
239-
240-
Filesystem::logWarningWhenAddingStorageWrapper($prevLogging);
241+
}, 50, $existingMounts);
241242
}
242243

243244
/**
@@ -299,7 +300,7 @@ public function setupForUser(IUser $user): void {
299300
/**
300301
* Part of the user setup that is run only once per user.
301302
*/
302-
private function oneTimeUserSetup(IUser $user) {
303+
private function oneTimeUserSetup(IUser $user): void {
303304
if ($this->isSetupStarted($user)) {
304305
return;
305306
}
@@ -309,22 +310,24 @@ private function oneTimeUserSetup(IUser $user) {
309310

310311
$this->eventLogger->start('fs:setup:user:onetime', 'Onetime filesystem for user');
311312

312-
$this->setupBuiltinWrappers();
313-
314-
$prevLogging = Filesystem::logWarningWhenAddingStorageWrapper(false);
313+
$mounts = $this->mountManager->getAll();
314+
$this->setupBuiltinWrappers($mounts);
315315

316316
// TODO remove hook
317+
$prevLogging = Filesystem::logWarningWhenAddingStorageWrapper(false);
317318
OC_Hook::emit('OC_Filesystem', 'preSetup', ['user' => $user->getUID()]);
319+
Filesystem::logWarningWhenAddingStorageWrapper($prevLogging);
318320

319321
$event = new BeforeFileSystemSetupEvent($user);
320322
$this->eventDispatcher->dispatchTyped($event);
321323

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

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

330333
Filesystem::initInternal($userDir);
@@ -430,7 +433,8 @@ public function setupRoot(): void {
430433
return;
431434
}
432435

433-
$this->setupBuiltinWrappers();
436+
$mounts = $this->mountManager->getAll();
437+
$this->setupBuiltinWrappers($mounts);
434438

435439
$this->rootSetup = true;
436440

@@ -776,7 +780,7 @@ private function listenForNewMountProviders() {
776780
}
777781
}
778782

779-
private function setupListeners() {
783+
private function setupListeners(): void {
780784
// note that this event handling is intentionally pessimistic
781785
// clearing the cache to often is better than not enough
782786

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, IMountPoint $mountPoint): 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)