1
0

RepairInvalidPaths.php 6.2 KB

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