Skip to content

Commit b91e77c

Browse files
committed
move teardown logic to SetupManager
Signed-off-by: Robin Appelman <[email protected]>
1 parent 9ee3950 commit b91e77c

32 files changed

+347
-161
lines changed

apps/dav/tests/unit/Connector/Sabre/ObjectTreeTest.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
use OC\Files\View;
3535
use OCA\DAV\Connector\Sabre\Directory;
3636
use OCA\DAV\Connector\Sabre\ObjectTree;
37+
use OCP\Files\Mount\IMountManager;
3738

3839
/**
3940
* Class ObjectTreeTest
@@ -266,7 +267,7 @@ public function nodeForPathProvider() {
266267
];
267268
}
268269

269-
270+
270271
public function testGetNodeForPathInvalidPath() {
271272
$this->expectException(\OCA\DAV\Connector\Sabre\Exception\InvalidPath::class);
272273

@@ -287,8 +288,7 @@ public function testGetNodeForPathInvalidPath() {
287288
$rootNode = $this->getMockBuilder(Directory::class)
288289
->disableOriginalConstructor()
289290
->getMock();
290-
$mountManager = $this->getMockBuilder(Manager::class)
291-
->getMock();
291+
$mountManager = $this->createMock(IMountManager::class);
292292

293293
$tree = new \OCA\DAV\Connector\Sabre\ObjectTree();
294294
$tree->init($rootNode, $view, $mountManager);
@@ -314,8 +314,7 @@ public function testGetNodeForPathRoot() {
314314
$rootNode = $this->getMockBuilder(Directory::class)
315315
->disableOriginalConstructor()
316316
->getMock();
317-
$mountManager = $this->getMockBuilder(Manager::class)
318-
->getMock();
317+
$mountManager = $this->createMock(IMountManager::class);
319318

320319
$tree = new \OCA\DAV\Connector\Sabre\ObjectTree();
321320
$tree->init($rootNode, $view, $mountManager);

apps/files_sharing/lib/External/Manager.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
use OCP\Federation\ICloudFederationFactory;
4444
use OCP\Federation\ICloudFederationProviderManager;
4545
use OCP\Files;
46+
use OCP\Files\NotFoundException;
4647
use OCP\Files\Storage\IStorageFactory;
4748
use OCP\Http\Client\IClientService;
4849
use OCP\IDBConnection;
@@ -599,8 +600,9 @@ public function setMountPoint($source, $target) {
599600
}
600601

601602
public function removeShare($mountPoint): bool {
602-
$mountPointObj = $this->mountManager->find($mountPoint);
603-
if ($mountPointObj === null) {
603+
try {
604+
$mountPointObj = $this->mountManager->find($mountPoint);
605+
} catch (NotFoundException $e) {
604606
$this->logger->error('Mount point to remove share not found', ['mountPoint' => $mountPoint]);
605607
return false;
606608
}

apps/files_sharing/tests/External/ManagerTest.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,18 @@
3131
namespace OCA\Files_Sharing\Tests\External;
3232

3333
use OC\Federation\CloudIdManager;
34+
use OC\Files\SetupManager;
3435
use OC\Files\Storage\StorageFactory;
3536
use OCA\Files_Sharing\External\Manager;
3637
use OCA\Files_Sharing\External\MountProvider;
3738
use OCA\Files_Sharing\Tests\TestCase;
3839
use OCP\Contacts\IManager;
40+
use OCP\Diagnostics\IEventLogger;
3941
use OCP\EventDispatcher\IEventDispatcher;
4042
use OCP\Federation\ICloudFederationFactory;
4143
use OCP\Federation\ICloudFederationProviderManager;
44+
use OCP\Files\Config\IMountProviderCollection;
45+
use OCP\Files\NotFoundException;
4246
use OCP\Http\Client\IClientService;
4347
use OCP\Http\Client\IResponse;
4448
use OCP\IGroup;
@@ -102,9 +106,13 @@ protected function setUp(): void {
102106
parent::setUp();
103107

104108
$this->uid = $this->getUniqueID('user');
105-
$this->createUser($this->uid, '');
106-
$this->user = \OC::$server->getUserManager()->get($this->uid);
107-
$this->mountManager = new \OC\Files\Mount\Manager();
109+
$this->user = $this->createUser($this->uid, '');
110+
$this->mountManager = new \OC\Files\Mount\Manager(
111+
$this->createMock(IEventLogger::class),
112+
$this->createMock(IMountProviderCollection::class),
113+
$this->createMock(IUserSession::class),
114+
$this->createMock(IEventDispatcher::class)
115+
);
108116
$this->clientService = $this->getMockBuilder(IClientService::class)
109117
->disableOriginalConstructor()->getMock();
110118
$this->cloudFederationProviderManager = $this->createMock(ICloudFederationProviderManager::class);
@@ -143,6 +151,10 @@ protected function setUp(): void {
143151
['group1', $group1],
144152
['group2', $group2],
145153
]));
154+
155+
/** @var SetupManager $setupManager */
156+
$setupManager = \OC::$server->get(SetupManager::class);
157+
$setupManager->setupRoot();
146158
}
147159

148160
protected function tearDown(): void {
@@ -740,12 +752,12 @@ private function assertMount($mountPoint) {
740752

741753
private function assertNotMount($mountPoint) {
742754
$mountPoint = rtrim($mountPoint, '/');
743-
$mount = $this->mountManager->find($this->getFullPath($mountPoint));
744-
if ($mount) {
755+
try {
756+
$mount = $this->mountManager->find($this->getFullPath($mountPoint));
745757
$this->assertInstanceOf('\OCP\Files\Mount\IMountPoint', $mount);
746758
$this->assertNotEquals($this->getFullPath($mountPoint), rtrim($mount->getMountPoint(), '/'));
747-
} else {
748-
$this->assertNull($mount);
759+
} catch (NotFoundException $e) {
760+
749761
}
750762
}
751763

lib/private/Files/Config/MountProviderCollection.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,4 +222,10 @@ public function getRootMounts(): array {
222222
}, []);
223223
return $mounts;
224224
}
225+
226+
public function clearProviders() {
227+
$this->providers = [];
228+
$this->homeProviders = [];
229+
$this->rootProviders = [];
230+
}
225231
}

lib/private/Files/Filesystem.php

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@
4141
use OC\Files\Config\MountProviderCollection;
4242
use OC\Files\Mount\MountPoint;
4343
use OC\Lockdown\Filesystem\NullStorage;
44+
use OCP\EventDispatcher\IEventDispatcher;
4445
use OCP\Files\Config\IMountProvider;
46+
use OCP\Files\Events\Node\FilesystemTearedDownEvent;
4547
use OCP\Files\NotFoundException;
4648
use OCP\Files\Storage\IStorageFactory;
4749
use OCP\ILogger;
@@ -332,6 +334,13 @@ public static function init($user, $root) {
332334
}
333335
self::getLoader();
334336
self::$defaultInstance = new View($root);
337+
/** @var IEventDispatcher $eventDispatcher */
338+
$eventDispatcher = \OC::$server->get(IEventDispatcher::class);
339+
$eventDispatcher->addListener(FilesystemTearedDownEvent::class, function () {
340+
self::$defaultInstance = null;
341+
self::$usersSetup = [];
342+
self::$loaded = false;
343+
});
335344

336345
if (!self::$mounts) {
337346
self::$mounts = \OC::$server->getMountManager();
@@ -474,8 +483,7 @@ public static function getView() {
474483
* tear down the filesystem, removing all storage providers
475484
*/
476485
public static function tearDown() {
477-
self::clearMounts();
478-
self::$defaultInstance = null;
486+
\OC_Util::tearDownFS();
479487
}
480488

481489
/**
@@ -492,16 +500,6 @@ public static function getRoot() {
492500
return self::$defaultInstance->getRoot();
493501
}
494502

495-
/**
496-
* clear all mounts and storage backends
497-
*/
498-
public static function clearMounts() {
499-
if (self::$mounts) {
500-
self::$usersSetup = [];
501-
self::$mounts->clear();
502-
}
503-
}
504-
505503
/**
506504
* mount an \OC\Files\Storage\Storage in our virtual filesystem
507505
*

lib/private/Files/Mount/Manager.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,13 @@
3131
use OC\Cache\CappedMemoryCache;
3232
use OC\Files\Filesystem;
3333
use OC\Files\SetupManager;
34+
use OC\Setup;
3435
use OCP\Diagnostics\IEventLogger;
36+
use OCP\EventDispatcher\IEventDispatcher;
3537
use OCP\Files\Config\IMountProviderCollection;
3638
use OCP\Files\Mount\IMountManager;
3739
use OCP\Files\Mount\IMountPoint;
40+
use OCP\Files\NotFoundException;
3841
use OCP\IUserSession;
3942

4043
class Manager implements IMountManager {
@@ -47,11 +50,12 @@ class Manager implements IMountManager {
4750
public function __construct(
4851
IEventLogger $eventLogger,
4952
IMountProviderCollection $mountProviderCollection,
50-
IUserSession $userSession
53+
IUserSession $userSession,
54+
IEventDispatcher $eventDispatcher
5155
) {
5256
$this->pathCache = new CappedMemoryCache();
5357
$this->inPathCache = new CappedMemoryCache();
54-
$this->setupManager = new SetupManager($eventLogger, $mountProviderCollection, $this, $userSession);
58+
$this->setupManager = new SetupManager($eventLogger, $mountProviderCollection, $this, $userSession, $eventDispatcher);
5559
}
5660

5761
/**
@@ -119,7 +123,7 @@ public function find(string $path): ?MountPoint {
119123
}
120124

121125
if ($current === '') {
122-
return null;
126+
throw new NotFoundException("No mount for path " . $path . " existing mounts: " . implode(",", array_keys($this->mounts)));
123127
}
124128

125129
$current = dirname($current);
@@ -211,4 +215,8 @@ private function formatPath(string $path): string {
211215
}
212216
return $path;
213217
}
218+
219+
public function getSetupManager(): SetupManager {
220+
return $this->setupManager;
221+
}
214222
}

lib/private/Files/Node/Root.php

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,17 @@
3535
use OC\Cache\CappedMemoryCache;
3636
use OC\Files\Mount\Manager;
3737
use OC\Files\Mount\MountPoint;
38+
use OC\Files\View;
3839
use OC\Hooks\PublicEmitter;
3940
use OC\User\NoUserException;
41+
use OCP\EventDispatcher\IEventDispatcher;
4042
use OCP\Files\Config\IUserMountCache;
43+
use OCP\Files\Events\Node\FilesystemTearedDownEvent;
4144
use OCP\Files\IRootFolder;
4245
use OCP\Files\NotFoundException;
4346
use OCP\Files\NotPermittedException;
4447
use OCP\ILogger;
48+
use OCP\IUser;
4549
use OCP\IUserManager;
4650

4751
/**
@@ -64,35 +68,32 @@
6468
* @package OC\Files\Node
6569
*/
6670
class Root extends Folder implements IRootFolder {
67-
/** @var Manager */
68-
private $mountManager;
69-
/** @var PublicEmitter */
70-
private $emitter;
71-
/** @var null|\OC\User\User */
72-
private $user;
73-
/** @var CappedMemoryCache */
74-
private $userFolderCache;
75-
/** @var IUserMountCache */
76-
private $userMountCache;
77-
/** @var ILogger */
78-
private $logger;
79-
/** @var IUserManager */
80-
private $userManager;
71+
private Manager $mountManager;
72+
private PublicEmitter $emitter;
73+
private ?IUser $user;
74+
private CappedMemoryCache $userFolderCache;
75+
private IUserMountCache $userMountCache;
76+
private ILogger $logger;
77+
private IUserManager $userManager;
78+
private IEventDispatcher $eventDispatcher;
8179

8280
/**
83-
* @param \OC\Files\Mount\Manager $manager
84-
* @param \OC\Files\View $view
85-
* @param \OC\User\User|null $user
81+
* @param Manager $manager
82+
* @param View $view
83+
* @param IUser|null $user
8684
* @param IUserMountCache $userMountCache
8785
* @param ILogger $logger
8886
* @param IUserManager $userManager
8987
*/
90-
public function __construct($manager,
88+
public function __construct(
89+
$manager,
9190
$view,
9291
$user,
93-
IUserMountCache $userMountCache,
94-
ILogger $logger,
95-
IUserManager $userManager) {
92+
IUserMountCache $userMountCache,
93+
ILogger $logger,
94+
IUserManager $userManager,
95+
IEventDispatcher $eventDispatcher
96+
) {
9697
parent::__construct($this, $view, '');
9798
$this->mountManager = $manager;
9899
$this->user = $user;
@@ -101,6 +102,9 @@ public function __construct($manager,
101102
$this->userMountCache = $userMountCache;
102103
$this->logger = $logger;
103104
$this->userManager = $userManager;
105+
$eventDispatcher->addListener(FilesystemTearedDownEvent::class, function () {
106+
$this->userFolderCache = new CappedMemoryCache();
107+
});
104108
}
105109

106110
/**
@@ -393,10 +397,6 @@ public function getUserFolder($userId) {
393397
return $this->userFolderCache->get($userId);
394398
}
395399

396-
public function clearCache() {
397-
$this->userFolderCache = new CappedMemoryCache();
398-
}
399-
400400
public function getUserMountCache() {
401401
return $this->userMountCache;
402402
}

0 commit comments

Comments
 (0)