|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * @copyright Copyright (c) 2021 Robin Appelman <robin@icewind.nl> |
| 6 | + * |
| 7 | + * @license GNU AGPL version 3 or any later version |
| 8 | + * |
| 9 | + * This program is free software: you can redistribute it and/or modify |
| 10 | + * it under the terms of the GNU Affero General Public License as |
| 11 | + * published by the Free Software Foundation, either version 3 of the |
| 12 | + * License, or (at your option) any later version. |
| 13 | + * |
| 14 | + * This program is distributed in the hope that it will be useful, |
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | + * GNU Affero General Public License for more details. |
| 18 | + * |
| 19 | + * You should have received a copy of the GNU Affero General Public License |
| 20 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | + * |
| 22 | + */ |
| 23 | + |
| 24 | +namespace OCA\Files\Command; |
| 25 | + |
| 26 | +use OCP\IDBConnection; |
| 27 | +use Symfony\Component\Console\Command\Command; |
| 28 | +use Symfony\Component\Console\Input\InputInterface; |
| 29 | +use Symfony\Component\Console\Output\OutputInterface; |
| 30 | + |
| 31 | +class RepairTree extends Command { |
| 32 | + public const CHUNK_SIZE = 200; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var IDBConnection |
| 36 | + */ |
| 37 | + protected $connection; |
| 38 | + |
| 39 | + public function __construct(IDBConnection $connection) { |
| 40 | + $this->connection = $connection; |
| 41 | + parent::__construct(); |
| 42 | + } |
| 43 | + |
| 44 | + protected function configure() { |
| 45 | + $this |
| 46 | + ->setName('files:repair-tree') |
| 47 | + ->setDescription('Try and repair malformed filesystem tree structures') |
| 48 | + ->addOption('dry-run'); |
| 49 | + } |
| 50 | + |
| 51 | + public function execute(InputInterface $input, OutputInterface $output): int { |
| 52 | + $rows = $this->findBrokenTreeBits(); |
| 53 | + $fix = !$input->getOption('dry-run'); |
| 54 | + |
| 55 | + $output->writeln("Found " . count($rows) . " file entries with an invalid path"); |
| 56 | + |
| 57 | + if ($fix) { |
| 58 | + $this->connection->beginTransaction(); |
| 59 | + } |
| 60 | + |
| 61 | + $query = $this->connection->getQueryBuilder(); |
| 62 | + $query->update('filecache') |
| 63 | + ->set('path', $query->createParameter('path')) |
| 64 | + ->set('path_hash', $query->func()->md5($query->createParameter('path'))) |
| 65 | + ->where($query->expr()->eq('fileid', $query->createParameter('fileid'))); |
| 66 | + |
| 67 | + foreach ($rows as $row) { |
| 68 | + $output->writeln("Path of file ${row['fileid']} is ${row['path']} but should be ${row['parent_path']}/${row['name']} based on it's parent", OutputInterface::VERBOSITY_VERBOSE); |
| 69 | + |
| 70 | + if ($fix) { |
| 71 | + $query->setParameters([ |
| 72 | + 'fileid' => $row['fileid'], |
| 73 | + 'path' => $row['parent_path'] . '/' . $row['name'], |
| 74 | + ]); |
| 75 | + $query->execute(); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + if ($fix) { |
| 80 | + $this->connection->commit(); |
| 81 | + } |
| 82 | + |
| 83 | + return 0; |
| 84 | + } |
| 85 | + |
| 86 | + private function findBrokenTreeBits(): array { |
| 87 | + $query = $this->connection->getQueryBuilder(); |
| 88 | + |
| 89 | + $query->select('f.fileid', 'f.path', 'f.parent', 'f.name') |
| 90 | + ->selectAlias('p.path', 'parent_path') |
| 91 | + ->from('filecache', 'f') |
| 92 | + ->innerJoin('f', 'filecache', 'p', $query->expr()->eq('f.parent', 'p.fileid')) |
| 93 | + ->where($query->expr()->orX( |
| 94 | + $query->expr()->andX( |
| 95 | + $query->expr()->neq('p.path_hash', $query->createNamedParameter(md5(''))), |
| 96 | + $query->expr()->neq('f.path', $query->func()->concat('p.path', $query->func()->concat($query->createNamedParameter('/'), 'f.name'))) |
| 97 | + ), |
| 98 | + $query->expr()->andX( |
| 99 | + $query->expr()->eq('p.path_hash', $query->createNamedParameter(md5(''))), |
| 100 | + $query->expr()->neq('f.path', 'f.name') |
| 101 | + ), |
| 102 | + $query->expr()->neq('f.storage', 'p.storage') |
| 103 | + )); |
| 104 | + |
| 105 | + return $query->execute()->fetchAll(); |
| 106 | + } |
| 107 | +} |
0 commit comments