1
0

File.php 5.2 KB

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