diff options
Diffstat (limited to 'lib/private/Files/Storage')
21 files changed, 1031 insertions, 2891 deletions
diff --git a/lib/private/Files/Storage/Common.php b/lib/private/Files/Storage/Common.php index 3d5a2f098b2..2dc359169d7 100644 --- a/lib/private/Files/Storage/Common.php +++ b/lib/private/Files/Storage/Common.php @@ -1,68 +1,43 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Arthur Schiwon <blizzz@arthur-schiwon.de> - * @author Bart Visscher <bartv@thisnet.nl> - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Greta Doci <gretadoci@gmail.com> - * @author hkjolhede <hkjolhede@gmail.com> - * @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 Martin Mattel <martin.mattel@diemattels.at> - * @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 Roland Tapken <roland@bitarbeiter.net> - * @author Sam Tuke <mail@samtuke.com> - * @author scambra <sergio@entrecables.com> - * @author Stefan Weil <sw@weilnetz.de> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * @author Vincent Petry <vincent@nextcloud.com> - * @author Vinicius Cubas Brand <vinicius@eita.org.br> - * - * @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\Storage; use OC\Files\Cache\Cache; +use OC\Files\Cache\CacheDependencies; use OC\Files\Cache\Propagator; use OC\Files\Cache\Scanner; use OC\Files\Cache\Updater; use OC\Files\Cache\Watcher; +use OC\Files\FilenameValidator; use OC\Files\Filesystem; +use OC\Files\ObjectStore\ObjectStoreStorage; +use OC\Files\Storage\Wrapper\Encryption; use OC\Files\Storage\Wrapper\Jail; use OC\Files\Storage\Wrapper\Wrapper; -use OCP\Files\EmptyFileNameException; -use OCP\Files\FileNameTooLongException; +use OCP\Files; +use OCP\Files\Cache\ICache; +use OCP\Files\Cache\IPropagator; +use OCP\Files\Cache\IScanner; +use OCP\Files\Cache\IUpdater; +use OCP\Files\Cache\IWatcher; use OCP\Files\ForbiddenException; use OCP\Files\GenericFileException; -use OCP\Files\InvalidCharacterInPathException; -use OCP\Files\InvalidDirectoryException; +use OCP\Files\IFilenameValidator; use OCP\Files\InvalidPathException; -use OCP\Files\ReservedWordException; +use OCP\Files\Storage\IConstructableStorage; use OCP\Files\Storage\ILockingStorage; use OCP\Files\Storage\IStorage; use OCP\Files\Storage\IWriteStreamStorage; +use OCP\Files\StorageNotAvailableException; +use OCP\IConfig; use OCP\Lock\ILockingProvider; use OCP\Lock\LockedException; +use OCP\Server; use Psr\Log\LoggerInterface; /** @@ -76,85 +51,75 @@ use Psr\Log\LoggerInterface; * Some \OC\Files\Storage\Common methods call functions which are first defined * in classes which extend it, e.g. $this->stat() . */ -abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage { +abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage, IConstructableStorage { use LocalTempFileTrait; - protected $cache; - protected $scanner; - protected $watcher; - protected $propagator; + protected ?Cache $cache = null; + protected ?Scanner $scanner = null; + protected ?Watcher $watcher = null; + protected ?Propagator $propagator = null; protected $storageCache; - protected $updater; + protected ?Updater $updater = null; - protected $mountOptions = []; + protected array $mountOptions = []; protected $owner = null; - /** @var ?bool */ - private $shouldLogLocks = null; - /** @var ?LoggerInterface */ - private $logger; + private ?bool $shouldLogLocks = null; + private ?LoggerInterface $logger = null; + private ?IFilenameValidator $filenameValidator = null; - public function __construct($parameters) { + public function __construct(array $parameters) { } - /** - * Remove a file or folder - * - * @param string $path - * @return bool - */ - protected function remove($path) { - if ($this->is_dir($path)) { - return $this->rmdir($path); - } elseif ($this->is_file($path)) { - return $this->unlink($path); - } else { - return false; + protected function remove(string $path): bool { + if ($this->file_exists($path)) { + if ($this->is_dir($path)) { + return $this->rmdir($path); + } elseif ($this->is_file($path)) { + return $this->unlink($path); + } } + return false; } - public function is_dir($path) { + public function is_dir(string $path): bool { return $this->filetype($path) === 'dir'; } - public function is_file($path) { + public function is_file(string $path): bool { return $this->filetype($path) === 'file'; } - public function filesize($path): false|int|float { + public function filesize(string $path): int|float|false { if ($this->is_dir($path)) { return 0; //by definition } else { $stat = $this->stat($path); - if (isset($stat['size'])) { - return $stat['size']; - } else { - return 0; - } + return isset($stat['size']) ? $stat['size'] : 0; } } - public function isReadable($path) { + public function isReadable(string $path): bool { // at least check whether it exists // subclasses might want to implement this more thoroughly return $this->file_exists($path); } - public function isUpdatable($path) { + public function isUpdatable(string $path): bool { // at least check whether it exists // subclasses might want to implement this more thoroughly // a non-existing file/folder isn't updatable return $this->file_exists($path); } - public function isCreatable($path) { + public function isCreatable(string $path): bool { if ($this->is_dir($path) && $this->isUpdatable($path)) { return true; } return false; } - public function isDeletable($path) { + public function isDeletable(string $path): bool { if ($path === '' || $path === '/') { return $this->isUpdatable($path); } @@ -162,11 +127,11 @@ abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage { return $this->isUpdatable($parent) && $this->isUpdatable($path); } - public function isSharable($path) { + public function isSharable(string $path): bool { return $this->isReadable($path); } - public function getPermissions($path) { + public function getPermissions(string $path): int { $permissions = 0; if ($this->isCreatable($path)) { $permissions |= \OCP\Constants::PERMISSION_CREATE; @@ -186,7 +151,7 @@ abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage { return $permissions; } - public function filemtime($path) { + public function filemtime(string $path): int|false { $stat = $this->stat($path); if (isset($stat['mtime']) && $stat['mtime'] > 0) { return $stat['mtime']; @@ -195,8 +160,8 @@ abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage { } } - public function file_get_contents($path) { - $handle = $this->fopen($path, "r"); + public function file_get_contents(string $path): string|false { + $handle = $this->fopen($path, 'r'); if (!$handle) { return false; } @@ -205,8 +170,8 @@ abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage { return $data; } - public function file_put_contents($path, $data) { - $handle = $this->fopen($path, "w"); + public function file_put_contents(string $path, mixed $data): int|float|false { + $handle = $this->fopen($path, 'w'); if (!$handle) { return false; } @@ -216,19 +181,19 @@ abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage { return $count; } - public function rename($source, $target) { + public function rename(string $source, string $target): bool { $this->remove($target); $this->removeCachedFile($source); return $this->copy($source, $target) and $this->remove($source); } - public function copy($source, $target) { + public function copy(string $source, string $target): bool { if ($this->is_dir($source)) { $this->remove($target); $dir = $this->opendir($source); $this->mkdir($target); - while ($file = readdir($dir)) { + while (($file = readdir($dir)) !== false) { if (!Filesystem::isIgnoredDir($file)) { if (!$this->copy($source . '/' . $file, $target . '/' . $file)) { closedir($dir); @@ -241,16 +206,16 @@ abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage { } else { $sourceStream = $this->fopen($source, 'r'); $targetStream = $this->fopen($target, 'w'); - [, $result] = \OC_Helper::streamCopy($sourceStream, $targetStream); + [, $result] = Files::streamCopy($sourceStream, $targetStream, true); if (!$result) { - \OCP\Server::get(LoggerInterface::class)->warning("Failed to write data while copying $source to $target"); + Server::get(LoggerInterface::class)->warning("Failed to write data while copying $source to $target"); } $this->removeCachedFile($target); return $result; } } - public function getMimeType($path) { + public function getMimeType(string $path): string|false { if ($this->is_dir($path)) { return 'httpd/unix-directory'; } elseif ($this->file_exists($path)) { @@ -260,31 +225,26 @@ abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage { } } - public function hash($type, $path, $raw = false) { + public function hash(string $type, string $path, bool $raw = false): string|false { $fh = $this->fopen($path, 'rb'); + if (!$fh) { + return false; + } $ctx = hash_init($type); hash_update_stream($ctx, $fh); fclose($fh); return hash_final($ctx, $raw); } - public function search($query) { - return $this->searchInDir($query); - } - - public function getLocalFile($path) { + public function getLocalFile(string $path): string|false { return $this->getCachedFile($path); } - /** - * @param string $path - * @param string $target - */ - private function addLocalFolder($path, $target) { + private function addLocalFolder(string $path, string $target): void { $dh = $this->opendir($path); if (is_resource($dh)) { while (($file = readdir($dh)) !== false) { - if (!\OC\Files\Filesystem::isIgnoredDir($file)) { + if (!Filesystem::isIgnoredDir($file)) { if ($this->is_dir($path . '/' . $file)) { mkdir($target . '/' . $file); $this->addLocalFolder($path . '/' . $file, $target . '/' . $file); @@ -297,17 +257,12 @@ abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage { } } - /** - * @param string $query - * @param string $dir - * @return array - */ - protected function searchInDir($query, $dir = '') { + protected function searchInDir(string $query, string $dir = ''): array { $files = []; $dh = $this->opendir($dir); if (is_resource($dh)) { while (($item = readdir($dh)) !== false) { - if (\OC\Files\Filesystem::isIgnoredDir($item)) { + if (Filesystem::isIgnoredDir($item)) { continue; } if (strstr(strtolower($item), strtolower($query)) !== false) { @@ -323,97 +278,98 @@ abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage { } /** + * @inheritDoc * Check if a file or folder has been updated since $time * * The method is only used to check if the cache needs to be updated. Storage backends that don't support checking * the mtime should always return false here. As a result storage implementations that always return false expect * exclusive access to the backend and will not pick up files that have been added in a way that circumvents * Nextcloud filesystem. - * - * @param string $path - * @param int $time - * @return bool */ - public function hasUpdated($path, $time) { + public function hasUpdated(string $path, int $time): bool { return $this->filemtime($path) > $time; } - public function getCache($path = '', $storage = null) { + protected function getCacheDependencies(): CacheDependencies { + static $dependencies = null; + if (!$dependencies) { + $dependencies = Server::get(CacheDependencies::class); + } + return $dependencies; + } + + public function getCache(string $path = '', ?IStorage $storage = null): ICache { if (!$storage) { $storage = $this; } + /** @var self $storage */ if (!isset($storage->cache)) { - $storage->cache = new Cache($storage); + $storage->cache = new Cache($storage, $this->getCacheDependencies()); } return $storage->cache; } - public function getScanner($path = '', $storage = null) { + public function getScanner(string $path = '', ?IStorage $storage = null): IScanner { if (!$storage) { $storage = $this; } + if (!$storage->instanceOfStorage(self::class)) { + throw new \InvalidArgumentException('Storage is not of the correct class'); + } if (!isset($storage->scanner)) { $storage->scanner = new Scanner($storage); } return $storage->scanner; } - public function getWatcher($path = '', $storage = null) { + public function getWatcher(string $path = '', ?IStorage $storage = null): IWatcher { if (!$storage) { $storage = $this; } if (!isset($this->watcher)) { $this->watcher = new Watcher($storage); - $globalPolicy = \OC::$server->getConfig()->getSystemValue('filesystem_check_changes', Watcher::CHECK_NEVER); + $globalPolicy = Server::get(IConfig::class)->getSystemValueInt('filesystem_check_changes', Watcher::CHECK_NEVER); $this->watcher->setPolicy((int)$this->getMountOption('filesystem_check_changes', $globalPolicy)); } return $this->watcher; } - /** - * get a propagator instance for the cache - * - * @param \OC\Files\Storage\Storage (optional) the storage to pass to the watcher - * @return \OC\Files\Cache\Propagator - */ - public function getPropagator($storage = null) { + public function getPropagator(?IStorage $storage = null): IPropagator { if (!$storage) { $storage = $this; } + if (!$storage->instanceOfStorage(self::class)) { + throw new \InvalidArgumentException('Storage is not of the correct class'); + } + /** @var self $storage */ if (!isset($storage->propagator)) { - $config = \OC::$server->getSystemConfig(); - $storage->propagator = new Propagator($storage, \OC::$server->getDatabaseConnection(), ['appdata_' . $config->getValue('instanceid')]); + $config = Server::get(IConfig::class); + $storage->propagator = new Propagator($storage, \OC::$server->getDatabaseConnection(), ['appdata_' . $config->getSystemValueString('instanceid')]); } return $storage->propagator; } - public function getUpdater($storage = null) { + public function getUpdater(?IStorage $storage = null): IUpdater { if (!$storage) { $storage = $this; } + if (!$storage->instanceOfStorage(self::class)) { + throw new \InvalidArgumentException('Storage is not of the correct class'); + } + /** @var self $storage */ if (!isset($storage->updater)) { $storage->updater = new Updater($storage); } return $storage->updater; } - public function getStorageCache($storage = null) { - if (!$storage) { - $storage = $this; - } - if (!isset($this->storageCache)) { - $this->storageCache = new \OC\Files\Cache\Storage($storage); - } - return $this->storageCache; + public function getStorageCache(?IStorage $storage = null): \OC\Files\Cache\Storage { + /** @var Cache $cache */ + $cache = $this->getCache(storage: $storage); + return $cache->getStorageCache(); } - /** - * get the owner of a path - * - * @param string $path The path to get the owner - * @return string|false uid or false - */ - public function getOwner($path) { + public function getOwner(string $path): string|false { if ($this->owner === null) { $this->owner = \OC_User::getUser(); } @@ -421,13 +377,7 @@ abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage { return $this->owner; } - /** - * get the ETag for a file or folder - * - * @param string $path - * @return string - */ - public function getETag($path) { + public function getETag(string $path): string|false { return uniqid(); } @@ -438,8 +388,8 @@ abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage { * @param string $path The path to clean * @return string cleaned path */ - public function cleanPath($path) { - if (strlen($path) == 0 or $path[0] != '/') { + public function cleanPath(string $path): string { + if (strlen($path) == 0 || $path[0] != '/') { $path = '/' . $path; } @@ -457,39 +407,28 @@ abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage { /** * Test a storage for availability - * - * @return bool */ - public function test() { + public function test(): bool { try { if ($this->stat('')) { return true; } - \OC::$server->get(LoggerInterface::class)->info("External storage not available: stat() failed"); + Server::get(LoggerInterface::class)->info('External storage not available: stat() failed'); return false; } catch (\Exception $e) { - \OC::$server->get(LoggerInterface::class)->warning( - "External storage not available: " . $e->getMessage(), + Server::get(LoggerInterface::class)->warning( + 'External storage not available: ' . $e->getMessage(), ['exception' => $e] ); return false; } } - /** - * get the free space in the storage - * - * @param string $path - * @return int|float|false - */ - public function free_space($path) { + public function free_space(string $path): int|float|false { return \OCP\Files\FileInfo::SPACE_UNKNOWN; } - /** - * {@inheritdoc} - */ - public function isLocal() { + public function isLocal(): bool { // the common implementation returns a temporary file by // default, which is not local return false; @@ -497,11 +436,8 @@ abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage { /** * Check if the storage is an instance of $class or is a wrapper for a storage that is an instance of $class - * - * @param string $class - * @return bool */ - public function instanceOfStorage($class) { + public function instanceOfStorage(string $class): bool { if (ltrim($class, '\\') === 'OC\Files\Storage\Shared') { // FIXME Temporary fix to keep existing checks working $class = '\OCA\Files_Sharing\SharedStorage'; @@ -513,105 +449,49 @@ abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage { * A custom storage implementation can return an url for direct download of a give file. * * For now the returned array can hold the parameter url - in future more attributes might follow. - * - * @param string $path - * @return array|false */ - public function getDirectDownload($path) { + public function getDirectDownload(string $path): array|false { return []; } - /** - * @inheritdoc - * @throws InvalidPathException - */ - public function verifyPath($path, $fileName) { - // verify empty and dot files - $trimmed = trim($fileName); - if ($trimmed === '') { - throw new EmptyFileNameException(); - } - - if (\OC\Files\Filesystem::isIgnoredDir($trimmed)) { - throw new InvalidDirectoryException(); - } + public function verifyPath(string $path, string $fileName): void { + $this->getFilenameValidator() + ->validateFilename($fileName); - if (!\OC::$server->getDatabaseConnection()->supports4ByteText()) { - // verify database - e.g. mysql only 3-byte chars - if (preg_match('%(?: - \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 - | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 - | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 -)%xs', $fileName)) { - throw new InvalidCharacterInPathException(); + // verify also the path is valid + if ($path && $path !== '/' && $path !== '.') { + try { + $this->verifyPath(dirname($path), basename($path)); + } catch (InvalidPathException $e) { + // Ignore invalid file type exceptions on directories + if ($e->getCode() !== FilenameValidator::INVALID_FILE_TYPE) { + $l = \OCP\Util::getL10N('lib'); + throw new InvalidPathException($l->t('Invalid parent path'), previous: $e); + } } } - - // 255 characters is the limit on common file systems (ext/xfs) - // oc_filecache has a 250 char length limit for the filename - if (isset($fileName[250])) { - throw new FileNameTooLongException(); - } - - // NOTE: $path will remain unverified for now - $this->verifyPosixPath($fileName); - } - - /** - * @param string $fileName - * @throws InvalidPathException - */ - protected function verifyPosixPath($fileName) { - $this->scanForInvalidCharacters($fileName, "\\/"); - $fileName = trim($fileName); - $reservedNames = ['*']; - if (in_array($fileName, $reservedNames)) { - throw new ReservedWordException(); - } } /** - * @param string $fileName - * @param string $invalidChars - * @throws InvalidPathException + * Get the filename validator + * (cached for performance) */ - private function scanForInvalidCharacters($fileName, $invalidChars) { - foreach (str_split($invalidChars) as $char) { - if (str_contains($fileName, $char)) { - throw new InvalidCharacterInPathException(); - } - } - - $sanitizedFileName = filter_var($fileName, FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW); - if ($sanitizedFileName !== $fileName) { - throw new InvalidCharacterInPathException(); + protected function getFilenameValidator(): IFilenameValidator { + if ($this->filenameValidator === null) { + $this->filenameValidator = Server::get(IFilenameValidator::class); } + return $this->filenameValidator; } - /** - * @param array $options - */ - public function setMountOptions(array $options) { + public function setMountOptions(array $options): void { $this->mountOptions = $options; } - /** - * @param string $name - * @param mixed $default - * @return mixed - */ - public function getMountOption($name, $default = null) { + public function getMountOption(string $name, mixed $default = null): mixed { return $this->mountOptions[$name] ?? $default; } - /** - * @param IStorage $sourceStorage - * @param string $sourceInternalPath - * @param string $targetInternalPath - * @param bool $preserveMtime - * @return bool - */ - public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime = false) { + public function copyFromStorage(IStorage $sourceStorage, string $sourceInternalPath, string $targetInternalPath, bool $preserveMtime = false): bool { if ($sourceStorage === $this) { return $this->copy($sourceInternalPath, $targetInternalPath); } @@ -621,7 +501,7 @@ abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage { $result = $this->mkdir($targetInternalPath); if (is_resource($dh)) { $result = true; - while ($result and ($file = readdir($dh)) !== false) { + while ($result && ($file = readdir($dh)) !== false) { if (!Filesystem::isIgnoredDir($file)) { $result &= $this->copyFromStorage($sourceStorage, $sourceInternalPath . '/' . $file, $targetInternalPath . '/' . $file); } @@ -635,7 +515,7 @@ abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage { $this->writeStream($targetInternalPath, $source); $result = true; } catch (\Exception $e) { - \OC::$server->get(LoggerInterface::class)->warning('Failed to copy stream to storage', ['exception' => $e]); + Server::get(LoggerInterface::class)->warning('Failed to copy stream to storage', ['exception' => $e]); } } @@ -656,14 +536,11 @@ abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage { /** * Check if a storage is the same as the current one, including wrapped storages - * - * @param IStorage $storage - * @return bool */ private function isSameStorage(IStorage $storage): bool { while ($storage->instanceOfStorage(Wrapper::class)) { /** - * @var Wrapper $sourceStorage + * @var Wrapper $storage */ $storage = $storage->getWrapperStorage(); } @@ -671,14 +548,11 @@ abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage { return $storage === $this; } - /** - * @param IStorage $sourceStorage - * @param string $sourceInternalPath - * @param string $targetInternalPath - * @return bool - */ - public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) { - if ($this->isSameStorage($sourceStorage)) { + public function moveFromStorage(IStorage $sourceStorage, string $sourceInternalPath, string $targetInternalPath): bool { + if ( + !$sourceStorage->instanceOfStorage(Encryption::class) + && $this->isSameStorage($sourceStorage) + ) { // resolve any jailed paths while ($sourceStorage->instanceOfStorage(Jail::class)) { /** @@ -697,19 +571,27 @@ abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage { $result = $this->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath, true); if ($result) { - if ($sourceStorage->is_dir($sourceInternalPath)) { - $result = $sourceStorage->rmdir($sourceInternalPath); - } else { - $result = $sourceStorage->unlink($sourceInternalPath); + if ($sourceStorage->instanceOfStorage(ObjectStoreStorage::class)) { + /** @var ObjectStoreStorage $sourceStorage */ + $sourceStorage->setPreserveCacheOnDelete(true); + } + try { + if ($sourceStorage->is_dir($sourceInternalPath)) { + $result = $sourceStorage->rmdir($sourceInternalPath); + } else { + $result = $sourceStorage->unlink($sourceInternalPath); + } + } finally { + if ($sourceStorage->instanceOfStorage(ObjectStoreStorage::class)) { + /** @var ObjectStoreStorage $sourceStorage */ + $sourceStorage->setPreserveCacheOnDelete(false); + } } } return $result; } - /** - * @inheritdoc - */ - public function getMetaData($path) { + public function getMetaData(string $path): ?array { if (Filesystem::isFileBlacklisted($path)) { throw new ForbiddenException('Invalid path: ' . $path, false); } @@ -739,13 +621,7 @@ abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage { return $data; } - /** - * @param string $path - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE - * @param \OCP\Lock\ILockingProvider $provider - * @throws \OCP\Lock\LockedException - */ - public function acquireLock($path, $type, ILockingProvider $provider) { + public function acquireLock(string $path, int $type, ILockingProvider $provider): void { $logger = $this->getLockLogger(); if ($logger) { $typeString = ($type === ILockingProvider::LOCK_SHARED) ? 'shared' : 'exclusive'; @@ -772,13 +648,7 @@ abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage { } } - /** - * @param string $path - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE - * @param \OCP\Lock\ILockingProvider $provider - * @throws \OCP\Lock\LockedException - */ - public function releaseLock($path, $type, ILockingProvider $provider) { + public function releaseLock(string $path, int $type, ILockingProvider $provider): void { $logger = $this->getLockLogger(); if ($logger) { $typeString = ($type === ILockingProvider::LOCK_SHARED) ? 'shared' : 'exclusive'; @@ -805,13 +675,7 @@ abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage { } } - /** - * @param string $path - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE - * @param \OCP\Lock\ILockingProvider $provider - * @throws \OCP\Lock\LockedException - */ - public function changeLock($path, $type, ILockingProvider $provider) { + public function changeLock(string $path, int $type, ILockingProvider $provider): void { $logger = $this->getLockLogger(); if ($logger) { $typeString = ($type === ILockingProvider::LOCK_SHARED) ? 'shared' : 'exclusive'; @@ -840,8 +704,8 @@ abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage { private function getLockLogger(): ?LoggerInterface { if (is_null($this->shouldLogLocks)) { - $this->shouldLogLocks = \OC::$server->getConfig()->getSystemValueBool('filelocking.debug', false); - $this->logger = $this->shouldLogLocks ? \OC::$server->get(LoggerInterface::class) : null; + $this->shouldLogLocks = Server::get(IConfig::class)->getSystemValueBool('filelocking.debug', false); + $this->logger = $this->shouldLogLocks ? Server::get(LoggerInterface::class) : null; } return $this->logger; } @@ -849,41 +713,31 @@ abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage { /** * @return array [ available, last_checked ] */ - public function getAvailability() { + public function getAvailability(): array { return $this->getStorageCache()->getAvailability(); } - /** - * @param bool $isAvailable - */ - public function setAvailability($isAvailable) { + public function setAvailability(bool $isAvailable): void { $this->getStorageCache()->setAvailability($isAvailable); } - /** - * @return bool - */ - public function needsPartFile() { + public function setOwner(?string $user): void { + $this->owner = $user; + } + + public function needsPartFile(): bool { return true; } - /** - * fallback implementation - * - * @param string $path - * @param resource $stream - * @param int $size - * @return int - */ - public function writeStream(string $path, $stream, int $size = null): int { + public function writeStream(string $path, $stream, ?int $size = null): int { $target = $this->fopen($path, 'w'); if (!$target) { throw new GenericFileException("Failed to open $path for writing"); } try { - [$count, $result] = \OC_Helper::streamCopy($stream, $target); + [$count, $result] = Files::streamCopy($stream, $target, true); if (!$result) { - throw new GenericFileException("Failed to copy stream"); + throw new GenericFileException('Failed to copy stream'); } } finally { fclose($target); @@ -892,8 +746,13 @@ abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage { return $count; } - public function getDirectoryContent($directory): \Traversable { + public function getDirectoryContent(string $directory): \Traversable { $dh = $this->opendir($directory); + + if ($dh === false) { + throw new StorageNotAvailableException('Directory listing failed'); + } + if (is_resource($dh)) { $basePath = rtrim($directory, '/'); while (($file = readdir($dh)) !== false) { diff --git a/lib/private/Files/Storage/CommonTest.php b/lib/private/Files/Storage/CommonTest.php index 3800bba2b52..da796130899 100644 --- a/lib/private/Files/Storage/CommonTest.php +++ b/lib/private/Files/Storage/CommonTest.php @@ -1,30 +1,9 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Bart Visscher <bartv@thisnet.nl> - * @author Christopher Schäpers <kondou@ts.unde.re> - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Felix Moeller <mail@felixmoeller.de> - * @author Michael Gapczynski <GapczynskiM@gmail.com> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Robin Appelman <robin@icewind.nl> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * - * @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\Storage; @@ -35,47 +14,47 @@ class CommonTest extends \OC\Files\Storage\Common { */ private $storage; - public function __construct($params) { - $this->storage = new \OC\Files\Storage\Local($params); + public function __construct(array $parameters) { + $this->storage = new \OC\Files\Storage\Local($parameters); } - public function getId() { - return 'test::'.$this->storage->getId(); + public function getId(): string { + return 'test::' . $this->storage->getId(); } - public function mkdir($path) { + public function mkdir(string $path): bool { return $this->storage->mkdir($path); } - public function rmdir($path) { + public function rmdir(string $path): bool { return $this->storage->rmdir($path); } - public function opendir($path) { + public function opendir(string $path) { return $this->storage->opendir($path); } - public function stat($path) { + public function stat(string $path): array|false { return $this->storage->stat($path); } - public function filetype($path) { + public function filetype(string $path): string|false { return @$this->storage->filetype($path); } - public function isReadable($path) { + public function isReadable(string $path): bool { return $this->storage->isReadable($path); } - public function isUpdatable($path) { + public function isUpdatable(string $path): bool { return $this->storage->isUpdatable($path); } - public function file_exists($path) { + public function file_exists(string $path): bool { return $this->storage->file_exists($path); } - public function unlink($path) { + public function unlink(string $path): bool { return $this->storage->unlink($path); } - public function fopen($path, $mode) { + public function fopen(string $path, string $mode) { return $this->storage->fopen($path, $mode); } - public function free_space($path) { + public function free_space(string $path): int|float|false { return $this->storage->free_space($path); } - public function touch($path, $mtime = null) { + public function touch(string $path, ?int $mtime = null): bool { return $this->storage->touch($path, $mtime); } } diff --git a/lib/private/Files/Storage/DAV.php b/lib/private/Files/Storage/DAV.php index 35add2c606b..2d166b5438d 100644 --- a/lib/private/Files/Storage/DAV.php +++ b/lib/private/Files/Storage/DAV.php @@ -1,39 +1,9 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Arthur Schiwon <blizzz@arthur-schiwon.de> - * @author Bart Visscher <bartv@thisnet.nl> - * @author Björn Schießle <bjoern@schiessle.org> - * @author Carlos Cerrillo <ccerrillo@gmail.com> - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Daniel Kesselberg <mail@danielkesselberg.de> - * @author Joas Schilling <coding@schilljs.com> - * @author Jörn Friedrich Dreyer <jfd@butonic.de> - * @author Lukas Reschke <lukas@statuscode.ch> - * @author Michael Gapczynski <GapczynskiM@gmail.com> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Philipp Kapfer <philipp.kapfer@gmx.at> - * @author Robin Appelman <robin@icewind.nl> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * @author Tigran Mkrtchyan <tigran.mkrtchyan@desy.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\Storage; @@ -50,9 +20,11 @@ use OCP\Files\ForbiddenException; use OCP\Files\IMimeTypeDetector; use OCP\Files\StorageInvalidException; use OCP\Files\StorageNotAvailableException; +use OCP\Http\Client\IClient; use OCP\Http\Client\IClientService; use OCP\ICertificateManager; use OCP\IConfig; +use OCP\Server; use OCP\Util; use Psr\Http\Message\ResponseInterface; use Psr\Log\LoggerInterface; @@ -111,31 +83,31 @@ class DAV extends Common { ]; /** - * @param array $params + * @param array $parameters * @throws \Exception */ - public function __construct($params) { + public function __construct(array $parameters) { $this->statCache = new ArrayCache(); - $this->httpClientService = \OC::$server->getHTTPClientService(); - if (isset($params['host']) && isset($params['user']) && isset($params['password'])) { - $host = $params['host']; + $this->httpClientService = Server::get(IClientService::class); + if (isset($parameters['host']) && isset($parameters['user']) && isset($parameters['password'])) { + $host = $parameters['host']; //remove leading http[s], will be generated in createBaseUri() - if (str_starts_with($host, "https://")) { + if (str_starts_with($host, 'https://')) { $host = substr($host, 8); - } elseif (str_starts_with($host, "http://")) { + } elseif (str_starts_with($host, 'http://')) { $host = substr($host, 7); } $this->host = $host; - $this->user = $params['user']; - $this->password = $params['password']; - if (isset($params['authType'])) { - $this->authType = $params['authType']; + $this->user = $parameters['user']; + $this->password = $parameters['password']; + if (isset($parameters['authType'])) { + $this->authType = $parameters['authType']; } - if (isset($params['secure'])) { - if (is_string($params['secure'])) { - $this->secure = ($params['secure'] === 'true'); + if (isset($parameters['secure'])) { + if (is_string($parameters['secure'])) { + $this->secure = ($parameters['secure'] === 'true'); } else { - $this->secure = (bool)$params['secure']; + $this->secure = (bool)$parameters['secure']; } } else { $this->secure = false; @@ -144,20 +116,20 @@ class DAV extends Common { // inject mock for testing $this->certManager = \OC::$server->getCertificateManager(); } - $this->root = $params['root'] ?? '/'; + $this->root = rawurldecode($parameters['root'] ?? '/'); $this->root = '/' . ltrim($this->root, '/'); $this->root = rtrim($this->root, '/') . '/'; } else { throw new \Exception('Invalid webdav storage configuration'); } - $this->logger = \OC::$server->get(LoggerInterface::class); - $this->eventLogger = \OC::$server->get(IEventLogger::class); + $this->logger = Server::get(LoggerInterface::class); + $this->eventLogger = Server::get(IEventLogger::class); // This timeout value will be used for the download and upload of files - $this->timeout = \OC::$server->get(IConfig::class)->getSystemValueInt('davstorage.request_timeout', 30); + $this->timeout = Server::get(IConfig::class)->getSystemValueInt('davstorage.request_timeout', IClient::DEFAULT_REQUEST_TIMEOUT); $this->mimeTypeDetector = \OC::$server->getMimeTypeDetector(); } - protected function init() { + protected function init(): void { if ($this->ready) { return; } @@ -172,7 +144,7 @@ class DAV extends Common { $settings['authType'] = $this->authType; } - $proxy = \OC::$server->getConfig()->getSystemValueString('proxy', ''); + $proxy = Server::get(IConfig::class)->getSystemValueString('proxy', ''); if ($proxy !== '') { $settings['proxy'] = $proxy; } @@ -192,13 +164,13 @@ class DAV extends Common { $lastRequestStart = 0; $this->client->on('beforeRequest', function (RequestInterface $request) use (&$lastRequestStart) { - $this->logger->debug("sending dav " . $request->getMethod() . " request to external storage: " . $request->getAbsoluteUrl(), ['app' => 'dav']); + $this->logger->debug('sending dav ' . $request->getMethod() . ' request to external storage: ' . $request->getAbsoluteUrl(), ['app' => 'dav']); $lastRequestStart = microtime(true); - $this->eventLogger->start('fs:storage:dav:request', "Sending dav request to external storage"); + $this->eventLogger->start('fs:storage:dav:request', 'Sending dav request to external storage'); }); $this->client->on('afterRequest', function (RequestInterface $request) use (&$lastRequestStart) { $elapsed = microtime(true) - $lastRequestStart; - $this->logger->debug("dav " . $request->getMethod() . " request to external storage: " . $request->getAbsoluteUrl() . " took " . round($elapsed * 1000, 1) . "ms", ['app' => 'dav']); + $this->logger->debug('dav ' . $request->getMethod() . ' request to external storage: ' . $request->getAbsoluteUrl() . ' took ' . round($elapsed * 1000, 1) . 'ms', ['app' => 'dav']); $this->eventLogger->end('fs:storage:dav:request'); }); } @@ -206,27 +178,24 @@ class DAV extends Common { /** * Clear the stat cache */ - public function clearStatCache() { + public function clearStatCache(): void { $this->statCache->clear(); } - /** {@inheritdoc} */ - public function getId() { + public function getId(): string { return 'webdav::' . $this->user . '@' . $this->host . '/' . $this->root; } - /** {@inheritdoc} */ - public function createBaseUri() { + public function createBaseUri(): string { $baseUri = 'http'; if ($this->secure) { $baseUri .= 's'; } - $baseUri .= '://' . $this->host . $this->root; + $baseUri .= '://' . $this->host . $this->encodePath($this->root); return $baseUri; } - /** {@inheritdoc} */ - public function mkdir($path) { + public function mkdir(string $path): bool { $this->init(); $path = $this->cleanPath($path); $result = $this->simpleResponse('MKCOL', $path, null, 201); @@ -236,8 +205,7 @@ class DAV extends Common { return $result; } - /** {@inheritdoc} */ - public function rmdir($path) { + public function rmdir(string $path): bool { $this->init(); $path = $this->cleanPath($path); // FIXME: some WebDAV impl return 403 when trying to DELETE @@ -248,8 +216,7 @@ class DAV extends Common { return $result; } - /** {@inheritdoc} */ - public function opendir($path) { + public function opendir(string $path) { $this->init(); $path = $this->cleanPath($path); try { @@ -273,16 +240,17 @@ class DAV extends Common { * * @param string $path path to propfind * - * @return array|boolean propfind response or false if the entry was not found + * @return array|false propfind response or false if the entry was not found * * @throws ClientHttpException */ - protected function propfind($path) { + protected function propfind(string $path): array|false { $path = $this->cleanPath($path); $cachedResponse = $this->statCache->get($path); // we either don't know it, or we know it exists but need more details if (is_null($cachedResponse) || $cachedResponse === true) { $this->init(); + $response = false; try { $response = $this->client->propFind( $this->encodePath($path), @@ -293,9 +261,9 @@ class DAV extends Common { if ($e->getHttpStatus() === 404 || $e->getHttpStatus() === 405) { $this->statCache->clear($path . '/'); $this->statCache->set($path, false); - return false; + } else { + $this->convertException($e, $path); } - $this->convertException($e, $path); } catch (\Exception $e) { $this->convertException($e, $path); } @@ -305,27 +273,25 @@ class DAV extends Common { return $response; } - /** {@inheritdoc} */ - public function filetype($path) { + public function filetype(string $path): string|false { try { $response = $this->propfind($path); if ($response === false) { return false; } $responseType = []; - if (isset($response["{DAV:}resourcetype"])) { + if (isset($response['{DAV:}resourcetype'])) { /** @var ResourceType[] $response */ - $responseType = $response["{DAV:}resourcetype"]->getValue(); + $responseType = $response['{DAV:}resourcetype']->getValue(); } - return (count($responseType) > 0 and $responseType[0] == "{DAV:}collection") ? 'dir' : 'file'; + return (count($responseType) > 0 && $responseType[0] == '{DAV:}collection') ? 'dir' : 'file'; } catch (\Exception $e) { $this->convertException($e, $path); } return false; } - /** {@inheritdoc} */ - public function file_exists($path) { + public function file_exists(string $path): bool { try { $path = $this->cleanPath($path); $cachedState = $this->statCache->get($path); @@ -343,8 +309,7 @@ class DAV extends Common { return false; } - /** {@inheritdoc} */ - public function unlink($path) { + public function unlink(string $path): bool { $this->init(); $path = $this->cleanPath($path); $result = $this->simpleResponse('DELETE', $path, null, 204); @@ -353,8 +318,7 @@ class DAV extends Common { return $result; } - /** {@inheritdoc} */ - public function fopen($path, $mode) { + public function fopen(string $path, string $mode) { $this->init(); $path = $this->cleanPath($path); switch ($mode) { @@ -382,11 +346,17 @@ class DAV extends Common { if ($response->getStatusCode() === Http::STATUS_LOCKED) { throw new \OCP\Lock\LockedException($path); } else { - \OC::$server->get(LoggerInterface::class)->error('Guzzle get returned status code ' . $response->getStatusCode(), ['app' => 'webdav client']); + $this->logger->error('Guzzle get returned status code ' . $response->getStatusCode(), ['app' => 'webdav client']); } } - return $response->getBody(); + $content = $response->getBody(); + + if ($content === null || is_string($content)) { + return false; + } + + return $content; case 'w': case 'wb': case 'a': @@ -410,7 +380,7 @@ class DAV extends Common { if (!$this->isUpdatable($path)) { return false; } - if ($mode === 'w' or $mode === 'w+') { + if ($mode === 'w' || $mode === 'w+') { $tmpFile = $tempManager->getTemporaryFile($ext); } else { $tmpFile = $this->getCachedFile($path); @@ -426,18 +396,16 @@ class DAV extends Common { $this->writeBack($tmpFile, $path); }); } + + return false; } - /** - * @param string $tmpFile - */ - public function writeBack($tmpFile, $path) { + public function writeBack(string $tmpFile, string $path): void { $this->uploadFile($tmpFile, $path); unlink($tmpFile); } - /** {@inheritdoc} */ - public function free_space($path) { + public function free_space(string $path): int|float|false { $this->init(); $path = $this->cleanPath($path); try { @@ -455,8 +423,7 @@ class DAV extends Common { } } - /** {@inheritdoc} */ - public function touch($path, $mtime = null) { + public function touch(string $path, ?int $mtime = null): bool { $this->init(); if (is_null($mtime)) { $mtime = time(); @@ -470,9 +437,6 @@ class DAV extends Common { $this->client->proppatch($this->encodePath($path), ['{DAV:}lastmodified' => $mtime]); // non-owncloud clients might not have accepted the property, need to recheck it $response = $this->client->propfind($this->encodePath($path), ['{DAV:}getlastmodified'], 0); - if ($response === false) { - return false; - } if (isset($response['{DAV:}getlastmodified'])) { $remoteMtime = strtotime($response['{DAV:}getlastmodified']); if ($remoteMtime !== $mtime) { @@ -496,23 +460,14 @@ class DAV extends Common { return true; } - /** - * @param string $path - * @param mixed $data - * @return int|float|false - */ - public function file_put_contents($path, $data) { + public function file_put_contents(string $path, mixed $data): int|float|false { $path = $this->cleanPath($path); $result = parent::file_put_contents($path, $data); $this->statCache->remove($path); return $result; } - /** - * @param string $path - * @param string $target - */ - protected function uploadFile($path, $target) { + protected function uploadFile(string $path, string $target): void { $this->init(); // invalidate @@ -532,8 +487,7 @@ class DAV extends Common { $this->removeCachedFile($target); } - /** {@inheritdoc} */ - public function rename($source, $target) { + public function rename(string $source, string $target): bool { $this->init(); $source = $this->cleanPath($source); $target = $this->cleanPath($target); @@ -564,8 +518,7 @@ class DAV extends Common { return false; } - /** {@inheritdoc} */ - public function copy($source, $target) { + public function copy(string $source, string $target): bool { $this->init(); $source = $this->cleanPath($source); $target = $this->cleanPath($target); @@ -593,7 +546,7 @@ class DAV extends Common { return false; } - public function getMetaData($path) { + public function getMetaData(string $path): ?array { if (Filesystem::isFileBlacklisted($path)) { throw new ForbiddenException('Invalid path: ' . $path, false); } @@ -615,11 +568,11 @@ class DAV extends Common { } $responseType = []; - if (isset($response["{DAV:}resourcetype"])) { + if (isset($response['{DAV:}resourcetype'])) { /** @var ResourceType[] $response */ - $responseType = $response["{DAV:}resourcetype"]->getValue(); + $responseType = $response['{DAV:}resourcetype']->getValue(); } - $type = (count($responseType) > 0 and $responseType[0] == "{DAV:}collection") ? 'dir' : 'file'; + $type = (count($responseType) > 0 && $responseType[0] == '{DAV:}collection') ? 'dir' : 'file'; if ($type === 'dir') { $mimeType = 'httpd/unix-directory'; } elseif (isset($response['{DAV:}getcontenttype'])) { @@ -655,31 +608,18 @@ class DAV extends Common { ]; } - /** {@inheritdoc} */ - public function stat($path) { + public function stat(string $path): array|false { $meta = $this->getMetaData($path); - if (!$meta) { - return false; - } else { - return $meta; - } + return $meta ?: false; + } - /** {@inheritdoc} */ - public function getMimeType($path) { + public function getMimeType(string $path): string|false { $meta = $this->getMetaData($path); - if ($meta) { - return $meta['mimetype']; - } else { - return false; - } + return $meta ? $meta['mimetype'] : false; } - /** - * @param string $path - * @return string - */ - public function cleanPath($path) { + public function cleanPath(string $path): string { if ($path === '') { return $path; } @@ -694,21 +634,17 @@ class DAV extends Common { * @param string $path to encode * @return string encoded path */ - protected function encodePath($path) { + protected function encodePath(string $path): string { // slashes need to stay return str_replace('%2F', '/', rawurlencode($path)); } /** - * @param string $method - * @param string $path - * @param string|resource|null $body - * @param int $expected * @return bool * @throws StorageInvalidException * @throws StorageNotAvailableException */ - protected function simpleResponse($method, $path, $body, $expected) { + protected function simpleResponse(string $method, string $path, ?string $body, int $expected): bool { $path = $this->cleanPath($path); try { $response = $this->client->request($method, $this->encodePath($path), $body); @@ -730,55 +666,37 @@ class DAV extends Common { /** * check if curl is installed */ - public static function checkDependencies() { + public static function checkDependencies(): bool { return true; } - /** {@inheritdoc} */ - public function isUpdatable($path) { + public function isUpdatable(string $path): bool { return (bool)($this->getPermissions($path) & Constants::PERMISSION_UPDATE); } - /** {@inheritdoc} */ - public function isCreatable($path) { + public function isCreatable(string $path): bool { return (bool)($this->getPermissions($path) & Constants::PERMISSION_CREATE); } - /** {@inheritdoc} */ - public function isSharable($path) { + public function isSharable(string $path): bool { return (bool)($this->getPermissions($path) & Constants::PERMISSION_SHARE); } - /** {@inheritdoc} */ - public function isDeletable($path) { + public function isDeletable(string $path): bool { return (bool)($this->getPermissions($path) & Constants::PERMISSION_DELETE); } - /** {@inheritdoc} */ - public function getPermissions($path) { + public function getPermissions(string $path): int { $stat = $this->getMetaData($path); - if ($stat) { - return $stat['permissions']; - } else { - return 0; - } + return $stat ? $stat['permissions'] : 0; } - /** {@inheritdoc} */ - public function getETag($path) { + public function getETag(string $path): string|false { $meta = $this->getMetaData($path); - if ($meta) { - return $meta['etag']; - } else { - return null; - } + return $meta ? $meta['etag'] : false; } - /** - * @param string $permissionsString - * @return int - */ - protected function parsePermissions($permissionsString) { + protected function parsePermissions(string $permissionsString): int { $permissions = Constants::PERMISSION_READ; if (str_contains($permissionsString, 'R')) { $permissions |= Constants::PERMISSION_SHARE; @@ -796,15 +714,7 @@ class DAV extends Common { return $permissions; } - /** - * check if a file or folder has been updated since $time - * - * @param string $path - * @param int $time - * @throws \OCP\Files\StorageNotAvailableException - * @return bool - */ - public function hasUpdated($path, $time) { + public function hasUpdated(string $path, int $time): bool { $this->init(); $path = $this->cleanPath($path); try { @@ -865,13 +775,13 @@ class DAV extends Common { * @param string $path optional path from the operation * * @throws StorageInvalidException if the storage is invalid, for example - * when the authentication expired or is invalid + * when the authentication expired or is invalid * @throws StorageNotAvailableException if the storage is not available, - * which might be temporary + * which might be temporary * @throws ForbiddenException if the action is not allowed */ - protected function convertException(Exception $e, $path = '') { - \OC::$server->get(LoggerInterface::class)->debug($e->getMessage(), ['app' => 'files_external', 'exception' => $e]); + protected function convertException(Exception $e, string $path = ''): void { + $this->logger->debug($e->getMessage(), ['app' => 'files_external', 'exception' => $e]); if ($e instanceof ClientHttpException) { if ($e->getHttpStatus() === Http::STATUS_LOCKED) { throw new \OCP\Lock\LockedException($path); @@ -902,7 +812,7 @@ class DAV extends Common { // TODO: only log for now, but in the future need to wrap/rethrow exception } - public function getDirectoryContent($directory): \Traversable { + public function getDirectoryContent(string $directory): \Traversable { $this->init(); $directory = $this->cleanPath($directory); try { @@ -911,9 +821,6 @@ class DAV extends Common { self::PROPFIND_PROPS, 1 ); - if ($responses === false) { - return; - } array_shift($responses); //the first entry is the current directory if (!$this->statCache->hasKey($directory)) { @@ -921,7 +828,7 @@ class DAV extends Common { } foreach ($responses as $file => $response) { - $file = urldecode($file); + $file = rawurldecode($file); $file = substr($file, strlen($this->root)); $file = $this->cleanPath($file); $this->statCache->set($file, $response); diff --git a/lib/private/Files/Storage/FailedStorage.php b/lib/private/Files/Storage/FailedStorage.php index 07b3b21d965..a8288de48d0 100644 --- a/lib/private/Files/Storage/FailedStorage.php +++ b/lib/private/Files/Storage/FailedStorage.php @@ -1,27 +1,9 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Lukas Reschke <lukas@statuscode.ch> - * @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: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ namespace OC\Files\Storage; @@ -38,177 +20,172 @@ class FailedStorage extends Common { protected $e; /** - * @param array $params ['exception' => \Exception] + * @param array $parameters ['exception' => \Exception] */ - public function __construct($params) { - $this->e = $params['exception']; + public function __construct(array $parameters) { + $this->e = $parameters['exception']; if (!$this->e) { throw new \InvalidArgumentException('Missing "exception" argument in FailedStorage constructor'); } } - public function getId() { + public function getId(): string { // we can't return anything sane here return 'failedstorage'; } - public function mkdir($path) { - throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e); - } - - public function rmdir($path) { + public function mkdir(string $path): never { throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e); } - public function opendir($path) { + public function rmdir(string $path): never { throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e); } - public function is_dir($path) { + public function opendir(string $path): never { throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e); } - public function is_file($path) { + public function is_dir(string $path): never { throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e); } - public function stat($path) { + public function is_file(string $path): never { throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e); } - public function filetype($path) { + public function stat(string $path): never { throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e); } - public function filesize($path): false|int|float { + public function filetype(string $path): never { throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e); } - public function isCreatable($path) { + public function filesize(string $path): never { throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e); } - public function isReadable($path) { + public function isCreatable(string $path): never { throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e); } - public function isUpdatable($path) { + public function isReadable(string $path): never { throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e); } - public function isDeletable($path) { + public function isUpdatable(string $path): never { throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e); } - public function isSharable($path) { + public function isDeletable(string $path): never { throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e); } - public function getPermissions($path) { + public function isSharable(string $path): never { throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e); } - public function file_exists($path) { + public function getPermissions(string $path): never { throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e); } - public function filemtime($path) { + public function file_exists(string $path): never { throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e); } - public function file_get_contents($path) { + public function filemtime(string $path): never { throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e); } - public function file_put_contents($path, $data) { + public function file_get_contents(string $path): never { throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e); } - public function unlink($path) { + public function file_put_contents(string $path, mixed $data): never { throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e); } - public function rename($source, $target) { + public function unlink(string $path): never { throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e); } - public function copy($source, $target) { + public function rename(string $source, string $target): never { throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e); } - public function fopen($path, $mode) { + public function copy(string $source, string $target): never { throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e); } - public function getMimeType($path) { + public function fopen(string $path, string $mode): never { throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e); } - public function hash($type, $path, $raw = false) { + public function getMimeType(string $path): never { throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e); } - public function free_space($path) { + public function hash(string $type, string $path, bool $raw = false): never { throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e); } - public function search($query) { + public function free_space(string $path): never { throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e); } - public function touch($path, $mtime = null) { + public function touch(string $path, ?int $mtime = null): never { throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e); } - public function getLocalFile($path) { + public function getLocalFile(string $path): never { throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e); } - public function hasUpdated($path, $time) { + public function hasUpdated(string $path, int $time): never { throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e); } - public function getETag($path) { + public function getETag(string $path): never { throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e); } - public function getDirectDownload($path) { + public function getDirectDownload(string $path): never { throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e); } - public function verifyPath($path, $fileName) { - return true; + public function verifyPath(string $path, string $fileName): void { } - public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime = false) { + public function copyFromStorage(IStorage $sourceStorage, string $sourceInternalPath, string $targetInternalPath, bool $preserveMtime = false): never { throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e); } - public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) { + public function moveFromStorage(IStorage $sourceStorage, string $sourceInternalPath, string $targetInternalPath): never { throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e); } - public function acquireLock($path, $type, ILockingProvider $provider) { + public function acquireLock(string $path, int $type, ILockingProvider $provider): never { throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e); } - public function releaseLock($path, $type, ILockingProvider $provider) { + public function releaseLock(string $path, int $type, ILockingProvider $provider): never { throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e); } - public function changeLock($path, $type, ILockingProvider $provider) { + public function changeLock(string $path, int $type, ILockingProvider $provider): never { throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e); } - public function getAvailability() { + public function getAvailability(): never { throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e); } - public function setAvailability($isAvailable) { + public function setAvailability(bool $isAvailable): never { throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e); } - public function getCache($path = '', $storage = null) { + public function getCache(string $path = '', ?IStorage $storage = null): FailedCache { return new FailedCache(); } } diff --git a/lib/private/Files/Storage/Home.php b/lib/private/Files/Storage/Home.php index 5100b15215b..91b8071ac30 100644 --- a/lib/private/Files/Storage/Home.php +++ b/lib/private/Files/Storage/Home.php @@ -1,31 +1,16 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Björn Schießle <bjoern@schiessle.org> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Robin Appelman <robin@icewind.nl> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * @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\Storage; use OC\Files\Cache\HomePropagator; +use OCP\Files\Cache\ICache; +use OCP\Files\Cache\IPropagator; +use OCP\Files\Storage\IStorage; use OCP\IUser; /** @@ -45,41 +30,32 @@ class Home extends Local implements \OCP\Files\IHomeStorage { /** * Construct a Home storage instance * - * @param array $arguments array with "user" containing the - * storage owner + * @param array $parameters array with "user" containing the + * storage owner */ - public function __construct($arguments) { - $this->user = $arguments['user']; + public function __construct(array $parameters) { + $this->user = $parameters['user']; $datadir = $this->user->getHome(); $this->id = 'home::' . $this->user->getUID(); parent::__construct(['datadir' => $datadir]); } - public function getId() { + public function getId(): string { return $this->id; } - /** - * @return \OC\Files\Cache\HomeCache - */ - public function getCache($path = '', $storage = null) { + public function getCache(string $path = '', ?IStorage $storage = null): ICache { if (!$storage) { $storage = $this; } if (!isset($this->cache)) { - $this->cache = new \OC\Files\Cache\HomeCache($storage); + $this->cache = new \OC\Files\Cache\HomeCache($storage, $this->getCacheDependencies()); } return $this->cache; } - /** - * get a propagator instance for the cache - * - * @param \OC\Files\Storage\Storage (optional) the storage to pass to the watcher - * @return \OC\Files\Cache\Propagator - */ - public function getPropagator($storage = null) { + public function getPropagator(?IStorage $storage = null): IPropagator { if (!$storage) { $storage = $this; } @@ -90,22 +66,11 @@ class Home extends Local implements \OCP\Files\IHomeStorage { } - /** - * Returns the owner of this home storage - * - * @return \OC\User\User owner of this home storage - */ public function getUser(): IUser { return $this->user; } - /** - * get the owner of a path - * - * @param string $path The path to get the owner - * @return string uid or false - */ - public function getOwner($path) { + public function getOwner(string $path): string|false { return $this->user->getUID(); } } diff --git a/lib/private/Files/Storage/Local.php b/lib/private/Files/Storage/Local.php index 0fca853da59..260f9218a88 100644 --- a/lib/private/Files/Storage/Local.php +++ b/lib/private/Files/Storage/Local.php @@ -1,45 +1,9 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author aler9 <46489434+aler9@users.noreply.github.com> - * @author Arthur Schiwon <blizzz@arthur-schiwon.de> - * @author Bart Visscher <bartv@thisnet.nl> - * @author Boris Rybalkin <ribalkin@gmail.com> - * @author Brice Maron <brice@bmaron.net> - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author J0WI <J0WI@users.noreply.github.com> - * @author Jakob Sack <mail@jakobsack.de> - * @author Joas Schilling <coding@schilljs.com> - * @author Johannes Leuker <j.leuker@hosting.de> - * @author Jörn Friedrich Dreyer <jfd@butonic.de> - * @author Klaas Freitag <freitag@owncloud.com> - * @author Lukas Reschke <lukas@statuscode.ch> - * @author Martin Brugnara <martin@0x6d62.eu> - * @author Michael Gapczynski <GapczynskiM@gmail.com> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Robin Appelman <robin@icewind.nl> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Sjors van der Pluijm <sjors@desjors.nl> - * @author Stefan Weil <sw@weilnetz.de> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * @author Tigran Mkrtchyan <tigran.mkrtchyan@desy.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\Storage; @@ -53,6 +17,7 @@ use OCP\Files\IMimeTypeDetector; use OCP\Files\Storage\IStorage; use OCP\Files\StorageNotAvailableException; use OCP\IConfig; +use OCP\Server; use OCP\Util; use Psr\Log\LoggerInterface; @@ -76,11 +41,11 @@ class Local extends \OC\Files\Storage\Common { protected bool $caseInsensitive = false; - public function __construct($arguments) { - if (!isset($arguments['datadir']) || !is_string($arguments['datadir'])) { + public function __construct(array $parameters) { + if (!isset($parameters['datadir']) || !is_string($parameters['datadir'])) { throw new \InvalidArgumentException('No data directory set for local storage'); } - $this->datadir = str_replace('//', '/', $arguments['datadir']); + $this->datadir = str_replace('//', '/', $parameters['datadir']); // some crazy code uses a local storage on root... if ($this->datadir === '/') { $this->realDataDir = $this->datadir; @@ -92,15 +57,15 @@ class Local extends \OC\Files\Storage\Common { $this->datadir .= '/'; } $this->dataDirLength = strlen($this->realDataDir); - $this->config = \OC::$server->get(IConfig::class); - $this->mimeTypeDetector = \OC::$server->get(IMimeTypeDetector::class); + $this->config = Server::get(IConfig::class); + $this->mimeTypeDetector = Server::get(IMimeTypeDetector::class); $this->defUMask = $this->config->getSystemValue('localstorage.umask', 0022); $this->caseInsensitive = $this->config->getSystemValueBool('localstorage.case_insensitive', false); // support Write-Once-Read-Many file systems $this->unlinkOnTruncate = $this->config->getSystemValueBool('localstorage.unlink_on_truncate', false); - if (isset($arguments['isExternal']) && $arguments['isExternal'] && !$this->stat('')) { + if (isset($parameters['isExternal']) && $parameters['isExternal'] && !$this->stat('')) { // data dir not accessible or available, can happen when using an external storage of type Local // on an unmounted system mount point throw new StorageNotAvailableException('Local storage path does not exist "' . $this->getSourcePath('') . '"'); @@ -110,11 +75,11 @@ class Local extends \OC\Files\Storage\Common { public function __destruct() { } - public function getId() { + public function getId(): string { return 'local::' . $this->datadir; } - public function mkdir($path) { + public function mkdir(string $path): bool { $sourcePath = $this->getSourcePath($path); $oldMask = umask($this->defUMask); $result = @mkdir($sourcePath, 0777, true); @@ -122,7 +87,7 @@ class Local extends \OC\Files\Storage\Common { return $result; } - public function rmdir($path) { + public function rmdir(string $path): bool { if (!$this->isDeletable($path)) { return false; } @@ -142,7 +107,7 @@ class Local extends \OC\Files\Storage\Common { * @var \SplFileInfo $file */ $file = $it->current(); - clearstatcache(true, $this->getSourcePath($file)); + clearstatcache(true, $file->getRealPath()); if (in_array($file->getBasename(), ['.', '..'])) { $it->next(); continue; @@ -153,6 +118,7 @@ class Local extends \OC\Files\Storage\Common { } $it->next(); } + unset($it); // Release iterator and thereby its potential directory lock (e.g. in case of VirtualBox shared folders) clearstatcache(true, $this->getSourcePath($path)); return rmdir($this->getSourcePath($path)); } catch (\UnexpectedValueException $e) { @@ -160,11 +126,11 @@ class Local extends \OC\Files\Storage\Common { } } - public function opendir($path) { + public function opendir(string $path) { return opendir($this->getSourcePath($path)); } - public function is_dir($path) { + public function is_dir(string $path): bool { if ($this->caseInsensitive && !$this->file_exists($path)) { return false; } @@ -174,14 +140,14 @@ class Local extends \OC\Files\Storage\Common { return is_dir($this->getSourcePath($path)); } - public function is_file($path) { + public function is_file(string $path): bool { if ($this->caseInsensitive && !$this->file_exists($path)) { return false; } return is_file($this->getSourcePath($path)); } - public function stat($path) { + public function stat(string $path): array|false { $fullPath = $this->getSourcePath($path); clearstatcache(true, $fullPath); if (!file_exists($fullPath)) { @@ -199,10 +165,7 @@ class Local extends \OC\Files\Storage\Common { return $statResult; } - /** - * @inheritdoc - */ - public function getMetaData($path) { + public function getMetaData(string $path): ?array { try { $stat = $this->stat($path); } catch (ForbiddenException $e) { @@ -251,7 +214,7 @@ class Local extends \OC\Files\Storage\Common { return $data; } - public function filetype($path) { + public function filetype(string $path): string|false { $filetype = filetype($this->getSourcePath($path)); if ($filetype == 'link') { $filetype = filetype(realpath($this->getSourcePath($path))); @@ -259,7 +222,7 @@ class Local extends \OC\Files\Storage\Common { return $filetype; } - public function filesize($path): false|int|float { + public function filesize(string $path): int|float|false { if (!$this->is_file($path)) { return 0; } @@ -271,25 +234,29 @@ class Local extends \OC\Files\Storage\Common { return filesize($fullPath); } - public function isReadable($path) { + public function isReadable(string $path): bool { return is_readable($this->getSourcePath($path)); } - public function isUpdatable($path) { + public function isUpdatable(string $path): bool { return is_writable($this->getSourcePath($path)); } - public function file_exists($path) { + public function file_exists(string $path): bool { if ($this->caseInsensitive) { $fullPath = $this->getSourcePath($path); - $content = scandir(dirname($fullPath), SCANDIR_SORT_NONE); + $parentPath = dirname($fullPath); + if (!is_dir($parentPath)) { + return false; + } + $content = scandir($parentPath, SCANDIR_SORT_NONE); return is_array($content) && array_search(basename($fullPath), $content) !== false; } else { return file_exists($this->getSourcePath($path)); } } - public function filemtime($path) { + public function filemtime(string $path): int|false { $fullPath = $this->getSourcePath($path); clearstatcache(true, $fullPath); if (!$this->file_exists($path)) { @@ -302,11 +269,11 @@ class Local extends \OC\Files\Storage\Common { return filemtime($fullPath); } - public function touch($path, $mtime = null) { + public function touch(string $path, ?int $mtime = null): bool { // sets the modification time of the file to the given value. // If mtime is nil the current time is set. // note that the access time of the file always changes to the current time. - if ($this->file_exists($path) and !$this->isUpdatable($path)) { + if ($this->file_exists($path) && !$this->isUpdatable($path)) { return false; } $oldMask = umask($this->defUMask); @@ -323,11 +290,11 @@ class Local extends \OC\Files\Storage\Common { return $result; } - public function file_get_contents($path) { + public function file_get_contents(string $path): string|false { return file_get_contents($this->getSourcePath($path)); } - public function file_put_contents($path, $data) { + public function file_put_contents(string $path, mixed $data): int|float|false { $oldMask = umask($this->defUMask); if ($this->unlinkOnTruncate) { $this->unlink($path); @@ -337,7 +304,7 @@ class Local extends \OC\Files\Storage\Common { return $result; } - public function unlink($path) { + public function unlink(string $path): bool { if ($this->is_dir($path)) { return $this->rmdir($path); } elseif ($this->is_file($path)) { @@ -347,7 +314,7 @@ class Local extends \OC\Files\Storage\Common { } } - private function checkTreeForForbiddenItems(string $path) { + private function checkTreeForForbiddenItems(string $path): void { $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path)); foreach ($iterator as $file) { /** @var \SplFileInfo $file */ @@ -357,29 +324,31 @@ class Local extends \OC\Files\Storage\Common { } } - public function rename($source, $target): bool { + public function rename(string $source, string $target): bool { $srcParent = dirname($source); $dstParent = dirname($target); if (!$this->isUpdatable($srcParent)) { - \OC::$server->get(LoggerInterface::class)->error('unable to rename, source directory is not writable : ' . $srcParent, ['app' => 'core']); + Server::get(LoggerInterface::class)->error('unable to rename, source directory is not writable : ' . $srcParent, ['app' => 'core']); return false; } if (!$this->isUpdatable($dstParent)) { - \OC::$server->get(LoggerInterface::class)->error('unable to rename, destination directory is not writable : ' . $dstParent, ['app' => 'core']); + Server::get(LoggerInterface::class)->error('unable to rename, destination directory is not writable : ' . $dstParent, ['app' => 'core']); return false; } if (!$this->file_exists($source)) { - \OC::$server->get(LoggerInterface::class)->error('unable to rename, file does not exists : ' . $source, ['app' => 'core']); + Server::get(LoggerInterface::class)->error('unable to rename, file does not exists : ' . $source, ['app' => 'core']); return false; } - if ($this->is_dir($target)) { - $this->rmdir($target); - } elseif ($this->is_file($target)) { - $this->unlink($target); + if ($this->file_exists($target)) { + if ($this->is_dir($target)) { + $this->rmdir($target); + } elseif ($this->is_file($target)) { + $this->unlink($target); + } } if ($this->is_dir($source)) { @@ -398,7 +367,7 @@ class Local extends \OC\Files\Storage\Common { return $this->copy($source, $target) && $this->unlink($source); } - public function copy($source, $target) { + public function copy(string $source, string $target): bool { if ($this->is_dir($source)) { return parent::copy($source, $target); } else { @@ -417,7 +386,7 @@ class Local extends \OC\Files\Storage\Common { } } - public function fopen($path, $mode) { + public function fopen(string $path, string $mode) { $sourcePath = $this->getSourcePath($path); if (!file_exists($sourcePath) && $mode === 'r') { return false; @@ -431,11 +400,11 @@ class Local extends \OC\Files\Storage\Common { return $result; } - public function hash($type, $path, $raw = false) { + public function hash(string $type, string $path, bool $raw = false): string|false { return hash_file($type, $this->getSourcePath($path), $raw); } - public function free_space($path) { + public function free_space(string $path): int|float|false { $sourcePath = $this->getSourcePath($path); // using !is_dir because $sourcePath might be a part file or // non-existing file, so we'd still want to use the parent dir @@ -451,20 +420,15 @@ class Local extends \OC\Files\Storage\Common { return Util::numericToNumber($space); } - public function search($query) { + public function search(string $query): array { return $this->searchInDir($query); } - public function getLocalFile($path) { + public function getLocalFile(string $path): string|false { return $this->getSourcePath($path); } - /** - * @param string $query - * @param string $dir - * @return array - */ - protected function searchInDir($query, $dir = '') { + protected function searchInDir(string $query, string $dir = ''): array { $files = []; $physicalDir = $this->getSourcePath($dir); foreach (scandir($physicalDir) as $item) { @@ -483,14 +447,7 @@ class Local extends \OC\Files\Storage\Common { return $files; } - /** - * check if a file or folder has been updated since $time - * - * @param string $path - * @param int $time - * @return bool - */ - public function hasUpdated($path, $time) { + public function hasUpdated(string $path, int $time): bool { if ($this->file_exists($path)) { return $this->filemtime($path) > $time; } else { @@ -501,11 +458,9 @@ class Local extends \OC\Files\Storage\Common { /** * Get the source path (on disk) of a given path * - * @param string $path - * @return string * @throws ForbiddenException */ - public function getSourcePath($path) { + public function getSourcePath(string $path): string { if (Filesystem::isFileBlacklisted($path)) { throw new ForbiddenException('Invalid path: ' . $path, false); } @@ -520,6 +475,7 @@ class Local extends \OC\Files\Storage\Common { $realPath = realpath($pathToResolve); while ($realPath === false) { // for non existing files check the parent directory $currentPath = dirname($currentPath); + /** @psalm-suppress TypeDoesNotContainType Let's be extra cautious and still check for empty string */ if ($currentPath === '' || $currentPath === '.') { return $fullPath; } @@ -532,28 +488,19 @@ class Local extends \OC\Files\Storage\Common { return $fullPath; } - \OC::$server->get(LoggerInterface::class)->error("Following symlinks is not allowed ('$fullPath' -> '$realPath' not inside '{$this->realDataDir}')", ['app' => 'core']); + Server::get(LoggerInterface::class)->error("Following symlinks is not allowed ('$fullPath' -> '$realPath' not inside '{$this->realDataDir}')", ['app' => 'core']); throw new ForbiddenException('Following symlinks is not allowed', false); } - /** - * {@inheritdoc} - */ - public function isLocal() { + public function isLocal(): bool { return true; } - /** - * get the ETag for a file or folder - * - * @param string $path - * @return string - */ - public function getETag($path) { + public function getETag(string $path): string|false { return $this->calculateEtag($path, $this->stat($path)); } - private function calculateEtag(string $path, array $stat): string { + private function calculateEtag(string $path, array $stat): string|false { if ($stat['mode'] & 0x4000 && !($stat['mode'] & 0x8000)) { // is_dir & not socket return parent::getETag($path); } else { @@ -579,30 +526,28 @@ class Local extends \OC\Files\Storage\Common { } } - private function canDoCrossStorageMove(IStorage $sourceStorage) { + private function canDoCrossStorageMove(IStorage $sourceStorage): bool { + /** @psalm-suppress UndefinedClass,InvalidArgument */ return $sourceStorage->instanceOfStorage(Local::class) // Don't treat ACLStorageWrapper like local storage where copy can be done directly. // Instead, use the slower recursive copying in php from Common::copyFromStorage with // more permissions checks. && !$sourceStorage->instanceOfStorage('OCA\GroupFolders\ACL\ACLStorageWrapper') + // Same for access control + && !$sourceStorage->instanceOfStorage(\OCA\FilesAccessControl\StorageWrapper::class) // when moving encrypted files we have to handle keys and the target might not be encrypted && !$sourceStorage->instanceOfStorage(Encryption::class); } - /** - * @param IStorage $sourceStorage - * @param string $sourceInternalPath - * @param string $targetInternalPath - * @param bool $preserveMtime - * @return bool - */ - public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime = false) { + public function copyFromStorage(IStorage $sourceStorage, string $sourceInternalPath, string $targetInternalPath, bool $preserveMtime = false): bool { if ($this->canDoCrossStorageMove($sourceStorage)) { - if ($sourceStorage->instanceOfStorage(Jail::class)) { + // resolve any jailed paths + while ($sourceStorage->instanceOfStorage(Jail::class)) { /** * @var \OC\Files\Storage\Wrapper\Jail $sourceStorage */ $sourceInternalPath = $sourceStorage->getUnjailedPath($sourceInternalPath); + $sourceStorage = $sourceStorage->getUnjailedStorage(); } /** * @var \OC\Files\Storage\Local $sourceStorage @@ -614,19 +559,15 @@ class Local extends \OC\Files\Storage\Common { } } - /** - * @param IStorage $sourceStorage - * @param string $sourceInternalPath - * @param string $targetInternalPath - * @return bool - */ - public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) { + public function moveFromStorage(IStorage $sourceStorage, string $sourceInternalPath, string $targetInternalPath): bool { if ($this->canDoCrossStorageMove($sourceStorage)) { - if ($sourceStorage->instanceOfStorage(Jail::class)) { + // resolve any jailed paths + while ($sourceStorage->instanceOfStorage(Jail::class)) { /** * @var \OC\Files\Storage\Wrapper\Jail $sourceStorage */ $sourceInternalPath = $sourceStorage->getUnjailedPath($sourceInternalPath); + $sourceStorage = $sourceStorage->getUnjailedStorage(); } /** * @var \OC\Files\Storage\Local $sourceStorage @@ -638,7 +579,7 @@ class Local extends \OC\Files\Storage\Common { } } - public function writeStream(string $path, $stream, int $size = null): int { + public function writeStream(string $path, $stream, ?int $size = null): int { /** @var int|false $result We consider here that returned size will never be a float because we write less than 4GB */ $result = $this->file_put_contents($path, $stream); if (is_resource($stream)) { diff --git a/lib/private/Files/Storage/LocalRootStorage.php b/lib/private/Files/Storage/LocalRootStorage.php index 71584afef08..2e0645e092a 100644 --- a/lib/private/Files/Storage/LocalRootStorage.php +++ b/lib/private/Files/Storage/LocalRootStorage.php @@ -3,38 +3,20 @@ 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\Storage; use OC\Files\Cache\LocalRootScanner; +use OCP\Files\Cache\IScanner; +use OCP\Files\Storage\IStorage; class LocalRootStorage extends Local { - public function getScanner($path = '', $storage = null) { + public function getScanner(string $path = '', ?IStorage $storage = null): IScanner { if (!$storage) { $storage = $this; } - if (!isset($storage->scanner)) { - $storage->scanner = new LocalRootScanner($storage); - } - return $storage->scanner; + return $storage->scanner ?? ($storage->scanner = new LocalRootScanner($storage)); } } diff --git a/lib/private/Files/Storage/LocalTempFileTrait.php b/lib/private/Files/Storage/LocalTempFileTrait.php index 314e38cb277..fffc3e789f3 100644 --- a/lib/private/Files/Storage/LocalTempFileTrait.php +++ b/lib/private/Files/Storage/LocalTempFileTrait.php @@ -1,28 +1,14 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Lukas Reschke <lukas@statuscode.ch> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * - * @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\Storage; +use OCP\Files; + /** * Storage backend class for providing common filesystem operation methods * which are not storage-backend specific. @@ -45,10 +31,7 @@ trait LocalTempFileTrait { return $this->cachedFiles[$path]; } - /** - * @param string $path - */ - protected function removeCachedFile($path) { + protected function removeCachedFile(string $path): void { unset($this->cachedFiles[$path]); } @@ -64,7 +47,7 @@ trait LocalTempFileTrait { } $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($extension); $target = fopen($tmpFile, 'w'); - \OC_Helper::streamCopy($source, $target); + Files::streamCopy($source, $target); fclose($target); return $tmpFile; } diff --git a/lib/private/Files/Storage/PolyFill/CopyDirectory.php b/lib/private/Files/Storage/PolyFill/CopyDirectory.php index ff05eecb134..2f6167ef85e 100644 --- a/lib/private/Files/Storage/PolyFill/CopyDirectory.php +++ b/lib/private/Files/Storage/PolyFill/CopyDirectory.php @@ -1,70 +1,41 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Martin Mattel <martin.mattel@diemattels.at> - * @author Robin Appelman <robin@icewind.nl> - * @author Stefan Weil <sw@weilnetz.de> - * - * @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\Storage\PolyFill; trait CopyDirectory { /** * Check if a path is a directory - * - * @param string $path - * @return bool */ - abstract public function is_dir($path); + abstract public function is_dir(string $path): bool; /** * Check if a file or folder exists - * - * @param string $path - * @return bool */ - abstract public function file_exists($path); + abstract public function file_exists(string $path): bool; /** * Delete a file or folder - * - * @param string $path - * @return bool */ - abstract public function unlink($path); + abstract public function unlink(string $path): bool; /** * Open a directory handle for a folder * - * @param string $path - * @return resource | bool + * @return resource|false */ - abstract public function opendir($path); + abstract public function opendir(string $path); /** * Create a new folder - * - * @param string $path - * @return bool */ - abstract public function mkdir($path); + abstract public function mkdir(string $path): bool; - public function copy($source, $target) { + public function copy(string $source, string $target): bool { if ($this->is_dir($source)) { if ($this->file_exists($target)) { $this->unlink($target); @@ -78,15 +49,11 @@ trait CopyDirectory { /** * For adapters that don't support copying folders natively - * - * @param $source - * @param $target - * @return bool */ - protected function copyRecursive($source, $target) { + protected function copyRecursive(string $source, string $target): bool { $dh = $this->opendir($source); $result = true; - while ($file = readdir($dh)) { + while (($file = readdir($dh)) !== false) { if (!\OC\Files\Filesystem::isIgnoredDir($file)) { if ($this->is_dir($source . '/' . $file)) { $this->mkdir($target . '/' . $file); diff --git a/lib/private/Files/Storage/Storage.php b/lib/private/Files/Storage/Storage.php index 0a2511de164..aa17c12b309 100644 --- a/lib/private/Files/Storage/Storage.php +++ b/lib/private/Files/Storage/Storage.php @@ -1,130 +1,44 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Robin Appelman <robin@icewind.nl> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * - * @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\Storage; -use OCP\Lock\ILockingProvider; +use OCP\Files\Cache\ICache; +use OCP\Files\Cache\IPropagator; +use OCP\Files\Cache\IScanner; +use OCP\Files\Cache\IUpdater; +use OCP\Files\Cache\IWatcher; +use OCP\Files\Storage\ILockingStorage; +use OCP\Files\Storage\IStorage; /** * Provide a common interface to all different storage options * * All paths passed to the storage are relative to the storage and should NOT have a leading slash. */ -interface Storage extends \OCP\Files\Storage { - /** - * get a cache instance for the storage - * - * @param string $path - * @param \OC\Files\Storage\Storage|null (optional) the storage to pass to the cache - * @return \OC\Files\Cache\Cache - */ - public function getCache($path = '', $storage = null); - - /** - * get a scanner instance for the storage - * - * @param string $path - * @param \OC\Files\Storage\Storage (optional) the storage to pass to the scanner - * @return \OC\Files\Cache\Scanner - */ - public function getScanner($path = '', $storage = null); - - - /** - * get the user id of the owner of a file or folder - * - * @param string $path - * @return string - */ - public function getOwner($path); - - /** - * get a watcher instance for the cache - * - * @param string $path - * @param \OC\Files\Storage\Storage (optional) the storage to pass to the watcher - * @return \OC\Files\Cache\Watcher - */ - public function getWatcher($path = '', $storage = null); +interface Storage extends IStorage, ILockingStorage { + public function getCache(string $path = '', ?IStorage $storage = null): ICache; - /** - * get a propagator instance for the cache - * - * @param \OC\Files\Storage\Storage (optional) the storage to pass to the watcher - * @return \OC\Files\Cache\Propagator - */ - public function getPropagator($storage = null); + public function getScanner(string $path = '', ?IStorage $storage = null): IScanner; - /** - * get a updater instance for the cache - * - * @param \OC\Files\Storage\Storage (optional) the storage to pass to the watcher - * @return \OC\Files\Cache\Updater - */ - public function getUpdater($storage = null); + public function getWatcher(string $path = '', ?IStorage $storage = null): IWatcher; - /** - * @return \OC\Files\Cache\Storage - */ - public function getStorageCache(); + public function getPropagator(?IStorage $storage = null): IPropagator; - /** - * @param string $path - * @return array|null - */ - public function getMetaData($path); - - /** - * @param string $path The path of the file to acquire the lock for - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE - * @param \OCP\Lock\ILockingProvider $provider - * @throws \OCP\Lock\LockedException - */ - public function acquireLock($path, $type, ILockingProvider $provider); + public function getUpdater(?IStorage $storage = null): IUpdater; - /** - * @param string $path The path of the file to release the lock for - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE - * @param \OCP\Lock\ILockingProvider $provider - * @throws \OCP\Lock\LockedException - */ - public function releaseLock($path, $type, ILockingProvider $provider); + public function getStorageCache(): \OC\Files\Cache\Storage; - /** - * @param string $path The path of the file to change the lock for - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE - * @param \OCP\Lock\ILockingProvider $provider - * @throws \OCP\Lock\LockedException - */ - public function changeLock($path, $type, ILockingProvider $provider); + public function getMetaData(string $path): ?array; /** * Get the contents of a directory with metadata * - * @param string $directory - * @return \Traversable an iterator, containing file metadata - * * The metadata array will contain the following fields * * - name @@ -135,5 +49,5 @@ interface Storage extends \OCP\Files\Storage { * - storage_mtime * - permissions */ - public function getDirectoryContent($directory): \Traversable; + public function getDirectoryContent(string $directory): \Traversable; } diff --git a/lib/private/Files/Storage/StorageFactory.php b/lib/private/Files/Storage/StorageFactory.php index cab739c4a81..603df7fe007 100644 --- a/lib/private/Files/Storage/StorageFactory.php +++ b/lib/private/Files/Storage/StorageFactory.php @@ -1,31 +1,17 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Jörn Friedrich Dreyer <jfd@butonic.de> - * @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\Storage; use OCP\Files\Mount\IMountPoint; +use OCP\Files\Storage\IConstructableStorage; +use OCP\Files\Storage\IStorage; use OCP\Files\Storage\IStorageFactory; +use Psr\Log\LoggerInterface; class StorageFactory implements IStorageFactory { /** @@ -33,19 +19,7 @@ class StorageFactory implements IStorageFactory { */ private $storageWrappers = []; - /** - * allow modifier storage behaviour by adding wrappers around storages - * - * $callback should be a function of type (string $mountPoint, Storage $storage) => Storage - * - * @param string $wrapperName name of the wrapper - * @param callable $callback callback - * @param int $priority wrappers with the lower priority are applied last (meaning they get called first) - * @param \OCP\Files\Mount\IMountPoint[] $existingMounts existing mount points to apply the wrapper to - * @return bool true if the wrapper was added, false if there was already a wrapper with this - * name registered - */ - public function addStorageWrapper($wrapperName, $callback, $priority = 50, $existingMounts = []) { + public function addStorageWrapper(string $wrapperName, callable $callback, int $priority = 50, array $existingMounts = []): bool { if (isset($this->storageWrappers[$wrapperName])) { return false; } @@ -63,31 +37,23 @@ class StorageFactory implements IStorageFactory { * Remove a storage wrapper by name. * Note: internal method only to be used for cleanup * - * @param string $wrapperName name of the wrapper * @internal */ - public function removeStorageWrapper($wrapperName) { + public function removeStorageWrapper(string $wrapperName): void { unset($this->storageWrappers[$wrapperName]); } /** * Create an instance of a storage and apply the registered storage wrappers - * - * @param \OCP\Files\Mount\IMountPoint $mountPoint - * @param string $class - * @param array $arguments - * @return \OCP\Files\Storage */ - public function getInstance(IMountPoint $mountPoint, $class, $arguments) { + public function getInstance(IMountPoint $mountPoint, string $class, array $arguments): IStorage { + if (!is_a($class, IConstructableStorage::class, true)) { + \OCP\Server::get(LoggerInterface::class)->warning('Building a storage not implementing IConstructableStorage is deprecated since 31.0.0', ['class' => $class]); + } return $this->wrap($mountPoint, new $class($arguments)); } - /** - * @param \OCP\Files\Mount\IMountPoint $mountPoint - * @param \OCP\Files\Storage $storage - * @return \OCP\Files\Storage - */ - public function wrap(IMountPoint $mountPoint, $storage) { + public function wrap(IMountPoint $mountPoint, IStorage $storage): IStorage { $wrappers = array_values($this->storageWrappers); usort($wrappers, function ($a, $b) { return $b['priority'] - $a['priority']; @@ -98,7 +64,7 @@ class StorageFactory implements IStorageFactory { }, $wrappers); foreach ($wrappers as $wrapper) { $storage = $wrapper($mountPoint->getMountPoint(), $storage, $mountPoint); - if (!($storage instanceof \OCP\Files\Storage)) { + if (!($storage instanceof IStorage)) { throw new \Exception('Invalid result from storage wrapper'); } } diff --git a/lib/private/Files/Storage/Temporary.php b/lib/private/Files/Storage/Temporary.php index 393a37f834a..ecf8a1315a9 100644 --- a/lib/private/Files/Storage/Temporary.php +++ b/lib/private/Files/Storage/Temporary.php @@ -1,40 +1,26 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Robin Appelman <robin@icewind.nl> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * @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\Storage; +use OCP\Files; +use OCP\ITempManager; +use OCP\Server; + /** * local storage backend in temporary folder for testing purpose */ class Temporary extends Local { - public function __construct($arguments = null) { - parent::__construct(['datadir' => \OC::$server->getTempManager()->getTemporaryFolder()]); + public function __construct(array $parameters = []) { + parent::__construct(['datadir' => Server::get(ITempManager::class)->getTemporaryFolder()]); } - public function cleanUp() { - \OC_Helper::rmdirr($this->datadir); + public function cleanUp(): void { + Files::rmdirr($this->datadir); } public function __destruct() { @@ -42,7 +28,7 @@ class Temporary extends Local { $this->cleanUp(); } - public function getDataDir() { + public function getDataDir(): array|string { return $this->datadir; } } diff --git a/lib/private/Files/Storage/Wrapper/Availability.php b/lib/private/Files/Storage/Wrapper/Availability.php index 693d943f0dc..32c51a1b25e 100644 --- a/lib/private/Files/Storage/Wrapper/Availability.php +++ b/lib/private/Files/Storage/Wrapper/Availability.php @@ -1,28 +1,9 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Arthur Schiwon <blizzz@arthur-schiwon.de> - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Lukas Reschke <lukas@statuscode.ch> - * @author Robin Appelman <robin@icewind.nl> - * @author Robin McCorkell <robin@mccorkell.me.uk> - * @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\Storage\Wrapper; @@ -42,12 +23,12 @@ class Availability extends Wrapper { /** @var IConfig */ protected $config; - public function __construct($parameters) { - $this->config = $parameters['config'] ?? \OC::$server->getConfig(); + public function __construct(array $parameters) { + $this->config = $parameters['config'] ?? \OCP\Server::get(IConfig::class); parent::__construct($parameters); } - public static function shouldRecheck($availability) { + public static function shouldRecheck($availability): bool { if (!$availability['available']) { // trigger a recheck if TTL reached if ((time() - $availability['last_checked']) > self::RECHECK_TTL_SEC) { @@ -59,10 +40,8 @@ class Availability extends Wrapper { /** * Only called if availability === false - * - * @return bool */ - private function updateAvailability() { + private function updateAvailability(): bool { // reset availability to false so that multiple requests don't recheck concurrently $this->setAvailability(false); try { @@ -74,10 +53,7 @@ class Availability extends Wrapper { return $result; } - /** - * @return bool - */ - private function isAvailable() { + private function isAvailable(): bool { $availability = $this->getAvailability(); if (self::shouldRecheck($availability)) { return $this->updateAvailability(); @@ -88,297 +64,137 @@ class Availability extends Wrapper { /** * @throws StorageNotAvailableException */ - private function checkAvailability() { + private function checkAvailability(): void { if (!$this->isAvailable()) { throw new StorageNotAvailableException(); } } - /** {@inheritdoc} */ - public function mkdir($path) { + /** + * Handles availability checks and delegates method calls dynamically + */ + private function handleAvailability(string $method, mixed ...$args): mixed { $this->checkAvailability(); try { - return parent::mkdir($path); + return call_user_func_array([parent::class, $method], $args); } catch (StorageNotAvailableException $e) { $this->setUnavailable($e); + return false; } } - /** {@inheritdoc} */ - public function rmdir($path) { - $this->checkAvailability(); - try { - return parent::rmdir($path); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - } + public function mkdir(string $path): bool { + return $this->handleAvailability('mkdir', $path); } - /** {@inheritdoc} */ - public function opendir($path) { - $this->checkAvailability(); - try { - return parent::opendir($path); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - } + public function rmdir(string $path): bool { + return $this->handleAvailability('rmdir', $path); } - /** {@inheritdoc} */ - public function is_dir($path) { - $this->checkAvailability(); - try { - return parent::is_dir($path); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - } + public function opendir(string $path) { + return $this->handleAvailability('opendir', $path); } - /** {@inheritdoc} */ - public function is_file($path) { - $this->checkAvailability(); - try { - return parent::is_file($path); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - } + public function is_dir(string $path): bool { + return $this->handleAvailability('is_dir', $path); } - /** {@inheritdoc} */ - public function stat($path) { - $this->checkAvailability(); - try { - return parent::stat($path); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - } + public function is_file(string $path): bool { + return $this->handleAvailability('is_file', $path); } - /** {@inheritdoc} */ - public function filetype($path) { - $this->checkAvailability(); - try { - return parent::filetype($path); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - } + public function stat(string $path): array|false { + return $this->handleAvailability('stat', $path); } - /** {@inheritdoc} */ - public function filesize($path): false|int|float { - $this->checkAvailability(); - try { - return parent::filesize($path); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - } + public function filetype(string $path): string|false { + return $this->handleAvailability('filetype', $path); } - /** {@inheritdoc} */ - public function isCreatable($path) { - $this->checkAvailability(); - try { - return parent::isCreatable($path); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - } + public function filesize(string $path): int|float|false { + return $this->handleAvailability('filesize', $path); } - /** {@inheritdoc} */ - public function isReadable($path) { - $this->checkAvailability(); - try { - return parent::isReadable($path); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - } + public function isCreatable(string $path): bool { + return $this->handleAvailability('isCreatable', $path); } - /** {@inheritdoc} */ - public function isUpdatable($path) { - $this->checkAvailability(); - try { - return parent::isUpdatable($path); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - } + public function isReadable(string $path): bool { + return $this->handleAvailability('isReadable', $path); } - /** {@inheritdoc} */ - public function isDeletable($path) { - $this->checkAvailability(); - try { - return parent::isDeletable($path); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - } + public function isUpdatable(string $path): bool { + return $this->handleAvailability('isUpdatable', $path); } - /** {@inheritdoc} */ - public function isSharable($path) { - $this->checkAvailability(); - try { - return parent::isSharable($path); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - } + public function isDeletable(string $path): bool { + return $this->handleAvailability('isDeletable', $path); } - /** {@inheritdoc} */ - public function getPermissions($path) { - $this->checkAvailability(); - try { - return parent::getPermissions($path); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - } + public function isSharable(string $path): bool { + return $this->handleAvailability('isSharable', $path); } - /** {@inheritdoc} */ - public function file_exists($path) { - if ($path === '') { - return true; - } - $this->checkAvailability(); - try { - return parent::file_exists($path); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - } + public function getPermissions(string $path): int { + return $this->handleAvailability('getPermissions', $path); } - /** {@inheritdoc} */ - public function filemtime($path) { - $this->checkAvailability(); - try { - return parent::filemtime($path); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); + public function file_exists(string $path): bool { + if ($path === '') { + return true; } + return $this->handleAvailability('file_exists', $path); } - /** {@inheritdoc} */ - public function file_get_contents($path) { - $this->checkAvailability(); - try { - return parent::file_get_contents($path); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - } + public function filemtime(string $path): int|false { + return $this->handleAvailability('filemtime', $path); } - /** {@inheritdoc} */ - public function file_put_contents($path, $data) { - $this->checkAvailability(); - try { - return parent::file_put_contents($path, $data); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - } + public function file_get_contents(string $path): string|false { + return $this->handleAvailability('file_get_contents', $path); } - /** {@inheritdoc} */ - public function unlink($path) { - $this->checkAvailability(); - try { - return parent::unlink($path); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - } + public function file_put_contents(string $path, mixed $data): int|float|false { + return $this->handleAvailability('file_put_contents', $path, $data); } - /** {@inheritdoc} */ - public function rename($source, $target) { - $this->checkAvailability(); - try { - return parent::rename($source, $target); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - } + public function unlink(string $path): bool { + return $this->handleAvailability('unlink', $path); } - /** {@inheritdoc} */ - public function copy($source, $target) { - $this->checkAvailability(); - try { - return parent::copy($source, $target); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - } + public function rename(string $source, string $target): bool { + return $this->handleAvailability('rename', $source, $target); } - /** {@inheritdoc} */ - public function fopen($path, $mode) { - $this->checkAvailability(); - try { - return parent::fopen($path, $mode); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - } + public function copy(string $source, string $target): bool { + return $this->handleAvailability('copy', $source, $target); } - /** {@inheritdoc} */ - public function getMimeType($path) { - $this->checkAvailability(); - try { - return parent::getMimeType($path); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - } + public function fopen(string $path, string $mode) { + return $this->handleAvailability('fopen', $path, $mode); } - /** {@inheritdoc} */ - public function hash($type, $path, $raw = false) { - $this->checkAvailability(); - try { - return parent::hash($type, $path, $raw); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - } + public function getMimeType(string $path): string|false { + return $this->handleAvailability('getMimeType', $path); } - /** {@inheritdoc} */ - public function free_space($path) { - $this->checkAvailability(); - try { - return parent::free_space($path); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - } + public function hash(string $type, string $path, bool $raw = false): string|false { + return $this->handleAvailability('hash', $type, $path, $raw); } - /** {@inheritdoc} */ - public function search($query) { - $this->checkAvailability(); - try { - return parent::search($query); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - } + public function free_space(string $path): int|float|false { + return $this->handleAvailability('free_space', $path); } - /** {@inheritdoc} */ - public function touch($path, $mtime = null) { - $this->checkAvailability(); - try { - return parent::touch($path, $mtime); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - } + public function touch(string $path, ?int $mtime = null): bool { + return $this->handleAvailability('touch', $path, $mtime); } - /** {@inheritdoc} */ - public function getLocalFile($path) { - $this->checkAvailability(); - try { - return parent::getLocalFile($path); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - } + public function getLocalFile(string $path): string|false { + return $this->handleAvailability('getLocalFile', $path); } - /** {@inheritdoc} */ - public function hasUpdated($path, $time) { + public function hasUpdated(string $path, int $time): bool { if (!$this->isAvailable()) { return false; } @@ -391,62 +207,38 @@ class Availability extends Wrapper { } } - /** {@inheritdoc} */ - public function getOwner($path) { + public function getOwner(string $path): string|false { try { return parent::getOwner($path); } catch (StorageNotAvailableException $e) { $this->setUnavailable($e); + return false; } } - /** {@inheritdoc} */ - public function getETag($path) { - $this->checkAvailability(); - try { - return parent::getETag($path); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - } + public function getETag(string $path): string|false { + return $this->handleAvailability('getETag', $path); } - /** {@inheritdoc} */ - public function getDirectDownload($path) { - $this->checkAvailability(); - try { - return parent::getDirectDownload($path); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - } + public function getDirectDownload(string $path): array|false { + return $this->handleAvailability('getDirectDownload', $path); } - /** {@inheritdoc} */ - public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) { - $this->checkAvailability(); - try { - return parent::copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - } + public function copyFromStorage(IStorage $sourceStorage, string $sourceInternalPath, string $targetInternalPath): bool { + return $this->handleAvailability('copyFromStorage', $sourceStorage, $sourceInternalPath, $targetInternalPath); } - /** {@inheritdoc} */ - public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) { - $this->checkAvailability(); - try { - return parent::moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - } + public function moveFromStorage(IStorage $sourceStorage, string $sourceInternalPath, string $targetInternalPath): bool { + return $this->handleAvailability('moveFromStorage', $sourceStorage, $sourceInternalPath, $targetInternalPath); } - /** {@inheritdoc} */ - public function getMetaData($path) { + public function getMetaData(string $path): ?array { $this->checkAvailability(); try { return parent::getMetaData($path); } catch (StorageNotAvailableException $e) { $this->setUnavailable($e); + return null; } } @@ -473,12 +265,13 @@ class Availability extends Wrapper { - public function getDirectoryContent($directory): \Traversable { + public function getDirectoryContent(string $directory): \Traversable { $this->checkAvailability(); try { return parent::getDirectoryContent($directory); } catch (StorageNotAvailableException $e) { $this->setUnavailable($e); + return new \EmptyIterator(); } } } diff --git a/lib/private/Files/Storage/Wrapper/Encoding.php b/lib/private/Files/Storage/Wrapper/Encoding.php index 1bdb0e39f14..92e20cfb3df 100644 --- a/lib/private/Files/Storage/Wrapper/Encoding.php +++ b/lib/private/Files/Storage/Wrapper/Encoding.php @@ -1,35 +1,15 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author J0WI <J0WI@users.noreply.github.com> - * @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 Tigran Mkrtchyan <tigran.mkrtchyan@desy.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\Storage\Wrapper; use OC\Files\Filesystem; use OCP\Cache\CappedMemoryCache; +use OCP\Files\Cache\IScanner; use OCP\Files\Storage\IStorage; use OCP\ICache; @@ -48,19 +28,15 @@ class Encoding extends Wrapper { /** * @param array $parameters */ - public function __construct($parameters) { + public function __construct(array $parameters) { $this->storage = $parameters['storage']; $this->namesCache = new CappedMemoryCache(); } /** * Returns whether the given string is only made of ASCII characters - * - * @param string $str string - * - * @return bool true if the string is all ASCII, false otherwise */ - private function isAscii($str) { + private function isAscii(string $str): bool { return !preg_match('/[\\x80-\\xff]+/', $str); } @@ -69,11 +45,9 @@ class Encoding extends Wrapper { * each form for each path section and returns the correct form. * If no existing path found, returns the path as it was given. * - * @param string $fullPath path to check - * * @return string original or converted path */ - private function findPathToUse($fullPath) { + private function findPathToUse(string $fullPath): string { $cachedPath = $this->namesCache[$fullPath]; if ($cachedPath !== null) { return $cachedPath; @@ -97,12 +71,11 @@ class Encoding extends Wrapper { * Checks whether the last path section of the given path exists in NFC or NFD form * and returns the correct form. If no existing path found, returns null. * - * @param string $basePath base path to check * @param string $lastSection last section of the path to check for NFD/NFC variations * * @return string|null original or converted path, or null if none of the forms was found */ - private function findPathToUseLastSection($basePath, $lastSection) { + private function findPathToUseLastSection(string $basePath, string $lastSection): ?string { $fullPath = $basePath . $lastSection; if ($lastSection === '' || $this->isAscii($lastSection) || $this->storage->file_exists($fullPath)) { $this->namesCache[$fullPath] = $fullPath; @@ -126,13 +99,7 @@ class Encoding extends Wrapper { return null; } - /** - * see https://www.php.net/manual/en/function.mkdir.php - * - * @param string $path - * @return bool - */ - public function mkdir($path) { + public function mkdir(string $path): bool { // note: no conversion here, method should not be called with non-NFC names! $result = $this->storage->mkdir($path); if ($result) { @@ -141,13 +108,7 @@ class Encoding extends Wrapper { return $result; } - /** - * see https://www.php.net/manual/en/function.rmdir.php - * - * @param string $path - * @return bool - */ - public function rmdir($path) { + public function rmdir(string $path): bool { $result = $this->storage->rmdir($this->findPathToUse($path)); if ($result) { unset($this->namesCache[$path]); @@ -155,175 +116,72 @@ class Encoding extends Wrapper { return $result; } - /** - * see https://www.php.net/manual/en/function.opendir.php - * - * @param string $path - * @return resource|false - */ - public function opendir($path) { + public function opendir(string $path) { $handle = $this->storage->opendir($this->findPathToUse($path)); return EncodingDirectoryWrapper::wrap($handle); } - /** - * see https://www.php.net/manual/en/function.is_dir.php - * - * @param string $path - * @return bool - */ - public function is_dir($path) { + public function is_dir(string $path): bool { return $this->storage->is_dir($this->findPathToUse($path)); } - /** - * see https://www.php.net/manual/en/function.is_file.php - * - * @param string $path - * @return bool - */ - public function is_file($path) { + public function is_file(string $path): bool { return $this->storage->is_file($this->findPathToUse($path)); } - /** - * see https://www.php.net/manual/en/function.stat.php - * only the following keys are required in the result: size and mtime - * - * @param string $path - * @return array|bool - */ - public function stat($path) { + public function stat(string $path): array|false { return $this->storage->stat($this->findPathToUse($path)); } - /** - * see https://www.php.net/manual/en/function.filetype.php - * - * @param string $path - * @return string|bool - */ - public function filetype($path) { + public function filetype(string $path): string|false { return $this->storage->filetype($this->findPathToUse($path)); } - /** - * see https://www.php.net/manual/en/function.filesize.php - * The result for filesize when called on a folder is required to be 0 - */ - public function filesize($path): false|int|float { + public function filesize(string $path): int|float|false { return $this->storage->filesize($this->findPathToUse($path)); } - /** - * check if a file can be created in $path - * - * @param string $path - * @return bool - */ - public function isCreatable($path) { + public function isCreatable(string $path): bool { return $this->storage->isCreatable($this->findPathToUse($path)); } - /** - * check if a file can be read - * - * @param string $path - * @return bool - */ - public function isReadable($path) { + public function isReadable(string $path): bool { return $this->storage->isReadable($this->findPathToUse($path)); } - /** - * check if a file can be written to - * - * @param string $path - * @return bool - */ - public function isUpdatable($path) { + public function isUpdatable(string $path): bool { return $this->storage->isUpdatable($this->findPathToUse($path)); } - /** - * check if a file can be deleted - * - * @param string $path - * @return bool - */ - public function isDeletable($path) { + public function isDeletable(string $path): bool { return $this->storage->isDeletable($this->findPathToUse($path)); } - /** - * check if a file can be shared - * - * @param string $path - * @return bool - */ - public function isSharable($path) { + public function isSharable(string $path): bool { return $this->storage->isSharable($this->findPathToUse($path)); } - /** - * get the full permissions of a path. - * Should return a combination of the PERMISSION_ constants defined in lib/public/constants.php - * - * @param string $path - * @return int - */ - public function getPermissions($path) { + public function getPermissions(string $path): int { return $this->storage->getPermissions($this->findPathToUse($path)); } - /** - * see https://www.php.net/manual/en/function.file_exists.php - * - * @param string $path - * @return bool - */ - public function file_exists($path) { + public function file_exists(string $path): bool { return $this->storage->file_exists($this->findPathToUse($path)); } - /** - * see https://www.php.net/manual/en/function.filemtime.php - * - * @param string $path - * @return int|bool - */ - public function filemtime($path) { + public function filemtime(string $path): int|false { return $this->storage->filemtime($this->findPathToUse($path)); } - /** - * see https://www.php.net/manual/en/function.file_get_contents.php - * - * @param string $path - * @return string|false - */ - public function file_get_contents($path) { + public function file_get_contents(string $path): string|false { return $this->storage->file_get_contents($this->findPathToUse($path)); } - /** - * see https://www.php.net/manual/en/function.file_put_contents.php - * - * @param string $path - * @param mixed $data - * @return int|float|false - */ - public function file_put_contents($path, $data) { + public function file_put_contents(string $path, mixed $data): int|float|false { return $this->storage->file_put_contents($this->findPathToUse($path), $data); } - /** - * see https://www.php.net/manual/en/function.unlink.php - * - * @param string $path - * @return bool - */ - public function unlink($path) { + public function unlink(string $path): bool { $result = $this->storage->unlink($this->findPathToUse($path)); if ($result) { unset($this->namesCache[$path]); @@ -331,37 +189,16 @@ class Encoding extends Wrapper { return $result; } - /** - * see https://www.php.net/manual/en/function.rename.php - * - * @param string $source - * @param string $target - * @return bool - */ - public function rename($source, $target) { + public function rename(string $source, string $target): bool { // second name always NFC return $this->storage->rename($this->findPathToUse($source), $this->findPathToUse($target)); } - /** - * see https://www.php.net/manual/en/function.copy.php - * - * @param string $source - * @param string $target - * @return bool - */ - public function copy($source, $target) { + public function copy(string $source, string $target): bool { return $this->storage->copy($this->findPathToUse($source), $this->findPathToUse($target)); } - /** - * see https://www.php.net/manual/en/function.fopen.php - * - * @param string $path - * @param string $mode - * @return resource|bool - */ - public function fopen($path, $mode) { + public function fopen(string $path, string $mode) { $result = $this->storage->fopen($this->findPathToUse($path), $mode); if ($result && $mode !== 'r' && $mode !== 'rb') { unset($this->namesCache[$path]); @@ -369,131 +206,49 @@ class Encoding extends Wrapper { return $result; } - /** - * get the mimetype for a file or folder - * The mimetype for a folder is required to be "httpd/unix-directory" - * - * @param string $path - * @return string|bool - */ - public function getMimeType($path) { + public function getMimeType(string $path): string|false { return $this->storage->getMimeType($this->findPathToUse($path)); } - /** - * see https://www.php.net/manual/en/function.hash.php - * - * @param string $type - * @param string $path - * @param bool $raw - * @return string|bool - */ - public function hash($type, $path, $raw = false) { + public function hash(string $type, string $path, bool $raw = false): string|false { return $this->storage->hash($type, $this->findPathToUse($path), $raw); } - /** - * see https://www.php.net/manual/en/function.free_space.php - * - * @param string $path - * @return int|float|bool - */ - public function free_space($path) { + public function free_space(string $path): int|float|false { return $this->storage->free_space($this->findPathToUse($path)); } - /** - * search for occurrences of $query in file names - * - * @param string $query - * @return array|bool - */ - public function search($query) { - return $this->storage->search($query); - } - - /** - * see https://www.php.net/manual/en/function.touch.php - * If the backend does not support the operation, false should be returned - * - * @param string $path - * @param int $mtime - * @return bool - */ - public function touch($path, $mtime = null) { + public function touch(string $path, ?int $mtime = null): bool { return $this->storage->touch($this->findPathToUse($path), $mtime); } - /** - * get the path to a local version of the file. - * The local version of the file can be temporary and doesn't have to be persistent across requests - * - * @param string $path - * @return string|false - */ - public function getLocalFile($path) { + public function getLocalFile(string $path): string|false { return $this->storage->getLocalFile($this->findPathToUse($path)); } - /** - * check if a file or folder has been updated since $time - * - * @param string $path - * @param int $time - * @return bool - * - * hasUpdated for folders should return at least true if a file inside the folder is add, removed or renamed. - * returning true for other changes in the folder is optional - */ - public function hasUpdated($path, $time) { + public function hasUpdated(string $path, int $time): bool { return $this->storage->hasUpdated($this->findPathToUse($path), $time); } - /** - * get a cache instance for the storage - * - * @param string $path - * @param \OC\Files\Storage\Storage (optional) the storage to pass to the cache - * @return \OC\Files\Cache\Cache - */ - public function getCache($path = '', $storage = null) { + public function getCache(string $path = '', ?IStorage $storage = null): \OCP\Files\Cache\ICache { if (!$storage) { $storage = $this; } return $this->storage->getCache($this->findPathToUse($path), $storage); } - /** - * get a scanner instance for the storage - * - * @param string $path - * @param \OC\Files\Storage\Storage (optional) the storage to pass to the scanner - * @return \OC\Files\Cache\Scanner - */ - public function getScanner($path = '', $storage = null) { + public function getScanner(string $path = '', ?IStorage $storage = null): IScanner { if (!$storage) { $storage = $this; } return $this->storage->getScanner($this->findPathToUse($path), $storage); } - /** - * get the ETag for a file or folder - * - * @param string $path - * @return string|false - */ - public function getETag($path) { + public function getETag(string $path): string|false { return $this->storage->getETag($this->findPathToUse($path)); } - /** - * @param IStorage $sourceStorage - * @param string $sourceInternalPath - * @param string $targetInternalPath - * @return bool - */ - public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) { + public function copyFromStorage(IStorage $sourceStorage, string $sourceInternalPath, string $targetInternalPath): bool { if ($sourceStorage === $this) { return $this->copy($sourceInternalPath, $this->findPathToUse($targetInternalPath)); } @@ -505,13 +260,7 @@ class Encoding extends Wrapper { return $result; } - /** - * @param IStorage $sourceStorage - * @param string $sourceInternalPath - * @param string $targetInternalPath - * @return bool - */ - public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) { + public function moveFromStorage(IStorage $sourceStorage, string $sourceInternalPath, string $targetInternalPath): bool { if ($sourceStorage === $this) { $result = $this->rename($sourceInternalPath, $this->findPathToUse($targetInternalPath)); if ($result) { @@ -529,13 +278,15 @@ class Encoding extends Wrapper { return $result; } - public function getMetaData($path) { + public function getMetaData(string $path): ?array { $entry = $this->storage->getMetaData($this->findPathToUse($path)); - $entry['name'] = trim(Filesystem::normalizePath($entry['name']), '/'); + if ($entry !== null) { + $entry['name'] = trim(Filesystem::normalizePath($entry['name']), '/'); + } return $entry; } - public function getDirectoryContent($directory): \Traversable { + public function getDirectoryContent(string $directory): \Traversable { $entries = $this->storage->getDirectoryContent($this->findPathToUse($directory)); foreach ($entries as $entry) { $entry['name'] = trim(Filesystem::normalizePath($entry['name']), '/'); diff --git a/lib/private/Files/Storage/Wrapper/EncodingDirectoryWrapper.php b/lib/private/Files/Storage/Wrapper/EncodingDirectoryWrapper.php index 3cda399f22d..0a90b49f0f1 100644 --- a/lib/private/Files/Storage/Wrapper/EncodingDirectoryWrapper.php +++ b/lib/private/Files/Storage/Wrapper/EncodingDirectoryWrapper.php @@ -1,26 +1,9 @@ <?php + /** - * @copyright Copyright (c) 2021, Nextcloud GmbH. - * - * @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: 2021 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-only */ - namespace OC\Files\Storage\Wrapper; use Icewind\Streams\DirectoryWrapper; @@ -30,11 +13,7 @@ use OC\Files\Filesystem; * Normalize file names while reading directory entries */ class EncodingDirectoryWrapper extends DirectoryWrapper { - /** - * @psalm-suppress ImplementedReturnTypeMismatch Until return type is fixed upstream - * @return string|false - */ - public function dir_readdir() { + public function dir_readdir(): string|false { $file = readdir($this->source); if ($file !== false && $file !== '.' && $file !== '..') { $file = trim(Filesystem::normalizePath($file), '/'); @@ -45,7 +24,6 @@ class EncodingDirectoryWrapper extends DirectoryWrapper { /** * @param resource $source - * @param callable $filter * @return resource|false */ public static function wrap($source) { diff --git a/lib/private/Files/Storage/Wrapper/Encryption.php b/lib/private/Files/Storage/Wrapper/Encryption.php index 7ce4338256f..58bd4dfddcf 100644 --- a/lib/private/Files/Storage/Wrapper/Encryption.php +++ b/lib/private/Files/Storage/Wrapper/Encryption.php @@ -1,57 +1,29 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Arthur Schiwon <blizzz@arthur-schiwon.de> - * @author Bjoern Schiessle <bjoern@schiessle.org> - * @author Björn Schießle <bjoern@schiessle.org> - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author J0WI <J0WI@users.noreply.github.com> - * @author jknockaert <jasper@knockaert.nl> - * @author Joas Schilling <coding@schilljs.com> - * @author Lukas Reschke <lukas@statuscode.ch> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Piotr M <mrow4a@yahoo.com> - * @author Robin Appelman <robin@icewind.nl> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * @author Tigran Mkrtchyan <tigran.mkrtchyan@desy.de> - * @author Vincent Petry <vincent@nextcloud.com> - * @author Richard Steinmetz <richard@steinmetz.cloud> - * - * @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\Storage\Wrapper; use OC\Encryption\Exceptions\ModuleDoesNotExistsException; -use OC\Encryption\Update; use OC\Encryption\Util; use OC\Files\Cache\CacheEntry; use OC\Files\Filesystem; use OC\Files\Mount\Manager; use OC\Files\ObjectStore\ObjectStoreStorage; +use OC\Files\Storage\Common; use OC\Files\Storage\LocalTempFileTrait; use OC\Memcache\ArrayCache; use OCP\Cache\CappedMemoryCache; -use OCP\Encryption\Exceptions\GenericEncryptionException; +use OCP\Encryption\Exceptions\InvalidHeaderException; use OCP\Encryption\IFile; use OCP\Encryption\IManager; use OCP\Encryption\Keys\IStorage; +use OCP\Files; use OCP\Files\Cache\ICacheEntry; +use OCP\Files\GenericFileException; use OCP\Files\Mount\IMountPoint; use OCP\Files\Storage; use Psr\Log\LoggerInterface; @@ -59,90 +31,42 @@ use Psr\Log\LoggerInterface; class Encryption extends Wrapper { use LocalTempFileTrait; - /** @var string */ - private $mountPoint; - - /** @var \OC\Encryption\Util */ - private $util; - - /** @var \OCP\Encryption\IManager */ - private $encryptionManager; - - private LoggerInterface $logger; - - /** @var string */ - private $uid; - - /** @var array */ - protected $unencryptedSize; - - /** @var \OCP\Encryption\IFile */ - private $fileHelper; - - /** @var IMountPoint */ - private $mount; - - /** @var IStorage */ - private $keyStorage; - - /** @var Update */ - private $update; - - /** @var Manager */ - private $mountManager; - - /** @var array remember for which path we execute the repair step to avoid recursions */ - private $fixUnencryptedSizeOf = []; - - /** @var ArrayCache */ - private $arrayCache; - + private string $mountPoint; + protected array $unencryptedSize = []; + private IMountPoint $mount; + /** for which path we execute the repair step to avoid recursions */ + private array $fixUnencryptedSizeOf = []; /** @var CappedMemoryCache<bool> */ private CappedMemoryCache $encryptedPaths; - - private $enabled = true; + private bool $enabled = true; /** * @param array $parameters */ public function __construct( - $parameters, - IManager $encryptionManager = null, - Util $util = null, - LoggerInterface $logger = null, - IFile $fileHelper = null, - $uid = null, - IStorage $keyStorage = null, - Update $update = null, - Manager $mountManager = null, - ArrayCache $arrayCache = null + array $parameters, + private IManager $encryptionManager, + private Util $util, + private LoggerInterface $logger, + private IFile $fileHelper, + private ?string $uid, + private IStorage $keyStorage, + private Manager $mountManager, + private ArrayCache $arrayCache, ) { $this->mountPoint = $parameters['mountPoint']; $this->mount = $parameters['mount']; - $this->encryptionManager = $encryptionManager; - $this->util = $util; - $this->logger = $logger; - $this->uid = $uid; - $this->fileHelper = $fileHelper; - $this->keyStorage = $keyStorage; - $this->unencryptedSize = []; - $this->update = $update; - $this->mountManager = $mountManager; - $this->arrayCache = $arrayCache; $this->encryptedPaths = new CappedMemoryCache(); parent::__construct($parameters); } - /** - * see https://www.php.net/manual/en/function.filesize.php - * The result for filesize when called on a folder is required to be 0 - */ - public function filesize($path): false|int|float { + public function filesize(string $path): int|float|false { $fullPath = $this->getFullPath($path); $info = $this->getCache()->get($path); if ($info === false) { - return false; + /* Pass call to wrapped storage, it may be a special file like a part file */ + return $this->storage->filesize($path); } if (isset($this->unencryptedSize[$fullPath])) { $size = $this->unencryptedSize[$fullPath]; @@ -180,11 +104,6 @@ class Encryption extends Wrapper { return $this->storage->filesize($path); } - /** - * @param string $path - * @param array $data - * @return array - */ private function modifyMetaData(string $path, array $data): array { $fullPath = $this->getFullPath($path); $info = $this->getCache()->get($path); @@ -208,7 +127,7 @@ class Encryption extends Wrapper { return $data; } - public function getMetaData($path) { + public function getMetaData(string $path): ?array { $data = $this->storage->getMetaData($path); if (is_null($data)) { return null; @@ -216,24 +135,18 @@ class Encryption extends Wrapper { return $this->modifyMetaData($path, $data); } - public function getDirectoryContent($directory): \Traversable { + public function getDirectoryContent(string $directory): \Traversable { $parent = rtrim($directory, '/'); foreach ($this->getWrapperStorage()->getDirectoryContent($directory) as $data) { yield $this->modifyMetaData($parent . '/' . $data['name'], $data); } } - /** - * see https://www.php.net/manual/en/function.file_get_contents.php - * - * @param string $path - * @return string|false - */ - public function file_get_contents($path) { + public function file_get_contents(string $path): string|false { $encryptionModule = $this->getEncryptionModule($path); if ($encryptionModule) { - $handle = $this->fopen($path, "r"); + $handle = $this->fopen($path, 'r'); if (!$handle) { return false; } @@ -244,14 +157,7 @@ class Encryption extends Wrapper { return $this->storage->file_get_contents($path); } - /** - * see https://www.php.net/manual/en/function.file_put_contents.php - * - * @param string $path - * @param mixed $data - * @return int|false - */ - public function file_put_contents($path, $data) { + public function file_put_contents(string $path, mixed $data): int|float|false { // file put content will always be translated to a stream write $handle = $this->fopen($path, 'w'); if (is_resource($handle)) { @@ -263,13 +169,7 @@ class Encryption extends Wrapper { return false; } - /** - * see https://www.php.net/manual/en/function.unlink.php - * - * @param string $path - * @return bool - */ - public function unlink($path) { + public function unlink(string $path): bool { $fullPath = $this->getFullPath($path); if ($this->util->isExcluded($fullPath)) { return $this->storage->unlink($path); @@ -283,21 +183,14 @@ class Encryption extends Wrapper { return $this->storage->unlink($path); } - /** - * see https://www.php.net/manual/en/function.rename.php - * - * @param string $source - * @param string $target - * @return bool - */ - public function rename($source, $target) { + public function rename(string $source, string $target): bool { $result = $this->storage->rename($source, $target); - if ($result && + if ($result // versions always use the keys from the original file, so we can skip // this step for versions - $this->isVersion($target) === false && - $this->encryptionManager->isEnabled()) { + && $this->isVersion($target) === false + && $this->encryptionManager->isEnabled()) { $sourcePath = $this->getFullPath($source); if (!$this->util->isExcluded($sourcePath)) { $targetPath = $this->getFullPath($target); @@ -315,18 +208,12 @@ class Encryption extends Wrapper { return $result; } - /** - * see https://www.php.net/manual/en/function.rmdir.php - * - * @param string $path - * @return bool - */ - public function rmdir($path) { + public function rmdir(string $path): bool { $result = $this->storage->rmdir($path); $fullPath = $this->getFullPath($path); - if ($result && - $this->util->isExcluded($fullPath) === false && - $this->encryptionManager->isEnabled() + if ($result + && $this->util->isExcluded($fullPath) === false + && $this->encryptionManager->isEnabled() ) { $this->keyStorage->deleteAllFileKeys($fullPath); } @@ -334,20 +221,14 @@ class Encryption extends Wrapper { return $result; } - /** - * check if a file can be read - * - * @param string $path - * @return bool - */ - public function isReadable($path) { + public function isReadable(string $path): bool { $isReadable = true; $metaData = $this->getMetaData($path); if ( - !$this->is_dir($path) && - isset($metaData['encrypted']) && - $metaData['encrypted'] === true + !$this->is_dir($path) + && isset($metaData['encrypted']) + && $metaData['encrypted'] === true ) { $fullPath = $this->getFullPath($path); $module = $this->getEncryptionModule($path); @@ -357,13 +238,7 @@ class Encryption extends Wrapper { return $this->storage->isReadable($path) && $isReadable; } - /** - * see https://www.php.net/manual/en/function.copy.php - * - * @param string $source - * @param string $target - */ - public function copy($source, $target): bool { + public function copy(string $source, string $target): bool { $sourcePath = $this->getFullPath($source); if ($this->util->isExcluded($sourcePath)) { @@ -376,16 +251,7 @@ class Encryption extends Wrapper { return $this->copyFromStorage($this, $source, $target); } - /** - * see https://www.php.net/manual/en/function.fopen.php - * - * @param string $path - * @param string $mode - * @return resource|bool - * @throws GenericEncryptionException - * @throws ModuleDoesNotExistsException - */ - public function fopen($path, $mode) { + public function fopen(string $path, string $mode) { // check if the file is stored in the array cache, this means that we // copy a file over to the versions folder, in this case we don't want to // decrypt it @@ -438,10 +304,8 @@ class Encryption extends Wrapper { // if we update a encrypted file with a un-encrypted one we change the db flag if ($targetIsEncrypted && $encryptionEnabled === false) { $cache = $this->storage->getCache(); - if ($cache) { - $entry = $cache->get($path); - $cache->update($entry->getId(), ['encrypted' => 0]); - } + $entry = $cache->get($path); + $cache->update($entry->getId(), ['encrypted' => 0]); } if ($encryptionEnabled) { // if $encryptionModuleId is empty, the default module will be used @@ -456,7 +320,7 @@ class Encryption extends Wrapper { if (!empty($encryptionModuleId)) { $encryptionModule = $this->encryptionManager->getEncryptionModule($encryptionModuleId); $shouldEncrypt = true; - } elseif (empty($encryptionModuleId) && $info['encrypted'] === true) { + } elseif ($info !== false && $info['encrypted'] === true) { // we come from a old installation. No header and/or no module defined // but the file is encrypted. In this case we need to use the // OC_DEFAULT_MODULE to read the file @@ -482,6 +346,16 @@ class Encryption extends Wrapper { if ($shouldEncrypt === true && $encryptionModule !== null) { $this->encryptedPaths->set($this->util->stripPartialFileExtension($path), true); $headerSize = $this->getHeaderSize($path); + if ($mode === 'r' && $headerSize === 0) { + $firstBlock = $this->readFirstBlock($path); + if (!$firstBlock) { + throw new InvalidHeaderException("Unable to get header block for $path"); + } elseif (!str_starts_with($firstBlock, Util::HEADER_START)) { + throw new InvalidHeaderException("Unable to get header size for $path, file doesn't start with encryption header"); + } else { + throw new InvalidHeaderException("Unable to get header size for $path, even though file does start with encryption header"); + } + } $source = $this->storage->fopen($path, $mode); if (!is_resource($source)) { return false; @@ -499,7 +373,7 @@ class Encryption extends Wrapper { /** - * perform some plausibility checks if the the unencrypted size is correct. + * perform some plausibility checks if the unencrypted size is correct. * If not, we calculate the correct unencrypted size and return it * * @param string $path internal path relative to the storage root @@ -511,9 +385,9 @@ class Encryption extends Wrapper { $size = $this->storage->filesize($path); $result = $unencryptedSize; - if ($unencryptedSize < 0 || - ($size > 0 && $unencryptedSize === $size) || - $unencryptedSize > $size + if ($unencryptedSize < 0 + || ($size > 0 && $unencryptedSize === $size) + || $unencryptedSize > $size ) { // check if we already calculate the unencrypted size for the // given path to avoid recursions @@ -537,10 +411,8 @@ class Encryption extends Wrapper { * @param string $path internal path relative to the storage root * @param int $size size of the physical file * @param int $unencryptedSize size of the unencrypted file - * - * @return int calculated unencrypted size */ - protected function fixUnencryptedSize(string $path, int $size, int $unencryptedSize): int { + protected function fixUnencryptedSize(string $path, int $size, int $unencryptedSize): int|float { $headerSize = $this->getHeaderSize($path); $header = $this->getHeader($path); $encryptionModule = $this->getEncryptionModule($path); @@ -609,12 +481,10 @@ class Encryption extends Wrapper { // write to cache if applicable $cache = $this->storage->getCache(); - if ($cache) { - $entry = $cache->get($path); - $cache->update($entry['fileid'], [ - 'unencrypted_size' => $newUnencryptedSize - ]); - } + $entry = $cache->get($path); + $cache->update($entry['fileid'], [ + 'unencrypted_size' => $newUnencryptedSize + ]); return $newUnencryptedSize; } @@ -628,7 +498,7 @@ class Encryption extends Wrapper { * This is required as stream_read only returns smaller chunks of data when the stream fetches from a * remote storage over the internet and it does not care about the given $blockSize. * - * @param handle the stream to read from + * @param resource $handle the stream to read from * @param int $blockSize Length of requested data block in bytes * @return string Data fetched from stream. */ @@ -646,19 +516,12 @@ class Encryption extends Wrapper { return $data; } - /** - * @param Storage\IStorage $sourceStorage - * @param string $sourceInternalPath - * @param string $targetInternalPath - * @param bool $preserveMtime - * @return bool - */ public function moveFromStorage( Storage\IStorage $sourceStorage, - $sourceInternalPath, - $targetInternalPath, - $preserveMtime = true - ) { + string $sourceInternalPath, + string $targetInternalPath, + $preserveMtime = true, + ): bool { if ($sourceStorage === $this) { return $this->rename($sourceInternalPath, $targetInternalPath); } @@ -675,31 +538,34 @@ class Encryption extends Wrapper { $result = $this->copyBetweenStorage($sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime, true); if ($result) { - if ($sourceStorage->is_dir($sourceInternalPath)) { - $result &= $sourceStorage->rmdir($sourceInternalPath); - } else { - $result &= $sourceStorage->unlink($sourceInternalPath); + $setPreserveCacheOnDelete = $sourceStorage->instanceOfStorage(ObjectStoreStorage::class) && !$this->instanceOfStorage(ObjectStoreStorage::class); + if ($setPreserveCacheOnDelete) { + /** @var ObjectStoreStorage $sourceStorage */ + $sourceStorage->setPreserveCacheOnDelete(true); + } + try { + if ($sourceStorage->is_dir($sourceInternalPath)) { + $result = $sourceStorage->rmdir($sourceInternalPath); + } else { + $result = $sourceStorage->unlink($sourceInternalPath); + } + } finally { + if ($setPreserveCacheOnDelete) { + /** @var ObjectStoreStorage $sourceStorage */ + $sourceStorage->setPreserveCacheOnDelete(false); + } } } return $result; } - - /** - * @param Storage\IStorage $sourceStorage - * @param string $sourceInternalPath - * @param string $targetInternalPath - * @param bool $preserveMtime - * @param bool $isRename - * @return bool - */ public function copyFromStorage( Storage\IStorage $sourceStorage, - $sourceInternalPath, - $targetInternalPath, + string $sourceInternalPath, + string $targetInternalPath, $preserveMtime = false, - $isRename = false - ) { + $isRename = false, + ): bool { // TODO clean this up once the underlying moveFromStorage in OC\Files\Storage\Wrapper\Common is fixed: // - call $this->storage->copyFromStorage() instead of $this->copyBetweenStorage // - copy the file cache update from $this->copyBetweenStorage to this method @@ -711,20 +577,14 @@ class Encryption extends Wrapper { /** * Update the encrypted cache version in the database - * - * @param Storage\IStorage $sourceStorage - * @param string $sourceInternalPath - * @param string $targetInternalPath - * @param bool $isRename - * @param bool $keepEncryptionVersion */ private function updateEncryptedVersion( Storage\IStorage $sourceStorage, - $sourceInternalPath, - $targetInternalPath, - $isRename, - $keepEncryptionVersion - ) { + string $sourceInternalPath, + string $targetInternalPath, + bool $isRename, + bool $keepEncryptionVersion, + ): void { $isEncrypted = $this->encryptionManager->isEnabled() && $this->shouldEncrypt($targetInternalPath); $cacheInformation = [ 'encrypted' => $isEncrypted, @@ -764,26 +624,19 @@ class Encryption extends Wrapper { /** * copy file between two storages - * - * @param Storage\IStorage $sourceStorage - * @param string $sourceInternalPath - * @param string $targetInternalPath - * @param bool $preserveMtime - * @param bool $isRename - * @return bool * @throws \Exception */ private function copyBetweenStorage( Storage\IStorage $sourceStorage, - $sourceInternalPath, - $targetInternalPath, - $preserveMtime, - $isRename - ) { + string $sourceInternalPath, + string $targetInternalPath, + bool $preserveMtime, + bool $isRename, + ): bool { // for versions we have nothing to do, because versions should always use the // key from the original file. Just create a 1:1 copy and done - if ($this->isVersion($targetInternalPath) || - $this->isVersion($sourceInternalPath)) { + if ($this->isVersion($targetInternalPath) + || $this->isVersion($sourceInternalPath)) { // remember that we try to create a version so that we can detect it during // fopen($sourceInternalPath) and by-pass the encryption in order to // create a 1:1 copy of the file @@ -806,9 +659,8 @@ class Encryption extends Wrapper { // first copy the keys that we reuse the existing file key on the target location // and don't create a new one which would break versions for example. - $mount = $this->mountManager->findByStorageId($sourceStorage->getId()); - if (count($mount) === 1) { - $mountPoint = $mount[0]->getMountPoint(); + if ($sourceStorage->instanceOfStorage(Common::class) && $sourceStorage->getMountOption('mount_point')) { + $mountPoint = $sourceStorage->getMountOption('mount_point'); $source = $mountPoint . '/' . $sourceInternalPath; $target = $this->getFullPath($targetInternalPath); $this->copyKeys($source, $target); @@ -824,9 +676,9 @@ class Encryption extends Wrapper { $result = true; } if (is_resource($dh)) { - while ($result and ($file = readdir($dh)) !== false) { + while ($result && ($file = readdir($dh)) !== false) { if (!Filesystem::isIgnoredDir($file)) { - $result &= $this->copyFromStorage($sourceStorage, $sourceInternalPath . '/' . $file, $targetInternalPath . '/' . $file, false, $isRename); + $result = $this->copyFromStorage($sourceStorage, $sourceInternalPath . '/' . $file, $targetInternalPath . '/' . $file, $preserveMtime, $isRename); } } } @@ -834,12 +686,16 @@ class Encryption extends Wrapper { try { $source = $sourceStorage->fopen($sourceInternalPath, 'r'); $target = $this->fopen($targetInternalPath, 'w'); - [, $result] = \OC_Helper::streamCopy($source, $target); + if ($source === false || $target === false) { + $result = false; + } else { + [, $result] = Files::streamCopy($source, $target, true); + } } finally { - if (is_resource($source)) { + if (isset($source) && $source !== false) { fclose($source); } - if (is_resource($target)) { + if (isset($target) && $target !== false) { fclose($target); } } @@ -858,7 +714,7 @@ class Encryption extends Wrapper { return (bool)$result; } - public function getLocalFile($path) { + public function getLocalFile(string $path): string|false { if ($this->encryptionManager->isEnabled()) { $cachedFile = $this->getCachedFile($path); if (is_string($cachedFile)) { @@ -868,14 +724,14 @@ class Encryption extends Wrapper { return $this->storage->getLocalFile($path); } - public function isLocal() { + public function isLocal(): bool { if ($this->encryptionManager->isEnabled()) { return false; } return $this->storage->isLocal(); } - public function stat($path) { + public function stat(string $path): array|false { $stat = $this->storage->stat($path); if (!$stat) { return false; @@ -887,8 +743,11 @@ class Encryption extends Wrapper { return $stat; } - public function hash($type, $path, $raw = false) { + public function hash(string $type, string $path, bool $raw = false): string|false { $fh = $this->fopen($path, 'rb'); + if ($fh === false) { + return false; + } $ctx = hash_init($type); hash_update_stream($ctx, $fh); fclose($fh); @@ -901,21 +760,21 @@ class Encryption extends Wrapper { * @param string $path relative to mount point * @return string full path including mount point */ - protected function getFullPath($path) { + protected function getFullPath(string $path): string { return Filesystem::normalizePath($this->mountPoint . '/' . $path); } /** * read first block of encrypted file, typically this will contain the * encryption header - * - * @param string $path - * @return string */ - protected function readFirstBlock($path) { + protected function readFirstBlock(string $path): string { $firstBlock = ''; if ($this->storage->is_file($path)) { $handle = $this->storage->fopen($path, 'r'); + if ($handle === false) { + return ''; + } $firstBlock = fread($handle, $this->util->getHeaderSize()); fclose($handle); } @@ -924,11 +783,8 @@ class Encryption extends Wrapper { /** * return header size of given file - * - * @param string $path - * @return int */ - protected function getHeaderSize($path) { + protected function getHeaderSize(string $path): int { $headerSize = 0; $realFile = $this->util->stripPartialFileExtension($path); if ($this->storage->is_file($realFile)) { @@ -945,11 +801,8 @@ class Encryption extends Wrapper { /** * read header from file - * - * @param string $path - * @return array */ - protected function getHeader($path) { + protected function getHeader(string $path): array { $realFile = $this->util->stripPartialFileExtension($path); $exists = $this->storage->is_file($realFile); if ($exists) { @@ -981,12 +834,10 @@ class Encryption extends Wrapper { /** * read encryption module needed to read/write the file located at $path * - * @param string $path - * @return null|\OCP\Encryption\IEncryptionModule * @throws ModuleDoesNotExistsException * @throws \Exception */ - protected function getEncryptionModule($path) { + protected function getEncryptionModule(string $path): ?\OCP\Encryption\IEncryptionModule { $encryptionModule = null; $header = $this->getHeader($path); $encryptionModuleId = $this->util->getEncryptionModuleId($header); @@ -1002,11 +853,7 @@ class Encryption extends Wrapper { return $encryptionModule; } - /** - * @param string $path - * @param int $unencryptedSize - */ - public function updateUnencryptedSize($path, $unencryptedSize) { + public function updateUnencryptedSize(string $path, int|float $unencryptedSize): void { $this->unencryptedSize[$path] = $unencryptedSize; } @@ -1015,9 +862,8 @@ class Encryption extends Wrapper { * * @param string $source path relative to data/ * @param string $target path relative to data/ - * @return bool */ - protected function copyKeys($source, $target) { + protected function copyKeys(string $source, string $target): bool { if (!$this->util->isExcluded($source)) { return $this->keyStorage->copyKeys($source, $target); } @@ -1027,22 +873,16 @@ class Encryption extends Wrapper { /** * check if path points to a files version - * - * @param $path - * @return bool */ - protected function isVersion($path) { + protected function isVersion(string $path): bool { $normalized = Filesystem::normalizePath($path); return substr($normalized, 0, strlen('/files_versions/')) === '/files_versions/'; } /** * check if the given storage should be encrypted or not - * - * @param $path - * @return bool */ - protected function shouldEncrypt($path) { + protected function shouldEncrypt(string $path): bool { $fullPath = $this->getFullPath($path); $mountPointConfig = $this->mount->getOption('encrypt', true); if ($mountPointConfig === false) { @@ -1062,10 +902,13 @@ class Encryption extends Wrapper { return $encryptionModule->shouldEncrypt($fullPath); } - public function writeStream(string $path, $stream, int $size = null): int { + public function writeStream(string $path, $stream, ?int $size = null): int { // always fall back to fopen $target = $this->fopen($path, 'w'); - [$count, $result] = \OC_Helper::streamCopy($stream, $target); + if ($target === false) { + throw new GenericFileException("Failed to open $path for writing"); + } + [$count, $result] = Files::streamCopy($stream, $target, true); fclose($stream); fclose($target); @@ -1084,11 +927,20 @@ class Encryption extends Wrapper { /** * Allow temporarily disabling the wrapper - * - * @param bool $enabled - * @return void */ public function setEnabled(bool $enabled): void { $this->enabled = $enabled; } + + /** + * Check if the on-disk data for a file has a valid encrypted header + * + * @param string $path + * @return bool + */ + public function hasValidHeader(string $path): bool { + $firstBlock = $this->readFirstBlock($path); + $header = $this->util->parseRawHeader($firstBlock); + return (count($header) > 0); + } } diff --git a/lib/private/Files/Storage/Wrapper/Jail.php b/lib/private/Files/Storage/Wrapper/Jail.php index 592acd418ec..38b113cef88 100644 --- a/lib/private/Files/Storage/Wrapper/Jail.php +++ b/lib/private/Files/Storage/Wrapper/Jail.php @@ -1,36 +1,20 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author J0WI <J0WI@users.noreply.github.com> - * @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 Tigran Mkrtchyan <tigran.mkrtchyan@desy.de> - * - * @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\Storage\Wrapper; use OC\Files\Cache\Wrapper\CacheJail; use OC\Files\Cache\Wrapper\JailPropagator; +use OC\Files\Cache\Wrapper\JailWatcher; use OC\Files\Filesystem; +use OCP\Files; +use OCP\Files\Cache\ICache; +use OCP\Files\Cache\IPropagator; +use OCP\Files\Cache\IWatcher; use OCP\Files\Storage\IStorage; use OCP\Files\Storage\IWriteStreamStorage; use OCP\Lock\ILockingProvider; @@ -47,29 +31,29 @@ class Jail extends Wrapper { protected $rootPath; /** - * @param array $arguments ['storage' => $storage, 'root' => $root] + * @param array $parameters ['storage' => $storage, 'root' => $root] * * $storage: The storage that will be wrapper * $root: The folder in the wrapped storage that will become the root folder of the wrapped storage */ - public function __construct($arguments) { - parent::__construct($arguments); - $this->rootPath = $arguments['root']; + public function __construct(array $parameters) { + parent::__construct($parameters); + $this->rootPath = $parameters['root']; } - public function getUnjailedPath($path) { + public function getUnjailedPath(string $path): string { return trim(Filesystem::normalizePath($this->rootPath . '/' . $path), '/'); } /** * This is separate from Wrapper::getWrapperStorage so we can get the jailed storage consistently even if the jail is inside another wrapper */ - public function getUnjailedStorage() { + public function getUnjailedStorage(): IStorage { return $this->storage; } - public function getJailedPath($path) { + public function getJailedPath(string $path): ?string { $root = rtrim($this->rootPath, '/') . '/'; if ($path !== $this->rootPath && !str_starts_with($path, $root)) { @@ -80,429 +64,178 @@ class Jail extends Wrapper { } } - public function getId() { + public function getId(): string { return parent::getId(); } - /** - * see https://www.php.net/manual/en/function.mkdir.php - * - * @param string $path - * @return bool - */ - public function mkdir($path) { + public function mkdir(string $path): bool { return $this->getWrapperStorage()->mkdir($this->getUnjailedPath($path)); } - /** - * see https://www.php.net/manual/en/function.rmdir.php - * - * @param string $path - * @return bool - */ - public function rmdir($path) { + public function rmdir(string $path): bool { return $this->getWrapperStorage()->rmdir($this->getUnjailedPath($path)); } - /** - * see https://www.php.net/manual/en/function.opendir.php - * - * @param string $path - * @return resource|false - */ - public function opendir($path) { + public function opendir(string $path) { return $this->getWrapperStorage()->opendir($this->getUnjailedPath($path)); } - /** - * see https://www.php.net/manual/en/function.is_dir.php - * - * @param string $path - * @return bool - */ - public function is_dir($path) { + public function is_dir(string $path): bool { return $this->getWrapperStorage()->is_dir($this->getUnjailedPath($path)); } - /** - * see https://www.php.net/manual/en/function.is_file.php - * - * @param string $path - * @return bool - */ - public function is_file($path) { + public function is_file(string $path): bool { return $this->getWrapperStorage()->is_file($this->getUnjailedPath($path)); } - /** - * see https://www.php.net/manual/en/function.stat.php - * only the following keys are required in the result: size and mtime - * - * @param string $path - * @return array|bool - */ - public function stat($path) { + public function stat(string $path): array|false { return $this->getWrapperStorage()->stat($this->getUnjailedPath($path)); } - /** - * see https://www.php.net/manual/en/function.filetype.php - * - * @param string $path - * @return bool - */ - public function filetype($path) { + public function filetype(string $path): string|false { return $this->getWrapperStorage()->filetype($this->getUnjailedPath($path)); } - /** - * see https://www.php.net/manual/en/function.filesize.php - * The result for filesize when called on a folder is required to be 0 - */ - public function filesize($path): false|int|float { + public function filesize(string $path): int|float|false { return $this->getWrapperStorage()->filesize($this->getUnjailedPath($path)); } - /** - * check if a file can be created in $path - * - * @param string $path - * @return bool - */ - public function isCreatable($path) { + public function isCreatable(string $path): bool { return $this->getWrapperStorage()->isCreatable($this->getUnjailedPath($path)); } - /** - * check if a file can be read - * - * @param string $path - * @return bool - */ - public function isReadable($path) { + public function isReadable(string $path): bool { return $this->getWrapperStorage()->isReadable($this->getUnjailedPath($path)); } - /** - * check if a file can be written to - * - * @param string $path - * @return bool - */ - public function isUpdatable($path) { + public function isUpdatable(string $path): bool { return $this->getWrapperStorage()->isUpdatable($this->getUnjailedPath($path)); } - /** - * check if a file can be deleted - * - * @param string $path - * @return bool - */ - public function isDeletable($path) { + public function isDeletable(string $path): bool { return $this->getWrapperStorage()->isDeletable($this->getUnjailedPath($path)); } - /** - * check if a file can be shared - * - * @param string $path - * @return bool - */ - public function isSharable($path) { + public function isSharable(string $path): bool { return $this->getWrapperStorage()->isSharable($this->getUnjailedPath($path)); } - /** - * get the full permissions of a path. - * Should return a combination of the PERMISSION_ constants defined in lib/public/constants.php - * - * @param string $path - * @return int - */ - public function getPermissions($path) { + public function getPermissions(string $path): int { return $this->getWrapperStorage()->getPermissions($this->getUnjailedPath($path)); } - /** - * see https://www.php.net/manual/en/function.file_exists.php - * - * @param string $path - * @return bool - */ - public function file_exists($path) { + public function file_exists(string $path): bool { return $this->getWrapperStorage()->file_exists($this->getUnjailedPath($path)); } - /** - * see https://www.php.net/manual/en/function.filemtime.php - * - * @param string $path - * @return int|bool - */ - public function filemtime($path) { + public function filemtime(string $path): int|false { return $this->getWrapperStorage()->filemtime($this->getUnjailedPath($path)); } - /** - * see https://www.php.net/manual/en/function.file_get_contents.php - * - * @param string $path - * @return string|false - */ - public function file_get_contents($path) { + public function file_get_contents(string $path): string|false { return $this->getWrapperStorage()->file_get_contents($this->getUnjailedPath($path)); } - /** - * see https://www.php.net/manual/en/function.file_put_contents.php - * - * @param string $path - * @param mixed $data - * @return int|float|false - */ - public function file_put_contents($path, $data) { + public function file_put_contents(string $path, mixed $data): int|float|false { return $this->getWrapperStorage()->file_put_contents($this->getUnjailedPath($path), $data); } - /** - * see https://www.php.net/manual/en/function.unlink.php - * - * @param string $path - * @return bool - */ - public function unlink($path) { + public function unlink(string $path): bool { return $this->getWrapperStorage()->unlink($this->getUnjailedPath($path)); } - /** - * see https://www.php.net/manual/en/function.rename.php - * - * @param string $source - * @param string $target - * @return bool - */ - public function rename($source, $target) { + public function rename(string $source, string $target): bool { return $this->getWrapperStorage()->rename($this->getUnjailedPath($source), $this->getUnjailedPath($target)); } - /** - * see https://www.php.net/manual/en/function.copy.php - * - * @param string $source - * @param string $target - * @return bool - */ - public function copy($source, $target) { + public function copy(string $source, string $target): bool { return $this->getWrapperStorage()->copy($this->getUnjailedPath($source), $this->getUnjailedPath($target)); } - /** - * see https://www.php.net/manual/en/function.fopen.php - * - * @param string $path - * @param string $mode - * @return resource|bool - */ - public function fopen($path, $mode) { + public function fopen(string $path, string $mode) { return $this->getWrapperStorage()->fopen($this->getUnjailedPath($path), $mode); } - /** - * get the mimetype for a file or folder - * The mimetype for a folder is required to be "httpd/unix-directory" - * - * @param string $path - * @return string|bool - */ - public function getMimeType($path) { + public function getMimeType(string $path): string|false { return $this->getWrapperStorage()->getMimeType($this->getUnjailedPath($path)); } - /** - * see https://www.php.net/manual/en/function.hash.php - * - * @param string $type - * @param string $path - * @param bool $raw - * @return string|bool - */ - public function hash($type, $path, $raw = false) { + public function hash(string $type, string $path, bool $raw = false): string|false { return $this->getWrapperStorage()->hash($type, $this->getUnjailedPath($path), $raw); } - /** - * see https://www.php.net/manual/en/function.free_space.php - * - * @param string $path - * @return int|float|bool - */ - public function free_space($path) { + public function free_space(string $path): int|float|false { return $this->getWrapperStorage()->free_space($this->getUnjailedPath($path)); } - /** - * search for occurrences of $query in file names - * - * @param string $query - * @return array|bool - */ - public function search($query) { - return $this->getWrapperStorage()->search($query); - } - - /** - * see https://www.php.net/manual/en/function.touch.php - * If the backend does not support the operation, false should be returned - * - * @param string $path - * @param int $mtime - * @return bool - */ - public function touch($path, $mtime = null) { + public function touch(string $path, ?int $mtime = null): bool { return $this->getWrapperStorage()->touch($this->getUnjailedPath($path), $mtime); } - /** - * get the path to a local version of the file. - * The local version of the file can be temporary and doesn't have to be persistent across requests - * - * @param string $path - * @return string|false - */ - public function getLocalFile($path) { + public function getLocalFile(string $path): string|false { return $this->getWrapperStorage()->getLocalFile($this->getUnjailedPath($path)); } - /** - * check if a file or folder has been updated since $time - * - * @param string $path - * @param int $time - * @return bool - * - * hasUpdated for folders should return at least true if a file inside the folder is add, removed or renamed. - * returning true for other changes in the folder is optional - */ - public function hasUpdated($path, $time) { + public function hasUpdated(string $path, int $time): bool { return $this->getWrapperStorage()->hasUpdated($this->getUnjailedPath($path), $time); } - /** - * get a cache instance for the storage - * - * @param string $path - * @param \OC\Files\Storage\Storage|null (optional) the storage to pass to the cache - * @return \OC\Files\Cache\Cache - */ - public function getCache($path = '', $storage = null) { + public function getCache(string $path = '', ?IStorage $storage = null): ICache { $sourceCache = $this->getWrapperStorage()->getCache($this->getUnjailedPath($path)); return new CacheJail($sourceCache, $this->rootPath); } - /** - * get the user id of the owner of a file or folder - * - * @param string $path - * @return string - */ - public function getOwner($path) { + public function getOwner(string $path): string|false { return $this->getWrapperStorage()->getOwner($this->getUnjailedPath($path)); } - /** - * get a watcher instance for the cache - * - * @param string $path - * @param \OC\Files\Storage\Storage (optional) the storage to pass to the watcher - * @return \OC\Files\Cache\Watcher - */ - public function getWatcher($path = '', $storage = null) { - if (!$storage) { - $storage = $this; - } - return $this->getWrapperStorage()->getWatcher($this->getUnjailedPath($path), $storage); + public function getWatcher(string $path = '', ?IStorage $storage = null): IWatcher { + $sourceWatcher = $this->getWrapperStorage()->getWatcher($this->getUnjailedPath($path), $this->getWrapperStorage()); + return new JailWatcher($sourceWatcher, $this->rootPath); } - /** - * get the ETag for a file or folder - * - * @param string $path - * @return string|false - */ - public function getETag($path) { + public function getETag(string $path): string|false { return $this->getWrapperStorage()->getETag($this->getUnjailedPath($path)); } - public function getMetaData($path) { + public function getMetaData(string $path): ?array { return $this->getWrapperStorage()->getMetaData($this->getUnjailedPath($path)); } - /** - * @param string $path - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE - * @param \OCP\Lock\ILockingProvider $provider - * @throws \OCP\Lock\LockedException - */ - public function acquireLock($path, $type, ILockingProvider $provider) { + public function acquireLock(string $path, int $type, ILockingProvider $provider): void { $this->getWrapperStorage()->acquireLock($this->getUnjailedPath($path), $type, $provider); } - /** - * @param string $path - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE - * @param \OCP\Lock\ILockingProvider $provider - */ - public function releaseLock($path, $type, ILockingProvider $provider) { + public function releaseLock(string $path, int $type, ILockingProvider $provider): void { $this->getWrapperStorage()->releaseLock($this->getUnjailedPath($path), $type, $provider); } - /** - * @param string $path - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE - * @param \OCP\Lock\ILockingProvider $provider - */ - public function changeLock($path, $type, ILockingProvider $provider) { + public function changeLock(string $path, int $type, ILockingProvider $provider): void { $this->getWrapperStorage()->changeLock($this->getUnjailedPath($path), $type, $provider); } /** * Resolve the path for the source of the share - * - * @param string $path - * @return array */ - public function resolvePath($path) { + public function resolvePath(string $path): array { return [$this->getWrapperStorage(), $this->getUnjailedPath($path)]; } - /** - * @param IStorage $sourceStorage - * @param string $sourceInternalPath - * @param string $targetInternalPath - * @return bool - */ - public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) { + public function copyFromStorage(IStorage $sourceStorage, string $sourceInternalPath, string $targetInternalPath): bool { if ($sourceStorage === $this) { return $this->copy($sourceInternalPath, $targetInternalPath); } return $this->getWrapperStorage()->copyFromStorage($sourceStorage, $sourceInternalPath, $this->getUnjailedPath($targetInternalPath)); } - /** - * @param IStorage $sourceStorage - * @param string $sourceInternalPath - * @param string $targetInternalPath - * @return bool - */ - public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) { + public function moveFromStorage(IStorage $sourceStorage, string $sourceInternalPath, string $targetInternalPath): bool { if ($sourceStorage === $this) { return $this->rename($sourceInternalPath, $targetInternalPath); } return $this->getWrapperStorage()->moveFromStorage($sourceStorage, $sourceInternalPath, $this->getUnjailedPath($targetInternalPath)); } - public function getPropagator($storage = null) { + public function getPropagator(?IStorage $storage = null): IPropagator { if (isset($this->propagator)) { return $this->propagator; } @@ -514,21 +247,21 @@ class Jail extends Wrapper { return $this->propagator; } - public function writeStream(string $path, $stream, int $size = null): int { + public function writeStream(string $path, $stream, ?int $size = null): int { $storage = $this->getWrapperStorage(); if ($storage->instanceOfStorage(IWriteStreamStorage::class)) { /** @var IWriteStreamStorage $storage */ return $storage->writeStream($this->getUnjailedPath($path), $stream, $size); } else { $target = $this->fopen($path, 'w'); - [$count, $result] = \OC_Helper::streamCopy($stream, $target); + $count = Files::streamCopy($stream, $target); fclose($stream); fclose($target); return $count; } } - public function getDirectoryContent($directory): \Traversable { + public function getDirectoryContent(string $directory): \Traversable { return $this->getWrapperStorage()->getDirectoryContent($this->getUnjailedPath($directory)); } } diff --git a/lib/private/Files/Storage/Wrapper/KnownMtime.php b/lib/private/Files/Storage/Wrapper/KnownMtime.php index dde209c44ab..657c6c9250c 100644 --- a/lib/private/Files/Storage/Wrapper/KnownMtime.php +++ b/lib/private/Files/Storage/Wrapper/KnownMtime.php @@ -1,5 +1,9 @@ <?php +/** + * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ namespace OC\Files\Storage\Wrapper; use OCP\Cache\CappedMemoryCache; @@ -16,13 +20,13 @@ class KnownMtime extends Wrapper { private CappedMemoryCache $knowMtimes; private ClockInterface $clock; - public function __construct($arguments) { - parent::__construct($arguments); + public function __construct(array $parameters) { + parent::__construct($parameters); $this->knowMtimes = new CappedMemoryCache(); - $this->clock = $arguments['clock']; + $this->clock = $parameters['clock']; } - public function file_put_contents($path, $data) { + public function file_put_contents(string $path, mixed $data): int|float|false { $result = parent::file_put_contents($path, $data); if ($result) { $now = $this->clock->now()->getTimestamp(); @@ -31,7 +35,7 @@ class KnownMtime extends Wrapper { return $result; } - public function stat($path) { + public function stat(string $path): array|false { $stat = parent::stat($path); if ($stat) { $this->applyKnownMtime($path, $stat); @@ -39,7 +43,7 @@ class KnownMtime extends Wrapper { return $stat; } - public function getMetaData($path) { + public function getMetaData(string $path): ?array { $stat = parent::getMetaData($path); if ($stat) { $this->applyKnownMtime($path, $stat); @@ -47,19 +51,19 @@ class KnownMtime extends Wrapper { return $stat; } - private function applyKnownMtime(string $path, array &$stat) { + private function applyKnownMtime(string $path, array &$stat): void { if (isset($stat['mtime'])) { $knownMtime = $this->knowMtimes->get($path) ?? 0; $stat['mtime'] = max($stat['mtime'], $knownMtime); } } - public function filemtime($path) { + public function filemtime(string $path): int|false { $knownMtime = $this->knowMtimes->get($path) ?? 0; return max(parent::filemtime($path), $knownMtime); } - public function mkdir($path) { + public function mkdir(string $path): bool { $result = parent::mkdir($path); if ($result) { $this->knowMtimes->set($path, $this->clock->now()->getTimestamp()); @@ -67,7 +71,7 @@ class KnownMtime extends Wrapper { return $result; } - public function rmdir($path) { + public function rmdir(string $path): bool { $result = parent::rmdir($path); if ($result) { $this->knowMtimes->set($path, $this->clock->now()->getTimestamp()); @@ -75,7 +79,7 @@ class KnownMtime extends Wrapper { return $result; } - public function unlink($path) { + public function unlink(string $path): bool { $result = parent::unlink($path); if ($result) { $this->knowMtimes->set($path, $this->clock->now()->getTimestamp()); @@ -83,7 +87,7 @@ class KnownMtime extends Wrapper { return $result; } - public function rename($source, $target) { + public function rename(string $source, string $target): bool { $result = parent::rename($source, $target); if ($result) { $this->knowMtimes->set($target, $this->clock->now()->getTimestamp()); @@ -92,7 +96,7 @@ class KnownMtime extends Wrapper { return $result; } - public function copy($source, $target) { + public function copy(string $source, string $target): bool { $result = parent::copy($source, $target); if ($result) { $this->knowMtimes->set($target, $this->clock->now()->getTimestamp()); @@ -100,7 +104,7 @@ class KnownMtime extends Wrapper { return $result; } - public function fopen($path, $mode) { + public function fopen(string $path, string $mode) { $result = parent::fopen($path, $mode); if ($result && $mode === 'w') { $this->knowMtimes->set($path, $this->clock->now()->getTimestamp()); @@ -108,7 +112,7 @@ class KnownMtime extends Wrapper { return $result; } - public function touch($path, $mtime = null) { + public function touch(string $path, ?int $mtime = null): bool { $result = parent::touch($path, $mtime); if ($result) { $this->knowMtimes->set($path, $mtime ?? $this->clock->now()->getTimestamp()); @@ -116,7 +120,7 @@ class KnownMtime extends Wrapper { return $result; } - public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) { + public function copyFromStorage(IStorage $sourceStorage, string $sourceInternalPath, string $targetInternalPath): bool { $result = parent::copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); if ($result) { $this->knowMtimes->set($targetInternalPath, $this->clock->now()->getTimestamp()); @@ -124,7 +128,7 @@ class KnownMtime extends Wrapper { return $result; } - public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) { + public function moveFromStorage(IStorage $sourceStorage, string $sourceInternalPath, string $targetInternalPath): bool { $result = parent::moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); if ($result) { $this->knowMtimes->set($targetInternalPath, $this->clock->now()->getTimestamp()); @@ -132,7 +136,7 @@ class KnownMtime extends Wrapper { return $result; } - public function writeStream(string $path, $stream, int $size = null): int { + public function writeStream(string $path, $stream, ?int $size = null): int { $result = parent::writeStream($path, $stream, $size); if ($result) { $this->knowMtimes->set($path, $this->clock->now()->getTimestamp()); diff --git a/lib/private/Files/Storage/Wrapper/PermissionsMask.php b/lib/private/Files/Storage/Wrapper/PermissionsMask.php index a79eaad0569..684040146ba 100644 --- a/lib/private/Files/Storage/Wrapper/PermissionsMask.php +++ b/lib/private/Files/Storage/Wrapper/PermissionsMask.php @@ -1,34 +1,15 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Joas Schilling <coding@schilljs.com> - * @author Jörn Friedrich Dreyer <jfd@butonic.de> - * @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 Stefan Weil <sw@weilnetz.de> - * - * @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\Storage\Wrapper; use OC\Files\Cache\Wrapper\CachePermissionsMask; use OCP\Constants; +use OCP\Files\Storage\IStorage; /** * Mask the permissions of a storage @@ -44,41 +25,41 @@ class PermissionsMask extends Wrapper { private $mask; /** - * @param array $arguments ['storage' => $storage, 'mask' => $mask] + * @param array $parameters ['storage' => $storage, 'mask' => $mask] * * $storage: The storage the permissions mask should be applied on * $mask: The permission bits that should be kept, a combination of the \OCP\Constant::PERMISSION_ constants */ - public function __construct($arguments) { - parent::__construct($arguments); - $this->mask = $arguments['mask']; + public function __construct(array $parameters) { + parent::__construct($parameters); + $this->mask = $parameters['mask']; } - private function checkMask($permissions) { + private function checkMask(int $permissions): bool { return ($this->mask & $permissions) === $permissions; } - public function isUpdatable($path) { + public function isUpdatable(string $path): bool { return $this->checkMask(Constants::PERMISSION_UPDATE) and parent::isUpdatable($path); } - public function isCreatable($path) { + public function isCreatable(string $path): bool { return $this->checkMask(Constants::PERMISSION_CREATE) and parent::isCreatable($path); } - public function isDeletable($path) { + public function isDeletable(string $path): bool { return $this->checkMask(Constants::PERMISSION_DELETE) and parent::isDeletable($path); } - public function isSharable($path) { + public function isSharable(string $path): bool { return $this->checkMask(Constants::PERMISSION_SHARE) and parent::isSharable($path); } - public function getPermissions($path) { + public function getPermissions(string $path): int { return $this->storage->getPermissions($path) & $this->mask; } - public function rename($source, $target) { + public function rename(string $source, string $target): bool { //This is a rename of the transfer file to the original file if (dirname($source) === dirname($target) && strpos($source, '.ocTransferId') > 0) { return $this->checkMask(Constants::PERMISSION_CREATE) and parent::rename($source, $target); @@ -86,33 +67,33 @@ class PermissionsMask extends Wrapper { return $this->checkMask(Constants::PERMISSION_UPDATE) and parent::rename($source, $target); } - public function copy($source, $target) { + public function copy(string $source, string $target): bool { return $this->checkMask(Constants::PERMISSION_CREATE) and parent::copy($source, $target); } - public function touch($path, $mtime = null) { + public function touch(string $path, ?int $mtime = null): bool { $permissions = $this->file_exists($path) ? Constants::PERMISSION_UPDATE : Constants::PERMISSION_CREATE; return $this->checkMask($permissions) and parent::touch($path, $mtime); } - public function mkdir($path) { + public function mkdir(string $path): bool { return $this->checkMask(Constants::PERMISSION_CREATE) and parent::mkdir($path); } - public function rmdir($path) { + public function rmdir(string $path): bool { return $this->checkMask(Constants::PERMISSION_DELETE) and parent::rmdir($path); } - public function unlink($path) { + public function unlink(string $path): bool { return $this->checkMask(Constants::PERMISSION_DELETE) and parent::unlink($path); } - public function file_put_contents($path, $data) { + public function file_put_contents(string $path, mixed $data): int|float|false { $permissions = $this->file_exists($path) ? Constants::PERMISSION_UPDATE : Constants::PERMISSION_CREATE; return $this->checkMask($permissions) ? parent::file_put_contents($path, $data) : false; } - public function fopen($path, $mode) { + public function fopen(string $path, string $mode) { if ($mode === 'r' or $mode === 'rb') { return parent::fopen($path, $mode); } else { @@ -121,14 +102,7 @@ class PermissionsMask extends Wrapper { } } - /** - * get a cache instance for the storage - * - * @param string $path - * @param \OC\Files\Storage\Storage (optional) the storage to pass to the cache - * @return \OC\Files\Cache\Cache - */ - public function getCache($path = '', $storage = null) { + public function getCache(string $path = '', ?IStorage $storage = null): \OCP\Files\Cache\ICache { if (!$storage) { $storage = $this; } @@ -136,7 +110,7 @@ class PermissionsMask extends Wrapper { return new CachePermissionsMask($sourceCache, $this->mask); } - public function getMetaData($path) { + public function getMetaData(string $path): ?array { $data = parent::getMetaData($path); if ($data && isset($data['permissions'])) { @@ -146,14 +120,14 @@ class PermissionsMask extends Wrapper { return $data; } - public function getScanner($path = '', $storage = null) { + public function getScanner(string $path = '', ?IStorage $storage = null): \OCP\Files\Cache\IScanner { if (!$storage) { $storage = $this->storage; } return parent::getScanner($path, $storage); } - public function getDirectoryContent($directory): \Traversable { + public function getDirectoryContent(string $directory): \Traversable { foreach ($this->getWrapperStorage()->getDirectoryContent($directory) as $data) { $data['scan_permissions'] = $data['scan_permissions'] ?? $data['permissions']; $data['permissions'] &= $this->mask; diff --git a/lib/private/Files/Storage/Wrapper/Quota.php b/lib/private/Files/Storage/Wrapper/Quota.php index 35dbc9fcd26..35a265f8c8e 100644 --- a/lib/private/Files/Storage/Wrapper/Quota.php +++ b/lib/private/Files/Storage/Wrapper/Quota.php @@ -1,34 +1,9 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author J0WI <J0WI@users.noreply.github.com> - * @author John Molakvoæ <skjnldsv@protonmail.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 Robin McCorkell <robin@mccorkell.me.uk> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Tigran Mkrtchyan <tigran.mkrtchyan@desy.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\Storage\Wrapper; @@ -46,11 +21,12 @@ class Quota extends Wrapper { protected string $sizeRoot; private SystemConfig $config; private bool $quotaIncludeExternalStorage; + private bool $enabled = true; /** * @param array $parameters */ - public function __construct($parameters) { + public function __construct(array $parameters) { parent::__construct($parameters); $this->quota = $parameters['quota'] ?? null; $this->quotaCallback = $parameters['quotaCallback'] ?? null; @@ -58,14 +34,11 @@ class Quota extends Wrapper { $this->quotaIncludeExternalStorage = $parameters['include_external_storage'] ?? false; } - /** - * @return int|float quota value - */ public function getQuota(): int|float { if ($this->quota === null) { $quotaCallback = $this->quotaCallback; if ($quotaCallback === null) { - throw new \Exception("No quota or quota callback provider"); + throw new \Exception('No quota or quota callback provider'); } $this->quota = $quotaCallback(); } @@ -74,15 +47,13 @@ class Quota extends Wrapper { } private function hasQuota(): bool { + if (!$this->enabled) { + return false; + } return $this->getQuota() !== FileInfo::SPACE_UNLIMITED; } - /** - * @param string $path - * @param IStorage $storage - * @return int|float - */ - protected function getSize($path, $storage = null) { + protected function getSize(string $path, ?IStorage $storage = null): int|float { if ($this->quotaIncludeExternalStorage) { $rootInfo = Filesystem::getFileInfo('', 'ext'); if ($rootInfo) { @@ -100,13 +71,7 @@ class Quota extends Wrapper { } } - /** - * Get free space as limited by the quota - * - * @param string $path - * @return int|float|bool - */ - public function free_space($path) { + public function free_space(string $path): int|float|false { if (!$this->hasQuota()) { return $this->storage->free_space($path); } @@ -126,14 +91,7 @@ class Quota extends Wrapper { } } - /** - * see https://www.php.net/manual/en/function.file_put_contents.php - * - * @param string $path - * @param mixed $data - * @return int|float|false - */ - public function file_put_contents($path, $data) { + public function file_put_contents(string $path, mixed $data): int|float|false { if (!$this->hasQuota()) { return $this->storage->file_put_contents($path, $data); } @@ -145,14 +103,7 @@ class Quota extends Wrapper { } } - /** - * see https://www.php.net/manual/en/function.copy.php - * - * @param string $source - * @param string $target - * @return bool - */ - public function copy($source, $target) { + public function copy(string $source, string $target): bool { if (!$this->hasQuota()) { return $this->storage->copy($source, $target); } @@ -164,14 +115,7 @@ class Quota extends Wrapper { } } - /** - * see https://www.php.net/manual/en/function.fopen.php - * - * @param string $path - * @param string $mode - * @return resource|bool - */ - public function fopen($path, $mode) { + public function fopen(string $path, string $mode) { if (!$this->hasQuota()) { return $this->storage->fopen($path, $mode); } @@ -195,10 +139,9 @@ class Quota extends Wrapper { * Checks whether the given path is a part file * * @param string $path Path that may identify a .part file - * @return bool * @note this is needed for reusing keys */ - private function isPartFile($path) { + private function isPartFile(string $path): bool { $extension = pathinfo($path, PATHINFO_EXTENSION); return ($extension === 'part'); @@ -207,17 +150,11 @@ class Quota extends Wrapper { /** * Only apply quota for files, not metadata, trash or others */ - private function shouldApplyQuota(string $path): bool { + protected function shouldApplyQuota(string $path): bool { return str_starts_with(ltrim($path, '/'), 'files/'); } - /** - * @param IStorage $sourceStorage - * @param string $sourceInternalPath - * @param string $targetInternalPath - * @return bool - */ - public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) { + public function copyFromStorage(IStorage $sourceStorage, string $sourceInternalPath, string $targetInternalPath): bool { if (!$this->hasQuota()) { return $this->storage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); } @@ -229,13 +166,7 @@ class Quota extends Wrapper { } } - /** - * @param IStorage $sourceStorage - * @param string $sourceInternalPath - * @param string $targetInternalPath - * @return bool - */ - public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) { + public function moveFromStorage(IStorage $sourceStorage, string $sourceInternalPath, string $targetInternalPath): bool { if (!$this->hasQuota()) { return $this->storage->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); } @@ -247,7 +178,7 @@ class Quota extends Wrapper { } } - public function mkdir($path) { + public function mkdir(string $path): bool { if (!$this->hasQuota()) { return $this->storage->mkdir($path); } @@ -259,7 +190,7 @@ class Quota extends Wrapper { return parent::mkdir($path); } - public function touch($path, $mtime = null) { + public function touch(string $path, ?int $mtime = null): bool { if (!$this->hasQuota()) { return $this->storage->touch($path, $mtime); } @@ -270,4 +201,8 @@ class Quota extends Wrapper { return parent::touch($path, $mtime); } + + public function enableQuota(bool $enabled): void { + $this->enabled = $enabled; + } } diff --git a/lib/private/Files/Storage/Wrapper/Wrapper.php b/lib/private/Files/Storage/Wrapper/Wrapper.php index 9f5564b4490..7af11dd5ef7 100644 --- a/lib/private/Files/Storage/Wrapper/Wrapper.php +++ b/lib/private/Files/Storage/Wrapper/Wrapper.php @@ -1,41 +1,26 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author J0WI <J0WI@users.noreply.github.com> - * @author Joas Schilling <coding@schilljs.com> - * @author Lukas Reschke <lukas@statuscode.ch> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Robin Appelman <robin@icewind.nl> - * @author Robin McCorkell <robin@mccorkell.me.uk> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * @author Tigran Mkrtchyan <tigran.mkrtchyan@desy.de> - * @author Vincent Petry <vincent@nextcloud.com> - * @author Vinicius Cubas Brand <vinicius@eita.org.br> - * - * @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\Storage\Wrapper; -use OCP\Files\InvalidPathException; +use OC\Files\Storage\FailedStorage; +use OC\Files\Storage\Storage; +use OCP\Files; +use OCP\Files\Cache\ICache; +use OCP\Files\Cache\IPropagator; +use OCP\Files\Cache\IScanner; +use OCP\Files\Cache\IUpdater; +use OCP\Files\Cache\IWatcher; use OCP\Files\Storage\ILockingStorage; use OCP\Files\Storage\IStorage; use OCP\Files\Storage\IWriteStreamStorage; use OCP\Lock\ILockingProvider; +use OCP\Server; +use Psr\Log\LoggerInterface; class Wrapper implements \OC\Files\Storage\Storage, ILockingStorage, IWriteStreamStorage { /** @@ -52,441 +37,192 @@ class Wrapper implements \OC\Files\Storage\Storage, ILockingStorage, IWriteStrea /** * @param array $parameters */ - public function __construct($parameters) { + public function __construct(array $parameters) { $this->storage = $parameters['storage']; } - /** - * @return \OC\Files\Storage\Storage - */ - public function getWrapperStorage() { + public function getWrapperStorage(): Storage { + if (!$this->storage) { + $message = 'storage wrapper ' . get_class($this) . " doesn't have a wrapped storage set"; + $logger = Server::get(LoggerInterface::class); + $logger->error($message); + $this->storage = new FailedStorage(['exception' => new \Exception($message)]); + } return $this->storage; } - /** - * Get the identifier for the storage, - * the returned id should be the same for every storage object that is created with the same parameters - * and two storage objects with the same id should refer to two storages that display the same files. - * - * @return string - */ - public function getId() { + public function getId(): string { return $this->getWrapperStorage()->getId(); } - /** - * see https://www.php.net/manual/en/function.mkdir.php - * - * @param string $path - * @return bool - */ - public function mkdir($path) { + public function mkdir(string $path): bool { return $this->getWrapperStorage()->mkdir($path); } - /** - * see https://www.php.net/manual/en/function.rmdir.php - * - * @param string $path - * @return bool - */ - public function rmdir($path) { + public function rmdir(string $path): bool { return $this->getWrapperStorage()->rmdir($path); } - /** - * see https://www.php.net/manual/en/function.opendir.php - * - * @param string $path - * @return resource|false - */ - public function opendir($path) { + public function opendir(string $path) { return $this->getWrapperStorage()->opendir($path); } - /** - * see https://www.php.net/manual/en/function.is_dir.php - * - * @param string $path - * @return bool - */ - public function is_dir($path) { + public function is_dir(string $path): bool { return $this->getWrapperStorage()->is_dir($path); } - /** - * see https://www.php.net/manual/en/function.is_file.php - * - * @param string $path - * @return bool - */ - public function is_file($path) { + public function is_file(string $path): bool { return $this->getWrapperStorage()->is_file($path); } - /** - * see https://www.php.net/manual/en/function.stat.php - * only the following keys are required in the result: size and mtime - * - * @param string $path - * @return array|bool - */ - public function stat($path) { + public function stat(string $path): array|false { return $this->getWrapperStorage()->stat($path); } - /** - * see https://www.php.net/manual/en/function.filetype.php - * - * @param string $path - * @return string|bool - */ - public function filetype($path) { + public function filetype(string $path): string|false { return $this->getWrapperStorage()->filetype($path); } - /** - * see https://www.php.net/manual/en/function.filesize.php - * The result for filesize when called on a folder is required to be 0 - */ - public function filesize($path): false|int|float { + public function filesize(string $path): int|float|false { return $this->getWrapperStorage()->filesize($path); } - /** - * check if a file can be created in $path - * - * @param string $path - * @return bool - */ - public function isCreatable($path) { + public function isCreatable(string $path): bool { return $this->getWrapperStorage()->isCreatable($path); } - /** - * check if a file can be read - * - * @param string $path - * @return bool - */ - public function isReadable($path) { + public function isReadable(string $path): bool { return $this->getWrapperStorage()->isReadable($path); } - /** - * check if a file can be written to - * - * @param string $path - * @return bool - */ - public function isUpdatable($path) { + public function isUpdatable(string $path): bool { return $this->getWrapperStorage()->isUpdatable($path); } - /** - * check if a file can be deleted - * - * @param string $path - * @return bool - */ - public function isDeletable($path) { + public function isDeletable(string $path): bool { return $this->getWrapperStorage()->isDeletable($path); } - /** - * check if a file can be shared - * - * @param string $path - * @return bool - */ - public function isSharable($path) { + public function isSharable(string $path): bool { return $this->getWrapperStorage()->isSharable($path); } - /** - * get the full permissions of a path. - * Should return a combination of the PERMISSION_ constants defined in lib/public/constants.php - * - * @param string $path - * @return int - */ - public function getPermissions($path) { + public function getPermissions(string $path): int { return $this->getWrapperStorage()->getPermissions($path); } - /** - * see https://www.php.net/manual/en/function.file_exists.php - * - * @param string $path - * @return bool - */ - public function file_exists($path) { + public function file_exists(string $path): bool { return $this->getWrapperStorage()->file_exists($path); } - /** - * see https://www.php.net/manual/en/function.filemtime.php - * - * @param string $path - * @return int|bool - */ - public function filemtime($path) { + public function filemtime(string $path): int|false { return $this->getWrapperStorage()->filemtime($path); } - /** - * see https://www.php.net/manual/en/function.file_get_contents.php - * - * @param string $path - * @return string|false - */ - public function file_get_contents($path) { + public function file_get_contents(string $path): string|false { return $this->getWrapperStorage()->file_get_contents($path); } - /** - * see https://www.php.net/manual/en/function.file_put_contents.php - * - * @param string $path - * @param mixed $data - * @return int|float|false - */ - public function file_put_contents($path, $data) { + public function file_put_contents(string $path, mixed $data): int|float|false { return $this->getWrapperStorage()->file_put_contents($path, $data); } - /** - * see https://www.php.net/manual/en/function.unlink.php - * - * @param string $path - * @return bool - */ - public function unlink($path) { + public function unlink(string $path): bool { return $this->getWrapperStorage()->unlink($path); } - /** - * see https://www.php.net/manual/en/function.rename.php - * - * @param string $source - * @param string $target - * @return bool - */ - public function rename($source, $target) { + public function rename(string $source, string $target): bool { return $this->getWrapperStorage()->rename($source, $target); } - /** - * see https://www.php.net/manual/en/function.copy.php - * - * @param string $source - * @param string $target - * @return bool - */ - public function copy($source, $target) { + public function copy(string $source, string $target): bool { return $this->getWrapperStorage()->copy($source, $target); } - /** - * see https://www.php.net/manual/en/function.fopen.php - * - * @param string $path - * @param string $mode - * @return resource|bool - */ - public function fopen($path, $mode) { + public function fopen(string $path, string $mode) { return $this->getWrapperStorage()->fopen($path, $mode); } - /** - * get the mimetype for a file or folder - * The mimetype for a folder is required to be "httpd/unix-directory" - * - * @param string $path - * @return string|bool - */ - public function getMimeType($path) { + public function getMimeType(string $path): string|false { return $this->getWrapperStorage()->getMimeType($path); } - /** - * see https://www.php.net/manual/en/function.hash.php - * - * @param string $type - * @param string $path - * @param bool $raw - * @return string|bool - */ - public function hash($type, $path, $raw = false) { + public function hash(string $type, string $path, bool $raw = false): string|false { return $this->getWrapperStorage()->hash($type, $path, $raw); } - /** - * see https://www.php.net/manual/en/function.free_space.php - * - * @param string $path - * @return int|float|bool - */ - public function free_space($path) { + public function free_space(string $path): int|float|false { return $this->getWrapperStorage()->free_space($path); } - /** - * search for occurrences of $query in file names - * - * @param string $query - * @return array|bool - */ - public function search($query) { - return $this->getWrapperStorage()->search($query); - } - - /** - * see https://www.php.net/manual/en/function.touch.php - * If the backend does not support the operation, false should be returned - * - * @param string $path - * @param int $mtime - * @return bool - */ - public function touch($path, $mtime = null) { + public function touch(string $path, ?int $mtime = null): bool { return $this->getWrapperStorage()->touch($path, $mtime); } - /** - * get the path to a local version of the file. - * The local version of the file can be temporary and doesn't have to be persistent across requests - * - * @param string $path - * @return string|false - */ - public function getLocalFile($path) { + public function getLocalFile(string $path): string|false { return $this->getWrapperStorage()->getLocalFile($path); } - /** - * check if a file or folder has been updated since $time - * - * @param string $path - * @param int $time - * @return bool - * - * hasUpdated for folders should return at least true if a file inside the folder is add, removed or renamed. - * returning true for other changes in the folder is optional - */ - public function hasUpdated($path, $time) { + public function hasUpdated(string $path, int $time): bool { return $this->getWrapperStorage()->hasUpdated($path, $time); } - /** - * get a cache instance for the storage - * - * @param string $path - * @param \OC\Files\Storage\Storage|null (optional) the storage to pass to the cache - * @return \OC\Files\Cache\Cache - */ - public function getCache($path = '', $storage = null) { + public function getCache(string $path = '', ?IStorage $storage = null): ICache { if (!$storage) { $storage = $this; } return $this->getWrapperStorage()->getCache($path, $storage); } - /** - * get a scanner instance for the storage - * - * @param string $path - * @param \OC\Files\Storage\Storage (optional) the storage to pass to the scanner - * @return \OC\Files\Cache\Scanner - */ - public function getScanner($path = '', $storage = null) { + public function getScanner(string $path = '', ?IStorage $storage = null): IScanner { if (!$storage) { $storage = $this; } return $this->getWrapperStorage()->getScanner($path, $storage); } - - /** - * get the user id of the owner of a file or folder - * - * @param string $path - * @return string - */ - public function getOwner($path) { + public function getOwner(string $path): string|false { return $this->getWrapperStorage()->getOwner($path); } - /** - * get a watcher instance for the cache - * - * @param string $path - * @param \OC\Files\Storage\Storage (optional) the storage to pass to the watcher - * @return \OC\Files\Cache\Watcher - */ - public function getWatcher($path = '', $storage = null) { + public function getWatcher(string $path = '', ?IStorage $storage = null): IWatcher { if (!$storage) { $storage = $this; } return $this->getWrapperStorage()->getWatcher($path, $storage); } - public function getPropagator($storage = null) { + public function getPropagator(?IStorage $storage = null): IPropagator { if (!$storage) { $storage = $this; } return $this->getWrapperStorage()->getPropagator($storage); } - public function getUpdater($storage = null) { + public function getUpdater(?IStorage $storage = null): IUpdater { if (!$storage) { $storage = $this; } return $this->getWrapperStorage()->getUpdater($storage); } - /** - * @return \OC\Files\Cache\Storage - */ - public function getStorageCache() { + public function getStorageCache(): \OC\Files\Cache\Storage { return $this->getWrapperStorage()->getStorageCache(); } - /** - * get the ETag for a file or folder - * - * @param string $path - * @return string|false - */ - public function getETag($path) { + public function getETag(string $path): string|false { return $this->getWrapperStorage()->getETag($path); } - /** - * Returns true - * - * @return true - */ - public function test() { + public function test(): bool { return $this->getWrapperStorage()->test(); } - /** - * Returns the wrapped storage's value for isLocal() - * - * @return bool wrapped storage's isLocal() value - */ - public function isLocal() { + public function isLocal(): bool { return $this->getWrapperStorage()->isLocal(); } - /** - * Check if the storage is an instance of $class or is a wrapper for a storage that is an instance of $class - * - * @param class-string<IStorage> $class - * @return bool - */ - public function instanceOfStorage($class) { + public function instanceOfStorage(string $class): bool { if (ltrim($class, '\\') === 'OC\Files\Storage\Shared') { // FIXME Temporary fix to keep existing checks working $class = '\OCA\Files_Sharing\SharedStorage'; @@ -499,7 +235,7 @@ class Wrapper implements \OC\Files\Storage\Storage, ILockingStorage, IWriteStrea * @psalm-param class-string<T> $class * @psalm-return T|null */ - public function getInstanceOfStorage(string $class) { + public function getInstanceOfStorage(string $class): ?IStorage { $storage = $this; while ($storage instanceof Wrapper) { if ($storage instanceof $class) { @@ -516,61 +252,29 @@ class Wrapper implements \OC\Files\Storage\Storage, ILockingStorage, IWriteStrea /** * Pass any methods custom to specific storage implementations to the wrapped storage * - * @param string $method - * @param array $args * @return mixed */ - public function __call($method, $args) { + public function __call(string $method, array $args) { return call_user_func_array([$this->getWrapperStorage(), $method], $args); } - /** - * A custom storage implementation can return an url for direct download of a give file. - * - * For now the returned array can hold the parameter url - in future more attributes might follow. - * - * @param string $path - * @return array|bool - */ - public function getDirectDownload($path) { + public function getDirectDownload(string $path): array|false { return $this->getWrapperStorage()->getDirectDownload($path); } - /** - * Get availability of the storage - * - * @return array [ available, last_checked ] - */ - public function getAvailability() { + public function getAvailability(): array { return $this->getWrapperStorage()->getAvailability(); } - /** - * Set availability of the storage - * - * @param bool $isAvailable - */ - public function setAvailability($isAvailable) { + public function setAvailability(bool $isAvailable): void { $this->getWrapperStorage()->setAvailability($isAvailable); } - /** - * @param string $path the path of the target folder - * @param string $fileName the name of the file itself - * @return void - * @throws InvalidPathException - */ - public function verifyPath($path, $fileName) { + public function verifyPath(string $path, string $fileName): void { $this->getWrapperStorage()->verifyPath($path, $fileName); } - /** - * @param IStorage $sourceStorage - * @param string $sourceInternalPath - * @param string $targetInternalPath - * @return bool - */ - public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) { + public function copyFromStorage(IStorage $sourceStorage, string $sourceInternalPath, string $targetInternalPath): bool { if ($sourceStorage === $this) { return $this->copy($sourceInternalPath, $targetInternalPath); } @@ -578,13 +282,7 @@ class Wrapper implements \OC\Files\Storage\Storage, ILockingStorage, IWriteStrea return $this->getWrapperStorage()->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); } - /** - * @param IStorage $sourceStorage - * @param string $sourceInternalPath - * @param string $targetInternalPath - * @return bool - */ - public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) { + public function moveFromStorage(IStorage $sourceStorage, string $sourceInternalPath, string $targetInternalPath): bool { if ($sourceStorage === $this) { return $this->rename($sourceInternalPath, $targetInternalPath); } @@ -592,66 +290,62 @@ class Wrapper implements \OC\Files\Storage\Storage, ILockingStorage, IWriteStrea return $this->getWrapperStorage()->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); } - public function getMetaData($path) { + public function getMetaData(string $path): ?array { return $this->getWrapperStorage()->getMetaData($path); } - /** - * @param string $path - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE - * @param \OCP\Lock\ILockingProvider $provider - * @throws \OCP\Lock\LockedException - */ - public function acquireLock($path, $type, ILockingProvider $provider) { + public function acquireLock(string $path, int $type, ILockingProvider $provider): void { if ($this->getWrapperStorage()->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { $this->getWrapperStorage()->acquireLock($path, $type, $provider); } } - /** - * @param string $path - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE - * @param \OCP\Lock\ILockingProvider $provider - */ - public function releaseLock($path, $type, ILockingProvider $provider) { + public function releaseLock(string $path, int $type, ILockingProvider $provider): void { if ($this->getWrapperStorage()->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { $this->getWrapperStorage()->releaseLock($path, $type, $provider); } } - /** - * @param string $path - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE - * @param \OCP\Lock\ILockingProvider $provider - */ - public function changeLock($path, $type, ILockingProvider $provider) { + public function changeLock(string $path, int $type, ILockingProvider $provider): void { if ($this->getWrapperStorage()->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { $this->getWrapperStorage()->changeLock($path, $type, $provider); } } - /** - * @return bool - */ - public function needsPartFile() { + public function needsPartFile(): bool { return $this->getWrapperStorage()->needsPartFile(); } - public function writeStream(string $path, $stream, int $size = null): int { + public function writeStream(string $path, $stream, ?int $size = null): int { $storage = $this->getWrapperStorage(); if ($storage->instanceOfStorage(IWriteStreamStorage::class)) { /** @var IWriteStreamStorage $storage */ return $storage->writeStream($path, $stream, $size); } else { $target = $this->fopen($path, 'w'); - [$count, $result] = \OC_Helper::streamCopy($stream, $target); + $count = Files::streamCopy($stream, $target); fclose($stream); fclose($target); return $count; } } - public function getDirectoryContent($directory): \Traversable { + public function getDirectoryContent(string $directory): \Traversable { return $this->getWrapperStorage()->getDirectoryContent($directory); } + + public function isWrapperOf(IStorage $storage): bool { + $wrapped = $this->getWrapperStorage(); + if ($wrapped === $storage) { + return true; + } + if ($wrapped instanceof Wrapper) { + return $wrapped->isWrapperOf($storage); + } + return false; + } + + public function setOwner(?string $user): void { + $this->getWrapperStorage()->setOwner($user); + } } |