From 77a9b4b5d22c377e1f637a574d76b073772f45cd Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Wed, 13 Jul 2016 21:02:00 -0700 Subject: [PATCH] Remove unnecessary throws in CorePsPlatform --- .../CoreCLR/CorePsPlatform.cs | 164 ++++-------------- 1 file changed, 35 insertions(+), 129 deletions(-) diff --git a/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs b/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs index eb9d3b2624..5a4dfccb69 100644 --- a/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs +++ b/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs @@ -318,22 +318,12 @@ namespace System.Management.Automation internal static bool NonWindowsIsHardLink(FileSystemInfo fileInfo) { - if (!IsWindows) - { - return LinuxPlatform.IsHardLink(fileInfo); - } - - throw new PlatformNotSupportedException(); + return LinuxPlatform.IsHardLink(fileInfo); } internal static bool NonWindowsIsSymLink(FileSystemInfo fileInfo) { - if (!IsWindows) - { - return LinuxPlatform.IsSymLink(fileInfo); - } - - throw new PlatformNotSupportedException(); + return LinuxPlatform.IsSymLink(fileInfo); } internal static string NonWindowsInternalGetTarget(SafeFileHandle handle) @@ -344,166 +334,91 @@ namespace System.Management.Automation internal static string NonWindowsInternalGetTarget(string path) { - if (!IsWindows) - { - return LinuxPlatform.FollowSymLink(path); - } - else - { - throw new PlatformNotSupportedException(); - } + return LinuxPlatform.FollowSymLink(path); } internal static string NonWindowsGetUserFromPid(int path) { - if (!IsWindows) - { - return LinuxPlatform.GetUserFromPid(path); - } - else - { - throw new PlatformNotSupportedException(); - } + return LinuxPlatform.GetUserFromPid(path); } #if CORECLR internal static string NonWindowsGetFolderPath(SpecialFolder folder) { - if (!IsWindows) - { - return LinuxPlatform.GetFolderPath(folder); - } - else - { - throw new PlatformNotSupportedException(); - } + return LinuxPlatform.GetFolderPath(folder); } #endif internal static string NonWindowsInternalGetLinkType(FileSystemInfo fileInfo) { - if (!IsWindows) + if (NonWindowsIsSymLink(fileInfo)) { - if (NonWindowsIsSymLink(fileInfo)) - { - return "SymbolicLink"; - } - if (NonWindowsIsHardLink(fileInfo)) - { - return "HardLink"; - } - return null; - } - else - { - throw new PlatformNotSupportedException(); + return "SymbolicLink"; } + if (NonWindowsIsHardLink(fileInfo)) + { + return "HardLink"; + } + + return null; } internal static bool NonWindowsCreateSymbolicLink(string path, string strTargetPath, bool isDirectory) { - if (!IsWindows) - { - // Linux doesn't care if target is a directory or not - return LinuxPlatform.CreateSymbolicLink(path, strTargetPath); - } - else - { - throw new PlatformNotSupportedException(); - } + // Linux doesn't care if target is a directory or not + return LinuxPlatform.CreateSymbolicLink(path, strTargetPath); } internal static bool NonWindowsCreateHardLink(string path, string strTargetPath) { - if (!IsWindows) - { - return LinuxPlatform.CreateHardLink(path, strTargetPath); - } - else - { - throw new PlatformNotSupportedException(); - } + return LinuxPlatform.CreateHardLink(path, strTargetPath); } internal static void NonWindowsSetDate(DateTime dateToUse) { - if (!IsWindows) - { - LinuxPlatform.SetDateInfoInternal date = new LinuxPlatform.SetDateInfoInternal(dateToUse); - LinuxPlatform.SetDate(date); - } - else - { - throw new PlatformNotSupportedException(); - } + LinuxPlatform.SetDateInfoInternal date = new LinuxPlatform.SetDateInfoInternal(dateToUse); + LinuxPlatform.SetDate(date); } internal static string NonWindowsGetDomainName() { - if (!IsWindows) + string fullyQualifiedName = LinuxPlatform.Native.GetFullyQualifiedName(); + if (string.IsNullOrEmpty(fullyQualifiedName)) { - string fullyQualifiedName = LinuxPlatform.Native.GetFullyQualifiedName(); - if (string.IsNullOrEmpty(fullyQualifiedName)) - { - int lastError = Marshal.GetLastWin32Error(); - throw new InvalidOperationException("LinuxPlatform.NonWindowsGetDomainName error: " + lastError); - } - - int index = fullyQualifiedName.IndexOf('.'); - if (index >= 0) - { - return fullyQualifiedName.Substring(index + 1); - } - - return ""; + int lastError = Marshal.GetLastWin32Error(); + throw new InvalidOperationException("LinuxPlatform.NonWindowsGetDomainName error: " + lastError); } - else + + int index = fullyQualifiedName.IndexOf('.'); + if (index >= 0) { - throw new PlatformNotSupportedException(); + return fullyQualifiedName.Substring(index + 1); } + + return ""; } internal static string NonWindowsGetUserName() { - if (!IsWindows) - { - return LinuxPlatform.UserName; - } - else - { - throw new PlatformNotSupportedException(); - } + return LinuxPlatform.UserName; } // Hostname in this context seems to be the FQDN internal static string NonWindowsGetHostName() { - if (!IsWindows) + string hostName = LinuxPlatform.Native.GetFullyQualifiedName(); + if (string.IsNullOrEmpty(hostName)) { - string hostName = LinuxPlatform.Native.GetFullyQualifiedName(); - if (string.IsNullOrEmpty(hostName)) - { - int lastError = Marshal.GetLastWin32Error(); - throw new InvalidOperationException("LinuxPlatform.NonWindowsHostName error: " + lastError); - } - return hostName; - - } - else - { - throw new PlatformNotSupportedException(); + int lastError = Marshal.GetLastWin32Error(); + throw new InvalidOperationException("LinuxPlatform.NonWindowsHostName error: " + lastError); } + return hostName; } internal static bool NonWindowsIsExecutable(string path) { - if (!IsWindows) - { - return LinuxPlatform.IsExecutable(path); - } - - throw new PlatformNotSupportedException(); + return LinuxPlatform.IsExecutable(path); } internal static uint NonWindowsGetThreadId() @@ -512,15 +427,6 @@ namespace System.Management.Automation return 0; } - /// - /// This exception is meant to be thrown if a code path is not supported due - /// to platform restrictions - /// - internal class PlatformNotSupportedException : System.Exception - { - public PlatformNotSupportedException() : base() {} - } - /// /// This models the native call CommandLineToArgvW in managed code. ///