Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(install): Avoid checking all files for unlink persisted data #4681

Merged
merged 7 commits into from
Feb 25, 2022
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
- **scoop-cleanup:** Remove apps other than current version ([#4665](https://github.com/ScoopInstaller/Scoop/issues/4665))
- **scoop-update:** Skip updating non git buckets ([#4670](https://github.com/ScoopInstaller/Scoop/issues/4670), [#4672](https://github.com/ScoopInstaller/Scoop/issues/4672))

### Performance Improvements

- **uninstall:** Avoid checking all files for unlinking persisted data ([#4681](https://github.com/ScoopInstaller/Scoop/issues/4681))

### Code Refactoring

- **depends:** Rewrite 'depends.ps1' ([#4638](https://github.com/ScoopInstaller/Scoop/issues/4638), [#4673](https://github.com/ScoopInstaller/Scoop/issues/4673))
Expand Down
2 changes: 1 addition & 1 deletion lib/core.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ function shim($path, $global, $name, $arg) {
} else {
warn_on_overwrite "$shim.cmd" $path
# find path to Git's bash so that batch scripts can run bash scripts
$gitdir = (Get-Item (Get-Command git -ErrorAction:Stop).Source -ErrorAction:Stop).Directory.Parent
$gitdir = (Get-Item (Get-Command git -CommandType:Application -ErrorAction:Stop).Source -ErrorAction:Stop).Directory.Parent
if ($gitdir.FullName -imatch 'mingw') {
$gitdir = $gitdir.Parent
}
Expand Down
34 changes: 19 additions & 15 deletions lib/install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1177,7 +1177,7 @@ function persist_data($manifest, $original_dir, $persist_dir) {
# ensure target parent folder exist
ensure (Split-Path -Path $target) | Out-Null
Move-Item $source $target
# we don't have neither source nor target data! we need to crate an empty target,
# we don't have neither source nor target data! we need to create an empty target,
# but we can't make a judgement that the data should be a file or directory...
# so we create a directory by default. to avoid this, use pre_install
# to create the source file before persisting (DON'T use post_install)
Expand All @@ -1199,21 +1199,25 @@ function persist_data($manifest, $original_dir, $persist_dir) {
}
}

function unlink_persist_data($dir) {
function unlink_persist_data($manifest, $dir) {
$persist = $manifest.persist
# unlink all junction / hard link in the directory
Get-ChildItem -Recurse $dir | ForEach-Object {
$file = $_
if ($null -ne $file.LinkType) {
$filepath = $file.FullName
# directory (junction)
if ($file -is [System.IO.DirectoryInfo]) {
# remove read-only attribute on the link
attrib -R /L $filepath
# remove the junction
Remove-Item -Path $filepath -Recurse -Force -ErrorAction SilentlyContinue
} else {
# remove the hard link
Remove-Item -Path $filepath -Force -ErrorAction SilentlyContinue
if ($persist) {
@($persist) | ForEach-Object {
$source, $null = persist_def $_
$source = Get-Item "$dir\$source"
if ($source.LinkType) {
$source_path = $source.FullName
# directory (junction)
if ($source -is [System.IO.DirectoryInfo]) {
# remove read-only attribute on the link
attrib -R /L $source_path
# remove the junction
Remove-Item -Path $source_path -Recurse -Force -ErrorAction SilentlyContinue
} else {
# remove the hard link
Remove-Item -Path $source_path -Force -ErrorAction SilentlyContinue
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion libexec/scoop-cleanup.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function cleanup($app, $global, $verbose, $cache) {
Write-Host " $version" -NoNewline
$dir = versiondir $app $version $global
# unlink all potential old link before doing recursive Remove-Item
unlink_persist_data $dir
unlink_persist_data $manifest $dir
Remove-Item $dir -ErrorAction Stop -Recurse -Force
}
$leftVersions = Get-ChildItem $appDir
Expand Down
2 changes: 1 addition & 1 deletion libexec/scoop-reset.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ $apps | ForEach-Object {
env_add_path $manifest $dir $global $architecture
env_set $manifest $dir $global $architecture
# unlink all potential old link before re-persisting
unlink_persist_data $original_dir
unlink_persist_data $manifest $original_dir
persist_data $manifest $original_dir $persist_dir
persist_permission $manifest $global
}
Expand Down
4 changes: 2 additions & 2 deletions libexec/scoop-uninstall.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ if (!$apps) { exit 0 }

try {
# unlink all potential old link before doing recursive Remove-Item
unlink_persist_data $dir
unlink_persist_data $manifest $dir
Remove-Item $dir -Recurse -Force -ErrorAction Stop
} catch {
if (Test-Path $dir) {
Expand All @@ -107,7 +107,7 @@ if (!$apps) { exit 0 }
$dir = versiondir $app $version $global
try {
# unlink all potential old link before doing recursive Remove-Item
unlink_persist_data $dir
unlink_persist_data $manifest $dir
Remove-Item $dir -Recurse -Force -ErrorAction Stop
} catch {
error "Couldn't remove '$(friendly_path $dir)'; it may be in use."
Expand Down