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
16 changes: 16 additions & 0 deletions apps/dav/lib/Connector/Sabre/QuotaPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,22 @@ public function beforeMove(string $sourcePath, string $destinationPath): bool {
return true;
}

// When moving a file within the same storage, net space consumed on that storage is zero.
// Bypassing the quota check prevents false InsufficientStorage exceptions when free space
// on the disk is smaller than the file being moved.
try {
if ($sourceNode instanceof Node) {
$sourceStorage = $sourceNode->getFileInfo()?->getStorage();
$destInfo = $this->view->getFileInfo(ltrim($quotaPath, '/'));
$destStorage = $destInfo?->getStorage();
if ($sourceStorage !== null && $destStorage !== null && $sourceStorage->getId() === $destStorage->getId()) {
return true;
}
}
} catch (\Throwable) {
// Fallback to regular quota check if storage detection fails
}

return $this->checkQuota($quotaPath, $sourceNode->getSize());
}

Expand Down
95 changes: 95 additions & 0 deletions apps/dav/tests/unit/Connector/Sabre/QuotaPluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,101 @@ public function testCheckQuotaOnPath(int $quota, array $headers): void {
$this->assertTrue($result);
}

public function testBeforeMoveSameStorageSkipsQuota(): void {
$sourceStorage = $this->createMock(\OCP\Files\Storage\IStorage::class);
$sourceStorage->method('getId')->willReturn('storage1');

$sourceFileInfo = $this->createMock(FileInfo::class);
$sourceFileInfo->method('getStorage')->willReturn($sourceStorage);

$sourceNode = $this->createMock(\OCA\DAV\Connector\Sabre\File::class);
$sourceNode->method('getFileInfo')->willReturn($sourceFileInfo);
$sourceNode->method('getSize')->willReturn(1024 * 1024 * 1024);

$destStorage = $this->createMock(\OCP\Files\Storage\IStorage::class);
$destStorage->method('getId')->willReturn('storage1');

$destFileInfo = $this->createMock(FileInfo::class);
$destFileInfo->method('getStorage')->willReturn($destStorage);

$parentNode = $this->createMock(\OCA\DAV\Connector\Sabre\Directory::class);
$parentNode->method('getPath')->willReturn('dest_folder');

$tree = $this->createMock(\Sabre\DAV\Tree::class);
$tree->method('getNodeForPath')
->willReturnMap([
['source_file', $sourceNode],
['dest_folder', $parentNode],
]);
$tree->method('nodeExists')
->with('dest_folder/target_file')
->willReturn(false);

$view = $this->getMockBuilder(View::class)
->onlyMethods(['free_space', 'getFileInfo'])
->disableOriginalConstructor()
->getMock();
$view->method('free_space')->willReturn(100);
$view->method('getFileInfo')->with('dest_folder')->willReturn($destFileInfo);

$server = new \Sabre\DAV\Server();
$server->tree = $tree;

$plugin = new QuotaPlugin($view);
$plugin->initialize($server);

$result = $plugin->beforeMove('source_file', 'dest_folder/target_file');
$this->assertTrue($result);
}

public function testBeforeMoveDifferentStorageExceedsQuota(): void {
$this->expectException(\Sabre\DAV\Exception\InsufficientStorage::class);

$sourceStorage = $this->createMock(\OCP\Files\Storage\IStorage::class);
$sourceStorage->method('getId')->willReturn('storage1');

$sourceFileInfo = $this->createMock(FileInfo::class);
$sourceFileInfo->method('getStorage')->willReturn($sourceStorage);

$sourceNode = $this->createMock(\OCA\DAV\Connector\Sabre\File::class);
$sourceNode->method('getFileInfo')->willReturn($sourceFileInfo);
$sourceNode->method('getSize')->willReturn(1024 * 1024 * 1024);

$destStorage = $this->createMock(\OCP\Files\Storage\IStorage::class);
$destStorage->method('getId')->willReturn('storage2');

$destFileInfo = $this->createMock(FileInfo::class);
$destFileInfo->method('getStorage')->willReturn($destStorage);

$parentNode = $this->createMock(\OCA\DAV\Connector\Sabre\Directory::class);
$parentNode->method('getPath')->willReturn('dest_folder');

$tree = $this->createMock(\Sabre\DAV\Tree::class);
$tree->method('getNodeForPath')
->willReturnMap([
['source_file', $sourceNode],
['dest_folder', $parentNode],
]);
$tree->method('nodeExists')
->with('dest_folder/target_file')
->willReturn(false);

$view = $this->getMockBuilder(View::class)
->onlyMethods(['free_space', 'getFileInfo'])
->disableOriginalConstructor()
->getMock();
$view->method('free_space')->willReturn(100);
$view->method('getFileInfo')->with('dest_folder')->willReturn($destFileInfo);

$server = new \Sabre\DAV\Server();
$server->tree = $tree;

$plugin = new QuotaPlugin($view);
$plugin->initialize($server);

$plugin->beforeMove('source_file', 'dest_folder/target_file');
}

public static function quotaOkayProvider(): array {
return [
[1024, []],
Expand Down