Merge pull request #3413 from lambdageek/reflection-split
[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 // Copyright 2011 Xamarin Inc (http://www.xamarin.com).
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 using Microsoft.Win32.SafeHandles;
39 #if MOBILE
40 using System.IO.IsolatedStorage;
41 #endif
42
43 namespace System.IO
44 {
45         unsafe static class MonoIO {
46                 public const int FileAlreadyExistsHResult = unchecked ((int) 0x80070000) | (int)MonoIOError.ERROR_FILE_EXISTS;
47
48                 public const FileAttributes
49                         InvalidFileAttributes = (FileAttributes)(-1);
50
51                 public static readonly IntPtr
52                         InvalidHandle = (IntPtr)(-1L);
53
54                 static bool dump_handles = Environment.GetEnvironmentVariable ("MONO_DUMP_HANDLES_ON_ERROR_TOO_MANY_OPEN_FILES") != null;
55
56                 // error methods
57                 public static Exception GetException (MonoIOError error)
58                 {
59                         /* This overload is currently only called from
60                          * File.MoveFile(), Directory.Move() and
61                          * Directory.GetCurrentDirectory() -
62                          * everywhere else supplies a path to format
63                          * with the error text.
64                          */
65                         switch(error) {
66                         case MonoIOError.ERROR_ACCESS_DENIED:
67                                 return new UnauthorizedAccessException ("Access to the path is denied.");
68                         case MonoIOError.ERROR_FILE_EXISTS:
69                                 string message = "Cannot create a file that already exist.";
70                                 return new IOException (message, FileAlreadyExistsHResult);
71                         default:
72                                 /* Add more mappings here if other
73                                  * errors trigger the named but empty
74                                  * path bug (see bug 82141.) For
75                                  * everything else, fall through to
76                                  * the other overload
77                                  */
78                                 return GetException (String.Empty, error);
79                         }
80                 }
81
82                 public static Exception GetException (string path,
83                                                       MonoIOError error)
84                 {
85                         string message;
86
87                         switch (error) {
88                         // FIXME: add more exception mappings here
89                         case MonoIOError.ERROR_FILE_NOT_FOUND:
90                                 message = String.Format ("Could not find file \"{0}\"", path);
91                                 return new FileNotFoundException (message, path);
92
93                         case MonoIOError.ERROR_TOO_MANY_OPEN_FILES:
94                                 if (dump_handles)
95                                         DumpHandles ();
96                                 return new IOException ("Too many open files", unchecked((int)0x80070000) | (int)error);
97                                 
98                         case MonoIOError.ERROR_PATH_NOT_FOUND:
99                                 message = String.Format ("Could not find a part of the path \"{0}\"", path);
100                                 return new DirectoryNotFoundException (message);
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 !MOBILE
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                         case MonoIOError.ERROR_DIRECTORY:
160                                 message = "The directory name is invalid";
161                                 return new IOException (message, unchecked((int)0x80070000) | (int)error);
162                                 
163                         default:
164                                 message = String.Format ("Win32 IO returned {0}. Path: {1}", error, path);
165                                 return new IOException (message, unchecked((int)0x80070000) | (int)error);
166                         }
167                 }
168
169                 // directory methods
170
171                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
172                 public extern static bool CreateDirectory (string path, out MonoIOError error);
173
174                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
175                 public extern static bool RemoveDirectory (string path, out MonoIOError error);
176
177                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
178                 public extern static string [] GetFileSystemEntries (string path, string path_with_pattern, int attrs, int mask, out MonoIOError error);
179
180                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
181                 public extern static string GetCurrentDirectory (out MonoIOError error);
182
183                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
184                 public extern static bool SetCurrentDirectory (string path, out MonoIOError error);
185
186                 // file methods
187
188                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
189                 public extern static bool MoveFile (string path, string dest,
190                                                     out MonoIOError error);
191
192                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
193                 public extern static bool CopyFile (string path, string dest,
194                                                     bool overwrite,
195                                                     out MonoIOError error);
196
197                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
198                 public extern static bool DeleteFile (string path,
199                                                       out MonoIOError error);
200
201                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
202                 public extern static bool ReplaceFile (string sourceFileName, 
203                                                        string destinationFileName, 
204                                                        string destinationBackupFileName, 
205                                                        bool ignoreMetadataErrors,
206                                                        out MonoIOError error);
207
208                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
209                 public extern static FileAttributes GetFileAttributes (string path, out MonoIOError error);
210
211                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
212                 public extern static bool SetFileAttributes (string path, FileAttributes attrs, out MonoIOError error);
213
214                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
215                 private extern static MonoFileType GetFileType (IntPtr handle, out MonoIOError error);
216
217                 public static MonoFileType GetFileType (SafeHandle safeHandle, out MonoIOError error)
218                 {
219                         bool release = false;
220                         try {
221                                 safeHandle.DangerousAddRef (ref release);
222                                 return GetFileType (safeHandle.DangerousGetHandle (), out error);
223                         } finally {
224                                 if (release)
225                                         safeHandle.DangerousRelease ();
226                         }
227                 }
228
229                 //
230                 // Find file methods
231                 //
232                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
233                 public extern static string FindFirst (string path, string pattern, out FileAttributes result_attr, out MonoIOError error, out IntPtr handle);
234                 
235                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
236                 public extern static string FindNext (IntPtr handle, out FileAttributes result_attr, out MonoIOError error);
237                 
238                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
239                 public extern static int FindClose (IntPtr handle);
240                 
241                 public static bool Exists (string path, out MonoIOError error)
242                 {
243                         FileAttributes attrs = GetFileAttributes (path,
244                                                                   out error);
245                         if (attrs == InvalidFileAttributes)
246                                 return false;
247
248                         return true;
249                 }
250
251                 public static bool ExistsFile (string path,
252                                                out MonoIOError error)
253                 {
254                         FileAttributes attrs = GetFileAttributes (path,
255                                                                   out error);
256                         if (attrs == InvalidFileAttributes)
257                                 return false;
258
259                         if ((attrs & FileAttributes.Directory) != 0)
260                                 return false;
261
262                         return true;
263                 }
264
265                 public static bool ExistsDirectory (string path,
266                                                     out MonoIOError error)
267                 {
268                         FileAttributes attrs = GetFileAttributes (path,
269                                                                   out error);
270                                                                   
271                         // Actually, we are looking for a directory, not a file
272                         if (error == MonoIOError.ERROR_FILE_NOT_FOUND)
273                                 error = MonoIOError.ERROR_PATH_NOT_FOUND;
274                                 
275                         if (attrs == InvalidFileAttributes)
276                                 return false;
277
278                         if ((attrs & FileAttributes.Directory) == 0)
279                                 return false;
280
281                         return true;
282                 }
283
284                 public static bool ExistsSymlink (string path,
285                                                   out MonoIOError error)
286                 {
287                         FileAttributes attrs = GetFileAttributes (path,
288                                                                   out error);
289                         if (attrs == InvalidFileAttributes)
290                                 return false;
291                         
292                         if ((attrs & FileAttributes.ReparsePoint) == 0)
293                                 return false;
294
295                         return true;
296                 }
297
298                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
299                 public extern static bool GetFileStat (string path,
300                                                        out MonoIOStat stat,
301                                                        out MonoIOError error);
302
303                 // handle methods
304
305                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
306                 public extern static IntPtr Open (string filename,
307                                                   FileMode mode,
308                                                   FileAccess access,
309                                                   FileShare share,
310                                                   FileOptions options,
311                                                   out MonoIOError error);
312
313                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
314                 public extern static bool Close (IntPtr handle,
315                                                  out MonoIOError error);
316                 
317                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
318                 private extern static int Read (IntPtr handle, byte [] dest,
319                                                int dest_offset, int count,
320                                                out MonoIOError error);
321
322                 public static int Read (SafeHandle safeHandle, byte [] dest,
323                                                int dest_offset, int count,
324                                                out MonoIOError error)
325                 {
326                         bool release = false;
327                         try {
328                                 safeHandle.DangerousAddRef (ref release);
329                                 return Read (safeHandle.DangerousGetHandle (), dest, dest_offset, count, out error);
330                         } finally {
331                                 if (release)
332                                         safeHandle.DangerousRelease ();
333                         }
334                 }
335                 
336                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
337                 private extern static int Write (IntPtr handle, [In] byte [] src,
338                                                 int src_offset, int count,
339                                                 out MonoIOError error);
340
341                 public static int Write (SafeHandle safeHandle, byte [] src,
342                                                 int src_offset, int count,
343                                                 out MonoIOError error)
344                 {
345                         bool release = false;
346                         try {
347                                 safeHandle.DangerousAddRef (ref release);
348                                 return Write (safeHandle.DangerousGetHandle (), src, src_offset, count, out error);
349                         } finally {
350                                 if (release)
351                                         safeHandle.DangerousRelease ();
352                         }
353                 }
354                 
355                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
356                 private extern static long Seek (IntPtr handle, long offset,
357                                                 SeekOrigin origin,
358                                                 out MonoIOError error);
359
360                 public static long Seek (SafeHandle safeHandle, long offset,
361                                                 SeekOrigin origin,
362                                                 out MonoIOError error)
363                 {
364                         bool release = false;
365                         try {
366                                 safeHandle.DangerousAddRef (ref release);
367                                 return Seek (safeHandle.DangerousGetHandle (), offset, origin, out error);
368                         } finally {
369                                 if (release)
370                                         safeHandle.DangerousRelease ();
371                         }
372                 }
373                 
374                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
375                 private extern static bool Flush (IntPtr handle,
376                                                  out MonoIOError error);
377
378                 public static bool Flush (SafeHandle safeHandle,
379                                                  out MonoIOError error)
380                 {
381                         bool release = false;
382                         try {
383                                 safeHandle.DangerousAddRef (ref release);
384                                 return Flush (safeHandle.DangerousGetHandle (), out error);
385                         } finally {
386                                 if (release)
387                                         safeHandle.DangerousRelease ();
388                         }
389                 }
390
391                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
392                 private extern static long GetLength (IntPtr handle,
393                                                      out MonoIOError error);
394
395                 public static long GetLength (SafeHandle safeHandle,
396                                                      out MonoIOError error)
397                 {
398                         bool release = false;
399                         try {
400                                 safeHandle.DangerousAddRef (ref release);
401                                 return GetLength (safeHandle.DangerousGetHandle (), out error);
402                         } finally {
403                                 if (release)
404                                         safeHandle.DangerousRelease ();
405                         }
406                 }
407
408                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
409                 private extern static bool SetLength (IntPtr handle,
410                                                      long length,
411                                                      out MonoIOError error);
412
413                 public static bool SetLength (SafeHandle safeHandle,
414                                                      long length,
415                                                      out MonoIOError error)
416                 {
417                         bool release = false;
418                         try {
419                                 safeHandle.DangerousAddRef (ref release);
420                                 return SetLength (safeHandle.DangerousGetHandle (), length, out error);
421                         } finally {
422                                 if (release)
423                                         safeHandle.DangerousRelease ();
424                         }
425                 }
426
427                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
428                 private extern static bool SetFileTime (IntPtr handle,
429                                                        long creation_time,
430                                                        long last_access_time,
431                                                        long last_write_time,
432                                                        out MonoIOError error);
433
434                 public static bool SetFileTime (SafeHandle safeHandle,
435                                                        long creation_time,
436                                                        long last_access_time,
437                                                        long last_write_time,
438                                                        out MonoIOError error)
439                 {
440                         bool release = false;
441                         try {
442                                 safeHandle.DangerousAddRef (ref release);
443                                 return SetFileTime (safeHandle.DangerousGetHandle (), creation_time, last_access_time, last_write_time, out error);
444                         } finally {
445                                 if (release)
446                                         safeHandle.DangerousRelease ();
447                         }
448                 }
449
450                 public static bool SetFileTime (string path,
451                                                 long creation_time,
452                                                 long last_access_time,
453                                                 long last_write_time,
454                                                 out MonoIOError error)
455                 {
456                         return SetFileTime (path,
457                                 0,
458                                 creation_time,
459                                 last_access_time,
460                                 last_write_time,
461                                 DateTime.MinValue,
462                                 out error);
463                 }
464
465                 public static bool SetCreationTime (string path,
466                                                 DateTime dateTime,
467                                                 out MonoIOError error)
468                 {
469                         return SetFileTime (path, 1, -1, -1, -1, dateTime, out error);
470                 }
471
472                 public static bool SetLastAccessTime (string path,
473                                                 DateTime dateTime,
474                                                 out MonoIOError error)
475                 {
476                         return SetFileTime (path, 2, -1, -1, -1, dateTime, out error);
477                 }
478
479                 public static bool SetLastWriteTime (string path,
480                                                 DateTime dateTime,
481                                                 out MonoIOError error)
482                 {
483                         return SetFileTime (path, 3, -1, -1, -1, dateTime, out error);
484                 }
485
486                 public static bool SetFileTime (string path,
487                                                 int type,
488                                                 long creation_time,
489                                                 long last_access_time,
490                                                 long last_write_time,
491                                                 DateTime dateTime,
492                                                 out MonoIOError error)
493                 {
494                         IntPtr handle;
495                         bool result;
496
497                         handle = Open (path, FileMode.Open,
498                                        FileAccess.ReadWrite,
499                                        FileShare.ReadWrite, FileOptions.None, out error);
500                         if (handle == MonoIO.InvalidHandle)
501                                 return false;
502
503                         switch (type) {
504                         case 1:
505                                 creation_time = dateTime.ToFileTime ();
506                                 break;
507                         case 2:
508                                 last_access_time = dateTime.ToFileTime ();
509                                 break;
510                         case 3:
511                                 last_write_time = dateTime.ToFileTime ();
512                                 break;
513                         }
514
515                         result = SetFileTime (new SafeFileHandle(handle, false), creation_time,
516                                               last_access_time,
517                                               last_write_time, out error);
518
519                         MonoIOError ignore_error;
520                         Close (handle, out ignore_error);
521                         
522                         return result;
523                 }
524
525                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
526                 private extern static void Lock (IntPtr handle,
527                                                 long position, long length,
528                                                 out MonoIOError error);
529
530                 public static void Lock (SafeHandle safeHandle,
531                                                 long position, long length,
532                                                 out MonoIOError error)
533                 {
534                         bool release = false;
535                         try {
536                                 safeHandle.DangerousAddRef (ref release);
537                                 Lock (safeHandle.DangerousGetHandle (), position, length, out error);
538                         } finally {
539                                 if (release)
540                                         safeHandle.DangerousRelease ();
541                         }
542                 }
543
544                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
545                 private extern static void Unlock (IntPtr handle,
546                                                   long position, long length,
547                                                   out MonoIOError error);
548
549                 public static void Unlock (SafeHandle safeHandle,
550                                                   long position, long length,
551                                                   out MonoIOError error)
552                 {
553                         bool release = false;
554                         try {
555                                 safeHandle.DangerousAddRef (ref release);
556                                 Unlock (safeHandle.DangerousGetHandle (), position, length, out error);
557                         } finally {
558                                 if (release)
559                                         safeHandle.DangerousRelease ();
560                         }
561                 }
562
563                 // console handles
564
565                 public extern static IntPtr ConsoleOutput {
566                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
567                         get;
568                 }
569
570                 public extern static IntPtr ConsoleInput {
571                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
572                         get;
573                 }
574
575                 public extern static IntPtr ConsoleError {
576                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
577                         get;
578                 }
579
580                 // pipe handles
581
582                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
583                 public extern static bool CreatePipe (out IntPtr read_handle, out IntPtr write_handle, out MonoIOError error);
584
585                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
586                 public extern static bool DuplicateHandle (IntPtr source_process_handle, IntPtr source_handle,
587                         IntPtr target_process_handle, out IntPtr target_handle, int access, int inherit, int options, out MonoIOError error);
588
589                 // path characters
590
591                 public extern static char VolumeSeparatorChar {
592                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
593                         get;
594                 }
595
596                 public extern static char DirectorySeparatorChar {
597                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
598                         get;
599                 }
600
601                 public extern static char AltDirectorySeparatorChar {
602                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
603                         get;
604                 }
605
606                 public extern static char PathSeparator {
607                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
608                         get;
609                 }
610
611                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
612                 extern static void DumpHandles ();
613         }
614 }
615