예제 #1
0
        public override void Sync(long totalUnsynced)
        {
            if (DisposeOnceRunner.Disposed)
            {
                ThrowAlreadyDisposedException();
            }

            if ((_fileAttributes & Win32NativeFileAttributes.Temporary) == Win32NativeFileAttributes.Temporary ||
                (_fileAttributes & Win32NativeFileAttributes.DeleteOnClose) == Win32NativeFileAttributes.DeleteOnClose)
            {
                return;
            }

            using (var metric = Options.IoMetrics.MeterIoRate(FileName.FullPath, IoMetrics.MeterType.DataSync, 0))
            {
                metric.IncrementSize(totalUnsynced);
                metric.SetFileSize(_totalAllocationSize);

                if (Win32MemoryMapNativeMethods.FlushFileBuffers(_handle) == false)
                {
                    var lastWin32Error = Marshal.GetLastWin32Error();
                    throw new Win32Exception($"Unable to flush file buffers on {FileName}",
                                             new Win32Exception(lastWin32Error));
                }
            }
        }
예제 #2
0
            protected override void Dispose(bool disposing)
            {
                if (stream != null)
                {
                    stream.Dispose();
                }

                GC.SuppressFinalize(this);

                if (isOriginal == false)
                {
                    return;
                }

                cts.Cancel();
                if (basePtr != null)
                {
                    Win32MemoryMapNativeMethods.UnmapViewOfFile(basePtr);
                }
                if (mmf != IntPtr.Zero)
                {
                    Win32NativeMethods.CloseHandle(mmf);
                }
                if (fileHandle != null)
                {
                    fileHandle.Close();
                }
            }
예제 #3
0
 public override void Sync()
 {
     if (Win32MemoryMapNativeMethods.FlushFileBuffers(_handle) == false)
     {
         throw new Win32Exception();
     }
 }
예제 #4
0
            public CodecIndexInput(FileInfo file, Func <Stream, Stream> applyCodecs)
            {
                try
                {
                    this.file        = file;
                    this.applyCodecs = applyCodecs;
                    if (file.Length == 0)
                    {
                        stream = applyCodecs(Stream.Null);
                        return;
                    }

                    fileHandle = Win32NativeFileMethods.CreateFile(file.FullName,
                                                                   Win32NativeFileAccess.GenericRead,
                                                                   Win32NativeFileShare.Read | Win32NativeFileShare.Write | Win32NativeFileShare.Delete,
                                                                   IntPtr.Zero,
                                                                   Win32NativeFileCreationDisposition.OpenExisting,
                                                                   Win32NativeFileAttributes.RandomAccess,
                                                                   IntPtr.Zero);

                    if (fileHandle.IsInvalid)
                    {
                        const int ERROR_FILE_NOT_FOUND = 2;
                        if (Marshal.GetLastWin32Error() == ERROR_FILE_NOT_FOUND)
                        {
                            throw new FileNotFoundException(file.FullName);
                        }
                        throw new Win32Exception(Marshal.GetLastWin32Error(), "Could not open file " + file.FullName);
                    }

                    mmf = Win32MemoryMapNativeMethods.CreateFileMapping(fileHandle.DangerousGetHandle(), IntPtr.Zero, Win32MemoryMapNativeMethods.FileMapProtection.PageReadonly,
                                                                        0, 0, null);
                    if (mmf == IntPtr.Zero)
                    {
                        throw new Win32Exception(Marshal.GetLastWin32Error(), "Could not create file mapping for " + file.FullName);
                    }

                    basePtr = Win32MemoryMapNativeMethods.MapViewOfFileEx(mmf,
                                                                          Win32MemoryMapNativeMethods.NativeFileMapAccessType.Read,
                                                                          0, 0, UIntPtr.Zero, null);
                    if (basePtr == null)
                    {
                        throw new Win32Exception(Marshal.GetLastWin32Error(), "Could not map file " + file.FullName);
                    }

                    stream = applyCodecs(new MmapStream(file.FullName, basePtr, file.Length));
                }
                catch (Exception)
                {
                    Dispose(false);
                    throw;
                }
            }