Fix 'Set-Location DriveName:' to restore current working directory in the drive (#6774)

This commit is contained in:
Matthew Bobke
2018-05-16 17:13:22 -07:00
committed by Dongbo Wang
parent aa0af5ed56
commit 4e3db1bb93
4 changed files with 89 additions and 9 deletions
@@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Management.Automation.Provider;
using Dbg = System.Management.Automation;
@@ -262,10 +263,18 @@ namespace System.Management.Automation
// Since the path is an absolute path
// we need to change the current working
// drive
PSDriveInfo newWorkingDrive = GetDrive(driveName);
CurrentDrive = newWorkingDrive;
// If the path is simply a colon-terminated drive,
// not a slash-terminated path to the root of a drive,
// set the path to the current working directory of that drive.
string colonTerminatedVolume = CurrentDrive.Name + ':';
if (CurrentDrive.VolumeSeparatedByColon && (path.Length == colonTerminatedVolume.Length))
{
path = Path.Combine((colonTerminatedVolume + Path.DirectorySeparatorChar), CurrentDrive.CurrentLocation);
}
// Now that the current working drive is set,
// process the rest of the path as a relative path.
}
@@ -2013,6 +2013,9 @@ namespace System.Management.Automation
// 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(
@@ -2080,6 +2083,12 @@ namespace System.Management.Automation
// 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;
}
}
}
}
@@ -2111,14 +2120,23 @@ namespace System.Management.Automation
// 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)