-
Notifications
You must be signed in to change notification settings - Fork 5.1k
File preallocationSize: align Windows and Unix behavior. #59338
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
Changes from all commits
3a591dd
3245d91
ff29b43
c2aef89
d3db729
a5fde03
31de574
ec6e98f
a62b89c
fc75560
a58c0e2
77d1978
7decfba
7482a40
3ea664a
9f4e556
26282f3
ec9412b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// 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 | ||
{ | ||
// Value taken from https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-setfileinformationbyhandle#remarks: | ||
internal const int FileAllocationInfo = 5; | ||
|
||
internal struct FILE_ALLOCATION_INFO | ||
{ | ||
internal long AllocationSize; | ||
} | ||
} | ||
} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,28 +7,24 @@ | |
namespace System.IO.Tests | ||
{ | ||
// to avoid a lot of code duplication, we reuse FileStream tests | ||
public class File_OpenHandle : FileStream_ctor_options_as | ||
public class File_OpenHandle : FileStream_ctor_options | ||
{ | ||
protected override string GetExpectedParamName(string paramName) => paramName; | ||
|
||
protected override FileStream CreateFileStream(string path, FileMode mode) | ||
{ | ||
FileAccess access = mode == FileMode.Append ? FileAccess.Write : FileAccess.ReadWrite; | ||
return new FileStream(File.OpenHandle(path, mode, access, preallocationSize: PreallocationSize), access); | ||
return new FileStream(File.OpenHandle(path, mode, access), access); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What was the verdict on testing coverage with all of these tests being updated to not pass in a preallocation size? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Coverage is good now. I refactored tests to follow the pattern that was already used by adding an overload of |
||
} | ||
|
||
protected override FileStream CreateFileStream(string path, FileMode mode, FileAccess access) | ||
=> new FileStream(File.OpenHandle(path, mode, access, preallocationSize: PreallocationSize), access); | ||
=> new FileStream(File.OpenHandle(path, mode, access), access); | ||
|
||
protected override FileStream CreateFileStream(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, FileOptions options) | ||
=> new FileStream(File.OpenHandle(path, mode, access, share, options, PreallocationSize), access, bufferSize, (options & FileOptions.Asynchronous) != 0); | ||
=> new FileStream(File.OpenHandle(path, mode, access, share, options), access, bufferSize, (options & FileOptions.Asynchronous) != 0); | ||
|
||
[Fact] | ||
public override void NegativePreallocationSizeThrows() | ||
{ | ||
ArgumentOutOfRangeException ex = Assert.Throws<ArgumentOutOfRangeException>( | ||
() => File.OpenHandle("validPath", FileMode.CreateNew, FileAccess.Write, FileShare.None, FileOptions.None, preallocationSize: -1)); | ||
} | ||
protected override FileStream CreateFileStream(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, FileOptions options, long preallocationSize) | ||
=> new FileStream(File.OpenHandle(path, mode, access, share, options, preallocationSize), access, bufferSize, (options & FileOptions.Asynchronous) != 0); | ||
|
||
[ActiveIssue("https://github.com/dotnet/runtime/issues/53432")] | ||
[Theory, MemberData(nameof(StreamSpecifiers))] | ||
|
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. | ||
|
||
namespace System.IO.Tests | ||
{ | ||
public partial class FileStream_ctor_options | ||
{ | ||
private static long GetAllocatedSize(FileStream fileStream) | ||
{ | ||
return 0; | ||
} | ||
|
||
private static bool SupportsPreallocation => false; | ||
|
||
private static bool IsGetAllocatedSizeImplemented => false; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Diagnostics; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace System.IO.Tests | ||
{ | ||
public partial class FileStream_ctor_options | ||
{ | ||
private static long GetAllocatedSize(FileStream fileStream) | ||
{ | ||
bool isOSX = RuntimeInformation.IsOSPlatform(OSPlatform.OSX); | ||
// Call 'stat' to get the number of blocks, and size of blocks. | ||
using var px = Process.Start(new ProcessStartInfo | ||
{ | ||
FileName = "stat", | ||
ArgumentList = { isOSX ? "-f" : "-c", | ||
isOSX ? "%b %k" : "%b %B", | ||
fileStream.Name }, | ||
RedirectStandardOutput = true | ||
}); | ||
string stdout = px.StandardOutput.ReadToEnd(); | ||
|
||
string[] parts = stdout.Split(' '); | ||
return long.Parse(parts[0]) * long.Parse(parts[1]); | ||
tmds marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
private static bool SupportsPreallocation => | ||
RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || | ||
RuntimeInformation.IsOSPlatform(OSPlatform.OSX); | ||
tmds marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Mobile platforms don't support Process.Start. | ||
private static bool IsGetAllocatedSizeImplemented => !PlatformDetection.IsMobile; | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.