From 48fe84c56e931b03bf8afdcf2270986e55efc5e8 Mon Sep 17 00:00:00 2001 From: bones_was_here Date: Tue, 18 Mar 2025 09:05:33 +1000 Subject: [PATCH] rsync: improve robustness of scripts, fix non-interactive mode POSIX shell: - In non-interactive mode an infinite loop occurred unless the --yes argument was passed. - `which` is a non-standard program, `command -v` is a POSIX standard shell builtin. - `exec` prevents potential glitches when the script is updated by rsync while still running. Windows batch: - Variable assignments persisted after batch exit, potentially interfering with other batch execution. - If copying failed the window closed before the error could be read. --- misc/tools/rsync-updater/update-to-autobuild.bat | 5 ++++- misc/tools/rsync-updater/update-to-autobuild.sh | 10 ++++++++-- misc/tools/rsync-updater/update-to-release.bat | 5 ++++- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/misc/tools/rsync-updater/update-to-autobuild.bat b/misc/tools/rsync-updater/update-to-autobuild.bat index c1ebb47c..5c02c30c 100644 --- a/misc/tools/rsync-updater/update-to-autobuild.bat +++ b/misc/tools/rsync-updater/update-to-autobuild.bat @@ -1,4 +1,5 @@ @echo off +setlocal if "%1" == "did-copy" goto copied cd %~dp0 @@ -6,7 +7,9 @@ rmdir /s /q %TEMP%\xonotic-rsync-updater mkdir %TEMP%\xonotic-rsync-updater for %%f in (*.exe *.dll *.bat) do copy /b %%f %TEMP%\xonotic-rsync-updater\ %TEMP%\xonotic-rsync-updater\%~n0 did-copy -exit +:: can only get here if above batch file couldn't be created +pause +exit /b :copied diff --git a/misc/tools/rsync-updater/update-to-autobuild.sh b/misc/tools/rsync-updater/update-to-autobuild.sh index d4872f64..c48629fb 100755 --- a/misc/tools/rsync-updater/update-to-autobuild.sh +++ b/misc/tools/rsync-updater/update-to-autobuild.sh @@ -2,13 +2,18 @@ cd "${0%/*}" || exit 1 -if ! which rsync > /dev/null; then +[ -t 2 ] && [ -t 1 ] && [ -t 0 ] && interactive=true || interactive=false + +if ! command -v rsync > /dev/null; then echo >&2 "FATAL: rsync not found, please install the rsync package" exit 1 fi if [ "$1" = "-y" ] || [ "$1" = "--yes" ]; then choice=y +elif [ $interactive = false ]; then + printf >&2 "\033[1;31mFATAL: non-interactive mode requires the \033[1;37m--yes\033[1;31m argument to acknowledge that this script will DELETE any custom files in the Xonotic directory.\033[m\n" + exit 1 fi until [ "$choice" = y ] || [ "$choice" = Y ]; do printf "This script will DELETE any custom files in the Xonotic folder. Do you want to continue [Y/N]? " @@ -91,4 +96,5 @@ fi resolvedtarget=$(cd $target && [ "${PWD#$HOME}" != "$PWD" ] && printf "~${PWD#$HOME}" || printf "$PWD") printf "Updating \033[1;34m$resolvedtarget\033[m from \033[0;36m$url \033[m...\n" -rsync $options $excludes "rsync://$url/" "$target" +# exec ensures this script stops before it's updated to prevent potential glitches +exec rsync $options $excludes "rsync://$url/" "$target" diff --git a/misc/tools/rsync-updater/update-to-release.bat b/misc/tools/rsync-updater/update-to-release.bat index 53dcab92..14735bd1 100644 --- a/misc/tools/rsync-updater/update-to-release.bat +++ b/misc/tools/rsync-updater/update-to-release.bat @@ -1,4 +1,5 @@ @echo off +setlocal if "%1" == "did-copy" goto copied cd %~dp0 @@ -6,7 +7,9 @@ rmdir /s /q %TEMP%\xonotic-rsync-updater mkdir %TEMP%\xonotic-rsync-updater for %%f in (*.exe *.dll *.bat) do copy /b %%f %TEMP%\xonotic-rsync-updater\ %TEMP%\xonotic-rsync-updater\%~n0 did-copy -exit +:: can only get here if above batch file couldn't be created +pause +exit /b :copied -- 2.39.5