public override void Write(Byte[] Buffer, UInt64 BlockAddress, UInt32 BlockCount, Boolean Flush, ref StorageUnitStatus Status) { lock (this) /* I want pwrite */ { _Stream.Position = (long)(BlockAddress * _BlockLength); _Stream.Write(Buffer, 0, (int)(BlockCount * _BlockLength)); } if (Flush) this.Flush(BlockAddress, BlockCount, ref Status); }
public override void Read(Byte[] Buffer, UInt64 BlockAddress, UInt32 BlockCount, Boolean Flush, ref StorageUnitStatus Status) { if (Flush) this.Flush(BlockAddress, BlockCount, ref Status); lock (this) /* I want pread */ { _Stream.Position = (long)(BlockAddress * _BlockLength); _Stream.Read(Buffer, 0, (int)(BlockCount * _BlockLength)); /* FIX: we assume that we are reading from a file and ignore the return value */ } }
public override void Unmap(UnmapDescriptor[] Descriptors, ref StorageUnitStatus Status) { FILE_ZERO_DATA_INFORMATION Zero; UInt32 BytesTransferred; for (int I = 0; Descriptors.Length > I; I++) { Boolean SetZero = false; if (_Sparse) { Zero.FileOffset = (long)(Descriptors[I].BlockAddress * _BlockLength); Zero.BeyondFinalZero = (long)((Descriptors[I].BlockAddress + Descriptors[I].BlockCount) * _BlockLength); SetZero = DeviceIoControl(_Stream.SafeFileHandle.DangerousGetHandle(), 0x980c8 /*FSCTL_SET_ZERO_DATA*/, ref Zero, (UInt32)Marshal.SizeOf(Zero), IntPtr.Zero, 0, out BytesTransferred, IntPtr.Zero); } if (!SetZero) { lock (this) /* I want pwrite */ { _Stream.Position = (long)(Descriptors[I].BlockAddress * _BlockLength); int TotalLength = (int)(Descriptors[I].BlockCount * _BlockLength); Byte[] Buffer = new Byte[Math.Min(64 * 1024, TotalLength)]; while (0 < TotalLength) { _Stream.Write(Buffer, 0, Buffer.Length); TotalLength -= Buffer.Length; } } } } }
public override void Flush(UInt64 BlockAddress, UInt32 BlockCount, ref StorageUnitStatus Status) { _Stream.Flush(); }