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.php104
1 files changed, 61 insertions, 43 deletions
diff --git a/lib/private/Files/Node/Root.php b/lib/private/Files/Node/Root.php
index ee344f9be8b..d82ec08f362 100644
--- a/lib/private/Files/Node/Root.php
+++ b/lib/private/Files/Node/Root.php
@@ -1,35 +1,10 @@
<?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\Files\FileInfo;
@@ -49,8 +24,11 @@ use OCP\Files\Mount\IMountPoint;
use OCP\Files\Node as INode;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
+use OCP\ICache;
+use OCP\ICacheFactory;
use OCP\IUser;
use OCP\IUserManager;
+use OCP\Server;
use Psr\Log\LoggerInterface;
/**
@@ -81,6 +59,7 @@ class Root extends Folder implements IRootFolder {
private LoggerInterface $logger;
private IUserManager $userManager;
private IEventDispatcher $eventDispatcher;
+ private ICache $pathByIdCache;
/**
* @param Manager $manager
@@ -94,7 +73,8 @@ class Root extends Folder implements IRootFolder {
IUserMountCache $userMountCache,
LoggerInterface $logger,
IUserManager $userManager,
- IEventDispatcher $eventDispatcher
+ IEventDispatcher $eventDispatcher,
+ ICacheFactory $cacheFactory,
) {
parent::__construct($this, $view, '');
$this->mountManager = $manager;
@@ -107,6 +87,7 @@ class Root extends Folder implements IRootFolder {
$eventDispatcher->addListener(FilesystemTornDownEvent::class, function () {
$this->userFolderCache = new CappedMemoryCache();
});
+ $this->pathByIdCache = $cacheFactory->createLocal('path-by-id');
}
/**
@@ -132,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);
}
@@ -190,12 +171,6 @@ class Root extends Folder implements IRootFolder {
$this->mountManager->remove($mount);
}
- /**
- * @param string $path
- * @return Node
- * @throws \OCP\Files\NotPermittedException
- * @throws \OCP\Files\NotFoundException
- */
public function get($path) {
$path = $this->normalizePath($path);
if ($this->isValidPath($path)) {
@@ -383,7 +358,7 @@ class Root extends Folder implements IRootFolder {
try {
$folder = $this->get('/' . $userId . '/files');
if (!$folder instanceof \OCP\Files\Folder) {
- throw new \Exception("User folder for $userId exists as a file");
+ throw new \Exception("Account folder for \"$userId\" exists as a file");
}
} catch (NotFoundException $e) {
if (!$this->nodeExists('/' . $userId)) {
@@ -405,6 +380,35 @@ class Root extends Folder implements IRootFolder {
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[]
@@ -454,7 +458,7 @@ class Root extends Folder implements IRootFolder {
if ($folder instanceof Folder) {
return $folder->getByIdInRootMount($id);
} else {
- throw new \Exception("getByIdInPath with non folder");
+ throw new \Exception('getByIdInPath with non folder');
}
}
return [];
@@ -472,9 +476,23 @@ class Root extends Folder implements IRootFolder {
$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, $mount->getStorage(), $cacheEntry->getPath(), $cacheEntry, $mount,
- \OC::$server->getUserManager()->get($mount->getStorage()->getOwner($pathRelativeToMount))
+ $absolutePath,
+ $storage,
+ $cacheEntry->getPath(),
+ $cacheEntry,
+ $mount,
+ $owner,
));
}, $mountsContainingFile);
@@ -508,9 +526,9 @@ class Root extends Folder implements IRootFolder {
$isDir = $info->getType() === FileInfo::TYPE_FOLDER;
$view = new View('');
if ($isDir) {
- return new Folder($this, $view, $path, $info, $parent);
+ return new Folder($this, $view, $fullPath, $info, $parent);
} else {
- return new File($this, $view, $path, $info, $parent);
+ return new File($this, $view, $fullPath, $info, $parent);
}
}
}