12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- using System.Collections.Generic;
- using System.IO;
- using System.Text;
- using DiskAccessLibrary.Mod.Utility;
- namespace DiskAccessLibrary
- {
- public class PddInfo
- {
- public const int BlockIndexEntrySize = 8;
- public string SnapshotImagePath { get; }
- public long Length { get; }
- public int BlockSize { get; }
- public int NumberOfBlocks { get; }
- public long DataOffset { get; }
- public long EntryTableOffset { get; }
- public BlockEntryAllocateFlagIndexer Allocated { get; }
- public BlockEntryOffsetIndexer Offset { get; set; }
- public IReadOnlyList<ulong> Entries { get; }
- public PddInfo(string snapshotImagePath, bool readEntries = true)
- {
- SnapshotImagePath = snapshotImagePath;
- using var fs = new FileStream(snapshotImagePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
- using var reader = new BinaryReader(fs);
- BlockSize = reader.ReadInt32(); //4
- NumberOfBlocks = reader.ReadInt32(); //4
- var snapMatchLength = NumberOfBlocks * (long)BlockSize;
- Length = snapMatchLength;
- if (false == readEntries) return;
- EntryTableOffset = fs.Position;
- var arr = new ulong[NumberOfBlocks];
- for (var i = 0; i < NumberOfBlocks; i++)
- {
- arr[i] = reader.ReadUInt64();
- }
- Entries = arr;
- DataOffset = fs.Position;
- Allocated = new BlockEntryAllocateFlagIndexer(arr);
- Offset = new BlockEntryOffsetIndexer(arr);
- }
- public static int CalcHeaderSize(string basedImagePath, int blockSize, out int blocks)
- {
- var baseLength = new FileInfo(basedImagePath).Length;
- blocks = (int)(baseLength / blockSize);
- return 2 + Encoding.UTF8.GetByteCount(basedImagePath) + 8 + blocks * BlockIndexEntrySize;
- }
- public static int CalcHeaderSize(long baseLength, int blockSize, out int blocks)
- {
- blocks = (int)(baseLength / blockSize);
- return 2 + +8 + blocks * BlockIndexEntrySize;
- }
- }
- }
|