We often need to convert between file sizes, for formatting output, but also code-internal. Some methods expect kilobytes, some gigabytes and sometimes we need bytes.
While conversion from smaller to bigger units can be simply done with a left-shift, the opposite conversion may need more attention - depending on the used context. If we allocate disks this is quite critical. For example, if we need to allocate a disk with size 1023 bytes using the PVE::Storage::vdisk_alloc method (which expects kilobytes) a right shift by 10 (<=> division by 1024) would result in "0", which obviously fails. Thus we round up the converted value if a remainder was lost on the transformation in this new method. This behaviour is opt-out, to be on the safe side. The method can be used in a clear way, as it gives information about the source and target unit size, unlike "$var *= 1024", which doesn't gives direct information at all, if not commented or derived somewhere from its context. my $kilobytes = convert_unit($value, 'gb' => 'kb'); We return also the remainder in list context, which may be useful for formatting output, e.g. if a float is desired. Signed-off-by: Thomas Lamprecht <t.lampre...@proxmox.com> --- changes v1 -> v2: * ensure remainder is 0 down conversion src/PVE/Tools.pm | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/PVE/Tools.pm b/src/PVE/Tools.pm index 9ddcfda..a02654c 100644 --- a/src/PVE/Tools.pm +++ b/src/PVE/Tools.pm @@ -1617,4 +1617,36 @@ sub encrypt_pw { return crypt(encode("utf8", $pw), "\$5\$$salt\$"); } +# intended usage: convert_size($val, "kb" => "gb") +# on reduction (converting to a bigger unit) we round up by default if +# information got lost. E.g. `convert_size(1023, "b" => "kb")` returns 1 +# use $no_round_up to switch this off, above example would then return 0 +sub convert_size { + my ($value, $from, $to, $no_round_up) = @_; + + my $units = { + b => 0, + kb => 1, + mb => 2, + gb => 3, + tb => 4, + pb => 5, + }; + + $from = lc($from); $to = lc($to); + + my $shift_amount = $units->{$from} - $units->{$to}; + my $remainder = 0; + + if ($shift_amount > 0) { + $value <<= ($shift_amount * 10); + } elsif ($shift_amount < 0) { + $remainder = ($value & (1 << abs($shift_amount)*10) - 1); + $value >>= abs($shift_amount * 10); + $value++ if $remainder && !$no_round_up; + } + + return wantarray ? ($value, $remainder) : $value; +} + 1; -- 2.11.0 _______________________________________________ pve-devel mailing list pve-devel@pve.proxmox.com https://pve.proxmox.com/cgi-bin/mailman/listinfo/pve-devel