BlockDifferencingDiskImage.cs 10 KB

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