-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
[stable25] add wrapper for external storage to ensure we don't get an mtime that is lower than we know it is #40571
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
<?php | ||
|
||
namespace OC\Files\Storage\Wrapper; | ||
|
||
use OCP\AppFramework\Utility\ITimeFactory; | ||
use OCP\Cache\CappedMemoryCache; | ||
use OCP\Files\Storage\IStorage; | ||
|
||
/** | ||
* Wrapper that overwrites the mtime return by stat/getMetaData if the returned value | ||
* is lower than when we last modified the file. | ||
* | ||
* This is useful because some storage servers can return an outdated mtime right after writes | ||
*/ | ||
class KnownMtime extends Wrapper { | ||
private CappedMemoryCache $knowMtimes; | ||
private ITimeFactory $clock; | ||
|
||
public function __construct($arguments) { | ||
parent::__construct($arguments); | ||
$this->knowMtimes = new CappedMemoryCache(); | ||
$this->clock = $arguments['clock']; | ||
} | ||
|
||
public function file_put_contents($path, $data) { | ||
$result = parent::file_put_contents($path, $data); | ||
if ($result) { | ||
$this->knowMtimes->set($path, $this->clock->getTime()); | ||
} | ||
return $result; | ||
} | ||
|
||
public function stat($path) { | ||
$stat = parent::stat($path); | ||
if ($stat) { | ||
$this->applyKnownMtime($path, $stat); | ||
} | ||
return $stat; | ||
} | ||
|
||
public function getMetaData($path) { | ||
$stat = parent::getMetaData($path); | ||
if ($stat) { | ||
$this->applyKnownMtime($path, $stat); | ||
} | ||
return $stat; | ||
} | ||
|
||
private function applyKnownMtime(string $path, array &$stat) { | ||
if (isset($stat['mtime'])) { | ||
$knownMtime = $this->knowMtimes->get($path) ?? 0; | ||
$stat['mtime'] = max($stat['mtime'], $knownMtime); | ||
} | ||
} | ||
|
||
public function filemtime($path) { | ||
$knownMtime = $this->knowMtimes->get($path) ?? 0; | ||
return max(parent::filemtime($path), $knownMtime); | ||
} | ||
|
||
public function mkdir($path) { | ||
$result = parent::mkdir($path); | ||
if ($result) { | ||
$this->knowMtimes->set($path, $this->clock->getTime()); | ||
} | ||
return $result; | ||
} | ||
|
||
public function rmdir($path) { | ||
$result = parent::rmdir($path); | ||
if ($result) { | ||
$this->knowMtimes->set($path, $this->clock->getTime()); | ||
} | ||
return $result; | ||
} | ||
|
||
public function unlink($path) { | ||
$result = parent::unlink($path); | ||
if ($result) { | ||
$this->knowMtimes->set($path, $this->clock->getTime()); | ||
} | ||
return $result; | ||
} | ||
|
||
public function rename($source, $target) { | ||
Check failure Code scanning / Psalm ParamNameMismatch
Argument 2 of OC\Files\Storage\Wrapper\KnownMtime::rename has wrong name $target, expecting $path2 as defined by OCP\Files\Storage\IStorage::rename
|
||
$result = parent::rename($source, $target); | ||
if ($result) { | ||
$this->knowMtimes->set($target, $this->clock->getTime()); | ||
$this->knowMtimes->set($source, $this->clock->getTime()); | ||
} | ||
return $result; | ||
} | ||
|
||
public function copy($source, $target) { | ||
Check failure Code scanning / Psalm ParamNameMismatch
Argument 1 of OC\Files\Storage\Wrapper\KnownMtime::copy has wrong name $source, expecting $path1 as defined by OCP\Files\Storage\IStorage::copy
Check failure Code scanning / Psalm ParamNameMismatch
Argument 2 of OC\Files\Storage\Wrapper\KnownMtime::copy has wrong name $target, expecting $path2 as defined by OCP\Files\Storage\IStorage::copy
|
||
$result = parent::copy($source, $target); | ||
if ($result) { | ||
$this->knowMtimes->set($target, $this->clock->getTime()); | ||
} | ||
return $result; | ||
} | ||
|
||
public function fopen($path, $mode) { | ||
$result = parent::fopen($path, $mode); | ||
if ($result && $mode === 'w') { | ||
$this->knowMtimes->set($path, $this->clock->getTime()); | ||
} | ||
return $result; | ||
} | ||
|
||
public function touch($path, $mtime = null) { | ||
$result = parent::touch($path, $mtime); | ||
if ($result) { | ||
$this->knowMtimes->set($path, $mtime ?? $this->clock->getTime()); | ||
} | ||
return $result; | ||
} | ||
|
||
public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) { | ||
$result = parent::copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); | ||
if ($result) { | ||
$this->knowMtimes->set($targetInternalPath, $this->clock->getTime()); | ||
} | ||
return $result; | ||
} | ||
|
||
public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) { | ||
$result = parent::moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); | ||
if ($result) { | ||
$this->knowMtimes->set($targetInternalPath, $this->clock->getTime()); | ||
} | ||
return $result; | ||
} | ||
|
||
public function writeStream(string $path, $stream, int $size = null): int { | ||
$result = parent::writeStream($path, $stream, $size); | ||
if ($result) { | ||
$this->knowMtimes->set($path, $this->clock->getTime()); | ||
} | ||
return $result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
/** | ||
* Copyright (c) 2014 Robin Appelman <[email protected]> | ||
* This file is licensed under the Affero General Public License version 3 or | ||
* later. | ||
* See the COPYING-README file. | ||
*/ | ||
|
||
namespace lib\Files\Storage\Wrapper; | ||
|
||
use OC\Files\Storage\Temporary; | ||
use OC\Files\Storage\Wrapper\KnownMtime; | ||
use OCP\AppFramework\Utility\ITimeFactory; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use Test\Files\Storage\Storage; | ||
|
||
/** | ||
* @group DB | ||
*/ | ||
class KnownMtimeTest extends Storage { | ||
/** @var Temporary */ | ||
private $sourceStorage; | ||
|
||
/** @var ITimeFactory|MockObject */ | ||
private $clock; | ||
private int $fakeTime = 0; | ||
|
||
protected function setUp(): void { | ||
parent::setUp(); | ||
$this->fakeTime = 0; | ||
$this->sourceStorage = new Temporary([]); | ||
$this->clock = $this->createMock(ITimeFactory::class); | ||
$this->clock->method('getTime')->willReturnCallback(function () { | ||
return $this->fakeTime; | ||
}); | ||
$this->instance = $this->getWrappedStorage(); | ||
} | ||
|
||
protected function tearDown(): void { | ||
$this->sourceStorage->cleanUp(); | ||
parent::tearDown(); | ||
} | ||
|
||
protected function getWrappedStorage() { | ||
return new KnownMtime([ | ||
'storage' => $this->sourceStorage, | ||
'clock' => $this->clock, | ||
]); | ||
} | ||
|
||
public function testNewerKnownMtime() { | ||
$future = time() + 1000; | ||
$this->fakeTime = $future; | ||
|
||
$this->instance->file_put_contents('foo.txt', 'bar'); | ||
|
||
// fuzzy match since the clock might have ticked | ||
$this->assertLessThan(2, abs(time() - $this->sourceStorage->filemtime('foo.txt'))); | ||
$this->assertEquals($this->sourceStorage->filemtime('foo.txt'), $this->sourceStorage->stat('foo.txt')['mtime']); | ||
$this->assertEquals($this->sourceStorage->filemtime('foo.txt'), $this->sourceStorage->getMetaData('foo.txt')['mtime']); | ||
|
||
$this->assertEquals($future, $this->instance->filemtime('foo.txt')); | ||
$this->assertEquals($future, $this->instance->stat('foo.txt')['mtime']); | ||
$this->assertEquals($future, $this->instance->getMetaData('foo.txt')['mtime']); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check failure
Code scanning / Psalm
ParamNameMismatch