Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Fallback to read/write if pread/pwrite fails
  • Loading branch information
David Cantu authored and github-actions committed Oct 12, 2021
commit ce3a5d2fab9511d65dd85b46d87ca324c5ecd455
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace System.IO.Tests
{
[PlatformSpecific(TestPlatforms.AnyUnix)]
public class CharacterDevice
{
const string CharacterDevicePath = "/dev/tty";
[Theory]
[InlineData(FileOptions.None)]
[InlineData(FileOptions.Asynchronous)]
public void CharacterDevice_Write(FileOptions fileOptions)
{
using FileStream fs = new(CharacterDevicePath, new FileStreamOptions { Options = fileOptions, Access = FileAccess.Write });
fs.Write(Encoding.UTF8.GetBytes("foo"));
}

[Theory]
[InlineData(FileOptions.None)]
[InlineData(FileOptions.Asynchronous)]
public async Task CharacterDevice_WriteAsync(FileOptions fileOptions)
{
using FileStream fs = new(CharacterDevicePath, new FileStreamOptions { Options = fileOptions, Access = FileAccess.Write });
await fs.WriteAsync(Encoding.UTF8.GetBytes("foo"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
<Compile Include="FileInfo\GetSetAttributes.cs" />
<Compile Include="FileInfo\Length.cs" />
<Compile Include="FileInfo\Open.cs" />
<Compile Include="FileStream\CharacterDevice.cs" />
<Compile Include="FileStream\FlushAsync.cs" />
<Compile Include="FileStream\FileStreamConformanceTests.cs" />
<Compile Include="FileStream\SafeFileHandle.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,15 @@ internal static unsafe int ReadAtOffset(SafeFileHandle handle, Span<byte> buffer
// The Windows implementation uses ReadFile, which ignores the offset if the handle
// isn't seekable. We do the same manually with PRead vs Read, in order to enable
// the function to be used by FileStream for all the same situations.
int result = handle.CanSeek ?
Interop.Sys.PRead(handle, bufPtr, buffer.Length, fileOffset) :
Interop.Sys.Read(handle, bufPtr, buffer.Length);
int result = Interop.Sys.PRead(handle, bufPtr, buffer.Length, fileOffset);

// Fallback to read if pread fails.
if (result == -1)
{
Debug.Assert(Interop.Sys.GetLastErrorInfo().Error == Interop.Error.ENXIO || !handle.CanSeek); // We want to catch other errors and add unit tests for them.
result = Interop.Sys.Read(handle, bufPtr, buffer.Length);
}

FileStreamHelpers.CheckFileCall(result, handle.Path);
return result;
}
Expand Down Expand Up @@ -90,9 +96,15 @@ internal static unsafe void WriteAtOffset(SafeFileHandle handle, ReadOnlySpan<by
// The Windows implementation uses WriteFile, which ignores the offset if the handle
// isn't seekable. We do the same manually with PWrite vs Write, in order to enable
// the function to be used by FileStream for all the same situations.
int bytesWritten = handle.CanSeek ?
Interop.Sys.PWrite(handle, bufPtr, GetNumberOfBytesToWrite(buffer.Length), fileOffset) :
Interop.Sys.Write(handle, bufPtr, GetNumberOfBytesToWrite(buffer.Length));
int bytesToWrite = GetNumberOfBytesToWrite(buffer.Length);
int bytesWritten = Interop.Sys.PWrite(handle, bufPtr, bytesToWrite, fileOffset);

// Fallback to write if pwrite fails.
if (bytesWritten == -1)
{
Debug.Assert(Interop.Sys.GetLastErrorInfo().Error == Interop.Error.ENXIO || !handle.CanSeek); // We want to catch other errors and add unit tests for them.
bytesWritten = Interop.Sys.Write(handle, bufPtr, bytesToWrite);
}

FileStreamHelpers.CheckFileCall(bytesWritten, handle.Path);
if (bytesWritten == buffer.Length)
Expand Down