Scan.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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(' ' . $e->getMessage());
  155. $output->writeln('Make sure you\'re running the scan command only as the user the web server runs as');
  156. } catch (InterruptedException $e) {
  157. # exit the function if ctrl-c has been pressed
  158. $output->writeln('Interrupted by user');
  159. } catch (NotFoundException $e) {
  160. $output->writeln('<error>Path not found: ' . $e->getMessage() . '</error>');
  161. } catch (\Exception $e) {
  162. $output->writeln('<error>Exception during scan: ' . $e->getMessage() . '</error>');
  163. $output->writeln('<error>' . $e->getTraceAsString() . '</error>');
  164. }
  165. }
  166. public function filterHomeMount(IMountPoint $mountPoint) {
  167. // any mountpoint inside '/$user/files/'
  168. return substr_count($mountPoint->getMountPoint(), '/') <= 3;
  169. }
  170. protected function execute(InputInterface $input, OutputInterface $output): int {
  171. $inputPath = $input->getOption('path');
  172. if ($inputPath) {
  173. $inputPath = '/' . trim($inputPath, '/');
  174. [, $user,] = explode('/', $inputPath, 3);
  175. $users = [$user];
  176. } elseif ($input->getOption('all')) {
  177. $users = $this->userManager->search('');
  178. } else {
  179. $users = $input->getArgument('user_id');
  180. }
  181. # restrict the verbosity level to VERBOSITY_VERBOSE
  182. if ($output->getVerbosity() > OutputInterface::VERBOSITY_VERBOSE) {
  183. $output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE);
  184. }
  185. # check quantity of users to be process and show it on the command line
  186. $users_total = count($users);
  187. if ($users_total === 0) {
  188. $output->writeln('<error>Please specify the user id to scan, --all to scan for all users or --path=...</error>');
  189. return 1;
  190. }
  191. $this->initTools();
  192. $user_count = 0;
  193. foreach ($users as $user) {
  194. if (is_object($user)) {
  195. $user = $user->getUID();
  196. }
  197. $path = $inputPath ? $inputPath : '/' . $user;
  198. ++$user_count;
  199. if ($this->userManager->userExists($user)) {
  200. $output->writeln("Starting scan for user $user_count out of $users_total ($user)");
  201. $this->scanFiles($user, $path, $input->getOption('generate-metadata'), $output, $input->getOption('unscanned'), !$input->getOption('shallow'), $input->getOption('home-only'));
  202. $output->writeln('', OutputInterface::VERBOSITY_VERBOSE);
  203. } else {
  204. $output->writeln("<error>Unknown user $user_count $user</error>");
  205. $output->writeln('', OutputInterface::VERBOSITY_VERBOSE);
  206. }
  207. try {
  208. $this->abortIfInterrupted();
  209. } catch (InterruptedException $e) {
  210. break;
  211. }
  212. }
  213. $this->presentStats($output);
  214. return 0;
  215. }
  216. /**
  217. * Initialises some useful tools for the Command
  218. */
  219. protected function initTools() {
  220. // Start the timer
  221. $this->execTime = -microtime(true);
  222. // Convert PHP errors to exceptions
  223. set_error_handler([$this, 'exceptionErrorHandler'], E_ALL);
  224. }
  225. /**
  226. * Processes PHP errors as exceptions in order to be able to keep track of problems
  227. *
  228. * @see https://www.php.net/manual/en/function.set-error-handler.php
  229. *
  230. * @param int $severity the level of the error raised
  231. * @param string $message
  232. * @param string $file the filename that the error was raised in
  233. * @param int $line the line number the error was raised
  234. *
  235. * @throws \ErrorException
  236. */
  237. public function exceptionErrorHandler($severity, $message, $file, $line) {
  238. if (!(error_reporting() & $severity)) {
  239. // This error code is not included in error_reporting
  240. return;
  241. }
  242. throw new \ErrorException($message, 0, $severity, $file, $line);
  243. }
  244. /**
  245. * @param OutputInterface $output
  246. */
  247. protected function presentStats(OutputInterface $output) {
  248. // Stop the timer
  249. $this->execTime += microtime(true);
  250. $headers = [
  251. 'Folders', 'Files', 'Elapsed time'
  252. ];
  253. $this->showSummary($headers, null, $output);
  254. }
  255. /**
  256. * Shows a summary of operations
  257. *
  258. * @param string[] $headers
  259. * @param string[] $rows
  260. * @param OutputInterface $output
  261. */
  262. protected function showSummary($headers, $rows, OutputInterface $output) {
  263. $niceDate = $this->formatExecTime();
  264. if (!$rows) {
  265. $rows = [
  266. $this->foldersCounter,
  267. $this->filesCounter,
  268. $niceDate,
  269. ];
  270. }
  271. $table = new Table($output);
  272. $table
  273. ->setHeaders($headers)
  274. ->setRows([$rows]);
  275. $table->render();
  276. }
  277. /**
  278. * Formats microtime into a human readable format
  279. *
  280. * @return string
  281. */
  282. protected function formatExecTime() {
  283. $secs = (int)round($this->execTime);
  284. # convert seconds into HH:MM:SS form
  285. return sprintf('%02d:%02d:%02d', (int)($secs / 3600), ((int)($secs / 60) % 60), $secs % 60);
  286. }
  287. protected function reconnectToDatabase(OutputInterface $output): Connection {
  288. /** @var Connection $connection */
  289. $connection = \OC::$server->get(Connection::class);
  290. try {
  291. $connection->close();
  292. } catch (\Exception $ex) {
  293. $output->writeln("<info>Error while disconnecting from database: {$ex->getMessage()}</info>");
  294. }
  295. while (!$connection->isConnected()) {
  296. try {
  297. $connection->connect();
  298. } catch (\Exception $ex) {
  299. $output->writeln("<info>Error while re-connecting to database: {$ex->getMessage()}</info>");
  300. sleep(60);
  301. }
  302. }
  303. return $connection;
  304. }
  305. }