* Makefile: Build the make-map.exe in Mono.Unix.Native; add /nowarn:0618 to
[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                 // convert from octal representation.
22                 public static FilePermissions FromOctalPermissionString (string value)
23                 {
24                         uint n = Convert.ToUInt32 (value, 8);
25                         return ToFilePermissions (n);
26                 }
27
28                 public static string ToOctalPermissionString (FilePermissions value)
29                 {
30                         string s = Convert.ToString ((int) (value & ~FilePermissions.S_IFMT), 8);
31                         return new string ('0', 4-s.Length) + s;
32                 }
33
34                 public static FilePermissions FromUnixPermissionString (string value)
35                 {
36                         if (value == null)
37                                 throw new ArgumentNullException ("value");
38                         if (value.Length != 9 && value.Length != 10)
39                                 throw new ArgumentException ("value", "must contain 9 or 10 characters");
40
41                         int i = 0;
42                         FilePermissions perms = new FilePermissions ();
43
44                         if (value.Length == 10) {
45                                 perms |= GetUnixPermissionDevice (value [i]);
46                                 ++i;
47                         }
48
49                         perms |= GetUnixPermissionGroup (
50                                 value [i++], FilePermissions.S_IRUSR,
51                                 value [i++], FilePermissions.S_IWUSR,
52                                 value [i++], FilePermissions.S_IXUSR,
53                                 's', 'S', FilePermissions.S_ISUID);
54
55                         perms |= GetUnixPermissionGroup (
56                                 value [i++], FilePermissions.S_IRGRP,
57                                 value [i++], FilePermissions.S_IWGRP,
58                                 value [i++], FilePermissions.S_IXGRP,
59                                 's', 'S', FilePermissions.S_ISGID);
60
61                         perms |= GetUnixPermissionGroup (
62                                 value [i++], FilePermissions.S_IROTH,
63                                 value [i++], FilePermissions.S_IWOTH,
64                                 value [i++], FilePermissions.S_IXOTH,
65                                 't', 'T', FilePermissions.S_ISVTX);
66
67                         return perms;
68                 }
69
70                 private static FilePermissions GetUnixPermissionDevice (char value)
71                 {
72                         switch (value) {
73                         case 'd': return FilePermissions.S_IFDIR;
74                         case 'c': return FilePermissions.S_IFCHR;
75                         case 'b': return FilePermissions.S_IFBLK;
76                         case '-': return FilePermissions.S_IFREG;
77                         case 'p': return FilePermissions.S_IFIFO;
78                         case 'l': return FilePermissions.S_IFLNK;
79                         case 's': return FilePermissions.S_IFSOCK;
80                         }
81                         throw new ArgumentException ("value", "invalid device specification: " + 
82                                 value);
83                 }
84
85                 private static FilePermissions GetUnixPermissionGroup (
86                         char read, FilePermissions readb, 
87                         char write, FilePermissions writeb, 
88                         char exec, FilePermissions execb,
89                         char xboth, char xbitonly, FilePermissions xbit)
90                 {
91                         FilePermissions perms = new FilePermissions ();
92                         if (read == 'r')
93                                 perms |= readb;
94                         if (write == 'w')
95                                 perms |= writeb;
96                         if (exec == 'x')
97                                 perms |= execb;
98                         else if (exec == xbitonly)
99                                 perms |= xbit;
100                         else if (exec == xboth)
101                                 perms |= (execb | xbit);
102                         return perms;
103                 }
104
105                 // Create ls(1) drwxrwxrwx permissions display
106                 public static string ToUnixPermissionString (FilePermissions value)
107                 {
108                         char [] access = new char[] {
109                                 '-',            // device
110                                 '-', '-', '-',  // owner
111                                 '-', '-', '-',  // group
112                                 '-', '-', '-',  // other
113                         };
114                         bool have_device = true;
115                         switch (value & FilePermissions.S_IFMT) {
116                                 case FilePermissions.S_IFDIR:   access [0] = 'd'; break;
117                                 case FilePermissions.S_IFCHR:   access [0] = 'c'; break;
118                                 case FilePermissions.S_IFBLK:   access [0] = 'b'; break;
119                                 case FilePermissions.S_IFREG:   access [0] = '-'; break;
120                                 case FilePermissions.S_IFIFO:   access [0] = 'p'; break;
121                                 case FilePermissions.S_IFLNK:   access [0] = 'l'; break;
122                                 case FilePermissions.S_IFSOCK:  access [0] = 's'; break;
123                                 default:                        have_device = false; break;
124                         }
125                         SetUnixPermissionGroup (value, access, 1, 
126                                 FilePermissions.S_IRUSR, FilePermissions.S_IWUSR, FilePermissions.S_IXUSR,
127                                 's', 'S', FilePermissions.S_ISUID);
128                         SetUnixPermissionGroup (value, access, 4, 
129                                 FilePermissions.S_IRGRP, FilePermissions.S_IWGRP, FilePermissions.S_IXGRP,
130                                 's', 'S', FilePermissions.S_ISGID);
131                         SetUnixPermissionGroup (value, access, 7, 
132                                 FilePermissions.S_IROTH, FilePermissions.S_IWOTH, FilePermissions.S_IXOTH,
133                                 't', 'T', FilePermissions.S_ISVTX);
134                         return have_device 
135                                 ? new string (access)
136                                 : new string (access, 1, 9);
137                 }
138
139                 private static void SetUnixPermissionGroup (FilePermissions value,
140                         char[] access, int index,
141                         FilePermissions read, FilePermissions write, FilePermissions exec,
142                         char both, char setonly, FilePermissions setxbit)
143                 {
144                         if (UnixFileSystemInfo.IsType (value, read))
145                                 access [index] = 'r';
146                         if (UnixFileSystemInfo.IsType (value, write))
147                                 access [index+1] = 'w';
148                         access [index+2] = GetSymbolicMode (value, exec, both, setonly, setxbit);
149                 }
150
151                 // Implement the GNU ls(1) permissions spec; see `info coreutils ls`,
152                 // section 10.1.2, the `-l' argument information.
153                 private static char GetSymbolicMode (FilePermissions value, 
154                         FilePermissions xbit, char both, char setonly, FilePermissions setxbit)
155                 {
156                         bool is_x  = UnixFileSystemInfo.IsType (value, xbit);
157                         bool is_sx = UnixFileSystemInfo.IsType (value, setxbit);
158                         
159                         if (is_x && is_sx)
160                                 return both;
161                         if (is_sx)
162                                 return setonly;
163                         if (is_x)
164                                 return 'x';
165                         return '-';
166                 }
167
168                 public static readonly DateTime LocalUnixEpoch = 
169                         new DateTime (1970, 1, 1).ToLocalTime();
170
171                 public static DateTime ToDateTime (long time)
172                 {
173                         return FromTimeT (time);
174                 }
175
176                 public static long FromDateTime (DateTime time)
177                 {
178                         return ToTimeT (time);
179                 }
180
181                 public static DateTime FromTimeT (long time)
182                 {
183                         DateTime r = LocalUnixEpoch.AddSeconds (time);
184                         return r;
185                 }
186
187                 public static long ToTimeT (DateTime time)
188                 {
189                         return (long) time.Subtract (LocalUnixEpoch).TotalSeconds;
190                 }
191
192                 public static OpenFlags ToOpenFlags (FileMode mode, FileAccess access)
193                 {
194                         OpenFlags flags = 0;
195                         switch (mode) {
196                         case FileMode.CreateNew:
197                                 flags = OpenFlags.O_CREAT | OpenFlags.O_EXCL;
198                                 break;
199                         case FileMode.Create:
200                                 flags = OpenFlags.O_CREAT | OpenFlags.O_TRUNC;
201                                 break;
202                         case FileMode.Open:
203                                 // do nothing
204                                 break;
205                         case FileMode.OpenOrCreate:
206                                 flags = OpenFlags.O_CREAT;
207                                 break;
208                         case FileMode.Truncate:
209                                 flags = OpenFlags.O_TRUNC;
210                                 break;
211                         case FileMode.Append:
212                                 flags = OpenFlags.O_APPEND;
213                                 break;
214                         default:
215                                 throw new ArgumentException (Locale.GetText ("Unsupported mode value"), "mode");
216                         }
217
218                         // Is O_LARGEFILE supported?
219                         int _v;
220                         if (TryFromOpenFlags (OpenFlags.O_LARGEFILE, out _v))
221                                 flags |= OpenFlags.O_LARGEFILE;
222
223                         switch (access) {
224                         case FileAccess.Read:
225                                 flags |= OpenFlags.O_RDONLY;
226                                 break;
227                         case FileAccess.Write:
228                                 flags |= OpenFlags.O_WRONLY;
229                                 break;
230                         case FileAccess.ReadWrite:
231                                 flags |= OpenFlags.O_RDWR;
232                                 break;
233                         default:
234                                 throw new ArgumentOutOfRangeException (Locale.GetText ("Unsupported access value"), "access");
235                         }
236
237                         return flags;
238                 }
239
240                 public static string ToFopenMode (FileAccess access)
241                 {
242                         switch (access) {
243                                 case FileAccess.Read:       return "rb";
244                                 case FileAccess.Write:      return "wb";
245                                 case FileAccess.ReadWrite:  return "r+b";
246                                 default:                    throw new ArgumentOutOfRangeException ("access");
247                         }
248                 }
249
250                 public static string ToFopenMode (FileMode mode)
251                 {
252                         switch (mode) {
253                                 case FileMode.CreateNew: case FileMode.Create:        return "w+b";
254                                 case FileMode.Open:      case FileMode.OpenOrCreate:  return "r+b";
255                                 case FileMode.Truncate: return "w+b";
256                                 case FileMode.Append:   return "a+b";
257                                 default:                throw new ArgumentOutOfRangeException ("mode");
258                         }
259                 }
260
261                 private static readonly string[][] fopen_modes = new string[][]{
262                         //                                         Read                       Write ReadWrite
263                         /*    FileMode.CreateNew: */  new string[]{"Can't Read+Create",       "wb", "w+b"},
264                         /*       FileMode.Create: */  new string[]{"Can't Read+Create",       "wb", "w+b"},
265                         /*         FileMode.Open: */  new string[]{"rb",                      "wb", "r+b"},
266                         /* FileMode.OpenOrCreate: */  new string[]{"rb",                      "wb", "r+b"},
267                         /*     FileMode.Truncate: */  new string[]{"Cannot Truncate and Read","wb", "w+b"},
268                         /*       FileMode.Append: */  new string[]{"Cannot Append and Read",  "ab", "a+b"},
269                 };
270
271                 public static string ToFopenMode (FileMode mode, FileAccess access)
272                 {
273                         int fm = -1, fa = -1;
274                         switch (mode) {
275                                 case FileMode.CreateNew:    fm = 0; break;
276                                 case FileMode.Create:       fm = 1; break;
277                                 case FileMode.Open:         fm = 2; break;
278                                 case FileMode.OpenOrCreate: fm = 3; break;
279                                 case FileMode.Truncate:     fm = 4; break;
280                                 case FileMode.Append:       fm = 5; break;
281                         }
282                         switch (access) {
283                                 case FileAccess.Read:       fa = 0; break;
284                                 case FileAccess.Write:      fa = 1; break;
285                                 case FileAccess.ReadWrite:  fa = 2; break;
286                         }
287
288                         if (fm == -1)
289                                 throw new ArgumentOutOfRangeException ("mode");
290                         if (fa == -1)
291                                 throw new ArgumentOutOfRangeException ("access");
292
293                         string fopen_mode = fopen_modes [fm][fa];
294                         if (fopen_mode [0] != 'r' && fopen_mode [0] != 'w' && fopen_mode [0] != 'a')
295                                 throw new ArgumentException (fopen_mode);
296                         return fopen_mode;
297                 }
298         }
299 }
300
301 // vim: noexpandtab