Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add command to list mounts for user #42817

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion apps/files_external/lib/Lib/Storage/AmazonS3.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,13 @@
use OCP\Constants;
use OCP\Files\FileInfo;
use OCP\Files\IMimeTypeDetector;
use OCP\Files\Storage\IStorageDebugInfo;
use OCP\ICache;
use OCP\ICacheFactory;
use OCP\Server;
use Psr\Log\LoggerInterface;

class AmazonS3 extends \OC\Files\Storage\Common {
class AmazonS3 extends \OC\Files\Storage\Common implements IStorageDebugInfo {

Check notice

Code scanning / Psalm

DeprecatedInterface Note

OCP\Files\Storage is marked deprecated

Check notice

Code scanning / Psalm

PropertyNotSetInConstructor Note

Property OCA\Files_External\Lib\Storage\AmazonS3::$connection is not defined in constructor of OCA\Files_External\Lib\Storage\AmazonS3 or in any private or final methods called in the constructor

Check notice

Code scanning / Psalm

PropertyNotSetInConstructor Note

Property OCA\Files_External\Lib\Storage\AmazonS3::$bucket is not defined in constructor of OCA\Files_External\Lib\Storage\AmazonS3 or in any private or final methods called in the constructor

Check notice

Code scanning / Psalm

PropertyNotSetInConstructor Note

Property OCA\Files_External\Lib\Storage\AmazonS3::$timeout is not defined in constructor of OCA\Files_External\Lib\Storage\AmazonS3 or in any private or final methods called in the constructor

Check notice

Code scanning / Psalm

PropertyNotSetInConstructor Note

Property OCA\Files_External\Lib\Storage\AmazonS3::$proxy is not defined in constructor of OCA\Files_External\Lib\Storage\AmazonS3 or in any private or final methods called in the constructor

Check notice

Code scanning / Psalm

PropertyNotSetInConstructor Note

Property OCA\Files_External\Lib\Storage\AmazonS3::$storageClass is not defined in constructor of OCA\Files_External\Lib\Storage\AmazonS3 or in any private or final methods called in the constructor

Check notice

Code scanning / Psalm

PropertyNotSetInConstructor Note

Property OCA\Files_External\Lib\Storage\AmazonS3::$uploadPartSize is not defined in constructor of OCA\Files_External\Lib\Storage\AmazonS3 or in any private or final methods called in the constructor

Check notice

Code scanning / Psalm

PropertyNotSetInConstructor Note

Property OCA\Files_External\Lib\Storage\AmazonS3::$putSizeLimit is not defined in constructor of OCA\Files_External\Lib\Storage\AmazonS3 or in any private or final methods called in the constructor

Check notice

Code scanning / Psalm

PropertyNotSetInConstructor Note

Property OCA\Files_External\Lib\Storage\AmazonS3::$copySizeLimit is not defined in constructor of OCA\Files_External\Lib\Storage\AmazonS3 or in any private or final methods called in the constructor
use S3ConnectionTrait;
use S3ObjectTrait;

Expand Down Expand Up @@ -787,4 +788,8 @@
return true;
}
}

function debugInfo(): string {
return "s3 bucket {$this->bucket}";
}
}
7 changes: 6 additions & 1 deletion apps/files_external/lib/Lib/Storage/FTP.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@
use OC\Files\Storage\PolyFill\CopyDirectory;
use OCP\Constants;
use OCP\Files\FileInfo;
use OCP\Files\Storage\IStorageDebugInfo;
use OCP\Files\StorageNotAvailableException;
use Psr\Log\LoggerInterface;

class FTP extends Common {
class FTP extends Common implements IStorageDebugInfo {

Check notice

Code scanning / Psalm

DeprecatedInterface Note

OCP\Files\Storage is marked deprecated
use CopyDirectory;

private $root;
Expand Down Expand Up @@ -377,4 +378,8 @@
yield $data;
}
}

function debugInfo(): string {
return "ftp share {$this->username}@{$this->host}/{$this->root}";
}
}
7 changes: 6 additions & 1 deletion apps/files_external/lib/Lib/Storage/SFTP.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,14 @@
use OCP\Constants;
use OCP\Files\FileInfo;
use OCP\Files\IMimeTypeDetector;
use OCP\Files\Storage\IStorageDebugInfo;
use phpseclib\Net\SFTP\Stream;

/**
* Uses phpseclib's Net\SFTP class and the Net\SFTP\Stream stream wrapper to
* provide access to SFTP servers.
*/
class SFTP extends Common {
class SFTP extends Common implements IStorageDebugInfo {

Check notice

Code scanning / Psalm

DeprecatedInterface Note

OCP\Files\Storage is marked deprecated
private $host;
private $user;
private $root;
Expand Down Expand Up @@ -243,7 +244,7 @@
try {
$keyPath = $this->hostKeysPath();
if ($keyPath && file_exists($keyPath)) {
$fp = fopen($keyPath, 'w');

Check failure on line 247 in apps/files_external/lib/Lib/Storage/SFTP.php

View workflow job for this annotation

GitHub Actions / static-code-analysis-security

TaintedFile

apps/files_external/lib/Lib/Storage/SFTP.php:247:17: TaintedFile: Detected tainted file handling (see https://psalm.dev/255)
foreach ($keys as $host => $key) {
fwrite($fp, $host . '::' . $key . "\n");
}
Expand All @@ -264,7 +265,7 @@
if (file_exists($keyPath)) {
$hosts = [];
$keys = [];
$lines = file($keyPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

Check failure on line 268 in apps/files_external/lib/Lib/Storage/SFTP.php

View workflow job for this annotation

GitHub Actions / static-code-analysis-security

TaintedFile

apps/files_external/lib/Lib/Storage/SFTP.php:268:19: TaintedFile: Detected tainted file handling (see https://psalm.dev/255)
if ($lines) {
foreach ($lines as $line) {
$hostKeyArray = explode("::", $line, 2);
Expand Down Expand Up @@ -409,7 +410,7 @@
case 'c':
case 'c+':
$context = stream_context_create(['sftp' => ['session' => $connection]]);
$handle = fopen($this->constructUrl($path), $mode, false, $context);

Check failure on line 413 in apps/files_external/lib/Lib/Storage/SFTP.php

View workflow job for this annotation

GitHub Actions / static-code-analysis-security

TaintedFile

apps/files_external/lib/Lib/Storage/SFTP.php:413:22: TaintedFile: Detected tainted file handling (see https://psalm.dev/255)
return RetryWrapper::wrap($handle);
}
} catch (\Exception $e) {
Expand Down Expand Up @@ -584,4 +585,8 @@
$keys = ['size', 'mtime', 'mimetype', 'etag', 'storage_mtime', 'permissions', 'name'];
return array_intersect_key($stat, array_flip($keys));
}

function debugInfo(): string {
return "sftp share {$this->user}@{$this->host}/{$this->root}";
}
}
26 changes: 25 additions & 1 deletion apps/files_external/lib/Lib/Storage/SMB.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
namespace OCA\Files_External\Lib\Storage;

use Icewind\SMB\ACL;
use Icewind\SMB\AnonymousAuth;
use Icewind\SMB\BasicAuth;
use Icewind\SMB\Exception\AlreadyExistsException;
use Icewind\SMB\Exception\ConnectException;
Expand All @@ -47,7 +48,9 @@
use Icewind\SMB\Exception\NotFoundException;
use Icewind\SMB\Exception\OutOfSpaceException;
use Icewind\SMB\Exception\TimedOutException;
use Icewind\SMB\IAuth;
use Icewind\SMB\IFileInfo;
use Icewind\SMB\KerberosAuth;
use Icewind\SMB\Native\NativeServer;
use Icewind\SMB\Options;
use Icewind\SMB\ServerFactory;
Expand All @@ -64,11 +67,12 @@
use OCP\Files\Notify\IRenameChange;
use OCP\Files\NotPermittedException;
use OCP\Files\Storage\INotifyStorage;
use OCP\Files\Storage\IStorageDebugInfo;
use OCP\Files\StorageAuthException;
use OCP\Files\StorageNotAvailableException;
use Psr\Log\LoggerInterface;

class SMB extends Common implements INotifyStorage {
class SMB extends Common implements INotifyStorage, IStorageDebugInfo {

Check notice

Code scanning / Psalm

DeprecatedInterface Note

OCP\Files\Storage is marked deprecated
/**
* @var \Icewind\SMB\IServer
*/
Expand Down Expand Up @@ -98,10 +102,14 @@
/** @var bool */
protected $checkAcl;

private array $params;
private IAuth $auth;

public function __construct($params) {
if (!isset($params['host'])) {
throw new \Exception('Invalid configuration, no host provided');
}
$this->params = $params;

if (isset($params['auth'])) {
$auth = $params['auth'];
Expand Down Expand Up @@ -133,6 +141,7 @@
}
}
$serverFactory = new ServerFactory($options);
$this->auth = $auth;
$this->server = $serverFactory->createServer($params['host'], $auth);
$this->share = $this->server->getShare(trim($params['share'], '/'));

Expand Down Expand Up @@ -782,4 +791,19 @@
$shareNotifyHandler = $this->share->notify($this->buildPath($path));
return new SMBNotifyHandler($shareNotifyHandler, $this->root);
}

public function debugInfo(): string {
$share = "//{$this->server->getHost()}/{$this->share->getName()}{$this->root}";
if (isset($this->params['user'])) {
$share = "{$this->params['user']}@$share";
}
if ($this->auth instanceof KerberosAuth) {
$auth = "kerberos";
} else if ($this->auth instanceof AnonymousAuth) {
$auth = "anonymous";
} else {
$auth = "password";
}
return "SMB share $share using $auth authentication";
}
}
7 changes: 6 additions & 1 deletion apps/files_external/lib/Lib/Storage/Swift.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@
use Icewind\Streams\IteratorDirectory;
use OC\Files\ObjectStore\SwiftFactory;
use OCP\Files\IMimeTypeDetector;
use OCP\Files\Storage\IStorageDebugInfo;
use OCP\Files\StorageBadConfigException;
use OpenStack\Common\Error\BadResponseError;
use OpenStack\ObjectStore\v1\Models\StorageObject;
use Psr\Log\LoggerInterface;

class Swift extends \OC\Files\Storage\Common {
class Swift extends \OC\Files\Storage\Common implements IStorageDebugInfo {

Check notice

Code scanning / Psalm

DeprecatedInterface Note

OCP\Files\Storage is marked deprecated
/** @var SwiftFactory */
private $connectionFactory;
/**
Expand Down Expand Up @@ -627,4 +628,8 @@
public static function checkDependencies() {
return true;
}

function debugInfo(): string {
return "swift bucket {$this->bucket}";
}
}
7 changes: 6 additions & 1 deletion apps/files_sharing/lib/External/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
use OCP\Files\NotFoundException;
use OCP\Files\Storage\IDisableEncryptionStorage;
use OCP\Files\Storage\IReliableEtagStorage;
use OCP\Files\Storage\IStorageDebugInfo;
use OCP\Files\StorageInvalidException;
use OCP\Files\StorageNotAvailableException;
use OCP\Http\Client\IClientService;
Expand All @@ -60,7 +61,7 @@
use OCP\Server;
use Psr\Log\LoggerInterface;

class Storage extends DAV implements ISharedStorage, IDisableEncryptionStorage, IReliableEtagStorage {
class Storage extends DAV implements ISharedStorage, IDisableEncryptionStorage, IReliableEtagStorage, IStorageDebugInfo {

Check notice

Code scanning / Psalm

DeprecatedInterface Note

OCP\Files\Storage is marked deprecated

Check notice

Code scanning / Psalm

PropertyNotSetInConstructor Note

Property OCA\Files_Sharing\External\Storage::$certPath is not defined in constructor of OCA\Files_Sharing\External\Storage or in any private or final methods called in the constructor

Check notice

Code scanning / Psalm

PropertyNotSetInConstructor Note

Property OCA\Files_Sharing\External\Storage::$ready is not defined in constructor of OCA\Files_Sharing\External\Storage or in any private or final methods called in the constructor

Check notice

Code scanning / Psalm

PropertyNotSetInConstructor Note

Property OCA\Files_Sharing\External\Storage::$client is not defined in constructor of OCA\Files_Sharing\External\Storage or in any private or final methods called in the constructor

Check notice

Code scanning / Psalm

PropertyNotSetInConstructor Note

Property OCA\Files_Sharing\External\Storage::$certManager is not defined in constructor of OCA\Files_Sharing\External\Storage or in any private or final methods called in the constructor
private ICloudId $cloudId;
private string $mountPoint;
private string $token;
Expand Down Expand Up @@ -453,4 +454,8 @@
public function free_space($path) {
return parent::free_space("");
}

function debugInfo(): string {
return "external share from {$this->getRemoteUser()}@{$this->getRemote()}";
}
}
38 changes: 37 additions & 1 deletion apps/files_sharing/lib/SharedStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,15 @@
use OCP\Files\NotFoundException;
use OCP\Files\Storage\IDisableEncryptionStorage;
use OCP\Files\Storage\IStorage;
use OCP\Files\Storage\IStorageDebugInfo;
use OCP\Lock\ILockingProvider;
use OCP\Share\IShare;
use Psr\Log\LoggerInterface;

/**
* Convert target path to source path and pass the function call to the correct storage provider
*/
class SharedStorage extends \OC\Files\Storage\Wrapper\Jail implements ISharedStorage, IDisableEncryptionStorage {
class SharedStorage extends \OC\Files\Storage\Wrapper\Jail implements ISharedStorage, IDisableEncryptionStorage, IStorageDebugInfo {

Check notice

Code scanning / Psalm

DeprecatedInterface Note

OCP\Files\Storage is marked deprecated
/** @var \OCP\Share\IShare */
private $superShare;

Expand Down Expand Up @@ -562,4 +563,39 @@
$this->init();
return parent::getUnjailedPath($path);
}

function debugInfo(): string {
$share = $this->getShare();
$shares = $this->groupedShares;
$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;
}

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() . ")";
}
}
}
50 changes: 10 additions & 40 deletions core/Command/Info/FileUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use OCP\Files\Mount\IMountPoint;
use OCP\Files\Node;
use OCP\Files\NotFoundException;
use OCP\Files\Storage\IStorageDebugInfo;
use OCP\Share\IShare;
use OCP\Util;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -123,48 +124,17 @@
*/
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();

if ($mountPoint instanceof ExternalMountPoint) {
$prefix = "external storage " . $mountPoint->getStorageConfig()->getId() . ": ";
} elseif ($mountPoint instanceof CircleMount) {
return "circle";
$prefix = "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() . ")";
if ($storage->instanceOfStorage(IStorageDebugInfo::class)) {

Check failure on line 133 in core/Command/Info/FileUtils.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

InvalidArgument

core/Command/Info/FileUtils.php:133:35: InvalidArgument: Argument 1 of OCP\Files\Storage\IStorage::instanceOfStorage expects class-string<OCP\Files\Storage\IStorage>, but OCP\Files\Storage\IStorageDebugInfo::class provided (see https://psalm.dev/004)

Check failure

Code scanning / Psalm

InvalidArgument Error

Argument 1 of OCP\Files\Storage\IStorage::instanceOfStorage expects class-string<OCP\Files\Storage\IStorage>, but OCP\Files\Storage\IStorageDebugInfo::class provided

Check notice

Code scanning / Psalm

PossiblyNullReference Note

Cannot call method instanceOfStorage on possibly null value
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

technically the complaint is right, the interface is not an IStorage. Does instance of not work here? Or method_exists in worst case?

/** @var IStorageDebugInfo $storage */
return $prefix . $storage->debugInfo();

Check notice

Code scanning / Psalm

PossiblyUndefinedVariable Note

Possibly undefined variable $prefix, first seen on line 129
} else {
return $prefix . get_class($mountPoint);

Check notice

Code scanning / Psalm

PossiblyUndefinedVariable Note

Possibly undefined variable $prefix, first seen on line 129
}
}

Expand Down
69 changes: 69 additions & 0 deletions core/Command/Info/UserMounts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);
/**
* @copyright Copyright (c) 2024 Robin Appelman <robin@icewind.nl>
*
* @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 <http://www.gnu.org/licenses/>.
*
*/

namespace OC\Core\Command\Info;

use OCP\Files\IRootFolder;
use OCP\Files\Mount\IMountPoint;
use OCP\IUserManager;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class UserMounts extends Command {
public function __construct(
private FileUtils $fileUtils,
private IUserManager $userManager,
private IRootFolder $rootFolder,
) {
parent::__construct();
}

protected function configure(): void {
$this
->setName('info:user:mounts')
->setDescription('list mounted storages available for a user')
->addArgument('user', InputArgument::REQUIRED, "User id to get mounted storages for");
}

public function execute(InputInterface $input, OutputInterface $output): int {
$userId = $input->getArgument('user');
$user = $this->userManager->get($userId);
if (!$user) {
$output->writeln("<error>user $userId not found</error>");
return 1;
}

$userFolder = $this->rootFolder->getUserFolder($userId);
$mounts = $this->rootFolder->getMountsIn($userFolder->getPath());
$mounts[] = $userFolder->getMountPoint();
usort($mounts, fn (IMountPoint $a, IMountPoint $b) => $a->getMountPoint() <=> $b->getMountPoint());

foreach ($mounts as $mount) {
$mountInfo = $this->fileUtils->formatMountType($mount);
$output->writeln("<info>{$mount->getMountPoint()}</info>: $mountInfo");
}
return 0;
}
}
1 change: 1 addition & 0 deletions core/register_command.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@

$application->add(\OC::$server->get(OC\Core\Command\Info\File::class));
$application->add(\OC::$server->get(OC\Core\Command\Info\Space::class));
$application->add(\OC::$server->get(OC\Core\Command\Info\UserMounts::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)));
Expand Down
Loading
Loading