1
0

MountPoint.php 6.9 KB

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