From 74c29d215d6e9b144b85bad2fde1f5353696c376 Mon Sep 17 00:00:00 2001 From: Kinin-Code-Offical <125186556+Kinin-Code-Offical@users.noreply.github.com> Date: Mon, 22 Dec 2025 05:14:29 +0300 Subject: [PATCH] feat(p1): harden portable update swap --- src/core/selfUpdate.ts | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/core/selfUpdate.ts b/src/core/selfUpdate.ts index 42820d8..233c691 100644 --- a/src/core/selfUpdate.ts +++ b/src/core/selfUpdate.ts @@ -321,9 +321,25 @@ try { if ($p) { $p.WaitForExit() } } catch {} +Write-Host "Staging new binary..." +$TempExe = "$TargetExe.tmp" +Copy-Item -Path $NewExe -Destination $TempExe -Force + Write-Host "Swapping binaries..." -Move-Item -Path $TargetExe -Destination $BackupExe -Force -ErrorAction SilentlyContinue -Move-Item -Path $NewExe -Destination $TargetExe -Force +try { + Move-Item -Path $TargetExe -Destination $BackupExe -Force -ErrorAction SilentlyContinue + Move-Item -Path $TempExe -Destination $TargetExe -Force + Remove-Item -Path $BackupExe -Force -ErrorAction SilentlyContinue +} catch { + Write-Host "Swap failed, attempting rollback..." + if (Test-Path $BackupExe) { + Move-Item -Path $BackupExe -Destination $TargetExe -Force -ErrorAction SilentlyContinue + } + if (Test-Path $TempExe) { + Remove-Item -Path $TempExe -Force -ErrorAction SilentlyContinue + } + throw +} Write-Host "Starting new version..." Start-Process -FilePath $TargetExe -ArgumentList "--version"