diff options
author | Robin Appelman <icewind@owncloud.com> | 2013-01-26 21:42:59 +0100 |
---|---|---|
committer | Robin Appelman <icewind@owncloud.com> | 2013-01-26 21:44:33 +0100 |
commit | f25db779d90814f1d54d594caf1bb830ea417b95 (patch) | |
tree | e69dcb5bfb214a1379017f6b1177a0eec82395eb | |
parent | 177537dbccac7c30ab42dfee56e92974e5cdf042 (diff) | |
download | nextcloud-server-f25db779d90814f1d54d594caf1bb830ea417b95.tar.gz nextcloud-server-f25db779d90814f1d54d594caf1bb830ea417b95.zip |
Filesystem: move mount management to it's own class
-rw-r--r-- | lib/files/filesystem.php | 99 | ||||
-rw-r--r-- | lib/files/mount.php | 171 |
2 files changed, 191 insertions, 79 deletions
diff --git a/lib/files/filesystem.php b/lib/files/filesystem.php index 9a3d13e343b..048116a44bb 100644 --- a/lib/files/filesystem.php +++ b/lib/files/filesystem.php @@ -30,8 +30,6 @@ namespace OC\Files; class Filesystem { - static private $storages = array(); - static private $mounts = array(); public static $loaded = false; /** * @var \OC\Files\View $defaultInstance @@ -143,19 +141,12 @@ class Filesystem { * @return string */ static public function getMountPoint($path) { - $path = self::normalizePath($path) . '/'; - \OC_Hook::emit(self::CLASSNAME, 'get_mountpoint', array('path' => $path)); - $foundMountPoint = ''; - $mountPoints = array_keys(self::$mounts); - foreach ($mountPoints as $mountpoint) { - if ($mountpoint == $path) { - return $mountpoint; - } - if (strpos($path, $mountpoint) === 0 and strlen($mountpoint) > strlen($foundMountPoint)) { - $foundMountPoint = $mountpoint; - } + $mount = Mount::find($path); + if ($mount) { + return $mount->getMountPoint(); + } else { + return ''; } - return $foundMountPoint; } /** @@ -165,18 +156,10 @@ class Filesystem { * @return string[] */ static public function getMountPoints($path) { - $path = self::normalizePath($path); - if (strlen($path) > 1) { - $path .= '/'; - } - $pathLength = strlen($path); - - $mountPoints = array_keys(self::$mounts); $result = array(); - foreach ($mountPoints as $mountPoint) { - if (substr($mountPoint, 0, $pathLength) === $path and strlen($mountPoint) > $pathLength) { - $result[] = $mountPoint; - } + $mounts = Mount::findIn($path); + foreach ($mounts as $mount) { + $result[] = $mount->getMountPoint(); } return $result; } @@ -188,11 +171,8 @@ class Filesystem { * @return \OC\Files\Storage\Storage */ public static function getStorage($mountPoint) { - if (!isset(self::$storages[$mountPoint])) { - $mount = self::$mounts[$mountPoint]; - self::$storages[$mountPoint] = self::createStorage($mount['class'], $mount['arguments']); - } - return self::$storages[$mountPoint]; + $mount = Mount::find($mountPoint); + return $mount->getStorage(); } /** @@ -202,15 +182,9 @@ class Filesystem { * @return array consisting of the storage and the internal path */ static public function resolvePath($path) { - $mountpoint = self::getMountPoint($path); - if ($mountpoint) { - $storage = self::getStorage($mountpoint); - if ($mountpoint === $path or $mountpoint . '/' === $path) { - $internalPath = ''; - } else { - $internalPath = substr($path, strlen($mountpoint)); - } - return array($storage, $internalPath); + $mount = Mount::find($path); + if ($mount) { + return array($mount->getStorage(), $mount->getInternalPath($path)); } else { return array(null, null); } @@ -291,6 +265,7 @@ class Filesystem { * fill in the correct values for $user, and $password placeholders * * @param string $input + * @param string $input * @return string */ private static function setUserVars($user, $input) { @@ -310,28 +285,7 @@ class Filesystem { * tear down the filesystem, removing all storage providers */ static public function tearDown() { - self::$storages = array(); - } - - /** - * create a new storage of a specific type - * - * @param string $type - * @param array $arguments - * @return \OC\Files\Storage\Storage - */ - static private function createStorage($class, $arguments) { - if (class_exists($class)) { - try { - return new $class($arguments); - } catch (\Exception $exception) { - \OC_Log::write('core', $exception->getMessage(), \OC_Log::ERROR); - return false; - } - } else { - \OC_Log::write('core', 'storage backend ' . $class . ' not found', \OC_Log::ERROR); - return false; - } + self::clearMounts(); } /** @@ -348,8 +302,7 @@ class Filesystem { * clear all mounts and storage backends */ public static function clearMounts() { - self::$mounts = array(); - self::$storages = array(); + Mount::clear(); } /** @@ -360,20 +313,7 @@ class Filesystem { * @param string $mountpoint */ static public function mount($class, $arguments, $mountpoint) { - $mountpoint = self::normalizePath($mountpoint); - if (strlen($mountpoint) > 1) { - $mountpoint .= '/'; - } - if ($class instanceof \OC\Files\Storage\Storage) { - self::$mounts[$mountpoint] = array('class' => get_class($class), 'arguments' => $arguments); - self::$storages[$mountpoint] = $class; - } else { - // Update old classes to new namespace - if (strpos($class, 'OC_Filestorage_') !== false) { - $class = '\OC\Files\Storage\\' . substr($class, 15); - } - self::$mounts[$mountpoint] = array('class' => $class, 'arguments' => $arguments); - } + new Mount($class, $mountpoint, $arguments); } /** @@ -578,6 +518,7 @@ class Filesystem { /** * check if a file or folder has been updated since $time * + * @param string $path * @param int $time * @return bool */ @@ -653,8 +594,8 @@ class Filesystem { * @param string $directory path under datadirectory * @return array */ - public static function getDirectoryContent($directory, $mimetype_filter = '') { - return self::$defaultInstance->getDirectoryContent($directory, $mimetype_filter); + public static function getDirectoryContent($directory) { + return self::$defaultInstance->getDirectoryContent($directory); } /** diff --git a/lib/files/mount.php b/lib/files/mount.php new file mode 100644 index 00000000000..c6746d2a337 --- /dev/null +++ b/lib/files/mount.php @@ -0,0 +1,171 @@ +<?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. + */ + +namespace OC\Files; + +class Mount { + static private $mounts = array(); + + /** + * @var \OC\Files\Storage\Storage $storage + */ + private $storage = null; + private $class; + private $storageId; + private $arguments = array(); + private $mountPoint; + + /** + * @param string|\OC\Files\Storage\Storage $storage + * @param string $mountpoint + * @param array $arguments + */ + public function __construct($storage, $mountpoint, $arguments = null) { + if (is_null($arguments)) { + $arguments = array(); + } + + $mountpoint = self::formatPath($mountpoint); + if ($storage instanceof \OC\Files\Storage\Storage) { + $this->class = get_class($storage); + $this->storage = $storage; + } 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; + + self::$mounts[$this->mountPoint] = $this; + } + + /** + * @return string + */ + public function getMountPoint() { + return $this->mountPoint; + } + + /** + * @return \OC\Files\Storage\Storage + */ + private function createStorage() { + if (class_exists($this->class)) { + try { + return new $this->class($this->arguments); + } catch (\Exception $exception) { + \OC_Log::write('core', $exception->getMessage(), \OC_Log::ERROR); + 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)) { + $this->storage = $this->createStorage(); + } + $this->storageId = $this->storage->getId(); + } + 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)); + } + return $internalPath; + } + + /** + * @param string $path + * @return string + */ + private static function formatPath($path) { + $path = Filesystem::normalizePath($path); + if (strlen($path) > 1) { + $path .= '/'; + } + return $path; + } + + /** + * Find the mount for $path + * + * @param $path + * @return Mount + */ + public static function find($path) { + $path = self::formatPath($path); + if (isset(self::$mounts[$path])) { + return self::$mounts[$path]; + } + + \OC_Hook::emit('OC_Filesystem', 'get_mountpoint', array('path' => $path)); + $foundMountPoint = ''; + $mountPoints = array_keys(self::$mounts); + foreach ($mountPoints as $mountpoint) { + if (strpos($path, $mountpoint) === 0 and strlen($mountpoint) > strlen($foundMountPoint)) { + $foundMountPoint = $mountpoint; + } + } + if (isset(self::$mounts[$foundMountPoint])) { + return self::$mounts[$foundMountPoint]; + } else { + return null; + } + } + + /** + * Find all mounts in $path + * + * @param $path + * @return Mount[] + */ + public static function findIn($path) { + $path = self::formatPath($path); + $result = array(); + $pathLength = strlen($path); + $mountPoints = array_keys(self::$mounts); + foreach ($mountPoints as $mountPoint) { + if (substr($mountPoint, 0, $pathLength) === $path and strlen($mountPoint) > $pathLength) { + $result[] = self::$mounts[$mountPoint]; + } + } + return $result; + } + + public static function clear() { + self::$mounts = array(); + } +} |