param( [Parameter(Mandatory=$true)][string]$Source, [Parameter(Mandatory=$true)][string]$RemotePartial, [string]$KnownHosts = 'C:\temp\prod2u-thermal\receipts\known_hosts', [ValidateRange(65536,419430400)][int]$ChunkBytes = 1048576 ) $ErrorActionPreference='Stop' if(-not (Test-Path -LiteralPath $Source -PathType Leaf)){throw 'SOURCE_NOT_FOUND'} if(-not (Test-Path -LiteralPath $KnownHosts -PathType Leaf)){throw 'REMOTE_HOST_KEY_CONFIRMATION_REQUIRED'} if(-not $RemotePartial.EndsWith('.partial')){throw 'REMOTE_PARTIAL_SUFFIX_REQUIRED'} $delays=2,5,10,20,30 $wholeSha=(Get-FileHash -Algorithm SHA256 -LiteralPath $Source).Hash.ToLowerInvariant() $final=$RemotePartial.Substring(0,$RemotePartial.Length-8) $remoteParts="$RemotePartial.parts-$wholeSha" $stage=Join-Path 'C:\temp\prod2u-thermal\staging' "transfer-$wholeSha" $ssh=@('-o','BatchMode=yes','-o','StrictHostKeyChecking=yes', '-o',"UserKnownHostsFile=$KnownHosts",'-o','ConnectTimeout=10','-o','ServerAliveInterval=15','-o','ServerAliveCountMax=4') function Invoke-RemoteChecked([string]$RemoteCommand){ $encoded=[Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($RemoteCommand)) for($i=0;$i -lt $delays.Count;$i++){ $savedPreference=$ErrorActionPreference $ErrorActionPreference='Continue' $output=& ssh @ssh edk2-uefi-lab "echo $encoded | base64 -d | bash" 2>&1 $exitCode=$LASTEXITCODE $ErrorActionPreference=$savedPreference if($exitCode -eq 0){return $output} if($i -eq $delays.Count-1){throw 'REMOTE_NETWORK_UNSTABLE'} Start-Sleep -Seconds $delays[$i] } } New-Item -ItemType Directory -Force -Path $stage | Out-Null try { $input=[IO.File]::OpenRead((Resolve-Path -LiteralPath $Source)) try { $buffer=New-Object byte[] $ChunkBytes $index=0 while(($count=$input.Read($buffer,0,$buffer.Length)) -gt 0){ $name=('part-{0:D6}' -f $index) $path=Join-Path $stage $name $out=[IO.File]::Open($path,[IO.FileMode]::Create,[IO.FileAccess]::Write,[IO.FileShare]::None) try {$out.Write($buffer,0,$count)} finally {$out.Dispose()} $index++ } } finally {$input.Dispose()} Invoke-RemoteChecked "mkdir -p '$remoteParts'" foreach($part in Get-ChildItem -LiteralPath $stage -File | Sort-Object Name){ $partSha=(Get-FileHash -Algorithm SHA256 -LiteralPath $part.FullName).Hash.ToLowerInvariant() $remotePart="$remoteParts/$($part.Name)" $present=Invoke-RemoteChecked "if test -f '$remotePart' && test \`$(sha256sum '$remotePart' | cut -d' ' -f1) = '$partSha'; then echo PRESENT; else echo MISSING; fi" if($present -notcontains 'PRESENT'){ $sent=$false for($i=0;$i -lt $delays.Count;$i++){ $savedPreference=$ErrorActionPreference $ErrorActionPreference='Continue' & scp @ssh $part.FullName "edk2-uefi-lab:$remotePart.partial" $exitCode=$LASTEXITCODE $ErrorActionPreference=$savedPreference if($exitCode -eq 0){ $verified=Invoke-RemoteChecked "test \`$(sha256sum '$remotePart.partial' | cut -d' ' -f1) = '$partSha' && mv '$remotePart.partial' '$remotePart' && echo VERIFIED" if($verified -contains 'VERIFIED'){$sent=$true;break} } if($i -lt $delays.Count-1){Start-Sleep -Seconds $delays[$i]} } if(-not $sent){throw 'REMOTE_NETWORK_UNSTABLE'} } } $count=(Get-ChildItem -LiteralPath $stage -File).Count Invoke-RemoteChecked "test \`$(find '$remoteParts' -maxdepth 1 -type f -name 'part-*' | wc -l) -eq $count && cat '$remoteParts'/part-* > '$RemotePartial' && test \`$(sha256sum '$RemotePartial' | cut -d' ' -f1) = '$wholeSha' && mv '$RemotePartial' '$final' && rm -rf '$remoteParts'" Write-Output "TRANSFER_PASS sha256=$wholeSha remote=$final" } finally { if(Test-Path -LiteralPath $stage){Remove-Item -LiteralPath $stage -Recurse -Force} }