diff options
Diffstat (limited to 'core/Command/Info')
-rw-r--r-- | core/Command/Info/File.php | 93 | ||||
-rw-r--r-- | core/Command/Info/FileUtils.php | 155 | ||||
-rw-r--r-- | core/Command/Info/Space.php | 8 | ||||
-rw-r--r-- | core/Command/Info/Storage.php | 49 | ||||
-rw-r--r-- | core/Command/Info/Storages.php | 43 |
5 files changed, 281 insertions, 67 deletions
diff --git a/core/Command/Info/File.php b/core/Command/Info/File.php index e86d8d0a5bd..287bd0e29cb 100644 --- a/core/Command/Info/File.php +++ b/core/Command/Info/File.php @@ -8,9 +8,12 @@ declare(strict_types=1); namespace OC\Core\Command\Info; use OC\Files\ObjectStore\ObjectStoreStorage; +use OC\Files\Storage\Wrapper\Encryption; +use OC\Files\Storage\Wrapper\Wrapper; use OC\Files\View; use OCA\Files_External\Config\ExternalMountPoint; use OCA\GroupFolders\Mount\GroupMountPoint; +use OCP\Files\File as OCPFile; use OCP\Files\Folder; use OCP\Files\IHomeStorage; use OCP\Files\Mount\IMountPoint; @@ -32,9 +35,9 @@ class File extends Command { public function __construct( IFactory $l10nFactory, private FileUtils $fileUtils, - private \OC\Encryption\Util $encryptionUtil + private \OC\Encryption\Util $encryptionUtil, ) { - $this->l10n = $l10nFactory->get("core"); + $this->l10n = $l10nFactory->get('core'); parent::__construct(); $this->rootView = new View(); } @@ -43,9 +46,9 @@ class File extends Command { $this ->setName('info:file') ->setDescription('get information for a file') - ->addArgument('file', InputArgument::REQUIRED, "File id or path") - ->addOption('children', 'c', InputOption::VALUE_NONE, "List children of folders") - ->addOption('storage-tree', null, InputOption::VALUE_NONE, "Show storage and cache wrapping tree"); + ->addArgument('file', InputArgument::REQUIRED, 'File id or path') + ->addOption('children', 'c', InputOption::VALUE_NONE, 'List children of folders') + ->addOption('storage-tree', null, InputOption::VALUE_NONE, 'Show storage and cache wrapping tree'); } public function execute(InputInterface $input, OutputInterface $output): int { @@ -58,50 +61,66 @@ class File extends Command { } $output->writeln($node->getName()); - $output->writeln(" fileid: " . $node->getId()); - $output->writeln(" mimetype: " . $node->getMimetype()); - $output->writeln(" modified: " . (string)$this->l10n->l("datetime", $node->getMTime())); - $output->writeln(" " . ($node->isEncrypted() ? "encrypted" : "not encrypted")); - if ($node->isEncrypted()) { + $output->writeln(' fileid: ' . $node->getId()); + $output->writeln(' mimetype: ' . $node->getMimetype()); + $output->writeln(' modified: ' . (string)$this->l10n->l('datetime', $node->getMTime())); + + if ($node instanceof OCPFile && $node->isEncrypted()) { + $output->writeln(' ' . 'server-side encrypted: yes'); $keyPath = $this->encryptionUtil->getFileKeyDir('', $node->getPath()); if ($this->rootView->file_exists($keyPath)) { - $output->writeln(" encryption key at: " . $keyPath); + $output->writeln(' encryption key at: ' . $keyPath); + } else { + $output->writeln(' <error>encryption key not found</error> should be located at: ' . $keyPath); + } + $storage = $node->getStorage(); + if ($storage->instanceOfStorage(Encryption::class)) { + /** @var Encryption $storage */ + if (!$storage->hasValidHeader($node->getInternalPath())) { + $output->writeln(' <error>file doesn\'t have a valid encryption header</error>'); + } } else { - $output->writeln(" <error>encryption key not found</error> should be located at: " . $keyPath); + $output->writeln(' <error>file is marked as encrypted, but encryption doesn\'t seem to be setup</error>'); } } - $output->writeln(" size: " . Util::humanFileSize($node->getSize())); - $output->writeln(" etag: " . $node->getEtag()); + + if ($node instanceof Folder && $node->isEncrypted() || $node instanceof OCPFile && $node->getParent()->isEncrypted()) { + $output->writeln(' ' . 'end-to-end encrypted: yes'); + } + + $output->writeln(' size: ' . Util::humanFileSize($node->getSize())); + $output->writeln(' etag: ' . $node->getEtag()); + $output->writeln(' permissions: ' . $this->fileUtils->formatPermissions($node->getType(), $node->getPermissions())); if ($node instanceof Folder) { $children = $node->getDirectoryListing(); $childSize = array_sum(array_map(function (Node $node) { return $node->getSize(); }, $children)); if ($childSize != $node->getSize()) { - $output->writeln(" <error>warning: folder has a size of " . Util::humanFileSize($node->getSize()) ." but it's children sum up to " . Util::humanFileSize($childSize) . "</error>."); - $output->writeln(" Run <info>occ files:scan --path " . $node->getPath() . "</info> to attempt to resolve this."); + $output->writeln(' <error>warning: folder has a size of ' . Util::humanFileSize($node->getSize()) . " but it's children sum up to " . Util::humanFileSize($childSize) . '</error>.'); + $output->writeln(' Run <info>occ files:scan --path ' . $node->getPath() . '</info> to attempt to resolve this.'); } if ($showChildren) { - $output->writeln(" children: " . count($children) . ":"); + $output->writeln(' children: ' . count($children) . ':'); foreach ($children as $child) { - $output->writeln(" - " . $child->getName()); + $output->writeln(' - ' . $child->getName()); } } else { - $output->writeln(" children: " . count($children) . " (use <info>--children</info> option to list)"); + $output->writeln(' children: ' . count($children) . ' (use <info>--children</info> option to list)'); } } $this->outputStorageDetails($node->getMountPoint(), $node, $input, $output); $filesPerUser = $this->fileUtils->getFilesByUser($node); - $output->writeln(""); - $output->writeln("The following users have access to the file"); - $output->writeln(""); + $output->writeln(''); + $output->writeln('The following users have access to the file'); + $output->writeln(''); foreach ($filesPerUser as $user => $files) { $output->writeln("$user:"); foreach ($files as $userFile) { - $output->writeln(" " . $userFile->getPath() . ": " . $this->fileUtils->formatPermissions($userFile->getType(), $userFile->getPermissions())); + $output->writeln(' ' . $userFile->getPath() . ': ' . $this->fileUtils->formatPermissions($userFile->getType(), $userFile->getPermissions())); $mount = $userFile->getMountPoint(); - $output->writeln(" " . $this->fileUtils->formatMountType($mount)); + $output->writeln(' ' . $this->fileUtils->formatMountType($mount)); } } @@ -118,7 +137,7 @@ class File extends Command { return; } if (!$storage->instanceOfStorage(IHomeStorage::class)) { - $output->writeln(" mounted at: " . $mountPoint->getMountPoint()); + $output->writeln(' mounted at: ' . $mountPoint->getMountPoint()); } if ($storage->instanceOfStorage(ObjectStoreStorage::class)) { /** @var ObjectStoreStorage $storage */ @@ -126,9 +145,9 @@ class File extends Command { $parts = explode(':', $objectStoreId); /** @var string $bucket */ $bucket = array_pop($parts); - $output->writeln(" bucket: " . $bucket); + $output->writeln(' bucket: ' . $bucket); if ($node instanceof \OC\Files\Node\File) { - $output->writeln(" object id: " . $storage->getURN($node->getId())); + $output->writeln(' object id: ' . $storage->getURN($node->getId())); try { $fh = $node->fopen('r'); if (!$fh) { @@ -137,32 +156,32 @@ class File extends Command { $stat = fstat($fh); fclose($fh); if ($stat['size'] !== $node->getSize()) { - $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"); + $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'); } } catch (\Exception $e) { - $output->writeln(" <error>warning: object not found in bucket</error>"); + $output->writeln(' <error>warning: object not found in bucket</error>'); } } } else { if (!$storage->file_exists($node->getInternalPath())) { - $output->writeln(" <error>warning: file not found in storage</error>"); + $output->writeln(' <error>warning: file not found in storage</error>'); } } if ($mountPoint instanceof ExternalMountPoint) { $storageConfig = $mountPoint->getStorageConfig(); - $output->writeln(" external storage id: " . $storageConfig->getId()); - $output->writeln(" external type: " . $storageConfig->getBackend()->getText()); + $output->writeln(' external storage id: ' . $storageConfig->getId()); + $output->writeln(' external type: ' . $storageConfig->getBackend()->getText()); } elseif ($mountPoint instanceof GroupMountPoint) { - $output->writeln(" groupfolder id: " . $mountPoint->getFolderId()); + $output->writeln(' groupfolder id: ' . $mountPoint->getFolderId()); } if ($input->getOption('storage-tree')) { $storageTmp = $storage; - $storageClass = get_class($storageTmp).' (cache:'.get_class($storageTmp->getCache()).')'; - while ($storageTmp instanceof \OC\Files\Storage\Wrapper\Wrapper) { + $storageClass = get_class($storageTmp) . ' (cache:' . get_class($storageTmp->getCache()) . ')'; + while ($storageTmp instanceof Wrapper) { $storageTmp = $storageTmp->getWrapperStorage(); - $storageClass .= "\n\t".'> '.get_class($storageTmp).' (cache:'.get_class($storageTmp->getCache()).')'; + $storageClass .= "\n\t" . '> ' . get_class($storageTmp) . ' (cache:' . get_class($storageTmp->getCache()) . ')'; } - $output->writeln(" storage wrapping: " . $storageClass); + $output->writeln(' storage wrapping: ' . $storageClass); } } diff --git a/core/Command/Info/FileUtils.php b/core/Command/Info/FileUtils.php index 6c490d31c3d..bc07535a289 100644 --- a/core/Command/Info/FileUtils.php +++ b/core/Command/Info/FileUtils.php @@ -8,11 +8,13 @@ declare(strict_types=1); namespace OC\Core\Command\Info; +use OC\User\NoUserException; use OCA\Circles\MountManager\CircleMount; use OCA\Files_External\Config\ExternalMountPoint; use OCA\Files_Sharing\SharedMount; use OCA\GroupFolders\Mount\GroupMountPoint; use OCP\Constants; +use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\Files\Config\IUserMountCache; use OCP\Files\FileInfo; use OCP\Files\Folder; @@ -21,22 +23,28 @@ use OCP\Files\IRootFolder; use OCP\Files\Mount\IMountPoint; use OCP\Files\Node; use OCP\Files\NotFoundException; +use OCP\Files\NotPermittedException; +use OCP\IDBConnection; use OCP\Share\IShare; use OCP\Util; use Symfony\Component\Console\Output\OutputInterface; +/** + * @psalm-type StorageInfo array{numeric_id: int, id: string, available: bool, last_checked: ?\DateTime, files: int, mount_id: ?int} + */ class FileUtils { public function __construct( private IRootFolder $rootFolder, private IUserMountCache $userMountCache, + private IDBConnection $connection, ) { } /** * @param FileInfo $file * @return array<string, Node[]> - * @throws \OCP\Files\NotPermittedException - * @throws \OC\User\NoUserException + * @throws NotPermittedException + * @throws NoUserException */ public function getFilesByUser(FileInfo $file): array { $id = $file->getId(); @@ -46,13 +54,12 @@ class FileUtils { $mounts = $this->userMountCache->getMountsForFileId($id); $result = []; - foreach ($mounts as $mount) { - if (isset($result[$mount->getUser()->getUID()])) { - continue; - } - - $userFolder = $this->rootFolder->getUserFolder($mount->getUser()->getUID()); - $result[$mount->getUser()->getUID()] = $userFolder->getById($id); + foreach ($mounts as $cachedMount) { + $mount = $this->rootFolder->getMount($cachedMount->getMountPoint()); + $cache = $mount->getStorage()->getCache(); + $cacheEntry = $cache->get($id); + $node = $this->rootFolder->getNodeFromCacheEntryAndMount($cacheEntry, $mount); + $result[$cachedMount->getUser()->getUID()][] = $node; } return $result; @@ -84,18 +91,18 @@ class FileUtils { public function formatPermissions(string $type, int $permissions): string { if ($permissions == Constants::PERMISSION_ALL || ($type === 'file' && $permissions == (Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE))) { - return "full permissions"; + return 'full permissions'; } $perms = []; - $allPerms = [Constants::PERMISSION_READ => "read", Constants::PERMISSION_UPDATE => "update", Constants::PERMISSION_CREATE => "create", Constants::PERMISSION_DELETE => "delete", Constants::PERMISSION_SHARE => "share"]; + $allPerms = [Constants::PERMISSION_READ => 'read', Constants::PERMISSION_UPDATE => 'update', Constants::PERMISSION_CREATE => 'create', Constants::PERMISSION_DELETE => 'delete', Constants::PERMISSION_SHARE => 'share']; foreach ($allPerms as $perm => $name) { if (($permissions & $perm) === $perm) { $perms[] = $name; } } - return implode(", ", $perms); + return implode(', ', $perms); } /** @@ -105,29 +112,29 @@ class FileUtils { public function formatMountType(IMountPoint $mountPoint): string { $storage = $mountPoint->getStorage(); if ($storage && $storage->instanceOfStorage(IHomeStorage::class)) { - return "home storage"; + return 'home storage'; } elseif ($mountPoint instanceof SharedMount) { $share = $mountPoint->getShare(); $shares = $mountPoint->getGroupedShares(); $sharedBy = array_map(function (IShare $share) { $shareType = $this->formatShareType($share); if ($shareType) { - return $share->getSharedBy() . " (via " . $shareType . " " . $share->getSharedWith() . ")"; + return $share->getSharedBy() . ' (via ' . $shareType . ' ' . $share->getSharedWith() . ')'; } else { return $share->getSharedBy(); } }, $shares); - $description = "shared by " . implode(', ', $sharedBy); + $description = 'shared by ' . implode(', ', $sharedBy); if ($share->getSharedBy() !== $share->getShareOwner()) { - $description .= " owned by " . $share->getShareOwner(); + $description .= ' owned by ' . $share->getShareOwner(); } return $description; } elseif ($mountPoint instanceof GroupMountPoint) { - return "groupfolder " . $mountPoint->getFolderId(); + return 'groupfolder ' . $mountPoint->getFolderId(); } elseif ($mountPoint instanceof ExternalMountPoint) { - return "external storage " . $mountPoint->getStorageConfig()->getId(); + return 'external storage ' . $mountPoint->getStorageConfig()->getId(); } elseif ($mountPoint instanceof CircleMount) { - return "circle"; + return 'circle'; } return get_class($mountPoint); } @@ -135,17 +142,17 @@ class FileUtils { public function formatShareType(IShare $share): ?string { switch ($share->getShareType()) { case IShare::TYPE_GROUP: - return "group"; + return 'group'; case IShare::TYPE_CIRCLE: - return "circle"; + return 'circle'; case IShare::TYPE_DECK: - return "deck"; + return 'deck'; case IShare::TYPE_ROOM: - return "room"; + return 'room'; case IShare::TYPE_USER: return null; default: - return "Unknown (" . $share->getShareType() . ")"; + return 'Unknown (' . $share->getShareType() . ')'; } } @@ -197,7 +204,7 @@ class FileUtils { $count += 1; /** @var Node $child */ - $output->writeln("$prefix- " . $child->getName() . ": <info>" . Util::humanFileSize($child->getSize()) . "</info>"); + $output->writeln("$prefix- " . $child->getName() . ': <info>' . Util::humanFileSize($child->getSize()) . '</info>'); if ($child instanceof Folder) { $recurseSizeLimits = $sizeLimits; if (!$all) { @@ -212,11 +219,107 @@ class FileUtils { } sort($recurseSizeLimits); } - $recurseCount = $this->outputLargeFilesTree($output, $child, $prefix . " ", $recurseSizeLimits, $all); + $recurseCount = $this->outputLargeFilesTree($output, $child, $prefix . ' ', $recurseSizeLimits, $all); $sizeLimits = array_slice($sizeLimits, $recurseCount); $count += $recurseCount; } } return $count; } + + public function getNumericStorageId(string $id): ?int { + if (is_numeric($id)) { + return (int)$id; + } + $query = $this->connection->getQueryBuilder(); + $query->select('numeric_id') + ->from('storages') + ->where($query->expr()->eq('id', $query->createNamedParameter($id))); + $result = $query->executeQuery()->fetchOne(); + return $result ? (int)$result : null; + } + + /** + * @param int|null $limit + * @return ?StorageInfo + * @throws \OCP\DB\Exception + */ + public function getStorage(int $id): ?array { + $query = $this->connection->getQueryBuilder(); + $query->select('numeric_id', 's.id', 'available', 'last_checked', 'mount_id') + ->selectAlias($query->func()->count('fileid'), 'files') + ->from('storages', 's') + ->innerJoin('s', 'filecache', 'f', $query->expr()->eq('f.storage', 's.numeric_id')) + ->leftJoin('s', 'mounts', 'm', $query->expr()->eq('s.numeric_id', 'm.storage_id')) + ->where($query->expr()->eq('s.numeric_id', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT))) + ->groupBy('s.numeric_id', 's.id', 's.available', 's.last_checked', 'mount_id'); + $row = $query->executeQuery()->fetch(); + if ($row) { + return [ + 'numeric_id' => $row['numeric_id'], + 'id' => $row['id'], + 'files' => $row['files'], + 'available' => (bool)$row['available'], + 'last_checked' => $row['last_checked'] ? new \DateTime('@' . $row['last_checked']) : null, + 'mount_id' => $row['mount_id'], + ]; + } else { + return null; + } + } + + /** + * @param int|null $limit + * @return \Iterator<StorageInfo> + * @throws \OCP\DB\Exception + */ + public function listStorages(?int $limit): \Iterator { + $query = $this->connection->getQueryBuilder(); + $query->select('numeric_id', 's.id', 'available', 'last_checked', 'mount_id') + ->selectAlias($query->func()->count('fileid'), 'files') + ->from('storages', 's') + ->innerJoin('s', 'filecache', 'f', $query->expr()->eq('f.storage', 's.numeric_id')) + ->leftJoin('s', 'mounts', 'm', $query->expr()->eq('s.numeric_id', 'm.storage_id')) + ->groupBy('s.numeric_id', 's.id', 's.available', 's.last_checked', 'mount_id') + ->orderBy('files', 'DESC'); + if ($limit !== null) { + $query->setMaxResults($limit); + } + $result = $query->executeQuery(); + while ($row = $result->fetch()) { + yield [ + 'numeric_id' => $row['numeric_id'], + 'id' => $row['id'], + 'files' => $row['files'], + 'available' => (bool)$row['available'], + 'last_checked' => $row['last_checked'] ? new \DateTime('@' . $row['last_checked']) : null, + 'mount_id' => $row['mount_id'], + ]; + } + } + + /** + * @param StorageInfo $storage + * @return array + */ + public function formatStorage(array $storage): array { + return [ + 'numeric_id' => $storage['numeric_id'], + 'id' => $storage['id'], + 'files' => $storage['files'], + 'available' => $storage['available'] ? 'true' : 'false', + 'last_checked' => $storage['last_checked']?->format(\DATE_ATOM), + 'external_mount_id' => $storage['mount_id'], + ]; + } + + /** + * @param \Iterator<StorageInfo> $storages + * @return \Iterator + */ + public function formatStorages(\Iterator $storages): \Iterator { + foreach ($storages as $storage) { + yield $this->formatStorage($storage); + } + } } diff --git a/core/Command/Info/Space.php b/core/Command/Info/Space.php index 645deae04b2..35c1d5c3228 100644 --- a/core/Command/Info/Space.php +++ b/core/Command/Info/Space.php @@ -27,9 +27,9 @@ class Space extends Command { $this ->setName('info:file:space') ->setDescription('Summarize space usage of specified folder') - ->addArgument('file', InputArgument::REQUIRED, "File id or path") - ->addOption('count', 'c', InputOption::VALUE_REQUIRED, "Number of items to display", 25) - ->addOption('all', 'a', InputOption::VALUE_NONE, "Display all items"); + ->addArgument('file', InputArgument::REQUIRED, 'File id or path') + ->addOption('count', 'c', InputOption::VALUE_REQUIRED, 'Number of items to display', 25) + ->addOption('all', 'a', InputOption::VALUE_NONE, 'Display all items'); } public function execute(InputInterface $input, OutputInterface $output): int { @@ -41,7 +41,7 @@ class Space extends Command { $output->writeln("<error>file $fileInput not found</error>"); return 1; } - $output->writeln($node->getName() . ": <info>" . Util::humanFileSize($node->getSize()) . "</info>"); + $output->writeln($node->getName() . ': <info>' . Util::humanFileSize($node->getSize()) . '</info>'); if ($node instanceof Folder) { $limits = $all ? [] : array_fill(0, $count - 1, 0); $this->fileUtils->outputLargeFilesTree($output, $node, '', $limits, $all); diff --git a/core/Command/Info/Storage.php b/core/Command/Info/Storage.php new file mode 100644 index 00000000000..c1d0e1725ca --- /dev/null +++ b/core/Command/Info/Storage.php @@ -0,0 +1,49 @@ +<?php + +declare(strict_types=1); +/** + * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +namespace OC\Core\Command\Info; + +use OC\Core\Command\Base; +use OCP\IDBConnection; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; + +class Storage extends Base { + public function __construct( + private readonly IDBConnection $connection, + private readonly FileUtils $fileUtils, + ) { + parent::__construct(); + } + + protected function configure(): void { + parent::configure(); + $this + ->setName('info:storage') + ->setDescription('Get information a single storage') + ->addArgument('storage', InputArgument::REQUIRED, 'Storage to get information for'); + } + + public function execute(InputInterface $input, OutputInterface $output): int { + $storage = $input->getArgument('storage'); + $storageId = $this->fileUtils->getNumericStorageId($storage); + if (!$storageId) { + $output->writeln('<error>No storage with id ' . $storage . ' found</error>'); + return 1; + } + + $info = $this->fileUtils->getStorage($storageId); + if (!$info) { + $output->writeln('<error>No storage with id ' . $storage . ' found</error>'); + return 1; + } + $this->writeArrayInOutputFormat($input, $output, $this->fileUtils->formatStorage($info)); + return 0; + } +} diff --git a/core/Command/Info/Storages.php b/core/Command/Info/Storages.php new file mode 100644 index 00000000000..ff767a2ff5d --- /dev/null +++ b/core/Command/Info/Storages.php @@ -0,0 +1,43 @@ +<?php + +declare(strict_types=1); +/** + * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +namespace OC\Core\Command\Info; + +use OC\Core\Command\Base; +use OCP\IDBConnection; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; + +class Storages extends Base { + public function __construct( + private readonly IDBConnection $connection, + private readonly FileUtils $fileUtils, + ) { + parent::__construct(); + } + + protected function configure(): void { + parent::configure(); + $this + ->setName('info:storages') + ->setDescription('List storages ordered by the number of files') + ->addOption('count', 'c', InputOption::VALUE_REQUIRED, 'Number of storages to display', 25) + ->addOption('all', 'a', InputOption::VALUE_NONE, 'Display all storages'); + } + + public function execute(InputInterface $input, OutputInterface $output): int { + $count = (int)$input->getOption('count'); + $all = $input->getOption('all'); + + $limit = $all ? null : $count; + $storages = $this->fileUtils->listStorages($limit); + $this->writeStreamingTableInOutputFormat($input, $output, $this->fileUtils->formatStorages($storages), 100); + return 0; + } +} |