Skip to content
Open
Show file tree
Hide file tree
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
28 changes: 27 additions & 1 deletion lib/download.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,32 @@ function Invoke-ScoopDownload ($app, $version, $manifest, $bucket, $architecture
return $urls.ForEach({ url_filename $_ })
}

function Link-OrCopyFile($Source, $Destination) {
if (Test-Path $Destination) {
Remove-Item $Destination -Force
}

# Only hardlink archives that will be extracted and removed.
# Standalone executables, scripts, or payloads that remain in the app directory
# are copied so in-place mutations cannot corrupt the shared cache.
$isArchive = $Source -match '\.(zip|7z|tar|gz|tgz|bz2|tbz2|xz|txz|zst|lzma|cab|iso|msi)$'

if ($isArchive) {
$sourceDrive = [System.IO.Path]::GetPathRoot([System.IO.Path]::GetFullPath($Source))
$destDrive = [System.IO.Path]::GetPathRoot([System.IO.Path]::GetFullPath($Destination))

if ($sourceDrive -and ($sourceDrive -eq $destDrive)) {
try {
$null = New-Item -ItemType HardLink -Path $Destination -Target $Source -Force -ErrorAction Stop
return
} catch {
# Fallback to copy if hardlink is unsupported on volume
}
}
}
Copy-Item $Source $Destination -Force
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

## [System.Net] downloader

function Invoke-CachedDownload ($app, $version, $url, $to, $cookies = $null, $use_cache = $true) {
Expand All @@ -62,7 +88,7 @@ function Invoke-CachedDownload ($app, $version, $url, $to, $cookies = $null, $us

if (!($null -eq $to)) {
if ($use_cache) {
Copy-Item $cached $to
Link-OrCopyFile $cached $to
} else {
Move-Item $cached $to -Force
}
Expand Down
38 changes: 38 additions & 0 deletions test/Scoop-Download.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,41 @@ Describe 'url_remote_filename' -Tag 'Scoop' {
url_remote_filename 'http://example.org/foo-v2.zip#/foo.zip' | Should -Be 'foo-v2.zip'
}
}

Describe 'Link-OrCopyFile' -Tag 'Scoop' {
BeforeAll {
$testDir = Join-Path $env:TEMP ("ScoopLinkTest_" + [guid]::NewGuid().ToString('N'))
New-Item -ItemType Directory -Path $testDir -Force | Out-Null
$cacheArchive = Join-Path $testDir 'app.zip'
Set-Content -Path $cacheArchive -Value 'test archive payload'
$cacheScript = Join-Path $testDir 'app.ps1'
Set-Content -Path $cacheScript -Value 'test script payload'
}
AfterAll {
Remove-Item -Recurse -Force $testDir -ErrorAction SilentlyContinue
}

It 'hardlinks archive payloads on the same volume' {
$stagedZip = Join-Path $testDir 'staged.zip'
Link-OrCopyFile $cacheArchive $stagedZip
Test-Path $stagedZip | Should -BeTrue
(Get-Item $stagedZip).LinkType | Should -Be 'HardLink'
}

It 'preserves cache file when staged archive link is deleted' {
$stagedZip = Join-Path $testDir 'staged_delete.zip'
Link-OrCopyFile $cacheArchive $stagedZip
Remove-Item $stagedZip -Force
Test-Path $cacheArchive | Should -BeTrue
(Get-Content $cacheArchive) | Should -Be 'test archive payload'
}

It 'copies non-archive payloads to prevent in-place cache mutation' {
$stagedScript = Join-Path $testDir 'staged.ps1'
Link-OrCopyFile $cacheScript $stagedScript
Test-Path $stagedScript | Should -BeTrue
Set-Content -Path $stagedScript -Value 'modified script'
(Get-Content $cacheArchive) | Should -Be 'test archive payload'
(Get-Content $cacheScript) | Should -Be 'test script payload'
}
}