2014-11-10 10:00:08 -05:00
|
|
|
<?php
|
2024-05-23 03:26:56 -04:00
|
|
|
|
2014-11-10 10:00:08 -05:00
|
|
|
/**
|
2024-05-23 03:26:56 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2014-11-10 10:00:08 -05:00
|
|
|
*/
|
|
|
|
|
namespace OC\Files\Storage\Wrapper;
|
|
|
|
|
|
|
|
|
|
use OC\Files\Cache\Wrapper\CacheJail;
|
2017-07-11 11:55:04 -04:00
|
|
|
use OC\Files\Cache\Wrapper\JailPropagator;
|
2024-04-08 11:05:33 -04:00
|
|
|
use OC\Files\Cache\Wrapper\JailWatcher;
|
2018-10-11 06:54:45 -04:00
|
|
|
use OC\Files\Filesystem;
|
2025-05-15 18:07:33 -04:00
|
|
|
use OCP\Files;
|
2024-09-19 12:19:34 -04:00
|
|
|
use OCP\Files\Cache\ICache;
|
|
|
|
|
use OCP\Files\Cache\IPropagator;
|
|
|
|
|
use OCP\Files\Cache\IWatcher;
|
2017-07-19 13:44:10 -04:00
|
|
|
use OCP\Files\Storage\IStorage;
|
2018-10-31 14:41:55 -04:00
|
|
|
use OCP\Files\Storage\IWriteStreamStorage;
|
2015-05-04 08:21:34 -04:00
|
|
|
use OCP\Lock\ILockingProvider;
|
2014-11-10 10:00:08 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
|
|
/**
|
2024-10-08 09:13:16 -04:00
|
|
|
* @param array $parameters ['storage' => $storage, 'root' => $root]
|
2014-11-10 10:00:08 -05:00
|
|
|
*
|
|
|
|
|
* $storage: The storage that will be wrapper
|
|
|
|
|
* $root: The folder in the wrapped storage that will become the root folder of the wrapped storage
|
|
|
|
|
*/
|
2024-10-08 09:13:16 -04:00
|
|
|
public function __construct(array $parameters) {
|
|
|
|
|
parent::__construct($parameters);
|
|
|
|
|
$this->rootPath = $parameters['root'];
|
2014-11-10 10:00:08 -05:00
|
|
|
}
|
|
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function getUnjailedPath(string $path): string {
|
2020-08-28 05:14:50 -04:00
|
|
|
return trim(Filesystem::normalizePath($this->rootPath . '/' . $path), '/');
|
2014-11-10 10:00:08 -05:00
|
|
|
}
|
|
|
|
|
|
2019-09-25 13:17:06 -04:00
|
|
|
/**
|
|
|
|
|
* This is separate from Wrapper::getWrapperStorage so we can get the jailed storage consistently even if the jail is inside another wrapper
|
|
|
|
|
*/
|
2024-09-19 12:19:34 -04:00
|
|
|
public function getUnjailedStorage(): IStorage {
|
2019-09-25 13:17:06 -04:00
|
|
|
return $this->storage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function getJailedPath(string $path): ?string {
|
2017-11-10 11:02:35 -05:00
|
|
|
$root = rtrim($this->rootPath, '/') . '/';
|
|
|
|
|
|
2023-05-15 07:47:19 -04:00
|
|
|
if ($path !== $this->rootPath && !str_starts_with($path, $root)) {
|
2017-11-10 11:02:35 -05:00
|
|
|
return null;
|
|
|
|
|
} else {
|
|
|
|
|
$path = substr($path, strlen($this->rootPath));
|
|
|
|
|
return trim($path, '/');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-19 12:19:34 -04:00
|
|
|
public function getId(): string {
|
2017-01-25 11:03:45 -05:00
|
|
|
return parent::getId();
|
2014-11-10 10:00:08 -05:00
|
|
|
}
|
|
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function mkdir(string $path): bool {
|
2017-04-12 08:55:47 -04:00
|
|
|
return $this->getWrapperStorage()->mkdir($this->getUnjailedPath($path));
|
2014-11-10 10:00:08 -05:00
|
|
|
}
|
|
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function rmdir(string $path): bool {
|
2017-04-12 08:55:47 -04:00
|
|
|
return $this->getWrapperStorage()->rmdir($this->getUnjailedPath($path));
|
2014-11-10 10:00:08 -05:00
|
|
|
}
|
|
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function opendir(string $path) {
|
2017-04-12 08:55:47 -04:00
|
|
|
return $this->getWrapperStorage()->opendir($this->getUnjailedPath($path));
|
2014-11-10 10:00:08 -05:00
|
|
|
}
|
|
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function is_dir(string $path): bool {
|
2017-04-12 08:55:47 -04:00
|
|
|
return $this->getWrapperStorage()->is_dir($this->getUnjailedPath($path));
|
2014-11-10 10:00:08 -05:00
|
|
|
}
|
|
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function is_file(string $path): bool {
|
2017-04-12 08:55:47 -04:00
|
|
|
return $this->getWrapperStorage()->is_file($this->getUnjailedPath($path));
|
2014-11-10 10:00:08 -05:00
|
|
|
}
|
|
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function stat(string $path): array|false {
|
2017-04-12 08:55:47 -04:00
|
|
|
return $this->getWrapperStorage()->stat($this->getUnjailedPath($path));
|
2014-11-10 10:00:08 -05:00
|
|
|
}
|
|
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function filetype(string $path): string|false {
|
2017-04-12 08:55:47 -04:00
|
|
|
return $this->getWrapperStorage()->filetype($this->getUnjailedPath($path));
|
2014-11-10 10:00:08 -05:00
|
|
|
}
|
|
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function filesize(string $path): int|float|false {
|
2017-04-12 08:55:47 -04:00
|
|
|
return $this->getWrapperStorage()->filesize($this->getUnjailedPath($path));
|
2014-11-10 10:00:08 -05:00
|
|
|
}
|
|
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function isCreatable(string $path): bool {
|
2017-04-12 08:55:47 -04:00
|
|
|
return $this->getWrapperStorage()->isCreatable($this->getUnjailedPath($path));
|
2014-11-10 10:00:08 -05:00
|
|
|
}
|
|
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function isReadable(string $path): bool {
|
2017-04-12 08:55:47 -04:00
|
|
|
return $this->getWrapperStorage()->isReadable($this->getUnjailedPath($path));
|
2014-11-10 10:00:08 -05:00
|
|
|
}
|
|
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function isUpdatable(string $path): bool {
|
2017-04-12 08:55:47 -04:00
|
|
|
return $this->getWrapperStorage()->isUpdatable($this->getUnjailedPath($path));
|
2014-11-10 10:00:08 -05:00
|
|
|
}
|
|
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function isDeletable(string $path): bool {
|
2017-04-12 08:55:47 -04:00
|
|
|
return $this->getWrapperStorage()->isDeletable($this->getUnjailedPath($path));
|
2014-11-10 10:00:08 -05:00
|
|
|
}
|
|
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function isSharable(string $path): bool {
|
2017-04-12 08:55:47 -04:00
|
|
|
return $this->getWrapperStorage()->isSharable($this->getUnjailedPath($path));
|
2014-11-10 10:00:08 -05:00
|
|
|
}
|
|
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function getPermissions(string $path): int {
|
2017-04-12 08:55:47 -04:00
|
|
|
return $this->getWrapperStorage()->getPermissions($this->getUnjailedPath($path));
|
2014-11-10 10:00:08 -05:00
|
|
|
}
|
|
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function file_exists(string $path): bool {
|
2017-04-12 08:55:47 -04:00
|
|
|
return $this->getWrapperStorage()->file_exists($this->getUnjailedPath($path));
|
2014-11-10 10:00:08 -05:00
|
|
|
}
|
|
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function filemtime(string $path): int|false {
|
2017-04-12 08:55:47 -04:00
|
|
|
return $this->getWrapperStorage()->filemtime($this->getUnjailedPath($path));
|
2014-11-10 10:00:08 -05:00
|
|
|
}
|
|
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function file_get_contents(string $path): string|false {
|
2017-04-12 08:55:47 -04:00
|
|
|
return $this->getWrapperStorage()->file_get_contents($this->getUnjailedPath($path));
|
2014-11-10 10:00:08 -05:00
|
|
|
}
|
|
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function file_put_contents(string $path, mixed $data): int|float|false {
|
2017-04-12 08:55:47 -04:00
|
|
|
return $this->getWrapperStorage()->file_put_contents($this->getUnjailedPath($path), $data);
|
2014-11-10 10:00:08 -05:00
|
|
|
}
|
|
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function unlink(string $path): bool {
|
2017-04-12 08:55:47 -04:00
|
|
|
return $this->getWrapperStorage()->unlink($this->getUnjailedPath($path));
|
2014-11-10 10:00:08 -05:00
|
|
|
}
|
|
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function rename(string $source, string $target): bool {
|
2022-10-18 06:49:34 -04:00
|
|
|
return $this->getWrapperStorage()->rename($this->getUnjailedPath($source), $this->getUnjailedPath($target));
|
2014-11-10 10:00:08 -05:00
|
|
|
}
|
|
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function copy(string $source, string $target): bool {
|
2022-10-18 06:49:34 -04:00
|
|
|
return $this->getWrapperStorage()->copy($this->getUnjailedPath($source), $this->getUnjailedPath($target));
|
2014-11-10 10:00:08 -05:00
|
|
|
}
|
|
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function fopen(string $path, string $mode) {
|
2017-04-12 08:55:47 -04:00
|
|
|
return $this->getWrapperStorage()->fopen($this->getUnjailedPath($path), $mode);
|
2014-11-10 10:00:08 -05:00
|
|
|
}
|
|
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function getMimeType(string $path): string|false {
|
2017-04-12 08:55:47 -04:00
|
|
|
return $this->getWrapperStorage()->getMimeType($this->getUnjailedPath($path));
|
2014-11-10 10:00:08 -05:00
|
|
|
}
|
|
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function hash(string $type, string $path, bool $raw = false): string|false {
|
2017-04-12 08:55:47 -04:00
|
|
|
return $this->getWrapperStorage()->hash($type, $this->getUnjailedPath($path), $raw);
|
2014-11-10 10:00:08 -05:00
|
|
|
}
|
|
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function free_space(string $path): int|float|false {
|
2017-04-12 08:55:47 -04:00
|
|
|
return $this->getWrapperStorage()->free_space($this->getUnjailedPath($path));
|
2014-11-10 10:00:08 -05:00
|
|
|
}
|
|
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function touch(string $path, ?int $mtime = null): bool {
|
2017-04-12 08:55:47 -04:00
|
|
|
return $this->getWrapperStorage()->touch($this->getUnjailedPath($path), $mtime);
|
2014-11-10 10:00:08 -05:00
|
|
|
}
|
|
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function getLocalFile(string $path): string|false {
|
2017-04-12 08:55:47 -04:00
|
|
|
return $this->getWrapperStorage()->getLocalFile($this->getUnjailedPath($path));
|
2014-11-10 10:00:08 -05:00
|
|
|
}
|
|
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function hasUpdated(string $path, int $time): bool {
|
2017-04-12 08:55:47 -04:00
|
|
|
return $this->getWrapperStorage()->hasUpdated($this->getUnjailedPath($path), $time);
|
2014-11-10 10:00:08 -05:00
|
|
|
}
|
|
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function getCache(string $path = '', ?IStorage $storage = null): ICache {
|
2023-10-19 13:27:00 -04:00
|
|
|
$sourceCache = $this->getWrapperStorage()->getCache($this->getUnjailedPath($path));
|
2014-11-10 10:00:08 -05:00
|
|
|
return new CacheJail($sourceCache, $this->rootPath);
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function getOwner(string $path): string|false {
|
2017-04-12 08:55:47 -04:00
|
|
|
return $this->getWrapperStorage()->getOwner($this->getUnjailedPath($path));
|
2014-11-10 10:00:08 -05:00
|
|
|
}
|
|
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function getWatcher(string $path = '', ?IStorage $storage = null): IWatcher {
|
2024-04-08 11:05:33 -04:00
|
|
|
$sourceWatcher = $this->getWrapperStorage()->getWatcher($this->getUnjailedPath($path), $this->getWrapperStorage());
|
|
|
|
|
return new JailWatcher($sourceWatcher, $this->rootPath);
|
2014-11-10 10:00:08 -05:00
|
|
|
}
|
|
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function getETag(string $path): string|false {
|
2017-04-12 08:55:47 -04:00
|
|
|
return $this->getWrapperStorage()->getETag($this->getUnjailedPath($path));
|
2014-11-10 10:00:08 -05:00
|
|
|
}
|
2015-05-04 08:21:34 -04:00
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function getMetaData(string $path): ?array {
|
2017-04-12 08:55:47 -04:00
|
|
|
return $this->getWrapperStorage()->getMetaData($this->getUnjailedPath($path));
|
2016-04-15 03:02:01 -04:00
|
|
|
}
|
|
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function acquireLock(string $path, int $type, ILockingProvider $provider): void {
|
2017-04-12 08:55:47 -04:00
|
|
|
$this->getWrapperStorage()->acquireLock($this->getUnjailedPath($path), $type, $provider);
|
2015-05-04 08:21:34 -04:00
|
|
|
}
|
|
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function releaseLock(string $path, int $type, ILockingProvider $provider): void {
|
2017-04-12 08:55:47 -04:00
|
|
|
$this->getWrapperStorage()->releaseLock($this->getUnjailedPath($path), $type, $provider);
|
2015-05-04 08:21:34 -04:00
|
|
|
}
|
2015-05-29 08:40:06 -04:00
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function changeLock(string $path, int $type, ILockingProvider $provider): void {
|
2017-04-12 08:55:47 -04:00
|
|
|
$this->getWrapperStorage()->changeLock($this->getUnjailedPath($path), $type, $provider);
|
2015-05-29 08:40:06 -04:00
|
|
|
}
|
2016-04-15 03:02:01 -04:00
|
|
|
|
2016-04-15 06:40:15 -04:00
|
|
|
/**
|
|
|
|
|
* Resolve the path for the source of the share
|
|
|
|
|
*/
|
2024-10-01 10:12:30 -04:00
|
|
|
public function resolvePath(string $path): array {
|
2017-04-12 08:55:47 -04:00
|
|
|
return [$this->getWrapperStorage(), $this->getUnjailedPath($path)];
|
2016-04-15 03:02:01 -04:00
|
|
|
}
|
2016-04-18 15:03:16 -04:00
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function copyFromStorage(IStorage $sourceStorage, string $sourceInternalPath, string $targetInternalPath): bool {
|
2016-04-18 15:03:16 -04:00
|
|
|
if ($sourceStorage === $this) {
|
|
|
|
|
return $this->copy($sourceInternalPath, $targetInternalPath);
|
|
|
|
|
}
|
2017-04-12 08:55:47 -04:00
|
|
|
return $this->getWrapperStorage()->copyFromStorage($sourceStorage, $sourceInternalPath, $this->getUnjailedPath($targetInternalPath));
|
2016-04-18 15:03:16 -04:00
|
|
|
}
|
|
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function moveFromStorage(IStorage $sourceStorage, string $sourceInternalPath, string $targetInternalPath): bool {
|
2016-04-18 15:03:16 -04:00
|
|
|
if ($sourceStorage === $this) {
|
|
|
|
|
return $this->rename($sourceInternalPath, $targetInternalPath);
|
|
|
|
|
}
|
2017-04-12 08:55:47 -04:00
|
|
|
return $this->getWrapperStorage()->moveFromStorage($sourceStorage, $sourceInternalPath, $this->getUnjailedPath($targetInternalPath));
|
2016-04-18 15:03:16 -04:00
|
|
|
}
|
2017-07-11 11:55:04 -04:00
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function getPropagator(?IStorage $storage = null): IPropagator {
|
2017-07-11 11:55:04 -04:00
|
|
|
if (isset($this->propagator)) {
|
|
|
|
|
return $this->propagator;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!$storage) {
|
|
|
|
|
$storage = $this;
|
|
|
|
|
}
|
|
|
|
|
$this->propagator = new JailPropagator($storage, \OC::$server->getDatabaseConnection());
|
|
|
|
|
return $this->propagator;
|
|
|
|
|
}
|
2018-10-31 14:41:55 -04:00
|
|
|
|
2024-03-28 11:13:19 -04:00
|
|
|
public function writeStream(string $path, $stream, ?int $size = null): int {
|
2018-10-31 14:41:55 -04:00
|
|
|
$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');
|
2025-05-15 18:07:33 -04:00
|
|
|
$count = Files::streamCopy($stream, $target);
|
2018-10-31 14:41:55 -04:00
|
|
|
fclose($stream);
|
|
|
|
|
fclose($target);
|
|
|
|
|
return $count;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-27 12:47:20 -04:00
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function getDirectoryContent(string $directory): \Traversable {
|
2020-04-29 10:34:41 -04:00
|
|
|
return $this->getWrapperStorage()->getDirectoryContent($this->getUnjailedPath($directory));
|
2020-03-27 12:47:20 -04:00
|
|
|
}
|
2014-11-10 10:00:08 -05:00
|
|
|
}
|