CacheJail.php 8.7 KB

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