/lib/private/Files/Cache/Watcher.php

https://gitlab.com/wuhang2003/core · PHP · 142 lines · 61 code · 14 blank · 67 comment · 9 complexity · bd64b00ceff562689f47b0b4ba255852 MD5 · raw file

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