Merge pull request #600 from tr8dr/master
[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
12 namespace Mono.Unix.Native {
13
14         [CLSCompliant (false)]
15         public sealed /* static */ partial class NativeConvert
16         {
17                 //
18                 // Non-generated exports
19                 //
20
21                 [DllImport (LIB, EntryPoint="Mono_Posix_FromRealTimeSignum")]
22                 private static extern int FromRealTimeSignum (Int32 offset, out Int32 rval);
23
24                 // convert a realtime signal to os signal
25                 public static int FromRealTimeSignum (RealTimeSignum sig)
26                 {
27                         int sigNum;
28                         if (FromRealTimeSignum (sig.Offset, out sigNum) == -1)
29                                 ThrowArgumentException (sig.Offset);
30                         return sigNum;
31                 }
32
33                 // convert an offset to an rt signum
34                 public static RealTimeSignum ToRealTimeSignum (int offset)
35                 {
36                         return new RealTimeSignum (offset);
37                 }
38
39                 // convert from octal representation.
40                 public static FilePermissions FromOctalPermissionString (string value)
41                 {
42                         uint n = Convert.ToUInt32 (value, 8);
43                         return ToFilePermissions (n);
44                 }
45
46                 public static string ToOctalPermissionString (FilePermissions value)
47                 {
48                         string s = Convert.ToString ((int) (value & ~FilePermissions.S_IFMT), 8);
49                         return new string ('0', 4-s.Length) + s;
50                 }
51
52                 public static FilePermissions FromUnixPermissionString (string value)
53                 {
54                         if (value == null)
55                                 throw new ArgumentNullException ("value");
56                         if (value.Length != 9 && value.Length != 10)
57                                 throw new ArgumentException ("value", "must contain 9 or 10 characters");
58
59                         int i = 0;
60                         FilePermissions perms = new FilePermissions ();
61
62                         if (value.Length == 10) {
63                                 perms |= GetUnixPermissionDevice (value [i]);
64                                 ++i;
65                         }
66
67                         perms |= GetUnixPermissionGroup (
68                                 value [i++], FilePermissions.S_IRUSR,
69                                 value [i++], FilePermissions.S_IWUSR,
70                                 value [i++], FilePermissions.S_IXUSR,
71                                 's', 'S', FilePermissions.S_ISUID);
72
73                         perms |= GetUnixPermissionGroup (
74                                 value [i++], FilePermissions.S_IRGRP,
75                                 value [i++], FilePermissions.S_IWGRP,
76                                 value [i++], FilePermissions.S_IXGRP,
77                                 's', 'S', FilePermissions.S_ISGID);
78
79                         perms |= GetUnixPermissionGroup (
80                                 value [i++], FilePermissions.S_IROTH,
81                                 value [i++], FilePermissions.S_IWOTH,
82                                 value [i++], FilePermissions.S_IXOTH,
83                                 't', 'T', FilePermissions.S_ISVTX);
84
85                         return perms;
86                 }
87
88                 private static FilePermissions GetUnixPermissionDevice (char value)
89                 {
90                         switch (value) {
91                         case 'd': return FilePermissions.S_IFDIR;
92                         case 'c': return FilePermissions.S_IFCHR;
93                         case 'b': return FilePermissions.S_IFBLK;
94                         case '-': return FilePermissions.S_IFREG;
95                         case 'p': return FilePermissions.S_IFIFO;
96                         case 'l': return FilePermissions.S_IFLNK;
97                         case 's': return FilePermissions.S_IFSOCK;
98                         }
99                         throw new ArgumentException ("value", "invalid device specification: " + 
100                                 value);
101                 }
102
103                 private static FilePermissions GetUnixPermissionGroup (
104                         char read, FilePermissions readb, 
105                         char write, FilePermissions writeb, 
106                         char exec, FilePermissions execb,
107                         char xboth, char xbitonly, FilePermissions xbit)
108                 {
109                         FilePermissions perms = new FilePermissions ();
110                         if (read == 'r')
111                                 perms |= readb;
112                         if (write == 'w')
113                                 perms |= writeb;
114                         if (exec == 'x')
115                                 perms |= execb;
116                         else if (exec == xbitonly)
117                                 perms |= xbit;
118                         else if (exec == xboth)
119                                 perms |= (execb | xbit);
120                         return perms;
121                 }
122
123                 // Create ls(1) drwxrwxrwx permissions display
124                 public static string ToUnixPermissionString (FilePermissions value)
125                 {
126                         char [] access = new char[] {
127                                 '-',            // device
128                                 '-', '-', '-',  // owner
129                                 '-', '-', '-',  // group
130                                 '-', '-', '-',  // other
131                         };
132                         bool have_device = true;
133                         switch (value & FilePermissions.S_IFMT) {
134                                 case FilePermissions.S_IFDIR:   access [0] = 'd'; break;
135                                 case FilePermissions.S_IFCHR:   access [0] = 'c'; break;
136                                 case FilePermissions.S_IFBLK:   access [0] = 'b'; break;
137                                 case FilePermissions.S_IFREG:   access [0] = '-'; break;
138                                 case FilePermissions.S_IFIFO:   access [0] = 'p'; break;
139                                 case FilePermissions.S_IFLNK:   access [0] = 'l'; break;
140                                 case FilePermissions.S_IFSOCK:  access [0] = 's'; break;
141                                 default:                        have_device = false; break;
142                         }
143                         SetUnixPermissionGroup (value, access, 1, 
144                                 FilePermissions.S_IRUSR, FilePermissions.S_IWUSR, FilePermissions.S_IXUSR,
145                                 's', 'S', FilePermissions.S_ISUID);
146                         SetUnixPermissionGroup (value, access, 4, 
147                                 FilePermissions.S_IRGRP, FilePermissions.S_IWGRP, FilePermissions.S_IXGRP,
148                                 's', 'S', FilePermissions.S_ISGID);
149                         SetUnixPermissionGroup (value, access, 7, 
150                                 FilePermissions.S_IROTH, FilePermissions.S_IWOTH, FilePermissions.S_IXOTH,
151                                 't', 'T', FilePermissions.S_ISVTX);
152                         return have_device 
153                                 ? new string (access)
154                                 : new string (access, 1, 9);
155                 }
156
157                 private static void SetUnixPermissionGroup (FilePermissions value,
158                         char[] access, int index,
159                         FilePermissions read, FilePermissions write, FilePermissions exec,
160                         char both, char setonly, FilePermissions setxbit)
161                 {
162                         if (UnixFileSystemInfo.IsSet (value, read))
163                                 access [index] = 'r';
164                         if (UnixFileSystemInfo.IsSet (value, write))
165                                 access [index+1] = 'w';
166                         access [index+2] = GetSymbolicMode (value, exec, both, setonly, setxbit);
167                 }
168
169                 // Implement the GNU ls(1) permissions spec; see `info coreutils ls`,
170                 // section 10.1.2, the `-l' argument information.
171                 private static char GetSymbolicMode (FilePermissions value, 
172                         FilePermissions xbit, char both, char setonly, FilePermissions setxbit)
173                 {
174                         bool is_x  = UnixFileSystemInfo.IsSet (value, xbit);
175                         bool is_sx = UnixFileSystemInfo.IsSet (value, setxbit);
176                         
177                         if (is_x && is_sx)
178                                 return both;
179                         if (is_sx)
180                                 return setonly;
181                         if (is_x)
182                                 return 'x';
183                         return '-';
184                 }
185
186                 public static readonly DateTime UnixEpoch =
187                         new DateTime (year:1970, month:1, day:1, hour:0, minute:0, second:0, kind:DateTimeKind.Utc);
188                 public static readonly DateTime LocalUnixEpoch = 
189                         new DateTime (1970, 1, 1);
190                 public static readonly TimeSpan LocalUtcOffset = 
191                         TimeZone.CurrentTimeZone.GetUtcOffset (DateTime.UtcNow);
192
193                 public static DateTime ToDateTime (long time)
194                 {
195                         return FromTimeT (time);
196                 }
197
198                 public static long FromDateTime (DateTime time)
199                 {
200                         return ToTimeT (time);
201                 }
202
203                 public static DateTime FromTimeT (long time)
204                 {
205                         return UnixEpoch.AddSeconds (time).ToLocalTime ();
206                 }
207
208                 public static long ToTimeT (DateTime time)
209                 {
210                         if (time.Kind == DateTimeKind.Unspecified)
211                                 throw new ArgumentException ("DateTimeKind.Unspecified is not supported. Use Local or Utc times.", "time");
212
213                         if (time.Kind == DateTimeKind.Local)
214                                 time = time.ToUniversalTime ();
215
216                         return (long) (time - UnixEpoch).TotalSeconds;
217                 }
218
219                 public static OpenFlags ToOpenFlags (FileMode mode, FileAccess access)
220                 {
221                         OpenFlags flags = 0;
222                         switch (mode) {
223                         case FileMode.CreateNew:
224                                 flags = OpenFlags.O_CREAT | OpenFlags.O_EXCL;
225                                 break;
226                         case FileMode.Create:
227                                 flags = OpenFlags.O_CREAT | OpenFlags.O_TRUNC;
228                                 break;
229                         case FileMode.Open:
230                                 // do nothing
231                                 break;
232                         case FileMode.OpenOrCreate:
233                                 flags = OpenFlags.O_CREAT;
234                                 break;
235                         case FileMode.Truncate:
236                                 flags = OpenFlags.O_TRUNC;
237                                 break;
238                         case FileMode.Append:
239                                 flags = OpenFlags.O_APPEND;
240                                 break;
241                         default:
242                                 throw new ArgumentException (Locale.GetText ("Unsupported mode value"), "mode");
243                         }
244
245                         // Is O_LARGEFILE supported?
246                         int _v;
247                         if (TryFromOpenFlags (OpenFlags.O_LARGEFILE, out _v))
248                                 flags |= OpenFlags.O_LARGEFILE;
249
250                         switch (access) {
251                         case FileAccess.Read:
252                                 flags |= OpenFlags.O_RDONLY;
253                                 break;
254                         case FileAccess.Write:
255                                 flags |= OpenFlags.O_WRONLY;
256                                 break;
257                         case FileAccess.ReadWrite:
258                                 flags |= OpenFlags.O_RDWR;
259                                 break;
260                         default:
261                                 throw new ArgumentOutOfRangeException (Locale.GetText ("Unsupported access value"), "access");
262                         }
263
264                         return flags;
265                 }
266
267                 public static string ToFopenMode (FileAccess access)
268                 {
269                         switch (access) {
270                                 case FileAccess.Read:       return "rb";
271                                 case FileAccess.Write:      return "wb";
272                                 case FileAccess.ReadWrite:  return "r+b";
273                                 default:                    throw new ArgumentOutOfRangeException ("access");
274                         }
275                 }
276
277                 public static string ToFopenMode (FileMode mode)
278                 {
279                         switch (mode) {
280                                 case FileMode.CreateNew: case FileMode.Create:        return "w+b";
281                                 case FileMode.Open:      case FileMode.OpenOrCreate:  return "r+b";
282                                 case FileMode.Truncate: return "w+b";
283                                 case FileMode.Append:   return "a+b";
284                                 default:                throw new ArgumentOutOfRangeException ("mode");
285                         }
286                 }
287
288                 private static readonly string[][] fopen_modes = new string[][]{
289                         //                                         Read                       Write ReadWrite
290                         /*    FileMode.CreateNew: */  new string[]{"Can't Read+Create",       "wb", "w+b"},
291                         /*       FileMode.Create: */  new string[]{"Can't Read+Create",       "wb", "w+b"},
292                         /*         FileMode.Open: */  new string[]{"rb",                      "wb", "r+b"},
293                         /* FileMode.OpenOrCreate: */  new string[]{"rb",                      "wb", "r+b"},
294                         /*     FileMode.Truncate: */  new string[]{"Cannot Truncate and Read","wb", "w+b"},
295                         /*       FileMode.Append: */  new string[]{"Cannot Append and Read",  "ab", "a+b"},
296                 };
297
298                 public static string ToFopenMode (FileMode mode, FileAccess access)
299                 {
300                         int fm = -1, fa = -1;
301                         switch (mode) {
302                                 case FileMode.CreateNew:    fm = 0; break;
303                                 case FileMode.Create:       fm = 1; break;
304                                 case FileMode.Open:         fm = 2; break;
305                                 case FileMode.OpenOrCreate: fm = 3; break;
306                                 case FileMode.Truncate:     fm = 4; break;
307                                 case FileMode.Append:       fm = 5; break;
308                         }
309                         switch (access) {
310                                 case FileAccess.Read:       fa = 0; break;
311                                 case FileAccess.Write:      fa = 1; break;
312                                 case FileAccess.ReadWrite:  fa = 2; break;
313                         }
314
315                         if (fm == -1)
316                                 throw new ArgumentOutOfRangeException ("mode");
317                         if (fa == -1)
318                                 throw new ArgumentOutOfRangeException ("access");
319
320                         string fopen_mode = fopen_modes [fm][fa];
321                         if (fopen_mode [0] != 'r' && fopen_mode [0] != 'w' && fopen_mode [0] != 'a')
322                                 throw new ArgumentException (fopen_mode);
323                         return fopen_mode;
324                 }
325
326                 [DllImport (LIB, EntryPoint="Mono_Posix_FromStat")]
327                 private static extern int FromStat (ref Stat source, IntPtr destination);
328
329                 public static bool TryCopy (ref Stat source, IntPtr destination)
330                 {
331                         return FromStat (ref source, destination) == 0;
332                 }
333
334                 [DllImport (LIB, EntryPoint="Mono_Posix_ToStat")]
335                 private static extern int ToStat (IntPtr source, out Stat destination);
336
337                 public static bool TryCopy (IntPtr source, out Stat destination)
338                 {
339                         return ToStat (source, out destination) == 0;
340                 }
341
342                 [DllImport (LIB, EntryPoint="Mono_Posix_FromStatvfs")]
343                 private static extern int FromStatvfs (ref Statvfs source, IntPtr destination);
344
345                 public static bool TryCopy (ref Statvfs source, IntPtr destination)
346                 {
347                         return FromStatvfs (ref source, destination) == 0;
348                 }
349
350                 [DllImport (LIB, EntryPoint="Mono_Posix_ToStatvfs")]
351                 private static extern int ToStatvfs (IntPtr source, out Statvfs destination);
352
353                 public static bool TryCopy (IntPtr source, out Statvfs destination)
354                 {
355                         return ToStatvfs (source, out destination) == 0;
356                 }
357         }
358 }
359
360 // vim: noexpandtab