mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
[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:
co-authored by
Patrick Meinecke
Travis Plunk
parent
52a7d8e958
commit
1218a29b87
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user