+
+ $info->{ctime} = $ctime if $ctime;
+
+ push @$res, $info;
+ }
+
+ return $res;
+}
+
+# list templates ($tt = <iso|vztmpl|backup|snippets>)
+my $get_subdir_files = sub {
+ my ($sid, $path, $tt, $vmid) = @_;
+
+ my $res = [];
+
+ foreach my $fn (<$path/*>) {
+ my $st = File::stat::stat($fn);
+
+ next if (!$st || S_ISDIR($st->mode));
+
+ my $info;
+
+ if ($tt eq 'iso') {
+ next if $fn !~ m!/([^/]+$PVE::Storage::iso_extension_re)$!i;
+
+ $info = { volid => "$sid:iso/$1", format => 'iso' };
+
+ } elsif ($tt eq 'vztmpl') {
+ next if $fn !~ m!/([^/]+$PVE::Storage::vztmpl_extension_re)$!;
+
+ $info = { volid => "$sid:vztmpl/$1", format => "t$2" };
+
+ } elsif ($tt eq 'backup') {
+ next if $fn !~ m!/([^/]+\.(tgz|(?:(?:tar|vma)(?:\.(${\COMPRESSOR_RE}))?)))$!;
+ my $original = $fn;
+ my $format = $2;
+ $fn = $1;
+
+ # only match for VMID now, to avoid false positives (VMID in parent directory name)
+ next if defined($vmid) && $fn !~ m/\S+-$vmid-\S+/;
+
+ $info = { volid => "$sid:backup/$fn", format => $format };
+
+ my $archive_info = eval { PVE::Storage::archive_info($fn) } // {};
+
+ $info->{ctime} = $archive_info->{ctime} if defined($archive_info->{ctime});
+
+ if (defined($vmid) || $fn =~ 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);
+ $info->{notes} = $notes if defined($notes);
+ }
+
+ } elsif ($tt eq 'snippets') {
+
+ $info = {
+ volid => "$sid:snippets/". basename($fn),
+ format => 'snippet',
+ };
+ }
+
+ $info->{size} = $st->size;
+ $info->{ctime} //= $st->ctime;
+
+ push @$res, $info;
+ }
+
+ return $res;
+};
+
+sub list_volumes {
+ my ($class, $storeid, $scfg, $vmid, $content_types) = @_;
+
+ my $res = [];
+ my $vmlist = PVE::Cluster::get_vmlist();
+ foreach my $type (@$content_types) {
+ my $data;
+
+ if ($type eq 'images' || $type eq 'rootdir') {
+ $data = $class->list_images($storeid, $scfg, $vmid);
+ } elsif ($scfg->{path}) {
+ my $path = $class->get_subdir($scfg, $type);
+
+ if ($type eq 'iso' && !defined($vmid)) {
+ $data = $get_subdir_files->($storeid, $path, 'iso');
+ } elsif ($type eq 'vztmpl'&& !defined($vmid)) {
+ $data = $get_subdir_files->($storeid, $path, 'vztmpl');
+ } elsif ($type eq 'backup') {
+ $data = $get_subdir_files->($storeid, $path, 'backup', $vmid);
+ } elsif ($type eq 'snippets') {
+ $data = $get_subdir_files->($storeid, $path, 'snippets');
+ }
+ }
+
+ next if !$data;
+
+ foreach my $item (@$data) {
+ if ($type eq 'images' || $type eq 'rootdir') {
+ my $vminfo = $vmlist->{ids}->{$item->{vmid}};
+ my $vmtype;
+ if (defined($vminfo)) {
+ $vmtype = $vminfo->{type};
+ }
+ if (defined($vmtype) && $vmtype eq 'lxc') {
+ $item->{content} = 'rootdir';
+ } else {
+ $item->{content} = 'images';
+ }
+ next if $type ne $item->{content};
+ } else {
+ $item->{content} = $type;
+ }
+
+ push @$res, $item;
+ }