File.php 6.6 KB

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