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 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Daniel Jagszent <daniel@jagszent.de>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. * @author Vincent Petry <pvince81@owncloud.com>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OC\Files\Cache;
  26. use OCP\Files\Cache\ICacheEntry;
  27. use OCP\Files\Cache\IWatcher;
  28. /**
  29. * check the storage backends for updates and change the cache accordingly
  30. */
  31. class Watcher implements IWatcher {
  32. protected $watchPolicy = self::CHECK_ONCE;
  33. protected $checkedPaths = array();
  34. /**
  35. * @var \OC\Files\Storage\Storage $storage
  36. */
  37. protected $storage;
  38. /**
  39. * @var Cache $cache
  40. */
  41. protected $cache;
  42. /**
  43. * @var Scanner $scanner ;
  44. */
  45. protected $scanner;
  46. /**
  47. * @param \OC\Files\Storage\Storage $storage
  48. */
  49. public function __construct(\OC\Files\Storage\Storage $storage) {
  50. $this->storage = $storage;
  51. $this->cache = $storage->getCache();
  52. $this->scanner = $storage->getScanner();
  53. }
  54. /**
  55. * @param int $policy either \OC\Files\Cache\Watcher::CHECK_NEVER, \OC\Files\Cache\Watcher::CHECK_ONCE, \OC\Files\Cache\Watcher::CHECK_ALWAYS
  56. */
  57. public function setPolicy($policy) {
  58. $this->watchPolicy = $policy;
  59. }
  60. /**
  61. * @return int either \OC\Files\Cache\Watcher::CHECK_NEVER, \OC\Files\Cache\Watcher::CHECK_ONCE, \OC\Files\Cache\Watcher::CHECK_ALWAYS
  62. */
  63. public function getPolicy() {
  64. return $this->watchPolicy;
  65. }
  66. /**
  67. * check $path for updates and update if needed
  68. *
  69. * @param string $path
  70. * @param ICacheEntry|null $cachedEntry
  71. * @return boolean true if path was updated
  72. */
  73. public function checkUpdate($path, $cachedEntry = null) {
  74. if (is_null($cachedEntry)) {
  75. $cachedEntry = $this->cache->get($path);
  76. }
  77. if ($this->needsUpdate($path, $cachedEntry)) {
  78. $this->update($path, $cachedEntry);
  79. return true;
  80. } else {
  81. return false;
  82. }
  83. }
  84. /**
  85. * Update the cache for changes to $path
  86. *
  87. * @param string $path
  88. * @param ICacheEntry $cachedData
  89. */
  90. public function update($path, $cachedData) {
  91. if ($this->storage->is_dir($path)) {
  92. $this->scanner->scan($path, Scanner::SCAN_SHALLOW);
  93. } else {
  94. $this->scanner->scanFile($path);
  95. }
  96. if ($cachedData['mimetype'] === 'httpd/unix-directory') {
  97. $this->cleanFolder($path);
  98. }
  99. if ($this->cache instanceof Cache) {
  100. $this->cache->correctFolderSize($path);
  101. }
  102. }
  103. /**
  104. * Check if the cache for $path needs to be updated
  105. *
  106. * @param string $path
  107. * @param ICacheEntry $cachedData
  108. * @return bool
  109. */
  110. public function needsUpdate($path, $cachedData) {
  111. if ($this->watchPolicy === self::CHECK_ALWAYS or ($this->watchPolicy === self::CHECK_ONCE and array_search($path, $this->checkedPaths) === false)) {
  112. $this->checkedPaths[] = $path;
  113. return $this->storage->hasUpdated($path, $cachedData['storage_mtime']);
  114. }
  115. return false;
  116. }
  117. /**
  118. * remove deleted files in $path from the cache
  119. *
  120. * @param string $path
  121. */
  122. public function cleanFolder($path) {
  123. $cachedContent = $this->cache->getFolderContents($path);
  124. foreach ($cachedContent as $entry) {
  125. if (!$this->storage->file_exists($entry['path'])) {
  126. $this->cache->remove($entry['path']);
  127. }
  128. }
  129. }
  130. }