CacheJail.php 8.7 KB

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