Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public static partial class Environment

private static string GetFolderPathCore(SpecialFolder folder, SpecialFolderOption _ /*option*/)
{
// No need to validate if 'folder' is defined; GetSpecialFolder handles this check.

if (s_specialFolders == null)
{
Interlocked.CompareExchange(ref s_specialFolders, new Dictionary<SpecialFolder, string>(), null);
Expand Down Expand Up @@ -92,7 +94,9 @@ private static string GetFolderPathCore(SpecialFolder folder, SpecialFolderOptio
return "/usr/share";

default:
return string.Empty;
if (!Enum.IsDefined(folder))
throw new ArgumentOutOfRangeException(nameof(folder), folder, SR.Format(SR.Arg_EnumIllegalVal, folder));
return null;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public static partial class Environment
{
private static string GetFolderPathCore(SpecialFolder folder, SpecialFolderOption option)
{
// Get the path for the SpecialFolder
string path = GetFolderPathCoreWithoutValidation(folder) ?? string.Empty;
Debug.Assert(path != null);
// No need to validate if 'folder' is defined; GetSpecialFolder handles this check.

string path = GetSpecialFolder(folder) ?? string.Empty;

// If we didn't get one, or if we got one but we're not supposed to verify it,
// or if we're supposed to verify it and it passes verification, return the path.
Expand All @@ -46,7 +46,7 @@ private static string GetFolderPathCore(SpecialFolder folder, SpecialFolderOptio
return path;
}

private static string? GetFolderPathCoreWithoutValidation(SpecialFolder folder)
private static string? GetSpecialFolder(SpecialFolder folder)
{
// First handle any paths that involve only static paths, avoiding the overheads of getting user-local paths.
// https://www.freedesktop.org/software/systemd/man/file-hierarchy.html
Expand Down Expand Up @@ -143,8 +143,11 @@ private static string GetFolderPathCore(SpecialFolder folder, SpecialFolderOptio
#endif
}

if (!Enum.IsDefined(folder))
throw new ArgumentOutOfRangeException(nameof(folder), folder, SR.Format(SR.Arg_EnumIllegalVal, folder));

// No known path for the SpecialFolder
return string.Empty;
return null;
}

private static string GetXdgConfig(string home)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,16 +216,14 @@ private static string GetFolderPathCore(SpecialFolder folder, SpecialFolderOptio
//
// The only SpecialFolderOption defines we have are equivalent to KnownFolderFlags.

string folderGuid;
ReadOnlySpan<byte> folderGuid;
string? fallbackEnv = null;
switch (folder)
{
// Special-cased values to not use SHGetFolderPath when we have a more direct option available.
case SpecialFolder.System:
// This assumes the system directory always exists and thus we don't need to do anything special for any SpecialFolderOption.
return SystemDirectory;
default:
return string.Empty;

// Map the SpecialFolder to the appropriate Guid
case SpecialFolder.ApplicationData:
Expand Down Expand Up @@ -374,6 +372,9 @@ private static string GetFolderPathCore(SpecialFolder folder, SpecialFolderOptio
folderGuid = Interop.Shell32.KnownFolders.Windows;
fallbackEnv = "windir";
break;
default:
Debug.Assert(!Enum.IsDefined(folder), $"Unexpected SpecialFolder value: {folder}. Please ensure all SpecialFolder enum values are handled in the switch statement.");
throw new ArgumentOutOfRangeException(nameof(folder), folder, SR.Format(SR.Arg_EnumIllegalVal, folder));
}

Guid folderId = new Guid(folderGuid);
Expand Down
15 changes: 10 additions & 5 deletions src/libraries/System.Private.CoreLib/src/System/Environment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,20 @@ public static string ExpandEnvironmentVariables(string name)
return ExpandEnvironmentVariablesCore(name);
}

public static string GetFolderPath(SpecialFolder folder) => GetFolderPath(folder, SpecialFolderOption.None);
public static string GetFolderPath(SpecialFolder folder) => GetFolderPathCore(folder, SpecialFolderOption.None);

public static string GetFolderPath(SpecialFolder folder, SpecialFolderOption option)
{
if (!Enum.IsDefined(folder))
throw new ArgumentOutOfRangeException(nameof(folder), folder, SR.Format(SR.Arg_EnumIllegalVal, folder));
// No need to validate if 'folder' is defined; GetFolderPathCore handles this check.

if (option != SpecialFolderOption.None && !Enum.IsDefined(option))
throw new ArgumentOutOfRangeException(nameof(option), option, SR.Format(SR.Arg_EnumIllegalVal, option));
if (option is not SpecialFolderOption.None and not SpecialFolderOption.Create and not SpecialFolderOption.DoNotVerify)
{
// Use a throw helper so that if 'option' is a constant,
// the JIT can inline this method and remove the validation check entirely.
Throw(option);
static void Throw(SpecialFolderOption option) =>
throw new ArgumentOutOfRangeException(nameof(option), option, SR.Format(SR.Arg_EnumIllegalVal, option));
}

return GetFolderPathCore(folder, option);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public static partial class Environment

private static string GetFolderPathCore(SpecialFolder folder, SpecialFolderOption _ /*option*/)
{
// No need to validate if 'folder' is defined; GetSpecialFolder handles this check.

if (s_specialFolders == null)
{
Interlocked.CompareExchange(ref s_specialFolders, new Dictionary<SpecialFolder, string>(), null);
Expand Down Expand Up @@ -91,7 +93,9 @@ private static string GetFolderPathCore(SpecialFolder folder, SpecialFolderOptio
return "/usr/share";

default:
return string.Empty;
if (!Enum.IsDefined(folder))
throw new ArgumentOutOfRangeException(nameof(folder), folder, SR.Format(SR.Arg_EnumIllegalVal, folder));
return null;
}

static string CombineSearchPath(NSSearchPathDirectory searchPath, string subdirectory)
Expand Down
Loading