From f15ed557f535a3ac06dfa8661654ebb50350746b Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Fri, 14 Apr 2023 19:16:29 +0200 Subject: [PATCH] [wip] add command for getting user file information Signed-off-by: Robin Appelman --- core/Command/Info/File.php | 108 +--------------- core/Command/Info/FileUtils.php | 215 ++++++++++++++++++++++++++++++++ core/Command/Info/UserFiles.php | 102 +++++++++++++++ core/register_command.php | 1 + 4 files changed, 324 insertions(+), 102 deletions(-) create mode 100644 core/Command/Info/FileUtils.php create mode 100644 core/Command/Info/UserFiles.php diff --git a/core/Command/Info/File.php b/core/Command/Info/File.php index cd15cb04f725f..ddf339b251203 100644 --- a/core/Command/Info/File.php +++ b/core/Command/Info/File.php @@ -5,13 +5,9 @@ namespace OC\Core\Command\Info; use OC\Files\ObjectStore\ObjectStoreStorage; -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\Files\Config\IUserMountCache; -use OCP\Files\FileInfo; use OCP\Files\Folder; use OCP\Files\IHomeStorage; use OCP\Files\IRootFolder; @@ -20,7 +16,6 @@ use OCP\Files\NotFoundException; use OCP\IL10N; use OCP\L10N\IFactory; -use OCP\Share\IShare; use OCP\Util; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; @@ -32,11 +27,13 @@ class File extends Command { private IRootFolder $rootFolder; private IUserMountCache $userMountCache; private IL10N $l10n; + private FileUtils $fileUtils; - public function __construct(IRootFolder $rootFolder, IUserMountCache $userMountCache, IFactory $l10nFactory) { + public function __construct(IRootFolder $rootFolder, IUserMountCache $userMountCache, IFactory $l10nFactory, FileUtils $fileUtils) { $this->rootFolder = $rootFolder; $this->userMountCache = $userMountCache; $this->l10n = $l10nFactory->get("files"); + $this->fileUtils = $fileUtils; parent::__construct(); } @@ -83,16 +80,16 @@ public function execute(InputInterface $input, OutputInterface $output): int { } $this->outputStorageDetails($node->getMountPoint(), $node, $output); - $filesPerUser = $this->getFilesByUser($node); + $filesPerUser = $this->fileUtils->getFilesByUser($node); $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->formatPermissions($userFile->getType(), $userFile->getPermissions())); + $output->writeln(" " . $userFile->getPath() . ": " . $this->fileUtils->formatPermissions($userFile->getType(), $userFile->getPermissions())); $mount = $userFile->getMountPoint(); - $output->writeln(" " . $this->formatMountType($mount)); + $output->writeln(" " . $this->fileUtils->formatMountType($mount)); } } @@ -121,99 +118,6 @@ private function getNode(string $fileInput): ?Node { } } - /** - * @param FileInfo $file - * @return array - * @throws \OCP\Files\NotPermittedException - * @throws \OC\User\NoUserException - */ - private function getFilesByUser(FileInfo $file): array { - $id = $file->getId(); - if (!$id) { - return []; - } - - $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); - } - - return $result; - } - - private function formatPermissions(string $type, int $permissions): string { - if ($permissions == Constants::PERMISSION_ALL || ($type === 'file' && $permissions == (Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE))) { - 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"]; - foreach ($allPerms as $perm => $name) { - if (($permissions & $perm) === $perm) { - $perms[] = $name; - } - } - - return implode(", ", $perms); - } - - /** - * @psalm-suppress UndefinedClass - * @psalm-suppress UndefinedInterfaceMethod - */ - private function formatMountType(IMountPoint $mountPoint): string { - $storage = $mountPoint->getStorage(); - if ($storage && $storage->instanceOfStorage(IHomeStorage::class)) { - 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() . ")"; - } else { - return $share->getSharedBy(); - } - }, $shares); - $description = "shared by " . implode(', ', $sharedBy); - if ($share->getSharedBy() !== $share->getShareOwner()) { - $description .= " owned by " . $share->getShareOwner(); - } - return $description; - } elseif ($mountPoint instanceof GroupMountPoint) { - return "groupfolder " . $mountPoint->getFolderId(); - } elseif ($mountPoint instanceof ExternalMountPoint) { - return "external storage " . $mountPoint->getStorageConfig()->getId(); - } elseif ($mountPoint instanceof CircleMount) { - return "circle"; - } - return get_class($mountPoint); - } - - private function formatShareType(IShare $share): ?string { - switch ($share->getShareType()) { - case IShare::TYPE_GROUP: - return "group"; - case IShare::TYPE_CIRCLE: - return "circle"; - case IShare::TYPE_DECK: - return "deck"; - case IShare::TYPE_ROOM: - return "room"; - case IShare::TYPE_USER: - return null; - default: - return "Unknown (".$share->getShareType().")"; - } - } - /** * @psalm-suppress UndefinedClass * @psalm-suppress UndefinedInterfaceMethod diff --git a/core/Command/Info/FileUtils.php b/core/Command/Info/FileUtils.php new file mode 100644 index 0000000000000..a630ecaeff899 --- /dev/null +++ b/core/Command/Info/FileUtils.php @@ -0,0 +1,215 @@ + + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OC\Core\Command\Info; + + +use OC\Files\SetupManager; +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\Files\Config\IUserMountCache; +use OCP\Files\FileInfo; +use OCP\Files\IHomeStorage; +use OCP\Files\IRootFolder; +use OCP\Files\Mount\IMountManager; +use OCP\Files\Mount\IMountPoint; +use OCP\Files\Node; +use OCP\IUser; +use OCP\Share\IShare; +use OCP\Util; +use Symfony\Component\Console\Output\OutputInterface; +use OCP\Files\Folder; + +class FileUtils { + private IRootFolder $rootFolder; + private IUserMountCache $userMountCache; + private IMountManager $mountManager; + private SetupManager $setupManager; + + public function __construct( + IRootFolder $rootFolder, + IUserMountCache $userMountCache, + IMountManager $mountManager, + SetupManager $setupManager + ) { + $this->rootFolder = $rootFolder; + $this->userMountCache = $userMountCache; + $this->mountManager = $mountManager; + $this->setupManager = $setupManager; + } + + /** + * @param FileInfo $file + * @return array + * @throws \OCP\Files\NotPermittedException + * @throws \OC\User\NoUserException + */ + public function getFilesByUser(FileInfo $file): array { + $id = $file->getId(); + if (!$id) { + return []; + } + + $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); + } + + return $result; + } + + 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"; + } + + $perms = []; + $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); + } + + /** + * @psalm-suppress UndefinedClass + * @psalm-suppress UndefinedInterfaceMethod + */ + public function formatMountType(IMountPoint $mountPoint): string { + $storage = $mountPoint->getStorage(); + if ($storage && $storage->instanceOfStorage(IHomeStorage::class)) { + 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() . ")"; + } else { + return $share->getSharedBy(); + } + }, $shares); + $description = "shared by " . implode(', ', $sharedBy); + if ($share->getSharedBy() !== $share->getShareOwner()) { + $description .= " owned by " . $share->getShareOwner(); + } + return $description; + } elseif ($mountPoint instanceof GroupMountPoint) { + return "groupfolder " . $mountPoint->getFolderId(); + } elseif ($mountPoint instanceof ExternalMountPoint) { + return "external storage " . $mountPoint->getStorageConfig()->getId(); + } elseif ($mountPoint instanceof CircleMount) { + return "circle"; + } + return get_class($mountPoint); + } + + public function formatShareType(IShare $share): ?string { + switch ($share->getShareType()) { + case IShare::TYPE_GROUP: + return "group"; + case IShare::TYPE_CIRCLE: + return "circle"; + case IShare::TYPE_DECK: + return "deck"; + case IShare::TYPE_ROOM: + return "room"; + case IShare::TYPE_USER: + return null; + default: + return "Unknown (" . $share->getShareType() . ")"; + } + } + + /** + * @param IUser $user + * @return IMountPoint[] + */ + public function getMountsForUser(IUser $user): array { + $this->setupManager->setupForUser($user); + $prefix = "/" . $user->getUID(); + return array_filter($this->mountManager->getAll(), function (IMountPoint $mount) use ($prefix) { + return str_starts_with($mount->getMountPoint(), $prefix); + }); + } + + /** + * Print out the largest count($sizeLimits) files in the directory tree + * + * @param OutputInterface $output + * @param Folder $node + * @param string $prefix + * @param array $sizeLimits largest items that are still in the queue to be printed, ordered ascending + * @return void + */ + public function outputLargeFilesTree( + OutputInterface $output, + Folder $node, + string $prefix, + array &$sizeLimits + ): int { + $count = 0; + $children = $node->getDirectoryListing(); + usort($children, function (Node $a, Node $b) { + return $b->getSize() <=> $a->getSize(); + }); + foreach ($children as $i => $child) { + if (count($sizeLimits) === 0 || $child->getSize() < $sizeLimits[0]) { + return $count; + } + array_shift($sizeLimits); + $count += 1; + +// var_dump(implode(", ", $sizeLimits)); + /** @var Node $child */ + $output->writeln("$prefix- " . $child->getName() . ": " . Util::humanFileSize($child->getSize())); + if ($child instanceof Folder) { + $recurseSizeLimits = $sizeLimits; + for ($j = 0; $j < count($recurseSizeLimits); $j++) { + $nextChildSize = (int)$children[$i + $j]?->getSize(); + if ($nextChildSize > $recurseSizeLimits[0]) { + array_shift($recurseSizeLimits); + $recurseSizeLimits[] = $nextChildSize; + } + } + sort($recurseSizeLimits); + $recurseCount = $this->outputLargeFilesTree($output, $child, $prefix . " ", $recurseSizeLimits); + $sizeLimits = array_slice($sizeLimits, $recurseCount); + $count += $recurseCount; + } + } + } +} diff --git a/core/Command/Info/UserFiles.php b/core/Command/Info/UserFiles.php new file mode 100644 index 0000000000000..0bc9b2d406194 --- /dev/null +++ b/core/Command/Info/UserFiles.php @@ -0,0 +1,102 @@ +rootFolder = $rootFolder; + $this->userManager = $userManager; + $this->l10n = $l10nFactory->get("core"); + $this->fileUtils = $fileUtils; + parent::__construct(); + } + + protected function configure(): void { + $this + ->setName('info:file:user') + ->setDescription('get file information for a user') + ->addArgument('user_id', InputArgument::REQUIRED, "User id") + ->addOption('large-files', 'l', InputOption::VALUE_REQUIRED, "Number of large files and folders to show", 25); + } + + public function execute(InputInterface $input, OutputInterface $output): int { + $userId = $input->getArgument('user_id'); + $user = $this->userManager->get($userId); + if (!$user) { + $output->writeln("usefr $userId not found"); + return 1; + } + + $output->writeln($user->getUID()); + + $mounts = $this->fileUtils->getMountsForUser($user); + $output->writeln(""); + $output->writeln(" available storages:"); + foreach ($mounts as $mount) { + $storage = $mount->getStorage(); + if (!$storage) { + continue; + } + $cache = $storage->getCache(); + $free = Util::humanFileSize($storage->free_space("")); + $output->write(" - " . $mount->getMountPoint() . ": " . $this->fileUtils->formatMountType($mount)); + if ($storage->instanceOfStorage(IHomeStorage::class)) { + $filesInfo = $cache->get("files"); + $trashInfo = $cache->get("files_trashbin"); + $versionsInfo = $cache->get("files_versions"); + $used = Util::humanFileSize($filesInfo ? $filesInfo->getSize() : 0); + $trashUsed = Util::humanFileSize($trashInfo ? $trashInfo->getSize() : 0); + $versionUsed = Util::humanFileSize($versionsInfo ? $versionsInfo->getSize() : 0); + $output->writeln(" ($used in files, $trashUsed in trash, $versionUsed in versions, $free free)"); + } else { + $rootInfo = $cache->get(""); + $used = Util::humanFileSize($rootInfo ? $rootInfo->getSize(): -1); + $output->writeln(" ($used used, $free free)"); + } + } + $output->writeln(""); + $count = (int)$input->getOption("large-files"); + $output->writeln(" large files and folders: "); + $this->fileUtils->outputLargeFilesTree($output, $this->rootFolder->getUserFolder($user->getUID()), " ", array_fill(0, $count, 0)); + + return 0; + } + +} diff --git a/core/register_command.php b/core/register_command.php index 4aac7fbf8ceb7..9ee9cbbf4dcef 100644 --- a/core/register_command.php +++ b/core/register_command.php @@ -104,6 +104,7 @@ $application->add(new OC\Core\Command\Config\System\SetConfig(\OC::$server->getSystemConfig())); $application->add(\OC::$server->get(OC\Core\Command\Info\File::class)); + $application->add(\OC::$server->get(OC\Core\Command\Info\UserFiles::class)); $application->add(new OC\Core\Command\Db\ConvertType(\OC::$server->getConfig(), new \OC\DB\ConnectionFactory(\OC::$server->getSystemConfig()))); $application->add(new OC\Core\Command\Db\ConvertMysqlToMB4(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection(), \OC::$server->getURLGenerator(), \OC::$server->get(LoggerInterface::class)));