RepairInvalidPaths.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl>
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace OC\Repair\NC13;
  22. use OCP\DB\QueryBuilder\IQueryBuilder;
  23. use OCP\IConfig;
  24. use OCP\IDBConnection;
  25. use OCP\Migration\IOutput;
  26. use OCP\Migration\IRepairStep;
  27. class RepairInvalidPaths implements IRepairStep {
  28. const MAX_ROWS = 1000;
  29. /** @var IDBConnection */
  30. private $connection;
  31. /** @var IConfig */
  32. private $config;
  33. private $getIdQuery;
  34. private $updateQuery;
  35. private $reparentQuery;
  36. private $deleteQuery;
  37. public function __construct(IDBConnection $connection, IConfig $config) {
  38. $this->connection = $connection;
  39. $this->config = $config;
  40. }
  41. public function getName() {
  42. return 'Repair invalid paths in file cache';
  43. }
  44. private function getInvalidEntries() {
  45. $builder = $this->connection->getQueryBuilder();
  46. $computedPath = $builder->func()->concat(
  47. 'p.path',
  48. $builder->func()->concat($builder->createNamedParameter('/'), 'f.name')
  49. );
  50. //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;
  51. $query = $builder->select('f.fileid', 'f.path', 'p.path AS parent_path', 'f.name', 'f.parent', 'f.storage')
  52. ->from('filecache', 'f')
  53. ->innerJoin('f', 'filecache', 'p', $builder->expr()->andX(
  54. $builder->expr()->eq('f.parent', 'p.fileid'),
  55. $builder->expr()->neq('p.name', $builder->createNamedParameter(''))
  56. ))
  57. ->where($builder->expr()->neq('f.path', $computedPath))
  58. ->setMaxResults(self::MAX_ROWS);
  59. do {
  60. $result = $query->execute();
  61. $rows = $result->fetchAll();
  62. foreach ($rows as $row) {
  63. yield $row;
  64. }
  65. $result->closeCursor();
  66. } while (count($rows) > 0);
  67. }
  68. private function getId($storage, $path) {
  69. if (!$this->getIdQuery) {
  70. $builder = $this->connection->getQueryBuilder();
  71. $this->getIdQuery = $builder->select('fileid')
  72. ->from('filecache')
  73. ->where($builder->expr()->eq('storage', $builder->createParameter('storage')))
  74. ->andWhere($builder->expr()->eq('path', $builder->createParameter('path')));
  75. }
  76. $this->getIdQuery->setParameter('storage', $storage, IQueryBuilder::PARAM_INT);
  77. $this->getIdQuery->setParameter('path', $path);
  78. return $this->getIdQuery->execute()->fetchColumn();
  79. }
  80. private function update($fileid, $newPath) {
  81. if (!$this->updateQuery) {
  82. $builder = $this->connection->getQueryBuilder();
  83. $this->updateQuery = $builder->update('filecache')
  84. ->set('path', $builder->createParameter('newpath'))
  85. ->set('path_hash', $builder->func()->md5($builder->createParameter('newpath')))
  86. ->where($builder->expr()->eq('fileid', $builder->createParameter('fileid')));
  87. }
  88. $this->updateQuery->setParameter('newpath', $newPath);
  89. $this->updateQuery->setParameter('fileid', $fileid, IQueryBuilder::PARAM_INT);
  90. $this->updateQuery->execute();
  91. }
  92. private function reparent($from, $to) {
  93. if (!$this->reparentQuery) {
  94. $builder = $this->connection->getQueryBuilder();
  95. $this->reparentQuery = $builder->update('filecache')
  96. ->set('parent', $builder->createParameter('to'))
  97. ->where($builder->expr()->eq('fileid', $builder->createParameter('from')));
  98. }
  99. $this->reparentQuery->setParameter('from', $from);
  100. $this->reparentQuery->setParameter('to', $to);
  101. $this->reparentQuery->execute();
  102. }
  103. private function delete($fileid) {
  104. if (!$this->deleteQuery) {
  105. $builder = $this->connection->getQueryBuilder();
  106. $this->deleteQuery = $builder->delete('filecache')
  107. ->where($builder->expr()->eq('fileid', $builder->createParameter('fileid')));
  108. }
  109. $this->deleteQuery->setParameter('fileid', $fileid, IQueryBuilder::PARAM_INT);
  110. $this->deleteQuery->execute();
  111. }
  112. private function repair() {
  113. $this->connection->beginTransaction();
  114. $entries = $this->getInvalidEntries();
  115. $count = 0;
  116. foreach ($entries as $entry) {
  117. $count++;
  118. $calculatedPath = $entry['parent_path'] . '/' . $entry['name'];
  119. if ($newId = $this->getId($entry['storage'], $calculatedPath)) {
  120. // a new entry with the correct path has already been created, reuse that one and delete the incorrect entry
  121. $this->reparent($entry['fileid'], $newId);
  122. $this->delete($entry['fileid']);
  123. } else {
  124. $this->update($entry['fileid'], $calculatedPath);
  125. }
  126. }
  127. $this->connection->commit();
  128. return $count;
  129. }
  130. public function run(IOutput $output) {
  131. $versionFromBeforeUpdate = $this->config->getSystemValue('version', '0.0.0');
  132. // was added to 12.0.0.30 and 13.0.0.1
  133. if (version_compare($versionFromBeforeUpdate, '12.0.0.30', '<') || version_compare($versionFromBeforeUpdate, '13.0.0.0', '==')) {
  134. $count = $this->repair();
  135. $output->info('Repaired ' . $count . ' paths');
  136. }
  137. }
  138. }