2013-09-01 18:40:50 +04:00
|
|
|
<?php
|
2013-09-19 21:12:16 +04:00
|
|
|
/**
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Bart Visscher <bartv@thisnet.nl>
|
|
|
|
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
|
2016-01-12 17:02:16 +03:00
|
|
|
* @author martin.mattel@diemattels.at <martin.mattel@diemattels.at>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
|
|
|
* @author Robin Appelman <icewind@owncloud.com>
|
|
|
|
* @author Vincent Petry <pvince81@owncloud.com>
|
|
|
|
*
|
2016-01-12 17:02:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
2015-03-26 13:44:34 +03: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 21:12:16 +04:00
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2013-09-01 18:40:50 +04:00
|
|
|
namespace OCA\Files\Command;
|
|
|
|
|
2014-06-25 17:22:49 +04:00
|
|
|
use OC\ForbiddenException;
|
2015-11-27 16:02:50 +03:00
|
|
|
use OCP\Files\StorageNotAvailableException;
|
2013-09-01 18:40:50 +04:00
|
|
|
use Symfony\Component\Console\Command\Command;
|
|
|
|
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 20:45:49 +03:00
|
|
|
use Symfony\Component\Console\Helper\Table;
|
2013-09-01 18:40:50 +04:00
|
|
|
|
2013-09-02 20:18:12 +04:00
|
|
|
class Scan extends Command {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \OC\User\Manager $userManager
|
|
|
|
*/
|
|
|
|
private $userManager;
|
2015-12-15 20:45:49 +03:00
|
|
|
/** @var float */
|
|
|
|
protected $execTime = 0;
|
|
|
|
/** @var int */
|
|
|
|
protected $foldersCounter = 0;
|
|
|
|
/** @var int */
|
|
|
|
protected $filesCounter = 0;
|
|
|
|
/** @var bool */
|
|
|
|
protected $interrupted = false;
|
2016-01-26 20:42:03 +03:00
|
|
|
/** @var bool */
|
|
|
|
protected $php_pcntl_signal = true;
|
|
|
|
|
2015-12-15 20:45:49 +03:00
|
|
|
|
2013-09-02 20:18:12 +04:00
|
|
|
|
|
|
|
public function __construct(\OC\User\Manager $userManager) {
|
|
|
|
$this->userManager = $userManager;
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function configure() {
|
2013-09-01 18:40:50 +04:00
|
|
|
$this
|
|
|
|
->setName('files:scan')
|
|
|
|
->setDescription('rescan filesystem')
|
|
|
|
->addArgument(
|
2014-06-25 17:22:49 +04:00
|
|
|
'user_id',
|
|
|
|
InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
|
|
|
|
'will rescan all files of the given user(s)'
|
|
|
|
)
|
2014-10-31 19:33:33 +03:00
|
|
|
->addOption(
|
|
|
|
'path',
|
2014-10-31 19:39:05 +03:00
|
|
|
'p',
|
2014-10-31 19:33:33 +03:00
|
|
|
InputArgument::OPTIONAL,
|
2014-12-10 13:04:17 +03: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 19:33:33 +03:00
|
|
|
)
|
2014-10-31 19:39:05 +03:00
|
|
|
->addOption(
|
|
|
|
'quiet',
|
|
|
|
'q',
|
|
|
|
InputOption::VALUE_NONE,
|
2015-12-15 20:45:49 +03:00
|
|
|
'suppress any output'
|
|
|
|
)
|
|
|
|
->addOption(
|
|
|
|
'verbose',
|
|
|
|
'-v|vv|vvv',
|
|
|
|
InputOption::VALUE_NONE,
|
|
|
|
'verbose the output'
|
2014-10-31 19:39:05 +03:00
|
|
|
)
|
2013-09-01 18:40:50 +04:00
|
|
|
->addOption(
|
2014-06-25 17:22:49 +04:00
|
|
|
'all',
|
|
|
|
null,
|
|
|
|
InputOption::VALUE_NONE,
|
|
|
|
'will rescan all files of all known users'
|
|
|
|
);
|
2013-09-01 18:40:50 +04:00
|
|
|
}
|
|
|
|
|
2015-12-15 20:45:49 +03:00
|
|
|
protected function scanFiles($user, $path, $verbose, OutputInterface $output) {
|
2015-11-27 16:02:50 +03:00
|
|
|
$scanner = new \OC\Files\Utils\Scanner($user, \OC::$server->getDatabaseConnection(), \OC::$server->getLogger());
|
2016-01-26 20:42:03 +03:00
|
|
|
# check on each file/folder if there was a user interrupt (ctrl-c) and throw an exeption
|
2015-12-15 20:45:49 +03:00
|
|
|
# printout and count
|
|
|
|
if ($verbose) {
|
2014-10-31 19:39:05 +03:00
|
|
|
$scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output) {
|
2016-01-26 20:42:03 +03:00
|
|
|
$output->writeln("\tFile <info>$path</info>");
|
2015-12-15 20:45:49 +03:00
|
|
|
$this->filesCounter += 1;
|
2016-01-26 20:42:03 +03:00
|
|
|
if ($this->hasBeenInterrupted()) {
|
|
|
|
throw new \Exception('ctrl-c');
|
|
|
|
}
|
2014-10-31 19:39:05 +03:00
|
|
|
});
|
|
|
|
$scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function ($path) use ($output) {
|
2016-01-26 20:42:03 +03:00
|
|
|
$output->writeln("\tFolder <info>$path</info>");
|
2015-12-15 20:45:49 +03:00
|
|
|
$this->foldersCounter += 1;
|
2016-01-26 20:42:03 +03:00
|
|
|
if ($this->hasBeenInterrupted()) {
|
|
|
|
throw new \Exception('ctrl-c');
|
|
|
|
}
|
2014-10-31 19:39:05 +03:00
|
|
|
});
|
2015-11-27 16:02:50 +03:00
|
|
|
$scanner->listen('\OC\Files\Utils\Scanner', 'StorageNotAvailable', function (StorageNotAvailableException $e) use ($output) {
|
|
|
|
$output->writeln("Error while scanning, storage not available (" . $e->getMessage() . ")");
|
|
|
|
});
|
2015-12-15 20:45:49 +03:00
|
|
|
# count only
|
|
|
|
} else {
|
|
|
|
$scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output) {
|
|
|
|
$this->filesCounter += 1;
|
2016-01-26 20:42:03 +03:00
|
|
|
if ($this->hasBeenInterrupted()) {
|
|
|
|
throw new \Exception('ctrl-c');
|
|
|
|
}
|
2015-12-15 20:45:49 +03:00
|
|
|
});
|
|
|
|
$scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function ($path) use ($output) {
|
|
|
|
$this->foldersCounter += 1;
|
2016-01-26 20:42:03 +03:00
|
|
|
if ($this->hasBeenInterrupted()) {
|
|
|
|
throw new \Exception('ctrl-c');
|
|
|
|
}
|
2015-12-15 20:45:49 +03:00
|
|
|
});
|
2014-10-31 19:39:05 +03:00
|
|
|
}
|
2015-12-15 20:45:49 +03:00
|
|
|
|
2014-06-25 17:22:49 +04:00
|
|
|
try {
|
2014-10-31 19:33:33 +03:00
|
|
|
$scanner->scan($path);
|
2014-06-25 17:22:49 +04:00
|
|
|
} catch (ForbiddenException $e) {
|
|
|
|
$output->writeln("<error>Home storage for user $user not writable</error>");
|
|
|
|
$output->writeln("Make sure you're running the scan command only as the user the web server runs as");
|
2016-01-26 20:42:03 +03:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
# exit the function if ctrl-c has been pressed
|
|
|
|
return;
|
2014-06-25 17:22:49 +04:00
|
|
|
}
|
2013-09-01 18:40:50 +04:00
|
|
|
}
|
|
|
|
|
2015-12-15 20:45:49 +03:00
|
|
|
|
2013-09-02 20:18:12 +04:00
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) {
|
2015-07-15 15:08:06 +03:00
|
|
|
$inputPath = $input->getOption('path');
|
|
|
|
if ($inputPath) {
|
|
|
|
$inputPath = '/' . trim($inputPath, '/');
|
|
|
|
list (, $user,) = explode('/', $inputPath, 3);
|
2014-12-10 13:04:17 +03:00
|
|
|
$users = array($user);
|
|
|
|
} else if ($input->getOption('all')) {
|
2013-09-02 20:18:12 +04:00
|
|
|
$users = $this->userManager->search('');
|
2013-09-01 18:40:50 +04:00
|
|
|
} else {
|
|
|
|
$users = $input->getArgument('user_id');
|
|
|
|
}
|
2014-12-10 13:04:17 +03:00
|
|
|
|
2015-12-15 20:45:49 +03:00
|
|
|
# no messaging level option means: no full printout but statistics
|
|
|
|
# $quiet means no print at all
|
|
|
|
# $verbose means full printout including statistics
|
|
|
|
# -q -v full stat
|
|
|
|
# 0 0 no yes
|
|
|
|
# 0 1 yes yes
|
|
|
|
# 1 -- no no (quiet overrules verbose)
|
|
|
|
$verbose = $input->getOption('verbose');
|
|
|
|
$quiet = $input->getOption('quiet');
|
|
|
|
# restrict the verbosity level to VERBOSITY_VERBOSE
|
|
|
|
if ($output->getVerbosity()>OutputInterface::VERBOSITY_VERBOSE) {
|
|
|
|
$output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE);
|
|
|
|
}
|
|
|
|
if ($quiet) {
|
|
|
|
$verbose = false;
|
|
|
|
}
|
|
|
|
|
2016-01-26 20:42:03 +03:00
|
|
|
# check quantity of users to be process and show it on the command line
|
|
|
|
$users_total = count($users);
|
|
|
|
if ($users_total === 0) {
|
|
|
|
$output->writeln("<error>Please specify the user id to scan, \"--all\" to scan for all users or \"--path=...\"</error>");
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
if ($users_total > 1) {
|
|
|
|
$output->writeln("\nScanning files for $users_total users");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-15 20:45:49 +03:00
|
|
|
$this->initTools();
|
|
|
|
|
2016-01-26 20:42:03 +03:00
|
|
|
$user_count = 0;
|
2013-09-01 18:40:50 +04:00
|
|
|
foreach ($users as $user) {
|
2013-09-02 20:18:12 +04:00
|
|
|
if (is_object($user)) {
|
|
|
|
$user = $user->getUID();
|
|
|
|
}
|
2015-07-15 15:08:06 +03:00
|
|
|
$path = $inputPath ? $inputPath : '/' . $user;
|
2016-01-26 20:42:03 +03:00
|
|
|
$user_count += 1;
|
2014-09-24 17:48:54 +04:00
|
|
|
if ($this->userManager->userExists($user)) {
|
2016-01-26 20:42:03 +03:00
|
|
|
# add an extra line when verbose is set to optical seperate users
|
|
|
|
if ($verbose) {$output->writeln(""); }
|
|
|
|
$output->writeln("Starting scan for user $user_count out of $users_total ($user)");
|
2015-12-15 20:45:49 +03:00
|
|
|
# full: printout data if $verbose was set
|
|
|
|
$this->scanFiles($user, $path, $verbose, $output);
|
2014-09-24 17:48:54 +04:00
|
|
|
} else {
|
2016-01-26 20:42:03 +03:00
|
|
|
$output->writeln("<error>Unknown user $user_count $user</error>");
|
|
|
|
}
|
|
|
|
# check on each user if there was a user interrupt (ctrl-c) and exit foreach
|
|
|
|
if ($this->hasBeenInterrupted()) {
|
|
|
|
break;
|
2014-09-24 17:48:54 +04:00
|
|
|
}
|
2013-09-01 18:40:50 +04:00
|
|
|
}
|
2015-12-15 20:45:49 +03:00
|
|
|
|
|
|
|
# stat: printout statistics if $quiet was not set
|
|
|
|
if (!$quiet) {
|
|
|
|
$this->presentStats($output);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
|
2016-01-26 20:42:03 +03:00
|
|
|
// check if the php pcntl_signal functions are accessible
|
|
|
|
if (function_exists('pcntl_signal')) {
|
|
|
|
// Collect interrupts and notify the running command
|
|
|
|
pcntl_signal(SIGTERM, [$this, 'cancelOperation']);
|
|
|
|
pcntl_signal(SIGINT, [$this, 'cancelOperation']);
|
|
|
|
} else {
|
|
|
|
$this->php_pcntl_signal = false;
|
|
|
|
}
|
2015-12-15 20:45:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2016-01-26 20:42:03 +03:00
|
|
|
* Changes the status of the command to "interrupted" if ctrl-c has been pressed
|
2015-12-15 20:45:49 +03:00
|
|
|
*
|
|
|
|
* Gives a chance to the command to properly terminate what it's doing
|
|
|
|
*/
|
|
|
|
private function cancelOperation() {
|
|
|
|
$this->interrupted = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-26 20:42:03 +03:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function hasBeenInterrupted() {
|
|
|
|
// return always false if pcntl_signal functions are not accessible
|
|
|
|
if ($this->php_pcntl_signal) {
|
|
|
|
pcntl_signal_dispatch();
|
|
|
|
if ($this->interrupted) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-15 20:45:49 +03:00
|
|
|
/**
|
|
|
|
* 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 18:40:50 +04:00
|
|
|
}
|
2015-12-15 20:45:49 +03:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param OutputInterface $output
|
|
|
|
*/
|
|
|
|
protected function presentStats(OutputInterface $output) {
|
|
|
|
// Stop the timer
|
|
|
|
$this->execTime += microtime(true);
|
|
|
|
$output->writeln("");
|
|
|
|
|
|
|
|
$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() {
|
|
|
|
list($secs, $tens) = explode('.', sprintf("%.1f", ($this->execTime)));
|
2016-01-26 20:42:03 +03:00
|
|
|
# add the following to $niceDate if you want to have microsecons added: . '.' . $tens;
|
|
|
|
$niceDate = date('H:i:s', $secs);
|
2015-12-15 20:45:49 +03:00
|
|
|
|
|
|
|
return $niceDate;
|
|
|
|
}
|
|
|
|
|
2013-09-01 18:40:50 +04:00
|
|
|
}
|