1
0

Cache.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. protected function getGetUnjailedRoot() {
  81. return $this->sourceRootInfo->getPath();
  82. }
  83. public function getCache() {
  84. if (is_null($this->cache)) {
  85. $sourceStorage = $this->storage->getSourceStorage();
  86. if ($sourceStorage) {
  87. $this->cache = $sourceStorage->getCache();
  88. } else {
  89. // don't set $this->cache here since sourceStorage will be set later
  90. return new FailedCache();
  91. }
  92. }
  93. return $this->cache;
  94. }
  95. public function getNumericStorageId() {
  96. if (isset($this->numericId)) {
  97. return $this->numericId;
  98. } else {
  99. return false;
  100. }
  101. }
  102. public function get($file) {
  103. if ($this->rootUnchanged && ($file === '' || $file === $this->sourceRootInfo->getId())) {
  104. return $this->formatCacheEntry(clone $this->sourceRootInfo, '');
  105. }
  106. return parent::get($file);
  107. }
  108. public function update($id, array $data) {
  109. $this->rootUnchanged = false;
  110. parent::update($id, $data);
  111. }
  112. public function insert($file, array $data) {
  113. $this->rootUnchanged = false;
  114. return parent::insert($file, $data);
  115. }
  116. public function remove($file) {
  117. $this->rootUnchanged = false;
  118. parent::remove($file);
  119. }
  120. public function moveFromCache(\OCP\Files\Cache\ICache $sourceCache, $sourcePath, $targetPath) {
  121. $this->rootUnchanged = false;
  122. return parent::moveFromCache($sourceCache, $sourcePath, $targetPath);
  123. }
  124. protected function formatCacheEntry($entry, $path = null) {
  125. if (is_null($path)) {
  126. $path = $entry['path'] ?? '';
  127. $entry['path'] = $this->getJailedPath($path);
  128. } else {
  129. $entry['path'] = $path;
  130. }
  131. try {
  132. if (isset($entry['permissions'])) {
  133. $entry['permissions'] &= $this->storage->getShare()->getPermissions();
  134. } else {
  135. $entry['permissions'] = $this->storage->getPermissions($entry['path']);
  136. }
  137. } catch (StorageNotAvailableException $e) {
  138. // thrown by FailedStorage e.g. when the sharer does not exist anymore
  139. // (IDE may say the exception is never thrown – false negative)
  140. $sharePermissions = 0;
  141. }
  142. $entry['uid_owner'] = $this->storage->getOwner('');
  143. $entry['displayname_owner'] = $this->getOwnerDisplayName();
  144. if ($path === '') {
  145. $entry['is_share_mount_point'] = true;
  146. }
  147. return $entry;
  148. }
  149. private function getOwnerDisplayName() {
  150. if (!$this->ownerDisplayName) {
  151. $this->ownerDisplayName = \OC_User::getDisplayName($this->storage->getOwner(''));
  152. }
  153. return $this->ownerDisplayName;
  154. }
  155. /**
  156. * remove all entries for files that are stored on the storage from the cache
  157. */
  158. public function clear() {
  159. // Not a valid action for Shared Cache
  160. }
  161. }