Skip to content
Snippets Groups Projects
Commit e05592d7 authored by martin.mattel@diemattels.at's avatar martin.mattel@diemattels.at Committed by Roeland Jago Douma
Browse files

occ scan:files Adding more details in the base print out (II)

Use proper method name

Fixed the interruption logic

Checks the availability of the pcntl_signal function

Fixed typo crtl-c --> ctrl-c

one overseen crtl-c typo
parent 1765e11f
No related branches found
No related tags found
No related merge requests found
...@@ -49,6 +49,9 @@ class Scan extends Command { ...@@ -49,6 +49,9 @@ class Scan extends Command {
protected $filesCounter = 0; protected $filesCounter = 0;
/** @var bool */ /** @var bool */
protected $interrupted = false; protected $interrupted = false;
/** @var bool */
protected $php_pcntl_signal = true;
public function __construct(\OC\User\Manager $userManager) { public function __construct(\OC\User\Manager $userManager) {
...@@ -93,15 +96,22 @@ class Scan extends Command { ...@@ -93,15 +96,22 @@ class Scan extends Command {
protected function scanFiles($user, $path, $verbose, OutputInterface $output) { protected function scanFiles($user, $path, $verbose, OutputInterface $output) {
$scanner = new \OC\Files\Utils\Scanner($user, \OC::$server->getDatabaseConnection(), \OC::$server->getLogger()); $scanner = new \OC\Files\Utils\Scanner($user, \OC::$server->getDatabaseConnection(), \OC::$server->getLogger());
# check on each file/folder if there was a user interrupt (ctrl-c) and throw an exeption
# printout and count # printout and count
if ($verbose) { if ($verbose) {
$scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output) { $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output) {
$output->writeln("Scanning file <info>$path</info>"); $output->writeln("\tFile <info>$path</info>");
$this->filesCounter += 1; $this->filesCounter += 1;
if ($this->hasBeenInterrupted()) {
throw new \Exception('ctrl-c');
}
}); });
$scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function ($path) use ($output) { $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function ($path) use ($output) {
$output->writeln("Scanning folder <info>$path</info>"); $output->writeln("\tFolder <info>$path</info>");
$this->foldersCounter += 1; $this->foldersCounter += 1;
if ($this->hasBeenInterrupted()) {
throw new \Exception('ctrl-c');
}
}); });
$scanner->listen('\OC\Files\Utils\Scanner', 'StorageNotAvailable', function (StorageNotAvailableException $e) use ($output) { $scanner->listen('\OC\Files\Utils\Scanner', 'StorageNotAvailable', function (StorageNotAvailableException $e) use ($output) {
$output->writeln("Error while scanning, storage not available (" . $e->getMessage() . ")"); $output->writeln("Error while scanning, storage not available (" . $e->getMessage() . ")");
...@@ -110,9 +120,15 @@ class Scan extends Command { ...@@ -110,9 +120,15 @@ class Scan extends Command {
} else { } else {
$scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output) { $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output) {
$this->filesCounter += 1; $this->filesCounter += 1;
if ($this->hasBeenInterrupted()) {
throw new \Exception('ctrl-c');
}
}); });
$scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function ($path) use ($output) { $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function ($path) use ($output) {
$this->foldersCounter += 1; $this->foldersCounter += 1;
if ($this->hasBeenInterrupted()) {
throw new \Exception('ctrl-c');
}
}); });
} }
...@@ -121,6 +137,9 @@ class Scan extends Command { ...@@ -121,6 +137,9 @@ class Scan extends Command {
} catch (ForbiddenException $e) { } catch (ForbiddenException $e) {
$output->writeln("<error>Home storage for user $user not writable</error>"); $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"); $output->writeln("Make sure you're running the scan command only as the user the web server runs as");
} catch (\Exception $e) {
# exit the function if ctrl-c has been pressed
return;
} }
} }
...@@ -137,11 +156,6 @@ class Scan extends Command { ...@@ -137,11 +156,6 @@ class Scan extends Command {
$users = $input->getArgument('user_id'); $users = $input->getArgument('user_id');
} }
if (count($users) === 0) {
$output->writeln("<error>Please specify the user id to scan, \"--all\" to scan for all users or \"--path=...\"</error>");
return;
}
# no messaging level option means: no full printout but statistics # no messaging level option means: no full printout but statistics
# $quiet means no print at all # $quiet means no print at all
# $verbose means full printout including statistics # $verbose means full printout including statistics
...@@ -159,18 +173,38 @@ class Scan extends Command { ...@@ -159,18 +173,38 @@ class Scan extends Command {
$verbose = false; $verbose = false;
} }
# 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");
}
}
$this->initTools(); $this->initTools();
$user_count = 0;
foreach ($users as $user) { foreach ($users as $user) {
if (is_object($user)) { if (is_object($user)) {
$user = $user->getUID(); $user = $user->getUID();
} }
$path = $inputPath ? $inputPath : '/' . $user; $path = $inputPath ? $inputPath : '/' . $user;
$user_count += 1;
if ($this->userManager->userExists($user)) { if ($this->userManager->userExists($user)) {
# 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)");
# full: printout data if $verbose was set # full: printout data if $verbose was set
$this->scanFiles($user, $path, $verbose, $output); $this->scanFiles($user, $path, $verbose, $output);
} else { } else {
$output->writeln("<error>Unknown user $user</error>"); $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;
} }
} }
...@@ -182,17 +216,6 @@ class Scan extends Command { ...@@ -182,17 +216,6 @@ class Scan extends Command {
} }
/**
* Checks if the command was interrupted by ctrl-c
*/
protected function checkForInterruption($output) {
if ($this->hasBeenInterrupted()) {
$this->presentResults($output);
exit;
}
}
/** /**
* Initialises some useful tools for the Command * Initialises some useful tools for the Command
*/ */
...@@ -202,14 +225,19 @@ class Scan extends Command { ...@@ -202,14 +225,19 @@ class Scan extends Command {
// Convert PHP errors to exceptions // Convert PHP errors to exceptions
set_error_handler([$this, 'exceptionErrorHandler'], E_ALL); set_error_handler([$this, 'exceptionErrorHandler'], E_ALL);
// Collect interrupts and notify the running command // check if the php pcntl_signal functions are accessible
pcntl_signal(SIGTERM, [$this, 'cancelOperation']); if (function_exists('pcntl_signal')) {
pcntl_signal(SIGINT, [$this, 'cancelOperation']); // Collect interrupts and notify the running command
pcntl_signal(SIGTERM, [$this, 'cancelOperation']);
pcntl_signal(SIGINT, [$this, 'cancelOperation']);
} else {
$this->php_pcntl_signal = false;
}
} }
/** /**
* Changes the status of the command to "interrupted" * Changes the status of the command to "interrupted" if ctrl-c has been pressed
* *
* Gives a chance to the command to properly terminate what it's doing * Gives a chance to the command to properly terminate what it's doing
*/ */
...@@ -218,6 +246,24 @@ class Scan extends Command { ...@@ -218,6 +246,24 @@ class Scan extends Command {
} }
/**
* @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;
}
}
/** /**
* Processes PHP errors as exceptions in order to be able to keep track of problems * Processes PHP errors as exceptions in order to be able to keep track of problems
* *
...@@ -239,20 +285,6 @@ class Scan extends Command { ...@@ -239,20 +285,6 @@ class Scan extends Command {
} }
/**
* @return bool
*/
protected function hasBeenInterrupted() {
$cancelled = false;
pcntl_signal_dispatch();
if ($this->interrupted) {
$cancelled = true;
}
return $cancelled;
}
/** /**
* @param OutputInterface $output * @param OutputInterface $output
*/ */
...@@ -300,7 +332,8 @@ class Scan extends Command { ...@@ -300,7 +332,8 @@ class Scan extends Command {
*/ */
protected function formatExecTime() { protected function formatExecTime() {
list($secs, $tens) = explode('.', sprintf("%.1f", ($this->execTime))); list($secs, $tens) = explode('.', sprintf("%.1f", ($this->execTime)));
$niceDate = date('H:i:s', $secs) . '.' . $tens; # add the following to $niceDate if you want to have microsecons added: . '.' . $tens;
$niceDate = date('H:i:s', $secs);
return $niceDate; return $niceDate;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment