1
0

DeleteOrphanedFiles.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. /**
  36. * @var IDBConnection
  37. */
  38. protected $connection;
  39. public function __construct(IDBConnection $connection) {
  40. $this->connection = $connection;
  41. parent::__construct();
  42. }
  43. protected function configure() {
  44. $this
  45. ->setName('files:cleanup')
  46. ->setDescription('cleanup filecache');
  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. $deletedMounts = $this->cleanupOrphanedMounts();
  72. $output->writeln("$deletedMounts orphaned mount entries deleted");
  73. return 0;
  74. }
  75. private function cleanupOrphanedMounts() {
  76. $deletedEntries = 0;
  77. $query = $this->connection->getQueryBuilder();
  78. $query->select('m.storage_id')
  79. ->from('mounts', 'm')
  80. ->where($query->expr()->isNull('s.numeric_id'))
  81. ->leftJoin('m', 'storages', 's', $query->expr()->eq('m.storage_id', 's.numeric_id'))
  82. ->groupBy('storage_id')
  83. ->setMaxResults(self::CHUNK_SIZE);
  84. $deleteQuery = $this->connection->getQueryBuilder();
  85. $deleteQuery->delete('mounts')
  86. ->where($deleteQuery->expr()->eq('storage_id', $deleteQuery->createParameter('storageid')));
  87. $deletedInLastChunk = self::CHUNK_SIZE;
  88. while ($deletedInLastChunk === self::CHUNK_SIZE) {
  89. $deletedInLastChunk = 0;
  90. $result = $query->execute();
  91. while ($row = $result->fetch()) {
  92. $deletedInLastChunk++;
  93. $deletedEntries += $deleteQuery->setParameter('storageid', (int) $row['storage_id'])
  94. ->execute();
  95. }
  96. $result->closeCursor();
  97. }
  98. return $deletedEntries;
  99. }
  100. }