2013-01-26 20:42:59 +00: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-25 22:01:36 +00:00
|
|
|
namespace OC\Files\Mount;
|
|
|
|
|
|
|
|
use \OC\Files\Filesystem;
|
2013-06-07 15:07:13 +00:00
|
|
|
use OC\Files\Storage\Loader;
|
|
|
|
use OC\Files\Storage\Storage;
|
2013-01-26 20:42:59 +00:00
|
|
|
|
|
|
|
class Mount {
|
|
|
|
/**
|
|
|
|
* @var \OC\Files\Storage\Storage $storage
|
|
|
|
*/
|
2014-05-21 23:39:24 +00:00
|
|
|
protected $storage = null;
|
|
|
|
protected $class;
|
|
|
|
protected $storageId;
|
|
|
|
protected $arguments = array();
|
|
|
|
protected $mountPoint;
|
2013-01-26 20:42:59 +00:00
|
|
|
|
2013-05-08 20:35:10 +00:00
|
|
|
/**
|
2013-06-07 15:07:13 +00:00
|
|
|
* @var \OC\Files\Storage\Loader $loader
|
2013-05-08 20:35:10 +00:00
|
|
|
*/
|
2013-06-07 15:07:13 +00:00
|
|
|
private $loader;
|
2013-05-08 20:35:10 +00:00
|
|
|
|
2013-01-26 20:42:59 +00:00
|
|
|
/**
|
2014-05-11 17:28:45 +00:00
|
|
|
* @param string|\OC\Files\Storage\Storage $storage
|
2013-01-26 20:42:59 +00:00
|
|
|
* @param string $mountpoint
|
2013-06-07 15:07:13 +00:00
|
|
|
* @param array $arguments (optional)\
|
|
|
|
* @param \OC\Files\Storage\Loader $loader
|
2013-01-26 20:42:59 +00:00
|
|
|
*/
|
2013-06-07 15:07:13 +00:00
|
|
|
public function __construct($storage, $mountpoint, $arguments = null, $loader = null) {
|
2013-01-26 20:42:59 +00:00
|
|
|
if (is_null($arguments)) {
|
|
|
|
$arguments = array();
|
|
|
|
}
|
2013-06-07 15:07:13 +00:00
|
|
|
if (is_null($loader)) {
|
|
|
|
$this->loader = new Loader();
|
|
|
|
} else {
|
|
|
|
$this->loader = $loader;
|
|
|
|
}
|
2013-01-26 20:42:59 +00:00
|
|
|
|
2013-04-25 22:01:36 +00:00
|
|
|
$mountpoint = $this->formatPath($mountpoint);
|
2013-06-07 15:07:13 +00:00
|
|
|
if ($storage instanceof Storage) {
|
2013-01-26 20:42:59 +00:00
|
|
|
$this->class = get_class($storage);
|
2013-06-07 15:07:13 +00:00
|
|
|
$this->storage = $this->loader->wrap($mountpoint, $storage);
|
2013-01-26 20:42:59 +00: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 14:56:09 +00:00
|
|
|
* get complete path to the mount point, relative to data/
|
|
|
|
*
|
2013-01-26 20:42:59 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getMountPoint() {
|
|
|
|
return $this->mountPoint;
|
|
|
|
}
|
|
|
|
|
2014-04-30 14:56:09 +00:00
|
|
|
/**
|
|
|
|
* get name of the mount point
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getMountPointName() {
|
|
|
|
return basename(rtrim($this->mountPoint, '/'));
|
|
|
|
}
|
|
|
|
|
2014-04-07 17:00:03 +00:00
|
|
|
/**
|
|
|
|
* @param string $mountPoint new mount point
|
|
|
|
*/
|
|
|
|
public function setMountPoint($mountPoint) {
|
|
|
|
$this->mountPoint = $mountPoint;
|
|
|
|
}
|
|
|
|
|
2013-01-26 20:42:59 +00:00
|
|
|
/**
|
2013-04-25 22:01:36 +00:00
|
|
|
* create the storage that is mounted
|
|
|
|
*
|
2013-01-26 20:42:59 +00:00
|
|
|
* @return \OC\Files\Storage\Storage
|
|
|
|
*/
|
|
|
|
private function createStorage() {
|
|
|
|
if (class_exists($this->class)) {
|
|
|
|
try {
|
2013-06-07 15:07:13 +00:00
|
|
|
return $this->loader->load($this->mountPoint, $this->class, $this->arguments);
|
2013-01-26 20:42:59 +00:00
|
|
|
} catch (\Exception $exception) {
|
2014-06-11 20:13:27 +00: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-26 20:42:59 +00: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 14:02:29 +00:00
|
|
|
$storage = $this->createStorage(); //FIXME: start using exceptions
|
|
|
|
if (is_null($storage)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$this->storage = $storage;
|
2013-01-26 20:42:59 +00:00
|
|
|
}
|
|
|
|
$this->storageId = $this->storage->getId();
|
2013-02-15 20:49:40 +00:00
|
|
|
if (strlen($this->storageId) > 64) {
|
|
|
|
$this->storageId = md5($this->storageId);
|
|
|
|
}
|
2013-01-26 20:42:59 +00: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 11:47:50 +00:00
|
|
|
// substr returns false instead of an empty string, we always want a string
|
2014-05-21 23:39:24 +00:00
|
|
|
return (string)$internalPath;
|
2013-01-26 20:42:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
* @return string
|
|
|
|
*/
|
2013-04-25 22:01:36 +00:00
|
|
|
private function formatPath($path) {
|
2013-01-26 20:42:59 +00:00
|
|
|
$path = Filesystem::normalizePath($path);
|
|
|
|
if (strlen($path) > 1) {
|
|
|
|
$path .= '/';
|
|
|
|
}
|
|
|
|
return $path;
|
|
|
|
}
|
2013-07-25 14:00:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param callable $wrapper
|
|
|
|
*/
|
|
|
|
public function wrapStorage($wrapper) {
|
2014-05-29 12:15:48 +00:00
|
|
|
$this->storage = $wrapper($this->mountPoint, $this->getStorage());
|
2013-07-25 14:00:24 +00:00
|
|
|
}
|
2013-01-26 20:42:59 +00:00
|
|
|
}
|