Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make RandomAccess.Read*|Write* methods work with non-seekable files #96711

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// 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;

internal static partial class Interop
{
internal static partial class Sys
{
[LibraryImport(Libraries.SystemNative, EntryPoint = "SystemNative_ReadV", SetLastError = true)]
internal static unsafe partial long ReadV(SafeHandle fd, IOVector* vectors, int vectorCount);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// 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;

internal static partial class Interop
{
internal static partial class Sys
{
[LibraryImport(Libraries.SystemNative, EntryPoint = "SystemNative_WriteV", SetLastError = true)]
internal static unsafe partial long WriteV(SafeHandle fd, IOVector* vectors, int vectorCount);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2378,6 +2378,9 @@
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.PRead.cs">
<Link>Common\Interop\Unix\System.Native\Interop.PRead.cs</Link>
</Compile>
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.ReadV.cs">
<Link>Common\Interop\Unix\System.Native\Interop.ReadV.cs</Link>
</Compile>
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.PReadV.cs">
<Link>Common\Interop\Unix\System.Native\Interop.PReadV.cs</Link>
</Compile>
Expand All @@ -2390,6 +2393,9 @@
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.Read.cs">
<Link>Common\Interop\Unix\System.Native\Interop.Read.cs</Link>
</Compile>
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.WriteV.cs">
<Link>Common\Interop\Unix\System.Native\Interop.WriteV.cs</Link>
</Compile>
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.ReadDir.cs">
<Link>Common\Interop\Unix\System.Native\Interop.ReadDir.cs</Link>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,9 @@ internal static unsafe int ReadAtOffset(SafeFileHandle handle, Span<byte> buffer
{
// Try pread for seekable files.
result = Interop.Sys.PRead(handle, bufPtr, buffer.Length, fileOffset);
if (result == -1)
if (result == -1 && NeedsNonOffsetFallback(handle))
{
// We need to fallback to the non-offset version for certain file types
// e.g: character devices (such as /dev/tty), pipes, and sockets.
Interop.ErrorInfo errorInfo = Interop.Sys.GetLastErrorInfo();

if (errorInfo.Error == Interop.Error.ENXIO ||
errorInfo.Error == Interop.Error.ESPIPE)
{
handle.SupportsRandomAccess = false;
result = Interop.Sys.Read(handle, bufPtr, buffer.Length);
}
result = Interop.Sys.Read(handle, bufPtr, buffer.Length);
}
}
else
Expand Down Expand Up @@ -77,7 +68,18 @@ internal static unsafe long ReadScatterAtOffset(SafeFileHandle handle, IReadOnly

fixed (Interop.Sys.IOVector* pinnedVectors = &MemoryMarshal.GetReference(vectors))
{
result = Interop.Sys.PReadV(handle, pinnedVectors, buffers.Count, fileOffset);
if (handle.SupportsRandomAccess)
{
result = Interop.Sys.PReadV(handle, pinnedVectors, buffers.Count, fileOffset);
if (result == -1 && NeedsNonOffsetFallback(handle))
{
result = Interop.Sys.ReadV(handle, pinnedVectors, buffers.Count);
}
}
else
{
result = Interop.Sys.ReadV(handle, pinnedVectors, buffers.Count);
}
}
}
finally
Expand Down Expand Up @@ -111,18 +113,9 @@ internal static unsafe void WriteAtOffset(SafeFileHandle handle, ReadOnlySpan<by
if (handle.SupportsRandomAccess)
{
bytesWritten = Interop.Sys.PWrite(handle, bufPtr, bytesToWrite, fileOffset);
if (bytesWritten == -1)
if (bytesWritten == -1 && NeedsNonOffsetFallback(handle))
{
// We need to fallback to the non-offset version for certain file types
// e.g: character devices (such as /dev/tty), pipes, and sockets.
Interop.ErrorInfo errorInfo = Interop.Sys.GetLastErrorInfo();

if (errorInfo.Error == Interop.Error.ENXIO ||
errorInfo.Error == Interop.Error.ESPIPE)
{
handle.SupportsRandomAccess = false;
bytesWritten = Interop.Sys.Write(handle, bufPtr, bytesToWrite);
}
bytesWritten = Interop.Sys.Write(handle, bufPtr, bytesToWrite);
}
}
else
Expand Down Expand Up @@ -198,7 +191,18 @@ internal static unsafe void WriteGatherAtOffset(SafeFileHandle handle, IReadOnly
long bytesWritten;
fixed (Interop.Sys.IOVector* pinnedVectors = &MemoryMarshal.GetReference(vectors))
{
bytesWritten = Interop.Sys.PWriteV(handle, pinnedVectors, buffersCount, fileOffset);
if (handle.SupportsRandomAccess)
{
bytesWritten = Interop.Sys.PWriteV(handle, pinnedVectors, buffersCount, fileOffset);
if (bytesWritten == -1 && NeedsNonOffsetFallback(handle))
{
bytesWritten = Interop.Sys.WriteV(handle, pinnedVectors, buffersCount);
}
}
else
{
bytesWritten = Interop.Sys.WriteV(handle, pinnedVectors, buffersCount);
}
}

FileStreamHelpers.CheckFileCall(bytesWritten, handle.Path);
Expand Down Expand Up @@ -243,5 +247,21 @@ internal static ValueTask WriteAtOffsetAsync(SafeFileHandle handle, ReadOnlyMemo

private static ValueTask WriteGatherAtOffsetAsync(SafeFileHandle handle, IReadOnlyList<ReadOnlyMemory<byte>> buffers, long fileOffset, CancellationToken cancellationToken)
=> handle.GetThreadPoolValueTaskSource().QueueWriteGather(buffers, fileOffset, cancellationToken);

private static bool NeedsNonOffsetFallback(SafeFileHandle handle)
{
// We need to fallback to the non-offset version for certain file types
// e.g: character devices (such as /dev/tty), pipes, and sockets.
Interop.ErrorInfo errorInfo = Interop.Sys.GetLastErrorInfo();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be easy for a future refactor to introduce something that resets last error before this is called? Maybe last error should be passed in so it's visible.


if (errorInfo.Error == Interop.Error.ENXIO ||
errorInfo.Error == Interop.Error.ESPIPE)
{
handle.SupportsRandomAccess = false;
return true;
}

return false;
}
}
}
Loading