-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Expand file tree
/
Copy pathManager.php
More file actions
286 lines (252 loc) · 6.92 KB
/
Copy pathManager.php
File metadata and controls
286 lines (252 loc) · 6.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
* SPDX-License-Identifier: AGPL-3.0-only
*/
namespace OC\Files\Mount;
use OC\Files\Filesystem;
use OC\Files\SetupManager;
use OC\Files\SetupManagerFactory;
use OCP\Cache\CappedMemoryCache;
use OCP\Files\Config\ICachedMountInfo;
use OCP\Files\Mount\IMountManager;
use OCP\Files\Mount\IMountPoint;
use OCP\Files\NotFoundException;
class Manager implements IMountManager {
/** @var array<string, IMountPoint> */
private array $mounts = [];
private array $mountsByProvider = [];
private bool $areMountsSorted = false;
/** @var list<string>|null $mountKeys */
private ?array $mountKeys = null;
/** @var CappedMemoryCache<IMountPoint> */
private CappedMemoryCache $pathCache;
/** @var CappedMemoryCache<IMountPoint[]> */
private CappedMemoryCache $inPathCache;
private SetupManager $setupManager;
public function __construct(SetupManagerFactory $setupManagerFactory) {
$this->pathCache = new CappedMemoryCache();
$this->inPathCache = new CappedMemoryCache();
$this->setupManager = $setupManagerFactory->create($this);
}
#[\Override]
public function addMount(IMountPoint $mount): void {
$mountPoint = $mount->getMountPoint();
$mountProvider = $mount->getMountProvider();
$this->mounts[$mountPoint] = $mount;
$this->mountsByProvider[$mountProvider] ??= [];
$this->mountsByProvider[$mountProvider][$mountPoint] = $mount;
$this->pathCache->clear();
$this->inPathCache->clear();
$this->areMountsSorted = false;
}
#[\Override]
public function removeMount(string $mountPoint): void {
$mountPoint = Filesystem::normalizePath($mountPoint);
if (\strlen($mountPoint) > 1) {
$mountPoint .= '/';
}
unset($this->mounts[$mountPoint]);
$this->pathCache->clear();
$this->inPathCache->clear();
$this->areMountsSorted = false;
}
#[\Override]
public function moveMount(string $mountPoint, string $target): void {
if ($mountPoint !== $target) {
$this->mounts[$target] = $this->mounts[$mountPoint];
$this->mounts[$target]->setMountPoint($target);
unset($this->mounts[$mountPoint]);
$this->pathCache->clear();
$this->inPathCache->clear();
$this->areMountsSorted = false;
}
}
/**
* Find the mount for $path
*/
#[\Override]
public function find(string $path): IMountPoint {
$this->setupManager->setupForPath($path);
$path = Filesystem::normalizePath($path);
if (isset($this->pathCache[$path])) {
return $this->pathCache[$path];
}
if (count($this->mounts) === 0) {
$this->setupManager->setupRoot();
if (count($this->mounts) === 0) {
throw new \Exception('No mounts even after explicitly setting up the root mounts');
}
}
$current = $path;
while (true) {
$mountPoint = $current . '/';
if (isset($this->mounts[$mountPoint])) {
$this->pathCache[$path] = $this->mounts[$mountPoint];
return $this->mounts[$mountPoint];
} elseif ($current === '') {
break;
}
$current = dirname($current);
if ($current === '.' || $current === '/') {
$current = '';
}
}
throw new NotFoundException('No mount for path ' . $path . ' existing mounts (' . count($this->mounts) . '): ' . implode(',', array_keys($this->mounts)));
}
/**
* Find all mounts in $path
*
* @return IMountPoint[]
*/
#[\Override]
public function findIn(string $path): array {
$this->setupManager->setupForPath($path, true);
$path = $this->formatPath($path);
if (isset($this->inPathCache[$path])) {
return $this->inPathCache[$path];
}
if (!$this->areMountsSorted) {
ksort($this->mounts, SORT_STRING);
$this->mountKeys = array_keys($this->mounts);
$this->areMountsSorted = true;
}
$result = $this->binarySearch($this->mounts, $this->mountKeys, $path);
$this->inPathCache[$path] = $result;
return $result;
}
/**
* Search for all entries in $sortedArray where $prefix is a prefix but not equal to their key.
*
* @template T
* @param array<string, T> $sortedArray
* @param list<string> $sortedKeys
* @param string $prefix
* @return list<T>
*/
private function binarySearch(array $sortedArray, array $sortedKeys, string $prefix): array {
$low = 0;
$high = count($sortedArray) - 1;
$start = null;
// binary search
while ($low <= $high) {
$mid = ($low + $high) >> 1;
if ($sortedKeys[$mid] < $prefix) {
$low = $mid + 1;
} else {
$start = $mid;
$high = $mid - 1;
}
}
$result = [];
if ($start !== null) {
for ($i = $start, $n = count($sortedKeys); $i < $n; $i++) {
$key = $sortedKeys[$i];
if (!str_starts_with($key, $prefix)) {
break;
}
if ($key !== $prefix) {
$result[] = $sortedArray[$key];
}
}
}
return $result;
}
#[\Override]
public function clear(): void {
$this->mounts = [];
$this->mountsByProvider = [];
$this->pathCache->clear();
$this->inPathCache->clear();
}
/**
* Find mounts by storage id
*
* @param string $id
* @return IMountPoint[]
*/
#[\Override]
public function findByStorageId(string $id): array {
if (\strlen($id) > 64) {
$id = md5($id);
}
$result = [];
foreach ($this->mounts as $mount) {
if ($mount->getStorageId() === $id) {
$result[] = $mount;
}
}
return $result;
}
#[\Override]
public function getAll(): array {
return $this->mounts;
}
/**
* Find mounts by numeric storage id
*
* @param int $id
* @return IMountPoint[]
*/
#[\Override]
public function findByNumericId(int $id): array {
$result = [];
foreach ($this->mounts as $mount) {
if ($mount->getNumericStorageId() === $id) {
$result[] = $mount;
}
}
return $result;
}
/**
* @param string $path
* @return string
*/
private function formatPath(string $path): string {
$path = Filesystem::normalizePath($path);
if (\strlen($path) > 1) {
$path .= '/';
}
return $path;
}
public function getSetupManager(): SetupManager {
return $this->setupManager;
}
/**
* Return all mounts in a path from a specific mount provider, indexed by mount point
*
* @param string $path
* @param string[] $mountProviders
* @return array<string, IMountPoint>
*/
public function getMountsByMountProvider(string $path, array $mountProviders): array {
$this->getSetupManager()->setupForProvider($path, $mountProviders);
if (\in_array('', $mountProviders)) {
return $this->mounts;
}
$mounts = [];
foreach ($mountProviders as $mountProvider) {
$mounts[] = $this->mountsByProvider[$mountProvider] ?? [];
}
return array_merge(...$mounts);
}
/**
* Return the mount matching a cached mount info (or mount file info)
*
* @param ICachedMountInfo $info
*
* @return IMountPoint|null
*/
#[\Override]
public function getMountFromMountInfo(ICachedMountInfo $info): ?IMountPoint {
$this->setupManager->setupForPath($info->getMountPoint());
foreach ($this->mounts as $mount) {
if ($mount->getMountPoint() === $info->getMountPoint()) {
return $mount;
}
}
return null;
}
}