DynamicDiskExtentHelper.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /* Copyright (C) 2014-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 Utilities;
  11. namespace DiskAccessLibrary.LogicalDiskManager
  12. {
  13. public class DynamicDiskExtentHelper
  14. {
  15. public static int GetIndexOfExtentID(List<DynamicDiskExtent> extents, ulong extentID)
  16. {
  17. for (int index = 0; index < extents.Count; index++)
  18. {
  19. if (extents[index].ExtentID == extentID)
  20. {
  21. return index;
  22. }
  23. }
  24. return -1;
  25. }
  26. public static DynamicDiskExtent GetByExtentID(List<DynamicDiskExtent> extents, ulong extentID)
  27. {
  28. int index = GetIndexOfExtentID(extents, extentID);
  29. if (index >= 0)
  30. {
  31. return extents[index];
  32. }
  33. else
  34. {
  35. return null;
  36. }
  37. }
  38. /// <summary>
  39. /// Support null disks
  40. /// </summary>
  41. public static DynamicDiskExtent GetDiskExtent(DynamicDisk dynamicDisk, ExtentRecord extentRecord)
  42. {
  43. long extentStartSector = GetExtentStartSector(dynamicDisk, extentRecord);
  44. long extentSize = (long)extentRecord.SizeLBA * PublicRegionHelper.BytesPerPublicRegionSector;
  45. Disk disk = null;
  46. Guid diskGuid = Guid.Empty;
  47. if (dynamicDisk != null)
  48. {
  49. disk = dynamicDisk.Disk;
  50. diskGuid = dynamicDisk.DiskGuid;
  51. }
  52. DynamicDiskExtent extent = new DynamicDiskExtent(disk, extentStartSector, extentSize, extentRecord.ExtentId);
  53. extent.Name = extentRecord.Name;
  54. extent.DiskGuid = diskGuid;
  55. return extent;
  56. }
  57. /// <summary>
  58. /// Support null disks
  59. /// </summary>
  60. public static long GetExtentStartSector(DynamicDisk disk, ExtentRecord extentRecord)
  61. {
  62. long publicRegionStartLBA = 0;
  63. int bytesPerDiskSector = DynamicColumn.DefaultBytesPerSector; // default for missing disks
  64. if (disk != null)
  65. {
  66. bytesPerDiskSector = disk.BytesPerSector;
  67. PrivateHeader privateHeader = disk.PrivateHeader;
  68. publicRegionStartLBA = (long)privateHeader.PublicRegionStartLBA;
  69. }
  70. return PublicRegionHelper.TranslateFromPublicRegionLBA((long)extentRecord.DiskOffsetLBA, publicRegionStartLBA, bytesPerDiskSector);
  71. }
  72. }
  73. }