diff mbox series

[v2] tools/hotplug: Fix dhcpd symlink removal in vif-nat

Message ID 401e31a2c521a34502461336b4e65468e7394e01.1599654067.git.diego.sueiro@arm.com
State New
Headers show
Series [v2] tools/hotplug: Fix dhcpd symlink removal in vif-nat | expand

Commit Message

Diego Sueiro Sept. 9, 2020, 12:35 p.m. UTC
Copy temp files used to add/remove dhcpd configurations to avoid
replacing potential symlinks.

If dhcp.conf is a symlink pointing to dhcp.conf.real, using 'mv'
creates a new file dhcp.conf where cp will actually modify
dhcp.conf.real instead of replacing the symlink with a real
file.

Using 'cp' prevents some mistakes where the user will actually
continue to modify dhcp.conf.real where it would not be the one
used anymore.

Signed-off-by: Diego Sueiro <diego.sueiro@arm.com>
---
Changes in v2:
  - Update commit message
  - Simplify the code when removing the temp file.
---
 tools/hotplug/Linux/vif-nat | 20 +++++++++-----------
 1 file changed, 9 insertions(+), 11 deletions(-)

Comments

Bertrand Marquis Sept. 9, 2020, 1:31 p.m. UTC | #1
> On 9 Sep 2020, at 13:35, Diego Sueiro <Diego.Sueiro@arm.com> wrote:
> 
> Copy temp files used to add/remove dhcpd configurations to avoid
> replacing potential symlinks.
> 
> If dhcp.conf is a symlink pointing to dhcp.conf.real, using 'mv'
> creates a new file dhcp.conf where cp will actually modify
> dhcp.conf.real instead of replacing the symlink with a real
> file.
> 
> Using 'cp' prevents some mistakes where the user will actually
> continue to modify dhcp.conf.real where it would not be the one
> used anymore.
> 
> Signed-off-by: Diego Sueiro <diego.sueiro@arm.com>
Reviewed-by: Bertrand Marquis <bertrand.marquis@arm.com>

> ---
> Changes in v2:
>  - Update commit message
>  - Simplify the code when removing the temp file.
> ---
> tools/hotplug/Linux/vif-nat | 20 +++++++++-----------
> 1 file changed, 9 insertions(+), 11 deletions(-)
> 
> diff --git a/tools/hotplug/Linux/vif-nat b/tools/hotplug/Linux/vif-nat
> index 2614435..fd34afb 100644
> --- a/tools/hotplug/Linux/vif-nat
> +++ b/tools/hotplug/Linux/vif-nat
> @@ -95,12 +95,11 @@ dhcparg_remove_entry()
> {
>   local tmpfile=$(mktemp)
>   sed -e "s/${dev} //" "$dhcpd_arg_file" >"$tmpfile"
> -  if diff "$tmpfile" "$dhcpd_arg_file" >/dev/null
> +  if ! diff "$tmpfile" "$dhcpd_arg_file" >/dev/null
>   then
> -    rm "$tmpfile"
> -  else
> -    mv "$tmpfile" "$dhcpd_arg_file"
> +    cp "$tmpfile" "$dhcpd_arg_file"
>   fi
> +  rm "$tmpfile"
> }
> 
> dhcparg_add_entry()
> @@ -109,11 +108,11 @@ dhcparg_add_entry()
>   local tmpfile=$(mktemp)
>   # handle Red Hat, SUSE, and Debian styles, with or without quotes
>   sed -e 's/^DHCPDARGS="*\([^"]*\)"*/DHCPDARGS="\1'"${dev} "'"/' \
> -     "$dhcpd_arg_file" >"$tmpfile" && mv "$tmpfile" "$dhcpd_arg_file"
> +     "$dhcpd_arg_file" >"$tmpfile" && cp "$tmpfile" "$dhcpd_arg_file"
>   sed -e 's/^DHCPD_INTERFACE="*\([^"]*\)"*/DHCPD_INTERFACE="\1'"${dev} "'"/' \
> -     "$dhcpd_arg_file" >"$tmpfile" && mv "$tmpfile" "$dhcpd_arg_file"
> +     "$dhcpd_arg_file" >"$tmpfile" && cp "$tmpfile" "$dhcpd_arg_file"
>   sed -e 's/^INTERFACES="*\([^"]*\)"*/INTERFACES="\1'"${dev} "'"/' \
> -     "$dhcpd_arg_file" >"$tmpfile" && mv "$tmpfile" "$dhcpd_arg_file"
> +     "$dhcpd_arg_file" >"$tmpfile" && cp "$tmpfile" "$dhcpd_arg_file"
>   rm -f "$tmpfile"
> }
> 
> @@ -121,12 +120,11 @@ dhcp_remove_entry()
> {
>   local tmpfile=$(mktemp)
>   grep -v "host $hostname" "$dhcpd_conf_file" >"$tmpfile"
> -  if diff "$tmpfile" "$dhcpd_conf_file" >/dev/null
> +  if ! diff "$tmpfile" "$dhcpd_conf_file" >/dev/null
>   then
> -    rm "$tmpfile"
> -  else
> -    mv "$tmpfile" "$dhcpd_conf_file"
> +    cp "$tmpfile" "$dhcpd_conf_file"
>   fi
> +  rm "$tmpfile"
>   dhcparg_remove_entry
> }
> 
> -- 
> 2.7.4
> 
>
diff mbox series

Patch

diff --git a/tools/hotplug/Linux/vif-nat b/tools/hotplug/Linux/vif-nat
index 2614435..fd34afb 100644
--- a/tools/hotplug/Linux/vif-nat
+++ b/tools/hotplug/Linux/vif-nat
@@ -95,12 +95,11 @@  dhcparg_remove_entry()
 {
   local tmpfile=$(mktemp)
   sed -e "s/${dev} //" "$dhcpd_arg_file" >"$tmpfile"
-  if diff "$tmpfile" "$dhcpd_arg_file" >/dev/null
+  if ! diff "$tmpfile" "$dhcpd_arg_file" >/dev/null
   then
-    rm "$tmpfile"
-  else
-    mv "$tmpfile" "$dhcpd_arg_file"
+    cp "$tmpfile" "$dhcpd_arg_file"
   fi
+  rm "$tmpfile"
 }
 
 dhcparg_add_entry()
@@ -109,11 +108,11 @@  dhcparg_add_entry()
   local tmpfile=$(mktemp)
   # handle Red Hat, SUSE, and Debian styles, with or without quotes
   sed -e 's/^DHCPDARGS="*\([^"]*\)"*/DHCPDARGS="\1'"${dev} "'"/' \
-     "$dhcpd_arg_file" >"$tmpfile" && mv "$tmpfile" "$dhcpd_arg_file"
+     "$dhcpd_arg_file" >"$tmpfile" && cp "$tmpfile" "$dhcpd_arg_file"
   sed -e 's/^DHCPD_INTERFACE="*\([^"]*\)"*/DHCPD_INTERFACE="\1'"${dev} "'"/' \
-     "$dhcpd_arg_file" >"$tmpfile" && mv "$tmpfile" "$dhcpd_arg_file"
+     "$dhcpd_arg_file" >"$tmpfile" && cp "$tmpfile" "$dhcpd_arg_file"
   sed -e 's/^INTERFACES="*\([^"]*\)"*/INTERFACES="\1'"${dev} "'"/' \
-     "$dhcpd_arg_file" >"$tmpfile" && mv "$tmpfile" "$dhcpd_arg_file"
+     "$dhcpd_arg_file" >"$tmpfile" && cp "$tmpfile" "$dhcpd_arg_file"
   rm -f "$tmpfile"
 }
 
@@ -121,12 +120,11 @@  dhcp_remove_entry()
 {
   local tmpfile=$(mktemp)
   grep -v "host $hostname" "$dhcpd_conf_file" >"$tmpfile"
-  if diff "$tmpfile" "$dhcpd_conf_file" >/dev/null
+  if ! diff "$tmpfile" "$dhcpd_conf_file" >/dev/null
   then
-    rm "$tmpfile"
-  else
-    mv "$tmpfile" "$dhcpd_conf_file"
+    cp "$tmpfile" "$dhcpd_conf_file"
   fi
+  rm "$tmpfile"
   dhcparg_remove_entry
 }