CacheJail.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Files\Cache\Wrapper;
  8. use OC\Files\Cache\Cache;
  9. use OC\Files\Cache\CacheDependencies;
  10. use OC\Files\Search\SearchBinaryOperator;
  11. use OC\Files\Search\SearchComparison;
  12. use OCP\Files\Cache\ICache;
  13. use OCP\Files\Cache\ICacheEntry;
  14. use OCP\Files\Search\ISearchBinaryOperator;
  15. use OCP\Files\Search\ISearchComparison;
  16. use OCP\Files\Search\ISearchOperator;
  17. /**
  18. * Jail to a subdirectory of the wrapped cache
  19. */
  20. class CacheJail extends CacheWrapper {
  21. /**
  22. * @var string
  23. */
  24. protected $root;
  25. protected $unjailedRoot;
  26. public function __construct(
  27. ?ICache $cache,
  28. string $root,
  29. ?CacheDependencies $dependencies = null,
  30. ) {
  31. parent::__construct($cache, $dependencies);
  32. $this->root = $root;
  33. if ($cache instanceof CacheJail) {
  34. $this->unjailedRoot = $cache->getSourcePath($root);
  35. } else {
  36. $this->unjailedRoot = $root;
  37. }
  38. }
  39. protected function getRoot() {
  40. return $this->root;
  41. }
  42. /**
  43. * Get the root path with any nested jails resolved
  44. *
  45. * @return string
  46. */
  47. protected function getGetUnjailedRoot() {
  48. return $this->unjailedRoot;
  49. }
  50. protected function getSourcePath($path) {
  51. if ($path === '') {
  52. return $this->getRoot();
  53. } else {
  54. return $this->getRoot() . '/' . ltrim($path, '/');
  55. }
  56. }
  57. /**
  58. * @param string $path
  59. * @param null|string $root
  60. * @return null|string the jailed path or null if the path is outside the jail
  61. */
  62. protected function getJailedPath(string $path, ?string $root = null) {
  63. if ($root === null) {
  64. $root = $this->getRoot();
  65. }
  66. if ($root === '') {
  67. return $path;
  68. }
  69. $rootLength = strlen($root) + 1;
  70. if ($path === $root) {
  71. return '';
  72. } elseif (substr($path, 0, $rootLength) === $root . '/') {
  73. return substr($path, $rootLength);
  74. } else {
  75. return null;
  76. }
  77. }
  78. protected function formatCacheEntry($entry) {
  79. if (isset($entry['path'])) {
  80. $entry['path'] = $this->getJailedPath($entry['path']);
  81. }
  82. return $entry;
  83. }
  84. /**
  85. * get the stored metadata of a file or folder
  86. *
  87. * @param string /int $file
  88. * @return ICacheEntry|false
  89. */
  90. public function get($file) {
  91. if (is_string($file) or $file == '') {
  92. $file = $this->getSourcePath($file);
  93. }
  94. return parent::get($file);
  95. }
  96. /**
  97. * insert meta data for a new file or folder
  98. *
  99. * @param string $file
  100. * @param array $data
  101. *
  102. * @return int file id
  103. * @throws \RuntimeException
  104. */
  105. public function insert($file, array $data) {
  106. return $this->getCache()->insert($this->getSourcePath($file), $data);
  107. }
  108. /**
  109. * update the metadata in the cache
  110. *
  111. * @param int $id
  112. * @param array $data
  113. */
  114. public function update($id, array $data) {
  115. $this->getCache()->update($id, $data);
  116. }
  117. /**
  118. * get the file id for a file
  119. *
  120. * @param string $file
  121. * @return int
  122. */
  123. public function getId($file) {
  124. return $this->getCache()->getId($this->getSourcePath($file));
  125. }
  126. /**
  127. * get the id of the parent folder of a file
  128. *
  129. * @param string $file
  130. * @return int
  131. */
  132. public function getParentId($file) {
  133. return $this->getCache()->getParentId($this->getSourcePath($file));
  134. }
  135. /**
  136. * check if a file is available in the cache
  137. *
  138. * @param string $file
  139. * @return bool
  140. */
  141. public function inCache($file) {
  142. return $this->getCache()->inCache($this->getSourcePath($file));
  143. }
  144. /**
  145. * remove a file or folder from the cache
  146. *
  147. * @param string $file
  148. */
  149. public function remove($file) {
  150. $this->getCache()->remove($this->getSourcePath($file));
  151. }
  152. /**
  153. * Move a file or folder in the cache
  154. *
  155. * @param string $source
  156. * @param string $target
  157. */
  158. public function move($source, $target) {
  159. $this->getCache()->move($this->getSourcePath($source), $this->getSourcePath($target));
  160. }
  161. /**
  162. * Get the storage id and path needed for a move
  163. *
  164. * @param string $path
  165. * @return array [$storageId, $internalPath]
  166. */
  167. protected function getMoveInfo($path) {
  168. return [$this->getNumericStorageId(), $this->getSourcePath($path)];
  169. }
  170. /**
  171. * remove all entries for files that are stored on the storage from the cache
  172. */
  173. public function clear() {
  174. $this->getCache()->remove($this->getRoot());
  175. }
  176. /**
  177. * @param string $file
  178. *
  179. * @return int Cache::NOT_FOUND, Cache::PARTIAL, Cache::SHALLOW or Cache::COMPLETE
  180. */
  181. public function getStatus($file) {
  182. return $this->getCache()->getStatus($this->getSourcePath($file));
  183. }
  184. /**
  185. * update the folder size and the size of all parent folders
  186. *
  187. * @param string|boolean $path
  188. * @param array $data (optional) meta data of the folder
  189. */
  190. public function correctFolderSize($path, $data = null, $isBackgroundScan = false) {
  191. if ($this->getCache() instanceof Cache) {
  192. $this->getCache()->correctFolderSize($this->getSourcePath($path), $data, $isBackgroundScan);
  193. }
  194. }
  195. /**
  196. * get the size of a folder and set it in the cache
  197. *
  198. * @param string $path
  199. * @param array|null|ICacheEntry $entry (optional) meta data of the folder
  200. * @return int|float
  201. */
  202. public function calculateFolderSize($path, $entry = null) {
  203. if ($this->getCache() instanceof Cache) {
  204. return $this->getCache()->calculateFolderSize($this->getSourcePath($path), $entry);
  205. } else {
  206. return 0;
  207. }
  208. }
  209. /**
  210. * get all file ids on the files on the storage
  211. *
  212. * @return int[]
  213. */
  214. public function getAll() {
  215. // not supported
  216. return [];
  217. }
  218. /**
  219. * find a folder in the cache which has not been fully scanned
  220. *
  221. * If multiply incomplete folders are in the cache, the one with the highest id will be returned,
  222. * use the one with the highest id gives the best result with the background scanner, since that is most
  223. * likely the folder where we stopped scanning previously
  224. *
  225. * @return string|false the path of the folder or false when no folder matched
  226. */
  227. public function getIncomplete() {
  228. // not supported
  229. return false;
  230. }
  231. /**
  232. * get the path of a file on this storage by it's id
  233. *
  234. * @param int $id
  235. * @return string|null
  236. */
  237. public function getPathById($id) {
  238. $path = $this->getCache()->getPathById($id);
  239. if ($path === null) {
  240. return null;
  241. }
  242. return $this->getJailedPath($path);
  243. }
  244. /**
  245. * Move a file or folder in the cache
  246. *
  247. * Note that this should make sure the entries are removed from the source cache
  248. *
  249. * @param \OCP\Files\Cache\ICache $sourceCache
  250. * @param string $sourcePath
  251. * @param string $targetPath
  252. */
  253. public function moveFromCache(\OCP\Files\Cache\ICache $sourceCache, $sourcePath, $targetPath) {
  254. if ($sourceCache === $this) {
  255. return $this->move($sourcePath, $targetPath);
  256. }
  257. return $this->getCache()->moveFromCache($sourceCache, $sourcePath, $this->getSourcePath($targetPath));
  258. }
  259. public function getQueryFilterForStorage(): ISearchOperator {
  260. return $this->addJailFilterQuery($this->getCache()->getQueryFilterForStorage());
  261. }
  262. protected function addJailFilterQuery(ISearchOperator $filter): ISearchOperator {
  263. if ($this->getGetUnjailedRoot() !== '' && $this->getGetUnjailedRoot() !== '/') {
  264. return new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_AND,
  265. [
  266. $filter,
  267. new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_OR,
  268. [
  269. new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'path', $this->getGetUnjailedRoot()),
  270. new SearchComparison(ISearchComparison::COMPARE_LIKE_CASE_SENSITIVE, 'path', SearchComparison::escapeLikeParameter($this->getGetUnjailedRoot()) . '/%'),
  271. ],
  272. )
  273. ]
  274. );
  275. } else {
  276. return $filter;
  277. }
  278. }
  279. public function getCacheEntryFromSearchResult(ICacheEntry $rawEntry): ?ICacheEntry {
  280. if ($this->getGetUnjailedRoot() === '' || str_starts_with($rawEntry->getPath(), $this->getGetUnjailedRoot())) {
  281. $rawEntry = $this->getCache()->getCacheEntryFromSearchResult($rawEntry);
  282. if ($rawEntry) {
  283. $jailedPath = $this->getJailedPath($rawEntry->getPath());
  284. if ($jailedPath !== null) {
  285. return $this->formatCacheEntry(clone $rawEntry) ?: null;
  286. }
  287. }
  288. }
  289. return null;
  290. }
  291. }