Cache.php 6.4 KB

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