Skip to content

Commit aaf07ab

Browse files
authored
Merge pull request #55072 from nextcloud/feature/54562/pathSpecificFSSetup
Introduces support for mount providers that can provide a partial list of mount points based on a path and the information related to mounts present in that path.
2 parents ac4e82d + d14a032 commit aaf07ab

File tree

9 files changed

+300
-51
lines changed

9 files changed

+300
-51
lines changed

build/psalm-baseline.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3641,12 +3641,6 @@
36413641
<InvalidOperand>
36423642
<code><![CDATA[$user]]></code>
36433643
</InvalidOperand>
3644-
<RedundantCondition>
3645-
<code><![CDATA[get_class($provider) !== 'OCA\Files_Sharing\MountProvider']]></code>
3646-
</RedundantCondition>
3647-
<TypeDoesNotContainType>
3648-
<code><![CDATA[get_class($provider) === 'OCA\Files_Sharing\MountProvider']]></code>
3649-
</TypeDoesNotContainType>
36503644
</file>
36513645
<file src="lib/private/Files/Config/UserMountCache.php">
36523646
<InvalidReturnType>

build/stubs/php-polyfill.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@
77
// PHP 8.4
88
function array_find(array $array, callable $callback) {}
99

10+
// PHP 8.5
11+
function array_any(array $array, callable $callback): bool {}
12+

lib/composer/composer/autoload_classmap.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,9 @@
420420
'OCP\\Files\\Config\\ICachedMountInfo' => $baseDir . '/lib/public/Files/Config/ICachedMountInfo.php',
421421
'OCP\\Files\\Config\\IHomeMountProvider' => $baseDir . '/lib/public/Files/Config/IHomeMountProvider.php',
422422
'OCP\\Files\\Config\\IMountProvider' => $baseDir . '/lib/public/Files/Config/IMountProvider.php',
423+
'OCP\\Files\\Config\\IMountProviderArgs' => $baseDir . '/lib/public/Files/Config/IMountProviderArgs.php',
423424
'OCP\\Files\\Config\\IMountProviderCollection' => $baseDir . '/lib/public/Files/Config/IMountProviderCollection.php',
425+
'OCP\\Files\\Config\\IPartialMountProvider' => $baseDir . '/lib/public/Files/Config/IPartialMountProvider.php',
424426
'OCP\\Files\\Config\\IRootMountProvider' => $baseDir . '/lib/public/Files/Config/IRootMountProvider.php',
425427
'OCP\\Files\\Config\\IUserMountCache' => $baseDir . '/lib/public/Files/Config/IUserMountCache.php',
426428
'OCP\\Files\\ConnectionLostException' => $baseDir . '/lib/public/Files/ConnectionLostException.php',

lib/composer/composer/autoload_static.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,9 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
461461
'OCP\\Files\\Config\\ICachedMountInfo' => __DIR__ . '/../../..' . '/lib/public/Files/Config/ICachedMountInfo.php',
462462
'OCP\\Files\\Config\\IHomeMountProvider' => __DIR__ . '/../../..' . '/lib/public/Files/Config/IHomeMountProvider.php',
463463
'OCP\\Files\\Config\\IMountProvider' => __DIR__ . '/../../..' . '/lib/public/Files/Config/IMountProvider.php',
464+
'OCP\\Files\\Config\\IMountProviderArgs' => __DIR__ . '/../../..' . '/lib/public/Files/Config/IMountProviderArgs.php',
464465
'OCP\\Files\\Config\\IMountProviderCollection' => __DIR__ . '/../../..' . '/lib/public/Files/Config/IMountProviderCollection.php',
466+
'OCP\\Files\\Config\\IPartialMountProvider' => __DIR__ . '/../../..' . '/lib/public/Files/Config/IPartialMountProvider.php',
465467
'OCP\\Files\\Config\\IRootMountProvider' => __DIR__ . '/../../..' . '/lib/public/Files/Config/IRootMountProvider.php',
466468
'OCP\\Files\\Config\\IUserMountCache' => __DIR__ . '/../../..' . '/lib/public/Files/Config/IUserMountCache.php',
467469
'OCP\\Files\\ConnectionLostException' => __DIR__ . '/../../..' . '/lib/public/Files/ConnectionLostException.php',

lib/private/Files/Config/MountProviderCollection.php

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,21 @@
99

1010
use OC\Hooks\Emitter;
1111
use OC\Hooks\EmitterTrait;
12+
use OCA\Files_Sharing\MountProvider;
1213
use OCP\Diagnostics\IEventLogger;
1314
use OCP\Files\Config\IHomeMountProvider;
1415
use OCP\Files\Config\IMountProvider;
16+
use OCP\Files\Config\IMountProviderArgs;
1517
use OCP\Files\Config\IMountProviderCollection;
18+
use OCP\Files\Config\IPartialMountProvider;
1619
use OCP\Files\Config\IRootMountProvider;
1720
use OCP\Files\Config\IUserMountCache;
1821
use OCP\Files\Mount\IMountManager;
1922
use OCP\Files\Mount\IMountPoint;
2023
use OCP\Files\Storage\IStorageFactory;
2124
use OCP\IUser;
25+
use function get_class;
26+
use function in_array;
2227

2328
class MountProviderCollection implements IMountProviderCollection, Emitter {
2429
use EmitterTrait;
@@ -29,7 +34,7 @@ class MountProviderCollection implements IMountProviderCollection, Emitter {
2934
private array $homeProviders = [];
3035

3136
/**
32-
* @var list<IMountProvider>
37+
* @var array<class-string<IMountProvider>, IMountProvider>
3338
*/
3439
private array $providers = [];
3540

@@ -67,28 +72,61 @@ private function getUserMountsForProviders(IUser $user, array $providers): array
6772
$mounts = array_map(function (IMountProvider $provider) use ($user, $loader) {
6873
return $this->getMountsFromProvider($provider, $user, $loader);
6974
}, $providers);
70-
$mounts = array_reduce($mounts, function (array $mounts, array $providerMounts) {
71-
return array_merge($mounts, $providerMounts);
72-
}, []);
75+
$mounts = array_merge(...$mounts);
7376
return $this->filterMounts($user, $mounts);
7477
}
7578

7679
/**
7780
* @return list<IMountPoint>
7881
*/
7982
public function getMountsForUser(IUser $user): array {
80-
return $this->getUserMountsForProviders($user, $this->providers);
83+
return $this->getUserMountsForProviders($user, array_values($this->providers));
8184
}
8285

8386
/**
87+
* @param IMountProviderArgs[] $mountProviderArgs
88+
* @return array<string, IMountPoint> IMountPoint array indexed by mount
89+
* point.
90+
*/
91+
public function getUserMountsFromProviderByPath(
92+
string $providerClass,
93+
string $path,
94+
array $mountProviderArgs,
95+
): array {
96+
$provider = $this->providers[$providerClass] ?? null;
97+
if ($provider === null) {
98+
return [];
99+
}
100+
101+
if (!is_a($providerClass, IPartialMountProvider::class, true)) {
102+
throw new \LogicException(
103+
'Mount provider does not support partial mounts'
104+
);
105+
}
106+
107+
/** @var IPartialMountProvider $provider */
108+
return $provider->getMountsForPath(
109+
$path,
110+
$mountProviderArgs,
111+
$this->loader,
112+
);
113+
}
114+
115+
/**
116+
* Returns the mounts for the user from the specified provider classes.
117+
* Providers not registered in the MountProviderCollection will be skipped.
118+
*
119+
* @inheritdoc
120+
*
84121
* @return list<IMountPoint>
85122
*/
86123
public function getUserMountsForProviderClasses(IUser $user, array $mountProviderClasses): array {
87124
$providers = array_filter(
88125
$this->providers,
89-
fn (IMountProvider $mountProvider) => (in_array(get_class($mountProvider), $mountProviderClasses))
126+
fn (string $providerClass) => in_array($providerClass, $mountProviderClasses),
127+
ARRAY_FILTER_USE_KEY
90128
);
91-
return $this->getUserMountsForProviders($user, $providers);
129+
return $this->getUserMountsForProviders($user, array_values($providers));
92130
}
93131

94132
/**
@@ -99,16 +137,21 @@ public function addMountForUser(IUser $user, IMountManager $mountManager, ?calla
99137
// to check for name collisions
100138
$firstMounts = [];
101139
if ($providerFilter) {
102-
$providers = array_filter($this->providers, $providerFilter);
140+
$providers = array_filter($this->providers, $providerFilter, ARRAY_FILTER_USE_KEY);
103141
} else {
104142
$providers = $this->providers;
105143
}
106-
$firstProviders = array_filter($providers, function (IMountProvider $provider) {
107-
return (get_class($provider) !== 'OCA\Files_Sharing\MountProvider');
108-
});
109-
$lastProviders = array_filter($providers, function (IMountProvider $provider) {
110-
return (get_class($provider) === 'OCA\Files_Sharing\MountProvider');
111-
});
144+
$firstProviders
145+
= array_filter(
146+
$providers,
147+
fn (string $providerClass) => ($providerClass !== MountProvider::class),
148+
ARRAY_FILTER_USE_KEY
149+
);
150+
$lastProviders = array_filter(
151+
$providers,
152+
fn (string $providerClass) => $providerClass === MountProvider::class,
153+
ARRAY_FILTER_USE_KEY
154+
);
112155
foreach ($firstProviders as $provider) {
113156
$mounts = $this->getMountsFromProvider($provider, $user, $this->loader);
114157
$firstMounts = array_merge($firstMounts, $mounts);
@@ -150,7 +193,7 @@ public function getHomeMountForUser(IUser $user): IMountPoint {
150193
* Add a provider for mount points
151194
*/
152195
public function registerProvider(IMountProvider $provider): void {
153-
$this->providers[] = $provider;
196+
$this->providers[get_class($provider)] = $provider;
154197

155198
$this->emit('\OC\Files\Config', 'registerMountProvider', [$provider]);
156199
}
@@ -228,7 +271,7 @@ public function clearProviders(): void {
228271
* @return list<IMountProvider>
229272
*/
230273
public function getProviders(): array {
231-
return $this->providers;
274+
return array_values($this->providers);
232275
}
233276

234277
/**

0 commit comments

Comments
 (0)