Skip to content

Commit 1f8f15b

Browse files
authored
Merge pull request #44748 from nextcloud/backport/44730/stable29
[stable29] fix: use proper jailed patch in watcher
2 parents ba12337 + 338c8aa commit 1f8f15b

File tree

5 files changed

+109
-4
lines changed

5 files changed

+109
-4
lines changed

lib/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,6 +1383,7 @@
13831383
'OC\\Files\\Cache\\Wrapper\\CachePermissionsMask' => $baseDir . '/lib/private/Files/Cache/Wrapper/CachePermissionsMask.php',
13841384
'OC\\Files\\Cache\\Wrapper\\CacheWrapper' => $baseDir . '/lib/private/Files/Cache/Wrapper/CacheWrapper.php',
13851385
'OC\\Files\\Cache\\Wrapper\\JailPropagator' => $baseDir . '/lib/private/Files/Cache/Wrapper/JailPropagator.php',
1386+
'OC\\Files\\Cache\\Wrapper\\JailWatcher' => $baseDir . '/lib/private/Files/Cache/Wrapper/JailWatcher.php',
13861387
'OC\\Files\\Config\\CachedMountFileInfo' => $baseDir . '/lib/private/Files/Config/CachedMountFileInfo.php',
13871388
'OC\\Files\\Config\\CachedMountInfo' => $baseDir . '/lib/private/Files/Config/CachedMountInfo.php',
13881389
'OC\\Files\\Config\\LazyPathCachedMountInfo' => $baseDir . '/lib/private/Files/Config/LazyPathCachedMountInfo.php',

lib/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,6 +1416,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
14161416
'OC\\Files\\Cache\\Wrapper\\CachePermissionsMask' => __DIR__ . '/../../..' . '/lib/private/Files/Cache/Wrapper/CachePermissionsMask.php',
14171417
'OC\\Files\\Cache\\Wrapper\\CacheWrapper' => __DIR__ . '/../../..' . '/lib/private/Files/Cache/Wrapper/CacheWrapper.php',
14181418
'OC\\Files\\Cache\\Wrapper\\JailPropagator' => __DIR__ . '/../../..' . '/lib/private/Files/Cache/Wrapper/JailPropagator.php',
1419+
'OC\\Files\\Cache\\Wrapper\\JailWatcher' => __DIR__ . '/../../..' . '/lib/private/Files/Cache/Wrapper/JailWatcher.php',
14191420
'OC\\Files\\Config\\CachedMountFileInfo' => __DIR__ . '/../../..' . '/lib/private/Files/Config/CachedMountFileInfo.php',
14201421
'OC\\Files\\Config\\CachedMountInfo' => __DIR__ . '/../../..' . '/lib/private/Files/Config/CachedMountInfo.php',
14211422
'OC\\Files\\Config\\LazyPathCachedMountInfo' => __DIR__ . '/../../..' . '/lib/private/Files/Config/LazyPathCachedMountInfo.php',
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* @copyright Copyright (c) 2024 Robin Appelman <[email protected]>
6+
*
7+
* @license GNU AGPL version 3 or any later version
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
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
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
24+
namespace OC\Files\Cache\Wrapper;
25+
26+
use OC\Files\Cache\Watcher;
27+
28+
class JailWatcher extends Watcher {
29+
private string $root;
30+
private Watcher $watcher;
31+
32+
public function __construct(Watcher $watcher, string $root) {
33+
$this->watcher = $watcher;
34+
$this->root = $root;
35+
}
36+
37+
protected function getRoot(): string {
38+
return $this->root;
39+
}
40+
41+
protected function getSourcePath($path): string {
42+
if ($path === '') {
43+
return $this->getRoot();
44+
} else {
45+
return $this->getRoot() . '/' . ltrim($path, '/');
46+
}
47+
}
48+
49+
public function setPolicy($policy) {
50+
$this->watcher->setPolicy($policy);
51+
}
52+
53+
public function getPolicy() {
54+
return $this->watcher->getPolicy();
55+
}
56+
57+
58+
public function checkUpdate($path, $cachedEntry = null) {
59+
return $this->watcher->checkUpdate($this->getSourcePath($path), $cachedEntry);
60+
}
61+
62+
public function update($path, $cachedData) {
63+
$this->watcher->update($this->getSourcePath($path), $cachedData);
64+
}
65+
66+
public function needsUpdate($path, $cachedData) {
67+
return $this->watcher->needsUpdate($this->getSourcePath($path), $cachedData);
68+
}
69+
70+
public function cleanFolder($path) {
71+
$this->watcher->cleanFolder($this->getSourcePath($path));
72+
}
73+
74+
}

lib/private/Files/Storage/Wrapper/Jail.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
use OC\Files\Cache\Wrapper\CacheJail;
3232
use OC\Files\Cache\Wrapper\JailPropagator;
33+
use OC\Files\Cache\Wrapper\JailWatcher;
3334
use OC\Files\Filesystem;
3435
use OCP\Files\Storage\IStorage;
3536
use OCP\Files\Storage\IWriteStreamStorage;
@@ -418,10 +419,8 @@ public function getOwner($path) {
418419
* @return \OC\Files\Cache\Watcher
419420
*/
420421
public function getWatcher($path = '', $storage = null) {
421-
if (!$storage) {
422-
$storage = $this;
423-
}
424-
return $this->getWrapperStorage()->getWatcher($this->getUnjailedPath($path), $storage);
422+
$sourceWatcher = $this->getWrapperStorage()->getWatcher($this->getUnjailedPath($path), $this->getWrapperStorage());
423+
return new JailWatcher($sourceWatcher, $this->rootPath);
425424
}
426425

427426
/**

tests/lib/Files/Cache/Wrapper/CacheJailTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use OC\Files\Cache\Wrapper\CacheJail;
1212
use OC\Files\Search\SearchComparison;
1313
use OC\Files\Search\SearchQuery;
14+
use OC\Files\Storage\Wrapper\Jail;
1415
use OC\User\User;
1516
use OCP\EventDispatcher\IEventDispatcher;
1617
use OCP\Files\Search\ISearchComparison;
@@ -218,4 +219,33 @@ public function testRootJail() {
218219
$this->assertCount(1, $result);
219220
$this->assertEquals('foo/bar/asd', $result[0]['path']);
220221
}
222+
223+
public function testWatcher() {
224+
$storage = new Jail([
225+
'storage' => $this->storage,
226+
'root' => 'foo'
227+
]);
228+
$storage->getScanner()->scan('');
229+
$storage->file_put_contents('bar', 'asd');
230+
231+
$this->assertFalse($this->cache->inCache('bar'));
232+
$storage->getWatcher()->update('bar', ['mimetype' => 'text/plain']);
233+
$this->assertTrue($this->cache->inCache('bar'));
234+
}
235+
236+
public function testWatcherAfterInnerWatcher() {
237+
$storage = new Jail([
238+
'storage' => $this->storage,
239+
'root' => 'foo'
240+
]);
241+
$storage->getScanner()->scan('');
242+
$storage->file_put_contents('bar', 'asd');
243+
244+
// let the underlying storage create it's watcher first
245+
$this->storage->getWatcher();
246+
247+
$this->assertFalse($this->cache->inCache('bar'));
248+
$storage->getWatcher()->update('bar', ['mimetype' => 'text/plain']);
249+
$this->assertTrue($this->cache->inCache('bar'));
250+
}
221251
}

0 commit comments

Comments
 (0)