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

Use SetFileInformationByHandle on FileStream.SetLength #44170

Merged
merged 4 commits into from
Nov 4, 2020
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

internal static partial class Interop
{
internal static partial class Kernel32
{
// From FILE_INFO_BY_HANDLE_CLASS
// Use for SetFileInformationByHandle
internal const int FileEndOfFileInfo = 6;

internal struct FILE_END_OF_FILE_INFO
{
internal long EndOfFile;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1307,6 +1307,9 @@
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.ExpandEnvironmentStrings.cs">
<Link>Common\Interop\Windows\Kernel32\Interop.ExpandEnvironmentStrings.cs</Link>
</Compile>
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.FILE_END_OF_FILE_INFO.cs">
<Link>Common\Interop\Windows\Kernel32\Interop.FILE_END_OF_FILE_INFO.cs</Link>
</Compile>
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.FILE_STANDARD_INFO.cs">
<Link>Common\Interop\Windows\Kernel32\Interop.FILE_STANDARD_INFO.cs</Link>
</Compile>
Expand Down Expand Up @@ -1475,8 +1478,8 @@
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.SetCurrentDirectory.cs">
<Link>Common\Interop\Windows\Kernel32\Interop.SetCurrentDirectory.cs</Link>
</Compile>
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.SetEndOfFile.cs">
<Link>Common\Interop\Windows\Kernel32\Interop.SetEndOfFile.cs</Link>
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.SetFileInformationByHandle.cs">
<Link>Common\Interop\Windows\Kernel32\Interop.SetFileInformationByHandle.cs</Link>
</Compile>
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.SetFilePointerEx.cs">
<Link>Common\Interop\Windows\Kernel32\Interop.SetFilePointerEx.cs</Link>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,28 +379,31 @@ private void SetLengthInternal(long value)

// We absolutely need this method broken out so that WriteInternalCoreAsync can call
// a method without having to go through buffering code that might call FlushWrite.
private void SetLengthCore(long value)
private unsafe void SetLengthCore(long value)
{
Debug.Assert(value >= 0, "value >= 0");
long origPos = _filePosition;

VerifyOSHandlePosition();
if (_filePosition != value)
SeekCore(_fileHandle, value, SeekOrigin.Begin);
if (!Interop.Kernel32.SetEndOfFile(_fileHandle))

var eofInfo = new Interop.Kernel32.FILE_END_OF_FILE_INFO
{
EndOfFile = value
};

if (!Interop.Kernel32.SetFileInformationByHandle(
_fileHandle,
Interop.Kernel32.FileEndOfFileInfo,
&eofInfo,
(uint)sizeof(Interop.Kernel32.FILE_END_OF_FILE_INFO)))
{
int errorCode = Marshal.GetLastWin32Error();
if (errorCode == Interop.Errors.ERROR_INVALID_PARAMETER)
throw new ArgumentOutOfRangeException(nameof(value), SR.ArgumentOutOfRange_FileLengthTooBig);
throw Win32Marshal.GetExceptionForWin32Error(errorCode, _path);
}
// Return file pointer to where it was before setting length
if (origPos != value)

if (_filePosition > value)
{
if (origPos < value)
SeekCore(_fileHandle, origPos, SeekOrigin.Begin);
else
SeekCore(_fileHandle, 0, SeekOrigin.End);
SeekCore(_fileHandle, 0, SeekOrigin.End);
}
}

Expand Down