From ce2dba079605204293373652a53627eb50dd39d7 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Wed, 19 Apr 2017 14:36:38 +0200 Subject: [PATCH 1/3] show error when trying to scan non existing path Signed-off-by: Robin Appelman --- apps/files/lib/Command/Scan.php | 19 ++++++++++++------- lib/private/Files/Utils/Scanner.php | 4 ++++ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/apps/files/lib/Command/Scan.php b/apps/files/lib/Command/Scan.php index 24b47aca9a..d81beb0eaf 100644 --- a/apps/files/lib/Command/Scan.php +++ b/apps/files/lib/Command/Scan.php @@ -32,6 +32,7 @@ use Doctrine\DBAL\Connection; use OC\Core\Command\Base; use OC\Core\Command\InterruptedException; use OC\ForbiddenException; +use OCP\Files\NotFoundException; use OCP\Files\StorageNotAvailableException; use OCP\IDBConnection; use OCP\IUserManager; @@ -131,7 +132,7 @@ class Scan extends Base { $scanner->listen('\OC\Files\Utils\Scanner', 'StorageNotAvailable', function (StorageNotAvailableException $e) use ($output) { $output->writeln("Error while scanning, storage not available (" . $e->getMessage() . ")"); }); - # count only + # count only } else { $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function () use ($output) { $this->filesCounter += 1; @@ -146,17 +147,17 @@ class Scan extends Base { } }); } - $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function($path) use ($output) { + $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output) { $this->checkScanWarning($path, $output); }); - $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function($path) use ($output) { + $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function ($path) use ($output) { $this->checkScanWarning($path, $output); }); try { if ($backgroundScan) { $scanner->backgroundScan($path); - }else { + } else { $scanner->scan($path); } } catch (ForbiddenException $e) { @@ -165,6 +166,8 @@ class Scan extends Base { } catch (InterruptedException $e) { # exit the function if ctrl-c has been pressed $output->writeln('Interrupted by user'); + } catch (NotFoundException $e) { + $output->writeln('Path not found: ' . $e->getMessage() . ''); } catch (\Exception $e) { $output->writeln('Exception during scan: ' . $e->getMessage() . ''); $output->writeln('' . $e->getTraceAsString() . ''); @@ -194,7 +197,7 @@ class Scan extends Base { $verbose = $input->getOption('verbose'); $quiet = $input->getOption('quiet'); # restrict the verbosity level to VERBOSITY_VERBOSE - if ($output->getVerbosity()>OutputInterface::VERBOSITY_VERBOSE) { + if ($output->getVerbosity() > OutputInterface::VERBOSITY_VERBOSE) { $output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE); } if ($quiet) { @@ -223,7 +226,9 @@ class Scan extends Base { $user_count += 1; if ($this->userManager->userExists($user)) { # add an extra line when verbose is set to optical separate users - if ($verbose) {$output->writeln(""); } + if ($verbose) { + $output->writeln(""); + } $output->writeln("Starting scan for user $user_count out of $users_total ($user)"); # full: printout data if $verbose was set $this->scanFiles($user, $path, $verbose, $output, $input->getOption('unscanned')); @@ -327,7 +332,7 @@ class Scan extends Base { * @return \OCP\IDBConnection */ protected function reconnectToDatabase(OutputInterface $output) { - /** @var Connection | IDBConnection $connection*/ + /** @var Connection | IDBConnection $connection */ $connection = \OC::$server->getDatabaseConnection(); try { $connection->close(); diff --git a/lib/private/Files/Utils/Scanner.php b/lib/private/Files/Utils/Scanner.php index e76f3225c3..f845ac31fe 100644 --- a/lib/private/Files/Utils/Scanner.php +++ b/lib/private/Files/Utils/Scanner.php @@ -32,6 +32,7 @@ use OC\ForbiddenException; use OC\Hooks\PublicEmitter; use OC\Lock\DBLockingProvider; use OCA\Files_Sharing\SharedStorage; +use OCP\Files\NotFoundException; use OCP\Files\Storage\IStorage; use OCP\Files\StorageNotAvailableException; use OCP\ILogger; @@ -216,6 +217,9 @@ class Scanner extends PublicEmitter { try { $propagator = $storage->getPropagator(); $propagator->beginBatch(); + if (!$storage->file_exists($relativePath)) { + throw new NotFoundException($dir); + } $scanner->scan($relativePath, \OC\Files\Cache\Scanner::SCAN_RECURSIVE, \OC\Files\Cache\Scanner::REUSE_ETAG | \OC\Files\Cache\Scanner::REUSE_SIZE); $cache = $storage->getCache(); if ($cache instanceof Cache) { From fbedea08076355f9c94b7aa4d14f6157749a4fe7 Mon Sep 17 00:00:00 2001 From: Morris Jobke Date: Wed, 19 Apr 2017 17:04:16 -0500 Subject: [PATCH 2/3] Add PHPDoc and handle exception in ScanAppData as well Signed-off-by: Morris Jobke --- apps/files/lib/Command/ScanAppData.php | 2 ++ lib/private/Files/Utils/Scanner.php | 1 + 2 files changed, 3 insertions(+) diff --git a/apps/files/lib/Command/ScanAppData.php b/apps/files/lib/Command/ScanAppData.php index 365e2e3cb2..6ad83d9a18 100644 --- a/apps/files/lib/Command/ScanAppData.php +++ b/apps/files/lib/Command/ScanAppData.php @@ -128,6 +128,8 @@ class ScanAppData extends Base { } catch (InterruptedException $e) { # exit the function if ctrl-c has been pressed $output->writeln('Interrupted by user'); + } catch (NotFoundException $e) { + $output->writeln('Path not found: ' . $e->getMessage() . ''); } catch (\Exception $e) { $output->writeln('Exception during scan: ' . $e->getMessage() . ''); $output->writeln('' . $e->getTraceAsString() . ''); diff --git a/lib/private/Files/Utils/Scanner.php b/lib/private/Files/Utils/Scanner.php index f845ac31fe..5d126f2bcc 100644 --- a/lib/private/Files/Utils/Scanner.php +++ b/lib/private/Files/Utils/Scanner.php @@ -162,6 +162,7 @@ class Scanner extends PublicEmitter { /** * @param string $dir * @throws \OC\ForbiddenException + * @throws \OCP\Files\NotFoundException */ public function scan($dir = '') { if (!Filesystem::isValidPath($dir)) { From a0e5107c0b01d6ade0fe28f248c418ab5fd95d62 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Thu, 20 Apr 2017 13:25:49 +0200 Subject: [PATCH 3/3] check for existence before we start the db transaction Signed-off-by: Robin Appelman --- lib/private/Files/Utils/Scanner.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/private/Files/Utils/Scanner.php b/lib/private/Files/Utils/Scanner.php index 5d126f2bcc..02f355fd4d 100644 --- a/lib/private/Files/Utils/Scanner.php +++ b/lib/private/Files/Utils/Scanner.php @@ -212,15 +212,15 @@ class Scanner extends PublicEmitter { $this->triggerPropagator($storage, $path); }); + if (!$storage->file_exists($relativePath)) { + throw new NotFoundException($dir); + } if (!$isDbLocking) { $this->db->beginTransaction(); } try { $propagator = $storage->getPropagator(); $propagator->beginBatch(); - if (!$storage->file_exists($relativePath)) { - throw new NotFoundException($dir); - } $scanner->scan($relativePath, \OC\Files\Cache\Scanner::SCAN_RECURSIVE, \OC\Files\Cache\Scanner::REUSE_ETAG | \OC\Files\Cache\Scanner::REUSE_SIZE); $cache = $storage->getCache(); if ($cache instanceof Cache) {