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