1
0

scan.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * @author Bart Visscher <bartv@thisnet.nl>
  4. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. * @author Robin Appelman <icewind@owncloud.com>
  7. * @author Vincent Petry <pvince81@owncloud.com>
  8. *
  9. * @copyright Copyright (c) 2015, ownCloud, Inc.
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\Files\Command;
  26. use OC\ForbiddenException;
  27. use Symfony\Component\Console\Command\Command;
  28. use Symfony\Component\Console\Input\InputArgument;
  29. use Symfony\Component\Console\Input\InputInterface;
  30. use Symfony\Component\Console\Input\InputOption;
  31. use Symfony\Component\Console\Output\OutputInterface;
  32. class Scan extends Command {
  33. /**
  34. * @var \OC\User\Manager $userManager
  35. */
  36. private $userManager;
  37. public function __construct(\OC\User\Manager $userManager) {
  38. $this->userManager = $userManager;
  39. parent::__construct();
  40. }
  41. protected function configure() {
  42. $this
  43. ->setName('files:scan')
  44. ->setDescription('rescan filesystem')
  45. ->addArgument(
  46. 'user_id',
  47. InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
  48. 'will rescan all files of the given user(s)'
  49. )
  50. ->addOption(
  51. 'path',
  52. 'p',
  53. InputArgument::OPTIONAL,
  54. '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'
  55. )
  56. ->addOption(
  57. 'quiet',
  58. 'q',
  59. InputOption::VALUE_NONE,
  60. 'suppress output'
  61. )
  62. ->addOption(
  63. 'all',
  64. null,
  65. InputOption::VALUE_NONE,
  66. 'will rescan all files of all known users'
  67. );
  68. }
  69. protected function scanFiles($user, $path, $quiet, OutputInterface $output) {
  70. $scanner = new \OC\Files\Utils\Scanner($user, \OC::$server->getDatabaseConnection());
  71. if (!$quiet) {
  72. $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output) {
  73. $output->writeln("Scanning file <info>$path</info>");
  74. });
  75. $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function ($path) use ($output) {
  76. $output->writeln("Scanning folder <info>$path</info>");
  77. });
  78. }
  79. try {
  80. $scanner->scan($path);
  81. } catch (ForbiddenException $e) {
  82. $output->writeln("<error>Home storage for user $user not writable</error>");
  83. $output->writeln("Make sure you're running the scan command only as the user the web server runs as");
  84. }
  85. }
  86. protected function execute(InputInterface $input, OutputInterface $output) {
  87. $path = $input->getOption('path');
  88. if ($path) {
  89. $path = '/'.trim($path, '/');
  90. list (, $user, ) = explode('/', $path, 3);
  91. $users = array($user);
  92. } else if ($input->getOption('all')) {
  93. $users = $this->userManager->search('');
  94. } else {
  95. $users = $input->getArgument('user_id');
  96. }
  97. $quiet = $input->getOption('quiet');
  98. if (count($users) === 0) {
  99. $output->writeln("<error>Please specify the user id to scan, \"--all\" to scan for all users or \"--path=...\"</error>");
  100. return;
  101. }
  102. foreach ($users as $user) {
  103. if (is_object($user)) {
  104. $user = $user->getUID();
  105. }
  106. if ($this->userManager->userExists($user)) {
  107. $this->scanFiles($user, $path, $quiet, $output);
  108. } else {
  109. $output->writeln("<error>Unknown user $user</error>");
  110. }
  111. }
  112. }
  113. }