Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mcs / class / Mono.Posix / Mono.Unix.Native / NativeConvert.cs
1 /*
2  * This file was automatically generated by make-map from Mono.Posix.dll.
3  *
4  * DO NOT MODIFY.
5  */
6
7 using System;
8 using System.IO;
9 using System.Runtime.InteropServices;
10 using Mono.Unix.Native;
11 #if MONODROID
12 using Mono.Unix.Android;
13 #endif
14
15 namespace Mono.Unix.Native {
16
17         [CLSCompliant (false)]
18         public sealed /* static */ partial class NativeConvert
19         {
20                 //
21                 // Non-generated exports
22                 //
23 #if MONODROID
24                 [DllImport (LIB, EntryPoint="Mono_Posix_FromRealTimeSignum")]
25                 static extern int HelperFromRealTimeSignum (Int32 offset, out Int32 rval);
26
27                 static int FromRealTimeSignum (Int32 offset, out Int32 rval)
28                 {
29                         if (!AndroidUtils.AreRealTimeSignalsSafe ())
30                                 throw new PlatformNotSupportedException ("Real-time signals are not supported on this Android architecture");
31                         return HelperFromRealTimeSignum (offset, out rval);
32                 }
33 #else
34                 [DllImport (LIB, EntryPoint="Mono_Posix_FromRealTimeSignum")]
35                 private static extern int FromRealTimeSignum (Int32 offset, out Int32 rval);
36 #endif
37                 // convert a realtime signal to os signal
38                 public static int FromRealTimeSignum (RealTimeSignum sig)
39                 {
40                         int sigNum;
41                         if (FromRealTimeSignum (sig.Offset, out sigNum) == -1)
42                                 ThrowArgumentException (sig.Offset);
43                         return sigNum;
44                 }
45
46                 // convert an offset to an rt signum
47                 public static RealTimeSignum ToRealTimeSignum (int offset)
48                 {
49                         return new RealTimeSignum (offset);
50                 }
51
52                 // convert from octal representation.
53                 public static FilePermissions FromOctalPermissionString (string value)
54                 {
55                         uint n = Convert.ToUInt32 (value, 8);
56                         return ToFilePermissions (n);
57                 }
58
59                 public static string ToOctalPermissionString (FilePermissions value)
60                 {
61                         string s = Convert.ToString ((int) (value & ~FilePermissions.S_IFMT), 8);
62                         return new string ('0', 4-s.Length) + s;
63                 }
64
65                 public static FilePermissions FromUnixPermissionString (string value)
66                 {
67                         if (value == null)
68                                 throw new ArgumentNullException ("value");
69                         if (value.Length != 9 && value.Length != 10)
70                                 throw new ArgumentException ("value", "must contain 9 or 10 characters");
71
72                         int i = 0;
73                         FilePermissions perms = new FilePermissions ();
74
75                         if (value.Length == 10) {
76                                 perms |= GetUnixPermissionDevice (value [i]);
77                                 ++i;
78                         }
79
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);
85
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);
91
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);
97
98                         return perms;
99                 }
100
101                 private static FilePermissions GetUnixPermissionDevice (char value)
102                 {
103                         switch (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;
111                         }
112                         throw new ArgumentException ("value", "invalid device specification: " + 
113                                 value);
114                 }
115
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)
121                 {
122                         FilePermissions perms = new FilePermissions ();
123                         if (read == 'r')
124                                 perms |= readb;
125                         if (write == 'w')
126                                 perms |= writeb;
127                         if (exec == 'x')
128                                 perms |= execb;
129                         else if (exec == xbitonly)
130                                 perms |= xbit;
131                         else if (exec == xboth)
132                                 perms |= (execb | xbit);
133                         return perms;
134                 }
135
136                 // Create ls(1) drwxrwxrwx permissions display
137                 public static string ToUnixPermissionString (FilePermissions value)
138                 {
139                         char [] access = new char[] {
140                                 '-',            // device
141                                 '-', '-', '-',  // owner
142                                 '-', '-', '-',  // group
143                                 '-', '-', '-',  // other
144                         };
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;
155                         }
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);
165                         return have_device 
166                                 ? new string (access)
167                                 : new string (access, 1, 9);
168                 }
169
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)
174                 {
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);
180                 }
181
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)
186                 {
187                         bool is_x  = UnixFileSystemInfo.IsSet (value, xbit);
188                         bool is_sx = UnixFileSystemInfo.IsSet (value, setxbit);
189                         
190                         if (is_x && is_sx)
191                                 return both;
192                         if (is_sx)
193                                 return setonly;
194                         if (is_x)
195                                 return 'x';
196                         return '-';
197                 }
198
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);
205
206                 public static DateTime ToDateTime (long time)
207                 {
208                         return FromTimeT (time);
209                 }
210
211                 public static DateTime ToDateTime (long time, long nanoTime)
212                 {
213                         return FromTimeT (time).AddMilliseconds (nanoTime / 1000);
214                 }
215
216                 public static long FromDateTime (DateTime time)
217                 {
218                         return ToTimeT (time);
219                 }
220
221                 public static DateTime FromTimeT (long time)
222                 {
223                         return UnixEpoch.AddSeconds (time).ToLocalTime ();
224                 }
225
226                 public static long ToTimeT (DateTime time)
227                 {
228                         if (time.Kind == DateTimeKind.Unspecified)
229                                 throw new ArgumentException ("DateTimeKind.Unspecified is not supported. Use Local or Utc times.", "time");
230
231                         if (time.Kind == DateTimeKind.Local)
232                                 time = time.ToUniversalTime ();
233
234                         return (long) (time - UnixEpoch).TotalSeconds;
235                 }
236
237                 public static OpenFlags ToOpenFlags (FileMode mode, FileAccess access)
238                 {
239                         OpenFlags flags = 0;
240                         switch (mode) {
241                         case FileMode.CreateNew:
242                                 flags = OpenFlags.O_CREAT | OpenFlags.O_EXCL;
243                                 break;
244                         case FileMode.Create:
245                                 flags = OpenFlags.O_CREAT | OpenFlags.O_TRUNC;
246                                 break;
247                         case FileMode.Open:
248                                 // do nothing
249                                 break;
250                         case FileMode.OpenOrCreate:
251                                 flags = OpenFlags.O_CREAT;
252                                 break;
253                         case FileMode.Truncate:
254                                 flags = OpenFlags.O_TRUNC;
255                                 break;
256                         case FileMode.Append:
257                                 flags = OpenFlags.O_APPEND;
258                                 break;
259                         default:
260                                 throw new ArgumentException (Locale.GetText ("Unsupported mode value"), "mode");
261                         }
262
263                         // Is O_LARGEFILE supported?
264                         int _v;
265                         if (TryFromOpenFlags (OpenFlags.O_LARGEFILE, out _v))
266                                 flags |= OpenFlags.O_LARGEFILE;
267
268                         switch (access) {
269                         case FileAccess.Read:
270                                 flags |= OpenFlags.O_RDONLY;
271                                 break;
272                         case FileAccess.Write:
273                                 flags |= OpenFlags.O_WRONLY;
274                                 break;
275                         case FileAccess.ReadWrite:
276                                 flags |= OpenFlags.O_RDWR;
277                                 break;
278                         default:
279                                 throw new ArgumentOutOfRangeException (Locale.GetText ("Unsupported access value"), "access");
280                         }
281
282                         return flags;
283                 }
284
285                 public static string ToFopenMode (FileAccess access)
286                 {
287                         switch (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");
292                         }
293                 }
294
295                 public static string ToFopenMode (FileMode mode)
296                 {
297                         switch (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");
303                         }
304                 }
305
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"},
314                 };
315
316                 public static string ToFopenMode (FileMode mode, FileAccess access)
317                 {
318                         int fm = -1, fa = -1;
319                         switch (mode) {
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;
326                         }
327                         switch (access) {
328                                 case FileAccess.Read:       fa = 0; break;
329                                 case FileAccess.Write:      fa = 1; break;
330                                 case FileAccess.ReadWrite:  fa = 2; break;
331                         }
332
333                         if (fm == -1)
334                                 throw new ArgumentOutOfRangeException ("mode");
335                         if (fa == -1)
336                                 throw new ArgumentOutOfRangeException ("access");
337
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);
341                         return fopen_mode;
342                 }
343
344                 [DllImport (LIB, EntryPoint="Mono_Posix_FromStat")]
345                 private static extern int FromStat (ref Stat source, IntPtr destination);
346
347                 public static bool TryCopy (ref Stat source, IntPtr destination)
348                 {
349                         return FromStat (ref source, destination) == 0;
350                 }
351
352                 [DllImport (LIB, EntryPoint="Mono_Posix_ToStat")]
353                 private static extern int ToStat (IntPtr source, out Stat destination);
354
355                 public static bool TryCopy (IntPtr source, out Stat destination)
356                 {
357                         return ToStat (source, out destination) == 0;
358                 }
359
360                 [DllImport (LIB, EntryPoint="Mono_Posix_FromStatvfs")]
361                 private static extern int FromStatvfs (ref Statvfs source, IntPtr destination);
362
363                 public static bool TryCopy (ref Statvfs source, IntPtr destination)
364                 {
365                         return FromStatvfs (ref source, destination) == 0;
366                 }
367
368                 [DllImport (LIB, EntryPoint="Mono_Posix_ToStatvfs")]
369                 private static extern int ToStatvfs (IntPtr source, out Statvfs destination);
370
371                 public static bool TryCopy (IntPtr source, out Statvfs destination)
372                 {
373                         return ToStatvfs (source, out destination) == 0;
374                 }
375
376                 [DllImport (LIB, EntryPoint="Mono_Posix_FromInAddr")]
377                 private static extern int FromInAddr (ref InAddr source, IntPtr destination);
378
379                 public static bool TryCopy (ref InAddr source, IntPtr destination)
380                 {
381                         return FromInAddr (ref source, destination) == 0;
382                 }
383
384                 [DllImport (LIB, EntryPoint="Mono_Posix_ToInAddr")]
385                 private static extern int ToInAddr (IntPtr source, out InAddr destination);
386
387                 public static bool TryCopy (IntPtr source, out InAddr destination)
388                 {
389                         return ToInAddr (source, out destination) == 0;
390                 }
391
392                 [DllImport (LIB, EntryPoint="Mono_Posix_FromIn6Addr")]
393                 private static extern int FromIn6Addr (ref In6Addr source, IntPtr destination);
394
395                 public static bool TryCopy (ref In6Addr source, IntPtr destination)
396                 {
397                         return FromIn6Addr (ref source, destination) == 0;
398                 }
399
400                 [DllImport (LIB, EntryPoint="Mono_Posix_ToIn6Addr")]
401                 private static extern int ToIn6Addr (IntPtr source, out In6Addr destination);
402
403                 public static bool TryCopy (IntPtr source, out In6Addr destination)
404                 {
405                         return ToIn6Addr (source, out destination) == 0;
406                 }
407
408                 public static InAddr ToInAddr (System.Net.IPAddress address)
409                 {
410                         if (address == null)
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 ());
415                 }
416
417                 public static System.Net.IPAddress ToIPAddress (InAddr address)
418                 {
419                         var bytes = new byte[4];
420                         address.CopyTo (bytes, 0);
421                         return new System.Net.IPAddress (bytes);
422                 }
423
424                 public static In6Addr ToIn6Addr (System.Net.IPAddress address)
425                 {
426                         if (address == null)
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 ());
431                 }
432
433                 public static System.Net.IPAddress ToIPAddress (In6Addr address)
434                 {
435                         var bytes = new byte[16];
436                         address.CopyTo (bytes, 0);
437                         return new System.Net.IPAddress (bytes);
438                 }
439
440                 [DllImport (LIB, EntryPoint="Mono_Posix_FromSockaddr")]
441                 private static extern unsafe int FromSockaddr (_SockaddrHeader* source, IntPtr destination);
442
443                 public static unsafe bool TryCopy (Sockaddr source, IntPtr destination)
444                 {
445                         if (source == null)
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 ());
451                                 return true;
452                         }
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;
457                         }
458                 }
459
460                 [DllImport (LIB, EntryPoint="Mono_Posix_ToSockaddr")]
461                 private static extern unsafe int ToSockaddr (IntPtr source, long size, _SockaddrHeader* destination);
462
463                 public static unsafe bool TryCopy (IntPtr source, long size, Sockaddr destination)
464                 {
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 ());
476                                 }
477                                 return r == 0;
478                         }
479                 }
480         }
481 }
482
483 // vim: noexpandtab
484 // Local Variables: 
485 // tab-width: 4
486 // c-basic-offset: 4
487 // indent-tabs-mode: t
488 // End: