diff --git a/apps/files_trashbin/lib/Trashbin.php b/apps/files_trashbin/lib/Trashbin.php index e21da9b8cd0b3..58d27b734f116 100644 --- a/apps/files_trashbin/lib/Trashbin.php +++ b/apps/files_trashbin/lib/Trashbin.php @@ -296,16 +296,70 @@ public static function move2trash($file_path, $ownerOnly = false) { $configuredTrashbinSize = static::getConfiguredTrashbinSize($owner); if ($configuredTrashbinSize >= 0 && $sourceInfo->getSize() >= $configuredTrashbinSize) { + $trashStorage->releaseLock($trashInternalPath, ILockingProvider::LOCK_EXCLUSIVE, $lockingProvider); return false; } + // there is still a possibility that the file has been deleted by a remote user + $deletedBy = self::overwriteDeletedBy($user); + + $deleteTrashRow = static function () use ($owner, $filename, $timestamp): void { + $query = Server::get(IDBConnection::class)->getQueryBuilder(); + $query->delete('files_trash') + ->where($query->expr()->eq('user', $query->createNamedParameter($owner))) + ->andWhere($query->expr()->eq('id', $query->createNamedParameter($filename))) + ->andWhere($query->expr()->eq('timestamp', $query->createNamedParameter($timestamp))); + $query->executeStatement(); + }; + + $query = Server::get(IDBConnection::class)->getQueryBuilder(); + $query->insert('files_trash') + ->setValue('id', $query->createNamedParameter($filename)) + ->setValue('timestamp', $query->createNamedParameter($timestamp)) + ->setValue('location', $query->createNamedParameter($location)) + ->setValue('user', $query->createNamedParameter($owner)) + ->setValue('deleted_by', $query->createNamedParameter($deletedBy)); + $inserted = false; try { - $moveSuccessful = true; + $inserted = ($query->executeStatement() === 1); + } catch (\Throwable $e) { + Server::get(LoggerInterface::class)->error( + 'trash bin database insert failed', + [ + 'app' => 'files_trashbin', + 'exception' => $e, + 'user' => $owner, + 'filename' => $filename, + 'timestamp' => $timestamp, + ] + ); + } + if (!$inserted) { + Server::get(LoggerInterface::class)->error( + 'trash bin database couldn\'t be updated, skipping trash move', + [ + 'app' => 'files_trashbin', + 'user' => $owner, + 'filename' => $filename, + 'timestamp' => $timestamp, + ] + ); + $trashStorage->releaseLock($trashInternalPath, ILockingProvider::LOCK_EXCLUSIVE, $lockingProvider); + return false; + } + $moveSuccessful = true; + try { $inCache = $sourceStorage->getCache()->inCache($sourceInternalPath); $trashStorage->moveFromStorage($sourceStorage, $sourceInternalPath, $trashInternalPath); if ($inCache) { $trashStorage->getUpdater()->renameFromStorage($sourceStorage, $sourceInternalPath, $trashInternalPath); + } else { + $sizeDifference = $sourceInfo->getSize(); + if ($sizeDifference < 0) { + $sizeDifference = null; + } + $trashStorage->getUpdater()->update($trashInternalPath, null, $sizeDifference); } } catch (CopyRecursiveException $e) { $moveSuccessful = false; @@ -328,24 +382,31 @@ public static function move2trash($file_path, $ownerOnly = false) { } else { $trashStorage->getUpdater()->remove($trashInternalPath); } - return false; + $moveSuccessful = false; } - if ($moveSuccessful) { - // there is still a possibility that the file has been deleted by a remote user - $deletedBy = self::overwriteDeletedBy($user); - - $query = Server::get(IDBConnection::class)->getQueryBuilder(); - $query->insert('files_trash') - ->setValue('id', $query->createNamedParameter($filename)) - ->setValue('timestamp', $query->createNamedParameter($timestamp)) - ->setValue('location', $query->createNamedParameter($location)) - ->setValue('user', $query->createNamedParameter($owner)) - ->setValue('deleted_by', $query->createNamedParameter($deletedBy)); - $result = $query->executeStatement(); - if (!$result) { - Server::get(LoggerInterface::class)->error('trash bin database couldn\'t be updated', ['app' => 'files_trashbin']); + if (!$moveSuccessful) { + Server::get(LoggerInterface::class)->error( + 'trash move failed, removing trash metadata and payload', + [ + 'app' => 'files_trashbin', + 'user' => $owner, + 'filename' => $filename, + 'timestamp' => $timestamp, + ] + ); + $deleteTrashRow(); + if ($trashStorage->file_exists($trashInternalPath)) { + if ($trashStorage->is_dir($trashInternalPath)) { + $trashStorage->rmdir($trashInternalPath); + } else { + $trashStorage->unlink($trashInternalPath); + } } + $trashStorage->getUpdater()->remove($trashInternalPath); + } + + if ($moveSuccessful) { Util::emitHook('\OCA\Files_Trashbin\Trashbin', 'post_moveToTrash', ['filePath' => Filesystem::normalizePath($file_path), 'trashPath' => Filesystem::normalizePath(static::getTrashFilename($filename, $timestamp))]); @@ -682,13 +743,6 @@ public static function delete($filename, $user, $timestamp = null) { $size = 0; if ($timestamp) { - $query = Server::get(IDBConnection::class)->getQueryBuilder(); - $query->delete('files_trash') - ->where($query->expr()->eq('user', $query->createNamedParameter($user))) - ->andWhere($query->expr()->eq('id', $query->createNamedParameter($filename))) - ->andWhere($query->expr()->eq('timestamp', $query->createNamedParameter($timestamp))); - $query->executeStatement(); - $file = static::getTrashFilename($filename, $timestamp); } else { $file = $filename; @@ -699,6 +753,14 @@ public static function delete($filename, $user, $timestamp = null) { try { $node = $userRoot->get('/files_trashbin/files/' . $file); } catch (NotFoundException $e) { + if ($timestamp) { + $query = Server::get(IDBConnection::class)->getQueryBuilder(); + $query->delete('files_trash') + ->where($query->expr()->eq('user', $query->createNamedParameter($user))) + ->andWhere($query->expr()->eq('id', $query->createNamedParameter($filename))) + ->andWhere($query->expr()->eq('timestamp', $query->createNamedParameter($timestamp))); + $query->executeStatement(); + } return $size; } @@ -712,6 +774,15 @@ public static function delete($filename, $user, $timestamp = null) { $node->delete(); self::emitTrashbinPostDelete('/files_trashbin/files/' . $file); + if ($timestamp) { + $query = Server::get(IDBConnection::class)->getQueryBuilder(); + $query->delete('files_trash') + ->where($query->expr()->eq('user', $query->createNamedParameter($user))) + ->andWhere($query->expr()->eq('id', $query->createNamedParameter($filename))) + ->andWhere($query->expr()->eq('timestamp', $query->createNamedParameter($timestamp))); + $query->executeStatement(); + } + return $size; } diff --git a/apps/files_trashbin/tests/StorageTest.php b/apps/files_trashbin/tests/StorageTest.php index 3b8441a412443..c0f218fabff30 100644 --- a/apps/files_trashbin/tests/StorageTest.php +++ b/apps/files_trashbin/tests/StorageTest.php @@ -118,6 +118,25 @@ public function testSingleStorageDeleteFile(): void { $this->assertEquals('test.txt', substr($name, 0, strrpos($name, '.'))); } + public function testTrashEntryCreatedWhenSourceNotInCache(): void { + $this->userView->file_put_contents('uncached.txt', 'foo'); + + [$storage, $internalPath] = $this->userView->resolvePath('uncached.txt'); + $cache = $storage->getCache(); + $cache->remove($internalPath); + $this->assertFalse($cache->inCache($internalPath)); + + $this->userView->unlink('uncached.txt'); + + $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files/'); + $this->assertCount(1, $results); + $name = $results[0]->getName(); + $this->assertEquals('uncached.txt', substr($name, 0, strrpos($name, '.'))); + + [$trashStorage, $trashInternalPath] = $this->rootView->resolvePath('/' . $this->user . '/files_trashbin/files/' . $name); + $this->assertTrue($trashStorage->getCache()->inCache($trashInternalPath)); + } + /** * Test that deleting a folder puts it into the trashbin. */