@@ -71,422 +71,422 @@ |
||
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); |
|
206 | - if ($fileInfo) { |
|
207 | - return $this->createNode($fullPath, $fileInfo); |
|
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 |
|
294 | - */ |
|
295 | - public function getSize($includeMounts = true) { |
|
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 | - $this->logger->error( |
|
369 | - sprintf( |
|
370 | - 'Backends provided no user object for %s', |
|
371 | - $userId |
|
372 | - ), |
|
373 | - [ |
|
374 | - 'app' => 'files', |
|
375 | - ] |
|
376 | - ); |
|
377 | - throw new NoUserException('Backends provided no user object'); |
|
378 | - } |
|
379 | - |
|
380 | - $userId = $userObject->getUID(); |
|
381 | - |
|
382 | - if (!$this->userFolderCache->hasKey($userId)) { |
|
383 | - if ($this->mountManager->getSetupManager()->isSetupComplete($userObject)) { |
|
384 | - try { |
|
385 | - $folder = $this->get('/' . $userId . '/files'); |
|
386 | - if (!$folder instanceof \OCP\Files\Folder) { |
|
387 | - throw new \Exception("User folder for $userId exists as a file"); |
|
388 | - } |
|
389 | - } catch (NotFoundException $e) { |
|
390 | - if (!$this->nodeExists('/' . $userId)) { |
|
391 | - $this->newFolder('/' . $userId); |
|
392 | - } |
|
393 | - $folder = $this->newFolder('/' . $userId . '/files'); |
|
394 | - } |
|
395 | - } else { |
|
396 | - $folder = new LazyUserFolder($this, $userObject); |
|
397 | - } |
|
398 | - |
|
399 | - $this->userFolderCache->set($userId, $folder); |
|
400 | - } |
|
401 | - |
|
402 | - return $this->userFolderCache->get($userId); |
|
403 | - } |
|
404 | - |
|
405 | - public function getUserMountCache() { |
|
406 | - return $this->userMountCache; |
|
407 | - } |
|
408 | - |
|
409 | - /** |
|
410 | - * @param int $id |
|
411 | - * @return Node[] |
|
412 | - */ |
|
413 | - public function getByIdInPath(int $id, string $path): array { |
|
414 | - $mountCache = $this->getUserMountCache(); |
|
415 | - if (strpos($path, '/', 1) > 0) { |
|
416 | - [, $user] = explode('/', $path); |
|
417 | - } else { |
|
418 | - $user = null; |
|
419 | - } |
|
420 | - $mountsContainingFile = $mountCache->getMountsForFileId($id, $user); |
|
421 | - |
|
422 | - // if the mount isn't in the cache yet, perform a setup first, then try again |
|
423 | - if (count($mountsContainingFile) === 0) { |
|
424 | - $this->mountManager->getSetupManager()->setupForPath($path, true); |
|
425 | - $mountsContainingFile = $mountCache->getMountsForFileId($id, $user); |
|
426 | - } |
|
427 | - |
|
428 | - // when a user has access trough the same storage trough multiple paths |
|
429 | - // (such as an external storage that is both mounted for a user and shared to the user) |
|
430 | - // the mount cache will only hold a single entry for the storage |
|
431 | - // this can lead to issues as the different ways the user has access to a storage can have different permissions |
|
432 | - // |
|
433 | - // so instead of using the cached entries directly, we instead filter the current mounts by the rootid of the cache entry |
|
434 | - |
|
435 | - $mountRootIds = array_map(function ($mount) { |
|
436 | - return $mount->getRootId(); |
|
437 | - }, $mountsContainingFile); |
|
438 | - $mountRootPaths = array_map(function ($mount) { |
|
439 | - return $mount->getRootInternalPath(); |
|
440 | - }, $mountsContainingFile); |
|
441 | - $mountProviders = array_unique(array_map(function ($mount) { |
|
442 | - return $mount->getMountProvider(); |
|
443 | - }, $mountsContainingFile)); |
|
444 | - $mountRoots = array_combine($mountRootIds, $mountRootPaths); |
|
445 | - |
|
446 | - $mounts = $this->mountManager->getMountsByMountProvider($path, $mountProviders); |
|
447 | - |
|
448 | - $mountsContainingFile = array_filter($mounts, function ($mount) use ($mountRoots) { |
|
449 | - return isset($mountRoots[$mount->getStorageRootId()]); |
|
450 | - }); |
|
451 | - |
|
452 | - if (count($mountsContainingFile) === 0) { |
|
453 | - if ($user === $this->getAppDataDirectoryName()) { |
|
454 | - $folder = $this->get($path); |
|
455 | - if ($folder instanceof Folder) { |
|
456 | - return $folder->getByIdInRootMount($id); |
|
457 | - } else { |
|
458 | - throw new \Exception("getByIdInPath with non folder"); |
|
459 | - } |
|
460 | - } |
|
461 | - return []; |
|
462 | - } |
|
463 | - |
|
464 | - $nodes = array_map(function (IMountPoint $mount) use ($id, $mountRoots) { |
|
465 | - $rootInternalPath = $mountRoots[$mount->getStorageRootId()]; |
|
466 | - $cacheEntry = $mount->getStorage()->getCache()->get($id); |
|
467 | - if (!$cacheEntry) { |
|
468 | - return null; |
|
469 | - } |
|
470 | - |
|
471 | - // cache jails will hide the "true" internal path |
|
472 | - $internalPath = ltrim($rootInternalPath . '/' . $cacheEntry->getPath(), '/'); |
|
473 | - $pathRelativeToMount = substr($internalPath, strlen($rootInternalPath)); |
|
474 | - $pathRelativeToMount = ltrim($pathRelativeToMount, '/'); |
|
475 | - $absolutePath = rtrim($mount->getMountPoint() . $pathRelativeToMount, '/'); |
|
476 | - return $this->createNode($absolutePath, new FileInfo( |
|
477 | - $absolutePath, $mount->getStorage(), $cacheEntry->getPath(), $cacheEntry, $mount, |
|
478 | - \OC::$server->getUserManager()->get($mount->getStorage()->getOwner($pathRelativeToMount)) |
|
479 | - )); |
|
480 | - }, $mountsContainingFile); |
|
481 | - |
|
482 | - $nodes = array_filter($nodes); |
|
483 | - |
|
484 | - $folders = array_filter($nodes, function (Node $node) use ($path) { |
|
485 | - return PathHelper::getRelativePath($path, $node->getPath()) !== null; |
|
486 | - }); |
|
487 | - usort($folders, function ($a, $b) { |
|
488 | - return $b->getPath() <=> $a->getPath(); |
|
489 | - }); |
|
490 | - return $folders; |
|
491 | - } |
|
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); |
|
206 | + if ($fileInfo) { |
|
207 | + return $this->createNode($fullPath, $fileInfo); |
|
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 |
|
294 | + */ |
|
295 | + public function getSize($includeMounts = true) { |
|
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 | + $this->logger->error( |
|
369 | + sprintf( |
|
370 | + 'Backends provided no user object for %s', |
|
371 | + $userId |
|
372 | + ), |
|
373 | + [ |
|
374 | + 'app' => 'files', |
|
375 | + ] |
|
376 | + ); |
|
377 | + throw new NoUserException('Backends provided no user object'); |
|
378 | + } |
|
379 | + |
|
380 | + $userId = $userObject->getUID(); |
|
381 | + |
|
382 | + if (!$this->userFolderCache->hasKey($userId)) { |
|
383 | + if ($this->mountManager->getSetupManager()->isSetupComplete($userObject)) { |
|
384 | + try { |
|
385 | + $folder = $this->get('/' . $userId . '/files'); |
|
386 | + if (!$folder instanceof \OCP\Files\Folder) { |
|
387 | + throw new \Exception("User folder for $userId exists as a file"); |
|
388 | + } |
|
389 | + } catch (NotFoundException $e) { |
|
390 | + if (!$this->nodeExists('/' . $userId)) { |
|
391 | + $this->newFolder('/' . $userId); |
|
392 | + } |
|
393 | + $folder = $this->newFolder('/' . $userId . '/files'); |
|
394 | + } |
|
395 | + } else { |
|
396 | + $folder = new LazyUserFolder($this, $userObject); |
|
397 | + } |
|
398 | + |
|
399 | + $this->userFolderCache->set($userId, $folder); |
|
400 | + } |
|
401 | + |
|
402 | + return $this->userFolderCache->get($userId); |
|
403 | + } |
|
404 | + |
|
405 | + public function getUserMountCache() { |
|
406 | + return $this->userMountCache; |
|
407 | + } |
|
408 | + |
|
409 | + /** |
|
410 | + * @param int $id |
|
411 | + * @return Node[] |
|
412 | + */ |
|
413 | + public function getByIdInPath(int $id, string $path): array { |
|
414 | + $mountCache = $this->getUserMountCache(); |
|
415 | + if (strpos($path, '/', 1) > 0) { |
|
416 | + [, $user] = explode('/', $path); |
|
417 | + } else { |
|
418 | + $user = null; |
|
419 | + } |
|
420 | + $mountsContainingFile = $mountCache->getMountsForFileId($id, $user); |
|
421 | + |
|
422 | + // if the mount isn't in the cache yet, perform a setup first, then try again |
|
423 | + if (count($mountsContainingFile) === 0) { |
|
424 | + $this->mountManager->getSetupManager()->setupForPath($path, true); |
|
425 | + $mountsContainingFile = $mountCache->getMountsForFileId($id, $user); |
|
426 | + } |
|
427 | + |
|
428 | + // when a user has access trough the same storage trough multiple paths |
|
429 | + // (such as an external storage that is both mounted for a user and shared to the user) |
|
430 | + // the mount cache will only hold a single entry for the storage |
|
431 | + // this can lead to issues as the different ways the user has access to a storage can have different permissions |
|
432 | + // |
|
433 | + // so instead of using the cached entries directly, we instead filter the current mounts by the rootid of the cache entry |
|
434 | + |
|
435 | + $mountRootIds = array_map(function ($mount) { |
|
436 | + return $mount->getRootId(); |
|
437 | + }, $mountsContainingFile); |
|
438 | + $mountRootPaths = array_map(function ($mount) { |
|
439 | + return $mount->getRootInternalPath(); |
|
440 | + }, $mountsContainingFile); |
|
441 | + $mountProviders = array_unique(array_map(function ($mount) { |
|
442 | + return $mount->getMountProvider(); |
|
443 | + }, $mountsContainingFile)); |
|
444 | + $mountRoots = array_combine($mountRootIds, $mountRootPaths); |
|
445 | + |
|
446 | + $mounts = $this->mountManager->getMountsByMountProvider($path, $mountProviders); |
|
447 | + |
|
448 | + $mountsContainingFile = array_filter($mounts, function ($mount) use ($mountRoots) { |
|
449 | + return isset($mountRoots[$mount->getStorageRootId()]); |
|
450 | + }); |
|
451 | + |
|
452 | + if (count($mountsContainingFile) === 0) { |
|
453 | + if ($user === $this->getAppDataDirectoryName()) { |
|
454 | + $folder = $this->get($path); |
|
455 | + if ($folder instanceof Folder) { |
|
456 | + return $folder->getByIdInRootMount($id); |
|
457 | + } else { |
|
458 | + throw new \Exception("getByIdInPath with non folder"); |
|
459 | + } |
|
460 | + } |
|
461 | + return []; |
|
462 | + } |
|
463 | + |
|
464 | + $nodes = array_map(function (IMountPoint $mount) use ($id, $mountRoots) { |
|
465 | + $rootInternalPath = $mountRoots[$mount->getStorageRootId()]; |
|
466 | + $cacheEntry = $mount->getStorage()->getCache()->get($id); |
|
467 | + if (!$cacheEntry) { |
|
468 | + return null; |
|
469 | + } |
|
470 | + |
|
471 | + // cache jails will hide the "true" internal path |
|
472 | + $internalPath = ltrim($rootInternalPath . '/' . $cacheEntry->getPath(), '/'); |
|
473 | + $pathRelativeToMount = substr($internalPath, strlen($rootInternalPath)); |
|
474 | + $pathRelativeToMount = ltrim($pathRelativeToMount, '/'); |
|
475 | + $absolutePath = rtrim($mount->getMountPoint() . $pathRelativeToMount, '/'); |
|
476 | + return $this->createNode($absolutePath, new FileInfo( |
|
477 | + $absolutePath, $mount->getStorage(), $cacheEntry->getPath(), $cacheEntry, $mount, |
|
478 | + \OC::$server->getUserManager()->get($mount->getStorage()->getOwner($pathRelativeToMount)) |
|
479 | + )); |
|
480 | + }, $mountsContainingFile); |
|
481 | + |
|
482 | + $nodes = array_filter($nodes); |
|
483 | + |
|
484 | + $folders = array_filter($nodes, function (Node $node) use ($path) { |
|
485 | + return PathHelper::getRelativePath($path, $node->getPath()) !== null; |
|
486 | + }); |
|
487 | + usort($folders, function ($a, $b) { |
|
488 | + return $b->getPath() <=> $a->getPath(); |
|
489 | + }); |
|
490 | + return $folders; |
|
491 | + } |
|
492 | 492 | } |