nextcloud/lib/private/Files/Mount/Manager.php

186 lines
4.4 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
/**
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/>
*
*/
namespace OC\Files\Mount;
use OC\Cache\CappedMemoryCache;
use OC\Files\Filesystem;
use OCP\Files\Mount\IMountManager;
2015-07-01 17:13:33 +03:00
use OCP\Files\Mount\IMountPoint;
class Manager implements IMountManager {
/** @var MountPoint[] */
private $mounts = [];
/** @var CappedMemoryCache */
private $pathCache;
public function __construct() {
$this->pathCache = new CappedMemoryCache();
}
/**
2015-07-01 17:13:33 +03:00
* @param IMountPoint $mount
*/
2015-07-01 17:13:33 +03:00
public function addMount(IMountPoint $mount) {
$this->mounts[$mount->getMountPoint()] = $mount;
$this->pathCache->clear();
}
/**
* @param string $mountPoint
*/
public function removeMount(string $mountPoint) {
$mountPoint = Filesystem::normalizePath($mountPoint);
if (\strlen($mountPoint) > 1) {
$mountPoint .= '/';
}
unset($this->mounts[$mountPoint]);
$this->pathCache->clear();
}
/**
* @param string $mountPoint
* @param string $target
*/
public function moveMount(string $mountPoint, string $target){
$this->mounts[$target] = $this->mounts[$mountPoint];
unset($this->mounts[$mountPoint]);
$this->pathCache->clear();
}
/**
* Find the mount for $path
*
* @param string $path
* @return MountPoint
*/
public function find(string $path): IMountPoint {
\OC_Util::setupFS();
$path = $this->formatPath($path);
if (isset($this->mounts[$path])) {
return $this->mounts[$path];
}
if (isset($this->pathCache[$path])) {
return $this->pathCache[$path];
}
\OC_Hook::emit('OC_Filesystem', 'get_mountpoint', ['path' => $path]);
$foundMountPoint = '';
$mountPoints = array_keys($this->mounts);
$foundMountPointLength = 0;
foreach ($mountPoints as $mountpoint) {
if (\strlen($mountpoint) > $foundMountPointLength && strpos($path, $mountpoint) === 0) {
$foundMountPoint = $mountpoint;
$foundMountPointLength = \strlen($foundMountPoint);
}
}
if (isset($this->mounts[$foundMountPoint])) {
$this->pathCache[$path] = $this->mounts[$foundMountPoint];
return $this->mounts[$foundMountPoint];
}
return null;
}
/**
* Find all mounts in $path
*
* @param string $path
* @return MountPoint[]
*/
public function findIn(string $path): array {
\OC_Util::setupFS();
$path = $this->formatPath($path);
$result = [];
$pathLength = \strlen($path);
$mountPoints = array_keys($this->mounts);
foreach ($mountPoints as $mountPoint) {
if (substr($mountPoint, 0, $pathLength) === $path && \strlen($mountPoint) > $pathLength) {
$result[] = $this->mounts[$mountPoint];
}
}
return $result;
}
public function clear() {
$this->mounts = [];
$this->pathCache->clear();
}
/**
* Find mounts by storage id
*
* @param string $id
* @return MountPoint[]
*/
public function findByStorageId(string $id): array {
\OC_Util::setupFS();
if (\strlen($id) > 64) {
$id = md5($id);
}
$result = [];
foreach ($this->mounts as $mount) {
if ($mount->getStorageId() === $id) {
$result[] = $mount;
}
}
return $result;
}
2013-07-25 18:00:24 +04:00
/**
* @return MountPoint[]
2013-07-25 18:00:24 +04:00
*/
public function getAll(): array {
2013-07-25 18:00:24 +04:00
return $this->mounts;
}
/**
* Find mounts by numeric storage id
*
* @param int $id
* @return MountPoint[]
*/
public function findByNumericId(int $id): array {
$storageId = \OC\Files\Cache\Storage::getStorageId($id);
return $this->findByStorageId($storageId);
}
/**
* @param string $path
* @return string
*/
private function formatPath(string $path): string {
$path = Filesystem::normalizePath($path);
if (\strlen($path) > 1) {
$path .= '/';
}
return $path;
}
}