1
0

DeleteOrphanedFiles.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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\IDBConnection;
  27. use Symfony\Component\Console\Command\Command;
  28. use Symfony\Component\Console\Input\InputInterface;
  29. use Symfony\Component\Console\Output\OutputInterface;
  30. /**
  31. * Delete all file entries that have no matching entries in the storage table.
  32. */
  33. class DeleteOrphanedFiles extends Command {
  34. public const CHUNK_SIZE = 200;
  35. public function __construct(
  36. protected IDBConnection $connection,
  37. ) {
  38. parent::__construct();
  39. }
  40. protected function configure(): void {
  41. $this
  42. ->setName('files:cleanup')
  43. ->setDescription('cleanup filecache');
  44. }
  45. public function execute(InputInterface $input, OutputInterface $output): int {
  46. $deletedEntries = 0;
  47. $query = $this->connection->getQueryBuilder();
  48. $query->select('fc.fileid')
  49. ->from('filecache', 'fc')
  50. ->where($query->expr()->isNull('s.numeric_id'))
  51. ->leftJoin('fc', 'storages', 's', $query->expr()->eq('fc.storage', 's.numeric_id'))
  52. ->setMaxResults(self::CHUNK_SIZE);
  53. $deleteQuery = $this->connection->getQueryBuilder();
  54. $deleteQuery->delete('filecache')
  55. ->where($deleteQuery->expr()->eq('fileid', $deleteQuery->createParameter('objectid')));
  56. $deletedInLastChunk = self::CHUNK_SIZE;
  57. while ($deletedInLastChunk === self::CHUNK_SIZE) {
  58. $deletedInLastChunk = 0;
  59. $result = $query->execute();
  60. while ($row = $result->fetch()) {
  61. $deletedInLastChunk++;
  62. $deletedEntries += $deleteQuery->setParameter('objectid', (int) $row['fileid'])
  63. ->execute();
  64. }
  65. $result->closeCursor();
  66. }
  67. $output->writeln("$deletedEntries orphaned file cache entries deleted");
  68. $deletedMounts = $this->cleanupOrphanedMounts();
  69. $output->writeln("$deletedMounts orphaned mount entries deleted");
  70. return self::SUCCESS;
  71. }
  72. private function cleanupOrphanedMounts(): int {
  73. $deletedEntries = 0;
  74. $query = $this->connection->getQueryBuilder();
  75. $query->select('m.storage_id')
  76. ->from('mounts', 'm')
  77. ->where($query->expr()->isNull('s.numeric_id'))
  78. ->leftJoin('m', 'storages', 's', $query->expr()->eq('m.storage_id', 's.numeric_id'))
  79. ->groupBy('storage_id')
  80. ->setMaxResults(self::CHUNK_SIZE);
  81. $deleteQuery = $this->connection->getQueryBuilder();
  82. $deleteQuery->delete('mounts')
  83. ->where($deleteQuery->expr()->eq('storage_id', $deleteQuery->createParameter('storageid')));
  84. $deletedInLastChunk = self::CHUNK_SIZE;
  85. while ($deletedInLastChunk === self::CHUNK_SIZE) {
  86. $deletedInLastChunk = 0;
  87. $result = $query->execute();
  88. while ($row = $result->fetch()) {
  89. $deletedInLastChunk++;
  90. $deletedEntries += $deleteQuery->setParameter('storageid', (int) $row['storage_id'])
  91. ->execute();
  92. }
  93. $result->closeCursor();
  94. }
  95. return $deletedEntries;
  96. }
  97. }