123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 |
- <?php
- namespace OC\Files\Utils;
- use OC\Files\Cache\Cache;
- use OC\Files\Filesystem;
- use OC\Files\Storage\FailedStorage;
- use OC\Files\Storage\Home;
- use OC\ForbiddenException;
- use OC\Hooks\PublicEmitter;
- use OC\Lock\DBLockingProvider;
- use OCA\Files_Sharing\SharedStorage;
- 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;
- use OCP\Files\NotFoundException;
- use OCP\Files\Storage\IStorage;
- use OCP\Files\StorageNotAvailableException;
- use OCP\IDBConnection;
- use Psr\Log\LoggerInterface;
- class Scanner extends PublicEmitter {
- public const MAX_ENTRIES_TO_COMMIT = 10000;
-
- private $user;
-
- protected $db;
-
- private $dispatcher;
- protected LoggerInterface $logger;
-
- protected $useTransaction;
-
- protected $entriesToCommit;
-
- public function __construct($user, $db, IEventDispatcher $dispatcher, LoggerInterface $logger) {
- $this->user = $user;
- $this->db = $db;
- $this->dispatcher = $dispatcher;
- $this->logger = $logger;
-
- $this->useTransaction = !(\OC::$server->getLockingProvider() instanceof DBLockingProvider);
- }
-
- protected function getMounts($dir) {
-
- \OC_Util::tearDownFS();
- \OC_Util::setupFS($this->user);
- $mountManager = Filesystem::getMountManager();
- $mounts = $mountManager->findIn($dir);
- $mounts[] = $mountManager->find($dir);
- $mounts = array_reverse($mounts);
- return $mounts;
- }
-
- protected function attachListener($mount) {
- $scanner = $mount->getStorage()->getScanner();
- $scanner->listen('\OC\Files\Cache\Scanner', 'scanFile', function ($path) use ($mount) {
- $this->emit('\OC\Files\Utils\Scanner', 'scanFile', [$mount->getMountPoint() . $path]);
- $this->dispatcher->dispatchTyped(new BeforeFileScannedEvent($mount->getMountPoint() . $path));
- });
- $scanner->listen('\OC\Files\Cache\Scanner', 'scanFolder', function ($path) use ($mount) {
- $this->emit('\OC\Files\Utils\Scanner', 'scanFolder', [$mount->getMountPoint() . $path]);
- $this->dispatcher->dispatchTyped(new BeforeFolderScannedEvent($mount->getMountPoint() . $path));
- });
- $scanner->listen('\OC\Files\Cache\Scanner', 'postScanFile', function ($path) use ($mount) {
- $this->emit('\OC\Files\Utils\Scanner', 'postScanFile', [$mount->getMountPoint() . $path]);
- $this->dispatcher->dispatchTyped(new FileScannedEvent($mount->getMountPoint() . $path));
- });
- $scanner->listen('\OC\Files\Cache\Scanner', 'postScanFolder', function ($path) use ($mount) {
- $this->emit('\OC\Files\Utils\Scanner', 'postScanFolder', [$mount->getMountPoint() . $path]);
- $this->dispatcher->dispatchTyped(new FolderScannedEvent($mount->getMountPoint() . $path));
- });
- $scanner->listen('\OC\Files\Cache\Scanner', 'normalizedNameMismatch', function ($path) use ($mount) {
- $this->emit('\OC\Files\Utils\Scanner', 'normalizedNameMismatch', [$path]);
- });
- }
-
- public function backgroundScan($dir) {
- $mounts = $this->getMounts($dir);
- foreach ($mounts as $mount) {
- $storage = $mount->getStorage();
- if (is_null($storage)) {
- continue;
- }
-
- if ($storage->instanceOfStorage(FailedStorage::class)) {
- continue;
- }
- $scanner = $storage->getScanner();
- $this->attachListener($mount);
- $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);
- });
- $propagator = $storage->getPropagator();
- $propagator->beginBatch();
- $scanner->backgroundScan();
- $propagator->commitBatch();
- }
- }
-
- public function scan($dir = '', $recursive = \OC\Files\Cache\Scanner::SCAN_RECURSIVE, callable $mountFilter = null) {
- if (!Filesystem::isValidPath($dir)) {
- throw new \InvalidArgumentException('Invalid path to scan');
- }
- $mounts = $this->getMounts($dir);
- foreach ($mounts as $mount) {
- if ($mountFilter && !$mountFilter($mount)) {
- continue;
- }
- $storage = $mount->getStorage();
- if (is_null($storage)) {
- continue;
- }
-
- if ($storage->instanceOfStorage(FailedStorage::class)) {
- continue;
- }
-
- if ($storage->instanceOfStorage(Home::class)) {
-
- foreach (['', 'files'] as $path) {
- if (!$storage->isCreatable($path)) {
- $fullPath = $storage->getSourcePath($path);
- if (!$storage->is_dir($path) && $storage->getCache()->inCache($path)) {
- throw new NotFoundException("User folder $fullPath exists in cache but not on disk");
- } elseif ($storage->is_dir($path)) {
- $ownerUid = fileowner($fullPath);
- $owner = posix_getpwuid($ownerUid);
- $owner = $owner['name'] ?? $ownerUid;
- $permissions = decoct(fileperms($fullPath));
- throw new ForbiddenException("User folder $fullPath is not writable, folders is owned by $owner and has mode $permissions");
- } else {
-
- break 2;
- }
- }
- }
- }
-
- if ($storage->instanceOfStorage(SharedStorage::class)) {
- continue;
- }
- $relativePath = $mount->getInternalPath($dir);
- $scanner = $storage->getScanner();
- $scanner->setUseTransactions(false);
- $this->attachListener($mount);
- $scanner->listen('\OC\Files\Cache\Scanner', 'removeFromCache', function ($path) use ($storage) {
- $this->postProcessEntry($storage, $path);
- $this->dispatcher->dispatchTyped(new NodeRemovedFromCache($storage, $path));
- });
- $scanner->listen('\OC\Files\Cache\Scanner', 'updateCache', function ($path) use ($storage) {
- $this->postProcessEntry($storage, $path);
- $this->dispatcher->dispatchTyped(new FileCacheUpdated($storage, $path));
- });
- $scanner->listen('\OC\Files\Cache\Scanner', 'addToCache', function ($path) use ($storage) {
- $this->postProcessEntry($storage, $path);
- $this->dispatcher->dispatchTyped(new NodeAddedToCache($storage, $path));
- });
- if (!$storage->file_exists($relativePath)) {
- throw new NotFoundException($dir);
- }
- if ($this->useTransaction) {
- $this->db->beginTransaction();
- }
- try {
- $propagator = $storage->getPropagator();
- $propagator->beginBatch();
- $scanner->scan($relativePath, $recursive, \OC\Files\Cache\Scanner::REUSE_ETAG | \OC\Files\Cache\Scanner::REUSE_SIZE);
- $cache = $storage->getCache();
- if ($cache instanceof Cache) {
-
- $cache->correctFolderSize($relativePath);
- }
- $propagator->commitBatch();
- } catch (StorageNotAvailableException $e) {
- $this->logger->error('Storage ' . $storage->getId() . ' not available', ['exception' => $e]);
- $this->emit('\OC\Files\Utils\Scanner', 'StorageNotAvailable', [$e]);
- }
- if ($this->useTransaction) {
- $this->db->commit();
- }
- }
- }
- private function triggerPropagator(IStorage $storage, $internalPath) {
- $storage->getPropagator()->propagateChange($internalPath, time());
- }
- private function postProcessEntry(IStorage $storage, $internalPath) {
- $this->triggerPropagator($storage, $internalPath);
- if ($this->useTransaction) {
- $this->entriesToCommit++;
- if ($this->entriesToCommit >= self::MAX_ENTRIES_TO_COMMIT) {
- $propagator = $storage->getPropagator();
- $this->entriesToCommit = 0;
- $this->db->commit();
- $propagator->commitBatch();
- $this->db->beginTransaction();
- $propagator->beginBatch();
- }
- }
- }
- }
|