@@ -14,133 +14,133 @@ |
||
| 14 | 14 | * check the storage backends for updates and change the cache accordingly |
| 15 | 15 | */ |
| 16 | 16 | class Watcher implements IWatcher { |
| 17 | - protected $watchPolicy = self::CHECK_ONCE; |
|
| 18 | - |
|
| 19 | - protected $checkedPaths = []; |
|
| 20 | - |
|
| 21 | - /** |
|
| 22 | - * @var \OC\Files\Storage\Storage $storage |
|
| 23 | - */ |
|
| 24 | - protected $storage; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * @var Cache $cache |
|
| 28 | - */ |
|
| 29 | - protected $cache; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * @var Scanner $scanner ; |
|
| 33 | - */ |
|
| 34 | - protected $scanner; |
|
| 35 | - |
|
| 36 | - /** @var callable[] */ |
|
| 37 | - protected $onUpdate = []; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * @param \OC\Files\Storage\Storage $storage |
|
| 41 | - */ |
|
| 42 | - public function __construct(\OC\Files\Storage\Storage $storage) { |
|
| 43 | - $this->storage = $storage; |
|
| 44 | - $this->cache = $storage->getCache(); |
|
| 45 | - $this->scanner = $storage->getScanner(); |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * @param int $policy either \OC\Files\Cache\Watcher::CHECK_NEVER, \OC\Files\Cache\Watcher::CHECK_ONCE, \OC\Files\Cache\Watcher::CHECK_ALWAYS |
|
| 50 | - */ |
|
| 51 | - public function setPolicy($policy) { |
|
| 52 | - $this->watchPolicy = $policy; |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * @return int either \OC\Files\Cache\Watcher::CHECK_NEVER, \OC\Files\Cache\Watcher::CHECK_ONCE, \OC\Files\Cache\Watcher::CHECK_ALWAYS |
|
| 57 | - */ |
|
| 58 | - public function getPolicy() { |
|
| 59 | - return $this->watchPolicy; |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * check $path for updates and update if needed |
|
| 64 | - * |
|
| 65 | - * @param string $path |
|
| 66 | - * @param ICacheEntry|null $cachedEntry |
|
| 67 | - * @return boolean true if path was updated |
|
| 68 | - */ |
|
| 69 | - public function checkUpdate($path, $cachedEntry = null) { |
|
| 70 | - if (is_null($cachedEntry)) { |
|
| 71 | - $cachedEntry = $this->cache->get($path); |
|
| 72 | - } |
|
| 73 | - if ($cachedEntry === false || $this->needsUpdate($path, $cachedEntry)) { |
|
| 74 | - $this->update($path, $cachedEntry); |
|
| 75 | - |
|
| 76 | - if ($cachedEntry === false) { |
|
| 77 | - return true; |
|
| 78 | - } else { |
|
| 79 | - // storage backends can sometimes return false positives, only return true if the scanner actually found a change |
|
| 80 | - $newEntry = $this->cache->get($path); |
|
| 81 | - return $newEntry->getStorageMTime() > $cachedEntry->getStorageMTime(); |
|
| 82 | - } |
|
| 83 | - } else { |
|
| 84 | - return false; |
|
| 85 | - } |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * Update the cache for changes to $path |
|
| 90 | - * |
|
| 91 | - * @param string $path |
|
| 92 | - * @param ICacheEntry $cachedData |
|
| 93 | - */ |
|
| 94 | - public function update($path, $cachedData) { |
|
| 95 | - if ($this->storage->is_dir($path)) { |
|
| 96 | - $this->scanner->scan($path, Scanner::SCAN_SHALLOW); |
|
| 97 | - } else { |
|
| 98 | - $this->scanner->scanFile($path); |
|
| 99 | - } |
|
| 100 | - if (is_array($cachedData) && $cachedData['mimetype'] === 'httpd/unix-directory') { |
|
| 101 | - $this->cleanFolder($path); |
|
| 102 | - } |
|
| 103 | - if ($this->cache instanceof Cache) { |
|
| 104 | - $this->cache->correctFolderSize($path); |
|
| 105 | - } |
|
| 106 | - foreach ($this->onUpdate as $callback) { |
|
| 107 | - $callback($path); |
|
| 108 | - } |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * Check if the cache for $path needs to be updated |
|
| 113 | - * |
|
| 114 | - * @param string $path |
|
| 115 | - * @param ICacheEntry $cachedData |
|
| 116 | - * @return bool |
|
| 117 | - */ |
|
| 118 | - public function needsUpdate($path, $cachedData) { |
|
| 119 | - if ($this->watchPolicy === self::CHECK_ALWAYS or ($this->watchPolicy === self::CHECK_ONCE and !in_array($path, $this->checkedPaths))) { |
|
| 120 | - $this->checkedPaths[] = $path; |
|
| 121 | - return $cachedData['storage_mtime'] === null || $this->storage->hasUpdated($path, $cachedData['storage_mtime']); |
|
| 122 | - } |
|
| 123 | - return false; |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * remove deleted files in $path from the cache |
|
| 128 | - * |
|
| 129 | - * @param string $path |
|
| 130 | - */ |
|
| 131 | - public function cleanFolder($path) { |
|
| 132 | - $cachedContent = $this->cache->getFolderContents($path); |
|
| 133 | - foreach ($cachedContent as $entry) { |
|
| 134 | - if (!$this->storage->file_exists($entry['path'])) { |
|
| 135 | - $this->cache->remove($entry['path']); |
|
| 136 | - } |
|
| 137 | - } |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * register a callback to be called whenever the watcher triggers and update |
|
| 142 | - */ |
|
| 143 | - public function onUpdate(callable $callback): void { |
|
| 144 | - $this->onUpdate[] = $callback; |
|
| 145 | - } |
|
| 17 | + protected $watchPolicy = self::CHECK_ONCE; |
|
| 18 | + |
|
| 19 | + protected $checkedPaths = []; |
|
| 20 | + |
|
| 21 | + /** |
|
| 22 | + * @var \OC\Files\Storage\Storage $storage |
|
| 23 | + */ |
|
| 24 | + protected $storage; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * @var Cache $cache |
|
| 28 | + */ |
|
| 29 | + protected $cache; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * @var Scanner $scanner ; |
|
| 33 | + */ |
|
| 34 | + protected $scanner; |
|
| 35 | + |
|
| 36 | + /** @var callable[] */ |
|
| 37 | + protected $onUpdate = []; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * @param \OC\Files\Storage\Storage $storage |
|
| 41 | + */ |
|
| 42 | + public function __construct(\OC\Files\Storage\Storage $storage) { |
|
| 43 | + $this->storage = $storage; |
|
| 44 | + $this->cache = $storage->getCache(); |
|
| 45 | + $this->scanner = $storage->getScanner(); |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * @param int $policy either \OC\Files\Cache\Watcher::CHECK_NEVER, \OC\Files\Cache\Watcher::CHECK_ONCE, \OC\Files\Cache\Watcher::CHECK_ALWAYS |
|
| 50 | + */ |
|
| 51 | + public function setPolicy($policy) { |
|
| 52 | + $this->watchPolicy = $policy; |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * @return int either \OC\Files\Cache\Watcher::CHECK_NEVER, \OC\Files\Cache\Watcher::CHECK_ONCE, \OC\Files\Cache\Watcher::CHECK_ALWAYS |
|
| 57 | + */ |
|
| 58 | + public function getPolicy() { |
|
| 59 | + return $this->watchPolicy; |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * check $path for updates and update if needed |
|
| 64 | + * |
|
| 65 | + * @param string $path |
|
| 66 | + * @param ICacheEntry|null $cachedEntry |
|
| 67 | + * @return boolean true if path was updated |
|
| 68 | + */ |
|
| 69 | + public function checkUpdate($path, $cachedEntry = null) { |
|
| 70 | + if (is_null($cachedEntry)) { |
|
| 71 | + $cachedEntry = $this->cache->get($path); |
|
| 72 | + } |
|
| 73 | + if ($cachedEntry === false || $this->needsUpdate($path, $cachedEntry)) { |
|
| 74 | + $this->update($path, $cachedEntry); |
|
| 75 | + |
|
| 76 | + if ($cachedEntry === false) { |
|
| 77 | + return true; |
|
| 78 | + } else { |
|
| 79 | + // storage backends can sometimes return false positives, only return true if the scanner actually found a change |
|
| 80 | + $newEntry = $this->cache->get($path); |
|
| 81 | + return $newEntry->getStorageMTime() > $cachedEntry->getStorageMTime(); |
|
| 82 | + } |
|
| 83 | + } else { |
|
| 84 | + return false; |
|
| 85 | + } |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * Update the cache for changes to $path |
|
| 90 | + * |
|
| 91 | + * @param string $path |
|
| 92 | + * @param ICacheEntry $cachedData |
|
| 93 | + */ |
|
| 94 | + public function update($path, $cachedData) { |
|
| 95 | + if ($this->storage->is_dir($path)) { |
|
| 96 | + $this->scanner->scan($path, Scanner::SCAN_SHALLOW); |
|
| 97 | + } else { |
|
| 98 | + $this->scanner->scanFile($path); |
|
| 99 | + } |
|
| 100 | + if (is_array($cachedData) && $cachedData['mimetype'] === 'httpd/unix-directory') { |
|
| 101 | + $this->cleanFolder($path); |
|
| 102 | + } |
|
| 103 | + if ($this->cache instanceof Cache) { |
|
| 104 | + $this->cache->correctFolderSize($path); |
|
| 105 | + } |
|
| 106 | + foreach ($this->onUpdate as $callback) { |
|
| 107 | + $callback($path); |
|
| 108 | + } |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * Check if the cache for $path needs to be updated |
|
| 113 | + * |
|
| 114 | + * @param string $path |
|
| 115 | + * @param ICacheEntry $cachedData |
|
| 116 | + * @return bool |
|
| 117 | + */ |
|
| 118 | + public function needsUpdate($path, $cachedData) { |
|
| 119 | + if ($this->watchPolicy === self::CHECK_ALWAYS or ($this->watchPolicy === self::CHECK_ONCE and !in_array($path, $this->checkedPaths))) { |
|
| 120 | + $this->checkedPaths[] = $path; |
|
| 121 | + return $cachedData['storage_mtime'] === null || $this->storage->hasUpdated($path, $cachedData['storage_mtime']); |
|
| 122 | + } |
|
| 123 | + return false; |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * remove deleted files in $path from the cache |
|
| 128 | + * |
|
| 129 | + * @param string $path |
|
| 130 | + */ |
|
| 131 | + public function cleanFolder($path) { |
|
| 132 | + $cachedContent = $this->cache->getFolderContents($path); |
|
| 133 | + foreach ($cachedContent as $entry) { |
|
| 134 | + if (!$this->storage->file_exists($entry['path'])) { |
|
| 135 | + $this->cache->remove($entry['path']); |
|
| 136 | + } |
|
| 137 | + } |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * register a callback to be called whenever the watcher triggers and update |
|
| 142 | + */ |
|
| 143 | + public function onUpdate(callable $callback): void { |
|
| 144 | + $this->onUpdate[] = $callback; |
|
| 145 | + } |
|
| 146 | 146 | } |