2013-09-01 14:40:50 +00:00
|
|
|
<?php
|
2013-09-19 17:12:16 +00:00
|
|
|
/**
|
2016-07-21 14:49:16 +00:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Bart Visscher <bartv@thisnet.nl>
|
|
|
|
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
|
2016-01-12 14:02:16 +00:00
|
|
|
* @author martin.mattel@diemattels.at <martin.mattel@diemattels.at>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-07-21 16:13:36 +00:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2016-03-01 16:25:15 +00:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
2016-05-26 17:56:05 +00:00
|
|
|
* @author Vincent Petry <pvince81@owncloud.com>
|
2015-03-26 10:44:34 +00:00
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* 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, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
2013-09-19 17:12:16 +00:00
|
|
|
*/
|
2015-02-26 10:37:37 +00:00
|
|
|
|
2013-09-01 14:40:50 +00:00
|
|
|
namespace OCA\Files\Command;
|
|
|
|
|
2016-08-18 12:18:02 +00:00
|
|
|
use Doctrine\DBAL\Connection;
|
2016-01-29 14:59:57 +00:00
|
|
|
use OC\Core\Command\Base;
|
2017-02-10 14:24:25 +00:00
|
|
|
use OC\Core\Command\InterruptedException;
|
2014-06-25 13:22:49 +00:00
|
|
|
use OC\ForbiddenException;
|
2018-06-28 14:09:25 +00:00
|
|
|
use OCP\Files\Mount\IMountPoint;
|
2017-04-19 12:36:38 +00:00
|
|
|
use OCP\Files\NotFoundException;
|
2015-11-27 13:02:50 +00:00
|
|
|
use OCP\Files\StorageNotAvailableException;
|
2016-08-18 12:18:02 +00:00
|
|
|
use OCP\IDBConnection;
|
2016-01-29 14:59:57 +00:00
|
|
|
use OCP\IUserManager;
|
2013-09-01 14:40:50 +00:00
|
|
|
use Symfony\Component\Console\Input\InputArgument;
|
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
use Symfony\Component\Console\Input\InputOption;
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
2015-12-15 17:45:49 +00:00
|
|
|
use Symfony\Component\Console\Helper\Table;
|
2013-09-01 14:40:50 +00:00
|
|
|
|
2016-01-29 14:59:57 +00:00
|
|
|
class Scan extends Base {
|
2013-09-02 16:18:12 +00:00
|
|
|
|
2016-01-29 14:59:57 +00:00
|
|
|
/** @var IUserManager $userManager */
|
2013-09-02 16:18:12 +00:00
|
|
|
private $userManager;
|
2015-12-15 17:45:49 +00:00
|
|
|
/** @var float */
|
|
|
|
protected $execTime = 0;
|
|
|
|
/** @var int */
|
|
|
|
protected $foldersCounter = 0;
|
|
|
|
/** @var int */
|
|
|
|
protected $filesCounter = 0;
|
2013-09-02 16:18:12 +00:00
|
|
|
|
2016-01-29 14:59:57 +00:00
|
|
|
public function __construct(IUserManager $userManager) {
|
2013-09-02 16:18:12 +00:00
|
|
|
$this->userManager = $userManager;
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function configure() {
|
2016-01-29 14:59:57 +00:00
|
|
|
parent::configure();
|
|
|
|
|
2013-09-01 14:40:50 +00:00
|
|
|
$this
|
|
|
|
->setName('files:scan')
|
|
|
|
->setDescription('rescan filesystem')
|
|
|
|
->addArgument(
|
2014-06-25 13:22:49 +00:00
|
|
|
'user_id',
|
|
|
|
InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
|
|
|
|
'will rescan all files of the given user(s)'
|
|
|
|
)
|
2014-10-31 16:33:33 +00:00
|
|
|
->addOption(
|
|
|
|
'path',
|
2014-10-31 16:39:05 +00:00
|
|
|
'p',
|
2014-10-31 16:33:33 +00:00
|
|
|
InputArgument::OPTIONAL,
|
2014-12-10 10:04:17 +00:00
|
|
|
'limit rescan to this path, eg. --path="/alice/files/Music", the user_id is determined by the path and the user_id parameter and --all are ignored'
|
2014-10-31 16:33:33 +00:00
|
|
|
)
|
2013-09-01 14:40:50 +00:00
|
|
|
->addOption(
|
2014-06-25 13:22:49 +00:00
|
|
|
'all',
|
|
|
|
null,
|
|
|
|
InputOption::VALUE_NONE,
|
|
|
|
'will rescan all files of all known users'
|
2016-05-18 13:29:37 +00:00
|
|
|
)->addOption(
|
|
|
|
'unscanned',
|
|
|
|
null,
|
|
|
|
InputOption::VALUE_NONE,
|
|
|
|
'only scan files which are marked as not fully scanned'
|
2018-05-18 21:50:32 +00:00
|
|
|
)->addOption(
|
|
|
|
'shallow',
|
|
|
|
null,
|
|
|
|
InputOption::VALUE_NONE,
|
|
|
|
'do not scan folders recursively'
|
2018-06-28 14:09:25 +00:00
|
|
|
)->addOption(
|
|
|
|
'home-only',
|
|
|
|
null,
|
|
|
|
InputOption::VALUE_NONE,
|
|
|
|
'only scan the home storage, ignoring any mounted external storage or share'
|
2014-06-25 13:22:49 +00:00
|
|
|
);
|
2013-09-01 14:40:50 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 08:43:07 +00: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>');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-11 18:51:35 +00:00
|
|
|
protected function scanFiles($user, $path, OutputInterface $output, $backgroundScan = false, $recursive = true, $homeOnly = false) {
|
2016-08-18 12:18:02 +00:00
|
|
|
$connection = $this->reconnectToDatabase($output);
|
|
|
|
$scanner = new \OC\Files\Utils\Scanner($user, $connection, \OC::$server->getLogger());
|
2018-10-14 16:38:23 +00:00
|
|
|
|
2016-01-29 14:59:57 +00:00
|
|
|
# check on each file/folder if there was a user interrupt (ctrl-c) and throw an exception
|
2018-10-11 18:51:35 +00:00
|
|
|
|
|
|
|
$scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output) {
|
|
|
|
$output->writeln("\tFile\t<info>$path</info>", OutputInterface::VERBOSITY_VERBOSE);
|
|
|
|
++$this->filesCounter;
|
|
|
|
$this->abortIfInterrupted();
|
|
|
|
});
|
|
|
|
|
|
|
|
$scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function ($path) use ($output) {
|
|
|
|
$output->writeln("\tFolder\t<info>$path</info>", OutputInterface::VERBOSITY_VERBOSE);
|
|
|
|
++$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);
|
|
|
|
});
|
|
|
|
|
2017-04-19 12:36:38 +00:00
|
|
|
$scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output) {
|
2016-04-29 08:43:07 +00:00
|
|
|
$this->checkScanWarning($path, $output);
|
|
|
|
});
|
2018-10-11 18:51:35 +00:00
|
|
|
|
2017-04-19 12:36:38 +00:00
|
|
|
$scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function ($path) use ($output) {
|
2016-04-29 08:43:07 +00:00
|
|
|
$this->checkScanWarning($path, $output);
|
|
|
|
});
|
2015-12-15 17:45:49 +00:00
|
|
|
|
2014-06-25 13:22:49 +00:00
|
|
|
try {
|
2016-05-18 13:29:37 +00:00
|
|
|
if ($backgroundScan) {
|
|
|
|
$scanner->backgroundScan($path);
|
2017-04-19 12:36:38 +00:00
|
|
|
} else {
|
2018-06-28 14:09:25 +00:00
|
|
|
$scanner->scan($path, $recursive, $homeOnly ? [$this, 'filterHomeMount'] : null);
|
2016-05-18 13:29:37 +00:00
|
|
|
}
|
2014-06-25 13:22:49 +00:00
|
|
|
} catch (ForbiddenException $e) {
|
|
|
|
$output->writeln("<error>Home storage for user $user not writable</error>");
|
2018-10-07 16:17:29 +00:00
|
|
|
$output->writeln('Make sure you\'re running the scan command only as the user the web server runs as');
|
2017-02-10 14:24:25 +00:00
|
|
|
} catch (InterruptedException $e) {
|
|
|
|
# exit the function if ctrl-c has been pressed
|
|
|
|
$output->writeln('Interrupted by user');
|
2017-04-19 12:36:38 +00:00
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
$output->writeln('<error>Path not found: ' . $e->getMessage() . '</error>');
|
2016-01-26 17:42:03 +00:00
|
|
|
} catch (\Exception $e) {
|
2017-02-10 14:24:25 +00:00
|
|
|
$output->writeln('<error>Exception during scan: ' . $e->getMessage() . '</error>');
|
|
|
|
$output->writeln('<error>' . $e->getTraceAsString() . '</error>');
|
2014-06-25 13:22:49 +00:00
|
|
|
}
|
2013-09-01 14:40:50 +00:00
|
|
|
}
|
|
|
|
|
2018-06-28 14:09:25 +00:00
|
|
|
public function filterHomeMount(IMountPoint $mountPoint) {
|
|
|
|
// any mountpoint inside '/$user/files/'
|
|
|
|
return substr_count($mountPoint->getMountPoint(), '/') <= 3;
|
|
|
|
}
|
2015-12-15 17:45:49 +00:00
|
|
|
|
2013-09-02 16:18:12 +00:00
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) {
|
2015-07-15 12:08:06 +00:00
|
|
|
$inputPath = $input->getOption('path');
|
|
|
|
if ($inputPath) {
|
|
|
|
$inputPath = '/' . trim($inputPath, '/');
|
|
|
|
list (, $user,) = explode('/', $inputPath, 3);
|
2014-12-10 10:04:17 +00:00
|
|
|
$users = array($user);
|
|
|
|
} else if ($input->getOption('all')) {
|
2013-09-02 16:18:12 +00:00
|
|
|
$users = $this->userManager->search('');
|
2013-09-01 14:40:50 +00:00
|
|
|
} else {
|
|
|
|
$users = $input->getArgument('user_id');
|
|
|
|
}
|
2014-12-10 10:04:17 +00:00
|
|
|
|
2015-12-15 17:45:49 +00:00
|
|
|
# restrict the verbosity level to VERBOSITY_VERBOSE
|
2017-04-19 12:36:38 +00:00
|
|
|
if ($output->getVerbosity() > OutputInterface::VERBOSITY_VERBOSE) {
|
2015-12-15 17:45:49 +00:00
|
|
|
$output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE);
|
|
|
|
}
|
|
|
|
|
2016-01-26 17:42:03 +00:00
|
|
|
# check quantity of users to be process and show it on the command line
|
|
|
|
$users_total = count($users);
|
|
|
|
if ($users_total === 0) {
|
2018-10-11 18:51:35 +00:00
|
|
|
$output->writeln('<error>Please specify the user id to scan, --all to scan for all users or --path=...</error>');
|
2016-01-26 17:42:03 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-12-15 17:45:49 +00:00
|
|
|
$this->initTools();
|
|
|
|
|
2016-01-26 17:42:03 +00:00
|
|
|
$user_count = 0;
|
2013-09-01 14:40:50 +00:00
|
|
|
foreach ($users as $user) {
|
2013-09-02 16:18:12 +00:00
|
|
|
if (is_object($user)) {
|
|
|
|
$user = $user->getUID();
|
|
|
|
}
|
2015-07-15 12:08:06 +00:00
|
|
|
$path = $inputPath ? $inputPath : '/' . $user;
|
2018-10-11 18:51:35 +00:00
|
|
|
++$user_count;
|
2014-09-24 13:48:54 +00:00
|
|
|
if ($this->userManager->userExists($user)) {
|
2016-01-26 17:42:03 +00:00
|
|
|
$output->writeln("Starting scan for user $user_count out of $users_total ($user)");
|
2018-10-11 18:51:35 +00:00
|
|
|
$this->scanFiles($user, $path, $output, $input->getOption('unscanned'), !$input->getOption('shallow'), $input->getOption('home-only'));
|
|
|
|
$output->writeln('', OutputInterface::VERBOSITY_VERBOSE);
|
2014-09-24 13:48:54 +00:00
|
|
|
} else {
|
2016-01-26 17:42:03 +00:00
|
|
|
$output->writeln("<error>Unknown user $user_count $user</error>");
|
2018-10-11 18:51:35 +00:00
|
|
|
$output->writeln('', OutputInterface::VERBOSITY_VERBOSE);
|
2016-01-26 17:42:03 +00:00
|
|
|
}
|
2018-10-07 16:17:29 +00:00
|
|
|
|
|
|
|
try {
|
2018-10-08 11:05:00 +00:00
|
|
|
$this->abortIfInterrupted();
|
2018-10-11 18:51:35 +00:00
|
|
|
} catch (InterruptedException $e) {
|
2016-01-26 17:42:03 +00:00
|
|
|
break;
|
2014-09-24 13:48:54 +00:00
|
|
|
}
|
2013-09-01 14:40:50 +00:00
|
|
|
}
|
2015-12-15 17:45:49 +00:00
|
|
|
|
2018-10-11 18:51:35 +00:00
|
|
|
$this->presentStats($output);
|
2015-12-15 17:45:49 +00: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);
|
2013-09-01 14:40:50 +00:00
|
|
|
}
|
2015-12-15 17:45:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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 14:29:03 +00:00
|
|
|
$secs = round($this->execTime);
|
|
|
|
# convert seconds into HH:MM:SS form
|
2019-02-22 15:25:24 +00:00
|
|
|
return sprintf('%02d:%02d:%02d', ($secs/3600), ($secs/60%60), $secs%60);
|
2015-12-15 17:45:49 +00:00
|
|
|
}
|
|
|
|
|
2016-08-18 12:18:02 +00:00
|
|
|
/**
|
|
|
|
* @return \OCP\IDBConnection
|
|
|
|
*/
|
|
|
|
protected function reconnectToDatabase(OutputInterface $output) {
|
2017-04-19 12:36:38 +00:00
|
|
|
/** @var Connection | IDBConnection $connection */
|
2016-08-18 12:18:02 +00:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2013-09-01 14:40:50 +00:00
|
|
|
}
|