From 666e83636a9b011e941294bacb3db79b0e069c40 Mon Sep 17 00:00:00 2001 From: Kinshuk Bairagi Date: Tue, 22 Mar 2022 20:11:48 +0530 Subject: [PATCH] Fix junction links --- lib/install.ps1 | 29 +++++++++++++++++++++++++++-- lib/psmodules.ps1 | 2 +- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/lib/install.ps1 b/lib/install.ps1 index 1a84bc2d38..78eab908e0 100644 --- a/lib/install.ps1 +++ b/lib/install.ps1 @@ -946,7 +946,7 @@ function link_current($versiondir) { Remove-Item $currentdir -Recurse -Force -ErrorAction Stop } - New-Item -Path $currentdir -ItemType Junction -Value $versiondir | Out-Null + New-DirectoryJunction($currentdir, $versiondir) attrib $currentdir +R /L return $currentdir } @@ -1197,7 +1197,7 @@ 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 + New-DirectoryJunction($source, $target) attrib $source +R /L } else { # target is a file, create hard link @@ -1260,3 +1260,28 @@ function test_running_process($app, $global) { return $false } } + +function Test-IsInsideContainer { + $foundService = Get-Service -Name cexecsvc -ErrorAction SilentlyContinue + if ( $foundService -eq $null ) { + $false + } + else { + $true + } +} + +function New-DirectoryJunction($source, $target) { + + if (Test-Path $target) { + warn "Target directory `$target` already exists." + return $false + } + + if (Test-IsInsideContainer) { + & "$env:COMSPEC" /c "mklink /j `"$source`" `"$target`"" | out-null + } else { + New-Item -Path $source -ItemType Junction -Value $target | Out-Null + } + +} \ No newline at end of file diff --git a/lib/psmodules.ps1 b/lib/psmodules.ps1 index fc32e99425..beac908d7e 100644 --- a/lib/psmodules.ps1 +++ b/lib/psmodules.ps1 @@ -26,7 +26,7 @@ function install_psmodule($manifest, $dir, $global) { Remove-Item -Path $linkfrom -Force -ErrorAction SilentlyContinue } - New-Item -Path $linkfrom -ItemType Junction -Value $dir | Out-Null + New-DirectoryJunction($linkfrom, $dir) } function uninstall_psmodule($manifest, $dir, $global) {