Skip to content

Commit

Permalink
move logic to decide what to setup to setupmanager
Browse files Browse the repository at this point in the history
Signed-off-by: Robin Appelman <robin@icewind.nl>
  • Loading branch information
icewind1991 committed Mar 2, 2022
1 parent 34f3b66 commit f6106b3
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 35 deletions.
24 changes: 6 additions & 18 deletions lib/private/Files/Mount/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,13 @@
use OC\Cache\CappedMemoryCache;
use OC\Files\Filesystem;
use OC\Files\SetupManager;
use OC\Setup;
use OCP\Diagnostics\IEventLogger;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Config\IMountProviderCollection;
use OCP\Files\Mount\IMountManager;
use OCP\Files\Mount\IMountPoint;
use OCP\Files\NotFoundException;
use OCP\IUserSession;
use OCP\IUserManager;

class Manager implements IMountManager {
/** @var MountPoint[] */
Expand All @@ -50,12 +49,12 @@ class Manager implements IMountManager {
public function __construct(
IEventLogger $eventLogger,
IMountProviderCollection $mountProviderCollection,
IUserSession $userSession,
IUserManager $userManager,
IEventDispatcher $eventDispatcher
) {
$this->pathCache = new CappedMemoryCache();
$this->inPathCache = new CappedMemoryCache();
$this->setupManager = new SetupManager($eventLogger, $mountProviderCollection, $this, $userSession, $eventDispatcher);
$this->setupManager = new SetupManager($eventLogger, $mountProviderCollection, $this, $userManager, $eventDispatcher);
}

/**
Expand Down Expand Up @@ -91,26 +90,14 @@ public function moveMount(string $mountPoint, string $target) {
$this->inPathCache->clear();
}

private function setupForFind(string $path) {
if (strpos($path, '/appdata_' . \OC_Util::getInstanceId()) === 0) {
// for appdata, we only setup the root bits, not the user bits
$this->setupManager->setupRoot();
} elseif (strpos($path, '/files_external/uploads/') === 0) {
// for OC\Security\CertificateManager, we only setup the root bits, not the user bits
$this->setupManager->setupRoot();
} else {
$this->setupManager->setupForCurrentUser();
}
}

/**
* Find the mount for $path
*
* @param string $path
* @return MountPoint|null
*/
public function find(string $path): ?MountPoint {
$this->setupForFind($path);
$this->setupManager->setupForPath($path);
$path = Filesystem::normalizePath($path);

if (isset($this->pathCache[$path])) {
Expand Down Expand Up @@ -143,7 +130,8 @@ public function find(string $path): ?MountPoint {
* @return MountPoint[]
*/
public function findIn(string $path): array {
$this->setupForFind($path);

$this->setupManager->setupForPath($path);
$path = $this->formatPath($path);

if (isset($this->inPathCache[$path])) {
Expand Down
48 changes: 33 additions & 15 deletions lib/private/Files/SetupManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,28 @@
use OCP\Files\Mount\IMountPoint;
use OCP\Files\Storage\IStorage;
use OCP\IUser;
use OCP\IUserSession;
use OCP\IUserManager;

class SetupManager {
private bool $rootSetup = false;
private IEventLogger $eventLogger;
private IMountProviderCollection $mountProviderCollection;
private IMountManager $mountManager;
private IUserSession $userSession;
private IUserManager $userManager;
private array $setupUsers = [];
private IEventDispatcher $eventDispatcher;

public function __construct(
IEventLogger $eventLogger,
IMountProviderCollection $mountProviderCollection,
IMountManager $mountManager,
IUserSession $userSession,
IUserManager $userManager,
IEventDispatcher $eventDispatcher
) {
$this->eventLogger = $eventLogger;
$this->mountProviderCollection = $mountProviderCollection;
$this->mountManager = $mountManager;
$this->userSession = $userSession;
$this->userManager = $userManager;
$this->eventDispatcher = $eventDispatcher;
}

Expand Down Expand Up @@ -138,16 +138,10 @@ private function setupBuiltinWrappers() {
});
}

public function setupForCurrentUser() {
$user = $this->userSession->getUser();
if ($user) {
$this->setupForUser($user);
} else {
$this->setupRoot();
}
}

public function setupForUser(IUser $user) {
/**
* Setup the full filesystem for the specified user
*/
public function setupForUser(IUser $user): void {
$this->setupRoot();

if (in_array($user->getUID(), $this->setupUsers)) {
Expand All @@ -172,7 +166,10 @@ public function setupForUser(IUser $user) {
$this->eventLogger->end('setup_fs');
}

public function setupRoot() {
/**
* Set up the root filesystem
*/
public function setupRoot(): void {
//setting up the filesystem twice can only lead to trouble
if ($this->rootSetup) {
return;
Expand All @@ -197,6 +194,27 @@ public function setupRoot() {
$this->eventLogger->end('setup_root_fs');
}

/**
* Set up the filesystem for the specified path
*/
public function setupForPath(string $path): void {
if (substr_count($path, '/') < 2 || strpos($path, '/appdata_' . \OC_Util::getInstanceId()) === 0 || strpos($path, '/files_external/') === 0) {
$this->setupRoot();
return;
} else {
[, $userId] = explode('/', $path);
}

$user = $this->userManager->get($userId);

if (!$user) {
$this->setupRoot();
return;
}

$this->setupForUser($user);
}

public function tearDown() {
$this->setupUsers = [];
$this->rootSetup = false;
Expand Down
4 changes: 2 additions & 2 deletions tests/lib/Files/Mount/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use OCP\Diagnostics\IEventLogger;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Config\IMountProviderCollection;
use OCP\IUserSession;
use OCP\IUserManager;

class LongId extends Temporary {
public function getId() {
Expand All @@ -31,7 +31,7 @@ protected function setUp(): void {
$this->manager = new \OC\Files\Mount\Manager(
$this->createMock(IEventLogger::class),
$this->createMock(IMountProviderCollection::class),
$this->createMock(IUserSession::class),
$this->createMock(IUserManager::class),
$this->createMock(IEventDispatcher::class),
);
}
Expand Down

0 comments on commit f6106b3

Please sign in to comment.