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