Cache.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 false;
  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. if ($this->storage->getShare()->getNodeId() === $entry['fileid']) {
  142. $entry['name'] = basename($this->storage->getShare()->getTarget());
  143. }
  144. } catch (StorageNotAvailableException $e) {
  145. // thrown by FailedStorage e.g. when the sharer does not exist anymore
  146. // (IDE may say the exception is never thrown – false negative)
  147. $sharePermissions = 0;
  148. }
  149. $entry['uid_owner'] = $this->storage->getOwner('');
  150. $entry['displayname_owner'] = $this->getOwnerDisplayName();
  151. if ($path === '') {
  152. $entry['is_share_mount_point'] = true;
  153. }
  154. return $entry;
  155. }
  156. private function getOwnerDisplayName() {
  157. if (!$this->ownerDisplayName) {
  158. $uid = $this->storage->getOwner('');
  159. $this->ownerDisplayName = $this->displayNameCache->getDisplayName($uid) ?? $uid;
  160. }
  161. return $this->ownerDisplayName;
  162. }
  163. /**
  164. * remove all entries for files that are stored on the storage from the cache
  165. */
  166. public function clear() {
  167. // Not a valid action for Shared Cache
  168. }
  169. public function getQueryFilterForStorage(): ISearchOperator {
  170. $storageFilter = \OC\Files\Cache\Cache::getQueryFilterForStorage();
  171. // Do the normal jail behavior for non files
  172. if ($this->storage->getItemType() !== 'file') {
  173. return $this->addJailFilterQuery($storageFilter);
  174. }
  175. // for single file shares we don't need to do the LIKE
  176. return new SearchBinaryOperator(
  177. ISearchBinaryOperator::OPERATOR_AND,
  178. [
  179. $storageFilter,
  180. new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'path', $this->getGetUnjailedRoot()),
  181. ]
  182. );
  183. }
  184. public function getCacheEntryFromSearchResult(ICacheEntry $rawEntry): ?ICacheEntry {
  185. if ($rawEntry->getStorageId() === $this->getNumericStorageId()) {
  186. return parent::getCacheEntryFromSearchResult($rawEntry);
  187. } else {
  188. return null;
  189. }
  190. }
  191. }