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.

200 lines
4.5 KiB

3 years ago
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Björn Schießle <bjoern@schiessle.org>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. * @author Robin McCorkell <robin@mccorkell.me.uk>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OC\Files\Mount;
  28. use OC\Cache\CappedMemoryCache;
  29. use OC\Files\Filesystem;
  30. use OCP\Files\Mount\IMountManager;
  31. use OCP\Files\Mount\IMountPoint;
  32. class Manager implements IMountManager {
  33. /** @var MountPoint[] */
  34. private $mounts = [];
  35. /** @var CappedMemoryCache */
  36. private $pathCache;
  37. /** @var CappedMemoryCache */
  38. private $inPathCache;
  39. public function __construct() {
  40. $this->pathCache = new CappedMemoryCache();
  41. $this->inPathCache = new CappedMemoryCache();
  42. }
  43. /**
  44. * @param IMountPoint $mount
  45. */
  46. public function addMount(IMountPoint $mount) {
  47. $this->mounts[$mount->getMountPoint()] = $mount;
  48. $this->pathCache->clear();
  49. $this->inPathCache->clear();
  50. }
  51. /**
  52. * @param string $mountPoint
  53. */
  54. public function removeMount(string $mountPoint) {
  55. $mountPoint = Filesystem::normalizePath($mountPoint);
  56. if (\strlen($mountPoint) > 1) {
  57. $mountPoint .= '/';
  58. }
  59. unset($this->mounts[$mountPoint]);
  60. $this->pathCache->clear();
  61. $this->inPathCache->clear();
  62. }
  63. /**
  64. * @param string $mountPoint
  65. * @param string $target
  66. */
  67. public function moveMount(string $mountPoint, string $target) {
  68. $this->mounts[$target] = $this->mounts[$mountPoint];
  69. unset($this->mounts[$mountPoint]);
  70. $this->pathCache->clear();
  71. $this->inPathCache->clear();
  72. }
  73. /**
  74. * Find the mount for $path
  75. *
  76. * @param string $path
  77. * @return MountPoint|null
  78. */
  79. public function find(string $path) {
  80. \OC_Util::setupFS();
  81. $path = Filesystem::normalizePath($path);
  82. if (isset($this->pathCache[$path])) {
  83. return $this->pathCache[$path];
  84. }
  85. $current = $path;
  86. while (true) {
  87. $mountPoint = $current . '/';
  88. if (isset($this->mounts[$mountPoint])) {
  89. $this->pathCache[$path] = $this->mounts[$mountPoint];
  90. return $this->mounts[$mountPoint];
  91. }
  92. if ($current === '') {
  93. return null;
  94. }
  95. $current = dirname($current);
  96. if ($current === '.' || $current === '/') {
  97. $current = '';
  98. }
  99. }
  100. }
  101. /**
  102. * Find all mounts in $path
  103. *
  104. * @param string $path
  105. * @return MountPoint[]
  106. */
  107. public function findIn(string $path): array {
  108. \OC_Util::setupFS();
  109. $path = $this->formatPath($path);
  110. if (isset($this->inPathCache[$path])) {
  111. return $this->inPathCache[$path];
  112. }
  113. $result = [];
  114. $pathLength = \strlen($path);
  115. $mountPoints = array_keys($this->mounts);
  116. foreach ($mountPoints as $mountPoint) {
  117. if (substr($mountPoint, 0, $pathLength) === $path && \strlen($mountPoint) > $pathLength) {
  118. $result[] = $this->mounts[$mountPoint];
  119. }
  120. }
  121. $this->inPathCache[$path] = $result;
  122. return $result;
  123. }
  124. public function clear() {
  125. $this->mounts = [];
  126. $this->pathCache->clear();
  127. $this->inPathCache->clear();
  128. }
  129. /**
  130. * Find mounts by storage id
  131. *
  132. * @param string $id
  133. * @return MountPoint[]
  134. */
  135. public function findByStorageId(string $id): array {
  136. \OC_Util::setupFS();
  137. if (\strlen($id) > 64) {
  138. $id = md5($id);
  139. }
  140. $result = [];
  141. foreach ($this->mounts as $mount) {
  142. if ($mount->getStorageId() === $id) {
  143. $result[] = $mount;
  144. }
  145. }
  146. return $result;
  147. }
  148. /**
  149. * @return MountPoint[]
  150. */
  151. public function getAll(): array {
  152. return $this->mounts;
  153. }
  154. /**
  155. * Find mounts by numeric storage id
  156. *
  157. * @param int $id
  158. * @return MountPoint[]
  159. */
  160. public function findByNumericId(int $id): array {
  161. $storageId = \OC\Files\Cache\Storage::getStorageId($id);
  162. return $this->findByStorageId($storageId);
  163. }
  164. /**
  165. * @param string $path
  166. * @return string
  167. */
  168. private function formatPath(string $path): string {
  169. $path = Filesystem::normalizePath($path);
  170. if (\strlen($path) > 1) {
  171. $path .= '/';
  172. }
  173. return $path;
  174. }
  175. }