From 1f839796998d2f0656d578c1f0ac19d773100789 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Thu, 27 Apr 2023 10:46:37 +0200 Subject: [PATCH 1/3] Do not stop at the first PHP error/warning in files:scan MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- apps/files/lib/Command/Scan.php | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/apps/files/lib/Command/Scan.php b/apps/files/lib/Command/Scan.php index a59665c56e94e..fbfc9cbca5036 100644 --- a/apps/files/lib/Command/Scan.php +++ b/apps/files/lib/Command/Scan.php @@ -204,7 +204,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int return 1; } - $this->initTools(); + $this->initTools($output); $user_count = 0; foreach ($users as $user) { @@ -236,15 +236,19 @@ protected function execute(InputInterface $input, OutputInterface $output): int /** * Initialises some useful tools for the Command */ - protected function initTools() { + protected function initTools(OutputInterface $output) { // Start the timer $this->execTime = -microtime(true); // Convert PHP errors to exceptions - set_error_handler([$this, 'exceptionErrorHandler'], E_ALL); + set_error_handler( + fn (int $severity, string $message, string $file, int $line): bool => + $this->exceptionErrorHandler($output, $severity, $message, $file, $line), + E_ALL + ); } /** - * Processes PHP errors as exceptions in order to be able to keep track of problems + * Processes PHP errors in order to be able to show them in the output * * @see https://www.php.net/manual/en/function.set-error-handler.php * @@ -252,15 +256,15 @@ protected function initTools() { * @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; + public function exceptionErrorHandler(OutputInterface $output, int $severity, string $message, string $file, int $line): bool { + if (($severity === E_DEPRECATED) || ($severity === E_USER_DEPRECATED)) { + // Do not show deprecation warnings + return false; } - throw new \ErrorException($message, 0, $severity, $file, $line); + $e = new \ErrorException($message, 0, $severity, $file, $line); + $output->writeln("\t$e"); + return true; } /** From 2ffa9fc79741e71c277be2913ff2e396de09a51c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Tue, 2 May 2023 10:01:38 +0200 Subject: [PATCH 2/3] Only show error stack trace on very verbose level MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- apps/files/lib/Command/Scan.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/apps/files/lib/Command/Scan.php b/apps/files/lib/Command/Scan.php index fbfc9cbca5036..1d59db1faa643 100644 --- a/apps/files/lib/Command/Scan.php +++ b/apps/files/lib/Command/Scan.php @@ -192,11 +192,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int $users = $input->getArgument('user_id'); } - # restrict the verbosity level to VERBOSITY_VERBOSE - if ($output->getVerbosity() > OutputInterface::VERBOSITY_VERBOSE) { - $output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE); - } - # check quantity of users to be process and show it on the command line $users_total = count($users); if ($users_total === 0) { @@ -263,7 +258,8 @@ public function exceptionErrorHandler(OutputInterface $output, int $severity, st return false; } $e = new \ErrorException($message, 0, $severity, $file, $line); - $output->writeln("\t$e"); + $output->writeln('Error during scan: ' . $e->getMessage() . ''); + $output->writeln('' . $e->getTraceAsString() . '', OutputInterface::VERBOSITY_VERY_VERBOSE); return true; } From 88405d320ebc143d78537708843706d72bc680ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Thu, 4 May 2023 17:06:46 +0200 Subject: [PATCH 3/3] Add an error counter MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- apps/files/lib/Command/Scan.php | 37 +++++++++++++++------------------ 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/apps/files/lib/Command/Scan.php b/apps/files/lib/Command/Scan.php index 1d59db1faa643..6c7a607d2afcf 100644 --- a/apps/files/lib/Command/Scan.php +++ b/apps/files/lib/Command/Scan.php @@ -58,6 +58,7 @@ class Scan extends Base { protected float $execTime = 0; protected int $foldersCounter = 0; protected int $filesCounter = 0; + protected int $errorsCounter = 0; private IRootFolder $root; private MetadataManager $metadataManager; @@ -148,10 +149,12 @@ protected function scanFiles(string $user, string $path, bool $scanMetadata, Out $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); + ++$this->errorsCounter; }); $scanner->listen('\OC\Files\Utils\Scanner', 'normalizedNameMismatch', function ($fullPath) use ($output) { $output->writeln("\tEntry \"" . $fullPath . '" will not be accessible due to incompatible encoding'); + ++$this->errorsCounter; }); try { @@ -164,14 +167,17 @@ protected function scanFiles(string $user, string $path, bool $scanMetadata, Out $output->writeln("Home storage for user $user not writable or 'files' subdirectory missing"); $output->writeln(' ' . $e->getMessage()); $output->writeln('Make sure you\'re running the scan command only as the user the web server runs as'); + ++$this->errorsCounter; } catch (InterruptedException $e) { # exit the function if ctrl-c has been pressed $output->writeln('Interrupted by user'); } catch (NotFoundException $e) { $output->writeln('Path not found: ' . $e->getMessage() . ''); + ++$this->errorsCounter; } catch (\Exception $e) { $output->writeln('Exception during scan: ' . $e->getMessage() . ''); $output->writeln('' . $e->getTraceAsString() . ''); + ++$this->errorsCounter; } } @@ -260,6 +266,7 @@ public function exceptionErrorHandler(OutputInterface $output, int $severity, st $e = new \ErrorException($message, 0, $severity, $file, $line); $output->writeln('Error during scan: ' . $e->getMessage() . ''); $output->writeln('' . $e->getTraceAsString() . '', OutputInterface::VERBOSITY_VERY_VERBOSE); + ++$this->errorsCounter; return true; } @@ -271,28 +278,18 @@ protected function presentStats(OutputInterface $output) { $this->execTime += microtime(true); $headers = [ - 'Folders', 'Files', 'Elapsed time' + 'Folders', + 'Files', + 'Errors', + '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, - ]; - } + $rows = [ + $this->foldersCounter, + $this->filesCounter, + $this->errorsCounter, + $niceDate, + ]; $table = new Table($output); $table ->setHeaders($headers)