Skip to content

Commit

Permalink
InitialSessionState: Refactor SetSessionStateDrive in preparation f…
Browse files Browse the repository at this point in the history
…or adding working directory configuration support (see next commit)

Behavior should be unchanged, except that the catch-all `try` block was shortened to the relevant part of the function – exceptions thrown by the rest of the function are unrelated to drive and location configuration and indicate serious issues which should probably be propagated.
  • Loading branch information
MatejKafka committed Apr 17, 2024
1 parent c19b651 commit 3e2976f
Showing 1 changed file with 59 additions and 59 deletions.
118 changes: 59 additions & 59 deletions src/System.Management.Automation/engine/InitialSessionState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3381,77 +3381,77 @@ internal void ResetRunspaceState(ExecutionContext context)
}
}

internal static void SetSessionStateDrive(ExecutionContext context, bool setLocation)
private static bool TryInitSessionStateCurrentDrive(ExecutionContext context)
{
// Set the starting location to the current process working directory
// Ignore any errors as the file system provider may not be loaded or
// a drive with the same name as the real file system drive may not have
// been mounted.
try
{
bool proceedWithSetLocation = true;
// Set the current drive to the first FileSystem drive if it exists.
ProviderInfo fsProvider = context.EngineSessionState.GetSingleProvider(context.ProviderNames.FileSystem);

if (context.EngineSessionState.ProviderCount > 0)
Collection<PSDriveInfo> fsDrives = fsProvider.Drives;
if (fsDrives != null && fsDrives.Count > 0)
{
// NTRAID#Windows Out Of Band Releases-908481-2005/07/01-JeffJon
// Make sure we have a CurrentDrive set so that we can deal with
// UNC paths
context.EngineSessionState.CurrentDrive = fsDrives[0];
return true;
}
}
catch (ProviderNotFoundException)
{
}

if (context.EngineSessionState.CurrentDrive == null)
{
bool fsDriveSet = false;
try
{
// Set the current drive to the first FileSystem drive if it exists.
ProviderInfo fsProvider = context.EngineSessionState.GetSingleProvider(context.ProviderNames.FileSystem);
Collection<PSDriveInfo> allDrives = context.EngineSessionState.Drives(null);

Collection<PSDriveInfo> fsDrives = fsProvider.Drives;
if (fsDrives != null && fsDrives.Count > 0)
{
context.EngineSessionState.CurrentDrive = fsDrives[0];
fsDriveSet = true;
}
}
catch (ProviderNotFoundException)
{
}
if (allDrives != null && allDrives.Count > 0)
{
context.EngineSessionState.CurrentDrive = allDrives[0];
return true;
}
else
{
ItemNotFoundException itemNotFound = new(Environment.CurrentDirectory, "PathNotFound", SessionStateStrings.PathNotFound);
context.ReportEngineStartupError(itemNotFound);
return false;
}
}

if (!fsDriveSet)
{
Collection<PSDriveInfo> allDrives = context.EngineSessionState.Drives(null);
internal static void SetSessionStateDrive(ExecutionContext context, bool setLocation)
{
if (context.EngineSessionState.ProviderCount == 0)
{
return;
}

if (allDrives != null && allDrives.Count > 0)
{
context.EngineSessionState.CurrentDrive = allDrives[0];
}
else
{
ItemNotFoundException itemNotFound =
new ItemNotFoundException(Directory.GetCurrentDirectory(), "PathNotFound", SessionStateStrings.PathNotFound);
// NTRAID#Windows Out Of Band Releases-908481-2005/07/01-JeffJon
// Make sure we have a CurrentDrive set so that we can deal with
// UNC paths
if (context.EngineSessionState.CurrentDrive == null && !TryInitSessionStateCurrentDrive(context))
{
return;
}

context.ReportEngineStartupError(itemNotFound);
proceedWithSetLocation = false;
}
}
}
if (!setLocation)
{
return;
}

if (proceedWithSetLocation && setLocation)
{
CmdletProviderContext providerContext = new CmdletProviderContext(context);
var providerContext = new CmdletProviderContext(context) { SuppressWildcardExpansion = true };

try
{
providerContext.SuppressWildcardExpansion = true;
context.EngineSessionState.SetLocation(Directory.GetCurrentDirectory(), providerContext);
}
catch (ItemNotFoundException)
{
// If we can't access the Environment.CurrentDirectory, we may be in an AppContainer. Set the
// default drive to $pshome
string defaultPath = System.IO.Path.GetDirectoryName(Environment.ProcessPath);
context.EngineSessionState.SetLocation(defaultPath, providerContext);
}
}
// Set the starting location to the current process working directory
// Ignore any errors as the file system provider may not be loaded or
// a drive with the same name as the real file system drive may not have
// been mounted.
try
{
try
{
context.EngineSessionState.SetLocation(Environment.CurrentDirectory, providerContext);
}
catch (ItemNotFoundException)
{
// If we can't access the Environment.CurrentDirectory, we may be in an AppContainer. Set the
// default drive to $pshome
string defaultPath = Path.GetDirectoryName(Environment.ProcessPath);
context.EngineSessionState.SetLocation(defaultPath, providerContext);
}
}
catch (Exception)
Expand Down

0 comments on commit 3e2976f

Please sign in to comment.