mirror of
https://git.proxmox.com/git/pve-common
synced 2025-08-05 21:48:05 +00:00
Added PVE::JSONSchema::parse_size/format_size
This commit is contained in:
parent
2421fba133
commit
878fea8ef0
@ -438,6 +438,45 @@ sub check_format {
|
||||
}
|
||||
}
|
||||
|
||||
sub parse_size {
|
||||
my ($value) = @_;
|
||||
|
||||
return undef if $value !~ m/^(\d+(\.\d+)?)([KMGT])?$/;
|
||||
my ($size, $unit) = ($1, $3);
|
||||
if ($unit) {
|
||||
if ($unit eq 'K') {
|
||||
$size = $size * 1024;
|
||||
} elsif ($unit eq 'M') {
|
||||
$size = $size * 1024 * 1024;
|
||||
} elsif ($unit eq 'G') {
|
||||
$size = $size * 1024 * 1024 * 1024;
|
||||
} elsif ($unit eq 'T') {
|
||||
$size = $size * 1024 * 1024 * 1024 * 1024;
|
||||
}
|
||||
}
|
||||
return int($size);
|
||||
};
|
||||
|
||||
sub format_size {
|
||||
my ($size) = @_;
|
||||
|
||||
$size = int($size);
|
||||
|
||||
my $kb = int($size/1024);
|
||||
return $size if $kb*1024 != $size;
|
||||
|
||||
my $mb = int($kb/1024);
|
||||
return "${kb}K" if $mb*1024 != $kb;
|
||||
|
||||
my $gb = int($mb/1024);
|
||||
return "${mb}M" if $gb*1024 != $mb;
|
||||
|
||||
my $tb = int($gb/1024);
|
||||
return "${gb}G" if $tb*1024 != $gb;
|
||||
|
||||
return "${tb}T";
|
||||
};
|
||||
|
||||
sub parse_property_string {
|
||||
my ($format, $data, $path) = @_;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user