1
0

Manager.php 4.4 KB

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