RawDiskImage.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /* Copyright (C) 2014-2017 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. public const int DefaultBytesPerSector = 512;
  17. const FileOptions FILE_FLAG_NO_BUFFERING = (FileOptions)0x20000000;
  18. private bool m_isExclusiveLock;
  19. private FileStream m_stream;
  20. private int m_bytesPerSector;
  21. private long m_size;
  22. /// <exception cref="System.IO.IOException"></exception>
  23. /// <exception cref="System.UnauthorizedAccessException"></exception>
  24. public RawDiskImage(string rawDiskImagePath) : base(rawDiskImagePath)
  25. {
  26. m_bytesPerSector = DetectBytesPerSector(rawDiskImagePath);
  27. m_size = new FileInfo(rawDiskImagePath).Length;
  28. }
  29. /// <exception cref="System.IO.IOException"></exception>
  30. /// <exception cref="System.UnauthorizedAccessException"></exception>
  31. public RawDiskImage(string rawDiskImagePath, int bytesPerSector) : base(rawDiskImagePath)
  32. {
  33. m_bytesPerSector = bytesPerSector;
  34. m_size = new FileInfo(rawDiskImagePath).Length;
  35. }
  36. /// <exception cref="System.IO.IOException"></exception>
  37. public override bool ExclusiveLock()
  38. {
  39. if (!m_isExclusiveLock)
  40. {
  41. m_isExclusiveLock = true;
  42. m_stream = OpenFileStream();
  43. return true;
  44. }
  45. else
  46. {
  47. return false;
  48. }
  49. }
  50. public override bool ReleaseLock()
  51. {
  52. if (m_isExclusiveLock)
  53. {
  54. m_isExclusiveLock = false;
  55. m_stream.Close();
  56. return true;
  57. }
  58. else
  59. {
  60. return false;
  61. }
  62. }
  63. private FileStream OpenFileStream()
  64. {
  65. FileAccess fileAccess = IsReadOnly ? FileAccess.Read : FileAccess.ReadWrite;
  66. // We should use noncached I/O operations to avoid excessive RAM usage.
  67. // Note: KB99794 provides information about FILE_FLAG_WRITE_THROUGH and FILE_FLAG_NO_BUFFERING.
  68. // We must avoid using buffered writes, using it will negatively affect the performance and reliability.
  69. // Note: once the file system write buffer is filled, Windows may delay any (buffer-dependent) pending write operations, which will create a deadlock.
  70. FileStream stream = new FileStream(this.Path, FileMode.Open, fileAccess, FileShare.Read, 0x1000, FILE_FLAG_NO_BUFFERING | FileOptions.WriteThrough);
  71. return stream;
  72. }
  73. /// <summary>
  74. /// Sector refers to physical disk sector, we can only read complete sectors
  75. /// </summary>
  76. public override byte[] ReadSectors(long sectorIndex, int sectorCount)
  77. {
  78. CheckBoundaries(sectorIndex, sectorCount);
  79. if (!m_isExclusiveLock)
  80. {
  81. m_stream = OpenFileStream();
  82. }
  83. long offset = sectorIndex * BytesPerSector;
  84. byte[] result = new byte[BytesPerSector * sectorCount];
  85. m_stream.Seek(offset, SeekOrigin.Begin);
  86. m_stream.Read(result, 0, BytesPerSector * sectorCount);
  87. if (!m_isExclusiveLock)
  88. {
  89. m_stream.Close();
  90. }
  91. return result;
  92. }
  93. public override void WriteSectors(long sectorIndex, byte[] data)
  94. {
  95. if (IsReadOnly)
  96. {
  97. throw new UnauthorizedAccessException("Attempted to perform write on a readonly disk");
  98. }
  99. CheckBoundaries(sectorIndex, data.Length / this.BytesPerSector);
  100. if (!m_isExclusiveLock)
  101. {
  102. m_stream = OpenFileStream();
  103. }
  104. long offset = sectorIndex * BytesPerSector;
  105. m_stream.Seek(offset, SeekOrigin.Begin);
  106. m_stream.Write(data, 0, data.Length);
  107. if (!m_isExclusiveLock)
  108. {
  109. m_stream.Close();
  110. }
  111. }
  112. /// <exception cref="System.IO.IOException"></exception>
  113. public override void Extend(long numberOfAdditionalBytes)
  114. {
  115. if (numberOfAdditionalBytes % this.BytesPerSector > 0)
  116. {
  117. throw new ArgumentException("numberOfAdditionalBytes must be a multiple of BytesPerSector");
  118. }
  119. #if Win32
  120. // calling AdjustTokenPrivileges and then immediately calling SetFileValidData will sometimes result in ERROR_PRIVILEGE_NOT_HELD.
  121. // We can work around the issue by obtaining the privilege before obtaining the handle.
  122. bool hasManageVolumePrivilege = SecurityUtils.ObtainManageVolumePrivilege();
  123. #endif
  124. if (!m_isExclusiveLock)
  125. {
  126. m_stream = OpenFileStream();
  127. }
  128. else
  129. {
  130. // Workaround for AdjustTokenPrivileges issue
  131. ReleaseLock();
  132. ExclusiveLock();
  133. }
  134. m_stream.SetLength(m_size + numberOfAdditionalBytes);
  135. m_size += numberOfAdditionalBytes;
  136. #if Win32
  137. if (hasManageVolumePrivilege)
  138. {
  139. try
  140. {
  141. FileStreamUtils.SetValidLength(m_stream, m_size);
  142. }
  143. catch (IOException)
  144. {
  145. }
  146. }
  147. #endif
  148. if (!m_isExclusiveLock)
  149. {
  150. m_stream.Close();
  151. }
  152. }
  153. public override int BytesPerSector
  154. {
  155. get
  156. {
  157. return m_bytesPerSector;
  158. }
  159. }
  160. public override long Size
  161. {
  162. get
  163. {
  164. return m_size;
  165. }
  166. }
  167. /// <param name="size">In bytes</param>
  168. /// <exception cref="System.IO.IOException"></exception>
  169. /// <exception cref="System.UnauthorizedAccessException"></exception>
  170. public static RawDiskImage Create(string path, long size)
  171. {
  172. int bytesPerSector = DetectBytesPerSector(path);
  173. return Create(path, size, bytesPerSector);
  174. }
  175. /// <param name="size">In bytes</param>
  176. /// <exception cref="System.IO.IOException"></exception>
  177. /// <exception cref="System.UnauthorizedAccessException"></exception>
  178. internal static RawDiskImage Create(string path, long size, int bytesPerSector)
  179. {
  180. if (size % bytesPerSector > 0)
  181. {
  182. throw new ArgumentException("size must be a multiple of bytesPerSector");
  183. }
  184. #if Win32
  185. // calling AdjustTokenPrivileges and then immediately calling SetFileValidData will sometimes result in ERROR_PRIVILEGE_NOT_HELD.
  186. // We can work around the issue by obtaining the privilege before obtaining the handle.
  187. bool hasManageVolumePrivilege = SecurityUtils.ObtainManageVolumePrivilege();
  188. #endif
  189. FileStream stream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None);
  190. try
  191. {
  192. stream.SetLength(size);
  193. }
  194. catch (IOException)
  195. {
  196. stream.Close();
  197. try
  198. {
  199. // Delete the incomplete file
  200. File.Delete(path);
  201. }
  202. catch (IOException)
  203. {
  204. }
  205. throw;
  206. }
  207. #if Win32
  208. if (hasManageVolumePrivilege)
  209. {
  210. try
  211. {
  212. FileStreamUtils.SetValidLength(stream, size);
  213. }
  214. catch (IOException)
  215. {
  216. }
  217. }
  218. #endif
  219. stream.Close();
  220. return new RawDiskImage(path, bytesPerSector);
  221. }
  222. public static int DetectBytesPerSector(string path)
  223. {
  224. FileInfo info = new FileInfo(path);
  225. string[] components = info.Name.Split('.');
  226. if (components.Length >= 3) // file.512.img
  227. {
  228. string bytesPerSectorString = components[components.Length - 2];
  229. int bytesPerSector = Conversion.ToInt32(bytesPerSectorString, DefaultBytesPerSector);
  230. return bytesPerSector;
  231. }
  232. else
  233. {
  234. return DefaultBytesPerSector;
  235. }
  236. }
  237. }
  238. }