[release/v7.6.2] Enable usage in AppContainers (#27423)

This commit is contained in:
Dongbo Wang
2026-05-13 15:03:23 -07:00
committed by GitHub
parent 712231cad4
commit 8afbd5fc9b
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);