]> git.proxmox.com Git - pve-storage.git/commitdiff
storage: replace build-in stat occurrences
authorAlwin Antreich <a.antreich@proxmox.com>
Tue, 28 Apr 2020 13:58:15 +0000 (15:58 +0200)
committerFabian Grünbichler <f.gruenbichler@proxmox.com>
Thu, 30 Apr 2020 16:37:19 +0000 (18:37 +0200)
with File::stat::stat to minimize variable declarations. And allow to
mock this method in tests instead of the perl build-in stat.

Signed-off-by: Alwin Antreich <a.antreich@proxmox.com>
PVE/Diskmanage.pm
PVE/Storage/Plugin.pm

index 13e7cd89b346487f1e44ea85ce9b6801fff1a957..cac944d46ca81e121fc6b8567e946770a8aa24bd 100644 (file)
@@ -6,6 +6,7 @@ use PVE::ProcFSTools;
 use Data::Dumper;
 use Cwd qw(abs_path);
 use Fcntl ':mode';
+use File::stat;
 use JSON;
 
 use PVE::Tools qw(extract_param run_command file_get_contents file_read_firstline dir_glob_regex dir_glob_foreach trim);
@@ -673,11 +674,11 @@ sub get_disks {
 sub get_partnum {
     my ($part_path) = @_;
 
-    my ($mode, $rdev) = (stat($part_path))[2,6];
+    my $st = stat($part_path);
 
-    next if !$mode || !S_ISBLK($mode) || !$rdev;
-    my $major = PVE::Tools::dev_t_major($rdev);
-    my $minor = PVE::Tools::dev_t_minor($rdev);
+    next if !$st->mode || !S_ISBLK($st->mode) || !$st->rdev;
+    my $major = PVE::Tools::dev_t_major($st->rdev);
+    my $minor = PVE::Tools::dev_t_minor($st->rdev);
     my $partnum_path = "/sys/dev/block/$major:$minor/";
 
     my $partnum;
index 4489a77d034314a0bd384b5703b4e3aa1993368e..dba6eb9adf7520f633c1e67b3292bc41fbb37b06 100644 (file)
@@ -7,6 +7,7 @@ use Fcntl ':mode';
 use File::chdir;
 use File::Path;
 use File::Basename;
+use File::stat qw();
 use Time::Local qw(timelocal);
 
 use PVE::Tools qw(run_command);
@@ -718,12 +719,10 @@ sub free_image {
 sub file_size_info {
     my ($filename, $timeout) = @_;
 
-    my @fs = stat($filename);
-    my $mode = $fs[2];
-    my $ctime = $fs[10];
+    my $st = File::stat::stat($filename);
 
-    if (S_ISDIR($mode)) {
-       return wantarray ? (0, 'subvol', 0, undef, $ctime) : 1;
+    if (S_ISDIR($st->mode)) {
+       return wantarray ? (0, 'subvol', 0, undef, $st->ctime) : 1;
     }
 
     my $json = '';
@@ -741,7 +740,7 @@ sub file_size_info {
 
     my ($size, $format, $used, $parent) = $info->@{qw(virtual-size format actual-size backing-filename)};
 
-    return wantarray ? ($size, $format, $used, $parent, $ctime) : $size;
+    return wantarray ? ($size, $format, $used, $parent, $st->ctime) : $size;
 }
 
 sub volume_size_info {
@@ -918,22 +917,9 @@ my $get_subdir_files = sub {
 
     foreach my $fn (<$path/*>) {
 
-       my ($dev,
-           $ino,
-           $mode,
-           $nlink,
-           $uid,
-           $gid,
-           $rdev,
-           $size,
-           $atime,
-           $mtime,
-           $ctime,
-           $blksize,
-           $blocks
-       ) = stat($fn);
-
-       next if S_ISDIR($mode);
+       my $st = File::stat::stat($fn);
+
+       next if S_ISDIR($st->mode);
 
        my $info;
 
@@ -972,8 +958,8 @@ my $get_subdir_files = sub {
            };
        }
 
-       $info->{size} = $size;
-       $info->{ctime} //= $ctime;
+       $info->{size} = $st->size;
+       $info->{ctime} //= $st->ctime;
 
        push @$res, $info;
     }