File.php 6.4 KB

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