You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Watcher.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Daniel Jagszent <daniel@jagszent.de>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Vincent Petry <vincent@nextcloud.com>
  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\Cache;
  28. use OCP\Files\Cache\ICacheEntry;
  29. use OCP\Files\Cache\IWatcher;
  30. /**
  31. * check the storage backends for updates and change the cache accordingly
  32. */
  33. class Watcher implements IWatcher {
  34. protected $watchPolicy = self::CHECK_ONCE;
  35. protected $checkedPaths = [];
  36. /**
  37. * @var \OC\Files\Storage\Storage $storage
  38. */
  39. protected $storage;
  40. /**
  41. * @var Cache $cache
  42. */
  43. protected $cache;
  44. /**
  45. * @var Scanner $scanner ;
  46. */
  47. protected $scanner;
  48. /**
  49. * @param \OC\Files\Storage\Storage $storage
  50. */
  51. public function __construct(\OC\Files\Storage\Storage $storage) {
  52. $this->storage = $storage;
  53. $this->cache = $storage->getCache();
  54. $this->scanner = $storage->getScanner();
  55. }
  56. /**
  57. * @param int $policy either \OC\Files\Cache\Watcher::CHECK_NEVER, \OC\Files\Cache\Watcher::CHECK_ONCE, \OC\Files\Cache\Watcher::CHECK_ALWAYS
  58. */
  59. public function setPolicy($policy) {
  60. $this->watchPolicy = $policy;
  61. }
  62. /**
  63. * @return int either \OC\Files\Cache\Watcher::CHECK_NEVER, \OC\Files\Cache\Watcher::CHECK_ONCE, \OC\Files\Cache\Watcher::CHECK_ALWAYS
  64. */
  65. public function getPolicy() {
  66. return $this->watchPolicy;
  67. }
  68. /**
  69. * check $path for updates and update if needed
  70. *
  71. * @param string $path
  72. * @param ICacheEntry|null $cachedEntry
  73. * @return boolean true if path was updated
  74. */
  75. public function checkUpdate($path, $cachedEntry = null) {
  76. if (is_null($cachedEntry)) {
  77. $cachedEntry = $this->cache->get($path);
  78. }
  79. if ($cachedEntry === false || $this->needsUpdate($path, $cachedEntry)) {
  80. $this->update($path, $cachedEntry);
  81. if ($cachedEntry === false) {
  82. return true;
  83. } else {
  84. // storage backends can sometimes return false positives, only return true if the scanner actually found a change
  85. $newEntry = $this->cache->get($path);
  86. return $newEntry->getStorageMTime() > $cachedEntry->getStorageMTime();
  87. }
  88. } else {
  89. return false;
  90. }
  91. }
  92. /**
  93. * Update the cache for changes to $path
  94. *
  95. * @param string $path
  96. * @param ICacheEntry $cachedData
  97. */
  98. public function update($path, $cachedData) {
  99. if ($this->storage->is_dir($path)) {
  100. $this->scanner->scan($path, Scanner::SCAN_SHALLOW);
  101. } else {
  102. $this->scanner->scanFile($path);
  103. }
  104. if (is_array($cachedData) && $cachedData['mimetype'] === 'httpd/unix-directory') {
  105. $this->cleanFolder($path);
  106. }
  107. if ($this->cache instanceof Cache) {
  108. $this->cache->correctFolderSize($path);
  109. }
  110. }
  111. /**
  112. * Check if the cache for $path needs to be updated
  113. *
  114. * @param string $path
  115. * @param ICacheEntry $cachedData
  116. * @return bool
  117. */
  118. public function needsUpdate($path, $cachedData) {
  119. if ($this->watchPolicy === self::CHECK_ALWAYS or ($this->watchPolicy === self::CHECK_ONCE and !in_array($path, $this->checkedPaths))) {
  120. $this->checkedPaths[] = $path;
  121. return $this->storage->hasUpdated($path, $cachedData['storage_mtime']);
  122. }
  123. return false;
  124. }
  125. /**
  126. * remove deleted files in $path from the cache
  127. *
  128. * @param string $path
  129. */
  130. public function cleanFolder($path) {
  131. $cachedContent = $this->cache->getFolderContents($path);
  132. foreach ($cachedContent as $entry) {
  133. if (!$this->storage->file_exists($entry['path'])) {
  134. $this->cache->remove($entry['path']);
  135. }
  136. }
  137. }
  138. }