Skip to content

Commit

Permalink
perf(install): Avoid checking all files for unlink persisted data (Sc…
Browse files Browse the repository at this point in the history
  • Loading branch information
younger-1 authored Feb 25, 2022
1 parent 40b2d95 commit 4047d69
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 20 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
- **scoop-update:** Skip updating non git buckets ([#4670](https://github.com/ScoopInstaller/Scoop/issues/4670), [#4672](https://github.com/ScoopInstaller/Scoop/issues/4672))
- **scoop-bucket:** Check for git before running command ([#4756](https://github.com/ScoopInstaller/Scoop/issues/4756))

### 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

0 comments on commit 4047d69

Please sign in to comment.