VirtualMachineDiskExtentEntry.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 Utilities;
  11. namespace DiskAccessLibrary.VMDK
  12. {
  13. public class VirtualMachineDiskExtentEntry
  14. {
  15. public bool ReadAccess;
  16. public bool WriteAccess;
  17. public long SizeInSectors;
  18. public ExtentType ExtentType;
  19. public string FileName;
  20. public Nullable<long> Offset;
  21. public string GetEntryLine()
  22. {
  23. string access;
  24. if (ReadAccess && WriteAccess)
  25. {
  26. access = "RW";
  27. }
  28. else if (ReadAccess)
  29. {
  30. access = "RDONLY";
  31. }
  32. else
  33. {
  34. access = "NOACCESS";
  35. }
  36. string line = String.Format("{0} {1} {2} \"{3}\"", access, SizeInSectors, ExtentType.ToString().ToUpper(), FileName);
  37. if (Offset.HasValue)
  38. {
  39. line += " " + Offset.Value.ToString();
  40. }
  41. return line;
  42. }
  43. public static VirtualMachineDiskExtentEntry ParseEntry(string line)
  44. {
  45. VirtualMachineDiskExtentEntry entry = new VirtualMachineDiskExtentEntry();
  46. List<string> parts = QuotedStringUtils.SplitIgnoreQuotedSeparators(line, ' ');
  47. if (String.Equals(parts[0], "RW", StringComparison.InvariantCultureIgnoreCase))
  48. {
  49. entry.WriteAccess = true;
  50. entry.ReadAccess = true;
  51. }
  52. else if (String.Equals(parts[0], "RDONLY", StringComparison.InvariantCultureIgnoreCase))
  53. {
  54. entry.ReadAccess = true;
  55. }
  56. entry.SizeInSectors = Conversion.ToInt64(parts[1]);
  57. entry.ExtentType = GetExtentTypeFromString(parts[2]);
  58. entry.FileName = QuotedStringUtils.Unquote(parts[3]);
  59. if (parts.Count > 4)
  60. {
  61. entry.Offset = Conversion.ToInt64(parts[4]);
  62. }
  63. return entry;
  64. }
  65. public static ExtentType GetExtentTypeFromString(string extentType)
  66. {
  67. switch (extentType.ToUpper())
  68. {
  69. case "FLAT":
  70. return ExtentType.Flat;
  71. case "SPARSE":
  72. return ExtentType.Sparse;
  73. case "ZERO":
  74. return ExtentType.Zero;
  75. case "VMFS":
  76. return ExtentType.VMFS;
  77. case "VMFSSPARSE":
  78. return ExtentType.VMFSSparse;
  79. case "VMFSRDM":
  80. return ExtentType.VMFSRDM;
  81. case "VMFSRAW":
  82. return ExtentType.VMFSRaw;
  83. default:
  84. return ExtentType.Zero;
  85. }
  86. }
  87. }
  88. }