Skip to content

Commit

Permalink
- Repair broken checksums
Browse files Browse the repository at this point in the history
- Improve description
  • Loading branch information
IljaN committed Apr 5, 2018
1 parent 1960690 commit aa499f9
Showing 1 changed file with 28 additions and 16 deletions.
44 changes: 28 additions & 16 deletions apps/files/lib/Command/VerifyChecksums.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,18 @@ class VerifyChecksums extends Command {
protected function configure() {
$this
->setName('files:checksums:verify')
->setDescription('Get all checksums in filecache and compares them by recalculating the checksum of the file. (Might take very long)')
->addOption('repair', 'r', InputOption::VALUE_NONE, "Reset cacheentry with missmatched checksums. They will be recalculated on first download.")
->setDescription(
"Get all checksums in filecache and compares them by recalculating the checksum of the file.\n" .
"If you have a lot of (large) files this operation might take very long because each file is read in to memory.\n".
"Use path and user options to narrow down the files which should be checked.\n".
"Note: Files without checksum are ignored"
)
->addOption('repair', 'r', InputOption::VALUE_NONE, "Repair filecache-entry with missmatched checksums.")
->addOption('user', 'u', InputOption::VALUE_REQUIRED, 'Specific user to check')
->addOption('path', 'p', InputOption::VALUE_REQUIRED, "Path to check relative to data e.g /john/files/", '');
}


private static function calculateActualChecksums($path, IStorage $storage) {
return sprintf(
'SHA1:%s MD5:%s ADLER32:%s',
$storage->hash('sha1', $path),
$storage->hash('md5', $path),
$storage->hash('adler32', $path)
);
}

public function execute(InputInterface $input, OutputInterface $output) {

$scanFunction = function(IUser $user) use ($input, $output) {
Expand All @@ -75,6 +71,8 @@ public function execute(InputInterface $input, OutputInterface $output) {
$currentChecksums = $file->getChecksum();
$output->writeln("$path => $currentChecksums", OutputInterface::VERBOSITY_VERBOSE);
} catch (\Exception $ex) {
$output->writeln("$path => Not in cache yet", OutputInterface::VERBOSITY_VERBOSE);

return;

}
Expand All @@ -90,7 +88,7 @@ public function execute(InputInterface $input, OutputInterface $output) {
$actualChecksums = self::calculateActualChecksums($pathWithoutUid, $file->getStorage());

if ($actualChecksums !== $currentChecksums) {
$this->fieIdsWithBrokenChecksums[] = $file;
$this->fieIdsWithBrokenChecksums[] = ['file' => $file, 'correctChecksums' => $actualChecksums];
$output->writeln(
"<info>Missmatch for $path:\n Filecache:\t$currentChecksums\n Actual:\t$actualChecksums</info>"
);
Expand Down Expand Up @@ -122,20 +120,24 @@ public function execute(InputInterface $input, OutputInterface $output) {

if (!empty($this->fieIdsWithBrokenChecksums)) {
if ($input->getOption('repair') || $questionHelper->ask($input, $output, $repairQuestion)) {
self::resetChecksumsForFileIds(array_unique($this->fieIdsWithBrokenChecksums));
self::repairChecksumsForFiles($this->fieIdsWithBrokenChecksums);
return;
}
}
}


/**
* @param $files
*/
private static function resetChecksumsForFileIds(array $files) {
private static function repairChecksumsForFiles(array $files) {
foreach ($files as $file) {
$storage = $file->getStorage();
$storage = $file['file']->getStorage();
$cache = $storage->getCache();
$cache->update($file->getId(), ['checksum' => '']);
$cache->update(
$file['file']->getId(),
['checksum' => $file['correctChecksums']]
);
}
}

Expand All @@ -160,4 +162,14 @@ protected function reconnectToDatabase(OutputInterface $output) {
}
return $connection;
}


private static function calculateActualChecksums($path, IStorage $storage) {
return sprintf(
'SHA1:%s MD5:%s ADLER32:%s',
$storage->hash('sha1', $path),
$storage->hash('md5', $path),
$storage->hash('adler32', $path)
);
}
}

0 comments on commit aa499f9

Please sign in to comment.