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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 OC\Core\Command\Base;
  37. use OC\Core\Command\InterruptedException;
  38. use OC\DB\Connection;
  39. use OC\DB\ConnectionAdapter;
  40. use OC\ForbiddenException;
  41. use OCP\EventDispatcher\IEventDispatcher;
  42. use OCP\Files\Mount\IMountPoint;
  43. use OCP\Files\NotFoundException;
  44. use OCP\Files\StorageNotAvailableException;
  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(
  112. $user,
  113. new ConnectionAdapter($connection),
  114. \OC::$server->query(IEventDispatcher::class),
  115. \OC::$server->getLogger()
  116. );
  117. # check on each file/folder if there was a user interrupt (ctrl-c) and throw an exception
  118. $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output) {
  119. $output->writeln("\tFile\t<info>$path</info>", OutputInterface::VERBOSITY_VERBOSE);
  120. ++$this->filesCounter;
  121. $this->abortIfInterrupted();
  122. });
  123. $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function ($path) use ($output) {
  124. $output->writeln("\tFolder\t<info>$path</info>", OutputInterface::VERBOSITY_VERBOSE);
  125. ++$this->foldersCounter;
  126. $this->abortIfInterrupted();
  127. });
  128. $scanner->listen('\OC\Files\Utils\Scanner', 'StorageNotAvailable', function (StorageNotAvailableException $e) use ($output) {
  129. $output->writeln('Error while scanning, storage not available (' . $e->getMessage() . ')', OutputInterface::VERBOSITY_VERBOSE);
  130. });
  131. $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output) {
  132. $this->checkScanWarning($path, $output);
  133. });
  134. $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function ($path) use ($output) {
  135. $this->checkScanWarning($path, $output);
  136. });
  137. try {
  138. if ($backgroundScan) {
  139. $scanner->backgroundScan($path);
  140. } else {
  141. $scanner->scan($path, $recursive, $homeOnly ? [$this, 'filterHomeMount'] : null);
  142. }
  143. } catch (ForbiddenException $e) {
  144. $output->writeln("<error>Home storage for user $user not writable</error>");
  145. $output->writeln('Make sure you\'re running the scan command only as the user the web server runs as');
  146. } catch (InterruptedException $e) {
  147. # exit the function if ctrl-c has been pressed
  148. $output->writeln('Interrupted by user');
  149. } catch (NotFoundException $e) {
  150. $output->writeln('<error>Path not found: ' . $e->getMessage() . '</error>');
  151. } catch (\Exception $e) {
  152. $output->writeln('<error>Exception during scan: ' . $e->getMessage() . '</error>');
  153. $output->writeln('<error>' . $e->getTraceAsString() . '</error>');
  154. }
  155. }
  156. public function filterHomeMount(IMountPoint $mountPoint) {
  157. // any mountpoint inside '/$user/files/'
  158. return substr_count($mountPoint->getMountPoint(), '/') <= 3;
  159. }
  160. protected function execute(InputInterface $input, OutputInterface $output): int {
  161. $inputPath = $input->getOption('path');
  162. if ($inputPath) {
  163. $inputPath = '/' . trim($inputPath, '/');
  164. [, $user,] = explode('/', $inputPath, 3);
  165. $users = [$user];
  166. } elseif ($input->getOption('all')) {
  167. $users = $this->userManager->search('');
  168. } else {
  169. $users = $input->getArgument('user_id');
  170. }
  171. # restrict the verbosity level to VERBOSITY_VERBOSE
  172. if ($output->getVerbosity() > OutputInterface::VERBOSITY_VERBOSE) {
  173. $output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE);
  174. }
  175. # check quantity of users to be process and show it on the command line
  176. $users_total = count($users);
  177. if ($users_total === 0) {
  178. $output->writeln('<error>Please specify the user id to scan, --all to scan for all users or --path=...</error>');
  179. return 1;
  180. }
  181. $this->initTools();
  182. $user_count = 0;
  183. foreach ($users as $user) {
  184. if (is_object($user)) {
  185. $user = $user->getUID();
  186. }
  187. $path = $inputPath ? $inputPath : '/' . $user;
  188. ++$user_count;
  189. if ($this->userManager->userExists($user)) {
  190. $output->writeln("Starting scan for user $user_count out of $users_total ($user)");
  191. $this->scanFiles($user, $path, $output, $input->getOption('unscanned'), !$input->getOption('shallow'), $input->getOption('home-only'));
  192. $output->writeln('', OutputInterface::VERBOSITY_VERBOSE);
  193. } else {
  194. $output->writeln("<error>Unknown user $user_count $user</error>");
  195. $output->writeln('', OutputInterface::VERBOSITY_VERBOSE);
  196. }
  197. try {
  198. $this->abortIfInterrupted();
  199. } catch (InterruptedException $e) {
  200. break;
  201. }
  202. }
  203. $this->presentStats($output);
  204. return 0;
  205. }
  206. /**
  207. * Initialises some useful tools for the Command
  208. */
  209. protected function initTools() {
  210. // Start the timer
  211. $this->execTime = -microtime(true);
  212. // Convert PHP errors to exceptions
  213. set_error_handler([$this, 'exceptionErrorHandler'], E_ALL);
  214. }
  215. /**
  216. * Processes PHP errors as exceptions in order to be able to keep track of problems
  217. *
  218. * @see https://www.php.net/manual/en/function.set-error-handler.php
  219. *
  220. * @param int $severity the level of the error raised
  221. * @param string $message
  222. * @param string $file the filename that the error was raised in
  223. * @param int $line the line number the error was raised
  224. *
  225. * @throws \ErrorException
  226. */
  227. public function exceptionErrorHandler($severity, $message, $file, $line) {
  228. if (!(error_reporting() & $severity)) {
  229. // This error code is not included in error_reporting
  230. return;
  231. }
  232. throw new \ErrorException($message, 0, $severity, $file, $line);
  233. }
  234. /**
  235. * @param OutputInterface $output
  236. */
  237. protected function presentStats(OutputInterface $output) {
  238. // Stop the timer
  239. $this->execTime += microtime(true);
  240. $headers = [
  241. 'Folders', 'Files', 'Elapsed time'
  242. ];
  243. $this->showSummary($headers, null, $output);
  244. }
  245. /**
  246. * Shows a summary of operations
  247. *
  248. * @param string[] $headers
  249. * @param string[] $rows
  250. * @param OutputInterface $output
  251. */
  252. protected function showSummary($headers, $rows, OutputInterface $output) {
  253. $niceDate = $this->formatExecTime();
  254. if (!$rows) {
  255. $rows = [
  256. $this->foldersCounter,
  257. $this->filesCounter,
  258. $niceDate,
  259. ];
  260. }
  261. $table = new Table($output);
  262. $table
  263. ->setHeaders($headers)
  264. ->setRows([$rows]);
  265. $table->render();
  266. }
  267. /**
  268. * Formats microtime into a human readable format
  269. *
  270. * @return string
  271. */
  272. protected function formatExecTime() {
  273. $secs = round($this->execTime);
  274. # convert seconds into HH:MM:SS form
  275. return sprintf('%02d:%02d:%02d', ($secs / 3600), ($secs / 60 % 60), $secs % 60);
  276. }
  277. protected function reconnectToDatabase(OutputInterface $output): Connection {
  278. /** @var Connection $connection */
  279. $connection = \OC::$server->get(Connection::class);
  280. try {
  281. $connection->close();
  282. } catch (\Exception $ex) {
  283. $output->writeln("<info>Error while disconnecting from database: {$ex->getMessage()}</info>");
  284. }
  285. while (!$connection->isConnected()) {
  286. try {
  287. $connection->connect();
  288. } catch (\Exception $ex) {
  289. $output->writeln("<info>Error while re-connecting to database: {$ex->getMessage()}</info>");
  290. sleep(60);
  291. }
  292. }
  293. return $connection;
  294. }
  295. }