From 36fc5dc8ae989f0694ca97c9175ee0debbc36142 Mon Sep 17 00:00:00 2001 From: =?utf8?q?C=C3=B4me=20Chilliet?= Date: Thu, 11 May 2023 11:24:16 +0200 Subject: [PATCH] Copy data back instead of renaming to avoid changing the fileid MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- apps/encryption/lib/Command/FixLegacyFileKey.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/apps/encryption/lib/Command/FixLegacyFileKey.php b/apps/encryption/lib/Command/FixLegacyFileKey.php index 7ea28f2babe..7b0b4ccfb73 100644 --- a/apps/encryption/lib/Command/FixLegacyFileKey.php +++ b/apps/encryption/lib/Command/FixLegacyFileKey.php @@ -128,8 +128,17 @@ class FixLegacyFileKey extends Command { try { $this->rootView->copy($source, $target); - $this->rootView->touch($target, $fileInfo->getMTime()); - $this->rootView->rename($target, $source); + $copyResource = $this->rootView->fopen($target, 'r'); + $sourceResource = $this->rootView->fopen($source, 'w'); + if ($copyResource === false || $sourceResource === false) { + throw new DecryptionFailedException('Failed to open '.$source.' or '.$target); + } + if (stream_copy_to_stream($copyResource, $sourceResource) === false) { + $output->writeln('Failed to copy '.$target.' data into '.$source.''); + $output->writeln('Leaving both files in there to avoid data loss'); + return; + } + $this->rootView->touch($source, $fileInfo->getMTime()); $output->writeln('Migrated ' . $source . '', OutputInterface::VERBOSITY_VERBOSE); } catch (DecryptionFailedException $e) { if ($this->rootView->file_exists($target)) { -- 2.39.5