Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unify/cleanup filesystem setup #31431

Merged
merged 9 commits into from
Mar 8, 2022
9 changes: 4 additions & 5 deletions apps/dav/tests/unit/Connector/Sabre/ObjectTreeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use OC\Files\View;
use OCA\DAV\Connector\Sabre\Directory;
use OCA\DAV\Connector\Sabre\ObjectTree;
use OCP\Files\Mount\IMountManager;

/**
* Class ObjectTreeTest
Expand Down Expand Up @@ -266,7 +267,7 @@ public function nodeForPathProvider() {
];
}


public function testGetNodeForPathInvalidPath() {
$this->expectException(\OCA\DAV\Connector\Sabre\Exception\InvalidPath::class);

Expand All @@ -287,8 +288,7 @@ public function testGetNodeForPathInvalidPath() {
$rootNode = $this->getMockBuilder(Directory::class)
->disableOriginalConstructor()
->getMock();
$mountManager = $this->getMockBuilder(Manager::class)
->getMock();
$mountManager = $this->createMock(IMountManager::class);

$tree = new \OCA\DAV\Connector\Sabre\ObjectTree();
$tree->init($rootNode, $view, $mountManager);
Expand All @@ -314,8 +314,7 @@ public function testGetNodeForPathRoot() {
$rootNode = $this->getMockBuilder(Directory::class)
->disableOriginalConstructor()
->getMock();
$mountManager = $this->getMockBuilder(Manager::class)
->getMock();
$mountManager = $this->createMock(IMountManager::class);

$tree = new \OCA\DAV\Connector\Sabre\ObjectTree();
$tree->init($rootNode, $view, $mountManager);
Expand Down
7 changes: 6 additions & 1 deletion apps/files_external/tests/PersonalMountTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,13 @@
namespace OCA\Files_External\Tests;

use OC\Files\Mount\Manager;
use OC\Files\SetupManagerFactory;
use OCA\Files_External\Lib\PersonalMount;
use OCA\Files_External\Lib\StorageConfig;
use OCP\Diagnostics\IEventLogger;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Config\IMountProviderCollection;
use OCP\IUserManager;
use Test\TestCase;

class PersonalMountTest extends TestCase {
Expand All @@ -47,7 +52,7 @@ public function testFindByStorageId() {

$mount = new PersonalMount($storageService, $storageConfig, 10, $storage, '/foo');

$mountManager = new Manager();
$mountManager = new Manager($this->createMock(SetupManagerFactory::class));
$mountManager->addMount($mount);

$this->assertEquals([$mount], $mountManager->findByStorageId('dummy'));
Expand Down
6 changes: 4 additions & 2 deletions apps/files_sharing/lib/External/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
use OCP\Federation\ICloudFederationFactory;
use OCP\Federation\ICloudFederationProviderManager;
use OCP\Files;
use OCP\Files\NotFoundException;
use OCP\Files\Storage\IStorageFactory;
use OCP\Http\Client\IClientService;
use OCP\IDBConnection;
Expand Down Expand Up @@ -599,8 +600,9 @@ public function setMountPoint($source, $target) {
}

public function removeShare($mountPoint): bool {
$mountPointObj = $this->mountManager->find($mountPoint);
if ($mountPointObj === null) {
try {
$mountPointObj = $this->mountManager->find($mountPoint);
} catch (NotFoundException $e) {
$this->logger->error('Mount point to remove share not found', ['mountPoint' => $mountPoint]);
return false;
}
Expand Down
2 changes: 2 additions & 0 deletions apps/files_sharing/lib/Updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
*/
namespace OCA\Files_Sharing;

use OC\Files\Mount\MountPoint;
use OCP\Constants;
use OCP\Share\IShare;

Expand Down Expand Up @@ -105,6 +106,7 @@ private static function renameChildren($oldPath, $newPath) {
$mountManager = \OC\Files\Filesystem::getMountManager();
$mountedShares = $mountManager->findIn('/' . \OC_User::getUser() . '/files/' . $oldPath);
foreach ($mountedShares as $mount) {
/** @var MountPoint $mount */
icewind1991 marked this conversation as resolved.
Show resolved Hide resolved
if ($mount->getStorage()->instanceOfStorage(ISharedStorage::class)) {
$mountPoint = $mount->getMountPoint();
$target = str_replace($absOldPath, $absNewPath, $mountPoint);
Expand Down
18 changes: 11 additions & 7 deletions apps/files_sharing/tests/External/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,19 @@
namespace OCA\Files_Sharing\Tests\External;

use OC\Federation\CloudIdManager;
use OC\Files\SetupManager;
use OC\Files\SetupManagerFactory;
use OC\Files\Storage\StorageFactory;
use OCA\Files_Sharing\External\Manager;
use OCA\Files_Sharing\External\MountProvider;
use OCA\Files_Sharing\Tests\TestCase;
use OCP\Contacts\IManager;
use OCP\Diagnostics\IEventLogger;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Federation\ICloudFederationFactory;
use OCP\Federation\ICloudFederationProviderManager;
use OCP\Files\Config\IMountProviderCollection;
use OCP\Files\NotFoundException;
use OCP\Http\Client\IClientService;
use OCP\Http\Client\IResponse;
use OCP\IGroup;
Expand Down Expand Up @@ -102,9 +107,8 @@ protected function setUp(): void {
parent::setUp();

$this->uid = $this->getUniqueID('user');
$this->createUser($this->uid, '');
$this->user = \OC::$server->getUserManager()->get($this->uid);
$this->mountManager = new \OC\Files\Mount\Manager();
$this->user = $this->createUser($this->uid, '');
$this->mountManager = new \OC\Files\Mount\Manager($this->createMock(SetupManagerFactory::class));
$this->clientService = $this->getMockBuilder(IClientService::class)
->disableOriginalConstructor()->getMock();
$this->cloudFederationProviderManager = $this->createMock(ICloudFederationProviderManager::class);
Expand Down Expand Up @@ -740,12 +744,12 @@ private function assertMount($mountPoint) {

private function assertNotMount($mountPoint) {
$mountPoint = rtrim($mountPoint, '/');
$mount = $this->mountManager->find($this->getFullPath($mountPoint));
if ($mount) {
try {
$mount = $this->mountManager->find($this->getFullPath($mountPoint));
$this->assertInstanceOf('\OCP\Files\Mount\IMountPoint', $mount);
$this->assertNotEquals($this->getFullPath($mountPoint), rtrim($mount->getMountPoint(), '/'));
} else {
$this->assertNull($mount);
} catch (NotFoundException $e) {

}
}

Expand Down
3 changes: 3 additions & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@
'OCP\\Files\\Events\\Node\\BeforeNodeRenamedEvent' => $baseDir . '/lib/public/Files/Events/Node/BeforeNodeRenamedEvent.php',
'OCP\\Files\\Events\\Node\\BeforeNodeTouchedEvent' => $baseDir . '/lib/public/Files/Events/Node/BeforeNodeTouchedEvent.php',
'OCP\\Files\\Events\\Node\\BeforeNodeWrittenEvent' => $baseDir . '/lib/public/Files/Events/Node/BeforeNodeWrittenEvent.php',
'OCP\\Files\\Events\\Node\\FilesystemTornDownEvent' => $baseDir . '/lib/public/Files/Events/Node/FilesystemTornDownEvent.php',
'OCP\\Files\\Events\\Node\\NodeCopiedEvent' => $baseDir . '/lib/public/Files/Events/Node/NodeCopiedEvent.php',
'OCP\\Files\\Events\\Node\\NodeCreatedEvent' => $baseDir . '/lib/public/Files/Events/Node/NodeCreatedEvent.php',
'OCP\\Files\\Events\\Node\\NodeDeletedEvent' => $baseDir . '/lib/public/Files/Events/Node/NodeDeletedEvent.php',
Expand Down Expand Up @@ -1162,6 +1163,8 @@
'OC\\Files\\Search\\SearchComparison' => $baseDir . '/lib/private/Files/Search/SearchComparison.php',
'OC\\Files\\Search\\SearchOrder' => $baseDir . '/lib/private/Files/Search/SearchOrder.php',
'OC\\Files\\Search\\SearchQuery' => $baseDir . '/lib/private/Files/Search/SearchQuery.php',
'OC\\Files\\SetupManager' => $baseDir . '/lib/private/Files/SetupManager.php',
'OC\\Files\\SetupManagerFactory' => $baseDir . '/lib/private/Files/SetupManagerFactory.php',
'OC\\Files\\SimpleFS\\NewSimpleFile' => $baseDir . '/lib/private/Files/SimpleFS/NewSimpleFile.php',
'OC\\Files\\SimpleFS\\SimpleFile' => $baseDir . '/lib/private/Files/SimpleFS/SimpleFile.php',
'OC\\Files\\SimpleFS\\SimpleFolder' => $baseDir . '/lib/private/Files/SimpleFS/SimpleFolder.php',
Expand Down
3 changes: 3 additions & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OCP\\Files\\Events\\Node\\BeforeNodeRenamedEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Events/Node/BeforeNodeRenamedEvent.php',
'OCP\\Files\\Events\\Node\\BeforeNodeTouchedEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Events/Node/BeforeNodeTouchedEvent.php',
'OCP\\Files\\Events\\Node\\BeforeNodeWrittenEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Events/Node/BeforeNodeWrittenEvent.php',
'OCP\\Files\\Events\\Node\\FilesystemTornDownEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Events/Node/FilesystemTornDownEvent.php',
'OCP\\Files\\Events\\Node\\NodeCopiedEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Events/Node/NodeCopiedEvent.php',
'OCP\\Files\\Events\\Node\\NodeCreatedEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Events/Node/NodeCreatedEvent.php',
'OCP\\Files\\Events\\Node\\NodeDeletedEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Events/Node/NodeDeletedEvent.php',
Expand Down Expand Up @@ -1191,6 +1192,8 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OC\\Files\\Search\\SearchComparison' => __DIR__ . '/../../..' . '/lib/private/Files/Search/SearchComparison.php',
'OC\\Files\\Search\\SearchOrder' => __DIR__ . '/../../..' . '/lib/private/Files/Search/SearchOrder.php',
'OC\\Files\\Search\\SearchQuery' => __DIR__ . '/../../..' . '/lib/private/Files/Search/SearchQuery.php',
'OC\\Files\\SetupManager' => __DIR__ . '/../../..' . '/lib/private/Files/SetupManager.php',
'OC\\Files\\SetupManagerFactory' => __DIR__ . '/../../..' . '/lib/private/Files/SetupManagerFactory.php',
'OC\\Files\\SimpleFS\\NewSimpleFile' => __DIR__ . '/../../..' . '/lib/private/Files/SimpleFS/NewSimpleFile.php',
'OC\\Files\\SimpleFS\\SimpleFile' => __DIR__ . '/../../..' . '/lib/private/Files/SimpleFS/SimpleFile.php',
'OC\\Files\\SimpleFS\\SimpleFolder' => __DIR__ . '/../../..' . '/lib/private/Files/SimpleFS/SimpleFolder.php',
Expand Down
28 changes: 24 additions & 4 deletions lib/private/Cache/CappedMemoryCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,42 @@
* In-memory cache with a capacity limit to keep memory usage in check
*
* Uses a simple FIFO expiry mechanism
* @template T
*/
class CappedMemoryCache implements ICache, \ArrayAccess {
private $capacity;
/** @var T[] */
private $cache = [];

public function __construct($capacity = 512) {
$this->capacity = $capacity;
}

public function hasKey($key) {
public function hasKey($key): bool {
return isset($this->cache[$key]);
}

/**
* @return ?T
*/
public function get($key) {
return isset($this->cache[$key]) ? $this->cache[$key] : null;
return $this->cache[$key] ?? null;
}

public function set($key, $value, $ttl = 0) {
/**
* @param string $key
* @param T $value
* @param int $ttl
* @return bool
*/
public function set($key, $value, $ttl = 0): bool {
if (is_null($key)) {
$this->cache[] = $value;
} else {
$this->cache[$key] = $value;
}
$this->garbageCollect();
return true;
}

public function remove($key) {
Expand All @@ -68,13 +80,18 @@ public function offsetExists($offset): bool {
}

/**
* @return mixed
* @return T
*/
#[\ReturnTypeWillChange]
public function &offsetGet($offset) {
return $this->cache[$offset];
}

/**
* @param string $key
* @param T $value
* @return void
*/
public function offsetSet($offset, $value): void {
$this->set($offset, $value);
}
Expand All @@ -83,6 +100,9 @@ public function offsetUnset($offset): void {
$this->remove($offset);
}

/**
* @return T[]
*/
public function getData() {
return $this->cache;
}
Expand Down
3 changes: 1 addition & 2 deletions lib/private/Files/Config/CachedMountFileInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
use OCP\IUser;

class CachedMountFileInfo extends CachedMountInfo implements ICachedMountFileInfo {
/** @var string */
private $internalPath;
private string $internalPath;

public function __construct(
IUser $user,
Expand Down
55 changes: 15 additions & 40 deletions lib/private/Files/Config/CachedMountInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,38 +28,13 @@
use OCP\IUser;

class CachedMountInfo implements ICachedMountInfo {
/**
* @var IUser
*/
protected $user;

/**
* @var int
*/
protected $storageId;

/**
* @var int
*/
protected $rootId;

/**
* @var string
*/
protected $mountPoint;

/**
* @var int|null
*/
protected $mountId;

/**
* @var string
*/
protected $rootInternalPath;

/** @var string */
protected $mountProvider;
protected IUser $user;
protected int $storageId;
protected int $rootId;
protected string $mountPoint;
protected ?int $mountId;
protected string $rootInternalPath;
protected string $mountProvider;

/**
* CachedMountInfo constructor.
Expand Down Expand Up @@ -95,28 +70,28 @@ public function __construct(
/**
* @return IUser
*/
public function getUser() {
public function getUser(): IUser {
return $this->user;
}

/**
* @return int the numeric storage id of the mount
*/
public function getStorageId() {
public function getStorageId(): int {
return $this->storageId;
}

/**
* @return int the fileid of the root of the mount
*/
public function getRootId() {
public function getRootId(): int {
return $this->rootId;
}

/**
* @return Node the root node of the mount
* @return Node|null the root node of the mount
*/
public function getMountPointNode() {
public function getMountPointNode(): ?Node {
// TODO injection etc
Filesystem::initMountPoints($this->getUser()->getUID());
$userNode = \OC::$server->getUserFolder($this->getUser()->getUID());
Expand All @@ -131,7 +106,7 @@ public function getMountPointNode() {
/**
* @return string the mount point of the mount for the user
*/
public function getMountPoint() {
public function getMountPoint(): string {
return $this->mountPoint;
}

Expand All @@ -141,7 +116,7 @@ public function getMountPoint() {
* @return int|null mount id or null if not applicable
* @since 9.1.0
*/
public function getMountId() {
public function getMountId(): ?int {
return $this->mountId;
}

Expand All @@ -150,7 +125,7 @@ public function getMountId() {
*
* @return string
*/
public function getRootInternalPath() {
public function getRootInternalPath(): string {
return $this->rootInternalPath;
}

Expand Down
Loading