From 10bd29265b3aebf149bc5e93e91462732c9240de Mon Sep 17 00:00:00 2001 From: Simon Kelley Date: Fri, 27 Sep 2013 21:07:30 +0100 Subject: [PATCH] macscript: create file if it doesn't exist. --- contrib/mactable/macscript | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/contrib/mactable/macscript b/contrib/mactable/macscript index 8d807c5..44a4477 100755 --- a/contrib/mactable/macscript +++ b/contrib/mactable/macscript @@ -11,20 +11,26 @@ action="$1" mac="$2" # IPv4 ip="$3" -if [ -n "$DNSMASQ_IAID" ]; then - if [ -z "$DNSMASQ_MAC" -a "$action" = "old" ]; then - # MAC not known by dnsmasq, probably due to restart. - exit 0; - fi - mac="$DNSMASQ_MAC" # IPv6 +# ensure it always exists. + +if [ ! -f "$STATUS_FILE" ]; then + touch "$STATUS_FILE" fi -if [ "$action" = "add" -o "$action" = "old" -o "$action" = "del" ]; then - if [ -f "$STATUS_FILE" ]; then - sed "/^${ip//./\.} / d" "$STATUS_FILE" > "$STATUS_FILE".new - fi - if [ "$action" = "add" -o "$action" = "old" ]; then - echo "$ip $mac" >> "$STATUS_FILE".new - fi - mv "$STATUS_FILE".new "$STATUS_FILE" # atomic update. +if [ -n "$DNSMASQ_IAID" ]; then + mac="$DNSMASQ_MAC" # IPv6 +fi + +# worry about an add or old action when the MAC address is not known: +# leave any old one in place in that case. + +if [ "$action" = "add" -o "$action" = "old" -o "$action" = "del" ]; then + if [ -n "$mac" -o "$action" = "del" ]; then + sed "/^${ip//./\.} / d" "$STATUS_FILE" > "$STATUS_FILE".new + + if [ "$action" = "add" -o "$action" = "old" ]; then + echo "$ip $mac" >> "$STATUS_FILE".new + fi + mv "$STATUS_FILE".new "$STATUS_FILE" # atomic update. + fi fi