Skip to content

Commit e9753e6

Browse files
Merge pull request #66334 from nextcloud/backport/64872/stable33
[stable33] fix(quota): Calculate used quota for foreign users the correct way
2 parents e97b498 + 2f258ee commit e9753e6

5 files changed

Lines changed: 377 additions & 8 deletions

File tree

‎build/psalm-baseline.xml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4181,8 +4181,13 @@
41814181
</InvalidArgument>
41824182
</file>
41834183
<file src="lib/private/legacy/OC_Helper.php">
4184+
<InternalClass>
4185+
<code><![CDATA[new View('/' . $user->getUID() . '/files')]]></code>
4186+
</InternalClass>
41844187
<InternalMethod>
41854188
<code><![CDATA[getAbsolutePath]]></code>
4189+
<code><![CDATA[getFileInfo]]></code>
4190+
<code><![CDATA[new View('/' . $user->getUID() . '/files')]]></code>
41864191
</InternalMethod>
41874192
</file>
41884193
<file src="lib/private/legacy/OC_User.php">

‎lib/private/Files/SetupManager.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ function ($mountPoint, IStorage $storage, IMountPoint $mount) use ($reSharingEna
206206
$user = $mount->getUser();
207207
return new Quota(['storage' => $storage, 'quotaCallback' => function () use ($user) {
208208
return $user->getQuotaBytes();
209-
}, 'root' => 'files', 'include_external_storage' => $quotaIncludeExternal]);
209+
}, 'root' => 'files', 'include_external_storage' => $quotaIncludeExternal, 'user' => $user]);
210210
}
211211

212212
return $storage;

‎lib/private/Files/Storage/Wrapper/Quota.php‎

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
namespace OC\Files\Storage\Wrapper;
99

1010
use OC\Files\Filesystem;
11+
use OC\Files\View;
1112
use OC\SystemConfig;
1213
use OCP\Files\Cache\ICacheEntry;
1314
use OCP\Files\FileInfo;
1415
use OCP\Files\Storage\IStorage;
16+
use OCP\IUser;
1517

1618
class Quota extends Wrapper {
1719
/** @var callable|null */
@@ -22,6 +24,8 @@ class Quota extends Wrapper {
2224
private SystemConfig $config;
2325
private bool $quotaIncludeExternalStorage;
2426
private bool $enabled = true;
27+
/** The user the wrapped home storage belongs to, if known */
28+
private ?IUser $user;
2529

2630
/**
2731
* @param array $parameters
@@ -32,6 +36,7 @@ public function __construct(array $parameters) {
3236
$this->quotaCallback = $parameters['quotaCallback'] ?? null;
3337
$this->sizeRoot = $parameters['root'] ?? '';
3438
$this->quotaIncludeExternalStorage = $parameters['include_external_storage'] ?? false;
39+
$this->user = $parameters['user'] ?? null;
3540
}
3641

3742
public function getQuota(): int|float {
@@ -53,13 +58,28 @@ private function hasQuota(): bool {
5358
return $this->getQuota() !== FileInfo::SPACE_UNLIMITED;
5459
}
5560

61+
/**
62+
* Usage of the quota user's files including the external storages mounted in them.
63+
* It is read through that user's view, since the session user differs when writing into their shares.
64+
* Storages using the quota user's session credentials count as empty in other sessions.
65+
*/
66+
private function getSizeIncludingExternalStorage(): int|float {
67+
if ($this->user === null) {
68+
$rootInfo = Filesystem::getFileInfo('', 'ext');
69+
} else {
70+
$view = new View('/' . $this->user->getUID() . '/files');
71+
$rootInfo = $view->getFileInfo('', 'ext');
72+
}
73+
74+
if ($rootInfo) {
75+
return $rootInfo->getSize(true);
76+
}
77+
return FileInfo::SPACE_NOT_COMPUTED;
78+
}
79+
5680
protected function getSize(string $path, ?IStorage $storage = null): int|float {
5781
if ($this->quotaIncludeExternalStorage) {
58-
$rootInfo = Filesystem::getFileInfo('', 'ext');
59-
if ($rootInfo) {
60-
return $rootInfo->getSize(true);
61-
}
62-
return FileInfo::SPACE_NOT_COMPUTED;
82+
return $this->getSizeIncludingExternalStorage();
6383
} else {
6484
$cache = is_null($storage) ? $this->getCache() : $storage->getCache();
6585
$data = $cache->get($path);

‎lib/private/legacy/OC_Helper.php‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@
1111
use OC\Files\ObjectStore\HomeObjectStoreStorage;
1212
use OC\Files\Storage\Home;
1313
use OC\Files\Storage\Wrapper\Quota;
14+
use OC\Files\View;
1415
use OCP\Files\FileInfo;
1516
use OCP\Files\Mount\IMountPoint;
17+
use OCP\Files\NotFoundException;
1618
use OCP\Files\Storage\ISharedStorage;
1719
use OCP\IBinaryFinder;
1820
use OCP\ICacheFactory;
1921
use OCP\IUser;
22+
use OCP\IUserManager;
2023
use OCP\Server;
2124
use OCP\Util;
2225
use Psr\Log\LoggerInterface;
@@ -199,7 +202,7 @@ public static function getStorageInfo($path, $rootInfo = null, $includeMountPoin
199202
/** @var Home|HomeObjectStoreStorage $storage */
200203
$user = $storage->getUser();
201204
} else {
202-
$user = \OC::$server->getUserSession()->getUser();
205+
$user = self::getUserOfPath($rootInfo->getPath()) ?? \OC::$server->getUserSession()->getUser();
203206
}
204207
$quota = $user?->getQuotaBytes() ?? FileInfo::SPACE_UNKNOWN;
205208
if ($user !== null && $quota !== FileInfo::SPACE_UNLIMITED) {
@@ -285,14 +288,32 @@ public static function getStorageInfo($path, $rootInfo = null, $includeMountPoin
285288
return $info;
286289
}
287290

291+
/**
292+
* The user whose tree an absolute path is in, e.g. the user `alice` for `/alice/files/...`
293+
*/
294+
private static function getUserOfPath(string $path): ?IUser {
295+
$userId = explode('/', ltrim($path, '/'))[0];
296+
if ($userId === '') {
297+
return null;
298+
}
299+
return Server::get(IUserManager::class)->get($userId);
300+
}
301+
288302
/**
289303
* Get storage info including all mount points and quota
290304
*
291305
* @psalm-suppress LessSpecificReturnStatement Legacy code outputs weird types - manually validated that they are correct
292306
* @return StorageInfo
307+
* @throws NotFoundException
293308
*/
294309
private static function getGlobalStorageInfo(int|float $quota, IUser $user, IMountPoint $mount): array {
295-
$rootInfo = \OC\Files\Filesystem::getFileInfo('', 'ext');
310+
// the usage has to be read from the home of the user the quota belongs to,
311+
// which is not necessarily the user of the current session
312+
$view = new View('/' . $user->getUID() . '/files');
313+
$rootInfo = $view->getFileInfo('', 'ext');
314+
if (!$rootInfo instanceof FileInfo) {
315+
throw new NotFoundException('The root directory of the user\'s files is missing');
316+
}
296317
/** @var int|float $used */
297318
$used = $rootInfo['size'];
298319
if ($used < 0) {

0 commit comments

Comments
 (0)