Skip to content

Commit 08e1d9b

Browse files
Merge pull request #58025 from nextcloud/carl/refactor-share-mountprovider
refactor(mount-provider): Refactor share mount provider
2 parents b933156 + 8f81b91 commit 08e1d9b

5 files changed

Lines changed: 29 additions & 70 deletions

File tree

apps/files_external/lib/Config/ConfigAdapter.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,7 @@ private function getAvailableStorages(array $storageConfigs, IUser $user): array
134134
}, $storages, $storageConfigs);
135135
}
136136

137-
/**
138-
* Get all mountpoints applicable for the user
139-
*
140-
* @return IMountPoint[]
141-
*/
137+
#[Override]
142138
public function getMountsForUser(IUser $user, IStorageFactory $loader): array {
143139
$this->userStoragesService->setUser($user);
144140
$this->userGlobalStoragesService->setUser($user);

apps/files_sharing/lib/MountProvider.php

Lines changed: 20 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use InvalidArgumentException;
1212
use OC\Files\View;
1313
use OCA\Files_Sharing\Event\ShareMountedEvent;
14-
use OCP\Cache\CappedMemoryCache;
1514
use OCP\EventDispatcher\IEventDispatcher;
1615
use OCP\Files\Config\IMountProvider;
1716
use OCP\Files\Config\IPartialMountProvider;
@@ -24,34 +23,24 @@
2423
use OCP\Share\IAttributes;
2524
use OCP\Share\IManager;
2625
use OCP\Share\IShare;
26+
use Override;
2727
use Psr\Log\LoggerInterface;
2828

2929
use function count;
3030

3131
class MountProvider implements IMountProvider, IPartialMountProvider {
32-
/**
33-
* @param IConfig $config
34-
* @param IManager $shareManager
35-
* @param LoggerInterface $logger
36-
*/
3732
public function __construct(
38-
protected IConfig $config,
39-
protected IManager $shareManager,
40-
protected LoggerInterface $logger,
41-
protected IEventDispatcher $eventDispatcher,
42-
protected ICacheFactory $cacheFactory,
43-
protected IMountManager $mountManager,
33+
protected readonly IConfig $config,
34+
protected readonly IManager $shareManager,
35+
protected readonly LoggerInterface $logger,
36+
protected readonly IEventDispatcher $eventDispatcher,
37+
protected readonly ICacheFactory $cacheFactory,
38+
protected readonly IMountManager $mountManager,
4439
) {
4540
}
4641

47-
/**
48-
* Get all mountpoints applicable for the user and check for shares where we need to update the etags
49-
*
50-
* @param IUser $user
51-
* @param IStorageFactory $loader
52-
* @return IMountPoint[]
53-
*/
54-
public function getMountsForUser(IUser $user, IStorageFactory $loader) {
42+
#[Override]
43+
public function getMountsForUser(IUser $user, IStorageFactory $loader): array {
5544
return array_values($this->getMountsFromSuperShares($user, $this->getSuperSharesForUser($user), $loader));
5645
}
5746

@@ -77,8 +66,8 @@ public function getSuperSharesForUser(IUser $user): array {
7766
* Groups shares by path (nodeId) and target path
7867
*
7968
* @param iterable<IShare> $shares
80-
* @return IShare[][] array of grouped shares, each element in the
81-
* array is a group which itself is an array of shares
69+
* @return list<list<IShare>> array of grouped shares, each element in the
70+
* array is a group which itself is an array of shares
8271
*/
8372
private function groupShares(iterable $shares): array {
8473
$tmp = [];
@@ -98,9 +87,9 @@ private function groupShares(iterable $shares): array {
9887
$aTime = $a->getShareTime()->getTimestamp();
9988
$bTime = $b->getShareTime()->getTimestamp();
10089
if ($aTime === $bTime) {
101-
return $a->getId() < $b->getId() ? -1 : 1;
90+
return $a->getId() <=> $b->getId();
10291
}
103-
return $aTime < $bTime ? -1 : 1;
92+
return $aTime <=> $bTime;
10493
});
10594
$result[] = $tmp2;
10695
}
@@ -117,7 +106,6 @@ private function groupShares(iterable $shares): array {
117106
* possible.
118107
*
119108
* @param iterable<IShare> $allShares
120-
* @param IUser $user user
121109
* @return list<array{IShare, array<IShare>}> Tuple of [superShare, groupedShares]
122110
*/
123111
private function buildSuperShares(iterable $allShares, IUser $user): array {
@@ -205,8 +193,6 @@ private function mergeAttributes(
205193
* DB queries to retrieve the same information.
206194
*
207195
* @param array<IShare> $shares
208-
* @param IShare $superShare
209-
* @return void
210196
*/
211197
private function combineNotes(
212198
array &$shares,
@@ -251,11 +237,8 @@ private function adjustTarget(
251237
}
252238
}
253239
/**
254-
* @param string $userId
255240
* @param list<array{IShare, array<IShare>}> $superShares
256-
* @param IStorageFactory $loader
257-
* @param IUser $user
258-
* @return array IMountPoint indexed by mount point
241+
* @return array<string, IMountPoint> indexed by mount point
259242
* @throws Exception
260243
*/
261244
public function getMountsFromSuperShares(
@@ -264,13 +247,9 @@ public function getMountsFromSuperShares(
264247
IStorageFactory $loader,
265248
): array {
266249
$userId = $user->getUID();
267-
$allMounts = $this->mountManager->getAll();
268250
$mounts = [];
269-
$view = new View('/' . $userId . '/files');
270251
$ownerViews = [];
271252
$sharingDisabledForUser = $this->shareManager->sharingDisabledForUser($userId);
272-
/** @var CappedMemoryCache<bool> $folderExistCache */
273-
$foldersExistCache = new CappedMemoryCache();
274253

275254
$validShareCache = $this->cacheFactory->createLocal('share-valid-mountpoint-max');
276255
$maxValidatedShare = $validShareCache->get($userId) ?? 0;
@@ -293,7 +272,7 @@ public function getMountsFromSuperShares(
293272
}
294273
$shareId = (int)$parentShare->getId();
295274
$mount = new SharedMount(
296-
'\OCA\Files_Sharing\SharedStorage',
275+
SharedStorage::class,
297276
[
298277
'user' => $userId,
299278
// parent share
@@ -313,10 +292,9 @@ public function getMountsFromSuperShares(
313292
$event = new ShareMountedEvent($mount);
314293
$this->eventDispatcher->dispatchTyped($event);
315294

316-
$mounts[$mount->getMountPoint()] = $allMounts[$mount->getMountPoint()] = $mount;
295+
$mounts[$mount->getMountPoint()] = $mount;
317296
foreach ($event->getAdditionalMounts() as $additionalMount) {
318297
$mounts[$additionalMount->getMountPoint()] = $additionalMount;
319-
$allMounts[$additionalMount->getMountPoint()] = $additionalMount;
320298
}
321299
} catch (Exception $e) {
322300
$this->logger->error(
@@ -354,6 +332,7 @@ private function filterShares(iterable $shares, string $userId): iterable {
354332
}
355333
}
356334

335+
#[Override]
357336
public function getMountsForPath(
358337
string $setupPathHint,
359338
bool $forChildren,
@@ -394,8 +373,9 @@ public function getMountsForPath(
394373
}
395374

396375
/**
397-
* @param iterable ...$iterables
398-
* @return iterable
376+
* @template T
377+
* @param iterable<T> ...$iterables
378+
* @return iterable<T>
399379
*/
400380
private function mergeIterables(...$iterables): iterable {
401381
foreach ($iterables as $iterable) {

build/psalm-baseline.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1627,11 +1627,9 @@
16271627
<file src="apps/files_sharing/lib/MountProvider.php">
16281628
<InternalClass>
16291629
<code><![CDATA[new View('/' . $owner . '/files')]]></code>
1630-
<code><![CDATA[new View('/' . $userId . '/files')]]></code>
16311630
</InternalClass>
16321631
<InternalMethod>
16331632
<code><![CDATA[new View('/' . $owner . '/files')]]></code>
1634-
<code><![CDATA[new View('/' . $userId . '/files')]]></code>
16351633
</InternalMethod>
16361634
</file>
16371635
<file src="apps/files_sharing/lib/Scanner.php">

lib/private/Files/Mount/CacheMountProvider.php

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,19 @@
1111
use OCP\Files\Storage\IStorageFactory;
1212
use OCP\IConfig;
1313
use OCP\IUser;
14+
use Override;
1415

1516
/**
1617
* Mount provider for custom cache storages
1718
*/
1819
class CacheMountProvider implements IMountProvider {
19-
/**
20-
* @var IConfig
21-
*/
22-
private $config;
23-
24-
/**
25-
* ObjectStoreHomeMountProvider constructor.
26-
*
27-
* @param IConfig $config
28-
*/
29-
public function __construct(IConfig $config) {
30-
$this->config = $config;
20+
public function __construct(
21+
private readonly IConfig $config,
22+
) {
3123
}
3224

33-
/**
34-
* Get the cache mount for a user
35-
*
36-
* @param IUser $user
37-
* @param IStorageFactory $loader
38-
* @return \OCP\Files\Mount\IMountPoint[]
39-
*/
40-
public function getMountsForUser(IUser $user, IStorageFactory $loader) {
25+
#[Override]
26+
public function getMountsForUser(IUser $user, IStorageFactory $loader): array {
4127
$cacheBaseDir = $this->config->getSystemValueString('cache_path', '');
4228
if ($cacheBaseDir !== '') {
4329
$cacheDir = rtrim($cacheBaseDir, '/') . '/' . $user->getUID();

lib/public/Files/Config/IMountProvider.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88
namespace OCP\Files\Config;
99

10+
use OCP\Files\Mount\IMountPoint;
1011
use OCP\Files\Storage\IStorageFactory;
1112
use OCP\IUser;
1213

@@ -18,9 +19,7 @@ interface IMountProvider {
1819
/**
1920
* Get all mountpoints applicable for the user
2021
*
21-
* @param \OCP\IUser $user
22-
* @param \OCP\Files\Storage\IStorageFactory $loader
23-
* @return \OCP\Files\Mount\IMountPoint[]
22+
* @return list<IMountPoint>
2423
* @since 8.0.0
2524
*/
2625
public function getMountsForUser(IUser $user, IStorageFactory $loader);

0 commit comments

Comments
 (0)