storage: replace build-in stat occurrences

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>
This commit is contained in:
Alwin Antreich
2020-04-28 15:58:15 +02:00
committed by Fabian Grünbichler
parent cd554b79d1
commit 92ae59df9e
2 changed files with 14 additions and 27 deletions

View 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;

View 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);
my $st = File::stat::stat($fn);
next if S_ISDIR($mode);
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;
}