/
FileStreamHelpers.Windows.cs
390 lines (343 loc) · 19.6 KB
/
FileStreamHelpers.Windows.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Buffers;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Win32.SafeHandles;
namespace System.IO.Strategies
{
// this type defines a set of stateless FileStream/FileStreamStrategy helper methods
internal static partial class FileStreamHelpers
{
// Async completion/return codes used by
// SafeFileHandle.OverlappedValueTaskSource
internal static class TaskSourceCodes
{
internal const long NoResult = 0;
internal const long ResultSuccess = (long)1 << 32;
internal const long ResultError = (long)2 << 32;
internal const long RegisteringCancellation = (long)4 << 32;
internal const long CompletedCallback = (long)8 << 32;
internal const ulong ResultMask = ((ulong)uint.MaxValue) << 32;
}
private static OSFileStreamStrategy ChooseStrategyCore(SafeFileHandle handle, FileAccess access, bool isAsync) =>
isAsync ?
new AsyncWindowsFileStreamStrategy(handle, access) :
new SyncWindowsFileStreamStrategy(handle, access);
private static FileStreamStrategy ChooseStrategyCore(string path, FileMode mode, FileAccess access, FileShare share, FileOptions options, long preallocationSize, UnixFileMode? unixCreateMode) =>
(options & FileOptions.Asynchronous) != 0 ?
new AsyncWindowsFileStreamStrategy(path, mode, access, share, options, preallocationSize, unixCreateMode) :
new SyncWindowsFileStreamStrategy(path, mode, access, share, options, preallocationSize, unixCreateMode);
internal static void FlushToDisk(SafeFileHandle handle)
{
if (!Interop.Kernel32.FlushFileBuffers(handle))
{
int errorCode = Marshal.GetLastPInvokeError();
// NOTE: unlike fsync() on Unix, the FlushFileBuffers() function on Windows doesn't
// support flushing handles opened for read-only access and will return an error. We
// ignore this error to harmonize the two platforms: i.e. users can flush handles
// opened for read-only access on BOTH platforms and no exception will be thrown.
if (errorCode != Interop.Errors.ERROR_ACCESS_DENIED)
{
throw Win32Marshal.GetExceptionForLastWin32Error(handle.Path);
}
}
}
internal static long Seek(SafeFileHandle handle, long offset, SeekOrigin origin, bool closeInvalidHandle = false)
{
Debug.Assert(origin >= SeekOrigin.Begin && origin <= SeekOrigin.End, "origin >= SeekOrigin.Begin && origin <= SeekOrigin.End");
if (!Interop.Kernel32.SetFilePointerEx(handle, offset, out long ret, (uint)origin))
{
if (closeInvalidHandle)
{
throw Win32Marshal.GetExceptionForWin32Error(GetLastWin32ErrorAndDisposeHandleIfInvalid(handle), handle.Path);
}
else
{
throw Win32Marshal.GetExceptionForLastWin32Error(handle.Path);
}
}
return ret;
}
internal static void ThrowInvalidArgument(SafeFileHandle handle) =>
throw Win32Marshal.GetExceptionForWin32Error(Interop.Errors.ERROR_INVALID_PARAMETER, handle.Path);
internal static int GetLastWin32ErrorAndDisposeHandleIfInvalid(SafeFileHandle handle)
{
int errorCode = Marshal.GetLastPInvokeError();
// If ERROR_INVALID_HANDLE is returned, it doesn't suffice to set
// the handle as invalid; the handle must also be closed.
//
// Marking the handle as invalid but not closing the handle
// resulted in exceptions during finalization and locked column
// values (due to invalid but unclosed handle) in SQL Win32FileStream
// scenarios.
//
// A more mainstream scenario involves accessing a file on a
// network share. ERROR_INVALID_HANDLE may occur because the network
// connection was dropped and the server closed the handle. However,
// the client side handle is still open and even valid for certain
// operations.
//
// Note that _parent.Dispose doesn't throw so we don't need to special case.
// SetHandleAsInvalid only sets _closed field to true (without
// actually closing handle) so we don't need to call that as well.
if (errorCode == Interop.Errors.ERROR_INVALID_HANDLE)
{
handle.Dispose();
}
return errorCode;
}
internal static void Lock(SafeFileHandle handle, bool _ /*canWrite*/, long position, long length)
{
int positionLow = unchecked((int)(position));
int positionHigh = unchecked((int)(position >> 32));
int lengthLow = unchecked((int)(length));
int lengthHigh = unchecked((int)(length >> 32));
if (!Interop.Kernel32.LockFile(handle, positionLow, positionHigh, lengthLow, lengthHigh))
{
throw Win32Marshal.GetExceptionForLastWin32Error(handle.Path);
}
}
internal static void Unlock(SafeFileHandle handle, long position, long length)
{
int positionLow = unchecked((int)(position));
int positionHigh = unchecked((int)(position >> 32));
int lengthLow = unchecked((int)(length));
int lengthHigh = unchecked((int)(length >> 32));
if (!Interop.Kernel32.UnlockFile(handle, positionLow, positionHigh, lengthLow, lengthHigh))
{
throw Win32Marshal.GetExceptionForLastWin32Error(handle.Path);
}
}
internal static unsafe int ReadFileNative(SafeFileHandle handle, Span<byte> bytes, NativeOverlapped* overlapped, out int errorCode)
{
Debug.Assert(handle != null, "handle != null");
int r;
int numBytesRead = 0;
fixed (byte* p = &MemoryMarshal.GetReference(bytes))
{
r = overlapped == null
? Interop.Kernel32.ReadFile(handle, p, bytes.Length, out numBytesRead, overlapped)
: Interop.Kernel32.ReadFile(handle, p, bytes.Length, IntPtr.Zero, overlapped);
}
if (r == 0)
{
errorCode = GetLastWin32ErrorAndDisposeHandleIfInvalid(handle);
return -1;
}
else
{
errorCode = 0;
return numBytesRead;
}
}
internal static async Task AsyncModeCopyToAsync(SafeFileHandle handle, bool canSeek, long filePosition, Stream destination, int bufferSize, CancellationToken cancellationToken)
{
// For efficiency, we avoid creating a new task and associated state for each asynchronous read.
// Instead, we create a single reusable awaitable object that will be triggered when an await completes
// and reset before going again.
var readAwaitable = new AsyncCopyToAwaitable(handle);
// Make sure we are reading from the position that we think we are.
// Only set the position in the awaitable if we can seek (e.g. not for pipes).
if (canSeek)
{
readAwaitable._position = filePosition;
}
// Get the buffer to use for the copy operation, as the base CopyToAsync does. We don't try to use
// _buffer here, even if it's not null, as concurrent operations are allowed, and another operation may
// actually be using the buffer already. Plus, it'll be rare for _buffer to be non-null, as typically
// CopyToAsync is used as the only operation performed on the stream, and the buffer is lazily initialized.
// Further, typically the CopyToAsync buffer size will be larger than that used by the FileStream, such that
// we'd likely be unable to use it anyway. Instead, we rent the buffer from a pool.
byte[] copyBuffer = ArrayPool<byte>.Shared.Rent(bufferSize);
// Allocate an Overlapped we can use repeatedly for all operations
var awaitableOverlapped = new PreAllocatedOverlapped(AsyncCopyToAwaitable.s_callback, readAwaitable, copyBuffer);
var cancellationReg = default(CancellationTokenRegistration);
try
{
// Register for cancellation. We do this once for the whole copy operation, and just try to cancel
// whatever read operation may currently be in progress, if there is one. It's possible the cancellation
// request could come in between operations, in which case we flag that with explicit calls to ThrowIfCancellationRequested
// in the read/write copy loop.
if (cancellationToken.CanBeCanceled)
{
cancellationReg = cancellationToken.UnsafeRegister(static s =>
{
Debug.Assert(s is AsyncCopyToAwaitable);
var innerAwaitable = (AsyncCopyToAwaitable)s;
unsafe
{
lock (innerAwaitable.CancellationLock) // synchronize with cleanup of the overlapped
{
if (innerAwaitable._nativeOverlapped != null)
{
// Try to cancel the I/O. We ignore the return value, as cancellation is opportunistic and we
// don't want to fail the operation because we couldn't cancel it.
Interop.Kernel32.CancelIoEx(innerAwaitable._fileHandle, innerAwaitable._nativeOverlapped);
}
}
}
}, readAwaitable);
}
// Repeatedly read from this FileStream and write the results to the destination stream.
while (true)
{
cancellationToken.ThrowIfCancellationRequested();
readAwaitable.ResetForNextOperation();
try
{
bool synchronousSuccess;
int errorCode;
unsafe
{
// Allocate a native overlapped for our reusable overlapped, and set position to read based on the next
// desired address stored in the awaitable. (This position may be 0, if either we're at the beginning or
// if the stream isn't seekable.)
readAwaitable._nativeOverlapped = handle.ThreadPoolBinding!.AllocateNativeOverlapped(awaitableOverlapped);
if (canSeek)
{
readAwaitable._nativeOverlapped->OffsetLow = unchecked((int)readAwaitable._position);
readAwaitable._nativeOverlapped->OffsetHigh = (int)(readAwaitable._position >> 32);
}
// Kick off the read.
synchronousSuccess = ReadFileNative(handle, copyBuffer, readAwaitable._nativeOverlapped, out errorCode) >= 0;
}
// If the operation did not synchronously succeed, it either failed or initiated the asynchronous operation.
if (!synchronousSuccess && errorCode != Interop.Errors.ERROR_IO_PENDING)
{
if (!RandomAccess.IsEndOfFile(errorCode, handle, readAwaitable._position))
{
throw Win32Marshal.GetExceptionForWin32Error(errorCode, handle.Path);
}
// We're at or past the end of the file, and the overlapped callback
// won't be raised in these cases. Mark it as completed so that the await
// below will see it as such.
readAwaitable.MarkCompleted();
}
// Wait for the async operation (which may or may not have already completed), then throw if it failed.
await readAwaitable;
if (readAwaitable._errorCode != Interop.Errors.ERROR_SUCCESS)
{
if (readAwaitable._errorCode == Interop.Errors.ERROR_OPERATION_ABORTED)
{
throw new OperationCanceledException(cancellationToken.IsCancellationRequested ? cancellationToken : new CancellationToken(true));
}
else if (!RandomAccess.IsEndOfFile((int)readAwaitable._errorCode, handle, readAwaitable._position))
{
throw Win32Marshal.GetExceptionForWin32Error((int)readAwaitable._errorCode, handle.Path);
}
Debug.Assert(readAwaitable._numBytes == 0, $"Expected 0 bytes read, got {readAwaitable._numBytes}");
}
// Successful operation. If we got zero bytes, we're done: exit the read/write loop.
int numBytesRead = (int)readAwaitable._numBytes;
if (numBytesRead == 0)
{
break;
}
// Otherwise, update the read position for next time accordingly.
if (canSeek)
{
readAwaitable._position += numBytesRead;
}
}
finally
{
// Free the resources for this read operation
unsafe
{
NativeOverlapped* overlapped;
lock (readAwaitable.CancellationLock) // just an Exchange, but we need this to be synchronized with cancellation, so using the same lock
{
overlapped = readAwaitable._nativeOverlapped;
readAwaitable._nativeOverlapped = null;
}
if (overlapped != null)
{
handle.ThreadPoolBinding!.FreeNativeOverlapped(overlapped);
}
}
}
// Write out the read data.
await destination.WriteAsync(new ReadOnlyMemory<byte>(copyBuffer, 0, (int)readAwaitable._numBytes), cancellationToken).ConfigureAwait(false);
}
}
finally
{
// Cleanup from the whole copy operation
cancellationReg.Dispose();
awaitableOverlapped.Dispose();
ArrayPool<byte>.Shared.Return(copyBuffer);
}
}
/// <summary>Used by AsyncWindowsFileStreamStrategy.CopyToAsync to enable awaiting the result of an overlapped I/O operation with minimal overhead.</summary>
private sealed unsafe class AsyncCopyToAwaitable : ICriticalNotifyCompletion
{
/// <summary>Sentinel object used to indicate that the I/O operation has completed before being awaited.</summary>
private static readonly Action s_sentinel = () => { };
/// <summary>Cached delegate to IOCallback.</summary>
internal static readonly IOCompletionCallback s_callback = IOCallback;
internal readonly SafeFileHandle _fileHandle;
/// <summary>Tracked position representing the next location from which to read.</summary>
internal long _position;
/// <summary>The current native overlapped pointer. This changes for each operation.</summary>
internal NativeOverlapped* _nativeOverlapped;
/// <summary>
/// null if the operation is still in progress,
/// s_sentinel if the I/O operation completed before the await,
/// s_callback if it completed after the await yielded.
/// </summary>
internal Action? _continuation;
/// <summary>Last error code from completed operation.</summary>
internal uint _errorCode;
/// <summary>Last number of read bytes from completed operation.</summary>
internal uint _numBytes;
/// <summary>Lock object used to protect cancellation-related access to _nativeOverlapped.</summary>
internal object CancellationLock => this;
/// <summary>Initialize the awaitable.</summary>
internal AsyncCopyToAwaitable(SafeFileHandle fileHandle) => _fileHandle = fileHandle;
/// <summary>Reset state to prepare for the next read operation.</summary>
internal void ResetForNextOperation()
{
Debug.Assert(_position >= 0, $"Expected non-negative position, got {_position}");
_continuation = null;
_errorCode = 0;
_numBytes = 0;
}
/// <summary>Overlapped callback: store the results, then invoke the continuation delegate.</summary>
internal static void IOCallback(uint errorCode, uint numBytes, NativeOverlapped* pOVERLAP)
{
var awaitable = (AsyncCopyToAwaitable?)ThreadPoolBoundHandle.GetNativeOverlappedState(pOVERLAP);
Debug.Assert(awaitable != null);
Debug.Assert(!ReferenceEquals(awaitable._continuation, s_sentinel), "Sentinel must not have already been set as the continuation");
awaitable._errorCode = errorCode;
awaitable._numBytes = numBytes;
(awaitable._continuation ?? Interlocked.CompareExchange(ref awaitable._continuation, s_sentinel, null))?.Invoke();
}
/// <summary>
/// Called when it's known that the I/O callback for an operation will not be invoked but we'll
/// still be awaiting the awaitable.
/// </summary>
internal void MarkCompleted()
{
Debug.Assert(_continuation == null, "Expected null continuation");
_continuation = s_sentinel;
}
public AsyncCopyToAwaitable GetAwaiter() => this;
public bool IsCompleted => ReferenceEquals(_continuation, s_sentinel);
public void GetResult() { }
public void OnCompleted(Action continuation) => UnsafeOnCompleted(continuation);
public void UnsafeOnCompleted(Action continuation)
{
if (ReferenceEquals(_continuation, s_sentinel) ||
Interlocked.CompareExchange(ref _continuation, continuation, null) != null)
{
Debug.Assert(ReferenceEquals(_continuation, s_sentinel), $"Expected continuation set to s_sentinel, got ${_continuation}");
Task.Run(continuation);
}
}
}
}
}