2013-04-26 02:01:36 +04:00
|
|
|
<?php
|
2018-03-26 17:34:19 +03:00
|
|
|
declare(strict_types=1);
|
2013-04-26 02:01:36 +04:00
|
|
|
/**
|
2016-07-21 18:07:57 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Björn Schießle <bjoern@schiessle.org>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-07-21 19:13:36 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2016-01-12 17:02:16 +03:00
|
|
|
* @author Robin McCorkell <robin@mccorkell.me.uk>
|
2015-03-26 13:44:34 +03: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,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
2013-04-26 02:01:36 +04:00
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2013-04-26 02:01:36 +04:00
|
|
|
namespace OC\Files\Mount;
|
|
|
|
|
2018-03-26 20:53:10 +03:00
|
|
|
use OC\Cache\CappedMemoryCache;
|
|
|
|
use OC\Files\Filesystem;
|
2015-07-01 16:57:04 +03:00
|
|
|
use OCP\Files\Mount\IMountManager;
|
2015-07-01 17:13:33 +03:00
|
|
|
use OCP\Files\Mount\IMountPoint;
|
2013-04-26 02:01:36 +04:00
|
|
|
|
2015-07-01 16:57:04 +03:00
|
|
|
class Manager implements IMountManager {
|
2018-03-26 20:53:10 +03:00
|
|
|
/** @var MountPoint[] */
|
2018-03-26 17:34:19 +03:00
|
|
|
private $mounts = [];
|
2013-04-26 02:01:36 +04:00
|
|
|
|
2018-03-26 20:53:10 +03:00
|
|
|
/** @var CappedMemoryCache */
|
|
|
|
private $pathCache;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->pathCache = new CappedMemoryCache();
|
|
|
|
}
|
|
|
|
|
2013-04-26 02:01:36 +04:00
|
|
|
/**
|
2015-07-01 17:13:33 +03:00
|
|
|
* @param IMountPoint $mount
|
2013-04-26 02:01:36 +04:00
|
|
|
*/
|
2015-07-01 17:13:33 +03:00
|
|
|
public function addMount(IMountPoint $mount) {
|
2013-04-26 02:01:36 +04:00
|
|
|
$this->mounts[$mount->getMountPoint()] = $mount;
|
2018-03-26 20:53:10 +03:00
|
|
|
$this->pathCache->clear();
|
2013-04-26 02:01:36 +04:00
|
|
|
}
|
|
|
|
|
2014-04-07 21:00:03 +04:00
|
|
|
/**
|
|
|
|
* @param string $mountPoint
|
|
|
|
*/
|
2018-03-26 17:34:19 +03:00
|
|
|
public function removeMount(string $mountPoint) {
|
2014-07-02 17:00:12 +04:00
|
|
|
$mountPoint = Filesystem::normalizePath($mountPoint);
|
2018-03-26 17:35:47 +03:00
|
|
|
if (\strlen($mountPoint) > 1) {
|
2014-07-02 17:00:12 +04:00
|
|
|
$mountPoint .= '/';
|
|
|
|
}
|
2014-04-07 21:00:03 +04:00
|
|
|
unset($this->mounts[$mountPoint]);
|
2018-03-26 20:53:10 +03:00
|
|
|
$this->pathCache->clear();
|
2014-04-07 21:00:03 +04:00
|
|
|
}
|
|
|
|
|
2014-05-22 03:39:24 +04:00
|
|
|
/**
|
|
|
|
* @param string $mountPoint
|
|
|
|
* @param string $target
|
|
|
|
*/
|
2018-03-26 17:34:19 +03:00
|
|
|
public function moveMount(string $mountPoint, string $target){
|
2014-05-22 03:39:24 +04:00
|
|
|
$this->mounts[$target] = $this->mounts[$mountPoint];
|
|
|
|
unset($this->mounts[$mountPoint]);
|
2018-03-26 20:53:10 +03:00
|
|
|
$this->pathCache->clear();
|
2014-05-22 03:39:24 +04:00
|
|
|
}
|
|
|
|
|
2013-04-26 02:01:36 +04:00
|
|
|
/**
|
|
|
|
* Find the mount for $path
|
|
|
|
*
|
2014-05-12 00:51:30 +04:00
|
|
|
* @param string $path
|
2018-04-04 13:50:54 +03:00
|
|
|
* @return MountPoint|null
|
2013-04-26 02:01:36 +04:00
|
|
|
*/
|
2018-04-04 13:50:54 +03:00
|
|
|
public function find(string $path) {
|
2013-04-26 02:01:36 +04:00
|
|
|
\OC_Util::setupFS();
|
|
|
|
$path = $this->formatPath($path);
|
|
|
|
if (isset($this->mounts[$path])) {
|
|
|
|
return $this->mounts[$path];
|
|
|
|
}
|
|
|
|
|
2018-03-26 20:53:10 +03:00
|
|
|
if (isset($this->pathCache[$path])) {
|
|
|
|
return $this->pathCache[$path];
|
|
|
|
}
|
|
|
|
|
2018-03-26 17:34:19 +03:00
|
|
|
\OC_Hook::emit('OC_Filesystem', 'get_mountpoint', ['path' => $path]);
|
2013-04-26 02:01:36 +04:00
|
|
|
$foundMountPoint = '';
|
|
|
|
$mountPoints = array_keys($this->mounts);
|
2018-03-26 17:47:46 +03:00
|
|
|
$foundMountPointLength = 0;
|
2013-04-26 02:01:36 +04:00
|
|
|
foreach ($mountPoints as $mountpoint) {
|
2018-03-26 17:48:20 +03:00
|
|
|
if (\strlen($mountpoint) > $foundMountPointLength && strpos($path, $mountpoint) === 0) {
|
2013-04-26 02:01:36 +04:00
|
|
|
$foundMountPoint = $mountpoint;
|
2018-03-26 17:47:46 +03:00
|
|
|
$foundMountPointLength = \strlen($foundMountPoint);
|
2013-04-26 02:01:36 +04:00
|
|
|
}
|
|
|
|
}
|
2018-03-26 17:35:47 +03:00
|
|
|
|
2013-04-26 02:01:36 +04:00
|
|
|
if (isset($this->mounts[$foundMountPoint])) {
|
2018-03-26 20:53:10 +03:00
|
|
|
$this->pathCache[$path] = $this->mounts[$foundMountPoint];
|
2013-04-26 02:01:36 +04:00
|
|
|
return $this->mounts[$foundMountPoint];
|
|
|
|
}
|
2018-03-26 17:35:47 +03:00
|
|
|
|
|
|
|
return null;
|
2013-04-26 02:01:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find all mounts in $path
|
|
|
|
*
|
2014-05-12 00:51:30 +04:00
|
|
|
* @param string $path
|
2014-11-24 17:54:42 +03:00
|
|
|
* @return MountPoint[]
|
2013-04-26 02:01:36 +04:00
|
|
|
*/
|
2018-03-26 17:34:19 +03:00
|
|
|
public function findIn(string $path): array {
|
2013-04-26 02:01:36 +04:00
|
|
|
\OC_Util::setupFS();
|
|
|
|
$path = $this->formatPath($path);
|
2018-03-26 17:34:19 +03:00
|
|
|
$result = [];
|
2018-03-26 17:35:47 +03:00
|
|
|
$pathLength = \strlen($path);
|
2013-04-26 02:01:36 +04:00
|
|
|
$mountPoints = array_keys($this->mounts);
|
|
|
|
foreach ($mountPoints as $mountPoint) {
|
2018-03-26 17:35:47 +03:00
|
|
|
if (substr($mountPoint, 0, $pathLength) === $path && \strlen($mountPoint) > $pathLength) {
|
2013-04-26 02:01:36 +04:00
|
|
|
$result[] = $this->mounts[$mountPoint];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function clear() {
|
2018-03-26 17:34:19 +03:00
|
|
|
$this->mounts = [];
|
2018-03-26 20:53:10 +03:00
|
|
|
$this->pathCache->clear();
|
2013-04-26 02:01:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find mounts by storage id
|
|
|
|
*
|
|
|
|
* @param string $id
|
2014-11-24 17:54:42 +03:00
|
|
|
* @return MountPoint[]
|
2013-04-26 02:01:36 +04:00
|
|
|
*/
|
2018-03-26 17:34:19 +03:00
|
|
|
public function findByStorageId(string $id): array {
|
2013-04-26 02:01:36 +04:00
|
|
|
\OC_Util::setupFS();
|
2018-03-26 17:35:47 +03:00
|
|
|
if (\strlen($id) > 64) {
|
2013-04-26 02:01:36 +04:00
|
|
|
$id = md5($id);
|
|
|
|
}
|
2018-03-26 17:34:19 +03:00
|
|
|
$result = [];
|
2013-04-26 02:01:36 +04:00
|
|
|
foreach ($this->mounts as $mount) {
|
|
|
|
if ($mount->getStorageId() === $id) {
|
|
|
|
$result[] = $mount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2013-07-25 18:00:24 +04:00
|
|
|
/**
|
2014-11-24 17:54:42 +03:00
|
|
|
* @return MountPoint[]
|
2013-07-25 18:00:24 +04:00
|
|
|
*/
|
2018-03-26 17:34:19 +03:00
|
|
|
public function getAll(): array {
|
2013-07-25 18:00:24 +04:00
|
|
|
return $this->mounts;
|
|
|
|
}
|
|
|
|
|
2013-04-26 02:01:36 +04:00
|
|
|
/**
|
|
|
|
* Find mounts by numeric storage id
|
|
|
|
*
|
2014-05-12 00:51:30 +04:00
|
|
|
* @param int $id
|
2014-11-24 17:54:42 +03:00
|
|
|
* @return MountPoint[]
|
2013-04-26 02:01:36 +04:00
|
|
|
*/
|
2018-03-26 17:34:19 +03:00
|
|
|
public function findByNumericId(int $id): array {
|
2013-04-26 02:01:36 +04:00
|
|
|
$storageId = \OC\Files\Cache\Storage::getStorageId($id);
|
|
|
|
return $this->findByStorageId($storageId);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
* @return string
|
|
|
|
*/
|
2018-03-26 17:34:19 +03:00
|
|
|
private function formatPath(string $path): string {
|
2013-04-26 02:01:36 +04:00
|
|
|
$path = Filesystem::normalizePath($path);
|
2018-03-26 17:35:47 +03:00
|
|
|
if (\strlen($path) > 1) {
|
2013-04-26 02:01:36 +04:00
|
|
|
$path .= '/';
|
|
|
|
}
|
|
|
|
return $path;
|
|
|
|
}
|
|
|
|
}
|