7542dabe9cb20830a63f83c24d4edaad93bd73b8
[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 GetCurrentDirectory (out MonoIOError error);
179
180                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
181                 public extern static bool SetCurrentDirectory (string path, out MonoIOError error);
182
183                 // file methods
184
185                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
186                 public extern static bool MoveFile (string path, string dest,
187                                                     out MonoIOError error);
188
189                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
190                 public extern static bool CopyFile (string path, string dest,
191                                                     bool overwrite,
192                                                     out MonoIOError error);
193
194                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
195                 public extern static bool DeleteFile (string path,
196                                                       out MonoIOError error);
197
198                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
199                 public extern static bool ReplaceFile (string sourceFileName, 
200                                                        string destinationFileName, 
201                                                        string destinationBackupFileName, 
202                                                        bool ignoreMetadataErrors,
203                                                        out MonoIOError error);
204
205                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
206                 public extern static FileAttributes GetFileAttributes (string path, out MonoIOError error);
207
208                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
209                 public extern static bool SetFileAttributes (string path, FileAttributes attrs, out MonoIOError error);
210
211                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
212                 private extern static MonoFileType GetFileType (IntPtr handle, out MonoIOError error);
213
214                 public static MonoFileType GetFileType (SafeHandle safeHandle, out MonoIOError error)
215                 {
216                         bool release = false;
217                         try {
218                                 safeHandle.DangerousAddRef (ref release);
219                                 return GetFileType (safeHandle.DangerousGetHandle (), out error);
220                         } finally {
221                                 if (release)
222                                         safeHandle.DangerousRelease ();
223                         }
224                 }
225
226                 //
227                 // Find file methods
228                 //
229                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
230                 public extern static string FindFirst (string path, string pattern, out FileAttributes result_attr, out MonoIOError error, out IntPtr handle);
231                 
232                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
233                 public extern static string FindNext (IntPtr handle, out FileAttributes result_attr, out MonoIOError error);
234                 
235                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
236                 public extern static int FindClose (IntPtr handle);
237
238                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
239                 public extern static IntPtr FindFirstFile (string path_with_pattern, out string fileName, out int fileAttr, out int error);
240
241                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
242                 public extern static bool FindNextFile (IntPtr hnd, out string fileName, out int fileAttr, out int error);
243
244                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
245                 public extern static bool FindCloseFile (IntPtr hnd);
246
247                 public static bool Exists (string path, out MonoIOError error)
248                 {
249                         FileAttributes attrs = GetFileAttributes (path,
250                                                                   out error);
251                         if (attrs == InvalidFileAttributes)
252                                 return false;
253
254                         return true;
255                 }
256
257                 public static bool ExistsFile (string path,
258                                                out MonoIOError error)
259                 {
260                         FileAttributes attrs = GetFileAttributes (path,
261                                                                   out error);
262                         if (attrs == InvalidFileAttributes)
263                                 return false;
264
265                         if ((attrs & FileAttributes.Directory) != 0)
266                                 return false;
267
268                         return true;
269                 }
270
271                 public static bool ExistsDirectory (string path,
272                                                     out MonoIOError error)
273                 {
274                         FileAttributes attrs = GetFileAttributes (path,
275                                                                   out error);
276                                                                   
277                         // Actually, we are looking for a directory, not a file
278                         if (error == MonoIOError.ERROR_FILE_NOT_FOUND)
279                                 error = MonoIOError.ERROR_PATH_NOT_FOUND;
280                                 
281                         if (attrs == InvalidFileAttributes)
282                                 return false;
283
284                         if ((attrs & FileAttributes.Directory) == 0)
285                                 return false;
286
287                         return true;
288                 }
289
290                 public static bool ExistsSymlink (string path,
291                                                   out MonoIOError error)
292                 {
293                         FileAttributes attrs = GetFileAttributes (path,
294                                                                   out error);
295                         if (attrs == InvalidFileAttributes)
296                                 return false;
297                         
298                         if ((attrs & FileAttributes.ReparsePoint) == 0)
299                                 return false;
300
301                         return true;
302                 }
303
304                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
305                 public extern static bool GetFileStat (string path,
306                                                        out MonoIOStat stat,
307                                                        out MonoIOError error);
308
309                 // handle methods
310
311                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
312                 public extern static IntPtr Open (string filename,
313                                                   FileMode mode,
314                                                   FileAccess access,
315                                                   FileShare share,
316                                                   FileOptions options,
317                                                   out MonoIOError error);
318
319                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
320                 public extern static bool Close (IntPtr handle,
321                                                  out MonoIOError error);
322                 
323                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
324                 private extern static int Read (IntPtr handle, byte [] dest,
325                                                int dest_offset, int count,
326                                                out MonoIOError error);
327
328                 public static int Read (SafeHandle safeHandle, byte [] dest,
329                                                int dest_offset, int count,
330                                                out MonoIOError error)
331                 {
332                         bool release = false;
333                         try {
334                                 safeHandle.DangerousAddRef (ref release);
335                                 return Read (safeHandle.DangerousGetHandle (), dest, dest_offset, count, out error);
336                         } finally {
337                                 if (release)
338                                         safeHandle.DangerousRelease ();
339                         }
340                 }
341                 
342                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
343                 private extern static int Write (IntPtr handle, [In] byte [] src,
344                                                 int src_offset, int count,
345                                                 out MonoIOError error);
346
347                 public static int Write (SafeHandle safeHandle, byte [] src,
348                                                 int src_offset, int count,
349                                                 out MonoIOError error)
350                 {
351                         bool release = false;
352                         try {
353                                 safeHandle.DangerousAddRef (ref release);
354                                 return Write (safeHandle.DangerousGetHandle (), src, src_offset, count, out error);
355                         } finally {
356                                 if (release)
357                                         safeHandle.DangerousRelease ();
358                         }
359                 }
360                 
361                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
362                 private extern static long Seek (IntPtr handle, long offset,
363                                                 SeekOrigin origin,
364                                                 out MonoIOError error);
365
366                 public static long Seek (SafeHandle safeHandle, long offset,
367                                                 SeekOrigin origin,
368                                                 out MonoIOError error)
369                 {
370                         bool release = false;
371                         try {
372                                 safeHandle.DangerousAddRef (ref release);
373                                 return Seek (safeHandle.DangerousGetHandle (), offset, origin, out error);
374                         } finally {
375                                 if (release)
376                                         safeHandle.DangerousRelease ();
377                         }
378                 }
379                 
380                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
381                 private extern static bool Flush (IntPtr handle,
382                                                  out MonoIOError error);
383
384                 public static bool Flush (SafeHandle safeHandle,
385                                                  out MonoIOError error)
386                 {
387                         bool release = false;
388                         try {
389                                 safeHandle.DangerousAddRef (ref release);
390                                 return Flush (safeHandle.DangerousGetHandle (), out error);
391                         } finally {
392                                 if (release)
393                                         safeHandle.DangerousRelease ();
394                         }
395                 }
396
397                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
398                 private extern static long GetLength (IntPtr handle,
399                                                      out MonoIOError error);
400
401                 public static long GetLength (SafeHandle safeHandle,
402                                                      out MonoIOError error)
403                 {
404                         bool release = false;
405                         try {
406                                 safeHandle.DangerousAddRef (ref release);
407                                 return GetLength (safeHandle.DangerousGetHandle (), out error);
408                         } finally {
409                                 if (release)
410                                         safeHandle.DangerousRelease ();
411                         }
412                 }
413
414                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
415                 private extern static bool SetLength (IntPtr handle,
416                                                      long length,
417                                                      out MonoIOError error);
418
419                 public static bool SetLength (SafeHandle safeHandle,
420                                                      long length,
421                                                      out MonoIOError error)
422                 {
423                         bool release = false;
424                         try {
425                                 safeHandle.DangerousAddRef (ref release);
426                                 return SetLength (safeHandle.DangerousGetHandle (), length, out error);
427                         } finally {
428                                 if (release)
429                                         safeHandle.DangerousRelease ();
430                         }
431                 }
432
433                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
434                 private extern static bool SetFileTime (IntPtr handle,
435                                                        long creation_time,
436                                                        long last_access_time,
437                                                        long last_write_time,
438                                                        out MonoIOError error);
439
440                 public static bool SetFileTime (SafeHandle safeHandle,
441                                                        long creation_time,
442                                                        long last_access_time,
443                                                        long last_write_time,
444                                                        out MonoIOError error)
445                 {
446                         bool release = false;
447                         try {
448                                 safeHandle.DangerousAddRef (ref release);
449                                 return SetFileTime (safeHandle.DangerousGetHandle (), creation_time, last_access_time, last_write_time, out error);
450                         } finally {
451                                 if (release)
452                                         safeHandle.DangerousRelease ();
453                         }
454                 }
455
456                 public static bool SetFileTime (string path,
457                                                 long creation_time,
458                                                 long last_access_time,
459                                                 long last_write_time,
460                                                 out MonoIOError error)
461                 {
462                         return SetFileTime (path,
463                                 0,
464                                 creation_time,
465                                 last_access_time,
466                                 last_write_time,
467                                 DateTime.MinValue,
468                                 out error);
469                 }
470
471                 public static bool SetCreationTime (string path,
472                                                 DateTime dateTime,
473                                                 out MonoIOError error)
474                 {
475                         return SetFileTime (path, 1, -1, -1, -1, dateTime, out error);
476                 }
477
478                 public static bool SetLastAccessTime (string path,
479                                                 DateTime dateTime,
480                                                 out MonoIOError error)
481                 {
482                         return SetFileTime (path, 2, -1, -1, -1, dateTime, out error);
483                 }
484
485                 public static bool SetLastWriteTime (string path,
486                                                 DateTime dateTime,
487                                                 out MonoIOError error)
488                 {
489                         return SetFileTime (path, 3, -1, -1, -1, dateTime, out error);
490                 }
491
492                 public static bool SetFileTime (string path,
493                                                 int type,
494                                                 long creation_time,
495                                                 long last_access_time,
496                                                 long last_write_time,
497                                                 DateTime dateTime,
498                                                 out MonoIOError error)
499                 {
500                         IntPtr handle;
501                         bool result;
502
503                         handle = Open (path, FileMode.Open,
504                                        FileAccess.ReadWrite,
505                                        FileShare.ReadWrite, FileOptions.None, out error);
506                         if (handle == MonoIO.InvalidHandle)
507                                 return false;
508
509                         switch (type) {
510                         case 1:
511                                 creation_time = dateTime.ToFileTime ();
512                                 break;
513                         case 2:
514                                 last_access_time = dateTime.ToFileTime ();
515                                 break;
516                         case 3:
517                                 last_write_time = dateTime.ToFileTime ();
518                                 break;
519                         }
520
521                         result = SetFileTime (new SafeFileHandle(handle, false), creation_time,
522                                               last_access_time,
523                                               last_write_time, out error);
524
525                         MonoIOError ignore_error;
526                         Close (handle, out ignore_error);
527                         
528                         return result;
529                 }
530
531                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
532                 private extern static void Lock (IntPtr handle,
533                                                 long position, long length,
534                                                 out MonoIOError error);
535
536                 public static void Lock (SafeHandle safeHandle,
537                                                 long position, long length,
538                                                 out MonoIOError error)
539                 {
540                         bool release = false;
541                         try {
542                                 safeHandle.DangerousAddRef (ref release);
543                                 Lock (safeHandle.DangerousGetHandle (), position, length, out error);
544                         } finally {
545                                 if (release)
546                                         safeHandle.DangerousRelease ();
547                         }
548                 }
549
550                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
551                 private extern static void Unlock (IntPtr handle,
552                                                   long position, long length,
553                                                   out MonoIOError error);
554
555                 public static void Unlock (SafeHandle safeHandle,
556                                                   long position, long length,
557                                                   out MonoIOError error)
558                 {
559                         bool release = false;
560                         try {
561                                 safeHandle.DangerousAddRef (ref release);
562                                 Unlock (safeHandle.DangerousGetHandle (), position, length, out error);
563                         } finally {
564                                 if (release)
565                                         safeHandle.DangerousRelease ();
566                         }
567                 }
568
569                 // console handles
570
571                 public extern static IntPtr ConsoleOutput {
572                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
573                         get;
574                 }
575
576                 public extern static IntPtr ConsoleInput {
577                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
578                         get;
579                 }
580
581                 public extern static IntPtr ConsoleError {
582                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
583                         get;
584                 }
585
586                 // pipe handles
587
588                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
589                 public extern static bool CreatePipe (out IntPtr read_handle, out IntPtr write_handle, out MonoIOError error);
590
591                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
592                 public extern static bool DuplicateHandle (IntPtr source_process_handle, IntPtr source_handle,
593                         IntPtr target_process_handle, out IntPtr target_handle, int access, int inherit, int options, out MonoIOError error);
594
595                 // path characters
596
597                 public extern static char VolumeSeparatorChar {
598                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
599                         get;
600                 }
601
602                 public extern static char DirectorySeparatorChar {
603                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
604                         get;
605                 }
606
607                 public extern static char AltDirectorySeparatorChar {
608                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
609                         get;
610                 }
611
612                 public extern static char PathSeparator {
613                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
614                         get;
615                 }
616
617                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
618                 extern static void DumpHandles ();
619         }
620 }
621