Scan.php 11 KB

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