From: Adam Sitnik Date: Mon, 10 Aug 2020 11:05:30 +0000 (+0200) Subject: Use new OS checks in src (#40520) X-Git-Tag: submit/tizen/20210909.063632~6117 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=fb18a11af37209018899ed31470bd639fb9fc9b6;p=platform%2Fupstream%2Fdotnet%2Fruntime.git Use new OS checks in src (#40520) * 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 --- diff --git a/src/libraries/Common/src/Extensions/CommandLineUtils/Utilities/DotNetMuxer.cs b/src/libraries/Common/src/Extensions/CommandLineUtils/Utilities/DotNetMuxer.cs index 1d13ec2..8892852 100644 --- a/src/libraries/Common/src/Extensions/CommandLineUtils/Utilities/DotNetMuxer.cs +++ b/src/libraries/Common/src/Extensions/CommandLineUtils/Utilities/DotNetMuxer.cs @@ -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"; } diff --git a/src/libraries/Common/src/System/Net/Http/aspnetcore/Quic/Interop/MsQuicStatusCodes.cs b/src/libraries/Common/src/System/Net/Http/aspnetcore/Quic/Interop/MsQuicStatusCodes.cs index face87e4..af7fd30 100644 --- a/src/libraries/Common/src/System/Net/Http/aspnetcore/Quic/Interop/MsQuicStatusCodes.cs +++ b/src/libraries/Common/src/System/Net/Http/aspnetcore/Quic/Interop/MsQuicStatusCodes.cs @@ -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 { diff --git a/src/libraries/Common/src/System/Net/Http/aspnetcore/Quic/Interop/MsQuicStatusHelper.cs b/src/libraries/Common/src/System/Net/Http/aspnetcore/Quic/Interop/MsQuicStatusHelper.cs index 726a548..2f64fa2 100644 --- a/src/libraries/Common/src/System/Net/Http/aspnetcore/Quic/Interop/MsQuicStatusHelper.cs +++ b/src/libraries/Common/src/System/Net/Http/aspnetcore/Quic/Interop/MsQuicStatusHelper.cs @@ -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; } diff --git a/src/libraries/Common/src/System/Net/NetworkInformation/UnixCommandLinePing.cs b/src/libraries/Common/src/System/Net/NetworkInformation/UnixCommandLinePing.cs index 24063dc..98db97f 100644 --- a/src/libraries/Common/src/System/Net/NetworkInformation/UnixCommandLinePing.cs +++ b/src/libraries/Common/src/System/Net/NetworkInformation/UnixCommandLinePing.cs @@ -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 s_isBusybox = new Lazy(() => 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) { diff --git a/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/Hosting/DirectoryCatalog.cs b/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/Hosting/DirectoryCatalog.cs index 0b9c5d2..7088f00 100644 --- a/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/Hosting/DirectoryCatalog.cs +++ b/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/Hosting/DirectoryCatalog.cs @@ -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(files, (file) => RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? file.ToUpperInvariant() : file); + + if (!IsWindows) + { + return files; + } + + return Array.ConvertAll(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))] diff --git a/src/libraries/System.Net.Http/src/System.Net.Http.csproj b/src/libraries/System.Net.Http/src/System.Net.Http.csproj index 420af9b..9e69e11 100644 --- a/src/libraries/System.Net.Http/src/System.Net.Http.csproj +++ b/src/libraries/System.Net.Http/src/System.Net.Http.csproj @@ -695,7 +695,6 @@ - diff --git a/src/libraries/System.Net.Ping/src/System.Net.Ping.csproj b/src/libraries/System.Net.Ping/src/System.Net.Ping.csproj index b817eba..fa5e3e7 100644 --- a/src/libraries/System.Net.Ping/src/System.Net.Ping.csproj +++ b/src/libraries/System.Net.Ping/src/System.Net.Ping.csproj @@ -104,6 +104,5 @@ - diff --git a/src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Unix.cs b/src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Unix.cs index 0086acc..44ac572 100644 --- a/src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Unix.cs +++ b/src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Unix.cs @@ -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); diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/CryptoConfig.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/CryptoConfig.cs index f82f782..a1aea6e 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/CryptoConfig.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/CryptoConfig.cs @@ -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); }