Skip to content

Commit

Permalink
Better windows only guard which can be removed by linker (#48594)
Browse files Browse the repository at this point in the history
Also, remove superfluous test poking into an internal state to mimic
non-Windows test environment
  • Loading branch information
marek-safar authored Mar 17, 2021
1 parent e30397a commit aeaa103
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ namespace System.Reflection.Internal
{
internal static class FileStreamReadLightUp
{
// internal for testing
internal static bool readFileNotAvailable = Path.DirectorySeparatorChar != '\\'; // Available on Windows only
internal static bool safeFileHandleNotAvailable;
private static bool IsReadFileAvailable =>
#if NETCOREAPP
OperatingSystem.IsWindows();
#else
Path.DirectorySeparatorChar == '\\';
#endif

internal static bool IsFileStream(Stream stream) => stream is FileStream;

internal static SafeHandle? GetSafeFileHandle(Stream stream)
private static SafeHandle? GetSafeFileHandle(Stream stream)
{
SafeHandle handle;
try
Expand All @@ -41,7 +44,7 @@ internal static class FileStreamReadLightUp

internal static unsafe bool TryReadFile(Stream stream, byte* buffer, long start, int size)
{
if (readFileNotAvailable)
if (!IsReadFileAvailable)
{
return false;
}
Expand All @@ -52,18 +55,7 @@ internal static unsafe bool TryReadFile(Stream stream, byte* buffer, long start,
return false;
}

int result;
int bytesRead;

try
{
result = Interop.Kernel32.ReadFile(handle, buffer, size, out bytesRead, IntPtr.Zero);
}
catch
{
readFileNotAvailable = true;
return false;
}
int result = Interop.Kernel32.ReadFile(handle, buffer, size, out int bytesRead, IntPtr.Zero);

if (result == 0 || bytesRead != size)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,21 +118,6 @@ public void Stream()
}
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/34493", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)]
public void FileStreamUnix()
{
try
{
FileStreamReadLightUp.readFileNotAvailable = true;
FileStream();
}
finally
{
FileStreamReadLightUp.readFileNotAvailable = false;
}
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/34493", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)]
public void FileStream()
Expand Down

0 comments on commit aeaa103

Please sign in to comment.