/// <summary> /// Sector refers to physical disk blocks, we can only read complete blocks /// </summary> public byte[] ReadSectorsUnbuffered(long sectorIndex, int sectorCount) { bool releaseHandle; SafeFileHandle handle = VolumeHandlePool.ObtainHandle(m_volumeGuid, FileAccess.Read, ShareMode.ReadWrite, out releaseHandle); if (!handle.IsInvalid) { FileStreamEx stream = new FileStreamEx(handle, FileAccess.Read); byte[] buffer = new byte[m_bytesPerSector * sectorCount]; try { stream.Seek(sectorIndex * m_bytesPerSector, SeekOrigin.Begin); stream.Read(buffer, 0, m_bytesPerSector * sectorCount); } finally { stream.Close(releaseHandle); if (releaseHandle) { VolumeHandlePool.ReleaseHandle(m_volumeGuid); } } return(buffer); } else { // we always release invalid handle VolumeHandlePool.ReleaseHandle(m_volumeGuid); // get error code and throw int errorCode = Marshal.GetLastWin32Error(); string message = String.Format("Can't read sector {0} from volume {1}, Win32 Error: {2}", sectorIndex, m_volumeGuid, errorCode); throw new IOException(message); } }
private void PopulateDescription() { bool releaseHandle; SafeFileHandle handle = PhysicalDiskHandlePool.ObtainHandle(m_physicalDiskIndex, FileAccess.Read, ShareMode.ReadWrite, out releaseHandle); if (!handle.IsInvalid) { m_description = PhysicalDiskUtils.GetDeviceDescription(handle); m_serialNumber = PhysicalDiskUtils.GetDeviceSerialNumber(handle); if (releaseHandle) { PhysicalDiskHandlePool.ReleaseHandle(m_physicalDiskIndex); } } else { // we always release invalid handle PhysicalDiskHandlePool.ReleaseHandle(m_physicalDiskIndex); // get error code and throw int errorCode = Marshal.GetLastWin32Error(); string message = String.Format("Can't read from disk {0}", m_physicalDiskIndex); if (errorCode == (int)Win32Error.ERROR_FILE_NOT_FOUND) { throw new DriveNotFoundException(message); } else { FileStreamEx.ThrowIOError(errorCode, message); } } }
/// <summary> /// Sector refers to physical disk blocks, we can only read complete blocks /// </summary> public byte[] ReadSectorsUnbuffered(long sectorIndex, int sectorCount) { bool releaseHandle; SafeFileHandle handle = PhysicalDiskHandlePool.ObtainHandle(m_physicalDiskIndex, FileAccess.Read, ShareMode.ReadWrite, out releaseHandle); if (!handle.IsInvalid) { FileStreamEx stream = new FileStreamEx(handle, FileAccess.Read); byte[] buffer = new byte[m_bytesPerSector * sectorCount]; try { stream.Seek(sectorIndex * m_bytesPerSector, SeekOrigin.Begin); stream.Read(buffer, 0, m_bytesPerSector * sectorCount); } finally { stream.Close(releaseHandle); if (releaseHandle) { PhysicalDiskHandlePool.ReleaseHandle(m_physicalDiskIndex); } } return(buffer); } else { // we always release invalid handle PhysicalDiskHandlePool.ReleaseHandle(m_physicalDiskIndex); // get error code and throw int errorCode = Marshal.GetLastWin32Error(); string message = String.Format("Failed to read sector {0} from disk {1}.", sectorIndex, m_physicalDiskIndex); IOExceptionHelper.ThrowIOError(errorCode, message); return(null); // this line will not be reached } }
public void WriteSectorsUnbuffered(long sectorIndex, byte[] data) { if (data.Length % m_bytesPerSector > 0) { throw new IOException("Cannot write partial sectors"); } if (IsReadOnly) { throw new UnauthorizedAccessException("Attempted to perform write on a readonly disk"); } bool releaseHandle; SafeFileHandle handle = PhysicalDiskHandlePool.ObtainHandle(m_physicalDiskIndex, FileAccess.ReadWrite, ShareMode.Read, out releaseHandle); if (!handle.IsInvalid) { FileStreamEx stream = new FileStreamEx(handle, FileAccess.Write); try { stream.Seek(sectorIndex * m_bytesPerSector, SeekOrigin.Begin); stream.Write(data, 0, data.Length); stream.Flush(); } finally { stream.Close(releaseHandle); if (releaseHandle) { PhysicalDiskHandlePool.ReleaseHandle(m_physicalDiskIndex); } } } else { // we always release invalid handle PhysicalDiskHandlePool.ReleaseHandle(m_physicalDiskIndex); // get error code and throw int errorCode = Marshal.GetLastWin32Error(); string message = String.Format("Can't write to sector {0} of disk {1}", sectorIndex, m_physicalDiskIndex); FileStreamEx.ThrowIOError(errorCode, message); } }
private void PopulateDiskInfo() { bool releaseHandle; SafeFileHandle handle = PhysicalDiskHandlePool.ObtainHandle(m_physicalDiskIndex, FileAccess.Read, ShareMode.ReadWrite, out releaseHandle); if (!handle.IsInvalid) { if (!PhysicalDiskUtils.IsMediaAccesible(handle)) { throw new DeviceNotReadyException(); } DISK_GEOMETRY diskGeometry = PhysicalDiskUtils.GetDiskGeometryAndSize(handle, out m_size); if (releaseHandle) { PhysicalDiskHandlePool.ReleaseHandle(m_physicalDiskIndex); } m_bytesPerSector = (int)diskGeometry.BytesPerSector; // CHS: m_cylinders = diskGeometry.Cylinders; m_tracksPerCylinder = (int)diskGeometry.TracksPerCylinder; m_sectorsPerTrack = (int)diskGeometry.SectorsPerTrack; } else { // we always release invalid handle PhysicalDiskHandlePool.ReleaseHandle(m_physicalDiskIndex); // get error code and throw int errorCode = Marshal.GetLastWin32Error(); string message = String.Format("Can't read from disk {0}", m_physicalDiskIndex); if (errorCode == (int)Win32Error.ERROR_FILE_NOT_FOUND) { throw new DriveNotFoundException(message); } else { FileStreamEx.ThrowIOError(errorCode, message); } } }
public void WriteSectorsUnbuffered(long sectorIndex, byte[] data) { if (data.Length % m_bytesPerSector > 0) { throw new IOException("Cannot write partial sectors"); } if (!m_isReadOnly) { bool releaseHandle; SafeFileHandle handle = VolumeHandlePool.ObtainHandle(m_volumeGuid, FileAccess.ReadWrite, ShareMode.None, out releaseHandle); if (!handle.IsInvalid) { FileStreamEx stream = new FileStreamEx(handle, FileAccess.Write); try { stream.Seek(sectorIndex * m_bytesPerSector, SeekOrigin.Begin); stream.Write(data, 0, data.Length); stream.Flush(); } finally { stream.Close(releaseHandle); if (releaseHandle) { VolumeHandlePool.ReleaseHandle(m_volumeGuid); } } } else { // we always release invalid handle VolumeHandlePool.ReleaseHandle(m_volumeGuid); // get error code and throw int errorCode = Marshal.GetLastWin32Error(); string message = String.Format("Can't write to sector {0} of volume {1}, Win32 errorCode: {2}", sectorIndex, m_volumeGuid, errorCode); throw new IOException(message); } } }