Skip to content

Commit

Permalink
Apply helper function from ScoopInstaller#3212
Browse files Browse the repository at this point in the history
  • Loading branch information
niheaven committed Mar 13, 2019
1 parent 3dfc4f2 commit f974052
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 30 deletions.
20 changes: 19 additions & 1 deletion lib/core.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,25 @@ function isFileLocked([string]$path) {
}

function is_directory([String] $path) {
return (Test-Path $path) -and (Get-Item $path) -is [System.IO.DirectoryInfo]
return (Test-Path $path) -and (Get-Item -Path $path) -is [System.IO.DirectoryInfo]
}

function create_junction([String] $link, [String] $target) {
if (!(Test-Path $link) -and (is_directory $target)) {
New-Item -ItemType Junction -Path $link -Target $target | Out-Null
$dirInfo = New-Object System.IO.DirectoryInfo($link)
$dirInfo.Attributes = $dirInfo.Attributes -bor [System.IO.FileAttributes]::ReadOnly
return $true
}
return $false
}

function create_hardlink([String] $link, [String] $target) {
if (!(Test-Path $link) -and (Test-Path $target) -and !(is_directory $target)) {
New-Item -ItemType HardLink -Path $link -Target $target | Out-Null
return $true
}
return $false
}

function movedir($from, $to) {
Expand Down
34 changes: 8 additions & 26 deletions lib/install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -927,14 +927,11 @@ function link_current($versiondir) {
abort "Error: Version 'current' is not allowed!"
}

if(test-path $currentdir) {
# remove the junction
attrib -R /L $currentdir
& "$env:COMSPEC" /c rmdir $currentdir
# recreate new junction
if (Test-Path $currentdir) {
Remove-Item $currentdir -Recurse -Force | Out-Null
}

& "$env:COMSPEC" /c mklink /j $currentdir $versiondir | out-null
attrib $currentdir +R /L
create_junction $currentdir $versiondir | Out-Null
return $currentdir
}

Expand All @@ -950,11 +947,7 @@ function unlink_current($versiondir) {
if(test-path $currentdir) {
write-host "Unlinking $(friendly_path $currentdir)"

# remove read-only attribute on link
attrib $currentdir -R /L

# remove the junction
& "$env:COMSPEC" /c "rmdir $currentdir"
Remove-Item $currentdir -Recurse -Force | Out-Null
return $currentdir
}
return $versiondir
Expand Down Expand Up @@ -1192,11 +1185,10 @@ function persist_data($manifest, $original_dir, $persist_dir) {
# create link
if (is_directory $target) {
# target is a directory, create junction
New-Item -Path $source -ItemType Junction -Value $target | Out-Null
attrib $source +R /L
create_junction $target $source | Out-Null
} else {
# target is a file, create hard link
New-Item -Path $source -ItemType HardLink -Value $target | Out-Null
create_hardlink $target $source | Out-Null
}
}
}
Expand All @@ -1207,17 +1199,7 @@ function unlink_persist_data($dir) {
Get-ChildItem -Recurse $dir | ForEach-Object {
$file = $_
if ($null -ne $file.LinkType) {
$filepath = $file.FullName
# directory (junction)
if (is_directory $filepath) {
# remove read-only attribute on the link
attrib -R /L $filepath
# remove the junction
Remove-Item -Path $filepath -Recurse -Force | Out-Null
} else {
# remove the hard link
Remove-Item -Path $filepath -Force | Out-Null
}
Remove-Item $file.FullName -Recurse -Force | Out-Null
}
}
}
Expand Down
43 changes: 40 additions & 3 deletions test/Scoop-Core.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,58 @@
$repo_dir = (Get-Item $MyInvocation.MyCommand.Path).directory.parent.FullName
$isUnix = is_unix

describe "is_directory" -Tag 'Scoop' {
describe "directory/junction/hardlink handling" -Tag 'Scoop' {
beforeall {
$working_dir = setup_working "is_directory"
}

it "is_directory recognize directories" {
it "is_directory recognizes directories" {
is_directory "$working_dir\i_am_a_directory" | should -be $true
}
it "is_directory recognize files" {

it "is_directory recognizes files" {
is_directory "$working_dir\i_am_a_file.txt" | should -be $false
}

it "is_directory is falsey on unknown path" {
is_directory "$working_dir\i_do_not_exist" | should -be $false
}

it "create_junction recognizes directories and creates junction" {
create_junction "$working_dir\i_am_a_junction" "$working_dir\i_am_a_directory" | should -be $true
Test-Path "$working_dir\i_am_a_junction" | should -be $true
}

it "create_junction recognizes existing target" {
create_junction "$working_dir\i_am_a_junction" "$working_dir\i_am_a_directory" | should -be $false
}

it "create_junction recognizes files" {
create_junction "$working_dir\i_am_a_junction_file.txt" "$working_dir\i_am_a_file.txt" | should -be $false
Test-Path "$working_dir\i_am_a_junction_file.txt" | should -be $false
}

it "create_junction is falsey on unknown path" {
create_junction "$working_dir\i_am_a_junction" "$working_dir\i_do_not_exist" | should -be $false
}

it "create_hardlink recognizes files and creates hardlink" {
create_hardlink "$working_dir\i_am_a_hardlinked_file.txt" "$working_dir\i_am_a_file.txt" | should -be $true
Test-Path "$working_dir\i_am_a_hardlinked_file.txt" | should -be $true
}

it "create_hardlink recognizes existing target" {
create_hardlink "$working_dir\i_am_a_hardlinked_file.txt" "$working_dir\i_am_a_file.txt" | should -be $false
}

it "create_hardlink recognizes directories" {
create_hardlink "$working_dir\i_am_a_hardlinked_directory" "$working_dir\i_am_a_directory" | should -be $false
Test-Path "$working_dir\i_am_a_hardlinked_directory" | should -be $false
}

it "create_hardlink is falsey on unknown path" {
create_hardlink "$working_dir\i_do_not_exist.txt" "$working_dir\i_do_not_exist.txt" | should -be $false
}
}

describe "movedir" -Tag 'Scoop' {
Expand Down

0 comments on commit f974052

Please sign in to comment.