[PATCH pve-storage v1 05/54] plugin: make get_subdir_files a proper subroutine and update style

Max R. Carrara
2026-04-22T11:13:50Z

This also renames a lot of variables so that they are in line with
the rest of code and also a bit more readable, as follows:

- $fn --> $filename
- $notes_fn --> $notes_filename
- $sid --> $storeid
- $tt --> $vtype

Signed-off-by: Max R. Carrara <m.carrara@proxmox.com>
---
src/PVE/Storage/Plugin.pm | 66 +++++++++++++++++++--------------------
1 file changed, 33 insertions(+), 33 deletions(-)

diff --git a/src/PVE/Storage/Plugin.pm b/src/PVE/Storage/Plugin.pm
index 84ac412a..07233db3 100644
--- a/src/PVE/Storage/Plugin.pm
+++ b/src/PVE/Storage/Plugin.pm
@@ -1669,69 +1669,69 @@ sub list_images {
return $res;
}

-# list templates ($tt = <iso|vztmpl|backup|snippets|import>)
-my $get_subdir_files = sub {
- my ($sid, $path, $tt, $vmid) = @_;
+# $vtype = <iso|vztmpl|backup|snippets|import>
+my sub get_subdir_files {
+ my ($storeid, $path, $vtype, $vmid) = @_;

my $res = [];

- foreach my $fn (<$path/*>) {
- my $st = File::stat::stat($fn);
+ for my $filename (<$path/*>) {
+ my $st = File::stat::stat($filename);

next if (!$st || S_ISDIR($st->mode));

my $info;

- if ($tt eq 'iso') {
- next if $fn !~ m!/([^/]+$PVE::Storage::ISO_EXT_RE_0)$!i;
+ if ($vtype eq 'iso') {
+ next if $filename !~ m!/([^/]+$PVE::Storage::ISO_EXT_RE_0)$!i;

- $info = { volid => "$sid:iso/$1", format => 'iso' };
+ $info = { volid => "$storeid:iso/$1", format => 'iso' };

- } elsif ($tt eq 'vztmpl') {
- next if $fn !~ m!/([^/]+$PVE::Storage::VZTMPL_EXT_RE_1)$!;
+ } elsif ($vtype eq 'vztmpl') {
+ next if $filename !~ m!/([^/]+$PVE::Storage::VZTMPL_EXT_RE_1)$!;

- $info = { volid => "$sid:vztmpl/$1", format => $2 eq 'tar' ? $2 : "t$2" };
+ $info = { volid => "$storeid:vztmpl/$1", format => $2 eq 'tar' ? $2 : "t$2" };

- } elsif ($tt eq 'backup') {
- next if $fn !~ m!/([^/]+$PVE::Storage::BACKUP_EXT_RE_2)$!;
- my $original = $fn;
+ } elsif ($vtype eq 'backup') {
+ next if $filename !~ m!/([^/]+$PVE::Storage::BACKUP_EXT_RE_2)$!;
+ my $original = $filename;
my $format = $2;
- $fn = $1;
+ $filename = $1;

# only match for VMID now, to avoid false positives (VMID in parent directory name)
- next if defined($vmid) && $fn !~ m/\S+-$vmid-\S+/;
+ next if defined($vmid) && $filename !~ m/\S+-$vmid-\S+/;

- $info = { volid => "$sid:backup/$fn", format => $format };
+ $info = { volid => "$storeid:backup/$filename", format => $format };

- my $archive_info = eval { PVE::Storage::archive_info($fn) } // {};
+ my $archive_info = eval { PVE::Storage::archive_info($filename) } // {};

$info->{ctime} = $archive_info->{ctime} if defined($archive_info->{ctime});
$info->{subtype} = $archive_info->{type} // 'unknown';

- if (defined($vmid) || $fn =~ m!\-([1-9][0-9]{2,8})\-[^/]+\.${format}$!) {
+ if (defined($vmid) || $filename =~ m!\-([1-9][0-9]{2,8})\-[^/]+\.${format}$!) {
$info->{vmid} = $vmid // $1;
}

- my $notes_fn = $original . NOTES_EXT;
- if (-f $notes_fn) {
- my $notes = PVE::Tools::file_read_firstline($notes_fn);
+ my $notes_filename = $original . NOTES_EXT;
+ if (-f $notes_filename) {
+ my $notes = PVE::Tools::file_read_firstline($notes_filename);
$info->{notes} = eval { decode('UTF-8', $notes, 1) } // $notes
if defined($notes);
}

$info->{protected} = 1 if -e PVE::Storage::protection_file_path($original);
- } elsif ($tt eq 'snippets') {
+ } elsif ($vtype eq 'snippets') {

$info = {
- volid => "$sid:snippets/" . basename($fn),
+ volid => "$storeid:snippets/" . basename($filename),
format => 'snippet',
};
- } elsif ($tt eq 'import') {
+ } elsif ($vtype eq 'import') {
next
- if $fn !~
+ if $filename !~
m!/(${PVE::Storage::SAFE_CHAR_CLASS_RE}+$PVE::Storage::IMPORT_EXT_RE_1)$!i;

- $info = { volid => "$sid:import/$1", format => "$2" };
+ $info = { volid => "$storeid:import/$1", format => "$2" };
}

$info->{size} = $st->size;
@@ -1741,7 +1741,7 @@ my $get_subdir_files = sub {
}

return $res;
-};
+}

# If attributes are set on a volume, they should be included in the result.
# See get_volume_attribute for a list of possible attributes.
@@ -1759,15 +1759,15 @@ sub list_volumes {
my $path = $class->get_subdir($scfg, $type);

if ($type eq 'iso' && !defined($vmid)) {
- $data = $get_subdir_files->($storeid, $path, 'iso');
+ $data = get_subdir_files($storeid, $path, 'iso');
} elsif ($type eq 'vztmpl' && !defined($vmid)) {
- $data = $get_subdir_files->($storeid, $path, 'vztmpl');
+ $data = get_subdir_files($storeid, $path, 'vztmpl');
} elsif ($type eq 'backup') {
- $data = $get_subdir_files->($storeid, $path, 'backup', $vmid);
+ $data = get_subdir_files($storeid, $path, 'backup', $vmid);
} elsif ($type eq 'snippets') {
- $data = $get_subdir_files->($storeid, $path, 'snippets');
+ $data = get_subdir_files($storeid, $path, 'snippets');
} elsif ($type eq 'import') {
- $data = $get_subdir_files->($storeid, $path, 'import');
+ $data = get_subdir_files($storeid, $path, 'import');
}
}

--
2.47.3