From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [IPv6:2a0f:8001:1:32::40]) by lore.proxmox.com (Postfix) with ESMTPS id 2DF371FF0AA for ; Tue, 17 Nov 2026 21:41:30 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id A299B21720; Tue, 17 Nov 2026 21:40:51 +0100 (CET) From: "Max R. Carrara" To: pve-devel@lists.proxmox.com Subject: [PATCH pve-storage v3 08/51] plugin: adapt get_subdir_files helper of list_volumes API method Date: Tue, 17 Nov 2026 21:38:40 +0100 Message-ID: <20261117203953.913035-9-m.carrara@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261117203953.913035-1-m.carrara@proxmox.com> References: <20261117203953.913035-1-m.carrara@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1794948010318 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.271 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: CW6XVF63P2J4Z4ID47W2GTFNIGV5PPYN X-Message-ID-Hash: CW6XVF63P2J4Z4ID47W2GTFNIGV5PPYN X-MailFrom: m.carrara@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Wolfgang Bumiller X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Pull the body of the loop inside `get_subdir_files()` out into a closure and break up the if-elsif chain into more readable if-clauses. Strip the vtype subdirectory from the beginning of the file paths being worked on inside that closure. This does not have an effect on how any of the subsequent regexes match on the path / file name, because they match at the end of a given path. Additionally, take the storage config hash `$scfg` instead of the volume type subdir `$path` as parameter. Passing `$scfg` along makes it possible to obtain the subdir for the given `$vtype` inside the helper directly instead of passing both the vtype and its associated subdirectory along. Also, pass the `$class` parameter along because it is still necessary to call the deprecated `get_subdir()` method for backwards compat. All of these things make future changes easier to manage. Signed-off-by: Max R. Carrara --- src/PVE/Storage/Plugin.pm | 102 +++++++++++++++++++++++++------------- 1 file changed, 67 insertions(+), 35 deletions(-) diff --git a/src/PVE/Storage/Plugin.pm b/src/PVE/Storage/Plugin.pm index d316caac..bbed4eb4 100644 --- a/src/PVE/Storage/Plugin.pm +++ b/src/PVE/Storage/Plugin.pm @@ -1870,44 +1870,59 @@ sub list_images { return $res; } # $vtype = my sub get_subdir_files { - my ($storeid, $path, $vtype, $vmid) = @_; + my ($class, $storeid, $scfg, $vtype, $vmid) = @_; + # FIXME: Call plugin_get_vtype_subdir() instead of $class->get_subdir() + # and remove the $class parameter on the next APIAGE reset. + my $vtype_subdir = $class->get_subdir($scfg, $vtype); my $res = []; - for my $filename (<$path/*>) { - # A volume ID cannot represent a name with a newline, see parse_volume_id(). - next if $filename =~ m/\n/; + my $get_subdir_file_info = sub { + my ($path, $st) = @_; - my $st = File::stat::stat($filename); - - next if (!$st || S_ISDIR($st->mode)); - - my $info; + # Strip the vtype subdir from beginning of the current path + # so that we don't have to take it into account when parsing the file name + my $filename = $path; + if ($filename !~ s!^\Q$vtype_subdir\E(?=/)!!) { + return; + } if ($vtype eq 'iso') { - next if $filename !~ m!/([^/]+$PVE::Storage::ISO_EXT_RE_0)$!i; + return if $filename !~ m!/([^/]+$PVE::Storage::ISO_EXT_RE_0)$!i; - $info = { volid => "$storeid:iso/$1", format => 'iso' }; + return { + volid => "$storeid:iso/$1", + format => 'iso', + }; + } - } elsif ($vtype eq 'vztmpl') { - next if $filename !~ m!/([^/]+$PVE::Storage::VZTMPL_EXT_RE_1)$!; + if ($vtype eq 'vztmpl') { + return if $filename !~ m!/([^/]+$PVE::Storage::VZTMPL_EXT_RE_1)$!; - $info = { volid => "$storeid:vztmpl/$1", format => $2 eq 'tar' ? $2 : "t$2" }; + return { + volid => "$storeid:vztmpl/$1", + format => $2 eq 'tar' ? $2 : "t$2", + }; + } - } elsif ($vtype eq 'backup') { - next if $filename !~ m!/([^/]+$PVE::Storage::BACKUP_EXT_RE_2)$!; - my $original = $filename; + if ($vtype eq 'backup') { + return if $filename !~ m!/([^/]+$PVE::Storage::BACKUP_EXT_RE_2)$!; + + my $original = $path; my $format = $2; $filename = $1; # Attribute archives to a guest like parse_volname() does, which the access checks use. next if defined($vmid) && $filename !~ m/^vzdump-(?:openvz|lxc|qemu)-\Q$vmid\E-/; - $info = { volid => "$storeid:backup/$filename", format => $format }; + my $info = { + volid => "$storeid:backup/$filename", + format => $format, + }; 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'; @@ -1922,28 +1937,49 @@ my sub get_subdir_files { $info->{notes} = eval { decode('UTF-8', $notes, 1) } // $notes if defined($notes); } $info->{protected} = 1 if -e PVE::Storage::protection_file_path($original); - } elsif ($vtype eq 'snippets') { - $info = { + return $info; + } + + if ($vtype eq 'snippets') { + return { volid => "$storeid:snippets/" . basename($filename), format => 'snippet', }; - } elsif ($vtype eq 'import') { - next + } + + if ($vtype eq 'import') { + return if $filename !~ m!/(${PVE::Storage::SAFE_CHAR_CLASS_RE}+$PVE::Storage::IMPORT_EXT_RE_1)$!i; - $info = { volid => "$storeid:import/$1", format => "$2" }; + return { + volid => "$storeid:import/$1", + format => "$2", + }; } - $info->{size} = $st->size; - $info->{ctime} //= $st->ctime; + return; + }; - push @$res, $info; + for my $path (<$vtype_subdir/*>) { + my $st = File::stat::stat($path); + + # A volume ID cannot represent a name with a newline, see parse_volume_id(). + next if $path =~ m/\n/; + + next if (!$st || S_ISDIR($st->mode)); + + if (defined(my $info = $get_subdir_file_info->($path, $st))) { + $info->{size} = $st->size; + $info->{ctime} //= $st->ctime; + + push $res->@*, $info; + } } return $res; } @@ -1958,24 +1994,20 @@ sub list_volumes { my $data; if ($type eq 'images' || $type eq 'rootdir') { $data = $class->list_images($storeid, $scfg, $vmid); } elsif ($scfg->{path}) { - # FIXME: Call plugin_get_vtype_subdir() instead of - # $plugin->get_subdir() on the next APIAGE reset. - my $path = $class->get_subdir($scfg, $type); - if ($type eq 'iso' && !defined($vmid)) { - $data = get_subdir_files($storeid, $path, 'iso'); + $data = get_subdir_files($class, $storeid, $scfg, 'iso', undef); } elsif ($type eq 'vztmpl' && !defined($vmid)) { - $data = get_subdir_files($storeid, $path, 'vztmpl'); + $data = get_subdir_files($class, $storeid, $scfg, 'vztmpl', undef); } elsif ($type eq 'backup') { - $data = get_subdir_files($storeid, $path, 'backup', $vmid); + $data = get_subdir_files($class, $storeid, $scfg, 'backup', $vmid); } elsif ($type eq 'snippets') { - $data = get_subdir_files($storeid, $path, 'snippets'); + $data = get_subdir_files($class, $storeid, $scfg, 'snippets', undef); } elsif ($type eq 'import') { - $data = get_subdir_files($storeid, $path, 'import'); + $data = get_subdir_files($class, $storeid, $scfg, 'import', undef); } } next if !$data; -- 2.47.3