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

Disks filtering by mount points from a configurable parameter #572

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
44 changes: 43 additions & 1 deletion lib/Os.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@

class Os implements IOperatingSystem {
private IOperatingSystem $backend;
private IConfig $config;

public function __construct(IConfig $config) {
$restrictedMode = $config->getAppValue('serverinfo', 'restricted_mode', 'no') === 'yes';
$this->backend = $this->getBackend($restrictedMode ? 'Dummy' : PHP_OS);
$this->config = $config;
}

public function supported(): bool {
Expand Down Expand Up @@ -70,7 +72,47 @@ public function getUptime(): int {
}

public function getDiskInfo(): array {
return $this->backend->getDiskInfo();
$disks = $this->backend->getDiskInfo();
$filters = $this->config->getSystemValue('serverinfo_disk_filter_paths', null);
Copy link
Collaborator

Choose a reason for hiding this comment

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

We have two configuration systems in Nextcloud: SystemConfig and AppConfig

I'm using AppConfig here in serverinfo nowadays.

Suggested change
$filters = $this->config->getSystemValue('serverinfo_disk_filter_paths', null);
$filters = $this->config->getAppValue('disk_filter', '');

You can also use occ to configure it.
For example like https://github.com/nextcloud/serverinfo?tab=readme-ov-file#restricted-mode--nextcloud-28

if ($filters === null) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
if ($filters === null) {
if ($filters === '') {

If you follow my suggestion from above, don't forget to replace null with empty string.

return $disks;
}
// apply defined filters to restrict the list of disks according to their mount point
$filtered_disks = [];
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
$filtered_disks = [];
$filteredDisks = [];

CamelCase please ;)

foreach (explode(':', $filters) as $filter) {
// convert special filters to their corresponding paths
switch ($filter) {
case 'DOCROOT': $path = '';
if (isset($_SERVER['SCRIPT_FILENAME'])) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't like the docroot special filter because it might be empty.

A good alternative might be \OC::$SERVERROOT which always point to the nextcloud installation dir.

$path = dirname($_SERVER['SCRIPT_FILENAME']);
}
elseif (isset($_SERVER['DOCUMENT_ROOT'])) {
$path = $_SERVER['DOCUMENT_ROOT'];
}
break;
case 'DATADIR': $path = $this->config->getSystemValue('datadirectory', '');
break;
case 'TEMPDIR': $path = $this->config->getSystemValue('tempdirectory', '');
break;
case 'LOGSDIR': $path = dirname($this->config->getSystemValue('logfile', ''));
break;
default: $path = $filter;
}
// find the disk whose mount point contains the filtering path
$find_index = null;
$find_mount = '';
foreach ($disks as $index => $disk) {
$mount = $disk->getMount();
if ((strncmp($path, $mount, strlen($mount)) === 0) && (strlen($mount) >= strlen($find_mount))) {
$find_mount = $mount;
$find_index = $index;
}
}
if ($find_index !== null) {
$filtered_disks[ $find_index ] = $disks[ $find_index ];
}
}
return $filtered_disks;
}

/**
Expand Down
Loading