diff --git a/lib/private/Files/SetupManager.php b/lib/private/Files/SetupManager.php index b92c608a81d25..eb800914062bb 100644 --- a/lib/private/Files/SetupManager.php +++ b/lib/private/Files/SetupManager.php @@ -158,7 +158,7 @@ function ($mountPoint, IStorage $storage, IMountPoint $mount) use ($reSharingEna $user = $mount->getUser(); return new Quota(['storage' => $storage, 'quotaCallback' => function () use ($user) { return $user->getQuotaBytes(); - }, 'root' => 'files', 'include_external_storage' => $quotaIncludeExternal]); + }, 'root' => 'files', 'include_external_storage' => $quotaIncludeExternal, 'user' => $user]); } return $storage; diff --git a/lib/private/Files/Storage/Wrapper/Quota.php b/lib/private/Files/Storage/Wrapper/Quota.php index ef9483e1564ac..9dee927227949 100644 --- a/lib/private/Files/Storage/Wrapper/Quota.php +++ b/lib/private/Files/Storage/Wrapper/Quota.php @@ -8,12 +8,14 @@ namespace OC\Files\Storage\Wrapper; use OC\Files\Filesystem; +use OC\Files\View; use OC\SystemConfig; use OCP\Files\Cache\ICacheEntry; use OCP\Files\FileInfo; use OCP\Files\GenericFileException; use OCP\Files\NotEnoughSpaceException; use OCP\Files\Storage\IStorage; +use OCP\IUser; class Quota extends Wrapper { /** @var callable|null */ @@ -24,6 +26,8 @@ class Quota extends Wrapper { private SystemConfig $config; private bool $quotaIncludeExternalStorage; private bool $enabled = true; + /** The user the wrapped home storage belongs to, if known */ + private ?IUser $user; /** * @param array $parameters @@ -34,6 +38,7 @@ public function __construct(array $parameters) { $this->quotaCallback = $parameters['quotaCallback'] ?? null; $this->sizeRoot = $parameters['root'] ?? ''; $this->quotaIncludeExternalStorage = $parameters['include_external_storage'] ?? false; + $this->user = $parameters['user'] ?? null; } public function getQuota(): int|float { @@ -55,13 +60,28 @@ private function hasQuota(): bool { return $this->getQuota() !== FileInfo::SPACE_UNLIMITED; } + /** + * Usage of the quota user's files including the external storages mounted in them. + * It is read through that user's view, since the session user differs when writing into their shares. + * Storages using the quota user's session credentials count as empty in other sessions. + */ + private function getSizeIncludingExternalStorage(): int|float { + if ($this->user === null) { + $rootInfo = Filesystem::getFileInfo('', 'ext'); + } else { + $view = new View('/' . $this->user->getUID() . '/files'); + $rootInfo = $view->getFileInfo('', 'ext'); + } + + if ($rootInfo) { + return $rootInfo->getSize(true); + } + return FileInfo::SPACE_NOT_COMPUTED; + } + protected function getSize(string $path, ?IStorage $storage = null): int|float { if ($this->quotaIncludeExternalStorage) { - $rootInfo = Filesystem::getFileInfo('', 'ext'); - if ($rootInfo) { - return $rootInfo->getSize(true); - } - return FileInfo::SPACE_NOT_COMPUTED; + return $this->getSizeIncludingExternalStorage(); } else { $cache = is_null($storage) ? $this->getCache() : $storage->getCache(); $data = $cache->get($path); diff --git a/lib/private/legacy/OC_Helper.php b/lib/private/legacy/OC_Helper.php index 4388f7756233b..0de372fd1a2d5 100644 --- a/lib/private/legacy/OC_Helper.php +++ b/lib/private/legacy/OC_Helper.php @@ -8,10 +8,16 @@ use bantu\IniGetWrapper\IniGetWrapper; use OC\Files\FilenameValidator; use OC\Files\Filesystem; +use OC\Files\Storage\Home; +use OC\Files\Storage\Wrapper\Quota; +use OC\Files\View; +use OCP\Files\FileInfo; use OCP\Files\Mount\IMountPoint; +use OCP\Files\NotFoundException; use OCP\IBinaryFinder; use OCP\ICacheFactory; use OCP\IUser; +use OCP\IUserManager; use OCP\Server; use OCP\Util; use Psr\Log\LoggerInterface; @@ -270,7 +276,7 @@ public static function getStorageInfo($path, $rootInfo = null, $includeMountPoin /** @var \OC\Files\Storage\Home $storage */ $user = $storage->getUser(); } else { - $user = \OC::$server->getUserSession()->getUser(); + $user = self::getUserOfPath($rootInfo->getPath()) ?? \OC::$server->getUserSession()->getUser(); } $quota = $user?->getQuotaBytes() ?? \OCP\Files\FileInfo::SPACE_UNKNOWN; if ($quota !== \OCP\Files\FileInfo::SPACE_UNLIMITED) { @@ -356,14 +362,32 @@ public static function getStorageInfo($path, $rootInfo = null, $includeMountPoin return $info; } + /** + * The user whose tree an absolute path is in, e.g. the user `alice` for `/alice/files/...` + */ + private static function getUserOfPath(string $path): ?IUser { + $userId = explode('/', ltrim($path, '/'))[0]; + if ($userId === '') { + return null; + } + return Server::get(IUserManager::class)->get($userId); + } + /** * Get storage info including all mount points and quota * * @psalm-suppress LessSpecificReturnStatement Legacy code outputs weird types - manually validated that they are correct * @return StorageInfo + * @throws NotFoundException */ private static function getGlobalStorageInfo(int|float $quota, IUser $user, IMountPoint $mount): array { - $rootInfo = \OC\Files\Filesystem::getFileInfo('', 'ext'); + // the usage has to be read from the home of the user the quota belongs to, + // which is not necessarily the user of the current session + $view = new View('/' . $user->getUID() . '/files'); + $rootInfo = $view->getFileInfo('', 'ext'); + if (!$rootInfo instanceof FileInfo) { + throw new NotFoundException('The root directory of the user\'s files is missing'); + } /** @var int|float $used */ $used = $rootInfo['size']; if ($used < 0) { diff --git a/tests/lib/Files/QuotaIncludeExternalStorageTest.php b/tests/lib/Files/QuotaIncludeExternalStorageTest.php new file mode 100644 index 0000000000000..463d58d5d8bea --- /dev/null +++ b/tests/lib/Files/QuotaIncludeExternalStorageTest.php @@ -0,0 +1,323 @@ +getUniqueID('quota_owner_'); + $otherUid = $this->getUniqueID('quota_other_'); + $this->createUser($ownerUid, $ownerUid); + $this->createUser($otherUid, $otherUid); + + $userManager = Server::get(IUserManager::class); + $this->owner = $userManager->get($ownerUid); + $this->otherUser = $userManager->get($otherUid); + + $this->previousIncludeExternalStorage = Server::get(IConfig::class)->getSystemValue('quota_include_external_storage', null); + + Filesystem::tearDown(); + \OC_User::setUserId($otherUid); + Filesystem::init($otherUid, '/' . $otherUid . '/files'); + + // Complete the setup for both users so that the lazy setup triggered + // while looking up mounts keeps the mounts installed by the tests. + $setupManager = Server::get(SetupManager::class); + $setupManager->setupForUser($this->owner); + $setupManager->setupForUser($this->otherUser); + + $mountManager = Server::get(IMountManager::class); + $mountManager->removeMount('/' . $ownerUid); + $mountManager->removeMount('/' . $otherUid); + } + + #[\Override] + protected function tearDown(): void { + $config = Server::get(IConfig::class); + if ($this->previousIncludeExternalStorage === null) { + $config->deleteSystemValue('quota_include_external_storage'); + } else { + $config->setSystemValue('quota_include_external_storage', $this->previousIncludeExternalStorage); + } + + Filesystem::tearDown(); + \OC_User::setUserId(''); + + parent::tearDown(); + } + + /** + * Mounts a scanned temporary storage holding a single file of $size bytes. + */ + private function mountStorageWithUsage(string $mountPoint, int $size): Temporary { + $storage = new Temporary([]); + $storage->file_put_contents('usage.bin', str_repeat('x', $size)); + $storage->getScanner()->scan(''); + Filesystem::mount($storage, [], $mountPoint); + return $storage; + } + + private function wrapInQuota(Temporary $storage, int $quota, string $mountPoint): Quota { + $wrapped = new Quota([ + 'storage' => $storage, + 'quota' => $quota, + 'include_external_storage' => true, + 'user' => $this->owner, + ]); + Filesystem::mount($wrapped, [], $mountPoint); + return $wrapped; + } + + /** + * Builds a setup manager that registers its storage wrappers in the loader + * of Filesystem with `quota_include_external_storage` enabled. + */ + private function createSetupManager(): SetupManager { + $config = $this->createMock(IConfig::class); + $config->method('getSystemValue') + ->willReturnCallback(fn (string $key, mixed $default = '') => $key === 'quota_include_external_storage' ? true : $default); + $config->method('getSystemValueBool') + ->willReturnCallback(fn (string $key, bool $default = false) => $default); + + $cacheFactory = $this->createMock(ICacheFactory::class); + $cacheFactory->method('createDistributed')->willReturn($this->createMock(ICache::class)); + + $mountManager = $this->createMock(IMountManager::class); + $mountManager->method('getAll')->willReturn([]); + + $mountProviderCollection = $this->createMock(MountProviderCollection::class); + $mountProviderCollection->method('getRootMounts')->willReturn([]); + + return new SetupManager( + $this->createMock(IEventLogger::class), + $mountProviderCollection, + $mountManager, + $this->createMock(IUserManager::class), + $this->createMock(IEventDispatcher::class), + $this->createMock(IUserMountCache::class), + $this->createMock(ILockdownManager::class), + $this->createMock(IUserSession::class), + $cacheFactory, + $this->createMock(LoggerInterface::class), + $config, + $this->createMock(ShareDisableChecker::class), + $this->createMock(IAppManager::class), + $this->createMock(FileAccess::class), + ); + } + + /** + * Writing into a share is accounted against the quota of the share owner, + * so the usage has to be read from the owner's home and not from the home + * of the session user performing the write. + */ + public function testFreeSpaceMeasuresStorageOwnerAndNotSessionUser(): void { + $ownerRoot = '/' . $this->owner->getUID() . '/files'; + $ownerStorage = $this->mountStorageWithUsage($ownerRoot, 5); + $quotaStorage = $this->wrapInQuota($ownerStorage, 100, $ownerRoot); + + $this->mountStorageWithUsage('/' . $this->otherUser->getUID() . '/files', 90); + + $this->assertEquals(95, $quotaStorage->free_space('')); + } + + /** + * The session user being over the owner's quota must not make the share + * read-only for them. + */ + public function testFreeSpaceIsNotExhaustedBySessionUserUsage(): void { + $ownerRoot = '/' . $this->owner->getUID() . '/files'; + $ownerStorage = $this->mountStorageWithUsage($ownerRoot, 5); + $quotaStorage = $this->wrapInQuota($ownerStorage, 100, $ownerRoot); + + $this->mountStorageWithUsage('/' . $this->otherUser->getUID() . '/files', 150); + + $this->assertEquals(95, $quotaStorage->free_space('')); + } + + /** + * External storages mounted in the owner's files count towards the owner's + * quota, which is what `quota_include_external_storage` enables. + */ + public function testFreeSpaceIncludesStorageMountedInOwnerHome(): void { + $ownerRoot = '/' . $this->owner->getUID() . '/files'; + $ownerStorage = $this->mountStorageWithUsage($ownerRoot, 5); + $quotaStorage = $this->wrapInQuota($ownerStorage, 100, $ownerRoot); + + $this->mountStorageWithUsage($ownerRoot . '/external', 20); + + $this->assertEquals(75, $quotaStorage->free_space('')); + } + + /** + * Without a known user the wrapper reads the session's view, the only one it + * can resolve. + */ + public function testFreeSpaceFallsBackToSessionViewWithoutUser(): void { + $otherRoot = '/' . $this->otherUser->getUID() . '/files'; + $storage = $this->mountStorageWithUsage($otherRoot, 30); + + $quotaStorage = new Quota([ + 'storage' => $storage, + 'quota' => 100, + 'include_external_storage' => true, + ]); + Filesystem::mount($quotaStorage, [], $otherRoot); + + $this->assertEquals(70, $quotaStorage->free_space('')); + } + + /** + * The quota wrapper the setup manager installs on a home mount point has to + * measure the user of that home, which is what makes writes into shares of + * other users use the right usage. + */ + public function testSetupManagerWrapsHomeWithQuotaOfHomeUser(): void { + $ownerUid = $this->owner->getUID(); + $ownerStorage = $this->mountStorageWithUsage('/' . $ownerUid . '/files', 5); + $this->mountStorageWithUsage('/' . $this->otherUser->getUID() . '/files', 90); + $this->owner->setQuota('100 B'); + + // The setup of the test users registered the wrapper of the shared setup manager already + Filesystem::getLoader()->removeStorageWrapper('oc_quota'); + $this->createSetupManager()->setupForUser($this->owner); + $wrapped = Filesystem::getLoader()->wrap(new HomeMountPoint($this->owner, $ownerStorage, '/' . $ownerUid), $ownerStorage); + + $this->assertTrue($wrapped->instanceOfStorage(Quota::class)); + $this->assertEquals(95, $wrapped->free_space('files')); + } + + /** + * The storage info of another user's home reports the quota of that user, + * so the usage it reports has to be the one of that user as well. + */ + public function testStorageInfoOfOtherUsersHomeReportsUsageOfHomeUser(): void { + $homeUid = $this->getUniqueID('quota_home_'); + $homeUser = $this->createMock(IUser::class); + $homeUser->method('getUID')->willReturn($homeUid); + $homeUser->method('getHome')->willReturn(Server::get(ITempManager::class)->getTemporaryFolder()); + $homeUser->method('getQuotaBytes')->willReturn(100); + $homeUser->method('getDisplayName')->willReturn('Home user'); + + $home = new Home(['user' => $homeUser]); + $home->mkdir('files'); + $home->file_put_contents('files/usage.bin', str_repeat('x', 5)); + $home->getScanner()->scan(''); + Filesystem::mount($home, [], '/' . $homeUid); + + $this->mountStorageWithUsage('/' . $this->otherUser->getUID() . '/files', 90); + + Server::get(IConfig::class)->setSystemValue('quota_include_external_storage', true); + + $rootInfo = (new View('/' . $homeUid . '/files'))->getFileInfo(''); + $storageInfo = \OC_Helper::getStorageInfo('', $rootInfo, true, false); + + $this->assertSame($homeUid, $storageInfo['owner']); + $this->assertEquals(5, $storageInfo['used']); + $this->assertEquals(95, $storageInfo['free']); + } + + /** + * When a user's files root is not their home storage, e.g. an external storage + * mounted at `/`, the storage info of that root reports that user's quota and + * usage, not the ones of the session user asking for it. + */ + public function testStorageInfoOfNonHomeFilesRootReportsQuotaOfItsUser(): void { + $ownerUid = $this->owner->getUID(); + $this->mountStorageWithUsage('/' . $ownerUid . '/files', 5); + $this->mountStorageWithUsage('/' . $this->otherUser->getUID() . '/files', 90); + $this->owner->setQuota('100 B'); + $this->otherUser->setQuota('1000 B'); + + Server::get(IConfig::class)->setSystemValue('quota_include_external_storage', true); + + $rootInfo = (new View('/' . $ownerUid . '/files'))->getFileInfo(''); + $storageInfo = \OC_Helper::getStorageInfo('', $rootInfo, true, false); + + $this->assertSame($ownerUid, $storageInfo['owner']); + $this->assertEquals(100, $storageInfo['quota']); + $this->assertEquals(5, $storageInfo['used']); + $this->assertEquals(95, $storageInfo['free']); + } + + /** + * A home storage whose user has no files root in the filesystem has no usage + * that could be reported. + */ + public function testStorageInfoOfHomeWithoutFilesRootThrows(): void { + $homeUid = $this->getUniqueID('quota_home_'); + $homeUser = $this->createMock(IUser::class); + $homeUser->method('getUID')->willReturn($homeUid); + $homeUser->method('getHome')->willReturn(Server::get(ITempManager::class)->getTemporaryFolder()); + $homeUser->method('getQuotaBytes')->willReturn(100); + + $home = new Home(['user' => $homeUser]); + $home->mkdir('files'); + $home->getScanner()->scan(''); + $mountPoint = '/' . $this->getUniqueID('quota_elsewhere_'); + Filesystem::mount($home, [], $mountPoint); + + Server::get(IConfig::class)->setSystemValue('quota_include_external_storage', true); + + $rootInfo = (new View($mountPoint . '/files'))->getFileInfo(''); + + $this->expectException(NotFoundException::class); + \OC_Helper::getStorageInfo('', $rootInfo, true, false); + } +}