示例#1
0
 public void Write(SafeFileHandle handle, byte *buffer, uint count, ref int written)
 {
     if (!WinNative.WriteFile(handle, buffer, count, ref written, IntPtr.Zero))
     {
         throw new Win32Exception();
     }
 }
示例#2
0
        private static void FSync(SafeFileHandle handle)
        {
#if !__MonoCS__ && !USE_UNIX_IO
            WinNative.FlushFileBuffers(handle);
#else
            Syscall.fsync(handle.DangerousGetHandle().ToInt32());
#endif
        }
示例#3
0
        public long GetFileSize(SafeFileHandle handle)
        {
            if (!WinNative.GetFileSizeEx(handle, out var size))
            {
                throw new Win32Exception();
            }

            return(size);
        }
示例#4
0
        public int Read(SafeFileHandle handle, byte *buffer, int offset, int count)
        {
            var read = 0;

            if (!WinNative.ReadFile(handle, buffer, count, ref read, 0))
            {
                throw new Win32Exception();
            }

            return(read);
        }
示例#5
0
        public static uint GetDriveSectorSize(string path)
        {
#if !__MonoCS__ && !USE_UNIX_IO
            uint size;
            uint dontcare;
            WinNative.GetDiskFreeSpace(Path.GetPathRoot(path), out dontcare, out size, out dontcare, out dontcare);
            return(size);
#else
            return(0);
#endif
        }
示例#6
0
        public void Seek(SafeFileHandle handle, long position, SeekOrigin origin)
        {
            var low  = (int)(position & 0xffffffff);
            var high = (int)(position >> 32);
            var f    = WinNative.SetFilePointer(handle, low, out high, WinNative.EMoveMethod.Begin);

            if (f == WinNative.INVALID_SET_FILE_POINTER)
            {
                throw new Win32Exception();
            }
        }
示例#7
0
        public void SetFileSize(SafeFileHandle handle, long count)
        {
            var low  = (int)(count & 0xffffffff);
            var high = (int)(count >> 32);

            WinNative.SetFilePointer(handle, low, out high, WinNative.EMoveMethod.Begin);
            if (!WinNative.SetEndOfFile(handle))
            {
                throw new Win32Exception();
            }

            FSync(handle);
        }
示例#8
0
        //TODO UNBUFF use FileAccess etc or do custom?
        public SafeFileHandle Create(string path, FileAccess acc, FileShare readWrite, FileMode mode, int flags)
        {
            var handle = WinNative.CreateFile(path,
                                              acc,
                                              FileShare.ReadWrite,
                                              IntPtr.Zero,
                                              mode,
                                              flags,
                                              IntPtr.Zero);

            if (handle.IsInvalid)
            {
                throw new Win32Exception();
            }

            return(handle);
        }
示例#9
0
        public static void Seek(SafeFileHandle handle, long position, SeekOrigin origin)
        {
#if !__MonoCS__ && !USE_UNIX_IO
            var low  = (int)(position & 0xffffffff);
            var high = (int)(position >> 32);
            var f    = WinNative.SetFilePointer(handle, low, out high, WinNative.EMoveMethod.Begin);
            if (f == WinNative.INVALID_SET_FILE_POINTER)
            {
                throw new Win32Exception();
            }
#else
            int r = 0;
            do
            {
                r = (int)Syscall.lseek(handle.DangerousGetHandle().ToInt32(), position, SeekFlags.SEEK_SET);
            } while (UnixMarshal.ShouldRetrySyscall(r));
            UnixMarshal.ThrowExceptionForLastErrorIf(r);
#endif
        }
示例#10
0
        public static SafeFileHandle CreateUnbufferedRW(string path, FileAccess acc, FileShare share, FileMode mode, bool writeThrough)
        {
#if !__MonoCS__ && !USE_UNIX_IO
            var flags = ExtendedFileOptions.NoBuffering;
            if (writeThrough)
            {
                flags = flags | ExtendedFileOptions.WriteThrough;
            }
            var handle = WinNative.CreateFile(path,
                                              acc,
                                              share,
                                              IntPtr.Zero,
                                              FileMode.OpenOrCreate,
                                              (int)flags,
                                              IntPtr.Zero);
            if (handle.IsInvalid)
            {
                throw new Win32Exception();
            }
            return(handle);
#else
            var ismac = OS.OsFlavor == OsFlavor.MacOS;
            //O_RDONLY is 0
            var direct = ismac ? OpenFlags.O_RDONLY : OpenFlags.O_DIRECT;
            var flags  = GetFlags(acc, mode) | direct;
            var han    = Syscall.open(path, flags, FilePermissions.S_IRWXU);
            if (han < 0)
            {
                throw new Win32Exception();
            }

            var handle = new SafeFileHandle((IntPtr)han, true);
            if (handle.IsInvalid)
            {
                throw new Exception("Invalid handle");
            }
            if (ismac)
            {
                TurnOffMacCaching(handle);
            }
            return(handle);
#endif
        }
示例#11
0
        public static long GetFileSize(SafeFileHandle handle)
        {
#if !__MonoCS__ && !USE_UNIX_IO
            long size = 0;
            if (!WinNative.GetFileSizeEx(handle, out size))
            {
                throw new Win32Exception();
            }
            return(size);
#else
            Stat s;
            int  r;
            do
            {
                r = (int)Syscall.fstat(handle.DangerousGetHandle().ToInt32(), out s);
            } while (UnixMarshal.ShouldRetrySyscall(r));
            UnixMarshal.ThrowExceptionForLastErrorIf(r);
            return(s.st_size);
#endif
        }
示例#12
0
        public static void Write(SafeFileHandle handle, byte *buffer, uint count, ref int written)
        {
#if !__MonoCS__ && !USE_UNIX_IO
            if (!WinNative.WriteFile(handle, buffer, count, ref written, IntPtr.Zero))
            {
                throw new Win32Exception();
            }
#else
            int ret = 0;
            do
            {
                ret = (int)Syscall.write(handle.DangerousGetHandle().ToInt32(), buffer, count);
            } while (Mono.Unix.UnixMarshal.ShouldRetrySyscall((int)ret));
            if (ret == -1)
            {
                Mono.Unix.UnixMarshal.ThrowExceptionForLastErrorIf((int)ret);
            }
            written = (int)count;
#endif
        }
示例#13
0
        public static void SetFileSize(SafeFileHandle handle, long count)
        {
#if !__MonoCS__ && !USE_UNIX_IO
            var low  = (int)(count & 0xffffffff);
            var high = (int)(count >> 32);
            WinNative.SetFilePointer(handle, low, out high, WinNative.EMoveMethod.Begin);
            if (!WinNative.SetEndOfFile(handle))
            {
                throw new Win32Exception();
            }
#else
            int r;
            do
            {
                r = Syscall.ftruncate(handle.DangerousGetHandle().ToInt32(), count);
            } while (UnixMarshal.ShouldRetrySyscall(r));
            UnixMarshal.ThrowExceptionForLastErrorIf(r);
#endif
            FSync(handle);
        }
示例#14
0
        //TODO UNBUFF use FileAccess etc or do custom?
        public static SafeFileHandle Create(string path, FileAccess acc, FileShare readWrite, FileMode mode, int flags)
        {
#if !__MonoCS__ && !USE_UNIX_IO
            var handle = WinNative.CreateFile(path,
                                              acc,
                                              FileShare.ReadWrite,
                                              IntPtr.Zero,
                                              mode,
                                              flags,
                                              IntPtr.Zero);
            if (handle.IsInvalid)
            {
                throw new Win32Exception();
            }
            return(handle);
#else
            //TODO convert flags or separate methods?
            return(new SafeFileHandle((IntPtr)0, true));
#endif
        }
示例#15
0
        public static int Read(SafeFileHandle handle, byte *buffer, int offset, int count)
        {
#if !__MonoCS__ && !USE_UNIX_IO
            var read = 0;

            if (!WinNative.ReadFile(handle, buffer, count, ref read, 0))
            {
                throw new Win32Exception();
            }
            return(read);
#else
            int r;
            do
            {
                r = (int)Syscall.read(handle.DangerousGetHandle().ToInt32(), buffer, (ulong)count);
            } while (UnixMarshal.ShouldRetrySyscall((int)r));
            if (r == -1)
            {
                UnixMarshal.ThrowExceptionForLastError();
            }
            return(count);
#endif
        }
示例#16
0
        public SafeFileHandle CreateUnbufferedRW(string path, FileAccess acc, FileShare share, FileMode mode,
                                                 bool writeThrough)
        {
            var flags = ExtendedFileOptions.NoBuffering;

            if (writeThrough)
            {
                flags = flags | ExtendedFileOptions.WriteThrough;
            }
            var handle = WinNative.CreateFile(path,
                                              acc,
                                              share,
                                              IntPtr.Zero,
                                              FileMode.OpenOrCreate,
                                              (int)flags,
                                              IntPtr.Zero);

            if (handle.IsInvalid)
            {
                throw new Win32Exception();
            }

            return(handle);
        }
示例#17
0
 public uint GetDriveSectorSize(string path)
 {
     WinNative.GetDiskFreeSpace(Path.GetPathRoot(path), out _, out var size, out _, out _);
     return(size);
 }
示例#18
0
 private static void FSync(SafeFileHandle handle)
 {
     WinNative.FlushFileBuffers(handle);
 }