CacheJail.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Daniel Jagszent <daniel@jagszent.de>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. * @author Robin McCorkell <robin@mccorkell.me.uk>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Vincent Petry <pvince81@owncloud.com>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OC\Files\Cache\Wrapper;
  28. use OC\Files\Cache\Cache;
  29. use OCP\Files\Cache\ICacheEntry;
  30. /**
  31. * Jail to a subdirectory of the wrapped cache
  32. */
  33. class CacheJail extends CacheWrapper {
  34. /**
  35. * @var string
  36. */
  37. protected $root;
  38. /**
  39. * @param \OCP\Files\Cache\ICache $cache
  40. * @param string $root
  41. */
  42. public function __construct($cache, $root) {
  43. parent::__construct($cache);
  44. $this->root = $root;
  45. }
  46. protected function getSourcePath($path) {
  47. if ($path === '') {
  48. return $this->root;
  49. } else {
  50. return $this->root . '/' . ltrim($path, '/');
  51. }
  52. }
  53. /**
  54. * @param string $path
  55. * @return null|string the jailed path or null if the path is outside the jail
  56. */
  57. protected function getJailedPath($path) {
  58. if ($this->root === '') {
  59. return $path;
  60. }
  61. $rootLength = strlen($this->root) + 1;
  62. if ($path === $this->root) {
  63. return '';
  64. } else if (substr($path, 0, $rootLength) === $this->root . '/') {
  65. return substr($path, $rootLength);
  66. } else {
  67. return null;
  68. }
  69. }
  70. /**
  71. * @param ICacheEntry|array $entry
  72. * @return array
  73. */
  74. protected function formatCacheEntry($entry) {
  75. if (isset($entry['path'])) {
  76. $entry['path'] = $this->getJailedPath($entry['path']);
  77. }
  78. return $entry;
  79. }
  80. protected function filterCacheEntry($entry) {
  81. $rootLength = strlen($this->root) + 1;
  82. return ($entry['path'] === $this->root) or (substr($entry['path'], 0, $rootLength) === $this->root . '/');
  83. }
  84. /**
  85. * get the stored metadata of a file or folder
  86. *
  87. * @param string /int $file
  88. * @return array|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. if ($file === '') {
  134. return -1;
  135. } else {
  136. return $this->getCache()->getParentId($this->getSourcePath($file));
  137. }
  138. }
  139. /**
  140. * check if a file is available in the cache
  141. *
  142. * @param string $file
  143. * @return bool
  144. */
  145. public function inCache($file) {
  146. return $this->getCache()->inCache($this->getSourcePath($file));
  147. }
  148. /**
  149. * remove a file or folder from the cache
  150. *
  151. * @param string $file
  152. */
  153. public function remove($file) {
  154. $this->getCache()->remove($this->getSourcePath($file));
  155. }
  156. /**
  157. * Move a file or folder in the cache
  158. *
  159. * @param string $source
  160. * @param string $target
  161. */
  162. public function move($source, $target) {
  163. $this->getCache()->move($this->getSourcePath($source), $this->getSourcePath($target));
  164. }
  165. /**
  166. * remove all entries for files that are stored on the storage from the cache
  167. */
  168. public function clear() {
  169. $this->getCache()->remove($this->root);
  170. }
  171. /**
  172. * @param string $file
  173. *
  174. * @return int Cache::NOT_FOUND, Cache::PARTIAL, Cache::SHALLOW or Cache::COMPLETE
  175. */
  176. public function getStatus($file) {
  177. return $this->getCache()->getStatus($this->getSourcePath($file));
  178. }
  179. private function formatSearchResults($results) {
  180. $results = array_filter($results, array($this, 'filterCacheEntry'));
  181. $results = array_values($results);
  182. return array_map(array($this, 'formatCacheEntry'), $results);
  183. }
  184. /**
  185. * search for files matching $pattern
  186. *
  187. * @param string $pattern
  188. * @return array an array of file data
  189. */
  190. public function search($pattern) {
  191. $results = $this->getCache()->search($pattern);
  192. return $this->formatSearchResults($results);
  193. }
  194. /**
  195. * search for files by mimetype
  196. *
  197. * @param string $mimetype
  198. * @return array
  199. */
  200. public function searchByMime($mimetype) {
  201. $results = $this->getCache()->searchByMime($mimetype);
  202. return $this->formatSearchResults($results);
  203. }
  204. /**
  205. * search for files by mimetype
  206. *
  207. * @param string|int $tag name or tag id
  208. * @param string $userId owner of the tags
  209. * @return array
  210. */
  211. public function searchByTag($tag, $userId) {
  212. $results = $this->getCache()->searchByTag($tag, $userId);
  213. return $this->formatSearchResults($results);
  214. }
  215. /**
  216. * update the folder size and the size of all parent folders
  217. *
  218. * @param string|boolean $path
  219. * @param array $data (optional) meta data of the folder
  220. */
  221. public function correctFolderSize($path, $data = null) {
  222. if ($this->getCache() instanceof Cache) {
  223. $this->getCache()->correctFolderSize($this->getSourcePath($path), $data);
  224. }
  225. }
  226. /**
  227. * get the size of a folder and set it in the cache
  228. *
  229. * @param string $path
  230. * @param array $entry (optional) meta data of the folder
  231. * @return int
  232. */
  233. public function calculateFolderSize($path, $entry = null) {
  234. if ($this->getCache() instanceof Cache) {
  235. return $this->getCache()->calculateFolderSize($this->getSourcePath($path), $entry);
  236. } else {
  237. return 0;
  238. }
  239. }
  240. /**
  241. * get all file ids on the files on the storage
  242. *
  243. * @return int[]
  244. */
  245. public function getAll() {
  246. // not supported
  247. return array();
  248. }
  249. /**
  250. * find a folder in the cache which has not been fully scanned
  251. *
  252. * If multiply incomplete folders are in the cache, the one with the highest id will be returned,
  253. * use the one with the highest id gives the best result with the background scanner, since that is most
  254. * likely the folder where we stopped scanning previously
  255. *
  256. * @return string|bool the path of the folder or false when no folder matched
  257. */
  258. public function getIncomplete() {
  259. // not supported
  260. return false;
  261. }
  262. /**
  263. * get the path of a file on this storage by it's id
  264. *
  265. * @param int $id
  266. * @return string|null
  267. */
  268. public function getPathById($id) {
  269. $path = $this->getCache()->getPathById($id);
  270. return $this->getJailedPath($path);
  271. }
  272. /**
  273. * Move a file or folder in the cache
  274. *
  275. * Note that this should make sure the entries are removed from the source cache
  276. *
  277. * @param \OCP\Files\Cache\ICache $sourceCache
  278. * @param string $sourcePath
  279. * @param string $targetPath
  280. */
  281. public function moveFromCache(\OCP\Files\Cache\ICache $sourceCache, $sourcePath, $targetPath) {
  282. if ($sourceCache === $this) {
  283. return $this->move($sourcePath, $targetPath);
  284. }
  285. return $this->getCache()->moveFromCache($sourceCache, $sourcePath, $this->getSourcePath($targetPath));
  286. }
  287. }