cachejail.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 Vincent Petry <pvince81@owncloud.com>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OC\Files\Cache\Wrapper;
  27. use OC\Files\Cache\Cache;
  28. /**
  29. * Jail to a subdirectory of the wrapped cache
  30. */
  31. class CacheJail extends CacheWrapper {
  32. /**
  33. * @var string
  34. */
  35. protected $root;
  36. /**
  37. * @param \OCP\Files\Cache\ICache $cache
  38. * @param string $root
  39. */
  40. public function __construct($cache, $root) {
  41. parent::__construct($cache);
  42. $this->root = $root;
  43. }
  44. protected function getSourcePath($path) {
  45. if ($path === '') {
  46. return $this->root;
  47. } else {
  48. return $this->root . '/' . ltrim($path, '/');
  49. }
  50. }
  51. /**
  52. * @param string $path
  53. * @return null|string the jailed path or null if the path is outside the jail
  54. */
  55. protected function getJailedPath($path) {
  56. if ($this->root === '') {
  57. return $path;
  58. }
  59. $rootLength = strlen($this->root) + 1;
  60. if ($path === $this->root) {
  61. return '';
  62. } else if (substr($path, 0, $rootLength) === $this->root . '/') {
  63. return substr($path, $rootLength);
  64. } else {
  65. return null;
  66. }
  67. }
  68. /**
  69. * @param array $entry
  70. * @return array
  71. */
  72. protected function formatCacheEntry($entry) {
  73. if (isset($entry['path'])) {
  74. $entry['path'] = $this->getJailedPath($entry['path']);
  75. }
  76. return $entry;
  77. }
  78. protected function filterCacheEntry($entry) {
  79. $rootLength = strlen($this->root) + 1;
  80. return ($entry['path'] === $this->root) or (substr($entry['path'], 0, $rootLength) === $this->root . '/');
  81. }
  82. /**
  83. * get the stored metadata of a file or folder
  84. *
  85. * @param string /int $file
  86. * @return array|false
  87. */
  88. public function get($file) {
  89. if (is_string($file) or $file == '') {
  90. $file = $this->getSourcePath($file);
  91. }
  92. return parent::get($file);
  93. }
  94. /**
  95. * insert meta data for a new file or folder
  96. *
  97. * @param string $file
  98. * @param array $data
  99. *
  100. * @return int file id
  101. * @throws \RuntimeException
  102. */
  103. public function insert($file, array $data) {
  104. return $this->cache->insert($this->getSourcePath($file), $data);
  105. }
  106. /**
  107. * update the metadata in the cache
  108. *
  109. * @param int $id
  110. * @param array $data
  111. */
  112. public function update($id, array $data) {
  113. $this->cache->update($id, $data);
  114. }
  115. /**
  116. * get the file id for a file
  117. *
  118. * @param string $file
  119. * @return int
  120. */
  121. public function getId($file) {
  122. return $this->cache->getId($this->getSourcePath($file));
  123. }
  124. /**
  125. * get the id of the parent folder of a file
  126. *
  127. * @param string $file
  128. * @return int
  129. */
  130. public function getParentId($file) {
  131. if ($file === '') {
  132. return -1;
  133. } else {
  134. return $this->cache->getParentId($this->getSourcePath($file));
  135. }
  136. }
  137. /**
  138. * check if a file is available in the cache
  139. *
  140. * @param string $file
  141. * @return bool
  142. */
  143. public function inCache($file) {
  144. return $this->cache->inCache($this->getSourcePath($file));
  145. }
  146. /**
  147. * remove a file or folder from the cache
  148. *
  149. * @param string $file
  150. */
  151. public function remove($file) {
  152. $this->cache->remove($this->getSourcePath($file));
  153. }
  154. /**
  155. * Move a file or folder in the cache
  156. *
  157. * @param string $source
  158. * @param string $target
  159. */
  160. public function move($source, $target) {
  161. $this->cache->move($this->getSourcePath($source), $this->getSourcePath($target));
  162. }
  163. /**
  164. * remove all entries for files that are stored on the storage from the cache
  165. */
  166. public function clear() {
  167. $this->cache->remove($this->root);
  168. }
  169. /**
  170. * @param string $file
  171. *
  172. * @return int Cache::NOT_FOUND, Cache::PARTIAL, Cache::SHALLOW or Cache::COMPLETE
  173. */
  174. public function getStatus($file) {
  175. return $this->cache->getStatus($this->getSourcePath($file));
  176. }
  177. private function formatSearchResults($results) {
  178. $results = array_filter($results, array($this, 'filterCacheEntry'));
  179. $results = array_values($results);
  180. return array_map(array($this, 'formatCacheEntry'), $results);
  181. }
  182. /**
  183. * search for files matching $pattern
  184. *
  185. * @param string $pattern
  186. * @return array an array of file data
  187. */
  188. public function search($pattern) {
  189. $results = $this->cache->search($pattern);
  190. return $this->formatSearchResults($results);
  191. }
  192. /**
  193. * search for files by mimetype
  194. *
  195. * @param string $mimetype
  196. * @return array
  197. */
  198. public function searchByMime($mimetype) {
  199. $results = $this->cache->searchByMime($mimetype);
  200. return $this->formatSearchResults($results);
  201. }
  202. /**
  203. * search for files by mimetype
  204. *
  205. * @param string|int $tag name or tag id
  206. * @param string $userId owner of the tags
  207. * @return array
  208. */
  209. public function searchByTag($tag, $userId) {
  210. $results = $this->cache->searchByTag($tag, $userId);
  211. return $this->formatSearchResults($results);
  212. }
  213. /**
  214. * update the folder size and the size of all parent folders
  215. *
  216. * @param string|boolean $path
  217. * @param array $data (optional) meta data of the folder
  218. */
  219. public function correctFolderSize($path, $data = null) {
  220. if ($this->cache instanceof Cache) {
  221. $this->cache->correctFolderSize($this->getSourcePath($path), $data);
  222. }
  223. }
  224. /**
  225. * get the size of a folder and set it in the cache
  226. *
  227. * @param string $path
  228. * @param array $entry (optional) meta data of the folder
  229. * @return int
  230. */
  231. public function calculateFolderSize($path, $entry = null) {
  232. if ($this->cache instanceof Cache) {
  233. return $this->cache->calculateFolderSize($this->getSourcePath($path), $entry);
  234. } else {
  235. return 0;
  236. }
  237. }
  238. /**
  239. * get all file ids on the files on the storage
  240. *
  241. * @return int[]
  242. */
  243. public function getAll() {
  244. // not supported
  245. return array();
  246. }
  247. /**
  248. * find a folder in the cache which has not been fully scanned
  249. *
  250. * If multiply incomplete folders are in the cache, the one with the highest id will be returned,
  251. * use the one with the highest id gives the best result with the background scanner, since that is most
  252. * likely the folder where we stopped scanning previously
  253. *
  254. * @return string|bool the path of the folder or false when no folder matched
  255. */
  256. public function getIncomplete() {
  257. // not supported
  258. return false;
  259. }
  260. /**
  261. * get the path of a file on this storage by it's id
  262. *
  263. * @param int $id
  264. * @return string|null
  265. */
  266. public function getPathById($id) {
  267. $path = $this->cache->getPathById($id);
  268. return $this->getJailedPath($path);
  269. }
  270. }