DiskRecord.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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.LogicalDiskManager
  12. {
  13. public enum DiskFlags : uint
  14. {
  15. Reserved = 0x01,
  16. Spare = 0x02,
  17. Failing = 0x04,
  18. EncapPending = 0x08,
  19. MergeRequired = 0x0010,
  20. Removed = 0x0100,
  21. Detached = 0x0200,
  22. }
  23. public class DiskRecord : DatabaseRecord
  24. {
  25. public Guid DiskGuid;
  26. public string LastDeviceName = String.Empty;
  27. public DiskFlags DiskFlags;
  28. public ulong CommitTransactionID;
  29. public Guid AltGuidRev4; // no clue what this is
  30. public DiskRecord()
  31. {
  32. this.RecordRevision = 3;
  33. this.RecordType = RecordType.Disk;
  34. }
  35. public DiskRecord(List<DatabaseRecordFragment> fragments) : base(fragments)
  36. {
  37. // Data begins at 0x10 (VBLK header is at 0x00)
  38. int offset = 0x00; // relative to Data
  39. ReadCommonFields(this.Data, ref offset);
  40. if (RecordRevision == 3)
  41. {
  42. string diskGuidString = ReadVarString(this.Data, ref offset);
  43. DiskGuid = new Guid(diskGuidString);
  44. }
  45. else if (RecordRevision == 4)
  46. {
  47. DiskGuid = BigEndianReader.ReadGuidBytes(this.Data, ref offset);
  48. AltGuidRev4 = BigEndianReader.ReadGuidBytes(this.Data, ref offset);
  49. }
  50. else
  51. {
  52. throw new NotImplementedException("Unsupported record revision");
  53. }
  54. LastDeviceName = ReadVarString(this.Data, ref offset);
  55. DiskFlags = (DiskFlags)BigEndianReader.ReadUInt32(this.Data, ref offset);
  56. CommitTransactionID = BigEndianReader.ReadUInt64(this.Data, ref offset);
  57. }
  58. public override byte[] GetDataBytes()
  59. {
  60. int dataLength = 0;
  61. dataLength += VarULongSize(DiskId);
  62. dataLength += Name.Length + 1;
  63. if (RecordRevision == 3)
  64. {
  65. dataLength += 20; // fixed length components
  66. dataLength += DiskGuid.ToString().Length + 1;
  67. dataLength += LastDeviceName.Length + 1;
  68. }
  69. else // RecordRevision == 4
  70. {
  71. dataLength += 53; // fixed length components
  72. }
  73. byte[] data = new byte[dataLength];
  74. int offset = 0x00;
  75. WriteCommonFields(data, ref offset);
  76. if (RecordRevision == 3)
  77. {
  78. WriteVarString(data, ref offset, DiskGuid.ToString());
  79. }
  80. else // RecordRevision == 4
  81. {
  82. BigEndianWriter.WriteGuidBytes(data, ref offset, DiskGuid);
  83. BigEndianWriter.WriteGuidBytes(data, ref offset, AltGuidRev4);
  84. }
  85. WriteVarString(data, ref offset, LastDeviceName);
  86. BigEndianWriter.WriteUInt32(data, ref offset, (uint)DiskFlags);
  87. BigEndianWriter.WriteUInt64(data, ref offset, CommitTransactionID);
  88. return data;
  89. }
  90. public ulong DiskId
  91. {
  92. get
  93. {
  94. return this.Id;
  95. }
  96. }
  97. public string DiskGuidString
  98. {
  99. get
  100. {
  101. return DiskGuid.ToString();
  102. }
  103. }
  104. }
  105. }