Skip to content

Commit ec69522

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 ec69522

11 files changed

Lines changed: 91 additions & 51 deletions

File tree

apps/dav/appinfo/v1/publicwebdav.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use OCP\EventDispatcher\IEventDispatcher;
2222
use OCP\Files\IRootFolder;
2323
use OCP\Files\Mount\IMountManager;
24+
use OCP\Files\Storage\IStorage;
2425
use OCP\IConfig;
2526
use OCP\IDBConnection;
2627
use OCP\IPreview;
@@ -109,14 +110,14 @@ function (\Sabre\DAV\Server $server) use (
109110

110111
// FIXME: should not add storage wrappers outside of preSetup, need to find a better way
111112
$previousLog = Filesystem::logWarningWhenAddingStorageWrapper(false);
112-
Filesystem::addStorageWrapper('sharePermissions', function ($mountPoint, $storage) use ($share) {
113+
Filesystem::addStorageWrapper('sharePermissions', function (string $mountPoint, IStorage $storage) use ($share) {
113114
return new DirPermissionsMask([
114115
'storage' => $storage,
115116
'mask' => $share->getPermissions() | Constants::PERMISSION_SHARE,
116117
'path' => 'files'
117118
]);
118119
});
119-
Filesystem::addStorageWrapper('shareOwner', function ($mountPoint, $storage) use ($share) {
120+
Filesystem::addStorageWrapper('shareOwner', function (string $mountPoint, IStorage $storage) use ($share) {
120121
return new PublicOwnerWrapper(['storage' => $storage, 'owner' => $share->getShareOwner()]);
121122
});
122123
Filesystem::logWarningWhenAddingStorageWrapper($previousLog);

apps/dav/appinfo/v2/publicremote.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use OCP\Files\IHomeStorage;
2727
use OCP\Files\IRootFolder;
2828
use OCP\Files\Mount\IMountManager;
29+
use OCP\Files\Storage\IStorage;
2930
use OCP\ICacheFactory;
3031
use OCP\IConfig;
3132
use OCP\IDBConnection;
@@ -120,7 +121,7 @@
120121
$previousLog = Filesystem::logWarningWhenAddingStorageWrapper(false);
121122

122123
/** @psalm-suppress MissingClosureParamType */
123-
Filesystem::addStorageWrapper('sharePermissions', function ($mountPoint, $storage) use ($requestUri, $baseuri, $share) {
124+
Filesystem::addStorageWrapper('sharePermissions', function (string $mountPoint, IStorage $storage) use ($requestUri, $baseuri, $share) {
124125
$mask = $share->getPermissions() | Constants::PERMISSION_SHARE;
125126

126127
// For chunked uploads it is necessary to have read and delete permission,
@@ -141,13 +142,13 @@
141142
});
142143

143144
/** @psalm-suppress MissingClosureParamType */
144-
Filesystem::addStorageWrapper('shareOwner', function ($mountPoint, $storage) use ($share) {
145+
Filesystem::addStorageWrapper('shareOwner', function (string $mountPoint, IStorage $storage) use ($share) {
145146
return new PublicOwnerWrapper(['storage' => $storage, 'owner' => $share->getShareOwner()]);
146147
});
147148

148149
// Ensure that also private shares have the `getShare` method
149150
/** @psalm-suppress MissingClosureParamType */
150-
Filesystem::addStorageWrapper('getShare', function ($mountPoint, $storage) use ($share) {
151+
Filesystem::addStorageWrapper('getShare', function (string $mountPoint, IStorage $storage) use ($share) {
151152
return new PublicShareWrapper(['storage' => $storage, 'share' => $share]);
152153
}, 0);
153154

apps/files_trashbin/lib/Listener/EventListener.php

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,33 @@
1010
namespace OCA\Files_Trashbin\Listener;
1111

1212
use OCA\Files_Trashbin\Storage;
13+
use OCA\Files_Trashbin\Trash\ITrashManager;
1314
use OCA\Files_Trashbin\Trashbin;
1415
use OCP\EventDispatcher\Event;
16+
use OCP\EventDispatcher\IEventDispatcher;
1517
use OCP\EventDispatcher\IEventListener;
1618
use OCP\Files\Events\BeforeFileSystemSetupEvent;
1719
use OCP\Files\Events\Node\NodeWrittenEvent;
1820
use OCP\Files\Folder;
1921
use OCP\Files\IRootFolder;
2022
use OCP\Files\NotFoundException;
2123
use OCP\Files\NotPermittedException;
24+
use OCP\Files\Storage\IStorage;
25+
use OCP\IRequest;
2226
use OCP\IUserManager;
2327
use OCP\User\Events\BeforeUserDeletedEvent;
28+
use Psr\Log\LoggerInterface;
2429

2530
/** @template-implements IEventListener<NodeWrittenEvent|BeforeUserDeletedEvent|BeforeFileSystemSetupEvent> */
2631
class EventListener implements IEventListener {
2732
public function __construct(
28-
private IUserManager $userManager,
29-
private IRootFolder $rootFolder,
30-
private ?string $userId = null,
33+
private readonly IUserManager $userManager,
34+
private readonly IRootFolder $rootFolder,
35+
private readonly IRequest $request,
36+
private readonly IEventDispatcher $eventDispatcher,
37+
private readonly LoggerInterface $logger,
38+
private readonly ITrashManager $trashManager,
39+
private readonly ?string $userId = null,
3140
) {
3241
}
3342

@@ -58,6 +67,21 @@ public function handle(Event $event): void {
5867

5968
if ($event instanceof BeforeFileSystemSetupEvent) {
6069
Storage::setupStorage();
70+
71+
$event->addStorageWrapper(
72+
Storage::class,
73+
function (string $mountPoint, IStorage $storage): Storage {
74+
return new Storage(
75+
['storage' => $storage, 'mountPoint' => $mountPoint],
76+
$this->trashManager,
77+
$this->userManager,
78+
$this->logger,
79+
$this->eventDispatcher,
80+
$this->rootFolder,
81+
$this->request,
82+
);
83+
},
84+
1);
6185
}
6286
}
6387
}

lib/private/Encryption/Manager.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,11 @@ public function getDefaultEncryptionModuleId() {
203203
/**
204204
* Add storage wrapper
205205
*/
206-
public function setupStorage() {
206+
public function setupStorage(): void {
207207
// If encryption is disabled and there are no loaded modules it makes no sense to load the wrapper
208208
if (!empty($this->encryptionModules) || $this->isEnabled()) {
209209
$encryptionWrapper = new EncryptionWrapper($this->arrayCache, $this, $this->logger);
210-
Filesystem::addStorageWrapper('oc_encryption', [$encryptionWrapper, 'wrapStorage'], 2);
210+
Filesystem::addStorageWrapper('oc_encryption', $encryptionWrapper->wrapStorage(...), 2);
211211
}
212212
}
213213

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()]);
318319

319320
$event = new BeforeFileSystemSetupEvent($user);
320321
$this->eventDispatcher->dispatchTyped($event);
322+
Filesystem::logWarningWhenAddingStorageWrapper($prevLogging);
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<0, 100>}> $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<0, 100>}>
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;

0 commit comments

Comments
 (0)