You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

284 lines
7.0 KiB

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