[pve-devel] [PATCH container 3/5] use helper functions from GuestHelpers

Oguz Bektas o.bektas at proxmox.com
Tue Jan 28 16:03:19 CET 2020


remove safe_string_ne and safe_num_ne code which is now shared in
GuestHelpers. also change all the calls.

Signed-off-by: Oguz Bektas <o.bektas at proxmox.com>
---
 src/PVE/LXC.pm | 36 ++++++++----------------------------
 1 file changed, 8 insertions(+), 28 deletions(-)

diff --git a/src/PVE/LXC.pm b/src/PVE/LXC.pm
index 34949c6..0051c5c 100644
--- a/src/PVE/LXC.pm
+++ b/src/PVE/LXC.pm
@@ -814,26 +814,6 @@ sub vm_stop_cleanup {
     warn $@ if $@; # avoid errors - just warn
 }
 
-my $safe_num_ne = sub {
-    my ($a, $b) = @_;
-
-    return 0 if !defined($a) && !defined($b);
-    return 1 if !defined($a);
-    return 1 if !defined($b);
-
-    return $a != $b;
-};
-
-my $safe_string_ne = sub {
-    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 update_net {
     my ($vmid, $conf, $opt, $newnet, $netid, $rootdir) = @_;
 
@@ -848,8 +828,8 @@ sub update_net {
     if (my $oldnetcfg = $conf->{$opt}) {
 	my $oldnet = PVE::LXC::Config->parse_lxc_network($oldnetcfg);
 
-	if (&$safe_string_ne($oldnet->{hwaddr}, $newnet->{hwaddr}) ||
-	    &$safe_string_ne($oldnet->{name}, $newnet->{name})) {
+	if (PVE::GuestHelpers::safe_string_ne($oldnet->{hwaddr}, $newnet->{hwaddr}) ||
+	    PVE::GuestHelpers::safe_string_ne($oldnet->{name}, $newnet->{name})) {
 
 	    PVE::Network::veth_delete($veth);
 	    delete $conf->{$opt};
@@ -858,9 +838,9 @@ sub update_net {
 	    hotplug_net($vmid, $conf, $opt, $newnet, $netid);
 
 	} else {
-	    if (&$safe_string_ne($oldnet->{bridge}, $newnet->{bridge}) ||
-		&$safe_num_ne($oldnet->{tag}, $newnet->{tag}) ||
-		&$safe_num_ne($oldnet->{firewall}, $newnet->{firewall})) {
+	    if (PVE::GuestHelpers::safe_string_ne($oldnet->{bridge}, $newnet->{bridge}) ||
+		PVE::GuestHelpers::safe_num_ne($oldnet->{tag}, $newnet->{tag}) ||
+		PVE::GuestHelpers::safe_num_ne($oldnet->{firewall}, $newnet->{firewall})) {
 
 		if ($oldnet->{bridge}) {
 		    PVE::Network::tap_unplug($veth);
@@ -876,7 +856,7 @@ sub update_net {
 		foreach (qw(bridge tag firewall rate)) {
 		    $oldnet->{$_} = $newnet->{$_} if $newnet->{$_};
 		}
-	    } elsif (&$safe_string_ne($oldnet->{rate}, $newnet->{rate})) {
+	    } elsif (PVE::GuestHelpers::safe_string_ne($oldnet->{rate}, $newnet->{rate})) {
 		# Rate can be applied on its own but any change above needs to
 		# include the rate in tap_plug since OVS resets everything.
 		PVE::Network::tap_rate_limit($veth, $newnet->{rate});
@@ -946,8 +926,8 @@ sub update_ipconfig {
 	my $oldip = $optdata->{$ip};
 	my $oldgw = $optdata->{$gw};
 
-	my $change_ip = &$safe_string_ne($oldip, $newip);
-	my $change_gw = &$safe_string_ne($oldgw, $newgw);
+	my $change_ip = PVE::GuestHelpers::safe_string_ne($oldip, $newip);
+	my $change_gw = PVE::GuestHelpers::safe_string_ne($oldgw, $newgw);
 
 	return if !$change_ip && !$change_gw;
 
-- 
2.20.1




More information about the pve-devel mailing list