Switch to compiler-tester
[mono.git] / mcs / class / Mono.Posix / Mono.Unix / UnixConvert.cs
index 18405eb8bccd916e30c3be1d60224a969bf209c6..0b169c2dd6b0f0142b66567e346436cc2e359887 100644 (file)
@@ -599,34 +599,34 @@ namespace Mono.Unix {
                        return rval;
                }
 
-               [DllImport (LIB, EntryPoint="Mono_Posix_FromLockFlags")]
-               private static extern int FromLockFlags (LockFlags value, out Int32 rval);
+               [DllImport (LIB, EntryPoint="Mono_Posix_FromLockfCommand")]
+               private static extern int FromLockfCommand (LockfCommand value, out Int32 rval);
 
-               public static bool TryFromLockFlags (LockFlags value, out Int32 rval)
+               public static bool TryFromLockfCommand (LockfCommand value, out Int32 rval)
                {
-                       return FromLockFlags (value, out rval) == 0;
+                       return FromLockfCommand (value, out rval) == 0;
                }
 
-               public static Int32 FromLockFlags (LockFlags value)
+               public static Int32 FromLockfCommand (LockfCommand value)
                {
                        Int32 rval;
-                       if (FromLockFlags (value, out rval) == -1)
+                       if (FromLockfCommand (value, out rval) == -1)
                                ThrowArgumentException (value);
                        return rval;
                }
 
-               [DllImport (LIB, EntryPoint="Mono_Posix_ToLockFlags")]
-               private static extern int ToLockFlags (Int32 value, out LockFlags rval);
+               [DllImport (LIB, EntryPoint="Mono_Posix_ToLockfCommand")]
+               private static extern int ToLockfCommand (Int32 value, out LockfCommand rval);
 
-               public static bool TryToLockFlags (Int32 value, out LockFlags rval)
+               public static bool TryToLockfCommand (Int32 value, out LockfCommand rval)
                {
-                       return ToLockFlags (value, out rval) == 0;
+                       return ToLockfCommand (value, out rval) == 0;
                }
 
-               public static LockFlags ToLockFlags (Int32 value)
+               public static LockfCommand ToLockfCommand (Int32 value)
                {
-                       LockFlags rval;
-                       if (ToLockFlags (value, out rval) == -1)
+                       LockfCommand rval;
+                       if (ToLockfCommand (value, out rval) == -1)
                                ThrowArgumentException (value);
                        return rval;
                }
@@ -695,17 +695,349 @@ namespace Mono.Unix {
                        return rval;
                }
 
+               [DllImport (LIB, EntryPoint="Mono_Posix_FromMountFlags")]
+               private static extern int FromMountFlags (MountFlags value, out UInt64 rval);
+
+               public static bool TryFromMountFlags (MountFlags value, out UInt64 rval)
+               {
+                       return FromMountFlags (value, out rval) == 0;
+               }
+
+               public static UInt64 FromMountFlags (MountFlags value)
+               {
+                       UInt64 rval;
+                       if (FromMountFlags (value, out rval) == -1)
+                               ThrowArgumentException (value);
+                       return rval;
+               }
+
+               [DllImport (LIB, EntryPoint="Mono_Posix_ToMountFlags")]
+               private static extern int ToMountFlags (UInt64 value, out MountFlags rval);
+
+               public static bool TryToMountFlags (UInt64 value, out MountFlags rval)
+               {
+                       return ToMountFlags (value, out rval) == 0;
+               }
+
+               public static MountFlags ToMountFlags (UInt64 value)
+               {
+                       MountFlags rval;
+                       if (ToMountFlags (value, out rval) == -1)
+                               ThrowArgumentException (value);
+                       return rval;
+               }
+
+               [DllImport (LIB, EntryPoint="Mono_Posix_FromMmapFlags")]
+               private static extern int FromMmapFlags (MmapFlags value, out Int32 rval);
+
+               public static bool TryFromMmapFlags (MmapFlags value, out Int32 rval)
+               {
+                       return FromMmapFlags (value, out rval) == 0;
+               }
+
+               public static Int32 FromMmapFlags (MmapFlags value)
+               {
+                       Int32 rval;
+                       if (FromMmapFlags (value, out rval) == -1)
+                               ThrowArgumentException (value);
+                       return rval;
+               }
+
+               [DllImport (LIB, EntryPoint="Mono_Posix_ToMmapFlags")]
+               private static extern int ToMmapFlags (Int32 value, out MmapFlags rval);
+
+               public static bool TryToMmapFlags (Int32 value, out MmapFlags rval)
+               {
+                       return ToMmapFlags (value, out rval) == 0;
+               }
+
+               public static MmapFlags ToMmapFlags (Int32 value)
+               {
+                       MmapFlags rval;
+                       if (ToMmapFlags (value, out rval) == -1)
+                               ThrowArgumentException (value);
+                       return rval;
+               }
+
+               [DllImport (LIB, EntryPoint="Mono_Posix_FromMmapProt")]
+               private static extern int FromMmapProt (MmapProt value, out Int32 rval);
+
+               public static bool TryFromMmapProt (MmapProt value, out Int32 rval)
+               {
+                       return FromMmapProt (value, out rval) == 0;
+               }
+
+               public static Int32 FromMmapProt (MmapProt value)
+               {
+                       Int32 rval;
+                       if (FromMmapProt (value, out rval) == -1)
+                               ThrowArgumentException (value);
+                       return rval;
+               }
+
+               [DllImport (LIB, EntryPoint="Mono_Posix_ToMmapProt")]
+               private static extern int ToMmapProt (Int32 value, out MmapProt rval);
+
+               public static bool TryToMmapProt (Int32 value, out MmapProt rval)
+               {
+                       return ToMmapProt (value, out rval) == 0;
+               }
+
+               public static MmapProt ToMmapProt (Int32 value)
+               {
+                       MmapProt rval;
+                       if (ToMmapProt (value, out rval) == -1)
+                               ThrowArgumentException (value);
+                       return rval;
+               }
+
+               [DllImport (LIB, EntryPoint="Mono_Posix_FromMsyncFlags")]
+               private static extern int FromMsyncFlags (MsyncFlags value, out Int32 rval);
+
+               public static bool TryFromMsyncFlags (MsyncFlags value, out Int32 rval)
+               {
+                       return FromMsyncFlags (value, out rval) == 0;
+               }
+
+               public static Int32 FromMsyncFlags (MsyncFlags value)
+               {
+                       Int32 rval;
+                       if (FromMsyncFlags (value, out rval) == -1)
+                               ThrowArgumentException (value);
+                       return rval;
+               }
+
+               [DllImport (LIB, EntryPoint="Mono_Posix_ToMsyncFlags")]
+               private static extern int ToMsyncFlags (Int32 value, out MsyncFlags rval);
+
+               public static bool TryToMsyncFlags (Int32 value, out MsyncFlags rval)
+               {
+                       return ToMsyncFlags (value, out rval) == 0;
+               }
+
+               public static MsyncFlags ToMsyncFlags (Int32 value)
+               {
+                       MsyncFlags rval;
+                       if (ToMsyncFlags (value, out rval) == -1)
+                               ThrowArgumentException (value);
+                       return rval;
+               }
+
+               [DllImport (LIB, EntryPoint="Mono_Posix_FromMlockallFlags")]
+               private static extern int FromMlockallFlags (MlockallFlags value, out Int32 rval);
+
+               public static bool TryFromMlockallFlags (MlockallFlags value, out Int32 rval)
+               {
+                       return FromMlockallFlags (value, out rval) == 0;
+               }
+
+               public static Int32 FromMlockallFlags (MlockallFlags value)
+               {
+                       Int32 rval;
+                       if (FromMlockallFlags (value, out rval) == -1)
+                               ThrowArgumentException (value);
+                       return rval;
+               }
+
+               [DllImport (LIB, EntryPoint="Mono_Posix_ToMlockallFlags")]
+               private static extern int ToMlockallFlags (Int32 value, out MlockallFlags rval);
+
+               public static bool TryToMlockallFlags (Int32 value, out MlockallFlags rval)
+               {
+                       return ToMlockallFlags (value, out rval) == 0;
+               }
+
+               public static MlockallFlags ToMlockallFlags (Int32 value)
+               {
+                       MlockallFlags rval;
+                       if (ToMlockallFlags (value, out rval) == -1)
+                               ThrowArgumentException (value);
+                       return rval;
+               }
+
+               [DllImport (LIB, EntryPoint="Mono_Posix_FromMremapFlags")]
+               private static extern int FromMremapFlags (MremapFlags value, out UInt64 rval);
+
+               public static bool TryFromMremapFlags (MremapFlags value, out UInt64 rval)
+               {
+                       return FromMremapFlags (value, out rval) == 0;
+               }
+
+               public static UInt64 FromMremapFlags (MremapFlags value)
+               {
+                       UInt64 rval;
+                       if (FromMremapFlags (value, out rval) == -1)
+                               ThrowArgumentException (value);
+                       return rval;
+               }
+
+               [DllImport (LIB, EntryPoint="Mono_Posix_ToMremapFlags")]
+               private static extern int ToMremapFlags (UInt64 value, out MremapFlags rval);
+
+               public static bool TryToMremapFlags (UInt64 value, out MremapFlags rval)
+               {
+                       return ToMremapFlags (value, out rval) == 0;
+               }
+
+               public static MremapFlags ToMremapFlags (UInt64 value)
+               {
+                       MremapFlags rval;
+                       if (ToMremapFlags (value, out rval) == -1)
+                               ThrowArgumentException (value);
+                       return rval;
+               }
+
                //
                // Non-generated exports
                //
 
                // convert from octal representation.
-               public static FilePermissions ToFilePermissions (string value)
+               public static FilePermissions FromOctalPermissionString (string value)
                {
                        uint n = Convert.ToUInt32 (value, 8);
                        return ToFilePermissions (n);
                }
 
+               public static string ToOctalPermissionString (FilePermissions value)
+               {
+                       string s = Convert.ToString ((int) (value & ~FilePermissions.S_IFMT), 8);
+                       return new string ('0', 4-s.Length) + s;
+               }
+
+               public static FilePermissions FromUnixPermissionString (string value)
+               {
+                       if (value == null)
+                               throw new ArgumentNullException ("value");
+                       if (value.Length != 9 && value.Length != 10)
+                               throw new ArgumentException ("value", "must contain 9 or 10 characters");
+
+                       int i = 0;
+                       FilePermissions perms = new FilePermissions ();
+
+                       if (value.Length == 10) {
+                               perms |= GetUnixPermissionDevice (value [i]);
+                               ++i;
+                       }
+
+                       perms |= GetUnixPermissionGroup (
+                               value [i++], FilePermissions.S_IRUSR,
+                               value [i++], FilePermissions.S_IWUSR,
+                               value [i++], FilePermissions.S_IXUSR,
+                               's', 'S', FilePermissions.S_ISUID);
+
+                       perms |= GetUnixPermissionGroup (
+                               value [i++], FilePermissions.S_IRGRP,
+                               value [i++], FilePermissions.S_IWGRP,
+                               value [i++], FilePermissions.S_IXGRP,
+                               's', 'S', FilePermissions.S_ISGID);
+
+                       perms |= GetUnixPermissionGroup (
+                               value [i++], FilePermissions.S_IROTH,
+                               value [i++], FilePermissions.S_IWOTH,
+                               value [i++], FilePermissions.S_IXOTH,
+                               't', 'T', FilePermissions.S_ISVTX);
+
+                       return perms;
+               }
+
+               private static FilePermissions GetUnixPermissionDevice (char value)
+               {
+                       switch (value) {
+                       case 'd': return FilePermissions.S_IFDIR;
+                       case 'c': return FilePermissions.S_IFCHR;
+                       case 'b': return FilePermissions.S_IFBLK;
+                       case '-': return FilePermissions.S_IFREG;
+                       case 'p': return FilePermissions.S_IFIFO;
+                       case 'l': return FilePermissions.S_IFLNK;
+                       case 's': return FilePermissions.S_IFSOCK;
+                       }
+                       throw new ArgumentException ("value", "invalid device specification: " + 
+                               value);
+               }
+
+               private static FilePermissions GetUnixPermissionGroup (
+                       char read, FilePermissions readb, 
+                       char write, FilePermissions writeb, 
+                       char exec, FilePermissions execb,
+                       char xboth, char xbitonly, FilePermissions xbit)
+               {
+                       FilePermissions perms = new FilePermissions ();
+                       if (read == 'r')
+                               perms |= readb;
+                       if (write == 'w')
+                               perms |= writeb;
+                       if (exec == 'x')
+                               perms |= execb;
+                       else if (exec == xbitonly)
+                               perms |= xbit;
+                       else if (exec == xboth)
+                               perms |= (execb | xbit);
+                       return perms;
+               }
+
+               // Create ls(1) drwxrwxrwx permissions display
+               public static string ToUnixPermissionString (FilePermissions value)
+               {
+                       char [] access = new char[] {
+                               '-',            // device
+                               '-', '-', '-',  // owner
+                               '-', '-', '-',  // group
+                               '-', '-', '-',  // other
+                       };
+                       bool have_device = true;
+                       switch (value & FilePermissions.S_IFMT) {
+                               case FilePermissions.S_IFDIR:   access [0] = 'd'; break;
+                               case FilePermissions.S_IFCHR:   access [0] = 'c'; break;
+                               case FilePermissions.S_IFBLK:   access [0] = 'b'; break;
+                               case FilePermissions.S_IFREG:   access [0] = '-'; break;
+                               case FilePermissions.S_IFIFO:   access [0] = 'p'; break;
+                               case FilePermissions.S_IFLNK:   access [0] = 'l'; break;
+                               case FilePermissions.S_IFSOCK:  access [0] = 's'; break;
+                               default:                        have_device = false; break;
+                       }
+                       SetUnixPermissionGroup (value, access, 1, 
+                               FilePermissions.S_IRUSR, FilePermissions.S_IWUSR, FilePermissions.S_IXUSR,
+                               's', 'S', FilePermissions.S_ISUID);
+                       SetUnixPermissionGroup (value, access, 4, 
+                               FilePermissions.S_IRGRP, FilePermissions.S_IWGRP, FilePermissions.S_IXGRP,
+                               's', 'S', FilePermissions.S_ISGID);
+                       SetUnixPermissionGroup (value, access, 7, 
+                               FilePermissions.S_IROTH, FilePermissions.S_IWOTH, FilePermissions.S_IXOTH,
+                               't', 'T', FilePermissions.S_ISVTX);
+                       return have_device 
+                               ? new string (access)
+                               : new string (access, 1, 9);
+               }
+
+               private static void SetUnixPermissionGroup (FilePermissions value,
+                       char[] access, int index,
+                       FilePermissions read, FilePermissions write, FilePermissions exec,
+                       char both, char setonly, FilePermissions setxbit)
+               {
+                       if (UnixFileSystemInfo.IsType (value, read))
+                               access [index] = 'r';
+                       if (UnixFileSystemInfo.IsType (value, write))
+                               access [index+1] = 'w';
+                       access [index+2] = GetSymbolicMode (value, exec, both, setonly, setxbit);
+               }
+
+               // Implement the GNU ls(1) permissions spec; see `info coreutils ls`,
+               // section 10.1.2, the `-l' argument information.
+               private static char GetSymbolicMode (FilePermissions value, 
+                       FilePermissions xbit, char both, char setonly, FilePermissions setxbit)
+               {
+                       bool is_x  = UnixFileSystemInfo.IsType (value, xbit);
+                       bool is_sx = UnixFileSystemInfo.IsType (value, setxbit);
+                       
+                       if (is_x && is_sx)
+                               return both;
+                       if (is_sx)
+                               return setonly;
+                       if (is_x)
+                               return 'x';
+                       return '-';
+               }
+
                public static readonly DateTime LocalUnixEpoch = 
                        new DateTime (1970, 1, 1).ToLocalTime();