[Devel] [PATCH] Backup network config file if it lacks of warning
Kir Kolyshkin
kir at openvz.org
Tue Apr 30 17:28:33 PDT 2013
On 04/30/2013 04:44 PM, Igor Podlesny wrote:
> We do warn a user to not edit network config file since it can be
> overwritten, but we do this placing our warning inside overwritten file
> -- it's too late. Let's at least make a copy.
> ---
> etc/dists/scripts/debian-add_ip.sh | 11 ++++++++++-
> 1 file changed, 10 insertions(+), 1 deletion(-)
>
> diff --git a/etc/dists/scripts/debian-add_ip.sh b/etc/dists/scripts/debian-add_ip.sh
> index 0fb1f36..2ba555a 100755
> --- a/etc/dists/scripts/debian-add_ip.sh
> +++ b/etc/dists/scripts/debian-add_ip.sh
> @@ -38,9 +38,18 @@ fix_networking_conf()
>
> setup_network()
> {
> + local dont_edit='WARNING: Do not edit this file'
1 Add an empty line between variables declaration and code
2 I suggest using the very first line of comment we are leaving, i.e.
# This configuration file is auto-generated.
and using it as a whole, so we can grep for the whole line (less
probable false positive).
> + [ -r "$CFGFILE" ] &&
> + head -n20 "$CFGFILE" | fgrep -q -- "$dont_edit" || {
1 Lots of && and || and { makes this code a bit cryptic to read.
2 If we are using the first line we can do head -n1 (again less probable
false positive)
3 Also, we can remove check for file and do head $CFGFILE 2>/dev/null
instead
> + # -- It has no our warning inside, so we'd better
> + # back it up:
Moving this comment out of this block can make it one-liner, easier to read
> + cp "$CFGFILE" \
> + "${CFGFILE}-$(date --rfc-3339=seconds).bak"
1 Use cp -a to retain file attributes such as times
2 Do you really want spaces in file name? IMHO "interfaces.2013-04-30
17:20:57-07:00.bak" is an ugly name for a file.
3 Please make sure this date syntax works in ancient date versions (I
suggest checking centos4 or debian 3.1, or both).
4 Maybe it's easier to just use $$ (current shell pid).
> + }
> +
> echo "# This configuration file is auto-generated.
> #
> -# WARNING: Do not edit this file, your changes will be lost.
> +# $dont_edit, your changes will be lost.
> # Please create/edit $CFGFILE.head and
> # $CFGFILE.tail instead, their contents will be
> # inserted at the beginning and at the end of this file, respectively.
So, taking into account all the above comments,
what about something like this (untested):
local warn='# This configuration file is auto-generated.'
# Make a backup if the file does not look like generated by us
if ! head -n1 "$CFGFILE" 2>/dev/null | grep -q -- "^${warn}$"; then
cp -a "$CFGFILE" "$CFGFILE-$$.bak"
fi
echo "$warn
#
# WARNING: Do not edit this file, your changes will be lost.
....
More information about the Devel
mailing list