Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 additions & 2 deletions src-tauri/src/services/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ pub fn prepare_paths(exe_name: &std::ffi::OsStr) -> Result<UpdatePaths, String>
let new_exe = temp_dir.join(exe_name);
let batch_path = temp_dir.join("updater.bat");

Ok(UpdatePaths { temp_dir, new_exe, batch_path })
Ok(UpdatePaths {
temp_dir,
new_exe,
batch_path,
})
}

pub async fn download_new_exe<F>(
Expand Down Expand Up @@ -101,7 +105,7 @@ echo 启动新版本...
start "" /min "{current_exe}"

echo 清理临时文件...
start "" /min cmd /c "timeout /t 3 /nobreak >nul & if exist \"{temp_dir}\" rd /s /q \"{temp_dir}\""
start "" /min powershell -NoProfile -ExecutionPolicy Bypass -Command "param([string]$p) Start-Sleep -Seconds 3; if (Test-Path -LiteralPath $p) {{ Remove-Item -LiteralPath $p -Recurse -Force }}" "{temp_dir}"

exit /b 0
"#,
Expand All @@ -113,3 +117,35 @@ exit /b 0

batch_content.replace('\n', "\r\n")
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn build_updater_batch_uses_powershell_literalpath_cleanup() {
let content = build_updater_batch(
"endfield-cat.exe",
Path::new("C:\\Temp\\endfield-cat-update\\new.exe"),
Path::new("C:\\Program Files\\EndCat\\endfield-cat.exe"),
Path::new("C:\\Temp\\endfield-cat-update"),
);

// In cmd.exe, `\"` is not an escape; it can break parsing and lead to paths like `\\`.
assert!(!content.contains("\\\""));
assert!(content.contains(r#"powershell -NoProfile -ExecutionPolicy Bypass -Command "param([string]$p) Start-Sleep -Seconds 3; if (Test-Path -LiteralPath $p) { Remove-Item -LiteralPath $p -Recurse -Force }""#));
assert!(content.contains(r#""C:\Temp\endfield-cat-update""#));
}

#[test]
fn build_updater_batch_cleanup_quotes_ampersand_path() {
let content = build_updater_batch(
"endfield-cat.exe",
Path::new("C:\\Temp\\endfield-cat-update\\new.exe"),
Path::new("C:\\Program Files\\EndCat\\endfield-cat.exe"),
Path::new("C:\\Temp\\A&B\\endfield-cat-update"),
);

assert!(content.contains(r#""C:\Temp\A&B\endfield-cat-update""#));
}
}
Loading