BlockDifferencingDiskImage.cs 11 KB

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