new error tests
[mono.git] / mcs / class / corlib / System.IO / MonoIO.cs
1 // System.IO.MonoIO.cs: static interface to native filesystem.
2 //
3 // Author:
4 //   Dan Lewis (dihlewis@yahoo.co.uk)
5 //   Dick Porter (dick@ximian.com)
6 //
7 // (C) 2002
8 //
9
10 //
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 using System;
34 using System.Runtime.CompilerServices;
35 using System.Runtime.InteropServices;
36 using System.Threading;
37
38 namespace System.IO
39 {
40         unsafe internal sealed class MonoIO {
41                 public static readonly FileAttributes
42                         InvalidFileAttributes = (FileAttributes)(-1);
43
44                 public static readonly IntPtr
45                         InvalidHandle = (IntPtr)(-1L);
46
47                 // error methods
48                 public static Exception GetException (MonoIOError error)
49                 {
50                         return GetException (String.Empty, error);
51                 }
52
53                 public static Exception GetException (string path,
54                                                       MonoIOError error)
55                 {
56                         string message;
57
58                         switch (error) {
59                         // FIXME: add more exception mappings here
60                         case MonoIOError.ERROR_FILE_NOT_FOUND:
61                                 message = String.Format ("Could not find file \"{0}\"", path);
62                                 return new FileNotFoundException (message,
63                                                                   path);
64
65                         case MonoIOError.ERROR_TOO_MANY_OPEN_FILES:
66                                 return new IOException ("Too many open files");
67                                 
68                         case MonoIOError.ERROR_PATH_NOT_FOUND:
69                                 message = String.Format ("Could not find a part of the path \"{0}\"", path);
70                                 return new DirectoryNotFoundException (message);
71
72                         case MonoIOError.ERROR_ACCESS_DENIED:
73                                 message = String.Format ("Access to the path \"{0}\" is denied.", path);
74                                 return new UnauthorizedAccessException (message);
75
76                         case MonoIOError.ERROR_INVALID_HANDLE:
77                                 message = String.Format ("Invalid handle to path \"{0}\"", path);
78                                 return new IOException (message);
79                                 
80                         case MonoIOError.ERROR_FILE_EXISTS:
81                                 message = String.Format ("Could not create file \"{0}\". File already exists.", path);
82                                 return new IOException (message);
83
84                         case MonoIOError.ERROR_FILENAME_EXCED_RANGE:
85                                 message = String.Format ("Path is too long. Path: {0}", path); 
86                                 return new PathTooLongException (message);
87
88                         case MonoIOError.ERROR_INVALID_PARAMETER:
89                                 message = String.Format ("Invalid parameter");
90                                 return new IOException (message);
91
92                         case MonoIOError.ERROR_SHARING_VIOLATION:
93                                 message = String.Format ("Sharing violation on path {0}", path);
94                                 return new IOException (message);
95                                 
96                         case MonoIOError.ERROR_LOCK_VIOLATION:
97                                 message = String.Format ("Lock violation on path {0}", path);
98                                 return new IOException (message);
99                                 
100                         default:
101                                 message = String.Format ("Win32 IO returned {0}. Path: {1}", error, path);
102                                 return new IOException (message);
103                         }
104                 }
105
106                 // directory methods
107
108                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
109                 public extern static bool CreateDirectory (string path, out MonoIOError error);
110
111                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
112                 public extern static bool RemoveDirectory (string path, out MonoIOError error);
113
114                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
115                 public extern static IntPtr FindFirstFile (string path, out MonoIOStat stat, out MonoIOError error);
116
117                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
118                 public extern static bool FindNextFile (IntPtr find, out MonoIOStat stat, out MonoIOError error);
119
120                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
121                 public extern static bool FindClose (IntPtr find,
122                                                      out MonoIOError error);
123
124                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
125                 public extern static string GetCurrentDirectory (out MonoIOError error);
126
127                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
128                 public extern static bool SetCurrentDirectory (string path, out MonoIOError error);
129
130                 // file methods
131
132                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
133                 public extern static bool MoveFile (string path, string dest,
134                                                     out MonoIOError error);
135
136                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
137                 public extern static bool CopyFile (string path, string dest,
138                                                     bool overwrite,
139                                                     out MonoIOError error);
140
141                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
142                 public extern static bool DeleteFile (string path,
143                                                       out MonoIOError error);
144
145                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
146                 public extern static FileAttributes GetFileAttributes (string path, out MonoIOError error);
147
148                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
149                 public extern static bool SetFileAttributes (string path, FileAttributes attrs, out MonoIOError error);
150
151                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
152                 public extern static MonoFileType GetFileType (IntPtr handle, out MonoIOError error);
153
154                 public static bool Exists (string path, out MonoIOError error)
155                 {
156                         FileAttributes attrs = GetFileAttributes (path,
157                                                                   out error);
158                         if (attrs == InvalidFileAttributes)
159                                 return false;
160
161                         return true;
162                 }
163
164                 public static bool ExistsFile (string path,
165                                                out MonoIOError error)
166                 {
167                         FileAttributes attrs = GetFileAttributes (path,
168                                                                   out error);
169                         if (attrs == InvalidFileAttributes)
170                                 return false;
171
172                         if ((attrs & FileAttributes.Directory) != 0)
173                                 return false;
174
175                         return true;
176                 }
177
178                 public static bool ExistsDirectory (string path,
179                                                     out MonoIOError error)
180                 {
181                         FileAttributes attrs = GetFileAttributes (path,
182                                                                   out error);
183                                                                   
184                         // Actually, we are looking for a directory, not a file
185                         if (error == MonoIOError.ERROR_FILE_NOT_FOUND)
186                                 error = MonoIOError.ERROR_PATH_NOT_FOUND;
187                                 
188                         if (attrs == InvalidFileAttributes)
189                                 return false;
190
191                         if ((attrs & FileAttributes.Directory) == 0)
192                                 return false;
193
194                         return true;
195                 }
196
197                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
198                 public extern static bool GetFileStat (string path,
199                                                        out MonoIOStat stat,
200                                                        out MonoIOError error);
201
202                 // handle methods
203
204                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
205                 public extern static IntPtr Open (string filename,
206                                                   FileMode mode,
207                                                   FileAccess access,
208                                                   FileShare share,
209                                                   bool async,
210                                                   out MonoIOError error);
211                 
212                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
213                 public extern static bool Close (IntPtr handle,
214                                                  out MonoIOError error);
215                 
216                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
217                 public extern static int Read (IntPtr handle, byte [] dest,
218                                                int dest_offset, int count,
219                                                out MonoIOError error);
220                 
221                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
222                 public extern static int Write (IntPtr handle, [In] byte [] src,
223                                                 int src_offset, int count,
224                                                 out MonoIOError error);
225                 
226                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
227                 public extern static long Seek (IntPtr handle, long offset,
228                                                 SeekOrigin origin,
229                                                 out MonoIOError error);
230                 
231                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
232                 public extern static bool Flush (IntPtr handle,
233                                                  out MonoIOError error);
234
235                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
236                 public extern static long GetLength (IntPtr handle,
237                                                      out MonoIOError error);
238
239                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
240                 public extern static bool SetLength (IntPtr handle,
241                                                      long length,
242                                                      out MonoIOError error);
243
244                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
245                 public extern static bool SetFileTime (IntPtr handle,
246                                                        long creation_time,
247                                                        long last_access_time,
248                                                        long last_write_time,
249                                                        out MonoIOError error);
250
251                 public static bool SetFileTime (string path,
252                                                 long creation_time,
253                                                 long last_access_time,
254                                                 long last_write_time,
255                                                 out MonoIOError error)
256                 {
257                         return SetFileTime (path,
258                                 0,
259                                 creation_time,
260                                 last_access_time,
261                                 last_write_time,
262                                 DateTime.MinValue,
263                                 out error);
264                 }
265
266                 public static bool SetCreationTime (string path,
267                                                 DateTime dateTime,
268                                                 out MonoIOError error)
269                 {
270                         return SetFileTime (path, 1, -1, -1, -1, dateTime, out error);
271                 }
272
273                 public static bool SetLastAccessTime (string path,
274                                                 DateTime dateTime,
275                                                 out MonoIOError error)
276                 {
277                         return SetFileTime (path, 2, -1, -1, -1, dateTime, out error);
278                 }
279
280                 public static bool SetLastWriteTime (string path,
281                                                 DateTime dateTime,
282                                                 out MonoIOError error)
283                 {
284                         return SetFileTime (path, 3, -1, -1, -1, dateTime, out error);
285                 }
286
287                 public static bool SetFileTime (string path,
288                                                 int type,
289                                                 long creation_time,
290                                                 long last_access_time,
291                                                 long last_write_time,
292                                                 DateTime dateTime,
293                                                 out MonoIOError error)
294                 {
295                         IntPtr handle;
296                         bool result;
297
298                         handle = Open (path, FileMode.Open,
299                                        FileAccess.ReadWrite,
300                                        FileShare.ReadWrite, false, out error);
301                         if (handle == MonoIO.InvalidHandle)
302                                 return false;
303
304                         switch (type) {
305                         case 1:
306                                 creation_time = dateTime.ToFileTime ();
307                                 break;
308                         case 2:
309                                 last_access_time = dateTime.ToFileTime ();
310                                 break;
311                         case 3:
312                                 last_write_time = dateTime.ToFileTime ();
313                                 break;
314                         }
315
316                         result = SetFileTime (handle, creation_time,
317                                               last_access_time,
318                                               last_write_time, out error);
319
320                         MonoIOError ignore_error;
321                         Close (handle, out ignore_error);
322                         
323                         return result;
324                 }
325
326                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
327                 public extern static void Lock (IntPtr handle,
328                                                 long position, long length,
329                                                 out MonoIOError error);
330
331                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
332                 public extern static void Unlock (IntPtr handle,
333                                                   long position, long length,
334                                                   out MonoIOError error);
335
336                 // console handles
337
338                 public extern static IntPtr ConsoleOutput {
339                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
340                         get;
341                 }
342
343                 public extern static IntPtr ConsoleInput {
344                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
345                         get;
346                 }
347
348                 public extern static IntPtr ConsoleError {
349                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
350                         get;
351                 }
352
353                 // pipe handles
354
355                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
356                 public extern static bool CreatePipe (out IntPtr read_handle, out IntPtr write_handle);
357
358                 // path characters
359
360                 public extern static char VolumeSeparatorChar {
361                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
362                         get;
363                 }
364
365                 public extern static char DirectorySeparatorChar {
366                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
367                         get;
368                 }
369
370                 public extern static char AltDirectorySeparatorChar {
371                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
372                         get;
373                 }
374
375                 public extern static char PathSeparator {
376                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
377                         get;
378                 }
379
380                 public extern static char [] InvalidPathChars {
381                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
382                         get;
383                 }
384
385                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
386                 public extern static int GetTempPath(out string path);
387         }
388 }
389