diff --git a/lib/Service/DocumentService.php b/lib/Service/DocumentService.php index 43533caafb1..ba4a3452f69 100644 --- a/lib/Service/DocumentService.php +++ b/lib/Service/DocumentService.php @@ -341,27 +341,34 @@ public function getSteps(int $documentId, int $lastVersion): array { * @throws InvalidPathException * @throws NotFoundException */ - public function assertNoOutsideConflict(Document $document, File $file, bool $force = false, ?string $shareToken = null): void { + public function assertNoOutsideConflict(Document $document, File $file, bool $force = false, ?string $shareToken = null): Document { $documentId = $document->getId(); - $lastMTime = $document->getLastSavedVersionTime(); - $lastEtag = $document->getLastSavedVersionEtag(); - if ($lastMTime <= 0 || $force || $this->isReadOnly($file, $shareToken) || $this->cache->get('document-save-lock-' . $documentId)) { - return; + if ($document->getLastSavedVersionTime() <= 0 || $force || $this->isReadOnly($file, $shareToken) || $this->cache->get('document-save-lock-' . $documentId)) { + return $document; } $fileChecksum = self::computeCheckSum($file->getContent()); if ($document->getChecksum() !== $fileChecksum) { - throw new DocumentSaveConflictException('File changed in the meantime from outside'); + // Another save by Text may have finished after this request loaded the document. + $stored = $this->getDocument($documentId); + if ($stored === null) { + throw new NotFoundException('Document was reset in the meantime'); + } + if ($stored->getChecksum() !== $fileChecksum) { + throw new DocumentSaveConflictException('File changed in the meantime from outside'); + } + $document = $stored; } $fileMtime = $file->getMtime(); $fileEtag = $file->getEtag(); - if ($lastEtag !== $fileEtag || $lastMTime !== $fileMtime) { + if ($document->getLastSavedVersionEtag() !== $fileEtag || $document->getLastSavedVersionTime() !== $fileMtime) { $document->setLastSavedVersionTime($fileMtime); $document->setLastSavedVersionEtag($fileEtag); $this->documentMapper->update($document); } + return $document; } /** @@ -387,7 +394,13 @@ public function autosave(Document $document, File $file, int $version, string $a throw new NotPermittedException('Read-only permission cannot save document changes. Please reload the page.'); } - $this->assertNoOutsideConflict($document, $file, $force); + // Only save once every AUTOSAVE_MINIMUM_DELAY seconds + $lastMTime = $document->getLastSavedVersionTime(); + if ($lastMTime > time() - self::AUTOSAVE_MINIMUM_DELAY && $manualSave === false) { + return $document; + } + + $document = $this->assertNoOutsideConflict($document, $file, $force); // Abort autosave if already saving. if ($this->cache->get('document-save-lock-' . $documentId) && $manualSave === false) { @@ -404,12 +417,6 @@ public function autosave(Document $document, File $file, int $version, string $a return $document; } - // Only save once every AUTOSAVE_MINIMUM_DELAY seconds - $lastMTime = $document->getLastSavedVersionTime(); - if ($file->getMTime() === $lastMTime && $lastMTime > time() - self::AUTOSAVE_MINIMUM_DELAY && $manualSave === false) { - return $document; - } - if (empty($autoSaveDocument)) { $this->logger->warning('Saving empty document', [ 'requestVersion' => $version, @@ -442,15 +449,17 @@ public function autosave(Document $document, File $file, int $version, string $a $file, ILock::TYPE_APP, Application::APP_NAME - ), function () use ($file, $autoSaveDocument, $documentState) { + ), function () use ($document, $file, $autoSaveDocument, $documentState) { $this->saveFromText = true; $file->putContent($autoSaveDocument); + // Store the new checksum first so a failure below cannot leave it outdated. + $document->setLastSavedVersionTime($file->getMTime()); + $document->setLastSavedVersionEtag($file->getEtag()); + $document->setChecksum(self::computeCheckSum($autoSaveDocument)); + $this->documentMapper->update($document); $this->writeDocumentState($file->getId(), $documentState); }); $document->setLastSavedVersion($version); - $document->setLastSavedVersionTime($file->getMTime()); - $document->setLastSavedVersionEtag($file->getEtag()); - $document->setChecksum(self::computeCheckSum($autoSaveDocument)); $this->documentMapper->update($document); } catch (LockedException $e) { // Ignore lock since it might occur when multiple people save at the same time diff --git a/tests/unit/Service/DocumentServiceTest.php b/tests/unit/Service/DocumentServiceTest.php index 2e0adc402f1..71c10fa3033 100644 --- a/tests/unit/Service/DocumentServiceTest.php +++ b/tests/unit/Service/DocumentServiceTest.php @@ -7,7 +7,9 @@ use OCA\Text\Db\Session; use OCA\Text\Db\SessionMapper; use OCA\Text\Db\StepMapper; +use OCA\Text\Exception\DocumentSaveConflictException; use OCA\Text\Service\DocumentService; +use OCP\AppFramework\Db\DoesNotExistException; use OCP\Constants; use OCP\Files\Config\IUserMountCache; use OCP\Files\File; @@ -15,10 +17,15 @@ use OCP\Files\IAppData; use OCP\Files\IRootFolder; use OCP\Files\Lock\ILockManager; +use OCP\Files\Lock\LockContext; +use OCP\Files\NotFoundException; use OCP\Files\NotPermittedException; +use OCP\Files\SimpleFS\ISimpleFile; +use OCP\Files\SimpleFS\ISimpleFolder; use OCP\ICacheFactory; use OCP\IConfig; use OCP\IRequest; +use OCP\Lock\LockedException; use OCP\Share\IManager; use OCP\Share\IShare; use Psr\Log\LoggerInterface; @@ -203,6 +210,7 @@ private function createDocumentWithContent(string $content): Document { private function createFile(string $content): File { $file = $this->createMock(File::class); $file->method('getId')->willReturn(42); + $file->method('isUpdateable')->willReturn(true); $file->method('getContent')->willReturn($content); $file->method('getMTime')->willReturn(1000); $file->method('getEtag')->willReturn('etag'); @@ -239,4 +247,95 @@ public function testResetsDocumentWhenFileChangedOutside(): void { self::assertNotSame($loaded, $document); self::assertSame(DocumentService::computeCheckSum('changed outside'), $document->getChecksum()); } + + public function testNoConflictIfTextSavedInTheMeantime(): void { + $document = $this->createDocumentWithContent('old content'); + $document->setLastSavedVersionTime(1000); + $document->setLastSavedVersionEtag('etag'); + $document->resetUpdatedFields(); + $stored = $this->createDocumentWithContent('saved by text'); + $stored->setLastSavedVersion(5); + $stored->setLastSavedVersionTime(1000); + $stored->setLastSavedVersionEtag('etag'); + $file = $this->createFile('saved by text'); + $this->documentMapper->method('find')->with(42)->willReturn($stored); + $this->setpMapper->method('getLatestVersion')->willReturn(5); + $file->expects(self::never())->method('putContent'); + + $result = $this->documentService->autosave($document, $file, 4, 'new content', 'doc state'); + self::assertSame($stored, $result); + } + + public function testConflictIfFileChangedOutside(): void { + $document = $this->createDocumentWithContent('old content'); + $document->setLastSavedVersionTime(1000); + $document->resetUpdatedFields(); + $stored = $this->createDocumentWithContent('old content'); + $file = $this->createFile('outside content'); + $this->documentMapper->method('find')->with(42)->willReturn($stored); + $file->expects(self::never())->method('putContent'); + + $this->expectException(DocumentSaveConflictException::class); + $this->documentService->autosave($document, $file, 1, 'new content', 'doc state'); + } + + public function testNotFoundIfDocumentWasResetInTheMeantime(): void { + $document = $this->createDocumentWithContent('old content'); + $document->setLastSavedVersionTime(1000); + $document->resetUpdatedFields(); + $file = $this->createFile('outside content'); + $this->documentMapper->method('find')->with(42) + ->willThrowException(new DoesNotExistException('Document was reset')); + $file->expects(self::never())->method('putContent'); + + $this->expectException(NotFoundException::class); + $this->documentService->autosave($document, $file, 1, 'new content', 'doc state'); + } + + public function testStoresChecksumIfWritingTheStateFails(): void { + $document = $this->createDocumentWithContent('old content'); + $document->setLastSavedVersionTime(1000); + $document->setLastSavedVersionEtag('etag'); + $document->resetUpdatedFields(); + $file = $this->createFile('old content'); + $this->setpMapper->method('getLatestVersion')->willReturn(4); + $this->lockManager->method('runInScope')->willReturnCallback( + static fn (LockContext $lock, callable $callback) => $callback() + ); + $stateFile = $this->createMock(ISimpleFile::class); + $stateFile->method('putContent')->willThrowException(new LockedException('documents/42.yjs')); + $folder = $this->createMock(ISimpleFolder::class); + $folder->method('getFile')->willReturn($stateFile); + $this->appData->method('getFolder')->willReturn($folder); + $file->expects(self::once())->method('putContent')->with('new content'); + $this->documentMapper->expects(self::once())->method('update')->with($document); + + $this->documentService->autosave($document, $file, 4, 'new content', 'doc state'); + self::assertSame(DocumentService::computeCheckSum('new content'), $document->getChecksum()); + } + + public function testThrottledAutosaveSkipsConflictCheck(): void { + $document = $this->createDocumentWithContent('content'); + $document->setLastSavedVersionTime(time()); + $document->resetUpdatedFields(); + $file = $this->createFile('outside content'); + $file->expects(self::never())->method('getContent'); + $file->expects(self::never())->method('putContent'); + + $result = $this->documentService->autosave($document, $file, 3, 'new content', 'doc state'); + self::assertSame($document, $result); + } + + public function testManualSaveChecksForConflictsWithinAutosaveDelay(): void { + $document = $this->createDocumentWithContent('old content'); + $document->setLastSavedVersionTime(time()); + $document->resetUpdatedFields(); + $stored = $this->createDocumentWithContent('old content'); + $file = $this->createFile('outside content'); + $this->documentMapper->method('find')->with(42)->willReturn($stored); + $file->expects(self::never())->method('putContent'); + + $this->expectException(DocumentSaveConflictException::class); + $this->documentService->autosave($document, $file, 3, 'new content', 'doc state', false, true); + } }