setName('files:scan-app-data') ->setDescription('rescan the AppData folder'); $this->addArgument('folder', InputArgument::OPTIONAL, 'The appdata subfolder to scan', ''); } protected function scanFiles(OutputInterface $output, string $folder): int { try { /** @var Folder $appData */ $appData = $this->getAppDataFolder(); } catch (NotFoundException $e) { $output->writeln('NoAppData folder found'); return self::FAILURE; } if ($folder !== '') { try { $appData = $appData->get($folder); } catch (NotFoundException $e) { $output->writeln('Could not find folder: ' . $folder . ''); return self::FAILURE; } } $connection = $this->reconnectToDatabase($output); $scanner = new Scanner( null, new ConnectionAdapter($connection), \OC::$server->query(IEventDispatcher::class), \OC::$server->get(LoggerInterface::class) ); # check on each file/folder if there was a user interrupt (ctrl-c) and throw an exception $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output): void { $output->writeln("\tFile $path", OutputInterface::VERBOSITY_VERBOSE); ++$this->filesCounter; $this->abortIfInterrupted(); }); $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function ($path) use ($output): void { $output->writeln("\tFolder $path", OutputInterface::VERBOSITY_VERBOSE); ++$this->foldersCounter; $this->abortIfInterrupted(); }); $scanner->listen('\OC\Files\Utils\Scanner', 'StorageNotAvailable', function (StorageNotAvailableException $e) use ($output): void { $output->writeln('Error while scanning, storage not available (' . $e->getMessage() . ')', OutputInterface::VERBOSITY_VERBOSE); }); $scanner->listen('\OC\Files\Utils\Scanner', 'normalizedNameMismatch', function ($fullPath) use ($output): void { $output->writeln("\tEntry \"" . $fullPath . '" will not be accessible due to incompatible encoding'); }); try { $scanner->scan($appData->getPath()); } catch (ForbiddenException $e) { $output->writeln('Storage not writable'); $output->writeln('Make sure you\'re running the scan command only as the user the web server runs as'); return self::FAILURE; } catch (InterruptedException $e) { # exit the function if ctrl-c has been pressed $output->writeln('Interrupted by user'); return self::FAILURE; } catch (NotFoundException $e) { $output->writeln('Path not found: ' . $e->getMessage() . ''); return self::FAILURE; } catch (\Exception $e) { $output->writeln('Exception during scan: ' . $e->getMessage() . ''); $output->writeln('' . $e->getTraceAsString() . ''); return self::FAILURE; } return self::SUCCESS; } protected function execute(InputInterface $input, OutputInterface $output): int { # restrict the verbosity level to VERBOSITY_VERBOSE if ($output->getVerbosity() > OutputInterface::VERBOSITY_VERBOSE) { $output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE); } $output->writeln('Scanning AppData for files'); $output->writeln(''); $folder = $input->getArgument('folder'); $this->initTools(); $exitCode = $this->scanFiles($output, $folder); if ($exitCode === 0) { $this->presentStats($output); } return $exitCode; } /** * Initialises some useful tools for the Command */ protected function initTools(): void { // 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://www.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); } protected function presentStats(OutputInterface $output): void { // 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 */ protected function showSummary($headers, $rows, OutputInterface $output): void { $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 */ protected function formatExecTime(): string { $secs = round($this->execTime); # convert seconds into HH:MM:SS form return sprintf('%02d:%02d:%02d', (int)($secs / 3600), ((int)($secs / 60) % 60), (int)$secs % 60); } protected function reconnectToDatabase(OutputInterface $output): Connection { /** @var Connection $connection */ $connection = \OC::$server->get(Connection::class); try { $connection->close(); } catch (\Exception $ex) { $output->writeln("Error while disconnecting from database: {$ex->getMessage()}"); } while (!$connection->isConnected()) { try { $connection->connect(); } catch (\Exception $ex) { $output->writeln("Error while re-connecting to database: {$ex->getMessage()}"); sleep(60); } } return $connection; } /** * @throws NotFoundException */ private function getAppDataFolder(): Node { $instanceId = $this->config->getSystemValue('instanceid', null); if ($instanceId === null) { throw new NotFoundException(); } return $this->rootFolder->get('appdata_' . $instanceId); } }