2013-07-19 18:44:47 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 18:07:57 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Blaok <i@blaok.me>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2015-10-05 21:54:56 +03:00
|
|
|
* @author Olivier Paroz <github@oparoz.com>
|
2016-07-21 19:13:36 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Vincent Petry <pvince81@owncloud.com>
|
2015-03-26 13:44:34 +03: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,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2015-03-26 13:44:34 +03:00
|
|
|
*
|
2013-07-19 18:44:47 +04:00
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2013-07-19 18:44:47 +04:00
|
|
|
namespace OC\Files\Utils;
|
|
|
|
|
2016-05-30 12:14:31 +03:00
|
|
|
use OC\Files\Cache\Cache;
|
2013-07-19 18:44:47 +04:00
|
|
|
use OC\Files\Filesystem;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OC\Files\Storage\FailedStorage;
|
2014-06-25 17:22:49 +04:00
|
|
|
use OC\ForbiddenException;
|
2013-07-26 17:43:10 +04:00
|
|
|
use OC\Hooks\PublicEmitter;
|
2015-10-08 16:29:04 +03:00
|
|
|
use OC\Lock\DBLockingProvider;
|
2016-12-19 21:53:35 +03:00
|
|
|
use OCA\Files_Sharing\SharedStorage;
|
2019-12-11 13:27:05 +03:00
|
|
|
use OCP\EventDispatcher\IEventDispatcher;
|
|
|
|
use OCP\Files\Events\BeforeFileScannedEvent;
|
|
|
|
use OCP\Files\Events\BeforeFolderScannedEvent;
|
|
|
|
use OCP\Files\Events\NodeAddedToCache;
|
|
|
|
use OCP\Files\Events\FileCacheUpdated;
|
|
|
|
use OCP\Files\Events\NodeRemovedFromCache;
|
|
|
|
use OCP\Files\Events\FileScannedEvent;
|
|
|
|
use OCP\Files\Events\FolderScannedEvent;
|
2017-04-19 15:36:38 +03:00
|
|
|
use OCP\Files\NotFoundException;
|
2016-04-25 18:23:48 +03:00
|
|
|
use OCP\Files\Storage\IStorage;
|
2015-11-27 16:02:50 +03:00
|
|
|
use OCP\Files\StorageNotAvailableException;
|
2019-12-11 13:27:05 +03:00
|
|
|
use OCP\IDBConnection;
|
2015-11-27 16:02:50 +03:00
|
|
|
use OCP\ILogger;
|
2013-07-19 18:44:47 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class Scanner
|
|
|
|
*
|
|
|
|
* Hooks available in scope \OC\Utils\Scanner
|
|
|
|
* - scanFile(string $absolutePath)
|
|
|
|
* - scanFolder(string $absolutePath)
|
|
|
|
*
|
|
|
|
* @package OC\Files\Utils
|
|
|
|
*/
|
2013-07-26 17:43:10 +04:00
|
|
|
class Scanner extends PublicEmitter {
|
2017-03-28 20:18:58 +03:00
|
|
|
const MAX_ENTRIES_TO_COMMIT = 10000;
|
|
|
|
|
2019-12-11 13:27:05 +03:00
|
|
|
/** @var string $user */
|
2013-07-19 18:44:47 +04:00
|
|
|
private $user;
|
|
|
|
|
2019-12-11 13:27:05 +03:00
|
|
|
/** @var IDBConnection */
|
2014-09-08 03:34:03 +04:00
|
|
|
protected $db;
|
|
|
|
|
2019-12-11 13:27:05 +03:00
|
|
|
/** @var IEventDispatcher */
|
|
|
|
private $dispatcher;
|
|
|
|
|
|
|
|
/** @var ILogger */
|
2015-11-27 16:02:50 +03:00
|
|
|
protected $logger;
|
|
|
|
|
2017-03-28 20:18:58 +03:00
|
|
|
/**
|
|
|
|
* Whether to use a DB transaction
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $useTransaction;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Number of entries scanned to commit
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
protected $entriesToCommit;
|
|
|
|
|
2013-07-19 18:44:47 +04:00
|
|
|
/**
|
|
|
|
* @param string $user
|
2019-12-11 13:27:05 +03:00
|
|
|
* @param IDBConnection|null $db
|
|
|
|
* @param IEventDispatcher $dispatcher
|
2015-11-27 16:02:50 +03:00
|
|
|
* @param ILogger $logger
|
2013-07-19 18:44:47 +04:00
|
|
|
*/
|
2019-12-11 13:27:05 +03:00
|
|
|
public function __construct($user, $db, IEventDispatcher $dispatcher, ILogger $logger) {
|
2013-07-19 18:44:47 +04:00
|
|
|
$this->user = $user;
|
2014-09-08 03:34:03 +04:00
|
|
|
$this->db = $db;
|
2019-12-11 13:27:05 +03:00
|
|
|
$this->dispatcher = $dispatcher;
|
|
|
|
$this->logger = $logger;
|
2017-04-03 12:49:30 +03:00
|
|
|
// when DB locking is used, no DB transactions will be used
|
2017-03-28 20:18:58 +03:00
|
|
|
$this->useTransaction = !(\OC::$server->getLockingProvider() instanceof DBLockingProvider);
|
2013-07-19 18:44:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get all storages for $dir
|
|
|
|
*
|
|
|
|
* @param string $dir
|
2014-11-24 17:54:42 +03:00
|
|
|
* @return \OC\Files\Mount\MountPoint[]
|
2013-07-19 18:44:47 +04: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 15:02:38 +03:00
|
|
|
$mounts = $mountManager->findIn($dir);
|
|
|
|
$mounts[] = $mountManager->find($dir);
|
2013-07-19 18:44:47 +04:00
|
|
|
$mounts = array_reverse($mounts); //start with the mount of $dir
|
|
|
|
|
|
|
|
return $mounts;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* attach listeners to the scanner
|
|
|
|
*
|
2014-11-24 17:54:42 +03:00
|
|
|
* @param \OC\Files\Mount\MountPoint $mount
|
2013-07-19 18:44:47 +04:00
|
|
|
*/
|
|
|
|
protected function attachListener($mount) {
|
|
|
|
$scanner = $mount->getStorage()->getScanner();
|
2019-12-11 13:27:05 +03:00
|
|
|
$scanner->listen('\OC\Files\Cache\Scanner', 'scanFile', function ($path) use ($mount) {
|
|
|
|
$this->emit('\OC\Files\Utils\Scanner', 'scanFile', array($mount->getMountPoint() . $path));
|
|
|
|
$this->dispatcher->dispatchTyped(new BeforeFileScannedEvent($mount->getMountPoint() . $path));
|
2013-07-19 18:44:47 +04:00
|
|
|
});
|
2019-12-11 13:27:05 +03:00
|
|
|
$scanner->listen('\OC\Files\Cache\Scanner', 'scanFolder', function ($path) use ($mount) {
|
|
|
|
$this->emit('\OC\Files\Utils\Scanner', 'scanFolder', array($mount->getMountPoint() . $path));
|
|
|
|
$this->dispatcher->dispatchTyped(new BeforeFolderScannedEvent($mount->getMountPoint() . $path));
|
2013-07-19 18:44:47 +04:00
|
|
|
});
|
2019-12-11 13:27:05 +03:00
|
|
|
$scanner->listen('\OC\Files\Cache\Scanner', 'postScanFile', function ($path) use ($mount) {
|
|
|
|
$this->emit('\OC\Files\Utils\Scanner', 'postScanFile', array($mount->getMountPoint() . $path));
|
|
|
|
$this->dispatcher->dispatchTyped(new FileScannedEvent($mount->getMountPoint() . $path));
|
2015-09-30 02:53:49 +03:00
|
|
|
});
|
2019-12-11 13:27:05 +03:00
|
|
|
$scanner->listen('\OC\Files\Cache\Scanner', 'postScanFolder', function ($path) use ($mount) {
|
|
|
|
$this->emit('\OC\Files\Utils\Scanner', 'postScanFolder', array($mount->getMountPoint() . $path));
|
|
|
|
$this->dispatcher->dispatchTyped(new FolderScannedEvent($mount->getMountPoint() . $path));
|
2015-09-30 02:53:49 +03:00
|
|
|
});
|
2013-07-19 18:44:47 +04:00
|
|
|
}
|
|
|
|
|
2014-02-19 12:31:54 +04:00
|
|
|
/**
|
|
|
|
* @param string $dir
|
|
|
|
*/
|
2013-07-19 18:44:47 +04:00
|
|
|
public function backgroundScan($dir) {
|
|
|
|
$mounts = $this->getMounts($dir);
|
|
|
|
foreach ($mounts as $mount) {
|
2016-11-10 17:44:18 +03:00
|
|
|
$storage = $mount->getStorage();
|
|
|
|
if (is_null($storage)) {
|
2013-09-15 22:38:57 +04:00
|
|
|
continue;
|
|
|
|
}
|
2016-11-02 20:04:17 +03:00
|
|
|
|
|
|
|
// don't bother scanning failed storages (shortcut for same result)
|
2018-01-26 01:16:13 +03:00
|
|
|
if ($storage->instanceOfStorage(FailedStorage::class)) {
|
2016-11-02 20:04:17 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-11-10 17:44:18 +03:00
|
|
|
// don't scan received local shares, these can be scanned when scanning the owner's storage
|
2016-12-19 21:53:35 +03:00
|
|
|
if ($storage->instanceOfStorage(SharedStorage::class)) {
|
2016-02-22 16:30:32 +03:00
|
|
|
continue;
|
|
|
|
}
|
2016-05-18 15:31:43 +03:00
|
|
|
$scanner = $storage->getScanner();
|
2013-07-19 18:44:47 +04:00
|
|
|
$this->attachListener($mount);
|
2016-05-18 15:31:43 +03: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);
|
|
|
|
});
|
|
|
|
|
2016-07-18 12:35:14 +03:00
|
|
|
$propagator = $storage->getPropagator();
|
|
|
|
$propagator->beginBatch();
|
2013-07-19 18:44:47 +04:00
|
|
|
$scanner->backgroundScan();
|
2016-07-18 12:35:14 +03:00
|
|
|
$propagator->commitBatch();
|
2013-07-19 18:44:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-06 19:30:58 +04:00
|
|
|
/**
|
|
|
|
* @param string $dir
|
2018-06-28 17:09:25 +03:00
|
|
|
* @param $recursive
|
|
|
|
* @param callable|null $mountFilter
|
|
|
|
* @throws ForbiddenException
|
|
|
|
* @throws NotFoundException
|
2014-02-06 19:30:58 +04:00
|
|
|
*/
|
2018-06-28 17:09:25 +03:00
|
|
|
public function scan($dir = '', $recursive = \OC\Files\Cache\Scanner::SCAN_RECURSIVE, callable $mountFilter = null) {
|
2015-07-26 20:13:31 +03:00
|
|
|
if (!Filesystem::isValidPath($dir)) {
|
|
|
|
throw new \InvalidArgumentException('Invalid path to scan');
|
|
|
|
}
|
2013-07-19 18:44:47 +04:00
|
|
|
$mounts = $this->getMounts($dir);
|
|
|
|
foreach ($mounts as $mount) {
|
2018-06-28 17:09:25 +03:00
|
|
|
if ($mountFilter && !$mountFilter($mount)) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-11-10 17:44:18 +03:00
|
|
|
$storage = $mount->getStorage();
|
|
|
|
if (is_null($storage)) {
|
2013-09-15 22:38:57 +04:00
|
|
|
continue;
|
|
|
|
}
|
2016-11-02 20:04:17 +03:00
|
|
|
|
|
|
|
// don't bother scanning failed storages (shortcut for same result)
|
2018-01-26 01:16:13 +03:00
|
|
|
if ($storage->instanceOfStorage(FailedStorage::class)) {
|
2016-11-02 20:04:17 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-06-25 17:22:49 +04:00
|
|
|
// 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'))
|
|
|
|
) {
|
2016-06-08 15:19:42 +03:00
|
|
|
if ($storage->file_exists('') or $storage->getCache()->inCache('')) {
|
|
|
|
throw new ForbiddenException();
|
|
|
|
} else {// if the root exists in neither the cache nor the storage the user isn't setup yet
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-06-25 17:22:49 +04:00
|
|
|
}
|
2016-11-10 17:44:18 +03:00
|
|
|
|
|
|
|
// don't scan received local shares, these can be scanned when scanning the owner's storage
|
2016-12-19 21:53:35 +03:00
|
|
|
if ($storage->instanceOfStorage(SharedStorage::class)) {
|
2016-11-10 17:44:18 +03:00
|
|
|
continue;
|
|
|
|
}
|
2014-12-10 13:04:17 +03:00
|
|
|
$relativePath = $mount->getInternalPath($dir);
|
2014-06-25 17:22:49 +04:00
|
|
|
$scanner = $storage->getScanner();
|
2014-09-10 16:47:03 +04:00
|
|
|
$scanner->setUseTransactions(false);
|
2013-07-19 18:44:47 +04:00
|
|
|
$this->attachListener($mount);
|
2016-04-25 18:23:48 +03:00
|
|
|
|
|
|
|
$scanner->listen('\OC\Files\Cache\Scanner', 'removeFromCache', function ($path) use ($storage) {
|
2017-03-28 20:18:58 +03:00
|
|
|
$this->postProcessEntry($storage, $path);
|
2019-12-11 13:27:05 +03:00
|
|
|
$this->dispatcher->dispatchTyped(new NodeRemovedFromCache($storage, $path));
|
2016-04-25 18:23:48 +03:00
|
|
|
});
|
|
|
|
$scanner->listen('\OC\Files\Cache\Scanner', 'updateCache', function ($path) use ($storage) {
|
2017-03-28 20:18:58 +03:00
|
|
|
$this->postProcessEntry($storage, $path);
|
2019-12-11 13:27:05 +03:00
|
|
|
$this->dispatcher->dispatchTyped(new FileCacheUpdated($storage, $path));
|
2016-04-25 18:23:48 +03:00
|
|
|
});
|
|
|
|
$scanner->listen('\OC\Files\Cache\Scanner', 'addToCache', function ($path) use ($storage) {
|
2017-03-28 20:18:58 +03:00
|
|
|
$this->postProcessEntry($storage, $path);
|
2019-12-11 13:27:05 +03:00
|
|
|
$this->dispatcher->dispatchTyped(new NodeAddedToCache($storage, $path));
|
2016-04-25 18:23:48 +03:00
|
|
|
});
|
|
|
|
|
2017-04-20 14:25:49 +03:00
|
|
|
if (!$storage->file_exists($relativePath)) {
|
|
|
|
throw new NotFoundException($dir);
|
|
|
|
}
|
2017-03-28 20:18:58 +03:00
|
|
|
|
|
|
|
if ($this->useTransaction) {
|
2015-10-08 16:29:04 +03:00
|
|
|
$this->db->beginTransaction();
|
|
|
|
}
|
2015-11-27 16:02:50 +03:00
|
|
|
try {
|
2016-07-18 12:35:14 +03:00
|
|
|
$propagator = $storage->getPropagator();
|
|
|
|
$propagator->beginBatch();
|
2018-05-19 00:50:32 +03:00
|
|
|
$scanner->scan($relativePath, $recursive, \OC\Files\Cache\Scanner::REUSE_ETAG | \OC\Files\Cache\Scanner::REUSE_SIZE);
|
2016-05-30 12:14:31 +03:00
|
|
|
$cache = $storage->getCache();
|
|
|
|
if ($cache instanceof Cache) {
|
|
|
|
// only re-calculate for the root folder we scanned, anything below that is taken care of by the scanner
|
|
|
|
$cache->correctFolderSize($relativePath);
|
|
|
|
}
|
2016-07-18 12:35:14 +03:00
|
|
|
$propagator->commitBatch();
|
2015-11-27 16:02:50 +03:00
|
|
|
} catch (StorageNotAvailableException $e) {
|
|
|
|
$this->logger->error('Storage ' . $storage->getId() . ' not available');
|
2015-11-27 16:28:15 +03:00
|
|
|
$this->logger->logException($e);
|
2015-11-27 16:02:50 +03:00
|
|
|
$this->emit('\OC\Files\Utils\Scanner', 'StorageNotAvailable', [$e]);
|
|
|
|
}
|
2017-03-28 20:18:58 +03:00
|
|
|
if ($this->useTransaction) {
|
2015-10-08 16:29:04 +03:00
|
|
|
$this->db->commit();
|
|
|
|
}
|
2013-07-19 18:44:47 +04:00
|
|
|
}
|
|
|
|
}
|
2016-04-25 18:23:48 +03:00
|
|
|
|
|
|
|
private function triggerPropagator(IStorage $storage, $internalPath) {
|
|
|
|
$storage->getPropagator()->propagateChange($internalPath, time());
|
|
|
|
}
|
2017-03-28 20:18:58 +03:00
|
|
|
|
|
|
|
private function postProcessEntry(IStorage $storage, $internalPath) {
|
|
|
|
$this->triggerPropagator($storage, $internalPath);
|
|
|
|
if ($this->useTransaction) {
|
2017-04-03 12:49:30 +03:00
|
|
|
$this->entriesToCommit++;
|
2017-03-28 20:18:58 +03:00
|
|
|
if ($this->entriesToCommit >= self::MAX_ENTRIES_TO_COMMIT) {
|
2017-04-03 12:49:30 +03:00
|
|
|
$propagator = $storage->getPropagator();
|
2017-03-28 20:18:58 +03:00
|
|
|
$this->entriesToCommit = 0;
|
|
|
|
$this->db->commit();
|
|
|
|
$propagator->commitBatch();
|
|
|
|
$this->db->beginTransaction();
|
|
|
|
$propagator->beginBatch();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-19 18:44:47 +04:00
|
|
|
}
|