WindowsVolumeManager.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* Copyright (C) 2014 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.IO;
  10. using System.Text;
  11. using Microsoft.Win32.SafeHandles;
  12. using Utilities;
  13. using DiskAccessLibrary.LogicalDiskManager;
  14. namespace DiskAccessLibrary
  15. {
  16. public class WindowsVolumeManager
  17. {
  18. public static bool ExclusiveLock(Guid windowsVolumeGuid)
  19. {
  20. return ExclusiveLock(windowsVolumeGuid, FileAccess.ReadWrite);
  21. }
  22. /// <summary>
  23. /// Windows will flush all cached data to the volume before locking it.
  24. /// Note: we can only lock a dynamic volume if the disk is online and the volume is operational.
  25. /// </summary>
  26. /// <returns>True if a new lock has been obtained</returns>
  27. public static bool ExclusiveLock(Guid windowsVolumeGuid, FileAccess fileAccess)
  28. {
  29. bool newAllocation;
  30. // Windows Vista / 7: Valid handle cannot be obtained for a dynamic volume using ShareMode.ReadWrite.
  31. // The FSCTL_LOCK_VOLUME documentation demands ShareMode.ReadWrite, but ShareMode.None or ShareMode.Write will work too.
  32. SafeFileHandle handle = VolumeHandlePool.ObtainHandle(windowsVolumeGuid, fileAccess, ShareMode.None, out newAllocation);
  33. if (newAllocation)
  34. {
  35. if (!handle.IsInvalid)
  36. {
  37. bool success = VolumeUtils.LockVolume(handle);
  38. if (!success)
  39. {
  40. VolumeHandlePool.ReleaseHandle(windowsVolumeGuid);
  41. }
  42. return success;
  43. }
  44. else
  45. {
  46. VolumeHandlePool.ReleaseHandle(windowsVolumeGuid);
  47. return false;
  48. }
  49. }
  50. else
  51. {
  52. return false;
  53. }
  54. }
  55. /// <summary>
  56. /// Note that the volume will be remounted again on first access.
  57. /// </summary>
  58. public static bool DismountVolume(Guid windowsVolumeGuid)
  59. {
  60. bool releaseHandle;
  61. SafeFileHandle handle = VolumeHandlePool.ObtainHandle(windowsVolumeGuid, FileAccess.ReadWrite, ShareMode.ReadWrite, out releaseHandle);
  62. bool success = false;
  63. if (!handle.IsInvalid)
  64. {
  65. success = VolumeUtils.DismountVolume(handle);
  66. }
  67. if (releaseHandle) // new allocation
  68. {
  69. VolumeHandlePool.ReleaseHandle(windowsVolumeGuid);
  70. }
  71. return success;
  72. }
  73. public static bool ReleaseLock(Guid windowsVolumeGuid)
  74. {
  75. return VolumeHandlePool.ReleaseHandle(windowsVolumeGuid);
  76. }
  77. /// <summary>
  78. /// If Windows Automount is turned off, then partitions on new disks are not mounted. (but can be locked).
  79. /// Failed dynamic volumes are not mounted as well. (and can't be locked).
  80. /// Note: A volume can have 0 mount points and still be mounted and access by Windows.
  81. /// </summary>
  82. public static bool IsMounted(Guid windowsVolumeGuid)
  83. {
  84. return VolumeUtils.IsVolumeMounted(windowsVolumeGuid);
  85. }
  86. public static List<string> GetMountPoints(Guid windowsVolumeGuid)
  87. {
  88. return VolumeUtils.GetVolumeMountPoints(windowsVolumeGuid);
  89. }
  90. /// <summary>
  91. /// A volume can be mounted by the OS even if it has no mount points
  92. /// </summary>
  93. public static bool HasMountPoints(Guid windowsVolumeGuid)
  94. {
  95. return (GetMountPoints(windowsVolumeGuid).Count > 0);
  96. }
  97. }
  98. }