New test.
[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.IsSet (value, read))
145                                 access [index] = 'r';
146                         if (UnixFileSystemInfo.IsSet (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.IsSet (value, xbit);
157                         bool is_sx = UnixFileSystemInfo.IsSet (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);
170                 public static readonly TimeSpan LocalUtcOffset = 
171                         TimeZone.CurrentTimeZone.GetUtcOffset (DateTime.UtcNow);
172
173                 public static DateTime ToDateTime (long time)
174                 {
175                         return FromTimeT (time);
176                 }
177
178                 public static long FromDateTime (DateTime time)
179                 {
180                         return ToTimeT (time);
181                 }
182
183                 public static DateTime FromTimeT (long time)
184                 {
185                         DateTime r = LocalUnixEpoch.AddSeconds ((double) time + 
186                                         LocalUtcOffset.TotalSeconds);
187                         return r;
188                 }
189
190                 public static long ToTimeT (DateTime time)
191                 {
192                         TimeSpan unixTime = time.Subtract (LocalUnixEpoch) - LocalUtcOffset;
193                         return (long) unixTime.TotalSeconds;
194                 }
195
196                 public static OpenFlags ToOpenFlags (FileMode mode, FileAccess access)
197                 {
198                         OpenFlags flags = 0;
199                         switch (mode) {
200                         case FileMode.CreateNew:
201                                 flags = OpenFlags.O_CREAT | OpenFlags.O_EXCL;
202                                 break;
203                         case FileMode.Create:
204                                 flags = OpenFlags.O_CREAT | OpenFlags.O_TRUNC;
205                                 break;
206                         case FileMode.Open:
207                                 // do nothing
208                                 break;
209                         case FileMode.OpenOrCreate:
210                                 flags = OpenFlags.O_CREAT;
211                                 break;
212                         case FileMode.Truncate:
213                                 flags = OpenFlags.O_TRUNC;
214                                 break;
215                         case FileMode.Append:
216                                 flags = OpenFlags.O_APPEND;
217                                 break;
218                         default:
219                                 throw new ArgumentException (Locale.GetText ("Unsupported mode value"), "mode");
220                         }
221
222                         // Is O_LARGEFILE supported?
223                         int _v;
224                         if (TryFromOpenFlags (OpenFlags.O_LARGEFILE, out _v))
225                                 flags |= OpenFlags.O_LARGEFILE;
226
227                         switch (access) {
228                         case FileAccess.Read:
229                                 flags |= OpenFlags.O_RDONLY;
230                                 break;
231                         case FileAccess.Write:
232                                 flags |= OpenFlags.O_WRONLY;
233                                 break;
234                         case FileAccess.ReadWrite:
235                                 flags |= OpenFlags.O_RDWR;
236                                 break;
237                         default:
238                                 throw new ArgumentOutOfRangeException (Locale.GetText ("Unsupported access value"), "access");
239                         }
240
241                         return flags;
242                 }
243
244                 public static string ToFopenMode (FileAccess access)
245                 {
246                         switch (access) {
247                                 case FileAccess.Read:       return "rb";
248                                 case FileAccess.Write:      return "wb";
249                                 case FileAccess.ReadWrite:  return "r+b";
250                                 default:                    throw new ArgumentOutOfRangeException ("access");
251                         }
252                 }
253
254                 public static string ToFopenMode (FileMode mode)
255                 {
256                         switch (mode) {
257                                 case FileMode.CreateNew: case FileMode.Create:        return "w+b";
258                                 case FileMode.Open:      case FileMode.OpenOrCreate:  return "r+b";
259                                 case FileMode.Truncate: return "w+b";
260                                 case FileMode.Append:   return "a+b";
261                                 default:                throw new ArgumentOutOfRangeException ("mode");
262                         }
263                 }
264
265                 private static readonly string[][] fopen_modes = new string[][]{
266                         //                                         Read                       Write ReadWrite
267                         /*    FileMode.CreateNew: */  new string[]{"Can't Read+Create",       "wb", "w+b"},
268                         /*       FileMode.Create: */  new string[]{"Can't Read+Create",       "wb", "w+b"},
269                         /*         FileMode.Open: */  new string[]{"rb",                      "wb", "r+b"},
270                         /* FileMode.OpenOrCreate: */  new string[]{"rb",                      "wb", "r+b"},
271                         /*     FileMode.Truncate: */  new string[]{"Cannot Truncate and Read","wb", "w+b"},
272                         /*       FileMode.Append: */  new string[]{"Cannot Append and Read",  "ab", "a+b"},
273                 };
274
275                 public static string ToFopenMode (FileMode mode, FileAccess access)
276                 {
277                         int fm = -1, fa = -1;
278                         switch (mode) {
279                                 case FileMode.CreateNew:    fm = 0; break;
280                                 case FileMode.Create:       fm = 1; break;
281                                 case FileMode.Open:         fm = 2; break;
282                                 case FileMode.OpenOrCreate: fm = 3; break;
283                                 case FileMode.Truncate:     fm = 4; break;
284                                 case FileMode.Append:       fm = 5; break;
285                         }
286                         switch (access) {
287                                 case FileAccess.Read:       fa = 0; break;
288                                 case FileAccess.Write:      fa = 1; break;
289                                 case FileAccess.ReadWrite:  fa = 2; break;
290                         }
291
292                         if (fm == -1)
293                                 throw new ArgumentOutOfRangeException ("mode");
294                         if (fa == -1)
295                                 throw new ArgumentOutOfRangeException ("access");
296
297                         string fopen_mode = fopen_modes [fm][fa];
298                         if (fopen_mode [0] != 'r' && fopen_mode [0] != 'w' && fopen_mode [0] != 'a')
299                                 throw new ArgumentException (fopen_mode);
300                         return fopen_mode;
301                 }
302         }
303 }
304
305 // vim: noexpandtab