migrate therinaldos.com data
Build & Deploy to DigitalOcean Space / build (push) Failing after 2m38s

This commit is contained in:
2024-05-05 15:50:45 -04:00
commit ef1ff240d4
23182 changed files with 3801898 additions and 0 deletions
@@ -0,0 +1,910 @@
<?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/>
*
*/
namespace OC\Files\Storage;
use OC\Files\Cache\Cache;
use OC\Files\Cache\Propagator;
use OC\Files\Cache\Scanner;
use OC\Files\Cache\Updater;
use OC\Files\Cache\Watcher;
use OC\Files\Filesystem;
use OC\Files\Storage\Wrapper\Jail;
use OC\Files\Storage\Wrapper\Wrapper;
use OCP\Files\EmptyFileNameException;
use OCP\Files\FileNameTooLongException;
use OCP\Files\ForbiddenException;
use OCP\Files\GenericFileException;
use OCP\Files\InvalidCharacterInPathException;
use OCP\Files\InvalidDirectoryException;
use OCP\Files\InvalidPathException;
use OCP\Files\ReservedWordException;
use OCP\Files\Storage\ILockingStorage;
use OCP\Files\Storage\IStorage;
use OCP\Files\Storage\IWriteStreamStorage;
use OCP\Lock\ILockingProvider;
use OCP\Lock\LockedException;
use Psr\Log\LoggerInterface;
/**
* Storage backend class for providing common filesystem operation methods
* which are not storage-backend specific.
*
* \OC\Files\Storage\Common is never used directly; it is extended by all other
* storage backends, where its methods may be overridden, and additional
* (backend-specific) methods are defined.
*
* 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 {
use LocalTempFileTrait;
protected $cache;
protected $scanner;
protected $watcher;
protected $propagator;
protected $storageCache;
protected $updater;
protected $mountOptions = [];
protected $owner = null;
/** @var ?bool */
private $shouldLogLocks = null;
/** @var ?LoggerInterface */
private $logger;
public function __construct($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;
}
}
public function is_dir($path) {
return $this->filetype($path) === 'dir';
}
public function is_file($path) {
return $this->filetype($path) === 'file';
}
public function filesize($path): false|int|float {
if ($this->is_dir($path)) {
return 0; //by definition
} else {
$stat = $this->stat($path);
if (isset($stat['size'])) {
return $stat['size'];
} else {
return 0;
}
}
}
public function isReadable($path) {
// at least check whether it exists
// subclasses might want to implement this more thoroughly
return $this->file_exists($path);
}
public function isUpdatable($path) {
// 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) {
if ($this->is_dir($path) && $this->isUpdatable($path)) {
return true;
}
return false;
}
public function isDeletable($path) {
if ($path === '' || $path === '/') {
return $this->isUpdatable($path);
}
$parent = dirname($path);
return $this->isUpdatable($parent) && $this->isUpdatable($path);
}
public function isSharable($path) {
return $this->isReadable($path);
}
public function getPermissions($path) {
$permissions = 0;
if ($this->isCreatable($path)) {
$permissions |= \OCP\Constants::PERMISSION_CREATE;
}
if ($this->isReadable($path)) {
$permissions |= \OCP\Constants::PERMISSION_READ;
}
if ($this->isUpdatable($path)) {
$permissions |= \OCP\Constants::PERMISSION_UPDATE;
}
if ($this->isDeletable($path)) {
$permissions |= \OCP\Constants::PERMISSION_DELETE;
}
if ($this->isSharable($path)) {
$permissions |= \OCP\Constants::PERMISSION_SHARE;
}
return $permissions;
}
public function filemtime($path) {
$stat = $this->stat($path);
if (isset($stat['mtime']) && $stat['mtime'] > 0) {
return $stat['mtime'];
} else {
return 0;
}
}
public function file_get_contents($path) {
$handle = $this->fopen($path, "r");
if (!$handle) {
return false;
}
$data = stream_get_contents($handle);
fclose($handle);
return $data;
}
public function file_put_contents($path, $data) {
$handle = $this->fopen($path, "w");
if (!$handle) {
return false;
}
$this->removeCachedFile($path);
$count = fwrite($handle, $data);
fclose($handle);
return $count;
}
public function rename($source, $target) {
$this->remove($target);
$this->removeCachedFile($source);
return $this->copy($source, $target) and $this->remove($source);
}
public function copy($source, $target) {
if ($this->is_dir($source)) {
$this->remove($target);
$dir = $this->opendir($source);
$this->mkdir($target);
while ($file = readdir($dir)) {
if (!Filesystem::isIgnoredDir($file)) {
if (!$this->copy($source . '/' . $file, $target . '/' . $file)) {
closedir($dir);
return false;
}
}
}
closedir($dir);
return true;
} else {
$sourceStream = $this->fopen($source, 'r');
$targetStream = $this->fopen($target, 'w');
[, $result] = \OC_Helper::streamCopy($sourceStream, $targetStream);
if (!$result) {
\OCP\Server::get(LoggerInterface::class)->warning("Failed to write data while copying $source to $target");
}
$this->removeCachedFile($target);
return $result;
}
}
public function getMimeType($path) {
if ($this->is_dir($path)) {
return 'httpd/unix-directory';
} elseif ($this->file_exists($path)) {
return \OC::$server->getMimeTypeDetector()->detectPath($path);
} else {
return false;
}
}
public function hash($type, $path, $raw = false) {
$fh = $this->fopen($path, 'rb');
$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) {
return $this->getCachedFile($path);
}
/**
* @param string $path
* @param string $target
*/
private function addLocalFolder($path, $target) {
$dh = $this->opendir($path);
if (is_resource($dh)) {
while (($file = readdir($dh)) !== false) {
if (!\OC\Files\Filesystem::isIgnoredDir($file)) {
if ($this->is_dir($path . '/' . $file)) {
mkdir($target . '/' . $file);
$this->addLocalFolder($path . '/' . $file, $target . '/' . $file);
} else {
$tmp = $this->toTmpFile($path . '/' . $file);
rename($tmp, $target . '/' . $file);
}
}
}
}
}
/**
* @param string $query
* @param string $dir
* @return array
*/
protected function searchInDir($query, $dir = '') {
$files = [];
$dh = $this->opendir($dir);
if (is_resource($dh)) {
while (($item = readdir($dh)) !== false) {
if (\OC\Files\Filesystem::isIgnoredDir($item)) {
continue;
}
if (strstr(strtolower($item), strtolower($query)) !== false) {
$files[] = $dir . '/' . $item;
}
if ($this->is_dir($dir . '/' . $item)) {
$files = array_merge($files, $this->searchInDir($query, $dir . '/' . $item));
}
}
}
closedir($dh);
return $files;
}
/**
* 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) {
return $this->filemtime($path) > $time;
}
public function getCache($path = '', $storage = null) {
if (!$storage) {
$storage = $this;
}
if (!isset($storage->cache)) {
$storage->cache = new Cache($storage);
}
return $storage->cache;
}
public function getScanner($path = '', $storage = null) {
if (!$storage) {
$storage = $this;
}
if (!isset($storage->scanner)) {
$storage->scanner = new Scanner($storage);
}
return $storage->scanner;
}
public function getWatcher($path = '', $storage = null) {
if (!$storage) {
$storage = $this;
}
if (!isset($this->watcher)) {
$this->watcher = new Watcher($storage);
$globalPolicy = \OC::$server->getConfig()->getSystemValue('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) {
if (!$storage) {
$storage = $this;
}
if (!isset($storage->propagator)) {
$config = \OC::$server->getSystemConfig();
$storage->propagator = new Propagator($storage, \OC::$server->getDatabaseConnection(), ['appdata_' . $config->getValue('instanceid')]);
}
return $storage->propagator;
}
public function getUpdater($storage = null) {
if (!$storage) {
$storage = $this;
}
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;
}
/**
* 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) {
if ($this->owner === null) {
$this->owner = \OC_User::getUser();
}
return $this->owner;
}
/**
* get the ETag for a file or folder
*
* @param string $path
* @return string
*/
public function getETag($path) {
return uniqid();
}
/**
* clean a path, i.e. remove all redundant '.' and '..'
* making sure that it can't point to higher than '/'
*
* @param string $path The path to clean
* @return string cleaned path
*/
public function cleanPath($path) {
if (strlen($path) == 0 or $path[0] != '/') {
$path = '/' . $path;
}
$output = [];
foreach (explode('/', $path) as $chunk) {
if ($chunk == '..') {
array_pop($output);
} elseif ($chunk == '.') {
} else {
$output[] = $chunk;
}
}
return implode('/', $output);
}
/**
* Test a storage for availability
*
* @return bool
*/
public function test() {
try {
if ($this->stat('')) {
return true;
}
\OC::$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(),
['exception' => $e]
);
return false;
}
}
/**
* get the free space in the storage
*
* @param string $path
* @return int|float|false
*/
public function free_space($path) {
return \OCP\Files\FileInfo::SPACE_UNKNOWN;
}
/**
* {@inheritdoc}
*/
public function isLocal() {
// the common implementation returns a temporary file by
// default, which is not local
return false;
}
/**
* 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) {
if (ltrim($class, '\\') === 'OC\Files\Storage\Shared') {
// FIXME Temporary fix to keep existing checks working
$class = '\OCA\Files_Sharing\SharedStorage';
}
return is_a($this, $class);
}
/**
* 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) {
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();
}
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();
}
}
// 255 characters is the limit on common file systems (ext/xfs)
// oc_filecache has a 250 char length limit for the filename
if (isset($fileName[250])) {
throw new FileNameTooLongException();
}
// NOTE: $path will remain unverified for now
$this->verifyPosixPath($fileName);
}
/**
* @param string $fileName
* @throws InvalidPathException
*/
protected function verifyPosixPath($fileName) {
$this->scanForInvalidCharacters($fileName, "\\/");
$fileName = trim($fileName);
$reservedNames = ['*'];
if (in_array($fileName, $reservedNames)) {
throw new ReservedWordException();
}
}
/**
* @param string $fileName
* @param string $invalidChars
* @throws InvalidPathException
*/
private function scanForInvalidCharacters($fileName, $invalidChars) {
foreach (str_split($invalidChars) as $char) {
if (str_contains($fileName, $char)) {
throw new InvalidCharacterInPathException();
}
}
$sanitizedFileName = filter_var($fileName, FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW);
if ($sanitizedFileName !== $fileName) {
throw new InvalidCharacterInPathException();
}
}
/**
* @param array $options
*/
public function setMountOptions(array $options) {
$this->mountOptions = $options;
}
/**
* @param string $name
* @param mixed $default
* @return mixed
*/
public function getMountOption($name, $default = null) {
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) {
if ($sourceStorage === $this) {
return $this->copy($sourceInternalPath, $targetInternalPath);
}
if ($sourceStorage->is_dir($sourceInternalPath)) {
$dh = $sourceStorage->opendir($sourceInternalPath);
$result = $this->mkdir($targetInternalPath);
if (is_resource($dh)) {
$result = true;
while ($result and ($file = readdir($dh)) !== false) {
if (!Filesystem::isIgnoredDir($file)) {
$result &= $this->copyFromStorage($sourceStorage, $sourceInternalPath . '/' . $file, $targetInternalPath . '/' . $file);
}
}
}
} else {
$source = $sourceStorage->fopen($sourceInternalPath, 'r');
$result = false;
if ($source) {
try {
$this->writeStream($targetInternalPath, $source);
$result = true;
} catch (\Exception $e) {
\OC::$server->get(LoggerInterface::class)->warning('Failed to copy stream to storage', ['exception' => $e]);
}
}
if ($result && $preserveMtime) {
$mtime = $sourceStorage->filemtime($sourceInternalPath);
$this->touch($targetInternalPath, is_int($mtime) ? $mtime : null);
}
if (!$result) {
// delete partially written target file
$this->unlink($targetInternalPath);
// delete cache entry that was created by fopen
$this->getCache()->remove($targetInternalPath);
}
}
return (bool)$result;
}
/**
* 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
*/
$storage = $storage->getWrapperStorage();
}
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)) {
// resolve any jailed paths
while ($sourceStorage->instanceOfStorage(Jail::class)) {
/**
* @var Jail $sourceStorage
*/
$sourceInternalPath = $sourceStorage->getUnjailedPath($sourceInternalPath);
$sourceStorage = $sourceStorage->getUnjailedStorage();
}
return $this->rename($sourceInternalPath, $targetInternalPath);
}
if (!$sourceStorage->isDeletable($sourceInternalPath)) {
return false;
}
$result = $this->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath, true);
if ($result) {
if ($sourceStorage->is_dir($sourceInternalPath)) {
$result = $sourceStorage->rmdir($sourceInternalPath);
} else {
$result = $sourceStorage->unlink($sourceInternalPath);
}
}
return $result;
}
/**
* @inheritdoc
*/
public function getMetaData($path) {
if (Filesystem::isFileBlacklisted($path)) {
throw new ForbiddenException('Invalid path: ' . $path, false);
}
$permissions = $this->getPermissions($path);
if (!$permissions & \OCP\Constants::PERMISSION_READ) {
//can't read, nothing we can do
return null;
}
$data = [];
$data['mimetype'] = $this->getMimeType($path);
$data['mtime'] = $this->filemtime($path);
if ($data['mtime'] === false) {
$data['mtime'] = time();
}
if ($data['mimetype'] == 'httpd/unix-directory') {
$data['size'] = -1; //unknown
} else {
$data['size'] = $this->filesize($path);
}
$data['etag'] = $this->getETag($path);
$data['storage_mtime'] = $data['mtime'];
$data['permissions'] = $permissions;
$data['name'] = basename($path);
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) {
$logger = $this->getLockLogger();
if ($logger) {
$typeString = ($type === ILockingProvider::LOCK_SHARED) ? 'shared' : 'exclusive';
$logger->info(
sprintf(
'acquire %s lock on "%s" on storage "%s"',
$typeString,
$path,
$this->getId()
),
[
'app' => 'locking',
]
);
}
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]);
}
throw $e;
}
}
/**
* @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) {
$logger = $this->getLockLogger();
if ($logger) {
$typeString = ($type === ILockingProvider::LOCK_SHARED) ? 'shared' : 'exclusive';
$logger->info(
sprintf(
'release %s lock on "%s" on storage "%s"',
$typeString,
$path,
$this->getId()
),
[
'app' => 'locking',
]
);
}
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]);
}
throw $e;
}
}
/**
* @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) {
$logger = $this->getLockLogger();
if ($logger) {
$typeString = ($type === ILockingProvider::LOCK_SHARED) ? 'shared' : 'exclusive';
$logger->info(
sprintf(
'change lock on "%s" to %s on storage "%s"',
$path,
$typeString,
$this->getId()
),
[
'app' => 'locking',
]
);
}
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]);
}
throw $e;
}
}
private function getLockLogger(): ?LoggerInterface {
if (is_null($this->shouldLogLocks)) {
$this->shouldLogLocks = \OC::$server->getConfig()->getSystemValueBool('filelocking.debug', false);
$this->logger = $this->shouldLogLocks ? \OC::$server->get(LoggerInterface::class) : null;
}
return $this->logger;
}
/**
* @return array [ available, last_checked ]
*/
public function getAvailability() {
return $this->getStorageCache()->getAvailability();
}
/**
* @param bool $isAvailable
*/
public function setAvailability($isAvailable) {
$this->getStorageCache()->setAvailability($isAvailable);
}
/**
* @return bool
*/
public function needsPartFile() {
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 {
$target = $this->fopen($path, 'w');
if (!$target) {
throw new GenericFileException("Failed to open $path for writing");
}
try {
[$count, $result] = \OC_Helper::streamCopy($stream, $target);
if (!$result) {
throw new GenericFileException("Failed to copy stream");
}
} finally {
fclose($target);
fclose($stream);
}
return $count;
}
public function getDirectoryContent($directory): \Traversable {
$dh = $this->opendir($directory);
if (is_resource($dh)) {
$basePath = rtrim($directory, '/');
while (($file = readdir($dh)) !== false) {
if (!Filesystem::isIgnoredDir($file)) {
$childPath = $basePath . '/' . trim($file, '/');
$metadata = $this->getMetaData($childPath);
if ($metadata !== null) {
yield $metadata;
}
}
}
}
}
}
@@ -0,0 +1,81 @@
<?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/>
*
*/
namespace OC\Files\Storage;
class CommonTest extends \OC\Files\Storage\Common {
/**
* underlying local storage used for missing functions
* @var \OC\Files\Storage\Local
*/
private $storage;
public function __construct($params) {
$this->storage = new \OC\Files\Storage\Local($params);
}
public function getId() {
return 'test::'.$this->storage->getId();
}
public function mkdir($path) {
return $this->storage->mkdir($path);
}
public function rmdir($path) {
return $this->storage->rmdir($path);
}
public function opendir($path) {
return $this->storage->opendir($path);
}
public function stat($path) {
return $this->storage->stat($path);
}
public function filetype($path) {
return @$this->storage->filetype($path);
}
public function isReadable($path) {
return $this->storage->isReadable($path);
}
public function isUpdatable($path) {
return $this->storage->isUpdatable($path);
}
public function file_exists($path) {
return $this->storage->file_exists($path);
}
public function unlink($path) {
return $this->storage->unlink($path);
}
public function fopen($path, $mode) {
return $this->storage->fopen($path, $mode);
}
public function free_space($path) {
return $this->storage->free_space($path);
}
public function touch($path, $mtime = null) {
return $this->storage->touch($path, $mtime);
}
}
@@ -0,0 +1,934 @@
<?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/>
*
*/
namespace OC\Files\Storage;
use Exception;
use Icewind\Streams\CallbackWrapper;
use Icewind\Streams\IteratorDirectory;
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\IClientService;
use OCP\ICertificateManager;
use OCP\IConfig;
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 Sabre\HTTP\RequestInterface;
/**
* Class DAV
*
* @package OC\Files\Storage
*/
class DAV extends Common {
/** @var string */
protected $password;
/** @var string */
protected $user;
/** @var string|null */
protected $authType;
/** @var string */
protected $host;
/** @var bool */
protected $secure;
/** @var string */
protected $root;
/** @var string */
protected $certPath;
/** @var bool */
protected $ready;
/** @var Client */
protected $client;
/** @var ArrayCache */
protected $statCache;
/** @var IClientService */
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
* @throws \Exception
*/
public function __construct($params) {
$this->statCache = new ArrayCache();
$this->httpClientService = \OC::$server->getHTTPClientService();
if (isset($params['host']) && isset($params['user']) && isset($params['password'])) {
$host = $params['host'];
//remove leading http[s], will be generated in createBaseUri()
if (str_starts_with($host, "https://")) {
$host = substr($host, 8);
} 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'];
}
if (isset($params['secure'])) {
if (is_string($params['secure'])) {
$this->secure = ($params['secure'] === 'true');
} else {
$this->secure = (bool)$params['secure'];
}
} else {
$this->secure = false;
}
if ($this->secure === true) {
// inject mock for testing
$this->certManager = \OC::$server->getCertificateManager();
}
$this->root = $params['root'] ?? '/';
$this->root = '/' . ltrim($this->root, '/');
$this->root = rtrim($this->root, '/') . '/';
} else {
throw new \Exception('Invalid webdav storage configuration');
}
$this->logger = \OC::$server->get(LoggerInterface::class);
$this->eventLogger = \OC::$server->get(IEventLogger::class);
// This timeout value will be used for the download and upload of files
$this->timeout = \OC::$server->get(IConfig::class)->getSystemValueInt('davstorage.request_timeout', 30);
$this->mimeTypeDetector = \OC::$server->getMimeTypeDetector();
}
protected function init() {
if ($this->ready) {
return;
}
$this->ready = true;
$settings = [
'baseUri' => $this->createBaseUri(),
'userName' => $this->user,
'password' => $this->password,
];
if ($this->authType !== null) {
$settings['authType'] = $this->authType;
}
$proxy = \OC::$server->getConfig()->getSystemValueString('proxy', '');
if ($proxy !== '') {
$settings['proxy'] = $proxy;
}
$this->client = new Client($settings);
$this->client->setThrowExceptions(true);
if ($this->secure === true) {
$certPath = $this->certManager->getAbsoluteBundlePath();
if (file_exists($certPath)) {
$this->certPath = $certPath;
}
if ($this->certPath) {
$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() {
$this->statCache->clear();
}
/** {@inheritdoc} */
public function getId() {
return 'webdav::' . $this->user . '@' . $this->host . '/' . $this->root;
}
/** {@inheritdoc} */
public function createBaseUri() {
$baseUri = 'http';
if ($this->secure) {
$baseUri .= 's';
}
$baseUri .= '://' . $this->host . $this->root;
return $baseUri;
}
/** {@inheritdoc} */
public function mkdir($path) {
$this->init();
$path = $this->cleanPath($path);
$result = $this->simpleResponse('MKCOL', $path, null, 201);
if ($result) {
$this->statCache->set($path, true);
}
return $result;
}
/** {@inheritdoc} */
public function rmdir($path) {
$this->init();
$path = $this->cleanPath($path);
// FIXME: some WebDAV impl return 403 when trying to DELETE
// a non-empty folder
$result = $this->simpleResponse('DELETE', $path . '/', null, 204);
$this->statCache->clear($path . '/');
$this->statCache->remove($path);
return $result;
}
/** {@inheritdoc} */
public function opendir($path) {
$this->init();
$path = $this->cleanPath($path);
try {
$content = $this->getDirectoryContent($path);
$files = [];
foreach ($content as $child) {
$files[] = $child['name'];
}
return IteratorDirectory::wrap($files);
} catch (\Exception $e) {
$this->convertException($e, $path);
}
return false;
}
/**
* Propfind call with cache handling.
*
* First checks if information is cached.
* If not, request it from the server then store to cache.
*
* @param string $path path to propfind
*
* @return array|boolean propfind response or false if the entry was not found
*
* @throws ClientHttpException
*/
protected function propfind($path) {
$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();
try {
$response = $this->client->propFind(
$this->encodePath($path),
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;
}
$this->convertException($e, $path);
} catch (\Exception $e) {
$this->convertException($e, $path);
}
} else {
$response = $cachedResponse;
}
return $response;
}
/** {@inheritdoc} */
public function filetype($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();
}
return (count($responseType) > 0 and $responseType[0] == "{DAV:}collection") ? 'dir' : 'file';
} catch (\Exception $e) {
$this->convertException($e, $path);
}
return false;
}
/** {@inheritdoc} */
public function file_exists($path) {
try {
$path = $this->cleanPath($path);
$cachedState = $this->statCache->get($path);
if ($cachedState === false) {
// we know the file doesn't exist
return false;
} elseif (!is_null($cachedState)) {
return true;
}
// need to get from server
return ($this->propfind($path) !== false);
} catch (\Exception $e) {
$this->convertException($e, $path);
}
return false;
}
/** {@inheritdoc} */
public function unlink($path) {
$this->init();
$path = $this->cleanPath($path);
$result = $this->simpleResponse('DELETE', $path, null, 204);
$this->statCache->clear($path . '/');
$this->statCache->remove($path);
return $result;
}
/** {@inheritdoc} */
public function fopen($path, $mode) {
$this->init();
$path = $this->cleanPath($path);
switch ($mode) {
case 'r':
case 'rb':
try {
$response = $this->httpClientService
->newClient()
->get($this->createBaseUri() . $this->encodePath($path), [
'auth' => [$this->user, $this->password],
'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
&& $e->getResponse()->getStatusCode() === 404) {
return false;
} else {
throw $e;
}
}
if ($response->getStatusCode() !== Http::STATUS_OK) {
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']);
}
}
return $response->getBody();
case 'w':
case 'wb':
case 'a':
case 'ab':
case 'r+':
case 'w+':
case 'wb+':
case 'a+':
case 'x':
case 'x+':
case 'c':
case 'c+':
//emulate these
$tempManager = \OC::$server->getTempManager();
if (strrpos($path, '.') !== false) {
$ext = substr($path, strrpos($path, '.'));
} else {
$ext = '';
}
if ($this->file_exists($path)) {
if (!$this->isUpdatable($path)) {
return false;
}
if ($mode === 'w' or $mode === 'w+') {
$tmpFile = $tempManager->getTemporaryFile($ext);
} else {
$tmpFile = $this->getCachedFile($path);
}
} else {
if (!$this->isCreatable(dirname($path))) {
return false;
}
$tmpFile = $tempManager->getTemporaryFile($ext);
}
$handle = fopen($tmpFile, $mode);
return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) {
$this->writeBack($tmpFile, $path);
});
}
}
/**
* @param string $tmpFile
*/
public function writeBack($tmpFile, $path) {
$this->uploadFile($tmpFile, $path);
unlink($tmpFile);
}
/** {@inheritdoc} */
public function free_space($path) {
$this->init();
$path = $this->cleanPath($path);
try {
$response = $this->propfind($path);
if ($response === false) {
return FileInfo::SPACE_UNKNOWN;
}
if (isset($response['{DAV:}quota-available-bytes'])) {
return Util::numericToNumber($response['{DAV:}quota-available-bytes']);
} else {
return FileInfo::SPACE_UNKNOWN;
}
} catch (\Exception $e) {
return FileInfo::SPACE_UNKNOWN;
}
}
/** {@inheritdoc} */
public function touch($path, $mtime = null) {
$this->init();
if (is_null($mtime)) {
$mtime = time();
}
$path = $this->cleanPath($path);
// if file exists, update the mtime, else create a new empty file
if ($this->file_exists($path)) {
try {
$this->statCache->remove($path);
$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) {
// server has not accepted the mtime
return false;
}
}
} catch (ClientHttpException $e) {
if ($e->getHttpStatus() === 501) {
return false;
}
$this->convertException($e, $path);
return false;
} catch (\Exception $e) {
$this->convertException($e, $path);
return false;
}
} else {
$this->file_put_contents($path, '');
}
return true;
}
/**
* @param string $path
* @param mixed $data
* @return int|float|false
*/
public function file_put_contents($path, $data) {
$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) {
$this->init();
// invalidate
$target = $this->cleanPath($target);
$this->statCache->remove($target);
$source = fopen($path, 'r');
$this->httpClientService
->newClient()
->put($this->createBaseUri() . $this->encodePath($target), [
'body' => $source,
'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($source, $target) {
$this->init();
$source = $this->cleanPath($source);
$target = $this->cleanPath($target);
try {
// overwrite directory ?
if ($this->is_dir($target)) {
// needs trailing slash in destination
$target = rtrim($target, '/') . '/';
}
$this->client->request(
'MOVE',
$this->encodePath($source),
null,
[
'Destination' => $this->createBaseUri() . $this->encodePath($target),
]
);
$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);
}
return false;
}
/** {@inheritdoc} */
public function copy($source, $target) {
$this->init();
$source = $this->cleanPath($source);
$target = $this->cleanPath($target);
try {
// overwrite directory ?
if ($this->is_dir($target)) {
// needs trailing slash in destination
$target = rtrim($target, '/') . '/';
}
$this->client->request(
'COPY',
$this->encodePath($source),
null,
[
'Destination' => $this->createBaseUri() . $this->encodePath($target),
]
);
$this->statCache->clear($target . '/');
$this->statCache->set($target, true);
$this->removeCachedFile($target);
return true;
} catch (\Exception $e) {
$this->convertException($e);
}
return false;
}
public function getMetaData($path) {
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);
}
}
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);
}
$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') {
$mimeType = 'httpd/unix-directory';
} elseif (isset($response['{DAV:}getcontenttype'])) {
$mimeType = $response['{DAV:}getcontenttype'];
} else {
$mimeType = $this->mimeTypeDetector->detectPath($path);
}
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,
];
}
/** {@inheritdoc} */
public function stat($path) {
$meta = $this->getMetaData($path);
if (!$meta) {
return false;
} else {
return $meta;
}
}
/** {@inheritdoc} */
public function getMimeType($path) {
$meta = $this->getMetaData($path);
if ($meta) {
return $meta['mimetype'];
} else {
return false;
}
}
/**
* @param string $path
* @return string
*/
public function cleanPath($path) {
if ($path === '') {
return $path;
}
$path = Filesystem::normalizePath($path);
// remove leading slash
return substr($path, 1);
}
/**
* URL encodes the given path but keeps the slashes
*
* @param string $path to encode
* @return string encoded path
*/
protected function encodePath($path) {
// 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) {
$path = $this->cleanPath($path);
try {
$response = $this->client->request($method, $this->encodePath($path), $body);
return $response['statusCode'] == $expected;
} catch (ClientHttpException $e) {
if ($e->getHttpStatus() === 404 && $method === 'DELETE') {
$this->statCache->clear($path . '/');
$this->statCache->set($path, false);
return false;
}
$this->convertException($e, $path);
} catch (\Exception $e) {
$this->convertException($e, $path);
}
return false;
}
/**
* check if curl is installed
*/
public static function checkDependencies() {
return true;
}
/** {@inheritdoc} */
public function isUpdatable($path) {
return (bool)($this->getPermissions($path) & Constants::PERMISSION_UPDATE);
}
/** {@inheritdoc} */
public function isCreatable($path) {
return (bool)($this->getPermissions($path) & Constants::PERMISSION_CREATE);
}
/** {@inheritdoc} */
public function isSharable($path) {
return (bool)($this->getPermissions($path) & Constants::PERMISSION_SHARE);
}
/** {@inheritdoc} */
public function isDeletable($path) {
return (bool)($this->getPermissions($path) & Constants::PERMISSION_DELETE);
}
/** {@inheritdoc} */
public function getPermissions($path) {
$stat = $this->getMetaData($path);
if ($stat) {
return $stat['permissions'];
} else {
return 0;
}
}
/** {@inheritdoc} */
public function getETag($path) {
$meta = $this->getMetaData($path);
if ($meta) {
return $meta['etag'];
} else {
return null;
}
}
/**
* @param string $permissionsString
* @return int
*/
protected function parsePermissions($permissionsString) {
$permissions = Constants::PERMISSION_READ;
if (str_contains($permissionsString, 'R')) {
$permissions |= Constants::PERMISSION_SHARE;
}
if (str_contains($permissionsString, 'D')) {
$permissions |= Constants::PERMISSION_DELETE;
}
if (str_contains($permissionsString, 'W')) {
$permissions |= Constants::PERMISSION_UPDATE;
}
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) {
$this->init();
$path = $this->cleanPath($path);
try {
// force refresh for $path
$this->statCache->remove($path);
$response = $this->propfind($path);
if ($response === false) {
if ($path === '') {
// if root is gone it means the storage is not available
throw new StorageNotAvailableException('root is gone');
}
return false;
}
if (isset($response['{DAV:}getetag'])) {
$cachedData = $this->getCache()->get($path);
$etag = trim($response['{DAV:}getetag'], '"');
if (($cachedData === false) || (!empty($etag) && ($cachedData['etag'] !== $etag))) {
return true;
} elseif (isset($response['{http://open-collaboration-services.org/ns}share-permissions'])) {
$sharePermissions = (int)$response['{http://open-collaboration-services.org/ns}share-permissions'];
return $sharePermissions !== $cachedData['permissions'];
} elseif (isset($response['{http://owncloud.org/ns}permissions'])) {
$permissions = $this->parsePermissions($response['{http://owncloud.org/ns}permissions']);
return $permissions !== $cachedData['permissions'];
} else {
return false;
}
} elseif (isset($response['{DAV:}getlastmodified'])) {
$remoteMtime = strtotime($response['{DAV:}getlastmodified']);
return $remoteMtime > $time;
} else {
// neither `getetag` nor `getlastmodified` is set
return false;
}
} catch (ClientHttpException $e) {
if ($e->getHttpStatus() === 405) {
if ($path === '') {
// if root is gone it means the storage is not available
throw new StorageNotAvailableException(get_class($e) . ': ' . $e->getMessage());
}
return false;
}
$this->convertException($e, $path);
return false;
} catch (\Exception $e) {
$this->convertException($e, $path);
return false;
}
}
/**
* Interpret the given exception and decide whether it is due to an
* unavailable storage, invalid storage or other.
* This will either throw StorageInvalidException, StorageNotAvailableException
* or do nothing.
*
* @param Exception $e sabre exception
* @param string $path optional path from the operation
*
* @throws StorageInvalidException if the storage is invalid, for example
* when the authentication expired or is invalid
* @throws StorageNotAvailableException if the storage is not available,
* 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]);
if ($e instanceof ClientHttpException) {
if ($e->getHttpStatus() === Http::STATUS_LOCKED) {
throw new \OCP\Lock\LockedException($path);
}
if ($e->getHttpStatus() === Http::STATUS_UNAUTHORIZED) {
// either password was changed or was invalid all along
throw new StorageInvalidException(get_class($e) . ': ' . $e->getMessage());
} elseif ($e->getHttpStatus() === Http::STATUS_METHOD_NOT_ALLOWED) {
// ignore exception for MethodNotAllowed, false will be returned
return;
} elseif ($e->getHttpStatus() === Http::STATUS_FORBIDDEN) {
// The operation is forbidden. Fail somewhat gracefully
throw new ForbiddenException(get_class($e) . ':' . $e->getMessage(), false);
}
throw new StorageNotAvailableException(get_class($e) . ': ' . $e->getMessage());
} elseif ($e instanceof ClientException) {
// connection timeout or refused, server could be temporarily down
throw new StorageNotAvailableException(get_class($e) . ': ' . $e->getMessage());
} elseif ($e instanceof \InvalidArgumentException) {
// parse error because the server returned HTML instead of XML,
// possibly temporarily down
throw new StorageNotAvailableException(get_class($e) . ': ' . $e->getMessage());
} elseif (($e instanceof StorageNotAvailableException) || ($e instanceof StorageInvalidException)) {
// rethrow
throw $e;
}
// TODO: only log for now, but in the future need to wrap/rethrow exception
}
public function getDirectoryContent($directory): \Traversable {
$this->init();
$directory = $this->cleanPath($directory);
try {
$responses = $this->client->propFind(
$this->encodePath($directory),
self::PROPFIND_PROPS,
1
);
if ($responses === false) {
return;
}
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);
}
}
}
@@ -0,0 +1,214 @@
<?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/>
*
*/
namespace OC\Files\Storage;
use OC\Files\Cache\FailedCache;
use OCP\Files\Storage\IStorage;
use OCP\Files\StorageNotAvailableException;
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]
*/
public function __construct($params) {
$this->e = $params['exception'];
if (!$this->e) {
throw new \InvalidArgumentException('Missing "exception" argument in FailedStorage constructor');
}
}
public function getId() {
// 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) {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
public function is_dir($path) {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
public function is_file($path) {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
public function stat($path) {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
public function filetype($path) {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
public function filesize($path): false|int|float {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
public function isCreatable($path) {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
public function isReadable($path) {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
public function isUpdatable($path) {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
public function isDeletable($path) {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
public function isSharable($path) {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
public function getPermissions($path) {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
public function file_exists($path) {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
public function filemtime($path) {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
public function file_get_contents($path) {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
public function file_put_contents($path, $data) {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
public function unlink($path) {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
public function rename($source, $target) {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
public function copy($source, $target) {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
public function fopen($path, $mode) {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
public function getMimeType($path) {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
public function hash($type, $path, $raw = false) {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
public function free_space($path) {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
public function search($query) {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
public function touch($path, $mtime = null) {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
public function getLocalFile($path) {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
public function hasUpdated($path, $time) {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
public function getETag($path) {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
public function getDirectDownload($path) {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
public function verifyPath($path, $fileName) {
return true;
}
public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime = false) {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
public function acquireLock($path, $type, ILockingProvider $provider) {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
public function releaseLock($path, $type, ILockingProvider $provider) {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
public function changeLock($path, $type, ILockingProvider $provider) {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
public function getAvailability() {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
public function setAvailability($isAvailable) {
throw new StorageNotAvailableException($this->e->getMessage(), $this->e->getCode(), $this->e);
}
public function getCache($path = '', $storage = null) {
return new FailedCache();
}
}
@@ -0,0 +1,111 @@
<?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/>
*
*/
namespace OC\Files\Storage;
use OC\Files\Cache\HomePropagator;
use OCP\IUser;
/**
* Specialized version of Local storage for home directory usage
*/
class Home extends Local implements \OCP\Files\IHomeStorage {
/**
* @var string
*/
protected $id;
/**
* @var \OC\User\User $user
*/
protected $user;
/**
* Construct a Home storage instance
*
* @param array $arguments array with "user" containing the
* storage owner
*/
public function __construct($arguments) {
$this->user = $arguments['user'];
$datadir = $this->user->getHome();
$this->id = 'home::' . $this->user->getUID();
parent::__construct(['datadir' => $datadir]);
}
public function getId() {
return $this->id;
}
/**
* @return \OC\Files\Cache\HomeCache
*/
public function getCache($path = '', $storage = null) {
if (!$storage) {
$storage = $this;
}
if (!isset($this->cache)) {
$this->cache = new \OC\Files\Cache\HomeCache($storage);
}
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) {
if (!$storage) {
$storage = $this;
}
if (!isset($this->propagator)) {
$this->propagator = new HomePropagator($storage, \OC::$server->getDatabaseConnection());
}
return $this->propagator;
}
/**
* Returns the owner of this home storage
*
* @return \OC\User\User owner of this home storage
*/
public function getUser(): IUser {
return $this->user;
}
/**
* get the owner of a path
*
* @param string $path The path to get the owner
* @return string uid or false
*/
public function getOwner($path) {
return $this->user->getUID();
}
}
@@ -0,0 +1,653 @@
<?php
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
* @author aler9 <46489434+aler9@users.noreply.github.com>
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
* @author Bart Visscher <bartv@thisnet.nl>
* @author Boris Rybalkin <ribalkin@gmail.com>
* @author Brice Maron <brice@bmaron.net>
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @author J0WI <J0WI@users.noreply.github.com>
* @author Jakob Sack <mail@jakobsack.de>
* @author Joas Schilling <coding@schilljs.com>
* @author Johannes Leuker <j.leuker@hosting.de>
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
* @author Klaas Freitag <freitag@owncloud.com>
* @author Lukas Reschke <lukas@statuscode.ch>
* @author Martin Brugnara <martin@0x6d62.eu>
* @author Michael Gapczynski <GapczynskiM@gmail.com>
* @author Morris Jobke <hey@morrisjobke.de>
* @author Robin Appelman <robin@icewind.nl>
* @author Roeland Jago Douma <roeland@famdouma.nl>
* @author Sjors van der Pluijm <sjors@desjors.nl>
* @author Stefan Weil <sw@weilnetz.de>
* @author Thomas Müller <thomas.mueller@tmit.eu>
* @author Tigran Mkrtchyan <tigran.mkrtchyan@desy.de>
* @author Vincent Petry <vincent@nextcloud.com>
*
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
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\Util;
use Psr\Log\LoggerInterface;
/**
* for local filestore, we only have to map the paths
*/
class Local extends \OC\Files\Storage\Common {
protected $datadir;
protected $dataDirLength;
protected $realDataDir;
private IConfig $config;
private IMimeTypeDetector $mimeTypeDetector;
private $defUMask;
protected bool $unlinkOnTruncate;
protected bool $caseInsensitive = false;
public function __construct($arguments) {
if (!isset($arguments['datadir']) || !is_string($arguments['datadir'])) {
throw new \InvalidArgumentException('No data directory set for local storage');
}
$this->datadir = str_replace('//', '/', $arguments['datadir']);
// some crazy code uses a local storage on root...
if ($this->datadir === '/') {
$this->realDataDir = $this->datadir;
} else {
$realPath = realpath($this->datadir) ?: $this->datadir;
$this->realDataDir = rtrim($realPath, '/') . '/';
}
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->defUMask = $this->config->getSystemValue('localstorage.umask', 0022);
$this->caseInsensitive = $this->config->getSystemValueBool('localstorage.case_insensitive', false);
// support Write-Once-Read-Many file systems
$this->unlinkOnTruncate = $this->config->getSystemValueBool('localstorage.unlink_on_truncate', false);
if (isset($arguments['isExternal']) && $arguments['isExternal'] && !$this->stat('')) {
// 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() {
return 'local::' . $this->datadir;
}
public function mkdir($path) {
$sourcePath = $this->getSourcePath($path);
$oldMask = umask($this->defUMask);
$result = @mkdir($sourcePath, 0777, true);
umask($oldMask);
return $result;
}
public function rmdir($path) {
if (!$this->isDeletable($path)) {
return false;
}
try {
$it = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($this->getSourcePath($path)),
\RecursiveIteratorIterator::CHILD_FIRST
);
/**
* RecursiveDirectoryIterator on an NFS path isn't iterable with foreach
* This bug is fixed in PHP 5.5.9 or before
* See #8376
*/
$it->rewind();
while ($it->valid()) {
/**
* @var \SplFileInfo $file
*/
$file = $it->current();
clearstatcache(true, $this->getSourcePath($file));
if (in_array($file->getBasename(), ['.', '..'])) {
$it->next();
continue;
} elseif ($file->isFile() || $file->isLink()) {
unlink($file->getPathname());
} elseif ($file->isDir()) {
rmdir($file->getPathname());
}
$it->next();
}
clearstatcache(true, $this->getSourcePath($path));
return rmdir($this->getSourcePath($path));
} catch (\UnexpectedValueException $e) {
return false;
}
}
public function opendir($path) {
return opendir($this->getSourcePath($path));
}
public function is_dir($path) {
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) {
if ($this->caseInsensitive && !$this->file_exists($path)) {
return false;
}
return is_file($this->getSourcePath($path));
}
public function stat($path) {
$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);
$statResult['size'] = $filesize;
$statResult[7] = $filesize;
}
if (is_array($statResult)) {
$statResult['full_path'] = $fullPath;
}
return $statResult;
}
/**
* @inheritdoc
*/
public function getMetaData($path) {
try {
$stat = $this->stat($path);
} catch (ForbiddenException $e) {
return null;
}
if (!$stat) {
return null;
}
$permissions = Constants::PERMISSION_SHARE;
$statPermissions = $stat['mode'];
$isDir = ($statPermissions & 0x4000) === 0x4000 && !($statPermissions & 0x8000);
if ($statPermissions & 0x0100) {
$permissions += Constants::PERMISSION_READ;
}
if ($statPermissions & 0x0080) {
$permissions += Constants::PERMISSION_UPDATE;
if ($isDir) {
$permissions += Constants::PERMISSION_CREATE;
}
}
if (!($path === '' || $path === '/')) { // deletable depends on the parents unix permissions
$parent = dirname($stat['full_path']);
if (is_writable($parent)) {
$permissions += Constants::PERMISSION_DELETE;
}
}
$data = [];
$data['mimetype'] = $isDir ? 'httpd/unix-directory' : $this->mimeTypeDetector->detectPath($path);
$data['mtime'] = $stat['mtime'];
if ($data['mtime'] === false) {
$data['mtime'] = time();
}
if ($isDir) {
$data['size'] = -1; //unknown
} else {
$data['size'] = $stat['size'];
}
$data['etag'] = $this->calculateEtag($path, $stat);
$data['storage_mtime'] = $data['mtime'];
$data['permissions'] = $permissions;
$data['name'] = basename($path);
return $data;
}
public function filetype($path) {
$filetype = filetype($this->getSourcePath($path));
if ($filetype == 'link') {
$filetype = filetype(realpath($this->getSourcePath($path)));
}
return $filetype;
}
public function filesize($path): false|int|float {
if (!$this->is_file($path)) {
return 0;
}
$fullPath = $this->getSourcePath($path);
if (PHP_INT_SIZE === 4) {
$helper = new \OC\LargeFileHelper;
return $helper->getFileSize($fullPath);
}
return filesize($fullPath);
}
public function isReadable($path) {
return is_readable($this->getSourcePath($path));
}
public function isUpdatable($path) {
return is_writable($this->getSourcePath($path));
}
public function file_exists($path) {
if ($this->caseInsensitive) {
$fullPath = $this->getSourcePath($path);
$content = scandir(dirname($fullPath), SCANDIR_SORT_NONE);
return is_array($content) && array_search(basename($fullPath), $content) !== false;
} else {
return file_exists($this->getSourcePath($path));
}
}
public function filemtime($path) {
$fullPath = $this->getSourcePath($path);
clearstatcache(true, $fullPath);
if (!$this->file_exists($path)) {
return false;
}
if (PHP_INT_SIZE === 4) {
$helper = new \OC\LargeFileHelper();
return $helper->getFileMtime($fullPath);
}
return filemtime($fullPath);
}
public function touch($path, $mtime = null) {
// 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)) {
return false;
}
$oldMask = umask($this->defUMask);
if (!is_null($mtime)) {
$result = @touch($this->getSourcePath($path), $mtime);
} else {
$result = @touch($this->getSourcePath($path));
}
umask($oldMask);
if ($result) {
clearstatcache(true, $this->getSourcePath($path));
}
return $result;
}
public function file_get_contents($path) {
return file_get_contents($this->getSourcePath($path));
}
public function file_put_contents($path, $data) {
$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) {
if ($this->is_dir($path)) {
return $this->rmdir($path);
} elseif ($this->is_file($path)) {
return unlink($this->getSourcePath($path));
} else {
return false;
}
}
private function checkTreeForForbiddenItems(string $path) {
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path));
foreach ($iterator as $file) {
/** @var \SplFileInfo $file */
if (Filesystem::isFileBlacklisted($file->getBasename())) {
throw new ForbiddenException('Invalid path: ' . $file->getPathname(), false);
}
}
}
public function rename($source, $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']);
return false;
}
if (!$this->isUpdatable($dstParent)) {
\OC::$server->get(LoggerInterface::class)->error('unable to rename, destination directory is not writable : ' . $dstParent, ['app' => 'core']);
return false;
}
if (!$this->file_exists($source)) {
\OC::$server->get(LoggerInterface::class)->error('unable to rename, file does not exists : ' . $source, ['app' => 'core']);
return false;
}
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 (@rename($this->getSourcePath($source), $this->getSourcePath($target))) {
if ($this->caseInsensitive) {
if (mb_strtolower($target) === mb_strtolower($source) && !$this->file_exists($target)) {
return false;
}
}
return true;
}
return $this->copy($source, $target) && $this->unlink($source);
}
public function copy($source, $target) {
if ($this->is_dir($source)) {
return parent::copy($source, $target);
} else {
$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) {
$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) {
return hash_file($type, $this->getSourcePath($path), $raw);
}
public function free_space($path) {
$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
// in such cases
if (!is_dir($sourcePath)) {
// disk_free_space doesn't work on files
$sourcePath = dirname($sourcePath);
}
$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 Util::numericToNumber($space);
}
public function search($query) {
return $this->searchInDir($query);
}
public function getLocalFile($path) {
return $this->getSourcePath($path);
}
/**
* @param string $query
* @param string $dir
* @return array
*/
protected function searchInDir($query, $dir = '') {
$files = [];
$physicalDir = $this->getSourcePath($dir);
foreach (scandir($physicalDir) as $item) {
if (\OC\Files\Filesystem::isIgnoredDir($item)) {
continue;
}
$physicalItem = $physicalDir . '/' . $item;
if (strstr(strtolower($item), strtolower($query)) !== false) {
$files[] = $dir . '/' . $item;
}
if (is_dir($physicalItem)) {
$files = array_merge($files, $this->searchInDir($query, $dir . '/' . $item));
}
}
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) {
if ($this->file_exists($path)) {
return $this->filemtime($path) > $time;
} else {
return true;
}
}
/**
* Get the source path (on disk) of a given path
*
* @param string $path
* @return string
* @throws ForbiddenException
*/
public function getSourcePath($path) {
if (Filesystem::isFileBlacklisted($path)) {
throw new ForbiddenException('Invalid path: ' . $path, false);
}
$fullPath = $this->datadir . $path;
$currentPath = $path;
$allowSymlinks = $this->config->getSystemValueBool('localstorage.allowsymlinks', false);
if ($allowSymlinks || $currentPath === '') {
return $fullPath;
}
$pathToResolve = $fullPath;
$realPath = realpath($pathToResolve);
while ($realPath === false) { // for non existing files check the parent directory
$currentPath = dirname($currentPath);
if ($currentPath === '' || $currentPath === '.') {
return $fullPath;
}
$realPath = realpath($this->datadir . $currentPath);
}
if ($realPath) {
$realPath = $realPath . '/';
}
if (substr($realPath, 0, $this->dataDirLength) === $this->realDataDir) {
return $fullPath;
}
\OC::$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() {
return true;
}
/**
* get the ETag for a file or folder
*
* @param string $path
* @return string
*/
public function getETag($path) {
return $this->calculateEtag($path, $this->stat($path));
}
private function calculateEtag(string $path, array $stat): string {
if ($stat['mode'] & 0x4000 && !($stat['mode'] & 0x8000)) { // is_dir & not socket
return parent::getETag($path);
} else {
if ($stat === false) {
return md5('');
}
$toHash = '';
if (isset($stat['mtime'])) {
$toHash .= $stat['mtime'];
}
if (isset($stat['ino'])) {
$toHash .= $stat['ino'];
}
if (isset($stat['dev'])) {
$toHash .= $stat['dev'];
}
if (isset($stat['size'])) {
$toHash .= $stat['size'];
}
return md5($toHash);
}
}
private function canDoCrossStorageMove(IStorage $sourceStorage) {
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')
// when moving encrypted files we have to handle keys and the target might not be encrypted
&& !$sourceStorage->instanceOfStorage(Encryption::class);
}
/**
* @param IStorage $sourceStorage
* @param string $sourceInternalPath
* @param string $targetInternalPath
* @param bool $preserveMtime
* @return bool
*/
public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime = false) {
if ($this->canDoCrossStorageMove($sourceStorage)) {
if ($sourceStorage->instanceOfStorage(Jail::class)) {
/**
* @var \OC\Files\Storage\Wrapper\Jail $sourceStorage
*/
$sourceInternalPath = $sourceStorage->getUnjailedPath($sourceInternalPath);
}
/**
* @var \OC\Files\Storage\Local $sourceStorage
*/
$rootStorage = new Local(['datadir' => '/']);
return $rootStorage->copy($sourceStorage->getSourcePath($sourceInternalPath), $this->getSourcePath($targetInternalPath));
} else {
return parent::copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
}
}
/**
* @param IStorage $sourceStorage
* @param string $sourceInternalPath
* @param string $targetInternalPath
* @return bool
*/
public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
if ($this->canDoCrossStorageMove($sourceStorage)) {
if ($sourceStorage->instanceOfStorage(Jail::class)) {
/**
* @var \OC\Files\Storage\Wrapper\Jail $sourceStorage
*/
$sourceInternalPath = $sourceStorage->getUnjailedPath($sourceInternalPath);
}
/**
* @var \OC\Files\Storage\Local $sourceStorage
*/
$rootStorage = new Local(['datadir' => '/']);
return $rootStorage->rename($sourceStorage->getSourcePath($sourceInternalPath), $this->getSourcePath($targetInternalPath));
} else {
return parent::moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
}
}
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);
}
if ($result === false) {
throw new GenericFileException("Failed write stream to $path");
} else {
return $result;
}
}
}
@@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2020 Robin Appelman <robin@icewind.nl>
*
* @author Robin Appelman <robin@icewind.nl>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OC\Files\Storage;
use OC\Files\Cache\LocalRootScanner;
class LocalRootStorage extends Local {
public function getScanner($path = '', $storage = null) {
if (!$storage) {
$storage = $this;
}
if (!isset($storage->scanner)) {
$storage->scanner = new LocalRootScanner($storage);
}
return $storage->scanner;
}
}
@@ -0,0 +1,71 @@
<?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/>
*
*/
namespace OC\Files\Storage;
/**
* Storage backend class for providing common filesystem operation methods
* which are not storage-backend specific.
*
* \OC\Files\Storage\Common is never used directly; it is extended by all other
* storage backends, where its methods may be overridden, and additional
* (backend-specific) methods are defined.
*
* Some \OC\Files\Storage\Common methods call functions which are first defined
* in classes which extend it, e.g. $this->stat() .
*/
trait LocalTempFileTrait {
/** @var array<string,string|false> */
protected array $cachedFiles = [];
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) {
unset($this->cachedFiles[$path]);
}
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;
}
if ($pos = strrpos($path, '.')) {
$extension = substr($path, $pos);
} else {
$extension = '';
}
$tmpFile = \OC::$server->getTempManager()->getTemporaryFile($extension);
$target = fopen($tmpFile, 'w');
\OC_Helper::streamCopy($source, $target);
fclose($target);
return $tmpFile;
}
}
@@ -0,0 +1,104 @@
<?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/>
*
*/
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);
/**
* Check if a file or folder exists
*
* @param string $path
* @return bool
*/
abstract public function file_exists($path);
/**
* Delete a file or folder
*
* @param string $path
* @return bool
*/
abstract public function unlink($path);
/**
* Open a directory handle for a folder
*
* @param string $path
* @return resource | bool
*/
abstract public function opendir($path);
/**
* Create a new folder
*
* @param string $path
* @return bool
*/
abstract public function mkdir($path);
public function copy($source, $target) {
if ($this->is_dir($source)) {
if ($this->file_exists($target)) {
$this->unlink($target);
}
$this->mkdir($target);
return $this->copyRecursive($source, $target);
} else {
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) {
$dh = $this->opendir($source);
$result = true;
while ($file = readdir($dh)) {
if (!\OC\Files\Filesystem::isIgnoredDir($file)) {
if ($this->is_dir($source . '/' . $file)) {
$this->mkdir($target . '/' . $file);
$result = $this->copyRecursive($source . '/' . $file, $target . '/' . $file);
} else {
$result = parent::copy($source . '/' . $file, $target . '/' . $file);
}
if (!$result) {
break;
}
}
}
return $result;
}
}
@@ -0,0 +1,139 @@
<?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/>
*
*/
namespace OC\Files\Storage;
use OCP\Lock\ILockingProvider;
/**
* Provide a common interface to all different storage options
*
* All paths passed to the storage are relative to the storage and should NOT have a leading slash.
*/
interface Storage extends \OCP\Files\Storage {
/**
* get a cache instance for the storage
*
* @param string $path
* @param \OC\Files\Storage\Storage|null (optional) the storage to pass to the cache
* @return \OC\Files\Cache\Cache
*/
public function getCache($path = '', $storage = null);
/**
* get a scanner instance for the storage
*
* @param string $path
* @param \OC\Files\Storage\Storage (optional) the storage to pass to the scanner
* @return \OC\Files\Cache\Scanner
*/
public function getScanner($path = '', $storage = null);
/**
* get the user id of the owner of a file or folder
*
* @param string $path
* @return string
*/
public function getOwner($path);
/**
* get a watcher instance for the cache
*
* @param string $path
* @param \OC\Files\Storage\Storage (optional) the storage to pass to the watcher
* @return \OC\Files\Cache\Watcher
*/
public function getWatcher($path = '', $storage = null);
/**
* 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);
/**
* 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
* - mimetype
* - mtime
* - size
* - etag
* - storage_mtime
* - permissions
*/
public function getDirectoryContent($directory): \Traversable;
}
@@ -0,0 +1,107 @@
<?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/>
*
*/
namespace OC\Files\Storage;
use OCP\Files\Mount\IMountPoint;
use OCP\Files\Storage\IStorageFactory;
class StorageFactory implements IStorageFactory {
/**
* @var array[] [$name=>['priority'=>$priority, 'wrapper'=>$callable] $storageWrappers
*/
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 = []) {
if (isset($this->storageWrappers[$wrapperName])) {
return false;
}
// apply to existing mounts before registering it to prevent applying it double in MountPoint::createStorage
foreach ($existingMounts as $mount) {
$mount->wrapStorage($callback);
}
$this->storageWrappers[$wrapperName] = ['wrapper' => $callback, 'priority' => $priority];
return true;
}
/**
* 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) {
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) {
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) {
$wrappers = array_values($this->storageWrappers);
usort($wrappers, function ($a, $b) {
return $b['priority'] - $a['priority'];
});
/** @var callable[] $wrappers */
$wrappers = array_map(function ($wrapper) {
return $wrapper['wrapper'];
}, $wrappers);
foreach ($wrappers as $wrapper) {
$storage = $wrapper($mountPoint->getMountPoint(), $storage, $mountPoint);
if (!($storage instanceof \OCP\Files\Storage)) {
throw new \Exception('Invalid result from storage wrapper');
}
}
return $storage;
}
}
@@ -0,0 +1,48 @@
<?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/>
*
*/
namespace OC\Files\Storage;
/**
* 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 cleanUp() {
\OC_Helper::rmdirr($this->datadir);
}
public function __destruct() {
parent::__destruct();
$this->cleanUp();
}
public function getDataDir() {
return $this->datadir;
}
}
@@ -0,0 +1,484 @@
<?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/>
*
*/
namespace OC\Files\Storage\Wrapper;
use OCP\Files\Storage\IStorage;
use OCP\Files\StorageAuthException;
use OCP\Files\StorageNotAvailableException;
use OCP\IConfig;
/**
* Availability checker for storages
*
* Throws a StorageNotAvailableException for storages with known failures
*/
class Availability extends Wrapper {
public const RECHECK_TTL_SEC = 600; // 10 minutes
/** @var IConfig */
protected $config;
public function __construct($parameters) {
$this->config = $parameters['config'] ?? \OC::$server->getConfig();
parent::__construct($parameters);
}
public static function shouldRecheck($availability) {
if (!$availability['available']) {
// trigger a recheck if TTL reached
if ((time() - $availability['last_checked']) > self::RECHECK_TTL_SEC) {
return true;
}
}
return false;
}
/**
* Only called if availability === false
*
* @return bool
*/
private function updateAvailability() {
// reset availability to false so that multiple requests don't recheck concurrently
$this->setAvailability(false);
try {
$result = $this->test();
} catch (\Exception $e) {
$result = false;
}
$this->setAvailability($result);
return $result;
}
/**
* @return bool
*/
private function isAvailable() {
$availability = $this->getAvailability();
if (self::shouldRecheck($availability)) {
return $this->updateAvailability();
}
return $availability['available'];
}
/**
* @throws StorageNotAvailableException
*/
private function checkAvailability() {
if (!$this->isAvailable()) {
throw new StorageNotAvailableException();
}
}
/** {@inheritdoc} */
public function mkdir($path) {
$this->checkAvailability();
try {
return parent::mkdir($path);
} catch (StorageNotAvailableException $e) {
$this->setUnavailable($e);
}
}
/** {@inheritdoc} */
public function rmdir($path) {
$this->checkAvailability();
try {
return parent::rmdir($path);
} catch (StorageNotAvailableException $e) {
$this->setUnavailable($e);
}
}
/** {@inheritdoc} */
public function opendir($path) {
$this->checkAvailability();
try {
return parent::opendir($path);
} catch (StorageNotAvailableException $e) {
$this->setUnavailable($e);
}
}
/** {@inheritdoc} */
public function is_dir($path) {
$this->checkAvailability();
try {
return parent::is_dir($path);
} catch (StorageNotAvailableException $e) {
$this->setUnavailable($e);
}
}
/** {@inheritdoc} */
public function is_file($path) {
$this->checkAvailability();
try {
return parent::is_file($path);
} catch (StorageNotAvailableException $e) {
$this->setUnavailable($e);
}
}
/** {@inheritdoc} */
public function stat($path) {
$this->checkAvailability();
try {
return parent::stat($path);
} catch (StorageNotAvailableException $e) {
$this->setUnavailable($e);
}
}
/** {@inheritdoc} */
public function filetype($path) {
$this->checkAvailability();
try {
return parent::filetype($path);
} catch (StorageNotAvailableException $e) {
$this->setUnavailable($e);
}
}
/** {@inheritdoc} */
public function filesize($path): false|int|float {
$this->checkAvailability();
try {
return parent::filesize($path);
} catch (StorageNotAvailableException $e) {
$this->setUnavailable($e);
}
}
/** {@inheritdoc} */
public function isCreatable($path) {
$this->checkAvailability();
try {
return parent::isCreatable($path);
} catch (StorageNotAvailableException $e) {
$this->setUnavailable($e);
}
}
/** {@inheritdoc} */
public function isReadable($path) {
$this->checkAvailability();
try {
return parent::isReadable($path);
} catch (StorageNotAvailableException $e) {
$this->setUnavailable($e);
}
}
/** {@inheritdoc} */
public function isUpdatable($path) {
$this->checkAvailability();
try {
return parent::isUpdatable($path);
} catch (StorageNotAvailableException $e) {
$this->setUnavailable($e);
}
}
/** {@inheritdoc} */
public function isDeletable($path) {
$this->checkAvailability();
try {
return parent::isDeletable($path);
} catch (StorageNotAvailableException $e) {
$this->setUnavailable($e);
}
}
/** {@inheritdoc} */
public function isSharable($path) {
$this->checkAvailability();
try {
return parent::isSharable($path);
} catch (StorageNotAvailableException $e) {
$this->setUnavailable($e);
}
}
/** {@inheritdoc} */
public function getPermissions($path) {
$this->checkAvailability();
try {
return parent::getPermissions($path);
} catch (StorageNotAvailableException $e) {
$this->setUnavailable($e);
}
}
/** {@inheritdoc} */
public function file_exists($path) {
if ($path === '') {
return true;
}
$this->checkAvailability();
try {
return parent::file_exists($path);
} catch (StorageNotAvailableException $e) {
$this->setUnavailable($e);
}
}
/** {@inheritdoc} */
public function filemtime($path) {
$this->checkAvailability();
try {
return parent::filemtime($path);
} catch (StorageNotAvailableException $e) {
$this->setUnavailable($e);
}
}
/** {@inheritdoc} */
public function file_get_contents($path) {
$this->checkAvailability();
try {
return parent::file_get_contents($path);
} catch (StorageNotAvailableException $e) {
$this->setUnavailable($e);
}
}
/** {@inheritdoc} */
public function file_put_contents($path, $data) {
$this->checkAvailability();
try {
return parent::file_put_contents($path, $data);
} catch (StorageNotAvailableException $e) {
$this->setUnavailable($e);
}
}
/** {@inheritdoc} */
public function unlink($path) {
$this->checkAvailability();
try {
return parent::unlink($path);
} catch (StorageNotAvailableException $e) {
$this->setUnavailable($e);
}
}
/** {@inheritdoc} */
public function rename($source, $target) {
$this->checkAvailability();
try {
return parent::rename($source, $target);
} catch (StorageNotAvailableException $e) {
$this->setUnavailable($e);
}
}
/** {@inheritdoc} */
public function copy($source, $target) {
$this->checkAvailability();
try {
return parent::copy($source, $target);
} catch (StorageNotAvailableException $e) {
$this->setUnavailable($e);
}
}
/** {@inheritdoc} */
public function fopen($path, $mode) {
$this->checkAvailability();
try {
return parent::fopen($path, $mode);
} catch (StorageNotAvailableException $e) {
$this->setUnavailable($e);
}
}
/** {@inheritdoc} */
public function getMimeType($path) {
$this->checkAvailability();
try {
return parent::getMimeType($path);
} catch (StorageNotAvailableException $e) {
$this->setUnavailable($e);
}
}
/** {@inheritdoc} */
public function hash($type, $path, $raw = false) {
$this->checkAvailability();
try {
return parent::hash($type, $path, $raw);
} catch (StorageNotAvailableException $e) {
$this->setUnavailable($e);
}
}
/** {@inheritdoc} */
public function free_space($path) {
$this->checkAvailability();
try {
return parent::free_space($path);
} catch (StorageNotAvailableException $e) {
$this->setUnavailable($e);
}
}
/** {@inheritdoc} */
public function search($query) {
$this->checkAvailability();
try {
return parent::search($query);
} catch (StorageNotAvailableException $e) {
$this->setUnavailable($e);
}
}
/** {@inheritdoc} */
public function touch($path, $mtime = null) {
$this->checkAvailability();
try {
return parent::touch($path, $mtime);
} catch (StorageNotAvailableException $e) {
$this->setUnavailable($e);
}
}
/** {@inheritdoc} */
public function getLocalFile($path) {
$this->checkAvailability();
try {
return parent::getLocalFile($path);
} catch (StorageNotAvailableException $e) {
$this->setUnavailable($e);
}
}
/** {@inheritdoc} */
public function hasUpdated($path, $time) {
if (!$this->isAvailable()) {
return false;
}
try {
return parent::hasUpdated($path, $time);
} catch (StorageNotAvailableException $e) {
// set unavailable but don't rethrow
$this->setUnavailable(null);
return false;
}
}
/** {@inheritdoc} */
public function getOwner($path) {
try {
return parent::getOwner($path);
} catch (StorageNotAvailableException $e) {
$this->setUnavailable($e);
}
}
/** {@inheritdoc} */
public function getETag($path) {
$this->checkAvailability();
try {
return parent::getETag($path);
} catch (StorageNotAvailableException $e) {
$this->setUnavailable($e);
}
}
/** {@inheritdoc} */
public function getDirectDownload($path) {
$this->checkAvailability();
try {
return parent::getDirectDownload($path);
} catch (StorageNotAvailableException $e) {
$this->setUnavailable($e);
}
}
/** {@inheritdoc} */
public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
$this->checkAvailability();
try {
return parent::copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
} catch (StorageNotAvailableException $e) {
$this->setUnavailable($e);
}
}
/** {@inheritdoc} */
public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
$this->checkAvailability();
try {
return parent::moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
} catch (StorageNotAvailableException $e) {
$this->setUnavailable($e);
}
}
/** {@inheritdoc} */
public function getMetaData($path) {
$this->checkAvailability();
try {
return parent::getMetaData($path);
} catch (StorageNotAvailableException $e) {
$this->setUnavailable($e);
}
}
/**
* @template T of StorageNotAvailableException|null
* @param T $e
* @psalm-return (T is null ? void : never)
* @throws StorageNotAvailableException
*/
protected function setUnavailable(?StorageNotAvailableException $e): void {
$delay = self::RECHECK_TTL_SEC;
if ($e instanceof StorageAuthException) {
$delay = max(
// 30min
$this->config->getSystemValueInt('external_storage.auth_availability_delay', 1800),
self::RECHECK_TTL_SEC
);
}
$this->getStorageCache()->setAvailability(false, $delay);
if ($e !== null) {
throw $e;
}
}
public function getDirectoryContent($directory): \Traversable {
$this->checkAvailability();
try {
return parent::getDirectoryContent($directory);
} catch (StorageNotAvailableException $e) {
$this->setUnavailable($e);
}
}
}
@@ -0,0 +1,545 @@
<?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/>
*
*/
namespace OC\Files\Storage\Wrapper;
use OC\Files\Filesystem;
use OCP\Cache\CappedMemoryCache;
use OCP\Files\Storage\IStorage;
use OCP\ICache;
/**
* Encoding wrapper that deals with file names that use unsupported encodings like NFD.
*
* When applied and a UTF-8 path name was given, the wrapper will first attempt to access
* the actual given name and then try its NFD form.
*/
class Encoding extends Wrapper {
/**
* @var ICache
*/
private $namesCache;
/**
* @param array $parameters
*/
public function __construct($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) {
return !preg_match('/[\\x80-\\xff]+/', $str);
}
/**
* Checks whether the given path exists in NFC or NFD form after checking
* 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) {
$cachedPath = $this->namesCache[$fullPath];
if ($cachedPath !== null) {
return $cachedPath;
}
$sections = explode('/', $fullPath);
$path = '';
foreach ($sections as $section) {
$convertedPath = $this->findPathToUseLastSection($path, $section);
if ($convertedPath === null) {
// no point in continuing if the section was not found, use original path
return $fullPath;
}
$path = $convertedPath . '/';
}
$path = rtrim($path, '/');
return $path;
}
/**
* 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) {
$fullPath = $basePath . $lastSection;
if ($lastSection === '' || $this->isAscii($lastSection) || $this->storage->file_exists($fullPath)) {
$this->namesCache[$fullPath] = $fullPath;
return $fullPath;
}
// swap encoding
if (\Normalizer::isNormalized($lastSection, \Normalizer::FORM_C)) {
$otherFormPath = \Normalizer::normalize($lastSection, \Normalizer::FORM_D);
} else {
$otherFormPath = \Normalizer::normalize($lastSection, \Normalizer::FORM_C);
}
$otherFullPath = $basePath . $otherFormPath;
if ($this->storage->file_exists($otherFullPath)) {
$this->namesCache[$fullPath] = $otherFullPath;
return $otherFullPath;
}
// return original path, file did not exist at all
$this->namesCache[$fullPath] = $fullPath;
return null;
}
/**
* see https://www.php.net/manual/en/function.mkdir.php
*
* @param string $path
* @return bool
*/
public function mkdir($path) {
// note: no conversion here, method should not be called with non-NFC names!
$result = $this->storage->mkdir($path);
if ($result) {
$this->namesCache[$path] = $path;
}
return $result;
}
/**
* see https://www.php.net/manual/en/function.rmdir.php
*
* @param string $path
* @return bool
*/
public function rmdir($path) {
$result = $this->storage->rmdir($this->findPathToUse($path));
if ($result) {
unset($this->namesCache[$path]);
}
return $result;
}
/**
* see https://www.php.net/manual/en/function.opendir.php
*
* @param string $path
* @return resource|false
*/
public function opendir($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) {
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) {
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) {
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) {
return $this->storage->filetype($this->findPathToUse($path));
}
/**
* see https://www.php.net/manual/en/function.filesize.php
* The result for filesize when called on a folder is required to be 0
*/
public function filesize($path): false|int|float {
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) {
return $this->storage->isCreatable($this->findPathToUse($path));
}
/**
* check if a file can be read
*
* @param string $path
* @return bool
*/
public function isReadable($path) {
return $this->storage->isReadable($this->findPathToUse($path));
}
/**
* check if a file can be written to
*
* @param string $path
* @return bool
*/
public function isUpdatable($path) {
return $this->storage->isUpdatable($this->findPathToUse($path));
}
/**
* check if a file can be deleted
*
* @param string $path
* @return bool
*/
public function isDeletable($path) {
return $this->storage->isDeletable($this->findPathToUse($path));
}
/**
* check if a file can be shared
*
* @param string $path
* @return bool
*/
public function isSharable($path) {
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) {
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) {
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) {
return $this->storage->filemtime($this->findPathToUse($path));
}
/**
* see https://www.php.net/manual/en/function.file_get_contents.php
*
* @param string $path
* @return string|false
*/
public function file_get_contents($path) {
return $this->storage->file_get_contents($this->findPathToUse($path));
}
/**
* see https://www.php.net/manual/en/function.file_put_contents.php
*
* @param string $path
* @param mixed $data
* @return int|float|false
*/
public function file_put_contents($path, $data) {
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) {
$result = $this->storage->unlink($this->findPathToUse($path));
if ($result) {
unset($this->namesCache[$path]);
}
return $result;
}
/**
* see https://www.php.net/manual/en/function.rename.php
*
* @param string $source
* @param string $target
* @return bool
*/
public function rename($source, $target) {
// second name always NFC
return $this->storage->rename($this->findPathToUse($source), $this->findPathToUse($target));
}
/**
* see https://www.php.net/manual/en/function.copy.php
*
* @param string $source
* @param string $target
* @return bool
*/
public function copy($source, $target) {
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) {
$result = $this->storage->fopen($this->findPathToUse($path), $mode);
if ($result && $mode !== 'r' && $mode !== 'rb') {
unset($this->namesCache[$path]);
}
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) {
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) {
return $this->storage->hash($type, $this->findPathToUse($path), $raw);
}
/**
* see https://www.php.net/manual/en/function.free_space.php
*
* @param string $path
* @return int|float|bool
*/
public function free_space($path) {
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) {
return $this->storage->touch($this->findPathToUse($path), $mtime);
}
/**
* get the path to a local version of the file.
* The local version of the file can be temporary and doesn't have to be persistent across requests
*
* @param string $path
* @return string|false
*/
public function getLocalFile($path) {
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) {
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) {
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) {
if (!$storage) {
$storage = $this;
}
return $this->storage->getScanner($this->findPathToUse($path), $storage);
}
/**
* get the ETag for a file or folder
*
* @param string $path
* @return string|false
*/
public function getETag($path) {
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) {
if ($sourceStorage === $this) {
return $this->copy($sourceInternalPath, $this->findPathToUse($targetInternalPath));
}
$result = $this->storage->copyFromStorage($sourceStorage, $sourceInternalPath, $this->findPathToUse($targetInternalPath));
if ($result) {
unset($this->namesCache[$targetInternalPath]);
}
return $result;
}
/**
* @param IStorage $sourceStorage
* @param string $sourceInternalPath
* @param string $targetInternalPath
* @return bool
*/
public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
if ($sourceStorage === $this) {
$result = $this->rename($sourceInternalPath, $this->findPathToUse($targetInternalPath));
if ($result) {
unset($this->namesCache[$sourceInternalPath]);
unset($this->namesCache[$targetInternalPath]);
}
return $result;
}
$result = $this->storage->moveFromStorage($sourceStorage, $sourceInternalPath, $this->findPathToUse($targetInternalPath));
if ($result) {
unset($this->namesCache[$sourceInternalPath]);
unset($this->namesCache[$targetInternalPath]);
}
return $result;
}
public function getMetaData($path) {
$entry = $this->storage->getMetaData($this->findPathToUse($path));
$entry['name'] = trim(Filesystem::normalizePath($entry['name']), '/');
return $entry;
}
public function getDirectoryContent($directory): \Traversable {
$entries = $this->storage->getDirectoryContent($this->findPathToUse($directory));
foreach ($entries as $entry) {
$entry['name'] = trim(Filesystem::normalizePath($entry['name']), '/');
yield $entry;
}
}
}
@@ -0,0 +1,56 @@
<?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/>
*
*/
namespace OC\Files\Storage\Wrapper;
use Icewind\Streams\DirectoryWrapper;
use OC\Files\Filesystem;
/**
* Normalize file names while reading directory entries
*/
class EncodingDirectoryWrapper extends DirectoryWrapper {
/**
* @psalm-suppress ImplementedReturnTypeMismatch Until return type is fixed upstream
* @return string|false
*/
public function dir_readdir() {
$file = readdir($this->source);
if ($file !== false && $file !== '.' && $file !== '..') {
$file = trim(Filesystem::normalizePath($file), '/');
}
return $file;
}
/**
* @param resource $source
* @param callable $filter
* @return resource|false
*/
public static function wrap($source) {
return self::wrapSource($source, [
'source' => $source,
]);
}
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,534 @@
<?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/>
*
*/
namespace OC\Files\Storage\Wrapper;
use OC\Files\Cache\Wrapper\CacheJail;
use OC\Files\Cache\Wrapper\JailPropagator;
use OC\Files\Filesystem;
use OCP\Files\Storage\IStorage;
use OCP\Files\Storage\IWriteStreamStorage;
use OCP\Lock\ILockingProvider;
/**
* Jail to a subdirectory of the wrapped storage
*
* This restricts access to a subfolder of the wrapped storage with the subfolder becoming the root folder new storage
*/
class Jail extends Wrapper {
/**
* @var string
*/
protected $rootPath;
/**
* @param array $arguments ['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 getUnjailedPath($path) {
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() {
return $this->storage;
}
public function getJailedPath($path) {
$root = rtrim($this->rootPath, '/') . '/';
if ($path !== $this->rootPath && !str_starts_with($path, $root)) {
return null;
} else {
$path = substr($path, strlen($this->rootPath));
return trim($path, '/');
}
}
public function getId() {
return parent::getId();
}
/**
* see https://www.php.net/manual/en/function.mkdir.php
*
* @param string $path
* @return bool
*/
public function mkdir($path) {
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) {
return $this->getWrapperStorage()->rmdir($this->getUnjailedPath($path));
}
/**
* see https://www.php.net/manual/en/function.opendir.php
*
* @param string $path
* @return resource|false
*/
public function opendir($path) {
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) {
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) {
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) {
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) {
return $this->getWrapperStorage()->filetype($this->getUnjailedPath($path));
}
/**
* see https://www.php.net/manual/en/function.filesize.php
* The result for filesize when called on a folder is required to be 0
*/
public function filesize($path): false|int|float {
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) {
return $this->getWrapperStorage()->isCreatable($this->getUnjailedPath($path));
}
/**
* check if a file can be read
*
* @param string $path
* @return bool
*/
public function isReadable($path) {
return $this->getWrapperStorage()->isReadable($this->getUnjailedPath($path));
}
/**
* check if a file can be written to
*
* @param string $path
* @return bool
*/
public function isUpdatable($path) {
return $this->getWrapperStorage()->isUpdatable($this->getUnjailedPath($path));
}
/**
* check if a file can be deleted
*
* @param string $path
* @return bool
*/
public function isDeletable($path) {
return $this->getWrapperStorage()->isDeletable($this->getUnjailedPath($path));
}
/**
* check if a file can be shared
*
* @param string $path
* @return bool
*/
public function isSharable($path) {
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) {
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) {
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) {
return $this->getWrapperStorage()->filemtime($this->getUnjailedPath($path));
}
/**
* see https://www.php.net/manual/en/function.file_get_contents.php
*
* @param string $path
* @return string|false
*/
public function file_get_contents($path) {
return $this->getWrapperStorage()->file_get_contents($this->getUnjailedPath($path));
}
/**
* see https://www.php.net/manual/en/function.file_put_contents.php
*
* @param string $path
* @param mixed $data
* @return int|float|false
*/
public function file_put_contents($path, $data) {
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) {
return $this->getWrapperStorage()->unlink($this->getUnjailedPath($path));
}
/**
* see https://www.php.net/manual/en/function.rename.php
*
* @param string $source
* @param string $target
* @return bool
*/
public function rename($source, $target) {
return $this->getWrapperStorage()->rename($this->getUnjailedPath($source), $this->getUnjailedPath($target));
}
/**
* see https://www.php.net/manual/en/function.copy.php
*
* @param string $source
* @param string $target
* @return bool
*/
public function copy($source, $target) {
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) {
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) {
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) {
return $this->getWrapperStorage()->hash($type, $this->getUnjailedPath($path), $raw);
}
/**
* see https://www.php.net/manual/en/function.free_space.php
*
* @param string $path
* @return int|float|bool
*/
public function free_space($path) {
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) {
return $this->getWrapperStorage()->touch($this->getUnjailedPath($path), $mtime);
}
/**
* get the path to a local version of the file.
* The local version of the file can be temporary and doesn't have to be persistent across requests
*
* @param string $path
* @return string|false
*/
public function getLocalFile($path) {
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) {
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) {
$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) {
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);
}
/**
* get the ETag for a file or folder
*
* @param string $path
* @return string|false
*/
public function getETag($path) {
return $this->getWrapperStorage()->getETag($this->getUnjailedPath($path));
}
public function getMetaData($path) {
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) {
$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) {
$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) {
$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) {
return [$this->getWrapperStorage(), $this->getUnjailedPath($path)];
}
/**
* @param IStorage $sourceStorage
* @param string $sourceInternalPath
* @param string $targetInternalPath
* @return bool
*/
public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
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) {
if ($sourceStorage === $this) {
return $this->rename($sourceInternalPath, $targetInternalPath);
}
return $this->getWrapperStorage()->moveFromStorage($sourceStorage, $sourceInternalPath, $this->getUnjailedPath($targetInternalPath));
}
public function getPropagator($storage = null) {
if (isset($this->propagator)) {
return $this->propagator;
}
if (!$storage) {
$storage = $this;
}
$this->propagator = new JailPropagator($storage, \OC::$server->getDatabaseConnection());
return $this->propagator;
}
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);
fclose($stream);
fclose($target);
return $count;
}
}
public function getDirectoryContent($directory): \Traversable {
return $this->getWrapperStorage()->getDirectoryContent($this->getUnjailedPath($directory));
}
}
@@ -0,0 +1,142 @@
<?php
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($arguments) {
parent::__construct($arguments);
$this->knowMtimes = new CappedMemoryCache();
$this->clock = $arguments['clock'];
}
public function file_put_contents($path, $data) {
$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($path) {
$stat = parent::stat($path);
if ($stat) {
$this->applyKnownMtime($path, $stat);
}
return $stat;
}
public function getMetaData($path) {
$stat = parent::getMetaData($path);
if ($stat) {
$this->applyKnownMtime($path, $stat);
}
return $stat;
}
private function applyKnownMtime(string $path, array &$stat) {
if (isset($stat['mtime'])) {
$knownMtime = $this->knowMtimes->get($path) ?? 0;
$stat['mtime'] = max($stat['mtime'], $knownMtime);
}
}
public function filemtime($path) {
$knownMtime = $this->knowMtimes->get($path) ?? 0;
return max(parent::filemtime($path), $knownMtime);
}
public function mkdir($path) {
$result = parent::mkdir($path);
if ($result) {
$this->knowMtimes->set($path, $this->clock->now()->getTimestamp());
}
return $result;
}
public function rmdir($path) {
$result = parent::rmdir($path);
if ($result) {
$this->knowMtimes->set($path, $this->clock->now()->getTimestamp());
}
return $result;
}
public function unlink($path) {
$result = parent::unlink($path);
if ($result) {
$this->knowMtimes->set($path, $this->clock->now()->getTimestamp());
}
return $result;
}
public function rename($source, $target) {
$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($source, $target) {
$result = parent::copy($source, $target);
if ($result) {
$this->knowMtimes->set($target, $this->clock->now()->getTimestamp());
}
return $result;
}
public function fopen($path, $mode) {
$result = parent::fopen($path, $mode);
if ($result && $mode === 'w') {
$this->knowMtimes->set($path, $this->clock->now()->getTimestamp());
}
return $result;
}
public function touch($path, $mtime = null) {
$result = parent::touch($path, $mtime);
if ($result) {
$this->knowMtimes->set($path, $mtime ?? $this->clock->now()->getTimestamp());
}
return $result;
}
public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
$result = parent::copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
if ($result) {
$this->knowMtimes->set($targetInternalPath, $this->clock->now()->getTimestamp());
}
return $result;
}
public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
$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;
}
}
@@ -0,0 +1,164 @@
<?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/>
*
*/
namespace OC\Files\Storage\Wrapper;
use OC\Files\Cache\Wrapper\CachePermissionsMask;
use OCP\Constants;
/**
* Mask the permissions of a storage
*
* This can be used to restrict update, create, delete and/or share permissions of a storage
*
* Note that the read permissions can't be masked
*/
class PermissionsMask extends Wrapper {
/**
* @var int the permissions bits we want to keep
*/
private $mask;
/**
* @param array $arguments ['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'];
}
private function checkMask($permissions) {
return ($this->mask & $permissions) === $permissions;
}
public function isUpdatable($path) {
return $this->checkMask(Constants::PERMISSION_UPDATE) and parent::isUpdatable($path);
}
public function isCreatable($path) {
return $this->checkMask(Constants::PERMISSION_CREATE) and parent::isCreatable($path);
}
public function isDeletable($path) {
return $this->checkMask(Constants::PERMISSION_DELETE) and parent::isDeletable($path);
}
public function isSharable($path) {
return $this->checkMask(Constants::PERMISSION_SHARE) and parent::isSharable($path);
}
public function getPermissions($path) {
return $this->storage->getPermissions($path) & $this->mask;
}
public function rename($source, $target) {
//This is a rename of the transfer file to the original file
if (dirname($source) === dirname($target) && strpos($source, '.ocTransferId') > 0) {
return $this->checkMask(Constants::PERMISSION_CREATE) and parent::rename($source, $target);
}
return $this->checkMask(Constants::PERMISSION_UPDATE) and parent::rename($source, $target);
}
public function copy($source, $target) {
return $this->checkMask(Constants::PERMISSION_CREATE) and parent::copy($source, $target);
}
public function touch($path, $mtime = null) {
$permissions = $this->file_exists($path) ? Constants::PERMISSION_UPDATE : Constants::PERMISSION_CREATE;
return $this->checkMask($permissions) and parent::touch($path, $mtime);
}
public function mkdir($path) {
return $this->checkMask(Constants::PERMISSION_CREATE) and parent::mkdir($path);
}
public function rmdir($path) {
return $this->checkMask(Constants::PERMISSION_DELETE) and parent::rmdir($path);
}
public function unlink($path) {
return $this->checkMask(Constants::PERMISSION_DELETE) and parent::unlink($path);
}
public function file_put_contents($path, $data) {
$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) {
if ($mode === 'r' or $mode === 'rb') {
return parent::fopen($path, $mode);
} else {
$permissions = $this->file_exists($path) ? Constants::PERMISSION_UPDATE : Constants::PERMISSION_CREATE;
return $this->checkMask($permissions) ? parent::fopen($path, $mode) : false;
}
}
/**
* 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) {
if (!$storage) {
$storage = $this;
}
$sourceCache = parent::getCache($path, $storage);
return new CachePermissionsMask($sourceCache, $this->mask);
}
public function getMetaData($path) {
$data = parent::getMetaData($path);
if ($data && isset($data['permissions'])) {
$data['scan_permissions'] = $data['scan_permissions'] ?? $data['permissions'];
$data['permissions'] &= $this->mask;
}
return $data;
}
public function getScanner($path = '', $storage = null) {
if (!$storage) {
$storage = $this->storage;
}
return parent::getScanner($path, $storage);
}
public function getDirectoryContent($directory): \Traversable {
foreach ($this->getWrapperStorage()->getDirectoryContent($directory) as $data) {
$data['scan_permissions'] = $data['scan_permissions'] ?? $data['permissions'];
$data['permissions'] &= $this->mask;
yield $data;
}
}
}
@@ -0,0 +1,273 @@
<?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/>
*
*/
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 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;
/**
* @param array $parameters
*/
public function __construct($parameters) {
parent::__construct($parameters);
$this->quota = $parameters['quota'] ?? null;
$this->quotaCallback = $parameters['quotaCallback'] ?? null;
$this->sizeRoot = $parameters['root'] ?? '';
$this->quotaIncludeExternalStorage = $parameters['include_external_storage'] ?? false;
}
/**
* @return int|float quota value
*/
public function getQuota(): int|float {
if ($this->quota === null) {
$quotaCallback = $this->quotaCallback;
if ($quotaCallback === null) {
throw new \Exception("No quota or quota callback provider");
}
$this->quota = $quotaCallback();
}
return $this->quota;
}
private function hasQuota(): bool {
return $this->getQuota() !== FileInfo::SPACE_UNLIMITED;
}
/**
* @param string $path
* @param IStorage $storage
* @return int|float
*/
protected function getSize($path, $storage = null) {
if ($this->quotaIncludeExternalStorage) {
$rootInfo = Filesystem::getFileInfo('', 'ext');
if ($rootInfo) {
return $rootInfo->getSize(true);
}
return FileInfo::SPACE_NOT_COMPUTED;
} else {
$cache = is_null($storage) ? $this->getCache() : $storage->getCache();
$data = $cache->get($path);
if ($data instanceof ICacheEntry && isset($data['size'])) {
return $data['size'];
} else {
return FileInfo::SPACE_NOT_COMPUTED;
}
}
}
/**
* Get free space as limited by the quota
*
* @param string $path
* @return int|float|bool
*/
public function free_space($path) {
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 FileInfo::SPACE_NOT_COMPUTED;
} else {
$free = $this->storage->free_space($path);
$quotaFree = max($this->getQuota() - $used, 0);
// if free space is known
$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|float|false
*/
public function file_put_contents($path, $data) {
if (!$this->hasQuota()) {
return $this->storage->file_put_contents($path, $data);
}
$free = $this->free_space($path);
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) {
if (!$this->hasQuota()) {
return $this->storage->copy($source, $target);
}
$free = $this->free_space($target);
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) {
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) || 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;
}
/**
* Checks whether the given path is a part file
*
* @param string $path Path that may identify a .part file
* @return bool
* @note this is needed for reusing keys
*/
private function isPartFile($path) {
$extension = pathinfo($path, PATHINFO_EXTENSION);
return ($extension === 'part');
}
/**
* Only apply quota for files, not metadata, trash or others
*/
private 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) {
if (!$this->hasQuota()) {
return $this->storage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
}
$free = $this->free_space($targetInternalPath);
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) {
if (!$this->hasQuota()) {
return $this->storage->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
}
$free = $this->free_space($targetInternalPath);
if ($free < 0 || $this->getSize($sourceInternalPath, $sourceStorage) < $free) {
return $this->storage->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
} else {
return false;
}
}
public function mkdir($path) {
if (!$this->hasQuota()) {
return $this->storage->mkdir($path);
}
$free = $this->free_space($path);
if ($this->shouldApplyQuota($path) && $free == 0) {
return false;
}
return parent::mkdir($path);
}
public function touch($path, $mtime = null) {
if (!$this->hasQuota()) {
return $this->storage->touch($path, $mtime);
}
$free = $this->free_space($path);
if ($free == 0) {
return false;
}
return parent::touch($path, $mtime);
}
}
@@ -0,0 +1,657 @@
<?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/>
*
*/
namespace OC\Files\Storage\Wrapper;
use OCP\Files\InvalidPathException;
use OCP\Files\Storage\ILockingStorage;
use OCP\Files\Storage\IStorage;
use OCP\Files\Storage\IWriteStreamStorage;
use OCP\Lock\ILockingProvider;
class Wrapper implements \OC\Files\Storage\Storage, ILockingStorage, IWriteStreamStorage {
/**
* @var \OC\Files\Storage\Storage $storage
*/
protected $storage;
public $cache;
public $scanner;
public $watcher;
public $propagator;
public $updater;
/**
* @param array $parameters
*/
public function __construct($parameters) {
$this->storage = $parameters['storage'];
}
/**
* @return \OC\Files\Storage\Storage
*/
public function getWrapperStorage() {
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() {
return $this->getWrapperStorage()->getId();
}
/**
* see https://www.php.net/manual/en/function.mkdir.php
*
* @param string $path
* @return bool
*/
public function mkdir($path) {
return $this->getWrapperStorage()->mkdir($path);
}
/**
* see https://www.php.net/manual/en/function.rmdir.php
*
* @param string $path
* @return bool
*/
public function rmdir($path) {
return $this->getWrapperStorage()->rmdir($path);
}
/**
* see https://www.php.net/manual/en/function.opendir.php
*
* @param string $path
* @return resource|false
*/
public function opendir($path) {
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) {
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) {
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) {
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) {
return $this->getWrapperStorage()->filetype($path);
}
/**
* see https://www.php.net/manual/en/function.filesize.php
* The result for filesize when called on a folder is required to be 0
*/
public function filesize($path): false|int|float {
return $this->getWrapperStorage()->filesize($path);
}
/**
* check if a file can be created in $path
*
* @param string $path
* @return bool
*/
public function isCreatable($path) {
return $this->getWrapperStorage()->isCreatable($path);
}
/**
* check if a file can be read
*
* @param string $path
* @return bool
*/
public function isReadable($path) {
return $this->getWrapperStorage()->isReadable($path);
}
/**
* check if a file can be written to
*
* @param string $path
* @return bool
*/
public function isUpdatable($path) {
return $this->getWrapperStorage()->isUpdatable($path);
}
/**
* check if a file can be deleted
*
* @param string $path
* @return bool
*/
public function isDeletable($path) {
return $this->getWrapperStorage()->isDeletable($path);
}
/**
* check if a file can be shared
*
* @param string $path
* @return bool
*/
public function isSharable($path) {
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) {
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) {
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) {
return $this->getWrapperStorage()->filemtime($path);
}
/**
* see https://www.php.net/manual/en/function.file_get_contents.php
*
* @param string $path
* @return string|false
*/
public function file_get_contents($path) {
return $this->getWrapperStorage()->file_get_contents($path);
}
/**
* see https://www.php.net/manual/en/function.file_put_contents.php
*
* @param string $path
* @param mixed $data
* @return int|float|false
*/
public function file_put_contents($path, $data) {
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) {
return $this->getWrapperStorage()->unlink($path);
}
/**
* see https://www.php.net/manual/en/function.rename.php
*
* @param string $source
* @param string $target
* @return bool
*/
public function rename($source, $target) {
return $this->getWrapperStorage()->rename($source, $target);
}
/**
* see https://www.php.net/manual/en/function.copy.php
*
* @param string $source
* @param string $target
* @return bool
*/
public function copy($source, $target) {
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) {
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) {
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) {
return $this->getWrapperStorage()->hash($type, $path, $raw);
}
/**
* see https://www.php.net/manual/en/function.free_space.php
*
* @param string $path
* @return int|float|bool
*/
public function free_space($path) {
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) {
return $this->getWrapperStorage()->touch($path, $mtime);
}
/**
* get the path to a local version of the file.
* The local version of the file can be temporary and doesn't have to be persistent across requests
*
* @param string $path
* @return string|false
*/
public function getLocalFile($path) {
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) {
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) {
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) {
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) {
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) {
if (!$storage) {
$storage = $this;
}
return $this->getWrapperStorage()->getWatcher($path, $storage);
}
public function getPropagator($storage = null) {
if (!$storage) {
$storage = $this;
}
return $this->getWrapperStorage()->getPropagator($storage);
}
public function getUpdater($storage = null) {
if (!$storage) {
$storage = $this;
}
return $this->getWrapperStorage()->getUpdater($storage);
}
/**
* @return \OC\Files\Cache\Storage
*/
public function getStorageCache() {
return $this->getWrapperStorage()->getStorageCache();
}
/**
* get the ETag for a file or folder
*
* @param string $path
* @return string|false
*/
public function getETag($path) {
return $this->getWrapperStorage()->getETag($path);
}
/**
* Returns true
*
* @return true
*/
public function test() {
return $this->getWrapperStorage()->test();
}
/**
* Returns the wrapped storage's value for isLocal()
*
* @return bool wrapped storage's isLocal() value
*/
public function isLocal() {
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) {
if (ltrim($class, '\\') === 'OC\Files\Storage\Shared') {
// FIXME Temporary fix to keep existing checks working
$class = '\OCA\Files_Sharing\SharedStorage';
}
return is_a($this, $class) or $this->getWrapperStorage()->instanceOfStorage($class);
}
/**
* @psalm-template T of IStorage
* @psalm-param class-string<T> $class
* @psalm-return T|null
*/
public function getInstanceOfStorage(string $class) {
$storage = $this;
while ($storage instanceof Wrapper) {
if ($storage instanceof $class) {
break;
}
$storage = $storage->getWrapperStorage();
}
if (!($storage instanceof $class)) {
return null;
}
return $storage;
}
/**
* 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) {
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) {
return $this->getWrapperStorage()->getDirectDownload($path);
}
/**
* Get availability of the storage
*
* @return array [ available, last_checked ]
*/
public function getAvailability() {
return $this->getWrapperStorage()->getAvailability();
}
/**
* Set availability of the storage
*
* @param bool $isAvailable
*/
public function setAvailability($isAvailable) {
$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) {
$this->getWrapperStorage()->verifyPath($path, $fileName);
}
/**
* @param IStorage $sourceStorage
* @param string $sourceInternalPath
* @param string $targetInternalPath
* @return bool
*/
public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
if ($sourceStorage === $this) {
return $this->copy($sourceInternalPath, $targetInternalPath);
}
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) {
if ($sourceStorage === $this) {
return $this->rename($sourceInternalPath, $targetInternalPath);
}
return $this->getWrapperStorage()->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
}
public function getMetaData($path) {
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) {
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) {
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) {
if ($this->getWrapperStorage()->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) {
$this->getWrapperStorage()->changeLock($path, $type, $provider);
}
}
/**
* @return bool
*/
public function needsPartFile() {
return $this->getWrapperStorage()->needsPartFile();
}
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);
fclose($stream);
fclose($target);
return $count;
}
}
public function getDirectoryContent($directory): \Traversable {
return $this->getWrapperStorage()->getDirectoryContent($directory);
}
}