Skip to content

Commit cd39271

Browse files
check windows download for corruption and ensure file length is valid
1 parent dcc3a92 commit cd39271

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

.github/workflows/scripts/windows/swift/install-swift.ps1

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,41 @@ function Invoke-WebRequestWithRetry {
2929
Write-Host "Retry attempt $attempt of $MaxRetries after ${RetryDelay}s delay..."
3030
Start-Sleep -Seconds $RetryDelay
3131
}
32-
33-
# Use -Resume to support partial downloads if the connection drops
34-
Invoke-WebRequest -Uri $Uri -OutFile $OutFile -TimeoutSec $TimeoutSec -UseBasicParsing
35-
36-
Write-Host "Download completed successfully"
32+
33+
Write-Host "Attempt $attempt`: Downloading from $Uri"
34+
35+
# Clean up any existing partial download
36+
if (Test-Path $OutFile) {
37+
Remove-Item -Force $OutFile -ErrorAction SilentlyContinue
38+
}
39+
40+
# Get expected file size from HTTP headers
41+
$headRequest = Invoke-WebRequest -Uri $Uri -Method Head -UseBasicParsing -TimeoutSec 30
42+
$expectedSize = $null
43+
if ($headRequest.Headers.ContainsKey('Content-Length')) {
44+
$expectedSize = [long]$headRequest.Headers['Content-Length'][0]
45+
Write-Host "Expected file size: $($expectedSize / 1MB) MB"
46+
}
47+
48+
# Download with progress tracking disabled for better performance
49+
$webClient = New-Object System.Net.WebClient
50+
$webClient.DownloadFile($Uri, $OutFile)
51+
$webClient.Dispose()
52+
53+
# Verify file exists and has content
54+
if (-not (Test-Path $OutFile)) {
55+
throw "Download completed but file not found at $OutFile"
56+
}
57+
58+
$actualSize = (Get-Item $OutFile).Length
59+
Write-Host "Downloaded file size: $($actualSize / 1MB) MB"
60+
61+
# Verify file size matches expected size
62+
if ($expectedSize -and $actualSize -ne $expectedSize) {
63+
throw "File size mismatch. Expected: $expectedSize bytes, Got: $actualSize bytes"
64+
}
65+
66+
Write-Host "Download completed and verified successfully"
3767
return $true
3868
}
3969
catch {

0 commit comments

Comments
 (0)