diff --git a/apps/dav/lib/Connector/Sabre/Directory.php b/apps/dav/lib/Connector/Sabre/Directory.php index c29070fe921fb..49354deb778b6 100644 --- a/apps/dav/lib/Connector/Sabre/Directory.php +++ b/apps/dav/lib/Connector/Sabre/Directory.php @@ -315,20 +315,22 @@ public function delete() { } } + private function getLogger(): LoggerInterface { + return \OC::$server->get(LoggerInterface::class); + } + /** * Returns available diskspace information * * @return array */ public function getQuotaInfo() { - /** @var LoggerInterface $logger */ - $logger = \OC::$server->get(LoggerInterface::class); if ($this->quotaInfo) { return $this->quotaInfo; } $relativePath = $this->fileView->getRelativePath($this->info->getPath()); if ($relativePath === null) { - $logger->warning("error while getting quota as the relative path cannot be found"); + $this->getLogger()->warning("error while getting quota as the relative path cannot be found"); return [0, 0]; } @@ -345,13 +347,13 @@ public function getQuotaInfo() { ]; return $this->quotaInfo; } catch (\OCP\Files\NotFoundException $e) { - $logger->warning("error while getting quota into", ['exception' => $e]); + $this->getLogger()->warning("error while getting quota into", ['exception' => $e]); return [0, 0]; } catch (\OCP\Files\StorageNotAvailableException $e) { - $logger->warning("error while getting quota into", ['exception' => $e]); + $this->getLogger()->warning("error while getting quota into", ['exception' => $e]); return [0, 0]; } catch (NotPermittedException $e) { - $logger->warning("error while getting quota into", ['exception' => $e]); + $this->getLogger()->warning("error while getting quota into", ['exception' => $e]); return [0, 0]; } } diff --git a/apps/files_trashbin/lib/Storage.php b/apps/files_trashbin/lib/Storage.php index f08d8d25a55a9..09b0dcf0a1a9f 100644 --- a/apps/files_trashbin/lib/Storage.php +++ b/apps/files_trashbin/lib/Storage.php @@ -66,7 +66,7 @@ class Storage extends Wrapper { * Storage constructor. * * @param array $parameters - * @param ITrashManager $trashManager + * @param ITrashManager|null $trashManager * @param IUserManager|null $userManager * @param ILogger|null $logger * @param EventDispatcherInterface|null $eventDispatcher @@ -208,19 +208,27 @@ private function doDelete($path, $method) { } /** - * Setup the storate wrapper callback + * Setup the storage wrapper callback */ public static function setupStorage() { - \OC\Files\Filesystem::addStorageWrapper('oc_trashbin', function ($mountPoint, $storage) { - return new \OCA\Files_Trashbin\Storage( - ['storage' => $storage, 'mountPoint' => $mountPoint], - \OC::$server->query(ITrashManager::class), - \OC::$server->getUserManager(), - \OC::$server->getLogger(), - \OC::$server->getEventDispatcher(), - \OC::$server->getLazyRootFolder() - ); - }, 1); + $trashManager = \OC::$server->get(ITrashManager::class); + $userManager = \OC::$server->get(IUserManager::class); + $logger = \OC::$server->get(ILogger::class); + $eventDispatcher = \OC::$server->get(EventDispatcherInterface::class); + $rootFolder = \OC::$server->get(IRootFolder::class); + Filesystem::addStorageWrapper( + 'oc_trashbin', + function (string $mountPoint, IStorage $storage) use ($trashManager, $userManager, $logger, $eventDispatcher, $rootFolder) { + return new Storage( + ['storage' => $storage, 'mountPoint' => $mountPoint], + $trashManager, + $userManager, + $logger, + $eventDispatcher, + $rootFolder, + ); + }, + 1); } public function getMountPoint() { diff --git a/lib/private/Files/Cache/Wrapper/CacheWrapper.php b/lib/private/Files/Cache/Wrapper/CacheWrapper.php index 6479ea793b000..39a78f31343c3 100644 --- a/lib/private/Files/Cache/Wrapper/CacheWrapper.php +++ b/lib/private/Files/Cache/Wrapper/CacheWrapper.php @@ -33,8 +33,10 @@ use OC\Files\Cache\QuerySearchHelper; use OCP\Files\Cache\ICache; use OCP\Files\Cache\ICacheEntry; +use OCP\Files\IMimeTypeLoader; use OCP\Files\Search\ISearchOperator; use OCP\Files\Search\ISearchQuery; +use OCP\IDBConnection; class CacheWrapper extends Cache { /** @@ -47,9 +49,15 @@ class CacheWrapper extends Cache { */ public function __construct($cache) { $this->cache = $cache; - $this->mimetypeLoader = \OC::$server->getMimeTypeLoader(); - $this->connection = \OC::$server->getDatabaseConnection(); - $this->querySearchHelper = \OC::$server->get(QuerySearchHelper::class); + if ($cache instanceof Cache) { + $this->mimetypeLoader = $cache->mimetypeLoader; + $this->connection = $cache->connection; + $this->querySearchHelper = $cache->querySearchHelper; + } else { + $this->mimetypeLoader = \OC::$server->get(IMimeTypeLoader::class); + $this->connection = \OC::$server->get(IDBConnection::class); + $this->querySearchHelper = \OC::$server->get(QuerySearchHelper::class); + } } protected function getCache() { diff --git a/lib/private/Files/SetupManager.php b/lib/private/Files/SetupManager.php index dfc496524a895..916fcfb0a7e63 100644 --- a/lib/private/Files/SetupManager.php +++ b/lib/private/Files/SetupManager.php @@ -156,7 +156,8 @@ function ($mountPoint, IStorage $storage, IMountPoint $mount) use ($reSharingEna return $storage; }); - Filesystem::addStorageWrapper('oc_quota', function ($mountPoint, $storage) { + $quotaIncludeExternal = $this->config->getSystemValue('quota_include_external_storage', false); + Filesystem::addStorageWrapper('oc_quota', function ($mountPoint, $storage) use ($quotaIncludeExternal) { // set up quota for home storages, even for other users // which can happen when using sharing @@ -168,7 +169,7 @@ function ($mountPoint, IStorage $storage, IMountPoint $mount) use ($reSharingEna $user = $storage->getUser(); return new Quota(['storage' => $storage, 'quotaCallback' => function () use ($user) { return OC_Util::getUserQuota($user); - }, 'root' => 'files']); + }, 'root' => 'files', 'include_external_storage' => $quotaIncludeExternal]); } } diff --git a/lib/private/Files/Storage/Wrapper/Quota.php b/lib/private/Files/Storage/Wrapper/Quota.php index cfb2c455a2ce8..b13aa6f9f04e4 100644 --- a/lib/private/Files/Storage/Wrapper/Quota.php +++ b/lib/private/Files/Storage/Wrapper/Quota.php @@ -45,6 +45,7 @@ class Quota extends Wrapper { protected int|float|null $quota; protected string $sizeRoot; private SystemConfig $config; + private bool $quotaIncludeExternalStorage; /** * @param array $parameters @@ -54,7 +55,7 @@ public function __construct($parameters) { $this->quota = $parameters['quota'] ?? null; $this->quotaCallback = $parameters['quotaCallback'] ?? null; $this->sizeRoot = $parameters['root'] ?? ''; - $this->config = \OC::$server->get(SystemConfig::class); + $this->quotaIncludeExternalStorage = $parameters['include_external_storage'] ?? false; } /** @@ -82,7 +83,7 @@ private function hasQuota(): bool { * @return int|float */ protected function getSize($path, $storage = null) { - if ($this->config->getValue('quota_include_external_storage', false)) { + if ($this->quotaIncludeExternalStorage) { $rootInfo = Filesystem::getFileInfo('', 'ext'); if ($rootInfo) { return $rootInfo->getSize(true); diff --git a/lib/private/legacy/OC_Helper.php b/lib/private/legacy/OC_Helper.php index 07d81933d0009..39672dc6a7211 100644 --- a/lib/private/legacy/OC_Helper.php +++ b/lib/private/legacy/OC_Helper.php @@ -57,6 +57,8 @@ */ class OC_Helper { private static $templateManager; + private static ?ICacheFactory $cacheFactory = null; + private static ?bool $quotaIncludeExternalStorage = null; /** * Make a human file size @@ -462,12 +464,15 @@ public static function findBinaryPath(string $program): ?string { * @throws \OCP\Files\NotFoundException */ public static function getStorageInfo($path, $rootInfo = null, $includeMountPoints = true, $useCache = true) { - /** @var ICacheFactory $cacheFactory */ - $cacheFactory = \OC::$server->get(ICacheFactory::class); - $memcache = $cacheFactory->createLocal('storage_info'); + if (!self::$cacheFactory) { + self::$cacheFactory = \OC::$server->get(ICacheFactory::class); + } + $memcache = self::$cacheFactory->createLocal('storage_info'); // return storage info without adding mount points - $includeExtStorage = \OC::$server->getSystemConfig()->getValue('quota_include_external_storage', false); + if (self::$quotaIncludeExternalStorage === null) { + self::$quotaIncludeExternalStorage = \OC::$server->getSystemConfig()->getValue('quota_include_external_storage', false); + } $view = Filesystem::getView(); if (!$view) { @@ -484,7 +489,7 @@ public static function getStorageInfo($path, $rootInfo = null, $includeMountPoin } if (!$rootInfo) { - $rootInfo = \OC\Files\Filesystem::getFileInfo($path, $includeExtStorage ? 'ext' : false); + $rootInfo = \OC\Files\Filesystem::getFileInfo($path, self::$quotaIncludeExternalStorage ? 'ext' : false); } if (!$rootInfo instanceof \OCP\Files\FileInfo) { throw new \OCP\Files\NotFoundException(); @@ -498,9 +503,9 @@ public static function getStorageInfo($path, $rootInfo = null, $includeMountPoin $storage = $mount->getStorage(); $sourceStorage = $storage; if ($storage->instanceOfStorage('\OCA\Files_Sharing\SharedStorage')) { - $includeExtStorage = false; + self::$quotaIncludeExternalStorage = false; } - if ($includeExtStorage) { + if (self::$quotaIncludeExternalStorage) { if ($storage->instanceOfStorage('\OC\Files\Storage\Home') || $storage->instanceOfStorage('\OC\Files\ObjectStore\HomeObjectStoreStorage') ) { diff --git a/tests/lib/Files/ViewTest.php b/tests/lib/Files/ViewTest.php index d8c9331fa450b..7d1115470329a 100644 --- a/tests/lib/Files/ViewTest.php +++ b/tests/lib/Files/ViewTest.php @@ -23,6 +23,7 @@ use OCP\Files\GenericFileException; use OCP\Files\Mount\IMountManager; use OCP\Files\Storage\IStorage; +use OCP\IDBConnection; use OCP\Lock\ILockingProvider; use OCP\Lock\LockedException; use OCP\Share\IShare; @@ -1591,7 +1592,7 @@ private function createTestMovableMountPoints($mountPoints) { ->getMock(); $storage->method('getId')->willReturn('non-null-id'); $storage->method('getStorageCache')->willReturnCallback(function () use ($storage) { - return new \OC\Files\Cache\Storage($storage); + return new \OC\Files\Cache\Storage($storage, true, \OC::$server->get(IDBConnection::class)); }); $mounts[] = $this->getMockBuilder(TestMoveableMountPoint::class) diff --git a/tests/lib/HelperStorageTest.php b/tests/lib/HelperStorageTest.php index d3f480502b2d8..0643c4b68a49f 100644 --- a/tests/lib/HelperStorageTest.php +++ b/tests/lib/HelperStorageTest.php @@ -10,6 +10,7 @@ use OC\Files\Storage\Temporary; use OCP\Files\Mount\IMountManager; +use OCP\IConfig; use Test\Traits\UserTrait; /** @@ -26,12 +27,14 @@ class HelperStorageTest extends \Test\TestCase { private $storageMock; /** @var \OC\Files\Storage\Storage */ private $storage; + private bool $savedQuotaIncludeExternalStorage; protected function setUp(): void { parent::setUp(); $this->user = $this->getUniqueID('user_'); $this->createUser($this->user, $this->user); + $this->savedQuotaIncludeExternalStorage = $this->getIncludeExternalStorage(); \OC\Files\Filesystem::tearDown(); \OC_User::setUserId($this->user); @@ -45,6 +48,7 @@ protected function setUp(): void { } protected function tearDown(): void { + $this->setIncludeExternalStorage($this->savedQuotaIncludeExternalStorage); $this->user = null; if ($this->storageMock) { @@ -91,6 +95,19 @@ public function testGetStorageInfo() { $this->assertEquals(5, $storageInfo['used']); $this->assertEquals(17, $storageInfo['total']); } + private function getIncludeExternalStorage(): bool { + $class = new \ReflectionClass(\OC_Helper::class); + $prop = $class->getProperty('quotaIncludeExternalStorage'); + $prop->setAccessible(true); + return $prop->getValue(null) ?? false; + } + + private function setIncludeExternalStorage(bool $include) { + $class = new \ReflectionClass(\OC_Helper::class); + $prop = $class->getProperty('quotaIncludeExternalStorage'); + $prop->setAccessible(true); + $prop->setValue(null, $include); + } /** * Test getting the storage info, ignoring extra mount points @@ -104,8 +121,7 @@ public function testGetStorageInfoExcludingExtStorage() { $extStorage->file_put_contents('extfile.txt', 'abcdefghijklmnopq'); $extStorage->getScanner()->scan(''); // update root size - $config = \OC::$server->getConfig(); - $config->setSystemValue('quota_include_external_storage', false); + $this->setIncludeExternalStorage(false); \OC\Files\Filesystem::mount($extStorage, [], '/' . $this->user . '/files/ext'); @@ -129,10 +145,9 @@ public function testGetStorageInfoIncludingExtStorage() { \OC\Files\Filesystem::mount($extStorage, [], '/' . $this->user . '/files/ext'); - $config = \OC::$server->getConfig(); - $oldConfig = $config->getSystemValue('quota_include_external_storage', false); - $config->setSystemValue('quota_include_external_storage', 'true'); + $this->setIncludeExternalStorage(true); + $config = \OC::$server->get(IConfig::class); $config->setUserValue($this->user, 'files', 'quota', '25'); $storageInfo = \OC_Helper::getStorageInfo(''); @@ -140,7 +155,6 @@ public function testGetStorageInfoIncludingExtStorage() { $this->assertEquals(22, $storageInfo['used']); $this->assertEquals(25, $storageInfo['total']); - $config->setSystemValue('quota_include_external_storage', $oldConfig); $config->setUserValue($this->user, 'files', 'quota', 'default'); } @@ -161,15 +175,12 @@ public function testGetStorageInfoIncludingExtStorageWithNoUserQuota() { \OC\Files\Filesystem::mount($extStorage, [], '/' . $this->user . '/files/ext'); $config = \OC::$server->getConfig(); - $oldConfig = $config->getSystemValue('quota_include_external_storage', false); - $config->setSystemValue('quota_include_external_storage', 'true'); + $this->setIncludeExternalStorage(true); $storageInfo = \OC_Helper::getStorageInfo(''); $this->assertEquals(12, $storageInfo['free'], '12 bytes free in home storage'); $this->assertEquals(22, $storageInfo['used'], '5 bytes of home storage and 17 bytes of the temporary storage are used'); $this->assertEquals(34, $storageInfo['total'], '5 bytes used and 12 bytes free in home storage as well as 17 bytes used in temporary storage'); - - $config->setSystemValue('quota_include_external_storage', $oldConfig); }