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>
pull/38080/head
Côme Chilliet 2023-05-09 11:52:36 +07:00
parent c9c49bfef8
commit 725403cb0d
No known key found for this signature in database
GPG Key ID: A3E2F658B28C760A
1 changed files with 21 additions and 12 deletions

@ -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
*/