Skip to content

Commit f1bfd7f

Browse files
authored
Merge pull request #38154 from nextcloud/backport/37944/stable26
[stable26] Do not stop at the first PHP error/warning in files:scan
2 parents b7602ca + f664d42 commit f1bfd7f

File tree

1 file changed

+33
-36
lines changed

1 file changed

+33
-36
lines changed

apps/files/lib/Command/Scan.php

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class Scan extends Base {
5858
protected float $execTime = 0;
5959
protected int $foldersCounter = 0;
6060
protected int $filesCounter = 0;
61+
protected int $errorsCounter = 0;
6162
private IRootFolder $root;
6263
private MetadataManager $metadataManager;
6364

@@ -148,10 +149,12 @@ protected function scanFiles(string $user, string $path, bool $scanMetadata, Out
148149

149150
$scanner->listen('\OC\Files\Utils\Scanner', 'StorageNotAvailable', function (StorageNotAvailableException $e) use ($output) {
150151
$output->writeln('Error while scanning, storage not available (' . $e->getMessage() . ')', OutputInterface::VERBOSITY_VERBOSE);
152+
++$this->errorsCounter;
151153
});
152154

153155
$scanner->listen('\OC\Files\Utils\Scanner', 'normalizedNameMismatch', function ($fullPath) use ($output) {
154156
$output->writeln("\t<error>Entry \"" . $fullPath . '" will not be accessible due to incompatible encoding</error>');
157+
++$this->errorsCounter;
155158
});
156159

157160
try {
@@ -163,14 +166,17 @@ protected function scanFiles(string $user, string $path, bool $scanMetadata, Out
163166
} catch (ForbiddenException $e) {
164167
$output->writeln("<error>Home storage for user $user not writable or 'files' subdirectory missing</error>");
165168
$output->writeln('Make sure you\'re running the scan command only as the user the web server runs as');
169+
++$this->errorsCounter;
166170
} catch (InterruptedException $e) {
167171
# exit the function if ctrl-c has been pressed
168172
$output->writeln('Interrupted by user');
169173
} catch (NotFoundException $e) {
170174
$output->writeln('<error>Path not found: ' . $e->getMessage() . '</error>');
175+
++$this->errorsCounter;
171176
} catch (\Exception $e) {
172177
$output->writeln('<error>Exception during scan: ' . $e->getMessage() . '</error>');
173178
$output->writeln('<error>' . $e->getTraceAsString() . '</error>');
179+
++$this->errorsCounter;
174180
}
175181
}
176182

@@ -191,19 +197,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
191197
$users = $input->getArgument('user_id');
192198
}
193199

194-
# restrict the verbosity level to VERBOSITY_VERBOSE
195-
if ($output->getVerbosity() > OutputInterface::VERBOSITY_VERBOSE) {
196-
$output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE);
197-
}
198-
199200
# check quantity of users to be process and show it on the command line
200201
$users_total = count($users);
201202
if ($users_total === 0) {
202203
$output->writeln('<error>Please specify the user id to scan, --all to scan for all users or --path=...</error>');
203204
return 1;
204205
}
205206

206-
$this->initTools();
207+
$this->initTools($output);
207208

208209
$user_count = 0;
209210
foreach ($users as $user) {
@@ -235,31 +236,37 @@ protected function execute(InputInterface $input, OutputInterface $output): int
235236
/**
236237
* Initialises some useful tools for the Command
237238
*/
238-
protected function initTools() {
239+
protected function initTools(OutputInterface $output) {
239240
// Start the timer
240241
$this->execTime = -microtime(true);
241242
// Convert PHP errors to exceptions
242-
set_error_handler([$this, 'exceptionErrorHandler'], E_ALL);
243+
set_error_handler(
244+
fn (int $severity, string $message, string $file, int $line): bool =>
245+
$this->exceptionErrorHandler($output, $severity, $message, $file, $line),
246+
E_ALL
247+
);
243248
}
244249

245250
/**
246-
* Processes PHP errors as exceptions in order to be able to keep track of problems
251+
* Processes PHP errors in order to be able to show them in the output
247252
*
248253
* @see https://www.php.net/manual/en/function.set-error-handler.php
249254
*
250255
* @param int $severity the level of the error raised
251256
* @param string $message
252257
* @param string $file the filename that the error was raised in
253258
* @param int $line the line number the error was raised
254-
*
255-
* @throws \ErrorException
256259
*/
257-
public function exceptionErrorHandler($severity, $message, $file, $line) {
258-
if (!(error_reporting() & $severity)) {
259-
// This error code is not included in error_reporting
260-
return;
260+
public function exceptionErrorHandler(OutputInterface $output, int $severity, string $message, string $file, int $line): bool {
261+
if (($severity === E_DEPRECATED) || ($severity === E_USER_DEPRECATED)) {
262+
// Do not show deprecation warnings
263+
return false;
261264
}
262-
throw new \ErrorException($message, 0, $severity, $file, $line);
265+
$e = new \ErrorException($message, 0, $severity, $file, $line);
266+
$output->writeln('<error>Error during scan: ' . $e->getMessage() . '</error>');
267+
$output->writeln('<error>' . $e->getTraceAsString() . '</error>', OutputInterface::VERBOSITY_VERY_VERBOSE);
268+
++$this->errorsCounter;
269+
return true;
263270
}
264271

265272
/**
@@ -270,28 +277,18 @@ protected function presentStats(OutputInterface $output) {
270277
$this->execTime += microtime(true);
271278

272279
$headers = [
273-
'Folders', 'Files', 'Elapsed time'
280+
'Folders',
281+
'Files',
282+
'Errors',
283+
'Elapsed time',
274284
];
275-
276-
$this->showSummary($headers, null, $output);
277-
}
278-
279-
/**
280-
* Shows a summary of operations
281-
*
282-
* @param string[] $headers
283-
* @param string[] $rows
284-
* @param OutputInterface $output
285-
*/
286-
protected function showSummary($headers, $rows, OutputInterface $output) {
287285
$niceDate = $this->formatExecTime();
288-
if (!$rows) {
289-
$rows = [
290-
$this->foldersCounter,
291-
$this->filesCounter,
292-
$niceDate,
293-
];
294-
}
286+
$rows = [
287+
$this->foldersCounter,
288+
$this->filesCounter,
289+
$this->errorsCounter,
290+
$niceDate,
291+
];
295292
$table = new Table($output);
296293
$table
297294
->setHeaders($headers)

0 commit comments

Comments
 (0)