RawImageStorage.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System;
  2. using System.IO;
  3. namespace SvdCli.Storage.Implements
  4. {
  5. public class RawImageStorage : ISvdStorage
  6. {
  7. private readonly string _imageFilePath;
  8. private FileStream _fileStream;
  9. private bool _sparse;
  10. public string ProductId { get; } = "Svd RawImage Disk";
  11. public string ProductVersion { get; } = "1.0";
  12. public Guid Guid { get; } = Guid.NewGuid();
  13. public uint BlockSize { get; } = 512;
  14. public uint MaxTransferLength { get; } = 64 * CapacityUnits.KiloByte;
  15. public bool SupportTrim { get; }
  16. public ulong BlockCount { get; }
  17. public bool WriteProtect { get; }
  18. public event EventHandler Mounted;
  19. public RawImageStorage(string imageFilePath, bool readOnly = false)
  20. {
  21. _imageFilePath = imageFilePath;
  22. WriteProtect = readOnly;
  23. SupportTrim = !readOnly;
  24. BlockCount = (ulong)new FileInfo(_imageFilePath).Length / BlockSize;
  25. }
  26. public void Init()
  27. {
  28. if (null != _fileStream) return;
  29. var access = WriteProtect ? FileAccess.Read : FileAccess.ReadWrite;
  30. _fileStream = new FileStream(_imageFilePath, FileMode.Open, access, FileShare.None, (int)MaxTransferLength);
  31. if (false == WriteProtect) _sparse = _fileStream.SetSparse();
  32. }
  33. public void Exit()
  34. {
  35. if (null == _fileStream) return;
  36. if (false == WriteProtect)
  37. {
  38. lock (_fileStream)
  39. {
  40. _fileStream.Flush(true);
  41. }
  42. }
  43. lock (_fileStream)
  44. {
  45. _fileStream.Close();
  46. _fileStream = null;
  47. }
  48. }
  49. public void ReadBlocks(byte[] buffer, ulong blockIndex, uint blockCount)
  50. {
  51. lock (_fileStream)
  52. {
  53. _fileStream.Position = (long)(blockIndex * BlockSize);
  54. var length = (int)(blockCount * BlockSize);
  55. var offset = 0;
  56. while (length > 0)
  57. {
  58. var read = _fileStream.Read(buffer, offset, length);
  59. length -= read;
  60. offset += read;
  61. }
  62. }
  63. }
  64. public void WriteBlocks(byte[] buffer, ulong blockIndex, uint blockCount)
  65. {
  66. lock (_fileStream)
  67. {
  68. _fileStream.Position = (long)(blockIndex * BlockSize);
  69. _fileStream.Write(buffer, 0, (int)(blockCount * BlockSize));
  70. }
  71. }
  72. public int Read(byte[] buffer, long offset, int index, int count)
  73. {
  74. lock (_fileStream)
  75. {
  76. _fileStream.Position = offset;
  77. return _fileStream.Read(buffer, index, count);
  78. }
  79. }
  80. public void Write(byte[] buffer, long offset, int index, int count)
  81. {
  82. lock (_fileStream)
  83. {
  84. _fileStream.Position = offset;
  85. _fileStream.Write(buffer, index, count);
  86. }
  87. }
  88. public void Flush()
  89. {
  90. lock (_fileStream) _fileStream.Flush(true);
  91. }
  92. public void Trim(params TrimDescriptor[] descriptors)
  93. {
  94. lock (_fileStream)
  95. {
  96. foreach (var trim in descriptors)
  97. {
  98. if (_sparse && _fileStream.Trim(trim.Offset, trim.Length)) continue;
  99. _fileStream.Position = trim.Offset;
  100. var totalLength = (int)trim.Length;
  101. var buffer = new byte[Math.Min(64 * 1024, totalLength)];
  102. while (0 < totalLength)
  103. {
  104. _fileStream.Write(buffer, 0, buffer.Length);
  105. totalLength -= buffer.Length;
  106. }
  107. }
  108. }
  109. }
  110. public void FireMountedEvent()
  111. {
  112. Mounted?.Invoke(this, EventArgs.Empty);
  113. }
  114. }
  115. }