public unsafe override bool ReadHeader(string filename, FileHeader *header)
            {
                var path = Path.Combine(_basePath, filename);

                if (File.Exists(path) == false)
                {
                    return(false);
                }
                using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    if (fs.Length != sizeof(FileHeader))
                    {
                        return(false);                // wrong file size
                    }
                    var ptr       = (byte *)header;
                    int remaining = sizeof(FileHeader);
                    while (remaining > 0)
                    {
                        int read;
                        if (NativeFileMethods.ReadFile(fs.SafeFileHandle, ptr, remaining, out read, null) == false)
                        {
                            throw new Win32Exception();
                        }
                        if (read == 0)
                        {
                            return(false);                    // we should be reading _something_ here, if we can't, then it is an error and we assume corruption
                        }
                        ptr       += read;
                        remaining -= read;
                    }
                    return(true);
                }
            }
Esempio n. 2
0
            public unsafe override bool ReadHeader(string filename, FileHeader *header)
            {
                var path = Path.Combine(_basePath, filename);

                if (File.Exists(path) == false)
                {
                    return(false);
                }
                using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    var ptr       = (byte *)header;
                    int remaining = sizeof(FileHeader);
                    while (remaining > 0)
                    {
                        int read;
                        if (NativeFileMethods.ReadFile(fs.SafeFileHandle, ptr, remaining, out read, null) == false)
                        {
                            throw new Win32Exception();
                        }
                        ptr       += read;
                        remaining -= read;
                    }
                    return(true);
                }
            }
Esempio n. 3
0
        public bool Read(long pageNumber, byte *buffer, int count)
        {
            if (_readHandle == null)
            {
                _readHandle = NativeFileMethods.CreateFile(_filename,
                                                           NativeFileAccess.GenericRead,
                                                           NativeFileShare.Write | NativeFileShare.Read | NativeFileShare.Delete,
                                                           IntPtr.Zero,
                                                           NativeFileCreationDisposition.OpenExisting,
                                                           NativeFileAttributes.Normal,
                                                           IntPtr.Zero);
            }

            long position   = pageNumber * AbstractPager.PageSize;
            var  overlapped = new Overlapped((int)(position & 0xffffffff), (int)(position >> 32), IntPtr.Zero, null);
            NativeOverlapped *nativeOverlapped = overlapped.Pack(null, null);

            try
            {
                while (count > 0)
                {
                    int read;
                    if (NativeFileMethods.ReadFile(_readHandle, buffer, count, out read, nativeOverlapped) == false)
                    {
                        int lastWin32Error = Marshal.GetLastWin32Error();
                        if (lastWin32Error == ErrorHandleEof)
                        {
                            return(false);
                        }
                        throw new Win32Exception(lastWin32Error);
                    }
                    count  -= read;
                    buffer += read;
                }
                return(true);
            }
            finally
            {
                Overlapped.Free(nativeOverlapped);
            }
        }