2013-04-25 22:01:36 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2015-02-23 10:28:53 +00:00
|
|
|
* @author Bjoern Schiessle <schiessle@owncloud.com>
|
|
|
|
* @author Robin Appelman <icewind@owncloud.com>
|
|
|
|
* @author Robin McCorkell <rmccorkell@karoshi.org.uk>
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc.
|
|
|
|
* @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-25 22:01:36 +00:00
|
|
|
*/
|
|
|
|
namespace OC\Files\Mount;
|
|
|
|
|
|
|
|
use \OC\Files\Filesystem;
|
|
|
|
|
|
|
|
class Manager {
|
|
|
|
/**
|
2014-11-24 14:54:42 +00:00
|
|
|
* @var MountPoint[]
|
2013-04-25 22:01:36 +00:00
|
|
|
*/
|
|
|
|
private $mounts = array();
|
|
|
|
|
|
|
|
/**
|
2014-11-24 14:54:42 +00:00
|
|
|
* @param MountPoint $mount
|
2013-04-25 22:01:36 +00:00
|
|
|
*/
|
2014-11-24 14:54:42 +00:00
|
|
|
public function addMount(MountPoint $mount) {
|
2013-04-25 22:01:36 +00:00
|
|
|
$this->mounts[$mount->getMountPoint()] = $mount;
|
|
|
|
}
|
|
|
|
|
2014-04-07 17:00:03 +00:00
|
|
|
/**
|
|
|
|
* @param string $mountPoint
|
|
|
|
*/
|
|
|
|
public function removeMount($mountPoint) {
|
2014-07-02 13:00:12 +00:00
|
|
|
$mountPoint = Filesystem::normalizePath($mountPoint);
|
|
|
|
if (strlen($mountPoint) > 1) {
|
|
|
|
$mountPoint .= '/';
|
|
|
|
}
|
2014-04-07 17:00:03 +00:00
|
|
|
unset($this->mounts[$mountPoint]);
|
|
|
|
}
|
|
|
|
|
2014-05-21 23:39:24 +00:00
|
|
|
/**
|
|
|
|
* @param string $mountPoint
|
|
|
|
* @param string $target
|
|
|
|
*/
|
|
|
|
public function moveMount($mountPoint, $target){
|
|
|
|
$this->mounts[$target] = $this->mounts[$mountPoint];
|
|
|
|
unset($this->mounts[$mountPoint]);
|
|
|
|
}
|
|
|
|
|
2013-04-25 22:01:36 +00:00
|
|
|
/**
|
|
|
|
* Find the mount for $path
|
|
|
|
*
|
2014-05-11 20:51:30 +00:00
|
|
|
* @param string $path
|
2014-11-24 14:54:42 +00:00
|
|
|
* @return MountPoint
|
2013-04-25 22:01:36 +00:00
|
|
|
*/
|
|
|
|
public function find($path) {
|
|
|
|
\OC_Util::setupFS();
|
|
|
|
$path = $this->formatPath($path);
|
|
|
|
if (isset($this->mounts[$path])) {
|
|
|
|
return $this->mounts[$path];
|
|
|
|
}
|
|
|
|
|
|
|
|
\OC_Hook::emit('OC_Filesystem', 'get_mountpoint', array('path' => $path));
|
|
|
|
$foundMountPoint = '';
|
|
|
|
$mountPoints = array_keys($this->mounts);
|
|
|
|
foreach ($mountPoints as $mountpoint) {
|
|
|
|
if (strpos($path, $mountpoint) === 0 and strlen($mountpoint) > strlen($foundMountPoint)) {
|
|
|
|
$foundMountPoint = $mountpoint;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (isset($this->mounts[$foundMountPoint])) {
|
|
|
|
return $this->mounts[$foundMountPoint];
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find all mounts in $path
|
|
|
|
*
|
2014-05-11 20:51:30 +00:00
|
|
|
* @param string $path
|
2014-11-24 14:54:42 +00:00
|
|
|
* @return MountPoint[]
|
2013-04-25 22:01:36 +00:00
|
|
|
*/
|
|
|
|
public function findIn($path) {
|
|
|
|
\OC_Util::setupFS();
|
|
|
|
$path = $this->formatPath($path);
|
|
|
|
$result = array();
|
|
|
|
$pathLength = strlen($path);
|
|
|
|
$mountPoints = array_keys($this->mounts);
|
|
|
|
foreach ($mountPoints as $mountPoint) {
|
|
|
|
if (substr($mountPoint, 0, $pathLength) === $path and strlen($mountPoint) > $pathLength) {
|
|
|
|
$result[] = $this->mounts[$mountPoint];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function clear() {
|
|
|
|
$this->mounts = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find mounts by storage id
|
|
|
|
*
|
|
|
|
* @param string $id
|
2014-11-24 14:54:42 +00:00
|
|
|
* @return MountPoint[]
|
2013-04-25 22:01:36 +00:00
|
|
|
*/
|
|
|
|
public function findByStorageId($id) {
|
|
|
|
\OC_Util::setupFS();
|
|
|
|
if (strlen($id) > 64) {
|
|
|
|
$id = md5($id);
|
|
|
|
}
|
|
|
|
$result = array();
|
|
|
|
foreach ($this->mounts as $mount) {
|
|
|
|
if ($mount->getStorageId() === $id) {
|
|
|
|
$result[] = $mount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2013-07-25 14:00:24 +00:00
|
|
|
/**
|
2014-11-24 14:54:42 +00:00
|
|
|
* @return MountPoint[]
|
2013-07-25 14:00:24 +00:00
|
|
|
*/
|
|
|
|
public function getAll() {
|
|
|
|
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
|
2014-11-24 14:54:42 +00:00
|
|
|
* @return MountPoint[]
|
2013-04-25 22:01:36 +00:00
|
|
|
*/
|
|
|
|
public function findByNumericId($id) {
|
|
|
|
$storageId = \OC\Files\Cache\Storage::getStorageId($id);
|
|
|
|
return $this->findByStorageId($storageId);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function formatPath($path) {
|
|
|
|
$path = Filesystem::normalizePath($path);
|
|
|
|
if (strlen($path) > 1) {
|
|
|
|
$path .= '/';
|
|
|
|
}
|
|
|
|
return $path;
|
|
|
|
}
|
|
|
|
}
|