Completed
Push — master ( 5a8129...e548d2 )
by Joas
116:48 queued 100:17
created

Scanner   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 193
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 13

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 193
rs 10
wmc 26
lcom 1
cbo 13

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getMounts() 0 12 1
A attachListener() 0 16 1
C backgroundScan() 0 41 7
C scan() 0 71 15
A triggerPropagator() 0 3 1
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Jörn Friedrich Dreyer <[email protected]>
6
 * @author Morris Jobke <[email protected]>
7
 * @author Olivier Paroz <[email protected]>
8
 * @author Robin Appelman <[email protected]>
9
 * @author Thomas Müller <[email protected]>
10
 *
11
 * @license AGPL-3.0
12
 *
13
 * This code is free software: you can redistribute it and/or modify
14
 * it under the terms of the GNU Affero General Public License, version 3,
15
 * as published by the Free Software Foundation.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License, version 3,
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
24
 *
25
 */
26
27
namespace OC\Files\Utils;
28
29
use OC\Files\Cache\Cache;
30
use OC\Files\Filesystem;
31
use OC\ForbiddenException;
32
use OC\Hooks\PublicEmitter;
33
use OC\Lock\DBLockingProvider;
34
use OCA\Files_Sharing\SharedStorage;
35
use OCP\Files\Storage\IStorage;
36
use OCP\Files\StorageNotAvailableException;
37
use OCP\ILogger;
38
39
/**
40
 * Class Scanner
41
 *
42
 * Hooks available in scope \OC\Utils\Scanner
43
 *  - scanFile(string $absolutePath)
44
 *  - scanFolder(string $absolutePath)
45
 *
46
 * @package OC\Files\Utils
47
 */
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);
0 ignored issues
show
Bug introduced by
It seems like $mount defined by $mount on line 121 can be null; however, OC\Files\Utils\Scanner::attachListener() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $mount defined by $mount on line 170 can be null; however, OC\Files\Utils\Scanner::attachListener() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
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
}
241
242