Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix warnings in FixEncryptedVersion command
Fixed code warnings

Signed-off-by: Vincent Petry <[email protected]>
  • Loading branch information
PVince81 authored and backportbot[bot] committed Jun 30, 2021
commit bf279980ac5f7c30cfa6300dd85f48791b79f32a
39 changes: 26 additions & 13 deletions apps/encryption/lib/Command/FixEncryptedVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use OC\HintException;
use OCP\Files\IRootFolder;
use OCP\IConfig;
use OCP\ILogger;
use OCP\IUserManager;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
Expand All @@ -36,6 +37,9 @@ class FixEncryptedVersion extends Command {
/** @var IConfig */
private $config;

/** @var ILogger */
private $logger;

/** @var IRootFolder */
private $rootFolder;

Expand All @@ -45,15 +49,16 @@ class FixEncryptedVersion extends Command {
/** @var View */
private $view;

public function __construct(IConfig $config, IRootFolder $rootFolder, IUserManager $userManager, View $view) {
public function __construct(IConfig $config, ILogger $logger, IRootFolder $rootFolder, IUserManager $userManager, View $view) {
$this->config = $config;
$this->logger = $logger;
$this->rootFolder = $rootFolder;
$this->userManager = $userManager;
$this->view = $view;
parent::__construct();
}

protected function configure() {
protected function configure(): void {
parent::configure();

$this
Expand All @@ -76,15 +81,15 @@ protected function configure() {
* @param OutputInterface $output
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output) {
protected function execute(InputInterface $input, OutputInterface $output): int {
$skipSignatureCheck = $this->config->getSystemValue('encryption_skip_signature_check', false);

if ($skipSignatureCheck) {
$output->writeln("<error>Repairing is not possible when \"encryption_skip_signature_check\" is set. Please disable this flag in the configuration.</error>\n");
return 1;
}

$user = $input->getArgument('user');
$user = (string)$input->getArgument('user');
$pathToWalk = "/$user/files";

/**
Expand Down Expand Up @@ -113,7 +118,7 @@ protected function execute(InputInterface $input, OutputInterface $output) {
* @param OutputInterface $output
* @return int 0 for success, 1 for error
*/
private function walkPathOfUser($user, $path, OutputInterface $output) {
private function walkPathOfUser($user, $path, OutputInterface $output): int {
$this->setupUserFs($user);
if (!$this->view->file_exists($path)) {
$output->writeln("<error>Path $path does not exist. Please provide a valid path.</error>");
Expand Down Expand Up @@ -147,7 +152,7 @@ private function walkPathOfUser($user, $path, OutputInterface $output) {
* @param OutputInterface $output
* @param bool $ignoreCorrectEncVersionCall, setting this variable to false avoids recursion
*/
private function verifyFileContent($path, OutputInterface $output, $ignoreCorrectEncVersionCall = true) {
private function verifyFileContent($path, OutputInterface $output, $ignoreCorrectEncVersionCall = true): bool {
try {
/**
* In encryption, the files are read in a block size of 8192 bytes
Expand All @@ -166,7 +171,7 @@ private function verifyFileContent($path, OutputInterface $output, $ignoreCorrec

return true;
} catch (HintException $e) {
\OC::$server->getLogger()->warning("Issue: " . $e->getMessage());
$this->logger->warning("Issue: " . $e->getMessage());
//If allowOnce is set to false, this becomes recursive.
if ($ignoreCorrectEncVersionCall === true) {
//Lets rectify the file by correcting encrypted version
Expand All @@ -182,8 +187,12 @@ private function verifyFileContent($path, OutputInterface $output, $ignoreCorrec
* @param OutputInterface $output
* @return bool
*/
private function correctEncryptedVersion($path, OutputInterface $output) {
private function correctEncryptedVersion($path, OutputInterface $output): bool {
$fileInfo = $this->view->getFileInfo($path);
if (!$fileInfo) {
$output->writeln("<warning>File info not found for file: \"$path\"</warning>");
return true;
}
$fileId = $fileInfo->getId();
$encryptedVersion = $fileInfo->getEncryptedVersion();
$wrongEncryptedVersion = $encryptedVersion;
Expand All @@ -192,9 +201,13 @@ private function correctEncryptedVersion($path, OutputInterface $output) {

$cache = $storage->getCache();
$fileCache = $cache->get($fileId);
if (!$fileCache) {
$output->writeln("<warning>File cache entry not found for file: \"$path\"</warning>");
return true;
}

if ($storage->instanceOfStorage('OCA\Files_Sharing\ISharedStorage')) {
$output->writeln("<info>The file: $path is a share. Hence kindly fix this by running the script for the owner of share</info>");
$output->writeln("<info>The file: \"$path\" is a share. Please also run the script for the owner of the share</info>");
return true;
}

Expand All @@ -208,7 +221,7 @@ private function correctEncryptedVersion($path, OutputInterface $output) {
$cache->put($fileCache->getPath(), $cacheInfo);
$output->writeln("<info>Decrement the encrypted version to $encryptedVersion</info>");
if ($this->verifyFileContent($path, $output, false) === true) {
$output->writeln("<info>Fixed the file: $path with version " . $encryptedVersion . "</info>");
$output->writeln("<info>Fixed the file: \"$path\" with version " . $encryptedVersion . "</info>");
return true;
}
$encryptedVersion--;
Expand All @@ -231,7 +244,7 @@ private function correctEncryptedVersion($path, OutputInterface $output) {
$cache->put($fileCache->getPath(), $cacheInfo);
$output->writeln("<info>Increment the encrypted version to $newEncryptedVersion</info>");
if ($this->verifyFileContent($path, $output, false) === true) {
$output->writeln("<info>Fixed the file: $path with version " . $newEncryptedVersion . "</info>");
$output->writeln("<info>Fixed the file: \"$path\" with version " . $newEncryptedVersion . "</info>");
return true;
}
$increment++;
Expand All @@ -240,7 +253,7 @@ private function correctEncryptedVersion($path, OutputInterface $output) {

$cacheInfo = ['encryptedVersion' => $originalEncryptedVersion, 'encrypted' => $originalEncryptedVersion];
$cache->put($fileCache->getPath(), $cacheInfo);
$output->writeln("<info>No fix found for $path, restored version to original: $originalEncryptedVersion</info>");
$output->writeln("<info>No fix found for \"$path\", restored version to original: $originalEncryptedVersion</info>");

return false;
}
Expand All @@ -249,7 +262,7 @@ private function correctEncryptedVersion($path, OutputInterface $output) {
* Setup user file system
* @param string $uid
*/
private function setupUserFs($uid) {
private function setupUserFs($uid): void {
\OC_Util::tearDownFS();
\OC_Util::setupFS($uid);
}
Expand Down