Enable usage in AppContainers (#27266)

Co-authored-by: Travis Plunk <travis.plunk@microsoft.com>
This commit is contained in:
Patrick Meinecke
2026-04-20 11:49:52 -05:00
committed by GitHub
co-authored by Travis Plunk
parent 6cc1e7071d
commit 2475feb876
2 changed files with 28 additions and 3 deletions
@@ -967,7 +967,9 @@ namespace System.Management.Automation
#if UNIX
return Platform.SelectProductNameForDirectory(Platform.XDG_Type.USER_MODULES);
#else
string myDocumentsPath = InternalTestHooks.SetMyDocumentsSpecialFolderToBlank ? string.Empty : Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string myDocumentsPath = InternalTestHooks.SetMyDocumentsSpecialFolderToBlank
? string.Empty
: Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments, Environment.SpecialFolderOption.DoNotVerify);
return string.IsNullOrEmpty(myDocumentsPath) ? null : Path.Combine(myDocumentsPath, Utils.ModuleDirectory);
#endif
}
@@ -569,7 +569,7 @@ namespace Microsoft.PowerShell.Commands
if (driveIsFixed)
{
// Since the drive is fixed, ensure the root is valid.
validDrive = Directory.Exists(drive.Root);
validDrive = SafeDoesPathExist(drive.Root);
}
if (validDrive)
@@ -908,7 +908,7 @@ namespace Microsoft.PowerShell.Commands
if (newDrive.DriveType == DriveType.Fixed)
{
if (!newDrive.RootDirectory.Exists)
if (!SafeDoesPathExist(newDrive.RootDirectory.FullName))
{
continue;
}
@@ -1227,6 +1227,29 @@ namespace Microsoft.PowerShell.Commands
}
}
private static bool SafeDoesPathExist(string rootDirectory)
{
if (Directory.Exists(rootDirectory))
{
return true;
}
try
{
return (File.GetAttributes(rootDirectory) & FileAttributes.Directory) is not 0;
}
// In some scenarios (like AppContainers) direct access to the root directory may
// be prevented, but more specific paths may be accessible.
catch (UnauthorizedAccessException)
{
return true;
}
catch
{
return false;
}
}
private FileSystemInfo GetFileSystemItem(string path, ref bool isContainer, bool showHidden)
{
path = NormalizePath(path);