diff options
author | Robin Appelman <robin@icewind.nl> | 2017-06-20 16:31:52 +0200 |
---|---|---|
committer | Robin Appelman <robin@icewind.nl> | 2017-06-21 15:52:27 +0200 |
commit | d3c20eefca1e8be222022f09384d629f7933c561 (patch) | |
tree | 72deac57eeab5837b9198a06bb119f4897ee4f27 /lib/private | |
parent | fa817599170e81b0eb28cb59446c32f0b5645f03 (diff) | |
download | nextcloud-server-d3c20eefca1e8be222022f09384d629f7933c561.tar.gz nextcloud-server-d3c20eefca1e8be222022f09384d629f7933c561.zip |
Add repair step for invalid paths
Signed-off-by: Robin Appelman <robin@icewind.nl>
Diffstat (limited to 'lib/private')
-rw-r--r-- | lib/private/Repair.php | 6 | ||||
-rw-r--r-- | lib/private/Repair/RepairInvalidPaths.php | 121 |
2 files changed, 125 insertions, 2 deletions
diff --git a/lib/private/Repair.php b/lib/private/Repair.php index 4d14bf2550c..ca8dbd98758 100644 --- a/lib/private/Repair.php +++ b/lib/private/Repair.php @@ -42,6 +42,7 @@ use OC\Repair\NC12\UpdateLanguageCodes; use OC\Repair\OldGroupMembershipShares; use OC\Repair\Owncloud\SaveAccountsTableData; use OC\Repair\RemoveRootShares; +use OC\Repair\RepairInvalidPaths; use OC\Repair\SqliteAutoincrement; use OC\Repair\RepairMimeTypes; use OC\Repair\RepairInvalidShares; @@ -143,7 +144,8 @@ class Repair implements IOutput{ \OC::$server->query(BundleFetcher::class), \OC::$server->getConfig(), \OC::$server->query(Installer::class) - ) + ), + new RepairInvalidPaths(\OC::$server->getDatabaseConnection()) ]; } @@ -155,7 +157,7 @@ class Repair implements IOutput{ */ public static function getExpensiveRepairSteps() { return [ - new OldGroupMembershipShares(\OC::$server->getDatabaseConnection(), \OC::$server->getGroupManager()), + new OldGroupMembershipShares(\OC::$server->getDatabaseConnection(), \OC::$server->getGroupManager()) ]; } diff --git a/lib/private/Repair/RepairInvalidPaths.php b/lib/private/Repair/RepairInvalidPaths.php new file mode 100644 index 00000000000..cdd0906295f --- /dev/null +++ b/lib/private/Repair/RepairInvalidPaths.php @@ -0,0 +1,121 @@ +<?php +/** + * @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl> + * + * @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; + + +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) { + $count = $this->repair(); + + $output->info('Repaired ' . $count . ' paths'); + } +} |