Merge pull request #59995 from nextcloud/quota-writestream-fopen

This commit is contained in:
Benjamin Gaussorgues 2026-05-04 10:06:18 +02:00 committed by GitHub
commit aa797a36f1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 77 additions and 10 deletions

View file

@ -621,6 +621,9 @@ class File extends Node implements IFile {
if ($e instanceof NotFoundException) {
throw new NotFound($this->l10n->t('File not found: %1$s', [$e->getMessage()]), 0, $e);
}
if ($e instanceof Files\NotEnoughSpaceException) {
throw new EntityTooLarge($this->l10n->t('Insufficient space'), 0, $e);
}
throw new \Sabre\DAV\Exception($e->getMessage(), 0, $e);
}

View file

@ -11,6 +11,8 @@ use OC\Files\Filesystem;
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;
class Quota extends Wrapper {
@ -120,19 +122,20 @@ class Quota extends Wrapper {
#[\Override]
public function fopen(string $path, string $mode) {
if (!$this->hasQuota()) {
if (!$this->hasQuota() || $this->isPartFile($path)) {
return $this->getWrapperStorage()->fopen($path, $mode);
}
$source = $this->getWrapperStorage()->fopen($path, $mode);
// don't apply quota for part files
if (!$this->isPartFile($path)) {
$free = $this->free_space($path);
if ($source && (is_int($free) || is_float($free)) && $free >= 0 && $mode !== 'r' && $mode !== 'rb') {
// only apply quota for files, not metadata, trash or others
if ($this->shouldApplyQuota($path)) {
return \OC\Files\Stream\Quota::wrap($source, $free);
}
$free = $this->free_space($path);
if ($this->shouldApplyQuota($path) && $free == 0) {
return false;
}
$source = $this->getWrapperStorage()->fopen($path, $mode);
if ($source && (is_int($free) || is_float($free)) && $free >= 0 && $mode !== 'r' && $mode !== 'rb') {
// only apply quota for files, not metadata, trash or others
if ($this->shouldApplyQuota($path)) {
return \OC\Files\Stream\Quota::wrap($source, $free);
}
}
@ -213,4 +216,31 @@ class Quota extends Wrapper {
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);
if ($this->shouldApplyQuota($path) && $free == 0) {
throw new NotEnoughSpaceException();
}
if ($size !== null) {
if ($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();
}
}
}
}

View file

@ -392,6 +392,13 @@ class Wrapper implements Storage, ILockingStorage, IWriteStreamStorage {
return $storage->writeStream($path, $stream, $size);
}
return $this->writeStreamFallback($path, $stream);
}
/**
* @param resource $stream
*/
protected function writeStreamFallback(string $path, $stream): int {
$target = $this->fopen($path, 'w');
if ($target === false) {
throw new GenericFileException('Failed to open ' . $path);

View file

@ -229,4 +229,31 @@ class QuotaTest extends \Test\Files\Storage\Storage {
$instance = $this->getLimitedStorage(0.0);
$this->assertFalse($instance->touch('foobar'));
}
public function testNoFopenQuotaZero(): void {
$instance = $this->getLimitedStorage(0.0);
$fh = $instance->fopen('files/test.txt', 'w');
$this->assertFalse($fh);
}
public function testNoWriteStreamQuota(): void {
$instance = $this->getLimitedStorage(5.0);
$stream = fopen('php://temp', 'w+');
fwrite($stream, 'foo');
rewind($stream);
$instance->writeStream('files/test.txt', $stream);
$stream = fopen('php://temp', 'w+');
fwrite($stream, 'foobar');
rewind($stream);
$this->expectException(Files\NotEnoughSpaceException::class);
$instance->writeStream('files/test.txt', $stream);
}
public function testNoWriteStreamQuotaZero(): void {
$instance = $this->getLimitedStorage(0.0);
$stream = fopen('php://temp', 'w+');
$this->expectException(Files\NotEnoughSpaceException::class);
$instance->writeStream('files/test.txt', $stream);
}
}