- //
- // Turns the FileMode into the first half of open(2) flags
- //
- static OpenFlags ToUnixMode (FileMode mode)
- {
- switch (mode){
- case FileMode.CreateNew:
- return OpenFlags.O_CREAT | OpenFlags.O_EXCL;
-
- case FileMode.Create:
- return OpenFlags.O_CREAT | OpenFlags.O_TRUNC;
-
- case FileMode.OpenOrCreate:
- return OpenFlags.O_CREAT;
-
- case FileMode.Truncate:
- return OpenFlags.O_TRUNC;
-
- case FileMode.Append:
- return OpenFlags.O_APPEND;
- default:
- case FileMode.Open:
- return 0;
- }
- }
-
- //
- // Turns the MemoryMappedFileAccess into the second half of open(2) flags
- //
- static OpenFlags ToUnixMode (MemoryMappedFileAccess access)
- {
- switch (access){
- case MemoryMappedFileAccess.CopyOnWrite:
- case MemoryMappedFileAccess.ReadWriteExecute:
- case MemoryMappedFileAccess.ReadWrite:
- return OpenFlags.O_RDWR;
-
- case MemoryMappedFileAccess.Write:
- return OpenFlags.O_WRONLY;
-
- case MemoryMappedFileAccess.ReadExecute:
- case MemoryMappedFileAccess.Read:
- default:
- return OpenFlags.O_RDONLY;
- }
- }
-
- static MmapProts ToUnixProts (MemoryMappedFileAccess access)
- {
- switch (access){
- case MemoryMappedFileAccess.ReadWrite:
- return MmapProts.PROT_WRITE | MmapProts.PROT_READ;
-
- case MemoryMappedFileAccess.Write:
- return MmapProts.PROT_WRITE;
-
- case MemoryMappedFileAccess.CopyOnWrite:
- return MmapProts.PROT_WRITE | MmapProts.PROT_READ;
-
- case MemoryMappedFileAccess.ReadExecute:
- return MmapProts.PROT_EXEC;
-
- case MemoryMappedFileAccess.ReadWriteExecute:
- return MmapProts.PROT_WRITE | MmapProts.PROT_READ | MmapProts.PROT_EXEC;
-
- case MemoryMappedFileAccess.Read:
- default:
- return MmapProts.PROT_READ;
- }
- }
-
- internal static int Open (string path, FileMode mode, ref long capacity, MemoryMappedFileAccess access)
- {
- if (MonoUtil.IsUnix){
- Stat buf;
- if (Syscall.stat (path, out buf) == -1)
- UnixMarshal.ThrowExceptionForLastError ();
-
- if (capacity == 0) {
- // Special files such as FIFOs, sockets, and devices can
- // have a size of 0. Specifying a capacity for these
- // also makes little sense, so don't do the check if the
- // file is one of these.
- if (buf.st_size == 0 &&
- (buf.st_mode & (FilePermissions.S_IFCHR |
- FilePermissions.S_IFBLK |
- FilePermissions.S_IFIFO |
- FilePermissions.S_IFSOCK)) == 0) {
- throw new ArgumentException ("A positive capacity must be specified for a Memory Mapped File backed by an empty file.");
- }
-
- capacity = buf.st_size;
- } else if (capacity < buf.st_size) {
- throw new ArgumentException ("The capacity may not be smaller than the file size.");
- }
-
- int fd = Syscall.open (path, ToUnixMode (mode) | ToUnixMode (access), FilePermissions.DEFFILEMODE);
-
- if (fd == -1)
- UnixMarshal.ThrowExceptionForLastError ();
- return fd;
- }
-
- throw new NotImplementedException ();
- }
-
- internal static void CloseFD (int fd) {
- Syscall.close (fd);
- }
-
- internal static void Flush (int fd) {
- if (MonoUtil.IsUnix)
- Syscall.fsync (fd);
- else
- throw new NotImplementedException ("Not implemented on Windows");
-
- }
-
- static int pagesize;
-
- internal static unsafe void Map (int file_handle, long offset, ref long size, MemoryMappedFileAccess access, out IntPtr map_addr, out int offset_diff)
- {
- if (!MonoUtil.IsUnix)
- throw new NotImplementedException ("Not implemented on windows.");
-
- if (pagesize == 0)
- pagesize = Syscall.getpagesize ();
-
- Stat buf;
- Syscall.fstat (file_handle, out buf);
- long fsize = buf.st_size;
-
- if (size == 0 || size > fsize)
- size = fsize;
-
- // Align offset
- long real_offset = offset & ~(pagesize - 1);
-
- offset_diff = (int)(offset - real_offset);
-
- // FIXME: Need to determine the unix fd for the file, Handle is only
- // equal to it by accident
- //
- // The new API no longer uses FileStream everywhere, but exposes instead
- // the filename (with one exception), we could move this API to use
- // file descriptors instead of the FileStream plus its Handle.
- //
- map_addr = Syscall.mmap (IntPtr.Zero, (ulong) size,
- ToUnixProts (access),
- access == MemoryMappedFileAccess.CopyOnWrite ? MmapFlags.MAP_PRIVATE : MmapFlags.MAP_SHARED,
- file_handle, real_offset);
-
- if (map_addr == (IntPtr)(-1))
- throw new IOException ("mmap failed for fd#" + file_handle + "(" + offset + ", " + size + ")");
- }
-
- internal static bool Unmap (IntPtr map_addr, ulong map_size)
- {
- if (!MonoUtil.IsUnix)
- return false;
- return Syscall.munmap (map_addr, map_size) == 0;
- }
-
- static void ConfigureUnixFD (IntPtr handle, HandleInheritability h)
- {
- // TODO: Mono.Posix is lacking O_CLOEXEC definitions for fcntl.
- }
-
-
- [DllImport("kernel32", SetLastError = true)]
- static extern bool SetHandleInformation (IntPtr hObject, int dwMask, int dwFlags);
- static void ConfigureWindowsFD (IntPtr handle, HandleInheritability h)
- {
- SetHandleInformation (handle, 1 /* FLAG_INHERIT */, h == HandleInheritability.None ? 0 : 1);
- }
-
- internal static void ConfigureFD (IntPtr handle, HandleInheritability inheritability)
- {
- if (MonoUtil.IsUnix)
- ConfigureUnixFD (handle, inheritability);
- else
- ConfigureWindowsFD (handle, inheritability);
- }
-
- }
-#else
- internal static class MemoryMapImpl {
- [DllImport ("libc")]
- static extern int fsync (int fd);
-
- [DllImport ("libc")]
- static extern int close (int fd);
-
- [DllImport ("libc")]
- static extern int fcntl (int fd, int cmd, int arg0);
-
- //XXX check if android off_t is 64bits or not. on iOS / darwin it is.
- [DllImport ("libc")]
- static extern IntPtr mmap (IntPtr addr, IntPtr len, int prot, int flags, int fd, long offset);
-
- [DllImport ("libc")]
- static extern int munmap (IntPtr addr, IntPtr size);
-
- [DllImport ("libc", SetLastError=true)]
- static extern int open (string path, int flags, int access);
-
-#if MONODROID
- [DllImport ("__Internal")]
- static extern int monodroid_getpagesize ();
-
- static int getpagesize ()
- {
- return monodroid_getpagesize ();
- }
-#else
- [DllImport ("libc")]
- static extern int getpagesize ();
-#endif
-