watcher.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /**
  3. * Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace OC\Files\Cache;
  9. /**
  10. * check the storage backends for updates and change the cache accordingly
  11. */
  12. class Watcher {
  13. /**
  14. * @var \OC\Files\Storage\Storage $storage
  15. */
  16. private $storage;
  17. /**
  18. * @var Cache $cache
  19. */
  20. private $cache;
  21. /**
  22. * @var Scanner $scanner;
  23. */
  24. private $scanner;
  25. /**
  26. * @param \OC\Files\Storage\Storage $storage
  27. */
  28. public function __construct(\OC\Files\Storage\Storage $storage) {
  29. $this->storage = $storage;
  30. $this->cache = $storage->getCache();
  31. $this->scanner = $storage->getScanner();
  32. }
  33. /**
  34. * check $path for updates
  35. *
  36. * @param string $path
  37. */
  38. public function checkUpdate($path) {
  39. $cachedEntry = $this->cache->get($path);
  40. if ($this->storage->hasUpdated($path, $cachedEntry['storage_mtime'])) {
  41. if ($this->storage->is_dir($path)) {
  42. $this->scanner->scan($path, Scanner::SCAN_SHALLOW);
  43. } else {
  44. $this->scanner->scanFile($path);
  45. }
  46. if ($cachedEntry['mimetype'] === 'httpd/unix-directory') {
  47. $this->cleanFolder($path);
  48. }
  49. $this->cache->correctFolderSize($path);
  50. }
  51. }
  52. /**
  53. * remove deleted files in $path from the cache
  54. *
  55. * @param string $path
  56. */
  57. public function cleanFolder($path) {
  58. $cachedContent = $this->cache->getFolderContents($path);
  59. foreach ($cachedContent as $entry) {
  60. if (!$this->storage->file_exists($entry['path'])) {
  61. $this->cache->remove($entry['path']);
  62. }
  63. }
  64. }
  65. }