PageRenderTime 55ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/files/mount/manager.php

https://github.com/sezuan/core
PHP | 120 lines | 69 code | 11 blank | 40 comment | 8 complexity | 9c571ce4567439da7e60fc97c8365556 MD5 | raw file
Possible License(s): AGPL-3.0, AGPL-1.0, MPL-2.0-no-copyleft-exception
  1. <?php
  2. /**
  3. * Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace OC\Files\Mount;
  9. use \OC\Files\Filesystem;
  10. class Manager {
  11. /**
  12. * @var Mount[]
  13. */
  14. private $mounts = array();
  15. /**
  16. * @param Mount $mount
  17. */
  18. public function addMount($mount) {
  19. $this->mounts[$mount->getMountPoint()] = $mount;
  20. }
  21. /**
  22. * Find the mount for $path
  23. *
  24. * @param $path
  25. * @return Mount
  26. */
  27. public function find($path) {
  28. \OC_Util::setupFS();
  29. $path = $this->formatPath($path);
  30. if (isset($this->mounts[$path])) {
  31. return $this->mounts[$path];
  32. }
  33. \OC_Hook::emit('OC_Filesystem', 'get_mountpoint', array('path' => $path));
  34. $foundMountPoint = '';
  35. $mountPoints = array_keys($this->mounts);
  36. foreach ($mountPoints as $mountpoint) {
  37. if (strpos($path, $mountpoint) === 0 and strlen($mountpoint) > strlen($foundMountPoint)) {
  38. $foundMountPoint = $mountpoint;
  39. }
  40. }
  41. if (isset($this->mounts[$foundMountPoint])) {
  42. return $this->mounts[$foundMountPoint];
  43. } else {
  44. return null;
  45. }
  46. }
  47. /**
  48. * Find all mounts in $path
  49. *
  50. * @param $path
  51. * @return Mount[]
  52. */
  53. public function findIn($path) {
  54. \OC_Util::setupFS();
  55. $path = $this->formatPath($path);
  56. $result = array();
  57. $pathLength = strlen($path);
  58. $mountPoints = array_keys($this->mounts);
  59. foreach ($mountPoints as $mountPoint) {
  60. if (substr($mountPoint, 0, $pathLength) === $path and strlen($mountPoint) > $pathLength) {
  61. $result[] = $this->mounts[$mountPoint];
  62. }
  63. }
  64. return $result;
  65. }
  66. public function clear() {
  67. $this->mounts = array();
  68. }
  69. /**
  70. * Find mounts by storage id
  71. *
  72. * @param string $id
  73. * @return Mount[]
  74. */
  75. public function findByStorageId($id) {
  76. \OC_Util::setupFS();
  77. if (strlen($id) > 64) {
  78. $id = md5($id);
  79. }
  80. $result = array();
  81. foreach ($this->mounts as $mount) {
  82. if ($mount->getStorageId() === $id) {
  83. $result[] = $mount;
  84. }
  85. }
  86. return $result;
  87. }
  88. /**
  89. * Find mounts by numeric storage id
  90. *
  91. * @param string $id
  92. * @return Mount
  93. */
  94. public function findByNumericId($id) {
  95. $storageId = \OC\Files\Cache\Storage::getStorageId($id);
  96. return $this->findByStorageId($storageId);
  97. }
  98. /**
  99. * @param string $path
  100. * @return string
  101. */
  102. private function formatPath($path) {
  103. $path = Filesystem::normalizePath($path);
  104. if (strlen($path) > 1) {
  105. $path .= '/';
  106. }
  107. return $path;
  108. }
  109. }