You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Cache.php 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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\Storage\Wrapper\Jail;
  33. use OCP\Files\Cache\ICacheEntry;
  34. use OCP\Files\StorageNotAvailableException;
  35. /**
  36. * Metadata cache for shared files
  37. *
  38. * don't use this class directly if you need to get metadata, use \OC\Files\Filesystem::getFileInfo instead
  39. */
  40. class Cache extends CacheJail {
  41. /**
  42. * @var \OCA\Files_Sharing\SharedStorage
  43. */
  44. private $storage;
  45. /**
  46. * @var ICacheEntry
  47. */
  48. private $sourceRootInfo;
  49. private $rootUnchanged = true;
  50. private $ownerDisplayName;
  51. private $numericId;
  52. /**
  53. * @param \OCA\Files_Sharing\SharedStorage $storage
  54. * @param ICacheEntry $sourceRootInfo
  55. */
  56. public function __construct($storage, ICacheEntry $sourceRootInfo) {
  57. $this->storage = $storage;
  58. $this->sourceRootInfo = $sourceRootInfo;
  59. $this->numericId = $sourceRootInfo->getStorageId();
  60. parent::__construct(
  61. null,
  62. ''
  63. );
  64. }
  65. protected function getRoot() {
  66. if ($this->root === '') {
  67. $absoluteRoot = $this->sourceRootInfo->getPath();
  68. // the sourceRootInfo path is the absolute path of the folder in the "real" storage
  69. // in the case where a folder is shared from a Jail we need to ensure that the share Jail
  70. // has it's root set relative to the source Jail
  71. $currentStorage = $this->storage->getSourceStorage();
  72. if ($currentStorage->instanceOfStorage(Jail::class)) {
  73. /** @var Jail $currentStorage */
  74. $absoluteRoot = $currentStorage->getJailedPath($absoluteRoot);
  75. }
  76. $this->root = $absoluteRoot;
  77. }
  78. return $this->root;
  79. }
  80. public function getCache() {
  81. if (is_null($this->cache)) {
  82. $sourceStorage = $this->storage->getSourceStorage();
  83. if ($sourceStorage) {
  84. $this->cache = $sourceStorage->getCache();
  85. } else {
  86. // don't set $this->cache here since sourceStorage will be set later
  87. return new FailedCache();
  88. }
  89. }
  90. return $this->cache;
  91. }
  92. public function getNumericStorageId() {
  93. if (isset($this->numericId)) {
  94. return $this->numericId;
  95. } else {
  96. return false;
  97. }
  98. }
  99. public function get($file) {
  100. if ($this->rootUnchanged && ($file === '' || $file === $this->sourceRootInfo->getId())) {
  101. return $this->formatCacheEntry(clone $this->sourceRootInfo, '');
  102. }
  103. return parent::get($file);
  104. }
  105. public function update($id, array $data) {
  106. $this->rootUnchanged = false;
  107. parent::update($id, $data);
  108. }
  109. public function insert($file, array $data) {
  110. $this->rootUnchanged = false;
  111. return parent::insert($file, $data);
  112. }
  113. public function remove($file) {
  114. $this->rootUnchanged = false;
  115. parent::remove($file);
  116. }
  117. public function moveFromCache(\OCP\Files\Cache\ICache $sourceCache, $sourcePath, $targetPath) {
  118. $this->rootUnchanged = false;
  119. return parent::moveFromCache($sourceCache, $sourcePath, $targetPath);
  120. }
  121. protected function formatCacheEntry($entry, $path = null) {
  122. if (is_null($path)) {
  123. $path = $entry['path'] ?? '';
  124. $entry['path'] = $this->getJailedPath($path);
  125. } else {
  126. $entry['path'] = $path;
  127. }
  128. try {
  129. if (isset($entry['permissions'])) {
  130. $entry['permissions'] &= $this->storage->getShare()->getPermissions();
  131. } else {
  132. $entry['permissions'] = $this->storage->getPermissions($entry['path']);
  133. }
  134. } catch (StorageNotAvailableException $e) {
  135. // thrown by FailedStorage e.g. when the sharer does not exist anymore
  136. // (IDE may say the exception is never thrown – false negative)
  137. $sharePermissions = 0;
  138. }
  139. $entry['uid_owner'] = $this->storage->getOwner('');
  140. $entry['displayname_owner'] = $this->getOwnerDisplayName();
  141. if ($path === '') {
  142. $entry['is_share_mount_point'] = true;
  143. }
  144. return $entry;
  145. }
  146. private function getOwnerDisplayName() {
  147. if (!$this->ownerDisplayName) {
  148. $this->ownerDisplayName = \OC_User::getDisplayName($this->storage->getOwner(''));
  149. }
  150. return $this->ownerDisplayName;
  151. }
  152. /**
  153. * remove all entries for files that are stored on the storage from the cache
  154. */
  155. public function clear() {
  156. // Not a valid action for Shared Cache
  157. }
  158. }