Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/private/Files/SetupManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
30 changes: 25 additions & 5 deletions lib/private/Files/Storage/Wrapper/Quota.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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);
Expand Down
28 changes: 26 additions & 2 deletions lib/private/legacy/OC_Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
Loading
Loading