summaryrefslogtreecommitdiffstats
path: root/apps
diff options
context:
space:
mode:
authorCôme Chilliet <come.chilliet@nextcloud.com>2023-05-09 11:52:36 +0200
committerCôme Chilliet <come.chilliet@nextcloud.com>2023-05-09 12:02:57 +0200
commit725403cb0da4a78e0c0a28feb22e1bb8d1e2430b (patch)
tree60711f88bdfc7de3f0696d8e7fc003e3d65d7494 /apps
parentc9c49bfef8ff39eb052d9bddb796882290467c98 (diff)
downloadnextcloud-server-725403cb0da4a78e0c0a28feb22e1bb8d1e2430b.tar.gz
nextcloud-server-725403cb0da4a78e0c0a28feb22e1bb8d1e2430b.zip
Copy and move files to migrate them to the new key
We have to rewrite the header, so the whole file needs to be rewritten, so we just use the same strategy as DecryptAll. Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
Diffstat (limited to 'apps')
-rw-r--r--apps/encryption/lib/Command/FixLegacyFileKey.php33
1 files changed, 21 insertions, 12 deletions
diff --git a/apps/encryption/lib/Command/FixLegacyFileKey.php b/apps/encryption/lib/Command/FixLegacyFileKey.php
index 7a17e637d41..7ea28f2babe 100644
--- a/apps/encryption/lib/Command/FixLegacyFileKey.php
+++ b/apps/encryption/lib/Command/FixLegacyFileKey.php
@@ -26,6 +26,8 @@ declare(strict_types=1);
namespace OCA\Encryption\Command;
+use OC\Encryption\Exceptions\DecryptionFailedException;
+use OC\Files\FileInfo;
use OC\Files\View;
use OCA\Encryption\KeyManager;
use OCP\Encryption\Exceptions\GenericEncryptionException;
@@ -112,18 +114,7 @@ class FixLegacyFileKey extends Command {
/* If that did not throw and filekey is not empty, a legacy filekey is used */
$clean = false;
$output->writeln($path . ' is using a legacy filekey, migrating');
- $file = $this->rootView->fopen($path, 'r+');
- if ($file) {
- $firstByte = fread($file, 1);
- if ($firstByte === false) {
- $output->writeln('<error>failed to read ' . $path . '</error>');
- continue;
- }
- fwrite($file, $firstByte);
- fclose($file);
- } else {
- $output->writeln('<error>failed to open ' . $path . '</error>');
- }
+ $this->migrateSinglefile($path, $item, $output);
}
}
}
@@ -131,6 +122,24 @@ class FixLegacyFileKey extends Command {
return $clean;
}
+ private function migrateSinglefile(string $path, FileInfo $fileInfo, OutputInterface $output): void {
+ $source = $path;
+ $target = $path . '.reencrypted.' . time();
+
+ try {
+ $this->rootView->copy($source, $target);
+ $this->rootView->touch($target, $fileInfo->getMTime());
+ $this->rootView->rename($target, $source);
+ $output->writeln('<info>Migrated ' . $source . '</info>', OutputInterface::VERBOSITY_VERBOSE);
+ } catch (DecryptionFailedException $e) {
+ if ($this->rootView->file_exists($target)) {
+ $this->rootView->unlink($target);
+ }
+ $output->writeln('<error>Failed to migrate ' . $path . '</error>');
+ $output->writeln('<error>' . $e . '</error>', OutputInterface::VERBOSITY_VERBOSE);
+ }
+ }
+
/**
* setup user file system
*/