DiskImage.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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.IO;
  10. using System.Text;
  11. using Utilities;
  12. namespace DiskAccessLibrary
  13. {
  14. public abstract partial class DiskImage : Disk
  15. {
  16. private string m_path;
  17. public DiskImage(string diskImagePath)
  18. {
  19. m_path = diskImagePath;
  20. }
  21. public void CheckBoundaries(long sectorIndex, int sectorCount)
  22. {
  23. if (sectorIndex < 0 || sectorIndex + (sectorCount - 1) >= this.TotalSectors)
  24. {
  25. throw new ArgumentOutOfRangeException("Attempted to access data outside of disk");
  26. }
  27. }
  28. public abstract void Extend(long numberOfAdditionalBytes);
  29. public abstract bool ExclusiveLock();
  30. public abstract bool ReleaseLock();
  31. public string Path
  32. {
  33. get
  34. {
  35. return m_path;
  36. }
  37. }
  38. /// <exception cref="System.IO.IOException"></exception>
  39. /// <exception cref="System.IO.InvalidDataException"></exception>
  40. /// <exception cref="System.NotImplementedException"></exception>
  41. /// <exception cref="System.UnauthorizedAccessException"></exception>
  42. public static DiskImage GetDiskImage(string path)
  43. {
  44. if (path.EndsWith(".vhd", StringComparison.InvariantCultureIgnoreCase))
  45. {
  46. return new VirtualHardDisk(path);
  47. }
  48. else if (path.EndsWith(".vmdk", StringComparison.InvariantCultureIgnoreCase))
  49. {
  50. return new VirtualMachineDisk(path);
  51. }
  52. else
  53. {
  54. return new RawDiskImage(path);
  55. }
  56. }
  57. }
  58. }