Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable30] fix: don't use cached root info from shared cache if the watcher has detected an update #50761

Open
wants to merge 2 commits into
base: stable30
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions apps/files_sharing/lib/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,8 @@ public function getCacheEntryFromSearchResult(ICacheEntry $rawEntry): ?ICacheEnt
return null;
}
}

public function markRootChanged(): void {
$this->rootUnchanged = false;
}
}
5 changes: 5 additions & 0 deletions apps/files_sharing/lib/SharedStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,12 @@
// for shares from the home storage we can rely on the home storage to keep itself up to date
// for other storages we need use the proper watcher
if (!(str_starts_with($storageId, 'home::') || str_starts_with($storageId, 'object::user'))) {
$cache = $this->getCache();
$this->watcher = parent::getWatcher($path, $storage);
if ($cache instanceof Cache && $this->watcher instanceof Watcher) {

Check notice

Code scanning / Psalm

RedundantConditionGivenDocblockType Note

Docblock-defined type OC\Files\Cache\Watcher for $this->watcher is always OC\Files\Cache\Watcher
$this->watcher->onUpdate([$cache, 'markRootChanged']);
}

return $this->watcher;
}
}
Expand Down
37 changes: 37 additions & 0 deletions apps/files_sharing/tests/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
*/
namespace OCA\Files_Sharing\Tests;

use OC\Files\Filesystem;
use OC\Files\Storage\Temporary;
use OC\Files\Storage\Wrapper\Jail;
use OCA\Files_Sharing\SharedStorage;
use OCP\Constants;
use OCP\Files\Cache\IWatcher;
use OCP\Share\IShare;

/**
Expand Down Expand Up @@ -564,4 +567,38 @@ public function testSearchShareJailedStorage() {
$results = $sharedStorage->getCache()->search("foo.txt");
$this->assertCount(1, $results);
}

public function testWatcherRootChange() {
$sourceStorage = new Temporary();
$sourceStorage->mkdir('shared');
$sourceStorage->file_put_contents('shared/foo.txt', 'foo');
$sourceStorage->getScanner()->scan('');
$sourceStorage->getWatcher()->setPolicy(IWatcher::CHECK_ALWAYS);
$this->registerMount(self::TEST_FILES_SHARING_API_USER1, $sourceStorage, '/' . self::TEST_FILES_SHARING_API_USER1 . '/files/foo');

self::loginHelper(self::TEST_FILES_SHARING_API_USER1);

$rootFolder = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER1);
$node = $rootFolder->get('foo/shared');
$this->assertEquals(3, $node->getSize());

$share = $this->shareManager->newShare();
$share->setNode($node)
->setShareType(IShare::TYPE_USER)
->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
->setPermissions(Constants::PERMISSION_ALL);
$share = $this->shareManager->createShare($share);
$share->setStatus(IShare::STATUS_ACCEPTED);
$this->shareManager->updateShare($share);
\OC_Util::tearDownFS();

self::loginHelper(self::TEST_FILES_SHARING_API_USER2);

$view = Filesystem::getView();

$sourceStorage->rmdir('shared');

$this->assertFalse($view->getFileInfo('shared'));
}
}
13 changes: 13 additions & 0 deletions lib/private/Files/Cache/Watcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class Watcher implements IWatcher {
*/
protected $scanner;

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

/**
* @param \OC\Files\Storage\Storage $storage
*/
Expand Down Expand Up @@ -100,6 +103,9 @@ public function update($path, $cachedData) {
if ($this->cache instanceof Cache) {
$this->cache->correctFolderSize($path);
}
foreach ($this->onUpdate as $callback) {
$callback($path);
}
}

/**
Expand Down Expand Up @@ -130,4 +136,11 @@ public function cleanFolder($path) {
}
}
}

/**
* register a callback to be called whenever the watcher triggers and update
*/
public function onUpdate(callable $callback): void {
$this->onUpdate[] = $callback;
}
}
3 changes: 3 additions & 0 deletions lib/private/Files/Cache/Wrapper/JailWatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,7 @@ public function cleanFolder($path) {
$this->watcher->cleanFolder($this->getSourcePath($path));
}

public function onUpdate(callable $callback): void {
$this->watcher->onUpdate($callback);
}
}
Loading