File.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. declare(strict_types=1);
  3. namespace OC\Core\Command\Info;
  4. use OC\Files\ObjectStore\ObjectStoreStorage;
  5. use OC\Files\View;
  6. use OCA\Files_External\Config\ExternalMountPoint;
  7. use OCA\GroupFolders\Mount\GroupMountPoint;
  8. use OCP\Files\Folder;
  9. use OCP\Files\IHomeStorage;
  10. use OCP\Files\Mount\IMountPoint;
  11. use OCP\Files\Node;
  12. use OCP\Files\NotFoundException;
  13. use OCP\IL10N;
  14. use OCP\L10N\IFactory;
  15. use OCP\Util;
  16. use Symfony\Component\Console\Command\Command;
  17. use Symfony\Component\Console\Input\InputArgument;
  18. use Symfony\Component\Console\Input\InputInterface;
  19. use Symfony\Component\Console\Input\InputOption;
  20. use Symfony\Component\Console\Output\OutputInterface;
  21. class File extends Command {
  22. private IL10N $l10n;
  23. private View $rootView;
  24. public function __construct(
  25. IFactory $l10nFactory,
  26. private FileUtils $fileUtils,
  27. private \OC\Encryption\Util $encryptionUtil
  28. ) {
  29. $this->l10n = $l10nFactory->get("core");
  30. parent::__construct();
  31. $this->rootView = new View();
  32. }
  33. protected function configure(): void {
  34. $this
  35. ->setName('info:file')
  36. ->setDescription('get information for a file')
  37. ->addArgument('file', InputArgument::REQUIRED, "File id or path")
  38. ->addOption('children', 'c', InputOption::VALUE_NONE, "List children of folders");
  39. }
  40. public function execute(InputInterface $input, OutputInterface $output): int {
  41. $fileInput = $input->getArgument('file');
  42. $showChildren = $input->getOption('children');
  43. $node = $this->fileUtils->getNode($fileInput);
  44. if (!$node) {
  45. $output->writeln("<error>file $fileInput not found</error>");
  46. return 1;
  47. }
  48. $output->writeln($node->getName());
  49. $output->writeln(" fileid: " . $node->getId());
  50. $output->writeln(" mimetype: " . $node->getMimetype());
  51. $output->writeln(" modified: " . (string)$this->l10n->l("datetime", $node->getMTime()));
  52. $output->writeln(" " . ($node->isEncrypted() ? "encrypted" : "not encrypted"));
  53. if ($node->isEncrypted()) {
  54. $keyPath = $this->encryptionUtil->getFileKeyDir('', $node->getPath());
  55. if ($this->rootView->file_exists($keyPath)) {
  56. $output->writeln(" encryption key at: " . $keyPath);
  57. } else {
  58. $output->writeln(" <error>encryption key not found</error> should be located at: " . $keyPath);
  59. }
  60. }
  61. $output->writeln(" size: " . Util::humanFileSize($node->getSize()));
  62. $output->writeln(" etag: " . $node->getEtag());
  63. if ($node instanceof Folder) {
  64. $children = $node->getDirectoryListing();
  65. $childSize = array_sum(array_map(function (Node $node) {
  66. return $node->getSize();
  67. }, $children));
  68. if ($childSize != $node->getSize()) {
  69. $output->writeln(" <error>warning: folder has a size of " . Util::humanFileSize($node->getSize()) ." but it's children sum up to " . Util::humanFileSize($childSize) . "</error>.");
  70. $output->writeln(" Run <info>occ files:scan --path " . $node->getPath() . "</info> to attempt to resolve this.");
  71. }
  72. if ($showChildren) {
  73. $output->writeln(" children: " . count($children) . ":");
  74. foreach ($children as $child) {
  75. $output->writeln(" - " . $child->getName());
  76. }
  77. } else {
  78. $output->writeln(" children: " . count($children) . " (use <info>--children</info> option to list)");
  79. }
  80. }
  81. $this->outputStorageDetails($node->getMountPoint(), $node, $output);
  82. $filesPerUser = $this->fileUtils->getFilesByUser($node);
  83. $output->writeln("");
  84. $output->writeln("The following users have access to the file");
  85. $output->writeln("");
  86. foreach ($filesPerUser as $user => $files) {
  87. $output->writeln("$user:");
  88. foreach ($files as $userFile) {
  89. $output->writeln(" " . $userFile->getPath() . ": " . $this->fileUtils->formatPermissions($userFile->getType(), $userFile->getPermissions()));
  90. $mount = $userFile->getMountPoint();
  91. $output->writeln(" " . $this->fileUtils->formatMountType($mount));
  92. }
  93. }
  94. return 0;
  95. }
  96. /**
  97. * @psalm-suppress UndefinedClass
  98. * @psalm-suppress UndefinedInterfaceMethod
  99. */
  100. private function outputStorageDetails(IMountPoint $mountPoint, Node $node, OutputInterface $output): void {
  101. $storage = $mountPoint->getStorage();
  102. if (!$storage) {
  103. return;
  104. }
  105. if (!$storage->instanceOfStorage(IHomeStorage::class)) {
  106. $output->writeln(" mounted at: " . $mountPoint->getMountPoint());
  107. }
  108. if ($storage->instanceOfStorage(ObjectStoreStorage::class)) {
  109. /** @var ObjectStoreStorage $storage */
  110. $objectStoreId = $storage->getObjectStore()->getStorageId();
  111. $parts = explode(':', $objectStoreId);
  112. /** @var string $bucket */
  113. $bucket = array_pop($parts);
  114. $output->writeln(" bucket: " . $bucket);
  115. if ($node instanceof \OC\Files\Node\File) {
  116. $output->writeln(" object id: " . $storage->getURN($node->getId()));
  117. try {
  118. $fh = $node->fopen('r');
  119. if (!$fh) {
  120. throw new NotFoundException();
  121. }
  122. $stat = fstat($fh);
  123. fclose($fh);
  124. if ($stat['size'] !== $node->getSize()) {
  125. $output->writeln(" <error>warning: object had a size of " . $stat['size'] . " but cache entry has a size of " . $node->getSize() . "</error>. This should have been automatically repaired");
  126. }
  127. } catch (\Exception $e) {
  128. $output->writeln(" <error>warning: object not found in bucket</error>");
  129. }
  130. }
  131. } else {
  132. if (!$storage->file_exists($node->getInternalPath())) {
  133. $output->writeln(" <error>warning: file not found in storage</error>");
  134. }
  135. }
  136. if ($mountPoint instanceof ExternalMountPoint) {
  137. $storageConfig = $mountPoint->getStorageConfig();
  138. $output->writeln(" external storage id: " . $storageConfig->getId());
  139. $output->writeln(" external type: " . $storageConfig->getBackend()->getText());
  140. } elseif ($mountPoint instanceof GroupMountPoint) {
  141. $output->writeln(" groupfolder id: " . $mountPoint->getFolderId());
  142. }
  143. }
  144. }