Cache.php 6.5 KB

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