RepairTree.php 4.3 KB

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