Cache.php 6.4 KB

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