LockUtils.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* Copyright (C) 2016 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  2. *
  3. * You can redistribute this program and/or modify it under the terms of
  4. * the GNU Lesser Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Text;
  10. using DiskAccessLibrary;
  11. using DiskAccessLibrary.Mod;
  12. namespace ISCSIConsole
  13. {
  14. public class LockUtils
  15. {
  16. public static void ReleaseDisks(List<Disk> disks)
  17. {
  18. foreach (Disk disk in disks)
  19. {
  20. ReleaseDisk(disk);
  21. }
  22. }
  23. public static void ReleaseDisk(Disk disk)
  24. {
  25. if (disk is DiskImage)
  26. {
  27. ((DiskImage)disk).ReleaseLock();
  28. }
  29. else if (disk is RAMDisk)
  30. {
  31. ((RAMDisk)disk).Free();
  32. }
  33. else if (disk is LargeRamDisk)
  34. {
  35. ((LargeRamDisk)disk).Free();
  36. }
  37. else if (disk is UnmanagedGigabyteBlockSeparatedRamDisk)
  38. {
  39. ((UnmanagedGigabyteBlockSeparatedRamDisk)disk).Free();
  40. }
  41. #if Win32
  42. else if (disk is PhysicalDisk)
  43. {
  44. if (!DiskAccessLibrary.LogicalDiskManager.DynamicDisk.IsDynamicDisk(disk))
  45. {
  46. LockHelper.UnlockBasicDiskAndVolumes((PhysicalDisk)disk);
  47. try
  48. {
  49. ((PhysicalDisk)disk).UpdateProperties();
  50. }
  51. catch (System.IO.IOException)
  52. {
  53. }
  54. }
  55. }
  56. else if (disk is VolumeDisk)
  57. {
  58. bool skippedLock = (Environment.OSVersion.Version.Major >= 6 && VolumeInfo.IsOffline(((VolumeDisk)disk).Volume));
  59. if (!skippedLock)
  60. {
  61. Guid? windowsVolumeGuid = WindowsVolumeHelper.GetWindowsVolumeGuid(((VolumeDisk)disk).Volume);
  62. if (windowsVolumeGuid.HasValue)
  63. {
  64. WindowsVolumeManager.ReleaseLock(windowsVolumeGuid.Value);
  65. }
  66. }
  67. }
  68. #endif
  69. }
  70. }
  71. }