cache.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Andreas Fischer <bantu@owncloud.com>
  6. * @author Björn Schießle <bjoern@schiessle.org>
  7. * @author Florin Peter <github@florin-peter.de>
  8. * @author Jens-Christian Fischer <jens-christian.fischer@switch.ch>
  9. * @author Joas Schilling <coding@schilljs.com>
  10. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  11. * @author Lukas Reschke <lukas@statuscode.ch>
  12. * @author Michael Gapczynski <GapczynskiM@gmail.com>
  13. * @author Morris Jobke <hey@morrisjobke.de>
  14. * @author Robin Appelman <robin@icewind.nl>
  15. * @author Robin McCorkell <robin@mccorkell.me.uk>
  16. * @author Roeland Jago Douma <roeland@famdouma.nl>
  17. * @author TheSFReader <TheSFReader@gmail.com>
  18. * @author Thomas Müller <thomas.mueller@tmit.eu>
  19. * @author Vincent Petry <pvince81@owncloud.com>
  20. *
  21. * @license AGPL-3.0
  22. *
  23. * This code is free software: you can redistribute it and/or modify
  24. * it under the terms of the GNU Affero General Public License, version 3,
  25. * as published by the Free Software Foundation.
  26. *
  27. * This program is distributed in the hope that it will be useful,
  28. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30. * GNU Affero General Public License for more details.
  31. *
  32. * You should have received a copy of the GNU Affero General Public License, version 3,
  33. * along with this program. If not, see <http://www.gnu.org/licenses/>
  34. *
  35. */
  36. namespace OC\Files\Cache;
  37. use OCP\Files\Cache\ICache;
  38. use OCP\Files\Cache\ICacheEntry;
  39. use \OCP\Files\IMimeTypeLoader;
  40. use OCP\IDBConnection;
  41. /**
  42. * Metadata cache for a storage
  43. *
  44. * The cache stores the metadata for all files and folders in a storage and is kept up to date trough the following mechanisms:
  45. *
  46. * - Scanner: scans the storage and updates the cache where needed
  47. * - Watcher: checks for changes made to the filesystem outside of the ownCloud instance and rescans files and folder when a change is detected
  48. * - Updater: listens to changes made to the filesystem inside of the ownCloud instance and updates the cache where needed
  49. * - ChangePropagator: updates the mtime and etags of parent folders whenever a change to the cache is made to the cache by the updater
  50. */
  51. class Cache implements ICache {
  52. use MoveFromCacheTrait {
  53. MoveFromCacheTrait::moveFromCache as moveFromCacheFallback;
  54. }
  55. /**
  56. * @var array partial data for the cache
  57. */
  58. protected $partial = array();
  59. /**
  60. * @var string
  61. */
  62. protected $storageId;
  63. /**
  64. * @var Storage $storageCache
  65. */
  66. protected $storageCache;
  67. /** @var IMimeTypeLoader */
  68. protected $mimetypeLoader;
  69. /**
  70. * @var IDBConnection
  71. */
  72. protected $connection;
  73. /**
  74. * @param \OC\Files\Storage\Storage|string $storage
  75. */
  76. public function __construct($storage) {
  77. if ($storage instanceof \OC\Files\Storage\Storage) {
  78. $this->storageId = $storage->getId();
  79. } else {
  80. $this->storageId = $storage;
  81. }
  82. if (strlen($this->storageId) > 64) {
  83. $this->storageId = md5($this->storageId);
  84. }
  85. $this->storageCache = new Storage($storage);
  86. $this->mimetypeLoader = \OC::$server->getMimeTypeLoader();
  87. $this->connection = \OC::$server->getDatabaseConnection();
  88. }
  89. /**
  90. * Get the numeric storage id for this cache's storage
  91. *
  92. * @return int
  93. */
  94. public function getNumericStorageId() {
  95. return $this->storageCache->getNumericId();
  96. }
  97. /**
  98. * get the stored metadata of a file or folder
  99. *
  100. * @param string | int $file either the path of a file or folder or the file id for a file or folder
  101. * @return ICacheEntry|false the cache entry as array of false if the file is not found in the cache
  102. */
  103. public function get($file) {
  104. if (is_string($file) or $file == '') {
  105. // normalize file
  106. $file = $this->normalize($file);
  107. $where = 'WHERE `storage` = ? AND `path_hash` = ?';
  108. $params = array($this->getNumericStorageId(), md5($file));
  109. } else { //file id
  110. $where = 'WHERE `fileid` = ?';
  111. $params = array($file);
  112. }
  113. $sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`,
  114. `storage_mtime`, `encrypted`, `etag`, `permissions`, `checksum`
  115. FROM `*PREFIX*filecache` ' . $where;
  116. $result = $this->connection->executeQuery($sql, $params);
  117. $data = $result->fetch();
  118. //FIXME hide this HACK in the next database layer, or just use doctrine and get rid of MDB2 and PDO
  119. //PDO returns false, MDB2 returns null, oracle always uses MDB2, so convert null to false
  120. if ($data === null) {
  121. $data = false;
  122. }
  123. //merge partial data
  124. if (!$data and is_string($file)) {
  125. if (isset($this->partial[$file])) {
  126. $data = $this->partial[$file];
  127. }
  128. return $data;
  129. } else {
  130. //fix types
  131. $data['fileid'] = (int)$data['fileid'];
  132. $data['parent'] = (int)$data['parent'];
  133. $data['size'] = 0 + $data['size'];
  134. $data['mtime'] = (int)$data['mtime'];
  135. $data['storage_mtime'] = (int)$data['storage_mtime'];
  136. $data['encryptedVersion'] = (int)$data['encrypted'];
  137. $data['encrypted'] = (bool)$data['encrypted'];
  138. $data['storage'] = $this->storageId;
  139. $data['mimetype'] = $this->mimetypeLoader->getMimetypeById($data['mimetype']);
  140. $data['mimepart'] = $this->mimetypeLoader->getMimetypeById($data['mimepart']);
  141. if ($data['storage_mtime'] == 0) {
  142. $data['storage_mtime'] = $data['mtime'];
  143. }
  144. $data['permissions'] = (int)$data['permissions'];
  145. return new CacheEntry($data);
  146. }
  147. }
  148. /**
  149. * get the metadata of all files stored in $folder
  150. *
  151. * @param string $folder
  152. * @return ICacheEntry[]
  153. */
  154. public function getFolderContents($folder) {
  155. $fileId = $this->getId($folder);
  156. return $this->getFolderContentsById($fileId);
  157. }
  158. /**
  159. * get the metadata of all files stored in $folder
  160. *
  161. * @param int $fileId the file id of the folder
  162. * @return ICacheEntry[]
  163. */
  164. public function getFolderContentsById($fileId) {
  165. if ($fileId > -1) {
  166. $sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`,
  167. `storage_mtime`, `encrypted`, `etag`, `permissions`, `checksum`
  168. FROM `*PREFIX*filecache` WHERE `parent` = ? ORDER BY `name` ASC';
  169. $result = $this->connection->executeQuery($sql, [$fileId]);
  170. $files = $result->fetchAll();
  171. foreach ($files as &$file) {
  172. $file['mimetype'] = $this->mimetypeLoader->getMimetypeById($file['mimetype']);
  173. $file['mimepart'] = $this->mimetypeLoader->getMimetypeById($file['mimepart']);
  174. if ($file['storage_mtime'] == 0) {
  175. $file['storage_mtime'] = $file['mtime'];
  176. }
  177. $file['permissions'] = (int)$file['permissions'];
  178. $file['mtime'] = (int)$file['mtime'];
  179. $file['storage_mtime'] = (int)$file['storage_mtime'];
  180. $file['size'] = 0 + $file['size'];
  181. }
  182. return array_map(function (array $data) {
  183. return new CacheEntry($data);
  184. }, $files);
  185. } else {
  186. return array();
  187. }
  188. }
  189. /**
  190. * insert or update meta data for a file or folder
  191. *
  192. * @param string $file
  193. * @param array $data
  194. *
  195. * @return int file id
  196. * @throws \RuntimeException
  197. */
  198. public function put($file, array $data) {
  199. if (($id = $this->getId($file)) > -1) {
  200. $this->update($id, $data);
  201. return $id;
  202. } else {
  203. return $this->insert($file, $data);
  204. }
  205. }
  206. /**
  207. * insert meta data for a new file or folder
  208. *
  209. * @param string $file
  210. * @param array $data
  211. *
  212. * @return int file id
  213. * @throws \RuntimeException
  214. */
  215. public function insert($file, array $data) {
  216. // normalize file
  217. $file = $this->normalize($file);
  218. if (isset($this->partial[$file])) { //add any saved partial data
  219. $data = array_merge($this->partial[$file], $data);
  220. unset($this->partial[$file]);
  221. }
  222. $requiredFields = array('size', 'mtime', 'mimetype');
  223. foreach ($requiredFields as $field) {
  224. if (!isset($data[$field])) { //data not complete save as partial and return
  225. $this->partial[$file] = $data;
  226. return -1;
  227. }
  228. }
  229. $data['path'] = $file;
  230. $data['parent'] = $this->getParentId($file);
  231. $data['name'] = \OC_Util::basename($file);
  232. list($queryParts, $params) = $this->buildParts($data);
  233. $queryParts[] = '`storage`';
  234. $params[] = $this->getNumericStorageId();
  235. $queryParts = array_map(function ($item) {
  236. return trim($item, "`");
  237. }, $queryParts);
  238. $values = array_combine($queryParts, $params);
  239. if (\OC::$server->getDatabaseConnection()->insertIfNotExist('*PREFIX*filecache', $values, [
  240. 'storage',
  241. 'path_hash',
  242. ])
  243. ) {
  244. return (int)$this->connection->lastInsertId('*PREFIX*filecache');
  245. }
  246. // The file was created in the mean time
  247. if (($id = $this->getId($file)) > -1) {
  248. $this->update($id, $data);
  249. return $id;
  250. } else {
  251. throw new \RuntimeException('File entry could not be inserted with insertIfNotExist() but could also not be selected with getId() in order to perform an update. Please try again.');
  252. }
  253. }
  254. /**
  255. * update the metadata of an existing file or folder in the cache
  256. *
  257. * @param int $id the fileid of the existing file or folder
  258. * @param array $data [$key => $value] the metadata to update, only the fields provided in the array will be updated, non-provided values will remain unchanged
  259. */
  260. public function update($id, array $data) {
  261. if (isset($data['path'])) {
  262. // normalize path
  263. $data['path'] = $this->normalize($data['path']);
  264. }
  265. if (isset($data['name'])) {
  266. // normalize path
  267. $data['name'] = $this->normalize($data['name']);
  268. }
  269. list($queryParts, $params) = $this->buildParts($data);
  270. // duplicate $params because we need the parts twice in the SQL statement
  271. // once for the SET part, once in the WHERE clause
  272. $params = array_merge($params, $params);
  273. $params[] = $id;
  274. // don't update if the data we try to set is the same as the one in the record
  275. // some databases (Postgres) don't like superfluous updates
  276. $sql = 'UPDATE `*PREFIX*filecache` SET ' . implode(' = ?, ', $queryParts) . '=? ' .
  277. 'WHERE (' .
  278. implode(' <> ? OR ', $queryParts) . ' <> ? OR ' .
  279. implode(' IS NULL OR ', $queryParts) . ' IS NULL' .
  280. ') AND `fileid` = ? ';
  281. $this->connection->executeQuery($sql, $params);
  282. }
  283. /**
  284. * extract query parts and params array from data array
  285. *
  286. * @param array $data
  287. * @return array [$queryParts, $params]
  288. * $queryParts: string[], the (escaped) column names to be set in the query
  289. * $params: mixed[], the new values for the columns, to be passed as params to the query
  290. */
  291. protected function buildParts(array $data) {
  292. $fields = array(
  293. 'path', 'parent', 'name', 'mimetype', 'size', 'mtime', 'storage_mtime', 'encrypted',
  294. 'etag', 'permissions', 'checksum');
  295. $doNotCopyStorageMTime = false;
  296. if (array_key_exists('mtime', $data) && $data['mtime'] === null) {
  297. // this horrific magic tells it to not copy storage_mtime to mtime
  298. unset($data['mtime']);
  299. $doNotCopyStorageMTime = true;
  300. }
  301. $params = array();
  302. $queryParts = array();
  303. foreach ($data as $name => $value) {
  304. if (array_search($name, $fields) !== false) {
  305. if ($name === 'path') {
  306. $params[] = md5($value);
  307. $queryParts[] = '`path_hash`';
  308. } elseif ($name === 'mimetype') {
  309. $params[] = $this->mimetypeLoader->getId(substr($value, 0, strpos($value, '/')));
  310. $queryParts[] = '`mimepart`';
  311. $value = $this->mimetypeLoader->getId($value);
  312. } elseif ($name === 'storage_mtime') {
  313. if (!$doNotCopyStorageMTime && !isset($data['mtime'])) {
  314. $params[] = $value;
  315. $queryParts[] = '`mtime`';
  316. }
  317. } elseif ($name === 'encrypted') {
  318. if(isset($data['encryptedVersion'])) {
  319. $value = $data['encryptedVersion'];
  320. } else {
  321. // Boolean to integer conversion
  322. $value = $value ? 1 : 0;
  323. }
  324. }
  325. $params[] = $value;
  326. $queryParts[] = '`' . $name . '`';
  327. }
  328. }
  329. return array($queryParts, $params);
  330. }
  331. /**
  332. * get the file id for a file
  333. *
  334. * A file id is a numeric id for a file or folder that's unique within an owncloud instance which stays the same for the lifetime of a file
  335. *
  336. * File ids are easiest way for apps to store references to a file since unlike paths they are not affected by renames or sharing
  337. *
  338. * @param string $file
  339. * @return int
  340. */
  341. public function getId($file) {
  342. // normalize file
  343. $file = $this->normalize($file);
  344. $pathHash = md5($file);
  345. $sql = 'SELECT `fileid` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path_hash` = ?';
  346. $result = $this->connection->executeQuery($sql, array($this->getNumericStorageId(), $pathHash));
  347. if ($row = $result->fetch()) {
  348. return $row['fileid'];
  349. } else {
  350. return -1;
  351. }
  352. }
  353. /**
  354. * get the id of the parent folder of a file
  355. *
  356. * @param string $file
  357. * @return int
  358. */
  359. public function getParentId($file) {
  360. if ($file === '') {
  361. return -1;
  362. } else {
  363. $parent = $this->getParentPath($file);
  364. return (int)$this->getId($parent);
  365. }
  366. }
  367. private function getParentPath($path) {
  368. $parent = dirname($path);
  369. if ($parent === '.') {
  370. $parent = '';
  371. }
  372. return $parent;
  373. }
  374. /**
  375. * check if a file is available in the cache
  376. *
  377. * @param string $file
  378. * @return bool
  379. */
  380. public function inCache($file) {
  381. return $this->getId($file) != -1;
  382. }
  383. /**
  384. * remove a file or folder from the cache
  385. *
  386. * when removing a folder from the cache all files and folders inside the folder will be removed as well
  387. *
  388. * @param string $file
  389. */
  390. public function remove($file) {
  391. $entry = $this->get($file);
  392. $sql = 'DELETE FROM `*PREFIX*filecache` WHERE `fileid` = ?';
  393. $this->connection->executeQuery($sql, array($entry['fileid']));
  394. if ($entry['mimetype'] === 'httpd/unix-directory') {
  395. $this->removeChildren($entry);
  396. }
  397. }
  398. /**
  399. * Get all sub folders of a folder
  400. *
  401. * @param array $entry the cache entry of the folder to get the subfolders for
  402. * @return array[] the cache entries for the subfolders
  403. */
  404. private function getSubFolders($entry) {
  405. $children = $this->getFolderContentsById($entry['fileid']);
  406. return array_filter($children, function ($child) {
  407. return $child['mimetype'] === 'httpd/unix-directory';
  408. });
  409. }
  410. /**
  411. * Recursively remove all children of a folder
  412. *
  413. * @param array $entry the cache entry of the folder to remove the children of
  414. * @throws \OC\DatabaseException
  415. */
  416. private function removeChildren($entry) {
  417. $subFolders = $this->getSubFolders($entry);
  418. foreach ($subFolders as $folder) {
  419. $this->removeChildren($folder);
  420. }
  421. $sql = 'DELETE FROM `*PREFIX*filecache` WHERE `parent` = ?';
  422. $this->connection->executeQuery($sql, array($entry['fileid']));
  423. }
  424. /**
  425. * Move a file or folder in the cache
  426. *
  427. * @param string $source
  428. * @param string $target
  429. */
  430. public function move($source, $target) {
  431. $this->moveFromCache($this, $source, $target);
  432. }
  433. /**
  434. * Get the storage id and path needed for a move
  435. *
  436. * @param string $path
  437. * @return array [$storageId, $internalPath]
  438. */
  439. protected function getMoveInfo($path) {
  440. return [$this->getNumericStorageId(), $path];
  441. }
  442. /**
  443. * Move a file or folder in the cache
  444. *
  445. * @param \OCP\Files\Cache\ICache $sourceCache
  446. * @param string $sourcePath
  447. * @param string $targetPath
  448. * @throws \OC\DatabaseException
  449. */
  450. public function moveFromCache(ICache $sourceCache, $sourcePath, $targetPath) {
  451. if ($sourceCache instanceof Cache) {
  452. // normalize source and target
  453. $sourcePath = $this->normalize($sourcePath);
  454. $targetPath = $this->normalize($targetPath);
  455. $sourceData = $sourceCache->get($sourcePath);
  456. $sourceId = $sourceData['fileid'];
  457. $newParentId = $this->getParentId($targetPath);
  458. list($sourceStorageId, $sourcePath) = $sourceCache->getMoveInfo($sourcePath);
  459. list($targetStorageId, $targetPath) = $this->getMoveInfo($targetPath);
  460. // sql for final update
  461. $moveSql = 'UPDATE `*PREFIX*filecache` SET `storage` = ?, `path` = ?, `path_hash` = ?, `name` = ?, `parent` =? WHERE `fileid` = ?';
  462. if ($sourceData['mimetype'] === 'httpd/unix-directory') {
  463. //find all child entries
  464. $sql = 'SELECT `path`, `fileid` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path` LIKE ?';
  465. $result = $this->connection->executeQuery($sql, [$sourceStorageId, $this->connection->escapeLikeParameter($sourcePath) . '/%']);
  466. $childEntries = $result->fetchAll();
  467. $sourceLength = strlen($sourcePath);
  468. $this->connection->beginTransaction();
  469. $query = $this->connection->prepare('UPDATE `*PREFIX*filecache` SET `storage` = ?, `path` = ?, `path_hash` = ? WHERE `fileid` = ?');
  470. foreach ($childEntries as $child) {
  471. $newTargetPath = $targetPath . substr($child['path'], $sourceLength);
  472. $query->execute([$targetStorageId, $newTargetPath, md5($newTargetPath), $child['fileid']]);
  473. }
  474. $this->connection->executeQuery($moveSql, [$targetStorageId, $targetPath, md5($targetPath), basename($targetPath), $newParentId, $sourceId]);
  475. $this->connection->commit();
  476. } else {
  477. $this->connection->executeQuery($moveSql, [$targetStorageId, $targetPath, md5($targetPath), basename($targetPath), $newParentId, $sourceId]);
  478. }
  479. } else {
  480. $this->moveFromCacheFallback($sourceCache, $sourcePath, $targetPath);
  481. }
  482. }
  483. /**
  484. * remove all entries for files that are stored on the storage from the cache
  485. */
  486. public function clear() {
  487. $sql = 'DELETE FROM `*PREFIX*filecache` WHERE `storage` = ?';
  488. $this->connection->executeQuery($sql, array($this->getNumericStorageId()));
  489. $sql = 'DELETE FROM `*PREFIX*storages` WHERE `id` = ?';
  490. $this->connection->executeQuery($sql, array($this->storageId));
  491. }
  492. /**
  493. * Get the scan status of a file
  494. *
  495. * - Cache::NOT_FOUND: File is not in the cache
  496. * - Cache::PARTIAL: File is not stored in the cache but some incomplete data is known
  497. * - Cache::SHALLOW: The folder and it's direct children are in the cache but not all sub folders are fully scanned
  498. * - Cache::COMPLETE: The file or folder, with all it's children) are fully scanned
  499. *
  500. * @param string $file
  501. *
  502. * @return int Cache::NOT_FOUND, Cache::PARTIAL, Cache::SHALLOW or Cache::COMPLETE
  503. */
  504. public function getStatus($file) {
  505. // normalize file
  506. $file = $this->normalize($file);
  507. $pathHash = md5($file);
  508. $sql = 'SELECT `size` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path_hash` = ?';
  509. $result = $this->connection->executeQuery($sql, array($this->getNumericStorageId(), $pathHash));
  510. if ($row = $result->fetch()) {
  511. if ((int)$row['size'] === -1) {
  512. return self::SHALLOW;
  513. } else {
  514. return self::COMPLETE;
  515. }
  516. } else {
  517. if (isset($this->partial[$file])) {
  518. return self::PARTIAL;
  519. } else {
  520. return self::NOT_FOUND;
  521. }
  522. }
  523. }
  524. /**
  525. * search for files matching $pattern
  526. *
  527. * @param string $pattern the search pattern using SQL search syntax (e.g. '%searchstring%')
  528. * @return ICacheEntry[] an array of cache entries where the name matches the search pattern
  529. */
  530. public function search($pattern) {
  531. // normalize pattern
  532. $pattern = $this->normalize($pattern);
  533. $sql = '
  534. SELECT `fileid`, `storage`, `path`, `parent`, `name`,
  535. `mimetype`, `mimepart`, `size`, `mtime`, `encrypted`,
  536. `etag`, `permissions`, `checksum`
  537. FROM `*PREFIX*filecache`
  538. WHERE `storage` = ? AND `name` ILIKE ?';
  539. $result = $this->connection->executeQuery($sql,
  540. [$this->getNumericStorageId(), $pattern]
  541. );
  542. $files = [];
  543. while ($row = $result->fetch()) {
  544. $row['mimetype'] = $this->mimetypeLoader->getMimetypeById($row['mimetype']);
  545. $row['mimepart'] = $this->mimetypeLoader->getMimetypeById($row['mimepart']);
  546. $files[] = $row;
  547. }
  548. return array_map(function(array $data) {
  549. return new CacheEntry($data);
  550. }, $files);
  551. }
  552. /**
  553. * search for files by mimetype
  554. *
  555. * @param string $mimetype either a full mimetype to search ('text/plain') or only the first part of a mimetype ('image')
  556. * where it will search for all mimetypes in the group ('image/*')
  557. * @return ICacheEntry[] an array of cache entries where the mimetype matches the search
  558. */
  559. public function searchByMime($mimetype) {
  560. if (strpos($mimetype, '/')) {
  561. $where = '`mimetype` = ?';
  562. } else {
  563. $where = '`mimepart` = ?';
  564. }
  565. $sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`, `encrypted`, `etag`, `permissions`, `checksum`
  566. FROM `*PREFIX*filecache` WHERE ' . $where . ' AND `storage` = ?';
  567. $mimetype = $this->mimetypeLoader->getId($mimetype);
  568. $result = $this->connection->executeQuery($sql, array($mimetype, $this->getNumericStorageId()));
  569. $files = array();
  570. while ($row = $result->fetch()) {
  571. $row['mimetype'] = $this->mimetypeLoader->getMimetypeById($row['mimetype']);
  572. $row['mimepart'] = $this->mimetypeLoader->getMimetypeById($row['mimepart']);
  573. $files[] = $row;
  574. }
  575. return array_map(function (array $data) {
  576. return new CacheEntry($data);
  577. }, $files);
  578. }
  579. /**
  580. * Search for files by tag of a given users.
  581. *
  582. * Note that every user can tag files differently.
  583. *
  584. * @param string|int $tag name or tag id
  585. * @param string $userId owner of the tags
  586. * @return ICacheEntry[] file data
  587. */
  588. public function searchByTag($tag, $userId) {
  589. $sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, ' .
  590. '`mimetype`, `mimepart`, `size`, `mtime`, ' .
  591. '`encrypted`, `etag`, `permissions`, `checksum` ' .
  592. 'FROM `*PREFIX*filecache` `file`, ' .
  593. '`*PREFIX*vcategory_to_object` `tagmap`, ' .
  594. '`*PREFIX*vcategory` `tag` ' .
  595. // JOIN filecache to vcategory_to_object
  596. 'WHERE `file`.`fileid` = `tagmap`.`objid` ' .
  597. // JOIN vcategory_to_object to vcategory
  598. 'AND `tagmap`.`type` = `tag`.`type` ' .
  599. 'AND `tagmap`.`categoryid` = `tag`.`id` ' .
  600. // conditions
  601. 'AND `file`.`storage` = ? ' .
  602. 'AND `tag`.`type` = \'files\' ' .
  603. 'AND `tag`.`uid` = ? ';
  604. if (is_int($tag)) {
  605. $sql .= 'AND `tag`.`id` = ? ';
  606. } else {
  607. $sql .= 'AND `tag`.`category` = ? ';
  608. }
  609. $result = $this->connection->executeQuery(
  610. $sql,
  611. [
  612. $this->getNumericStorageId(),
  613. $userId,
  614. $tag
  615. ]
  616. );
  617. $files = array();
  618. while ($row = $result->fetch()) {
  619. $files[] = $row;
  620. }
  621. return array_map(function (array $data) {
  622. return new CacheEntry($data);
  623. }, $files);
  624. }
  625. /**
  626. * Re-calculate the folder size and the size of all parent folders
  627. *
  628. * @param string|boolean $path
  629. * @param array $data (optional) meta data of the folder
  630. */
  631. public function correctFolderSize($path, $data = null) {
  632. $this->calculateFolderSize($path, $data);
  633. if ($path !== '') {
  634. $parent = dirname($path);
  635. if ($parent === '.' or $parent === '/') {
  636. $parent = '';
  637. }
  638. $this->correctFolderSize($parent);
  639. }
  640. }
  641. /**
  642. * calculate the size of a folder and set it in the cache
  643. *
  644. * @param string $path
  645. * @param array $entry (optional) meta data of the folder
  646. * @return int
  647. */
  648. public function calculateFolderSize($path, $entry = null) {
  649. $totalSize = 0;
  650. if (is_null($entry) or !isset($entry['fileid'])) {
  651. $entry = $this->get($path);
  652. }
  653. if (isset($entry['mimetype']) && $entry['mimetype'] === 'httpd/unix-directory') {
  654. $id = $entry['fileid'];
  655. $sql = 'SELECT SUM(`size`) AS f1, MIN(`size`) AS f2 ' .
  656. 'FROM `*PREFIX*filecache` ' .
  657. 'WHERE `parent` = ? AND `storage` = ?';
  658. $result = $this->connection->executeQuery($sql, array($id, $this->getNumericStorageId()));
  659. if ($row = $result->fetch()) {
  660. $result->closeCursor();
  661. list($sum, $min) = array_values($row);
  662. $sum = 0 + $sum;
  663. $min = 0 + $min;
  664. if ($min === -1) {
  665. $totalSize = $min;
  666. } else {
  667. $totalSize = $sum;
  668. }
  669. $update = array();
  670. if ($entry['size'] !== $totalSize) {
  671. $update['size'] = $totalSize;
  672. }
  673. if (count($update) > 0) {
  674. $this->update($id, $update);
  675. }
  676. } else {
  677. $result->closeCursor();
  678. }
  679. }
  680. return $totalSize;
  681. }
  682. /**
  683. * get all file ids on the files on the storage
  684. *
  685. * @return int[]
  686. */
  687. public function getAll() {
  688. $sql = 'SELECT `fileid` FROM `*PREFIX*filecache` WHERE `storage` = ?';
  689. $result = $this->connection->executeQuery($sql, array($this->getNumericStorageId()));
  690. $ids = array();
  691. while ($row = $result->fetch()) {
  692. $ids[] = $row['fileid'];
  693. }
  694. return $ids;
  695. }
  696. /**
  697. * find a folder in the cache which has not been fully scanned
  698. *
  699. * If multiple incomplete folders are in the cache, the one with the highest id will be returned,
  700. * use the one with the highest id gives the best result with the background scanner, since that is most
  701. * likely the folder where we stopped scanning previously
  702. *
  703. * @return string|bool the path of the folder or false when no folder matched
  704. */
  705. public function getIncomplete() {
  706. $query = $this->connection->prepare('SELECT `path` FROM `*PREFIX*filecache`'
  707. . ' WHERE `storage` = ? AND `size` = -1 ORDER BY `fileid` DESC', 1);
  708. $query->execute([$this->getNumericStorageId()]);
  709. if ($row = $query->fetch()) {
  710. return $row['path'];
  711. } else {
  712. return false;
  713. }
  714. }
  715. /**
  716. * get the path of a file on this storage by it's file id
  717. *
  718. * @param int $id the file id of the file or folder to search
  719. * @return string|null the path of the file (relative to the storage) or null if a file with the given id does not exists within this cache
  720. */
  721. public function getPathById($id) {
  722. $sql = 'SELECT `path` FROM `*PREFIX*filecache` WHERE `fileid` = ? AND `storage` = ?';
  723. $result = $this->connection->executeQuery($sql, array($id, $this->getNumericStorageId()));
  724. if ($row = $result->fetch()) {
  725. // Oracle stores empty strings as null...
  726. if ($row['path'] === null) {
  727. return '';
  728. }
  729. return $row['path'];
  730. } else {
  731. return null;
  732. }
  733. }
  734. /**
  735. * get the storage id of the storage for a file and the internal path of the file
  736. * unlike getPathById this does not limit the search to files on this storage and
  737. * instead does a global search in the cache table
  738. *
  739. * @param int $id
  740. * @deprecated use getPathById() instead
  741. * @return array first element holding the storage id, second the path
  742. */
  743. static public function getById($id) {
  744. $connection = \OC::$server->getDatabaseConnection();
  745. $sql = 'SELECT `storage`, `path` FROM `*PREFIX*filecache` WHERE `fileid` = ?';
  746. $result = $connection->executeQuery($sql, array($id));
  747. if ($row = $result->fetch()) {
  748. $numericId = $row['storage'];
  749. $path = $row['path'];
  750. } else {
  751. return null;
  752. }
  753. if ($id = Storage::getStorageId($numericId)) {
  754. return array($id, $path);
  755. } else {
  756. return null;
  757. }
  758. }
  759. /**
  760. * normalize the given path
  761. *
  762. * @param string $path
  763. * @return string
  764. */
  765. public function normalize($path) {
  766. return trim(\OC_Util::normalizeUnicode($path), '/');
  767. }
  768. }