mountpoint.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. /**
  3. * @author Björn Schießle <schiessle@owncloud.com>
  4. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. * @author Robin Appelman <icewind@owncloud.com>
  7. * @author Robin McCorkell <rmccorkell@karoshi.org.uk>
  8. * @author Vincent Petry <pvince81@owncloud.com>
  9. *
  10. * @copyright Copyright (c) 2015, ownCloud, Inc.
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OC\Files\Mount;
  27. use \OC\Files\Filesystem;
  28. use OC\Files\Storage\StorageFactory;
  29. use OC\Files\Storage\Storage;
  30. use OCP\Files\Mount\IMountPoint;
  31. class MountPoint implements IMountPoint {
  32. /**
  33. * @var \OC\Files\Storage\Storage $storage
  34. */
  35. protected $storage = null;
  36. protected $class;
  37. protected $storageId;
  38. /**
  39. * Configuration options for the storage backend
  40. *
  41. * @var array
  42. */
  43. protected $arguments = array();
  44. protected $mountPoint;
  45. /**
  46. * Mount specific options
  47. *
  48. * @var array
  49. */
  50. protected $mountOptions = array();
  51. /**
  52. * @var \OC\Files\Storage\StorageFactory $loader
  53. */
  54. private $loader;
  55. /**
  56. * Specified whether the storage is invalid after failing to
  57. * instantiate it.
  58. *
  59. * @var bool
  60. */
  61. private $invalidStorage = false;
  62. /**
  63. * @param string|\OC\Files\Storage\Storage $storage
  64. * @param string $mountpoint
  65. * @param array $arguments (optional) configuration for the storage backend
  66. * @param \OCP\Files\Storage\IStorageFactory $loader
  67. * @param array $mountOptions mount specific options
  68. */
  69. public function __construct($storage, $mountpoint, $arguments = null, $loader = null, $mountOptions = null) {
  70. if (is_null($arguments)) {
  71. $arguments = array();
  72. }
  73. if (is_null($loader)) {
  74. $this->loader = new StorageFactory();
  75. } else {
  76. $this->loader = $loader;
  77. }
  78. if (!is_null($mountOptions)) {
  79. $this->mountOptions = $mountOptions;
  80. }
  81. $mountpoint = $this->formatPath($mountpoint);
  82. $this->mountPoint = $mountpoint;
  83. if ($storage instanceof Storage) {
  84. $this->class = get_class($storage);
  85. $this->storage = $this->loader->wrap($this, $storage);
  86. } else {
  87. // Update old classes to new namespace
  88. if (strpos($storage, 'OC_Filestorage_') !== false) {
  89. $storage = '\OC\Files\Storage\\' . substr($storage, 15);
  90. }
  91. $this->class = $storage;
  92. $this->arguments = $arguments;
  93. }
  94. }
  95. /**
  96. * get complete path to the mount point, relative to data/
  97. *
  98. * @return string
  99. */
  100. public function getMountPoint() {
  101. return $this->mountPoint;
  102. }
  103. /**
  104. * Sets the mount point path, relative to data/
  105. *
  106. * @param string $mountPoint new mount point
  107. */
  108. public function setMountPoint($mountPoint) {
  109. $this->mountPoint = $this->formatPath($mountPoint);
  110. }
  111. /**
  112. * create the storage that is mounted
  113. *
  114. * @return \OC\Files\Storage\Storage
  115. */
  116. private function createStorage() {
  117. if ($this->invalidStorage) {
  118. return null;
  119. }
  120. if (class_exists($this->class)) {
  121. try {
  122. return $this->loader->getInstance($this, $this->class, $this->arguments);
  123. } catch (\Exception $exception) {
  124. $this->invalidStorage = true;
  125. if ($this->mountPoint === '/') {
  126. // the root storage could not be initialized, show the user!
  127. throw new \Exception('The root storage could not be initialized. Please contact your local administrator.', $exception->getCode(), $exception);
  128. } else {
  129. \OCP\Util::writeLog('core', $exception->getMessage(), \OCP\Util::ERROR);
  130. }
  131. return null;
  132. }
  133. } else {
  134. \OCP\Util::writeLog('core', 'storage backend ' . $this->class . ' not found', \OCP\Util::ERROR);
  135. $this->invalidStorage = true;
  136. return null;
  137. }
  138. }
  139. /**
  140. * @return \OC\Files\Storage\Storage
  141. */
  142. public function getStorage() {
  143. if (is_null($this->storage)) {
  144. $this->storage = $this->createStorage();
  145. }
  146. return $this->storage;
  147. }
  148. /**
  149. * @return string
  150. */
  151. public function getStorageId() {
  152. if (!$this->storageId) {
  153. if (is_null($this->storage)) {
  154. $storage = $this->createStorage(); //FIXME: start using exceptions
  155. if (is_null($storage)) {
  156. return null;
  157. }
  158. $this->storage = $storage;
  159. }
  160. $this->storageId = $this->storage->getId();
  161. if (strlen($this->storageId) > 64) {
  162. $this->storageId = md5($this->storageId);
  163. }
  164. }
  165. return $this->storageId;
  166. }
  167. /**
  168. * @param string $path
  169. * @return string
  170. */
  171. public function getInternalPath($path) {
  172. if ($this->mountPoint === $path or $this->mountPoint . '/' === $path) {
  173. $internalPath = '';
  174. } else {
  175. $internalPath = substr($path, strlen($this->mountPoint));
  176. }
  177. // substr returns false instead of an empty string, we always want a string
  178. return (string)$internalPath;
  179. }
  180. /**
  181. * @param string $path
  182. * @return string
  183. */
  184. private function formatPath($path) {
  185. $path = Filesystem::normalizePath($path);
  186. if (strlen($path) > 1) {
  187. $path .= '/';
  188. }
  189. return $path;
  190. }
  191. /**
  192. * @param callable $wrapper
  193. */
  194. public function wrapStorage($wrapper) {
  195. $storage = $this->getStorage();
  196. // storage can be null if it couldn't be initialized
  197. if ($storage != null) {
  198. $this->storage = $wrapper($this->mountPoint, $storage, $this);
  199. }
  200. }
  201. /**
  202. * Get a mount option
  203. *
  204. * @param string $name Name of the mount option to get
  205. * @param mixed $default Default value for the mount option
  206. * @return mixed
  207. */
  208. public function getOption($name, $default) {
  209. return isset($this->mountOptions[$name]) ? $this->mountOptions[$name] : $default;
  210. }
  211. /**
  212. * Get all options for the mount
  213. *
  214. * @return array
  215. */
  216. public function getOptions() {
  217. return $this->mountOptions;
  218. }
  219. }