File.php 5.2 KB

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