Cache.php 6.2 KB

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