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.

146 lines
3.9 KiB

3 years ago
  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 <pvince81@owncloud.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. return true;
  82. } else {
  83. return false;
  84. }
  85. }
  86. /**
  87. * Update the cache for changes to $path
  88. *
  89. * @param string $path
  90. * @param ICacheEntry $cachedData
  91. */
  92. public function update($path, $cachedData) {
  93. if ($this->storage->is_dir($path)) {
  94. $this->scanner->scan($path, Scanner::SCAN_SHALLOW);
  95. } else {
  96. $this->scanner->scanFile($path);
  97. }
  98. if (is_array($cachedData) && $cachedData['mimetype'] === 'httpd/unix-directory') {
  99. $this->cleanFolder($path);
  100. }
  101. if ($this->cache instanceof Cache) {
  102. $this->cache->correctFolderSize($path);
  103. }
  104. }
  105. /**
  106. * Check if the cache for $path needs to be updated
  107. *
  108. * @param string $path
  109. * @param ICacheEntry $cachedData
  110. * @return bool
  111. */
  112. public function needsUpdate($path, $cachedData) {
  113. if ($this->watchPolicy === self::CHECK_ALWAYS or ($this->watchPolicy === self::CHECK_ONCE and array_search($path, $this->checkedPaths) === false)) {
  114. $this->checkedPaths[] = $path;
  115. return $this->storage->hasUpdated($path, $cachedData['storage_mtime']);
  116. }
  117. return false;
  118. }
  119. /**
  120. * remove deleted files in $path from the cache
  121. *
  122. * @param string $path
  123. */
  124. public function cleanFolder($path) {
  125. $cachedContent = $this->cache->getFolderContents($path);
  126. foreach ($cachedContent as $entry) {
  127. if (!$this->storage->file_exists($entry['path'])) {
  128. $this->cache->remove($entry['path']);
  129. }
  130. }
  131. }
  132. }