@@ -71,424 +71,424 @@ |
||
71 | 71 | * @package OC\Files\Node |
72 | 72 | */ |
73 | 73 | class Root extends Folder implements IRootFolder { |
74 | - private Manager $mountManager; |
|
75 | - private PublicEmitter $emitter; |
|
76 | - private ?IUser $user; |
|
77 | - private CappedMemoryCache $userFolderCache; |
|
78 | - private IUserMountCache $userMountCache; |
|
79 | - private LoggerInterface $logger; |
|
80 | - private IUserManager $userManager; |
|
81 | - private IEventDispatcher $eventDispatcher; |
|
82 | - |
|
83 | - /** |
|
84 | - * @param Manager $manager |
|
85 | - * @param View $view |
|
86 | - * @param IUser|null $user |
|
87 | - */ |
|
88 | - public function __construct( |
|
89 | - $manager, |
|
90 | - $view, |
|
91 | - $user, |
|
92 | - IUserMountCache $userMountCache, |
|
93 | - LoggerInterface $logger, |
|
94 | - IUserManager $userManager, |
|
95 | - IEventDispatcher $eventDispatcher |
|
96 | - ) { |
|
97 | - parent::__construct($this, $view, ''); |
|
98 | - $this->mountManager = $manager; |
|
99 | - $this->user = $user; |
|
100 | - $this->emitter = new PublicEmitter(); |
|
101 | - $this->userFolderCache = new CappedMemoryCache(); |
|
102 | - $this->userMountCache = $userMountCache; |
|
103 | - $this->logger = $logger; |
|
104 | - $this->userManager = $userManager; |
|
105 | - $eventDispatcher->addListener(FilesystemTornDownEvent::class, function () { |
|
106 | - $this->userFolderCache = new CappedMemoryCache(); |
|
107 | - }); |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * Get the user for which the filesystem is setup |
|
112 | - * |
|
113 | - * @return \OC\User\User |
|
114 | - */ |
|
115 | - public function getUser() { |
|
116 | - return $this->user; |
|
117 | - } |
|
118 | - |
|
119 | - /** |
|
120 | - * @param string $scope |
|
121 | - * @param string $method |
|
122 | - * @param callable $callback |
|
123 | - */ |
|
124 | - public function listen($scope, $method, callable $callback) { |
|
125 | - $this->emitter->listen($scope, $method, $callback); |
|
126 | - } |
|
127 | - |
|
128 | - /** |
|
129 | - * @param string $scope optional |
|
130 | - * @param string $method optional |
|
131 | - * @param callable $callback optional |
|
132 | - */ |
|
133 | - public function removeListener($scope = null, $method = null, callable $callback = null) { |
|
134 | - $this->emitter->removeListener($scope, $method, $callback); |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * @param string $scope |
|
139 | - * @param string $method |
|
140 | - * @param Node[] $arguments |
|
141 | - */ |
|
142 | - public function emit($scope, $method, $arguments = []) { |
|
143 | - $this->emitter->emit($scope, $method, $arguments); |
|
144 | - } |
|
145 | - |
|
146 | - /** |
|
147 | - * @param \OC\Files\Storage\Storage $storage |
|
148 | - * @param string $mountPoint |
|
149 | - * @param array $arguments |
|
150 | - */ |
|
151 | - public function mount($storage, $mountPoint, $arguments = []) { |
|
152 | - $mount = new MountPoint($storage, $mountPoint, $arguments); |
|
153 | - $this->mountManager->addMount($mount); |
|
154 | - } |
|
155 | - |
|
156 | - /** |
|
157 | - * @param string $mountPoint |
|
158 | - * @return \OC\Files\Mount\MountPoint |
|
159 | - */ |
|
160 | - public function getMount($mountPoint) { |
|
161 | - return $this->mountManager->find($mountPoint); |
|
162 | - } |
|
163 | - |
|
164 | - /** |
|
165 | - * @param string $mountPoint |
|
166 | - * @return \OC\Files\Mount\MountPoint[] |
|
167 | - */ |
|
168 | - public function getMountsIn($mountPoint) { |
|
169 | - return $this->mountManager->findIn($mountPoint); |
|
170 | - } |
|
171 | - |
|
172 | - /** |
|
173 | - * @param string $storageId |
|
174 | - * @return \OC\Files\Mount\MountPoint[] |
|
175 | - */ |
|
176 | - public function getMountByStorageId($storageId) { |
|
177 | - return $this->mountManager->findByStorageId($storageId); |
|
178 | - } |
|
179 | - |
|
180 | - /** |
|
181 | - * @param int $numericId |
|
182 | - * @return MountPoint[] |
|
183 | - */ |
|
184 | - public function getMountByNumericStorageId($numericId) { |
|
185 | - return $this->mountManager->findByNumericId($numericId); |
|
186 | - } |
|
187 | - |
|
188 | - /** |
|
189 | - * @param \OC\Files\Mount\MountPoint $mount |
|
190 | - */ |
|
191 | - public function unMount($mount) { |
|
192 | - $this->mountManager->remove($mount); |
|
193 | - } |
|
194 | - |
|
195 | - /** |
|
196 | - * @param string $path |
|
197 | - * @return Node |
|
198 | - * @throws \OCP\Files\NotPermittedException |
|
199 | - * @throws \OCP\Files\NotFoundException |
|
200 | - */ |
|
201 | - public function get($path) { |
|
202 | - $path = $this->normalizePath($path); |
|
203 | - if ($this->isValidPath($path)) { |
|
204 | - $fullPath = $this->getFullPath($path); |
|
205 | - $fileInfo = $this->view->getFileInfo($fullPath, false); |
|
206 | - if ($fileInfo) { |
|
207 | - return $this->createNode($fullPath, $fileInfo, false); |
|
208 | - } else { |
|
209 | - throw new NotFoundException($path); |
|
210 | - } |
|
211 | - } else { |
|
212 | - throw new NotPermittedException(); |
|
213 | - } |
|
214 | - } |
|
215 | - |
|
216 | - //most operations can't be done on the root |
|
217 | - |
|
218 | - /** |
|
219 | - * @param string $targetPath |
|
220 | - * @return Node |
|
221 | - * @throws \OCP\Files\NotPermittedException |
|
222 | - */ |
|
223 | - public function rename($targetPath) { |
|
224 | - throw new NotPermittedException(); |
|
225 | - } |
|
226 | - |
|
227 | - public function delete() { |
|
228 | - throw new NotPermittedException(); |
|
229 | - } |
|
230 | - |
|
231 | - /** |
|
232 | - * @param string $targetPath |
|
233 | - * @return Node |
|
234 | - * @throws \OCP\Files\NotPermittedException |
|
235 | - */ |
|
236 | - public function copy($targetPath) { |
|
237 | - throw new NotPermittedException(); |
|
238 | - } |
|
239 | - |
|
240 | - /** |
|
241 | - * @param int $mtime |
|
242 | - * @throws \OCP\Files\NotPermittedException |
|
243 | - */ |
|
244 | - public function touch($mtime = null) { |
|
245 | - throw new NotPermittedException(); |
|
246 | - } |
|
247 | - |
|
248 | - /** |
|
249 | - * @return \OC\Files\Storage\Storage |
|
250 | - * @throws \OCP\Files\NotFoundException |
|
251 | - */ |
|
252 | - public function getStorage() { |
|
253 | - throw new NotFoundException(); |
|
254 | - } |
|
255 | - |
|
256 | - /** |
|
257 | - * @return string |
|
258 | - */ |
|
259 | - public function getPath() { |
|
260 | - return '/'; |
|
261 | - } |
|
262 | - |
|
263 | - /** |
|
264 | - * @return string |
|
265 | - */ |
|
266 | - public function getInternalPath() { |
|
267 | - return ''; |
|
268 | - } |
|
269 | - |
|
270 | - /** |
|
271 | - * @return int |
|
272 | - */ |
|
273 | - public function getId() { |
|
274 | - return 0; |
|
275 | - } |
|
276 | - |
|
277 | - /** |
|
278 | - * @return array |
|
279 | - */ |
|
280 | - public function stat() { |
|
281 | - return []; |
|
282 | - } |
|
283 | - |
|
284 | - /** |
|
285 | - * @return int |
|
286 | - */ |
|
287 | - public function getMTime() { |
|
288 | - return 0; |
|
289 | - } |
|
290 | - |
|
291 | - /** |
|
292 | - * @param bool $includeMounts |
|
293 | - * @return int|float |
|
294 | - */ |
|
295 | - public function getSize($includeMounts = true): int|float { |
|
296 | - return 0; |
|
297 | - } |
|
298 | - |
|
299 | - /** |
|
300 | - * @return string |
|
301 | - */ |
|
302 | - public function getEtag() { |
|
303 | - return ''; |
|
304 | - } |
|
305 | - |
|
306 | - /** |
|
307 | - * @return int |
|
308 | - */ |
|
309 | - public function getPermissions() { |
|
310 | - return \OCP\Constants::PERMISSION_CREATE; |
|
311 | - } |
|
312 | - |
|
313 | - /** |
|
314 | - * @return bool |
|
315 | - */ |
|
316 | - public function isReadable() { |
|
317 | - return false; |
|
318 | - } |
|
319 | - |
|
320 | - /** |
|
321 | - * @return bool |
|
322 | - */ |
|
323 | - public function isUpdateable() { |
|
324 | - return false; |
|
325 | - } |
|
326 | - |
|
327 | - /** |
|
328 | - * @return bool |
|
329 | - */ |
|
330 | - public function isDeletable() { |
|
331 | - return false; |
|
332 | - } |
|
333 | - |
|
334 | - /** |
|
335 | - * @return bool |
|
336 | - */ |
|
337 | - public function isShareable() { |
|
338 | - return false; |
|
339 | - } |
|
340 | - |
|
341 | - /** |
|
342 | - * @return Node |
|
343 | - * @throws \OCP\Files\NotFoundException |
|
344 | - */ |
|
345 | - public function getParent() { |
|
346 | - throw new NotFoundException(); |
|
347 | - } |
|
348 | - |
|
349 | - /** |
|
350 | - * @return string |
|
351 | - */ |
|
352 | - public function getName() { |
|
353 | - return ''; |
|
354 | - } |
|
355 | - |
|
356 | - /** |
|
357 | - * Returns a view to user's files folder |
|
358 | - * |
|
359 | - * @param string $userId user ID |
|
360 | - * @return \OCP\Files\Folder |
|
361 | - * @throws NoUserException |
|
362 | - * @throws NotPermittedException |
|
363 | - */ |
|
364 | - public function getUserFolder($userId) { |
|
365 | - $userObject = $this->userManager->get($userId); |
|
366 | - |
|
367 | - if (is_null($userObject)) { |
|
368 | - $e = new NoUserException('Backends provided no user object'); |
|
369 | - $this->logger->error( |
|
370 | - sprintf( |
|
371 | - 'Backends provided no user object for %s', |
|
372 | - $userId |
|
373 | - ), |
|
374 | - [ |
|
375 | - 'app' => 'files', |
|
376 | - 'exception' => $e, |
|
377 | - ] |
|
378 | - ); |
|
379 | - throw $e; |
|
380 | - } |
|
381 | - |
|
382 | - $userId = $userObject->getUID(); |
|
383 | - |
|
384 | - if (!$this->userFolderCache->hasKey($userId)) { |
|
385 | - if ($this->mountManager->getSetupManager()->isSetupComplete($userObject)) { |
|
386 | - try { |
|
387 | - $folder = $this->get('/' . $userId . '/files'); |
|
388 | - if (!$folder instanceof \OCP\Files\Folder) { |
|
389 | - throw new \Exception("User folder for $userId exists as a file"); |
|
390 | - } |
|
391 | - } catch (NotFoundException $e) { |
|
392 | - if (!$this->nodeExists('/' . $userId)) { |
|
393 | - $this->newFolder('/' . $userId); |
|
394 | - } |
|
395 | - $folder = $this->newFolder('/' . $userId . '/files'); |
|
396 | - } |
|
397 | - } else { |
|
398 | - $folder = new LazyUserFolder($this, $userObject, $this->mountManager); |
|
399 | - } |
|
400 | - |
|
401 | - $this->userFolderCache->set($userId, $folder); |
|
402 | - } |
|
403 | - |
|
404 | - return $this->userFolderCache->get($userId); |
|
405 | - } |
|
406 | - |
|
407 | - public function getUserMountCache() { |
|
408 | - return $this->userMountCache; |
|
409 | - } |
|
410 | - |
|
411 | - /** |
|
412 | - * @param int $id |
|
413 | - * @return Node[] |
|
414 | - */ |
|
415 | - public function getByIdInPath(int $id, string $path): array { |
|
416 | - $mountCache = $this->getUserMountCache(); |
|
417 | - if (strpos($path, '/', 1) > 0) { |
|
418 | - [, $user] = explode('/', $path); |
|
419 | - } else { |
|
420 | - $user = null; |
|
421 | - } |
|
422 | - $mountsContainingFile = $mountCache->getMountsForFileId($id, $user); |
|
423 | - |
|
424 | - // if the mount isn't in the cache yet, perform a setup first, then try again |
|
425 | - if (count($mountsContainingFile) === 0) { |
|
426 | - $this->mountManager->getSetupManager()->setupForPath($path, true); |
|
427 | - $mountsContainingFile = $mountCache->getMountsForFileId($id, $user); |
|
428 | - } |
|
429 | - |
|
430 | - // when a user has access through the same storage through multiple paths |
|
431 | - // (such as an external storage that is both mounted for a user and shared to the user) |
|
432 | - // the mount cache will only hold a single entry for the storage |
|
433 | - // this can lead to issues as the different ways the user has access to a storage can have different permissions |
|
434 | - // |
|
435 | - // so instead of using the cached entries directly, we instead filter the current mounts by the rootid of the cache entry |
|
436 | - |
|
437 | - $mountRootIds = array_map(function ($mount) { |
|
438 | - return $mount->getRootId(); |
|
439 | - }, $mountsContainingFile); |
|
440 | - $mountRootPaths = array_map(function ($mount) { |
|
441 | - return $mount->getRootInternalPath(); |
|
442 | - }, $mountsContainingFile); |
|
443 | - $mountProviders = array_unique(array_map(function ($mount) { |
|
444 | - return $mount->getMountProvider(); |
|
445 | - }, $mountsContainingFile)); |
|
446 | - $mountRoots = array_combine($mountRootIds, $mountRootPaths); |
|
447 | - |
|
448 | - $mounts = $this->mountManager->getMountsByMountProvider($path, $mountProviders); |
|
449 | - |
|
450 | - $mountsContainingFile = array_filter($mounts, function ($mount) use ($mountRoots) { |
|
451 | - return isset($mountRoots[$mount->getStorageRootId()]); |
|
452 | - }); |
|
453 | - |
|
454 | - if (count($mountsContainingFile) === 0) { |
|
455 | - if ($user === $this->getAppDataDirectoryName()) { |
|
456 | - $folder = $this->get($path); |
|
457 | - if ($folder instanceof Folder) { |
|
458 | - return $folder->getByIdInRootMount($id); |
|
459 | - } else { |
|
460 | - throw new \Exception("getByIdInPath with non folder"); |
|
461 | - } |
|
462 | - } |
|
463 | - return []; |
|
464 | - } |
|
465 | - |
|
466 | - $nodes = array_map(function (IMountPoint $mount) use ($id, $mountRoots) { |
|
467 | - $rootInternalPath = $mountRoots[$mount->getStorageRootId()]; |
|
468 | - $cacheEntry = $mount->getStorage()->getCache()->get($id); |
|
469 | - if (!$cacheEntry) { |
|
470 | - return null; |
|
471 | - } |
|
472 | - |
|
473 | - // cache jails will hide the "true" internal path |
|
474 | - $internalPath = ltrim($rootInternalPath . '/' . $cacheEntry->getPath(), '/'); |
|
475 | - $pathRelativeToMount = substr($internalPath, strlen($rootInternalPath)); |
|
476 | - $pathRelativeToMount = ltrim($pathRelativeToMount, '/'); |
|
477 | - $absolutePath = rtrim($mount->getMountPoint() . $pathRelativeToMount, '/'); |
|
478 | - return $this->createNode($absolutePath, new FileInfo( |
|
479 | - $absolutePath, $mount->getStorage(), $cacheEntry->getPath(), $cacheEntry, $mount, |
|
480 | - \OC::$server->getUserManager()->get($mount->getStorage()->getOwner($pathRelativeToMount)) |
|
481 | - )); |
|
482 | - }, $mountsContainingFile); |
|
483 | - |
|
484 | - $nodes = array_filter($nodes); |
|
485 | - |
|
486 | - $folders = array_filter($nodes, function (Node $node) use ($path) { |
|
487 | - return PathHelper::getRelativePath($path, $node->getPath()) !== null; |
|
488 | - }); |
|
489 | - usort($folders, function ($a, $b) { |
|
490 | - return $b->getPath() <=> $a->getPath(); |
|
491 | - }); |
|
492 | - return $folders; |
|
493 | - } |
|
74 | + private Manager $mountManager; |
|
75 | + private PublicEmitter $emitter; |
|
76 | + private ?IUser $user; |
|
77 | + private CappedMemoryCache $userFolderCache; |
|
78 | + private IUserMountCache $userMountCache; |
|
79 | + private LoggerInterface $logger; |
|
80 | + private IUserManager $userManager; |
|
81 | + private IEventDispatcher $eventDispatcher; |
|
82 | + |
|
83 | + /** |
|
84 | + * @param Manager $manager |
|
85 | + * @param View $view |
|
86 | + * @param IUser|null $user |
|
87 | + */ |
|
88 | + public function __construct( |
|
89 | + $manager, |
|
90 | + $view, |
|
91 | + $user, |
|
92 | + IUserMountCache $userMountCache, |
|
93 | + LoggerInterface $logger, |
|
94 | + IUserManager $userManager, |
|
95 | + IEventDispatcher $eventDispatcher |
|
96 | + ) { |
|
97 | + parent::__construct($this, $view, ''); |
|
98 | + $this->mountManager = $manager; |
|
99 | + $this->user = $user; |
|
100 | + $this->emitter = new PublicEmitter(); |
|
101 | + $this->userFolderCache = new CappedMemoryCache(); |
|
102 | + $this->userMountCache = $userMountCache; |
|
103 | + $this->logger = $logger; |
|
104 | + $this->userManager = $userManager; |
|
105 | + $eventDispatcher->addListener(FilesystemTornDownEvent::class, function () { |
|
106 | + $this->userFolderCache = new CappedMemoryCache(); |
|
107 | + }); |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * Get the user for which the filesystem is setup |
|
112 | + * |
|
113 | + * @return \OC\User\User |
|
114 | + */ |
|
115 | + public function getUser() { |
|
116 | + return $this->user; |
|
117 | + } |
|
118 | + |
|
119 | + /** |
|
120 | + * @param string $scope |
|
121 | + * @param string $method |
|
122 | + * @param callable $callback |
|
123 | + */ |
|
124 | + public function listen($scope, $method, callable $callback) { |
|
125 | + $this->emitter->listen($scope, $method, $callback); |
|
126 | + } |
|
127 | + |
|
128 | + /** |
|
129 | + * @param string $scope optional |
|
130 | + * @param string $method optional |
|
131 | + * @param callable $callback optional |
|
132 | + */ |
|
133 | + public function removeListener($scope = null, $method = null, callable $callback = null) { |
|
134 | + $this->emitter->removeListener($scope, $method, $callback); |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * @param string $scope |
|
139 | + * @param string $method |
|
140 | + * @param Node[] $arguments |
|
141 | + */ |
|
142 | + public function emit($scope, $method, $arguments = []) { |
|
143 | + $this->emitter->emit($scope, $method, $arguments); |
|
144 | + } |
|
145 | + |
|
146 | + /** |
|
147 | + * @param \OC\Files\Storage\Storage $storage |
|
148 | + * @param string $mountPoint |
|
149 | + * @param array $arguments |
|
150 | + */ |
|
151 | + public function mount($storage, $mountPoint, $arguments = []) { |
|
152 | + $mount = new MountPoint($storage, $mountPoint, $arguments); |
|
153 | + $this->mountManager->addMount($mount); |
|
154 | + } |
|
155 | + |
|
156 | + /** |
|
157 | + * @param string $mountPoint |
|
158 | + * @return \OC\Files\Mount\MountPoint |
|
159 | + */ |
|
160 | + public function getMount($mountPoint) { |
|
161 | + return $this->mountManager->find($mountPoint); |
|
162 | + } |
|
163 | + |
|
164 | + /** |
|
165 | + * @param string $mountPoint |
|
166 | + * @return \OC\Files\Mount\MountPoint[] |
|
167 | + */ |
|
168 | + public function getMountsIn($mountPoint) { |
|
169 | + return $this->mountManager->findIn($mountPoint); |
|
170 | + } |
|
171 | + |
|
172 | + /** |
|
173 | + * @param string $storageId |
|
174 | + * @return \OC\Files\Mount\MountPoint[] |
|
175 | + */ |
|
176 | + public function getMountByStorageId($storageId) { |
|
177 | + return $this->mountManager->findByStorageId($storageId); |
|
178 | + } |
|
179 | + |
|
180 | + /** |
|
181 | + * @param int $numericId |
|
182 | + * @return MountPoint[] |
|
183 | + */ |
|
184 | + public function getMountByNumericStorageId($numericId) { |
|
185 | + return $this->mountManager->findByNumericId($numericId); |
|
186 | + } |
|
187 | + |
|
188 | + /** |
|
189 | + * @param \OC\Files\Mount\MountPoint $mount |
|
190 | + */ |
|
191 | + public function unMount($mount) { |
|
192 | + $this->mountManager->remove($mount); |
|
193 | + } |
|
194 | + |
|
195 | + /** |
|
196 | + * @param string $path |
|
197 | + * @return Node |
|
198 | + * @throws \OCP\Files\NotPermittedException |
|
199 | + * @throws \OCP\Files\NotFoundException |
|
200 | + */ |
|
201 | + public function get($path) { |
|
202 | + $path = $this->normalizePath($path); |
|
203 | + if ($this->isValidPath($path)) { |
|
204 | + $fullPath = $this->getFullPath($path); |
|
205 | + $fileInfo = $this->view->getFileInfo($fullPath, false); |
|
206 | + if ($fileInfo) { |
|
207 | + return $this->createNode($fullPath, $fileInfo, false); |
|
208 | + } else { |
|
209 | + throw new NotFoundException($path); |
|
210 | + } |
|
211 | + } else { |
|
212 | + throw new NotPermittedException(); |
|
213 | + } |
|
214 | + } |
|
215 | + |
|
216 | + //most operations can't be done on the root |
|
217 | + |
|
218 | + /** |
|
219 | + * @param string $targetPath |
|
220 | + * @return Node |
|
221 | + * @throws \OCP\Files\NotPermittedException |
|
222 | + */ |
|
223 | + public function rename($targetPath) { |
|
224 | + throw new NotPermittedException(); |
|
225 | + } |
|
226 | + |
|
227 | + public function delete() { |
|
228 | + throw new NotPermittedException(); |
|
229 | + } |
|
230 | + |
|
231 | + /** |
|
232 | + * @param string $targetPath |
|
233 | + * @return Node |
|
234 | + * @throws \OCP\Files\NotPermittedException |
|
235 | + */ |
|
236 | + public function copy($targetPath) { |
|
237 | + throw new NotPermittedException(); |
|
238 | + } |
|
239 | + |
|
240 | + /** |
|
241 | + * @param int $mtime |
|
242 | + * @throws \OCP\Files\NotPermittedException |
|
243 | + */ |
|
244 | + public function touch($mtime = null) { |
|
245 | + throw new NotPermittedException(); |
|
246 | + } |
|
247 | + |
|
248 | + /** |
|
249 | + * @return \OC\Files\Storage\Storage |
|
250 | + * @throws \OCP\Files\NotFoundException |
|
251 | + */ |
|
252 | + public function getStorage() { |
|
253 | + throw new NotFoundException(); |
|
254 | + } |
|
255 | + |
|
256 | + /** |
|
257 | + * @return string |
|
258 | + */ |
|
259 | + public function getPath() { |
|
260 | + return '/'; |
|
261 | + } |
|
262 | + |
|
263 | + /** |
|
264 | + * @return string |
|
265 | + */ |
|
266 | + public function getInternalPath() { |
|
267 | + return ''; |
|
268 | + } |
|
269 | + |
|
270 | + /** |
|
271 | + * @return int |
|
272 | + */ |
|
273 | + public function getId() { |
|
274 | + return 0; |
|
275 | + } |
|
276 | + |
|
277 | + /** |
|
278 | + * @return array |
|
279 | + */ |
|
280 | + public function stat() { |
|
281 | + return []; |
|
282 | + } |
|
283 | + |
|
284 | + /** |
|
285 | + * @return int |
|
286 | + */ |
|
287 | + public function getMTime() { |
|
288 | + return 0; |
|
289 | + } |
|
290 | + |
|
291 | + /** |
|
292 | + * @param bool $includeMounts |
|
293 | + * @return int|float |
|
294 | + */ |
|
295 | + public function getSize($includeMounts = true): int|float { |
|
296 | + return 0; |
|
297 | + } |
|
298 | + |
|
299 | + /** |
|
300 | + * @return string |
|
301 | + */ |
|
302 | + public function getEtag() { |
|
303 | + return ''; |
|
304 | + } |
|
305 | + |
|
306 | + /** |
|
307 | + * @return int |
|
308 | + */ |
|
309 | + public function getPermissions() { |
|
310 | + return \OCP\Constants::PERMISSION_CREATE; |
|
311 | + } |
|
312 | + |
|
313 | + /** |
|
314 | + * @return bool |
|
315 | + */ |
|
316 | + public function isReadable() { |
|
317 | + return false; |
|
318 | + } |
|
319 | + |
|
320 | + /** |
|
321 | + * @return bool |
|
322 | + */ |
|
323 | + public function isUpdateable() { |
|
324 | + return false; |
|
325 | + } |
|
326 | + |
|
327 | + /** |
|
328 | + * @return bool |
|
329 | + */ |
|
330 | + public function isDeletable() { |
|
331 | + return false; |
|
332 | + } |
|
333 | + |
|
334 | + /** |
|
335 | + * @return bool |
|
336 | + */ |
|
337 | + public function isShareable() { |
|
338 | + return false; |
|
339 | + } |
|
340 | + |
|
341 | + /** |
|
342 | + * @return Node |
|
343 | + * @throws \OCP\Files\NotFoundException |
|
344 | + */ |
|
345 | + public function getParent() { |
|
346 | + throw new NotFoundException(); |
|
347 | + } |
|
348 | + |
|
349 | + /** |
|
350 | + * @return string |
|
351 | + */ |
|
352 | + public function getName() { |
|
353 | + return ''; |
|
354 | + } |
|
355 | + |
|
356 | + /** |
|
357 | + * Returns a view to user's files folder |
|
358 | + * |
|
359 | + * @param string $userId user ID |
|
360 | + * @return \OCP\Files\Folder |
|
361 | + * @throws NoUserException |
|
362 | + * @throws NotPermittedException |
|
363 | + */ |
|
364 | + public function getUserFolder($userId) { |
|
365 | + $userObject = $this->userManager->get($userId); |
|
366 | + |
|
367 | + if (is_null($userObject)) { |
|
368 | + $e = new NoUserException('Backends provided no user object'); |
|
369 | + $this->logger->error( |
|
370 | + sprintf( |
|
371 | + 'Backends provided no user object for %s', |
|
372 | + $userId |
|
373 | + ), |
|
374 | + [ |
|
375 | + 'app' => 'files', |
|
376 | + 'exception' => $e, |
|
377 | + ] |
|
378 | + ); |
|
379 | + throw $e; |
|
380 | + } |
|
381 | + |
|
382 | + $userId = $userObject->getUID(); |
|
383 | + |
|
384 | + if (!$this->userFolderCache->hasKey($userId)) { |
|
385 | + if ($this->mountManager->getSetupManager()->isSetupComplete($userObject)) { |
|
386 | + try { |
|
387 | + $folder = $this->get('/' . $userId . '/files'); |
|
388 | + if (!$folder instanceof \OCP\Files\Folder) { |
|
389 | + throw new \Exception("User folder for $userId exists as a file"); |
|
390 | + } |
|
391 | + } catch (NotFoundException $e) { |
|
392 | + if (!$this->nodeExists('/' . $userId)) { |
|
393 | + $this->newFolder('/' . $userId); |
|
394 | + } |
|
395 | + $folder = $this->newFolder('/' . $userId . '/files'); |
|
396 | + } |
|
397 | + } else { |
|
398 | + $folder = new LazyUserFolder($this, $userObject, $this->mountManager); |
|
399 | + } |
|
400 | + |
|
401 | + $this->userFolderCache->set($userId, $folder); |
|
402 | + } |
|
403 | + |
|
404 | + return $this->userFolderCache->get($userId); |
|
405 | + } |
|
406 | + |
|
407 | + public function getUserMountCache() { |
|
408 | + return $this->userMountCache; |
|
409 | + } |
|
410 | + |
|
411 | + /** |
|
412 | + * @param int $id |
|
413 | + * @return Node[] |
|
414 | + */ |
|
415 | + public function getByIdInPath(int $id, string $path): array { |
|
416 | + $mountCache = $this->getUserMountCache(); |
|
417 | + if (strpos($path, '/', 1) > 0) { |
|
418 | + [, $user] = explode('/', $path); |
|
419 | + } else { |
|
420 | + $user = null; |
|
421 | + } |
|
422 | + $mountsContainingFile = $mountCache->getMountsForFileId($id, $user); |
|
423 | + |
|
424 | + // if the mount isn't in the cache yet, perform a setup first, then try again |
|
425 | + if (count($mountsContainingFile) === 0) { |
|
426 | + $this->mountManager->getSetupManager()->setupForPath($path, true); |
|
427 | + $mountsContainingFile = $mountCache->getMountsForFileId($id, $user); |
|
428 | + } |
|
429 | + |
|
430 | + // when a user has access through the same storage through multiple paths |
|
431 | + // (such as an external storage that is both mounted for a user and shared to the user) |
|
432 | + // the mount cache will only hold a single entry for the storage |
|
433 | + // this can lead to issues as the different ways the user has access to a storage can have different permissions |
|
434 | + // |
|
435 | + // so instead of using the cached entries directly, we instead filter the current mounts by the rootid of the cache entry |
|
436 | + |
|
437 | + $mountRootIds = array_map(function ($mount) { |
|
438 | + return $mount->getRootId(); |
|
439 | + }, $mountsContainingFile); |
|
440 | + $mountRootPaths = array_map(function ($mount) { |
|
441 | + return $mount->getRootInternalPath(); |
|
442 | + }, $mountsContainingFile); |
|
443 | + $mountProviders = array_unique(array_map(function ($mount) { |
|
444 | + return $mount->getMountProvider(); |
|
445 | + }, $mountsContainingFile)); |
|
446 | + $mountRoots = array_combine($mountRootIds, $mountRootPaths); |
|
447 | + |
|
448 | + $mounts = $this->mountManager->getMountsByMountProvider($path, $mountProviders); |
|
449 | + |
|
450 | + $mountsContainingFile = array_filter($mounts, function ($mount) use ($mountRoots) { |
|
451 | + return isset($mountRoots[$mount->getStorageRootId()]); |
|
452 | + }); |
|
453 | + |
|
454 | + if (count($mountsContainingFile) === 0) { |
|
455 | + if ($user === $this->getAppDataDirectoryName()) { |
|
456 | + $folder = $this->get($path); |
|
457 | + if ($folder instanceof Folder) { |
|
458 | + return $folder->getByIdInRootMount($id); |
|
459 | + } else { |
|
460 | + throw new \Exception("getByIdInPath with non folder"); |
|
461 | + } |
|
462 | + } |
|
463 | + return []; |
|
464 | + } |
|
465 | + |
|
466 | + $nodes = array_map(function (IMountPoint $mount) use ($id, $mountRoots) { |
|
467 | + $rootInternalPath = $mountRoots[$mount->getStorageRootId()]; |
|
468 | + $cacheEntry = $mount->getStorage()->getCache()->get($id); |
|
469 | + if (!$cacheEntry) { |
|
470 | + return null; |
|
471 | + } |
|
472 | + |
|
473 | + // cache jails will hide the "true" internal path |
|
474 | + $internalPath = ltrim($rootInternalPath . '/' . $cacheEntry->getPath(), '/'); |
|
475 | + $pathRelativeToMount = substr($internalPath, strlen($rootInternalPath)); |
|
476 | + $pathRelativeToMount = ltrim($pathRelativeToMount, '/'); |
|
477 | + $absolutePath = rtrim($mount->getMountPoint() . $pathRelativeToMount, '/'); |
|
478 | + return $this->createNode($absolutePath, new FileInfo( |
|
479 | + $absolutePath, $mount->getStorage(), $cacheEntry->getPath(), $cacheEntry, $mount, |
|
480 | + \OC::$server->getUserManager()->get($mount->getStorage()->getOwner($pathRelativeToMount)) |
|
481 | + )); |
|
482 | + }, $mountsContainingFile); |
|
483 | + |
|
484 | + $nodes = array_filter($nodes); |
|
485 | + |
|
486 | + $folders = array_filter($nodes, function (Node $node) use ($path) { |
|
487 | + return PathHelper::getRelativePath($path, $node->getPath()) !== null; |
|
488 | + }); |
|
489 | + usort($folders, function ($a, $b) { |
|
490 | + return $b->getPath() <=> $a->getPath(); |
|
491 | + }); |
|
492 | + return $folders; |
|
493 | + } |
|
494 | 494 | } |
@@ -102,7 +102,7 @@ discard block |
||
102 | 102 | $this->userMountCache = $userMountCache; |
103 | 103 | $this->logger = $logger; |
104 | 104 | $this->userManager = $userManager; |
105 | - $eventDispatcher->addListener(FilesystemTornDownEvent::class, function () { |
|
105 | + $eventDispatcher->addListener(FilesystemTornDownEvent::class, function() { |
|
106 | 106 | $this->userFolderCache = new CappedMemoryCache(); |
107 | 107 | }); |
108 | 108 | } |
@@ -292,7 +292,7 @@ discard block |
||
292 | 292 | * @param bool $includeMounts |
293 | 293 | * @return int|float |
294 | 294 | */ |
295 | - public function getSize($includeMounts = true): int|float { |
|
295 | + public function getSize($includeMounts = true): int | float { |
|
296 | 296 | return 0; |
297 | 297 | } |
298 | 298 | |
@@ -384,15 +384,15 @@ discard block |
||
384 | 384 | if (!$this->userFolderCache->hasKey($userId)) { |
385 | 385 | if ($this->mountManager->getSetupManager()->isSetupComplete($userObject)) { |
386 | 386 | try { |
387 | - $folder = $this->get('/' . $userId . '/files'); |
|
387 | + $folder = $this->get('/'.$userId.'/files'); |
|
388 | 388 | if (!$folder instanceof \OCP\Files\Folder) { |
389 | 389 | throw new \Exception("User folder for $userId exists as a file"); |
390 | 390 | } |
391 | 391 | } catch (NotFoundException $e) { |
392 | - if (!$this->nodeExists('/' . $userId)) { |
|
393 | - $this->newFolder('/' . $userId); |
|
392 | + if (!$this->nodeExists('/'.$userId)) { |
|
393 | + $this->newFolder('/'.$userId); |
|
394 | 394 | } |
395 | - $folder = $this->newFolder('/' . $userId . '/files'); |
|
395 | + $folder = $this->newFolder('/'.$userId.'/files'); |
|
396 | 396 | } |
397 | 397 | } else { |
398 | 398 | $folder = new LazyUserFolder($this, $userObject, $this->mountManager); |
@@ -434,20 +434,20 @@ discard block |
||
434 | 434 | // |
435 | 435 | // so instead of using the cached entries directly, we instead filter the current mounts by the rootid of the cache entry |
436 | 436 | |
437 | - $mountRootIds = array_map(function ($mount) { |
|
437 | + $mountRootIds = array_map(function($mount) { |
|
438 | 438 | return $mount->getRootId(); |
439 | 439 | }, $mountsContainingFile); |
440 | - $mountRootPaths = array_map(function ($mount) { |
|
440 | + $mountRootPaths = array_map(function($mount) { |
|
441 | 441 | return $mount->getRootInternalPath(); |
442 | 442 | }, $mountsContainingFile); |
443 | - $mountProviders = array_unique(array_map(function ($mount) { |
|
443 | + $mountProviders = array_unique(array_map(function($mount) { |
|
444 | 444 | return $mount->getMountProvider(); |
445 | 445 | }, $mountsContainingFile)); |
446 | 446 | $mountRoots = array_combine($mountRootIds, $mountRootPaths); |
447 | 447 | |
448 | 448 | $mounts = $this->mountManager->getMountsByMountProvider($path, $mountProviders); |
449 | 449 | |
450 | - $mountsContainingFile = array_filter($mounts, function ($mount) use ($mountRoots) { |
|
450 | + $mountsContainingFile = array_filter($mounts, function($mount) use ($mountRoots) { |
|
451 | 451 | return isset($mountRoots[$mount->getStorageRootId()]); |
452 | 452 | }); |
453 | 453 | |
@@ -463,7 +463,7 @@ discard block |
||
463 | 463 | return []; |
464 | 464 | } |
465 | 465 | |
466 | - $nodes = array_map(function (IMountPoint $mount) use ($id, $mountRoots) { |
|
466 | + $nodes = array_map(function(IMountPoint $mount) use ($id, $mountRoots) { |
|
467 | 467 | $rootInternalPath = $mountRoots[$mount->getStorageRootId()]; |
468 | 468 | $cacheEntry = $mount->getStorage()->getCache()->get($id); |
469 | 469 | if (!$cacheEntry) { |
@@ -471,10 +471,10 @@ discard block |
||
471 | 471 | } |
472 | 472 | |
473 | 473 | // cache jails will hide the "true" internal path |
474 | - $internalPath = ltrim($rootInternalPath . '/' . $cacheEntry->getPath(), '/'); |
|
474 | + $internalPath = ltrim($rootInternalPath.'/'.$cacheEntry->getPath(), '/'); |
|
475 | 475 | $pathRelativeToMount = substr($internalPath, strlen($rootInternalPath)); |
476 | 476 | $pathRelativeToMount = ltrim($pathRelativeToMount, '/'); |
477 | - $absolutePath = rtrim($mount->getMountPoint() . $pathRelativeToMount, '/'); |
|
477 | + $absolutePath = rtrim($mount->getMountPoint().$pathRelativeToMount, '/'); |
|
478 | 478 | return $this->createNode($absolutePath, new FileInfo( |
479 | 479 | $absolutePath, $mount->getStorage(), $cacheEntry->getPath(), $cacheEntry, $mount, |
480 | 480 | \OC::$server->getUserManager()->get($mount->getStorage()->getOwner($pathRelativeToMount)) |
@@ -483,10 +483,10 @@ discard block |
||
483 | 483 | |
484 | 484 | $nodes = array_filter($nodes); |
485 | 485 | |
486 | - $folders = array_filter($nodes, function (Node $node) use ($path) { |
|
486 | + $folders = array_filter($nodes, function(Node $node) use ($path) { |
|
487 | 487 | return PathHelper::getRelativePath($path, $node->getPath()) !== null; |
488 | 488 | }); |
489 | - usort($folders, function ($a, $b) { |
|
489 | + usort($folders, function($a, $b) { |
|
490 | 490 | return $b->getPath() <=> $a->getPath(); |
491 | 491 | }); |
492 | 492 | return $folders; |
@@ -31,53 +31,53 @@ |
||
31 | 31 | use OCP\IUser; |
32 | 32 | |
33 | 33 | class LazyUserFolder extends LazyFolder { |
34 | - private IRootFolder $root; |
|
35 | - private IUser $user; |
|
36 | - private string $path; |
|
37 | - private IMountManager $mountManager; |
|
34 | + private IRootFolder $root; |
|
35 | + private IUser $user; |
|
36 | + private string $path; |
|
37 | + private IMountManager $mountManager; |
|
38 | 38 | |
39 | - public function __construct(IRootFolder $rootFolder, IUser $user, IMountManager $mountManager) { |
|
40 | - $this->root = $rootFolder; |
|
41 | - $this->user = $user; |
|
42 | - $this->mountManager = $mountManager; |
|
43 | - $this->path = '/' . $user->getUID() . '/files'; |
|
44 | - parent::__construct(function () use ($user) { |
|
45 | - try { |
|
46 | - return $this->root->get('/' . $user->getUID() . '/files'); |
|
47 | - } catch (NotFoundException $e) { |
|
48 | - if (!$this->root->nodeExists('/' . $user->getUID())) { |
|
49 | - $this->root->newFolder('/' . $user->getUID()); |
|
50 | - } |
|
51 | - return $this->root->newFolder('/' . $user->getUID() . '/files'); |
|
52 | - } |
|
53 | - }, [ |
|
54 | - 'path' => $this->path, |
|
55 | - 'permissions' => Constants::PERMISSION_ALL, |
|
56 | - 'type' => FileInfo::TYPE_FOLDER, |
|
57 | - 'mimetype' => FileInfo::MIMETYPE_FOLDER, |
|
58 | - ]); |
|
59 | - } |
|
39 | + public function __construct(IRootFolder $rootFolder, IUser $user, IMountManager $mountManager) { |
|
40 | + $this->root = $rootFolder; |
|
41 | + $this->user = $user; |
|
42 | + $this->mountManager = $mountManager; |
|
43 | + $this->path = '/' . $user->getUID() . '/files'; |
|
44 | + parent::__construct(function () use ($user) { |
|
45 | + try { |
|
46 | + return $this->root->get('/' . $user->getUID() . '/files'); |
|
47 | + } catch (NotFoundException $e) { |
|
48 | + if (!$this->root->nodeExists('/' . $user->getUID())) { |
|
49 | + $this->root->newFolder('/' . $user->getUID()); |
|
50 | + } |
|
51 | + return $this->root->newFolder('/' . $user->getUID() . '/files'); |
|
52 | + } |
|
53 | + }, [ |
|
54 | + 'path' => $this->path, |
|
55 | + 'permissions' => Constants::PERMISSION_ALL, |
|
56 | + 'type' => FileInfo::TYPE_FOLDER, |
|
57 | + 'mimetype' => FileInfo::MIMETYPE_FOLDER, |
|
58 | + ]); |
|
59 | + } |
|
60 | 60 | |
61 | - public function get($path) { |
|
62 | - return $this->root->get('/' . $this->user->getUID() . '/files/' . ltrim($path, '/')); |
|
63 | - } |
|
61 | + public function get($path) { |
|
62 | + return $this->root->get('/' . $this->user->getUID() . '/files/' . ltrim($path, '/')); |
|
63 | + } |
|
64 | 64 | |
65 | - /** |
|
66 | - * @param int $id |
|
67 | - * @return \OCP\Files\Node[] |
|
68 | - */ |
|
69 | - public function getById($id) { |
|
70 | - return $this->root->getByIdInPath((int)$id, $this->getPath()); |
|
71 | - } |
|
65 | + /** |
|
66 | + * @param int $id |
|
67 | + * @return \OCP\Files\Node[] |
|
68 | + */ |
|
69 | + public function getById($id) { |
|
70 | + return $this->root->getByIdInPath((int)$id, $this->getPath()); |
|
71 | + } |
|
72 | 72 | |
73 | - public function getMountPoint() { |
|
74 | - if ($this->folder !== null) { |
|
75 | - return $this->folder->getMountPoint(); |
|
76 | - } |
|
77 | - $mountPoint = $this->mountManager->find('/' . $this->user->getUID()); |
|
78 | - if (is_null($mountPoint)) { |
|
79 | - throw new \Exception("No mountpoint for user folder"); |
|
80 | - } |
|
81 | - return $mountPoint; |
|
82 | - } |
|
73 | + public function getMountPoint() { |
|
74 | + if ($this->folder !== null) { |
|
75 | + return $this->folder->getMountPoint(); |
|
76 | + } |
|
77 | + $mountPoint = $this->mountManager->find('/' . $this->user->getUID()); |
|
78 | + if (is_null($mountPoint)) { |
|
79 | + throw new \Exception("No mountpoint for user folder"); |
|
80 | + } |
|
81 | + return $mountPoint; |
|
82 | + } |
|
83 | 83 | } |
@@ -40,15 +40,15 @@ discard block |
||
40 | 40 | $this->root = $rootFolder; |
41 | 41 | $this->user = $user; |
42 | 42 | $this->mountManager = $mountManager; |
43 | - $this->path = '/' . $user->getUID() . '/files'; |
|
44 | - parent::__construct(function () use ($user) { |
|
43 | + $this->path = '/'.$user->getUID().'/files'; |
|
44 | + parent::__construct(function() use ($user) { |
|
45 | 45 | try { |
46 | - return $this->root->get('/' . $user->getUID() . '/files'); |
|
46 | + return $this->root->get('/'.$user->getUID().'/files'); |
|
47 | 47 | } catch (NotFoundException $e) { |
48 | - if (!$this->root->nodeExists('/' . $user->getUID())) { |
|
49 | - $this->root->newFolder('/' . $user->getUID()); |
|
48 | + if (!$this->root->nodeExists('/'.$user->getUID())) { |
|
49 | + $this->root->newFolder('/'.$user->getUID()); |
|
50 | 50 | } |
51 | - return $this->root->newFolder('/' . $user->getUID() . '/files'); |
|
51 | + return $this->root->newFolder('/'.$user->getUID().'/files'); |
|
52 | 52 | } |
53 | 53 | }, [ |
54 | 54 | 'path' => $this->path, |
@@ -59,7 +59,7 @@ discard block |
||
59 | 59 | } |
60 | 60 | |
61 | 61 | public function get($path) { |
62 | - return $this->root->get('/' . $this->user->getUID() . '/files/' . ltrim($path, '/')); |
|
62 | + return $this->root->get('/'.$this->user->getUID().'/files/'.ltrim($path, '/')); |
|
63 | 63 | } |
64 | 64 | |
65 | 65 | /** |
@@ -67,14 +67,14 @@ discard block |
||
67 | 67 | * @return \OCP\Files\Node[] |
68 | 68 | */ |
69 | 69 | public function getById($id) { |
70 | - return $this->root->getByIdInPath((int)$id, $this->getPath()); |
|
70 | + return $this->root->getByIdInPath((int) $id, $this->getPath()); |
|
71 | 71 | } |
72 | 72 | |
73 | 73 | public function getMountPoint() { |
74 | 74 | if ($this->folder !== null) { |
75 | 75 | return $this->folder->getMountPoint(); |
76 | 76 | } |
77 | - $mountPoint = $this->mountManager->find('/' . $this->user->getUID()); |
|
77 | + $mountPoint = $this->mountManager->find('/'.$this->user->getUID()); |
|
78 | 78 | if (is_null($mountPoint)) { |
79 | 79 | throw new \Exception("No mountpoint for user folder"); |
80 | 80 | } |