Skip to content

Commit 6a2e0f8

Browse files
Merge pull request #52442 from nextcloud/ext-store-check-update-filter
2 parents b33fdaf + 047ff27 commit 6a2e0f8

File tree

4 files changed

+74
-7
lines changed

4 files changed

+74
-7
lines changed

lib/private/Files/Cache/Watcher.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77
*/
88
namespace OC\Files\Cache;
99

10+
use OCP\Files\Cache\ICache;
1011
use OCP\Files\Cache\ICacheEntry;
12+
use OCP\Files\Cache\IScanner;
1113
use OCP\Files\Cache\IWatcher;
14+
use OCP\Files\Storage\IStorage;
1215

1316
/**
1417
* check the storage backends for updates and change the cache accordingly
@@ -19,27 +22,26 @@ class Watcher implements IWatcher {
1922
protected $checkedPaths = [];
2023

2124
/**
22-
* @var \OC\Files\Storage\Storage $storage
25+
* @var IStorage $storage
2326
*/
2427
protected $storage;
2528

2629
/**
27-
* @var Cache $cache
30+
* @var ICache $cache
2831
*/
2932
protected $cache;
3033

3134
/**
32-
* @var Scanner $scanner ;
35+
* @var IScanner $scanner ;
3336
*/
3437
protected $scanner;
3538

3639
/** @var callable[] */
3740
protected $onUpdate = [];
3841

39-
/**
40-
* @param \OC\Files\Storage\Storage $storage
41-
*/
42-
public function __construct(\OC\Files\Storage\Storage $storage) {
42+
protected ?string $checkFilter = null;
43+
44+
public function __construct(IStorage $storage) {
4345
$this->storage = $storage;
4446
$this->cache = $storage->getCache();
4547
$this->scanner = $storage->getScanner();
@@ -52,6 +54,10 @@ public function setPolicy($policy) {
5254
$this->watchPolicy = $policy;
5355
}
5456

57+
public function setCheckFilter(?string $filter): void {
58+
$this->checkFilter = $filter;
59+
}
60+
5561
/**
5662
* @return int either \OC\Files\Cache\Watcher::CHECK_NEVER, \OC\Files\Cache\Watcher::CHECK_ONCE, \OC\Files\Cache\Watcher::CHECK_ALWAYS
5763
*/
@@ -116,6 +122,12 @@ public function update($path, $cachedData) {
116122
* @return bool
117123
*/
118124
public function needsUpdate($path, $cachedData) {
125+
if ($this->checkFilter !== null) {
126+
if (!preg_match($this->checkFilter, $path)) {
127+
return false;
128+
}
129+
}
130+
119131
if ($this->watchPolicy === self::CHECK_ALWAYS || ($this->watchPolicy === self::CHECK_ONCE && !in_array($path, $this->checkedPaths))) {
120132
$this->checkedPaths[] = $path;
121133
return $cachedData['storage_mtime'] === null || $this->storage->hasUpdated($path, $cachedData['storage_mtime']);

lib/private/Files/Storage/Common.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ public function getWatcher(string $path = '', ?IStorage $storage = null): IWatch
331331
$this->watcher = new Watcher($storage);
332332
$globalPolicy = Server::get(IConfig::class)->getSystemValueInt('filesystem_check_changes', Watcher::CHECK_NEVER);
333333
$this->watcher->setPolicy((int)$this->getMountOption('filesystem_check_changes', $globalPolicy));
334+
$this->watcher->setCheckFilter($this->getMountOption('filesystem_check_filter'));
334335
}
335336
return $this->watcher;
336337
}

lib/public/Files/Cache/IWatcher.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ interface IWatcher {
3434
*/
3535
public function setPolicy($policy);
3636

37+
/**
38+
* Set a filter regex, only paths matching the regex will be checked for updates.
39+
*
40+
* When set to `null`, every path will be checked for updates
41+
*
42+
* @param ?string $filter
43+
* @return void
44+
* @since 33.0.0
45+
*/
46+
public function setCheckFilter(?string $filter): void;
47+
3748
/**
3849
* @return int either IWatcher::CHECK_NEVER, IWatcher::CHECK_ONCE, IWatcher::CHECK_ALWAYS
3950
* @since 9.0.0

tests/lib/Files/Cache/WatcherTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@
88

99
namespace Test\Files\Cache;
1010

11+
use OC\Files\Cache\CacheEntry;
1112
use OC\Files\Cache\Watcher;
1213
use OC\Files\Storage\Storage;
1314
use OC\Files\Storage\Temporary;
15+
use OCP\Files\Cache\IWatcher;
16+
use OCP\Files\Storage\IStorage;
17+
use PHPUnit\Framework\Attributes\DataProvider;
1418

1519
/**
1620
* Class WatcherTest
@@ -192,4 +196,43 @@ private function getTestStorage($scan = true) {
192196
$this->storages[] = $storage;
193197
return $storage;
194198
}
199+
200+
public static function checkFilterProvider(): array {
201+
return [
202+
[null, [
203+
'' => true,
204+
'foo' => true,
205+
'foo.txt' => true,
206+
]],
207+
['/^.+$/', [
208+
'' => false,
209+
'foo' => true,
210+
'foo.txt' => true,
211+
]],
212+
['/^.+\..+$/', [
213+
'' => false,
214+
'foo' => false,
215+
'foo.txt' => true,
216+
]]
217+
];
218+
}
219+
220+
#[DataProvider('checkFilterProvider')]
221+
public function testCheckFilter($filter, $paths) {
222+
$storage = $this->createMock(IStorage::class);
223+
$storage->method('hasUpdated')
224+
->willReturn(true);
225+
$watcher = new Watcher($storage);
226+
$watcher->setPolicy(IWatcher::CHECK_ALWAYS);
227+
228+
$watcher->setCheckFilter($filter);
229+
230+
$entry = new CacheEntry([
231+
'storage_mtime' => 0,
232+
]);
233+
234+
foreach ($paths as $patch => $shouldUpdate) {
235+
$this->assertEquals($shouldUpdate, $watcher->needsUpdate($patch, $entry));
236+
}
237+
}
195238
}

0 commit comments

Comments
 (0)