Skip to content

Commit

Permalink
Fix for files/folders not created when given path is drive root (Powe…
Browse files Browse the repository at this point in the history
…rShell#5228)

[Feature] Checks to see if the root of a PSDrive was given as the Path argument to a parameter that supports it. If so, returns the current context's Drive's Root.
  • Loading branch information
mcbobke committed Apr 24, 2018
1 parent 46fa226 commit b101d52
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 8 deletions.
32 changes: 25 additions & 7 deletions src/System.Management.Automation/namespaces/LocationGlobber.cs
Expand Up @@ -2013,6 +2013,9 @@ internal bool IsShellVirtualDrive(string driveName, out SessionStateScope scope)
// Check to see if the path is relative or absolute
bool isPathForCurrentDrive = false;

// Check to see if the path is to the root of a drive
bool isPathForRootOfDrive = false;

if (IsAbsolutePath(path, out driveName))
{
Dbg.Diagnostics.Assert(
Expand Down Expand Up @@ -2080,6 +2083,12 @@ internal bool IsShellVirtualDrive(string driveName, out SessionStateScope scope)
// this is the default behavior for all windows drives, and all non-filesystem
// drives on non-windows
path = path.Substring(driveName.Length + 1);

if (String.IsNullOrEmpty(path))
{
// path was to the root of a drive such as 'c:'
isPathForRootOfDrive = true;
}
}
}
}
Expand Down Expand Up @@ -2111,14 +2120,23 @@ internal bool IsShellVirtualDrive(string driveName, out SessionStateScope scope)
// have access to it.
context.Drive = workingDriveForPath;

string relativePath =
GenerateRelativePath(
workingDriveForPath,
path,
escapeCurrentLocation,
providerInstance,
context);
string relativePath = String.Empty;

if (isPathForRootOfDrive)
{
relativePath = context.Drive.Root;
}
else
{
relativePath =
GenerateRelativePath(
workingDriveForPath,
path,
escapeCurrentLocation,
providerInstance,
context);
}

return relativePath;
}
catch (PSNotSupportedException)
Expand Down
Expand Up @@ -117,6 +117,37 @@ Describe "New-Item" -Tags "CI" {
$fileInfo.Target | Should -BeNullOrEmpty
$fileInfo.LinkType | Should -BeExactly "HardLink"
}

It "Should create a file at the root of the drive while the current working directory is not the root" {
try {
Push-Location -Path "TestDrive:\"
New-Item -Name $testfolder -Path "TestDrive:\" -ItemType directory > $null
Push-Location -Path "TestDrive:\$testfolder"
New-Item -Name $testfile -Path "TestDrive:\" -ItemType file > $null
$FullyQualifiedFile | Should -Exist
}
finally {
Pop-Location
Pop-Location
}
}

It "Should create a folder at the root of the drive while the current working directory is not the root" {
$testfolder2 = "newDirectory2"
$FullyQualifiedFolder2 = Join-Path -Path $tmpDirectory -ChildPath $testfolder2

try {
Push-Location -Path "TestDrive:\"
New-Item -Name $testfolder -Path "TestDrive:\" -ItemType directory > $null
Push-Location -Path "TestDrive:\$testfolder"
New-Item -Name $testfolder2 -Path "TestDrive:\" -ItemType directory > $null
$FullyQualifiedFolder2 | Should -Exist
}
finally {
Pop-Location
Pop-Location
}
}
}

# More precisely these tests require SeCreateSymbolicLinkPrivilege.
Expand Down Expand Up @@ -186,7 +217,7 @@ Describe "New-Item with links" -Tags @('CI', 'RequireAdminOnWindows') {
}

It "New-Item -ItemType SymbolicLink should understand directory path ending with slash" {
$folderName = [System.IO.Path]::GetRandomFileName()
$folderName = [System.IO.Path]::GetRandomFileName()
$symbolicLinkPath = New-Item -ItemType SymbolicLink -Path "$tmpDirectory/$folderName/" -Value "/bar/"
$symbolicLinkPath | Should -Not -BeNullOrEmpty
}
Expand Down

0 comments on commit b101d52

Please sign in to comment.