RepairInvalidShares.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Thomas Müller <thomas.mueller@tmit.eu>
  8. * @author Vincent Petry <pvince81@owncloud.com>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OC\Repair;
  26. use OCP\Migration\IOutput;
  27. use OCP\Migration\IRepairStep;
  28. use Doctrine\DBAL\Platforms\OraclePlatform;
  29. /**
  30. * Repairs shares with invalid data
  31. */
  32. class RepairInvalidShares implements IRepairStep {
  33. const CHUNK_SIZE = 200;
  34. /** @var \OCP\IConfig */
  35. protected $config;
  36. /** @var \OCP\IDBConnection */
  37. protected $connection;
  38. /**
  39. * @param \OCP\IConfig $config
  40. * @param \OCP\IDBConnection $connection
  41. */
  42. public function __construct($config, $connection) {
  43. $this->connection = $connection;
  44. $this->config = $config;
  45. }
  46. public function getName() {
  47. return 'Repair invalid shares';
  48. }
  49. /**
  50. * Adjust file share permissions
  51. */
  52. private function adjustFileSharePermissions(IOutput $out) {
  53. $mask = \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_SHARE;
  54. $builder = $this->connection->getQueryBuilder();
  55. $permsFunc = $builder->expr()->bitwiseAnd('permissions', $mask);
  56. $builder
  57. ->update('share')
  58. ->set('permissions', $permsFunc)
  59. ->where($builder->expr()->eq('item_type', $builder->expr()->literal('file')))
  60. ->andWhere($builder->expr()->neq('permissions', $permsFunc));
  61. $updatedEntries = $builder->execute();
  62. if ($updatedEntries > 0) {
  63. $out->info('Fixed file share permissions for ' . $updatedEntries . ' shares');
  64. }
  65. }
  66. /**
  67. * Remove shares where the parent share does not exist anymore
  68. */
  69. private function removeSharesNonExistingParent(IOutput $out) {
  70. $deletedEntries = 0;
  71. $query = $this->connection->getQueryBuilder();
  72. $query->select('s1.parent')
  73. ->from('share', 's1')
  74. ->where($query->expr()->isNotNull('s1.parent'))
  75. ->andWhere($query->expr()->isNull('s2.id'))
  76. ->leftJoin('s1', 'share', 's2', $query->expr()->eq('s1.parent', 's2.id'))
  77. ->groupBy('s1.parent')
  78. ->setMaxResults(self::CHUNK_SIZE);
  79. $deleteQuery = $this->connection->getQueryBuilder();
  80. $deleteQuery->delete('share')
  81. ->where($deleteQuery->expr()->eq('parent', $deleteQuery->createParameter('parent')));
  82. $deletedInLastChunk = self::CHUNK_SIZE;
  83. while ($deletedInLastChunk === self::CHUNK_SIZE) {
  84. $deletedInLastChunk = 0;
  85. $result = $query->execute();
  86. while ($row = $result->fetch()) {
  87. $deletedInLastChunk++;
  88. $deletedEntries += $deleteQuery->setParameter('parent', (int) $row['parent'])
  89. ->execute();
  90. }
  91. $result->closeCursor();
  92. }
  93. if ($deletedEntries) {
  94. $out->info('Removed ' . $deletedEntries . ' shares where the parent did not exist');
  95. }
  96. }
  97. public function run(IOutput $out) {
  98. $ocVersionFromBeforeUpdate = $this->config->getSystemValue('version', '0.0.0');
  99. if (version_compare($ocVersionFromBeforeUpdate, '12.0.0.11', '<')) {
  100. $this->adjustFileSharePermissions($out);
  101. }
  102. $this->removeSharesNonExistingParent($out);
  103. }
  104. }