From e7a8b6c4ea7e11d98b76de56c917ab83cc33f5a3 Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Thu, 24 Jun 2021 12:04:53 +0200 Subject: [PATCH 1/2] allow FileStream to open any types of files from path --- .../FileStreamConformanceTests.Windows.cs | 2 +- .../Strategies/FileStreamHelpers.Windows.cs | 28 ------------------- .../Net5CompatFileStreamStrategy.Windows.cs | 2 -- .../Strategies/WindowsFileStreamStrategy.cs | 6 ++-- 4 files changed, 3 insertions(+), 35 deletions(-) diff --git a/src/libraries/System.IO.FileSystem/tests/FileStream/FileStreamConformanceTests.Windows.cs b/src/libraries/System.IO.FileSystem/tests/FileStream/FileStreamConformanceTests.Windows.cs index 1cfbd351824de..4933fc7db2405 100644 --- a/src/libraries/System.IO.FileSystem/tests/FileStream/FileStreamConformanceTests.Windows.cs +++ b/src/libraries/System.IO.FileSystem/tests/FileStream/FileStreamConformanceTests.Windows.cs @@ -23,7 +23,7 @@ protected override async Task CreateConnectedStreamsAsync() string pipePath = Path.GetFullPath($@"\\.\pipe\{pipeName}"); var server = new NamedPipeServerStream(pipeName, PipeDirection.In); - var clienStream = new FileStream(File.OpenHandle(pipePath, FileMode.Open, FileAccess.Write, FileShare.None), FileAccess.Write); + var clienStream = new FileStream(pipePath, FileMode.Open, FileAccess.Write, FileShare.None); await server.WaitForConnectionAsync(); diff --git a/src/libraries/System.Private.CoreLib/src/System/IO/Strategies/FileStreamHelpers.Windows.cs b/src/libraries/System.Private.CoreLib/src/System/IO/Strategies/FileStreamHelpers.Windows.cs index 39275721010fb..69df5e16ae0e5 100644 --- a/src/libraries/System.Private.CoreLib/src/System/IO/Strategies/FileStreamHelpers.Windows.cs +++ b/src/libraries/System.Private.CoreLib/src/System/IO/Strategies/FileStreamHelpers.Windows.cs @@ -142,32 +142,6 @@ internal static void Unlock(SafeFileHandle handle, string? path, long position, } } - internal static void ValidateFileTypeForNonExtendedPaths(SafeFileHandle handle, string originalPath) - { - if (!PathInternal.IsExtended(originalPath)) - { - // To help avoid stumbling into opening COM/LPT ports by accident, we will block on non file handles unless - // we were explicitly passed a path that has \\?\. GetFullPath() will turn paths like C:\foo\con.txt into - // \\.\CON, so we'll only allow the \\?\ syntax. - - int fileType = handle.GetFileType(); - if (fileType != Interop.Kernel32.FileTypes.FILE_TYPE_DISK) - { - int errorCode = fileType == Interop.Kernel32.FileTypes.FILE_TYPE_UNKNOWN - ? Marshal.GetLastPInvokeError() - : Interop.Errors.ERROR_SUCCESS; - - handle.Dispose(); - - if (errorCode != Interop.Errors.ERROR_SUCCESS) - { - throw Win32Marshal.GetExceptionForWin32Error(errorCode); - } - throw new NotSupportedException(SR.NotSupported_FileStreamOnNonFiles); - } - } - } - internal static unsafe void SetFileLength(SafeFileHandle handle, string? path, long length) { var eofInfo = new Interop.Kernel32.FILE_END_OF_FILE_INFO @@ -188,8 +162,6 @@ internal static unsafe void SetFileLength(SafeFileHandle handle, string? path, l } } - - internal static async Task AsyncModeCopyToAsync(SafeFileHandle handle, string? path, 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. diff --git a/src/libraries/System.Private.CoreLib/src/System/IO/Strategies/Net5CompatFileStreamStrategy.Windows.cs b/src/libraries/System.Private.CoreLib/src/System/IO/Strategies/Net5CompatFileStreamStrategy.Windows.cs index ec74ccf1ac497..8fdfdb80f997d 100644 --- a/src/libraries/System.Private.CoreLib/src/System/IO/Strategies/Net5CompatFileStreamStrategy.Windows.cs +++ b/src/libraries/System.Private.CoreLib/src/System/IO/Strategies/Net5CompatFileStreamStrategy.Windows.cs @@ -46,8 +46,6 @@ internal sealed partial class Net5CompatFileStreamStrategy : FileStreamStrategy private void Init(FileMode mode, string originalPath, FileOptions options) { - FileStreamHelpers.ValidateFileTypeForNonExtendedPaths(_fileHandle, originalPath); - Debug.Assert(!_useAsyncIO || _fileHandle.ThreadPoolBinding != null); // For Append mode... diff --git a/src/libraries/System.Private.CoreLib/src/System/IO/Strategies/WindowsFileStreamStrategy.cs b/src/libraries/System.Private.CoreLib/src/System/IO/Strategies/WindowsFileStreamStrategy.cs index dee1c5a954857..9d69e52ee4ef5 100644 --- a/src/libraries/System.Private.CoreLib/src/System/IO/Strategies/WindowsFileStreamStrategy.cs +++ b/src/libraries/System.Private.CoreLib/src/System/IO/Strategies/WindowsFileStreamStrategy.cs @@ -54,7 +54,7 @@ internal WindowsFileStreamStrategy(string path, FileMode mode, FileAccess access try { - Init(mode, path); + Init(mode); } catch { @@ -230,10 +230,8 @@ public sealed override long Seek(long offset, SeekOrigin origin) internal sealed override void Unlock(long position, long length) => FileStreamHelpers.Unlock(_fileHandle, _path, position, length); - private void Init(FileMode mode, string originalPath) + private void Init(FileMode mode) { - FileStreamHelpers.ValidateFileTypeForNonExtendedPaths(_fileHandle, originalPath); - // For Append mode... if (mode == FileMode.Append) { From 2bca9837cea3dd0b7ba2575b0b887a894f37cbcc Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Fri, 9 Jul 2021 18:25:40 +0200 Subject: [PATCH 2/2] remove the unused ex message --- .../System.Private.CoreLib/src/Resources/Strings.resx | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/Resources/Strings.resx b/src/libraries/System.Private.CoreLib/src/Resources/Strings.resx index ce9ef00df0373..23a96351e049c 100644 --- a/src/libraries/System.Private.CoreLib/src/Resources/Strings.resx +++ b/src/libraries/System.Private.CoreLib/src/Resources/Strings.resx @@ -2845,9 +2845,6 @@ The invoked member is not supported in a dynamic module. - - FileStream was asked to open a device that was not a file. For support for devices like 'com1:' or 'lpt1:', call CreateFile, then use the FileStream constructors that take an OS handle as an IntPtr. - Collection was of a fixed size.