Limit file clobbering during update

Don't clobber files in-place unless there's actual changes.
This commit is contained in:
Dan Church 2022-11-25 14:58:27 -06:00
parent 65ade612e7
commit 59ef0fd513
Signed by: h3xx
GPG Key ID: EA2BF379CD2CDBD0
1 changed files with 32 additions and 32 deletions

View File

@ -36,6 +36,34 @@ cleanup() {
}
trap 'cleanup' EXIT
copy_perms() {
local -r FROM=$1 TO=$2
chmod --reference="$FROM" -- "$TO"
if [[ $UID -eq 0 ]]; then
chown --reference="$FROM" -- "$TO"
fi
}
replace_with() {
local -r ORIG=$1 NEW=$2
if ! diff -q -- "$ORIG" "$NEW" &>/dev/null; then
# There's a change
if [[ -f $ORIG ]]; then
copy_perms "$ORIG" "$NEW"
if [[ -n $BACKUPSUFFIX ]]; then
mv -- "$ORIG" "$ORIG$BACKUPSUFFIX"
fi
else
mkdir -p -- "${ORIG%/*}"
fi
mv -- "$NEW" "$ORIG"
else
printf 'File "%s" not modified\n' \
"$ORIG" \
>&2
fi
}
(cd "$WORKDIR" && git submodule update --init --remote)
for (( I = 0 ; I < ${#OUT[@]} ; ++I )); do
@ -52,44 +80,16 @@ for (( I = 0 ; I < ${#OUT[@]} ; ++I )); do
TEMP_OUT=$(mktemp -p "$TEMP_DIR")
if [[ -f $MY_OUT ]]; then
cp -a -- "$MY_OUT" "$TEMP_OUT"
fi
wget \
-O "$TEMP_OUT" \
"$MY_URL"
if [[ -f $MY_OUT ]]; then
chmod --reference="$MY_OUT" "$TEMP_OUT"
if [[ -n $BACKUPSUFFIX ]]; then
mv -- "$MY_OUT" "$MY_OUT$BACKUPSUFFIX"
fi
fi
mkdir -p -- "${MY_OUT%/*}"
mv -- "$TEMP_OUT" "$MY_OUT"
# If the old one is the same, don't keep it around
if [[ -n $BACKUPSUFFIX && -f $MY_OUT$BACKUPSUFFIX ]]; then
if diff -q "$MY_OUT" "$MY_OUT$BACKUPSUFFIX"; then
rm -f -- "$MY_OUT$BACKUPSUFFIX"
fi
fi
replace_with "$MY_OUT" "$TEMP_OUT"
done
if [[ -n $BACKUPSUFFIX && -f $BLOCKLIST ]]; then
mv -- "$BLOCKLIST" "$BLOCKLIST$BACKUPSUFFIX"
fi
"$WORKDIR/make-block.pl" --out="$BLOCKLIST"
# If the old one is the same the same, don't keep it around
if [[ -n $BACKUPSUFFIX && -f $BLOCKLIST$BACKUPSUFFIX ]]; then
if diff -q "$BLOCKLIST" "$BLOCKLIST$BACKUPSUFFIX"; then
rm -f -- "$BLOCKLIST$BACKUPSUFFIX"
fi
fi
TEMP_BLOCKLIST=$(mktemp -p "$TEMP_DIR")
"$WORKDIR/make-block.pl" --out="$TEMP_BLOCKLIST"
replace_with "$BLOCKLIST" "$TEMP_BLOCKLIST"
if [[ ${#DNSMASQ_RESTART_COMMAND[@]} -gt 0 ]]; then
"${DNSMASQ_RESTART_COMMAND[@]}"