[release/v7.7.0-preview.1] Enable usage in AppContainers (#27305)

Co-authored-by: Patrick Meinecke <SeeminglyScience@users.noreply.github.com>
Co-authored-by: Travis Plunk <travis.plunk@microsoft.com>
This commit is contained in:
Justin Chung
2026-04-20 17:35:53 +00:00
committed by GitHub
co-authored by Patrick Meinecke Travis Plunk
parent 52a7d8e958
commit 1218a29b87
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);