1
0

ScanLegacyFormat.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Encryption\Command;
  8. use OC\Files\View;
  9. use OCA\Encryption\Util;
  10. use OCP\IConfig;
  11. use OCP\IUserManager;
  12. use Symfony\Component\Console\Command\Command;
  13. use Symfony\Component\Console\Helper\QuestionHelper;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. class ScanLegacyFormat extends Command {
  17. private View $rootView;
  18. public function __construct(
  19. protected Util $util,
  20. protected IConfig $config,
  21. protected QuestionHelper $questionHelper,
  22. private IUserManager $userManager,
  23. ) {
  24. parent::__construct();
  25. $this->rootView = new View();
  26. }
  27. protected function configure(): void {
  28. $this
  29. ->setName('encryption:scan:legacy-format')
  30. ->setDescription('Scan the files for the legacy format');
  31. }
  32. protected function execute(InputInterface $input, OutputInterface $output): int {
  33. $result = true;
  34. $output->writeln('Scanning all files for legacy encryption');
  35. foreach ($this->userManager->getBackends() as $backend) {
  36. $limit = 500;
  37. $offset = 0;
  38. do {
  39. $users = $backend->getUsers('', $limit, $offset);
  40. foreach ($users as $user) {
  41. $output->writeln('Scanning all files for ' . $user);
  42. $this->setupUserFS($user);
  43. $result = $result && $this->scanFolder($output, '/' . $user);
  44. }
  45. $offset += $limit;
  46. } while (count($users) >= $limit);
  47. }
  48. if ($result) {
  49. $output->writeln('All scanned files are properly encrypted. You can disable the legacy compatibility mode.');
  50. return self::SUCCESS;
  51. }
  52. return self::FAILURE;
  53. }
  54. private function scanFolder(OutputInterface $output, string $folder): bool {
  55. $clean = true;
  56. foreach ($this->rootView->getDirectoryContent($folder) as $item) {
  57. $path = $folder . '/' . $item['name'];
  58. if ($this->rootView->is_dir($path)) {
  59. if ($this->scanFolder($output, $path) === false) {
  60. $clean = false;
  61. }
  62. } else {
  63. if (!$item->isEncrypted()) {
  64. // ignore
  65. continue;
  66. }
  67. $stats = $this->rootView->stat($path);
  68. if (!isset($stats['hasHeader']) || $stats['hasHeader'] === false) {
  69. $clean = false;
  70. $output->writeln($path . ' does not have a proper header');
  71. }
  72. }
  73. }
  74. return $clean;
  75. }
  76. /**
  77. * setup user file system
  78. */
  79. protected function setupUserFS(string $uid): void {
  80. \OC_Util::tearDownFS();
  81. \OC_Util::setupFS($uid);
  82. }
  83. }