MountPoint.php 6.8 KB

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