@@ -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); |
|
| 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 | - $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); |
|
| 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 trough the same storage trough 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); |
|
| 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 | + $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); |
|
| 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 trough the same storage trough 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 | } |