RawDiskImage.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* Copyright (C) 2014 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  2. *
  3. * You can redistribute this program and/or modify it under the terms of
  4. * the GNU Lesser Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using System.Text;
  11. using Utilities;
  12. namespace DiskAccessLibrary
  13. {
  14. public partial class RawDiskImage : DiskImage
  15. {
  16. const FileOptions FILE_FLAG_NO_BUFFERING = (FileOptions)0x20000000;
  17. private bool m_isExclusiveLock;
  18. private FileStream m_stream;
  19. public RawDiskImage(string rawDiskImagePath) : base(rawDiskImagePath)
  20. {
  21. }
  22. /// <exception cref="System.IO.IOException"></exception>
  23. public override bool ExclusiveLock()
  24. {
  25. if (!m_isExclusiveLock)
  26. {
  27. m_isExclusiveLock = true;
  28. FileAccess fileAccess = IsReadOnly ? FileAccess.Read : FileAccess.ReadWrite;
  29. // We should use noncached I/O operations to avoid excessive RAM usage.
  30. // Note: KB99794 provides information about FILE_FLAG_WRITE_THROUGH and FILE_FLAG_NO_BUFFERING.
  31. m_stream = new FileStream(this.Path, FileMode.Open, fileAccess, FileShare.Read, 0x1000, FILE_FLAG_NO_BUFFERING | FileOptions.WriteThrough);
  32. return true;
  33. }
  34. else
  35. {
  36. return false;
  37. }
  38. }
  39. public override bool ReleaseLock()
  40. {
  41. if (m_isExclusiveLock)
  42. {
  43. m_isExclusiveLock = false;
  44. m_stream.Close();
  45. return true;
  46. }
  47. else
  48. {
  49. return false;
  50. }
  51. }
  52. /// <summary>
  53. /// Sector refers to physical disk sector, we can only read complete sectors
  54. /// </summary>
  55. public override byte[] ReadSectors(long sectorIndex, int sectorCount)
  56. {
  57. CheckBoundaries(sectorIndex, sectorCount);
  58. if (!m_isExclusiveLock)
  59. {
  60. // We should use noncached I/O operations to avoid excessive RAM usage.
  61. // Note: KB99794 provides information about FILE_FLAG_WRITE_THROUGH and FILE_FLAG_NO_BUFFERING.
  62. m_stream = new FileStream(this.Path, FileMode.Open, FileAccess.Read, FileShare.Read, 0x1000, FILE_FLAG_NO_BUFFERING | FileOptions.WriteThrough);
  63. }
  64. long offset = sectorIndex * BytesPerSector;
  65. m_stream.Seek(offset, SeekOrigin.Begin);
  66. byte[] result = new byte[BytesPerSector * sectorCount];
  67. m_stream.Read(result, 0, BytesPerSector * sectorCount);
  68. if (!m_isExclusiveLock)
  69. {
  70. m_stream.Close();
  71. }
  72. return result;
  73. }
  74. public override void WriteSectors(long sectorIndex, byte[] data)
  75. {
  76. if (IsReadOnly)
  77. {
  78. throw new UnauthorizedAccessException("Attempted to perform write on a readonly disk");
  79. }
  80. CheckBoundaries(sectorIndex, data.Length / this.BytesPerSector);
  81. if (!m_isExclusiveLock)
  82. {
  83. // We should use noncached I/O operations to avoid excessive RAM usage.
  84. // We must avoid using buffered writes, using it will negatively affect the performance and reliability.
  85. // Note: once the file system write buffer is filled, Windows may delay any (buffer-dependent) pending write operations, which will create a deadlock.
  86. m_stream = new FileStream(this.Path, FileMode.Open, FileAccess.ReadWrite, FileShare.Read, 0x1000, FILE_FLAG_NO_BUFFERING | FileOptions.WriteThrough);
  87. }
  88. long offset = sectorIndex * BytesPerSector;
  89. m_stream.Seek(offset, SeekOrigin.Begin);
  90. m_stream.Write(data, 0, data.Length);
  91. if (!m_isExclusiveLock)
  92. {
  93. m_stream.Close();
  94. }
  95. }
  96. /// <exception cref="System.IO.IOException"></exception>
  97. public override void Extend(long additionalNumberOfBytes)
  98. {
  99. if (additionalNumberOfBytes % this.BytesPerSector > 0)
  100. {
  101. throw new ArgumentException("additionalNumberOfBytes must be a multiple of BytesPerSector");
  102. }
  103. long length = this.Size;
  104. if (!m_isExclusiveLock)
  105. {
  106. m_stream = new FileStream(this.Path, FileMode.Open, FileAccess.ReadWrite, FileShare.Read, 0x1000, FILE_FLAG_NO_BUFFERING | FileOptions.WriteThrough);
  107. }
  108. m_stream.SetLength(length + additionalNumberOfBytes);
  109. if (!m_isExclusiveLock)
  110. {
  111. m_stream.Close();
  112. }
  113. }
  114. public override int BytesPerSector
  115. {
  116. get
  117. {
  118. FileInfo info = new FileInfo(this.Path);
  119. string[] components = info.Name.Split('.');
  120. if (components.Length >= 3) // file.512.img
  121. {
  122. string bytesPerSectorString = components[components.Length - 2];
  123. int bytesPerSector = Conversion.ToInt32(bytesPerSectorString, BytesPerDiskImageSector);
  124. return bytesPerSector;
  125. }
  126. else
  127. {
  128. return BytesPerDiskImageSector;
  129. }
  130. }
  131. }
  132. public override long Size
  133. {
  134. get
  135. {
  136. FileInfo info = new FileInfo(this.Path);
  137. return info.Length;
  138. }
  139. }
  140. }
  141. }