Cache.php 4.7 KB

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