2013-01-26 15:42:59 -05:00
|
|
|
<?php
|
2024-05-23 03:26:56 -04:00
|
|
|
|
2013-01-26 15:42:59 -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
|
2013-01-26 15:42:59 -05:00
|
|
|
*/
|
2013-04-25 18:01:36 -04:00
|
|
|
namespace OC\Files\Mount;
|
|
|
|
|
|
2019-11-22 14:52:10 -05:00
|
|
|
use OC\Files\Filesystem;
|
2013-06-07 11:07:13 -04:00
|
|
|
use OC\Files\Storage\Storage;
|
2019-11-22 14:52:10 -05:00
|
|
|
use OC\Files\Storage\StorageFactory;
|
2014-11-24 09:54:42 -05:00
|
|
|
use OCP\Files\Mount\IMountPoint;
|
2022-02-02 10:12:57 -05:00
|
|
|
use OCP\Files\Storage\IStorageFactory;
|
2022-03-30 04:55:41 -04:00
|
|
|
use Psr\Log\LoggerInterface;
|
2013-01-26 15:42:59 -05:00
|
|
|
|
2014-11-24 09:54:42 -05:00
|
|
|
class MountPoint implements IMountPoint {
|
2013-01-26 15:42:59 -05:00
|
|
|
/**
|
2021-03-24 09:56:40 -04:00
|
|
|
* @var \OC\Files\Storage\Storage|null $storage
|
2013-01-26 15:42:59 -05:00
|
|
|
*/
|
2014-05-21 19:39:24 -04:00
|
|
|
protected $storage = null;
|
|
|
|
|
protected $class;
|
|
|
|
|
protected $storageId;
|
2023-02-06 12:20:08 -05:00
|
|
|
protected $numericStorageId = null;
|
2016-08-25 09:49:18 -04:00
|
|
|
protected $rootId = null;
|
2014-12-16 07:33:58 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Configuration options for the storage backend
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2020-03-26 04:30:18 -04:00
|
|
|
protected $arguments = [];
|
2014-05-21 19:39:24 -04:00
|
|
|
protected $mountPoint;
|
2013-01-26 15:42:59 -05:00
|
|
|
|
2014-12-16 07:33:58 -05:00
|
|
|
/**
|
|
|
|
|
* Mount specific options
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2020-03-26 04:30:18 -04:00
|
|
|
protected $mountOptions = [];
|
2014-12-16 07:33:58 -05:00
|
|
|
|
2013-05-08 16:35:10 -04:00
|
|
|
/**
|
2014-11-24 09:54:42 -05:00
|
|
|
* @var \OC\Files\Storage\StorageFactory $loader
|
2013-05-08 16:35:10 -04:00
|
|
|
*/
|
2013-06-07 11:07:13 -04:00
|
|
|
private $loader;
|
2013-05-08 16:35:10 -04:00
|
|
|
|
2015-01-27 10:55:59 -05:00
|
|
|
/**
|
|
|
|
|
* Specified whether the storage is invalid after failing to
|
|
|
|
|
* instantiate it.
|
|
|
|
|
*
|
|
|
|
|
* @var bool
|
|
|
|
|
*/
|
|
|
|
|
private $invalidStorage = false;
|
|
|
|
|
|
2016-08-22 09:12:39 -04:00
|
|
|
/** @var int|null */
|
2016-07-13 10:29:51 -04:00
|
|
|
protected $mountId;
|
|
|
|
|
|
2022-02-02 10:12:57 -05:00
|
|
|
/** @var string */
|
|
|
|
|
protected $mountProvider;
|
|
|
|
|
|
2013-01-26 15:42:59 -05:00
|
|
|
/**
|
2014-05-11 13:28:45 -04:00
|
|
|
* @param string|\OC\Files\Storage\Storage $storage
|
2013-01-26 15:42:59 -05:00
|
|
|
* @param string $mountpoint
|
2014-12-16 07:33:58 -05:00
|
|
|
* @param array $arguments (optional) configuration for the storage backend
|
2014-11-24 09:54:42 -05:00
|
|
|
* @param \OCP\Files\Storage\IStorageFactory $loader
|
2014-12-16 07:33:58 -05:00
|
|
|
* @param array $mountOptions mount specific options
|
2016-07-13 10:29:51 -04:00
|
|
|
* @param int|null $mountId
|
2022-02-02 10:12:57 -05:00
|
|
|
* @param string|null $mountProvider
|
2016-07-13 10:29:51 -04:00
|
|
|
* @throws \Exception
|
2013-01-26 15:42:59 -05:00
|
|
|
*/
|
2022-02-02 10:12:57 -05:00
|
|
|
public function __construct(
|
|
|
|
|
$storage,
|
|
|
|
|
string $mountpoint,
|
2024-03-28 11:13:19 -04:00
|
|
|
?array $arguments = null,
|
|
|
|
|
?IStorageFactory $loader = null,
|
|
|
|
|
?array $mountOptions = null,
|
|
|
|
|
?int $mountId = null,
|
|
|
|
|
?string $mountProvider = null,
|
2022-02-02 10:12:57 -05:00
|
|
|
) {
|
2013-01-26 15:42:59 -05:00
|
|
|
if (is_null($arguments)) {
|
2020-03-26 04:30:18 -04:00
|
|
|
$arguments = [];
|
2013-01-26 15:42:59 -05:00
|
|
|
}
|
2013-06-07 11:07:13 -04:00
|
|
|
if (is_null($loader)) {
|
2014-11-24 09:54:42 -05:00
|
|
|
$this->loader = new StorageFactory();
|
2013-06-07 11:07:13 -04:00
|
|
|
} else {
|
|
|
|
|
$this->loader = $loader;
|
|
|
|
|
}
|
2013-01-26 15:42:59 -05:00
|
|
|
|
2014-12-16 07:33:58 -05:00
|
|
|
if (!is_null($mountOptions)) {
|
|
|
|
|
$this->mountOptions = $mountOptions;
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-25 18:01:36 -04:00
|
|
|
$mountpoint = $this->formatPath($mountpoint);
|
2015-03-05 06:28:17 -05:00
|
|
|
$this->mountPoint = $mountpoint;
|
2020-08-31 10:38:47 -04:00
|
|
|
$this->mountId = $mountId;
|
2013-06-07 11:07:13 -04:00
|
|
|
if ($storage instanceof Storage) {
|
2013-01-26 15:42:59 -05:00
|
|
|
$this->class = get_class($storage);
|
2015-08-10 18:20:27 -04:00
|
|
|
$this->storage = $this->loader->wrap($this, $storage);
|
2013-01-26 15:42:59 -05:00
|
|
|
} else {
|
|
|
|
|
// Update old classes to new namespace
|
2023-05-15 07:47:19 -04:00
|
|
|
if (str_contains($storage, 'OC_Filestorage_')) {
|
2013-01-26 15:42:59 -05:00
|
|
|
$storage = '\OC\Files\Storage\\' . substr($storage, 15);
|
|
|
|
|
}
|
|
|
|
|
$this->class = $storage;
|
|
|
|
|
$this->arguments = $arguments;
|
|
|
|
|
}
|
2022-02-02 10:12:57 -05:00
|
|
|
if ($mountProvider) {
|
|
|
|
|
if (strlen($mountProvider) > 128) {
|
|
|
|
|
throw new \Exception("Mount provider $mountProvider name exceeds the limit of 128 characters");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$this->mountProvider = $mountProvider ?? '';
|
2013-01-26 15:42:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-04-30 10:56:09 -04:00
|
|
|
* get complete path to the mount point, relative to data/
|
|
|
|
|
*
|
2013-01-26 15:42:59 -05:00
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getMountPoint() {
|
|
|
|
|
return $this->mountPoint;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-07 13:00:03 -04:00
|
|
|
/**
|
2015-04-13 06:36:47 -04:00
|
|
|
* Sets the mount point path, relative to data/
|
|
|
|
|
*
|
2014-04-07 13:00:03 -04:00
|
|
|
* @param string $mountPoint new mount point
|
|
|
|
|
*/
|
|
|
|
|
public function setMountPoint($mountPoint) {
|
2015-04-13 06:36:47 -04:00
|
|
|
$this->mountPoint = $this->formatPath($mountPoint);
|
2014-04-07 13:00:03 -04:00
|
|
|
}
|
|
|
|
|
|
2013-01-26 15:42:59 -05:00
|
|
|
/**
|
2013-04-25 18:01:36 -04:00
|
|
|
* create the storage that is mounted
|
2013-01-26 15:42:59 -05:00
|
|
|
*/
|
|
|
|
|
private function createStorage() {
|
2015-01-27 10:55:59 -05:00
|
|
|
if ($this->invalidStorage) {
|
2016-08-22 09:12:39 -04:00
|
|
|
return;
|
2015-01-27 10:55:59 -05:00
|
|
|
}
|
|
|
|
|
|
2013-01-26 15:42:59 -05:00
|
|
|
if (class_exists($this->class)) {
|
|
|
|
|
try {
|
2016-08-22 09:12:39 -04:00
|
|
|
$class = $this->class;
|
|
|
|
|
// prevent recursion by setting the storage before applying wrappers
|
|
|
|
|
$this->storage = new $class($this->arguments);
|
|
|
|
|
$this->storage = $this->loader->wrap($this, $this->storage);
|
2013-01-26 15:42:59 -05:00
|
|
|
} catch (\Exception $exception) {
|
2016-08-22 09:12:39 -04:00
|
|
|
$this->storage = null;
|
2015-01-27 10:55:59 -05:00
|
|
|
$this->invalidStorage = true;
|
2014-06-11 16:13:27 -04:00
|
|
|
if ($this->mountPoint === '/') {
|
|
|
|
|
// the root storage could not be initialized, show the user!
|
|
|
|
|
throw new \Exception('The root storage could not be initialized. Please contact your local administrator.', $exception->getCode(), $exception);
|
|
|
|
|
} else {
|
2022-03-30 04:55:41 -04:00
|
|
|
\OC::$server->get(LoggerInterface::class)->error($exception->getMessage(), ['exception' => $exception]);
|
2014-06-11 16:13:27 -04:00
|
|
|
}
|
2016-08-22 09:12:39 -04:00
|
|
|
return;
|
2013-01-26 15:42:59 -05:00
|
|
|
}
|
|
|
|
|
} else {
|
2022-03-30 04:55:41 -04:00
|
|
|
\OC::$server->get(LoggerInterface::class)->error('Storage backend ' . $this->class . ' not found', ['app' => 'core']);
|
2015-01-27 10:55:59 -05:00
|
|
|
$this->invalidStorage = true;
|
2016-08-22 09:12:39 -04:00
|
|
|
return;
|
2013-01-26 15:42:59 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-03-24 09:56:40 -04:00
|
|
|
* @return \OC\Files\Storage\Storage|null
|
2013-01-26 15:42:59 -05:00
|
|
|
*/
|
|
|
|
|
public function getStorage() {
|
|
|
|
|
if (is_null($this->storage)) {
|
2016-08-22 09:12:39 -04:00
|
|
|
$this->createStorage();
|
2013-01-26 15:42:59 -05:00
|
|
|
}
|
|
|
|
|
return $this->storage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-02-06 12:56:54 -05:00
|
|
|
* @return string|null
|
2013-01-26 15:42:59 -05:00
|
|
|
*/
|
|
|
|
|
public function getStorageId() {
|
|
|
|
|
if (!$this->storageId) {
|
2023-02-06 12:56:54 -05:00
|
|
|
$storage = $this->getStorage();
|
|
|
|
|
if (is_null($storage)) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
$this->storageId = $storage->getId();
|
2013-02-15 15:49:40 -05:00
|
|
|
if (strlen($this->storageId) > 64) {
|
|
|
|
|
$this->storageId = md5($this->storageId);
|
|
|
|
|
}
|
2013-01-26 15:42:59 -05:00
|
|
|
}
|
|
|
|
|
return $this->storageId;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-09 09:52:13 -04:00
|
|
|
/**
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
public function getNumericStorageId() {
|
2023-02-06 12:20:08 -05:00
|
|
|
if (is_null($this->numericStorageId)) {
|
2023-02-06 12:56:54 -05:00
|
|
|
$storage = $this->getStorage();
|
|
|
|
|
if (is_null($storage)) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2025-01-13 11:59:17 -05:00
|
|
|
$this->numericStorageId = $storage->getCache()->getNumericStorageId();
|
2023-02-06 12:20:08 -05:00
|
|
|
}
|
|
|
|
|
return $this->numericStorageId;
|
2016-08-09 09:52:13 -04:00
|
|
|
}
|
|
|
|
|
|
2013-01-26 15:42:59 -05:00
|
|
|
/**
|
|
|
|
|
* @param string $path
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getInternalPath($path) {
|
2016-05-11 07:12:27 -04:00
|
|
|
$path = Filesystem::normalizePath($path, true, false, true);
|
2013-01-26 15:42:59 -05:00
|
|
|
if ($this->mountPoint === $path or $this->mountPoint . '/' === $path) {
|
|
|
|
|
$internalPath = '';
|
|
|
|
|
} else {
|
|
|
|
|
$internalPath = substr($path, strlen($this->mountPoint));
|
|
|
|
|
}
|
2014-05-28 07:47:50 -04:00
|
|
|
// substr returns false instead of an empty string, we always want a string
|
2014-05-21 19:39:24 -04:00
|
|
|
return (string)$internalPath;
|
2013-01-26 15:42:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $path
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2013-04-25 18:01:36 -04:00
|
|
|
private function formatPath($path) {
|
2013-01-26 15:42:59 -05:00
|
|
|
$path = Filesystem::normalizePath($path);
|
|
|
|
|
if (strlen($path) > 1) {
|
|
|
|
|
$path .= '/';
|
|
|
|
|
}
|
|
|
|
|
return $path;
|
|
|
|
|
}
|
2013-07-25 10:00:24 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param callable $wrapper
|
|
|
|
|
*/
|
|
|
|
|
public function wrapStorage($wrapper) {
|
2015-01-27 10:55:59 -05:00
|
|
|
$storage = $this->getStorage();
|
|
|
|
|
// storage can be null if it couldn't be initialized
|
|
|
|
|
if ($storage != null) {
|
2015-03-05 11:22:48 -05:00
|
|
|
$this->storage = $wrapper($this->mountPoint, $storage, $this);
|
2015-01-27 10:55:59 -05:00
|
|
|
}
|
2013-07-25 10:00:24 -04:00
|
|
|
}
|
2014-12-16 07:33:58 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a mount option
|
|
|
|
|
*
|
|
|
|
|
* @param string $name Name of the mount option to get
|
|
|
|
|
* @param mixed $default Default value for the mount option
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
|
|
|
|
public function getOption($name, $default) {
|
2023-07-07 06:13:21 -04:00
|
|
|
return $this->mountOptions[$name] ?? $default;
|
2014-12-16 07:33:58 -05:00
|
|
|
}
|
2015-03-05 11:10:52 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get all options for the mount
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function getOptions() {
|
|
|
|
|
return $this->mountOptions;
|
|
|
|
|
}
|
2016-04-15 08:03:48 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the file id of the root of the storage
|
|
|
|
|
*
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
public function getStorageRootId() {
|
2020-07-08 07:58:27 -04:00
|
|
|
if (is_null($this->rootId) || $this->rootId === -1) {
|
2021-06-17 08:32:38 -04:00
|
|
|
$storage = $this->getStorage();
|
|
|
|
|
// if we can't create the storage return -1 as root id, this is then handled the same as if the root isn't scanned yet
|
|
|
|
|
if ($storage === null) {
|
|
|
|
|
$this->rootId = -1;
|
|
|
|
|
} else {
|
|
|
|
|
$this->rootId = (int)$storage->getCache()->getId('');
|
|
|
|
|
}
|
2016-08-25 09:49:18 -04:00
|
|
|
}
|
|
|
|
|
return $this->rootId;
|
2016-04-15 08:03:48 -04:00
|
|
|
}
|
2016-07-13 10:29:51 -04:00
|
|
|
|
|
|
|
|
public function getMountId() {
|
|
|
|
|
return $this->mountId;
|
|
|
|
|
}
|
2017-04-26 08:53:11 -04:00
|
|
|
|
|
|
|
|
public function getMountType() {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
2022-02-02 10:12:57 -05:00
|
|
|
|
|
|
|
|
public function getMountProvider(): string {
|
|
|
|
|
return $this->mountProvider;
|
|
|
|
|
}
|
2013-01-26 15:42:59 -05:00
|
|
|
}
|