Skip to content

Commit 24df3e7

Browse files
authored
Merge pull request #56447 from nextcloud/mount-commands
occ commands for listing and refreshing mounts
2 parents 9fcc293 + ef5e014 commit 24df3e7

File tree

5 files changed

+139
-0
lines changed

5 files changed

+139
-0
lines changed

apps/files/appinfo/info.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
<command>OCA\Files\Command\Copy</command>
4848
<command>OCA\Files\Command\Move</command>
4949
<command>OCA\Files\Command\SanitizeFilenames</command>
50+
<command>OCA\Files\Command\Mount\Refresh</command>
51+
<command>OCA\Files\Command\Mount\ListMounts</command>
5052
<command>OCA\Files\Command\Object\Delete</command>
5153
<command>OCA\Files\Command\Object\Get</command>
5254
<command>OCA\Files\Command\Object\Put</command>

apps/files/composer/composer/autoload_classmap.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
'OCA\\Files\\Command\\Delete' => $baseDir . '/../lib/Command/Delete.php',
3434
'OCA\\Files\\Command\\DeleteOrphanedFiles' => $baseDir . '/../lib/Command/DeleteOrphanedFiles.php',
3535
'OCA\\Files\\Command\\Get' => $baseDir . '/../lib/Command/Get.php',
36+
'OCA\\Files\\Command\\Mount\\ListMounts' => $baseDir . '/../lib/Command/Mount/ListMounts.php',
37+
'OCA\\Files\\Command\\Mount\\Refresh' => $baseDir . '/../lib/Command/Mount/Refresh.php',
3638
'OCA\\Files\\Command\\Move' => $baseDir . '/../lib/Command/Move.php',
3739
'OCA\\Files\\Command\\Object\\Delete' => $baseDir . '/../lib/Command/Object/Delete.php',
3840
'OCA\\Files\\Command\\Object\\Get' => $baseDir . '/../lib/Command/Object/Get.php',

apps/files/composer/composer/autoload_static.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ class ComposerStaticInitFiles
4848
'OCA\\Files\\Command\\Delete' => __DIR__ . '/..' . '/../lib/Command/Delete.php',
4949
'OCA\\Files\\Command\\DeleteOrphanedFiles' => __DIR__ . '/..' . '/../lib/Command/DeleteOrphanedFiles.php',
5050
'OCA\\Files\\Command\\Get' => __DIR__ . '/..' . '/../lib/Command/Get.php',
51+
'OCA\\Files\\Command\\Mount\\ListMounts' => __DIR__ . '/..' . '/../lib/Command/Mount/ListMounts.php',
52+
'OCA\\Files\\Command\\Mount\\Refresh' => __DIR__ . '/..' . '/../lib/Command/Mount/Refresh.php',
5153
'OCA\\Files\\Command\\Move' => __DIR__ . '/..' . '/../lib/Command/Move.php',
5254
'OCA\\Files\\Command\\Object\\Delete' => __DIR__ . '/..' . '/../lib/Command/Object/Delete.php',
5355
'OCA\\Files\\Command\\Object\\Get' => __DIR__ . '/..' . '/../lib/Command/Object/Get.php',
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
6+
* SPDX-License-Identifier: AGPL-3.0-or-later
7+
*/
8+
9+
namespace OCA\Files\Command\Mount;
10+
11+
use OCP\Files\Config\ICachedMountInfo;
12+
use OCP\Files\Config\IMountProviderCollection;
13+
use OCP\Files\Config\IUserMountCache;
14+
use OCP\Files\Mount\IMountPoint;
15+
use OCP\IUserManager;
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\Output\OutputInterface;
20+
21+
class ListMounts extends Command {
22+
public function __construct(
23+
private readonly IUserManager $userManager,
24+
private readonly IUserMountCache $userMountCache,
25+
private readonly IMountProviderCollection $mountProviderCollection,
26+
) {
27+
parent::__construct();
28+
}
29+
30+
protected function configure(): void {
31+
$this
32+
->setName('files:mount:list')
33+
->setDescription('List of mounts for a user')
34+
->addArgument('user', InputArgument::REQUIRED, 'User to list mounts for');
35+
}
36+
37+
public function execute(InputInterface $input, OutputInterface $output): int {
38+
$userId = $input->getArgument('user');
39+
$user = $this->userManager->get($userId);
40+
if (!$user) {
41+
$output->writeln("<error>User $userId not found</error>");
42+
return 1;
43+
}
44+
45+
$mounts = $this->mountProviderCollection->getMountsForUser($user);
46+
$mounts[] = $this->mountProviderCollection->getHomeMountForUser($user);
47+
/** @var array<string, IMountPoint> $cachedByMountpoint */
48+
$mountsByMountpoint = array_combine(array_map(fn (IMountPoint $mount) => $mount->getMountPoint(), $mounts), $mounts);
49+
usort($mounts, fn (IMountPoint $a, IMountPoint $b) => $a->getMountPoint() <=> $b->getMountPoint());
50+
51+
$cachedMounts = $this->userMountCache->getMountsForUser($user);
52+
usort($cachedMounts, fn (ICachedMountInfo $a, ICachedMountInfo $b) => $a->getMountPoint() <=> $b->getMountPoint());
53+
/** @var array<string, ICachedMountInfo> $cachedByMountpoint */
54+
$cachedByMountpoint = array_combine(array_map(fn (ICachedMountInfo $mount) => $mount->getMountPoint(), $cachedMounts), $cachedMounts);
55+
56+
foreach ($mounts as $mount) {
57+
$output->writeln('<info>' . $mount->getMountPoint() . '</info>: ' . $mount->getStorageId());
58+
if (isset($cachedByMountpoint[$mount->getMountPoint()])) {
59+
$cached = $cachedByMountpoint[$mount->getMountPoint()];
60+
$output->writeln("\t- provider: " . $cached->getMountProvider());
61+
$output->writeln("\t- storage id: " . $cached->getStorageId());
62+
$output->writeln("\t- root id: " . $cached->getRootId());
63+
} else {
64+
$output->writeln("\t<error>not registered</error>");
65+
}
66+
}
67+
foreach ($cachedMounts as $cachedMount) {
68+
if (!isset($mountsByMountpoint[$cachedMount->getMountPoint()])) {
69+
$output->writeln('<info>' . $cachedMount->getMountPoint() . '</info>:');
70+
$output->writeln("\t<error>registered but no longer provided</error>");
71+
$output->writeln("\t- provider: " . $cachedMount->getMountProvider());
72+
$output->writeln("\t- storage id: " . $cachedMount->getStorageId());
73+
$output->writeln("\t- root id: " . $cachedMount->getRootId());
74+
}
75+
}
76+
77+
return 0;
78+
}
79+
80+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
6+
* SPDX-License-Identifier: AGPL-3.0-or-later
7+
*/
8+
9+
namespace OCA\Files\Command\Mount;
10+
11+
use OCP\Files\Config\IMountProviderCollection;
12+
use OCP\Files\Config\IUserMountCache;
13+
use OCP\IUserManager;
14+
use Symfony\Component\Console\Command\Command;
15+
use Symfony\Component\Console\Input\InputArgument;
16+
use Symfony\Component\Console\Input\InputInterface;
17+
use Symfony\Component\Console\Output\OutputInterface;
18+
19+
class Refresh extends Command {
20+
public function __construct(
21+
private readonly IUserManager $userManager,
22+
private readonly IUserMountCache $userMountCache,
23+
private readonly IMountProviderCollection $mountProviderCollection,
24+
) {
25+
parent::__construct();
26+
}
27+
28+
protected function configure(): void {
29+
$this
30+
->setName('files:mount:refresh')
31+
->setDescription('Refresh the list of mounts for a user')
32+
->addArgument('user', InputArgument::REQUIRED, 'User to refresh mounts for');
33+
}
34+
35+
public function execute(InputInterface $input, OutputInterface $output): int {
36+
$userId = $input->getArgument('user');
37+
$user = $this->userManager->get($userId);
38+
if (!$user) {
39+
$output->writeln("<error>User $userId not found</error>");
40+
return 1;
41+
}
42+
43+
$mounts = $this->mountProviderCollection->getMountsForUser($user);
44+
$mounts[] = $this->mountProviderCollection->getHomeMountForUser($user);
45+
46+
$this->userMountCache->registerMounts($user, $mounts);
47+
48+
$output->writeln('Registered <info>' . count($mounts) . '</info> mounts');
49+
50+
return 0;
51+
}
52+
53+
}

0 commit comments

Comments
 (0)