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.

181 lines
5.0 KiB

10 years ago
10 years ago
10 years ago
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christopher Schäpers <kondou@ts.unde.re>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  9. * @author Michael Gapczynski <GapczynskiM@gmail.com>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Robin Appelman <robin@icewind.nl>
  12. * @author Roeland Jago Douma <roeland@famdouma.nl>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OCA\Files_Sharing;
  30. use OC\Files\Cache\FailedCache;
  31. use OC\Files\Cache\Wrapper\CacheJail;
  32. use OC\Files\Storage\Wrapper\Jail;
  33. use OCP\Files\Cache\ICacheEntry;
  34. use OCP\Files\StorageNotAvailableException;
  35. /**
  36. * Metadata cache for shared files
  37. *
  38. * don't use this class directly if you need to get metadata, use \OC\Files\Filesystem::getFileInfo instead
  39. */
  40. class Cache extends CacheJail {
  41. /**
  42. * @var \OCA\Files_Sharing\SharedStorage
  43. */
  44. private $storage;
  45. /**
  46. * @var ICacheEntry
  47. */
  48. private $sourceRootInfo;
  49. private $rootUnchanged = true;
  50. private $ownerDisplayName;
  51. private $numericId;
  52. /**
  53. * @param \OCA\Files_Sharing\SharedStorage $storage
  54. * @param ICacheEntry $sourceRootInfo
  55. */
  56. public function __construct($storage, ICacheEntry $sourceRootInfo) {
  57. $this->storage = $storage;
  58. $this->sourceRootInfo = $sourceRootInfo;
  59. $this->numericId = $sourceRootInfo->getStorageId();
  60. parent::__construct(
  61. null,
  62. ''
  63. );
  64. }
  65. protected function getRoot() {
  66. if ($this->root === '') {
  67. $absoluteRoot = $this->sourceRootInfo->getPath();
  68. // the sourceRootInfo path is the absolute path of the folder in the "real" storage
  69. // in the case where a folder is shared from a Jail we need to ensure that the share Jail
  70. // has it's root set relative to the source Jail
  71. $currentStorage = $this->storage->getSourceStorage();
  72. if ($currentStorage->instanceOfStorage(Jail::class)) {
  73. /** @var Jail $currentStorage */
  74. $absoluteRoot = $currentStorage->getJailedPath($absoluteRoot);
  75. }
  76. $this->root = $absoluteRoot;
  77. }
  78. return $this->root;
  79. }
  80. public function getCache() {
  81. if (is_null($this->cache)) {
  82. $sourceStorage = $this->storage->getSourceStorage();
  83. if ($sourceStorage) {
  84. $this->cache = $sourceStorage->getCache();
  85. } else {
  86. // don't set $this->cache here since sourceStorage will be set later
  87. return new FailedCache();
  88. }
  89. }
  90. return $this->cache;
  91. }
  92. public function getNumericStorageId() {
  93. if (isset($this->numericId)) {
  94. return $this->numericId;
  95. } else {
  96. return false;
  97. }
  98. }
  99. public function get($file) {
  100. if ($this->rootUnchanged && ($file === '' || $file === $this->sourceRootInfo->getId())) {
  101. return $this->formatCacheEntry(clone $this->sourceRootInfo, '');
  102. }
  103. return parent::get($file);
  104. }
  105. public function update($id, array $data) {
  106. $this->rootUnchanged = false;
  107. parent::update($id, $data);
  108. }
  109. public function insert($file, array $data) {
  110. $this->rootUnchanged = false;
  111. return parent::insert($file, $data);
  112. }
  113. public function remove($file) {
  114. $this->rootUnchanged = false;
  115. parent::remove($file);
  116. }
  117. public function moveFromCache(\OCP\Files\Cache\ICache $sourceCache, $sourcePath, $targetPath) {
  118. $this->rootUnchanged = false;
  119. return parent::moveFromCache($sourceCache, $sourcePath, $targetPath);
  120. }
  121. protected function formatCacheEntry($entry, $path = null) {
  122. if (is_null($path)) {
  123. $path = $entry['path'] ?? '';
  124. $entry['path'] = $this->getJailedPath($path);
  125. } else {
  126. $entry['path'] = $path;
  127. }
  128. try {
  129. $sharePermissions = $this->storage->getPermissions($entry['path']);
  130. } catch (StorageNotAvailableException $e) {
  131. // thrown by FailedStorage e.g. when the sharer does not exist anymore
  132. // (IDE may say the exception is never thrown – false negative)
  133. $sharePermissions = 0;
  134. }
  135. if (isset($entry['permissions'])) {
  136. $entry['permissions'] &= $sharePermissions;
  137. } else {
  138. $entry['permissions'] = $sharePermissions;
  139. }
  140. $entry['uid_owner'] = $this->storage->getOwner('');
  141. $entry['displayname_owner'] = $this->getOwnerDisplayName();
  142. if ($path === '') {
  143. $entry['is_share_mount_point'] = true;
  144. }
  145. return $entry;
  146. }
  147. private function getOwnerDisplayName() {
  148. if (!$this->ownerDisplayName) {
  149. $this->ownerDisplayName = \OC_User::getDisplayName($this->storage->getOwner(''));
  150. }
  151. return $this->ownerDisplayName;
  152. }
  153. /**
  154. * remove all entries for files that are stored on the storage from the cache
  155. */
  156. public function clear() {
  157. // Not a valid action for Shared Cache
  158. }
  159. }