[pve-devel] [PATCH guest-common 1/5] guesthelpers: move/add safe comparison functions from lxc and qemu
Oguz Bektas
o.bektas at proxmox.com
Tue Jan 28 16:03:17 CET 2020
move the safe_string_ne and safe_num_ne functions to guesthelpers to
remove duplicate code.
add the new safe_boolean_ne and typesafe_ne helper functions
Signed-off-by: Oguz Bektas <o.bektas at proxmox.com>
---
these will be used in the partial fast plug function in this series
PVE/GuestHelpers.pm | 49 +++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 49 insertions(+)
diff --git a/PVE/GuestHelpers.pm b/PVE/GuestHelpers.pm
index 07a62ce..b7133d0 100644
--- a/PVE/GuestHelpers.pm
+++ b/PVE/GuestHelpers.pm
@@ -14,6 +14,55 @@ use Scalar::Util qw(weaken);
our $lockdir = '/var/lock/pve-manager';
+# safe variable comparison functions
+
+sub safe_num_ne {
+ my ($a, $b) = @_;
+
+ return 0 if !defined($a) && !defined($b);
+ return 1 if !defined($a);
+ return 1 if !defined($b);
+
+ return $a != $b;
+}
+
+sub safe_string_ne {
+ my ($a, $b) = @_;
+
+ return 0 if !defined($a) && !defined($b);
+ return 1 if !defined($a);
+ return 1 if !defined($b);
+
+ return $a ne $b;
+}
+
+sub safe_boolean_ne {
+ my ($a, $b) = @_;
+
+ # we don't check if value is defined, since undefined
+ # is false (so it's a valid boolean)
+
+ # negate both values to normalize and compare
+ return !$a != !$b;
+}
+
+sub typesafe_ne {
+ my ($a, $b, $type) = @_;
+
+ return 0 if !defined($a) && !defined($b);
+ return 1 if !defined($a);
+ return 1 if !defined($b);
+
+ if ($type eq 'string') {
+ return safe_string_ne($a, $b);
+ } elsif ($type eq 'number') {
+ return safe_num_ne($a, $b);
+ } elsif ($type eq 'boolean') {
+ return safe_boolean_ne($a, $b);
+ }
+}
+
+
sub guest_migration_lock {
my ($vmid, $timeout, $func, @param) = @_;
--
2.20.1
More information about the pve-devel
mailing list