allow FileStream to open any types of files from path (#54676)
authorAdam Sitnik <adam.sitnik@gmail.com>
Fri, 9 Jul 2021 19:01:38 +0000 (21:01 +0200)
committerGitHub <noreply@github.com>
Fri, 9 Jul 2021 19:01:38 +0000 (21:01 +0200)
src/libraries/System.IO.FileSystem/tests/FileStream/FileStreamConformanceTests.Windows.cs
src/libraries/System.Private.CoreLib/src/Resources/Strings.resx
src/libraries/System.Private.CoreLib/src/System/IO/Strategies/FileStreamHelpers.Unix.cs
src/libraries/System.Private.CoreLib/src/System/IO/Strategies/FileStreamHelpers.Windows.cs
src/libraries/System.Private.CoreLib/src/System/IO/Strategies/Net5CompatFileStreamStrategy.Windows.cs
src/libraries/System.Private.CoreLib/src/System/IO/Strategies/OSFileStreamStrategy.cs

index a372213550fa8143398e70c8a1b3c3ca2da66e54..b429317ef8d7220a2968a06d1efedebf3a46e4f5 100644 (file)
@@ -23,7 +23,7 @@ namespace System.IO.Tests
             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();
 
index ce9ef00df0373f5e7a1baba187dea94bd8c1a75c..23a96351e049c238a572dada507eb47842323198 100644 (file)
   <data name="NotSupported_DynamicModule" xml:space="preserve">
     <value>The invoked member is not supported in a dynamic module.</value>
   </data>
-  <data name="NotSupported_FileStreamOnNonFiles" xml:space="preserve">
-    <value>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.</value>
-  </data>
   <data name="NotSupported_FixedSizeCollection" xml:space="preserve">
     <value>Collection was of a fixed size.</value>
   </data>
index 1d508870956788dd8a207c93945782b878a9e73b..edf3c003fac32681e715f99c337a1fc040201704 100644 (file)
@@ -28,8 +28,6 @@ namespace System.IO.Strategies
             return result;
         }
 
-        internal static void ValidateFileTypeForNonExtendedPaths(SafeFileHandle handle, string originalPath) { /* nop */ }
-
         internal static long Seek(SafeFileHandle handle, long offset, SeekOrigin origin, bool closeInvalidHandle = false) =>
             CheckFileCall(Interop.Sys.LSeek(handle, offset, (Interop.Sys.SeekWhence)(int)origin), handle.Path); // SeekOrigin values are the same as Interop.libc.SeekWhence values
 
index 3f29ed92b431f6ef700f7ebeebe6da058648b4a0..af4dc71000afbff3406c2a9a57936a43161684ed 100644 (file)
@@ -122,32 +122,6 @@ namespace System.IO.Strategies
             }
         }
 
-        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, long length)
         {
             var eofInfo = new Interop.Kernel32.FILE_END_OF_FILE_INFO
@@ -168,14 +142,12 @@ namespace System.IO.Strategies
             }
         }
 
-
         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
index e9e4e640a0a33deb185c209ac94d79a2efe9aa10..a2e7666114524e97f6124c161f79540d50e2b58a 100644 (file)
@@ -47,8 +47,6 @@ namespace System.IO.Strategies
 
         private void Init(FileMode mode, string originalPath, FileOptions options)
         {
-            FileStreamHelpers.ValidateFileTypeForNonExtendedPaths(_fileHandle, originalPath);
-
             Debug.Assert(!_useAsyncIO || _fileHandle.ThreadPoolBinding != null);
 
             // For Append mode...
index 2be66fffde521edda43d8c691949b46b32927f96..b49b8ce2f33f8d5faf4465a7685380960be36545 100644 (file)
@@ -53,8 +53,6 @@ namespace System.IO.Strategies
 
             try
             {
-                FileStreamHelpers.ValidateFileTypeForNonExtendedPaths(_fileHandle, path);
-
                 if (mode == FileMode.Append && CanSeek)
                 {
                     _appendStart = _filePosition = Length;