2 * This file was automatically generated by make-map from Mono.Posix.dll.
9 using System.Runtime.InteropServices;
10 using Mono.Unix.Native;
12 using Mono.Unix.Android;
15 namespace Mono.Unix.Native {
17 [CLSCompliant (false)]
18 public sealed /* static */ partial class NativeConvert
21 // Non-generated exports
24 [DllImport (LIB, EntryPoint="Mono_Posix_FromRealTimeSignum")]
25 static extern int HelperFromRealTimeSignum (Int32 offset, out Int32 rval);
27 static int FromRealTimeSignum (Int32 offset, out Int32 rval)
29 if (!AndroidUtils.AreRealTimeSignalsSafe ())
30 throw new PlatformNotSupportedException ("Real-time signals are not supported on this Android architecture");
31 return HelperFromRealTimeSignum (offset, out rval);
34 [DllImport (LIB, EntryPoint="Mono_Posix_FromRealTimeSignum")]
35 private static extern int FromRealTimeSignum (Int32 offset, out Int32 rval);
37 // convert a realtime signal to os signal
38 public static int FromRealTimeSignum (RealTimeSignum sig)
41 if (FromRealTimeSignum (sig.Offset, out sigNum) == -1)
42 ThrowArgumentException (sig.Offset);
46 // convert an offset to an rt signum
47 public static RealTimeSignum ToRealTimeSignum (int offset)
49 return new RealTimeSignum (offset);
52 // convert from octal representation.
53 public static FilePermissions FromOctalPermissionString (string value)
55 uint n = Convert.ToUInt32 (value, 8);
56 return ToFilePermissions (n);
59 public static string ToOctalPermissionString (FilePermissions value)
61 string s = Convert.ToString ((int) (value & ~FilePermissions.S_IFMT), 8);
62 return new string ('0', 4-s.Length) + s;
65 public static FilePermissions FromUnixPermissionString (string value)
68 throw new ArgumentNullException ("value");
69 if (value.Length != 9 && value.Length != 10)
70 throw new ArgumentException ("value", "must contain 9 or 10 characters");
73 FilePermissions perms = new FilePermissions ();
75 if (value.Length == 10) {
76 perms |= GetUnixPermissionDevice (value [i]);
80 perms |= GetUnixPermissionGroup (
81 value [i++], FilePermissions.S_IRUSR,
82 value [i++], FilePermissions.S_IWUSR,
83 value [i++], FilePermissions.S_IXUSR,
84 's', 'S', FilePermissions.S_ISUID);
86 perms |= GetUnixPermissionGroup (
87 value [i++], FilePermissions.S_IRGRP,
88 value [i++], FilePermissions.S_IWGRP,
89 value [i++], FilePermissions.S_IXGRP,
90 's', 'S', FilePermissions.S_ISGID);
92 perms |= GetUnixPermissionGroup (
93 value [i++], FilePermissions.S_IROTH,
94 value [i++], FilePermissions.S_IWOTH,
95 value [i++], FilePermissions.S_IXOTH,
96 't', 'T', FilePermissions.S_ISVTX);
101 private static FilePermissions GetUnixPermissionDevice (char value)
104 case 'd': return FilePermissions.S_IFDIR;
105 case 'c': return FilePermissions.S_IFCHR;
106 case 'b': return FilePermissions.S_IFBLK;
107 case '-': return FilePermissions.S_IFREG;
108 case 'p': return FilePermissions.S_IFIFO;
109 case 'l': return FilePermissions.S_IFLNK;
110 case 's': return FilePermissions.S_IFSOCK;
112 throw new ArgumentException ("value", "invalid device specification: " +
116 private static FilePermissions GetUnixPermissionGroup (
117 char read, FilePermissions readb,
118 char write, FilePermissions writeb,
119 char exec, FilePermissions execb,
120 char xboth, char xbitonly, FilePermissions xbit)
122 FilePermissions perms = new FilePermissions ();
129 else if (exec == xbitonly)
131 else if (exec == xboth)
132 perms |= (execb | xbit);
136 // Create ls(1) drwxrwxrwx permissions display
137 public static string ToUnixPermissionString (FilePermissions value)
139 char [] access = new char[] {
141 '-', '-', '-', // owner
142 '-', '-', '-', // group
143 '-', '-', '-', // other
145 bool have_device = true;
146 switch (value & FilePermissions.S_IFMT) {
147 case FilePermissions.S_IFDIR: access [0] = 'd'; break;
148 case FilePermissions.S_IFCHR: access [0] = 'c'; break;
149 case FilePermissions.S_IFBLK: access [0] = 'b'; break;
150 case FilePermissions.S_IFREG: access [0] = '-'; break;
151 case FilePermissions.S_IFIFO: access [0] = 'p'; break;
152 case FilePermissions.S_IFLNK: access [0] = 'l'; break;
153 case FilePermissions.S_IFSOCK: access [0] = 's'; break;
154 default: have_device = false; break;
156 SetUnixPermissionGroup (value, access, 1,
157 FilePermissions.S_IRUSR, FilePermissions.S_IWUSR, FilePermissions.S_IXUSR,
158 's', 'S', FilePermissions.S_ISUID);
159 SetUnixPermissionGroup (value, access, 4,
160 FilePermissions.S_IRGRP, FilePermissions.S_IWGRP, FilePermissions.S_IXGRP,
161 's', 'S', FilePermissions.S_ISGID);
162 SetUnixPermissionGroup (value, access, 7,
163 FilePermissions.S_IROTH, FilePermissions.S_IWOTH, FilePermissions.S_IXOTH,
164 't', 'T', FilePermissions.S_ISVTX);
166 ? new string (access)
167 : new string (access, 1, 9);
170 private static void SetUnixPermissionGroup (FilePermissions value,
171 char[] access, int index,
172 FilePermissions read, FilePermissions write, FilePermissions exec,
173 char both, char setonly, FilePermissions setxbit)
175 if (UnixFileSystemInfo.IsSet (value, read))
176 access [index] = 'r';
177 if (UnixFileSystemInfo.IsSet (value, write))
178 access [index+1] = 'w';
179 access [index+2] = GetSymbolicMode (value, exec, both, setonly, setxbit);
182 // Implement the GNU ls(1) permissions spec; see `info coreutils ls`,
183 // section 10.1.2, the `-l' argument information.
184 private static char GetSymbolicMode (FilePermissions value,
185 FilePermissions xbit, char both, char setonly, FilePermissions setxbit)
187 bool is_x = UnixFileSystemInfo.IsSet (value, xbit);
188 bool is_sx = UnixFileSystemInfo.IsSet (value, setxbit);
199 public static readonly DateTime UnixEpoch =
200 new DateTime (year:1970, month:1, day:1, hour:0, minute:0, second:0, kind:DateTimeKind.Utc);
201 public static readonly DateTime LocalUnixEpoch =
202 new DateTime (1970, 1, 1);
203 public static readonly TimeSpan LocalUtcOffset =
204 TimeZone.CurrentTimeZone.GetUtcOffset (DateTime.UtcNow);
206 public static DateTime ToDateTime (long time)
208 return FromTimeT (time);
211 public static DateTime ToDateTime (long time, long nanoTime)
213 return FromTimeT (time).AddMilliseconds (nanoTime / 1000);
216 public static long FromDateTime (DateTime time)
218 return ToTimeT (time);
221 public static DateTime FromTimeT (long time)
223 return UnixEpoch.AddSeconds (time).ToLocalTime ();
226 public static long ToTimeT (DateTime time)
228 if (time.Kind == DateTimeKind.Unspecified)
229 throw new ArgumentException ("DateTimeKind.Unspecified is not supported. Use Local or Utc times.", "time");
231 if (time.Kind == DateTimeKind.Local)
232 time = time.ToUniversalTime ();
234 return (long) (time - UnixEpoch).TotalSeconds;
237 public static OpenFlags ToOpenFlags (FileMode mode, FileAccess access)
241 case FileMode.CreateNew:
242 flags = OpenFlags.O_CREAT | OpenFlags.O_EXCL;
244 case FileMode.Create:
245 flags = OpenFlags.O_CREAT | OpenFlags.O_TRUNC;
250 case FileMode.OpenOrCreate:
251 flags = OpenFlags.O_CREAT;
253 case FileMode.Truncate:
254 flags = OpenFlags.O_TRUNC;
256 case FileMode.Append:
257 flags = OpenFlags.O_APPEND;
260 throw new ArgumentException (Locale.GetText ("Unsupported mode value"), "mode");
263 // Is O_LARGEFILE supported?
265 if (TryFromOpenFlags (OpenFlags.O_LARGEFILE, out _v))
266 flags |= OpenFlags.O_LARGEFILE;
269 case FileAccess.Read:
270 flags |= OpenFlags.O_RDONLY;
272 case FileAccess.Write:
273 flags |= OpenFlags.O_WRONLY;
275 case FileAccess.ReadWrite:
276 flags |= OpenFlags.O_RDWR;
279 throw new ArgumentOutOfRangeException (Locale.GetText ("Unsupported access value"), "access");
285 public static string ToFopenMode (FileAccess access)
288 case FileAccess.Read: return "rb";
289 case FileAccess.Write: return "wb";
290 case FileAccess.ReadWrite: return "r+b";
291 default: throw new ArgumentOutOfRangeException ("access");
295 public static string ToFopenMode (FileMode mode)
298 case FileMode.CreateNew: case FileMode.Create: return "w+b";
299 case FileMode.Open: case FileMode.OpenOrCreate: return "r+b";
300 case FileMode.Truncate: return "w+b";
301 case FileMode.Append: return "a+b";
302 default: throw new ArgumentOutOfRangeException ("mode");
306 private static readonly string[][] fopen_modes = new string[][]{
307 // Read Write ReadWrite
308 /* FileMode.CreateNew: */ new string[]{"Can't Read+Create", "wb", "w+b"},
309 /* FileMode.Create: */ new string[]{"Can't Read+Create", "wb", "w+b"},
310 /* FileMode.Open: */ new string[]{"rb", "wb", "r+b"},
311 /* FileMode.OpenOrCreate: */ new string[]{"rb", "wb", "r+b"},
312 /* FileMode.Truncate: */ new string[]{"Cannot Truncate and Read","wb", "w+b"},
313 /* FileMode.Append: */ new string[]{"Cannot Append and Read", "ab", "a+b"},
316 public static string ToFopenMode (FileMode mode, FileAccess access)
318 int fm = -1, fa = -1;
320 case FileMode.CreateNew: fm = 0; break;
321 case FileMode.Create: fm = 1; break;
322 case FileMode.Open: fm = 2; break;
323 case FileMode.OpenOrCreate: fm = 3; break;
324 case FileMode.Truncate: fm = 4; break;
325 case FileMode.Append: fm = 5; break;
328 case FileAccess.Read: fa = 0; break;
329 case FileAccess.Write: fa = 1; break;
330 case FileAccess.ReadWrite: fa = 2; break;
334 throw new ArgumentOutOfRangeException ("mode");
336 throw new ArgumentOutOfRangeException ("access");
338 string fopen_mode = fopen_modes [fm][fa];
339 if (fopen_mode [0] != 'r' && fopen_mode [0] != 'w' && fopen_mode [0] != 'a')
340 throw new ArgumentException (fopen_mode);
344 [DllImport (LIB, EntryPoint="Mono_Posix_FromStat")]
345 private static extern int FromStat (ref Stat source, IntPtr destination);
347 public static bool TryCopy (ref Stat source, IntPtr destination)
349 return FromStat (ref source, destination) == 0;
352 [DllImport (LIB, EntryPoint="Mono_Posix_ToStat")]
353 private static extern int ToStat (IntPtr source, out Stat destination);
355 public static bool TryCopy (IntPtr source, out Stat destination)
357 return ToStat (source, out destination) == 0;
360 [DllImport (LIB, EntryPoint="Mono_Posix_FromStatvfs")]
361 private static extern int FromStatvfs (ref Statvfs source, IntPtr destination);
363 public static bool TryCopy (ref Statvfs source, IntPtr destination)
365 return FromStatvfs (ref source, destination) == 0;
368 [DllImport (LIB, EntryPoint="Mono_Posix_ToStatvfs")]
369 private static extern int ToStatvfs (IntPtr source, out Statvfs destination);
371 public static bool TryCopy (IntPtr source, out Statvfs destination)
373 return ToStatvfs (source, out destination) == 0;
376 [DllImport (LIB, EntryPoint="Mono_Posix_FromInAddr")]
377 private static extern int FromInAddr (ref InAddr source, IntPtr destination);
379 public static bool TryCopy (ref InAddr source, IntPtr destination)
381 return FromInAddr (ref source, destination) == 0;
384 [DllImport (LIB, EntryPoint="Mono_Posix_ToInAddr")]
385 private static extern int ToInAddr (IntPtr source, out InAddr destination);
387 public static bool TryCopy (IntPtr source, out InAddr destination)
389 return ToInAddr (source, out destination) == 0;
392 [DllImport (LIB, EntryPoint="Mono_Posix_FromIn6Addr")]
393 private static extern int FromIn6Addr (ref In6Addr source, IntPtr destination);
395 public static bool TryCopy (ref In6Addr source, IntPtr destination)
397 return FromIn6Addr (ref source, destination) == 0;
400 [DllImport (LIB, EntryPoint="Mono_Posix_ToIn6Addr")]
401 private static extern int ToIn6Addr (IntPtr source, out In6Addr destination);
403 public static bool TryCopy (IntPtr source, out In6Addr destination)
405 return ToIn6Addr (source, out destination) == 0;
408 public static InAddr ToInAddr (System.Net.IPAddress address)
411 throw new ArgumentNullException ("address");
412 if (address.AddressFamily != System.Net.Sockets.AddressFamily.InterNetwork)
413 throw new ArgumentException ("address", "address.AddressFamily != System.Net.Sockets.AddressFamily.InterNetwork");
414 return new InAddr (address.GetAddressBytes ());
417 public static System.Net.IPAddress ToIPAddress (InAddr address)
419 var bytes = new byte[4];
420 address.CopyTo (bytes, 0);
421 return new System.Net.IPAddress (bytes);
424 public static In6Addr ToIn6Addr (System.Net.IPAddress address)
427 throw new ArgumentNullException ("address");
428 if (address.AddressFamily != System.Net.Sockets.AddressFamily.InterNetworkV6)
429 throw new ArgumentException ("address", "address.AddressFamily != System.Net.Sockets.AddressFamily.InterNetworkV6");
430 return new In6Addr (address.GetAddressBytes ());
433 public static System.Net.IPAddress ToIPAddress (In6Addr address)
435 var bytes = new byte[16];
436 address.CopyTo (bytes, 0);
437 return new System.Net.IPAddress (bytes);
440 [DllImport (LIB, EntryPoint="Mono_Posix_FromSockaddr")]
441 private static extern unsafe int FromSockaddr (_SockaddrHeader* source, IntPtr destination);
443 public static unsafe bool TryCopy (Sockaddr source, IntPtr destination)
446 throw new ArgumentNullException ("source");
447 byte[] array = Sockaddr.GetDynamicData (source);
448 // SockaddrStorage has to be handled extra because the native code assumes that SockaddrStorage input is used in-place
449 if (source.type == (SockaddrType.SockaddrStorage | SockaddrType.MustBeWrapped)) {
450 Marshal.Copy (array, 0, destination, (int) source.GetDynamicLength ());
453 fixed (SockaddrType* addr = &Sockaddr.GetAddress (source).type)
454 fixed (byte* data = array) {
455 var dyn = new _SockaddrDynamic (source, data, useMaxLength: false);
456 return FromSockaddr (Sockaddr.GetNative (&dyn, addr), destination) == 0;
460 [DllImport (LIB, EntryPoint="Mono_Posix_ToSockaddr")]
461 private static extern unsafe int ToSockaddr (IntPtr source, long size, _SockaddrHeader* destination);
463 public static unsafe bool TryCopy (IntPtr source, long size, Sockaddr destination)
465 if (destination == null)
466 throw new ArgumentNullException ("destination");
467 byte[] array = Sockaddr.GetDynamicData (destination);
468 fixed (SockaddrType* addr = &Sockaddr.GetAddress (destination).type)
469 fixed (byte* data = Sockaddr.GetDynamicData (destination)) {
470 var dyn = new _SockaddrDynamic (destination, data, useMaxLength: true);
471 var r = ToSockaddr (source, size, Sockaddr.GetNative (&dyn, addr));
472 dyn.Update (destination);
473 // SockaddrStorage has to be handled extra because the native code assumes that SockaddrStorage input is used in-place
474 if (r == 0 && destination.type == (SockaddrType.SockaddrStorage | SockaddrType.MustBeWrapped)) {
475 Marshal.Copy (source, array, 0, (int) destination.GetDynamicLength ());
487 // indent-tabs-mode: t