diff options
Diffstat (limited to 'lib/private/Files/Node/Folder.php')
-rw-r--r-- | lib/private/Files/Node/Folder.php | 196 |
1 files changed, 95 insertions, 101 deletions
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, + ); + } } |