Watcher.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Files\Cache;
  8. use OCP\Files\Cache\ICacheEntry;
  9. use OCP\Files\Cache\IWatcher;
  10. /**
  11. * check the storage backends for updates and change the cache accordingly
  12. */
  13. class Watcher implements IWatcher {
  14. protected $watchPolicy = self::CHECK_ONCE;
  15. protected $checkedPaths = [];
  16. /**
  17. * @var \OC\Files\Storage\Storage $storage
  18. */
  19. protected $storage;
  20. /**
  21. * @var Cache $cache
  22. */
  23. protected $cache;
  24. /**
  25. * @var Scanner $scanner ;
  26. */
  27. protected $scanner;
  28. /**
  29. * @param \OC\Files\Storage\Storage $storage
  30. */
  31. public function __construct(\OC\Files\Storage\Storage $storage) {
  32. $this->storage = $storage;
  33. $this->cache = $storage->getCache();
  34. $this->scanner = $storage->getScanner();
  35. }
  36. /**
  37. * @param int $policy either \OC\Files\Cache\Watcher::CHECK_NEVER, \OC\Files\Cache\Watcher::CHECK_ONCE, \OC\Files\Cache\Watcher::CHECK_ALWAYS
  38. */
  39. public function setPolicy($policy) {
  40. $this->watchPolicy = $policy;
  41. }
  42. /**
  43. * @return int either \OC\Files\Cache\Watcher::CHECK_NEVER, \OC\Files\Cache\Watcher::CHECK_ONCE, \OC\Files\Cache\Watcher::CHECK_ALWAYS
  44. */
  45. public function getPolicy() {
  46. return $this->watchPolicy;
  47. }
  48. /**
  49. * check $path for updates and update if needed
  50. *
  51. * @param string $path
  52. * @param ICacheEntry|null $cachedEntry
  53. * @return boolean true if path was updated
  54. */
  55. public function checkUpdate($path, $cachedEntry = null) {
  56. if (is_null($cachedEntry)) {
  57. $cachedEntry = $this->cache->get($path);
  58. }
  59. if ($cachedEntry === false || $this->needsUpdate($path, $cachedEntry)) {
  60. $this->update($path, $cachedEntry);
  61. if ($cachedEntry === false) {
  62. return true;
  63. } else {
  64. // storage backends can sometimes return false positives, only return true if the scanner actually found a change
  65. $newEntry = $this->cache->get($path);
  66. return $newEntry->getStorageMTime() > $cachedEntry->getStorageMTime();
  67. }
  68. } else {
  69. return false;
  70. }
  71. }
  72. /**
  73. * Update the cache for changes to $path
  74. *
  75. * @param string $path
  76. * @param ICacheEntry $cachedData
  77. */
  78. public function update($path, $cachedData) {
  79. if ($this->storage->is_dir($path)) {
  80. $this->scanner->scan($path, Scanner::SCAN_SHALLOW);
  81. } else {
  82. $this->scanner->scanFile($path);
  83. }
  84. if (is_array($cachedData) && $cachedData['mimetype'] === 'httpd/unix-directory') {
  85. $this->cleanFolder($path);
  86. }
  87. if ($this->cache instanceof Cache) {
  88. $this->cache->correctFolderSize($path);
  89. }
  90. }
  91. /**
  92. * Check if the cache for $path needs to be updated
  93. *
  94. * @param string $path
  95. * @param ICacheEntry $cachedData
  96. * @return bool
  97. */
  98. public function needsUpdate($path, $cachedData) {
  99. if ($this->watchPolicy === self::CHECK_ALWAYS or ($this->watchPolicy === self::CHECK_ONCE and !in_array($path, $this->checkedPaths))) {
  100. $this->checkedPaths[] = $path;
  101. return $this->storage->hasUpdated($path, $cachedData['storage_mtime']);
  102. }
  103. return false;
  104. }
  105. /**
  106. * remove deleted files in $path from the cache
  107. *
  108. * @param string $path
  109. */
  110. public function cleanFolder($path) {
  111. $cachedContent = $this->cache->getFolderContents($path);
  112. foreach ($cachedContent as $entry) {
  113. if (!$this->storage->file_exists($entry['path'])) {
  114. $this->cache->remove($entry['path']);
  115. }
  116. }
  117. }
  118. }