Completed
Push — master ( 53eb0f...62e19d )
by Lukas
35:26 queued 21:10
created

Root   B

Complexity

Total Complexity 39

Size/Duplication

Total Lines 318
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 10

Importance

Changes 0
Metric Value
dl 0
loc 318
rs 8.2857
c 0
b 0
f 0
wmc 39
lcom 3
cbo 10

34 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A getUser() 0 3 1
A listen() 0 3 1
A removeListener() 0 3 1
A emit() 0 3 1
A mount() 0 4 1
A getMount() 0 3 1
A getMountsIn() 0 3 1
A getMountByStorageId() 0 3 1
A getMountByNumericStorageId() 0 3 1
A unMount() 0 3 1
A get() 0 14 3
A rename() 0 3 1
A delete() 0 3 1
A copy() 0 3 1
A touch() 0 3 1
A getStorage() 0 3 1
A getPath() 0 3 1
A getInternalPath() 0 3 1
A getId() 0 3 1
A stat() 0 3 1
A getMTime() 0 3 1
A getSize() 0 3 1
A getEtag() 0 3 1
A getPermissions() 0 3 1
A isReadable() 0 3 1
A isUpdateable() 0 3 1
A isDeletable() 0 3 1
A isShareable() 0 3 1
A getParent() 0 3 1
A getName() 0 3 1
A getUserFolder() 0 19 4
A clearCache() 0 3 1
A getUserMountCache() 0 3 1
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Bernhard Posselt <[email protected]>
6
 * @author Joas Schilling <[email protected]>
7
 * @author Jörn Friedrich Dreyer <[email protected]>
8
 * @author Morris Jobke <[email protected]>
9
 * @author Robin Appelman <[email protected]>
10
 * @author Roeland Jago Douma <[email protected]>
11
 * @author Stefan Weil <[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\Node;
30
31
use OC\Cache\CappedMemoryCache;
32
use OC\Files\Mount\Manager;
33
use OC\Files\Mount\MountPoint;
34
use OCP\Files\Config\IUserMountCache;
35
use OCP\Files\NotFoundException;
36
use OCP\Files\NotPermittedException;
37
use OC\Hooks\PublicEmitter;
38
use OCP\Files\IRootFolder;
39
40
/**
41
 * Class Root
42
 *
43
 * Hooks available in scope \OC\Files
44
 * - preWrite(\OCP\Files\Node $node)
45
 * - postWrite(\OCP\Files\Node $node)
46
 * - preCreate(\OCP\Files\Node $node)
47
 * - postCreate(\OCP\Files\Node $node)
48
 * - preDelete(\OCP\Files\Node $node)
49
 * - postDelete(\OCP\Files\Node $node)
50
 * - preTouch(\OC\FilesP\Node $node, int $mtime)
51
 * - postTouch(\OCP\Files\Node $node)
52
 * - preCopy(\OCP\Files\Node $source, \OCP\Files\Node $target)
53
 * - postCopy(\OCP\Files\Node $source, \OCP\Files\Node $target)
54
 * - preRename(\OCP\Files\Node $source, \OCP\Files\Node $target)
55
 * - postRename(\OCP\Files\Node $source, \OCP\Files\Node $target)
56
 *
57
 * @package OC\Files\Node
58
 */
59
class Root extends Folder implements IRootFolder {
60
61
	/**
62
	 * @var \OC\Files\Mount\Manager $mountManager
63
	 */
64
	private $mountManager;
65
66
	/**
67
	 * @var \OC\Hooks\PublicEmitter
68
	 */
69
	private $emitter;
70
71
	/**
72
	 * @var \OC\User\User $user
73
	 */
74
	private $user;
75
76
	private $userFolderCache;
77
78
	/**
79
	 * @var IUserMountCache
80
	 */
81
	private $userMountCache;
82
83
	/**
84
	 * @param \OC\Files\Mount\Manager $manager
85
	 * @param \OC\Files\View $view
86
	 * @param \OC\User\User|null $user
87
	 * @param IUserMountCache $userMountCache
88
	 */
89
	public function __construct($manager, $view, $user, IUserMountCache $userMountCache) {
90
		parent::__construct($this, $view, '');
91
		$this->mountManager = $manager;
92
		$this->user = $user;
93
		$this->emitter = new PublicEmitter();
94
		$this->userFolderCache = new CappedMemoryCache();
95
		$this->userMountCache = $userMountCache;
96
	}
97
98
	/**
99
	 * Get the user for which the filesystem is setup
100
	 *
101
	 * @return \OC\User\User
102
	 */
103
	public function getUser() {
104
		return $this->user;
105
	}
106
107
	/**
108
	 * @param string $scope
109
	 * @param string $method
110
	 * @param callable $callback
111
	 */
112
	public function listen($scope, $method, callable $callback) {
113
		$this->emitter->listen($scope, $method, $callback);
114
	}
115
116
	/**
117
	 * @param string $scope optional
118
	 * @param string $method optional
119
	 * @param callable $callback optional
120
	 */
121
	public function removeListener($scope = null, $method = null, callable $callback = null) {
122
		$this->emitter->removeListener($scope, $method, $callback);
123
	}
124
125
	/**
126
	 * @param string $scope
127
	 * @param string $method
128
	 * @param Node[] $arguments
129
	 */
130
	public function emit($scope, $method, $arguments = array()) {
131
		$this->emitter->emit($scope, $method, $arguments);
132
	}
133
134
	/**
135
	 * @param \OC\Files\Storage\Storage $storage
136
	 * @param string $mountPoint
137
	 * @param array $arguments
138
	 */
139
	public function mount($storage, $mountPoint, $arguments = array()) {
140
		$mount = new MountPoint($storage, $mountPoint, $arguments);
141
		$this->mountManager->addMount($mount);
142
	}
143
144
	/**
145
	 * @param string $mountPoint
146
	 * @return \OC\Files\Mount\MountPoint
147
	 */
148
	public function getMount($mountPoint) {
149
		return $this->mountManager->find($mountPoint);
150
	}
151
152
	/**
153
	 * @param string $mountPoint
154
	 * @return \OC\Files\Mount\MountPoint[]
155
	 */
156
	public function getMountsIn($mountPoint) {
157
		return $this->mountManager->findIn($mountPoint);
158
	}
159
160
	/**
161
	 * @param string $storageId
162
	 * @return \OC\Files\Mount\MountPoint[]
163
	 */
164
	public function getMountByStorageId($storageId) {
165
		return $this->mountManager->findByStorageId($storageId);
166
	}
167
168
	/**
169
	 * @param int $numericId
170
	 * @return MountPoint[]
171
	 */
172
	public function getMountByNumericStorageId($numericId) {
173
		return $this->mountManager->findByNumericId($numericId);
174
	}
175
176
	/**
177
	 * @param \OC\Files\Mount\MountPoint $mount
178
	 */
179
	public function unMount($mount) {
180
		$this->mountManager->remove($mount);
0 ignored issues
show
Bug introduced by
The method remove() does not exist on OC\Files\Mount\Manager. Did you maybe mean removeMount()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
181
	}
182
183
	/**
184
	 * @param string $path
185
	 * @throws \OCP\Files\NotFoundException
186
	 * @throws \OCP\Files\NotPermittedException
187
	 * @return string
188
	 */
189
	public function get($path) {
190
		$path = $this->normalizePath($path);
191
		if ($this->isValidPath($path)) {
192
			$fullPath = $this->getFullPath($path);
193
			$fileInfo = $this->view->getFileInfo($fullPath);
194
			if ($fileInfo) {
195
				return $this->createNode($fullPath, $fileInfo);
196
			} else {
197
				throw new NotFoundException($path);
198
			}
199
		} else {
200
			throw new NotPermittedException();
201
		}
202
	}
203
204
	//most operations can't be done on the root
205
206
	/**
207
	 * @param string $targetPath
208
	 * @throws \OCP\Files\NotPermittedException
209
	 * @return \OC\Files\Node\Node
210
	 */
211
	public function rename($targetPath) {
0 ignored issues
show
Unused Code introduced by
The parameter $targetPath is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
212
		throw new NotPermittedException();
213
	}
214
215
	public function delete() {
216
		throw new NotPermittedException();
217
	}
218
219
	/**
220
	 * @param string $targetPath
221
	 * @throws \OCP\Files\NotPermittedException
222
	 * @return \OC\Files\Node\Node
223
	 */
224
	public function copy($targetPath) {
225
		throw new NotPermittedException();
226
	}
227
228
	/**
229
	 * @param int $mtime
230
	 * @throws \OCP\Files\NotPermittedException
231
	 */
232
	public function touch($mtime = null) {
233
		throw new NotPermittedException();
234
	}
235
236
	/**
237
	 * @return \OC\Files\Storage\Storage
238
	 * @throws \OCP\Files\NotFoundException
239
	 */
240
	public function getStorage() {
241
		throw new NotFoundException();
242
	}
243
244
	/**
245
	 * @return string
246
	 */
247
	public function getPath() {
248
		return '/';
249
	}
250
251
	/**
252
	 * @return string
253
	 */
254
	public function getInternalPath() {
255
		return '';
256
	}
257
258
	/**
259
	 * @return int
260
	 */
261
	public function getId() {
262
		return null;
263
	}
264
265
	/**
266
	 * @return array
267
	 */
268
	public function stat() {
269
		return null;
270
	}
271
272
	/**
273
	 * @return int
274
	 */
275
	public function getMTime() {
276
		return null;
277
	}
278
279
	/**
280
	 * @return int
281
	 */
282
	public function getSize() {
283
		return null;
284
	}
285
286
	/**
287
	 * @return string
288
	 */
289
	public function getEtag() {
290
		return null;
291
	}
292
293
	/**
294
	 * @return int
295
	 */
296
	public function getPermissions() {
297
		return \OCP\Constants::PERMISSION_CREATE;
298
	}
299
300
	/**
301
	 * @return bool
302
	 */
303
	public function isReadable() {
304
		return false;
305
	}
306
307
	/**
308
	 * @return bool
309
	 */
310
	public function isUpdateable() {
311
		return false;
312
	}
313
314
	/**
315
	 * @return bool
316
	 */
317
	public function isDeletable() {
318
		return false;
319
	}
320
321
	/**
322
	 * @return bool
323
	 */
324
	public function isShareable() {
325
		return false;
326
	}
327
328
	/**
329
	 * @return Node
330
	 * @throws \OCP\Files\NotFoundException
331
	 */
332
	public function getParent() {
333
		throw new NotFoundException();
334
	}
335
336
	/**
337
	 * @return string
338
	 */
339
	public function getName() {
340
		return '';
341
	}
342
343
	/**
344
	 * Returns a view to user's files folder
345
	 *
346
	 * @param String $userId user ID
347
	 * @return \OCP\Files\Folder
348
	 */
349
	public function getUserFolder($userId) {
350
		if (!$this->userFolderCache->hasKey($userId)) {
351
			\OC\Files\Filesystem::initMountPoints($userId);
352
353
			try {
354
				$folder = $this->get('/' . $userId . '/files');
355
			} catch (NotFoundException $e) {
356
				if (!$this->nodeExists('/' . $userId)) {
357
					$this->newFolder('/' . $userId);
358
				}
359
				$folder = $this->newFolder('/' . $userId . '/files');
360
				\OC_Util::copySkeleton($userId, $folder);
361
			}
362
363
			$this->userFolderCache->set($userId, $folder);
364
		}
365
366
		return $this->userFolderCache->get($userId);
367
	}
368
369
	public function clearCache() {
370
		$this->userFolderCache = new CappedMemoryCache();
371
	}
372
373
	public function getUserMountCache() {
374
		return $this->userMountCache;
375
	}
376
}
377