Scan.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bart Visscher <bartv@thisnet.nl>
  6. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  7. * @author martin.mattel@diemattels.at <martin.mattel@diemattels.at>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. * @author Vincent Petry <pvince81@owncloud.com>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OCA\Files\Command;
  29. use Doctrine\DBAL\Connection;
  30. use OC\Core\Command\Base;
  31. use OC\Core\Command\InterruptedException;
  32. use OC\ForbiddenException;
  33. use OCP\Files\Mount\IMountPoint;
  34. use OCP\Files\NotFoundException;
  35. use OCP\Files\StorageNotAvailableException;
  36. use OCP\IDBConnection;
  37. use OCP\IUserManager;
  38. use Symfony\Component\Console\Input\InputArgument;
  39. use Symfony\Component\Console\Input\InputInterface;
  40. use Symfony\Component\Console\Input\InputOption;
  41. use Symfony\Component\Console\Output\OutputInterface;
  42. use Symfony\Component\Console\Helper\Table;
  43. class Scan extends Base {
  44. /** @var IUserManager $userManager */
  45. private $userManager;
  46. /** @var float */
  47. protected $execTime = 0;
  48. /** @var int */
  49. protected $foldersCounter = 0;
  50. /** @var int */
  51. protected $filesCounter = 0;
  52. public function __construct(IUserManager $userManager) {
  53. $this->userManager = $userManager;
  54. parent::__construct();
  55. }
  56. protected function configure() {
  57. parent::configure();
  58. $this
  59. ->setName('files:scan')
  60. ->setDescription('rescan filesystem')
  61. ->addArgument(
  62. 'user_id',
  63. InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
  64. 'will rescan all files of the given user(s)'
  65. )
  66. ->addOption(
  67. 'path',
  68. 'p',
  69. InputArgument::OPTIONAL,
  70. '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'
  71. )
  72. ->addOption(
  73. 'quiet',
  74. 'q',
  75. InputOption::VALUE_NONE,
  76. 'suppress any output'
  77. )
  78. ->addOption(
  79. 'verbose',
  80. '-v|vv|vvv',
  81. InputOption::VALUE_NONE,
  82. 'verbose the output'
  83. )
  84. ->addOption(
  85. 'all',
  86. null,
  87. InputOption::VALUE_NONE,
  88. 'will rescan all files of all known users'
  89. )->addOption(
  90. 'unscanned',
  91. null,
  92. InputOption::VALUE_NONE,
  93. 'only scan files which are marked as not fully scanned'
  94. )->addOption(
  95. 'shallow',
  96. null,
  97. InputOption::VALUE_NONE,
  98. 'do not scan folders recursively'
  99. )->addOption(
  100. 'home-only',
  101. null,
  102. InputOption::VALUE_NONE,
  103. 'only scan the home storage, ignoring any mounted external storage or share'
  104. );
  105. }
  106. public function checkScanWarning($fullPath, OutputInterface $output) {
  107. $normalizedPath = basename(\OC\Files\Filesystem::normalizePath($fullPath));
  108. $path = basename($fullPath);
  109. if ($normalizedPath !== $path) {
  110. $output->writeln("\t<error>Entry \"" . $fullPath . '" will not be accessible due to incompatible encoding</error>');
  111. }
  112. }
  113. protected function scanFiles($user, $path, $verbose, OutputInterface $output, $backgroundScan = false, $recursive = true, $homeOnly = false) {
  114. $connection = $this->reconnectToDatabase($output);
  115. $scanner = new \OC\Files\Utils\Scanner($user, $connection, \OC::$server->getLogger());
  116. # check on each file/folder if there was a user interrupt (ctrl-c) and throw an exception
  117. # printout and count
  118. if ($verbose) {
  119. $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output) {
  120. $output->writeln("\tFile <info>$path</info>");
  121. $this->filesCounter += 1;
  122. $this->abortIfInterrupted();
  123. });
  124. $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function ($path) use ($output) {
  125. $output->writeln("\tFolder <info>$path</info>");
  126. $this->foldersCounter += 1;
  127. $this->abortIfInterrupted();
  128. });
  129. $scanner->listen('\OC\Files\Utils\Scanner', 'StorageNotAvailable', function (StorageNotAvailableException $e) use ($output) {
  130. $output->writeln('Error while scanning, storage not available (' . $e->getMessage() . ')');
  131. });
  132. # count only
  133. } else {
  134. $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function () use ($output) {
  135. $this->filesCounter += 1;
  136. $this->abortIfInterrupted();
  137. });
  138. $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function () use ($output) {
  139. $this->foldersCounter += 1;
  140. $this->abortIfInterrupted();
  141. });
  142. }
  143. $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output) {
  144. $this->checkScanWarning($path, $output);
  145. });
  146. $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function ($path) use ($output) {
  147. $this->checkScanWarning($path, $output);
  148. });
  149. try {
  150. if ($backgroundScan) {
  151. $scanner->backgroundScan($path);
  152. } else {
  153. $scanner->scan($path, $recursive, $homeOnly ? [$this, 'filterHomeMount'] : null);
  154. }
  155. } catch (ForbiddenException $e) {
  156. $output->writeln("<error>Home storage for user $user not writable</error>");
  157. $output->writeln('Make sure you\'re running the scan command only as the user the web server runs as');
  158. } catch (InterruptedException $e) {
  159. # exit the function if ctrl-c has been pressed
  160. $output->writeln('Interrupted by user');
  161. } catch (NotFoundException $e) {
  162. $output->writeln('<error>Path not found: ' . $e->getMessage() . '</error>');
  163. } catch (\Exception $e) {
  164. $output->writeln('<error>Exception during scan: ' . $e->getMessage() . '</error>');
  165. $output->writeln('<error>' . $e->getTraceAsString() . '</error>');
  166. }
  167. }
  168. public function filterHomeMount(IMountPoint $mountPoint) {
  169. // any mountpoint inside '/$user/files/'
  170. return substr_count($mountPoint->getMountPoint(), '/') <= 3;
  171. }
  172. protected function execute(InputInterface $input, OutputInterface $output) {
  173. $inputPath = $input->getOption('path');
  174. if ($inputPath) {
  175. $inputPath = '/' . trim($inputPath, '/');
  176. list (, $user,) = explode('/', $inputPath, 3);
  177. $users = array($user);
  178. } else if ($input->getOption('all')) {
  179. $users = $this->userManager->search('');
  180. } else {
  181. $users = $input->getArgument('user_id');
  182. }
  183. # no messaging level option means: no full printout but statistics
  184. # $quiet means no print at all
  185. # $verbose means full printout including statistics
  186. # -q -v full stat
  187. # 0 0 no yes
  188. # 0 1 yes yes
  189. # 1 -- no no (quiet overrules verbose)
  190. $verbose = $input->getOption('verbose');
  191. $quiet = $input->getOption('quiet');
  192. # restrict the verbosity level to VERBOSITY_VERBOSE
  193. if ($output->getVerbosity() > OutputInterface::VERBOSITY_VERBOSE) {
  194. $output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE);
  195. }
  196. if ($quiet) {
  197. $verbose = false;
  198. }
  199. # check quantity of users to be process and show it on the command line
  200. $users_total = count($users);
  201. if ($users_total === 0) {
  202. $output->writeln("<error>Please specify the user id to scan, \"--all\" to scan for all users or \"--path=...\"</error>");
  203. return;
  204. } else {
  205. if ($users_total > 1) {
  206. $output->writeln("\nScanning files for $users_total users");
  207. }
  208. }
  209. $this->initTools();
  210. $user_count = 0;
  211. foreach ($users as $user) {
  212. if (is_object($user)) {
  213. $user = $user->getUID();
  214. }
  215. $path = $inputPath ? $inputPath : '/' . $user;
  216. $user_count += 1;
  217. if ($this->userManager->userExists($user)) {
  218. # add an extra line when verbose is set to optical separate users
  219. if ($verbose) {
  220. $output->writeln("");
  221. }
  222. $output->writeln("Starting scan for user $user_count out of $users_total ($user)");
  223. # full: printout data if $verbose was set
  224. $this->scanFiles($user, $path, $verbose, $output, $input->getOption('unscanned'), ! $input->getOption('shallow'), $input->getOption('home-only'));
  225. } else {
  226. $output->writeln("<error>Unknown user $user_count $user</error>");
  227. }
  228. try {
  229. $this->abortIfInterrupted();
  230. } catch(InterruptedException $e) {
  231. break;
  232. }
  233. }
  234. # stat: printout statistics if $quiet was not set
  235. if (!$quiet) {
  236. $this->presentStats($output);
  237. }
  238. }
  239. /**
  240. * Initialises some useful tools for the Command
  241. */
  242. protected function initTools() {
  243. // Start the timer
  244. $this->execTime = -microtime(true);
  245. // Convert PHP errors to exceptions
  246. set_error_handler([$this, 'exceptionErrorHandler'], E_ALL);
  247. }
  248. /**
  249. * Processes PHP errors as exceptions in order to be able to keep track of problems
  250. *
  251. * @see https://secure.php.net/manual/en/function.set-error-handler.php
  252. *
  253. * @param int $severity the level of the error raised
  254. * @param string $message
  255. * @param string $file the filename that the error was raised in
  256. * @param int $line the line number the error was raised
  257. *
  258. * @throws \ErrorException
  259. */
  260. public function exceptionErrorHandler($severity, $message, $file, $line) {
  261. if (!(error_reporting() & $severity)) {
  262. // This error code is not included in error_reporting
  263. return;
  264. }
  265. throw new \ErrorException($message, 0, $severity, $file, $line);
  266. }
  267. /**
  268. * @param OutputInterface $output
  269. */
  270. protected function presentStats(OutputInterface $output) {
  271. // Stop the timer
  272. $this->execTime += microtime(true);
  273. $output->writeln("");
  274. $headers = [
  275. 'Folders', 'Files', 'Elapsed time'
  276. ];
  277. $this->showSummary($headers, null, $output);
  278. }
  279. /**
  280. * Shows a summary of operations
  281. *
  282. * @param string[] $headers
  283. * @param string[] $rows
  284. * @param OutputInterface $output
  285. */
  286. protected function showSummary($headers, $rows, OutputInterface $output) {
  287. $niceDate = $this->formatExecTime();
  288. if (!$rows) {
  289. $rows = [
  290. $this->foldersCounter,
  291. $this->filesCounter,
  292. $niceDate,
  293. ];
  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. * @return string
  305. */
  306. protected function formatExecTime() {
  307. list($secs, ) = explode('.', sprintf("%.1f", $this->execTime));
  308. # if you want to have microseconds add this: . '.' . $tens;
  309. return date('H:i:s', $secs);
  310. }
  311. /**
  312. * @return \OCP\IDBConnection
  313. */
  314. protected function reconnectToDatabase(OutputInterface $output) {
  315. /** @var Connection | IDBConnection $connection */
  316. $connection = \OC::$server->getDatabaseConnection();
  317. try {
  318. $connection->close();
  319. } catch (\Exception $ex) {
  320. $output->writeln("<info>Error while disconnecting from database: {$ex->getMessage()}</info>");
  321. }
  322. while (!$connection->isConnected()) {
  323. try {
  324. $connection->connect();
  325. } catch (\Exception $ex) {
  326. $output->writeln("<info>Error while re-connecting to database: {$ex->getMessage()}</info>");
  327. sleep(60);
  328. }
  329. }
  330. return $connection;
  331. }
  332. }