1
0

Scan.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bart Visscher <bartv@thisnet.nl>
  6. * @author Blaok <i@blaok.me>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  9. * @author J0WI <J0WI@users.noreply.github.com>
  10. * @author Joas Schilling <coding@schilljs.com>
  11. * @author Joel S <joel.devbox@protonmail.com>
  12. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  13. * @author martin.mattel@diemattels.at <martin.mattel@diemattels.at>
  14. * @author Maxence Lange <maxence@artificial-owl.com>
  15. * @author Robin Appelman <robin@icewind.nl>
  16. * @author Roeland Jago Douma <roeland@famdouma.nl>
  17. * @author Thomas Müller <thomas.mueller@tmit.eu>
  18. * @author Vincent Petry <vincent@nextcloud.com>
  19. *
  20. * @license AGPL-3.0
  21. *
  22. * This code is free software: you can redistribute it and/or modify
  23. * it under the terms of the GNU Affero General Public License, version 3,
  24. * as published by the Free Software Foundation.
  25. *
  26. * This program is distributed in the hope that it will be useful,
  27. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29. * GNU Affero General Public License for more details.
  30. *
  31. * You should have received a copy of the GNU Affero General Public License, version 3,
  32. * along with this program. If not, see <http://www.gnu.org/licenses/>
  33. *
  34. */
  35. namespace OCA\Files\Command;
  36. use OC\Core\Command\Base;
  37. use OC\Core\Command\InterruptedException;
  38. use OC\DB\Connection;
  39. use OC\DB\ConnectionAdapter;
  40. use OC\FilesMetadata\FilesMetadataManager;
  41. use OC\ForbiddenException;
  42. use OCP\EventDispatcher\IEventDispatcher;
  43. use OCP\Files\Events\FileCacheUpdated;
  44. use OCP\Files\Events\NodeAddedToCache;
  45. use OCP\Files\Events\NodeRemovedFromCache;
  46. use OCP\Files\IRootFolder;
  47. use OCP\Files\Mount\IMountPoint;
  48. use OCP\Files\NotFoundException;
  49. use OCP\Files\StorageNotAvailableException;
  50. use OCP\FilesMetadata\IFilesMetadataManager;
  51. use OCP\IUserManager;
  52. use Psr\Log\LoggerInterface;
  53. use Symfony\Component\Console\Helper\Table;
  54. use Symfony\Component\Console\Input\InputArgument;
  55. use Symfony\Component\Console\Input\InputInterface;
  56. use Symfony\Component\Console\Input\InputOption;
  57. use Symfony\Component\Console\Output\OutputInterface;
  58. class Scan extends Base {
  59. protected float $execTime = 0;
  60. protected int $foldersCounter = 0;
  61. protected int $filesCounter = 0;
  62. protected int $errorsCounter = 0;
  63. protected int $newCounter = 0;
  64. protected int $updatedCounter = 0;
  65. protected int $removedCounter = 0;
  66. public function __construct(
  67. private IUserManager $userManager,
  68. private IRootFolder $rootFolder,
  69. private FilesMetadataManager $filesMetadataManager,
  70. private IEventDispatcher $eventDispatcher,
  71. private LoggerInterface $logger,
  72. ) {
  73. parent::__construct();
  74. }
  75. protected function configure(): void {
  76. parent::configure();
  77. $this
  78. ->setName('files:scan')
  79. ->setDescription('rescan filesystem')
  80. ->addArgument(
  81. 'user_id',
  82. InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
  83. 'will rescan all files of the given user(s)'
  84. )
  85. ->addOption(
  86. 'path',
  87. 'p',
  88. InputOption::VALUE_REQUIRED,
  89. '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'
  90. )
  91. ->addOption(
  92. 'generate-metadata',
  93. null,
  94. InputOption::VALUE_OPTIONAL,
  95. 'Generate metadata for all scanned files; if specified only generate for named value',
  96. ''
  97. )
  98. ->addOption(
  99. 'all',
  100. null,
  101. InputOption::VALUE_NONE,
  102. 'will rescan all files of all known users'
  103. )->addOption(
  104. 'unscanned',
  105. null,
  106. InputOption::VALUE_NONE,
  107. 'only scan files which are marked as not fully scanned'
  108. )->addOption(
  109. 'shallow',
  110. null,
  111. InputOption::VALUE_NONE,
  112. 'do not scan folders recursively'
  113. )->addOption(
  114. 'home-only',
  115. null,
  116. InputOption::VALUE_NONE,
  117. 'only scan the home storage, ignoring any mounted external storage or share'
  118. );
  119. }
  120. protected function scanFiles(string $user, string $path, ?string $scanMetadata, OutputInterface $output, bool $backgroundScan = false, bool $recursive = true, bool $homeOnly = false): void {
  121. $connection = $this->reconnectToDatabase($output);
  122. $scanner = new \OC\Files\Utils\Scanner(
  123. $user,
  124. new ConnectionAdapter($connection),
  125. \OC::$server->get(IEventDispatcher::class),
  126. \OC::$server->get(LoggerInterface::class)
  127. );
  128. # check on each file/folder if there was a user interrupt (ctrl-c) and throw an exception
  129. $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function (string $path) use ($output, $scanMetadata) {
  130. $output->writeln("\tFile\t<info>$path</info>", OutputInterface::VERBOSITY_VERBOSE);
  131. ++$this->filesCounter;
  132. $this->abortIfInterrupted();
  133. if ($scanMetadata !== null) {
  134. $node = $this->rootFolder->get($path);
  135. $this->filesMetadataManager->refreshMetadata(
  136. $node,
  137. ($scanMetadata !== '') ? IFilesMetadataManager::PROCESS_NAMED : IFilesMetadataManager::PROCESS_LIVE | IFilesMetadataManager::PROCESS_BACKGROUND,
  138. $scanMetadata
  139. );
  140. }
  141. });
  142. $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function ($path) use ($output) {
  143. $output->writeln("\tFolder\t<info>$path</info>", OutputInterface::VERBOSITY_VERBOSE);
  144. ++$this->foldersCounter;
  145. $this->abortIfInterrupted();
  146. });
  147. $scanner->listen('\OC\Files\Utils\Scanner', 'StorageNotAvailable', function (StorageNotAvailableException $e) use ($output) {
  148. $output->writeln('Error while scanning, storage not available (' . $e->getMessage() . ')', OutputInterface::VERBOSITY_VERBOSE);
  149. ++$this->errorsCounter;
  150. });
  151. $scanner->listen('\OC\Files\Utils\Scanner', 'normalizedNameMismatch', function ($fullPath) use ($output) {
  152. $output->writeln("\t<error>Entry \"" . $fullPath . '" will not be accessible due to incompatible encoding</error>');
  153. ++$this->errorsCounter;
  154. });
  155. $this->eventDispatcher->addListener(NodeAddedToCache::class, function () {
  156. ++$this->newCounter;
  157. });
  158. $this->eventDispatcher->addListener(FileCacheUpdated::class, function () {
  159. ++$this->updatedCounter;
  160. });
  161. $this->eventDispatcher->addListener(NodeRemovedFromCache::class, function () {
  162. ++$this->removedCounter;
  163. });
  164. try {
  165. if ($backgroundScan) {
  166. $scanner->backgroundScan($path);
  167. } else {
  168. $scanner->scan($path, $recursive, $homeOnly ? [$this, 'filterHomeMount'] : null);
  169. }
  170. } catch (ForbiddenException $e) {
  171. $output->writeln("<error>Home storage for user $user not writable or 'files' subdirectory missing</error>");
  172. $output->writeln(' ' . $e->getMessage());
  173. $output->writeln('Make sure you\'re running the scan command only as the user the web server runs as');
  174. ++$this->errorsCounter;
  175. } catch (InterruptedException $e) {
  176. # exit the function if ctrl-c has been pressed
  177. $output->writeln('Interrupted by user');
  178. } catch (NotFoundException $e) {
  179. $output->writeln('<error>Path not found: ' . $e->getMessage() . '</error>');
  180. ++$this->errorsCounter;
  181. } catch (\Exception $e) {
  182. $output->writeln('<error>Exception during scan: ' . $e->getMessage() . '</error>');
  183. $output->writeln('<error>' . $e->getTraceAsString() . '</error>');
  184. ++$this->errorsCounter;
  185. }
  186. }
  187. public function filterHomeMount(IMountPoint $mountPoint): bool {
  188. // any mountpoint inside '/$user/files/'
  189. return substr_count($mountPoint->getMountPoint(), '/') <= 3;
  190. }
  191. protected function execute(InputInterface $input, OutputInterface $output): int {
  192. $inputPath = $input->getOption('path');
  193. if ($inputPath) {
  194. $inputPath = '/' . trim($inputPath, '/');
  195. [, $user,] = explode('/', $inputPath, 3);
  196. $users = [$user];
  197. } elseif ($input->getOption('all')) {
  198. $users = $this->userManager->search('');
  199. } else {
  200. $users = $input->getArgument('user_id');
  201. }
  202. # check quantity of users to be process and show it on the command line
  203. $users_total = count($users);
  204. if ($users_total === 0) {
  205. $output->writeln('<error>Please specify the user id to scan, --all to scan for all users or --path=...</error>');
  206. return self::FAILURE;
  207. }
  208. $this->initTools($output);
  209. // getOption() logic on VALUE_OPTIONAL
  210. $metadata = null; // null if --generate-metadata is not set, empty if option have no value, value if set
  211. if ($input->getOption('generate-metadata') !== '') {
  212. $metadata = $input->getOption('generate-metadata') ?? '';
  213. }
  214. $user_count = 0;
  215. foreach ($users as $user) {
  216. if (is_object($user)) {
  217. $user = $user->getUID();
  218. }
  219. $path = $inputPath ?: '/' . $user;
  220. ++$user_count;
  221. if ($this->userManager->userExists($user)) {
  222. $output->writeln("Starting scan for user $user_count out of $users_total ($user)");
  223. $this->scanFiles($user, $path, $metadata, $output, $input->getOption('unscanned'), !$input->getOption('shallow'), $input->getOption('home-only'));
  224. $output->writeln('', OutputInterface::VERBOSITY_VERBOSE);
  225. } else {
  226. $output->writeln("<error>Unknown user $user_count $user</error>");
  227. $output->writeln('', OutputInterface::VERBOSITY_VERBOSE);
  228. }
  229. try {
  230. $this->abortIfInterrupted();
  231. } catch (InterruptedException $e) {
  232. break;
  233. }
  234. }
  235. $this->presentStats($output);
  236. return self::SUCCESS;
  237. }
  238. /**
  239. * Initialises some useful tools for the Command
  240. */
  241. protected function initTools(OutputInterface $output): void {
  242. // Start the timer
  243. $this->execTime = -microtime(true);
  244. // Convert PHP errors to exceptions
  245. set_error_handler(
  246. fn (int $severity, string $message, string $file, int $line): bool =>
  247. $this->exceptionErrorHandler($output, $severity, $message, $file, $line),
  248. E_ALL
  249. );
  250. }
  251. /**
  252. * Processes PHP errors in order to be able to show them in the output
  253. *
  254. * @see https://www.php.net/manual/en/function.set-error-handler.php
  255. *
  256. * @param int $severity the level of the error raised
  257. * @param string $message
  258. * @param string $file the filename that the error was raised in
  259. * @param int $line the line number the error was raised
  260. */
  261. public function exceptionErrorHandler(OutputInterface $output, int $severity, string $message, string $file, int $line): bool {
  262. if (($severity === E_DEPRECATED) || ($severity === E_USER_DEPRECATED)) {
  263. // Do not show deprecation warnings
  264. return false;
  265. }
  266. $e = new \ErrorException($message, 0, $severity, $file, $line);
  267. $output->writeln('<error>Error during scan: ' . $e->getMessage() . '</error>');
  268. $output->writeln('<error>' . $e->getTraceAsString() . '</error>', OutputInterface::VERBOSITY_VERY_VERBOSE);
  269. ++$this->errorsCounter;
  270. return true;
  271. }
  272. protected function presentStats(OutputInterface $output): void {
  273. // Stop the timer
  274. $this->execTime += microtime(true);
  275. $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");
  276. $headers = [
  277. 'Folders',
  278. 'Files',
  279. 'New',
  280. 'Updated',
  281. 'Removed',
  282. 'Errors',
  283. 'Elapsed time',
  284. ];
  285. $niceDate = $this->formatExecTime();
  286. $rows = [
  287. $this->foldersCounter,
  288. $this->filesCounter,
  289. $this->newCounter,
  290. $this->updatedCounter,
  291. $this->removedCounter,
  292. $this->errorsCounter,
  293. $niceDate,
  294. ];
  295. $table = new Table($output);
  296. $table
  297. ->setHeaders($headers)
  298. ->setRows([$rows]);
  299. $table->render();
  300. }
  301. /**
  302. * Formats microtime into a human-readable format
  303. */
  304. protected function formatExecTime(): string {
  305. $secs = (int)round($this->execTime);
  306. # convert seconds into HH:MM:SS form
  307. return sprintf('%02d:%02d:%02d', (int)($secs / 3600), ((int)($secs / 60) % 60), $secs % 60);
  308. }
  309. protected function reconnectToDatabase(OutputInterface $output): Connection {
  310. /** @var Connection $connection */
  311. $connection = \OC::$server->get(Connection::class);
  312. try {
  313. $connection->close();
  314. } catch (\Exception $ex) {
  315. $output->writeln("<info>Error while disconnecting from database: {$ex->getMessage()}</info>");
  316. }
  317. while (!$connection->isConnected()) {
  318. try {
  319. $connection->connect();
  320. } catch (\Exception $ex) {
  321. $output->writeln("<info>Error while re-connecting to database: {$ex->getMessage()}</info>");
  322. sleep(60);
  323. }
  324. }
  325. return $connection;
  326. }
  327. }