2013-01-27 00:42:59 +04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
2013-04-26 02:01:36 +04:00
|
|
|
namespace OC\Files\Mount;
|
|
|
|
|
|
|
|
use \OC\Files\Filesystem;
|
2013-06-07 19:07:13 +04:00
|
|
|
use OC\Files\Storage\Loader;
|
|
|
|
use OC\Files\Storage\Storage;
|
2013-01-27 00:42:59 +04:00
|
|
|
|
|
|
|
class Mount {
|
|
|
|
/**
|
|
|
|
* @var \OC\Files\Storage\Storage $storage
|
|
|
|
*/
|
2014-05-22 03:39:24 +04:00
|
|
|
protected $storage = null;
|
|
|
|
protected $class;
|
|
|
|
protected $storageId;
|
|
|
|
protected $arguments = array();
|
|
|
|
protected $mountPoint;
|
2013-01-27 00:42:59 +04:00
|
|
|
|
2013-05-09 00:35:10 +04:00
|
|
|
/**
|
2013-06-07 19:07:13 +04:00
|
|
|
* @var \OC\Files\Storage\Loader $loader
|
2013-05-09 00:35:10 +04:00
|
|
|
*/
|
2013-06-07 19:07:13 +04:00
|
|
|
private $loader;
|
2013-05-09 00:35:10 +04:00
|
|
|
|
2013-01-27 00:42:59 +04:00
|
|
|
/**
|
2014-05-11 21:28:45 +04:00
|
|
|
* @param string|\OC\Files\Storage\Storage $storage
|
2013-01-27 00:42:59 +04:00
|
|
|
* @param string $mountpoint
|
2013-06-07 19:07:13 +04:00
|
|
|
* @param array $arguments (optional)\
|
|
|
|
* @param \OC\Files\Storage\Loader $loader
|
2013-01-27 00:42:59 +04:00
|
|
|
*/
|
2013-06-07 19:07:13 +04:00
|
|
|
public function __construct($storage, $mountpoint, $arguments = null, $loader = null) {
|
2013-01-27 00:42:59 +04:00
|
|
|
if (is_null($arguments)) {
|
|
|
|
$arguments = array();
|
|
|
|
}
|
2013-06-07 19:07:13 +04:00
|
|
|
if (is_null($loader)) {
|
|
|
|
$this->loader = new Loader();
|
|
|
|
} else {
|
|
|
|
$this->loader = $loader;
|
|
|
|
}
|
2013-01-27 00:42:59 +04:00
|
|
|
|
2013-04-26 02:01:36 +04:00
|
|
|
$mountpoint = $this->formatPath($mountpoint);
|
2013-06-07 19:07:13 +04:00
|
|
|
if ($storage instanceof Storage) {
|
2013-01-27 00:42:59 +04:00
|
|
|
$this->class = get_class($storage);
|
2013-06-07 19:07:13 +04:00
|
|
|
$this->storage = $this->loader->wrap($mountpoint, $storage);
|
2013-01-27 00:42:59 +04:00
|
|
|
} else {
|
|
|
|
// Update old classes to new namespace
|
|
|
|
if (strpos($storage, 'OC_Filestorage_') !== false) {
|
|
|
|
$storage = '\OC\Files\Storage\\' . substr($storage, 15);
|
|
|
|
}
|
|
|
|
$this->class = $storage;
|
|
|
|
$this->arguments = $arguments;
|
|
|
|
}
|
|
|
|
$this->mountPoint = $mountpoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-04-30 18:56:09 +04:00
|
|
|
* get complete path to the mount point, relative to data/
|
|
|
|
*
|
2013-01-27 00:42:59 +04:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getMountPoint() {
|
|
|
|
return $this->mountPoint;
|
|
|
|
}
|
|
|
|
|
2014-04-30 18:56:09 +04:00
|
|
|
/**
|
|
|
|
* get name of the mount point
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getMountPointName() {
|
|
|
|
return basename(rtrim($this->mountPoint, '/'));
|
|
|
|
}
|
|
|
|
|
2014-04-07 21:00:03 +04:00
|
|
|
/**
|
|
|
|
* @param string $mountPoint new mount point
|
|
|
|
*/
|
|
|
|
public function setMountPoint($mountPoint) {
|
|
|
|
$this->mountPoint = $mountPoint;
|
|
|
|
}
|
|
|
|
|
2013-01-27 00:42:59 +04:00
|
|
|
/**
|
2013-04-26 02:01:36 +04:00
|
|
|
* create the storage that is mounted
|
|
|
|
*
|
2013-01-27 00:42:59 +04:00
|
|
|
* @return \OC\Files\Storage\Storage
|
|
|
|
*/
|
|
|
|
private function createStorage() {
|
|
|
|
if (class_exists($this->class)) {
|
|
|
|
try {
|
2013-06-07 19:07:13 +04:00
|
|
|
return $this->loader->load($this->mountPoint, $this->class, $this->arguments);
|
2013-01-27 00:42:59 +04:00
|
|
|
} catch (\Exception $exception) {
|
2014-06-12 00: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 {
|
|
|
|
\OC_Log::write('core', $exception->getMessage(), \OC_Log::ERROR);
|
|
|
|
}
|
2013-01-27 00:42:59 +04:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
\OC_Log::write('core', 'storage backend ' . $this->class . ' not found', \OC_Log::ERROR);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return \OC\Files\Storage\Storage
|
|
|
|
*/
|
|
|
|
public function getStorage() {
|
|
|
|
if (is_null($this->storage)) {
|
|
|
|
$this->storage = $this->createStorage();
|
|
|
|
}
|
|
|
|
return $this->storage;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getStorageId() {
|
|
|
|
if (!$this->storageId) {
|
|
|
|
if (is_null($this->storage)) {
|
2013-03-19 18:02:29 +04:00
|
|
|
$storage = $this->createStorage(); //FIXME: start using exceptions
|
|
|
|
if (is_null($storage)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$this->storage = $storage;
|
2013-01-27 00:42:59 +04:00
|
|
|
}
|
|
|
|
$this->storageId = $this->storage->getId();
|
2013-02-16 00:49:40 +04:00
|
|
|
if (strlen($this->storageId) > 64) {
|
|
|
|
$this->storageId = md5($this->storageId);
|
|
|
|
}
|
2013-01-27 00:42:59 +04:00
|
|
|
}
|
|
|
|
return $this->storageId;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getInternalPath($path) {
|
|
|
|
if ($this->mountPoint === $path or $this->mountPoint . '/' === $path) {
|
|
|
|
$internalPath = '';
|
|
|
|
} else {
|
|
|
|
$internalPath = substr($path, strlen($this->mountPoint));
|
|
|
|
}
|
2014-05-28 15:47:50 +04:00
|
|
|
// substr returns false instead of an empty string, we always want a string
|
2014-05-22 03:39:24 +04:00
|
|
|
return (string)$internalPath;
|
2013-01-27 00:42:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
* @return string
|
|
|
|
*/
|
2013-04-26 02:01:36 +04:00
|
|
|
private function formatPath($path) {
|
2013-01-27 00:42:59 +04:00
|
|
|
$path = Filesystem::normalizePath($path);
|
|
|
|
if (strlen($path) > 1) {
|
|
|
|
$path .= '/';
|
|
|
|
}
|
|
|
|
return $path;
|
|
|
|
}
|
2013-07-25 18:00:24 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param callable $wrapper
|
|
|
|
*/
|
|
|
|
public function wrapStorage($wrapper) {
|
2014-05-29 16:15:48 +04:00
|
|
|
$this->storage = $wrapper($this->mountPoint, $this->getStorage());
|
2013-07-25 18:00:24 +04:00
|
|
|
}
|
2013-01-27 00:42:59 +04:00
|
|
|
}
|