Cache.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_Sharing;
  8. use OC\Files\Cache\CacheDependencies;
  9. use OC\Files\Cache\FailedCache;
  10. use OC\Files\Cache\Wrapper\CacheJail;
  11. use OC\Files\Search\SearchBinaryOperator;
  12. use OC\Files\Search\SearchComparison;
  13. use OC\Files\Storage\Wrapper\Jail;
  14. use OC\User\DisplayNameCache;
  15. use OCP\Files\Cache\ICache;
  16. use OCP\Files\Cache\ICacheEntry;
  17. use OCP\Files\Search\ISearchBinaryOperator;
  18. use OCP\Files\Search\ISearchComparison;
  19. use OCP\Files\Search\ISearchOperator;
  20. use OCP\Files\StorageNotAvailableException;
  21. use OCP\Share\IShare;
  22. /**
  23. * Metadata cache for shared files
  24. *
  25. * don't use this class directly if you need to get metadata, use \OC\Files\Filesystem::getFileInfo instead
  26. */
  27. class Cache extends CacheJail {
  28. /** @var SharedStorage */
  29. private $storage;
  30. private ICacheEntry $sourceRootInfo;
  31. private bool $rootUnchanged = true;
  32. private ?string $ownerDisplayName = null;
  33. private $numericId;
  34. private DisplayNameCache $displayNameCache;
  35. private IShare $share;
  36. /**
  37. * @param SharedStorage $storage
  38. */
  39. public function __construct(
  40. $storage,
  41. ICacheEntry $sourceRootInfo,
  42. CacheDependencies $dependencies,
  43. IShare $share,
  44. ) {
  45. $this->storage = $storage;
  46. $this->sourceRootInfo = $sourceRootInfo;
  47. $this->numericId = $sourceRootInfo->getStorageId();
  48. $this->displayNameCache = $dependencies->getDisplayNameCache();
  49. $this->share = $share;
  50. parent::__construct(
  51. null,
  52. '',
  53. $dependencies,
  54. );
  55. }
  56. protected function getRoot() {
  57. if ($this->root === '') {
  58. $absoluteRoot = $this->sourceRootInfo->getPath();
  59. // the sourceRootInfo path is the absolute path of the folder in the "real" storage
  60. // in the case where a folder is shared from a Jail we need to ensure that the share Jail
  61. // has its root set relative to the source Jail
  62. $currentStorage = $this->storage->getSourceStorage();
  63. if ($currentStorage->instanceOfStorage(Jail::class)) {
  64. /** @var Jail $currentStorage */
  65. $absoluteRoot = $currentStorage->getJailedPath($absoluteRoot);
  66. }
  67. $this->root = $absoluteRoot;
  68. }
  69. return $this->root;
  70. }
  71. protected function getGetUnjailedRoot() {
  72. return $this->sourceRootInfo->getPath();
  73. }
  74. public function getCache(): ICache {
  75. if (is_null($this->cache)) {
  76. $sourceStorage = $this->storage->getSourceStorage();
  77. if ($sourceStorage) {
  78. $this->cache = $sourceStorage->getCache();
  79. } else {
  80. // don't set $this->cache here since sourceStorage will be set later
  81. return new FailedCache();
  82. }
  83. }
  84. return $this->cache;
  85. }
  86. public function getNumericStorageId() {
  87. if (isset($this->numericId)) {
  88. return $this->numericId;
  89. } else {
  90. return -1;
  91. }
  92. }
  93. public function get($file) {
  94. if ($this->rootUnchanged && ($file === '' || $file === $this->sourceRootInfo->getId())) {
  95. return $this->formatCacheEntry(clone $this->sourceRootInfo, '');
  96. }
  97. return parent::get($file);
  98. }
  99. public function update($id, array $data) {
  100. $this->rootUnchanged = false;
  101. parent::update($id, $data);
  102. }
  103. public function insert($file, array $data) {
  104. $this->rootUnchanged = false;
  105. return parent::insert($file, $data);
  106. }
  107. public function remove($file) {
  108. $this->rootUnchanged = false;
  109. parent::remove($file);
  110. }
  111. public function moveFromCache(\OCP\Files\Cache\ICache $sourceCache, $sourcePath, $targetPath) {
  112. $this->rootUnchanged = false;
  113. return parent::moveFromCache($sourceCache, $sourcePath, $targetPath);
  114. }
  115. protected function formatCacheEntry($entry, $path = null) {
  116. if (is_null($path)) {
  117. $path = $entry['path'] ?? '';
  118. $entry['path'] = $this->getJailedPath($path);
  119. } else {
  120. $entry['path'] = $path;
  121. }
  122. try {
  123. if (isset($entry['permissions'])) {
  124. $entry['permissions'] &= $this->share->getPermissions();
  125. } else {
  126. $entry['permissions'] = $this->storage->getPermissions($entry['path']);
  127. }
  128. if ($this->share->getNodeId() === $entry['fileid']) {
  129. $entry['name'] = basename($this->share->getTarget());
  130. }
  131. } catch (StorageNotAvailableException $e) {
  132. // thrown by FailedStorage e.g. when the sharer does not exist anymore
  133. // (IDE may say the exception is never thrown – false negative)
  134. $sharePermissions = 0;
  135. }
  136. $entry['uid_owner'] = $this->share->getShareOwner();
  137. $entry['displayname_owner'] = $this->getOwnerDisplayName();
  138. if ($path === '') {
  139. $entry['is_share_mount_point'] = true;
  140. }
  141. return $entry;
  142. }
  143. private function getOwnerDisplayName() {
  144. if (!$this->ownerDisplayName) {
  145. $uid = $this->share->getShareOwner();
  146. $this->ownerDisplayName = $this->displayNameCache->getDisplayName($uid) ?? $uid;
  147. }
  148. return $this->ownerDisplayName;
  149. }
  150. /**
  151. * remove all entries for files that are stored on the storage from the cache
  152. */
  153. public function clear() {
  154. // Not a valid action for Shared Cache
  155. }
  156. public function getQueryFilterForStorage(): ISearchOperator {
  157. $storageFilter = \OC\Files\Cache\Cache::getQueryFilterForStorage();
  158. // Do the normal jail behavior for non files
  159. if ($this->storage->getItemType() !== 'file') {
  160. return $this->addJailFilterQuery($storageFilter);
  161. }
  162. // for single file shares we don't need to do the LIKE
  163. return new SearchBinaryOperator(
  164. ISearchBinaryOperator::OPERATOR_AND,
  165. [
  166. $storageFilter,
  167. new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'path', $this->getGetUnjailedRoot()),
  168. ]
  169. );
  170. }
  171. public function getCacheEntryFromSearchResult(ICacheEntry $rawEntry): ?ICacheEntry {
  172. if ($rawEntry->getStorageId() === $this->getNumericStorageId()) {
  173. return parent::getCacheEntryFromSearchResult($rawEntry);
  174. } else {
  175. return null;
  176. }
  177. }
  178. }