BlockDifferencingDiskImage.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. using DiskAccessLibrary.Mod.BlockDifferencingDiskImage;
  2. using System;
  3. using System.IO;
  4. using System.Security.Authentication;
  5. using System.Text;
  6. namespace DiskAccessLibrary
  7. {
  8. public class BlockDifferencingDiskImage : DiskImage
  9. {
  10. private const int SectorSize = 512;
  11. private BddInfo BddInfo { get; }
  12. public bool IsNoBaseImage => null == _basedFileStream;
  13. public int NumberOfBlocks => BddInfo.NumberOfBlocks;
  14. public int BlockSize => BddInfo.BlockSize;
  15. public IReadOnlyIndexer<int, bool> IsBlockAllocated { get; }
  16. private bool _isExclusiveLock;
  17. private FileStream _basedFileStream;
  18. private FileStream _snapshotFileStream;
  19. public BlockDifferencingDiskImage(string diskImagePath, bool isReadOnly) : base(diskImagePath, isReadOnly)
  20. {
  21. BddInfo = new BddInfo(diskImagePath);
  22. IsBlockAllocated = new IReadOnlyIndexer<int, bool>(i => BddInfo.Allocated[i]);
  23. }
  24. public void OpenForRead()
  25. {
  26. _snapshotFileStream = File.Open(Path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
  27. }
  28. public void CloseForRead()
  29. {
  30. _snapshotFileStream?.Close();
  31. }
  32. public void ReadBlock(byte[] buffer, int offset, int count, int blockIndex, int blockOffset)
  33. {
  34. if (false == BddInfo.Allocated[blockIndex])
  35. {
  36. if (null == _basedFileStream) return;
  37. _basedFileStream.Position = (long)blockIndex * BddInfo.BlockSize + blockOffset;
  38. while (count > 0)
  39. {
  40. var read = _basedFileStream.Read(buffer, offset, count);
  41. offset += read;
  42. count -= read;
  43. }
  44. }
  45. else
  46. {
  47. _snapshotFileStream.Position = BddInfo.Offset[blockIndex] + blockOffset;
  48. while (count > 0)
  49. {
  50. var read = _snapshotFileStream.Read(buffer, offset, count);
  51. offset += read;
  52. count -= read;
  53. }
  54. }
  55. }
  56. private void WriteBlock(byte[] buffer, int offset, int count, int blockIndex, int blockOffset)
  57. {
  58. if (false == BddInfo.Allocated[blockIndex])
  59. {
  60. //COW:allocate block
  61. _snapshotFileStream.Position = _snapshotFileStream.Length;
  62. BddInfo.Offset[blockIndex] = _snapshotFileStream.Position;
  63. //copy leading block from based image
  64. if (blockOffset > 0)
  65. {
  66. var readCount = blockOffset;
  67. var bufLeading = new byte[readCount];
  68. if (null != _basedFileStream)
  69. {
  70. _basedFileStream.Position = (long)blockIndex * BddInfo.BlockSize;
  71. var readOffset = 0;
  72. while (readCount > 0)
  73. {
  74. var read = _basedFileStream.Read(bufLeading, readOffset, readCount);
  75. readOffset += read;
  76. readCount -= read;
  77. }
  78. }
  79. _snapshotFileStream.Position = BddInfo.Offset[blockIndex];
  80. _snapshotFileStream.Write(bufLeading, 0, bufLeading.Length);
  81. }
  82. //write to snapshot directly
  83. _snapshotFileStream.Position = BddInfo.Offset[blockIndex] + blockOffset;
  84. _snapshotFileStream.Write(buffer, offset, count);
  85. //copy suffix block from based image
  86. if (blockOffset + count < BddInfo.BlockSize)
  87. {
  88. var suffixOffset = blockOffset + count;
  89. var readCount = BddInfo.BlockSize - suffixOffset;
  90. var bufSuffix = new byte[readCount];
  91. if (null != _basedFileStream)
  92. {
  93. _basedFileStream.Position = (long)blockIndex * BddInfo.BlockSize + suffixOffset;
  94. var readOffset = 0;
  95. while (readCount > 0)
  96. {
  97. var read = _basedFileStream.Read(bufSuffix, readOffset, readCount);
  98. readOffset += read;
  99. readCount -= read;
  100. }
  101. }
  102. _snapshotFileStream.Position = BddInfo.Offset[blockIndex] + suffixOffset;
  103. _snapshotFileStream.Write(bufSuffix, 0, bufSuffix.Length);
  104. }
  105. BddInfo.Allocated[blockIndex] = true;
  106. _snapshotFileStream.Position = BddInfo.EntryTableOffset + BddInfo.BlockIndexEntrySize * blockIndex;
  107. var bufEntry = BitConverter.GetBytes(BddInfo.Entries[blockIndex]);
  108. _snapshotFileStream.Write(bufEntry, 0, bufEntry.Length);
  109. }
  110. else
  111. {
  112. _snapshotFileStream.Position = BddInfo.Offset[blockIndex] + blockOffset;
  113. _snapshotFileStream.Write(buffer, offset, count);
  114. }
  115. }
  116. public override byte[] ReadSectors(long sectorIndex, int sectorCount)
  117. {
  118. var rawOffset = sectorIndex * SectorSize;
  119. var rawSize = sectorCount * SectorSize;
  120. var buffer = new byte[rawSize];
  121. for (var i = 0; i < rawSize;)
  122. {
  123. var blockIndex = (int)(rawOffset / BddInfo.BlockSize);
  124. var blockOffset = (int)(rawOffset % BddInfo.BlockSize);
  125. var bytesToRead = BddInfo.BlockSize - blockOffset;
  126. if (i + bytesToRead > rawSize)
  127. {
  128. bytesToRead = rawSize - i;
  129. }
  130. ReadBlock(buffer, i, bytesToRead, blockIndex, blockOffset);
  131. i += bytesToRead;
  132. rawOffset += bytesToRead;
  133. }
  134. return buffer;
  135. }
  136. public override void WriteSectors(long sectorIndex, byte[] data)
  137. {
  138. if (IsReadOnly) throw new AuthenticationException("Read only");
  139. var rawOffset = sectorIndex * SectorSize;
  140. for (var i = 0; i < data.Length;)
  141. {
  142. var blockIndex = (int)(rawOffset / BddInfo.BlockSize);
  143. var blockOffset = (int)(rawOffset % BddInfo.BlockSize);
  144. var bytesToWrite = BddInfo.BlockSize - blockOffset;
  145. if (i + bytesToWrite > data.Length)
  146. {
  147. bytesToWrite = data.Length - i;
  148. }
  149. WriteBlock(data, i, bytesToWrite, blockIndex, blockOffset);
  150. i += bytesToWrite;
  151. rawOffset += bytesToWrite;
  152. }
  153. }
  154. public override bool ExclusiveLock()
  155. {
  156. if (_isExclusiveLock) return true;
  157. _basedFileStream?.Close();
  158. _snapshotFileStream?.Close();
  159. if (null != BddInfo.BasedImagePath) _basedFileStream = new FileStream(BddInfo.BasedImagePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
  160. _snapshotFileStream = IsReadOnly
  161. ? File.Open(Path, FileMode.Open, FileAccess.Read, FileShare.Read)
  162. : File.Open(Path, FileMode.Open, FileAccess.ReadWrite, FileShare.Read);
  163. _isExclusiveLock = true;
  164. return true;
  165. }
  166. public void Clean()
  167. {
  168. if (IsReadOnly) return;
  169. for (var blockIndex = 0; blockIndex < BddInfo.NumberOfBlocks; blockIndex++)
  170. {
  171. BddInfo.Allocated[blockIndex] = false;
  172. }
  173. for (var blockIndex = 0; blockIndex < BddInfo.NumberOfBlocks; blockIndex++)
  174. {
  175. _snapshotFileStream.Position = BddInfo.EntryTableOffset + BddInfo.BlockIndexEntrySize * blockIndex;
  176. var bufEntry = BitConverter.GetBytes(BddInfo.Entries[blockIndex]);
  177. _snapshotFileStream.Write(bufEntry, 0, bufEntry.Length);
  178. }
  179. _snapshotFileStream.SetLength(BddInfo.EntryTableOffset + BddInfo.NumberOfBlocks * BddInfo.BlockIndexEntrySize);
  180. }
  181. public override bool ReleaseLock()
  182. {
  183. _basedFileStream?.Close();
  184. _snapshotFileStream?.Close();
  185. _isExclusiveLock = false;
  186. return true;
  187. }
  188. public static BlockDifferencingDiskImage Create(string snapshot, long size, int blockSize)
  189. {
  190. var numberOfBlocks = (int)Math.DivRem(size, blockSize, out var rem);
  191. if (rem != 0) throw new ArgumentException("Size no align to blockSize");
  192. using var stream = File.Create(snapshot);
  193. using var writer = new BinaryWriter(stream);
  194. writer.Write((ushort)0);
  195. writer.Write(blockSize);
  196. writer.Write(numberOfBlocks);
  197. writer.Flush();
  198. stream.SetLength(stream.Length + numberOfBlocks * BddInfo.BlockIndexEntrySize);
  199. writer.Close();
  200. stream.Close();
  201. //return instance
  202. return new BlockDifferencingDiskImage(snapshot);
  203. }
  204. public static BlockDifferencingDiskImage Create(string based, string snapshot, int blockSize)
  205. {
  206. var fileInfo = new FileInfo(based);
  207. if (fileInfo.Length % blockSize != 0) throw new ArgumentException("Block size no mul to base image size", nameof(blockSize));
  208. using var stream = File.Create(snapshot);
  209. using var writer = new BinaryWriter(stream);
  210. //create bdd struct
  211. var bufBasedPath = Encoding.UTF8.GetBytes(System.IO.Path.GetFullPath(based));
  212. var numberOfBlocks = (int)(fileInfo.Length / blockSize);
  213. writer.Write((ushort)bufBasedPath.Length);
  214. writer.Write(bufBasedPath);
  215. writer.Write(blockSize);
  216. writer.Write(numberOfBlocks);
  217. writer.Flush();
  218. stream.SetLength(stream.Length + numberOfBlocks * BddInfo.BlockIndexEntrySize);
  219. writer.Close();
  220. stream.Close();
  221. //return instance
  222. return new BlockDifferencingDiskImage(snapshot);
  223. }
  224. public override long Size => BddInfo.Length;
  225. public override int BytesPerSector => SectorSize;
  226. ////////////////////////////////////////////////////////////////////////////////////
  227. public BlockDifferencingDiskImage(string diskImagePath) : this(diskImagePath, false)
  228. {
  229. }
  230. public override bool ExclusiveLock(bool useOverlappedIO)
  231. {
  232. throw new System.NotImplementedException();
  233. }
  234. public override void Extend(long numberOfAdditionalBytes)
  235. {
  236. throw new System.NotImplementedException();
  237. }
  238. }
  239. }