[pve-devel] [RFC pve-network 9/9] dhcp : dnsmasq: add_mapping: remove old mac, ip before append
Alexandre Derumier
aderumier at odiso.com
Mon Nov 13 11:04:19 CET 2023
Signed-off-by: Alexandre Derumier <aderumier at odiso.com>
---
src/PVE/Network/SDN/Dhcp/Dnsmasq.pm | 18 +++++++++++++++---
1 file changed, 15 insertions(+), 3 deletions(-)
diff --git a/src/PVE/Network/SDN/Dhcp/Dnsmasq.pm b/src/PVE/Network/SDN/Dhcp/Dnsmasq.pm
index 64895ef..21a6ddd 100644
--- a/src/PVE/Network/SDN/Dhcp/Dnsmasq.pm
+++ b/src/PVE/Network/SDN/Dhcp/Dnsmasq.pm
@@ -54,12 +54,24 @@ sub del_ip_mapping {
sub add_ip_mapping {
my ($class, $dhcpid, $mac, $ip) = @_;
+
my $ethers_file = "$DNSMASQ_CONFIG_ROOT/$dhcpid/ethers";
+ my $ethers_tmp_file = "$ethers_file.tmp";
my $appendFn = sub {
- open(my $fh, '>>', $ethers_file) or die "Could not open file '$ethers_file' $!\n";
- print $fh "$mac,$ip\n";
- close $fh;
+ open(my $in, '<', $ethers_file) or die "Could not open file '$ethers_file' $!\n";
+ open(my $out, '>', $ethers_tmp_file) or die "Could not open file '$ethers_tmp_file' $!\n";
+
+ while (my $line = <$in>) {
+ next if $line =~ m/^$mac/;
+ print $out $line;
+ }
+
+ print $out "$mac,$ip\n";
+ close $in;
+ close $out;
+ move $ethers_tmp_file, $ethers_file;
+ chmod 0644, $ethers_file;
};
PVE::Tools::lock_file($ethers_file, 10, $appendFn);
--
2.39.2
More information about the pve-devel
mailing list