MountPoint.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. protected $rootId = null;
  42. /**
  43. * Configuration options for the storage backend
  44. *
  45. * @var array
  46. */
  47. protected $arguments = array();
  48. protected $mountPoint;
  49. /**
  50. * Mount specific options
  51. *
  52. * @var array
  53. */
  54. protected $mountOptions = array();
  55. /**
  56. * @var \OC\Files\Storage\StorageFactory $loader
  57. */
  58. private $loader;
  59. /**
  60. * Specified whether the storage is invalid after failing to
  61. * instantiate it.
  62. *
  63. * @var bool
  64. */
  65. private $invalidStorage = false;
  66. /** @var int|null */
  67. protected $mountId;
  68. /**
  69. * @param string|\OC\Files\Storage\Storage $storage
  70. * @param string $mountpoint
  71. * @param array $arguments (optional) configuration for the storage backend
  72. * @param \OCP\Files\Storage\IStorageFactory $loader
  73. * @param array $mountOptions mount specific options
  74. * @param int|null $mountId
  75. * @throws \Exception
  76. */
  77. public function __construct($storage, $mountpoint, $arguments = null, $loader = null, $mountOptions = null, $mountId = null) {
  78. if (is_null($arguments)) {
  79. $arguments = array();
  80. }
  81. if (is_null($loader)) {
  82. $this->loader = new StorageFactory();
  83. } else {
  84. $this->loader = $loader;
  85. }
  86. if (!is_null($mountOptions)) {
  87. $this->mountOptions = $mountOptions;
  88. }
  89. $mountpoint = $this->formatPath($mountpoint);
  90. $this->mountPoint = $mountpoint;
  91. if ($storage instanceof Storage) {
  92. $this->class = get_class($storage);
  93. $this->storage = $this->loader->wrap($this, $storage);
  94. } else {
  95. // Update old classes to new namespace
  96. if (strpos($storage, 'OC_Filestorage_') !== false) {
  97. $storage = '\OC\Files\Storage\\' . substr($storage, 15);
  98. }
  99. $this->class = $storage;
  100. $this->arguments = $arguments;
  101. }
  102. $this->mountId = $mountId;
  103. }
  104. /**
  105. * get complete path to the mount point, relative to data/
  106. *
  107. * @return string
  108. */
  109. public function getMountPoint() {
  110. return $this->mountPoint;
  111. }
  112. /**
  113. * Sets the mount point path, relative to data/
  114. *
  115. * @param string $mountPoint new mount point
  116. */
  117. public function setMountPoint($mountPoint) {
  118. $this->mountPoint = $this->formatPath($mountPoint);
  119. }
  120. /**
  121. * create the storage that is mounted
  122. */
  123. private function createStorage() {
  124. if ($this->invalidStorage) {
  125. return;
  126. }
  127. if (class_exists($this->class)) {
  128. try {
  129. $class = $this->class;
  130. // prevent recursion by setting the storage before applying wrappers
  131. $this->storage = new $class($this->arguments);
  132. $this->storage = $this->loader->wrap($this, $this->storage);
  133. } catch (\Exception $exception) {
  134. $this->storage = null;
  135. $this->invalidStorage = true;
  136. if ($this->mountPoint === '/') {
  137. // the root storage could not be initialized, show the user!
  138. throw new \Exception('The root storage could not be initialized. Please contact your local administrator.', $exception->getCode(), $exception);
  139. } else {
  140. \OCP\Util::writeLog('core', $exception->getMessage(), \OCP\Util::ERROR);
  141. }
  142. return;
  143. }
  144. } else {
  145. \OCP\Util::writeLog('core', 'storage backend ' . $this->class . ' not found', \OCP\Util::ERROR);
  146. $this->invalidStorage = true;
  147. return;
  148. }
  149. }
  150. /**
  151. * @return \OC\Files\Storage\Storage
  152. */
  153. public function getStorage() {
  154. if (is_null($this->storage)) {
  155. $this->createStorage();
  156. }
  157. return $this->storage;
  158. }
  159. /**
  160. * @return string
  161. */
  162. public function getStorageId() {
  163. if (!$this->storageId) {
  164. if (is_null($this->storage)) {
  165. $storage = $this->createStorage(); //FIXME: start using exceptions
  166. if (is_null($storage)) {
  167. return null;
  168. }
  169. $this->storage = $storage;
  170. }
  171. $this->storageId = $this->storage->getId();
  172. if (strlen($this->storageId) > 64) {
  173. $this->storageId = md5($this->storageId);
  174. }
  175. }
  176. return $this->storageId;
  177. }
  178. /**
  179. * @return int
  180. */
  181. public function getNumericStorageId() {
  182. return $this->getStorage()->getStorageCache()->getNumericId();
  183. }
  184. /**
  185. * @param string $path
  186. * @return string
  187. */
  188. public function getInternalPath($path) {
  189. $path = Filesystem::normalizePath($path, true, false, true);
  190. if ($this->mountPoint === $path or $this->mountPoint . '/' === $path) {
  191. $internalPath = '';
  192. } else {
  193. $internalPath = substr($path, strlen($this->mountPoint));
  194. }
  195. // substr returns false instead of an empty string, we always want a string
  196. return (string)$internalPath;
  197. }
  198. /**
  199. * @param string $path
  200. * @return string
  201. */
  202. private function formatPath($path) {
  203. $path = Filesystem::normalizePath($path);
  204. if (strlen($path) > 1) {
  205. $path .= '/';
  206. }
  207. return $path;
  208. }
  209. /**
  210. * @param callable $wrapper
  211. */
  212. public function wrapStorage($wrapper) {
  213. $storage = $this->getStorage();
  214. // storage can be null if it couldn't be initialized
  215. if ($storage != null) {
  216. $this->storage = $wrapper($this->mountPoint, $storage, $this);
  217. }
  218. }
  219. /**
  220. * Get a mount option
  221. *
  222. * @param string $name Name of the mount option to get
  223. * @param mixed $default Default value for the mount option
  224. * @return mixed
  225. */
  226. public function getOption($name, $default) {
  227. return isset($this->mountOptions[$name]) ? $this->mountOptions[$name] : $default;
  228. }
  229. /**
  230. * Get all options for the mount
  231. *
  232. * @return array
  233. */
  234. public function getOptions() {
  235. return $this->mountOptions;
  236. }
  237. /**
  238. * Get the file id of the root of the storage
  239. *
  240. * @return int
  241. */
  242. public function getStorageRootId() {
  243. if (is_null($this->rootId)) {
  244. $this->rootId = (int)$this->getStorage()->getCache()->getId('');
  245. }
  246. return $this->rootId;
  247. }
  248. public function getMountId() {
  249. return $this->mountId;
  250. }
  251. }