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

[stable24] trigger a rescan when trying to fopen a file that exists in cache but not on disk #34410

Merged
merged 2 commits into from
Oct 27, 2022
Merged
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: 2 additions & 2 deletions apps/dav/tests/unit/Connector/Sabre/FileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1151,7 +1151,7 @@ public function testGetFopenFails() {

$info = new \OC\Files\FileInfo('/test.txt', $this->getMockStorage(), null, [
'permissions' => \OCP\Constants::PERMISSION_ALL,
'type' => FileInfo::TYPE_FOLDER,
'type' => FileInfo::TYPE_FILE,
], null);

$file = new \OCA\DAV\Connector\Sabre\File($view, $info);
Expand All @@ -1172,7 +1172,7 @@ public function testGetFopenThrows() {

$info = new \OC\Files\FileInfo('/test.txt', $this->getMockStorage(), null, [
'permissions' => \OCP\Constants::PERMISSION_ALL,
'type' => FileInfo::TYPE_FOLDER,
'type' => FileInfo::TYPE_FILE,
], null);

$file = new \OCA\DAV\Connector\Sabre\File($view, $info);
Expand Down
9 changes: 8 additions & 1 deletion lib/private/Files/Storage/Local.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ public function is_file($path) {
public function stat($path) {
$fullPath = $this->getSourcePath($path);
clearstatcache(true, $fullPath);
if (!file_exists($fullPath)) {
return false;
}
$statResult = @stat($fullPath);
if (PHP_INT_SIZE === 4 && $statResult && !$this->is_dir($path)) {
$filesize = $this->filesize($path);
Expand Down Expand Up @@ -373,8 +376,12 @@ public function copy($path1, $path2) {
}

public function fopen($path, $mode) {
$sourcePath = $this->getSourcePath($path);
if (!file_exists($sourcePath) && $mode === 'r') {
return false;
}
$oldMask = umask(022);
$result = fopen($this->getSourcePath($path), $mode);
$result = @fopen($sourcePath, $mode);
umask($oldMask);
return $result;
}
Expand Down
12 changes: 11 additions & 1 deletion lib/private/Files/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -1007,7 +1007,17 @@ public function fopen($path, $mode) {
\OC::$server->getLogger()->info('Trying to open a file with a mode other than "r" or "w" can cause severe performance issues with some backends');
}

return $this->basicOperation('fopen', $path, $hooks, $mode);
$handle = $this->basicOperation('fopen', $path, $hooks, $mode);
if (!is_resource($handle) && $mode === 'r') {
// trying to read a file that isn't on disk, check if the cache is out of sync and rescan if needed
$mount = $this->getMount($path);
$internalPath = $mount->getInternalPath($this->getAbsolutePath($path));
$storage = $mount->getStorage();
if ($storage->getCache()->inCache($internalPath) && !$storage->file_exists($path)) {
$this->writeUpdate($storage, $internalPath);
}
}
return $handle;
}

/**
Expand Down
19 changes: 19 additions & 0 deletions tests/lib/Files/ViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2722,4 +2722,23 @@ public function testCacheExtension() {
$this->assertEquals(25, $info->getUploadTime());
$this->assertEquals(0, $info->getCreationTime());
}

public function testFopenGone() {
$storage = new Temporary([]);
$scanner = $storage->getScanner();
$storage->file_put_contents('foo.txt', 'bar');
$scanner->scan('');
$cache = $storage->getCache();

Filesystem::mount($storage, [], '/test/');
$view = new View('/test');

$storage->unlink('foo.txt');

$this->assertTrue($cache->inCache('foo.txt'));

$this->assertFalse($view->fopen('foo.txt', 'r'));

$this->assertFalse($cache->inCache('foo.txt'));
}
}