diff --git a/lib/private/Files/Storage/Wrapper/Quota.php b/lib/private/Files/Storage/Wrapper/Quota.php index 86811f675f15a..f0904bbb87d8c 100644 --- a/lib/private/Files/Storage/Wrapper/Quota.php +++ b/lib/private/Files/Storage/Wrapper/Quota.php @@ -76,23 +76,62 @@ 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 + && $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] @@ -100,8 +139,9 @@ 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; @@ -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; @@ -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; @@ -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; @@ -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; @@ -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)) { @@ -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) { @@ -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(); diff --git a/tests/lib/Files/Storage/Wrapper/QuotaTest.php b/tests/lib/Files/Storage/Wrapper/QuotaTest.php index df9d6f6dd92f7..441a96b756b5c 100644 --- a/tests/lib/Files/Storage/Wrapper/QuotaTest.php +++ b/tests/lib/Files/Storage/Wrapper/QuotaTest.php @@ -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; @@ -21,7 +20,6 @@ /** * Class QuotaTest * - * * @package Test\Files\Storage\Wrapper */ #[\PHPUnit\Framework\Attributes\Group('DB')] @@ -61,6 +59,42 @@ 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')); @@ -68,6 +102,108 @@ public function testCopyNotEnoughSpace(): void { $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('')); @@ -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'); @@ -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'])