2009-11-22 Miguel de Icaza <miguel@novell.com>
[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 #if NET_2_1
38 using System.IO.IsolatedStorage;
39 #endif
40
41 namespace System.IO
42 {
43         unsafe internal sealed class MonoIO {
44                 public static readonly FileAttributes
45                         InvalidFileAttributes = (FileAttributes)(-1);
46
47                 public static readonly IntPtr
48                         InvalidHandle = (IntPtr)(-1L);
49
50                 // error methods
51                 public static Exception GetException (MonoIOError error)
52                 {
53                         /* This overload is currently only called from
54                          * File.MoveFile(), Directory.Move() and
55                          * Directory.GetCurrentDirectory() -
56                          * everywhere else supplies a path to format
57                          * with the error text.
58                          */
59                         switch(error) {
60                         case MonoIOError.ERROR_ACCESS_DENIED:
61                                 return new UnauthorizedAccessException ("Access to the path is denied.");
62                         case MonoIOError.ERROR_FILE_EXISTS:
63                                 string message = "Cannot create a file that already exist.";
64                                 return new IOException (message, unchecked ((int) 0x80070000) | (int) error);
65                         default:
66                                 /* Add more mappings here if other
67                                  * errors trigger the named but empty
68                                  * path bug (see bug 82141.) For
69                                  * everything else, fall through to
70                                  * the other overload
71                                  */
72                                 return GetException (String.Empty, error);
73                         }
74                 }
75
76                 public static Exception GetException (string path,
77                                                       MonoIOError error)
78                 {
79                         string message;
80
81                         switch (error) {
82                         // FIXME: add more exception mappings here
83                         case MonoIOError.ERROR_FILE_NOT_FOUND:
84                                 message = String.Format ("Could not find file \"{0}\"", path);
85 #if NET_2_1
86                                 return new IsolatedStorageException (message);
87 #else
88                                 return new FileNotFoundException (message, path);
89 #endif
90
91                         case MonoIOError.ERROR_TOO_MANY_OPEN_FILES:
92                                 return new IOException ("Too many open files", unchecked((int)0x80070000) | (int)error);
93                                 
94                         case MonoIOError.ERROR_PATH_NOT_FOUND:
95                                 message = String.Format ("Could not find a part of the path \"{0}\"", path);
96 #if NET_2_1
97                                 return new IsolatedStorageException (message);
98 #else
99                                 return new DirectoryNotFoundException (message);
100 #endif
101
102                         case MonoIOError.ERROR_ACCESS_DENIED:
103                                 message = String.Format ("Access to the path \"{0}\" is denied.", path);
104                                 return new UnauthorizedAccessException (message);
105
106                         case MonoIOError.ERROR_INVALID_HANDLE:
107                                 message = String.Format ("Invalid handle to path \"{0}\"", path);
108                                 return new IOException (message, unchecked((int)0x80070000) | (int)error);
109                         case MonoIOError.ERROR_INVALID_DRIVE:
110                                 message = String.Format ("Could not find the drive  '{0}'. The drive might not be ready or might not be mapped.", path);
111 #if !NET_2_1
112                                 return new DriveNotFoundException (message);
113 #else
114                                 return new IOException (message, unchecked((int)0x80070000) | (int)error);
115 #endif
116                         case MonoIOError.ERROR_FILE_EXISTS:
117                                 message = String.Format ("Could not create file \"{0}\". File already exists.", path);
118                                 return new IOException (message, unchecked((int)0x80070000) | (int)error);
119
120                         case MonoIOError.ERROR_FILENAME_EXCED_RANGE:
121                                 message = String.Format ("Path is too long. Path: {0}", path); 
122                                 return new PathTooLongException (message);
123
124                         case MonoIOError.ERROR_INVALID_PARAMETER:
125                                 message = String.Format ("Invalid parameter");
126                                 return new IOException (message, unchecked((int)0x80070000) | (int)error);
127
128                         case MonoIOError.ERROR_WRITE_FAULT:
129                                 message = String.Format ("Write fault on path {0}", path);
130                                 return new IOException (message, unchecked((int)0x80070000) | (int)error);
131
132                         case MonoIOError.ERROR_SHARING_VIOLATION:
133                                 message = String.Format ("Sharing violation on path {0}", path);
134                                 return new IOException (message, unchecked((int)0x80070000) | (int)error);
135                                 
136                         case MonoIOError.ERROR_LOCK_VIOLATION:
137                                 message = String.Format ("Lock violation on path {0}", path);
138                                 return new IOException (message, unchecked((int)0x80070000) | (int)error);
139                         
140                         case MonoIOError.ERROR_HANDLE_DISK_FULL:
141                                 message = String.Format ("Disk full. Path {0}", path);
142                                 return new IOException (message, unchecked((int)0x80070000) | (int)error);
143                         
144                         case MonoIOError.ERROR_DIR_NOT_EMPTY:
145                                 message = String.Format ("Directory {0} is not empty", path);
146                                 return new IOException (message, unchecked((int)0x80070000) | (int)error);
147
148                         case MonoIOError.ERROR_ENCRYPTION_FAILED:
149                                 return new IOException ("Encryption failed", unchecked((int)0x80070000) | (int)error);
150
151                         case MonoIOError.ERROR_CANNOT_MAKE:
152                                 message = String.Format ("Path {0} is a directory", path);
153                                 return new IOException (message, unchecked((int)0x80070000) | (int)error);
154                                 
155                         case MonoIOError.ERROR_NOT_SAME_DEVICE:
156                                 message = "Source and destination are not on the same device";
157                                 return new IOException (message, unchecked((int)0x80070000) | (int)error);
158                                 
159                         default:
160                                 message = String.Format ("Win32 IO returned {0}. Path: {1}", error, path);
161                                 return new IOException (message, unchecked((int)0x80070000) | (int)error);
162                         }
163                 }
164
165                 // directory methods
166
167                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
168                 public extern static bool CreateDirectory (string path, out MonoIOError error);
169
170                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
171                 public extern static bool RemoveDirectory (string path, out MonoIOError error);
172
173                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
174                 public extern static string [] GetFileSystemEntries (string path, string path_with_pattern, int attrs, int mask, out MonoIOError error);
175
176                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
177                 public extern static string GetCurrentDirectory (out MonoIOError error);
178
179                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
180                 public extern static bool SetCurrentDirectory (string path, out MonoIOError error);
181
182                 // file methods
183
184                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
185                 public extern static bool MoveFile (string path, string dest,
186                                                     out MonoIOError error);
187
188                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
189                 public extern static bool CopyFile (string path, string dest,
190                                                     bool overwrite,
191                                                     out MonoIOError error);
192
193                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
194                 public extern static bool DeleteFile (string path,
195                                                       out MonoIOError error);
196
197                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
198                 public extern static bool ReplaceFile (string sourceFileName, 
199                                                        string destinationFileName, 
200                                                        string destinationBackupFileName, 
201                                                        bool ignoreMetadataErrors,
202                                                        out MonoIOError error);
203
204                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
205                 public extern static FileAttributes GetFileAttributes (string path, out MonoIOError error);
206
207                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
208                 public extern static bool SetFileAttributes (string path, FileAttributes attrs, out MonoIOError error);
209
210                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
211                 public extern static MonoFileType GetFileType (IntPtr handle, out MonoIOError error);
212
213                 //
214                 // Find file methods
215                 //
216                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
217                 public extern static string FindFirst (string path, string pattern, int attrs, int mask, out int error, out IntPtr handle);
218                 
219                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
220                 public extern static string FindNext (IntPtr handle, out int error);
221                 
222                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
223                 public extern static int FindClose (IntPtr handle);
224                 
225                 public static bool Exists (string path, out MonoIOError error)
226                 {
227                         FileAttributes attrs = GetFileAttributes (path,
228                                                                   out error);
229                         if (attrs == InvalidFileAttributes)
230                                 return false;
231
232                         return true;
233                 }
234
235                 public static bool ExistsFile (string path,
236                                                out MonoIOError error)
237                 {
238                         FileAttributes attrs = GetFileAttributes (path,
239                                                                   out error);
240                         if (attrs == InvalidFileAttributes)
241                                 return false;
242
243                         if ((attrs & FileAttributes.Directory) != 0)
244                                 return false;
245
246                         return true;
247                 }
248
249                 public static bool ExistsDirectory (string path,
250                                                     out MonoIOError error)
251                 {
252                         FileAttributes attrs = GetFileAttributes (path,
253                                                                   out error);
254                                                                   
255                         // Actually, we are looking for a directory, not a file
256                         if (error == MonoIOError.ERROR_FILE_NOT_FOUND)
257                                 error = MonoIOError.ERROR_PATH_NOT_FOUND;
258                                 
259                         if (attrs == InvalidFileAttributes)
260                                 return false;
261
262                         if ((attrs & FileAttributes.Directory) == 0)
263                                 return false;
264
265                         return true;
266                 }
267
268                 public static bool ExistsSymlink (string path,
269                                                   out MonoIOError error)
270                 {
271                         FileAttributes attrs = GetFileAttributes (path,
272                                                                   out error);
273                         if (attrs == InvalidFileAttributes)
274                                 return false;
275                         
276                         if ((attrs & FileAttributes.ReparsePoint) == 0)
277                                 return false;
278
279                         return true;
280                 }
281
282                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
283                 public extern static bool GetFileStat (string path,
284                                                        out MonoIOStat stat,
285                                                        out MonoIOError error);
286
287                 // handle methods
288
289                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
290                 public extern static IntPtr Open (string filename,
291                                                   FileMode mode,
292                                                   FileAccess access,
293                                                   FileShare share,
294                                                   FileOptions options,
295                                                   out MonoIOError error);
296                 
297                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
298                 public extern static bool Close (IntPtr handle,
299                                                  out MonoIOError error);
300                 
301                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
302                 public extern static int Read (IntPtr handle, byte [] dest,
303                                                int dest_offset, int count,
304                                                out MonoIOError error);
305                 
306                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
307                 public extern static int Write (IntPtr handle, [In] byte [] src,
308                                                 int src_offset, int count,
309                                                 out MonoIOError error);
310                 
311                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
312                 public extern static long Seek (IntPtr handle, long offset,
313                                                 SeekOrigin origin,
314                                                 out MonoIOError error);
315                 
316                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
317                 public extern static bool Flush (IntPtr handle,
318                                                  out MonoIOError error);
319
320                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
321                 public extern static long GetLength (IntPtr handle,
322                                                      out MonoIOError error);
323
324                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
325                 public extern static bool SetLength (IntPtr handle,
326                                                      long length,
327                                                      out MonoIOError error);
328
329                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
330                 public extern static bool SetFileTime (IntPtr handle,
331                                                        long creation_time,
332                                                        long last_access_time,
333                                                        long last_write_time,
334                                                        out MonoIOError error);
335
336                 public static bool SetFileTime (string path,
337                                                 long creation_time,
338                                                 long last_access_time,
339                                                 long last_write_time,
340                                                 out MonoIOError error)
341                 {
342                         return SetFileTime (path,
343                                 0,
344                                 creation_time,
345                                 last_access_time,
346                                 last_write_time,
347                                 DateTime.MinValue,
348                                 out error);
349                 }
350
351                 public static bool SetCreationTime (string path,
352                                                 DateTime dateTime,
353                                                 out MonoIOError error)
354                 {
355                         return SetFileTime (path, 1, -1, -1, -1, dateTime, out error);
356                 }
357
358                 public static bool SetLastAccessTime (string path,
359                                                 DateTime dateTime,
360                                                 out MonoIOError error)
361                 {
362                         return SetFileTime (path, 2, -1, -1, -1, dateTime, out error);
363                 }
364
365                 public static bool SetLastWriteTime (string path,
366                                                 DateTime dateTime,
367                                                 out MonoIOError error)
368                 {
369                         return SetFileTime (path, 3, -1, -1, -1, dateTime, out error);
370                 }
371
372                 public static bool SetFileTime (string path,
373                                                 int type,
374                                                 long creation_time,
375                                                 long last_access_time,
376                                                 long last_write_time,
377                                                 DateTime dateTime,
378                                                 out MonoIOError error)
379                 {
380                         IntPtr handle;
381                         bool result;
382
383                         handle = Open (path, FileMode.Open,
384                                        FileAccess.ReadWrite,
385                                        FileShare.ReadWrite, FileOptions.None, out error);
386                         if (handle == MonoIO.InvalidHandle)
387                                 return false;
388
389                         switch (type) {
390                         case 1:
391                                 creation_time = dateTime.ToFileTime ();
392                                 break;
393                         case 2:
394                                 last_access_time = dateTime.ToFileTime ();
395                                 break;
396                         case 3:
397                                 last_write_time = dateTime.ToFileTime ();
398                                 break;
399                         }
400
401                         result = SetFileTime (handle, creation_time,
402                                               last_access_time,
403                                               last_write_time, out error);
404
405                         MonoIOError ignore_error;
406                         Close (handle, out ignore_error);
407                         
408                         return result;
409                 }
410
411                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
412                 public extern static void Lock (IntPtr handle,
413                                                 long position, long length,
414                                                 out MonoIOError error);
415
416                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
417                 public extern static void Unlock (IntPtr handle,
418                                                   long position, long length,
419                                                   out MonoIOError error);
420
421                 // console handles
422
423                 public extern static IntPtr ConsoleOutput {
424                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
425                         get;
426                 }
427
428                 public extern static IntPtr ConsoleInput {
429                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
430                         get;
431                 }
432
433                 public extern static IntPtr ConsoleError {
434                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
435                         get;
436                 }
437
438                 // pipe handles
439
440                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
441                 public extern static bool CreatePipe (out IntPtr read_handle, out IntPtr write_handle);
442
443                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
444                 public extern static bool DuplicateHandle (IntPtr source_process_handle, IntPtr source_handle,
445                         IntPtr target_process_handle, out IntPtr target_handle, int access, int inherit, int options);
446
447                 // path characters
448
449                 public extern static char VolumeSeparatorChar {
450                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
451                         get;
452                 }
453
454                 public extern static char DirectorySeparatorChar {
455                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
456                         get;
457                 }
458
459                 public extern static char AltDirectorySeparatorChar {
460                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
461                         get;
462                 }
463
464                 public extern static char PathSeparator {
465                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
466                         get;
467                 }
468
469                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
470                 public extern static int GetTempPath(out string path);
471         }
472 }
473