From 13b1f0266e73860e587162532861d501990dde20 Mon Sep 17 00:00:00 2001 From: Chawye Hsu Date: Sun, 28 Apr 2024 12:55:40 +0800 Subject: [PATCH 1/8] feat(core): New cache filename format Signed-off-by: Chawye Hsu --- lib/core.ps1 | 17 ++++++++++++++++- libexec/scoop-cache.ps1 | 4 ++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/core.ps1 b/lib/core.ps1 index 2211a51972..7536128164 100644 --- a/lib/core.ps1 +++ b/lib/core.ps1 @@ -409,7 +409,22 @@ function currentdir($app, $global) { function persistdir($app, $global) { "$(basedir $global)\persist\$app" } function usermanifestsdir { "$(basedir)\workspace" } function usermanifest($app) { "$(usermanifestsdir)\$app.json" } -function cache_path($app, $version, $url) { "$cachedir\$app#$version#$($url -replace '[^\w\.\-]+', '_')" } +function cache_path($app, $version, $url) { + $underscoredUrl = $url -replace '[^\w\.\-]+', '_' + $filePath = "$cachedir\$app#$version#$underscoredUrl" + + # NOTE: Scoop cache files migration. Remove this 6 months after it ships. + if (Test-Path $filePath) { + return $filePath + } + + $urlStream = [System.IO.MemoryStream]::new([System.Text.Encoding]::UTF8.GetBytes($url)) + $sha = (Get-FileHash -Algorithm SHA1 -InputStream $urlStream).Hash.ToLower() + $extension = [System.IO.Path]::GetExtension($url) + $filePath = $filePath -replace "$underscoredUrl", "$sha$extension" + + return $filePath +} # apps function sanitary_path($path) { return [regex]::replace($path, "[/\\?:*<>|]", "") } diff --git a/libexec/scoop-cache.ps1 b/libexec/scoop-cache.ps1 index 959c73fcde..f390c258ad 100644 --- a/libexec/scoop-cache.ps1 +++ b/libexec/scoop-cache.ps1 @@ -16,7 +16,7 @@ param($cmd) function cacheinfo($file) { $app, $version, $url = $file.Name -split '#' - New-Object PSObject -Property @{ Name = $app; Version = $version; Length = $file.Length; URL = $url } + New-Object PSObject -Property @{ Name = $app; Version = $version; Length = $file.Length } } function cacheshow($app) { @@ -28,7 +28,7 @@ function cacheshow($app) { $files = @(Get-ChildItem $cachedir | Where-Object -Property Name -Value "^$app#" -Match) $totalLength = ($files | Measure-Object -Property Length -Sum).Sum - $files | ForEach-Object { cacheinfo $_ } | Select-Object Name, Version, Length, URL + $files | ForEach-Object { cacheinfo $_ } | Select-Object Name, Version, Length Write-Host "Total: $($files.Length) $(pluralize $files.Length 'file' 'files'), $(filesize $totalLength)" -ForegroundColor Yellow } From a06a461ce28c77fe0dd72af55f0a311a032240ba Mon Sep 17 00:00:00 2001 From: Chawye Hsu Date: Mon, 29 Apr 2024 11:19:38 +0800 Subject: [PATCH 2/8] update chglog Signed-off-by: Chawye Hsu --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b8c8cd75a6..3065fa7a72 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### Features - **scoop-search:** Use SQLite for caching apps to speed up local search ([#5851](https://github.com/ScoopInstaller/Scoop/issues/5851), [#5918](https://github.com/ScoopInstaller/Scoop/issues/5918)) +- **core:** New cache filename format ([#5929](https://github.com/ScoopInstaller/Scoop/issues/5929) ### Bug Fixes From a82807e4646510a6e1924c764ecb9afaaa25038c Mon Sep 17 00:00:00 2001 From: Chawye Hsu Date: Mon, 29 Apr 2024 11:58:19 +0800 Subject: [PATCH 3/8] add unit tests Signed-off-by: Chawye Hsu --- lib/core.ps1 | 2 +- test/Scoop-Core.Tests.ps1 | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/core.ps1 b/lib/core.ps1 index 7536128164..8007ea3ed6 100644 --- a/lib/core.ps1 +++ b/lib/core.ps1 @@ -413,7 +413,7 @@ function cache_path($app, $version, $url) { $underscoredUrl = $url -replace '[^\w\.\-]+', '_' $filePath = "$cachedir\$app#$version#$underscoredUrl" - # NOTE: Scoop cache files migration. Remove this 6 months after it ships. + # NOTE: Scoop cache files migration. Remove this 6 months after the feature ships. if (Test-Path $filePath) { return $filePath } diff --git a/test/Scoop-Core.Tests.ps1 b/test/Scoop-Core.Tests.ps1 index 3fb7fbd663..bb388ee3e3 100644 --- a/test/Scoop-Core.Tests.ps1 +++ b/test/Scoop-Core.Tests.ps1 @@ -259,6 +259,29 @@ Describe 'get_app_name_from_shim' -Tag 'Scoop', 'Windows' { } } +Describe 'cache_path' -Tag 'Scoop' { + BeforeAll { + $cachedir = "$([IO.Path]::GetTempPath())ScoopTestFixtures\cache" + } + + It 'returns the correct cache path for a given input' { + $url = 'https://example.com/git.zip' + $ret = cache_path 'git' '2.44.0' $url + $inputStream = [System.IO.MemoryStream]::new([System.Text.Encoding]::UTF8.GetBytes($url)) + $sha = (Get-FileHash -Algorithm SHA1 -InputStream $inputStream).Hash.ToLower() + $ret | Should -Be "$cachedir\git#2.44.0#$sha.zip" + } + + # # NOTE: Remove this 6 months after the feature ships. + It 'returns the old format cache path for a given input' { + $fixture = "$cachedir\git#2.44.0#https_example.com_git.zip" + New-Item -ItemType File -Path $fixture -Force + $ret = cache_path 'git' '2.44.0' 'https://example.com/git.zip' + $ret | Should -Be $fixture + Remove-Item -Path $fixture -Force + } +} + Describe 'sanitary_path' -Tag 'Scoop' { It 'removes invalid path characters from a string' { $path = 'test?.json' From 13085da7e2cbb01174490ca4e4743c1404b2a84a Mon Sep 17 00:00:00 2001 From: Chawye Hsu Date: Mon, 29 Apr 2024 17:58:38 +0800 Subject: [PATCH 4/8] Update lib/core.ps1 Co-authored-by: Hsiao-nan Cheung --- lib/core.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/core.ps1 b/lib/core.ps1 index 8007ea3ed6..90184fd5b6 100644 --- a/lib/core.ps1 +++ b/lib/core.ps1 @@ -419,7 +419,7 @@ function cache_path($app, $version, $url) { } $urlStream = [System.IO.MemoryStream]::new([System.Text.Encoding]::UTF8.GetBytes($url)) - $sha = (Get-FileHash -Algorithm SHA1 -InputStream $urlStream).Hash.ToLower() + $sha = (Get-FileHash -Algorithm SHA256 -InputStream $urlStream).Hash.ToLower().Substring(0, 7) $extension = [System.IO.Path]::GetExtension($url) $filePath = $filePath -replace "$underscoredUrl", "$sha$extension" From 5485698459a9ed613b7f165f36c6f4f12b110a2a Mon Sep 17 00:00:00 2001 From: Chawye Hsu Date: Mon, 29 Apr 2024 17:58:46 +0800 Subject: [PATCH 5/8] Update test/Scoop-Core.Tests.ps1 Co-authored-by: Hsiao-nan Cheung --- test/Scoop-Core.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Scoop-Core.Tests.ps1 b/test/Scoop-Core.Tests.ps1 index bb388ee3e3..c966896461 100644 --- a/test/Scoop-Core.Tests.ps1 +++ b/test/Scoop-Core.Tests.ps1 @@ -268,7 +268,7 @@ Describe 'cache_path' -Tag 'Scoop' { $url = 'https://example.com/git.zip' $ret = cache_path 'git' '2.44.0' $url $inputStream = [System.IO.MemoryStream]::new([System.Text.Encoding]::UTF8.GetBytes($url)) - $sha = (Get-FileHash -Algorithm SHA1 -InputStream $inputStream).Hash.ToLower() + $sha = (Get-FileHash -Algorithm SHA256 -InputStream $inputStream).Hash.ToLower().Substring(0, 7) $ret | Should -Be "$cachedir\git#2.44.0#$sha.zip" } From deefb69a4ab1df08d9e8baf61a6793d11121ce71 Mon Sep 17 00:00:00 2001 From: Chawye Hsu Date: Mon, 29 Apr 2024 17:58:58 +0800 Subject: [PATCH 6/8] Update test/Scoop-Core.Tests.ps1 Co-authored-by: Hsiao-nan Cheung --- test/Scoop-Core.Tests.ps1 | 4 ---- 1 file changed, 4 deletions(-) diff --git a/test/Scoop-Core.Tests.ps1 b/test/Scoop-Core.Tests.ps1 index c966896461..7af9a29d07 100644 --- a/test/Scoop-Core.Tests.ps1 +++ b/test/Scoop-Core.Tests.ps1 @@ -260,10 +260,6 @@ Describe 'get_app_name_from_shim' -Tag 'Scoop', 'Windows' { } Describe 'cache_path' -Tag 'Scoop' { - BeforeAll { - $cachedir = "$([IO.Path]::GetTempPath())ScoopTestFixtures\cache" - } - It 'returns the correct cache path for a given input' { $url = 'https://example.com/git.zip' $ret = cache_path 'git' '2.44.0' $url From 9eafef227294dbb2b4943da92d16f03d2f7c6bd2 Mon Sep 17 00:00:00 2001 From: Chawye Hsu Date: Mon, 29 Apr 2024 17:59:06 +0800 Subject: [PATCH 7/8] Update test/Scoop-Core.Tests.ps1 Co-authored-by: Hsiao-nan Cheung --- test/Scoop-Core.Tests.ps1 | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/Scoop-Core.Tests.ps1 b/test/Scoop-Core.Tests.ps1 index 7af9a29d07..7c07a93900 100644 --- a/test/Scoop-Core.Tests.ps1 +++ b/test/Scoop-Core.Tests.ps1 @@ -270,8 +270,7 @@ Describe 'cache_path' -Tag 'Scoop' { # # NOTE: Remove this 6 months after the feature ships. It 'returns the old format cache path for a given input' { - $fixture = "$cachedir\git#2.44.0#https_example.com_git.zip" - New-Item -ItemType File -Path $fixture -Force + Mock Test-Path { $true } $ret = cache_path 'git' '2.44.0' 'https://example.com/git.zip' $ret | Should -Be $fixture Remove-Item -Path $fixture -Force From 2e4096e956fe39f79f288cd6c2b4358d71fbfd42 Mon Sep 17 00:00:00 2001 From: Chawye Hsu Date: Mon, 29 Apr 2024 17:59:12 +0800 Subject: [PATCH 8/8] Update test/Scoop-Core.Tests.ps1 Co-authored-by: Hsiao-nan Cheung --- test/Scoop-Core.Tests.ps1 | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/Scoop-Core.Tests.ps1 b/test/Scoop-Core.Tests.ps1 index 7c07a93900..f6846e5456 100644 --- a/test/Scoop-Core.Tests.ps1 +++ b/test/Scoop-Core.Tests.ps1 @@ -272,8 +272,7 @@ Describe 'cache_path' -Tag 'Scoop' { It 'returns the old format cache path for a given input' { Mock Test-Path { $true } $ret = cache_path 'git' '2.44.0' 'https://example.com/git.zip' - $ret | Should -Be $fixture - Remove-Item -Path $fixture -Force + $ret | Should -Be "$cachedir\git#2.44.0#https_example.com_git.zip" } }