aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private/Files/Storage
diff options
context:
space:
mode:
Diffstat (limited to 'lib/private/Files/Storage')
-rw-r--r--lib/private/Files/Storage/Common.php537
-rw-r--r--lib/private/Files/Storage/CommonTest.php61
-rw-r--r--lib/private/Files/Storage/DAV.php533
-rw-r--r--lib/private/Files/Storage/FailedStorage.php122
-rw-r--r--lib/private/Files/Storage/Home.php70
-rw-r--r--lib/private/Files/Storage/Local.php339
-rw-r--r--lib/private/Files/Storage/LocalRootStorage.php30
-rw-r--r--lib/private/Files/Storage/LocalTempFileTrait.php50
-rw-r--r--lib/private/Files/Storage/PolyFill/CopyDirectory.php71
-rw-r--r--lib/private/Files/Storage/Storage.php129
-rw-r--r--lib/private/Files/Storage/StorageFactory.php64
-rw-r--r--lib/private/Files/Storage/Temporary.php40
-rw-r--r--lib/private/Files/Storage/Wrapper/Availability.php384
-rw-r--r--lib/private/Files/Storage/Wrapper/Encoding.php353
-rw-r--r--lib/private/Files/Storage/Wrapper/EncodingDirectoryWrapper.php31
-rw-r--r--lib/private/Files/Storage/Wrapper/Encryption.php615
-rw-r--r--lib/private/Files/Storage/Wrapper/Jail.php405
-rw-r--r--lib/private/Files/Storage/Wrapper/KnownMtime.php146
-rw-r--r--lib/private/Files/Storage/Wrapper/PermissionsMask.php92
-rw-r--r--lib/private/Files/Storage/Wrapper/Quota.php221
-rw-r--r--lib/private/Files/Storage/Wrapper/Wrapper.php499
21 files changed, 1610 insertions, 3182 deletions
diff --git a/lib/private/Files/Storage/Common.php b/lib/private/Files/Storage/Common.php
index 3c970ee75f5..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) {
+ 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,29 +170,33 @@ 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;
+ }
$this->removeCachedFile($path);
$count = fwrite($handle, $data);
fclose($handle);
return $count;
}
- public function rename($path1, $path2) {
- $this->remove($path2);
+ public function rename(string $source, string $target): bool {
+ $this->remove($target);
- $this->removeCachedFile($path1);
- return $this->copy($path1, $path2) and $this->remove($path1);
+ $this->removeCachedFile($source);
+ return $this->copy($source, $target) and $this->remove($source);
}
- public function copy($path1, $path2) {
- if ($this->is_dir($path1)) {
- $this->remove($path2);
- $dir = $this->opendir($path1);
- $this->mkdir($path2);
- while ($file = readdir($dir)) {
+ 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)) !== false) {
if (!Filesystem::isIgnoredDir($file)) {
- if (!$this->copy($path1 . '/' . $file, $path2 . '/' . $file)) {
+ if (!$this->copy($source . '/' . $file, $target . '/' . $file)) {
+ closedir($dir);
return false;
}
}
@@ -235,18 +204,18 @@ abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage {
closedir($dir);
return true;
} else {
- $source = $this->fopen($path1, 'r');
- $target = $this->fopen($path2, 'w');
- [, $result] = \OC_Helper::streamCopy($source, $target);
+ $sourceStream = $this->fopen($source, 'r');
+ $targetStream = $this->fopen($target, 'w');
+ [, $result] = Files::streamCopy($sourceStream, $targetStream, true);
if (!$result) {
- \OC::$server->get(LoggerInterface::class)->warning("Failed to write data while copying $path1 to $path2");
+ Server::get(LoggerInterface::class)->warning("Failed to write data while copying $source to $target");
}
- $this->removeCachedFile($path2);
+ $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)) {
@@ -256,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);
@@ -293,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) {
@@ -319,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();
}
@@ -417,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();
}
@@ -434,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;
}
@@ -453,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|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;
@@ -493,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';
@@ -509,106 +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
+ * Get the filename validator
+ * (cached for performance)
*/
- protected function verifyPosixPath($fileName) {
- $this->scanForInvalidCharacters($fileName, "\\/");
- $fileName = trim($fileName);
- $reservedNames = ['*'];
- if (in_array($fileName, $reservedNames)) {
- throw new ReservedWordException();
+ protected function getFilenameValidator(): IFilenameValidator {
+ if ($this->filenameValidator === null) {
+ $this->filenameValidator = Server::get(IFilenameValidator::class);
}
+ return $this->filenameValidator;
}
- /**
- * @param string $fileName
- * @param string $invalidChars
- * @throws InvalidPathException
- */
- private function scanForInvalidCharacters($fileName, $invalidChars) {
- foreach (str_split($invalidChars) as $char) {
- if (strpos($fileName, $char) !== false) {
- throw new InvalidCharacterInPathException();
- }
- }
-
- $sanitizedFileName = filter_var($fileName, FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW);
- if ($sanitizedFileName !== $fileName) {
- throw new InvalidCharacterInPathException();
- }
- }
-
- /**
- * @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) {
- return isset($this->mountOptions[$name]) ? $this->mountOptions[$name] : $default;
+ 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);
}
@@ -618,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);
}
@@ -632,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]);
}
}
@@ -653,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();
}
@@ -668,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)) {
/**
@@ -694,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 = $result && $sourceStorage->rmdir($sourceInternalPath);
- } else {
- $result = $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);
}
@@ -736,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';
@@ -761,6 +640,7 @@ abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage {
try {
$provider->acquireLock('files/' . md5($this->getId() . '::' . trim($path, '/')), $type, $this->getId() . '::' . $path);
} catch (LockedException $e) {
+ $e = new LockedException($e->getPath(), $e, $e->getExistingLock(), $path);
if ($logger) {
$logger->info($e->getMessage(), ['exception' => $e]);
}
@@ -768,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';
@@ -793,6 +667,7 @@ abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage {
try {
$provider->releaseLock('files/' . md5($this->getId() . '::' . trim($path, '/')), $type);
} catch (LockedException $e) {
+ $e = new LockedException($e->getPath(), $e, $e->getExistingLock(), $path);
if ($logger) {
$logger->info($e->getMessage(), ['exception' => $e]);
}
@@ -800,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';
@@ -825,6 +694,7 @@ abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage {
try {
$provider->changeLock('files/' . md5($this->getId() . '::' . trim($path, '/')), $type);
} catch (LockedException $e) {
+ $e = new LockedException($e->getPath(), $e, $e->getExistingLock(), $path);
if ($logger) {
$logger->info($e->getMessage(), ['exception' => $e]);
}
@@ -834,8 +704,8 @@ abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage {
private function getLockLogger(): ?LoggerInterface {
if (is_null($this->shouldLogLocks)) {
- $this->shouldLogLocks = \OC::$server->getConfig()->getSystemValue('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;
}
@@ -843,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);
@@ -886,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 06bec9e39f1..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;
@@ -44,19 +14,25 @@ use OC\Files\Filesystem;
use OC\MemCache\ArrayCache;
use OCP\AppFramework\Http;
use OCP\Constants;
+use OCP\Diagnostics\IEventLogger;
use OCP\Files\FileInfo;
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;
use Sabre\DAV\Client;
use Sabre\DAV\Xml\Property\ResourceType;
use Sabre\HTTP\ClientException;
use Sabre\HTTP\ClientHttpException;
-use Psr\Log\LoggerInterface;
+use Sabre\HTTP\RequestInterface;
/**
* Class DAV
@@ -88,33 +64,50 @@ class DAV extends Common {
protected $httpClientService;
/** @var ICertificateManager */
protected $certManager;
+ protected LoggerInterface $logger;
+ protected IEventLogger $eventLogger;
+ protected IMimeTypeDetector $mimeTypeDetector;
+
+ /** @var int */
+ private $timeout;
+
+ protected const PROPFIND_PROPS = [
+ '{DAV:}getlastmodified',
+ '{DAV:}getcontentlength',
+ '{DAV:}getcontenttype',
+ '{http://owncloud.org/ns}permissions',
+ '{http://open-collaboration-services.org/ns}share-permissions',
+ '{DAV:}resourcetype',
+ '{DAV:}getetag',
+ '{DAV:}quota-available-bytes',
+ ];
/**
- * @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 (substr($host, 0, 8) == "https://") {
+ if (str_starts_with($host, 'https://')) {
$host = substr($host, 8);
- } elseif (substr($host, 0, 7) == "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;
@@ -123,15 +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 = 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 = 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;
}
@@ -146,7 +144,7 @@ class DAV extends Common {
$settings['authType'] = $this->authType;
}
- $proxy = \OC::$server->getConfig()->getSystemValue('proxy', '');
+ $proxy = Server::get(IConfig::class)->getSystemValueString('proxy', '');
if ($proxy !== '') {
$settings['proxy'] = $proxy;
}
@@ -163,32 +161,41 @@ class DAV extends Common {
$this->client->addCurlSetting(CURLOPT_CAINFO, $this->certPath);
}
}
+
+ $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']);
+ $lastRequestStart = microtime(true);
+ $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->eventLogger->end('fs:storage:dav:request');
+ });
}
/**
* 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);
@@ -198,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
@@ -210,36 +216,16 @@ class DAV extends Common {
return $result;
}
- /** {@inheritdoc} */
- public function opendir($path) {
+ public function opendir(string $path) {
$this->init();
$path = $this->cleanPath($path);
try {
- $response = $this->client->propFind(
- $this->encodePath($path),
- ['{DAV:}getetag'],
- 1
- );
- if ($response === false) {
- return false;
- }
- $content = [];
- $files = array_keys($response);
- array_shift($files); //the first entry is the current directory
-
- if (!$this->statCache->hasKey($path)) {
- $this->statCache->set($path, true);
- }
- foreach ($files as $file) {
- $file = urldecode($file);
- // do not store the real entry, we might not have all properties
- if (!$this->statCache->hasKey($path)) {
- $this->statCache->set($file, true);
- }
- $file = basename($file);
- $content[] = $file;
+ $content = $this->getDirectoryContent($path);
+ $files = [];
+ foreach ($content as $child) {
+ $files[] = $child['name'];
}
- return IteratorDirectory::wrap($content);
+ return IteratorDirectory::wrap($files);
} catch (\Exception $e) {
$this->convertException($e, $path);
}
@@ -254,38 +240,30 @@ 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),
- [
- '{DAV:}getlastmodified',
- '{DAV:}getcontentlength',
- '{DAV:}getcontenttype',
- '{http://owncloud.org/ns}permissions',
- '{http://open-collaboration-services.org/ns}share-permissions',
- '{DAV:}resourcetype',
- '{DAV:}getetag',
- '{DAV:}quota-available-bytes',
- ]
+ self::PROPFIND_PROPS
);
$this->statCache->set($path, $response);
} catch (ClientHttpException $e) {
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);
}
@@ -295,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);
@@ -333,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);
@@ -343,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) {
@@ -355,7 +329,9 @@ class DAV extends Common {
->newClient()
->get($this->createBaseUri() . $this->encodePath($path), [
'auth' => [$this->user, $this->password],
- 'stream' => true
+ 'stream' => true,
+ // set download timeout for users with slow connections or large files
+ 'timeout' => $this->timeout
]);
} catch (\GuzzleHttp\Exception\ClientException $e) {
if ($e->getResponse() instanceof ResponseInterface
@@ -370,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':
@@ -398,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);
@@ -414,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 {
@@ -434,7 +414,7 @@ class DAV extends Common {
return FileInfo::SPACE_UNKNOWN;
}
if (isset($response['{DAV:}quota-available-bytes'])) {
- return (int)$response['{DAV:}quota-available-bytes'];
+ return Util::numericToNumber($response['{DAV:}quota-available-bytes']);
} else {
return FileInfo::SPACE_UNKNOWN;
}
@@ -443,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();
@@ -458,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) {
@@ -484,23 +460,14 @@ class DAV extends Common {
return true;
}
- /**
- * @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 {
$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
@@ -512,37 +479,38 @@ class DAV extends Common {
->newClient()
->put($this->createBaseUri() . $this->encodePath($target), [
'body' => $source,
- 'auth' => [$this->user, $this->password]
+ 'auth' => [$this->user, $this->password],
+ // set upload timeout for users with slow connections or large files
+ 'timeout' => $this->timeout
]);
$this->removeCachedFile($target);
}
- /** {@inheritdoc} */
- public function rename($path1, $path2) {
+ public function rename(string $source, string $target): bool {
$this->init();
- $path1 = $this->cleanPath($path1);
- $path2 = $this->cleanPath($path2);
+ $source = $this->cleanPath($source);
+ $target = $this->cleanPath($target);
try {
// overwrite directory ?
- if ($this->is_dir($path2)) {
+ if ($this->is_dir($target)) {
// needs trailing slash in destination
- $path2 = rtrim($path2, '/') . '/';
+ $target = rtrim($target, '/') . '/';
}
$this->client->request(
'MOVE',
- $this->encodePath($path1),
+ $this->encodePath($source),
null,
[
- 'Destination' => $this->createBaseUri() . $this->encodePath($path2),
+ 'Destination' => $this->createBaseUri() . $this->encodePath($target),
]
);
- $this->statCache->clear($path1 . '/');
- $this->statCache->clear($path2 . '/');
- $this->statCache->set($path1, false);
- $this->statCache->set($path2, true);
- $this->removeCachedFile($path1);
- $this->removeCachedFile($path2);
+ $this->statCache->clear($source . '/');
+ $this->statCache->clear($target . '/');
+ $this->statCache->set($source, false);
+ $this->statCache->set($target, true);
+ $this->removeCachedFile($source);
+ $this->removeCachedFile($target);
return true;
} catch (\Exception $e) {
$this->convertException($e);
@@ -550,28 +518,27 @@ class DAV extends Common {
return false;
}
- /** {@inheritdoc} */
- public function copy($path1, $path2) {
+ public function copy(string $source, string $target): bool {
$this->init();
- $path1 = $this->cleanPath($path1);
- $path2 = $this->cleanPath($path2);
+ $source = $this->cleanPath($source);
+ $target = $this->cleanPath($target);
try {
// overwrite directory ?
- if ($this->is_dir($path2)) {
+ if ($this->is_dir($target)) {
// needs trailing slash in destination
- $path2 = rtrim($path2, '/') . '/';
+ $target = rtrim($target, '/') . '/';
}
$this->client->request(
'COPY',
- $this->encodePath($path1),
+ $this->encodePath($source),
null,
[
- 'Destination' => $this->createBaseUri() . $this->encodePath($path2),
+ 'Destination' => $this->createBaseUri() . $this->encodePath($target),
]
);
- $this->statCache->clear($path2 . '/');
- $this->statCache->set($path2, true);
- $this->removeCachedFile($path2);
+ $this->statCache->clear($target . '/');
+ $this->statCache->set($target, true);
+ $this->removeCachedFile($target);
return true;
} catch (\Exception $e) {
$this->convertException($e);
@@ -579,62 +546,80 @@ class DAV extends Common {
return false;
}
- /** {@inheritdoc} */
- public function stat($path) {
- try {
- $response = $this->propfind($path);
- if (!$response) {
- return false;
- }
- return [
- 'mtime' => isset($response['{DAV:}getlastmodified']) ? strtotime($response['{DAV:}getlastmodified']) : null,
- 'size' => (int)($response['{DAV:}getcontentlength'] ?? 0),
- ];
- } catch (\Exception $e) {
- $this->convertException($e, $path);
+ public function getMetaData(string $path): ?array {
+ if (Filesystem::isFileBlacklisted($path)) {
+ throw new ForbiddenException('Invalid path: ' . $path, false);
+ }
+ $response = $this->propfind($path);
+ if (!$response) {
+ return null;
+ } else {
+ return $this->getMetaFromPropfind($path, $response);
}
- return [];
}
+ private function getMetaFromPropfind(string $path, array $response): array {
+ if (isset($response['{DAV:}getetag'])) {
+ $etag = trim($response['{DAV:}getetag'], '"');
+ if (strlen($etag) > 40) {
+ $etag = md5($etag);
+ }
+ } else {
+ $etag = parent::getETag($path);
+ }
- /** {@inheritdoc} */
- public function getMimeType($path) {
- $remoteMimetype = $this->getMimeTypeFromRemote($path);
- if ($remoteMimetype === 'application/octet-stream') {
- return \OC::$server->getMimeTypeDetector()->detectPath($path);
+ $responseType = [];
+ if (isset($response['{DAV:}resourcetype'])) {
+ /** @var ResourceType[] $response */
+ $responseType = $response['{DAV:}resourcetype']->getValue();
+ }
+ $type = (count($responseType) > 0 && $responseType[0] == '{DAV:}collection') ? 'dir' : 'file';
+ if ($type === 'dir') {
+ $mimeType = 'httpd/unix-directory';
+ } elseif (isset($response['{DAV:}getcontenttype'])) {
+ $mimeType = $response['{DAV:}getcontenttype'];
} else {
- return $remoteMimetype;
+ $mimeType = $this->mimeTypeDetector->detectPath($path);
}
- }
- public function getMimeTypeFromRemote($path) {
- try {
- $response = $this->propfind($path);
- if ($response === false) {
- return false;
- }
- $responseType = [];
- if (isset($response["{DAV:}resourcetype"])) {
- /** @var ResourceType[] $response */
- $responseType = $response["{DAV:}resourcetype"]->getValue();
- }
- $type = (count($responseType) > 0 and $responseType[0] == "{DAV:}collection") ? 'dir' : 'file';
- if ($type == 'dir') {
- return 'httpd/unix-directory';
- } elseif (isset($response['{DAV:}getcontenttype'])) {
- return $response['{DAV:}getcontenttype'];
- } else {
- return 'application/octet-stream';
- }
- } catch (\Exception $e) {
- return false;
+ if (isset($response['{http://owncloud.org/ns}permissions'])) {
+ $permissions = $this->parsePermissions($response['{http://owncloud.org/ns}permissions']);
+ } elseif ($type === 'dir') {
+ $permissions = Constants::PERMISSION_ALL;
+ } else {
+ $permissions = Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE;
}
+
+ $mtime = isset($response['{DAV:}getlastmodified']) ? strtotime($response['{DAV:}getlastmodified']) : null;
+
+ if ($type === 'dir') {
+ $size = -1;
+ } else {
+ $size = Util::numericToNumber($response['{DAV:}getcontentlength'] ?? 0);
+ }
+
+ return [
+ 'name' => basename($path),
+ 'mtime' => $mtime,
+ 'storage_mtime' => $mtime,
+ 'size' => $size,
+ 'permissions' => $permissions,
+ 'etag' => $etag,
+ 'mimetype' => $mimeType,
+ ];
}
- /**
- * @param string $path
- * @return string
- */
- public function cleanPath($path) {
+ public function stat(string $path): array|false {
+ $meta = $this->getMetaData($path);
+ return $meta ?: false;
+
+ }
+
+ public function getMimeType(string $path): string|false {
+ $meta = $this->getMetaData($path);
+ return $meta ? $meta['mimetype'] : false;
+ }
+
+ public function cleanPath(string $path): string {
if ($path === '') {
return $path;
}
@@ -649,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);
@@ -685,98 +666,55 @@ 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) {
- $this->init();
- $path = $this->cleanPath($path);
- $response = $this->propfind($path);
- if ($response === false) {
- return 0;
- }
- if (isset($response['{http://owncloud.org/ns}permissions'])) {
- return $this->parsePermissions($response['{http://owncloud.org/ns}permissions']);
- } elseif ($this->is_dir($path)) {
- return Constants::PERMISSION_ALL;
- } elseif ($this->file_exists($path)) {
- return Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE;
- } else {
- return 0;
- }
+ public function getPermissions(string $path): int {
+ $stat = $this->getMetaData($path);
+ return $stat ? $stat['permissions'] : 0;
}
- /** {@inheritdoc} */
- public function getETag($path) {
- $this->init();
- $path = $this->cleanPath($path);
- $response = $this->propfind($path);
- if ($response === false) {
- return null;
- }
- if (isset($response['{DAV:}getetag'])) {
- $etag = trim($response['{DAV:}getetag'], '"');
- if (strlen($etag) > 40) {
- $etag = md5($etag);
- }
- return $etag;
- }
- return parent::getEtag($path);
+ public function getETag(string $path): string|false {
+ $meta = $this->getMetaData($path);
+ 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 (strpos($permissionsString, 'R') !== false) {
+ if (str_contains($permissionsString, 'R')) {
$permissions |= Constants::PERMISSION_SHARE;
}
- if (strpos($permissionsString, 'D') !== false) {
+ if (str_contains($permissionsString, 'D')) {
$permissions |= Constants::PERMISSION_DELETE;
}
- if (strpos($permissionsString, 'W') !== false) {
+ if (str_contains($permissionsString, 'W')) {
$permissions |= Constants::PERMISSION_UPDATE;
}
- if (strpos($permissionsString, 'CK') !== false) {
+ if (str_contains($permissionsString, 'CK')) {
$permissions |= Constants::PERMISSION_CREATE;
$permissions |= Constants::PERMISSION_UPDATE;
}
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 {
@@ -837,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);
@@ -873,4 +811,31 @@ class DAV extends Common {
// TODO: only log for now, but in the future need to wrap/rethrow exception
}
+
+ public function getDirectoryContent(string $directory): \Traversable {
+ $this->init();
+ $directory = $this->cleanPath($directory);
+ try {
+ $responses = $this->client->propFind(
+ $this->encodePath($directory),
+ self::PROPFIND_PROPS,
+ 1
+ );
+
+ array_shift($responses); //the first entry is the current directory
+ if (!$this->statCache->hasKey($directory)) {
+ $this->statCache->set($directory, true);
+ }
+
+ foreach ($responses as $file => $response) {
+ $file = rawurldecode($file);
+ $file = substr($file, strlen($this->root));
+ $file = $this->cleanPath($file);
+ $this->statCache->set($file, $response);
+ yield $this->getMetaFromPropfind($file, $response);
+ }
+ } catch (\Exception $e) {
+ $this->convertException($e, $directory);
+ }
+ }
}
diff --git a/lib/private/Files/Storage/FailedStorage.php b/lib/private/Files/Storage/FailedStorage.php
index 18a2c9c2bb5..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;
@@ -34,186 +16,176 @@ use OCP\Lock\ILockingProvider;
* Storage placeholder to represent a missing precondition, storage unavailable
*/
class FailedStorage extends Common {
-
/** @var \Exception */
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) {
- throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
- }
-
- public function opendir($path) {
+ public function mkdir(string $path): never {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
- public function is_dir($path) {
+ public function rmdir(string $path): never {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
- public function is_file($path) {
+ public function opendir(string $path): never {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
- public function stat($path) {
+ public function is_dir(string $path): never {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
- public function filetype($path) {
+ public function is_file(string $path): never {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
- public function filesize($path) {
+ public function stat(string $path): never {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
- public function isCreatable($path) {
+ public function filetype(string $path): never {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
- public function isReadable($path) {
+ public function filesize(string $path): never {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
- public function isUpdatable($path) {
+ public function isCreatable(string $path): never {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
- public function isDeletable($path) {
+ public function isReadable(string $path): never {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
- public function isSharable($path) {
+ public function isUpdatable(string $path): never {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
- public function getPermissions($path) {
+ public function isDeletable(string $path): never {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
- public function file_exists($path) {
+ public function isSharable(string $path): never {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
- public function filemtime($path) {
+ public function getPermissions(string $path): never {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
- public function file_get_contents($path) {
+ public function file_exists(string $path): never {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
- public function file_put_contents($path, $data) {
+ public function filemtime(string $path): never {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
- public function unlink($path) {
+ public function file_get_contents(string $path): never {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
- public function rename($path1, $path2) {
+ public function file_put_contents(string $path, mixed $data): never {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
- public function copy($path1, $path2) {
+ public function unlink(string $path): never {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
- public function fopen($path, $mode) {
+ public function rename(string $source, string $target): never {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
- public function getMimeType($path) {
+ public function copy(string $source, string $target): never {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
- public function hash($type, $path, $raw = false) {
+ public function fopen(string $path, string $mode): never {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
- public function free_space($path) {
+ public function getMimeType(string $path): never {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
- public function search($query) {
+ public function hash(string $type, string $path, bool $raw = false): never {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
- public function touch($path, $mtime = null) {
+ public function free_space(string $path): never {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
- public function getLocalFile($path) {
+ public function touch(string $path, ?int $mtime = null): never {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
- public function getLocalFolder($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 5427bc425c2..91b8071ac30 100644
--- a/lib/private/Files/Storage/Home.php
+++ b/lib/private/Files/Storage/Home.php
@@ -1,31 +1,17 @@
<?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;
/**
* Specialized version of Local storage for home directory usage
@@ -44,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;
}
@@ -89,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() {
+ 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 ee8a8c7d161..260f9218a88 100644
--- a/lib/private/Files/Storage/Local.php
+++ b/lib/private/Files/Storage/Local.php
@@ -1,55 +1,24 @@
<?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 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;
use OC\Files\Filesystem;
+use OC\Files\Storage\Wrapper\Encryption;
use OC\Files\Storage\Wrapper\Jail;
use OCP\Constants;
use OCP\Files\ForbiddenException;
use OCP\Files\GenericFileException;
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;
/**
@@ -66,11 +35,17 @@ class Local extends \OC\Files\Storage\Common {
private IMimeTypeDetector $mimeTypeDetector;
- public function __construct($arguments) {
- if (!isset($arguments['datadir']) || !is_string($arguments['datadir'])) {
+ private $defUMask;
+
+ protected bool $unlinkOnTruncate;
+
+ protected bool $caseInsensitive = false;
+
+ 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;
@@ -78,30 +53,41 @@ class Local extends \OC\Files\Storage\Common {
$realPath = realpath($this->datadir) ?: $this->datadir;
$this->realDataDir = rtrim($realPath, '/') . '/';
}
- if (substr($this->datadir, -1) !== '/') {
+ if (!str_ends_with($this->datadir, '/')) {
$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($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('') . '"');
+ }
}
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(022);
+ $oldMask = umask($this->defUMask);
$result = @mkdir($sourcePath, 0777, true);
umask($oldMask);
return $result;
}
- public function rmdir($path) {
+ public function rmdir(string $path): bool {
if (!$this->isDeletable($path)) {
return false;
}
@@ -121,17 +107,18 @@ 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;
- } elseif ($file->isDir()) {
- rmdir($file->getPathname());
} elseif ($file->isFile() || $file->isLink()) {
unlink($file->getPathname());
+ } elseif ($file->isDir()) {
+ rmdir($file->getPathname());
}
$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) {
@@ -139,24 +126,33 @@ 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) {
- if (substr($path, -1) == '/') {
+ public function is_dir(string $path): bool {
+ if ($this->caseInsensitive && !$this->file_exists($path)) {
+ return false;
+ }
+ if (str_ends_with($path, '/')) {
$path = substr($path, 0, -1);
}
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)) {
+ return false;
+ }
$statResult = @stat($fullPath);
if (PHP_INT_SIZE === 4 && $statResult && !$this->is_dir($path)) {
$filesize = $this->filesize($path);
@@ -169,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) {
@@ -221,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)));
@@ -229,7 +222,7 @@ class Local extends \OC\Files\Storage\Common {
return $filetype;
}
- public function filesize($path) {
+ public function filesize(string $path): int|float|false {
if (!$this->is_file($path)) {
return 0;
}
@@ -241,19 +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) {
- return file_exists($this->getSourcePath($path));
+ public function file_exists(string $path): bool {
+ if ($this->caseInsensitive) {
+ $fullPath = $this->getSourcePath($path);
+ $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)) {
@@ -266,14 +269,14 @@ 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(022);
+ $oldMask = umask($this->defUMask);
if (!is_null($mtime)) {
$result = @touch($this->getSourcePath($path), $mtime);
} else {
@@ -287,18 +290,21 @@ 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) {
- $oldMask = umask(022);
+ public function file_put_contents(string $path, mixed $data): int|float|false {
+ $oldMask = umask($this->defUMask);
+ if ($this->unlinkOnTruncate) {
+ $this->unlink($path);
+ }
$result = file_put_contents($this->getSourcePath($path), $data);
umask($oldMask);
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)) {
@@ -308,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 */
@@ -318,72 +324,87 @@ class Local extends \OC\Files\Storage\Common {
}
}
- public function rename($path1, $path2) {
- $srcParent = dirname($path1);
- $dstParent = dirname($path2);
+ 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($path1)) {
- \OC::$server->get(LoggerInterface::class)->error('unable to rename, file does not exists : ' . $path1, ['app' => 'core']);
+ if (!$this->file_exists($source)) {
+ Server::get(LoggerInterface::class)->error('unable to rename, file does not exists : ' . $source, ['app' => 'core']);
return false;
}
- if ($this->is_dir($path2)) {
- $this->rmdir($path2);
- } elseif ($this->is_file($path2)) {
- $this->unlink($path2);
+ 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)) {
+ $this->checkTreeForForbiddenItems($this->getSourcePath($source));
}
- if ($this->is_dir($path1)) {
- // we can't move folders across devices, use copy instead
- $stat1 = stat(dirname($this->getSourcePath($path1)));
- $stat2 = stat(dirname($this->getSourcePath($path2)));
- if ($stat1['dev'] !== $stat2['dev']) {
- $result = $this->copy($path1, $path2);
- if ($result) {
- $result &= $this->rmdir($path1);
+ if (@rename($this->getSourcePath($source), $this->getSourcePath($target))) {
+ if ($this->caseInsensitive) {
+ if (mb_strtolower($target) === mb_strtolower($source) && !$this->file_exists($target)) {
+ return false;
}
- return $result;
}
-
- $this->checkTreeForForbiddenItems($this->getSourcePath($path1));
+ return true;
}
- return rename($this->getSourcePath($path1), $this->getSourcePath($path2));
+ return $this->copy($source, $target) && $this->unlink($source);
}
- public function copy($path1, $path2) {
- if ($this->is_dir($path1)) {
- return parent::copy($path1, $path2);
+ public function copy(string $source, string $target): bool {
+ if ($this->is_dir($source)) {
+ return parent::copy($source, $target);
} else {
- $oldMask = umask(022);
- $result = copy($this->getSourcePath($path1), $this->getSourcePath($path2));
+ $oldMask = umask($this->defUMask);
+ if ($this->unlinkOnTruncate) {
+ $this->unlink($target);
+ }
+ $result = copy($this->getSourcePath($source), $this->getSourcePath($target));
umask($oldMask);
+ if ($this->caseInsensitive) {
+ if (mb_strtolower($target) === mb_strtolower($source) && !$this->file_exists($target)) {
+ return false;
+ }
+ }
return $result;
}
}
- public function fopen($path, $mode) {
- $oldMask = umask(022);
- $result = fopen($this->getSourcePath($path), $mode);
+ public function fopen(string $path, string $mode) {
+ $sourcePath = $this->getSourcePath($path);
+ if (!file_exists($sourcePath) && $mode === 'r') {
+ return false;
+ }
+ $oldMask = umask($this->defUMask);
+ if (($mode === 'w' || $mode === 'w+') && $this->unlinkOnTruncate) {
+ $this->unlink($path);
+ }
+ $result = @fopen($sourcePath, $mode);
umask($oldMask);
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
@@ -392,31 +413,22 @@ class Local extends \OC\Files\Storage\Common {
// disk_free_space doesn't work on files
$sourcePath = dirname($sourcePath);
}
- $space = function_exists('disk_free_space') ? disk_free_space($sourcePath) : false;
+ $space = (function_exists('disk_free_space') && is_dir($sourcePath)) ? disk_free_space($sourcePath) : false;
if ($space === false || is_null($space)) {
return \OCP\Files\FileInfo::SPACE_UNKNOWN;
}
- return $space;
+ return Util::numericToNumber($space);
}
- public function search($query) {
+ public function search(string $query): array {
return $this->searchInDir($query);
}
- public function getLocalFile($path) {
- return $this->getSourcePath($path);
- }
-
- public function getLocalFolder($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) {
@@ -435,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 {
@@ -453,18 +458,16 @@ 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);
}
$fullPath = $this->datadir . $path;
$currentPath = $path;
- $allowSymlinks = $this->config->getSystemValue('localstorage.allowsymlinks', false);
+ $allowSymlinks = $this->config->getSystemValueBool('localstorage.allowsymlinks', false);
if ($allowSymlinks || $currentPath === '') {
return $fullPath;
}
@@ -472,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;
}
@@ -484,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 {
@@ -531,23 +526,28 @@ class Local extends \OC\Files\Storage\Common {
}
}
- /**
- * @param IStorage $sourceStorage
- * @param string $sourceInternalPath
- * @param string $targetInternalPath
- * @param bool $preserveMtime
- * @return bool
- */
- public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime = false) {
- // 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.
- if ($sourceStorage->instanceOfStorage(Local::class) && !$sourceStorage->instanceOfStorage('OCA\GroupFolders\ACL\ACLStorageWrapper')) {
- if ($sourceStorage->instanceOfStorage(Jail::class)) {
+ 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);
+ }
+
+ public function copyFromStorage(IStorage $sourceStorage, string $sourceInternalPath, string $targetInternalPath, bool $preserveMtime = false): bool {
+ if ($this->canDoCrossStorageMove($sourceStorage)) {
+ // 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
@@ -559,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) {
- if ($sourceStorage->instanceOfStorage(Local::class)) {
- if ($sourceStorage->instanceOfStorage(Jail::class)) {
+ public function moveFromStorage(IStorage $sourceStorage, string $sourceInternalPath, string $targetInternalPath): bool {
+ if ($this->canDoCrossStorageMove($sourceStorage)) {
+ // 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
@@ -583,7 +579,8 @@ 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)) {
fclose($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 2a1338148f5..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.
@@ -35,33 +21,21 @@ namespace OC\Files\Storage;
* in classes which extend it, e.g. $this->stat() .
*/
trait LocalTempFileTrait {
+ /** @var array<string,string|false> */
+ protected array $cachedFiles = [];
- /** @var string[] */
- protected $cachedFiles = [];
-
- /**
- * @param string $path
- * @return string
- */
- protected function getCachedFile($path) {
+ protected function getCachedFile(string $path): string|false {
if (!isset($this->cachedFiles[$path])) {
$this->cachedFiles[$path] = $this->toTmpFile($path);
}
return $this->cachedFiles[$path];
}
- /**
- * @param string $path
- */
- protected function removeCachedFile($path) {
+ protected function removeCachedFile(string $path): void {
unset($this->cachedFiles[$path]);
}
- /**
- * @param string $path
- * @return string
- */
- protected function toTmpFile($path) { //no longer in the storage api, still useful here
+ protected function toTmpFile(string $path): string|false { //no longer in the storage api, still useful here
$source = $this->fopen($path, 'r');
if (!$source) {
return false;
@@ -73,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 7fd418f6dca..2f6167ef85e 100644
--- a/lib/private/Files/Storage/PolyFill/CopyDirectory.php
+++ b/lib/private/Files/Storage/PolyFill/CopyDirectory.php
@@ -1,92 +1,59 @@
<?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($path1, $path2) {
- if ($this->is_dir($path1)) {
- if ($this->file_exists($path2)) {
- $this->unlink($path2);
+ public function copy(string $source, string $target): bool {
+ if ($this->is_dir($source)) {
+ if ($this->file_exists($target)) {
+ $this->unlink($target);
}
- $this->mkdir($path2);
- return $this->copyRecursive($path1, $path2);
+ $this->mkdir($target);
+ return $this->copyRecursive($source, $target);
} else {
- return parent::copy($path1, $path2);
+ return parent::copy($source, $target);
}
}
/**
* 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 cc317de2669..aa17c12b309 100644
--- a/lib/private/Files/Storage/Storage.php
+++ b/lib/private/Files/Storage/Storage.php
@@ -1,131 +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 {
+interface Storage extends IStorage, ILockingStorage {
+ public function getCache(string $path = '', ?IStorage $storage = null): ICache;
- /**
- * 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 getScanner(string $path = '', ?IStorage $storage = null): IScanner;
- /**
- * 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 getWatcher(string $path = '', ?IStorage $storage = null): IWatcher;
+ public function getPropagator(?IStorage $storage = null): IPropagator;
- /**
- * get the user id of the owner of a file or folder
- *
- * @param string $path
- * @return string
- */
- public function getOwner($path);
+ public function getUpdater(?IStorage $storage = null): IUpdater;
- /**
- * 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 getStorageCache(): \OC\Files\Cache\Storage;
- /**
- * 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);
-
- /**
- * 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);
-
- /**
- * @return \OC\Files\Cache\Storage
- */
- public function getStorageCache();
-
- /**
- * @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);
-
- /**
- * @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);
-
- /**
- * @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
@@ -136,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 910ea369757..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) {
- $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($path1, $path2) {
- $this->checkAvailability();
- try {
- return parent::rename($path1, $path2);
- } catch (StorageNotAvailableException $e) {
- $this->setUnavailable($e);
- }
+ public function unlink(string $path): bool {
+ return $this->handleAvailability('unlink', $path);
}
- /** {@inheritdoc} */
- public function copy($path1, $path2) {
- $this->checkAvailability();
- try {
- return parent::copy($path1, $path2);
- } 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,66 +207,45 @@ 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;
}
}
/**
+ * @template T of StorageNotAvailableException|null
+ * @param T $e
+ * @psalm-return (T is null ? void : never)
* @throws StorageNotAvailableException
*/
protected function setUnavailable(?StorageNotAvailableException $e): void {
@@ -470,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 d6201dc8877..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\Cache\CappedMemoryCache;
use OC\Files\Filesystem;
+use OCP\Cache\CappedMemoryCache;
+use OCP\Files\Cache\IScanner;
use OCP\Files\Storage\IStorage;
use OCP\ICache;
@@ -40,7 +20,6 @@ use OCP\ICache;
* the actual given name and then try its NFD form.
*/
class Encoding extends Wrapper {
-
/**
* @var ICache
*/
@@ -49,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);
}
@@ -70,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;
@@ -98,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;
@@ -127,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) {
@@ -142,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]);
@@ -156,178 +116,72 @@ class Encoding extends Wrapper {
return $result;
}
- /**
- * see https://www.php.net/manual/en/function.opendir.php
- *
- * @param string $path
- * @return resource|bool
- */
- 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
- *
- * @param string $path
- * @return int|bool
- */
- public function filesize($path) {
+ 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|bool
- */
- 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|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]);
@@ -335,37 +189,16 @@ class Encoding extends Wrapper {
return $result;
}
- /**
- * see https://www.php.net/manual/en/function.rename.php
- *
- * @param string $path1
- * @param string $path2
- * @return bool
- */
- public function rename($path1, $path2) {
+ public function rename(string $source, string $target): bool {
// second name always NFC
- return $this->storage->rename($this->findPathToUse($path1), $this->findPathToUse($path2));
+ return $this->storage->rename($this->findPathToUse($source), $this->findPathToUse($target));
}
- /**
- * see https://www.php.net/manual/en/function.copy.php
- *
- * @param string $path1
- * @param string $path2
- * @return bool
- */
- public function copy($path1, $path2) {
- return $this->storage->copy($this->findPathToUse($path1), $this->findPathToUse($path2));
+ 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]);
@@ -373,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|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|bool
- */
- 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|bool
- */
- 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));
}
@@ -509,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) {
@@ -533,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 935a15af4cf..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,10 +13,7 @@ use OC\Files\Filesystem;
* Normalize file names while reading directory entries
*/
class EncodingDirectoryWrapper extends DirectoryWrapper {
- /**
- * @return string
- */
- public function dir_readdir() {
+ public function dir_readdir(): string|false {
$file = readdir($this->source);
if ($file !== false && $file !== '.' && $file !== '..') {
$file = trim(Filesystem::normalizePath($file), '/');
@@ -44,8 +24,7 @@ class EncodingDirectoryWrapper extends DirectoryWrapper {
/**
* @param resource $source
- * @param callable $filter
- * @return resource|bool
+ * @return resource|false
*/
public static function wrap($source) {
return self::wrapSource($source, [
diff --git a/lib/private/Files/Storage/Wrapper/Encryption.php b/lib/private/Files/Storage/Wrapper/Encryption.php
index 4cfe932cc9f..58bd4dfddcf 100644
--- a/lib/private/Files/Storage/Wrapper/Encryption.php
+++ b/lib/private/Files/Storage/Wrapper/Encryption.php
@@ -1,53 +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>
- *
- * @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\Encryption\Exceptions\GenericEncryptionException;
+use OCP\Cache\CappedMemoryCache;
+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;
@@ -55,107 +31,74 @@ 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 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
- *
- * @param string $path
- * @return int
- */
- public function filesize($path) {
+ public function filesize(string $path): int|float|false {
$fullPath = $this->getFullPath($path);
- /** @var CacheEntry $info */
$info = $this->getCache()->get($path);
+ if ($info === 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];
- // update file cache
- if ($info instanceof ICacheEntry) {
- $info = $info->getData();
- $info['encrypted'] = $info['encryptedVersion'];
- } else {
- if (!is_array($info)) {
- $info = [];
+
+ // Update file cache (only if file is already cached).
+ // Certain files are not cached (e.g. *.part).
+ if (isset($info['fileid'])) {
+ if ($info instanceof ICacheEntry) {
+ $info['encrypted'] = $info['encryptedVersion'];
+ } else {
+ /**
+ * @psalm-suppress RedundantCondition
+ */
+ if (!is_array($info)) {
+ $info = [];
+ }
+ $info['encrypted'] = true;
+ $info = new CacheEntry($info);
}
- $info['encrypted'] = true;
- }
- $info['size'] = $size;
- $this->getCache()->put($path, $info);
+ if ($size !== $info->getUnencryptedSize()) {
+ $this->getCache()->update($info->getId(), [
+ 'unencrypted_size' => $size
+ ]);
+ }
+ }
return $size;
}
if (isset($info['fileid']) && $info['encrypted']) {
- return $this->verifyUnencryptedSize($path, $info['size']);
+ return $this->verifyUnencryptedSize($path, $info->getUnencryptedSize());
}
return $this->storage->filesize($path);
@@ -168,10 +111,12 @@ class Encryption extends Wrapper {
if (isset($this->unencryptedSize[$fullPath])) {
$data['encrypted'] = true;
$data['size'] = $this->unencryptedSize[$fullPath];
+ $data['unencrypted_size'] = $data['size'];
} else {
if (isset($info['fileid']) && $info['encrypted']) {
- $data['size'] = $this->verifyUnencryptedSize($path, $info['size']);
+ $data['size'] = $this->verifyUnencryptedSize($path, $info->getUnencryptedSize());
$data['encrypted'] = true;
+ $data['unencrypted_size'] = $data['size'];
}
}
@@ -182,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;
@@ -190,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
- */
- 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;
}
@@ -218,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)) {
@@ -237,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);
@@ -257,31 +183,24 @@ class Encryption extends Wrapper {
return $this->storage->unlink($path);
}
- /**
- * see https://www.php.net/manual/en/function.rename.php
- *
- * @param string $path1
- * @param string $path2
- * @return bool
- */
- public function rename($path1, $path2) {
- $result = $this->storage->rename($path1, $path2);
+ 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($path2) === false &&
- $this->encryptionManager->isEnabled()) {
- $source = $this->getFullPath($path1);
- if (!$this->util->isExcluded($source)) {
- $target = $this->getFullPath($path2);
- if (isset($this->unencryptedSize[$source])) {
- $this->unencryptedSize[$target] = $this->unencryptedSize[$source];
+ && $this->isVersion($target) === false
+ && $this->encryptionManager->isEnabled()) {
+ $sourcePath = $this->getFullPath($source);
+ if (!$this->util->isExcluded($sourcePath)) {
+ $targetPath = $this->getFullPath($target);
+ if (isset($this->unencryptedSize[$sourcePath])) {
+ $this->unencryptedSize[$targetPath] = $this->unencryptedSize[$sourcePath];
}
- $this->keyStorage->renameKeys($source, $target);
- $module = $this->getEncryptionModule($path2);
+ $this->keyStorage->renameKeys($sourcePath, $targetPath);
+ $module = $this->getEncryptionModule($target);
if ($module) {
- $module->update($target, $this->uid, []);
+ $module->update($targetPath, $this->uid, []);
}
}
}
@@ -289,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);
}
@@ -308,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);
@@ -331,37 +238,20 @@ class Encryption extends Wrapper {
return $this->storage->isReadable($path) && $isReadable;
}
- /**
- * see https://www.php.net/manual/en/function.copy.php
- *
- * @param string $path1
- * @param string $path2
- * @return bool
- */
- public function copy($path1, $path2) {
- $source = $this->getFullPath($path1);
+ public function copy(string $source, string $target): bool {
+ $sourcePath = $this->getFullPath($source);
- if ($this->util->isExcluded($source)) {
- return $this->storage->copy($path1, $path2);
+ if ($this->util->isExcluded($sourcePath)) {
+ return $this->storage->copy($source, $target);
}
// need to stream copy file by file in case we copy between a encrypted
// and a unencrypted storage
- $this->unlink($path2);
- return $this->copyFromStorage($this, $path1, $path2);
+ $this->unlink($target);
+ 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
@@ -370,6 +260,10 @@ class Encryption extends Wrapper {
return $this->storage->fopen($path, $mode);
}
+ if (!$this->enabled) {
+ return $this->storage->fopen($path, $mode);
+ }
+
$encryptionEnabled = $this->encryptionManager->isEnabled();
$shouldEncrypt = false;
$encryptionModule = null;
@@ -410,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
@@ -428,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
@@ -452,7 +344,18 @@ 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;
@@ -470,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
@@ -478,12 +381,13 @@ class Encryption extends Wrapper {
*
* @return int unencrypted size
*/
- protected function verifyUnencryptedSize($path, $unencryptedSize) {
+ protected function verifyUnencryptedSize(string $path, int $unencryptedSize): int {
$size = $this->storage->filesize($path);
$result = $unencryptedSize;
- if ($unencryptedSize < 0 ||
- ($size > 0 && $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
@@ -507,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($path, $size, $unencryptedSize) {
+ protected function fixUnencryptedSize(string $path, int $size, int $unencryptedSize): int|float {
$headerSize = $this->getHeaderSize($path);
$header = $this->getHeader($path);
$encryptionModule = $this->getEncryptionModule($path);
@@ -579,10 +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'], ['size' => $newUnencryptedSize]);
- }
+ $entry = $cache->get($path);
+ $cache->update($entry['fileid'], [
+ 'unencrypted_size' => $newUnencryptedSize
+ ]);
return $newUnencryptedSize;
}
@@ -596,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.
*/
@@ -614,14 +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) {
+ public function moveFromStorage(
+ Storage\IStorage $sourceStorage,
+ string $sourceInternalPath,
+ string $targetInternalPath,
+ $preserveMtime = true,
+ ): bool {
if ($sourceStorage === $this) {
return $this->rename($sourceInternalPath, $targetInternalPath);
}
@@ -638,26 +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, $preserveMtime = false, $isRename = false) {
-
+ public function copyFromStorage(
+ Storage\IStorage $sourceStorage,
+ string $sourceInternalPath,
+ string $targetInternalPath,
+ $preserveMtime = 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
@@ -669,14 +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) {
+ private function updateEncryptedVersion(
+ Storage\IStorage $sourceStorage,
+ string $sourceInternalPath,
+ string $targetInternalPath,
+ bool $isRename,
+ bool $keepEncryptionVersion,
+ ): void {
$isEncrypted = $this->encryptionManager->isEnabled() && $this->shouldEncrypt($targetInternalPath);
$cacheInformation = [
'encrypted' => $isEncrypted,
@@ -716,21 +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) {
-
+ private function copyBetweenStorage(
+ Storage\IStorage $sourceStorage,
+ 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
@@ -743,7 +649,7 @@ class Encryption extends Wrapper {
if (isset($info['encrypted']) && $info['encrypted'] === true) {
$this->updateUnencryptedSize(
$this->getFullPath($targetInternalPath),
- $info['size']
+ $info->getUnencryptedSize()
);
}
$this->updateEncryptedVersion($sourceStorage, $sourceInternalPath, $targetInternalPath, $isRename, true);
@@ -753,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);
@@ -771,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);
}
}
}
@@ -781,17 +686,18 @@ class Encryption extends Wrapper {
try {
$source = $sourceStorage->fopen($sourceInternalPath, 'r');
$target = $this->fopen($targetInternalPath, 'w');
- [, $result] = \OC_Helper::streamCopy($source, $target);
- fclose($source);
- fclose($target);
- } catch (\Exception $e) {
- if (is_resource($source)) {
+ if ($source === false || $target === false) {
+ $result = false;
+ } else {
+ [, $result] = Files::streamCopy($source, $target, true);
+ }
+ } finally {
+ if (isset($source) && $source !== false) {
fclose($source);
}
- if (is_resource($target)) {
+ if (isset($target) && $target !== false) {
fclose($target);
}
- throw $e;
}
if ($result) {
if ($preserveMtime) {
@@ -808,14 +714,7 @@ class Encryption extends Wrapper {
return (bool)$result;
}
- /**
- * 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
- */
- public function getLocalFile($path) {
+ public function getLocalFile(string $path): string|false {
if ($this->encryptionManager->isEnabled()) {
$cachedFile = $this->getCachedFile($path);
if (is_string($cachedFile)) {
@@ -825,27 +724,18 @@ class Encryption extends Wrapper {
return $this->storage->getLocalFile($path);
}
- /**
- * Returns the wrapped storage's value for isLocal()
- *
- * @return bool wrapped storage's isLocal() value
- */
- public function isLocal() {
+ public function isLocal(): bool {
if ($this->encryptionManager->isEnabled()) {
return false;
}
return $this->storage->isLocal();
}
- /**
- * 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
- */
- public function stat($path) {
+ public function stat(string $path): array|false {
$stat = $this->storage->stat($path);
+ if (!$stat) {
+ return false;
+ }
$fileSize = $this->filesize($path);
$stat['size'] = $fileSize;
$stat[7] = $fileSize;
@@ -853,16 +743,11 @@ class Encryption extends Wrapper {
return $stat;
}
- /**
- * see https://www.php.net/manual/en/function.hash.php
- *
- * @param string $type
- * @param string $path
- * @param bool $raw
- * @return string
- */
- 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);
@@ -875,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);
}
@@ -898,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)) {
@@ -910,7 +792,7 @@ class Encryption extends Wrapper {
}
$firstBlock = $this->readFirstBlock($path);
- if (substr($firstBlock, 0, strlen(Util::HEADER_START)) === Util::HEADER_START) {
+ if (str_starts_with($firstBlock, Util::HEADER_START)) {
$headerSize = $this->util->getHeaderSize();
}
@@ -918,40 +800,9 @@ class Encryption extends Wrapper {
}
/**
- * parse raw header to array
- *
- * @param string $rawHeader
- * @return array
- */
- protected function parseRawHeader($rawHeader) {
- $result = [];
- if (substr($rawHeader, 0, strlen(Util::HEADER_START)) === Util::HEADER_START) {
- $header = $rawHeader;
- $endAt = strpos($header, Util::HEADER_END);
- if ($endAt !== false) {
- $header = substr($header, 0, $endAt + strlen(Util::HEADER_END));
-
- // +1 to not start with an ':' which would result in empty element at the beginning
- $exploded = explode(':', substr($header, strlen(Util::HEADER_START) + 1));
-
- $element = array_shift($exploded);
- while ($element !== Util::HEADER_END) {
- $result[$element] = array_shift($exploded);
- $element = array_shift($exploded);
- }
- }
- }
-
- return $result;
- }
-
- /**
* 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) {
@@ -960,13 +811,15 @@ class Encryption extends Wrapper {
$result = [];
- // first check if it is an encrypted file at all
- // We would do query to filecache only if we know that entry in filecache exists
+ $isEncrypted = $this->encryptedPaths->get($realFile);
+ if (is_null($isEncrypted)) {
+ $info = $this->getCache()->get($path);
+ $isEncrypted = isset($info['encrypted']) && $info['encrypted'] === true;
+ }
- $info = $this->getCache()->get($path);
- if (isset($info['encrypted']) && $info['encrypted'] === true) {
+ if ($isEncrypted) {
$firstBlock = $this->readFirstBlock($path);
- $result = $this->parseRawHeader($firstBlock);
+ $result = $this->util->parseRawHeader($firstBlock);
// if the header doesn't contain a encryption module we check if it is a
// legacy file. If true, we add the default encryption module
@@ -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,12 +902,45 @@ 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);
+
+ // object store, stores the size after write and doesn't update this during scan
+ // manually store the unencrypted size
+ if ($result && $this->getWrapperStorage()->instanceOfStorage(ObjectStoreStorage::class) && $this->shouldEncrypt($path)) {
+ $this->getCache()->put($path, ['unencrypted_size' => $count]);
+ }
+
return $count;
}
+
+ public function clearIsEncryptedCache(): void {
+ $this->encryptedPaths->clear();
+ }
+
+ /**
+ * Allow temporarily disabling the wrapper
+ */
+ 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 65ee6f1181a..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,32 +31,32 @@ 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 && strpos($path, $root) !== 0) {
+ if ($path !== $this->rootPath && !str_starts_with($path, $root)) {
return null;
} else {
$path = substr($path, strlen($this->rootPath));
@@ -80,435 +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|bool
- */
- 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
- *
- * @param string $path
- * @return int|bool
- */
- public function filesize($path) {
+ 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|bool
- */
- 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|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 $path1
- * @param string $path2
- * @return bool
- */
- public function rename($path1, $path2) {
- return $this->getWrapperStorage()->rename($this->getUnjailedPath($path1), $this->getUnjailedPath($path2));
+ 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 $path1
- * @param string $path2
- * @return bool
- */
- public function copy($path1, $path2) {
- return $this->getWrapperStorage()->copy($this->getUnjailedPath($path1), $this->getUnjailedPath($path2));
+ 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|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|bool
- */
- 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) {
- if (!$storage) {
- $storage = $this->getWrapperStorage();
- }
- $sourceCache = $this->getWrapperStorage()->getCache($this->getUnjailedPath($path), $storage);
+ 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|bool
- */
- 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;
}
@@ -520,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
new file mode 100644
index 00000000000..657c6c9250c
--- /dev/null
+++ b/lib/private/Files/Storage/Wrapper/KnownMtime.php
@@ -0,0 +1,146 @@
+<?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;
+use OCP\Files\Storage\IStorage;
+use Psr\Clock\ClockInterface;
+
+/**
+ * Wrapper that overwrites the mtime return by stat/getMetaData if the returned value
+ * is lower than when we last modified the file.
+ *
+ * This is useful because some storage servers can return an outdated mtime right after writes
+ */
+class KnownMtime extends Wrapper {
+ private CappedMemoryCache $knowMtimes;
+ private ClockInterface $clock;
+
+ public function __construct(array $parameters) {
+ parent::__construct($parameters);
+ $this->knowMtimes = new CappedMemoryCache();
+ $this->clock = $parameters['clock'];
+ }
+
+ 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();
+ $this->knowMtimes->set($path, $this->clock->now()->getTimestamp());
+ }
+ return $result;
+ }
+
+ public function stat(string $path): array|false {
+ $stat = parent::stat($path);
+ if ($stat) {
+ $this->applyKnownMtime($path, $stat);
+ }
+ return $stat;
+ }
+
+ public function getMetaData(string $path): ?array {
+ $stat = parent::getMetaData($path);
+ if ($stat) {
+ $this->applyKnownMtime($path, $stat);
+ }
+ return $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(string $path): int|false {
+ $knownMtime = $this->knowMtimes->get($path) ?? 0;
+ return max(parent::filemtime($path), $knownMtime);
+ }
+
+ public function mkdir(string $path): bool {
+ $result = parent::mkdir($path);
+ if ($result) {
+ $this->knowMtimes->set($path, $this->clock->now()->getTimestamp());
+ }
+ return $result;
+ }
+
+ public function rmdir(string $path): bool {
+ $result = parent::rmdir($path);
+ if ($result) {
+ $this->knowMtimes->set($path, $this->clock->now()->getTimestamp());
+ }
+ return $result;
+ }
+
+ public function unlink(string $path): bool {
+ $result = parent::unlink($path);
+ if ($result) {
+ $this->knowMtimes->set($path, $this->clock->now()->getTimestamp());
+ }
+ return $result;
+ }
+
+ public function rename(string $source, string $target): bool {
+ $result = parent::rename($source, $target);
+ if ($result) {
+ $this->knowMtimes->set($target, $this->clock->now()->getTimestamp());
+ $this->knowMtimes->set($source, $this->clock->now()->getTimestamp());
+ }
+ return $result;
+ }
+
+ public function copy(string $source, string $target): bool {
+ $result = parent::copy($source, $target);
+ if ($result) {
+ $this->knowMtimes->set($target, $this->clock->now()->getTimestamp());
+ }
+ return $result;
+ }
+
+ public function fopen(string $path, string $mode) {
+ $result = parent::fopen($path, $mode);
+ if ($result && $mode === 'w') {
+ $this->knowMtimes->set($path, $this->clock->now()->getTimestamp());
+ }
+ return $result;
+ }
+
+ 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());
+ }
+ return $result;
+ }
+
+ 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());
+ }
+ return $result;
+ }
+
+ 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());
+ }
+ return $result;
+ }
+
+ 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());
+ }
+ return $result;
+ }
+}
diff --git a/lib/private/Files/Storage/Wrapper/PermissionsMask.php b/lib/private/Files/Storage/Wrapper/PermissionsMask.php
index e54d3bb721a..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,75 +25,75 @@ 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($path1, $path2) {
+ public function rename(string $source, string $target): bool {
//This is a rename of the transfer file to the original file
- if (dirname($path1) === dirname($path2) && strpos($path1, '.ocTransferId') > 0) {
- return $this->checkMask(Constants::PERMISSION_CREATE) and parent::rename($path1, $path2);
+ if (dirname($source) === dirname($target) && strpos($source, '.ocTransferId') > 0) {
+ return $this->checkMask(Constants::PERMISSION_CREATE) and parent::rename($source, $target);
}
- return $this->checkMask(Constants::PERMISSION_UPDATE) and parent::rename($path1, $path2);
+ return $this->checkMask(Constants::PERMISSION_UPDATE) and parent::rename($source, $target);
}
- public function copy($path1, $path2) {
- return $this->checkMask(Constants::PERMISSION_CREATE) and parent::copy($path1, $path2);
+ 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,26 +110,26 @@ 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'])) {
- $data['scan_permissions'] = isset($data['scan_permissions']) ? $data['scan_permissions'] : $data['permissions'];
+ $data['scan_permissions'] = $data['scan_permissions'] ?? $data['permissions'];
$data['permissions'] &= $this->mask;
}
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'] = isset($data['scan_permissions']) ? $data['scan_permissions'] : $data['permissions'];
+ $data['scan_permissions'] = $data['scan_permissions'] ?? $data['permissions'];
$data['permissions'] &= $this->mask;
yield $data;
diff --git a/lib/private/Files/Storage/Wrapper/Quota.php b/lib/private/Files/Storage/Wrapper/Quota.php
index 4cd0a5e0b4a..35a265f8c8e 100644
--- a/lib/private/Files/Storage/Wrapper/Quota.php
+++ b/lib/private/Files/Storage/Wrapper/Quota.php
@@ -1,177 +1,137 @@
<?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;
use OC\Files\Filesystem;
+use OC\SystemConfig;
use OCP\Files\Cache\ICacheEntry;
+use OCP\Files\FileInfo;
use OCP\Files\Storage\IStorage;
class Quota extends Wrapper {
-
- /**
- * @var int $quota
- */
- protected $quota;
-
- /**
- * @var string $sizeRoot
- */
- protected $sizeRoot;
-
- private $config;
+ /** @var callable|null */
+ protected $quotaCallback;
+ /** @var int|float|null int on 64bits, float on 32bits for bigint */
+ protected int|float|null $quota;
+ 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'];
- $this->sizeRoot = isset($parameters['root']) ? $parameters['root'] : '';
- $this->config = \OC::$server->getSystemConfig();
+ $this->quota = $parameters['quota'] ?? null;
+ $this->quotaCallback = $parameters['quotaCallback'] ?? null;
+ $this->sizeRoot = $parameters['root'] ?? '';
+ $this->quotaIncludeExternalStorage = $parameters['include_external_storage'] ?? false;
}
- /**
- * @return int quota value
- */
- public function getQuota() {
+ public function getQuota(): int|float {
+ if ($this->quota === null) {
+ $quotaCallback = $this->quotaCallback;
+ if ($quotaCallback === null) {
+ throw new \Exception('No quota or quota callback provider');
+ }
+ $this->quota = $quotaCallback();
+ }
+
return $this->quota;
}
- /**
- * @param string $path
- * @param \OC\Files\Storage\Storage $storage
- */
- protected function getSize($path, $storage = null) {
- if ($this->config->getValue('quota_include_external_storage', false)) {
+ private function hasQuota(): bool {
+ if (!$this->enabled) {
+ return false;
+ }
+ return $this->getQuota() !== FileInfo::SPACE_UNLIMITED;
+ }
+
+ protected function getSize(string $path, ?IStorage $storage = null): int|float {
+ if ($this->quotaIncludeExternalStorage) {
$rootInfo = Filesystem::getFileInfo('', 'ext');
if ($rootInfo) {
return $rootInfo->getSize(true);
}
- return \OCP\Files\FileInfo::SPACE_NOT_COMPUTED;
+ return FileInfo::SPACE_NOT_COMPUTED;
} else {
- if (is_null($storage)) {
- $cache = $this->getCache();
- } else {
- $cache = $storage->getCache();
- }
+ $cache = is_null($storage) ? $this->getCache() : $storage->getCache();
$data = $cache->get($path);
- if ($data instanceof ICacheEntry and isset($data['size'])) {
+ if ($data instanceof ICacheEntry && isset($data['size'])) {
return $data['size'];
} else {
- return \OCP\Files\FileInfo::SPACE_NOT_COMPUTED;
+ return FileInfo::SPACE_NOT_COMPUTED;
}
}
}
- /**
- * Get free space as limited by the quota
- *
- * @param string $path
- * @return int|bool
- */
- public function free_space($path) {
- if ($this->quota < 0 || strpos($path, 'cache') === 0 || strpos($path, 'uploads') === 0) {
+ public function free_space(string $path): int|float|false {
+ if (!$this->hasQuota()) {
+ return $this->storage->free_space($path);
+ }
+ if ($this->getQuota() < 0 || str_starts_with($path, 'cache') || str_starts_with($path, 'uploads')) {
return $this->storage->free_space($path);
} else {
$used = $this->getSize($this->sizeRoot);
if ($used < 0) {
- return \OCP\Files\FileInfo::SPACE_NOT_COMPUTED;
+ return FileInfo::SPACE_NOT_COMPUTED;
} else {
$free = $this->storage->free_space($path);
- $quotaFree = max($this->quota - $used, 0);
+ $quotaFree = max($this->getQuota() - $used, 0);
// if free space is known
- if ($free >= 0) {
- $free = min($free, $quotaFree);
- } else {
- $free = $quotaFree;
- }
+ $free = $free >= 0 ? min($free, $quotaFree) : $quotaFree;
return $free;
}
}
}
- /**
- * 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 {
+ if (!$this->hasQuota()) {
+ return $this->storage->file_put_contents($path, $data);
+ }
$free = $this->free_space($path);
- if ($free < 0 or strlen($data) < $free) {
+ if ($free < 0 || strlen($data) < $free) {
return $this->storage->file_put_contents($path, $data);
} else {
return false;
}
}
- /**
- * 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);
+ }
$free = $this->free_space($target);
- if ($free < 0 or $this->getSize($source) < $free) {
+ if ($free < 0 || $this->getSize($source) < $free) {
return $this->storage->copy($source, $target);
} else {
return false;
}
}
- /**
- * 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);
+ }
$source = $this->storage->fopen($path, $mode);
// don't apply quota for part files
if (!$this->isPartFile($path)) {
$free = $this->free_space($path);
- if ($source && is_int($free) && $free >= 0 && $mode !== 'r' && $mode !== 'rb') {
+ if ($source && (is_int($free) || is_float($free)) && $free >= 0 && $mode !== 'r' && $mode !== 'rb') {
// only apply quota for files, not metadata, trash or others
if ($this->shouldApplyQuota($path)) {
return \OC\Files\Stream\Quota::wrap($source, $free);
}
}
}
+
return $source;
}
@@ -179,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 string File path without .part extension
* @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');
@@ -191,55 +150,59 @@ class Quota extends Wrapper {
/**
* Only apply quota for files, not metadata, trash or others
*/
- private function shouldApplyQuota(string $path): bool {
- return strpos(ltrim($path, '/'), 'files/') === 0;
+ 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);
+ }
$free = $this->free_space($targetInternalPath);
- if ($free < 0 or $this->getSize($sourceInternalPath, $sourceStorage) < $free) {
+ if ($free < 0 || $this->getSize($sourceInternalPath, $sourceStorage) < $free) {
return $this->storage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
} else {
return false;
}
}
- /**
- * @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);
+ }
$free = $this->free_space($targetInternalPath);
- if ($free < 0 or $this->getSize($sourceInternalPath, $sourceStorage) < $free) {
+ if ($free < 0 || $this->getSize($sourceInternalPath, $sourceStorage) < $free) {
return $this->storage->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
} else {
return false;
}
}
- public function mkdir($path) {
+ public function mkdir(string $path): bool {
+ if (!$this->hasQuota()) {
+ return $this->storage->mkdir($path);
+ }
$free = $this->free_space($path);
- if ($this->shouldApplyQuota($path) && $free === 0.0) {
+ if ($this->shouldApplyQuota($path) && $free == 0) {
return false;
}
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);
+ }
$free = $this->free_space($path);
- if ($free === 0.0) {
+ if ($free == 0) {
return false;
}
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 6bc66bf9c89..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,444 +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|bool
- */
- 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
- *
- * @param string $path
- * @return int|bool
- */
- public function filesize($path) {
+ 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|bool
- */
- 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|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 $path1
- * @param string $path2
- * @return bool
- */
- public function rename($path1, $path2) {
- return $this->getWrapperStorage()->rename($path1, $path2);
+ 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 $path1
- * @param string $path2
- * @return bool
- */
- public function copy($path1, $path2) {
- return $this->getWrapperStorage()->copy($path1, $path2);
+ 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|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|bool
- */
- 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|bool
- */
- 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';
@@ -502,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) {
@@ -519,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);
}
@@ -581,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);
}
@@ -595,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);
+ }
}