public VolumeStream(SafeFileHandle handle)
            : base(handle)
        {
            var volInfo = Win32Wrapper.GetNtfsVolumeData(handle);

            _length = volInfo.TotalClusters * volInfo.BytesPerCluster;
        }
        public NativeMethods.DiskExtent[] GetDiskExtents()
        {
            int numExtents = 1;
            int bufferSize = 8 + Marshal.SizeOf(typeof(NativeMethods.DiskExtent)) * numExtents;

            byte[] buffer = new byte[bufferSize];

            int bytesRet = 0;

            if (!NativeMethods.DeviceIoControl(_handle, NativeMethods.EIOControlCode.VolumeGetDiskExtents, null, 0, buffer, bufferSize, ref bytesRet, IntPtr.Zero))
            {
                if (Marshal.GetLastWin32Error() != NativeMethods.ERROR_MORE_DATA)
                {
                    throw Win32Wrapper.GetIOExceptionForLastError();
                }

                numExtents = Marshal.ReadInt32(buffer, 0);
                bufferSize = 8 + Marshal.SizeOf(typeof(NativeMethods.DiskExtent)) * numExtents;
                buffer     = new byte[bufferSize];

                if (!NativeMethods.DeviceIoControl(_handle, NativeMethods.EIOControlCode.VolumeGetDiskExtents, null, 0, buffer, bufferSize, ref bytesRet, IntPtr.Zero))
                {
                    throw Win32Wrapper.GetIOExceptionForLastError();
                }
            }

            return(Win32Wrapper.ByteArrayToStructureArray <NativeMethods.DiskExtent>(buffer, 8, 1));
        }
Esempio n. 3
0
        public static SafeFileHandle OpenFileHandle(string path)
        {
            SafeFileHandle handle = NativeMethods.CreateFileW(path, FileAccess.Read, FileShare.Read, IntPtr.Zero, FileMode.Open, 0, IntPtr.Zero);

            if (handle.IsInvalid)
            {
                throw Win32Wrapper.GetIOExceptionForLastError();
            }
            return(handle);
        }
Esempio n. 4
0
        public static NativeMethods.NtfsVolumeData GetNtfsVolumeData(SafeFileHandle volumeHandle)
        {
            NativeMethods.NtfsVolumeData volumeData = new NativeMethods.NtfsVolumeData();
            int bytesRet = Marshal.SizeOf(volumeData);

            if (!NativeMethods.DeviceIoControl(volumeHandle, NativeMethods.EIOControlCode.FsctlGetNtfsVolumeData, null, 0, volumeData, bytesRet, ref bytesRet, IntPtr.Zero))
            {
                throw Win32Wrapper.GetIOExceptionForLastError();
            }
            return(volumeData);
        }
Esempio n. 5
0
        public static NativeMethods.DiskGeometry GetDiskGeometry(SafeFileHandle handle)
        {
            NativeMethods.DiskGeometry diskGeometry = new NativeMethods.DiskGeometry();
            int bytesRet = Marshal.SizeOf(diskGeometry);

            if (!NativeMethods.DeviceIoControl(handle, NativeMethods.EIOControlCode.DiskGetDriveGeometry, null, 0, diskGeometry, bytesRet, ref bytesRet, IntPtr.Zero))
            {
                throw Win32Wrapper.GetIOExceptionForLastError();
            }
            return(diskGeometry);
        }
Esempio n. 6
0
        public static long GetDiskCapacity(SafeFileHandle diskHandle)
        {
            byte[] sizeBytes = new byte[8];
            int    bytesRet  = sizeBytes.Length;

            if (!NativeMethods.DeviceIoControl(diskHandle, NativeMethods.EIOControlCode.DiskGetLengthInfo, null, 0, sizeBytes, bytesRet, ref bytesRet, IntPtr.Zero))
            {
                throw Win32Wrapper.GetIOExceptionForLastError();
            }

            return(BitConverter.ToInt64(sizeBytes, 0));
        }
        public override int Read(byte[] buffer, int offset, int count)
        {
            int  totalBytesRead = 0;
            long length         = Length;

            while (totalBytesRead < count)
            {
                long alignedStart    = (_position / Alignment) * Alignment;
                int  alignmentOffset = (int)(_position - alignedStart);

                long newPos;
                if (!NativeMethods.SetFilePointerEx(_handle, alignedStart, out newPos, 0))
                {
                    throw Win32Wrapper.GetIOExceptionForLastError();
                }

                int toRead = (int)Math.Min(length - alignedStart, BufferSize);
                int numRead;
                if (!NativeMethods.ReadFile(_handle, _buffer, toRead, out numRead, IntPtr.Zero))
                {
                    throw Win32Wrapper.GetIOExceptionForLastError();
                }

                int usefulData = numRead - alignmentOffset;
                if (usefulData <= 0)
                {
                    return(totalBytesRead);
                }

                int toCopy = Math.Min(count - totalBytesRead, usefulData);

                Marshal.Copy(_buffer + alignmentOffset, buffer, offset + totalBytesRead, toCopy);

                totalBytesRead += toCopy;
                _position      += toCopy;
            }

            return(totalBytesRead);
        }
        public Volume(string path, long length)
        {
            _path   = path.TrimEnd('\\');
            _length = length;

            if (!_path.StartsWith(@"\\"))
            {
                _path = @"\\.\" + _path;
            }

            _handle = NativeMethods.CreateFileW(_path, FileAccess.Read, FileShare.ReadWrite, IntPtr.Zero, FileMode.Open, 0, IntPtr.Zero);
            if (_handle.IsInvalid)
            {
                throw Win32Wrapper.GetIOExceptionForLastError();
            }

            // Enable reading the full contents of the volume (not just the region bounded by the file system)
            int bytesRet = 0;

            if (!NativeMethods.DeviceIoControl(_handle, NativeMethods.EIOControlCode.FsctlAllowExtendedDasdIo, IntPtr.Zero, 0, IntPtr.Zero, 0, ref bytesRet, IntPtr.Zero))
            {
                throw Win32Wrapper.GetIOExceptionForLastError();
            }
        }
Esempio n. 9
0
 public Disk(uint number)
 {
     _path   = @"\\.\PhysicalDrive" + number;
     _handle = Win32Wrapper.OpenFileHandle(_path);
 }
Esempio n. 10
0
 public DiskStream(SafeFileHandle handle)
     : base(handle)
 {
     _length = Win32Wrapper.GetDiskCapacity(handle);
 }