Cache.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christopher Schäpers <kondou@ts.unde.re>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  8. * @author Michael Gapczynski <GapczynskiM@gmail.com>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Robin Appelman <robin@icewind.nl>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OCA\Files_Sharing;
  29. use OC\Files\Cache\FailedCache;
  30. use OC\Files\Cache\Wrapper\CacheJail;
  31. use OC\Files\Storage\Wrapper\Jail;
  32. use OCP\Files\Cache\ICacheEntry;
  33. use OCP\Files\StorageNotAvailableException;
  34. /**
  35. * Metadata cache for shared files
  36. *
  37. * don't use this class directly if you need to get metadata, use \OC\Files\Filesystem::getFileInfo instead
  38. */
  39. class Cache extends CacheJail {
  40. /**
  41. * @var \OCA\Files_Sharing\SharedStorage
  42. */
  43. private $storage;
  44. /**
  45. * @var ICacheEntry
  46. */
  47. private $sourceRootInfo;
  48. private $rootUnchanged = true;
  49. private $ownerDisplayName;
  50. private $numericId;
  51. /**
  52. * @param \OCA\Files_Sharing\SharedStorage $storage
  53. * @param ICacheEntry $sourceRootInfo
  54. */
  55. public function __construct($storage, ICacheEntry $sourceRootInfo) {
  56. $this->storage = $storage;
  57. $this->sourceRootInfo = $sourceRootInfo;
  58. $this->numericId = $sourceRootInfo->getStorageId();
  59. parent::__construct(
  60. null,
  61. null
  62. );
  63. }
  64. protected function getRoot() {
  65. if (is_null($this->root)) {
  66. $absoluteRoot = $this->sourceRootInfo->getPath();
  67. // the sourceRootInfo path is the absolute path of the folder in the "real" storage
  68. // in the case where a folder is shared from a Jail we need to ensure that the share Jail
  69. // has it's root set relative to the source Jail
  70. $currentStorage = $this->storage->getSourceStorage();
  71. if ($currentStorage->instanceOfStorage(Jail::class)) {
  72. /** @var Jail $currentStorage */
  73. $absoluteRoot = $currentStorage->getJailedPath($absoluteRoot);
  74. }
  75. $this->root = $absoluteRoot;
  76. }
  77. return $this->root;
  78. }
  79. public function getCache() {
  80. if (is_null($this->cache)) {
  81. $sourceStorage = $this->storage->getSourceStorage();
  82. if ($sourceStorage) {
  83. $this->cache = $sourceStorage->getCache();
  84. } else {
  85. // don't set $this->cache here since sourceStorage will be set later
  86. return new FailedCache();
  87. }
  88. }
  89. return $this->cache;
  90. }
  91. public function getNumericStorageId() {
  92. if (isset($this->numericId)) {
  93. return $this->numericId;
  94. } else {
  95. return false;
  96. }
  97. }
  98. public function get($file) {
  99. if ($this->rootUnchanged && ($file === '' || $file === $this->sourceRootInfo->getId())) {
  100. return $this->formatCacheEntry(clone $this->sourceRootInfo, '');
  101. }
  102. return parent::get($file);
  103. }
  104. public function update($id, array $data) {
  105. $this->rootUnchanged = false;
  106. parent::update($id, $data);
  107. }
  108. public function insert($file, array $data) {
  109. $this->rootUnchanged = false;
  110. return parent::insert($file, $data);
  111. }
  112. public function remove($file) {
  113. $this->rootUnchanged = false;
  114. parent::remove($file);
  115. }
  116. public function moveFromCache(\OCP\Files\Cache\ICache $sourceCache, $sourcePath, $targetPath) {
  117. $this->rootUnchanged = false;
  118. return parent::moveFromCache($sourceCache, $sourcePath, $targetPath);
  119. }
  120. protected function formatCacheEntry($entry, $path = null) {
  121. if (is_null($path)) {
  122. $path = isset($entry['path']) ? $entry['path'] : '';
  123. $entry['path'] = $this->getJailedPath($path);
  124. } else {
  125. $entry['path'] = $path;
  126. }
  127. try {
  128. $sharePermissions = $this->storage->getPermissions($entry['path']);
  129. } catch (StorageNotAvailableException $e) {
  130. // thrown by FailedStorage e.g. when the sharer does not exist anymore
  131. // (IDE may say the exception is never thrown – false negative)
  132. $sharePermissions = 0;
  133. }
  134. if (isset($entry['permissions'])) {
  135. $entry['permissions'] &= $sharePermissions;
  136. } else {
  137. $entry['permissions'] = $sharePermissions;
  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. }