Manager.php 3.8 KB

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