diff options
Diffstat (limited to 'lib/private/Files/Node')
-rw-r--r-- | lib/private/Files/Node/File.php | 45 | ||||
-rw-r--r-- | lib/private/Files/Node/Folder.php | 196 | ||||
-rw-r--r-- | lib/private/Files/Node/HookConnector.php | 114 | ||||
-rw-r--r-- | lib/private/Files/Node/LazyFolder.php | 138 | ||||
-rw-r--r-- | lib/private/Files/Node/LazyRoot.php | 54 | ||||
-rw-r--r-- | lib/private/Files/Node/LazyUserFolder.php | 73 | ||||
-rw-r--r-- | lib/private/Files/Node/Node.php | 197 | ||||
-rw-r--r-- | lib/private/Files/Node/NonExistingFile.php | 35 | ||||
-rw-r--r-- | lib/private/Files/Node/NonExistingFolder.php | 48 | ||||
-rw-r--r-- | lib/private/Files/Node/Root.php | 152 |
10 files changed, 552 insertions, 500 deletions
diff --git a/lib/private/Files/Node/File.php b/lib/private/Files/Node/File.php index d8a6741dc6e..eb6411d7d13 100644 --- a/lib/private/Files/Node/File.php +++ b/lib/private/Files/Node/File.php @@ -1,30 +1,9 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Arthur Schiwon <blizzz@arthur-schiwon.de> - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Joas Schilling <coding@schilljs.com> - * @author Julius Härtl <jus@bitgrid.net> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Robin Appelman <robin@icewind.nl> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @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; @@ -37,7 +16,7 @@ class File extends Node implements \OCP\Files\File { * Creates a Folder that represents a non-existing path * * @param string $path path - * @return string non-existing node class + * @return NonExistingFile non-existing node */ protected function createNonExistingNode($path) { return new NonExistingFile($this->root, $this->view, $path); @@ -46,14 +25,16 @@ class File extends Node implements \OCP\Files\File { /** * @return string * @throws NotPermittedException + * @throws GenericFileException * @throws LockedException */ public function getContent() { if ($this->checkPermissions(\OCP\Constants::PERMISSION_READ)) { - /** - * @var \OC\Files\Storage\Storage $storage; - */ - return $this->view->file_get_contents($this->path); + $content = $this->view->file_get_contents($this->path); + if ($content === false) { + throw new GenericFileException(); + } + return $content; } else { throw new NotPermittedException(); } @@ -62,7 +43,7 @@ class File extends Node implements \OCP\Files\File { /** * @param string|resource $data * @throws NotPermittedException - * @throws \OCP\Files\GenericFileException + * @throws GenericFileException * @throws LockedException */ public function putContent($data) { @@ -80,7 +61,7 @@ class File extends Node implements \OCP\Files\File { /** * @param string $mode - * @return resource + * @return resource|false * @throws NotPermittedException * @throws LockedException */ diff --git a/lib/private/Files/Node/Folder.php b/lib/private/Files/Node/Folder.php index bf9ae3c148d..7453b553119 100644 --- a/lib/private/Files/Node/Folder.php +++ b/lib/private/Files/Node/Folder.php @@ -1,45 +1,23 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Arthur Schiwon <blizzz@arthur-schiwon.de> - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Georg Ehrke <oc.list@georgehrke.com> - * @author Joas Schilling <coding@schilljs.com> - * @author Julius Härtl <jus@bitgrid.net> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Robin Appelman <robin@icewind.nl> - * @author Robin McCorkell <robin@mccorkell.me.uk> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @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: 2022 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ namespace OC\Files\Node; use OC\Files\Cache\QuerySearchHelper; use OC\Files\Search\SearchBinaryOperator; -use OC\Files\Cache\Wrapper\CacheJail; use OC\Files\Search\SearchComparison; use OC\Files\Search\SearchOrder; use OC\Files\Search\SearchQuery; use OC\Files\Utils\PathHelper; +use OC\User\LazyUser; use OCP\Files\Cache\ICacheEntry; use OCP\Files\FileInfo; use OCP\Files\Mount\IMountPoint; +use OCP\Files\Node as INode; use OCP\Files\NotFoundException; use OCP\Files\NotPermittedException; use OCP\Files\Search\ISearchBinaryOperator; @@ -50,11 +28,14 @@ use OCP\Files\Search\ISearchQuery; use OCP\IUserManager; class Folder extends Node implements \OCP\Files\Folder { + + private ?IUserManager $userManager = null; + /** * Creates a Folder that represents a non-existing path * * @param string $path path - * @return string non-existing node class + * @return NonExistingFolder non-existing node */ protected function createNonExistingNode($path) { return new NonExistingFolder($this->root, $this->view, $path); @@ -68,7 +49,7 @@ class Folder extends Node implements \OCP\Files\Folder { public function getFullPath($path) { $path = $this->normalizePath($path); if (!$this->isValidPath($path)) { - throw new NotPermittedException('Invalid path'); + throw new NotPermittedException('Invalid path "' . $path . '"'); } return $this->path . $path; } @@ -88,7 +69,7 @@ class Folder extends Node implements \OCP\Files\Folder { * @return bool */ public function isSubNode($node) { - return strpos($node->getPath(), $this->path . '/') === 0; + return str_starts_with($node->getPath(), $this->path . '/'); } /** @@ -98,7 +79,7 @@ class Folder extends Node implements \OCP\Files\Folder { * @throws \OCP\Files\NotFoundException */ public function getDirectoryListing() { - $folderContent = $this->view->getDirectoryContent($this->path, '', $this->getFileInfo()); + $folderContent = $this->view->getDirectoryContent($this->path, '', $this->getFileInfo(false)); return array_map(function (FileInfo $info) { if ($info->getMimetype() === FileInfo::MIMETYPE_FOLDER) { @@ -109,12 +90,7 @@ class Folder extends Node implements \OCP\Files\Folder { }, $folderContent); } - /** - * @param string $path - * @param FileInfo $info - * @return File|Folder - */ - protected function createNode($path, FileInfo $info = null) { + protected function createNode(string $path, ?FileInfo $info = null, bool $infoHasSubMountsIncluded = true): INode { if (is_null($info)) { $isDir = $this->view->is_dir($path); } else { @@ -122,32 +98,21 @@ class Folder extends Node implements \OCP\Files\Folder { } $parent = dirname($path) === $this->getPath() ? $this : null; if ($isDir) { - return new Folder($this->root, $this->view, $path, $info, $parent); + return new Folder($this->root, $this->view, $path, $info, $parent, $infoHasSubMountsIncluded); } else { return new File($this->root, $this->view, $path, $info, $parent); } } - /** - * Get the node at $path - * - * @param string $path - * @return \OC\Files\Node\Node - * @throws \OCP\Files\NotFoundException - */ public function get($path) { return $this->root->get($this->getFullPath($path)); } - /** - * @param string $path - * @return bool - */ public function nodeExists($path) { try { $this->get($path); return true; - } catch (NotFoundException $e) { + } catch (NotFoundException|NotPermittedException) { return false; } } @@ -163,14 +128,27 @@ class Folder extends Node implements \OCP\Files\Folder { $nonExisting = new NonExistingFolder($this->root, $this->view, $fullPath); $this->sendHooks(['preWrite', 'preCreate'], [$nonExisting]); if (!$this->view->mkdir($fullPath)) { - throw new NotPermittedException('Could not create folder'); + // maybe another concurrent process created the folder already + if (!$this->view->is_dir($fullPath)) { + throw new NotPermittedException('Could not create folder "' . $fullPath . '"'); + } else { + // we need to ensure we don't return before the concurrent request has finished updating the cache + $tries = 5; + while (!$this->view->getFileInfo($fullPath)) { + if ($tries < 1) { + throw new NotPermittedException('Could not create folder "' . $fullPath . '", folder exists but unable to get cache entry'); + } + usleep(5 * 1000); + $tries--; + } + } } $parent = dirname($fullPath) === $this->getPath() ? $this : null; $node = new Folder($this->root, $this->view, $fullPath, null, $parent); $this->sendHooks(['postWrite', 'postCreate'], [$node]); return $node; } else { - throw new NotPermittedException('No create permission for folder'); + throw new NotPermittedException('No create permission for folder "' . $path . '"'); } } @@ -181,7 +159,7 @@ class Folder extends Node implements \OCP\Files\Folder { * @throws \OCP\Files\NotPermittedException */ public function newFile($path, $content = null) { - if (empty($path)) { + if ($path === '') { throw new NotPermittedException('Could not create as provided path is empty'); } if ($this->checkPermissions(\OCP\Constants::PERMISSION_CREATE)) { @@ -194,24 +172,24 @@ class Folder extends Node implements \OCP\Files\Folder { $result = $this->view->touch($fullPath); } if ($result === false) { - throw new NotPermittedException('Could not create path'); + throw new NotPermittedException('Could not create path "' . $fullPath . '"'); } $node = new File($this->root, $this->view, $fullPath, null, $this); $this->sendHooks(['postWrite', 'postCreate'], [$node]); return $node; } - throw new NotPermittedException('No create permission for path'); + throw new NotPermittedException('No create permission for path "' . $path . '"'); } - private function queryFromOperator(ISearchOperator $operator, string $uid = null): ISearchQuery { + private function queryFromOperator(ISearchOperator $operator, ?string $uid = null, int $limit = 0, int $offset = 0): ISearchQuery { if ($uid === null) { $user = null; } else { /** @var IUserManager $userManager */ - $userManager = \OC::$server->query(IUserManager::class); + $userManager = \OCP\Server::get(IUserManager::class); $user = $userManager->get($uid); } - return new SearchQuery($operator, 0, 0, [], $user); + return new SearchQuery($operator, $limit, $offset, [], $user); } /** @@ -230,43 +208,16 @@ class Folder extends Node implements \OCP\Files\Folder { $limitToHome = $query->limitToHome(); if ($limitToHome && count(explode('/', $this->path)) !== 3) { - throw new \InvalidArgumentException('searching by owner is only allows on the users home folder'); - } - - $rootLength = strlen($this->path); - $mount = $this->root->getMount($this->path); - $storage = $mount->getStorage(); - $internalPath = $mount->getInternalPath($this->path); - - // collect all caches for this folder, indexed by their mountpoint relative to this folder - // and save the mount which is needed later to construct the FileInfo objects - - if ($internalPath !== '') { - // a temporary CacheJail is used to handle filtering down the results to within this folder - $caches = ['' => new CacheJail($storage->getCache(''), $internalPath)]; - } else { - $caches = ['' => $storage->getCache('')]; - } - $mountByMountPoint = ['' => $mount]; - - if (!$limitToHome) { - $mounts = $this->root->getMountsIn($this->path); - foreach ($mounts as $mount) { - $storage = $mount->getStorage(); - if ($storage) { - $relativeMountPoint = ltrim(substr($mount->getMountPoint(), $rootLength), '/'); - $caches[$relativeMountPoint] = $storage->getCache(''); - $mountByMountPoint[$relativeMountPoint] = $mount; - } - } + throw new \InvalidArgumentException('searching by owner is only allowed in the users home folder'); } /** @var QuerySearchHelper $searchHelper */ $searchHelper = \OC::$server->get(QuerySearchHelper::class); + [$caches, $mountByMountPoint] = $searchHelper->getCachesAndMountPointsForSearch($this->root, $this->path, $limitToHome); $resultsPerCache = $searchHelper->searchInCaches($query, $caches); // loop through all results per-cache, constructing the FileInfo object from the CacheEntry and merge them all - $files = array_merge(...array_map(function (array $results, $relativeMountPoint) use ($mountByMountPoint) { + $files = array_merge(...array_map(function (array $results, string $relativeMountPoint) use ($mountByMountPoint) { $mount = $mountByMountPoint[$relativeMountPoint]; return array_map(function (ICacheEntry $result) use ($relativeMountPoint, $mount) { return $this->cacheEntryToFileInfo($mount, $relativeMountPoint, $result); @@ -274,9 +225,9 @@ class Folder extends Node implements \OCP\Files\Folder { }, array_values($resultsPerCache), array_keys($resultsPerCache))); // don't include this folder in the results - $files = array_filter($files, function (FileInfo $file) { + $files = array_values(array_filter($files, function (FileInfo $file) { return $file->getPath() !== $this->getPath(); - }); + })); // since results were returned per-cache, they are no longer fully sorted $order = $query->getOrder(); @@ -301,7 +252,26 @@ class Folder extends Node implements \OCP\Files\Folder { $cacheEntry['internalPath'] = $cacheEntry['path']; $cacheEntry['path'] = rtrim($appendRoot . $cacheEntry->getPath(), '/'); $subPath = $cacheEntry['path'] !== '' ? '/' . $cacheEntry['path'] : ''; - return new \OC\Files\FileInfo($this->path . $subPath, $mount->getStorage(), $cacheEntry['internalPath'], $cacheEntry, $mount); + $storage = $mount->getStorage(); + + $owner = null; + $ownerId = $storage->getOwner($cacheEntry['internalPath']); + if ($ownerId !== false) { + // Cache the user manager (for performance) + if ($this->userManager === null) { + $this->userManager = \OCP\Server::get(IUserManager::class); + } + $owner = new LazyUser($ownerId, $this->userManager); + } + + return new \OC\Files\FileInfo( + $this->path . $subPath, + $storage, + $cacheEntry['internalPath'], + $cacheEntry, + $mount, + $owner, + ); } /** @@ -311,7 +281,7 @@ class Folder extends Node implements \OCP\Files\Folder { * @return Node[] */ public function searchByMime($mimetype) { - if (strpos($mimetype, '/') === false) { + if (!str_contains($mimetype, '/')) { $query = $this->queryFromOperator(new SearchComparison(ISearchComparison::COMPARE_LIKE, 'mimetype', $mimetype . '/%')); } else { $query = $this->queryFromOperator(new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'mimetype', $mimetype)); @@ -331,15 +301,24 @@ class Folder extends Node implements \OCP\Files\Folder { return $this->search($query); } + public function searchBySystemTag(string $tagName, string $userId, int $limit = 0, int $offset = 0): array { + $query = $this->queryFromOperator(new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'systemtag', $tagName), $userId, $limit, $offset); + return $this->search($query); + } + /** * @param int $id - * @return \OC\Files\Node\Node[] + * @return \OCP\Files\Node[] */ public function getById($id) { return $this->root->getByIdInPath((int)$id, $this->getPath()); } - protected function getAppDataDirectoryName(): string { + public function getFirstNodeById(int $id): ?\OCP\Files\Node { + return $this->root->getFirstNodeByIdInPath($id, $this->getPath()); + } + + public function getAppDataDirectoryName(): string { $instanceId = \OC::$server->getConfig()->getSystemValueString('instanceid'); return 'appdata_' . $instanceId; } @@ -357,8 +336,15 @@ class Folder extends Node implements \OCP\Files\Folder { * @return array */ protected function getByIdInRootMount(int $id): array { + if (!method_exists($this->root, 'createNode')) { + // Always expected to be false. Being a method of Folder, this is + // always implemented. For it is an internal method and should not + // be exposed and made public, it is not part of an interface. + return []; + } $mount = $this->root->getMount(''); - $cacheEntry = $mount->getStorage()->getCache($this->path)->get($id); + $storage = $mount->getStorage(); + $cacheEntry = $storage?->getCache($this->path)->get($id); if (!$cacheEntry) { return []; } @@ -366,14 +352,14 @@ class Folder extends Node implements \OCP\Files\Folder { $absolutePath = '/' . ltrim($cacheEntry->getPath(), '/'); $currentPath = rtrim($this->path, '/') . '/'; - if (strpos($absolutePath, $currentPath) !== 0) { + if (!str_starts_with($absolutePath, $currentPath)) { return []; } return [$this->root->createNode( $absolutePath, new \OC\Files\FileInfo( $absolutePath, - $mount->getStorage(), + $storage, $cacheEntry->getPath(), $cacheEntry, $mount @@ -392,7 +378,7 @@ class Folder extends Node implements \OCP\Files\Folder { $nonExisting = new NonExistingFolder($this->root, $this->view, $this->path, $fileInfo); $this->sendHooks(['postDelete'], [$nonExisting]); } else { - throw new NotPermittedException('No delete permission for path'); + throw new NotPermittedException('No delete permission for path "' . $this->path . '"'); } } @@ -411,7 +397,7 @@ class Folder extends Node implements \OCP\Files\Folder { /** * @param int $limit * @param int $offset - * @return \OCP\Files\Node[] + * @return INode[] */ public function getRecent($limit, $offset = 0) { $filterOutNonEmptyFolder = new SearchBinaryOperator( @@ -439,7 +425,7 @@ class Folder extends Node implements \OCP\Files\Folder { $filterNonRecentFiles = new SearchComparison( ISearchComparison::COMPARE_GREATER_THAN, 'mtime', - strtotime("-2 week") + strtotime('-2 week') ); if ($offset === 0 && $limit <= 100) { $query = new SearchQuery( @@ -475,4 +461,12 @@ class Folder extends Node implements \OCP\Files\Folder { return $this->search($query); } + + public function verifyPath($fileName, $readonly = false): void { + $this->view->verifyPath( + $this->getPath(), + $fileName, + $readonly, + ); + } } diff --git a/lib/private/Files/Node/HookConnector.php b/lib/private/Files/Node/HookConnector.php index 149ffafd46b..1149951174c 100644 --- a/lib/private/Files/Node/HookConnector.php +++ b/lib/private/Files/Node/HookConnector.php @@ -1,25 +1,10 @@ <?php + +declare(strict_types=1); /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Arthur Schiwon <blizzz@arthur-schiwon.de> - * @author Robin Appelman <robin@icewind.nl> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @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; @@ -27,6 +12,7 @@ use OC\Files\Filesystem; use OC\Files\View; use OCP\EventDispatcher\GenericEvent; use OCP\EventDispatcher\IEventDispatcher; +use OCP\Exceptions\AbortedEventException; use OCP\Files\Events\Node\BeforeNodeCopiedEvent; use OCP\Files\Events\Node\BeforeNodeCreatedEvent; use OCP\Files\Events\Node\BeforeNodeDeletedEvent; @@ -43,39 +29,18 @@ use OCP\Files\Events\Node\NodeWrittenEvent; use OCP\Files\FileInfo; use OCP\Files\IRootFolder; use OCP\Util; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use Psr\Log\LoggerInterface; class HookConnector { - /** @var IRootFolder */ - private $root; - - /** @var View */ - private $view; - /** @var FileInfo[] */ - private $deleteMetaCache = []; + private array $deleteMetaCache = []; - /** @var EventDispatcherInterface */ - private $legacyDispatcher; - - /** @var IEventDispatcher */ - private $dispatcher; - - /** - * HookConnector constructor. - * - * @param Root $root - * @param View $view - */ public function __construct( - IRootFolder $root, - View $view, - EventDispatcherInterface $legacyDispatcher, - IEventDispatcher $dispatcher) { - $this->root = $root; - $this->view = $view; - $this->legacyDispatcher = $legacyDispatcher; - $this->dispatcher = $dispatcher; + private IRootFolder $root, + private View $view, + private IEventDispatcher $dispatcher, + private LoggerInterface $logger, + ) { } public function viewToNode() { @@ -103,7 +68,7 @@ class HookConnector { public function write($arguments) { $node = $this->getNodeForPath($arguments['path']); $this->root->emit('\OC\Files', 'preWrite', [$node]); - $this->legacyDispatcher->dispatch('\OCP\Files::preWrite', new GenericEvent($node)); + $this->dispatcher->dispatch('\OCP\Files::preWrite', new GenericEvent($node)); $event = new BeforeNodeWrittenEvent($node); $this->dispatcher->dispatchTyped($event); @@ -112,7 +77,7 @@ class HookConnector { public function postWrite($arguments) { $node = $this->getNodeForPath($arguments['path']); $this->root->emit('\OC\Files', 'postWrite', [$node]); - $this->legacyDispatcher->dispatch('\OCP\Files::postWrite', new GenericEvent($node)); + $this->dispatcher->dispatch('\OCP\Files::postWrite', new GenericEvent($node)); $event = new NodeWrittenEvent($node); $this->dispatcher->dispatchTyped($event); @@ -121,7 +86,7 @@ class HookConnector { public function create($arguments) { $node = $this->getNodeForPath($arguments['path']); $this->root->emit('\OC\Files', 'preCreate', [$node]); - $this->legacyDispatcher->dispatch('\OCP\Files::preCreate', new GenericEvent($node)); + $this->dispatcher->dispatch('\OCP\Files::preCreate', new GenericEvent($node)); $event = new BeforeNodeCreatedEvent($node); $this->dispatcher->dispatchTyped($event); @@ -130,7 +95,7 @@ class HookConnector { public function postCreate($arguments) { $node = $this->getNodeForPath($arguments['path']); $this->root->emit('\OC\Files', 'postCreate', [$node]); - $this->legacyDispatcher->dispatch('\OCP\Files::postCreate', new GenericEvent($node)); + $this->dispatcher->dispatch('\OCP\Files::postCreate', new GenericEvent($node)); $event = new NodeCreatedEvent($node); $this->dispatcher->dispatchTyped($event); @@ -140,17 +105,22 @@ class HookConnector { $node = $this->getNodeForPath($arguments['path']); $this->deleteMetaCache[$node->getPath()] = $node->getFileInfo(); $this->root->emit('\OC\Files', 'preDelete', [$node]); - $this->legacyDispatcher->dispatch('\OCP\Files::preDelete', new GenericEvent($node)); + $this->dispatcher->dispatch('\OCP\Files::preDelete', new GenericEvent($node)); $event = new BeforeNodeDeletedEvent($node); - $this->dispatcher->dispatchTyped($event); + try { + $this->dispatcher->dispatchTyped($event); + } catch (AbortedEventException $e) { + $arguments['run'] = false; + $this->logger->warning('delete process aborted', ['exception' => $e]); + } } public function postDelete($arguments) { $node = $this->getNodeForPath($arguments['path']); unset($this->deleteMetaCache[$node->getPath()]); $this->root->emit('\OC\Files', 'postDelete', [$node]); - $this->legacyDispatcher->dispatch('\OCP\Files::postDelete', new GenericEvent($node)); + $this->dispatcher->dispatch('\OCP\Files::postDelete', new GenericEvent($node)); $event = new NodeDeletedEvent($node); $this->dispatcher->dispatchTyped($event); @@ -159,7 +129,7 @@ class HookConnector { public function touch($arguments) { $node = $this->getNodeForPath($arguments['path']); $this->root->emit('\OC\Files', 'preTouch', [$node]); - $this->legacyDispatcher->dispatch('\OCP\Files::preTouch', new GenericEvent($node)); + $this->dispatcher->dispatch('\OCP\Files::preTouch', new GenericEvent($node)); $event = new BeforeNodeTouchedEvent($node); $this->dispatcher->dispatchTyped($event); @@ -168,7 +138,7 @@ class HookConnector { public function postTouch($arguments) { $node = $this->getNodeForPath($arguments['path']); $this->root->emit('\OC\Files', 'postTouch', [$node]); - $this->legacyDispatcher->dispatch('\OCP\Files::postTouch', new GenericEvent($node)); + $this->dispatcher->dispatch('\OCP\Files::postTouch', new GenericEvent($node)); $event = new NodeTouchedEvent($node); $this->dispatcher->dispatchTyped($event); @@ -178,17 +148,22 @@ class HookConnector { $source = $this->getNodeForPath($arguments['oldpath']); $target = $this->getNodeForPath($arguments['newpath']); $this->root->emit('\OC\Files', 'preRename', [$source, $target]); - $this->legacyDispatcher->dispatch('\OCP\Files::preRename', new GenericEvent([$source, $target])); + $this->dispatcher->dispatch('\OCP\Files::preRename', new GenericEvent([$source, $target])); $event = new BeforeNodeRenamedEvent($source, $target); - $this->dispatcher->dispatchTyped($event); + try { + $this->dispatcher->dispatchTyped($event); + } catch (AbortedEventException $e) { + $arguments['run'] = false; + $this->logger->warning('rename process aborted', ['exception' => $e]); + } } public function postRename($arguments) { $source = $this->getNodeForPath($arguments['oldpath']); $target = $this->getNodeForPath($arguments['newpath']); $this->root->emit('\OC\Files', 'postRename', [$source, $target]); - $this->legacyDispatcher->dispatch('\OCP\Files::postRename', new GenericEvent([$source, $target])); + $this->dispatcher->dispatch('\OCP\Files::postRename', new GenericEvent([$source, $target])); $event = new NodeRenamedEvent($source, $target); $this->dispatcher->dispatchTyped($event); @@ -196,19 +171,24 @@ class HookConnector { public function copy($arguments) { $source = $this->getNodeForPath($arguments['oldpath']); - $target = $this->getNodeForPath($arguments['newpath']); + $target = $this->getNodeForPath($arguments['newpath'], $source instanceof Folder); $this->root->emit('\OC\Files', 'preCopy', [$source, $target]); - $this->legacyDispatcher->dispatch('\OCP\Files::preCopy', new GenericEvent([$source, $target])); + $this->dispatcher->dispatch('\OCP\Files::preCopy', new GenericEvent([$source, $target])); $event = new BeforeNodeCopiedEvent($source, $target); - $this->dispatcher->dispatchTyped($event); + try { + $this->dispatcher->dispatchTyped($event); + } catch (AbortedEventException $e) { + $arguments['run'] = false; + $this->logger->warning('copy process aborted', ['exception' => $e]); + } } public function postCopy($arguments) { $source = $this->getNodeForPath($arguments['oldpath']); $target = $this->getNodeForPath($arguments['newpath']); $this->root->emit('\OC\Files', 'postCopy', [$source, $target]); - $this->legacyDispatcher->dispatch('\OCP\Files::postCopy', new GenericEvent([$source, $target])); + $this->dispatcher->dispatch('\OCP\Files::postCopy', new GenericEvent([$source, $target])); $event = new NodeCopiedEvent($source, $target); $this->dispatcher->dispatchTyped($event); @@ -217,13 +197,13 @@ class HookConnector { public function read($arguments) { $node = $this->getNodeForPath($arguments['path']); $this->root->emit('\OC\Files', 'read', [$node]); - $this->legacyDispatcher->dispatch('\OCP\Files::read', new GenericEvent([$node])); + $this->dispatcher->dispatch('\OCP\Files::read', new GenericEvent([$node])); $event = new BeforeNodeReadEvent($node); $this->dispatcher->dispatchTyped($event); } - private function getNodeForPath($path) { + private function getNodeForPath(string $path, bool $isDir = false): Node { $info = Filesystem::getView()->getFileInfo($path); if (!$info) { $fullPath = Filesystem::getView()->getAbsolutePath($path); @@ -232,7 +212,7 @@ class HookConnector { } else { $info = null; } - if (Filesystem::is_dir($path)) { + if ($isDir || Filesystem::is_dir($path)) { return new NonExistingFolder($this->root, $this->view, $fullPath, $info); } else { return new NonExistingFile($this->root, $this->view, $fullPath, $info); diff --git a/lib/private/Files/Node/LazyFolder.php b/lib/private/Files/Node/LazyFolder.php index 1bae0f52e59..37b1efa0fad 100644 --- a/lib/private/Files/Node/LazyFolder.php +++ b/lib/private/Files/Node/LazyFolder.php @@ -1,33 +1,19 @@ <?php declare(strict_types=1); - /** - * @copyright Copyright (c) 2020 Robin Appelman <robin@icewind.nl> - * - * @author Robin Appelman <robin@icewind.nl> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * 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 - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ - namespace OC\Files\Node; +use OC\Files\Filesystem; use OC\Files\Utils\PathHelper; use OCP\Constants; +use OCP\Files\Folder; +use OCP\Files\IRootFolder; +use OCP\Files\Mount\IMountPoint; +use OCP\Files\NotPermittedException; /** * Class LazyFolder @@ -37,25 +23,35 @@ use OCP\Constants; * * @package OC\Files\Node */ -class LazyFolder implements \OCP\Files\Folder { - /** @var \Closure */ - private $folderClosure; - - /** @var LazyFolder | null */ - protected $folder = null; - +class LazyFolder implements Folder { + /** @var \Closure(): Folder */ + private \Closure $folderClosure; + protected ?Folder $folder = null; + protected IRootFolder $rootFolder; protected array $data; /** - * LazyFolder constructor. - * - * @param \Closure $folderClosure + * @param IRootFolder $rootFolder + * @param \Closure(): Folder $folderClosure + * @param array $data */ - public function __construct(\Closure $folderClosure, array $data = []) { + public function __construct(IRootFolder $rootFolder, \Closure $folderClosure, array $data = []) { + $this->rootFolder = $rootFolder; $this->folderClosure = $folderClosure; $this->data = $data; } + protected function getRootFolder(): IRootFolder { + return $this->rootFolder; + } + + protected function getRealFolder(): Folder { + if ($this->folder === null) { + $this->folder = call_user_func($this->folderClosure); + } + return $this->folder; + } + /** * Magic method to first get the real rootFolder and then * call $method with $args on it @@ -65,11 +61,7 @@ class LazyFolder implements \OCP\Files\Folder { * @return mixed */ public function __call($method, $args) { - if ($this->folder === null) { - $this->folder = call_user_func($this->folderClosure); - } - - return call_user_func_array([$this->folder, $method], $args); + return call_user_func_array([$this->getRealFolder(), $method], $args); } /** @@ -89,7 +81,7 @@ class LazyFolder implements \OCP\Files\Folder { /** * @inheritDoc */ - public function removeListener($scope = null, $method = null, callable $callback = null) { + public function removeListener($scope = null, $method = null, ?callable $callback = null) { $this->__call(__FUNCTION__, func_get_args()); } @@ -110,14 +102,14 @@ class LazyFolder implements \OCP\Files\Folder { /** * @inheritDoc */ - public function getMount($mountPoint) { + public function getMount(string $mountPoint): IMountPoint { return $this->__call(__FUNCTION__, func_get_args()); } /** - * @inheritDoc + * @return IMountPoint[] */ - public function getMountsIn($mountPoint) { + public function getMountsIn(string $mountPoint): array { return $this->__call(__FUNCTION__, func_get_args()); } @@ -142,11 +134,8 @@ class LazyFolder implements \OCP\Files\Folder { $this->__call(__FUNCTION__, func_get_args()); } - /** - * @inheritDoc - */ public function get($path) { - return $this->__call(__FUNCTION__, func_get_args()); + return $this->getRootFolder()->get($this->getFullPath($path)); } /** @@ -205,6 +194,9 @@ class LazyFolder implements \OCP\Files\Folder { * @inheritDoc */ public function getId() { + if (isset($this->data['fileid'])) { + return $this->data['fileid']; + } return $this->__call(__FUNCTION__, func_get_args()); } @@ -219,13 +211,19 @@ class LazyFolder implements \OCP\Files\Folder { * @inheritDoc */ public function getMTime() { + if (isset($this->data['mtime'])) { + return $this->data['mtime']; + } return $this->__call(__FUNCTION__, func_get_args()); } /** * @inheritDoc */ - public function getSize($includeMounts = true) { + public function getSize($includeMounts = true): int|float { + if (isset($this->data['size'])) { + return $this->data['size']; + } return $this->__call(__FUNCTION__, func_get_args()); } @@ -233,6 +231,9 @@ class LazyFolder implements \OCP\Files\Folder { * @inheritDoc */ public function getEtag() { + if (isset($this->data['etag'])) { + return $this->data['etag']; + } return $this->__call(__FUNCTION__, func_get_args()); } @@ -297,6 +298,12 @@ class LazyFolder implements \OCP\Files\Folder { * @inheritDoc */ public function getName() { + if (isset($this->data['path'])) { + return basename($this->data['path']); + } + if (isset($this->data['name'])) { + return $this->data['name']; + } return $this->__call(__FUNCTION__, func_get_args()); } @@ -388,6 +395,13 @@ class LazyFolder implements \OCP\Files\Folder { * @inheritDoc */ public function getFullPath($path) { + if (isset($this->data['path'])) { + $path = PathHelper::normalizePath($path); + if (!Filesystem::isValidPath($path)) { + throw new NotPermittedException('Invalid path "' . $path . '"'); + } + return $this->data['path'] . $path; + } return $this->__call(__FUNCTION__, func_get_args()); } @@ -405,9 +419,6 @@ class LazyFolder implements \OCP\Files\Folder { return $this->__call(__FUNCTION__, func_get_args()); } - /** - * @inheritDoc - */ public function nodeExists($path) { return $this->__call(__FUNCTION__, func_get_args()); } @@ -447,11 +458,19 @@ class LazyFolder implements \OCP\Files\Folder { return $this->__call(__FUNCTION__, func_get_args()); } + public function searchBySystemTag(string $tagName, string $userId, int $limit = 0, int $offset = 0) { + return $this->__call(__FUNCTION__, func_get_args()); + } + /** * @inheritDoc */ public function getById($id) { - return $this->__call(__FUNCTION__, func_get_args()); + return $this->getRootFolder()->getByIdInPath((int)$id, $this->getPath()); + } + + public function getFirstNodeById(int $id): ?\OCP\Files\Node { + return $this->getRootFolder()->getFirstNodeByIdInPath($id, $this->getPath()); } /** @@ -527,4 +546,23 @@ class LazyFolder implements \OCP\Files\Folder { public function getRelativePath($path) { return PathHelper::getRelativePath($this->getPath(), $path); } + + public function getParentId(): int { + if (isset($this->data['parent'])) { + return $this->data['parent']; + } + return $this->__call(__FUNCTION__, func_get_args()); + } + + /** + * @inheritDoc + * @return array<string, int|string|bool|float|string[]|int[]> + */ + public function getMetadata(): array { + return $this->data['metadata'] ?? $this->__call(__FUNCTION__, func_get_args()); + } + + public function verifyPath($fileName, $readonly = false): void { + $this->__call(__FUNCTION__, func_get_args()); + } } diff --git a/lib/private/Files/Node/LazyRoot.php b/lib/private/Files/Node/LazyRoot.php index c01b9fdbb83..bc3f3a2e80f 100644 --- a/lib/private/Files/Node/LazyRoot.php +++ b/lib/private/Files/Node/LazyRoot.php @@ -1,28 +1,17 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Robin Appelman <robin@icewind.nl> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @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 OCP\Files\Cache\ICacheEntry; use OCP\Files\IRootFolder; +use OCP\Files\Mount\IMountPoint; +use OCP\Files\Node; +use OCP\Files\Node as INode; /** * Class LazyRoot @@ -33,9 +22,18 @@ use OCP\Files\IRootFolder; * @package OC\Files\Node */ class LazyRoot extends LazyFolder implements IRootFolder { - /** - * @inheritDoc - */ + public function __construct(\Closure $folderClosure, array $data = []) { + parent::__construct($this, $folderClosure, $data); + } + + protected function getRootFolder(): IRootFolder { + $folder = $this->getRealFolder(); + if (!$folder instanceof IRootFolder) { + throw new \Exception('Lazy root folder closure didn\'t return a root folder'); + } + return $folder; + } + public function getUserFolder($userId) { return $this->__call(__FUNCTION__, func_get_args()); } @@ -43,4 +41,16 @@ class LazyRoot extends LazyFolder implements IRootFolder { public function getByIdInPath(int $id, string $path) { return $this->__call(__FUNCTION__, func_get_args()); } + + public function getFirstNodeByIdInPath(int $id, string $path): ?Node { + return $this->__call(__FUNCTION__, func_get_args()); + } + + public function getNodeFromCacheEntryAndMount(ICacheEntry $cacheEntry, IMountPoint $mountPoint): INode { + return $this->getRootFolder()->getNodeFromCacheEntryAndMount($cacheEntry, $mountPoint); + } + + public function getAppDataDirectoryName(): string { + return $this->__call(__FUNCTION__, func_get_args()); + } } diff --git a/lib/private/Files/Node/LazyUserFolder.php b/lib/private/Files/Node/LazyUserFolder.php index c85a356ddd3..77479c2fa5e 100644 --- a/lib/private/Files/Node/LazyUserFolder.php +++ b/lib/private/Files/Node/LazyUserFolder.php @@ -2,68 +2,65 @@ declare(strict_types=1); /** - * @copyright Copyright (c) 2022 Robin Appelman <robin@icewind.nl> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * 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 - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ - namespace OC\Files\Node; -use OCP\Files\FileInfo; use OCP\Constants; +use OCP\Files\File; +use OCP\Files\FileInfo; +use OCP\Files\Folder; use OCP\Files\IRootFolder; +use OCP\Files\Mount\IMountManager; use OCP\Files\NotFoundException; use OCP\IUser; +use Psr\Log\LoggerInterface; class LazyUserFolder extends LazyFolder { - private IRootFolder $root; private IUser $user; private string $path; + private IMountManager $mountManager; - public function __construct(IRootFolder $rootFolder, IUser $user) { - $this->root = $rootFolder; + public function __construct(IRootFolder $rootFolder, IUser $user, IMountManager $mountManager) { $this->user = $user; + $this->mountManager = $mountManager; $this->path = '/' . $user->getUID() . '/files'; - parent::__construct(function () use ($user) { + parent::__construct($rootFolder, function () use ($user): Folder { try { - return $this->root->get('/' . $user->getUID() . '/files'); + $node = $this->getRootFolder()->get($this->path); + if ($node instanceof File) { + $e = new \RuntimeException(); + \OCP\Server::get(LoggerInterface::class)->error('User root storage is not a folder: ' . $this->path, [ + 'exception' => $e, + ]); + throw $e; + } + return $node; } catch (NotFoundException $e) { - if (!$this->root->nodeExists('/' . $user->getUID())) { - $this->root->newFolder('/' . $user->getUID()); + if (!$this->getRootFolder()->nodeExists('/' . $user->getUID())) { + $this->getRootFolder()->newFolder('/' . $user->getUID()); } - return $this->root->newFolder('/' . $user->getUID() . '/files'); + return $this->getRootFolder()->newFolder($this->path); } }, [ 'path' => $this->path, - 'permissions' => Constants::PERMISSION_ALL, + // Sharing user root folder is not allowed + 'permissions' => Constants::PERMISSION_ALL ^ Constants::PERMISSION_SHARE, 'type' => FileInfo::TYPE_FOLDER, 'mimetype' => FileInfo::MIMETYPE_FOLDER, ]); } - public function get($path) { - return $this->root->get('/' . $this->user->getUID() . '/files/' . ltrim($path, '/')); - } - - /** - * @param int $id - * @return \OC\Files\Node\Node[] - */ - public function getById($id) { - return $this->root->getByIdInPath((int)$id, $this->getPath()); + public function getMountPoint() { + if ($this->folder !== null) { + return $this->folder->getMountPoint(); + } + $mountPoint = $this->mountManager->find('/' . $this->user->getUID()); + if (is_null($mountPoint)) { + throw new \Exception('No mountpoint for user folder'); + } + return $mountPoint; } } diff --git a/lib/private/Files/Node/Node.php b/lib/private/Files/Node/Node.php index 8a752ff281d..5dbdc4054bf 100644 --- a/lib/private/Files/Node/Node.php +++ b/lib/private/Files/Node/Node.php @@ -1,70 +1,45 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Arthur Schiwon <blizzz@arthur-schiwon.de> - * @author Bernhard Posselt <dev@bernhard-posselt.com> - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Joas Schilling <coding@schilljs.com> - * @author Julius Härtl <jus@bitgrid.net> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Robin Appelman <robin@icewind.nl> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @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\Filesystem; use OC\Files\Mount\MoveableMount; use OC\Files\Utils\PathHelper; +use OCP\EventDispatcher\GenericEvent; +use OCP\EventDispatcher\IEventDispatcher; use OCP\Files\FileInfo; use OCP\Files\InvalidPathException; +use OCP\Files\IRootFolder; +use OCP\Files\Node as INode; use OCP\Files\NotFoundException; use OCP\Files\NotPermittedException; use OCP\Lock\LockedException; -use Symfony\Component\EventDispatcher\GenericEvent; +use OCP\PreConditionNotMetException; -// FIXME: this class really should be abstract -class Node implements \OCP\Files\Node { +// FIXME: this class really should be abstract (+1) +class Node implements INode { /** * @var \OC\Files\View $view */ protected $view; - /** - * @var \OC\Files\Node\Root $root - */ - protected $root; + protected IRootFolder $root; /** - * @var string $path + * @var string $path Absolute path to the node (e.g. /admin/files/folder/file) */ protected $path; - /** - * @var \OCP\Files\FileInfo - */ - protected $fileInfo; + protected ?FileInfo $fileInfo; - /** - * @var Node|null - */ - protected $parent; + protected ?INode $parent; + + private bool $infoHasSubMountsIncluded; /** * @param \OC\Files\View $view @@ -72,19 +47,23 @@ class Node implements \OCP\Files\Node { * @param string $path * @param FileInfo $fileInfo */ - public function __construct($root, $view, $path, $fileInfo = null, ?Node $parent = null) { + public function __construct(IRootFolder $root, $view, $path, $fileInfo = null, ?INode $parent = null, bool $infoHasSubMountsIncluded = true) { + if (Filesystem::normalizePath($view->getRoot()) !== '/') { + throw new PreConditionNotMetException('The view passed to the node should not have any fake root set'); + } $this->view = $view; $this->root = $root; $this->path = $path; $this->fileInfo = $fileInfo; $this->parent = $parent; + $this->infoHasSubMountsIncluded = $infoHasSubMountsIncluded; } /** * Creates a Node of the same type that represents a non-existing path * * @param string $path path - * @return string non-existing node class + * @return Node non-existing node * @throws \Exception */ protected function createNonExistingNode($path) { @@ -98,17 +77,23 @@ class Node implements \OCP\Files\Node { * @throws InvalidPathException * @throws NotFoundException */ - public function getFileInfo() { + public function getFileInfo(bool $includeMountPoint = true) { if (!$this->fileInfo) { if (!Filesystem::isValidPath($this->path)) { throw new InvalidPathException(); } - $fileInfo = $this->view->getFileInfo($this->path); + $fileInfo = $this->view->getFileInfo($this->path, $includeMountPoint); + $this->infoHasSubMountsIncluded = $includeMountPoint; if ($fileInfo instanceof FileInfo) { $this->fileInfo = $fileInfo; } else { throw new NotFoundException(); } + } elseif ($includeMountPoint && !$this->infoHasSubMountsIncluded && $this instanceof Folder) { + if ($this->fileInfo instanceof \OC\Files\FileInfo) { + $this->view->addSubMounts($this->fileInfo); + } + $this->infoHasSubMountsIncluded = true; } return $this->fileInfo; } @@ -116,12 +101,22 @@ class Node implements \OCP\Files\Node { /** * @param string[] $hooks */ - protected function sendHooks($hooks, array $args = null) { + protected function sendHooks($hooks, ?array $args = null) { $args = !empty($args) ? $args : [$this]; - $dispatcher = \OC::$server->getEventDispatcher(); + /** @var IEventDispatcher $dispatcher */ + $dispatcher = \OC::$server->get(IEventDispatcher::class); foreach ($hooks as $hook) { - $this->root->emit('\OC\Files', $hook, $args); - $dispatcher->dispatch('\OCP\Files::' . $hook, new GenericEvent($args)); + if (method_exists($this->root, 'emit')) { + $this->root->emit('\OC\Files', $hook, $args); + } + + if (in_array($hook, ['preWrite', 'postWrite', 'preCreate', 'postCreate', 'preTouch', 'postTouch', 'preDelete', 'postDelete'], true)) { + $event = new GenericEvent($args[0]); + } else { + $event = new GenericEvent($args); + } + + $dispatcher->dispatch('\OCP\Files::' . $hook, $event); } } @@ -163,7 +158,7 @@ class Node implements \OCP\Files\Node { public function getStorage() { $storage = $this->getMountPoint()->getStorage(); if (!$storage) { - throw new \Exception("No storage for node"); + throw new \Exception('No storage for node'); } return $storage; } @@ -179,7 +174,7 @@ class Node implements \OCP\Files\Node { * @return string */ public function getInternalPath() { - return $this->getFileInfo()->getInternalPath(); + return $this->getFileInfo(false)->getInternalPath(); } /** @@ -188,7 +183,7 @@ class Node implements \OCP\Files\Node { * @throws NotFoundException */ public function getId() { - return $this->getFileInfo()->getId(); + return $this->getFileInfo(false)->getId() ?? -1; } /** @@ -209,11 +204,11 @@ class Node implements \OCP\Files\Node { /** * @param bool $includeMounts - * @return int + * @return int|float * @throws InvalidPathException * @throws NotFoundException */ - public function getSize($includeMounts = true) { + public function getSize($includeMounts = true): int|float { return $this->getFileInfo()->getSize($includeMounts); } @@ -232,7 +227,7 @@ class Node implements \OCP\Files\Node { * @throws NotFoundException */ public function getPermissions() { - return $this->getFileInfo()->getPermissions(); + return $this->getFileInfo(false)->getPermissions(); } /** @@ -241,7 +236,7 @@ class Node implements \OCP\Files\Node { * @throws NotFoundException */ public function isReadable() { - return $this->getFileInfo()->isReadable(); + return $this->getFileInfo(false)->isReadable(); } /** @@ -250,7 +245,7 @@ class Node implements \OCP\Files\Node { * @throws NotFoundException */ public function isUpdateable() { - return $this->getFileInfo()->isUpdateable(); + return $this->getFileInfo(false)->isUpdateable(); } /** @@ -259,7 +254,7 @@ class Node implements \OCP\Files\Node { * @throws NotFoundException */ public function isDeletable() { - return $this->getFileInfo()->isDeletable(); + return $this->getFileInfo(false)->isDeletable(); } /** @@ -268,7 +263,7 @@ class Node implements \OCP\Files\Node { * @throws NotFoundException */ public function isShareable() { - return $this->getFileInfo()->isShareable(); + return $this->getFileInfo(false)->isShareable(); } /** @@ -277,20 +272,35 @@ class Node implements \OCP\Files\Node { * @throws NotFoundException */ public function isCreatable() { - return $this->getFileInfo()->isCreatable(); + return $this->getFileInfo(false)->isCreatable(); } - /** - * @return Node - */ - public function getParent() { + public function getParent(): INode|IRootFolder { if ($this->parent === null) { $newPath = dirname($this->path); if ($newPath === '' || $newPath === '.' || $newPath === '/') { return $this->root; } - $this->parent = $this->root->get($newPath); + // Manually fetch the parent if the current node doesn't have a file info yet + try { + $fileInfo = $this->getFileInfo(); + } catch (NotFoundException) { + $this->parent = $this->root->get($newPath); + /** @var \OCP\Files\Folder $this->parent */ + return $this->parent; + } + + // gather the metadata we already know about our parent + $parentData = [ + 'path' => $newPath, + 'fileid' => $fileInfo->getParentId(), + ]; + + // and create lazy folder with it instead of always querying + $this->parent = new LazyFolder($this->root, function () use ($newPath) { + return $this->root->get($newPath); + }, $parentData); } return $this->parent; @@ -318,52 +328,46 @@ class Node implements \OCP\Files\Node { * @return bool */ public function isValidPath($path) { - if (!$path || $path[0] !== '/') { - $path = '/' . $path; - } - if (strstr($path, '/../') || strrchr($path, '/') === '/..') { - return false; - } - return true; + return Filesystem::isValidPath($path); } public function isMounted() { - return $this->getFileInfo()->isMounted(); + return $this->getFileInfo(false)->isMounted(); } public function isShared() { - return $this->getFileInfo()->isShared(); + return $this->getFileInfo(false)->isShared(); } public function getMimeType() { - return $this->getFileInfo()->getMimetype(); + return $this->getFileInfo(false)->getMimetype(); } public function getMimePart() { - return $this->getFileInfo()->getMimePart(); + return $this->getFileInfo(false)->getMimePart(); } public function getType() { - return $this->getFileInfo()->getType(); + return $this->getFileInfo(false)->getType(); } public function isEncrypted() { - return $this->getFileInfo()->isEncrypted(); + return $this->getFileInfo(false)->isEncrypted(); } public function getMountPoint() { - return $this->getFileInfo()->getMountPoint(); + return $this->getFileInfo(false)->getMountPoint(); } public function getOwner() { - return $this->getFileInfo()->getOwner(); + return $this->getFileInfo(false)->getOwner(); } public function getChecksum() { } public function getExtension(): string { - return $this->getFileInfo()->getExtension(); + return $this->getFileInfo(false)->getExtension(); } /** @@ -392,7 +396,7 @@ class Node implements \OCP\Files\Node { /** * @param string $targetPath - * @return \OC\Files\Node\Node + * @return INode * @throws InvalidPathException * @throws NotFoundException * @throws NotPermittedException if copy not allowed or failed @@ -418,7 +422,7 @@ class Node implements \OCP\Files\Node { /** * @param string $targetPath - * @return \OC\Files\Node\Node + * @return INode * @throws InvalidPathException * @throws NotFoundException * @throws NotPermittedException if move not allowed or failed @@ -428,11 +432,14 @@ class Node implements \OCP\Files\Node { $targetPath = $this->normalizePath($targetPath); $parent = $this->root->get(dirname($targetPath)); if ( - $parent instanceof Folder and - $this->isValidPath($targetPath) and - ( - $parent->isCreatable() || - ($parent->getInternalPath() === '' && $parent->getMountPoint() instanceof MoveableMount) + ($parent instanceof Folder) + && $this->isValidPath($targetPath) + && ( + $parent->isCreatable() + || ( + $parent->getInternalPath() === '' + && ($parent->getMountPoint() instanceof MoveableMount) + ) ) ) { $nonExisting = $this->createNonExistingNode($targetPath); @@ -467,4 +474,16 @@ class Node implements \OCP\Files\Node { public function getUploadTime(): int { return $this->getFileInfo()->getUploadTime(); } + + public function getParentId(): int { + return $this->fileInfo->getParentId(); + } + + /** + * @inheritDoc + * @return array<string, int|string|bool|float|string[]|int[]> + */ + public function getMetadata(): array { + return $this->fileInfo->getMetadata(); + } } diff --git a/lib/private/Files/Node/NonExistingFile.php b/lib/private/Files/Node/NonExistingFile.php index e1d706006ba..66ec2e6c040 100644 --- a/lib/private/Files/Node/NonExistingFile.php +++ b/lib/private/Files/Node/NonExistingFile.php @@ -1,24 +1,9 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Morris Jobke <hey@morrisjobke.de> - * @author Robin Appelman <robin@icewind.nl> - * - * @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; @@ -37,7 +22,7 @@ class NonExistingFile extends File { throw new NotFoundException(); } - public function copy($newPath) { + public function copy($targetPath) { throw new NotFoundException(); } @@ -53,6 +38,14 @@ class NonExistingFile extends File { } } + public function getInternalPath() { + if ($this->fileInfo) { + return parent::getInternalPath(); + } else { + return $this->getParent()->getMountPoint()->getInternalPath($this->getPath()); + } + } + public function stat() { throw new NotFoundException(); } @@ -65,7 +58,7 @@ class NonExistingFile extends File { } } - public function getSize($includeMounts = true) { + public function getSize($includeMounts = true): int|float { if ($this->fileInfo) { return parent::getSize($includeMounts); } else { diff --git a/lib/private/Files/Node/NonExistingFolder.php b/lib/private/Files/Node/NonExistingFolder.php index d99446e8ff8..4489fdaf010 100644 --- a/lib/private/Files/Node/NonExistingFolder.php +++ b/lib/private/Files/Node/NonExistingFolder.php @@ -1,25 +1,9 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Morris Jobke <hey@morrisjobke.de> - * @author Robin Appelman <robin@icewind.nl> - * @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; @@ -38,7 +22,7 @@ class NonExistingFolder extends Folder { throw new NotFoundException(); } - public function copy($newPath) { + public function copy($targetPath) { throw new NotFoundException(); } @@ -54,6 +38,14 @@ class NonExistingFolder extends Folder { } } + public function getInternalPath() { + if ($this->fileInfo) { + return parent::getInternalPath(); + } else { + return $this->getParent()->getMountPoint()->getInternalPath($this->getPath()); + } + } + public function stat() { throw new NotFoundException(); } @@ -66,7 +58,7 @@ class NonExistingFolder extends Folder { } } - public function getSize($includeMounts = true) { + public function getSize($includeMounts = true): int|float { if ($this->fileInfo) { return parent::getSize($includeMounts); } else { @@ -142,11 +134,11 @@ class NonExistingFolder extends Folder { throw new NotFoundException(); } - public function search($pattern) { + public function search($query) { throw new NotFoundException(); } - public function searchByMime($mime) { + public function searchByMime($mimetype) { throw new NotFoundException(); } @@ -154,10 +146,18 @@ class NonExistingFolder extends Folder { throw new NotFoundException(); } + public function searchBySystemTag(string $tagName, string $userId, int $limit = 0, int $offset = 0): array { + throw new NotFoundException(); + } + public function getById($id) { throw new NotFoundException(); } + public function getFirstNodeById(int $id): ?\OCP\Files\Node { + throw new NotFoundException(); + } + public function getFreeSpace() { throw new NotFoundException(); } diff --git a/lib/private/Files/Node/Root.php b/lib/private/Files/Node/Root.php index ca930c1002c..76afca9dee8 100644 --- a/lib/private/Files/Node/Root.php +++ b/lib/private/Files/Node/Root.php @@ -1,38 +1,12 @@ <?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 OCP\Cache\CappedMemoryCache; use OC\Files\FileInfo; use OC\Files\Mount\Manager; use OC\Files\Mount\MountPoint; @@ -40,15 +14,21 @@ 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\ICache; +use OCP\ICacheFactory; use OCP\IUser; use OCP\IUserManager; +use OCP\Server; use Psr\Log\LoggerInterface; /** @@ -79,6 +59,7 @@ class Root extends Folder implements IRootFolder { private LoggerInterface $logger; private IUserManager $userManager; private IEventDispatcher $eventDispatcher; + private ICache $pathByIdCache; /** * @param Manager $manager @@ -92,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; @@ -105,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'); } /** @@ -130,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); } @@ -153,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); } @@ -165,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); } @@ -192,19 +171,13 @@ 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)) { $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); } @@ -290,9 +263,9 @@ class Root extends Folder implements IRootFolder { /** * @param bool $includeMounts - * @return int + * @return int|float */ - public function getSize($includeMounts = true) { + public function getSize($includeMounts = true): int|float { return 0; } @@ -339,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(); } @@ -386,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)) { @@ -395,7 +367,7 @@ class Root extends Folder implements IRootFolder { $folder = $this->newFolder('/' . $userId . '/files'); } } else { - $folder = new LazyUserFolder($this, $userObject); + $folder = new LazyUserFolder($this, $userObject, $this->mountManager); } $this->userFolderCache->set($userId, $folder); @@ -408,13 +380,42 @@ 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[] */ public function getByIdInPath(int $id, string $path): array { $mountCache = $this->getUserMountCache(); - if (strpos($path, '/', 1) > 0) { + if ($path !== '' && strpos($path, '/', 1) > 0) { [, $user] = explode('/', $path); } else { $user = null; @@ -457,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 []; @@ -475,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); @@ -491,4 +506,29 @@ class Root extends Folder implements IRootFolder { }); 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); + } + } } |