123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- using System;
- using System.IO;
- using System.Security.Authentication;
- using System.Text;
- using DiskAccessLibrary.FileSystems;
- using DiskAccessLibrary.Mod.Utility;
- namespace DiskAccessLibrary
- {
- public class PhysicalDiskDifferencingDiskImage : DiskImage
- {
- private const int SectorSize = 512;
- private PddInfo PddInfo { get; }
- public int NumberOfBlocks => PddInfo.NumberOfBlocks;
- public int BlockSize => PddInfo.BlockSize;
- public ReadOnlyIndexer<int, bool> IsBlockAllocated { get; }
- private bool _isExclusiveLock;
- private readonly Disk _underlyingDisk;
- private FileStream _snapshotFileStream;
- public PhysicalDiskDifferencingDiskImage(string diskImagePath, bool isReadOnly, Disk underlyingDisk) : base(diskImagePath, isReadOnly)
- {
- _underlyingDisk = underlyingDisk;
- PddInfo = new PddInfo(diskImagePath);
- IsBlockAllocated = new ReadOnlyIndexer<int, bool>(i => PddInfo.Allocated[i]);
- }
- public void OpenForRead()
- {
- _snapshotFileStream = File.Open(Path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
- }
- public void CloseForRead()
- {
- _snapshotFileStream?.Close();
- }
- public void ReadBlock(byte[] buffer, int offset, int count, int blockIndex, int blockOffset)
- {
- if (false == PddInfo.Allocated[blockIndex])
- {
- //TODO:convert block to sectors
- var rawOffset = (long)blockIndex * PddInfo.BlockSize + blockOffset;
- var sectorIndex = rawOffset / _underlyingDisk.BytesPerSector;
- while (count > 0)
- {
- var read = _basedFileStream.Read(buffer, offset, count);
- offset += read;
- count -= read;
- }
- }
- else
- {
- _snapshotFileStream.Position = PddInfo.Offset[blockIndex] + blockOffset;
- while (count > 0)
- {
- var read = _snapshotFileStream.Read(buffer, offset, count);
- offset += read;
- count -= read;
- }
- }
- }
- private void WriteBlock(byte[] buffer, int offset, int count, int blockIndex, int blockOffset)
- {
- if (false == PddInfo.Allocated[blockIndex])
- {
- //COW:allocate block
- _snapshotFileStream.Position = _snapshotFileStream.Length;
- PddInfo.Offset[blockIndex] = _snapshotFileStream.Position;
- //copy leading block from based image
- if (blockOffset > 0)
- {
- var readCount = blockOffset;
- var bufLeading = new byte[readCount];
- //TODO:convert block to sectors
- _basedFileStream.Position = (long)blockIndex * PddInfo.BlockSize;
- var readOffset = 0;
- while (readCount > 0)
- {
- var read = _basedFileStream.Read(bufLeading, readOffset, readCount);
- readOffset += read;
- readCount -= read;
- }
- _snapshotFileStream.Position = PddInfo.Offset[blockIndex];
- _snapshotFileStream.Write(bufLeading, 0, bufLeading.Length);
- }
- //write to snapshot directly
- _snapshotFileStream.Position = PddInfo.Offset[blockIndex] + blockOffset;
- _snapshotFileStream.Write(buffer, offset, count);
- //copy suffix block from based image
- if (blockOffset + count < PddInfo.BlockSize)
- {
- var suffixOffset = blockOffset + count;
- var readCount = PddInfo.BlockSize - suffixOffset;
- var bufSuffix = new byte[readCount];
- //TODO:convert block to sectors
- _basedFileStream.Position = (long)blockIndex * PddInfo.BlockSize + suffixOffset;
- var readOffset = 0;
- while (readCount > 0)
- {
- var read = _basedFileStream.Read(bufSuffix, readOffset, readCount);
- readOffset += read;
- readCount -= read;
- }
- _snapshotFileStream.Position = PddInfo.Offset[blockIndex] + suffixOffset;
- _snapshotFileStream.Write(bufSuffix, 0, bufSuffix.Length);
- }
- PddInfo.Allocated[blockIndex] = true;
- _snapshotFileStream.Position = PddInfo.EntryTableOffset + PddInfo.BlockIndexEntrySize * blockIndex;
- var bufEntry = BitConverter.GetBytes(PddInfo.Entries[blockIndex]);
- _snapshotFileStream.Write(bufEntry, 0, bufEntry.Length);
- }
- else
- {
- _snapshotFileStream.Position = PddInfo.Offset[blockIndex] + blockOffset;
- _snapshotFileStream.Write(buffer, offset, count);
- }
- }
- public override byte[] ReadSectors(long sectorIndex, int sectorCount)
- {
- var rawOffset = sectorIndex * SectorSize;
- var rawSize = sectorCount * SectorSize;
- var buffer = new byte[rawSize];
- for (var i = 0; i < rawSize;)
- {
- var blockIndex = (int)(rawOffset / PddInfo.BlockSize);
- var blockOffset = (int)(rawOffset % PddInfo.BlockSize);
- var bytesToRead = PddInfo.BlockSize - blockOffset;
- if (i + bytesToRead > rawSize)
- {
- bytesToRead = rawSize - i;
- }
- ReadBlock(buffer, i, bytesToRead, blockIndex, blockOffset);
- i += bytesToRead;
- rawOffset += bytesToRead;
- }
- return buffer;
- }
- public override void WriteSectors(long sectorIndex, byte[] data)
- {
- if (IsReadOnly) throw new AuthenticationException("Read only");
- var rawOffset = sectorIndex * SectorSize;
- for (var i = 0; i < data.Length;)
- {
- var blockIndex = (int)(rawOffset / PddInfo.BlockSize);
- var blockOffset = (int)(rawOffset % PddInfo.BlockSize);
- var bytesToWrite = PddInfo.BlockSize - blockOffset;
- if (i + bytesToWrite > data.Length)
- {
- bytesToWrite = data.Length - i;
- }
- WriteBlock(data, i, bytesToWrite, blockIndex, blockOffset);
- i += bytesToWrite;
- rawOffset += bytesToWrite;
- }
- }
- public override bool ExclusiveLock()
- {
- if (_isExclusiveLock) return true;
- _snapshotFileStream?.Close();
- _snapshotFileStream = IsReadOnly
- ? File.Open(Path, FileMode.Open, FileAccess.Read, FileShare.Read)
- : File.Open(Path, FileMode.Open, FileAccess.ReadWrite, FileShare.Read);
- _isExclusiveLock = true;
- return true;
- }
- public void Clean()
- {
- if (IsReadOnly) return;
- for (var blockIndex = 0; blockIndex < PddInfo.NumberOfBlocks; blockIndex++)
- {
- PddInfo.Allocated[blockIndex] = false;
- }
- for (var blockIndex = 0; blockIndex < PddInfo.NumberOfBlocks; blockIndex++)
- {
- _snapshotFileStream.Position = PddInfo.EntryTableOffset + PddInfo.BlockIndexEntrySize * blockIndex;
- var bufEntry = BitConverter.GetBytes(PddInfo.Entries[blockIndex]);
- _snapshotFileStream.Write(bufEntry, 0, bufEntry.Length);
- }
- _snapshotFileStream.SetLength(PddInfo.EntryTableOffset + PddInfo.NumberOfBlocks * PddInfo.BlockIndexEntrySize);
- }
- public override bool ReleaseLock()
- {
- _snapshotFileStream?.Close();
- _isExclusiveLock = false;
- return true;
- }
- public static BlockDifferencingDiskImage Create(string snapshot, long size, int blockSize)
- {
- var numberOfBlocks = (int)Math.DivRem(size, blockSize, out var rem);
- if (rem != 0) throw new ArgumentException("Size no align to blockSize");
- using var stream = File.Create(snapshot);
- using var writer = new BinaryWriter(stream);
- writer.Write((ushort)0);
- writer.Write(blockSize);
- writer.Write(numberOfBlocks);
- writer.Flush();
- stream.SetLength(stream.Length + numberOfBlocks * PddInfo.BlockIndexEntrySize);
- writer.Close();
- stream.Close();
- //return instance
- return new BlockDifferencingDiskImage(snapshot);
- }
- public static BlockDifferencingDiskImage Create(string based, string snapshot, int blockSize)
- {
- var fileSize = IoUtility.GetRealFileSize(based);
- if (fileSize % blockSize != 0) throw new ArgumentException("Block size no mul to base image size", nameof(blockSize));
- using var stream = File.Create(snapshot);
- using var writer = new BinaryWriter(stream);
- //create bdd struct
- var bufBasedPath = Encoding.UTF8.GetBytes(System.IO.Path.GetFullPath(based));
- var numberOfBlocks = (int)(fileSize / blockSize);
- writer.Write((ushort)bufBasedPath.Length);
- writer.Write(bufBasedPath);
- writer.Write(blockSize);
- writer.Write(numberOfBlocks);
- writer.Flush();
- stream.SetLength(stream.Length + numberOfBlocks * PddInfo.BlockIndexEntrySize);
- writer.Close();
- stream.Close();
- //return instance
- return new BlockDifferencingDiskImage(snapshot);
- }
- public override long Size => PddInfo.Length;
- public override int BytesPerSector => SectorSize;
- ////////////////////////////////////////////////////////////////////////////////////
- public PhysicalDiskDifferencingDiskImage(string diskImagePath, Disk underlyingDisk) : this(diskImagePath, false, underlyingDisk)
- {
- }
- public override bool ExclusiveLock(bool useOverlappedIO)
- {
- throw new System.NotImplementedException();
- }
- public override void Extend(long numberOfAdditionalBytes)
- {
- throw new System.NotImplementedException();
- }
- }
- }
|