Completed
Push — stable12 ( a63043...ca2f2c )
by Morris
15:51
created

Manager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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