Remove old Interop.Kernel32.GetVolumeInformation overload (dotnet/corefx#31883)
authorMarco Rossignoli <marco.rossignoli@gmail.com>
Wed, 22 Aug 2018 16:32:57 +0000 (18:32 +0200)
committerDan Moseley <danmose@microsoft.com>
Wed, 22 Aug 2018 16:32:57 +0000 (09:32 -0700)
* Remove old Interop.Kernel32.GetVolumeInformation overload

* address PR feedback

* fix my presumption

Commit migrated from https://github.com/dotnet/corefx/commit/a032087727fd3f0a72643febea1eb406e8a4c757

src/libraries/Common/src/Interop/Windows/kernel32/Interop.GetVolumeInformation.cs
src/libraries/System.IO.FileSystem.DriveInfo/src/System.IO.FileSystem.DriveInfo.csproj
src/libraries/System.IO.FileSystem.DriveInfo/src/System/IO/DriveInfo.Windows.cs

index 324cdc6..70d83e6 100644 (file)
@@ -10,9 +10,6 @@ internal partial class Interop
     internal partial class Kernel32
     {
         [DllImport(Libraries.Kernel32, EntryPoint = "GetVolumeInformationW", CharSet = CharSet.Unicode, SetLastError = true, BestFitMapping = false)]
-        internal static extern bool GetVolumeInformation(string drive, [Out]StringBuilder volumeName, int volumeNameBufLen, out int volSerialNumber, out int maxFileNameLen, out int fileSystemFlags, [Out]StringBuilder fileSystemName, int fileSystemNameBufLen);
-
-        [DllImport(Libraries.Kernel32, EntryPoint = "GetVolumeInformationW", CharSet = CharSet.Unicode, SetLastError = true, BestFitMapping = false)]
         internal static unsafe extern bool GetVolumeInformation(string drive, char* volumeName, int volumeNameBufLen, int* volSerialNumber, int* maxFileNameLen, out int fileSystemFlags, char* fileSystemName, int fileSystemNameBufLen);
 
         internal const uint FILE_SUPPORTS_ENCRYPTION = 0x00020000;
index 0501281..e7f1042 100644 (file)
@@ -1,4 +1,4 @@
-<Project Sdk="Microsoft.NET.Sdk">
+<Project Sdk="Microsoft.NET.Sdk">
   <PropertyGroup>
     <RootNamespace>System.IO.FileSystem.DriveInfo</RootNamespace>
     <AssemblyName>System.IO.FileSystem.DriveInfo</AssemblyName>
@@ -35,6 +35,9 @@
     <Compile Include="$(CommonPath)\Interop\Windows\kernel32\Interop.GetDiskFreeSpaceEx.cs">
       <Link>Common\Interop\Windows\Interop.GetDiskFreeSpaceEx.cs</Link>
     </Compile>
+    <Compile Include="$(CommonPath)\Interop\Windows\kernel32\Interop.MaxLengths.cs">
+      <Link>Common\Interop\Windows\Interop.MaxLengths.cs</Link>
+    </Compile>
     <Compile Include="$(CommonPath)\Interop\Windows\kernel32\Interop.SetVolumeLabel.cs">
       <Link>Common\Interop\Windows\Interop.SetVolumeLabel.cs</Link>
     </Compile>
@@ -53,6 +56,9 @@
     <Compile Include="$(CommonPath)\CoreLib\Interop\Windows\Kernel32\Interop.FormatMessage.cs">
       <Link>Common\Interop\Windows\Interop.FormatMessage.cs</Link>
     </Compile>
+    <Compile Include="$(CommonPath)\CoreLib\System\IO\DisableMediaInsertionPrompt.cs">
+      <Link>Common\System\IO\DisableMediaInsertionPrompt.cs</Link>
+    </Compile>
     <Compile Include="$(CommonPath)\CoreLib\System\IO\Win32Marshal.cs">
       <Link>Common\CoreLib\System\IO\Win32Marshal.cs</Link>
     </Compile>
index 1b67c1e..412959a 100644 (file)
@@ -25,32 +25,20 @@ namespace System.IO
             }
         }
 
-        public string DriveFormat
+        public unsafe string DriveFormat
         {
             get
             {
-                const int volNameLen = 50;
-                StringBuilder volumeName = new StringBuilder(volNameLen);
-                const int fileSystemNameLen = 50;
-                StringBuilder fileSystemName = new StringBuilder(fileSystemNameLen);
-                int serialNumber, maxFileNameLen, fileSystemFlags;
+                char* fileSystemName = stackalloc char[Interop.Kernel32.MAX_PATH + 1];
 
-                uint oldMode;
-                bool success = Interop.Kernel32.SetThreadErrorMode(Interop.Kernel32.SEM_FAILCRITICALERRORS, out oldMode);
-                try
+                using (DisableMediaInsertionPrompt.Create())
                 {
-                    bool r = Interop.Kernel32.GetVolumeInformation(Name, volumeName, volNameLen, out serialNumber, out maxFileNameLen, out fileSystemFlags, fileSystemName, fileSystemNameLen);
-                    if (!r)
+                    if (!Interop.Kernel32.GetVolumeInformation(Name, null, 0, null, null, out int fileSystemFlags, fileSystemName, Interop.Kernel32.MAX_PATH + 1))
                     {
                         throw Error.GetExceptionForLastWin32DriveError(Name);
                     }
                 }
-                finally
-                {
-                    if (success)
-                        Interop.Kernel32.SetThreadErrorMode(oldMode, out oldMode);
-                }
-                return fileSystemName.ToString();
+                return new string(fileSystemName);
             }
         }
 
@@ -133,39 +121,21 @@ namespace System.IO
         }
 
         // Null is a valid volume label.
-        public string VolumeLabel
+        public unsafe string VolumeLabel
         {
             get
             {
-                // NTFS uses a limit of 32 characters for the volume label,
-                // as of Windows Server 2003.
-                const int volNameLen = 50;
-                StringBuilder volumeName = new StringBuilder(volNameLen);
-                const int fileSystemNameLen = 50;
-                StringBuilder fileSystemName = new StringBuilder(fileSystemNameLen);
-                int serialNumber, maxFileNameLen, fileSystemFlags;
+                char* volumeName = stackalloc char[Interop.Kernel32.MAX_PATH + 1];
 
-                uint oldMode;
-                bool success = Interop.Kernel32.SetThreadErrorMode(Interop.Kernel32.SEM_FAILCRITICALERRORS, out oldMode);
-                try
+                using (DisableMediaInsertionPrompt.Create())
                 {
-                    bool r = Interop.Kernel32.GetVolumeInformation(Name, volumeName, volNameLen, out serialNumber, out maxFileNameLen, out fileSystemFlags, fileSystemName, fileSystemNameLen);
-                    if (!r)
+                    if (!Interop.Kernel32.GetVolumeInformation(Name, volumeName, Interop.Kernel32.MAX_PATH + 1, null, null, out int fileSystemFlags, null, 0))
                     {
-                        int errorCode = Marshal.GetLastWin32Error();
-                        // Win9x appears to return ERROR_INVALID_DATA when a
-                        // drive doesn't exist.
-                        if (errorCode == Interop.Errors.ERROR_INVALID_DATA)
-                            errorCode = Interop.Errors.ERROR_INVALID_DRIVE;
-                        throw Error.GetExceptionForWin32DriveError(errorCode, Name);
+                        throw Error.GetExceptionForLastWin32DriveError(Name);
                     }
                 }
-                finally
-                {
-                    if (success)
-                        Interop.Kernel32.SetThreadErrorMode(oldMode, out oldMode);
-                }
-                return volumeName.ToString();
+
+                return new string(volumeName);
             }
             set
             {