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