RawImageStorage.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 uint BlockSize { get; } = 512;
  13. public uint MaxTransferLength { get; } = 64 * CapacityUnits.KiloByte;
  14. public bool SupportTrim { get; }
  15. public ulong BlockCount { get; }
  16. public bool WriteProtect { get; }
  17. public RawImageStorage(string imageFilePath, bool readOnly = false)
  18. {
  19. _imageFilePath = imageFilePath;
  20. WriteProtect = readOnly;
  21. SupportTrim = !readOnly;
  22. BlockCount = (ulong)new FileInfo(_imageFilePath).Length / BlockSize;
  23. }
  24. public void Init()
  25. {
  26. if (null != _fileStream) return;
  27. var access = WriteProtect ? FileAccess.Read : FileAccess.ReadWrite;
  28. _fileStream = new FileStream(_imageFilePath, FileMode.Open, access, FileShare.None, (int)MaxTransferLength);
  29. if (false == WriteProtect) _sparse = _fileStream.SetSparse();
  30. }
  31. public void Exit()
  32. {
  33. if (null == _fileStream) return;
  34. if (false == WriteProtect)
  35. {
  36. lock (_fileStream)
  37. {
  38. _fileStream.Flush(true);
  39. }
  40. }
  41. lock (_fileStream)
  42. {
  43. _fileStream.Close();
  44. _fileStream = null;
  45. }
  46. }
  47. public void ReadBlocks(byte[] buffer, ulong blockIndex, uint blockCount)
  48. {
  49. lock (_fileStream)
  50. {
  51. _fileStream.Position = (long)(blockIndex * BlockSize);
  52. var length = (int)(blockCount * BlockSize);
  53. var offset = 0;
  54. while (length > 0)
  55. {
  56. var read = _fileStream.Read(buffer, offset, length);
  57. length -= read;
  58. offset += read;
  59. }
  60. }
  61. }
  62. public void WriteBlocks(byte[] buffer, ulong blockIndex, uint blockCount)
  63. {
  64. lock (_fileStream)
  65. {
  66. _fileStream.Position = (long)(blockIndex * BlockSize);
  67. _fileStream.Write(buffer, 0, (int)(blockCount * BlockSize));
  68. }
  69. }
  70. public int Read(byte[] buffer, long offset, int index, int count)
  71. {
  72. lock (_fileStream)
  73. {
  74. _fileStream.Position = offset;
  75. return _fileStream.Read(buffer, index, count);
  76. }
  77. }
  78. public void Write(byte[] buffer, long offset, int index, int count)
  79. {
  80. lock (_fileStream)
  81. {
  82. _fileStream.Position = offset;
  83. _fileStream.Write(buffer, index, count);
  84. }
  85. }
  86. public void Flush()
  87. {
  88. lock (_fileStream) _fileStream.Flush(true);
  89. }
  90. public void Trim(params TrimDescriptor[] descriptors)
  91. {
  92. lock (_fileStream)
  93. {
  94. foreach (var trim in descriptors)
  95. {
  96. if (_sparse && _fileStream.Trim(trim.Offset, trim.Length)) continue;
  97. _fileStream.Position = trim.Offset;
  98. var totalLength = (int)trim.Length;
  99. var buffer = new byte[Math.Min(64 * 1024, totalLength)];
  100. while (0 < totalLength)
  101. {
  102. _fileStream.Write(buffer, 0, buffer.Length);
  103. totalLength -= buffer.Length;
  104. }
  105. }
  106. }
  107. }
  108. }
  109. }