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.

MountPoint.php 7.8KB

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