Skip to content

Commit

Permalink
Merge pull request #20789 from owncloud/scanner-skip-not-available
Browse files Browse the repository at this point in the history
Skip unavailable storages in scanner
  • Loading branch information
icewind1991 committed Nov 27, 2015
2 parents 99c28a6 + 816cd66 commit 4c43319
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 10 deletions.
2 changes: 1 addition & 1 deletion apps/files/ajax/scan.php
Expand Up @@ -47,7 +47,7 @@

foreach ($users as $user) {
$eventSource->send('user', $user);
$scanner = new \OC\Files\Utils\Scanner($user, \OC::$server->getDatabaseConnection());
$scanner = new \OC\Files\Utils\Scanner($user, \OC::$server->getDatabaseConnection(), \OC::$server->getLogger());
$scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', array($listener, 'file'));
try {
if ($force) {
Expand Down
6 changes: 5 additions & 1 deletion apps/files/command/scan.php
Expand Up @@ -26,6 +26,7 @@
namespace OCA\Files\Command;

use OC\ForbiddenException;
use OCP\Files\StorageNotAvailableException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -74,14 +75,17 @@ protected function configure() {
}

protected function scanFiles($user, $path, $quiet, OutputInterface $output) {
$scanner = new \OC\Files\Utils\Scanner($user, \OC::$server->getDatabaseConnection());
$scanner = new \OC\Files\Utils\Scanner($user, \OC::$server->getDatabaseConnection(), \OC::$server->getLogger());
if (!$quiet) {
$scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output) {
$output->writeln("Scanning file <info>$path</info>");
});
$scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function ($path) use ($output) {
$output->writeln("Scanning folder <info>$path</info>");
});
$scanner->listen('\OC\Files\Utils\Scanner', 'StorageNotAvailable', function (StorageNotAvailableException $e) use ($output) {
$output->writeln("Error while scanning, storage not available (" . $e->getMessage() . ")");
});
}
try {
$scanner->scan($path);
Expand Down
19 changes: 17 additions & 2 deletions lib/private/files/utils/scanner.php
Expand Up @@ -32,6 +32,8 @@
use OC\ForbiddenException;
use OC\Hooks\PublicEmitter;
use OC\Lock\DBLockingProvider;
use OCP\Files\StorageNotAvailableException;
use OCP\ILogger;

/**
* Class Scanner
Expand All @@ -58,11 +60,18 @@ class Scanner extends PublicEmitter {
*/
protected $db;

/**
* @var ILogger
*/
protected $logger;

/**
* @param string $user
* @param \OCP\IDBConnection $db
* @param ILogger $logger
*/
public function __construct($user, $db) {
public function __construct($user, $db, ILogger $logger) {
$this->logger = $logger;
$this->user = $user;
$this->propagator = new ChangePropagator(new View(''));
$this->db = $db;
Expand Down Expand Up @@ -161,7 +170,13 @@ public function scan($dir = '') {
if (!$isDbLocking) {
$this->db->beginTransaction();
}
$scanner->scan($relativePath, \OC\Files\Cache\Scanner::SCAN_RECURSIVE, \OC\Files\Cache\Scanner::REUSE_ETAG | \OC\Files\Cache\Scanner::REUSE_SIZE);
try {
$scanner->scan($relativePath, \OC\Files\Cache\Scanner::SCAN_RECURSIVE, \OC\Files\Cache\Scanner::REUSE_ETAG | \OC\Files\Cache\Scanner::REUSE_SIZE);
} catch (StorageNotAvailableException $e) {
$this->logger->error('Storage ' . $storage->getId() . ' not available');
$this->logger->logException($e);
$this->emit('\OC\Files\Utils\Scanner', 'StorageNotAvailable', [$e]);
}
if (!$isDbLocking) {
$this->db->commit();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/lib/files/etagtest.php
Expand Up @@ -59,7 +59,7 @@ public function testNewUser() {
$files = array('/foo.txt', '/folder/bar.txt', '/folder/subfolder', '/folder/subfolder/qwerty.txt');
$originalEtags = $this->getEtags($files);

$scanner = new \OC\Files\Utils\Scanner($user1, \OC::$server->getDatabaseConnection());
$scanner = new \OC\Files\Utils\Scanner($user1, \OC::$server->getDatabaseConnection(), \OC::$server->getLogger());
$scanner->backgroundScan('/');

$newEtags = $this->getEtags($files);
Expand Down
10 changes: 5 additions & 5 deletions tests/lib/files/utils/scanner.php
Expand Up @@ -70,7 +70,7 @@ public function testReuseExistingRoot() {
$storage->file_put_contents('foo.txt', 'qwerty');
$storage->file_put_contents('folder/bar.txt', 'qwerty');

$scanner = new TestScanner('', \OC::$server->getDatabaseConnection());
$scanner = new TestScanner('', \OC::$server->getDatabaseConnection(), \OC::$server->getLogger());
$scanner->addMount($mount);

$scanner->scan('');
Expand All @@ -92,7 +92,7 @@ public function testReuseExistingFile() {
$storage->file_put_contents('foo.txt', 'qwerty');
$storage->file_put_contents('folder/bar.txt', 'qwerty');

$scanner = new TestScanner('', \OC::$server->getDatabaseConnection());
$scanner = new TestScanner('', \OC::$server->getDatabaseConnection(), \OC::$server->getLogger());
$scanner->addMount($mount);

$scanner->scan('');
Expand Down Expand Up @@ -130,7 +130,7 @@ public function testScanSubMount() {
$storage->file_put_contents('foo.txt', 'qwerty');
$storage->file_put_contents('folder/bar.txt', 'qwerty');

$scanner = new \OC\Files\Utils\Scanner($uid, \OC::$server->getDatabaseConnection());
$scanner = new \OC\Files\Utils\Scanner($uid, \OC::$server->getDatabaseConnection(), \OC::$server->getLogger());

$this->assertFalse($cache->inCache('folder/bar.txt'));
$scanner->scan('/' . $uid . '/files/foo');
Expand All @@ -152,7 +152,7 @@ public function testChangePropagator() {
$storage->file_put_contents('foo.txt', 'qwerty');
$storage->file_put_contents('folder/bar.txt', 'qwerty');

$scanner = new TestScanner('', \OC::$server->getDatabaseConnection());
$scanner = new TestScanner('', \OC::$server->getDatabaseConnection(), \OC::$server->getLogger());
$originalPropagator = $scanner->getPropagator();
$scanner->setPropagator($propagator);
$scanner->addMount($mount);
Expand Down Expand Up @@ -214,7 +214,7 @@ public function invalidPathProvider() {
* @param string $invalidPath
*/
public function testInvalidPathScanning($invalidPath) {
$scanner = new TestScanner('', \OC::$server->getDatabaseConnection());
$scanner = new TestScanner('', \OC::$server->getDatabaseConnection(), \OC::$server->getLogger());
$scanner->scan($invalidPath);
}
}

0 comments on commit 4c43319

Please sign in to comment.