PddInfo.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Text;
  4. using DiskAccessLibrary.Mod.Utility;
  5. namespace DiskAccessLibrary
  6. {
  7. public class PddInfo
  8. {
  9. public const int BlockIndexEntrySize = 8;
  10. public string SnapshotImagePath { get; }
  11. public long Length { get; }
  12. public int BlockSize { get; }
  13. public int NumberOfBlocks { get; }
  14. public long DataOffset { get; }
  15. public long EntryTableOffset { get; }
  16. public BlockEntryAllocateFlagIndexer Allocated { get; }
  17. public BlockEntryOffsetIndexer Offset { get; set; }
  18. public IReadOnlyList<ulong> Entries { get; }
  19. public PddInfo(string snapshotImagePath, bool readEntries = true)
  20. {
  21. SnapshotImagePath = snapshotImagePath;
  22. using var fs = new FileStream(snapshotImagePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
  23. using var reader = new BinaryReader(fs);
  24. BlockSize = reader.ReadInt32(); //4
  25. NumberOfBlocks = reader.ReadInt32(); //4
  26. var snapMatchLength = NumberOfBlocks * (long)BlockSize;
  27. Length = snapMatchLength;
  28. if (false == readEntries) return;
  29. EntryTableOffset = fs.Position;
  30. var arr = new ulong[NumberOfBlocks];
  31. for (var i = 0; i < NumberOfBlocks; i++)
  32. {
  33. arr[i] = reader.ReadUInt64();
  34. }
  35. Entries = arr;
  36. DataOffset = fs.Position;
  37. Allocated = new BlockEntryAllocateFlagIndexer(arr);
  38. Offset = new BlockEntryOffsetIndexer(arr);
  39. }
  40. public static int CalcHeaderSize(string basedImagePath, int blockSize, out int blocks)
  41. {
  42. var baseLength = new FileInfo(basedImagePath).Length;
  43. blocks = (int)(baseLength / blockSize);
  44. return 2 + Encoding.UTF8.GetByteCount(basedImagePath) + 8 + blocks * BlockIndexEntrySize;
  45. }
  46. public static int CalcHeaderSize(long baseLength, int blockSize, out int blocks)
  47. {
  48. blocks = (int)(baseLength / blockSize);
  49. return 2 + +8 + blocks * BlockIndexEntrySize;
  50. }
  51. }
  52. }