mountpoint.php 6.1 KB

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