Skip to content
Open
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
99 changes: 74 additions & 25 deletions lib/private/Files/Storage/Wrapper/Quota.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,32 +76,72 @@ public function getSize(string $path, ?IStorage $storage = null): int|float {

#[\Override]
public function free_space(string $path): int|float|false {
if (!$this->hasQuota()) {
return $this->getWrapperStorage()->free_space($path);
return $this->calculateFreeSpace($path);
}

/**
* Calculate free space constrained by quota and physical storage capacity.
*
* This helper exists so the available space calculation can, optionally, be
* influenced for an operation that may replace a file.
*
* When specified, credit the size of the pre-existing file against quota usage,
* but not against the wrapped storage's physical free space. This is conservative
* because overwriting a file may not immediately reclaim physical space on every
* storage backend. Targets outside the quota scope or with unknown sizes are not
* credited.
*
* @param string $path Path to check
* @param bool $creditExistingTarget Whether to credit an eligible existing file at the path
*/
private function calculateFreeSpace(string $path, bool $creditExistingTarget = false): int|float|false {
$storage = $this->getWrapperStorage();

if (!$this->hasQuota()
|| $this->getQuota() < 0
|| str_starts_with($path, 'cache')
|| str_starts_with($path, 'uploads')
) {
return $storage->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;

$used = $this->getSize($this->sizeRoot);
if ($used < 0) {
return FileInfo::SPACE_NOT_COMPUTED;
}

if ($creditExistingTarget
&& !$this->quotaIncludeExternalStorage

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does quotaIncludeExternalStorage change anything here?

&& $this->shouldApplyQuota($path)
&& $storage->is_file($path)
) {
$root = trim($this->sizeRoot, '/');
$target = trim($path, '/');
$isWithinRoot = $root === ''
|| $target === $root
|| str_starts_with($target, $root . '/');

if ($isWithinRoot) {
$existingSize = $this->getSize($path);
if ($existingSize >= 0) {
$used = max(0, $used - $existingSize);
}
}
}

$quotaFree = max($this->getQuota() - $used, 0);
$diskFree = $storage->free_space($path);
return $diskFree >= 0 ? min($diskFree, $quotaFree) : $quotaFree;
}

#[\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) {

$free = $this->calculateFreeSpace($path, creditExistingTarget: true);
if ($free < 0 || strlen($data) <= $free) {
return $this->getWrapperStorage()->file_put_contents($path, $data);
} else {
return false;
Expand All @@ -113,8 +153,9 @@ 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) {

$free = $this->calculateFreeSpace($target, creditExistingTarget: true);
if ($free < 0 || $this->getSize($source) <= $free) {
return $this->getWrapperStorage()->copy($source, $target);
} else {
return false;
Expand All @@ -127,7 +168,7 @@ public function fopen(string $path, string $mode) {
return $this->getWrapperStorage()->fopen($path, $mode);
}

$free = $this->free_space($path);
$free = $this->calculateFreeSpace($path, creditExistingTarget: str_starts_with($mode, 'w'));
// treat a failed free_space() the same as no free space
if ($this->shouldApplyQuota($path) && ($free === 0 || $free === 0.0 || $free === false)) {
return false;
Expand Down Expand Up @@ -168,8 +209,9 @@ public function copyFromStorage(IStorage $sourceStorage, string $sourceInternalP
if (!$this->hasQuota()) {
return $this->getWrapperStorage()->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
}
$free = $this->free_space($targetInternalPath);
if ($free < 0 || $this->getSize($sourceInternalPath, $sourceStorage) < $free) {

$free = $this->calculateFreeSpace($targetInternalPath, creditExistingTarget: true);
if ($free < 0 || $this->getSize($sourceInternalPath, $sourceStorage) <= $free) {
return $this->getWrapperStorage()->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
} else {
return false;
Expand All @@ -181,8 +223,13 @@ public function moveFromStorage(IStorage $sourceStorage, string $sourceInternalP
if (!$this->hasQuota()) {
return $this->getWrapperStorage()->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
}
$free = $this->free_space($targetInternalPath);
if ($free < 0 || $this->getSize($sourceInternalPath, $sourceStorage) < $free) {

// Credit a known, in-scope target, but require room for the full source because it
// may be outside this quota's scope.
// Note: This can reject moves when the source is already included in this quota's usage.
// TODO: Account for the source's removal when it is known to be included in this quota.
$free = $this->calculateFreeSpace($targetInternalPath, creditExistingTarget: true);
if ($free < 0 || $this->getSize($sourceInternalPath, $sourceStorage) <= $free) {
return $this->getWrapperStorage()->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
} else {
return false;
Expand All @@ -194,6 +241,7 @@ 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)) {
Expand All @@ -208,6 +256,7 @@ 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) {
Expand All @@ -227,14 +276,14 @@ public function writeStream(string $path, $stream, ?int $size = null): int {
return parent::writeStream($path, $stream, $size);
}

$free = $this->free_space($path);
$free = $this->calculateFreeSpace($path, creditExistingTarget: true);
// 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) {
if ($free < 0 || $size <= $free) {
return parent::writeStream($path, $stream, $size);
} else {
throw new NotEnoughSpaceException();
Expand Down
169 changes: 167 additions & 2 deletions tests/lib/Files/Storage/Wrapper/QuotaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

namespace Test\Files\Storage\Wrapper;

//ensure the constants are loaded
use OC\Files\Cache\CacheEntry;
use OC\Files\Storage\Local;
use OC\Files\Storage\Wrapper\Quota;
Expand All @@ -21,7 +20,6 @@
/**
* Class QuotaTest
*
*
* @package Test\Files\Storage\Wrapper
*/
#[\PHPUnit\Framework\Attributes\Group('DB')]
Expand Down Expand Up @@ -61,13 +59,151 @@ public function testFilePutContentsNotEnoughSpace(): void {
$this->assertFalse($instance->file_put_contents('files/foo', 'foobar'));
}

public function testFilePutContentsExactlyFillsQuota(): void {
$instance = $this->getLimitedStorage(6);
$this->assertSame(6, $instance->file_put_contents('files/foo', 'foobar'));
}

public function testFilePutContentsCreditsExistingTargetSize(): void {
$instance = $this->getLimitedStorage(10);
$this->assertSame(6, $instance->file_put_contents('files/foo', 'foobar'));
$instance->getScanner()->scan('');

$this->assertSame(10, $instance->file_put_contents('files/foo', '0123456789'));
}

public function testFopenWriteCreditsExistingTargetSize(): void {
$instance = $this->getLimitedStorage(6);
$this->assertSame(6, $instance->file_put_contents('files/foo', 'foobar'));
$instance->getScanner()->scan('');

$stream = $instance->fopen('files/foo', 'w+');
$this->assertNotFalse($stream);
$this->assertSame(6, fwrite($stream, 'qwerty'));
fclose($stream);
}

public function testWriteStreamCreditsExistingTargetSize(): void {
$instance = $this->getLimitedStorage(6);
$this->assertSame(6, $instance->file_put_contents('files/foo', 'foobar'));
$instance->getScanner()->scan('');

$stream = fopen('php://temp', 'w+');
fwrite($stream, 'qwerty');
rewind($stream);

$this->assertSame(6, $instance->writeStream('files/foo', $stream, 6));
}

public function testCopyNotEnoughSpace(): void {
$instance = $this->getLimitedStorage(9);
$this->assertEquals(6, $instance->file_put_contents('files/foo', 'foobar'));
$instance->getScanner()->scan('');
$this->assertFalse($instance->copy('files/foo', 'files/bar'));
}

public function testCopyCreditsExistingTargetSize(): void {
$instance = $this->getLimitedStorage(12);
$this->assertSame(6, $instance->file_put_contents('files/source', 'source'));
$this->assertSame(6, $instance->file_put_contents('files/target', 'target'));
$instance->getScanner()->scan('');

$this->assertTrue($instance->copy('files/source', 'files/target'));
$this->assertSame('source', $instance->file_get_contents('files/target'));
}

public function testCopyExactlyFillsQuota(): void {
$instance = $this->getLimitedStorage(12);
$this->assertSame(6, $instance->file_put_contents('files/foo', 'foobar'));
$instance->getScanner()->scan('');

$this->assertTrue($instance->copy('files/foo', 'files/bar'));
}

public function testCopyFromStorageExactlyFillsQuota(): void {
$instance = $this->getLimitedStorage(12);
$this->assertSame(6, $instance->file_put_contents('files/foo', 'foobar'));
$instance->getScanner()->scan('');

$this->assertTrue($instance->copyFromStorage(
$instance->getWrapperStorage(),
'files/foo',
'files/bar',
));
}

public function testCopyFromStorageCreditsExistingTargetSize(): void {
$instance = $this->getLimitedStorage(6);
$this->assertSame(6, $instance->file_put_contents('files/target', 'target'));
$instance->getScanner()->scan('');

$sourcePath = $this->tmpDir . '/source';
mkdir($sourcePath);
$sourceStorage = new Local(['datadir' => $sourcePath]);
$sourceStorage->mkdir('files');
$this->assertSame(6, $sourceStorage->file_put_contents('files/source', 'source'));
$sourceStorage->getScanner()->scan('');

$this->assertTrue($instance->copyFromStorage(
$sourceStorage,
'files/source',
'files/target',
));
$this->assertSame('source', $instance->file_get_contents('files/target'));
}

public function testMoveFromStorageExactlyFillsQuota(): void {
$instance = $this->getLimitedStorage(12);
$this->assertSame(6, $instance->file_put_contents('files/existing', 'foobar'));
$instance->getScanner()->scan('');

$sourcePath = $this->tmpDir . '/source';
mkdir($sourcePath);
$sourceStorage = new Local(['datadir' => $sourcePath]);
$sourceStorage->mkdir('files');
$this->assertSame(6, $sourceStorage->file_put_contents('files/foo', 'foobar'));
$sourceStorage->getScanner()->scan('');

$this->assertTrue($instance->moveFromStorage(
$sourceStorage,
'files/foo',
'files/moved',
));
}

public function testMoveFromStorageCreditsExistingTargetSize(): void {
$instance = $this->getLimitedStorage(12);
$this->assertSame(6, $instance->file_put_contents('files/target', 'target'));
$instance->getScanner()->scan('');

$sourcePath = $this->tmpDir . '/source';
mkdir($sourcePath);
$sourceStorage = new Local(['datadir' => $sourcePath]);
$sourceStorage->mkdir('files');
$this->assertSame(12, $sourceStorage->file_put_contents('files/source', 'source-data'));
$sourceStorage->getScanner()->scan('');

$this->assertTrue($instance->moveFromStorage(
$sourceStorage,
'files/source',
'files/target',
));
$this->assertSame('source-data', $instance->file_get_contents('files/target'));
}

public function testMoveFromStorageDoesNotCreditSourceAlreadyInQuota(): void {
$instance = $this->getLimitedStorage(10);
$this->assertSame(6, $instance->file_put_contents('files/source', 'source'));
$this->assertSame(6, $instance->file_put_contents('files/target', 'target'));
$instance->getScanner()->scan('');

$this->assertFalse($instance->moveFromStorage(
$instance->getWrapperStorage(),
'files/source',
'files/target',
));
}

public function testFreeSpace(): void {
$instance = $this->getLimitedStorage(9);
$this->assertEquals(9, $instance->free_space(''));
Expand Down Expand Up @@ -115,6 +251,17 @@ public function testFWriteNotEnoughSpace(): void {
$this->assertEquals('foobarqwe', $instance->file_get_contents('files/foo'));
}

public function testFWriteChunksExactlyFillQuota(): void {
$instance = $this->getLimitedStorage(6);
$stream = $instance->fopen('files/foo', 'w+');

$this->assertSame(2, fwrite($stream, 'fo'));
$this->assertSame(4, fwrite($stream, 'obar'));
fclose($stream);

$this->assertSame('foobar', $instance->file_get_contents('files/foo'));
}

public function testStreamCopyWithEnoughSpace(): void {
$instance = $this->getLimitedStorage(16);
$inputStream = fopen('data://text/plain,foobarqwerty', 'r');
Expand Down Expand Up @@ -252,6 +399,24 @@ public function testNoWriteStreamQuota(): void {
$instance->writeStream('files/test.txt', $stream);
}

public function testWriteStreamWithKnownSizeExactlyFillsQuota(): void {
$instance = $this->getLimitedStorage(6);
$stream = fopen('php://temp', 'w+');
fwrite($stream, 'foobar');
rewind($stream);

$this->assertSame(6, $instance->writeStream('files/test.txt', $stream, 6));
}

public function testWriteStreamWithoutKnownSizeExactlyFillsQuota(): void {
$instance = $this->getLimitedStorage(6);
$stream = fopen('php://temp', 'w+');
fwrite($stream, 'foobar');
rewind($stream);

$this->assertSame(6, $instance->writeStream('files/test.txt', $stream));
}

public function testWriteStreamAllowsUploadPathWithUnlimitedFreeSpace(): void {
$storage = $this->getMockBuilder(Local::class)
->onlyMethods(['free_space'])
Expand Down
Loading