FileStream.cs 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using Microsoft.Win32.SafeHandles;
  7. using System.Diagnostics;
  8. namespace System.IO
  9. {
  10. public partial class FileStream : Stream
  11. {
  12. private const FileShare DefaultShare = FileShare.Read;
  13. private const bool DefaultIsAsync = false;
  14. internal const int DefaultBufferSize = 4096;
  15. private byte[] _buffer;
  16. private int _bufferLength;
  17. private readonly SafeFileHandle _fileHandle;
  18. /// <summary>Whether the file is opened for reading, writing, or both.</summary>
  19. private readonly FileAccess _access;
  20. /// <summary>The path to the opened file.</summary>
  21. private readonly string _path;
  22. /// <summary>The next available byte to be read from the _buffer.</summary>
  23. private int _readPos;
  24. /// <summary>The number of valid bytes in _buffer.</summary>
  25. private int _readLength;
  26. /// <summary>The next location in which a write should occur to the buffer.</summary>
  27. private int _writePos;
  28. /// <summary>
  29. /// Whether asynchronous read/write/flush operations should be performed using async I/O.
  30. /// On Windows FileOptions.Asynchronous controls how the file handle is configured,
  31. /// and then as a result how operations are issued against that file handle. On Unix,
  32. /// there isn't any distinction around how file descriptors are created for async vs
  33. /// sync, but we still differentiate how the operations are issued in order to provide
  34. /// similar behavioral semantics and performance characteristics as on Windows. On
  35. /// Windows, if non-async, async read/write requests just delegate to the base stream,
  36. /// and no attempt is made to synchronize between sync and async operations on the stream;
  37. /// if async, then async read/write requests are implemented specially, and sync read/write
  38. /// requests are coordinated with async ones by implementing the sync ones over the async
  39. /// ones. On Unix, we do something similar. If non-async, async read/write requests just
  40. /// delegate to the base stream, and no attempt is made to synchronize. If async, we use
  41. /// a semaphore to coordinate both sync and async operations.
  42. /// </summary>
  43. private readonly bool _useAsyncIO;
  44. /// <summary>cached task for read ops that complete synchronously</summary>
  45. private Task<int> _lastSynchronouslyCompletedTask = null;
  46. /// <summary>
  47. /// Currently cached position in the stream. This should always mirror the underlying file's actual position,
  48. /// and should only ever be out of sync if another stream with access to this same file manipulates it, at which
  49. /// point we attempt to error out.
  50. /// </summary>
  51. private long _filePosition;
  52. /// <summary>Whether the file stream's handle has been exposed.</summary>
  53. private bool _exposedHandle;
  54. [Obsolete("This constructor has been deprecated. Please use new FileStream(SafeFileHandle handle, FileAccess access) instead. https://go.microsoft.com/fwlink/?linkid=14202")]
  55. public FileStream(IntPtr handle, FileAccess access)
  56. : this(handle, access, true, DefaultBufferSize, false)
  57. {
  58. }
  59. [Obsolete("This constructor has been deprecated. Please use new FileStream(SafeFileHandle handle, FileAccess access) instead, and optionally make a new SafeFileHandle with ownsHandle=false if needed. https://go.microsoft.com/fwlink/?linkid=14202")]
  60. public FileStream(IntPtr handle, FileAccess access, bool ownsHandle)
  61. : this(handle, access, ownsHandle, DefaultBufferSize, false)
  62. {
  63. }
  64. [Obsolete("This constructor has been deprecated. Please use new FileStream(SafeFileHandle handle, FileAccess access, int bufferSize) instead, and optionally make a new SafeFileHandle with ownsHandle=false if needed. https://go.microsoft.com/fwlink/?linkid=14202")]
  65. public FileStream(IntPtr handle, FileAccess access, bool ownsHandle, int bufferSize)
  66. : this(handle, access, ownsHandle, bufferSize, false)
  67. {
  68. }
  69. [Obsolete("This constructor has been deprecated. Please use new FileStream(SafeFileHandle handle, FileAccess access, int bufferSize, bool isAsync) instead, and optionally make a new SafeFileHandle with ownsHandle=false if needed. https://go.microsoft.com/fwlink/?linkid=14202")]
  70. public FileStream(IntPtr handle, FileAccess access, bool ownsHandle, int bufferSize, bool isAsync)
  71. {
  72. SafeFileHandle safeHandle = new SafeFileHandle(handle, ownsHandle: ownsHandle);
  73. try
  74. {
  75. ValidateAndInitFromHandle(safeHandle, access, bufferSize, isAsync);
  76. }
  77. catch
  78. {
  79. // We don't want to take ownership of closing passed in handles
  80. // *unless* the constructor completes successfully.
  81. GC.SuppressFinalize(safeHandle);
  82. // This would also prevent Close from being called, but is unnecessary
  83. // as we've removed the object from the finalizer queue.
  84. //
  85. // safeHandle.SetHandleAsInvalid();
  86. throw;
  87. }
  88. // Note: Cleaner to set the following fields in ValidateAndInitFromHandle,
  89. // but we can't as they're readonly.
  90. _access = access;
  91. _useAsyncIO = isAsync;
  92. // As the handle was passed in, we must set the handle field at the very end to
  93. // avoid the finalizer closing the handle when we throw errors.
  94. _fileHandle = safeHandle;
  95. }
  96. public FileStream(SafeFileHandle handle, FileAccess access)
  97. : this(handle, access, DefaultBufferSize)
  98. {
  99. }
  100. public FileStream(SafeFileHandle handle, FileAccess access, int bufferSize)
  101. : this(handle, access, bufferSize, GetDefaultIsAsync(handle))
  102. {
  103. }
  104. private void ValidateAndInitFromHandle(SafeFileHandle handle, FileAccess access, int bufferSize, bool isAsync)
  105. {
  106. if (handle.IsInvalid)
  107. throw new ArgumentException(SR.Arg_InvalidHandle, nameof(handle));
  108. if (access < FileAccess.Read || access > FileAccess.ReadWrite)
  109. throw new ArgumentOutOfRangeException(nameof(access), SR.ArgumentOutOfRange_Enum);
  110. if (bufferSize <= 0)
  111. throw new ArgumentOutOfRangeException(nameof(bufferSize), SR.ArgumentOutOfRange_NeedPosNum);
  112. if (handle.IsClosed)
  113. throw new ObjectDisposedException(SR.ObjectDisposed_FileClosed);
  114. if (handle.IsAsync.HasValue && isAsync != handle.IsAsync.GetValueOrDefault())
  115. throw new ArgumentException(SR.Arg_HandleNotAsync, nameof(handle));
  116. _exposedHandle = true;
  117. _bufferLength = bufferSize;
  118. InitFromHandle(handle, access, isAsync);
  119. }
  120. public FileStream(SafeFileHandle handle, FileAccess access, int bufferSize, bool isAsync)
  121. {
  122. ValidateAndInitFromHandle(handle, access, bufferSize, isAsync);
  123. // Note: Cleaner to set the following fields in ValidateAndInitFromHandle,
  124. // but we can't as they're readonly.
  125. _access = access;
  126. _useAsyncIO = isAsync;
  127. // As the handle was passed in, we must set the handle field at the very end to
  128. // avoid the finalizer closing the handle when we throw errors.
  129. _fileHandle = handle;
  130. }
  131. public FileStream(string path, FileMode mode) :
  132. this(path, mode, (mode == FileMode.Append ? FileAccess.Write : FileAccess.ReadWrite), DefaultShare, DefaultBufferSize, DefaultIsAsync)
  133. { }
  134. public FileStream(string path, FileMode mode, FileAccess access) :
  135. this(path, mode, access, DefaultShare, DefaultBufferSize, DefaultIsAsync)
  136. { }
  137. public FileStream(string path, FileMode mode, FileAccess access, FileShare share) :
  138. this(path, mode, access, share, DefaultBufferSize, DefaultIsAsync)
  139. { }
  140. public FileStream(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize) :
  141. this(path, mode, access, share, bufferSize, DefaultIsAsync)
  142. { }
  143. public FileStream(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, bool useAsync) :
  144. this(path, mode, access, share, bufferSize, useAsync ? FileOptions.Asynchronous : FileOptions.None)
  145. { }
  146. public FileStream(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, FileOptions options)
  147. {
  148. if (path == null)
  149. throw new ArgumentNullException(nameof(path), SR.ArgumentNull_Path);
  150. if (path.Length == 0)
  151. throw new ArgumentException(SR.Argument_EmptyPath, nameof(path));
  152. // don't include inheritable in our bounds check for share
  153. FileShare tempshare = share & ~FileShare.Inheritable;
  154. string badArg = null;
  155. if (mode < FileMode.CreateNew || mode > FileMode.Append)
  156. badArg = nameof(mode);
  157. else if (access < FileAccess.Read || access > FileAccess.ReadWrite)
  158. badArg = nameof(access);
  159. else if (tempshare < FileShare.None || tempshare > (FileShare.ReadWrite | FileShare.Delete))
  160. badArg = nameof(share);
  161. if (badArg != null)
  162. throw new ArgumentOutOfRangeException(badArg, SR.ArgumentOutOfRange_Enum);
  163. // NOTE: any change to FileOptions enum needs to be matched here in the error validation
  164. if (options != FileOptions.None && (options & ~(FileOptions.WriteThrough | FileOptions.Asynchronous | FileOptions.RandomAccess | FileOptions.DeleteOnClose | FileOptions.SequentialScan | FileOptions.Encrypted | (FileOptions)0x20000000 /* NoBuffering */)) != 0)
  165. throw new ArgumentOutOfRangeException(nameof(options), SR.ArgumentOutOfRange_Enum);
  166. if (bufferSize <= 0)
  167. throw new ArgumentOutOfRangeException(nameof(bufferSize), SR.ArgumentOutOfRange_NeedPosNum);
  168. // Write access validation
  169. if ((access & FileAccess.Write) == 0)
  170. {
  171. if (mode == FileMode.Truncate || mode == FileMode.CreateNew || mode == FileMode.Create || mode == FileMode.Append)
  172. {
  173. // No write access, mode and access disagree but flag access since mode comes first
  174. throw new ArgumentException(SR.Format(SR.Argument_InvalidFileModeAndAccessCombo, mode, access), nameof(access));
  175. }
  176. }
  177. if ((access & FileAccess.Read) != 0 && mode == FileMode.Append)
  178. throw new ArgumentException(SR.Argument_InvalidAppendMode, nameof(access));
  179. string fullPath = Path.GetFullPath(path);
  180. _path = fullPath;
  181. _access = access;
  182. _bufferLength = bufferSize;
  183. if ((options & FileOptions.Asynchronous) != 0)
  184. _useAsyncIO = true;
  185. _fileHandle = OpenHandle(mode, share, options);
  186. try
  187. {
  188. Init(mode, share, path);
  189. }
  190. catch
  191. {
  192. // If anything goes wrong while setting up the stream, make sure we deterministically dispose
  193. // of the opened handle.
  194. _fileHandle.Dispose();
  195. _fileHandle = null;
  196. throw;
  197. }
  198. }
  199. [Obsolete("This property has been deprecated. Please use FileStream's SafeFileHandle property instead. https://go.microsoft.com/fwlink/?linkid=14202")]
  200. public virtual IntPtr Handle { get { return SafeFileHandle.DangerousGetHandle(); } }
  201. public virtual void Lock(long position, long length)
  202. {
  203. if (position < 0 || length < 0)
  204. {
  205. throw new ArgumentOutOfRangeException(position < 0 ? nameof(position) : nameof(length), SR.ArgumentOutOfRange_NeedNonNegNum);
  206. }
  207. if (_fileHandle.IsClosed)
  208. {
  209. throw Error.GetFileNotOpen();
  210. }
  211. LockInternal(position, length);
  212. }
  213. public virtual void Unlock(long position, long length)
  214. {
  215. if (position < 0 || length < 0)
  216. {
  217. throw new ArgumentOutOfRangeException(position < 0 ? nameof(position) : nameof(length), SR.ArgumentOutOfRange_NeedNonNegNum);
  218. }
  219. if (_fileHandle.IsClosed)
  220. {
  221. throw Error.GetFileNotOpen();
  222. }
  223. UnlockInternal(position, length);
  224. }
  225. public override Task FlushAsync(CancellationToken cancellationToken)
  226. {
  227. // If we have been inherited into a subclass, the following implementation could be incorrect
  228. // since it does not call through to Flush() which a subclass might have overridden. To be safe
  229. // we will only use this implementation in cases where we know it is safe to do so,
  230. // and delegate to our base class (which will call into Flush) when we are not sure.
  231. if (GetType() != typeof(FileStream))
  232. return base.FlushAsync(cancellationToken);
  233. return FlushAsyncInternal(cancellationToken);
  234. }
  235. public override int Read(byte[] array, int offset, int count)
  236. {
  237. ValidateReadWriteArgs(array, offset, count);
  238. return _useAsyncIO ?
  239. ReadAsyncTask(array, offset, count, CancellationToken.None).GetAwaiter().GetResult() :
  240. ReadSpan(new Span<byte>(array, offset, count));
  241. }
  242. public override int Read(Span<byte> buffer)
  243. {
  244. if (GetType() == typeof(FileStream) && !_useAsyncIO)
  245. {
  246. if (_fileHandle.IsClosed)
  247. {
  248. throw Error.GetFileNotOpen();
  249. }
  250. return ReadSpan(buffer);
  251. }
  252. else
  253. {
  254. // This type is derived from FileStream and/or the stream is in async mode. If this is a
  255. // derived type, it may have overridden Read(byte[], int, int) prior to this Read(Span<byte>)
  256. // overload being introduced. In that case, this Read(Span<byte>) overload should use the behavior
  257. // of Read(byte[],int,int) overload. Or if the stream is in async mode, we can't call the
  258. // synchronous ReadSpan, so we similarly call the base Read, which will turn delegate to
  259. // Read(byte[],int,int), which will do the right thing if we're in async mode.
  260. return base.Read(buffer);
  261. }
  262. }
  263. public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
  264. {
  265. if (buffer == null)
  266. throw new ArgumentNullException(nameof(buffer), SR.ArgumentNull_Buffer);
  267. if (offset < 0)
  268. throw new ArgumentOutOfRangeException(nameof(offset), SR.ArgumentOutOfRange_NeedNonNegNum);
  269. if (count < 0)
  270. throw new ArgumentOutOfRangeException(nameof(count), SR.ArgumentOutOfRange_NeedNonNegNum);
  271. if (buffer.Length - offset < count)
  272. throw new ArgumentException(SR.Argument_InvalidOffLen /*, no good single parameter name to pass*/);
  273. // If we have been inherited into a subclass, the following implementation could be incorrect
  274. // since it does not call through to Read() which a subclass might have overridden.
  275. // To be safe we will only use this implementation in cases where we know it is safe to do so,
  276. // and delegate to our base class (which will call into Read/ReadAsync) when we are not sure.
  277. // Similarly, if we weren't opened for asynchronous I/O, call to the base implementation so that
  278. // Read is invoked asynchronously.
  279. if (GetType() != typeof(FileStream) || !_useAsyncIO)
  280. return base.ReadAsync(buffer, offset, count, cancellationToken);
  281. if (cancellationToken.IsCancellationRequested)
  282. return Task.FromCanceled<int>(cancellationToken);
  283. if (IsClosed)
  284. throw Error.GetFileNotOpen();
  285. return ReadAsyncTask(buffer, offset, count, cancellationToken);
  286. }
  287. public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default)
  288. {
  289. if (!_useAsyncIO || GetType() != typeof(FileStream))
  290. {
  291. // If we're not using async I/O, delegate to the base, which will queue a call to Read.
  292. // Or if this isn't a concrete FileStream, a derived type may have overridden ReadAsync(byte[],...),
  293. // which was introduced first, so delegate to the base which will delegate to that.
  294. return base.ReadAsync(buffer, cancellationToken);
  295. }
  296. if (cancellationToken.IsCancellationRequested)
  297. {
  298. return new ValueTask<int>(Task.FromCanceled<int>(cancellationToken));
  299. }
  300. if (IsClosed)
  301. {
  302. throw Error.GetFileNotOpen();
  303. }
  304. Task<int> t = ReadAsyncInternal(buffer, cancellationToken, out int synchronousResult);
  305. return t != null ?
  306. new ValueTask<int>(t) :
  307. new ValueTask<int>(synchronousResult);
  308. }
  309. private Task<int> ReadAsyncTask(byte[] array, int offset, int count, CancellationToken cancellationToken)
  310. {
  311. Task<int> t = ReadAsyncInternal(new Memory<byte>(array, offset, count), cancellationToken, out int synchronousResult);
  312. if (t == null)
  313. {
  314. t = _lastSynchronouslyCompletedTask;
  315. Debug.Assert(t == null || t.IsCompletedSuccessfully, "Cached task should have completed successfully");
  316. if (t == null || t.Result != synchronousResult)
  317. {
  318. _lastSynchronouslyCompletedTask = t = Task.FromResult(synchronousResult);
  319. }
  320. }
  321. return t;
  322. }
  323. public override void Write(byte[] array, int offset, int count)
  324. {
  325. ValidateReadWriteArgs(array, offset, count);
  326. if (_useAsyncIO)
  327. {
  328. WriteAsyncInternal(new ReadOnlyMemory<byte>(array, offset, count), CancellationToken.None).GetAwaiter().GetResult();
  329. }
  330. else
  331. {
  332. WriteSpan(new ReadOnlySpan<byte>(array, offset, count));
  333. }
  334. }
  335. public override void Write(ReadOnlySpan<byte> buffer)
  336. {
  337. if (GetType() == typeof(FileStream) && !_useAsyncIO)
  338. {
  339. if (_fileHandle.IsClosed)
  340. {
  341. throw Error.GetFileNotOpen();
  342. }
  343. WriteSpan(buffer);
  344. }
  345. else
  346. {
  347. // This type is derived from FileStream and/or the stream is in async mode. If this is a
  348. // derived type, it may have overridden Write(byte[], int, int) prior to this Write(ReadOnlySpan<byte>)
  349. // overload being introduced. In that case, this Write(ReadOnlySpan<byte>) overload should use the behavior
  350. // of Write(byte[],int,int) overload. Or if the stream is in async mode, we can't call the
  351. // synchronous WriteSpan, so we similarly call the base Write, which will turn delegate to
  352. // Write(byte[],int,int), which will do the right thing if we're in async mode.
  353. base.Write(buffer);
  354. }
  355. }
  356. public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
  357. {
  358. if (buffer == null)
  359. throw new ArgumentNullException(nameof(buffer), SR.ArgumentNull_Buffer);
  360. if (offset < 0)
  361. throw new ArgumentOutOfRangeException(nameof(offset), SR.ArgumentOutOfRange_NeedNonNegNum);
  362. if (count < 0)
  363. throw new ArgumentOutOfRangeException(nameof(count), SR.ArgumentOutOfRange_NeedNonNegNum);
  364. if (buffer.Length - offset < count)
  365. throw new ArgumentException(SR.Argument_InvalidOffLen /*, no good single parameter name to pass*/);
  366. // If we have been inherited into a subclass, the following implementation could be incorrect
  367. // since it does not call through to Write() or WriteAsync() which a subclass might have overridden.
  368. // To be safe we will only use this implementation in cases where we know it is safe to do so,
  369. // and delegate to our base class (which will call into Write/WriteAsync) when we are not sure.
  370. if (!_useAsyncIO || GetType() != typeof(FileStream))
  371. return base.WriteAsync(buffer, offset, count, cancellationToken);
  372. if (cancellationToken.IsCancellationRequested)
  373. return Task.FromCanceled(cancellationToken);
  374. if (IsClosed)
  375. throw Error.GetFileNotOpen();
  376. return WriteAsyncInternal(new ReadOnlyMemory<byte>(buffer, offset, count), cancellationToken).AsTask();
  377. }
  378. public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default)
  379. {
  380. if (!_useAsyncIO || GetType() != typeof(FileStream))
  381. {
  382. // If we're not using async I/O, delegate to the base, which will queue a call to Write.
  383. // Or if this isn't a concrete FileStream, a derived type may have overridden WriteAsync(byte[],...),
  384. // which was introduced first, so delegate to the base which will delegate to that.
  385. return base.WriteAsync(buffer, cancellationToken);
  386. }
  387. if (cancellationToken.IsCancellationRequested)
  388. {
  389. return new ValueTask(Task.FromCanceled<int>(cancellationToken));
  390. }
  391. if (IsClosed)
  392. {
  393. throw Error.GetFileNotOpen();
  394. }
  395. return WriteAsyncInternal(buffer, cancellationToken);
  396. }
  397. /// <summary>
  398. /// Clears buffers for this stream and causes any buffered data to be written to the file.
  399. /// </summary>
  400. public override void Flush()
  401. {
  402. // Make sure that we call through the public virtual API
  403. Flush(flushToDisk: false);
  404. }
  405. /// <summary>
  406. /// Clears buffers for this stream, and if <param name="flushToDisk"/> is true,
  407. /// causes any buffered data to be written to the file.
  408. /// </summary>
  409. public virtual void Flush(bool flushToDisk)
  410. {
  411. if (IsClosed) throw Error.GetFileNotOpen();
  412. FlushInternalBuffer();
  413. if (flushToDisk && CanWrite)
  414. {
  415. FlushOSBuffer();
  416. }
  417. }
  418. /// <summary>Gets a value indicating whether the current stream supports reading.</summary>
  419. public override bool CanRead
  420. {
  421. get { return !_fileHandle.IsClosed && (_access & FileAccess.Read) != 0; }
  422. }
  423. /// <summary>Gets a value indicating whether the current stream supports writing.</summary>
  424. public override bool CanWrite
  425. {
  426. get { return !_fileHandle.IsClosed && (_access & FileAccess.Write) != 0; }
  427. }
  428. /// <summary>Validates arguments to Read and Write and throws resulting exceptions.</summary>
  429. /// <param name="array">The buffer to read from or write to.</param>
  430. /// <param name="offset">The zero-based offset into the array.</param>
  431. /// <param name="count">The maximum number of bytes to read or write.</param>
  432. private void ValidateReadWriteArgs(byte[] array, int offset, int count)
  433. {
  434. if (array == null)
  435. throw new ArgumentNullException(nameof(array), SR.ArgumentNull_Buffer);
  436. if (offset < 0)
  437. throw new ArgumentOutOfRangeException(nameof(offset), SR.ArgumentOutOfRange_NeedNonNegNum);
  438. if (count < 0)
  439. throw new ArgumentOutOfRangeException(nameof(count), SR.ArgumentOutOfRange_NeedNonNegNum);
  440. if (array.Length - offset < count)
  441. throw new ArgumentException(SR.Argument_InvalidOffLen /*, no good single parameter name to pass*/);
  442. if (_fileHandle.IsClosed)
  443. throw Error.GetFileNotOpen();
  444. }
  445. /// <summary>Sets the length of this stream to the given value.</summary>
  446. /// <param name="value">The new length of the stream.</param>
  447. public override void SetLength(long value)
  448. {
  449. if (value < 0)
  450. throw new ArgumentOutOfRangeException(nameof(value), SR.ArgumentOutOfRange_NeedNonNegNum);
  451. if (_fileHandle.IsClosed)
  452. throw Error.GetFileNotOpen();
  453. if (!CanSeek)
  454. throw Error.GetSeekNotSupported();
  455. if (!CanWrite)
  456. throw Error.GetWriteNotSupported();
  457. SetLengthInternal(value);
  458. }
  459. public virtual SafeFileHandle SafeFileHandle
  460. {
  461. get
  462. {
  463. Flush();
  464. _exposedHandle = true;
  465. return _fileHandle;
  466. }
  467. }
  468. /// <summary>Gets the path that was passed to the constructor.</summary>
  469. public virtual string Name { get { return _path ?? SR.IO_UnknownFileName; } }
  470. /// <summary>Gets a value indicating whether the stream was opened for I/O to be performed synchronously or asynchronously.</summary>
  471. public virtual bool IsAsync
  472. {
  473. get { return _useAsyncIO; }
  474. }
  475. /// <summary>Gets the length of the stream in bytes.</summary>
  476. public override long Length
  477. {
  478. get
  479. {
  480. if (_fileHandle.IsClosed) throw Error.GetFileNotOpen();
  481. if (!CanSeek) throw Error.GetSeekNotSupported();
  482. return GetLengthInternal();
  483. }
  484. }
  485. /// <summary>
  486. /// Verify that the actual position of the OS's handle equals what we expect it to.
  487. /// This will fail if someone else moved the UnixFileStream's handle or if
  488. /// our position updating code is incorrect.
  489. /// </summary>
  490. private void VerifyOSHandlePosition()
  491. {
  492. bool verifyPosition = _exposedHandle; // in release, only verify if we've given out the handle such that someone else could be manipulating it
  493. #if DEBUG
  494. verifyPosition = true; // in debug, always make sure our position matches what the OS says it should be
  495. #endif
  496. if (verifyPosition && CanSeek)
  497. {
  498. long oldPos = _filePosition; // SeekCore will override the current _position, so save it now
  499. long curPos = SeekCore(_fileHandle, 0, SeekOrigin.Current);
  500. if (oldPos != curPos)
  501. {
  502. // For reads, this is non-fatal but we still could have returned corrupted
  503. // data in some cases, so discard the internal buffer. For writes,
  504. // this is a problem; discard the buffer and error out.
  505. _readPos = _readLength = 0;
  506. if (_writePos > 0)
  507. {
  508. _writePos = 0;
  509. throw new IOException(SR.IO_FileStreamHandlePosition);
  510. }
  511. }
  512. }
  513. }
  514. /// <summary>Verifies that state relating to the read/write buffer is consistent.</summary>
  515. [Conditional("DEBUG")]
  516. private void AssertBufferInvariants()
  517. {
  518. // Read buffer values must be in range: 0 <= _bufferReadPos <= _bufferReadLength <= _bufferLength
  519. Debug.Assert(0 <= _readPos && _readPos <= _readLength && _readLength <= _bufferLength);
  520. // Write buffer values must be in range: 0 <= _bufferWritePos <= _bufferLength
  521. Debug.Assert(0 <= _writePos && _writePos <= _bufferLength);
  522. // Read buffering and write buffering can't both be active
  523. Debug.Assert((_readPos == 0 && _readLength == 0) || _writePos == 0);
  524. }
  525. /// <summary>Validates that we're ready to read from the stream.</summary>
  526. private void PrepareForReading()
  527. {
  528. if (_fileHandle.IsClosed)
  529. throw Error.GetFileNotOpen();
  530. if (_readLength == 0 && !CanRead)
  531. throw Error.GetReadNotSupported();
  532. AssertBufferInvariants();
  533. }
  534. /// <summary>Gets or sets the position within the current stream</summary>
  535. public override long Position
  536. {
  537. get
  538. {
  539. if (_fileHandle.IsClosed)
  540. throw Error.GetFileNotOpen();
  541. if (!CanSeek)
  542. throw Error.GetSeekNotSupported();
  543. AssertBufferInvariants();
  544. VerifyOSHandlePosition();
  545. // We may have read data into our buffer from the handle, such that the handle position
  546. // is artificially further along than the consumer's view of the stream's position.
  547. // Thus, when reading, our position is really starting from the handle position negatively
  548. // offset by the number of bytes in the buffer and positively offset by the number of
  549. // bytes into that buffer we've read. When writing, both the read length and position
  550. // must be zero, and our position is just the handle position offset positive by how many
  551. // bytes we've written into the buffer.
  552. return (_filePosition - _readLength) + _readPos + _writePos;
  553. }
  554. set
  555. {
  556. if (value < 0)
  557. throw new ArgumentOutOfRangeException(nameof(value), SR.ArgumentOutOfRange_NeedNonNegNum);
  558. Seek(value, SeekOrigin.Begin);
  559. }
  560. }
  561. internal virtual bool IsClosed => _fileHandle.IsClosed;
  562. private static bool IsIoRelatedException(Exception e) =>
  563. // These all derive from IOException
  564. // DirectoryNotFoundException
  565. // DriveNotFoundException
  566. // EndOfStreamException
  567. // FileLoadException
  568. // FileNotFoundException
  569. // PathTooLongException
  570. // PipeException
  571. e is IOException ||
  572. // Note that SecurityException is only thrown on runtimes that support CAS
  573. // e is SecurityException ||
  574. e is UnauthorizedAccessException ||
  575. e is NotSupportedException ||
  576. (e is ArgumentException && !(e is ArgumentNullException));
  577. /// <summary>
  578. /// Gets the array used for buffering reading and writing.
  579. /// If the array hasn't been allocated, this will lazily allocate it.
  580. /// </summary>
  581. /// <returns>The buffer.</returns>
  582. private byte[] GetBuffer()
  583. {
  584. Debug.Assert(_buffer == null || _buffer.Length == _bufferLength);
  585. if (_buffer == null)
  586. {
  587. _buffer = new byte[_bufferLength];
  588. OnBufferAllocated();
  589. }
  590. return _buffer;
  591. }
  592. partial void OnBufferAllocated();
  593. /// <summary>
  594. /// Flushes the internal read/write buffer for this stream. If write data has been buffered,
  595. /// that data is written out to the underlying file. Or if data has been buffered for
  596. /// reading from the stream, the data is dumped and our position in the underlying file
  597. /// is rewound as necessary. This does not flush the OS buffer.
  598. /// </summary>
  599. private void FlushInternalBuffer()
  600. {
  601. AssertBufferInvariants();
  602. if (_writePos > 0)
  603. {
  604. FlushWriteBuffer();
  605. }
  606. else if (_readPos < _readLength && CanSeek)
  607. {
  608. FlushReadBuffer();
  609. }
  610. }
  611. /// <summary>Dumps any read data in the buffer and rewinds our position in the stream, accordingly, as necessary.</summary>
  612. private void FlushReadBuffer()
  613. {
  614. // Reading is done by blocks from the file, but someone could read
  615. // 1 byte from the buffer then write. At that point, the OS's file
  616. // pointer is out of sync with the stream's position. All write
  617. // functions should call this function to preserve the position in the file.
  618. AssertBufferInvariants();
  619. Debug.Assert(_writePos == 0, "FileStream: Write buffer must be empty in FlushReadBuffer!");
  620. int rewind = _readPos - _readLength;
  621. if (rewind != 0)
  622. {
  623. Debug.Assert(CanSeek, "FileStream will lose buffered read data now.");
  624. SeekCore(_fileHandle, rewind, SeekOrigin.Current);
  625. }
  626. _readPos = _readLength = 0;
  627. }
  628. /// <summary>
  629. /// Reads a byte from the file stream. Returns the byte cast to an int
  630. /// or -1 if reading from the end of the stream.
  631. /// </summary>
  632. public override int ReadByte()
  633. {
  634. PrepareForReading();
  635. byte[] buffer = GetBuffer();
  636. if (_readPos == _readLength)
  637. {
  638. FlushWriteBuffer();
  639. _readLength = FillReadBufferForReadByte();
  640. _readPos = 0;
  641. if (_readLength == 0)
  642. {
  643. return -1;
  644. }
  645. }
  646. return buffer[_readPos++];
  647. }
  648. /// <summary>
  649. /// Writes a byte to the current position in the stream and advances the position
  650. /// within the stream by one byte.
  651. /// </summary>
  652. /// <param name="value">The byte to write to the stream.</param>
  653. public override void WriteByte(byte value)
  654. {
  655. PrepareForWriting();
  656. // Flush the write buffer if it's full
  657. if (_writePos == _bufferLength)
  658. FlushWriteBufferForWriteByte();
  659. // We now have space in the buffer. Store the byte.
  660. GetBuffer()[_writePos++] = value;
  661. }
  662. /// <summary>
  663. /// Validates that we're ready to write to the stream,
  664. /// including flushing a read buffer if necessary.
  665. /// </summary>
  666. private void PrepareForWriting()
  667. {
  668. if (_fileHandle.IsClosed)
  669. throw Error.GetFileNotOpen();
  670. // Make sure we're good to write. We only need to do this if there's nothing already
  671. // in our write buffer, since if there is something in the buffer, we've already done
  672. // this checking and flushing.
  673. if (_writePos == 0)
  674. {
  675. if (!CanWrite) throw Error.GetWriteNotSupported();
  676. FlushReadBuffer();
  677. Debug.Assert(_bufferLength > 0, "_bufferSize > 0");
  678. }
  679. }
  680. ~FileStream()
  681. {
  682. // Preserved for compatibility since FileStream has defined a
  683. // finalizer in past releases and derived classes may depend
  684. // on Dispose(false) call.
  685. Dispose(false);
  686. }
  687. public override IAsyncResult BeginRead(byte[] array, int offset, int numBytes, AsyncCallback callback, object state)
  688. {
  689. if (array == null)
  690. throw new ArgumentNullException(nameof(array));
  691. if (offset < 0)
  692. throw new ArgumentOutOfRangeException(nameof(offset), SR.ArgumentOutOfRange_NeedNonNegNum);
  693. if (numBytes < 0)
  694. throw new ArgumentOutOfRangeException(nameof(numBytes), SR.ArgumentOutOfRange_NeedNonNegNum);
  695. if (array.Length - offset < numBytes)
  696. throw new ArgumentException(SR.Argument_InvalidOffLen);
  697. if (IsClosed) throw new ObjectDisposedException(SR.ObjectDisposed_FileClosed);
  698. if (!CanRead) throw new NotSupportedException(SR.NotSupported_UnreadableStream);
  699. if (!IsAsync)
  700. return base.BeginRead(array, offset, numBytes, callback, state);
  701. else
  702. return TaskToApm.Begin(ReadAsyncTask(array, offset, numBytes, CancellationToken.None), callback, state);
  703. }
  704. public override IAsyncResult BeginWrite(byte[] array, int offset, int numBytes, AsyncCallback callback, object state)
  705. {
  706. if (array == null)
  707. throw new ArgumentNullException(nameof(array));
  708. if (offset < 0)
  709. throw new ArgumentOutOfRangeException(nameof(offset), SR.ArgumentOutOfRange_NeedNonNegNum);
  710. if (numBytes < 0)
  711. throw new ArgumentOutOfRangeException(nameof(numBytes), SR.ArgumentOutOfRange_NeedNonNegNum);
  712. if (array.Length - offset < numBytes)
  713. throw new ArgumentException(SR.Argument_InvalidOffLen);
  714. if (IsClosed) throw new ObjectDisposedException(SR.ObjectDisposed_FileClosed);
  715. if (!CanWrite) throw new NotSupportedException(SR.NotSupported_UnwritableStream);
  716. if (!IsAsync)
  717. return base.BeginWrite(array, offset, numBytes, callback, state);
  718. else
  719. return TaskToApm.Begin(WriteAsyncInternal(new ReadOnlyMemory<byte>(array, offset, numBytes), CancellationToken.None).AsTask(), callback, state);
  720. }
  721. public override int EndRead(IAsyncResult asyncResult)
  722. {
  723. if (asyncResult == null)
  724. throw new ArgumentNullException(nameof(asyncResult));
  725. if (!IsAsync)
  726. return base.EndRead(asyncResult);
  727. else
  728. return TaskToApm.End<int>(asyncResult);
  729. }
  730. public override void EndWrite(IAsyncResult asyncResult)
  731. {
  732. if (asyncResult == null)
  733. throw new ArgumentNullException(nameof(asyncResult));
  734. if (!IsAsync)
  735. base.EndWrite(asyncResult);
  736. else
  737. TaskToApm.End(asyncResult);
  738. }
  739. }
  740. }