ObjectStoreScanner.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  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\ObjectStore;
  28. use OC\Files\Cache\Scanner;
  29. use OCP\DB\QueryBuilder\IQueryBuilder;
  30. use OCP\Files\FileInfo;
  31. class ObjectStoreScanner extends Scanner {
  32. public function scanFile($file, $reuseExisting = 0, $parentId = -1, $cacheData = null, $lock = true, $data = null) {
  33. return [];
  34. }
  35. public function scan($path, $recursive = self::SCAN_RECURSIVE, $reuse = -1, $lock = true) {
  36. return [];
  37. }
  38. protected function scanChildren(string $path, $recursive, int $reuse, int $folderId, bool $lock, int $oldSize) {
  39. return 0;
  40. }
  41. public function backgroundScan() {
  42. $lastPath = null;
  43. // find any path marked as unscanned and run the scanner until no more paths are unscanned (or we get stuck)
  44. // we sort by path DESC to ensure that contents of a folder are handled before the parent folder
  45. while (($path = $this->getIncomplete()) !== false && $path !== $lastPath) {
  46. $this->runBackgroundScanJob(function () use ($path) {
  47. $item = $this->cache->get($path);
  48. if ($item && $item->getMimeType() !== FileInfo::MIMETYPE_FOLDER) {
  49. $fh = $this->storage->fopen($path, 'r');
  50. if ($fh) {
  51. $stat = fstat($fh);
  52. if ($stat['size']) {
  53. $this->cache->update($item->getId(), ['size' => $stat['size']]);
  54. }
  55. }
  56. }
  57. }, $path);
  58. // FIXME: this won't proceed with the next item, needs revamping of getIncomplete()
  59. // to make this possible
  60. $lastPath = $path;
  61. }
  62. }
  63. /**
  64. * Unlike the default Cache::getIncomplete this one sorts by path.
  65. *
  66. * This is needed since self::backgroundScan doesn't fix child entries when running on a parent folder.
  67. * By sorting by path we ensure that we encounter the child entries first.
  68. *
  69. * @return false|string
  70. * @throws \OCP\DB\Exception
  71. */
  72. private function getIncomplete() {
  73. $query = $this->connection->getQueryBuilder();
  74. $query->select('path')
  75. ->from('filecache')
  76. ->where($query->expr()->eq('storage', $query->createNamedParameter($this->cache->getNumericStorageId(), IQueryBuilder::PARAM_INT)))
  77. ->andWhere($query->expr()->lt('size', $query->createNamedParameter(0, IQueryBuilder::PARAM_INT)))
  78. ->orderBy('path', 'DESC')
  79. ->setMaxResults(1);
  80. $result = $query->executeQuery();
  81. $path = $result->fetchOne();
  82. $result->closeCursor();
  83. if ($path === false) {
  84. return false;
  85. }
  86. // Make sure Oracle does not continue with null for empty strings
  87. return (string)$path;
  88. }
  89. }