]> source.dussan.org Git - nextcloud-server.git/commitdiff
add logic to perform a full filesystem setup when needed
authorRobin Appelman <robin@icewind.nl>
Thu, 10 Mar 2022 17:27:54 +0000 (18:27 +0100)
committerRobin Appelman <robin@icewind.nl>
Thu, 24 Mar 2022 16:03:10 +0000 (17:03 +0100)
Signed-off-by: Robin Appelman <robin@icewind.nl>
lib/composer/composer/autoload_classmap.php
lib/composer/composer/autoload_static.php
lib/private/Files/SetupManager.php
lib/private/Files/SetupManagerFactory.php
lib/public/Files/Events/InvalidateMountCacheEvent.php [new file with mode: 0644]

index 2ce5d448518b1023a65859677522caa99ebaaa29..ad5e8047f6b5f5f80149320ddd2303a0ca632347 100644 (file)
@@ -260,6 +260,7 @@ return array(
     'OCP\\Files\\Events\\FileCacheUpdated' => $baseDir . '/lib/public/Files/Events/FileCacheUpdated.php',
     'OCP\\Files\\Events\\FileScannedEvent' => $baseDir . '/lib/public/Files/Events/FileScannedEvent.php',
     'OCP\\Files\\Events\\FolderScannedEvent' => $baseDir . '/lib/public/Files/Events/FolderScannedEvent.php',
+    'OCP\\Files\\Events\\InvalidateMountCacheEvent' => $baseDir . '/lib/public/Files/Events/InvalidateMountCacheEvent.php',
     'OCP\\Files\\Events\\NodeAddedToCache' => $baseDir . '/lib/public/Files/Events/NodeAddedToCache.php',
     'OCP\\Files\\Events\\NodeRemovedFromCache' => $baseDir . '/lib/public/Files/Events/NodeRemovedFromCache.php',
     'OCP\\Files\\Events\\Node\\AbstractNodeEvent' => $baseDir . '/lib/public/Files/Events/Node/AbstractNodeEvent.php',
@@ -1140,6 +1141,7 @@ return array(
     'OC\\Files\\Node\\HookConnector' => $baseDir . '/lib/private/Files/Node/HookConnector.php',
     'OC\\Files\\Node\\LazyFolder' => $baseDir . '/lib/private/Files/Node/LazyFolder.php',
     'OC\\Files\\Node\\LazyRoot' => $baseDir . '/lib/private/Files/Node/LazyRoot.php',
+    'OC\\Files\\Node\\LazyUserFolder' => $baseDir . '/lib/private/Files/Node/LazyUserFolder.php',
     'OC\\Files\\Node\\Node' => $baseDir . '/lib/private/Files/Node/Node.php',
     'OC\\Files\\Node\\NonExistingFile' => $baseDir . '/lib/private/Files/Node/NonExistingFile.php',
     'OC\\Files\\Node\\NonExistingFolder' => $baseDir . '/lib/private/Files/Node/NonExistingFolder.php',
index 4c35c1d1f85afd60cfe2d152749970c17617701b..e7288503f633c05f4f64a9408c08329ba7957827 100644 (file)
@@ -289,6 +289,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
         'OCP\\Files\\Events\\FileCacheUpdated' => __DIR__ . '/../../..' . '/lib/public/Files/Events/FileCacheUpdated.php',
         'OCP\\Files\\Events\\FileScannedEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Events/FileScannedEvent.php',
         'OCP\\Files\\Events\\FolderScannedEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Events/FolderScannedEvent.php',
+        'OCP\\Files\\Events\\InvalidateMountCacheEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Events/InvalidateMountCacheEvent.php',
         'OCP\\Files\\Events\\NodeAddedToCache' => __DIR__ . '/../../..' . '/lib/public/Files/Events/NodeAddedToCache.php',
         'OCP\\Files\\Events\\NodeRemovedFromCache' => __DIR__ . '/../../..' . '/lib/public/Files/Events/NodeRemovedFromCache.php',
         'OCP\\Files\\Events\\Node\\AbstractNodeEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Events/Node/AbstractNodeEvent.php',
@@ -1169,6 +1170,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
         'OC\\Files\\Node\\HookConnector' => __DIR__ . '/../../..' . '/lib/private/Files/Node/HookConnector.php',
         'OC\\Files\\Node\\LazyFolder' => __DIR__ . '/../../..' . '/lib/private/Files/Node/LazyFolder.php',
         'OC\\Files\\Node\\LazyRoot' => __DIR__ . '/../../..' . '/lib/private/Files/Node/LazyRoot.php',
+        'OC\\Files\\Node\\LazyUserFolder' => __DIR__ . '/../../..' . '/lib/private/Files/Node/LazyUserFolder.php',
         'OC\\Files\\Node\\Node' => __DIR__ . '/../../..' . '/lib/private/Files/Node/Node.php',
         'OC\\Files\\Node\\NonExistingFile' => __DIR__ . '/../../..' . '/lib/private/Files/Node/NonExistingFile.php',
         'OC\\Files\\Node\\NonExistingFolder' => __DIR__ . '/../../..' . '/lib/private/Files/Node/NonExistingFolder.php',
index ace408ecb49f8f66344a54361898041c70b2ef2e..7e091da0e01459ca6e3f18ee28b2c00be4476c18 100644 (file)
@@ -42,15 +42,21 @@ use OCP\Diagnostics\IEventLogger;
 use OCP\EventDispatcher\IEventDispatcher;
 use OCP\Files\Config\IMountProvider;
 use OCP\Files\Config\IUserMountCache;
+use OCP\Files\Events\InvalidateMountCacheEvent;
 use OCP\Files\Events\Node\FilesystemTornDownEvent;
 use OCP\Files\Mount\IMountManager;
 use OCP\Files\Mount\IMountPoint;
 use OCP\Files\NotFoundException;
 use OCP\Files\Storage\IStorage;
+use OCP\Group\Events\UserAddedEvent;
+use OCP\Group\Events\UserRemovedEvent;
+use OCP\ICache;
+use OCP\ICacheFactory;
 use OCP\IUser;
 use OCP\IUserManager;
 use OCP\IUserSession;
 use OCP\Lockdown\ILockdownManager;
+use OCP\Share\Events\ShareCreatedEvent;
 
 class SetupManager {
        private bool $rootSetup = false;
@@ -68,6 +74,7 @@ class SetupManager {
        private IUserMountCache $userMountCache;
        private ILockdownManager $lockdownManager;
        private IUserSession $userSession;
+       private ICache $cache;
        private bool $listeningForProviders;
 
        public function __construct(
@@ -78,7 +85,8 @@ class SetupManager {
                IEventDispatcher $eventDispatcher,
                IUserMountCache $userMountCache,
                ILockdownManager $lockdownManager,
-               IUserSession $userSession
+               IUserSession $userSession,
+               ICacheFactory $cacheFactory
        ) {
                $this->eventLogger = $eventLogger;
                $this->mountProviderCollection = $mountProviderCollection;
@@ -88,7 +96,25 @@ class SetupManager {
                $this->userMountCache = $userMountCache;
                $this->lockdownManager = $lockdownManager;
                $this->userSession = $userSession;
+               $this->cache = $cacheFactory->createDistributed('setupmanager::');
                $this->listeningForProviders = false;
+
+               $this->eventDispatcher->addListener(UserAddedEvent::class, function(UserAddedEvent $event) {
+                       $this->cache->remove($event->getUser()->getUID());
+               });
+               $this->eventDispatcher->addListener(UserRemovedEvent::class, function(UserRemovedEvent $event) {
+                       $this->cache->remove($event->getUser()->getUID());
+               });
+               $eventDispatcher->addListener(ShareCreatedEvent::class, function(ShareCreatedEvent $event) {
+                       $this->cache->remove($event->getShare()->getSharedWith());
+               });
+               $eventDispatcher->addListener(InvalidateMountCacheEvent::class, function(InvalidateMountCacheEvent $event) {
+                       if ($user = $event->getUser()) {
+                               $this->cache->remove($user->getUID());
+                       } else {
+                               $this->cache->clear();
+                       }
+               });
        }
 
        private function isSetupStarted(IUser $user): bool {
@@ -319,6 +345,16 @@ class SetupManager {
                        return;
                }
 
+               // we perform a "cached" setup only after having done the full setup recently
+               // this is also used to trigger a full setup after handling events that are likely
+               // to change the available mounts
+               $cachedSetup = $this->cache->get($user->getUID());
+               if (!$cachedSetup) {
+                       $this->setupForUser($user);
+                       $this->cache->set($user->getUID(), true, 5 * 60);
+                       return;
+               }
+
                if (!isset($this->setupUserMountProviders[$user->getUID()])) {
                        $this->setupUserMountProviders[$user->getUID()] = [];
                }
index 56e70d09961c7347ce87987b984f5da8b7197af4..122f82c02e925ad7a9b68f081b09f514912e1bd5 100644 (file)
@@ -28,6 +28,7 @@ use OCP\EventDispatcher\IEventDispatcher;
 use OCP\Files\Config\IMountProviderCollection;
 use OCP\Files\Config\IUserMountCache;
 use OCP\Files\Mount\IMountManager;
+use OCP\ICacheFactory;
 use OCP\IUserManager;
 use OCP\IUserSession;
 use OCP\Lockdown\ILockdownManager;
@@ -41,6 +42,7 @@ class SetupManagerFactory {
        private ILockdownManager $lockdownManager;
        private IUserSession $userSession;
        private ?SetupManager $setupManager;
+       private ICacheFactory $cacheFactory;
 
        public function __construct(
                IEventLogger $eventLogger,
@@ -49,7 +51,8 @@ class SetupManagerFactory {
                IEventDispatcher $eventDispatcher,
                IUserMountCache $userMountCache,
                ILockdownManager $lockdownManager,
-               IUserSession $userSession
+               IUserSession $userSession,
+               ICacheFactory $cacheFactory
        ) {
                $this->eventLogger = $eventLogger;
                $this->mountProviderCollection = $mountProviderCollection;
@@ -58,6 +61,7 @@ class SetupManagerFactory {
                $this->userMountCache = $userMountCache;
                $this->lockdownManager = $lockdownManager;
                $this->userSession = $userSession;
+               $this->cacheFactory = $cacheFactory;
                $this->setupManager = null;
        }
 
@@ -72,6 +76,7 @@ class SetupManagerFactory {
                                $this->userMountCache,
                                $this->lockdownManager,
                                $this->userSession,
+                               $this->cacheFactory
                        );
                }
                return $this->setupManager;
diff --git a/lib/public/Files/Events/InvalidateMountCacheEvent.php b/lib/public/Files/Events/InvalidateMountCacheEvent.php
new file mode 100644 (file)
index 0000000..ea8a9ea
--- /dev/null
@@ -0,0 +1,45 @@
+<?php
+
+declare(strict_types=1);
+/**
+ * @copyright Copyright (c) 2022 Robin Appelman <robin@icewind.nl>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace OCP\Files\Events;
+
+use OCP\EventDispatcher\Event;
+use OCP\IUser;
+
+/**
+ * Used to notify the filesystem setup manager that the available mounts for a user have changed
+ *
+ * @since 24.0.0
+ */
+class InvalidateMountCacheEvent extends Event {
+       private ?IUser $user;
+
+       public function __construct(?IUser $user) {
+               parent::__construct();
+               $this->user = $user;
+       }
+
+       public function getUser(): ?IUser {
+               return $this->user;
+       }
+}