Skip to content

Commit b2f7ad8

Browse files
feat(p1): harden portable update swap (#39)
1 parent ab17c22 commit b2f7ad8

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/core/selfUpdate.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,9 +321,25 @@ try {
321321
if ($p) { $p.WaitForExit() }
322322
} catch {}
323323
324+
Write-Host "Staging new binary..."
325+
$TempExe = "$TargetExe.tmp"
326+
Copy-Item -Path $NewExe -Destination $TempExe -Force
327+
324328
Write-Host "Swapping binaries..."
325-
Move-Item -Path $TargetExe -Destination $BackupExe -Force -ErrorAction SilentlyContinue
326-
Move-Item -Path $NewExe -Destination $TargetExe -Force
329+
try {
330+
Move-Item -Path $TargetExe -Destination $BackupExe -Force -ErrorAction SilentlyContinue
331+
Move-Item -Path $TempExe -Destination $TargetExe -Force
332+
Remove-Item -Path $BackupExe -Force -ErrorAction SilentlyContinue
333+
} catch {
334+
Write-Host "Swap failed, attempting rollback..."
335+
if (Test-Path $BackupExe) {
336+
Move-Item -Path $BackupExe -Destination $TargetExe -Force -ErrorAction SilentlyContinue
337+
}
338+
if (Test-Path $TempExe) {
339+
Remove-Item -Path $TempExe -Force -ErrorAction SilentlyContinue
340+
}
341+
throw
342+
}
327343
328344
Write-Host "Starting new version..."
329345
Start-Process -FilePath $TargetExe -ArgumentList "--version"

0 commit comments

Comments
 (0)