Completed
Push — master ( 69e92e...e4c3c4 )
by Morris
169:48 queued 151:27
created

Scanner::scan()   D

Complexity

Conditions 18
Paths 45

Size

Total Lines 77

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 18
nc 45
nop 3
dl 0
loc 77
rs 4.8666
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 Roeland Jago Douma <[email protected]>
10
 * @author Thomas Müller <[email protected]>
11
 * @author Vincent Petry <[email protected]>
12
 *
13
 * @license AGPL-3.0
14
 *
15
 * This code is free software: you can redistribute it and/or modify
16
 * it under the terms of the GNU Affero General Public License, version 3,
17
 * as published by the Free Software Foundation.
18
 *
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
 * GNU Affero General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU Affero General Public License, version 3,
25
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
26
 *
27
 */
28
29
namespace OC\Files\Utils;
30
31
use OC\Files\Cache\Cache;
32
use OC\Files\Filesystem;
33
use OC\ForbiddenException;
34
use OC\Hooks\PublicEmitter;
35
use OC\Lock\DBLockingProvider;
36
use OCA\Files_Sharing\SharedStorage;
37
use OCP\Files\NotFoundException;
38
use OCP\Files\Storage\IStorage;
39
use OCP\Files\StorageNotAvailableException;
40
use OCP\ILogger;
41
use OC\Files\Storage\FailedStorage;
42
43
/**
44
 * Class Scanner
45
 *
46
 * Hooks available in scope \OC\Utils\Scanner
47
 *  - scanFile(string $absolutePath)
48
 *  - scanFolder(string $absolutePath)
49
 *
50
 * @package OC\Files\Utils
51
 */
52
class Scanner extends PublicEmitter {
53
	const MAX_ENTRIES_TO_COMMIT = 10000;
54
55
	/**
56
	 * @var string $user
57
	 */
58
	private $user;
59
60
	/**
61
	 * @var \OCP\IDBConnection
62
	 */
63
	protected $db;
64
65
	/**
66
	 * @var ILogger
67
	 */
68
	protected $logger;
69
70
	/**
71
	 * Whether to use a DB transaction
72
	 *
73
	 * @var bool
74
	 */
75
	protected $useTransaction;
76
77
	/**
78
	 * Number of entries scanned to commit
79
	 *
80
	 * @var int
81
	 */
82
	protected $entriesToCommit;
83
84
	/**
85
	 * @param string $user
86
	 * @param \OCP\IDBConnection $db
87
	 * @param ILogger $logger
88
	 */
89
	public function __construct($user, $db, ILogger $logger) {
90
		$this->logger = $logger;
91
		$this->user = $user;
92
		$this->db = $db;
93
		// when DB locking is used, no DB transactions will be used
94
		$this->useTransaction = !(\OC::$server->getLockingProvider() instanceof DBLockingProvider);
95
	}
96
97
	/**
98
	 * get all storages for $dir
99
	 *
100
	 * @param string $dir
101
	 * @return \OC\Files\Mount\MountPoint[]
102
	 */
103
	protected function getMounts($dir) {
104
		//TODO: move to the node based fileapi once that's done
105
		\OC_Util::tearDownFS();
106
		\OC_Util::setupFS($this->user);
107
108
		$mountManager = Filesystem::getMountManager();
109
		$mounts = $mountManager->findIn($dir);
110
		$mounts[] = $mountManager->find($dir);
111
		$mounts = array_reverse($mounts); //start with the mount of $dir
112
113
		return $mounts;
114
	}
115
116
	/**
117
	 * attach listeners to the scanner
118
	 *
119
	 * @param \OC\Files\Mount\MountPoint $mount
120
	 */
121
	protected function attachListener($mount) {
122
		$scanner = $mount->getStorage()->getScanner();
123
		$emitter = $this;
124
		$scanner->listen('\OC\Files\Cache\Scanner', 'scanFile', function ($path) use ($mount, $emitter) {
125
			$emitter->emit('\OC\Files\Utils\Scanner', 'scanFile', array($mount->getMountPoint() . $path));
126
		});
127
		$scanner->listen('\OC\Files\Cache\Scanner', 'scanFolder', function ($path) use ($mount, $emitter) {
128
			$emitter->emit('\OC\Files\Utils\Scanner', 'scanFolder', array($mount->getMountPoint() . $path));
129
		});
130
		$scanner->listen('\OC\Files\Cache\Scanner', 'postScanFile', function ($path) use ($mount, $emitter) {
131
			$emitter->emit('\OC\Files\Utils\Scanner', 'postScanFile', array($mount->getMountPoint() . $path));
132
		});
133
		$scanner->listen('\OC\Files\Cache\Scanner', 'postScanFolder', function ($path) use ($mount, $emitter) {
134
			$emitter->emit('\OC\Files\Utils\Scanner', 'postScanFolder', array($mount->getMountPoint() . $path));
135
		});
136
	}
137
138
	/**
139
	 * @param string $dir
140
	 */
141
	public function backgroundScan($dir) {
142
		$mounts = $this->getMounts($dir);
143
		foreach ($mounts as $mount) {
144
			$storage = $mount->getStorage();
145
			if (is_null($storage)) {
146
				continue;
147
			}
148
149
			// don't bother scanning failed storages (shortcut for same result)
150
			if ($storage->instanceOfStorage(FailedStorage::class)) {
151
				continue;
152
			}
153
154
			// don't scan the root storage
155
			if ($storage->instanceOfStorage('\OC\Files\Storage\Local') && $mount->getMountPoint() === '/') {
156
				continue;
157
			}
158
159
			// don't scan received local shares, these can be scanned when scanning the owner's storage
160
			if ($storage->instanceOfStorage(SharedStorage::class)) {
161
				continue;
162
			}
163
			$scanner = $storage->getScanner();
164
			$this->attachListener($mount);
0 ignored issues
show
Bug introduced by
It seems like $mount defined by $mount on line 143 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...
165
166
			$scanner->listen('\OC\Files\Cache\Scanner', 'removeFromCache', function ($path) use ($storage) {
167
				$this->triggerPropagator($storage, $path);
168
			});
169
			$scanner->listen('\OC\Files\Cache\Scanner', 'updateCache', function ($path) use ($storage) {
170
				$this->triggerPropagator($storage, $path);
171
			});
172
			$scanner->listen('\OC\Files\Cache\Scanner', 'addToCache', function ($path) use ($storage) {
173
				$this->triggerPropagator($storage, $path);
174
			});
175
176
			$propagator = $storage->getPropagator();
177
			$propagator->beginBatch();
178
			$scanner->backgroundScan();
179
			$propagator->commitBatch();
180
		}
181
	}
182
183
	/**
184
	 * @param string $dir
185
	 * @param $recursive
186
	 * @param callable|null $mountFilter
187
	 * @throws ForbiddenException
188
	 * @throws NotFoundException
189
	 */
190
	public function scan($dir = '', $recursive = \OC\Files\Cache\Scanner::SCAN_RECURSIVE, callable $mountFilter = null) {
191
		if (!Filesystem::isValidPath($dir)) {
192
			throw new \InvalidArgumentException('Invalid path to scan');
193
		}
194
		$mounts = $this->getMounts($dir);
195
		foreach ($mounts as $mount) {
196
			if ($mountFilter && !$mountFilter($mount)) {
197
				continue;
198
			}
199
			$storage = $mount->getStorage();
200
			if (is_null($storage)) {
201
				continue;
202
			}
203
204
			// don't bother scanning failed storages (shortcut for same result)
205
			if ($storage->instanceOfStorage(FailedStorage::class)) {
206
				continue;
207
			}
208
209
			// if the home storage isn't writable then the scanner is run as the wrong user
210
			if ($storage->instanceOfStorage('\OC\Files\Storage\Home') and
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
211
				(!$storage->isCreatable('') or !$storage->isCreatable('files'))
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
212
			) {
213
				if ($storage->file_exists('') or $storage->getCache()->inCache('')) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
214
					throw new ForbiddenException();
215
				} else {// if the root exists in neither the cache nor the storage the user isn't setup yet
216
					break;
217
				}
218
219
			}
220
221
			// don't scan received local shares, these can be scanned when scanning the owner's storage
222
			if ($storage->instanceOfStorage(SharedStorage::class)) {
223
				continue;
224
			}
225
			$relativePath = $mount->getInternalPath($dir);
226
			$scanner = $storage->getScanner();
227
			$scanner->setUseTransactions(false);
228
			$this->attachListener($mount);
0 ignored issues
show
Bug introduced by
It seems like $mount defined by $mount on line 195 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...
229
230
			$scanner->listen('\OC\Files\Cache\Scanner', 'removeFromCache', function ($path) use ($storage) {
231
				$this->postProcessEntry($storage, $path);
232
			});
233
			$scanner->listen('\OC\Files\Cache\Scanner', 'updateCache', function ($path) use ($storage) {
234
				$this->postProcessEntry($storage, $path);
235
			});
236
			$scanner->listen('\OC\Files\Cache\Scanner', 'addToCache', function ($path) use ($storage) {
237
				$this->postProcessEntry($storage, $path);
238
			});
239
240
			if (!$storage->file_exists($relativePath)) {
241
				throw new NotFoundException($dir);
242
			}
243
244
			if ($this->useTransaction) {
245
				$this->db->beginTransaction();
246
			}
247
			try {
248
				$propagator = $storage->getPropagator();
249
				$propagator->beginBatch();
250
				$scanner->scan($relativePath, $recursive, \OC\Files\Cache\Scanner::REUSE_ETAG | \OC\Files\Cache\Scanner::REUSE_SIZE);
251
				$cache = $storage->getCache();
252
				if ($cache instanceof Cache) {
253
					// only re-calculate for the root folder we scanned, anything below that is taken care of by the scanner
254
					$cache->correctFolderSize($relativePath);
255
				}
256
				$propagator->commitBatch();
257
			} catch (StorageNotAvailableException $e) {
258
				$this->logger->error('Storage ' . $storage->getId() . ' not available');
259
				$this->logger->logException($e);
0 ignored issues
show
Documentation introduced by
$e is of type object<OCP\Files\StorageNotAvailableException>, but the function expects a object<Throwable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
260
				$this->emit('\OC\Files\Utils\Scanner', 'StorageNotAvailable', [$e]);
261
			}
262
			if ($this->useTransaction) {
263
				$this->db->commit();
264
			}
265
		}
266
	}
267
268
	private function triggerPropagator(IStorage $storage, $internalPath) {
269
		$storage->getPropagator()->propagateChange($internalPath, time());
270
	}
271
272
	private function postProcessEntry(IStorage $storage, $internalPath) {
273
		$this->triggerPropagator($storage, $internalPath);
274
		if ($this->useTransaction) {
275
			$this->entriesToCommit++;
276
			if ($this->entriesToCommit >= self::MAX_ENTRIES_TO_COMMIT) {
277
				$propagator = $storage->getPropagator();
278
				$this->entriesToCommit = 0;
279
				$this->db->commit();
280
				$propagator->commitBatch();
281
				$this->db->beginTransaction();
282
				$propagator->beginBatch();
283
			}
284
		}
285
	}
286
}
287
288