aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private/Files/Node/Root.php
diff options
context:
space:
mode:
Diffstat (limited to 'lib/private/Files/Node/Root.php')
-rw-r--r--lib/private/Files/Node/Root.php323
1 files changed, 227 insertions, 96 deletions
diff --git a/lib/private/Files/Node/Root.php b/lib/private/Files/Node/Root.php
index fba8a24b40f..76afca9dee8 100644
--- a/lib/private/Files/Node/Root.php
+++ b/lib/private/Files/Node/Root.php
@@ -1,48 +1,35 @@
<?php
+
/**
- * @copyright Copyright (c) 2016, ownCloud, Inc.
- *
- * @author Bernhard Posselt <dev@bernhard-posselt.com>
- * @author Christoph Wurst <christoph@winzerhof-wurst.at>
- * @author Joas Schilling <coding@schilljs.com>
- * @author Jörn Friedrich Dreyer <jfd@butonic.de>
- * @author Julius Härtl <jus@bitgrid.net>
- * @author Lukas Reschke <lukas@statuscode.ch>
- * @author Morris Jobke <hey@morrisjobke.de>
- * @author Robin Appelman <robin@icewind.nl>
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- * @author Stefan Weil <sw@weilnetz.de>
- * @author Vincent Petry <vincent@nextcloud.com>
- *
- * @license AGPL-3.0
- *
- * This code is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License, version 3,
- * as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License, version 3,
- * along with this program. If not, see <http://www.gnu.org/licenses/>
- *
+ * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
+ * SPDX-License-Identifier: AGPL-3.0-only
*/
-
namespace OC\Files\Node;
-use OC\Cache\CappedMemoryCache;
+use OC\Files\FileInfo;
use OC\Files\Mount\Manager;
use OC\Files\Mount\MountPoint;
+use OC\Files\Utils\PathHelper;
+use OC\Files\View;
use OC\Hooks\PublicEmitter;
use OC\User\NoUserException;
+use OCP\Cache\CappedMemoryCache;
+use OCP\EventDispatcher\IEventDispatcher;
+use OCP\Files\Cache\ICacheEntry;
use OCP\Files\Config\IUserMountCache;
+use OCP\Files\Events\Node\FilesystemTornDownEvent;
use OCP\Files\IRootFolder;
+use OCP\Files\Mount\IMountPoint;
+use OCP\Files\Node as INode;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
-use OCP\ILogger;
+use OCP\ICache;
+use OCP\ICacheFactory;
+use OCP\IUser;
use OCP\IUserManager;
+use OCP\Server;
+use Psr\Log\LoggerInterface;
/**
* Class Root
@@ -64,35 +51,31 @@ use OCP\IUserManager;
* @package OC\Files\Node
*/
class Root extends Folder implements IRootFolder {
- /** @var Manager */
- private $mountManager;
- /** @var PublicEmitter */
- private $emitter;
- /** @var null|\OC\User\User */
- private $user;
- /** @var CappedMemoryCache */
- private $userFolderCache;
- /** @var IUserMountCache */
- private $userMountCache;
- /** @var ILogger */
- private $logger;
- /** @var IUserManager */
- private $userManager;
+ private Manager $mountManager;
+ private PublicEmitter $emitter;
+ private ?IUser $user;
+ private CappedMemoryCache $userFolderCache;
+ private IUserMountCache $userMountCache;
+ private LoggerInterface $logger;
+ private IUserManager $userManager;
+ private IEventDispatcher $eventDispatcher;
+ private ICache $pathByIdCache;
/**
- * @param \OC\Files\Mount\Manager $manager
- * @param \OC\Files\View $view
- * @param \OC\User\User|null $user
- * @param IUserMountCache $userMountCache
- * @param ILogger $logger
- * @param IUserManager $userManager
+ * @param Manager $manager
+ * @param View $view
+ * @param IUser|null $user
*/
- public function __construct($manager,
- $view,
- $user,
- IUserMountCache $userMountCache,
- ILogger $logger,
- IUserManager $userManager) {
+ public function __construct(
+ $manager,
+ $view,
+ $user,
+ IUserMountCache $userMountCache,
+ LoggerInterface $logger,
+ IUserManager $userManager,
+ IEventDispatcher $eventDispatcher,
+ ICacheFactory $cacheFactory,
+ ) {
parent::__construct($this, $view, '');
$this->mountManager = $manager;
$this->user = $user;
@@ -101,6 +84,10 @@ class Root extends Folder implements IRootFolder {
$this->userMountCache = $userMountCache;
$this->logger = $logger;
$this->userManager = $userManager;
+ $eventDispatcher->addListener(FilesystemTornDownEvent::class, function () {
+ $this->userFolderCache = new CappedMemoryCache();
+ });
+ $this->pathByIdCache = $cacheFactory->createLocal('path-by-id');
}
/**
@@ -126,7 +113,7 @@ class Root extends Folder implements IRootFolder {
* @param string $method optional
* @param callable $callback optional
*/
- public function removeListener($scope = null, $method = null, callable $callback = null) {
+ public function removeListener($scope = null, $method = null, ?callable $callback = null) {
$this->emitter->removeListener($scope, $method, $callback);
}
@@ -149,11 +136,7 @@ class Root extends Folder implements IRootFolder {
$this->mountManager->addMount($mount);
}
- /**
- * @param string $mountPoint
- * @return \OC\Files\Mount\MountPoint
- */
- public function getMount($mountPoint) {
+ public function getMount(string $mountPoint): IMountPoint {
return $this->mountManager->find($mountPoint);
}
@@ -161,7 +144,7 @@ class Root extends Folder implements IRootFolder {
* @param string $mountPoint
* @return \OC\Files\Mount\MountPoint[]
*/
- public function getMountsIn($mountPoint) {
+ public function getMountsIn(string $mountPoint): array {
return $this->mountManager->findIn($mountPoint);
}
@@ -188,19 +171,13 @@ class Root extends Folder implements IRootFolder {
$this->mountManager->remove($mount);
}
- /**
- * @param string $path
- * @throws \OCP\Files\NotFoundException
- * @throws \OCP\Files\NotPermittedException
- * @return string
- */
public function get($path) {
$path = $this->normalizePath($path);
if ($this->isValidPath($path)) {
$fullPath = $this->getFullPath($path);
- $fileInfo = $this->view->getFileInfo($fullPath);
+ $fileInfo = $this->view->getFileInfo($fullPath, false);
if ($fileInfo) {
- return $this->createNode($fullPath, $fileInfo);
+ return $this->createNode($fullPath, $fileInfo, false);
} else {
throw new NotFoundException($path);
}
@@ -213,8 +190,8 @@ class Root extends Folder implements IRootFolder {
/**
* @param string $targetPath
+ * @return Node
* @throws \OCP\Files\NotPermittedException
- * @return \OC\Files\Node\Node
*/
public function rename($targetPath) {
throw new NotPermittedException();
@@ -226,8 +203,8 @@ class Root extends Folder implements IRootFolder {
/**
* @param string $targetPath
+ * @return Node
* @throws \OCP\Files\NotPermittedException
- * @return \OC\Files\Node\Node
*/
public function copy($targetPath) {
throw new NotPermittedException();
@@ -267,36 +244,36 @@ class Root extends Folder implements IRootFolder {
* @return int
*/
public function getId() {
- return null;
+ return 0;
}
/**
* @return array
*/
public function stat() {
- return null;
+ return [];
}
/**
* @return int
*/
public function getMTime() {
- return null;
+ return 0;
}
/**
* @param bool $includeMounts
- * @return int
+ * @return int|float
*/
- public function getSize($includeMounts = true) {
- return null;
+ public function getSize($includeMounts = true): int|float {
+ return 0;
}
/**
* @return string
*/
public function getEtag() {
- return null;
+ return '';
}
/**
@@ -335,10 +312,9 @@ class Root extends Folder implements IRootFolder {
}
/**
- * @return Node
* @throws \OCP\Files\NotFoundException
*/
- public function getParent() {
+ public function getParent(): INode|IRootFolder {
throw new NotFoundException();
}
@@ -361,6 +337,7 @@ class Root extends Folder implements IRootFolder {
$userObject = $this->userManager->get($userId);
if (is_null($userObject)) {
+ $e = new NoUserException('Backends provided no user object');
$this->logger->error(
sprintf(
'Backends provided no user object for %s',
@@ -368,23 +345,29 @@ class Root extends Folder implements IRootFolder {
),
[
'app' => 'files',
+ 'exception' => $e,
]
);
- throw new NoUserException('Backends provided no user object');
+ throw $e;
}
$userId = $userObject->getUID();
if (!$this->userFolderCache->hasKey($userId)) {
- \OC\Files\Filesystem::initMountPoints($userId);
-
- try {
- $folder = $this->get('/' . $userId . '/files');
- } catch (NotFoundException $e) {
- if (!$this->nodeExists('/' . $userId)) {
- $this->newFolder('/' . $userId);
+ if ($this->mountManager->getSetupManager()->isSetupComplete($userObject)) {
+ try {
+ $folder = $this->get('/' . $userId . '/files');
+ if (!$folder instanceof \OCP\Files\Folder) {
+ throw new \Exception("Account folder for \"$userId\" exists as a file");
+ }
+ } catch (NotFoundException $e) {
+ if (!$this->nodeExists('/' . $userId)) {
+ $this->newFolder('/' . $userId);
+ }
+ $folder = $this->newFolder('/' . $userId . '/files');
}
- $folder = $this->newFolder('/' . $userId . '/files');
+ } else {
+ $folder = new LazyUserFolder($this, $userObject, $this->mountManager);
}
$this->userFolderCache->set($userId, $folder);
@@ -393,11 +376,159 @@ class Root extends Folder implements IRootFolder {
return $this->userFolderCache->get($userId);
}
- public function clearCache() {
- $this->userFolderCache = new CappedMemoryCache();
- }
-
public function getUserMountCache() {
return $this->userMountCache;
}
+
+ public function getFirstNodeByIdInPath(int $id, string $path): ?INode {
+ // scope the cache by user, so we don't return nodes for different users
+ if ($this->user) {
+ $cachedPath = $this->pathByIdCache->get($this->user->getUID() . '::' . $id);
+ if ($cachedPath && str_starts_with($cachedPath, $path)) {
+ // getting the node by path is significantly cheaper than finding it by id
+ try {
+ $node = $this->get($cachedPath);
+ // by validating that the cached path still has the requested fileid we can work around the need to invalidate the cached path
+ // if the cached path is invalid or a different file now we fall back to the uncached logic
+ if ($node && $node->getId() === $id) {
+ return $node;
+ }
+ } catch (NotFoundException|NotPermittedException) {
+ // The file may be moved but the old path still in cache
+ }
+ }
+ }
+ $node = current($this->getByIdInPath($id, $path));
+ if (!$node) {
+ return null;
+ }
+
+ if ($this->user) {
+ $this->pathByIdCache->set($this->user->getUID() . '::' . $id, $node->getPath());
+ }
+ return $node;
+ }
+
+ /**
+ * @param int $id
+ * @return Node[]
+ */
+ public function getByIdInPath(int $id, string $path): array {
+ $mountCache = $this->getUserMountCache();
+ if ($path !== '' && strpos($path, '/', 1) > 0) {
+ [, $user] = explode('/', $path);
+ } else {
+ $user = null;
+ }
+ $mountsContainingFile = $mountCache->getMountsForFileId($id, $user);
+
+ // if the mount isn't in the cache yet, perform a setup first, then try again
+ if (count($mountsContainingFile) === 0) {
+ $this->mountManager->getSetupManager()->setupForPath($path, true);
+ $mountsContainingFile = $mountCache->getMountsForFileId($id, $user);
+ }
+
+ // when a user has access through the same storage through multiple paths
+ // (such as an external storage that is both mounted for a user and shared to the user)
+ // the mount cache will only hold a single entry for the storage
+ // this can lead to issues as the different ways the user has access to a storage can have different permissions
+ //
+ // so instead of using the cached entries directly, we instead filter the current mounts by the rootid of the cache entry
+
+ $mountRootIds = array_map(function ($mount) {
+ return $mount->getRootId();
+ }, $mountsContainingFile);
+ $mountRootPaths = array_map(function ($mount) {
+ return $mount->getRootInternalPath();
+ }, $mountsContainingFile);
+ $mountProviders = array_unique(array_map(function ($mount) {
+ return $mount->getMountProvider();
+ }, $mountsContainingFile));
+ $mountRoots = array_combine($mountRootIds, $mountRootPaths);
+
+ $mounts = $this->mountManager->getMountsByMountProvider($path, $mountProviders);
+
+ $mountsContainingFile = array_filter($mounts, function ($mount) use ($mountRoots) {
+ return isset($mountRoots[$mount->getStorageRootId()]);
+ });
+
+ if (count($mountsContainingFile) === 0) {
+ if ($user === $this->getAppDataDirectoryName()) {
+ $folder = $this->get($path);
+ if ($folder instanceof Folder) {
+ return $folder->getByIdInRootMount($id);
+ } else {
+ throw new \Exception('getByIdInPath with non folder');
+ }
+ }
+ return [];
+ }
+
+ $nodes = array_map(function (IMountPoint $mount) use ($id, $mountRoots) {
+ $rootInternalPath = $mountRoots[$mount->getStorageRootId()];
+ $cacheEntry = $mount->getStorage()->getCache()->get($id);
+ if (!$cacheEntry) {
+ return null;
+ }
+
+ // cache jails will hide the "true" internal path
+ $internalPath = ltrim($rootInternalPath . '/' . $cacheEntry->getPath(), '/');
+ $pathRelativeToMount = substr($internalPath, strlen($rootInternalPath));
+ $pathRelativeToMount = ltrim($pathRelativeToMount, '/');
+ $absolutePath = rtrim($mount->getMountPoint() . $pathRelativeToMount, '/');
+ $storage = $mount->getStorage();
+ if ($storage === null) {
+ return null;
+ }
+ $ownerId = $storage->getOwner($pathRelativeToMount);
+ if ($ownerId !== false) {
+ $owner = Server::get(IUserManager::class)->get($ownerId);
+ } else {
+ $owner = null;
+ }
+ return $this->createNode($absolutePath, new FileInfo(
+ $absolutePath,
+ $storage,
+ $cacheEntry->getPath(),
+ $cacheEntry,
+ $mount,
+ $owner,
+ ));
+ }, $mountsContainingFile);
+
+ $nodes = array_filter($nodes);
+
+ $folders = array_filter($nodes, function (Node $node) use ($path) {
+ return PathHelper::getRelativePath($path, $node->getPath()) !== null;
+ });
+ usort($folders, function ($a, $b) {
+ return $b->getPath() <=> $a->getPath();
+ });
+ return $folders;
+ }
+
+ public function getNodeFromCacheEntryAndMount(ICacheEntry $cacheEntry, IMountPoint $mountPoint): INode {
+ $path = $cacheEntry->getPath();
+ $fullPath = $mountPoint->getMountPoint() . $path;
+ // todo: LazyNode?
+ $info = new FileInfo($fullPath, $mountPoint->getStorage(), $path, $cacheEntry, $mountPoint);
+ $parentPath = dirname($fullPath);
+ $parent = new LazyFolder($this, function () use ($parentPath) {
+ $parent = $this->get($parentPath);
+ if ($parent instanceof \OCP\Files\Folder) {
+ return $parent;
+ } else {
+ throw new \Exception("parent $parentPath is not a folder");
+ }
+ }, [
+ 'path' => $parentPath,
+ ]);
+ $isDir = $info->getType() === FileInfo::TYPE_FOLDER;
+ $view = new View('');
+ if ($isDir) {
+ return new Folder($this, $view, $fullPath, $info, $parent);
+ } else {
+ return new File($this, $view, $fullPath, $info, $parent);
+ }
+ }
}