-
-
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
fix moving folders out of a cache jail #5424
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 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
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,125 @@ | ||
<?php | ||
/** | ||
* @copyright Copyright (c) 2017 Robin Appelman <[email protected]> | ||
* | ||
* @license GNU AGPL version 3 or any later version | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
namespace OC\Repair\NC13; | ||
|
||
|
||
use OCP\IDBConnection; | ||
use OCP\Migration\IOutput; | ||
use OCP\Migration\IRepairStep; | ||
|
||
class RepairInvalidPaths implements IRepairStep { | ||
/** @var IDBConnection */ | ||
private $connection; | ||
|
||
public function __construct(IDBConnection $connection) { | ||
$this->connection = $connection; | ||
} | ||
|
||
|
||
public function getName() { | ||
return 'Repair invalid paths in file cache'; | ||
} | ||
|
||
private function getInvalidEntries() { | ||
$builder = $this->connection->getQueryBuilder(); | ||
|
||
$computedPath = $builder->func()->concat( | ||
'p.path', | ||
$builder->func()->concat($builder->createNamedParameter('/'), 'f.name') | ||
); | ||
|
||
//select f.path, f.parent,p.path from oc_filecache f inner join oc_filecache p on f.parent=p.fileid and p.path!='' where f.path != p.path || '/' || f.name; | ||
$query = $builder->select('f.fileid', 'f.path', 'p.path AS parent_path', 'f.name', 'f.parent', 'f.storage') | ||
->from('filecache', 'f') | ||
->innerJoin('f', 'filecache', 'p', $builder->expr()->andX( | ||
$builder->expr()->eq('f.parent', 'p.fileid'), | ||
$builder->expr()->neq('p.name', $builder->createNamedParameter('')) | ||
)) | ||
->where($builder->expr()->neq('f.path', $computedPath)); | ||
|
||
return $query->execute()->fetchAll(); | ||
} | ||
|
||
private function getId($storage, $path) { | ||
$builder = $this->connection->getQueryBuilder(); | ||
|
||
$query = $builder->select('fileid') | ||
->from('filecache') | ||
->where($builder->expr()->eq('storage', $builder->createNamedParameter($storage))) | ||
->andWhere($builder->expr()->eq('path', $builder->createNamedParameter($path))); | ||
|
||
return $query->execute()->fetchColumn(); | ||
} | ||
|
||
private function update($fileid, $newPath) { | ||
$builder = $this->connection->getQueryBuilder(); | ||
|
||
$query = $builder->update('filecache') | ||
->set('path', $builder->createNamedParameter($newPath)) | ||
->set('path_hash', $builder->createNamedParameter(md5($newPath))) | ||
->where($builder->expr()->eq('fileid', $builder->createNamedParameter($fileid))); | ||
|
||
$query->execute(); | ||
} | ||
|
||
private function reparent($from, $to) { | ||
$builder = $this->connection->getQueryBuilder(); | ||
|
||
$query = $builder->update('filecache') | ||
->set('parent', $builder->createNamedParameter($to)) | ||
->where($builder->expr()->eq('fileid', $builder->createNamedParameter($from))); | ||
$query->execute(); | ||
} | ||
|
||
private function delete($fileid) { | ||
$builder = $this->connection->getQueryBuilder(); | ||
|
||
$query = $builder->delete('filecache') | ||
->where($builder->expr()->eq('fileid', $builder->createNamedParameter($fileid))); | ||
$query->execute(); | ||
} | ||
|
||
private function repair() { | ||
$entries = $this->getInvalidEntries(); | ||
foreach ($entries as $entry) { | ||
$calculatedPath = $entry['parent_path'] . '/' . $entry['name']; | ||
if ($newId = $this->getId($entry['storage'], $calculatedPath)) { | ||
// a new entry with the correct path has already been created, reuse that one and delete the incorrect entry | ||
$this->reparent($entry['fileid'], $newId); | ||
$this->delete($entry['fileid']); | ||
} else { | ||
$this->update($entry['fileid'], $calculatedPath); | ||
} | ||
} | ||
return count($entries); | ||
} | ||
|
||
public function run(IOutput $output) { | ||
$versionFromBeforeUpdate = $this->config->getSystemValue('version', '0.0.0'); | ||
// was added to 12.0.0.30 and 13.0.0.1 | ||
if (version_compare($versionFromBeforeUpdate, '12.0.0.30', '<') || version_compare($versionFromBeforeUpdate, '13.0.0.0', '==')) { | ||
$count = $this->repair(); | ||
|
||
$output->info('Repaired ' . $count . ' paths'); | ||
} | ||
} | ||
} |
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,111 @@ | ||
<?php | ||
/** | ||
* @copyright Copyright (c) 2017 Robin Appelman <[email protected]> | ||
* | ||
* @license GNU AGPL version 3 or any later version | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
namespace Test\Repair; | ||
|
||
use OC\Files\Cache\Cache; | ||
use OC\Files\Storage\Temporary; | ||
use OC\Repair\RepairInvalidPaths; | ||
use OCP\Migration\IOutput; | ||
use Test\TestCase; | ||
|
||
/** | ||
* @group DB | ||
*/ | ||
class RepairInvalidPathsTest extends TestCase { | ||
/** @var Temporary */ | ||
private $storage; | ||
/** @var Cache */ | ||
private $cache; | ||
/** @var RepairInvalidPaths */ | ||
private $repair; | ||
|
||
protected function setUp() { | ||
parent::setUp(); | ||
|
||
$this->storage = new Temporary(); | ||
$this->cache = $this->storage->getCache(); | ||
$this->repair = new RepairInvalidPaths(\OC::$server->getDatabaseConnection()); | ||
} | ||
|
||
protected function tearDown() { | ||
$this->cache->clear(); | ||
|
||
return parent::tearDown(); | ||
} | ||
|
||
public function testRepairNonDuplicate() { | ||
$this->storage->mkdir('foo/bar/asd'); | ||
$this->storage->mkdir('foo2'); | ||
$this->storage->getScanner()->scan(''); | ||
|
||
$folderId = $this->cache->getId('foo/bar'); | ||
$newParentFolderId = $this->cache->getId('foo2'); | ||
// failed rename, moved entry is updated but not it's children | ||
$this->cache->update($folderId, ['path' => 'foo2/bar', 'parent' => $newParentFolderId]); | ||
|
||
$this->assertTrue($this->cache->inCache('foo2/bar')); | ||
$this->assertTrue($this->cache->inCache('foo/bar/asd')); | ||
$this->assertFalse($this->cache->inCache('foo2/bar/asd')); | ||
|
||
$this->assertEquals($folderId, $this->cache->get('foo/bar/asd')['parent']); | ||
|
||
$this->repair->run($this->createMock(IOutput::class)); | ||
|
||
$this->assertTrue($this->cache->inCache('foo2/bar')); | ||
$this->assertTrue($this->cache->inCache('foo2/bar/asd')); | ||
$this->assertFalse($this->cache->inCache('foo/bar/asd')); | ||
|
||
$this->assertEquals($folderId, $this->cache->get('foo2/bar/asd')['parent']); | ||
$this->assertEquals($folderId, $this->cache->getId('foo2/bar')); | ||
} | ||
|
||
public function testRepairDuplicate() { | ||
$this->storage->mkdir('foo/bar/asd'); | ||
$this->storage->mkdir('foo2'); | ||
$this->storage->getScanner()->scan(''); | ||
|
||
$folderId = $this->cache->getId('foo/bar'); | ||
$newParentFolderId = $this->cache->getId('foo2'); | ||
// failed rename, moved entry is updated but not it's children | ||
$this->cache->update($folderId, ['path' => 'foo2/bar', 'parent' => $newParentFolderId]); | ||
$this->storage->rename('foo/bar', 'foo2/bar'); | ||
$this->storage->mkdir('foo2/bar/asd/foo'); | ||
|
||
// usage causes the renamed subfolder to be scanned | ||
$this->storage->getScanner()->scan('foo2/bar/asd'); | ||
|
||
$this->assertTrue($this->cache->inCache('foo2/bar')); | ||
$this->assertTrue($this->cache->inCache('foo/bar/asd')); | ||
$this->assertTrue($this->cache->inCache('foo2/bar/asd')); | ||
|
||
$this->assertEquals($folderId, $this->cache->get('foo/bar/asd')['parent']); | ||
|
||
$this->repair->run($this->createMock(IOutput::class)); | ||
|
||
$this->assertTrue($this->cache->inCache('foo2/bar')); | ||
$this->assertTrue($this->cache->inCache('foo2/bar/asd')); | ||
$this->assertFalse($this->cache->inCache('foo/bar/asd')); | ||
|
||
$this->assertEquals($this->cache->getId('foo2/bar'), $this->cache->get('foo2/bar/asd')['parent']); | ||
$this->assertEquals($this->cache->getId('foo2/bar/asd'), $this->cache->get('foo2/bar/asd/foo')['parent']); | ||
} | ||
} |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test not passing, you moved that file 😉