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