diff options
Diffstat (limited to 'lib/private/Files/Filesystem.php')
-rw-r--r-- | lib/private/Files/Filesystem.php | 147 |
1 files changed, 45 insertions, 102 deletions
diff --git a/lib/private/Files/Filesystem.php b/lib/private/Files/Filesystem.php index 64a6fc57b27..8fe56cf060c 100644 --- a/lib/private/Files/Filesystem.php +++ b/lib/private/Files/Filesystem.php @@ -1,73 +1,37 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Arthur Schiwon <blizzz@arthur-schiwon.de> - * @author Bart Visscher <bartv@thisnet.nl> - * @author Christopher Schäpers <kondou@ts.unde.re> - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Florin Peter <github@florin-peter.de> - * @author Joas Schilling <coding@schilljs.com> - * @author Jörn Friedrich Dreyer <jfd@butonic.de> - * @author korelstar <korelstar@users.noreply.github.com> - * @author Lukas Reschke <lukas@statuscode.ch> - * @author Michael Gapczynski <GapczynskiM@gmail.com> - * @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 Sam Tuke <mail@samtuke.com> - * @author Stephan Peijnik <speijnik@anexia-it.com> - * @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; -use OCP\Cache\CappedMemoryCache; use OC\Files\Mount\MountPoint; +use OC\Files\Storage\StorageFactory; use OC\User\NoUserException; +use OCP\Cache\CappedMemoryCache; use OCP\EventDispatcher\IEventDispatcher; use OCP\Files\Events\Node\FilesystemTornDownEvent; +use OCP\Files\Mount\IMountManager; use OCP\Files\NotFoundException; use OCP\Files\Storage\IStorageFactory; use OCP\IUser; use OCP\IUserManager; use OCP\IUserSession; +use Psr\Log\LoggerInterface; class Filesystem { - /** - * @var Mount\Manager $mounts - */ - private static $mounts; - - public static $loaded = false; - /** - * @var \OC\Files\View $defaultInstance - */ - private static $defaultInstance; + private static ?Mount\Manager $mounts = null; - private static $usersSetup = []; + public static bool $loaded = false; - private static $normalizedPathCache = null; + private static ?View $defaultInstance = null; - private static $listeningForProviders = false; + private static ?CappedMemoryCache $normalizedPathCache = null; /** @var string[]|null */ - private static $blacklist = null; + private static ?array $blacklist = null; /** * classname which used for hooks handling @@ -186,22 +150,18 @@ class Filesystem { public const signal_param_mount_type = 'mounttype'; public const signal_param_users = 'users'; - /** - * @var \OC\Files\Storage\StorageFactory $loader - */ - private static $loader; + private static ?\OC\Files\Storage\StorageFactory $loader = null; - /** @var bool */ - private static $logWarningWhenAddingStorageWrapper = true; + private static bool $logWarningWhenAddingStorageWrapper = true; /** * @param bool $shouldLog * @return bool previous value * @internal */ - public static function logWarningWhenAddingStorageWrapper($shouldLog) { + public static function logWarningWhenAddingStorageWrapper(bool $shouldLog): bool { $previousValue = self::$logWarningWhenAddingStorageWrapper; - self::$logWarningWhenAddingStorageWrapper = (bool) $shouldLog; + self::$logWarningWhenAddingStorageWrapper = $shouldLog; return $previousValue; } @@ -212,14 +172,16 @@ class Filesystem { */ public static function addStorageWrapper($wrapperName, $wrapper, $priority = 50) { if (self::$logWarningWhenAddingStorageWrapper) { - \OC::$server->getLogger()->warning("Storage wrapper '{wrapper}' was not registered via the 'OC_Filesystem - preSetup' hook which could cause potential problems.", [ + \OCP\Server::get(LoggerInterface::class)->warning("Storage wrapper '{wrapper}' was not registered via the 'OC_Filesystem - preSetup' hook which could cause potential problems.", [ 'wrapper' => $wrapperName, 'app' => 'filesystem', ]); } $mounts = self::getMountManager()->getAll(); - if (!self::getLoader()->addStorageWrapper($wrapperName, $wrapper, $priority, $mounts)) { + /** @var StorageFactory $loader */ + $loader = self::getLoader(); + if (!$loader->addStorageWrapper($wrapperName, $wrapper, $priority, $mounts)) { // do not re-wrap if storage with this name already existed return; } @@ -232,18 +194,17 @@ class Filesystem { */ public static function getLoader() { if (!self::$loader) { - self::$loader = \OC::$server->query(IStorageFactory::class); + self::$loader = \OC::$server->get(IStorageFactory::class); } return self::$loader; } /** * Returns the mount manager - * - * @return \OC\Files\Mount\Manager */ - public static function getMountManager($user = '') { + public static function getMountManager(): Mount\Manager { self::initMountManager(); + assert(self::$mounts !== null); return self::$mounts; } @@ -313,14 +274,14 @@ class Filesystem { * resolve a path to a storage and internal path * * @param string $path - * @return array an array consisting of the storage and the internal path + * @return array{?\OCP\Files\Storage\IStorage, string} an array consisting of the storage and the internal path */ - public static function resolvePath($path) { + public static function resolvePath($path): array { $mount = self::getMountManager()->find($path); return [$mount->getStorage(), rtrim($mount->getInternalPath($path), '/')]; } - public static function init($user, $root) { + public static function init(string|IUser|null $user, string $root): bool { if (self::$defaultInstance) { return false; } @@ -332,7 +293,7 @@ class Filesystem { return true; } - public static function initInternal($root) { + public static function initInternal(string $root): bool { if (self::$defaultInstance) { return false; } @@ -342,32 +303,28 @@ class Filesystem { $eventDispatcher = \OC::$server->get(IEventDispatcher::class); $eventDispatcher->addListener(FilesystemTornDownEvent::class, function () { self::$defaultInstance = null; - self::$usersSetup = []; self::$loaded = false; }); - if (!self::$mounts) { - self::$mounts = \OC::$server->getMountManager(); - } + self::initMountManager(); self::$loaded = true; return true; } - public static function initMountManager() { + public static function initMountManager(): void { if (!self::$mounts) { - self::$mounts = \OC::$server->getMountManager(); + self::$mounts = \OC::$server->get(IMountManager::class); } } /** * Initialize system and personal mount points for a user * - * @param string|IUser|null $user * @throws \OC\User\NoUserException if the user is not available */ - public static function initMountPoints($user = '') { + public static function initMountPoints(string|IUser|null $user = ''): void { /** @var IUserManager $userManager */ $userManager = \OC::$server->get(IUserManager::class); @@ -382,11 +339,9 @@ class Filesystem { } /** - * get the default filesystem view - * - * @return View + * Get the default filesystem view */ - public static function getView() { + public static function getView(): ?View { if (!self::$defaultInstance) { /** @var IUserSession $session */ $session = \OC::$server->get(IUserSession::class); @@ -409,7 +364,7 @@ class Filesystem { /** * get the relative path of the root data directory for the current user * - * @return string + * @return ?string * * Returns path like /admin/files */ @@ -439,23 +394,12 @@ class Filesystem { * return the path to a local version of the file * we need this because we can't know if a file is stored local or not from * outside the filestorage and for some purposes a local file is needed - * - * @param string $path - * @return string */ - public static function getLocalFile($path) { + public static function getLocalFile(string $path): string|false { return self::$defaultInstance->getLocalFile($path); } /** - * @param string $path - * @return string - */ - public static function getLocalFolder($path) { - return self::$defaultInstance->getLocalFolder($path); - } - - /** * return path to file which reflects one visible in browser * * @param string $path @@ -481,7 +425,7 @@ class Filesystem { if (!$path || $path[0] !== '/') { $path = '/' . $path; } - if (strpos($path, '/../') !== false || strrchr($path, '/') === '/..') { + if (str_contains($path, '/../') || strrchr($path, '/') === '/..') { return false; } return true; @@ -584,7 +528,7 @@ class Filesystem { } /** - * @return string + * @return string|false */ public static function file_get_contents($path) { return self::$defaultInstance->file_get_contents($path); @@ -611,9 +555,10 @@ class Filesystem { } /** - * @return string + * @param string $path + * @throws \OCP\Files\InvalidPathException */ - public static function toTmpFile($path) { + public static function toTmpFile($path): string|false { return self::$defaultInstance->toTmpFile($path); } @@ -671,6 +616,7 @@ class Filesystem { * @param bool $stripTrailingSlash whether to strip the trailing slash * @param bool $isAbsolutePath whether the given path is absolute * @param bool $keepUnicode true to disable unicode normalization + * @psalm-taint-escape file * @return string */ public static function normalizePath($path, $stripTrailingSlash = true, $isAbsolutePath = false, $keepUnicode = false) { @@ -730,8 +676,8 @@ class Filesystem { * get the filesystem info * * @param string $path - * @param boolean $includeMountPoints whether to add mountpoint sizes, - * defaults to true + * @param bool|string $includeMountPoints whether to add mountpoint sizes, + * defaults to true * @return \OC\Files\FileInfo|false False if file does not exist */ public static function getFileInfo($path, $includeMountPoints = true) { @@ -787,11 +733,8 @@ class Filesystem { /** * get the ETag for a file or folder - * - * @param string $path - * @return string */ - public static function getETag($path) { + public static function getETag(string $path): string|false { return self::$defaultInstance->getETag($path); } } |