Use new OS checks in src (#40520)
authorAdam Sitnik <adam.sitnik@gmail.com>
Mon, 10 Aug 2020 11:05:30 +0000 (13:05 +0200)
committerGitHub <noreply@github.com>
Mon, 10 Aug 2020 11:05:30 +0000 (13:05 +0200)
* use new OperatingSystem APIs in System.Net.Ping and remove dependency from System.Runtime.InteropServices.RuntimeInformation

* use new OperatingSystem APIs in System.Security.Cryptography.Algorithms

* use new OperatingSystem APIs in System.ComponentModel.Composition and improve perf..

* use new OperatingSystem APIs in System.Net.Http and remove dependency from System.Runtime.InteropServices.RuntimeInformation

* Apply suggestions from code review

* there is no need to cache the results anymore

src/libraries/Common/src/Extensions/CommandLineUtils/Utilities/DotNetMuxer.cs
src/libraries/Common/src/System/Net/Http/aspnetcore/Quic/Interop/MsQuicStatusCodes.cs
src/libraries/Common/src/System/Net/Http/aspnetcore/Quic/Interop/MsQuicStatusHelper.cs
src/libraries/Common/src/System/Net/NetworkInformation/UnixCommandLinePing.cs
src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/Hosting/DirectoryCatalog.cs
src/libraries/System.Net.Http/src/System.Net.Http.csproj
src/libraries/System.Net.Ping/src/System.Net.Ping.csproj
src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Unix.cs
src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/CryptoConfig.cs

index 1d13ec2..8892852 100644 (file)
@@ -7,7 +7,6 @@
 using System;
 using System.Diagnostics;
 using System.IO;
-using System.Runtime.InteropServices;
 
 namespace Microsoft.Extensions.CommandLineUtils
 {
@@ -39,7 +38,7 @@ namespace Microsoft.Extensions.CommandLineUtils
         private static string TryFindMuxerPath()
         {
             var fileName = MuxerName;
-            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
+            if (OperatingSystem.IsWindows())
             {
                 fileName += ".exe";
             }
index face87e..af7fd30 100644 (file)
@@ -1,22 +1,16 @@
 // Licensed to the .NET Foundation under one or more agreements.
 // The .NET Foundation licenses this file to you under the MIT license.
 
-using System.Runtime.InteropServices;
-
 namespace System.Net.Quic.Implementations.MsQuic.Internal
 {
     internal static class MsQuicStatusCodes
     {
-        internal static readonly uint Success = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? Windows.Success : Linux.Success;
-        internal static readonly uint Pending = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? Windows.Pending : Linux.Pending;
-        internal static readonly uint InternalError = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? Windows.InternalError : Linux.InternalError;
+        internal static uint Success => OperatingSystem.IsWindows() ? Windows.Success : Linux.Success;
+        internal static uint Pending => OperatingSystem.IsWindows() ? Windows.Pending : Linux.Pending;
+        internal static uint InternalError => OperatingSystem.IsWindows() ? Windows.InternalError : Linux.InternalError;
 
         // TODO return better error messages here.
-        public static string GetError(uint status)
-        {
-            return RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
-                ? Windows.GetError(status) : Linux.GetError(status);
-        }
+        public static string GetError(uint status) => OperatingSystem.IsWindows() ? Windows.GetError(status) : Linux.GetError(status);
 
         private static class Windows
         {
index 726a548..2f64fa2 100644 (file)
@@ -1,20 +1,18 @@
 // Licensed to the .NET Foundation under one or more agreements.
 // The .NET Foundation licenses this file to you under the MIT license.
 
-using System.Runtime.InteropServices;
-
 namespace System.Net.Quic.Implementations.MsQuic.Internal
 {
     internal static class MsQuicStatusHelper
     {
         internal static bool SuccessfulStatusCode(uint status)
         {
-            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
+            if (OperatingSystem.IsWindows())
             {
                 return status < 0x80000000;
             }
 
-            if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
+            if (OperatingSystem.IsLinux())
             {
                 return (int)status <= 0;
             }
index 24063dc..98db97f 100644 (file)
@@ -4,7 +4,6 @@
 #nullable enable
 using System.Globalization;
 using System.IO;
-using System.Runtime.InteropServices;
 using System.Text;
 
 namespace System.Net.NetworkInformation
@@ -18,8 +17,6 @@ namespace System.Net.NetworkInformation
 
         private static readonly string? s_discoveredPing4UtilityPath = GetPingUtilityPath(ipv4: true);
         private static readonly string? s_discoveredPing6UtilityPath = GetPingUtilityPath(ipv4: false);
-        private static readonly bool s_isOSX = RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
-        private static readonly bool s_isBSD = RuntimeInformation.IsOSPlatform(OSPlatform.FreeBSD);
         private static readonly Lazy<bool> s_isBusybox = new Lazy<bool>(() => IsBusyboxPing(s_discoveredPing4UtilityPath));
 
         // We don't want to pick up an arbitrary or malicious ping
@@ -92,7 +89,7 @@ namespace System.Net.NetworkInformation
             // FreeBSD: ping requires -W flag which accepts timeout in MILLISECONDS;
             // ping6 requires -x which accepts timeout in MILLISECONDS
             // OSX: ping requires -W flag which accepts timeout in MILLISECONDS; ping6 doesn't support timeout
-            if (s_isBSD)
+            if (OperatingSystem.IsFreeBSD())
             {
                 if (ipv4)
                 {
@@ -103,7 +100,7 @@ namespace System.Net.NetworkInformation
                     sb.Append(" -x ");
                 }
             }
-            else if (s_isOSX)
+            else if (OperatingSystem.IsMacOS())
             {
                 if (ipv4)
                 {
@@ -136,7 +133,7 @@ namespace System.Net.NetworkInformation
 
             if (ttl > 0)
             {
-                if (s_isBSD | s_isOSX)
+                if (OperatingSystem.IsFreeBSD() || OperatingSystem.IsMacOS())
                 {
                     // OSX and FreeBSD use -h to set hop limit for IPv6 and -m ttl for IPv4
                     if (ipv4)
@@ -159,7 +156,7 @@ namespace System.Net.NetworkInformation
 
             if (fragmentOption != PingFragmentOptions.Default)
             {
-                if (s_isBSD | s_isOSX)
+                if (OperatingSystem.IsFreeBSD() || OperatingSystem.IsMacOS())
                 {
                     // The bit is off by default on OSX & FreeBSD
                     if (fragmentOption == PingFragmentOptions.Dont) {
index 0b9c5d2..7088f00 100644 (file)
@@ -21,6 +21,13 @@ namespace System.ComponentModel.Composition.Hosting
     [DebuggerTypeProxy(typeof(DirectoryCatalogDebuggerProxy))]
     public partial class DirectoryCatalog : ComposablePartCatalog, INotifyComposablePartCatalogChanged, ICompositionElement
     {
+        private static bool IsWindows =>
+#if NETSTANDARD || NETCOREAPP2_0
+            RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
+#else
+            OperatingSystem.IsWindows();
+#endif
+
         private readonly Lock _thisLock = new Lock();
         private readonly ICompositionElement? _definitionOrigin;
         private ComposablePartCatalogCollection _catalogCollection;
@@ -736,13 +743,19 @@ namespace System.ComponentModel.Composition.Hosting
         private string[] GetFiles()
         {
             string[] files = Directory.GetFiles(_fullPath, _searchPattern);
-            return Array.ConvertAll<string, string>(files, (file) => RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? file.ToUpperInvariant() : file);
+
+            if (!IsWindows)
+            {
+                return files;
+            }
+
+            return Array.ConvertAll<string, string>(files, (file) => file.ToUpperInvariant());
         }
 
         private static string GetFullPath(string path)
         {
             var fullPath = IOPath.GetFullPath(path);
-            return RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? fullPath.ToUpperInvariant() : fullPath;
+            return IsWindows ? fullPath.ToUpperInvariant() : fullPath;
         }
 
         [MemberNotNull(nameof(_path))]
index 420af9b..9e69e11 100644 (file)
     <Reference Include="System.Runtime" />
     <Reference Include="System.Runtime.Extensions" />
     <Reference Include="System.Runtime.InteropServices" />
-    <Reference Include="System.Runtime.InteropServices.RuntimeInformation" />
     <Reference Include="System.Security.Claims" Condition="'$(TargetsWindows)' == 'true'" />
     <Reference Include="System.Security.Cryptography.Algorithms" />
     <Reference Include="System.Security.Cryptography.Csp" />
index b817eba..fa5e3e7 100644 (file)
   <ItemGroup Condition="'$(TargetsUnix)' == 'true'">
     <Reference Include="System.Diagnostics.Process" />
     <Reference Include="System.IO.FileSystem" />
-    <Reference Include="System.Runtime.InteropServices.RuntimeInformation" />
   </ItemGroup>
 </Project>
index 0086acc..44ac572 100644 (file)
@@ -18,8 +18,8 @@ namespace System.Net.NetworkInformation
         private const int IcmpHeaderLengthInBytes = 8;
         private const int MinIpHeaderLengthInBytes = 20;
         private const int MaxIpHeaderLengthInBytes = 60;
-        private static readonly bool _sendIpHeader = RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
-        private static readonly bool _needsConnect = RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
+        private static bool SendIpHeader => OperatingSystem.IsMacOS();
+        private static bool NeedsConnect => OperatingSystem.IsLinux();
         [ThreadStatic]
         private static Random? t_idGenerator;
 
@@ -56,7 +56,7 @@ namespace System.Net.NetworkInformation
             IpHeader iph = default;
 
             bool ipv4 = address.AddressFamily == AddressFamily.InterNetwork;
-            bool sendIpHeader = ipv4 && options != null && _sendIpHeader;
+            bool sendIpHeader = ipv4 && options != null && SendIpHeader;
 
              if (sendIpHeader)
              {
@@ -91,7 +91,7 @@ namespace System.Net.NetworkInformation
             Socket socket = new Socket(addrFamily, SocketType.Raw, socketConfig.ProtocolType);
             socket.ReceiveTimeout = socketConfig.Timeout;
             socket.SendTimeout = socketConfig.Timeout;
-            if (addrFamily == AddressFamily.InterNetworkV6 && RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
+            if (addrFamily == AddressFamily.InterNetworkV6 && OperatingSystem.IsMacOS())
             {
                 socket.DualMode = false;
             }
@@ -103,7 +103,7 @@ namespace System.Net.NetworkInformation
 
             if (socketConfig.Options != null && addrFamily == AddressFamily.InterNetwork)
             {
-                if (_sendIpHeader)
+                if (SendIpHeader)
                 {
                     // some platforms like OSX don't support DontFragment so we construct IP header instead.
                     socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, 1);
@@ -117,7 +117,7 @@ namespace System.Net.NetworkInformation
 #pragma warning disable 618
             // Disable warning about obsolete property. We could use GetAddressBytes but that allocates.
             // IPv4 multicast address starts with 1110 bits so mask rest and test if we get correct value e.g. 0xe0.
-            if (_needsConnect && !ep.Address.IsIPv6Multicast && !(addrFamily == AddressFamily.InterNetwork && (ep.Address.Address & 0xf0) == 0xe0))
+            if (NeedsConnect && !ep.Address.IsIPv6Multicast && !(addrFamily == AddressFamily.InterNetwork && (ep.Address.Address & 0xf0) == 0xe0))
             {
                 // If it is not multicast, use Connect to scope responses only to the target address.
                 socket.Connect(socketConfig.EndPoint);
index f82f782..a1aea6e 100644 (file)
@@ -188,7 +188,7 @@ namespace System.Security.Cryptography
                 ht.Add("System.Security.Cryptography.DSA", DSACryptoServiceProviderType);
 
                 // Windows will register the public ECDsaCng type.  Non-Windows gets a special handler.
-                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
+                if (OperatingSystem.IsWindows())
                 {
                     ht.Add(ECDsaIdentifier, ECDsaCngType);
                 }