diff options
author | Vincent Petry <vincent@nextcloud.com> | 2022-03-24 23:08:15 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-24 23:08:15 +0300 |
commit | a7e778b57fb81c8e62d093ece7f0689d34db4c45 (patch) | |
tree | f50492f0380bb21d0cce095231c2560f77b00d36 /lib/private/Files/Config | |
parent | 00076c07098dbff236d133998de03bee8d4fec33 (diff) | |
parent | 91ab4e1df4f3e234c5e4941d89cf0d9f19166c18 (diff) |
Merge pull request #31265 from nextcloud/fs-limited-setup
Fine grained filesystem setup
Diffstat (limited to 'lib/private/Files/Config')
-rw-r--r-- | lib/private/Files/Config/MountProviderCollection.php | 34 | ||||
-rw-r--r-- | lib/private/Files/Config/UserMountCache.php | 42 |
2 files changed, 66 insertions, 10 deletions
diff --git a/lib/private/Files/Config/MountProviderCollection.php b/lib/private/Files/Config/MountProviderCollection.php index cd8a2a2e29f..2b0acf7d69d 100644 --- a/lib/private/Files/Config/MountProviderCollection.php +++ b/lib/private/Files/Config/MountProviderCollection.php @@ -75,16 +75,15 @@ class MountProviderCollection implements IMountProviderCollection, Emitter { } /** - * Get all configured mount points for the user - * - * @param \OCP\IUser $user - * @return \OCP\Files\Mount\IMountPoint[] + * @param IUser $user + * @param IMountProvider[] $providers + * @return IMountPoint[] */ - public function getMountsForUser(IUser $user) { + private function getUserMountsForProviders(IUser $user, array $providers): array { $loader = $this->loader; $mounts = array_map(function (IMountProvider $provider) use ($user, $loader) { return $provider->getMountsForUser($user, $loader); - }, $this->providers); + }, $providers); $mounts = array_filter($mounts, function ($result) { return is_array($result); }); @@ -94,14 +93,31 @@ class MountProviderCollection implements IMountProviderCollection, Emitter { return $this->filterMounts($user, $mounts); } - public function addMountForUser(IUser $user, IMountManager $mountManager) { + public function getMountsForUser(IUser $user): array { + return $this->getUserMountsForProviders($user, $this->providers); + } + + public function getUserMountsForProviderClass(IUser $user, string $mountProviderClass): array { + $providers = array_filter( + $this->providers, + fn (IMountProvider $mountProvider) => (get_class($mountProvider) === $mountProviderClass) + ); + return $this->getUserMountsForProviders($user, $providers); + } + + public function addMountForUser(IUser $user, IMountManager $mountManager, callable $providerFilter = null) { // shared mount provider gets to go last since it needs to know existing files // to check for name collisions $firstMounts = []; - $firstProviders = array_filter($this->providers, function (IMountProvider $provider) { + if ($providerFilter) { + $providers = array_filter($this->providers, $providerFilter); + } else { + $providers = $this->providers; + } + $firstProviders = array_filter($providers, function (IMountProvider $provider) { return (get_class($provider) !== 'OCA\Files_Sharing\MountProvider'); }); - $lastProviders = array_filter($this->providers, function (IMountProvider $provider) { + $lastProviders = array_filter($providers, function (IMountProvider $provider) { return (get_class($provider) === 'OCA\Files_Sharing\MountProvider'); }); foreach ($firstProviders as $provider) { diff --git a/lib/private/Files/Config/UserMountCache.php b/lib/private/Files/Config/UserMountCache.php index dc2640361e7..9a5eddc4878 100644 --- a/lib/private/Files/Config/UserMountCache.php +++ b/lib/private/Files/Config/UserMountCache.php @@ -89,7 +89,7 @@ class UserMountCache implements IUserMountCache { $this->mountsForUsers = new CappedMemoryCache(); } - public function registerMounts(IUser $user, array $mounts) { + public function registerMounts(IUser $user, array $mounts, array $mountProviderClasses = null) { // filter out non-proper storages coming from unit tests $mounts = array_filter($mounts, function (IMountPoint $mount) { return $mount instanceof SharedMount || $mount->getStorage() && $mount->getStorage()->getCache(); @@ -110,6 +110,11 @@ class UserMountCache implements IUserMountCache { $newMounts = array_combine($newMountRootIds, $newMounts); $cachedMounts = $this->getMountsForUser($user); + if (is_array($mountProviderClasses)) { + $cachedMounts = array_filter($cachedMounts, function (ICachedMountInfo $mountInfo) use ($mountProviderClasses) { + return in_array($mountInfo->getMountProvider(), $mountProviderClasses); + }); + } $cachedMountRootIds = array_map(function (ICachedMountInfo $mount) { return $mount->getRootId(); }, $cachedMounts); @@ -446,4 +451,39 @@ class UserMountCache implements IUserMountCache { $this->cacheInfoCache = new CappedMemoryCache(); $this->mountsForUsers = new CappedMemoryCache(); } + + public function getMountForPath(IUser $user, string $path): ICachedMountInfo { + $mounts = $this->getMountsForUser($user); + $mountPoints = array_map(function (ICachedMountInfo $mount) { + return $mount->getMountPoint(); + }, $mounts); + $mounts = array_combine($mountPoints, $mounts); + + $current = $path; + // walk up the directory tree until we find a path that has a mountpoint set + // the loop will return if a mountpoint is found or break if none are found + while (true) { + $mountPoint = $current . '/'; + if (isset($mounts[$mountPoint])) { + return $mounts[$mountPoint]; + } elseif ($current === '') { + break; + } + + $current = dirname($current); + if ($current === '.' || $current === '/') { + $current = ''; + } + } + + throw new NotFoundException("No cached mount for path " . $path); + } + + public function getMountsInPath(IUser $user, string $path): array { + $path = rtrim($path, '/') . '/'; + $mounts = $this->getMountsForUser($user); + return array_filter($mounts, function (ICachedMountInfo $mount) use ($path) { + return $mount->getMountPoint() !== $path && strpos($mount->getMountPoint(), $path) === 0; + }); + } } |