watcher.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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) 2015, 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. /**
  25. * check the storage backends for updates and change the cache accordingly
  26. */
  27. class Watcher {
  28. const CHECK_NEVER = 0; // never check the underlying filesystem for updates
  29. const CHECK_ONCE = 1; // check the underlying filesystem for updates once every request for each file
  30. const CHECK_ALWAYS = 2; // always check the underlying filesystem for updates
  31. protected $watchPolicy = self::CHECK_ONCE;
  32. protected $checkedPaths = array();
  33. /**
  34. * @var \OC\Files\Storage\Storage $storage
  35. */
  36. protected $storage;
  37. /**
  38. * @var Cache $cache
  39. */
  40. protected $cache;
  41. /**
  42. * @var Scanner $scanner ;
  43. */
  44. protected $scanner;
  45. /**
  46. * @param \OC\Files\Storage\Storage $storage
  47. */
  48. public function __construct(\OC\Files\Storage\Storage $storage) {
  49. $this->storage = $storage;
  50. $this->cache = $storage->getCache();
  51. $this->scanner = $storage->getScanner();
  52. }
  53. /**
  54. * @param int $policy either \OC\Files\Cache\Watcher::CHECK_NEVER, \OC\Files\Cache\Watcher::CHECK_ONCE, \OC\Files\Cache\Watcher::CHECK_ALWAYS
  55. */
  56. public function setPolicy($policy) {
  57. $this->watchPolicy = $policy;
  58. }
  59. /**
  60. * @return int either \OC\Files\Cache\Watcher::CHECK_NEVER, \OC\Files\Cache\Watcher::CHECK_ONCE, \OC\Files\Cache\Watcher::CHECK_ALWAYS
  61. */
  62. public function getPolicy() {
  63. return $this->watchPolicy;
  64. }
  65. /**
  66. * check $path for updates
  67. *
  68. * @param string $path
  69. * @param array $cachedEntry
  70. * @return boolean true if path was updated
  71. */
  72. public function checkUpdate($path, $cachedEntry = null) {
  73. if ($this->watchPolicy === self::CHECK_ALWAYS or ($this->watchPolicy === self::CHECK_ONCE and array_search($path, $this->checkedPaths) === false)) {
  74. if (is_null($cachedEntry)) {
  75. $cachedEntry = $this->cache->get($path);
  76. }
  77. $this->checkedPaths[] = $path;
  78. if ($this->storage->hasUpdated($path, $cachedEntry['storage_mtime'])) {
  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 ($cachedEntry['mimetype'] === 'httpd/unix-directory') {
  85. $this->cleanFolder($path);
  86. }
  87. $this->cache->correctFolderSize($path);
  88. return true;
  89. }
  90. return false;
  91. } else {
  92. return false;
  93. }
  94. }
  95. /**
  96. * remove deleted files in $path from the cache
  97. *
  98. * @param string $path
  99. */
  100. public function cleanFolder($path) {
  101. $cachedContent = $this->cache->getFolderContents($path);
  102. foreach ($cachedContent as $entry) {
  103. if (!$this->storage->file_exists($entry['path'])) {
  104. $this->cache->remove($entry['path']);
  105. }
  106. }
  107. }
  108. }