mirror of
https://github.com/nextcloud/server.git
synced 2026-11-22 13:03:44 +01:00
When the `quota_include_external_storage` setting is enabled and a shared folder is mounted, the free space is being calculcated the wrong way. Example: - user1 has a quota of 10GB with 9GB used - user2 has a quota of 5GB and shares a folder with user1 - user1 tries to write a file to the shared folder - Upload fails due to `quota-available-bytes: 0` (the usage of 9GB being compared against the 5GB of user2) The issue is fixed by checking the usage of the folder's owner instead of the session user's. The same issue also applies to the admin overview, where the admin's own quota is being shown for users whose files folder is an external storage mounted at `/`. Assisted-by: ClaudeCode:claude-opus-5.5 Signed-off-by: David Dreschner <david.dreschner@nextcloud.com>
276 lines
8.4 KiB
PHP
276 lines
8.4 KiB
PHP
<?php
|
|
|
|
/**
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
*/
|
|
|
|
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 */
|
|
protected $quotaCallback;
|
|
/** @var int|float|null int on 64bits, float on 32bits for bigint */
|
|
protected int|float|null $quota;
|
|
protected string $sizeRoot;
|
|
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
|
|
*/
|
|
public function __construct(array $parameters) {
|
|
parent::__construct($parameters);
|
|
$this->quota = $parameters['quota'] ?? null;
|
|
$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 {
|
|
if ($this->quota === null) {
|
|
$quotaCallback = $this->quotaCallback;
|
|
if ($quotaCallback === null) {
|
|
throw new \Exception('No quota or quota callback provider');
|
|
}
|
|
$this->quota = $quotaCallback();
|
|
}
|
|
|
|
return $this->quota;
|
|
}
|
|
|
|
private function hasQuota(): bool {
|
|
if (!$this->enabled) {
|
|
return false;
|
|
}
|
|
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) {
|
|
return $this->getSizeIncludingExternalStorage();
|
|
} else {
|
|
$cache = is_null($storage) ? $this->getCache() : $storage->getCache();
|
|
$data = $cache->get($path);
|
|
if ($data instanceof ICacheEntry && isset($data['size'])) {
|
|
return $data['size'];
|
|
} else {
|
|
return FileInfo::SPACE_NOT_COMPUTED;
|
|
}
|
|
}
|
|
}
|
|
|
|
#[\Override]
|
|
public function free_space(string $path): int|float|false {
|
|
if (!$this->hasQuota()) {
|
|
return $this->getWrapperStorage()->free_space($path);
|
|
}
|
|
if ($this->getQuota() < 0 || str_starts_with($path, 'cache') || str_starts_with($path, 'uploads')) {
|
|
return $this->getWrapperStorage()->free_space($path);
|
|
} else {
|
|
$used = $this->getSize($this->sizeRoot);
|
|
if ($used < 0) {
|
|
return FileInfo::SPACE_NOT_COMPUTED;
|
|
} else {
|
|
$free = $this->getWrapperStorage()->free_space($path);
|
|
$quotaFree = max($this->getQuota() - $used, 0);
|
|
// if free space is known
|
|
$free = $free >= 0 ? min($free, $quotaFree) : $quotaFree;
|
|
return $free;
|
|
}
|
|
}
|
|
}
|
|
|
|
#[\Override]
|
|
public function file_put_contents(string $path, mixed $data): int|float|false {
|
|
if (!$this->hasQuota()) {
|
|
return $this->getWrapperStorage()->file_put_contents($path, $data);
|
|
}
|
|
$free = $this->free_space($path);
|
|
if ($free < 0 || strlen($data) < $free) {
|
|
return $this->getWrapperStorage()->file_put_contents($path, $data);
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
#[\Override]
|
|
public function copy(string $source, string $target): bool {
|
|
if (!$this->hasQuota()) {
|
|
return $this->getWrapperStorage()->copy($source, $target);
|
|
}
|
|
$free = $this->free_space($target);
|
|
if ($free < 0 || $this->getSize($source) < $free) {
|
|
return $this->getWrapperStorage()->copy($source, $target);
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
#[\Override]
|
|
public function fopen(string $path, string $mode) {
|
|
// reading doesn't consume quota, so it should not be restricted by it
|
|
$isReadMode = $mode === 'r' || $mode === 'rb';
|
|
if ($isReadMode || !$this->hasQuota() || $this->isPartFile($path)) {
|
|
return $this->getWrapperStorage()->fopen($path, $mode);
|
|
}
|
|
|
|
$free = $this->free_space($path);
|
|
// treat a failed free_space() the same as no free space
|
|
if ($this->shouldApplyQuota($path) && ($free === 0 || $free === 0.0 || $free === false)) {
|
|
return false;
|
|
}
|
|
|
|
$source = $this->getWrapperStorage()->fopen($path, $mode);
|
|
// writing past the remaining free space fails. Negative values mean the free
|
|
// space is unknown or unlimited (FileInfo::SPACE_*), so no limit is applied
|
|
if ($source && (is_int($free) || is_float($free)) && $free >= 0) {
|
|
// only apply quota for files, not metadata, trash or others
|
|
if ($this->shouldApplyQuota($path)) {
|
|
return \OC\Files\Stream\Quota::wrap($source, $free);
|
|
}
|
|
}
|
|
|
|
return $source;
|
|
}
|
|
|
|
/**
|
|
* Checks whether the given path is a part file
|
|
*
|
|
* @param string $path Path that may identify a .part file
|
|
* @note this is needed for reusing keys
|
|
*/
|
|
private function isPartFile(string $path): bool {
|
|
$extension = pathinfo($path, PATHINFO_EXTENSION);
|
|
|
|
return ($extension === 'part');
|
|
}
|
|
|
|
/**
|
|
* Only apply quota for files, not metadata, trash or others
|
|
*/
|
|
protected function shouldApplyQuota(string $path): bool {
|
|
return str_starts_with(ltrim($path, '/'), 'files/');
|
|
}
|
|
|
|
#[\Override]
|
|
public function copyFromStorage(IStorage $sourceStorage, string $sourceInternalPath, string $targetInternalPath): bool {
|
|
if (!$this->hasQuota()) {
|
|
return $this->getWrapperStorage()->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
|
|
}
|
|
$free = $this->free_space($targetInternalPath);
|
|
if ($free < 0 || $this->getSize($sourceInternalPath, $sourceStorage) < $free) {
|
|
return $this->getWrapperStorage()->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
#[\Override]
|
|
public function moveFromStorage(IStorage $sourceStorage, string $sourceInternalPath, string $targetInternalPath): bool {
|
|
if (!$this->hasQuota()) {
|
|
return $this->getWrapperStorage()->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
|
|
}
|
|
$free = $this->free_space($targetInternalPath);
|
|
if ($free < 0 || $this->getSize($sourceInternalPath, $sourceStorage) < $free) {
|
|
return $this->getWrapperStorage()->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
#[\Override]
|
|
public function mkdir(string $path): bool {
|
|
if (!$this->hasQuota()) {
|
|
return $this->getWrapperStorage()->mkdir($path);
|
|
}
|
|
$free = $this->free_space($path);
|
|
// treat a failed free_space() the same as no free space
|
|
if ($this->shouldApplyQuota($path) && ($free === 0 || $free === 0.0 || $free === false)) {
|
|
return false;
|
|
}
|
|
|
|
return parent::mkdir($path);
|
|
}
|
|
|
|
#[\Override]
|
|
public function touch(string $path, ?int $mtime = null): bool {
|
|
if (!$this->hasQuota()) {
|
|
return $this->getWrapperStorage()->touch($path, $mtime);
|
|
}
|
|
$free = $this->free_space($path);
|
|
// treat a failed free_space() the same as no free space
|
|
if ($free === 0 || $free === 0.0 || $free === false) {
|
|
return false;
|
|
}
|
|
|
|
return parent::touch($path, $mtime);
|
|
}
|
|
|
|
public function enableQuota(bool $enabled): void {
|
|
$this->enabled = $enabled;
|
|
}
|
|
|
|
#[\Override]
|
|
public function writeStream(string $path, $stream, ?int $size = null): int {
|
|
if (!$this->hasQuota()) {
|
|
return parent::writeStream($path, $stream, $size);
|
|
}
|
|
|
|
$free = $this->free_space($path);
|
|
// treat a failed free_space() the same as no free space
|
|
if ($this->shouldApplyQuota($path) && ($free === 0 || $free === 0.0 || $free === false)) {
|
|
throw new NotEnoughSpaceException();
|
|
}
|
|
|
|
if ($size !== null) {
|
|
if ($free < 0 || $size < $free) {
|
|
return parent::writeStream($path, $stream, $size);
|
|
} else {
|
|
throw new NotEnoughSpaceException();
|
|
}
|
|
} else {
|
|
// force fallback through `fopen` to handle the quota
|
|
try {
|
|
return parent::writeStreamFallback($path, $stream);
|
|
} catch (GenericFileException) {
|
|
throw new NotEnoughSpaceException();
|
|
}
|
|
}
|
|
}
|
|
}
|