From 19e9b30895fc690c3853449c38f0480ca6e2060c Mon Sep 17 00:00:00 2001 From: Thomas Lamprecht Date: Wed, 23 Oct 2019 10:38:12 +0200 Subject: [PATCH] introduce version_cmp helper for qemu_machine_feature_enabled will be reused for a "running KVM/QEMU version is at least" helper in a next patch Signed-off-by: Thomas Lamprecht --- PVE/QemuServer.pm | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm index 6d6e6633..455f0fe1 100644 --- a/PVE/QemuServer.pm +++ b/PVE/QemuServer.pm @@ -7178,9 +7178,29 @@ sub qemu_machine_feature_enabled { $current_minor = $2; } - return 1 if $current_major > $version_major || - ($current_major == $version_major && - $current_minor >= $version_minor); + return 1 if version_cmp($current_major, $version_major, $current_minor, $version_minor) >= 0; +} + +# gets in pairs the versions you want to compares, i.e.: +# ($a-major, $b-major, $a-minor, $b-minor, $a-extra, $b-extra, ...) +# returns 0 if same, -1 if $a is older than $b, +1 if $a is newer than $b +sub version_cmp { + my @versions = @_; + + my $size = scalar(@versions); + + return 0 if $size == 0; + die "cannot compare odd count of versions" if $size & 1; + + for (my $i = 0; $i < $size; $i += 2) { + my ($a, $b) = splice(@versions, 0, 2); + $a //= 0; + $b //= 0; + + return 1 if $a > $b; + return -1 if $a < $b; + } + return 0; } sub qemu_machine_pxe {