Jelajahi Sumber

Improvements in VHD creation mechanism

Tal Aloni 8 tahun lalu
induk
melakukan
4d8064fbb6

+ 18 - 7
DiskAccessLibrary/Disks/VHD/VirtualHardDisk.cs

@@ -241,17 +241,15 @@ namespace DiskAccessLibrary
         /// <exception cref="System.UnauthorizedAccessException"></exception>
         public static VirtualHardDisk Create(string path, long length)
         {
+#if Win32
+            // calling AdjustTokenPrivileges and then immediately calling SetFileValidData will sometimes result in ERROR_PRIVILEGE_NOT_HELD.
+            // We can work around the issue by obtaining the privilege before obtaining the handle.
+            bool hasManageVolumePrivilege = SecurityUtils.ObtainManageVolumePrivilege();
+#endif
             FileStream stream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None);
             try
             {
                 stream.SetLength(length + 512); // VHD footer is 512 bytes
-#if Win32
-                bool hasManageVolumePrivilege = SecurityUtils.ObtainManageVolumePrivilege();
-                if (hasManageVolumePrivilege)
-                {
-                    FileStreamUtils.SetValidLength(stream, length + 512);
-                }
-#endif
             }
             catch (IOException)
             {
@@ -267,6 +265,19 @@ namespace DiskAccessLibrary
                 throw;
             }
 
+#if Win32
+            if (hasManageVolumePrivilege)
+            {
+                try
+                {
+                    FileStreamUtils.SetValidLength(stream, length + 512);
+                }
+                catch (IOException)
+                {
+                }
+            }
+#endif
+
             VHDFooter footer = new VHDFooter();
             footer.OriginalSize = (ulong)length;
             footer.CurrentSize = (ulong)length;

+ 2 - 2
ISCSIConsole/Program.CreateCommand.cs

@@ -75,9 +75,9 @@ namespace ISCSIConsole
                     m_selectedDisk = VirtualHardDisk.Create(path, sizeInBytes);
                     Console.WriteLine("The virtual disk file was created successfully.");
                 }
-                catch (IOException)
+                catch (IOException ex)
                 {
-                    Console.WriteLine("Error: Could not write the virtual disk file.");
+                    Console.WriteLine("Error: Could not write the virtual disk file. {0}", ex.Message);
                     return;
                 }
                 catch (UnauthorizedAccessException)