You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
3.6 KiB

3 years ago
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Thomas Müller <thomas.mueller@tmit.eu>
  9. * @author Vincent Petry <pvince81@owncloud.com>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  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, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OC\Repair;
  27. use OCP\Migration\IOutput;
  28. use OCP\Migration\IRepairStep;
  29. /**
  30. * Repairs shares with invalid data
  31. */
  32. class RepairInvalidShares implements IRepairStep {
  33. public 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. }