aboutsummaryrefslogtreecommitdiffstats
path: root/apps/files/lib/Command/Scan.php
diff options
context:
space:
mode:
Diffstat (limited to 'apps/files/lib/Command/Scan.php')
-rw-r--r--apps/files/lib/Command/Scan.php263
1 files changed, 154 insertions, 109 deletions
diff --git a/apps/files/lib/Command/Scan.php b/apps/files/lib/Command/Scan.php
index 710c76de493..b9057139b0e 100644
--- a/apps/files/lib/Command/Scan.php
+++ b/apps/files/lib/Command/Scan.php
@@ -1,35 +1,9 @@
<?php
+
/**
- * @copyright Copyright (c) 2016, ownCloud, Inc.
- *
- * @author Bart Visscher <bartv@thisnet.nl>
- * @author Blaok <i@blaok.me>
- * @author Christoph Wurst <christoph@winzerhof-wurst.at>
- * @author Daniel Kesselberg <mail@danielkesselberg.de>
- * @author J0WI <J0WI@users.noreply.github.com>
- * @author Joas Schilling <coding@schilljs.com>
- * @author Joel S <joel.devbox@protonmail.com>
- * @author Jörn Friedrich Dreyer <jfd@butonic.de>
- * @author martin.mattel@diemattels.at <martin.mattel@diemattels.at>
- * @author Robin Appelman <robin@icewind.nl>
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- * @author Thomas Müller <thomas.mueller@tmit.eu>
- * @author Vincent Petry <vincent@nextcloud.com>
- *
- * @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/>
- *
+ * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
+ * SPDX-License-Identifier: AGPL-3.0-only
*/
namespace OCA\Files\Command;
@@ -37,15 +11,22 @@ use OC\Core\Command\Base;
use OC\Core\Command\InterruptedException;
use OC\DB\Connection;
use OC\DB\ConnectionAdapter;
-use OCP\Files\File;
+use OC\Files\Storage\Wrapper\Jail;
+use OC\Files\Utils\Scanner;
+use OC\FilesMetadata\FilesMetadataManager;
use OC\ForbiddenException;
-use OC\Metadata\MetadataManager;
use OCP\EventDispatcher\IEventDispatcher;
+use OCP\Files\Events\FileCacheUpdated;
+use OCP\Files\Events\NodeAddedToCache;
+use OCP\Files\Events\NodeRemovedFromCache;
use OCP\Files\IRootFolder;
use OCP\Files\Mount\IMountPoint;
use OCP\Files\NotFoundException;
use OCP\Files\StorageNotAvailableException;
+use OCP\FilesMetadata\IFilesMetadataManager;
use OCP\IUserManager;
+use OCP\Lock\LockedException;
+use OCP\Server;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputArgument;
@@ -54,25 +35,25 @@ use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class Scan extends Base {
- private IUserManager $userManager;
protected float $execTime = 0;
protected int $foldersCounter = 0;
protected int $filesCounter = 0;
- private IRootFolder $root;
- private MetadataManager $metadataManager;
+ protected int $errorsCounter = 0;
+ protected int $newCounter = 0;
+ protected int $updatedCounter = 0;
+ protected int $removedCounter = 0;
public function __construct(
- IUserManager $userManager,
- IRootFolder $rootFolder,
- MetadataManager $metadataManager
+ private IUserManager $userManager,
+ private IRootFolder $rootFolder,
+ private FilesMetadataManager $filesMetadataManager,
+ private IEventDispatcher $eventDispatcher,
+ private LoggerInterface $logger,
) {
- $this->userManager = $userManager;
parent::__construct();
- $this->root = $rootFolder;
- $this->metadataManager = $metadataManager;
}
- protected function configure() {
+ protected function configure(): void {
parent::configure();
$this
@@ -86,14 +67,15 @@ class Scan extends Base {
->addOption(
'path',
'p',
- InputArgument::OPTIONAL,
+ InputOption::VALUE_REQUIRED,
'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'
)
->addOption(
'generate-metadata',
null,
- InputOption::VALUE_NONE,
- 'Generate metadata for all scanned files'
+ InputOption::VALUE_OPTIONAL,
+ 'Generate metadata for all scanned files; if specified only generate for named value',
+ ''
)
->addOption(
'all',
@@ -118,63 +100,95 @@ class Scan extends Base {
);
}
- protected function scanFiles(string $user, string $path, bool $scanMetadata, OutputInterface $output, bool $backgroundScan = false, bool $recursive = true, bool $homeOnly = false): void {
+ protected function scanFiles(
+ string $user,
+ string $path,
+ ?string $scanMetadata,
+ OutputInterface $output,
+ callable $mountFilter,
+ bool $backgroundScan = false,
+ bool $recursive = true,
+ ): void {
$connection = $this->reconnectToDatabase($output);
- $scanner = new \OC\Files\Utils\Scanner(
+ $scanner = new Scanner(
$user,
new ConnectionAdapter($connection),
- \OC::$server->get(IEventDispatcher::class),
- \OC::$server->get(LoggerInterface::class)
+ Server::get(IEventDispatcher::class),
+ 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 (string $path) use ($output, $scanMetadata) {
+ $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function (string $path) use ($output, $scanMetadata): void {
$output->writeln("\tFile\t<info>$path</info>", OutputInterface::VERBOSITY_VERBOSE);
++$this->filesCounter;
$this->abortIfInterrupted();
- if ($scanMetadata) {
- $node = $this->root->get($path);
- if ($node instanceof File) {
- $this->metadataManager->generateMetadata($node, false);
- }
+ if ($scanMetadata !== null) {
+ $node = $this->rootFolder->get($path);
+ $this->filesMetadataManager->refreshMetadata(
+ $node,
+ ($scanMetadata !== '') ? IFilesMetadataManager::PROCESS_NAMED : IFilesMetadataManager::PROCESS_LIVE | IFilesMetadataManager::PROCESS_BACKGROUND,
+ $scanMetadata
+ );
}
});
- $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function ($path) use ($output) {
+ $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function ($path) use ($output): void {
$output->writeln("\tFolder\t<info>$path</info>", OutputInterface::VERBOSITY_VERBOSE);
++$this->foldersCounter;
$this->abortIfInterrupted();
});
- $scanner->listen('\OC\Files\Utils\Scanner', 'StorageNotAvailable', function (StorageNotAvailableException $e) use ($output) {
+ $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);
+ ++$this->errorsCounter;
});
- $scanner->listen('\OC\Files\Utils\Scanner', 'normalizedNameMismatch', function ($fullPath) use ($output) {
+ $scanner->listen('\OC\Files\Utils\Scanner', 'normalizedNameMismatch', function ($fullPath) use ($output): void {
$output->writeln("\t<error>Entry \"" . $fullPath . '" will not be accessible due to incompatible encoding</error>');
+ ++$this->errorsCounter;
+ });
+
+ $this->eventDispatcher->addListener(NodeAddedToCache::class, function (): void {
+ ++$this->newCounter;
+ });
+ $this->eventDispatcher->addListener(FileCacheUpdated::class, function (): void {
+ ++$this->updatedCounter;
+ });
+ $this->eventDispatcher->addListener(NodeRemovedFromCache::class, function (): void {
+ ++$this->removedCounter;
});
try {
if ($backgroundScan) {
$scanner->backgroundScan($path);
} else {
- $scanner->scan($path, $recursive, $homeOnly ? [$this, 'filterHomeMount'] : null);
+ $scanner->scan($path, $recursive, $mountFilter);
}
} catch (ForbiddenException $e) {
$output->writeln("<error>Home storage for user $user not writable or 'files' subdirectory missing</error>");
+ $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('<error>Path not found: ' . $e->getMessage() . '</error>');
+ ++$this->errorsCounter;
+ } catch (LockedException $e) {
+ if (str_starts_with($e->getPath(), 'scanner::')) {
+ $output->writeln('<error>Another process is already scanning \'' . substr($e->getPath(), strlen('scanner::')) . '\'</error>');
+ } else {
+ throw $e;
+ }
} catch (\Exception $e) {
$output->writeln('<error>Exception during scan: ' . $e->getMessage() . '</error>');
$output->writeln('<error>' . $e->getTraceAsString() . '</error>');
+ ++$this->errorsCounter;
}
}
- public function filterHomeMount(IMountPoint $mountPoint) {
+ public function isHomeMount(IMountPoint $mountPoint): bool {
// any mountpoint inside '/$user/files/'
return substr_count($mountPoint->getMountPoint(), '/') <= 3;
}
@@ -191,30 +205,62 @@ class Scan extends Base {
$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) {
$output->writeln('<error>Please specify the user id to scan, --all to scan for all users or --path=...</error>');
- return 1;
+ return self::FAILURE;
+ }
+
+ $this->initTools($output);
+
+ // getOption() logic on VALUE_OPTIONAL
+ $metadata = null; // null if --generate-metadata is not set, empty if option have no value, value if set
+ if ($input->getOption('generate-metadata') !== '') {
+ $metadata = $input->getOption('generate-metadata') ?? '';
}
- $this->initTools();
+ $homeOnly = $input->getOption('home-only');
+ $scannedStorages = [];
+ $mountFilter = function (IMountPoint $mount) use ($homeOnly, &$scannedStorages) {
+ if ($homeOnly && !$this->isHomeMount($mount)) {
+ return false;
+ }
+
+ // when scanning multiple users, the scanner might encounter the same storage multiple times (e.g. external storages, or group folders)
+ // we can filter out any storage we've already scanned to avoid double work
+ $storage = $mount->getStorage();
+ $storageKey = $storage->getId();
+ while ($storage->instanceOfStorage(Jail::class)) {
+ $storageKey .= '/' . $storage->getUnjailedPath('');
+ $storage = $storage->getUnjailedStorage();
+ }
+ if (array_key_exists($storageKey, $scannedStorages)) {
+ return false;
+ }
+
+ $scannedStorages[$storageKey] = true;
+ return true;
+ };
$user_count = 0;
foreach ($users as $user) {
if (is_object($user)) {
$user = $user->getUID();
}
- $path = $inputPath ? $inputPath : '/' . $user;
+ $path = $inputPath ?: '/' . $user;
++$user_count;
if ($this->userManager->userExists($user)) {
$output->writeln("Starting scan for user $user_count out of $users_total ($user)");
- $this->scanFiles($user, $path, $input->getOption('generate-metadata'), $output, $input->getOption('unscanned'), !$input->getOption('shallow'), $input->getOption('home-only'));
+ $this->scanFiles(
+ $user,
+ $path,
+ $metadata,
+ $output,
+ $mountFilter,
+ $input->getOption('unscanned'),
+ !$input->getOption('shallow'),
+ );
$output->writeln('', OutputInterface::VERBOSITY_VERBOSE);
} else {
$output->writeln("<error>Unknown user $user_count $user</error>");
@@ -229,21 +275,25 @@ class Scan extends Base {
}
$this->presentStats($output);
- return 0;
+ return self::SUCCESS;
}
/**
* Initialises some useful tools for the Command
*/
- protected function initTools() {
+ protected function initTools(OutputInterface $output): void {
// 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
*
@@ -251,47 +301,44 @@ class Scan extends Base {
* @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('<error>Error during scan: ' . $e->getMessage() . '</error>');
+ $output->writeln('<error>' . $e->getTraceAsString() . '</error>', OutputInterface::VERBOSITY_VERY_VERBOSE);
+ ++$this->errorsCounter;
+ return true;
}
- /**
- * @param OutputInterface $output
- */
- protected function presentStats(OutputInterface $output) {
+ protected function presentStats(OutputInterface $output): void {
// Stop the timer
$this->execTime += microtime(true);
+ $this->logger->info("Completed scan of {$this->filesCounter} files in {$this->foldersCounter} folder. Found {$this->newCounter} new, {$this->updatedCounter} updated and {$this->removedCounter} removed items");
+
$headers = [
- 'Folders', 'Files', 'Elapsed time'
+ 'Folders',
+ 'Files',
+ 'New',
+ 'Updated',
+ 'Removed',
+ '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->newCounter,
+ $this->updatedCounter,
+ $this->removedCounter,
+ $this->errorsCounter,
+ $niceDate,
+ ];
$table = new Table($output);
$table
->setHeaders($headers)
@@ -301,11 +348,9 @@ class Scan extends Base {
/**
- * Formats microtime into a human readable format
- *
- * @return string
+ * Formats microtime into a human-readable format
*/
- protected function formatExecTime() {
+ protected function formatExecTime(): string {
$secs = (int)round($this->execTime);
# convert seconds into HH:MM:SS form
return sprintf('%02d:%02d:%02d', (int)($secs / 3600), ((int)($secs / 60) % 60), $secs % 60);
@@ -313,7 +358,7 @@ class Scan extends Base {
protected function reconnectToDatabase(OutputInterface $output): Connection {
/** @var Connection $connection */
- $connection = \OC::$server->get(Connection::class);
+ $connection = Server::get(Connection::class);
try {
$connection->close();
} catch (\Exception $ex) {