nextcloud/lib/private/files/utils/scanner.php

158 lines
4.5 KiB
PHP
Raw Normal View History

2013-07-19 18:44:47 +04:00
<?php
/**
2015-03-26 13:44:34 +03:00
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
* @author Morris Jobke <hey@morrisjobke.de>
* @author Robin Appelman <icewind@owncloud.com>
* @author Scrutinizer Auto-Fixer <auto-fixer@scrutinizer-ci.com>
* @author Thomas Müller <thomas.mueller@tmit.eu>
*
* @copyright Copyright (c) 2015, ownCloud, Inc.
* @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 18:44:47 +04:00
*/
2013-07-19 18:44:47 +04:00
namespace OC\Files\Utils;
2014-06-02 17:17:00 +04:00
use OC\Files\View;
use OC\Files\Cache\ChangePropagator;
2013-07-19 18:44:47 +04:00
use OC\Files\Filesystem;
use OC\ForbiddenException;
use OC\Hooks\PublicEmitter;
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
*/
class Scanner extends PublicEmitter {
2013-07-19 18:44:47 +04:00
/**
* @var string $user
*/
private $user;
2014-06-02 17:17:00 +04:00
/**
* @var \OC\Files\Cache\ChangePropagator
*/
protected $propagator;
/**
* @var \OCP\IDBConnection
*/
protected $db;
2013-07-19 18:44:47 +04:00
/**
* @param string $user
* @param \OCP\IDBConnection $db
2013-07-19 18:44:47 +04:00
*/
public function __construct($user, $db) {
2013-07-19 18:44:47 +04:00
$this->user = $user;
2014-06-02 17:17:00 +04:00
$this->propagator = new ChangePropagator(new View(''));
$this->db = $db;
2013-07-19 18:44:47 +04:00
}
/**
* get all storages for $dir
*
* @param string $dir
* @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();
$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
*
* @param \OC\Files\Mount\MountPoint $mount
2013-07-19 18:44:47 +04:00
*/
protected function attachListener($mount) {
$scanner = $mount->getStorage()->getScanner();
$emitter = $this;
$scanner->listen('\OC\Files\Cache\Scanner', 'scanFile', function ($path) use ($mount, $emitter) {
$emitter->emit('\OC\Files\Utils\Scanner', 'scanFile', array($mount->getMountPoint() . $path));
2013-07-19 18:44:47 +04:00
});
$scanner->listen('\OC\Files\Cache\Scanner', 'scanFolder', function ($path) use ($mount, $emitter) {
$emitter->emit('\OC\Files\Utils\Scanner', 'scanFolder', array($mount->getMountPoint() . $path));
2013-07-19 18:44:47 +04:00
});
2014-06-02 17:17:00 +04:00
// propagate etag and mtimes when files are changed or removed
$propagator = $this->propagator;
$propagatorListener = function ($path) use ($mount, $propagator) {
$fullPath = Filesystem::normalizePath($mount->getMountPoint() . $path);
$propagator->addChange($fullPath);
};
$scanner->listen('\OC\Files\Cache\Scanner', 'addToCache', $propagatorListener);
$scanner->listen('\OC\Files\Cache\Scanner', 'removeFromCache', $propagatorListener);
2013-07-19 18:44:47 +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) {
2013-09-15 22:38:57 +04:00
if (is_null($mount->getStorage())) {
continue;
}
2013-07-19 18:44:47 +04:00
$scanner = $mount->getStorage()->getScanner();
$this->attachListener($mount);
$scanner->backgroundScan();
}
2014-06-02 17:17:00 +04:00
$this->propagator->propagateChanges(time());
2013-07-19 18:44:47 +04:00
}
/**
* @param string $dir
* @throws \OC\ForbiddenException
*/
2014-10-31 19:33:33 +03:00
public function scan($dir = '') {
2013-07-19 18:44:47 +04:00
$mounts = $this->getMounts($dir);
foreach ($mounts as $mount) {
2013-09-15 22:38:57 +04:00
if (is_null($mount->getStorage())) {
continue;
}
$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 13:04:17 +03:00
$relativePath = $mount->getInternalPath($dir);
$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);
$this->db->beginTransaction();
2014-12-10 13:04:17 +03:00
$scanner->scan($relativePath, \OC\Files\Cache\Scanner::SCAN_RECURSIVE, \OC\Files\Cache\Scanner::REUSE_ETAG | \OC\Files\Cache\Scanner::REUSE_SIZE);
$this->db->commit();
2013-07-19 18:44:47 +04:00
}
2014-06-02 17:17:00 +04:00
$this->propagator->propagateChanges(time());
2013-07-19 18:44:47 +04:00
}
}