v1.8.99: fix update script — delete-before-copy, size verification, error logging

Previous BAT script falsely reported success because `if exist` checked
the pre-existing file, not the copy result. Now: deletes old DST first,
copies with /B (binary), verifies size matches expected, logs all errors.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
chrome-storm-c442
2026-03-03 03:12:54 -05:00
parent c9e3ee8fc5
commit f2dc978c57
3 changed files with 33 additions and 11 deletions

View File

@@ -268,19 +268,23 @@ class UpdateChecker:
bat_path = os.path.join(tmp_dir, "sm_update.bat")
log_path = os.path.join(tmp_dir, "sm_update.log")
pid = os.getpid()
new_size = os.path.getsize(new_exe)
bat_content = f"""@echo off
setlocal enabledelayedexpansion
chcp 65001 >nul 2>&1
set "LOGFILE={log_path}"
set "SRC={new_exe}"
set "DST={current_exe}"
set "PID={pid}"
set "TMPDIR={tmp_dir}"
set "EXPECTED_SIZE={new_size}"
echo [%date% %time%] Update script started >> "%LOGFILE%"
echo [%date% %time%] PID to wait for: %PID% >> "%LOGFILE%"
echo [%date% %time%] SRC: %SRC% >> "%LOGFILE%"
echo [%date% %time%] DST: %DST% >> "%LOGFILE%"
echo [%date% %time%] Expected size: %EXPECTED_SIZE% >> "%LOGFILE%"
:wait_loop
tasklist /FI "PID eq %PID%" 2>nul | find "%PID%" >nul
@@ -300,17 +304,39 @@ for /d %%D in ("%TMPDIR%\\_MEI*") do (
)
timeout /t 1 /nobreak >nul
rem Copy with retry
rem Log source file size
for %%F in ("%SRC%") do (
echo [%date% %time%] SRC file size: %%~zF >> "%LOGFILE%"
)
rem Delete old DST first so copy is clean
echo [%date% %time%] Deleting old DST... >> "%LOGFILE%"
del /f /q "%DST%" >nul 2>&1
if exist "%DST%" (
echo [%date% %time%] WARNING: could not delete old DST >> "%LOGFILE%"
)
timeout /t 1 /nobreak >nul
rem Copy with retry and size verification
echo [%date% %time%] Starting copy... >> "%LOGFILE%"
set COPIED=0
for /L %%i in (1,1,5) do (
if !COPIED!==0 (
copy /Y "%SRC%" "%DST%" >nul 2>&1
echo [%date% %time%] Copy attempt %%i... >> "%LOGFILE%"
copy /Y /B "%SRC%" "%DST%" >> "%LOGFILE%" 2>&1
if exist "%DST%" (
echo [%date% %time%] Copy attempt %%i done >> "%LOGFILE%"
set COPIED=1
for %%F in ("%DST%") do set DST_SIZE=%%~zF
echo [%date% %time%] DST size after copy: !DST_SIZE! >> "%LOGFILE%"
if "!DST_SIZE!"=="%EXPECTED_SIZE%" (
echo [%date% %time%] Size verified OK >> "%LOGFILE%"
set COPIED=1
) else (
echo [%date% %time%] Size mismatch! Expected %EXPECTED_SIZE%, got !DST_SIZE! >> "%LOGFILE%"
del /f /q "%DST%" >nul 2>&1
timeout /t 2 /nobreak >nul
)
) else (
echo [%date% %time%] Copy attempt %%i failed >> "%LOGFILE%"
echo [%date% %time%] Copy failed - DST does not exist >> "%LOGFILE%"
timeout /t 2 /nobreak >nul
)
)
@@ -340,16 +366,12 @@ echo [%date% %time%] Cleanup done >> "%LOGFILE%"
rem Self-delete
del /f /q "%~f0" >nul 2>&1
"""
# Enable delayed expansion for variables inside for loops
bat_content = bat_content.replace("@echo off",
"@echo off\nsetlocal enabledelayedexpansion")
with open(bat_path, "w", encoding="utf-8") as f:
f.write(bat_content)
log.info(f"Update BAT: {bat_path}, log: {log_path}")
# Launch BAT minimized (not hidden — need cmd.exe to run)
# Launch BAT minimized
subprocess.Popen(
["cmd.exe", "/c", "start", "/min", "", bat_path],
creationflags=_SUBPROCESS_FLAGS,

Binary file not shown.

View File

@@ -1,6 +1,6 @@
"""Version info for ServerManager."""
__version__ = "1.8.98"
__version__ = "1.8.99"
__app_name__ = "ServerManager"
__author__ = "aibot777"
__description__ = "Desktop GUI for managing remote servers"