rsync: improve robustness of scripts, fix non-interactive mode
authorbones_was_here <bones_was_here@xonotic.au>
Mon, 17 Mar 2025 23:05:33 +0000 (09:05 +1000)
committerbones_was_here <bones_was_here@xonotic.au>
Thu, 20 Mar 2025 19:59:47 +0000 (05:59 +1000)
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
misc/tools/rsync-updater/update-to-autobuild.sh
misc/tools/rsync-updater/update-to-release.bat

index c1ebb47c9de58e7ee55557bf31a58eafcc9c8f8b..5c02c30c87983782c65047a4650535867207a12a 100644 (file)
@@ -1,4 +1,5 @@
 @echo off\r
+setlocal\r
 \r
 if "%1" == "did-copy" goto copied\r
 cd %~dp0\r
@@ -6,7 +7,9 @@ rmdir /s /q %TEMP%\xonotic-rsync-updater
 mkdir %TEMP%\xonotic-rsync-updater\r
 for %%f in (*.exe *.dll *.bat) do copy /b %%f %TEMP%\xonotic-rsync-updater\\r
 %TEMP%\xonotic-rsync-updater\%~n0 did-copy\r
-exit\r
+:: can only get here if above batch file couldn't be created\r
+pause\r
+exit /b\r
 \r
 :copied\r
 \r
index d4872f64e3cde3cc645b08d10cb374d39d0fc6b9..c48629fb4e1cfb6088ae587d6486ee7e40588207 100755 (executable)
@@ -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"
index 53dcab92eb7ef88d35d116f3c9a6d2d47152d356..14735bd177f9867d208916caa1797e89ed2309fd 100644 (file)
@@ -1,4 +1,5 @@
 @echo off\r
+setlocal\r
 \r
 if "%1" == "did-copy" goto copied\r
 cd %~dp0\r
@@ -6,7 +7,9 @@ rmdir /s /q %TEMP%\xonotic-rsync-updater
 mkdir %TEMP%\xonotic-rsync-updater\r
 for %%f in (*.exe *.dll *.bat) do copy /b %%f %TEMP%\xonotic-rsync-updater\\r
 %TEMP%\xonotic-rsync-updater\%~n0 did-copy\r
-exit\r
+:: can only get here if above batch file couldn't be created\r
+pause\r
+exit /b\r
 \r
 :copied\r
 \r