RetainHelper.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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.Text;
  10. using DiskAccessLibrary;
  11. using Utilities;
  12. namespace DiskAccessLibrary.LogicalDiskManager
  13. {
  14. public class RetainHelper
  15. {
  16. public static bool IsVolumeRetained(DynamicVolume volume, out bool isBootVolume)
  17. {
  18. if (volume is SimpleVolume)
  19. {
  20. return IsVolumeRetained((SimpleVolume)volume, out isBootVolume);
  21. }
  22. else if (volume is MirroredVolume)
  23. {
  24. DynamicVolume component = ((MirroredVolume)volume).Components[0];
  25. if (component is SimpleVolume)
  26. {
  27. return IsVolumeRetained((SimpleVolume)component, out isBootVolume);
  28. }
  29. }
  30. isBootVolume = false;
  31. return false;
  32. }
  33. public static bool IsVolumeRetained(SimpleVolume volume, out bool isBootVolume)
  34. {
  35. isBootVolume = false;
  36. DynamicDisk disk = DynamicDisk.ReadFromDisk(volume.Disk);
  37. long bootPartitionStartLBA;
  38. List<ExtentRecord> retained = GetRetainedExtentsOnDisk(disk, out bootPartitionStartLBA);
  39. foreach(ExtentRecord record in retained)
  40. {
  41. if (record.ExtentId == volume.DiskExtent.ExtentID)
  42. {
  43. if ((long)(disk.PrivateHeader.PublicRegionStartLBA + record.DiskOffsetLBA) == bootPartitionStartLBA)
  44. {
  45. isBootVolume = true;
  46. }
  47. return true;
  48. }
  49. }
  50. return false;
  51. }
  52. private static List<ExtentRecord> GetRetainedExtentsOnDisk(DynamicDisk disk, out long bootPartitionStartLBA)
  53. {
  54. VolumeManagerDatabase database = VolumeManagerDatabase.ReadFromDisk(disk);
  55. DiskRecord diskRecord = database.FindDiskByDiskGuid(disk.DiskGuid);
  56. bootPartitionStartLBA = -1;
  57. List<ExtentRecord> result = new List<ExtentRecord>();
  58. foreach (VolumeRecord volume in database.VolumeRecords)
  59. {
  60. if ((volume.VolumeFlags & VolumeFlags.RetainPartition) > 0)
  61. {
  62. List<ComponentRecord> components = database.FindComponentsByVolumeID(volume.VolumeId);
  63. foreach (ComponentRecord componentRecord in components)
  64. {
  65. if (componentRecord.ExtentLayout == ExtentLayoutName.Concatenated)
  66. {
  67. if (componentRecord.NumberOfExtents == 1)
  68. {
  69. List<ExtentRecord> extents = database.FindExtentsByComponentID(componentRecord.ComponentId);
  70. if (extents.Count == 1)
  71. {
  72. ExtentRecord extent = extents[0];
  73. if (extent.DiskId == diskRecord.DiskId)
  74. {
  75. result.Add(extent);
  76. if ((volume.VolumeFlags & VolumeFlags.BootVolume) > 0)
  77. {
  78. bootPartitionStartLBA = (long)(disk.PrivateHeader.PublicRegionStartLBA + extent.DiskOffsetLBA);
  79. }
  80. }
  81. }
  82. }
  83. }
  84. }
  85. }
  86. }
  87. return result;
  88. }
  89. }
  90. }