DiskImage.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 Utilities;
  12. namespace DiskAccessLibrary
  13. {
  14. public abstract partial class DiskImage : Disk
  15. {
  16. // There is no way to specify sector size for IMG/VHD/VMDK.
  17. public const int BytesPerDiskImageSector = 512;
  18. private string m_path;
  19. public DiskImage(string diskImagePath)
  20. {
  21. m_path = diskImagePath;
  22. }
  23. public void CheckBoundaries(long sectorIndex, int sectorCount)
  24. {
  25. if (sectorIndex < 0 || sectorIndex + (sectorCount - 1) >= this.TotalSectors)
  26. {
  27. throw new ArgumentOutOfRangeException("Attempted to access data outside of disk");
  28. }
  29. }
  30. public abstract void Extend(long additionalNumberOfBytes);
  31. public abstract bool ExclusiveLock();
  32. public abstract bool ReleaseLock();
  33. public override int BytesPerSector
  34. {
  35. get
  36. {
  37. return BytesPerDiskImageSector;
  38. }
  39. }
  40. public string Path
  41. {
  42. get
  43. {
  44. return m_path;
  45. }
  46. }
  47. public static DiskImage GetDiskImage(string path)
  48. {
  49. if (path.EndsWith(".vhd", StringComparison.InvariantCultureIgnoreCase))
  50. {
  51. return new VirtualHardDisk(path);
  52. }
  53. else if (path.EndsWith(".vmdk", StringComparison.InvariantCultureIgnoreCase))
  54. {
  55. return new VirtualMachineDisk(path);
  56. }
  57. else
  58. {
  59. return new RawDiskImage(path);
  60. }
  61. }
  62. }
  63. }