1
0

CacheJail.php 8.5 KB

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