DeleteOrphanedFiles.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 Julius Härtl <jus@bitgrid.net>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  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 OCA\Files\Command;
  26. use OCP\DB\QueryBuilder\IQueryBuilder;
  27. use OCP\IDBConnection;
  28. use Symfony\Component\Console\Command\Command;
  29. use Symfony\Component\Console\Input\InputInterface;
  30. use Symfony\Component\Console\Input\InputOption;
  31. use Symfony\Component\Console\Output\OutputInterface;
  32. /**
  33. * Delete all file entries that have no matching entries in the storage table.
  34. */
  35. class DeleteOrphanedFiles extends Command {
  36. public const CHUNK_SIZE = 200;
  37. public function __construct(
  38. protected IDBConnection $connection,
  39. ) {
  40. parent::__construct();
  41. }
  42. protected function configure(): void {
  43. $this
  44. ->setName('files:cleanup')
  45. ->setDescription('cleanup filecache')
  46. ->addOption('skip-filecache-extended', null, InputOption::VALUE_NONE, 'don\'t remove orphaned entries from filecache_extended');
  47. }
  48. public function execute(InputInterface $input, OutputInterface $output): int {
  49. $deletedEntries = 0;
  50. $query = $this->connection->getQueryBuilder();
  51. $query->select('fc.fileid')
  52. ->from('filecache', 'fc')
  53. ->where($query->expr()->isNull('s.numeric_id'))
  54. ->leftJoin('fc', 'storages', 's', $query->expr()->eq('fc.storage', 's.numeric_id'))
  55. ->setMaxResults(self::CHUNK_SIZE);
  56. $deleteQuery = $this->connection->getQueryBuilder();
  57. $deleteQuery->delete('filecache')
  58. ->where($deleteQuery->expr()->eq('fileid', $deleteQuery->createParameter('objectid')));
  59. $deletedInLastChunk = self::CHUNK_SIZE;
  60. while ($deletedInLastChunk === self::CHUNK_SIZE) {
  61. $deletedInLastChunk = 0;
  62. $result = $query->execute();
  63. while ($row = $result->fetch()) {
  64. $deletedInLastChunk++;
  65. $deletedEntries += $deleteQuery->setParameter('objectid', (int) $row['fileid'])
  66. ->execute();
  67. }
  68. $result->closeCursor();
  69. }
  70. $output->writeln("$deletedEntries orphaned file cache entries deleted");
  71. if (!$input->getOption('skip-filecache-extended')) {
  72. $deletedFileCacheExtended = $this->cleanupOrphanedFileCacheExtended();
  73. $output->writeln("$deletedFileCacheExtended orphaned file cache extended entries deleted");
  74. }
  75. $deletedMounts = $this->cleanupOrphanedMounts();
  76. $output->writeln("$deletedMounts orphaned mount entries deleted");
  77. return self::SUCCESS;
  78. }
  79. private function cleanupOrphanedFileCacheExtended(): int {
  80. $deletedEntries = 0;
  81. $query = $this->connection->getQueryBuilder();
  82. $query->select('fce.fileid')
  83. ->from('filecache_extended', 'fce')
  84. ->leftJoin('fce', 'filecache', 'fc', $query->expr()->eq('fce.fileid', 'fc.fileid'))
  85. ->where($query->expr()->isNull('fc.fileid'))
  86. ->setMaxResults(self::CHUNK_SIZE);
  87. $deleteQuery = $this->connection->getQueryBuilder();
  88. $deleteQuery->delete('filecache_extended')
  89. ->where($deleteQuery->expr()->in('fileid', $deleteQuery->createParameter('idsToDelete')));
  90. $result = $query->executeQuery();
  91. while ($result->rowCount() > 0) {
  92. $idsToDelete = $result->fetchAll(\PDO::FETCH_COLUMN);
  93. $deleteQuery->setParameter('idsToDelete', $idsToDelete, IQueryBuilder::PARAM_INT_ARRAY);
  94. $deletedEntries += $deleteQuery->executeStatement();
  95. $result = $query->executeQuery();
  96. }
  97. return $deletedEntries;
  98. }
  99. private function cleanupOrphanedMounts(): int {
  100. $deletedEntries = 0;
  101. $query = $this->connection->getQueryBuilder();
  102. $query->select('m.storage_id')
  103. ->from('mounts', 'm')
  104. ->where($query->expr()->isNull('s.numeric_id'))
  105. ->leftJoin('m', 'storages', 's', $query->expr()->eq('m.storage_id', 's.numeric_id'))
  106. ->groupBy('storage_id')
  107. ->setMaxResults(self::CHUNK_SIZE);
  108. $deleteQuery = $this->connection->getQueryBuilder();
  109. $deleteQuery->delete('mounts')
  110. ->where($deleteQuery->expr()->eq('storage_id', $deleteQuery->createParameter('storageid')));
  111. $deletedInLastChunk = self::CHUNK_SIZE;
  112. while ($deletedInLastChunk === self::CHUNK_SIZE) {
  113. $deletedInLastChunk = 0;
  114. $result = $query->execute();
  115. while ($row = $result->fetch()) {
  116. $deletedInLastChunk++;
  117. $deletedEntries += $deleteQuery->setParameter('storageid', (int) $row['storage_id'])
  118. ->execute();
  119. }
  120. $result->closeCursor();
  121. }
  122. return $deletedEntries;
  123. }
  124. }