@@ -46,196 +46,196 @@ |
||
| 46 | 46 | * @package OC\Files\Utils |
| 47 | 47 | */ |
| 48 | 48 | class Scanner extends PublicEmitter { |
| 49 | - /** |
|
| 50 | - * @var string $user |
|
| 51 | - */ |
|
| 52 | - private $user; |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * @var \OCP\IDBConnection |
|
| 56 | - */ |
|
| 57 | - protected $db; |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * @var ILogger |
|
| 61 | - */ |
|
| 62 | - protected $logger; |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * @param string $user |
|
| 66 | - * @param \OCP\IDBConnection $db |
|
| 67 | - * @param ILogger $logger |
|
| 68 | - */ |
|
| 69 | - public function __construct($user, $db, ILogger $logger) { |
|
| 70 | - $this->logger = $logger; |
|
| 71 | - $this->user = $user; |
|
| 72 | - $this->db = $db; |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * get all storages for $dir |
|
| 77 | - * |
|
| 78 | - * @param string $dir |
|
| 79 | - * @return \OC\Files\Mount\MountPoint[] |
|
| 80 | - */ |
|
| 81 | - protected function getMounts($dir) { |
|
| 82 | - //TODO: move to the node based fileapi once that's done |
|
| 83 | - \OC_Util::tearDownFS(); |
|
| 84 | - \OC_Util::setupFS($this->user); |
|
| 85 | - |
|
| 86 | - $mountManager = Filesystem::getMountManager(); |
|
| 87 | - $mounts = $mountManager->findIn($dir); |
|
| 88 | - $mounts[] = $mountManager->find($dir); |
|
| 89 | - $mounts = array_reverse($mounts); //start with the mount of $dir |
|
| 90 | - |
|
| 91 | - return $mounts; |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * attach listeners to the scanner |
|
| 96 | - * |
|
| 97 | - * @param \OC\Files\Mount\MountPoint $mount |
|
| 98 | - */ |
|
| 99 | - protected function attachListener($mount) { |
|
| 100 | - $scanner = $mount->getStorage()->getScanner(); |
|
| 101 | - $emitter = $this; |
|
| 102 | - $scanner->listen('\OC\Files\Cache\Scanner', 'scanFile', function ($path) use ($mount, $emitter) { |
|
| 103 | - $emitter->emit('\OC\Files\Utils\Scanner', 'scanFile', array($mount->getMountPoint() . $path)); |
|
| 104 | - }); |
|
| 105 | - $scanner->listen('\OC\Files\Cache\Scanner', 'scanFolder', function ($path) use ($mount, $emitter) { |
|
| 106 | - $emitter->emit('\OC\Files\Utils\Scanner', 'scanFolder', array($mount->getMountPoint() . $path)); |
|
| 107 | - }); |
|
| 108 | - $scanner->listen('\OC\Files\Cache\Scanner', 'postScanFile', function ($path) use ($mount, $emitter) { |
|
| 109 | - $emitter->emit('\OC\Files\Utils\Scanner', 'postScanFile', array($mount->getMountPoint() . $path)); |
|
| 110 | - }); |
|
| 111 | - $scanner->listen('\OC\Files\Cache\Scanner', 'postScanFolder', function ($path) use ($mount, $emitter) { |
|
| 112 | - $emitter->emit('\OC\Files\Utils\Scanner', 'postScanFolder', array($mount->getMountPoint() . $path)); |
|
| 113 | - }); |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * @param string $dir |
|
| 118 | - */ |
|
| 119 | - public function backgroundScan($dir) { |
|
| 120 | - $mounts = $this->getMounts($dir); |
|
| 121 | - foreach ($mounts as $mount) { |
|
| 122 | - $storage = $mount->getStorage(); |
|
| 123 | - if (is_null($storage)) { |
|
| 124 | - continue; |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - // don't bother scanning failed storages (shortcut for same result) |
|
| 128 | - if ($storage->instanceOfStorage('OC\Files\Storage\FailedStorage')) { |
|
| 129 | - continue; |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - // don't scan the root storage |
|
| 133 | - if ($storage->instanceOfStorage('\OC\Files\Storage\Local') && $mount->getMountPoint() === '/') { |
|
| 134 | - continue; |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - // don't scan received local shares, these can be scanned when scanning the owner's storage |
|
| 138 | - if ($storage->instanceOfStorage(SharedStorage::class)) { |
|
| 139 | - continue; |
|
| 140 | - } |
|
| 141 | - $scanner = $storage->getScanner(); |
|
| 142 | - $this->attachListener($mount); |
|
| 143 | - |
|
| 144 | - $scanner->listen('\OC\Files\Cache\Scanner', 'removeFromCache', function ($path) use ($storage) { |
|
| 145 | - $this->triggerPropagator($storage, $path); |
|
| 146 | - }); |
|
| 147 | - $scanner->listen('\OC\Files\Cache\Scanner', 'updateCache', function ($path) use ($storage) { |
|
| 148 | - $this->triggerPropagator($storage, $path); |
|
| 149 | - }); |
|
| 150 | - $scanner->listen('\OC\Files\Cache\Scanner', 'addToCache', function ($path) use ($storage) { |
|
| 151 | - $this->triggerPropagator($storage, $path); |
|
| 152 | - }); |
|
| 153 | - |
|
| 154 | - $propagator = $storage->getPropagator(); |
|
| 155 | - $propagator->beginBatch(); |
|
| 156 | - $scanner->backgroundScan(); |
|
| 157 | - $propagator->commitBatch(); |
|
| 158 | - } |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * @param string $dir |
|
| 163 | - * @throws \OC\ForbiddenException |
|
| 164 | - */ |
|
| 165 | - public function scan($dir = '') { |
|
| 166 | - if (!Filesystem::isValidPath($dir)) { |
|
| 167 | - throw new \InvalidArgumentException('Invalid path to scan'); |
|
| 168 | - } |
|
| 169 | - $mounts = $this->getMounts($dir); |
|
| 170 | - foreach ($mounts as $mount) { |
|
| 171 | - $storage = $mount->getStorage(); |
|
| 172 | - if (is_null($storage)) { |
|
| 173 | - continue; |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - // don't bother scanning failed storages (shortcut for same result) |
|
| 177 | - if ($storage->instanceOfStorage('OC\Files\Storage\FailedStorage')) { |
|
| 178 | - continue; |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - // if the home storage isn't writable then the scanner is run as the wrong user |
|
| 182 | - if ($storage->instanceOfStorage('\OC\Files\Storage\Home') and |
|
| 183 | - (!$storage->isCreatable('') or !$storage->isCreatable('files')) |
|
| 184 | - ) { |
|
| 185 | - if ($storage->file_exists('') or $storage->getCache()->inCache('')) { |
|
| 186 | - throw new ForbiddenException(); |
|
| 187 | - } else {// if the root exists in neither the cache nor the storage the user isn't setup yet |
|
| 188 | - break; |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - // don't scan received local shares, these can be scanned when scanning the owner's storage |
|
| 194 | - if ($storage->instanceOfStorage(SharedStorage::class)) { |
|
| 195 | - continue; |
|
| 196 | - } |
|
| 197 | - $relativePath = $mount->getInternalPath($dir); |
|
| 198 | - $scanner = $storage->getScanner(); |
|
| 199 | - $scanner->setUseTransactions(false); |
|
| 200 | - $this->attachListener($mount); |
|
| 201 | - $isDbLocking = \OC::$server->getLockingProvider() instanceof DBLockingProvider; |
|
| 202 | - |
|
| 203 | - $scanner->listen('\OC\Files\Cache\Scanner', 'removeFromCache', function ($path) use ($storage) { |
|
| 204 | - $this->triggerPropagator($storage, $path); |
|
| 205 | - }); |
|
| 206 | - $scanner->listen('\OC\Files\Cache\Scanner', 'updateCache', function ($path) use ($storage) { |
|
| 207 | - $this->triggerPropagator($storage, $path); |
|
| 208 | - }); |
|
| 209 | - $scanner->listen('\OC\Files\Cache\Scanner', 'addToCache', function ($path) use ($storage) { |
|
| 210 | - $this->triggerPropagator($storage, $path); |
|
| 211 | - }); |
|
| 212 | - |
|
| 213 | - if (!$isDbLocking) { |
|
| 214 | - $this->db->beginTransaction(); |
|
| 215 | - } |
|
| 216 | - try { |
|
| 217 | - $propagator = $storage->getPropagator(); |
|
| 218 | - $propagator->beginBatch(); |
|
| 219 | - $scanner->scan($relativePath, \OC\Files\Cache\Scanner::SCAN_RECURSIVE, \OC\Files\Cache\Scanner::REUSE_ETAG | \OC\Files\Cache\Scanner::REUSE_SIZE); |
|
| 220 | - $cache = $storage->getCache(); |
|
| 221 | - if ($cache instanceof Cache) { |
|
| 222 | - // only re-calculate for the root folder we scanned, anything below that is taken care of by the scanner |
|
| 223 | - $cache->correctFolderSize($relativePath); |
|
| 224 | - } |
|
| 225 | - $propagator->commitBatch(); |
|
| 226 | - } catch (StorageNotAvailableException $e) { |
|
| 227 | - $this->logger->error('Storage ' . $storage->getId() . ' not available'); |
|
| 228 | - $this->logger->logException($e); |
|
| 229 | - $this->emit('\OC\Files\Utils\Scanner', 'StorageNotAvailable', [$e]); |
|
| 230 | - } |
|
| 231 | - if (!$isDbLocking) { |
|
| 232 | - $this->db->commit(); |
|
| 233 | - } |
|
| 234 | - } |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - private function triggerPropagator(IStorage $storage, $internalPath) { |
|
| 238 | - $storage->getPropagator()->propagateChange($internalPath, time()); |
|
| 239 | - } |
|
| 49 | + /** |
|
| 50 | + * @var string $user |
|
| 51 | + */ |
|
| 52 | + private $user; |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * @var \OCP\IDBConnection |
|
| 56 | + */ |
|
| 57 | + protected $db; |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * @var ILogger |
|
| 61 | + */ |
|
| 62 | + protected $logger; |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * @param string $user |
|
| 66 | + * @param \OCP\IDBConnection $db |
|
| 67 | + * @param ILogger $logger |
|
| 68 | + */ |
|
| 69 | + public function __construct($user, $db, ILogger $logger) { |
|
| 70 | + $this->logger = $logger; |
|
| 71 | + $this->user = $user; |
|
| 72 | + $this->db = $db; |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * get all storages for $dir |
|
| 77 | + * |
|
| 78 | + * @param string $dir |
|
| 79 | + * @return \OC\Files\Mount\MountPoint[] |
|
| 80 | + */ |
|
| 81 | + protected function getMounts($dir) { |
|
| 82 | + //TODO: move to the node based fileapi once that's done |
|
| 83 | + \OC_Util::tearDownFS(); |
|
| 84 | + \OC_Util::setupFS($this->user); |
|
| 85 | + |
|
| 86 | + $mountManager = Filesystem::getMountManager(); |
|
| 87 | + $mounts = $mountManager->findIn($dir); |
|
| 88 | + $mounts[] = $mountManager->find($dir); |
|
| 89 | + $mounts = array_reverse($mounts); //start with the mount of $dir |
|
| 90 | + |
|
| 91 | + return $mounts; |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * attach listeners to the scanner |
|
| 96 | + * |
|
| 97 | + * @param \OC\Files\Mount\MountPoint $mount |
|
| 98 | + */ |
|
| 99 | + protected function attachListener($mount) { |
|
| 100 | + $scanner = $mount->getStorage()->getScanner(); |
|
| 101 | + $emitter = $this; |
|
| 102 | + $scanner->listen('\OC\Files\Cache\Scanner', 'scanFile', function ($path) use ($mount, $emitter) { |
|
| 103 | + $emitter->emit('\OC\Files\Utils\Scanner', 'scanFile', array($mount->getMountPoint() . $path)); |
|
| 104 | + }); |
|
| 105 | + $scanner->listen('\OC\Files\Cache\Scanner', 'scanFolder', function ($path) use ($mount, $emitter) { |
|
| 106 | + $emitter->emit('\OC\Files\Utils\Scanner', 'scanFolder', array($mount->getMountPoint() . $path)); |
|
| 107 | + }); |
|
| 108 | + $scanner->listen('\OC\Files\Cache\Scanner', 'postScanFile', function ($path) use ($mount, $emitter) { |
|
| 109 | + $emitter->emit('\OC\Files\Utils\Scanner', 'postScanFile', array($mount->getMountPoint() . $path)); |
|
| 110 | + }); |
|
| 111 | + $scanner->listen('\OC\Files\Cache\Scanner', 'postScanFolder', function ($path) use ($mount, $emitter) { |
|
| 112 | + $emitter->emit('\OC\Files\Utils\Scanner', 'postScanFolder', array($mount->getMountPoint() . $path)); |
|
| 113 | + }); |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * @param string $dir |
|
| 118 | + */ |
|
| 119 | + public function backgroundScan($dir) { |
|
| 120 | + $mounts = $this->getMounts($dir); |
|
| 121 | + foreach ($mounts as $mount) { |
|
| 122 | + $storage = $mount->getStorage(); |
|
| 123 | + if (is_null($storage)) { |
|
| 124 | + continue; |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + // don't bother scanning failed storages (shortcut for same result) |
|
| 128 | + if ($storage->instanceOfStorage('OC\Files\Storage\FailedStorage')) { |
|
| 129 | + continue; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + // don't scan the root storage |
|
| 133 | + if ($storage->instanceOfStorage('\OC\Files\Storage\Local') && $mount->getMountPoint() === '/') { |
|
| 134 | + continue; |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + // don't scan received local shares, these can be scanned when scanning the owner's storage |
|
| 138 | + if ($storage->instanceOfStorage(SharedStorage::class)) { |
|
| 139 | + continue; |
|
| 140 | + } |
|
| 141 | + $scanner = $storage->getScanner(); |
|
| 142 | + $this->attachListener($mount); |
|
| 143 | + |
|
| 144 | + $scanner->listen('\OC\Files\Cache\Scanner', 'removeFromCache', function ($path) use ($storage) { |
|
| 145 | + $this->triggerPropagator($storage, $path); |
|
| 146 | + }); |
|
| 147 | + $scanner->listen('\OC\Files\Cache\Scanner', 'updateCache', function ($path) use ($storage) { |
|
| 148 | + $this->triggerPropagator($storage, $path); |
|
| 149 | + }); |
|
| 150 | + $scanner->listen('\OC\Files\Cache\Scanner', 'addToCache', function ($path) use ($storage) { |
|
| 151 | + $this->triggerPropagator($storage, $path); |
|
| 152 | + }); |
|
| 153 | + |
|
| 154 | + $propagator = $storage->getPropagator(); |
|
| 155 | + $propagator->beginBatch(); |
|
| 156 | + $scanner->backgroundScan(); |
|
| 157 | + $propagator->commitBatch(); |
|
| 158 | + } |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * @param string $dir |
|
| 163 | + * @throws \OC\ForbiddenException |
|
| 164 | + */ |
|
| 165 | + public function scan($dir = '') { |
|
| 166 | + if (!Filesystem::isValidPath($dir)) { |
|
| 167 | + throw new \InvalidArgumentException('Invalid path to scan'); |
|
| 168 | + } |
|
| 169 | + $mounts = $this->getMounts($dir); |
|
| 170 | + foreach ($mounts as $mount) { |
|
| 171 | + $storage = $mount->getStorage(); |
|
| 172 | + if (is_null($storage)) { |
|
| 173 | + continue; |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + // don't bother scanning failed storages (shortcut for same result) |
|
| 177 | + if ($storage->instanceOfStorage('OC\Files\Storage\FailedStorage')) { |
|
| 178 | + continue; |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + // if the home storage isn't writable then the scanner is run as the wrong user |
|
| 182 | + if ($storage->instanceOfStorage('\OC\Files\Storage\Home') and |
|
| 183 | + (!$storage->isCreatable('') or !$storage->isCreatable('files')) |
|
| 184 | + ) { |
|
| 185 | + if ($storage->file_exists('') or $storage->getCache()->inCache('')) { |
|
| 186 | + throw new ForbiddenException(); |
|
| 187 | + } else {// if the root exists in neither the cache nor the storage the user isn't setup yet |
|
| 188 | + break; |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + // don't scan received local shares, these can be scanned when scanning the owner's storage |
|
| 194 | + if ($storage->instanceOfStorage(SharedStorage::class)) { |
|
| 195 | + continue; |
|
| 196 | + } |
|
| 197 | + $relativePath = $mount->getInternalPath($dir); |
|
| 198 | + $scanner = $storage->getScanner(); |
|
| 199 | + $scanner->setUseTransactions(false); |
|
| 200 | + $this->attachListener($mount); |
|
| 201 | + $isDbLocking = \OC::$server->getLockingProvider() instanceof DBLockingProvider; |
|
| 202 | + |
|
| 203 | + $scanner->listen('\OC\Files\Cache\Scanner', 'removeFromCache', function ($path) use ($storage) { |
|
| 204 | + $this->triggerPropagator($storage, $path); |
|
| 205 | + }); |
|
| 206 | + $scanner->listen('\OC\Files\Cache\Scanner', 'updateCache', function ($path) use ($storage) { |
|
| 207 | + $this->triggerPropagator($storage, $path); |
|
| 208 | + }); |
|
| 209 | + $scanner->listen('\OC\Files\Cache\Scanner', 'addToCache', function ($path) use ($storage) { |
|
| 210 | + $this->triggerPropagator($storage, $path); |
|
| 211 | + }); |
|
| 212 | + |
|
| 213 | + if (!$isDbLocking) { |
|
| 214 | + $this->db->beginTransaction(); |
|
| 215 | + } |
|
| 216 | + try { |
|
| 217 | + $propagator = $storage->getPropagator(); |
|
| 218 | + $propagator->beginBatch(); |
|
| 219 | + $scanner->scan($relativePath, \OC\Files\Cache\Scanner::SCAN_RECURSIVE, \OC\Files\Cache\Scanner::REUSE_ETAG | \OC\Files\Cache\Scanner::REUSE_SIZE); |
|
| 220 | + $cache = $storage->getCache(); |
|
| 221 | + if ($cache instanceof Cache) { |
|
| 222 | + // only re-calculate for the root folder we scanned, anything below that is taken care of by the scanner |
|
| 223 | + $cache->correctFolderSize($relativePath); |
|
| 224 | + } |
|
| 225 | + $propagator->commitBatch(); |
|
| 226 | + } catch (StorageNotAvailableException $e) { |
|
| 227 | + $this->logger->error('Storage ' . $storage->getId() . ' not available'); |
|
| 228 | + $this->logger->logException($e); |
|
| 229 | + $this->emit('\OC\Files\Utils\Scanner', 'StorageNotAvailable', [$e]); |
|
| 230 | + } |
|
| 231 | + if (!$isDbLocking) { |
|
| 232 | + $this->db->commit(); |
|
| 233 | + } |
|
| 234 | + } |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + private function triggerPropagator(IStorage $storage, $internalPath) { |
|
| 238 | + $storage->getPropagator()->propagateChange($internalPath, time()); |
|
| 239 | + } |
|
| 240 | 240 | } |
| 241 | 241 | |