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