Completed
Push — stable13 ( 9c0a9a...065ec7 )
by Morris
19:50 queued 05:17
created

Manager::clear()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Björn Schießle <[email protected]>
6
 * @author Morris Jobke <[email protected]>
7
 * @author Robin Appelman <[email protected]>
8
 * @author Robin McCorkell <[email protected]>
9
 *
10
 * @license AGPL-3.0
11
 *
12
 * This code is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License, version 3,
14
 * as published by the Free Software Foundation.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License, version 3,
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
23
 *
24
 */
25
26
namespace OC\Files\Mount;
27
28
use OC\Cache\CappedMemoryCache;
29
use \OC\Files\Filesystem;
30
use OCP\Files\Mount\IMountManager;
31
use OCP\Files\Mount\IMountPoint;
32
33
class Manager implements IMountManager {
34
	/** @var MountPoint[] */
35
	private $mounts = [];
36
37
	/** @var CappedMemoryCache */
38
	private $inPathCache;
39
40
	public function __construct() {
41
		$this->inPathCache = new CappedMemoryCache();
42
	}
43
44
	/**
45
	 * @param IMountPoint $mount
46
	 */
47
	public function addMount(IMountPoint $mount) {
48
		$this->mounts[$mount->getMountPoint()] = $mount;
49
		$this->inPathCache->clear();
50
	}
51
52
	/**
53
	 * @param string $mountPoint
54
	 */
55
	public function removeMount($mountPoint) {
56
		$mountPoint = Filesystem::normalizePath($mountPoint);
57
		if (strlen($mountPoint) > 1) {
58
			$mountPoint .= '/';
59
		}
60
		unset($this->mounts[$mountPoint]);
61
		$this->inPathCache->clear();
62
	}
63
64
	/**
65
	 * @param string $mountPoint
66
	 * @param string $target
67
	 */
68
	public function moveMount($mountPoint, $target) {
69
		$this->mounts[$target] = $this->mounts[$mountPoint];
70
		unset($this->mounts[$mountPoint]);
71
		$this->inPathCache->clear();
72
	}
73
74
	/**
75
	 * Find the mount for $path
76
	 *
77
	 * @param string $path
78
	 * @return MountPoint
79
	 */
80
	public function find($path) {
81
		\OC_Util::setupFS();
82
		$path = Filesystem::normalizePath($path);
83
84
		$current = $path;
85
		while (true) {
86
			$mountPoint = $current . '/';
87
			if (isset($this->mounts[$mountPoint])) {
88
				$this->pathCache[$path] = $this->mounts[$mountPoint];
0 ignored issues
show
Bug introduced by
The property pathCache does not seem to exist. Did you mean inPathCache?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
89
				return $this->mounts[$mountPoint];
90
			}
91
92
			if ($current === '') {
93
				return null;
94
			}
95
96
			$current = dirname($current);
97
			if ($current === '.' || $current === '/') {
98
				$current = '';
99
			}
100
		}
101
	}
102
103
	/**
104
	 * Find all mounts in $path
105
	 *
106
	 * @param string $path
107
	 * @return MountPoint[]
108
	 */
109
	public function findIn($path) {
110
		\OC_Util::setupFS();
111
		$path = $this->formatPath($path);
112
		$result = array();
113
		$pathLength = strlen($path);
114
		$mountPoints = array_keys($this->mounts);
115
		foreach ($mountPoints as $mountPoint) {
116
			if (substr($mountPoint, 0, $pathLength) === $path and strlen($mountPoint) > $pathLength) {
117
				$result[] = $this->mounts[$mountPoint];
118
			}
119
		}
120
121
		$this->inPathCache[$path] = $result;
122
		return $result;
123
	}
124
125
	public function clear() {
126
		$this->mounts = [];
127
		$this->inPathCache->clear();
128
	}
129
130
	/**
131
	 * Find mounts by storage id
132
	 *
133
	 * @param string $id
134
	 * @return MountPoint[]
135
	 */
136
	public function findByStorageId($id) {
137
		\OC_Util::setupFS();
138
		if (strlen($id) > 64) {
139
			$id = md5($id);
140
		}
141
		$result = array();
142
		foreach ($this->mounts as $mount) {
143
			if ($mount->getStorageId() === $id) {
144
				$result[] = $mount;
145
			}
146
		}
147
		return $result;
148
	}
149
150
	/**
151
	 * @return MountPoint[]
152
	 */
153
	public function getAll() {
154
		return $this->mounts;
155
	}
156
157
	/**
158
	 * Find mounts by numeric storage id
159
	 *
160
	 * @param int $id
161
	 * @return MountPoint[]
162
	 */
163
	public function findByNumericId($id) {
164
		$storageId = \OC\Files\Cache\Storage::getStorageId($id);
165
		return $this->findByStorageId($storageId);
166
	}
167
168
	/**
169
	 * @param string $path
170
	 * @return string
171
	 */
172 View Code Duplication
	private function formatPath($path) {
173
		$path = Filesystem::normalizePath($path);
174
		if (strlen($path) > 1) {
175
			$path .= '/';
176
		}
177
		return $path;
178
	}
179
}
180