1
0

ScanLegacyFormat.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. /** @var Util */
  18. protected $util;
  19. /** @var IConfig */
  20. protected $config;
  21. /** @var QuestionHelper */
  22. protected $questionHelper;
  23. /** @var IUserManager */
  24. private $userManager;
  25. /** @var View */
  26. private $rootView;
  27. /**
  28. * @param Util $util
  29. * @param IConfig $config
  30. * @param QuestionHelper $questionHelper
  31. */
  32. public function __construct(Util $util,
  33. IConfig $config,
  34. QuestionHelper $questionHelper,
  35. IUserManager $userManager) {
  36. parent::__construct();
  37. $this->util = $util;
  38. $this->config = $config;
  39. $this->questionHelper = $questionHelper;
  40. $this->userManager = $userManager;
  41. $this->rootView = new View();
  42. }
  43. protected function configure() {
  44. $this
  45. ->setName('encryption:scan:legacy-format')
  46. ->setDescription('Scan the files for the legacy format');
  47. }
  48. protected function execute(InputInterface $input, OutputInterface $output): int {
  49. $result = true;
  50. $output->writeln('Scanning all files for legacy encryption');
  51. foreach ($this->userManager->getBackends() as $backend) {
  52. $limit = 500;
  53. $offset = 0;
  54. do {
  55. $users = $backend->getUsers('', $limit, $offset);
  56. foreach ($users as $user) {
  57. $output->writeln('Scanning all files for ' . $user);
  58. $this->setupUserFS($user);
  59. $result = $result && $this->scanFolder($output, '/' . $user);
  60. }
  61. $offset += $limit;
  62. } while (count($users) >= $limit);
  63. }
  64. if ($result) {
  65. $output->writeln('All scanned files are properly encrypted. You can disable the legacy compatibility mode.');
  66. return 0;
  67. }
  68. return 1;
  69. }
  70. private function scanFolder(OutputInterface $output, string $folder): bool {
  71. $clean = true;
  72. foreach ($this->rootView->getDirectoryContent($folder) as $item) {
  73. $path = $folder . '/' . $item['name'];
  74. if ($this->rootView->is_dir($path)) {
  75. if ($this->scanFolder($output, $path) === false) {
  76. $clean = false;
  77. }
  78. } else {
  79. if (!$item->isEncrypted()) {
  80. // ignore
  81. continue;
  82. }
  83. $stats = $this->rootView->stat($path);
  84. if (!isset($stats['hasHeader']) || $stats['hasHeader'] === false) {
  85. $clean = false;
  86. $output->writeln($path . ' does not have a proper header');
  87. }
  88. }
  89. }
  90. return $clean;
  91. }
  92. /**
  93. * setup user file system
  94. *
  95. * @param string $uid
  96. */
  97. protected function setupUserFS($uid) {
  98. \OC_Util::tearDownFS();
  99. \OC_Util::setupFS($uid);
  100. }
  101. }