CleanupRemoteStorages.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud GmbH.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Daniel Calviño Sánchez <danxuliu@gmail.com>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  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 OCA\Files_Sharing\Command;
  27. use OCP\DB\QueryBuilder\IQueryBuilder;
  28. use OCP\Federation\ICloudIdManager;
  29. use OCP\IDBConnection;
  30. use Symfony\Component\Console\Command\Command;
  31. use Symfony\Component\Console\Input\InputInterface;
  32. use Symfony\Component\Console\Input\InputOption;
  33. use Symfony\Component\Console\Output\OutputInterface;
  34. /**
  35. * Cleanup 'shared::' storage entries that have no matching entries in the
  36. * shares_external table.
  37. */
  38. class CleanupRemoteStorages extends Command {
  39. /**
  40. * @var IDBConnection
  41. */
  42. protected $connection;
  43. /**
  44. * @var ICloudIdManager
  45. */
  46. private $cloudIdManager;
  47. public function __construct(IDBConnection $connection, ICloudIdManager $cloudIdManager) {
  48. $this->connection = $connection;
  49. $this->cloudIdManager = $cloudIdManager;
  50. parent::__construct();
  51. }
  52. protected function configure() {
  53. $this
  54. ->setName('sharing:cleanup-remote-storages')
  55. ->setDescription('Cleanup shared storage entries that have no matching entry in the shares_external table')
  56. ->addOption(
  57. 'dry-run',
  58. null,
  59. InputOption::VALUE_NONE,
  60. 'only show which storages would be deleted'
  61. );
  62. }
  63. public function execute(InputInterface $input, OutputInterface $output): int {
  64. $remoteStorages = $this->getRemoteStorages();
  65. $output->writeln(count($remoteStorages) . ' remote storage(s) need(s) to be checked');
  66. $remoteShareIds = $this->getRemoteShareIds();
  67. $output->writeln(count($remoteShareIds) . ' remote share(s) exist');
  68. foreach ($remoteShareIds as $id => $remoteShareId) {
  69. if (isset($remoteStorages[$remoteShareId])) {
  70. if ($input->getOption('dry-run') || $output->isVerbose()) {
  71. $output->writeln("<info>$remoteShareId belongs to remote share $id</info>");
  72. }
  73. unset($remoteStorages[$remoteShareId]);
  74. } else {
  75. $output->writeln("<comment>$remoteShareId for share $id has no matching storage, yet</comment>");
  76. }
  77. }
  78. if (empty($remoteStorages)) {
  79. $output->writeln('<info>no storages deleted</info>');
  80. } else {
  81. $dryRun = $input->getOption('dry-run');
  82. foreach ($remoteStorages as $id => $numericId) {
  83. if ($dryRun) {
  84. $output->writeln("<error>$id [$numericId] can be deleted</error>");
  85. $this->countFiles($numericId, $output);
  86. } else {
  87. $this->deleteStorage($id, $numericId, $output);
  88. }
  89. }
  90. }
  91. return 0;
  92. }
  93. public function countFiles($numericId, OutputInterface $output) {
  94. $queryBuilder = $this->connection->getQueryBuilder();
  95. $queryBuilder->select($queryBuilder->func()->count('fileid'))
  96. ->from('filecache')
  97. ->where($queryBuilder->expr()->eq(
  98. 'storage',
  99. $queryBuilder->createNamedParameter($numericId, IQueryBuilder::PARAM_STR),
  100. IQueryBuilder::PARAM_STR)
  101. );
  102. $result = $queryBuilder->executeQuery();
  103. $count = $result->fetchOne();
  104. $result->closeCursor();
  105. $output->writeln("$count files can be deleted for storage $numericId");
  106. }
  107. public function deleteStorage($id, $numericId, OutputInterface $output) {
  108. $queryBuilder = $this->connection->getQueryBuilder();
  109. $queryBuilder->delete('storages')
  110. ->where($queryBuilder->expr()->eq(
  111. 'id',
  112. $queryBuilder->createNamedParameter($id, IQueryBuilder::PARAM_STR),
  113. IQueryBuilder::PARAM_STR)
  114. );
  115. $output->write("deleting $id [$numericId] ... ");
  116. $count = $queryBuilder->executeStatement();
  117. $output->writeln("deleted $count storage");
  118. $this->deleteFiles($numericId, $output);
  119. }
  120. public function deleteFiles($numericId, OutputInterface $output) {
  121. $queryBuilder = $this->connection->getQueryBuilder();
  122. $queryBuilder->delete('filecache')
  123. ->where($queryBuilder->expr()->eq(
  124. 'storage',
  125. $queryBuilder->createNamedParameter($numericId, IQueryBuilder::PARAM_STR),
  126. IQueryBuilder::PARAM_STR)
  127. );
  128. $output->write("deleting files for storage $numericId ... ");
  129. $count = $queryBuilder->executeStatement();
  130. $output->writeln("deleted $count files");
  131. }
  132. public function getRemoteStorages() {
  133. $queryBuilder = $this->connection->getQueryBuilder();
  134. $queryBuilder->select(['id', 'numeric_id'])
  135. ->from('storages')
  136. ->where($queryBuilder->expr()->like(
  137. 'id',
  138. // match all 'shared::' + 32 characters storages
  139. $queryBuilder->createNamedParameter($this->connection->escapeLikeParameter('shared::') . str_repeat('_', 32)),
  140. IQueryBuilder::PARAM_STR)
  141. )
  142. ->andWhere($queryBuilder->expr()->notLike(
  143. 'id',
  144. // but not the ones starting with a '/', they are for normal shares
  145. $queryBuilder->createNamedParameter($this->connection->escapeLikeParameter('shared::/') . '%'),
  146. IQueryBuilder::PARAM_STR)
  147. )
  148. ->orderBy('numeric_id');
  149. $result = $queryBuilder->executeQuery();
  150. $remoteStorages = [];
  151. while ($row = $result->fetch()) {
  152. $remoteStorages[$row['id']] = $row['numeric_id'];
  153. }
  154. $result->closeCursor();
  155. return $remoteStorages;
  156. }
  157. public function getRemoteShareIds() {
  158. $queryBuilder = $this->connection->getQueryBuilder();
  159. $queryBuilder->select(['id', 'share_token', 'owner', 'remote'])
  160. ->from('share_external');
  161. $result = $queryBuilder->executeQuery();
  162. $remoteShareIds = [];
  163. while ($row = $result->fetch()) {
  164. $cloudId = $this->cloudIdManager->getCloudId($row['owner'], $row['remote']);
  165. $remote = $cloudId->getRemote();
  166. $remoteShareIds[$row['id']] = 'shared::' . md5($row['share_token'] . '@' . $remote);
  167. }
  168. $result->closeCursor();
  169. return $remoteShareIds;
  170. }
  171. }