2013-04-25 22:01:36 +00:00
|
|
|
<?php
|
2019-12-03 18:57:53 +00:00
|
|
|
|
2018-03-26 14:34:19 +00:00
|
|
|
declare(strict_types=1);
|
2019-12-03 18:57:53 +00:00
|
|
|
|
2013-04-25 22:01:36 +00:00
|
|
|
/**
|
2016-07-21 15:07:57 +00:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2016-05-26 17:56:05 +00:00
|
|
|
* @author Björn Schießle <bjoern@schiessle.org>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-07-21 16:13:36 +00:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2016-01-12 14:02:16 +00:00
|
|
|
* @author Robin McCorkell <robin@mccorkell.me.uk>
|
2019-12-03 18:57:53 +00:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2023-09-25 16:25:53 +00:00
|
|
|
* @author Jonas <jonas@freesources.org>
|
2015-03-26 10:44:34 +00:00
|
|
|
*
|
|
|
|
* @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,
|
2019-12-03 18:57:53 +00:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2015-03-26 10:44:34 +00:00
|
|
|
*
|
2013-04-25 22:01:36 +00:00
|
|
|
*/
|
2022-03-02 18:38:21 +00:00
|
|
|
|
2013-04-25 22:01:36 +00:00
|
|
|
namespace OC\Files\Mount;
|
|
|
|
|
2018-03-26 17:53:10 +00:00
|
|
|
use OC\Files\Filesystem;
|
2022-02-10 16:36:46 +00:00
|
|
|
use OC\Files\SetupManager;
|
2022-03-02 18:38:21 +00:00
|
|
|
use OC\Files\SetupManagerFactory;
|
2023-11-23 09:22:34 +00:00
|
|
|
use OCP\Cache\CappedMemoryCache;
|
2023-09-25 16:25:53 +00:00
|
|
|
use OCP\Files\Config\ICachedMountInfo;
|
2015-07-01 13:57:04 +00:00
|
|
|
use OCP\Files\Mount\IMountManager;
|
2015-07-01 14:13:33 +00:00
|
|
|
use OCP\Files\Mount\IMountPoint;
|
2022-02-23 17:11:46 +00:00
|
|
|
use OCP\Files\NotFoundException;
|
2013-04-25 22:01:36 +00:00
|
|
|
|
2015-07-01 13:57:04 +00:00
|
|
|
class Manager implements IMountManager {
|
2018-03-26 17:53:10 +00:00
|
|
|
/** @var MountPoint[] */
|
2022-02-10 13:15:07 +00:00
|
|
|
private array $mounts = [];
|
2022-03-02 18:24:26 +00:00
|
|
|
/** @var CappedMemoryCache<IMountPoint> */
|
2022-02-10 13:15:07 +00:00
|
|
|
private CappedMemoryCache $pathCache;
|
2022-03-02 18:24:26 +00:00
|
|
|
/** @var CappedMemoryCache<IMountPoint[]> */
|
2022-02-10 13:15:07 +00:00
|
|
|
private CappedMemoryCache $inPathCache;
|
2022-02-10 16:36:46 +00:00
|
|
|
private SetupManager $setupManager;
|
2018-08-16 17:02:00 +00:00
|
|
|
|
2022-03-02 18:38:21 +00:00
|
|
|
public function __construct(SetupManagerFactory $setupManagerFactory) {
|
2018-03-26 17:53:10 +00:00
|
|
|
$this->pathCache = new CappedMemoryCache();
|
2018-08-16 17:02:00 +00:00
|
|
|
$this->inPathCache = new CappedMemoryCache();
|
2022-03-02 18:38:21 +00:00
|
|
|
$this->setupManager = $setupManagerFactory->create($this);
|
2018-03-26 17:53:10 +00:00
|
|
|
}
|
|
|
|
|
2013-04-25 22:01:36 +00:00
|
|
|
/**
|
2015-07-01 14:13:33 +00:00
|
|
|
* @param IMountPoint $mount
|
2013-04-25 22:01:36 +00:00
|
|
|
*/
|
2015-07-01 14:13:33 +00:00
|
|
|
public function addMount(IMountPoint $mount) {
|
2013-04-25 22:01:36 +00:00
|
|
|
$this->mounts[$mount->getMountPoint()] = $mount;
|
2018-03-26 17:53:10 +00:00
|
|
|
$this->pathCache->clear();
|
2018-08-16 17:02:00 +00:00
|
|
|
$this->inPathCache->clear();
|
2013-04-25 22:01:36 +00:00
|
|
|
}
|
|
|
|
|
2014-04-07 17:00:03 +00:00
|
|
|
/**
|
|
|
|
* @param string $mountPoint
|
|
|
|
*/
|
2018-03-26 14:34:19 +00:00
|
|
|
public function removeMount(string $mountPoint) {
|
2014-07-02 13:00:12 +00:00
|
|
|
$mountPoint = Filesystem::normalizePath($mountPoint);
|
2018-03-26 14:35:47 +00:00
|
|
|
if (\strlen($mountPoint) > 1) {
|
2014-07-02 13:00:12 +00:00
|
|
|
$mountPoint .= '/';
|
|
|
|
}
|
2014-04-07 17:00:03 +00:00
|
|
|
unset($this->mounts[$mountPoint]);
|
2018-03-26 17:53:10 +00:00
|
|
|
$this->pathCache->clear();
|
2018-08-16 17:02:00 +00:00
|
|
|
$this->inPathCache->clear();
|
2014-04-07 17:00:03 +00:00
|
|
|
}
|
|
|
|
|
2014-05-21 23:39:24 +00:00
|
|
|
/**
|
|
|
|
* @param string $mountPoint
|
|
|
|
* @param string $target
|
|
|
|
*/
|
2018-08-16 17:02:00 +00:00
|
|
|
public function moveMount(string $mountPoint, string $target) {
|
2014-05-21 23:39:24 +00:00
|
|
|
$this->mounts[$target] = $this->mounts[$mountPoint];
|
|
|
|
unset($this->mounts[$mountPoint]);
|
2018-03-26 17:53:10 +00:00
|
|
|
$this->pathCache->clear();
|
2018-08-16 17:02:00 +00:00
|
|
|
$this->inPathCache->clear();
|
2014-05-21 23:39:24 +00:00
|
|
|
}
|
|
|
|
|
2013-04-25 22:01:36 +00:00
|
|
|
/**
|
|
|
|
* Find the mount for $path
|
|
|
|
*
|
2014-05-11 20:51:30 +00:00
|
|
|
* @param string $path
|
2022-03-02 18:24:26 +00:00
|
|
|
* @return IMountPoint
|
2013-04-25 22:01:36 +00:00
|
|
|
*/
|
2022-03-02 18:24:26 +00:00
|
|
|
public function find(string $path): IMountPoint {
|
2022-03-02 16:11:06 +00:00
|
|
|
$this->setupManager->setupForPath($path);
|
2018-08-16 16:53:32 +00:00
|
|
|
$path = Filesystem::normalizePath($path);
|
2013-04-25 22:01:36 +00:00
|
|
|
|
2018-03-26 17:53:10 +00:00
|
|
|
if (isset($this->pathCache[$path])) {
|
|
|
|
return $this->pathCache[$path];
|
|
|
|
}
|
|
|
|
|
2023-06-28 13:48:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
if (count($this->mounts) === 0) {
|
|
|
|
$this->setupManager->setupRoot();
|
|
|
|
if (count($this->mounts) === 0) {
|
|
|
|
throw new \Exception("No mounts even after explicitly setting up the root mounts");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-16 16:53:32 +00:00
|
|
|
$current = $path;
|
2018-08-16 17:02:00 +00:00
|
|
|
while (true) {
|
2018-08-16 16:53:32 +00:00
|
|
|
$mountPoint = $current . '/';
|
|
|
|
if (isset($this->mounts[$mountPoint])) {
|
|
|
|
$this->pathCache[$path] = $this->mounts[$mountPoint];
|
|
|
|
return $this->mounts[$mountPoint];
|
2022-03-03 16:15:02 +00:00
|
|
|
} elseif ($current === '') {
|
|
|
|
break;
|
2018-08-16 16:53:32 +00:00
|
|
|
}
|
2018-03-26 14:35:47 +00:00
|
|
|
|
2018-08-16 16:53:32 +00:00
|
|
|
$current = dirname($current);
|
|
|
|
if ($current === '.' || $current === '/') {
|
|
|
|
$current = '';
|
|
|
|
}
|
|
|
|
}
|
2022-03-03 16:15:02 +00:00
|
|
|
|
2023-06-28 13:48:55 +00:00
|
|
|
throw new NotFoundException("No mount for path " . $path . " existing mounts (" . count($this->mounts) ."): " . implode(",", array_keys($this->mounts)));
|
2013-04-25 22:01:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find all mounts in $path
|
|
|
|
*
|
2014-05-11 20:51:30 +00:00
|
|
|
* @param string $path
|
2022-03-02 18:24:26 +00:00
|
|
|
* @return IMountPoint[]
|
2013-04-25 22:01:36 +00:00
|
|
|
*/
|
2018-03-26 14:34:19 +00:00
|
|
|
public function findIn(string $path): array {
|
2022-03-08 16:14:15 +00:00
|
|
|
$this->setupManager->setupForPath($path, true);
|
2013-04-25 22:01:36 +00:00
|
|
|
$path = $this->formatPath($path);
|
2018-08-16 17:02:00 +00:00
|
|
|
|
|
|
|
if (isset($this->inPathCache[$path])) {
|
|
|
|
return $this->inPathCache[$path];
|
|
|
|
}
|
|
|
|
|
2018-03-26 14:34:19 +00:00
|
|
|
$result = [];
|
2018-03-26 14:35:47 +00:00
|
|
|
$pathLength = \strlen($path);
|
2013-04-25 22:01:36 +00:00
|
|
|
$mountPoints = array_keys($this->mounts);
|
|
|
|
foreach ($mountPoints as $mountPoint) {
|
2018-03-26 14:35:47 +00:00
|
|
|
if (substr($mountPoint, 0, $pathLength) === $path && \strlen($mountPoint) > $pathLength) {
|
2013-04-25 22:01:36 +00:00
|
|
|
$result[] = $this->mounts[$mountPoint];
|
|
|
|
}
|
|
|
|
}
|
2018-08-16 17:02:00 +00:00
|
|
|
|
|
|
|
$this->inPathCache[$path] = $result;
|
2013-04-25 22:01:36 +00:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function clear() {
|
2018-03-26 14:34:19 +00:00
|
|
|
$this->mounts = [];
|
2018-03-26 17:53:10 +00:00
|
|
|
$this->pathCache->clear();
|
2018-08-16 17:02:00 +00:00
|
|
|
$this->inPathCache->clear();
|
2013-04-25 22:01:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find mounts by storage id
|
|
|
|
*
|
|
|
|
* @param string $id
|
2022-03-02 18:24:26 +00:00
|
|
|
* @return IMountPoint[]
|
2013-04-25 22:01:36 +00:00
|
|
|
*/
|
2018-03-26 14:34:19 +00:00
|
|
|
public function findByStorageId(string $id): array {
|
2018-03-26 14:35:47 +00:00
|
|
|
if (\strlen($id) > 64) {
|
2013-04-25 22:01:36 +00:00
|
|
|
$id = md5($id);
|
|
|
|
}
|
2018-03-26 14:34:19 +00:00
|
|
|
$result = [];
|
2013-04-25 22:01:36 +00:00
|
|
|
foreach ($this->mounts as $mount) {
|
|
|
|
if ($mount->getStorageId() === $id) {
|
|
|
|
$result[] = $mount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2013-07-25 14:00:24 +00:00
|
|
|
/**
|
2022-03-02 18:24:26 +00:00
|
|
|
* @return IMountPoint[]
|
2013-07-25 14:00:24 +00:00
|
|
|
*/
|
2018-03-26 14:34:19 +00:00
|
|
|
public function getAll(): array {
|
2013-07-25 14:00:24 +00:00
|
|
|
return $this->mounts;
|
|
|
|
}
|
|
|
|
|
2013-04-25 22:01:36 +00:00
|
|
|
/**
|
|
|
|
* Find mounts by numeric storage id
|
|
|
|
*
|
2014-05-11 20:51:30 +00:00
|
|
|
* @param int $id
|
2022-03-02 18:24:26 +00:00
|
|
|
* @return IMountPoint[]
|
2013-04-25 22:01:36 +00:00
|
|
|
*/
|
2018-03-26 14:34:19 +00:00
|
|
|
public function findByNumericId(int $id): array {
|
2023-02-06 17:16:23 +00:00
|
|
|
$result = [];
|
|
|
|
foreach ($this->mounts as $mount) {
|
|
|
|
if ($mount->getNumericStorageId() === $id) {
|
|
|
|
$result[] = $mount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $result;
|
2013-04-25 22:01:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
* @return string
|
|
|
|
*/
|
2018-03-26 14:34:19 +00:00
|
|
|
private function formatPath(string $path): string {
|
2013-04-25 22:01:36 +00:00
|
|
|
$path = Filesystem::normalizePath($path);
|
2018-03-26 14:35:47 +00:00
|
|
|
if (\strlen($path) > 1) {
|
2013-04-25 22:01:36 +00:00
|
|
|
$path .= '/';
|
|
|
|
}
|
|
|
|
return $path;
|
|
|
|
}
|
2022-02-23 17:11:46 +00:00
|
|
|
|
|
|
|
public function getSetupManager(): SetupManager {
|
|
|
|
return $this->setupManager;
|
|
|
|
}
|
2022-03-28 16:45:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return all mounts in a path from a specific mount provider
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @param string[] $mountProviders
|
|
|
|
* @return MountPoint[]
|
|
|
|
*/
|
|
|
|
public function getMountsByMountProvider(string $path, array $mountProviders) {
|
|
|
|
$this->getSetupManager()->setupForProvider($path, $mountProviders);
|
|
|
|
if (in_array('', $mountProviders)) {
|
|
|
|
return $this->mounts;
|
|
|
|
} else {
|
|
|
|
return array_filter($this->mounts, function ($mount) use ($mountProviders) {
|
|
|
|
return in_array($mount->getMountProvider(), $mountProviders);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2023-09-25 16:25:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the mount matching a cached mount info (or mount file info)
|
|
|
|
*
|
|
|
|
* @param ICachedMountInfo $info
|
|
|
|
*
|
|
|
|
* @return IMountPoint|null
|
|
|
|
*/
|
|
|
|
public function getMountFromMountInfo(ICachedMountInfo $info): ?IMountPoint {
|
|
|
|
$this->setupManager->setupForPath($info->getMountPoint());
|
|
|
|
foreach ($this->mounts as $mount) {
|
|
|
|
if ($mount->getMountPoint() === $info->getMountPoint()) {
|
|
|
|
return $mount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2013-04-25 22:01:36 +00:00
|
|
|
}
|