2017-03-22 23:30:44 +03:00
|
|
|
<?php
|
2017-11-06 17:56:42 +03:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*
|
2019-12-19 15:16:31 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Daniel Kesselberg <mail@danielkesselberg.de>
|
2020-08-24 15:54:25 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Joel S <joel.devbox@protonmail.com>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2017-11-06 17:56:42 +03:00
|
|
|
*
|
|
|
|
*/
|
2019-11-22 22:52:10 +03:00
|
|
|
|
2017-03-22 23:30:44 +03:00
|
|
|
namespace OCA\Files\Command;
|
|
|
|
|
|
|
|
use Doctrine\DBAL\Connection;
|
|
|
|
use OC\Core\Command\Base;
|
|
|
|
use OC\Core\Command\InterruptedException;
|
|
|
|
use OC\ForbiddenException;
|
2019-12-11 13:27:05 +03:00
|
|
|
use OCP\EventDispatcher\IEventDispatcher;
|
2017-03-22 23:30:44 +03:00
|
|
|
use OCP\Files\IRootFolder;
|
|
|
|
use OCP\Files\NotFoundException;
|
|
|
|
use OCP\Files\StorageNotAvailableException;
|
|
|
|
use OCP\IConfig;
|
|
|
|
use OCP\IDBConnection;
|
2019-11-22 22:52:10 +03:00
|
|
|
use Symfony\Component\Console\Helper\Table;
|
2020-04-22 14:27:33 +03:00
|
|
|
use Symfony\Component\Console\Input\InputArgument;
|
2017-03-22 23:30:44 +03:00
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
|
|
|
|
class ScanAppData extends Base {
|
|
|
|
|
|
|
|
/** @var IRootFolder */
|
|
|
|
protected $root;
|
|
|
|
/** @var IConfig */
|
|
|
|
protected $config;
|
|
|
|
/** @var float */
|
|
|
|
protected $execTime = 0;
|
|
|
|
/** @var int */
|
|
|
|
protected $foldersCounter = 0;
|
|
|
|
/** @var int */
|
|
|
|
protected $filesCounter = 0;
|
|
|
|
|
|
|
|
public function __construct(IRootFolder $rootFolder, IConfig $config) {
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->root = $rootFolder;
|
|
|
|
$this->config = $config;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function configure() {
|
|
|
|
parent::configure();
|
|
|
|
|
|
|
|
$this
|
2017-03-23 00:59:55 +03:00
|
|
|
->setName('files:scan-app-data')
|
2018-10-21 23:06:44 +03:00
|
|
|
->setDescription('rescan the AppData folder');
|
2020-04-22 14:27:33 +03:00
|
|
|
|
|
|
|
$this->addArgument('folder', InputArgument::OPTIONAL, 'The appdata subfolder to scan', '');
|
2017-03-22 23:30:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function checkScanWarning($fullPath, OutputInterface $output) {
|
|
|
|
$normalizedPath = basename(\OC\Files\Filesystem::normalizePath($fullPath));
|
|
|
|
$path = basename($fullPath);
|
|
|
|
|
|
|
|
if ($normalizedPath !== $path) {
|
|
|
|
$output->writeln("\t<error>Entry \"" . $fullPath . '" will not be accessible due to incompatible encoding</error>');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-26 21:37:21 +03:00
|
|
|
protected function scanFiles(OutputInterface $output, string $folder): int {
|
2017-03-22 23:30:44 +03:00
|
|
|
try {
|
|
|
|
$appData = $this->getAppDataFolder();
|
|
|
|
} catch (NotFoundException $e) {
|
2020-04-26 21:37:21 +03:00
|
|
|
$output->writeln('<error>NoAppData folder found</error>');
|
|
|
|
return 1;
|
2017-03-22 23:30:44 +03:00
|
|
|
}
|
|
|
|
|
2020-04-22 14:27:33 +03:00
|
|
|
if ($folder !== '') {
|
|
|
|
try {
|
|
|
|
$appData = $appData->get($folder);
|
|
|
|
} catch (NotFoundException $e) {
|
2020-04-26 21:37:21 +03:00
|
|
|
$output->writeln('<error>Could not find folder: ' . $folder . '</error>');
|
|
|
|
return 1;
|
2020-04-22 14:27:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-22 23:30:44 +03:00
|
|
|
$connection = $this->reconnectToDatabase($output);
|
2019-12-11 13:27:05 +03:00
|
|
|
$scanner = new \OC\Files\Utils\Scanner(null, $connection, \OC::$server->query(IEventDispatcher::class), \OC::$server->getLogger());
|
2018-10-21 23:06:44 +03:00
|
|
|
|
2017-03-22 23:30:44 +03:00
|
|
|
# check on each file/folder if there was a user interrupt (ctrl-c) and throw an exception
|
2018-10-21 23:06:44 +03:00
|
|
|
$scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output) {
|
2018-10-22 15:29:49 +03:00
|
|
|
$output->writeln("\tFile <info>$path</info>", OutputInterface::VERBOSITY_VERBOSE);
|
2018-10-21 23:06:44 +03:00
|
|
|
++$this->filesCounter;
|
|
|
|
$this->abortIfInterrupted();
|
|
|
|
});
|
|
|
|
|
|
|
|
$scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function ($path) use ($output) {
|
2018-10-22 15:29:49 +03:00
|
|
|
$output->writeln("\tFolder <info>$path</info>", OutputInterface::VERBOSITY_VERBOSE);
|
2018-10-21 23:06:44 +03:00
|
|
|
++$this->foldersCounter;
|
|
|
|
$this->abortIfInterrupted();
|
|
|
|
});
|
|
|
|
|
|
|
|
$scanner->listen('\OC\Files\Utils\Scanner', 'StorageNotAvailable', function (StorageNotAvailableException $e) use ($output) {
|
|
|
|
$output->writeln('Error while scanning, storage not available (' . $e->getMessage() . ')', OutputInterface::VERBOSITY_VERBOSE);
|
|
|
|
});
|
|
|
|
|
|
|
|
$scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output) {
|
2017-03-22 23:30:44 +03:00
|
|
|
$this->checkScanWarning($path, $output);
|
|
|
|
});
|
2018-10-21 23:06:44 +03:00
|
|
|
|
|
|
|
$scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function ($path) use ($output) {
|
2017-03-22 23:30:44 +03:00
|
|
|
$this->checkScanWarning($path, $output);
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
$scanner->scan($appData->getPath());
|
|
|
|
} catch (ForbiddenException $e) {
|
2018-10-21 23:06:44 +03:00
|
|
|
$output->writeln('<error>Storage not writable</error>');
|
2020-04-26 21:37:21 +03:00
|
|
|
$output->writeln('<info>Make sure you\'re running the scan command only as the user the web server runs as</info>');
|
|
|
|
return 1;
|
2017-03-22 23:30:44 +03:00
|
|
|
} catch (InterruptedException $e) {
|
|
|
|
# exit the function if ctrl-c has been pressed
|
2020-04-26 21:37:21 +03:00
|
|
|
$output->writeln('<info>Interrupted by user</info>');
|
|
|
|
return 1;
|
2017-04-20 01:04:16 +03:00
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
$output->writeln('<error>Path not found: ' . $e->getMessage() . '</error>');
|
2020-04-26 21:37:21 +03:00
|
|
|
return 1;
|
2017-03-22 23:30:44 +03:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
$output->writeln('<error>Exception during scan: ' . $e->getMessage() . '</error>');
|
|
|
|
$output->writeln('<error>' . $e->getTraceAsString() . '</error>');
|
2020-04-26 21:37:21 +03:00
|
|
|
return 1;
|
2017-03-22 23:30:44 +03:00
|
|
|
}
|
2020-04-26 21:37:21 +03:00
|
|
|
|
|
|
|
return 0;
|
2017-03-22 23:30:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-26 16:12:11 +03:00
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int {
|
2017-03-22 23:30:44 +03:00
|
|
|
# restrict the verbosity level to VERBOSITY_VERBOSE
|
2017-03-23 00:59:55 +03:00
|
|
|
if ($output->getVerbosity() > OutputInterface::VERBOSITY_VERBOSE) {
|
2017-03-22 23:30:44 +03:00
|
|
|
$output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE);
|
|
|
|
}
|
|
|
|
|
2020-04-26 21:37:21 +03:00
|
|
|
$output->writeln('Scanning AppData for files');
|
|
|
|
$output->writeln('');
|
2017-03-22 23:30:44 +03:00
|
|
|
|
2020-04-22 14:27:33 +03:00
|
|
|
$folder = $input->getArgument('folder');
|
|
|
|
|
2017-03-22 23:30:44 +03:00
|
|
|
$this->initTools();
|
|
|
|
|
2020-04-26 21:37:21 +03:00
|
|
|
$exitCode = $this->scanFiles($output, $folder);
|
|
|
|
if ($exitCode === 0) {
|
|
|
|
$this->presentStats($output);
|
|
|
|
}
|
|
|
|
return $exitCode;
|
2017-03-22 23:30:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialises some useful tools for the Command
|
|
|
|
*/
|
|
|
|
protected function initTools() {
|
|
|
|
// Start the timer
|
|
|
|
$this->execTime = -microtime(true);
|
|
|
|
// Convert PHP errors to exceptions
|
|
|
|
set_error_handler([$this, 'exceptionErrorHandler'], E_ALL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Processes PHP errors as exceptions in order to be able to keep track of problems
|
|
|
|
*
|
|
|
|
* @see https://secure.php.net/manual/en/function.set-error-handler.php
|
|
|
|
*
|
|
|
|
* @param int $severity the level of the error raised
|
|
|
|
* @param string $message
|
|
|
|
* @param string $file the filename that the error was raised in
|
|
|
|
* @param int $line the line number the error was raised
|
|
|
|
*
|
|
|
|
* @throws \ErrorException
|
|
|
|
*/
|
|
|
|
public function exceptionErrorHandler($severity, $message, $file, $line) {
|
|
|
|
if (!(error_reporting() & $severity)) {
|
|
|
|
// This error code is not included in error_reporting
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
throw new \ErrorException($message, 0, $severity, $file, $line);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param OutputInterface $output
|
|
|
|
*/
|
|
|
|
protected function presentStats(OutputInterface $output) {
|
|
|
|
// Stop the timer
|
|
|
|
$this->execTime += microtime(true);
|
|
|
|
|
|
|
|
$headers = [
|
|
|
|
'Folders', 'Files', 'Elapsed time'
|
|
|
|
];
|
|
|
|
|
|
|
|
$this->showSummary($headers, null, $output);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shows a summary of operations
|
|
|
|
*
|
|
|
|
* @param string[] $headers
|
|
|
|
* @param string[] $rows
|
|
|
|
* @param OutputInterface $output
|
|
|
|
*/
|
|
|
|
protected function showSummary($headers, $rows, OutputInterface $output) {
|
|
|
|
$niceDate = $this->formatExecTime();
|
|
|
|
if (!$rows) {
|
|
|
|
$rows = [
|
|
|
|
$this->foldersCounter,
|
|
|
|
$this->filesCounter,
|
|
|
|
$niceDate,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
$table = new Table($output);
|
|
|
|
$table
|
|
|
|
->setHeaders($headers)
|
|
|
|
->setRows([$rows]);
|
|
|
|
$table->render();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Formats microtime into a human readable format
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function formatExecTime() {
|
2019-02-22 18:25:24 +03:00
|
|
|
$secs = round($this->execTime);
|
|
|
|
# convert seconds into HH:MM:SS form
|
|
|
|
return sprintf('%02d:%02d:%02d', ($secs/3600), ($secs/60%60), $secs%60);
|
2017-03-22 23:30:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return \OCP\IDBConnection
|
|
|
|
*/
|
|
|
|
protected function reconnectToDatabase(OutputInterface $output) {
|
|
|
|
/** @var Connection | IDBConnection $connection*/
|
|
|
|
$connection = \OC::$server->getDatabaseConnection();
|
|
|
|
try {
|
|
|
|
$connection->close();
|
|
|
|
} catch (\Exception $ex) {
|
|
|
|
$output->writeln("<info>Error while disconnecting from database: {$ex->getMessage()}</info>");
|
|
|
|
}
|
|
|
|
while (!$connection->isConnected()) {
|
|
|
|
try {
|
|
|
|
$connection->connect();
|
|
|
|
} catch (\Exception $ex) {
|
|
|
|
$output->writeln("<info>Error while re-connecting to database: {$ex->getMessage()}</info>");
|
|
|
|
sleep(60);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $connection;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return \OCP\Files\Folder
|
|
|
|
* @throws NotFoundException
|
|
|
|
*/
|
|
|
|
private function getAppDataFolder() {
|
|
|
|
$instanceId = $this->config->getSystemValue('instanceid', null);
|
|
|
|
|
|
|
|
if ($instanceId === null) {
|
|
|
|
throw new NotFoundException();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->root->get('appdata_'.$instanceId);
|
|
|
|
}
|
|
|
|
}
|