manager.php 3.8 KB

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