You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. ->addOption('storage-tree', null, InputOption::VALUE_NONE, "Show storage and cache wrapping tree");
  40. }
  41. public function execute(InputInterface $input, OutputInterface $output): int {
  42. $fileInput = $input->getArgument('file');
  43. $showChildren = $input->getOption('children');
  44. $node = $this->fileUtils->getNode($fileInput);
  45. if (!$node) {
  46. $output->writeln("<error>file $fileInput not found</error>");
  47. return 1;
  48. }
  49. $output->writeln($node->getName());
  50. $output->writeln(" fileid: " . $node->getId());
  51. $output->writeln(" mimetype: " . $node->getMimetype());
  52. $output->writeln(" modified: " . (string)$this->l10n->l("datetime", $node->getMTime()));
  53. $output->writeln(" " . ($node->isEncrypted() ? "encrypted" : "not encrypted"));
  54. if ($node->isEncrypted()) {
  55. $keyPath = $this->encryptionUtil->getFileKeyDir('', $node->getPath());
  56. if ($this->rootView->file_exists($keyPath)) {
  57. $output->writeln(" encryption key at: " . $keyPath);
  58. } else {
  59. $output->writeln(" <error>encryption key not found</error> should be located at: " . $keyPath);
  60. }
  61. }
  62. $output->writeln(" size: " . Util::humanFileSize($node->getSize()));
  63. $output->writeln(" etag: " . $node->getEtag());
  64. if ($node instanceof Folder) {
  65. $children = $node->getDirectoryListing();
  66. $childSize = array_sum(array_map(function (Node $node) {
  67. return $node->getSize();
  68. }, $children));
  69. if ($childSize != $node->getSize()) {
  70. $output->writeln(" <error>warning: folder has a size of " . Util::humanFileSize($node->getSize()) ." but it's children sum up to " . Util::humanFileSize($childSize) . "</error>.");
  71. $output->writeln(" Run <info>occ files:scan --path " . $node->getPath() . "</info> to attempt to resolve this.");
  72. }
  73. if ($showChildren) {
  74. $output->writeln(" children: " . count($children) . ":");
  75. foreach ($children as $child) {
  76. $output->writeln(" - " . $child->getName());
  77. }
  78. } else {
  79. $output->writeln(" children: " . count($children) . " (use <info>--children</info> option to list)");
  80. }
  81. }
  82. $this->outputStorageDetails($node->getMountPoint(), $node, $input, $output);
  83. $filesPerUser = $this->fileUtils->getFilesByUser($node);
  84. $output->writeln("");
  85. $output->writeln("The following users have access to the file");
  86. $output->writeln("");
  87. foreach ($filesPerUser as $user => $files) {
  88. $output->writeln("$user:");
  89. foreach ($files as $userFile) {
  90. $output->writeln(" " . $userFile->getPath() . ": " . $this->fileUtils->formatPermissions($userFile->getType(), $userFile->getPermissions()));
  91. $mount = $userFile->getMountPoint();
  92. $output->writeln(" " . $this->fileUtils->formatMountType($mount));
  93. }
  94. }
  95. return 0;
  96. }
  97. /**
  98. * @psalm-suppress UndefinedClass
  99. * @psalm-suppress UndefinedInterfaceMethod
  100. */
  101. private function outputStorageDetails(IMountPoint $mountPoint, Node $node, InputInterface $input, OutputInterface $output): void {
  102. $storage = $mountPoint->getStorage();
  103. if (!$storage) {
  104. return;
  105. }
  106. if (!$storage->instanceOfStorage(IHomeStorage::class)) {
  107. $output->writeln(" mounted at: " . $mountPoint->getMountPoint());
  108. }
  109. if ($storage->instanceOfStorage(ObjectStoreStorage::class)) {
  110. /** @var ObjectStoreStorage $storage */
  111. $objectStoreId = $storage->getObjectStore()->getStorageId();
  112. $parts = explode(':', $objectStoreId);
  113. /** @var string $bucket */
  114. $bucket = array_pop($parts);
  115. $output->writeln(" bucket: " . $bucket);
  116. if ($node instanceof \OC\Files\Node\File) {
  117. $output->writeln(" object id: " . $storage->getURN($node->getId()));
  118. try {
  119. $fh = $node->fopen('r');
  120. if (!$fh) {
  121. throw new NotFoundException();
  122. }
  123. $stat = fstat($fh);
  124. fclose($fh);
  125. if ($stat['size'] !== $node->getSize()) {
  126. $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");
  127. }
  128. } catch (\Exception $e) {
  129. $output->writeln(" <error>warning: object not found in bucket</error>");
  130. }
  131. }
  132. } else {
  133. if (!$storage->file_exists($node->getInternalPath())) {
  134. $output->writeln(" <error>warning: file not found in storage</error>");
  135. }
  136. }
  137. if ($mountPoint instanceof ExternalMountPoint) {
  138. $storageConfig = $mountPoint->getStorageConfig();
  139. $output->writeln(" external storage id: " . $storageConfig->getId());
  140. $output->writeln(" external type: " . $storageConfig->getBackend()->getText());
  141. } elseif ($mountPoint instanceof GroupMountPoint) {
  142. $output->writeln(" groupfolder id: " . $mountPoint->getFolderId());
  143. }
  144. if ($input->getOption('storage-tree')) {
  145. $storageTmp = $storage;
  146. $storageClass = get_class($storageTmp).' (cache:'.get_class($storageTmp->getCache()).')';
  147. while ($storageTmp instanceof \OC\Files\Storage\Wrapper\Wrapper) {
  148. $storageTmp = $storageTmp->getWrapperStorage();
  149. $storageClass .= "\n\t".'> '.get_class($storageTmp).' (cache:'.get_class($storageTmp->getCache()).')';
  150. }
  151. $output->writeln(" storage wrapping: " . $storageClass);
  152. }
  153. }
  154. }