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