Completed
Push — master ( 4d85ff...143172 )
by Jan-Christoph
10:37
created

Root   B

Complexity

Total Complexity 38

Size/Duplication

Total Lines 307
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 10

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 307
rs 8.3999
c 2
b 0
f 0
wmc 38
lcom 3
cbo 10

33 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 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
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\NotFoundException;
35
use OCP\Files\NotPermittedException;
36
use OC\Hooks\PublicEmitter;
37
use OCP\Files\IRootFolder;
38
39
/**
40
 * Class Root
41
 *
42
 * Hooks available in scope \OC\Files
43
 * - preWrite(\OCP\Files\Node $node)
44
 * - postWrite(\OCP\Files\Node $node)
45
 * - preCreate(\OCP\Files\Node $node)
46
 * - postCreate(\OCP\Files\Node $node)
47
 * - preDelete(\OCP\Files\Node $node)
48
 * - postDelete(\OCP\Files\Node $node)
49
 * - preTouch(\OC\FilesP\Node $node, int $mtime)
50
 * - postTouch(\OCP\Files\Node $node)
51
 * - preCopy(\OCP\Files\Node $source, \OCP\Files\Node $target)
52
 * - postCopy(\OCP\Files\Node $source, \OCP\Files\Node $target)
53
 * - preRename(\OCP\Files\Node $source, \OCP\Files\Node $target)
54
 * - postRename(\OCP\Files\Node $source, \OCP\Files\Node $target)
55
 *
56
 * @package OC\Files\Node
57
 */
58
class Root extends Folder implements IRootFolder {
59
60
	/**
61
	 * @var \OC\Files\Mount\Manager $mountManager
62
	 */
63
	private $mountManager;
64
65
	/**
66
	 * @var \OC\Hooks\PublicEmitter
67
	 */
68
	private $emitter;
69
70
	/**
71
	 * @var \OC\User\User $user
72
	 */
73
	private $user;
74
75
	private $userFolderCache;
76
77
	/**
78
	 * @param \OC\Files\Mount\Manager $manager
79
	 * @param \OC\Files\View $view
80
	 * @param \OC\User\User|null $user
81
	 */
82
	public function __construct($manager, $view, $user) {
83
		parent::__construct($this, $view, '');
84
		$this->mountManager = $manager;
85
		$this->user = $user;
86
		$this->emitter = new PublicEmitter();
87
		$this->userFolderCache = new CappedMemoryCache();
88
	}
89
90
	/**
91
	 * Get the user for which the filesystem is setup
92
	 *
93
	 * @return \OC\User\User
94
	 */
95
	public function getUser() {
96
		return $this->user;
97
	}
98
99
	/**
100
	 * @param string $scope
101
	 * @param string $method
102
	 * @param callable $callback
103
	 */
104
	public function listen($scope, $method, callable $callback) {
105
		$this->emitter->listen($scope, $method, $callback);
106
	}
107
108
	/**
109
	 * @param string $scope optional
110
	 * @param string $method optional
111
	 * @param callable $callback optional
112
	 */
113
	public function removeListener($scope = null, $method = null, callable $callback = null) {
114
		$this->emitter->removeListener($scope, $method, $callback);
115
	}
116
117
	/**
118
	 * @param string $scope
119
	 * @param string $method
120
	 * @param Node[] $arguments
121
	 */
122
	public function emit($scope, $method, $arguments = array()) {
123
		$this->emitter->emit($scope, $method, $arguments);
124
	}
125
126
	/**
127
	 * @param \OC\Files\Storage\Storage $storage
128
	 * @param string $mountPoint
129
	 * @param array $arguments
130
	 */
131
	public function mount($storage, $mountPoint, $arguments = array()) {
132
		$mount = new MountPoint($storage, $mountPoint, $arguments);
133
		$this->mountManager->addMount($mount);
134
	}
135
136
	/**
137
	 * @param string $mountPoint
138
	 * @return \OC\Files\Mount\MountPoint
139
	 */
140
	public function getMount($mountPoint) {
141
		return $this->mountManager->find($mountPoint);
142
	}
143
144
	/**
145
	 * @param string $mountPoint
146
	 * @return \OC\Files\Mount\MountPoint[]
147
	 */
148
	public function getMountsIn($mountPoint) {
149
		return $this->mountManager->findIn($mountPoint);
150
	}
151
152
	/**
153
	 * @param string $storageId
154
	 * @return \OC\Files\Mount\MountPoint[]
155
	 */
156
	public function getMountByStorageId($storageId) {
157
		return $this->mountManager->findByStorageId($storageId);
158
	}
159
160
	/**
161
	 * @param int $numericId
162
	 * @return MountPoint[]
163
	 */
164
	public function getMountByNumericStorageId($numericId) {
165
		return $this->mountManager->findByNumericId($numericId);
166
	}
167
168
	/**
169
	 * @param \OC\Files\Mount\MountPoint $mount
170
	 */
171
	public function unMount($mount) {
172
		$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...
173
	}
174
175
	/**
176
	 * @param string $path
177
	 * @throws \OCP\Files\NotFoundException
178
	 * @throws \OCP\Files\NotPermittedException
179
	 * @return string
180
	 */
181
	public function get($path) {
182
		$path = $this->normalizePath($path);
183
		if ($this->isValidPath($path)) {
184
			$fullPath = $this->getFullPath($path);
185
			$fileInfo = $this->view->getFileInfo($fullPath);
186
			if ($fileInfo) {
187
				return $this->createNode($fullPath, $fileInfo);
188
			} else {
189
				throw new NotFoundException($path);
190
			}
191
		} else {
192
			throw new NotPermittedException();
193
		}
194
	}
195
196
	//most operations can't be done on the root
197
198
	/**
199
	 * @param string $targetPath
200
	 * @throws \OCP\Files\NotPermittedException
201
	 * @return \OC\Files\Node\Node
202
	 */
203
	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...
204
		throw new NotPermittedException();
205
	}
206
207
	public function delete() {
208
		throw new NotPermittedException();
209
	}
210
211
	/**
212
	 * @param string $targetPath
213
	 * @throws \OCP\Files\NotPermittedException
214
	 * @return \OC\Files\Node\Node
215
	 */
216
	public function copy($targetPath) {
217
		throw new NotPermittedException();
218
	}
219
220
	/**
221
	 * @param int $mtime
222
	 * @throws \OCP\Files\NotPermittedException
223
	 */
224
	public function touch($mtime = null) {
225
		throw new NotPermittedException();
226
	}
227
228
	/**
229
	 * @return \OC\Files\Storage\Storage
230
	 * @throws \OCP\Files\NotFoundException
231
	 */
232
	public function getStorage() {
233
		throw new NotFoundException();
234
	}
235
236
	/**
237
	 * @return string
238
	 */
239
	public function getPath() {
240
		return '/';
241
	}
242
243
	/**
244
	 * @return string
245
	 */
246
	public function getInternalPath() {
247
		return '';
248
	}
249
250
	/**
251
	 * @return int
252
	 */
253
	public function getId() {
254
		return null;
255
	}
256
257
	/**
258
	 * @return array
259
	 */
260
	public function stat() {
261
		return null;
262
	}
263
264
	/**
265
	 * @return int
266
	 */
267
	public function getMTime() {
268
		return null;
269
	}
270
271
	/**
272
	 * @return int
273
	 */
274
	public function getSize() {
275
		return null;
276
	}
277
278
	/**
279
	 * @return string
280
	 */
281
	public function getEtag() {
282
		return null;
283
	}
284
285
	/**
286
	 * @return int
287
	 */
288
	public function getPermissions() {
289
		return \OCP\Constants::PERMISSION_CREATE;
290
	}
291
292
	/**
293
	 * @return bool
294
	 */
295
	public function isReadable() {
296
		return false;
297
	}
298
299
	/**
300
	 * @return bool
301
	 */
302
	public function isUpdateable() {
303
		return false;
304
	}
305
306
	/**
307
	 * @return bool
308
	 */
309
	public function isDeletable() {
310
		return false;
311
	}
312
313
	/**
314
	 * @return bool
315
	 */
316
	public function isShareable() {
317
		return false;
318
	}
319
320
	/**
321
	 * @return Node
322
	 * @throws \OCP\Files\NotFoundException
323
	 */
324
	public function getParent() {
325
		throw new NotFoundException();
326
	}
327
328
	/**
329
	 * @return string
330
	 */
331
	public function getName() {
332
		return '';
333
	}
334
335
	/**
336
	 * Returns a view to user's files folder
337
	 *
338
	 * @param String $userId user ID
339
	 * @return \OCP\Files\Folder
340
	 */
341
	public function getUserFolder($userId) {
342
		if (!$this->userFolderCache->hasKey($userId)) {
343
			\OC\Files\Filesystem::initMountPoints($userId);
344
345
			try {
346
				$folder = $this->get('/' . $userId . '/files');
347
			} catch (NotFoundException $e) {
348
				if (!$this->nodeExists('/' . $userId)) {
349
					$this->newFolder('/' . $userId);
350
				}
351
				$folder = $this->newFolder('/' . $userId . '/files');
352
				\OC_Util::copySkeleton($userId, $folder);
353
			}
354
355
			$this->userFolderCache->set($userId, $folder);
356
		}
357
358
		return $this->userFolderCache->get($userId);
359
	}
360
361
	public function clearCache() {
362
		$this->userFolderCache = new CappedMemoryCache();
363
	}
364
}
365