Skip to content

Commit

Permalink
Add "increment-range" option
Browse files Browse the repository at this point in the history
  • Loading branch information
IljaN committed May 28, 2021
1 parent 3d0cd77 commit 280dd47
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions lib/Command/FixEncryptedVersion.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
/**
* @author Sujith Haridasan <[email protected]>
* @author Ilja Neumann <[email protected]>
*
* @copyright Copyright (c) 2019, ownCloud GmbH
* @license AGPL-3.0
Expand Down Expand Up @@ -62,6 +63,12 @@ protected function configure() {
'p',
InputArgument::OPTIONAL,
'Limit files to fix with path, e.g., --path="/Music/Artist". If path indicates a directory, all the files inside directory will be fixed.'
)->addOption(
'increment-range',
'i',
InputArgument::OPTIONAL,
'Try versions within a given upper bound',
5
);
}

Expand Down Expand Up @@ -91,16 +98,17 @@ protected function execute(InputInterface $input, OutputInterface $output) {
$output->writeln("<error>User id $user does not exist. Please provide a valid user id</error>");
return 1;
}
return $this->walkPathOfUser($user, $pathToWalk, $output);
return $this->walkPathOfUser($user, $pathToWalk, $output, $input->getOption('increment-range'));
}

/**
* @param string $user
* @param string $path
* @param OutputInterface $output
* @param $incrRange int Range of versions to try (upper/lower bound)
* @return int 0 for success, 1 for error
*/
private function walkPathOfUser($user, $path, OutputInterface $output) {
private function walkPathOfUser($user, $path, OutputInterface $output, $incrRange) {
$this->setupUserFs($user);
if (!$this->view->file_exists($path)) {
$output->writeln("<error>Path $path does not exist. Please provide a valid path.</error>");
Expand All @@ -109,7 +117,7 @@ private function walkPathOfUser($user, $path, OutputInterface $output) {

if ($this->view->is_file($path)) {
$output->writeln("Verifying the content of file $path");
$this->verifyFileContent($path, $output);
$this->verifyFileContent($path, $output, $incrRange);
return 0;
}
$directories = [];
Expand All @@ -122,7 +130,7 @@ private function walkPathOfUser($user, $path, OutputInterface $output) {
$directories[] = $path;
} else {
$output->writeln("Verifying the content of file $path");
$this->verifyFileContent($path, $output);
$this->verifyFileContent($path, $output, $incrRange);
}
}
}
Expand All @@ -134,7 +142,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, $incrRange, $ignoreCorrectEncVersionCall = true) {
try {
/**
* In encryption, the files are read in a block size of 8192 bytes
Expand All @@ -158,7 +166,7 @@ private function verifyFileContent($path, OutputInterface $output, $ignoreCorrec
if ($ignoreCorrectEncVersionCall === true) {
//Lets rectify the file by correcting encrypted version
$output->writeln("<info>Attempting to fix the path: $path</info>");
return $this->correctEncryptedVersion($path, $output);
return $this->correctEncryptedVersion($path, $output, $incrRange);
}
return false;
}
Expand All @@ -169,7 +177,7 @@ private function verifyFileContent($path, OutputInterface $output, $ignoreCorrec
* @param OutputInterface $output
* @return bool
*/
private function correctEncryptedVersion($path, OutputInterface $output) {
private function correctEncryptedVersion($path, OutputInterface $output, $incrRange) {
$fileInfo = $this->view->getFileInfo($path);
$fileId = $fileInfo->getId();
$encryptedVersion = $fileInfo->getEncryptedVersion();
Expand All @@ -192,7 +200,7 @@ private function correctEncryptedVersion($path, OutputInterface $output) {
$cacheInfo = ['encryptedVersion' => $encryptedVersion, 'encrypted' => $encryptedVersion];
$cache->put($fileCache->getPath(), $cacheInfo);
$output->writeln("<info>Decrement the encrypted version to $encryptedVersion</info>");
if ($this->verifyFileContent($path, $output, false) === true) {
if ($this->verifyFileContent($path, $output, $incrRange, false) === true) {
$output->writeln("<info>Fixed the file: $path with version " . $encryptedVersion . "</info>");
return true;
}
Expand All @@ -201,7 +209,7 @@ private function correctEncryptedVersion($path, OutputInterface $output) {

//So decrementing did not work. Now lets increment. Max increment is till 5
$increment = 1;
while ($increment <= 5) {
while ($increment <= $incrRange) {
/**
* The wrongEncryptedVersion would not be incremented so nothing to worry about here.
* Only the newEncryptedVersion is incremented.
Expand All @@ -215,7 +223,7 @@ private function correctEncryptedVersion($path, OutputInterface $output) {
$cacheInfo = ['encryptedVersion' => $newEncryptedVersion, 'encrypted' => $newEncryptedVersion];
$cache->put($fileCache->getPath(), $cacheInfo);
$output->writeln("<info>Increment the encrypted version to $newEncryptedVersion</info>");
if ($this->verifyFileContent($path, $output, false) === true) {
if ($this->verifyFileContent($path, $output, $incrRange, false) === true) {
$output->writeln("<info>Fixed the file: $path with version " . $newEncryptedVersion . "</info>");
return true;
}
Expand Down

0 comments on commit 280dd47

Please sign in to comment.