2013-07-19 14:44:47 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2015-10-05 18:54:56 +00:00
|
|
|
* @author Olivier Paroz <github@oparoz.com>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Robin Appelman <icewind@owncloud.com>
|
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
|
|
|
*
|
2016-01-12 14:02:16 +00:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
2015-03-26 10:44:34 +00:00
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
2013-07-19 14:44:47 +00:00
|
|
|
*/
|
2015-02-26 10:37:37 +00:00
|
|
|
|
2013-07-19 14:44:47 +00:00
|
|
|
namespace OC\Files\Utils;
|
|
|
|
|
|
|
|
use OC\Files\Filesystem;
|
2014-06-25 13:22:49 +00:00
|
|
|
use OC\ForbiddenException;
|
2013-07-26 13:43:10 +00:00
|
|
|
use OC\Hooks\PublicEmitter;
|
2015-10-08 13:29:04 +00:00
|
|
|
use OC\Lock\DBLockingProvider;
|
2016-04-25 15:23:48 +00:00
|
|
|
use OCP\Files\Storage\IStorage;
|
2015-11-27 13:02:50 +00:00
|
|
|
use OCP\Files\StorageNotAvailableException;
|
|
|
|
use OCP\ILogger;
|
2013-07-19 14:44:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class Scanner
|
|
|
|
*
|
|
|
|
* Hooks available in scope \OC\Utils\Scanner
|
|
|
|
* - scanFile(string $absolutePath)
|
|
|
|
* - scanFolder(string $absolutePath)
|
|
|
|
*
|
|
|
|
* @package OC\Files\Utils
|
|
|
|
*/
|
2013-07-26 13:43:10 +00:00
|
|
|
class Scanner extends PublicEmitter {
|
2013-07-19 14:44:47 +00:00
|
|
|
/**
|
|
|
|
* @var string $user
|
|
|
|
*/
|
|
|
|
private $user;
|
|
|
|
|
2014-09-07 23:34:03 +00:00
|
|
|
/**
|
|
|
|
* @var \OCP\IDBConnection
|
|
|
|
*/
|
|
|
|
protected $db;
|
|
|
|
|
2015-11-27 13:02:50 +00:00
|
|
|
/**
|
|
|
|
* @var ILogger
|
|
|
|
*/
|
|
|
|
protected $logger;
|
|
|
|
|
2013-07-19 14:44:47 +00:00
|
|
|
/**
|
|
|
|
* @param string $user
|
2014-09-07 23:34:03 +00:00
|
|
|
* @param \OCP\IDBConnection $db
|
2015-11-27 13:02:50 +00:00
|
|
|
* @param ILogger $logger
|
2013-07-19 14:44:47 +00:00
|
|
|
*/
|
2015-11-27 13:02:50 +00:00
|
|
|
public function __construct($user, $db, ILogger $logger) {
|
|
|
|
$this->logger = $logger;
|
2013-07-19 14:44:47 +00:00
|
|
|
$this->user = $user;
|
2014-09-07 23:34:03 +00:00
|
|
|
$this->db = $db;
|
2013-07-19 14:44:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get all storages for $dir
|
|
|
|
*
|
|
|
|
* @param string $dir
|
2014-11-24 14:54:42 +00:00
|
|
|
* @return \OC\Files\Mount\MountPoint[]
|
2013-07-19 14:44:47 +00:00
|
|
|
*/
|
|
|
|
protected function getMounts($dir) {
|
|
|
|
//TODO: move to the node based fileapi once that's done
|
|
|
|
\OC_Util::tearDownFS();
|
|
|
|
\OC_Util::setupFS($this->user);
|
|
|
|
|
|
|
|
$mountManager = Filesystem::getMountManager();
|
2015-06-22 12:02:38 +00:00
|
|
|
$mounts = $mountManager->findIn($dir);
|
|
|
|
$mounts[] = $mountManager->find($dir);
|
2013-07-19 14:44:47 +00:00
|
|
|
$mounts = array_reverse($mounts); //start with the mount of $dir
|
|
|
|
|
|
|
|
return $mounts;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* attach listeners to the scanner
|
|
|
|
*
|
2014-11-24 14:54:42 +00:00
|
|
|
* @param \OC\Files\Mount\MountPoint $mount
|
2013-07-19 14:44:47 +00:00
|
|
|
*/
|
|
|
|
protected function attachListener($mount) {
|
|
|
|
$scanner = $mount->getStorage()->getScanner();
|
2013-07-26 13:43:10 +00:00
|
|
|
$emitter = $this;
|
2013-07-29 15:03:57 +00:00
|
|
|
$scanner->listen('\OC\Files\Cache\Scanner', 'scanFile', function ($path) use ($mount, $emitter) {
|
2013-07-26 13:43:10 +00:00
|
|
|
$emitter->emit('\OC\Files\Utils\Scanner', 'scanFile', array($mount->getMountPoint() . $path));
|
2013-07-19 14:44:47 +00:00
|
|
|
});
|
2013-07-29 15:03:57 +00:00
|
|
|
$scanner->listen('\OC\Files\Cache\Scanner', 'scanFolder', function ($path) use ($mount, $emitter) {
|
2013-07-26 13:43:10 +00:00
|
|
|
$emitter->emit('\OC\Files\Utils\Scanner', 'scanFolder', array($mount->getMountPoint() . $path));
|
2013-07-19 14:44:47 +00:00
|
|
|
});
|
2015-09-29 23:53:49 +00:00
|
|
|
$scanner->listen('\OC\Files\Cache\Scanner', 'postScanFile', function ($path) use ($mount, $emitter) {
|
|
|
|
$emitter->emit('\OC\Files\Utils\Scanner', 'postScanFile', array($mount->getMountPoint() . $path));
|
|
|
|
});
|
|
|
|
$scanner->listen('\OC\Files\Cache\Scanner', 'postScanFolder', function ($path) use ($mount, $emitter) {
|
|
|
|
$emitter->emit('\OC\Files\Utils\Scanner', 'postScanFolder', array($mount->getMountPoint() . $path));
|
|
|
|
});
|
2013-07-19 14:44:47 +00:00
|
|
|
}
|
|
|
|
|
2014-02-19 08:31:54 +00:00
|
|
|
/**
|
|
|
|
* @param string $dir
|
|
|
|
*/
|
2013-07-19 14:44:47 +00:00
|
|
|
public function backgroundScan($dir) {
|
|
|
|
$mounts = $this->getMounts($dir);
|
|
|
|
foreach ($mounts as $mount) {
|
2013-09-15 18:38:57 +00:00
|
|
|
if (is_null($mount->getStorage())) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-02-22 13:30:32 +00:00
|
|
|
// don't scan the root storage
|
|
|
|
if ($mount->getStorage()->instanceOfStorage('\OC\Files\Storage\Local') && $mount->getMountPoint() === '/') {
|
|
|
|
continue;
|
|
|
|
}
|
2013-07-19 14:44:47 +00:00
|
|
|
$scanner = $mount->getStorage()->getScanner();
|
|
|
|
$this->attachListener($mount);
|
|
|
|
$scanner->backgroundScan();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-06 15:30:58 +00:00
|
|
|
/**
|
|
|
|
* @param string $dir
|
2014-06-25 13:22:49 +00:00
|
|
|
* @throws \OC\ForbiddenException
|
2014-02-06 15:30:58 +00:00
|
|
|
*/
|
2014-10-31 16:33:33 +00:00
|
|
|
public function scan($dir = '') {
|
2015-07-26 17:13:31 +00:00
|
|
|
if (!Filesystem::isValidPath($dir)) {
|
|
|
|
throw new \InvalidArgumentException('Invalid path to scan');
|
|
|
|
}
|
2013-07-19 14:44:47 +00:00
|
|
|
$mounts = $this->getMounts($dir);
|
|
|
|
foreach ($mounts as $mount) {
|
2013-09-15 18:38:57 +00:00
|
|
|
if (is_null($mount->getStorage())) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-06-25 13:22:49 +00:00
|
|
|
$storage = $mount->getStorage();
|
|
|
|
// if the home storage isn't writable then the scanner is run as the wrong user
|
|
|
|
if ($storage->instanceOfStorage('\OC\Files\Storage\Home') and
|
|
|
|
(!$storage->isCreatable('') or !$storage->isCreatable('files'))
|
|
|
|
) {
|
|
|
|
throw new ForbiddenException();
|
|
|
|
}
|
2014-12-10 10:04:17 +00:00
|
|
|
$relativePath = $mount->getInternalPath($dir);
|
2014-06-25 13:22:49 +00:00
|
|
|
$scanner = $storage->getScanner();
|
2014-09-10 12:47:03 +00:00
|
|
|
$scanner->setUseTransactions(false);
|
2013-07-19 14:44:47 +00:00
|
|
|
$this->attachListener($mount);
|
2015-10-08 13:29:04 +00:00
|
|
|
$isDbLocking = \OC::$server->getLockingProvider() instanceof DBLockingProvider;
|
2016-04-25 15:23:48 +00:00
|
|
|
|
|
|
|
$scanner->listen('\OC\Files\Cache\Scanner', 'removeFromCache', function ($path) use ($storage) {
|
|
|
|
$this->triggerPropagator($storage, $path);
|
|
|
|
});
|
|
|
|
$scanner->listen('\OC\Files\Cache\Scanner', 'updateCache', function ($path) use ($storage) {
|
|
|
|
$this->triggerPropagator($storage, $path);
|
|
|
|
});
|
|
|
|
$scanner->listen('\OC\Files\Cache\Scanner', 'addToCache', function ($path) use ($storage) {
|
|
|
|
$this->triggerPropagator($storage, $path);
|
|
|
|
});
|
|
|
|
|
2015-10-08 13:29:04 +00:00
|
|
|
if (!$isDbLocking) {
|
|
|
|
$this->db->beginTransaction();
|
|
|
|
}
|
2015-11-27 13:02:50 +00:00
|
|
|
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');
|
2015-11-27 13:28:15 +00:00
|
|
|
$this->logger->logException($e);
|
2015-11-27 13:02:50 +00:00
|
|
|
$this->emit('\OC\Files\Utils\Scanner', 'StorageNotAvailable', [$e]);
|
|
|
|
}
|
2015-10-08 13:29:04 +00:00
|
|
|
if (!$isDbLocking) {
|
|
|
|
$this->db->commit();
|
|
|
|
}
|
2013-07-19 14:44:47 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-25 15:23:48 +00:00
|
|
|
|
|
|
|
private function triggerPropagator(IStorage $storage, $internalPath) {
|
|
|
|
$storage->getPropagator()->propagateChange($internalPath, time());
|
|
|
|
}
|
2013-07-19 14:44:47 +00:00
|
|
|
}
|
|
|
|
|