You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Scan.php 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 Morris Jobke <hey@morrisjobke.de>
  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 Doctrine\DBAL\Connection;
  37. use OC\Core\Command\Base;
  38. use OC\Core\Command\InterruptedException;
  39. use OC\ForbiddenException;
  40. use OCP\EventDispatcher\IEventDispatcher;
  41. use OCP\Files\Mount\IMountPoint;
  42. use OCP\Files\NotFoundException;
  43. use OCP\Files\StorageNotAvailableException;
  44. use OCP\IDBConnection;
  45. use OCP\IUserManager;
  46. use Symfony\Component\Console\Helper\Table;
  47. use Symfony\Component\Console\Input\InputArgument;
  48. use Symfony\Component\Console\Input\InputInterface;
  49. use Symfony\Component\Console\Input\InputOption;
  50. use Symfony\Component\Console\Output\OutputInterface;
  51. class Scan extends Base {
  52. /** @var IUserManager $userManager */
  53. private $userManager;
  54. /** @var float */
  55. protected $execTime = 0;
  56. /** @var int */
  57. protected $foldersCounter = 0;
  58. /** @var int */
  59. protected $filesCounter = 0;
  60. public function __construct(IUserManager $userManager) {
  61. $this->userManager = $userManager;
  62. parent::__construct();
  63. }
  64. protected function configure() {
  65. parent::configure();
  66. $this
  67. ->setName('files:scan')
  68. ->setDescription('rescan filesystem')
  69. ->addArgument(
  70. 'user_id',
  71. InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
  72. 'will rescan all files of the given user(s)'
  73. )
  74. ->addOption(
  75. 'path',
  76. 'p',
  77. InputArgument::OPTIONAL,
  78. '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'
  79. )
  80. ->addOption(
  81. 'all',
  82. null,
  83. InputOption::VALUE_NONE,
  84. 'will rescan all files of all known users'
  85. )->addOption(
  86. 'unscanned',
  87. null,
  88. InputOption::VALUE_NONE,
  89. 'only scan files which are marked as not fully scanned'
  90. )->addOption(
  91. 'shallow',
  92. null,
  93. InputOption::VALUE_NONE,
  94. 'do not scan folders recursively'
  95. )->addOption(
  96. 'home-only',
  97. null,
  98. InputOption::VALUE_NONE,
  99. 'only scan the home storage, ignoring any mounted external storage or share'
  100. );
  101. }
  102. public function checkScanWarning($fullPath, OutputInterface $output) {
  103. $normalizedPath = basename(\OC\Files\Filesystem::normalizePath($fullPath));
  104. $path = basename($fullPath);
  105. if ($normalizedPath !== $path) {
  106. $output->writeln("\t<error>Entry \"" . $fullPath . '" will not be accessible due to incompatible encoding</error>');
  107. }
  108. }
  109. protected function scanFiles($user, $path, OutputInterface $output, $backgroundScan = false, $recursive = true, $homeOnly = false) {
  110. $connection = $this->reconnectToDatabase($output);
  111. $scanner = new \OC\Files\Utils\Scanner($user, $connection, \OC::$server->query(IEventDispatcher::class), \OC::$server->getLogger());
  112. # check on each file/folder if there was a user interrupt (ctrl-c) and throw an exception
  113. $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output) {
  114. $output->writeln("\tFile\t<info>$path</info>", OutputInterface::VERBOSITY_VERBOSE);
  115. ++$this->filesCounter;
  116. $this->abortIfInterrupted();
  117. });
  118. $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function ($path) use ($output) {
  119. $output->writeln("\tFolder\t<info>$path</info>", OutputInterface::VERBOSITY_VERBOSE);
  120. ++$this->foldersCounter;
  121. $this->abortIfInterrupted();
  122. });
  123. $scanner->listen('\OC\Files\Utils\Scanner', 'StorageNotAvailable', function (StorageNotAvailableException $e) use ($output) {
  124. $output->writeln('Error while scanning, storage not available (' . $e->getMessage() . ')', OutputInterface::VERBOSITY_VERBOSE);
  125. });
  126. $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output) {
  127. $this->checkScanWarning($path, $output);
  128. });
  129. $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function ($path) use ($output) {
  130. $this->checkScanWarning($path, $output);
  131. });
  132. try {
  133. if ($backgroundScan) {
  134. $scanner->backgroundScan($path);
  135. } else {
  136. $scanner->scan($path, $recursive, $homeOnly ? [$this, 'filterHomeMount'] : null);
  137. }
  138. } catch (ForbiddenException $e) {
  139. $output->writeln("<error>Home storage for user $user not writable</error>");
  140. $output->writeln('Make sure you\'re running the scan command only as the user the web server runs as');
  141. } catch (InterruptedException $e) {
  142. # exit the function if ctrl-c has been pressed
  143. $output->writeln('Interrupted by user');
  144. } catch (NotFoundException $e) {
  145. $output->writeln('<error>Path not found: ' . $e->getMessage() . '</error>');
  146. } catch (\Exception $e) {
  147. $output->writeln('<error>Exception during scan: ' . $e->getMessage() . '</error>');
  148. $output->writeln('<error>' . $e->getTraceAsString() . '</error>');
  149. }
  150. }
  151. public function filterHomeMount(IMountPoint $mountPoint) {
  152. // any mountpoint inside '/$user/files/'
  153. return substr_count($mountPoint->getMountPoint(), '/') <= 3;
  154. }
  155. protected function execute(InputInterface $input, OutputInterface $output): int {
  156. $inputPath = $input->getOption('path');
  157. if ($inputPath) {
  158. $inputPath = '/' . trim($inputPath, '/');
  159. list(, $user,) = explode('/', $inputPath, 3);
  160. $users = [$user];
  161. } elseif ($input->getOption('all')) {
  162. $users = $this->userManager->search('');
  163. } else {
  164. $users = $input->getArgument('user_id');
  165. }
  166. # restrict the verbosity level to VERBOSITY_VERBOSE
  167. if ($output->getVerbosity() > OutputInterface::VERBOSITY_VERBOSE) {
  168. $output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE);
  169. }
  170. # check quantity of users to be process and show it on the command line
  171. $users_total = count($users);
  172. if ($users_total === 0) {
  173. $output->writeln('<error>Please specify the user id to scan, --all to scan for all users or --path=...</error>');
  174. return 1;
  175. }
  176. $this->initTools();
  177. $user_count = 0;
  178. foreach ($users as $user) {
  179. if (is_object($user)) {
  180. $user = $user->getUID();
  181. }
  182. $path = $inputPath ? $inputPath : '/' . $user;
  183. ++$user_count;
  184. if ($this->userManager->userExists($user)) {
  185. $output->writeln("Starting scan for user $user_count out of $users_total ($user)");
  186. $this->scanFiles($user, $path, $output, $input->getOption('unscanned'), !$input->getOption('shallow'), $input->getOption('home-only'));
  187. $output->writeln('', OutputInterface::VERBOSITY_VERBOSE);
  188. } else {
  189. $output->writeln("<error>Unknown user $user_count $user</error>");
  190. $output->writeln('', OutputInterface::VERBOSITY_VERBOSE);
  191. }
  192. try {
  193. $this->abortIfInterrupted();
  194. } catch (InterruptedException $e) {
  195. break;
  196. }
  197. }
  198. $this->presentStats($output);
  199. return 0;
  200. }
  201. /**
  202. * Initialises some useful tools for the Command
  203. */
  204. protected function initTools() {
  205. // Start the timer
  206. $this->execTime = -microtime(true);
  207. // Convert PHP errors to exceptions
  208. set_error_handler([$this, 'exceptionErrorHandler'], E_ALL);
  209. }
  210. /**
  211. * Processes PHP errors as exceptions in order to be able to keep track of problems
  212. *
  213. * @see https://www.php.net/manual/en/function.set-error-handler.php
  214. *
  215. * @param int $severity the level of the error raised
  216. * @param string $message
  217. * @param string $file the filename that the error was raised in
  218. * @param int $line the line number the error was raised
  219. *
  220. * @throws \ErrorException
  221. */
  222. public function exceptionErrorHandler($severity, $message, $file, $line) {
  223. if (!(error_reporting() & $severity)) {
  224. // This error code is not included in error_reporting
  225. return;
  226. }
  227. throw new \ErrorException($message, 0, $severity, $file, $line);
  228. }
  229. /**
  230. * @param OutputInterface $output
  231. */
  232. protected function presentStats(OutputInterface $output) {
  233. // Stop the timer
  234. $this->execTime += microtime(true);
  235. $headers = [
  236. 'Folders', 'Files', 'Elapsed time'
  237. ];
  238. $this->showSummary($headers, null, $output);
  239. }
  240. /**
  241. * Shows a summary of operations
  242. *
  243. * @param string[] $headers
  244. * @param string[] $rows
  245. * @param OutputInterface $output
  246. */
  247. protected function showSummary($headers, $rows, OutputInterface $output) {
  248. $niceDate = $this->formatExecTime();
  249. if (!$rows) {
  250. $rows = [
  251. $this->foldersCounter,
  252. $this->filesCounter,
  253. $niceDate,
  254. ];
  255. }
  256. $table = new Table($output);
  257. $table
  258. ->setHeaders($headers)
  259. ->setRows([$rows]);
  260. $table->render();
  261. }
  262. /**
  263. * Formats microtime into a human readable format
  264. *
  265. * @return string
  266. */
  267. protected function formatExecTime() {
  268. $secs = round($this->execTime);
  269. # convert seconds into HH:MM:SS form
  270. return sprintf('%02d:%02d:%02d', ($secs / 3600), ($secs / 60 % 60), $secs % 60);
  271. }
  272. /**
  273. * @return \OCP\IDBConnection
  274. */
  275. protected function reconnectToDatabase(OutputInterface $output) {
  276. /** @var Connection | IDBConnection $connection */
  277. $connection = \OC::$server->getDatabaseConnection();
  278. try {
  279. $connection->close();
  280. } catch (\Exception $ex) {
  281. $output->writeln("<info>Error while disconnecting from database: {$ex->getMessage()}</info>");
  282. }
  283. while (!$connection->isConnected()) {
  284. try {
  285. $connection->connect();
  286. } catch (\Exception $ex) {
  287. $output->writeln("<info>Error while re-connecting to database: {$ex->getMessage()}</info>");
  288. sleep(60);
  289. }
  290. }
  291. return $connection;
  292. }
  293. }