cache.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Michael Gapczynski
  6. * @copyright 2012 Michael Gapczynski mtgap@owncloud.com
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. namespace OC\Files\Cache;
  22. use OCP\Share_Backend_Collection;
  23. /**
  24. * Metadata cache for shared files
  25. *
  26. * don't use this class directly if you need to get metadata, use \OC\Files\Filesystem::getFileInfo instead
  27. */
  28. class Shared_Cache extends Cache {
  29. private $storage;
  30. private $files = array();
  31. public function __construct($storage) {
  32. $this->storage = $storage;
  33. }
  34. /**
  35. * @brief Get the source cache of a shared file or folder
  36. * @param string $target Shared target file path
  37. * @return \OC\Files\Cache\Cache
  38. */
  39. private function getSourceCache($target) {
  40. $source = \OC_Share_Backend_File::getSource($target);
  41. if (isset($source['path']) && isset($source['fileOwner'])) {
  42. \OC\Files\Filesystem::initMountPoints($source['fileOwner']);
  43. $mount = \OC\Files\Filesystem::getMountByNumericId($source['storage']);
  44. if (is_array($mount)) {
  45. $fullPath = $mount[key($mount)]->getMountPoint().$source['path'];
  46. list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($fullPath);
  47. if ($storage) {
  48. $this->files[$target] = $internalPath;
  49. $cache = $storage->getCache();
  50. $this->storageId = $storage->getId();
  51. $this->numericId = $cache->getNumericStorageId();
  52. return $cache;
  53. }
  54. }
  55. }
  56. return false;
  57. }
  58. public function getNumericStorageId() {
  59. if (isset($this->numericId)) {
  60. return $this->numericId;
  61. } else {
  62. return false;
  63. }
  64. }
  65. /**
  66. * get the stored metadata of a file or folder
  67. *
  68. * @param string/int $file
  69. * @return array
  70. */
  71. public function get($file) {
  72. if ($file == '') {
  73. $data = \OCP\Share::getItemsSharedWith('file', \OC_Share_Backend_File::FORMAT_FILE_APP_ROOT);
  74. $etag = \OCP\Config::getUserValue(\OCP\User::getUser(), 'files_sharing', 'etag');
  75. if (!isset($etag)) {
  76. $etag = $this->storage->getETag('');
  77. \OCP\Config::setUserValue(\OCP\User::getUser(), 'files_sharing', 'etag', $etag);
  78. }
  79. $data['etag'] = $etag;
  80. return $data;
  81. } else if (is_string($file)) {
  82. if ($cache = $this->getSourceCache($file)) {
  83. return $cache->get($this->files[$file]);
  84. }
  85. } else {
  86. $query = \OC_DB::prepare(
  87. 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`,'
  88. .' `size`, `mtime`, `encrypted`'
  89. .' FROM `*PREFIX*filecache` WHERE `fileid` = ?');
  90. $result = $query->execute(array($file));
  91. $data = $result->fetchRow();
  92. $data['fileid'] = (int)$data['fileid'];
  93. $data['size'] = (int)$data['size'];
  94. $data['mtime'] = (int)$data['mtime'];
  95. $data['storage_mtime'] = (int)$data['storage_mtime'];
  96. $data['encrypted'] = (bool)$data['encrypted'];
  97. $data['mimetype'] = $this->getMimetype($data['mimetype']);
  98. $data['mimepart'] = $this->getMimetype($data['mimepart']);
  99. if ($data['storage_mtime'] === 0) {
  100. $data['storage_mtime'] = $data['mtime'];
  101. }
  102. return $data;
  103. }
  104. return false;
  105. }
  106. /**
  107. * get the metadata of all files stored in $folder
  108. *
  109. * @param string $folder
  110. * @return array
  111. */
  112. public function getFolderContents($folder) {
  113. if ($folder == '') {
  114. $files = \OCP\Share::getItemsSharedWith('file', \OC_Share_Backend_File::FORMAT_GET_FOLDER_CONTENTS);
  115. foreach ($files as &$file) {
  116. $file['mimetype'] = $this->getMimetype($file['mimetype']);
  117. $file['mimepart'] = $this->getMimetype($file['mimepart']);
  118. }
  119. return $files;
  120. } else {
  121. if ($cache = $this->getSourceCache($folder)) {
  122. return $cache->getFolderContents($this->files[$folder]);
  123. }
  124. }
  125. return false;
  126. }
  127. /**
  128. * store meta data for a file or folder
  129. *
  130. * @param string $file
  131. * @param array $data
  132. *
  133. * @return int file id
  134. */
  135. public function put($file, array $data) {
  136. if ($file === '' && isset($data['etag'])) {
  137. return \OCP\Config::setUserValue(\OCP\User::getUser(), 'files_sharing', 'etag', $data['etag']);
  138. } else if ($cache = $this->getSourceCache($file)) {
  139. return $cache->put($this->files[$file], $data);
  140. }
  141. return false;
  142. }
  143. /**
  144. * get the file id for a file
  145. *
  146. * @param string $file
  147. * @return int
  148. */
  149. public function getId($file) {
  150. if ($cache = $this->getSourceCache($file)) {
  151. return $cache->getId($this->files[$file]);
  152. }
  153. return -1;
  154. }
  155. /**
  156. * check if a file is available in the cache
  157. *
  158. * @param string $file
  159. * @return bool
  160. */
  161. public function inCache($file) {
  162. if ($file == '') {
  163. return true;
  164. }
  165. return parent::inCache($file);
  166. }
  167. /**
  168. * remove a file or folder from the cache
  169. *
  170. * @param string $file
  171. */
  172. public function remove($file) {
  173. if ($cache = $this->getSourceCache($file)) {
  174. $cache->remove($this->files[$file]);
  175. }
  176. }
  177. /**
  178. * Move a file or folder in the cache
  179. *
  180. * @param string $source
  181. * @param string $target
  182. */
  183. public function move($source, $target) {
  184. if ($cache = $this->getSourceCache($source)) {
  185. $file = \OC_Share_Backend_File::getSource($target);
  186. if ($file && isset($file['path'])) {
  187. $cache->move($this->files[$source], $file['path']);
  188. }
  189. }
  190. }
  191. /**
  192. * remove all entries for files that are stored on the storage from the cache
  193. */
  194. public function clear() {
  195. // Not a valid action for Shared Cache
  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. if ($file == '') {
  204. return self::COMPLETE;
  205. }
  206. if ($cache = $this->getSourceCache($file)) {
  207. return $cache->getStatus($this->files[$file]);
  208. }
  209. return self::NOT_FOUND;
  210. }
  211. /**
  212. * search for files matching $pattern
  213. *
  214. * @param string $pattern
  215. * @return array of file data
  216. */
  217. public function search($pattern) {
  218. $where = '`name` LIKE ? AND ';
  219. // normalize pattern
  220. $value = $this->normalize($pattern);
  221. return $this->searchWithWhere($where, $value);
  222. }
  223. /**
  224. * search for files by mimetype
  225. *
  226. * @param string $mimetype
  227. * @return array
  228. */
  229. public function searchByMime($mimetype) {
  230. if (strpos($mimetype, '/')) {
  231. $where = '`mimetype` = ? AND ';
  232. } else {
  233. $where = '`mimepart` = ? AND ';
  234. }
  235. $value = $this->getMimetypeId($mimetype);
  236. return $this->searchWithWhere($where, $value);
  237. }
  238. /**
  239. * The maximum number of placeholders that can be used in an SQL query.
  240. * Value MUST be <= 1000 for oracle:
  241. * see ORA-01795 maximum number of expressions in a list is 1000
  242. * FIXME we should get this from doctrine as other DBs allow a lot more placeholders
  243. */
  244. const MAX_SQL_CHUNK_SIZE = 1000;
  245. /**
  246. * search for files with a custom where clause and value
  247. * the $wherevalue will be array_merge()d with the file id chunks
  248. *
  249. * @param string $sqlwhere
  250. * @param string $wherevalue
  251. * @return array
  252. */
  253. private function searchWithWhere($sqlwhere, $wherevalue, $chunksize = self::MAX_SQL_CHUNK_SIZE) {
  254. $ids = $this->getAll();
  255. $files = array();
  256. // divide into chunks
  257. $chunks = array_chunk($ids, $chunksize);
  258. foreach ($chunks as $chunk) {
  259. $placeholders = join(',', array_fill(0, count($chunk), '?'));
  260. $sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`,
  261. `encrypted`, `unencrypted_size`, `etag`
  262. FROM `*PREFIX*filecache` WHERE ' . $sqlwhere . ' `fileid` IN (' . $placeholders . ')';
  263. $stmt = \OC_DB::prepare($sql);
  264. $result = $stmt->execute(array_merge(array($wherevalue), $chunk));
  265. while ($row = $result->fetchRow()) {
  266. if (substr($row['path'], 0, 6) === 'files/') {
  267. $row['path'] = substr($row['path'], 6); // remove 'files/' from path as it's relative to '/Shared'
  268. }
  269. $row['mimetype'] = $this->getMimetype($row['mimetype']);
  270. $row['mimepart'] = $this->getMimetype($row['mimepart']);
  271. $files[] = $row;
  272. }
  273. }
  274. return $files;
  275. }
  276. /**
  277. * get the size of a folder and set it in the cache
  278. *
  279. * @param string $path
  280. * @return int
  281. */
  282. public function calculateFolderSize($path) {
  283. if ($cache = $this->getSourceCache($path)) {
  284. return $cache->calculateFolderSize($this->files[$path]);
  285. }
  286. return 0;
  287. }
  288. /**
  289. * get all file ids on the files on the storage
  290. *
  291. * @return int[]
  292. */
  293. public function getAll() {
  294. $ids = \OCP\Share::getItemsSharedWith('file', \OC_Share_Backend_File::FORMAT_GET_ALL);
  295. $folderBackend = \OCP\Share::getBackend('folder');
  296. if ($folderBackend instanceof Share_Backend_Collection) {
  297. foreach ($ids as $file) {
  298. /** @var $folderBackend Share_Backend_Collection */
  299. $children = $folderBackend->getChildren($file);
  300. foreach ($children as $child) {
  301. $ids[] = (int)$child['source'];
  302. }
  303. }
  304. }
  305. return $ids;
  306. }
  307. /**
  308. * find a folder in the cache which has not been fully scanned
  309. *
  310. * If multiply incomplete folders are in the cache, the one with the highest id will be returned,
  311. * use the one with the highest id gives the best result with the background scanner, since that is most
  312. * likely the folder where we stopped scanning previously
  313. *
  314. * @return string|bool the path of the folder or false when no folder matched
  315. */
  316. public function getIncomplete() {
  317. return false;
  318. }
  319. }