ObjectStoreScanner.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Files\ObjectStore;
  8. use OC\Files\Cache\Scanner;
  9. use OCP\DB\QueryBuilder\IQueryBuilder;
  10. use OCP\Files\FileInfo;
  11. class ObjectStoreScanner extends Scanner {
  12. public function scanFile($file, $reuseExisting = 0, $parentId = -1, $cacheData = null, $lock = true, $data = null) {
  13. return [];
  14. }
  15. public function scan($path, $recursive = self::SCAN_RECURSIVE, $reuse = -1, $lock = true) {
  16. return [];
  17. }
  18. protected function scanChildren(string $path, $recursive, int $reuse, int $folderId, bool $lock, int|float $oldSize, &$etagChanged = false) {
  19. return 0;
  20. }
  21. public function backgroundScan() {
  22. $lastPath = null;
  23. // find any path marked as unscanned and run the scanner until no more paths are unscanned (or we get stuck)
  24. // we sort by path DESC to ensure that contents of a folder are handled before the parent folder
  25. while (($path = $this->getIncomplete()) !== false && $path !== $lastPath) {
  26. $this->runBackgroundScanJob(function () use ($path) {
  27. $item = $this->cache->get($path);
  28. if ($item && $item->getMimeType() !== FileInfo::MIMETYPE_FOLDER) {
  29. $fh = $this->storage->fopen($path, 'r');
  30. if ($fh) {
  31. $stat = fstat($fh);
  32. if ($stat['size']) {
  33. $this->cache->update($item->getId(), ['size' => $stat['size']]);
  34. }
  35. }
  36. }
  37. }, $path);
  38. // FIXME: this won't proceed with the next item, needs revamping of getIncomplete()
  39. // to make this possible
  40. $lastPath = $path;
  41. }
  42. }
  43. /**
  44. * Unlike the default Cache::getIncomplete this one sorts by path.
  45. *
  46. * This is needed since self::backgroundScan doesn't fix child entries when running on a parent folder.
  47. * By sorting by path we ensure that we encounter the child entries first.
  48. *
  49. * @return false|string
  50. * @throws \OCP\DB\Exception
  51. */
  52. private function getIncomplete() {
  53. $query = $this->connection->getQueryBuilder();
  54. $query->select('path')
  55. ->from('filecache')
  56. ->where($query->expr()->eq('storage', $query->createNamedParameter($this->cache->getNumericStorageId(), IQueryBuilder::PARAM_INT)))
  57. ->andWhere($query->expr()->lt('size', $query->createNamedParameter(0, IQueryBuilder::PARAM_INT)))
  58. ->orderBy('path', 'DESC')
  59. ->setMaxResults(1);
  60. $result = $query->executeQuery();
  61. $path = $result->fetchOne();
  62. $result->closeCursor();
  63. if ($path === false) {
  64. return false;
  65. }
  66. // Make sure Oracle does not continue with null for empty strings
  67. return (string)$path;
  68. }
  69. }