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