CleanupRemoteStorages.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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->execute();
  103. $count = $result->fetchOne();
  104. $output->writeln("$count files can be deleted for storage $numericId");
  105. }
  106. public function deleteStorage($id, $numericId, OutputInterface $output) {
  107. $queryBuilder = $this->connection->getQueryBuilder();
  108. $queryBuilder->delete('storages')
  109. ->where($queryBuilder->expr()->eq(
  110. 'id',
  111. $queryBuilder->createNamedParameter($id, IQueryBuilder::PARAM_STR),
  112. IQueryBuilder::PARAM_STR)
  113. );
  114. $output->write("deleting $id [$numericId] ... ");
  115. $count = $queryBuilder->execute();
  116. $output->writeln("deleted $count storage");
  117. $this->deleteFiles($numericId, $output);
  118. }
  119. public function deleteFiles($numericId, OutputInterface $output) {
  120. $queryBuilder = $this->connection->getQueryBuilder();
  121. $queryBuilder->delete('filecache')
  122. ->where($queryBuilder->expr()->eq(
  123. 'storage',
  124. $queryBuilder->createNamedParameter($numericId, IQueryBuilder::PARAM_STR),
  125. IQueryBuilder::PARAM_STR)
  126. );
  127. $output->write("deleting files for storage $numericId ... ");
  128. $count = $queryBuilder->execute();
  129. $output->writeln("deleted $count files");
  130. }
  131. public function getRemoteStorages() {
  132. $queryBuilder = $this->connection->getQueryBuilder();
  133. $queryBuilder->select(['id', 'numeric_id'])
  134. ->from('storages')
  135. ->where($queryBuilder->expr()->like(
  136. 'id',
  137. // match all 'shared::' + 32 characters storages
  138. $queryBuilder->createNamedParameter($this->connection->escapeLikeParameter('shared::') . str_repeat('_', 32)),
  139. IQueryBuilder::PARAM_STR)
  140. )
  141. ->andWhere($queryBuilder->expr()->notLike(
  142. 'id',
  143. // but not the ones starting with a '/', they are for normal shares
  144. $queryBuilder->createNamedParameter($this->connection->escapeLikeParameter('shared::/') . '%'),
  145. IQueryBuilder::PARAM_STR)
  146. )->orderBy('numeric_id');
  147. $query = $queryBuilder->execute();
  148. $remoteStorages = [];
  149. while ($row = $query->fetch()) {
  150. $remoteStorages[$row['id']] = $row['numeric_id'];
  151. }
  152. return $remoteStorages;
  153. }
  154. public function getRemoteShareIds() {
  155. $queryBuilder = $this->connection->getQueryBuilder();
  156. $queryBuilder->select(['id', 'share_token', 'owner', 'remote'])
  157. ->from('share_external');
  158. $query = $queryBuilder->execute();
  159. $remoteShareIds = [];
  160. while ($row = $query->fetch()) {
  161. $cloudId = $this->cloudIdManager->getCloudId($row['owner'], $row['remote']);
  162. $remote = $cloudId->getRemote();
  163. $remoteShareIds[$row['id']] = 'shared::' . md5($row['share_token'] . '@' . $remote);
  164. }
  165. return $remoteShareIds;
  166. }
  167. }