[w32file] Move MonoIO.Find{First,Next,Close} to managed
[mono.git] / mcs / class / corlib / System.IO / MonoIO.cs
1 // System.IO.MonoIO.cs: static interface to native filesystem.
2 //
3 // Author:
4 //   Dan Lewis (dihlewis@yahoo.co.uk)
5 //   Dick Porter (dick@ximian.com)
6 //
7 // (C) 2002
8 // Copyright 2011 Xamarin Inc (http://www.xamarin.com).
9 //
10
11 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34 using System;
35 using System.Runtime.CompilerServices;
36 using System.Runtime.InteropServices;
37 using System.Threading;
38 using Microsoft.Win32.SafeHandles;
39 #if MOBILE
40 using System.IO.IsolatedStorage;
41 #endif
42
43 namespace System.IO
44 {
45         unsafe static class MonoIO {
46                 public const int FileAlreadyExistsHResult = unchecked ((int) 0x80070000) | (int)MonoIOError.ERROR_FILE_EXISTS;
47
48                 public const FileAttributes
49                         InvalidFileAttributes = (FileAttributes)(-1);
50
51                 public static readonly IntPtr
52                         InvalidHandle = (IntPtr)(-1L);
53
54                 static bool dump_handles = Environment.GetEnvironmentVariable ("MONO_DUMP_HANDLES_ON_ERROR_TOO_MANY_OPEN_FILES") != null;
55
56                 // error methods
57                 public static Exception GetException (MonoIOError error)
58                 {
59                         /* This overload is currently only called from
60                          * File.MoveFile(), Directory.Move() and
61                          * Directory.GetCurrentDirectory() -
62                          * everywhere else supplies a path to format
63                          * with the error text.
64                          */
65                         switch(error) {
66                         case MonoIOError.ERROR_ACCESS_DENIED:
67                                 return new UnauthorizedAccessException ("Access to the path is denied.");
68                         case MonoIOError.ERROR_FILE_EXISTS:
69                                 string message = "Cannot create a file that already exist.";
70                                 return new IOException (message, FileAlreadyExistsHResult);
71                         default:
72                                 /* Add more mappings here if other
73                                  * errors trigger the named but empty
74                                  * path bug (see bug 82141.) For
75                                  * everything else, fall through to
76                                  * the other overload
77                                  */
78                                 return GetException (String.Empty, error);
79                         }
80                 }
81
82                 public static Exception GetException (string path,
83                                                       MonoIOError error)
84                 {
85                         string message;
86
87                         switch (error) {
88                         // FIXME: add more exception mappings here
89                         case MonoIOError.ERROR_FILE_NOT_FOUND:
90                                 message = String.Format ("Could not find file \"{0}\"", path);
91                                 return new FileNotFoundException (message, path);
92
93                         case MonoIOError.ERROR_TOO_MANY_OPEN_FILES:
94                                 if (dump_handles)
95                                         DumpHandles ();
96                                 return new IOException ("Too many open files", unchecked((int)0x80070000) | (int)error);
97                                 
98                         case MonoIOError.ERROR_PATH_NOT_FOUND:
99                                 message = String.Format ("Could not find a part of the path \"{0}\"", path);
100                                 return new DirectoryNotFoundException (message);
101
102                         case MonoIOError.ERROR_ACCESS_DENIED:
103                                 message = String.Format ("Access to the path \"{0}\" is denied.", path);
104                                 return new UnauthorizedAccessException (message);
105
106                         case MonoIOError.ERROR_INVALID_HANDLE:
107                                 message = String.Format ("Invalid handle to path \"{0}\"", path);
108                                 return new IOException (message, unchecked((int)0x80070000) | (int)error);
109                         case MonoIOError.ERROR_INVALID_DRIVE:
110                                 message = String.Format ("Could not find the drive  '{0}'. The drive might not be ready or might not be mapped.", path);
111 #if !MOBILE
112                                 return new DriveNotFoundException (message);
113 #else
114                                 return new IOException (message, unchecked((int)0x80070000) | (int)error);
115 #endif
116                         case MonoIOError.ERROR_FILE_EXISTS:
117                                 message = String.Format ("Could not create file \"{0}\". File already exists.", path);
118                                 return new IOException (message, unchecked((int)0x80070000) | (int)error);
119
120                         case MonoIOError.ERROR_FILENAME_EXCED_RANGE:
121                                 message = String.Format ("Path is too long. Path: {0}", path); 
122                                 return new PathTooLongException (message);
123
124                         case MonoIOError.ERROR_INVALID_PARAMETER:
125                                 message = String.Format ("Invalid parameter");
126                                 return new IOException (message, unchecked((int)0x80070000) | (int)error);
127
128                         case MonoIOError.ERROR_WRITE_FAULT:
129                                 message = String.Format ("Write fault on path {0}", path);
130                                 return new IOException (message, unchecked((int)0x80070000) | (int)error);
131
132                         case MonoIOError.ERROR_SHARING_VIOLATION:
133                                 message = String.Format ("Sharing violation on path {0}", path);
134                                 return new IOException (message, unchecked((int)0x80070000) | (int)error);
135                                 
136                         case MonoIOError.ERROR_LOCK_VIOLATION:
137                                 message = String.Format ("Lock violation on path {0}", path);
138                                 return new IOException (message, unchecked((int)0x80070000) | (int)error);
139                         
140                         case MonoIOError.ERROR_HANDLE_DISK_FULL:
141                                 message = String.Format ("Disk full. Path {0}", path);
142                                 return new IOException (message, unchecked((int)0x80070000) | (int)error);
143                         
144                         case MonoIOError.ERROR_DIR_NOT_EMPTY:
145                                 message = String.Format ("Directory {0} is not empty", path);
146                                 return new IOException (message, unchecked((int)0x80070000) | (int)error);
147
148                         case MonoIOError.ERROR_ENCRYPTION_FAILED:
149                                 return new IOException ("Encryption failed", unchecked((int)0x80070000) | (int)error);
150
151                         case MonoIOError.ERROR_CANNOT_MAKE:
152                                 message = String.Format ("Path {0} is a directory", path);
153                                 return new IOException (message, unchecked((int)0x80070000) | (int)error);
154                                 
155                         case MonoIOError.ERROR_NOT_SAME_DEVICE:
156                                 message = "Source and destination are not on the same device";
157                                 return new IOException (message, unchecked((int)0x80070000) | (int)error);
158
159                         case MonoIOError.ERROR_DIRECTORY:
160                                 message = "The directory name is invalid";
161                                 return new IOException (message, unchecked((int)0x80070000) | (int)error);
162                                 
163                         default:
164                                 message = String.Format ("Win32 IO returned {0}. Path: {1}", error, path);
165                                 return new IOException (message, unchecked((int)0x80070000) | (int)error);
166                         }
167                 }
168
169                 // directory methods
170
171                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
172                 public extern static bool CreateDirectory (string path, out MonoIOError error);
173
174                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
175                 public extern static bool RemoveDirectory (string path, out MonoIOError error);
176
177                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
178                 public extern static string GetCurrentDirectory (out MonoIOError error);
179
180                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
181                 public extern static bool SetCurrentDirectory (string path, out MonoIOError error);
182
183                 // file methods
184
185                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
186                 public extern static bool MoveFile (string path, string dest,
187                                                     out MonoIOError error);
188
189                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
190                 public extern static bool CopyFile (string path, string dest,
191                                                     bool overwrite,
192                                                     out MonoIOError error);
193
194                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
195                 public extern static bool DeleteFile (string path,
196                                                       out MonoIOError error);
197
198                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
199                 public extern static bool ReplaceFile (string sourceFileName, 
200                                                        string destinationFileName, 
201                                                        string destinationBackupFileName, 
202                                                        bool ignoreMetadataErrors,
203                                                        out MonoIOError error);
204
205                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
206                 public extern static FileAttributes GetFileAttributes (string path, out MonoIOError error);
207
208                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
209                 public extern static bool SetFileAttributes (string path, FileAttributes attrs, out MonoIOError error);
210
211                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
212                 private extern static MonoFileType GetFileType (IntPtr handle, out MonoIOError error);
213
214                 public static MonoFileType GetFileType (SafeHandle safeHandle, out MonoIOError error)
215                 {
216                         bool release = false;
217                         try {
218                                 safeHandle.DangerousAddRef (ref release);
219                                 return GetFileType (safeHandle.DangerousGetHandle (), out error);
220                         } finally {
221                                 if (release)
222                                         safeHandle.DangerousRelease ();
223                         }
224                 }
225
226                 //
227                 // Find file methods
228                 //
229
230                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
231                 public extern static IntPtr FindFirstFile (string path_with_pattern, out string fileName, out int fileAttr, out int error);
232
233                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
234                 public extern static bool FindNextFile (IntPtr hnd, out string fileName, out int fileAttr, out int error);
235
236                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
237                 public extern static bool FindCloseFile (IntPtr hnd);
238
239                 public static bool Exists (string path, out MonoIOError error)
240                 {
241                         FileAttributes attrs = GetFileAttributes (path,
242                                                                   out error);
243                         if (attrs == InvalidFileAttributes)
244                                 return false;
245
246                         return true;
247                 }
248
249                 public static bool ExistsFile (string path,
250                                                out MonoIOError error)
251                 {
252                         FileAttributes attrs = GetFileAttributes (path,
253                                                                   out error);
254                         if (attrs == InvalidFileAttributes)
255                                 return false;
256
257                         if ((attrs & FileAttributes.Directory) != 0)
258                                 return false;
259
260                         return true;
261                 }
262
263                 public static bool ExistsDirectory (string path,
264                                                     out MonoIOError error)
265                 {
266                         FileAttributes attrs = GetFileAttributes (path,
267                                                                   out error);
268                                                                   
269                         // Actually, we are looking for a directory, not a file
270                         if (error == MonoIOError.ERROR_FILE_NOT_FOUND)
271                                 error = MonoIOError.ERROR_PATH_NOT_FOUND;
272                                 
273                         if (attrs == InvalidFileAttributes)
274                                 return false;
275
276                         if ((attrs & FileAttributes.Directory) == 0)
277                                 return false;
278
279                         return true;
280                 }
281
282                 public static bool ExistsSymlink (string path,
283                                                   out MonoIOError error)
284                 {
285                         FileAttributes attrs = GetFileAttributes (path,
286                                                                   out error);
287                         if (attrs == InvalidFileAttributes)
288                                 return false;
289                         
290                         if ((attrs & FileAttributes.ReparsePoint) == 0)
291                                 return false;
292
293                         return true;
294                 }
295
296                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
297                 public extern static bool GetFileStat (string path,
298                                                        out MonoIOStat stat,
299                                                        out MonoIOError error);
300
301                 // handle methods
302
303                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
304                 public extern static IntPtr Open (string filename,
305                                                   FileMode mode,
306                                                   FileAccess access,
307                                                   FileShare share,
308                                                   FileOptions options,
309                                                   out MonoIOError error);
310
311                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
312                 public extern static bool Close (IntPtr handle,
313                                                  out MonoIOError error);
314                 
315                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
316                 private extern static int Read (IntPtr handle, byte [] dest,
317                                                int dest_offset, int count,
318                                                out MonoIOError error);
319
320                 public static int Read (SafeHandle safeHandle, byte [] dest,
321                                                int dest_offset, int count,
322                                                out MonoIOError error)
323                 {
324                         bool release = false;
325                         try {
326                                 safeHandle.DangerousAddRef (ref release);
327                                 return Read (safeHandle.DangerousGetHandle (), dest, dest_offset, count, out error);
328                         } finally {
329                                 if (release)
330                                         safeHandle.DangerousRelease ();
331                         }
332                 }
333                 
334                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
335                 private extern static int Write (IntPtr handle, [In] byte [] src,
336                                                 int src_offset, int count,
337                                                 out MonoIOError error);
338
339                 public static int Write (SafeHandle safeHandle, byte [] src,
340                                                 int src_offset, int count,
341                                                 out MonoIOError error)
342                 {
343                         bool release = false;
344                         try {
345                                 safeHandle.DangerousAddRef (ref release);
346                                 return Write (safeHandle.DangerousGetHandle (), src, src_offset, count, out error);
347                         } finally {
348                                 if (release)
349                                         safeHandle.DangerousRelease ();
350                         }
351                 }
352                 
353                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
354                 private extern static long Seek (IntPtr handle, long offset,
355                                                 SeekOrigin origin,
356                                                 out MonoIOError error);
357
358                 public static long Seek (SafeHandle safeHandle, long offset,
359                                                 SeekOrigin origin,
360                                                 out MonoIOError error)
361                 {
362                         bool release = false;
363                         try {
364                                 safeHandle.DangerousAddRef (ref release);
365                                 return Seek (safeHandle.DangerousGetHandle (), offset, origin, out error);
366                         } finally {
367                                 if (release)
368                                         safeHandle.DangerousRelease ();
369                         }
370                 }
371                 
372                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
373                 private extern static bool Flush (IntPtr handle,
374                                                  out MonoIOError error);
375
376                 public static bool Flush (SafeHandle safeHandle,
377                                                  out MonoIOError error)
378                 {
379                         bool release = false;
380                         try {
381                                 safeHandle.DangerousAddRef (ref release);
382                                 return Flush (safeHandle.DangerousGetHandle (), out error);
383                         } finally {
384                                 if (release)
385                                         safeHandle.DangerousRelease ();
386                         }
387                 }
388
389                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
390                 private extern static long GetLength (IntPtr handle,
391                                                      out MonoIOError error);
392
393                 public static long GetLength (SafeHandle safeHandle,
394                                                      out MonoIOError error)
395                 {
396                         bool release = false;
397                         try {
398                                 safeHandle.DangerousAddRef (ref release);
399                                 return GetLength (safeHandle.DangerousGetHandle (), out error);
400                         } finally {
401                                 if (release)
402                                         safeHandle.DangerousRelease ();
403                         }
404                 }
405
406                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
407                 private extern static bool SetLength (IntPtr handle,
408                                                      long length,
409                                                      out MonoIOError error);
410
411                 public static bool SetLength (SafeHandle safeHandle,
412                                                      long length,
413                                                      out MonoIOError error)
414                 {
415                         bool release = false;
416                         try {
417                                 safeHandle.DangerousAddRef (ref release);
418                                 return SetLength (safeHandle.DangerousGetHandle (), length, out error);
419                         } finally {
420                                 if (release)
421                                         safeHandle.DangerousRelease ();
422                         }
423                 }
424
425                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
426                 private extern static bool SetFileTime (IntPtr handle,
427                                                        long creation_time,
428                                                        long last_access_time,
429                                                        long last_write_time,
430                                                        out MonoIOError error);
431
432                 public static bool SetFileTime (SafeHandle safeHandle,
433                                                        long creation_time,
434                                                        long last_access_time,
435                                                        long last_write_time,
436                                                        out MonoIOError error)
437                 {
438                         bool release = false;
439                         try {
440                                 safeHandle.DangerousAddRef (ref release);
441                                 return SetFileTime (safeHandle.DangerousGetHandle (), creation_time, last_access_time, last_write_time, out error);
442                         } finally {
443                                 if (release)
444                                         safeHandle.DangerousRelease ();
445                         }
446                 }
447
448                 public static bool SetFileTime (string path,
449                                                 long creation_time,
450                                                 long last_access_time,
451                                                 long last_write_time,
452                                                 out MonoIOError error)
453                 {
454                         return SetFileTime (path,
455                                 0,
456                                 creation_time,
457                                 last_access_time,
458                                 last_write_time,
459                                 DateTime.MinValue,
460                                 out error);
461                 }
462
463                 public static bool SetCreationTime (string path,
464                                                 DateTime dateTime,
465                                                 out MonoIOError error)
466                 {
467                         return SetFileTime (path, 1, -1, -1, -1, dateTime, out error);
468                 }
469
470                 public static bool SetLastAccessTime (string path,
471                                                 DateTime dateTime,
472                                                 out MonoIOError error)
473                 {
474                         return SetFileTime (path, 2, -1, -1, -1, dateTime, out error);
475                 }
476
477                 public static bool SetLastWriteTime (string path,
478                                                 DateTime dateTime,
479                                                 out MonoIOError error)
480                 {
481                         return SetFileTime (path, 3, -1, -1, -1, dateTime, out error);
482                 }
483
484                 public static bool SetFileTime (string path,
485                                                 int type,
486                                                 long creation_time,
487                                                 long last_access_time,
488                                                 long last_write_time,
489                                                 DateTime dateTime,
490                                                 out MonoIOError error)
491                 {
492                         IntPtr handle;
493                         bool result;
494
495                         handle = Open (path, FileMode.Open,
496                                        FileAccess.ReadWrite,
497                                        FileShare.ReadWrite, FileOptions.None, out error);
498                         if (handle == MonoIO.InvalidHandle)
499                                 return false;
500
501                         switch (type) {
502                         case 1:
503                                 creation_time = dateTime.ToFileTime ();
504                                 break;
505                         case 2:
506                                 last_access_time = dateTime.ToFileTime ();
507                                 break;
508                         case 3:
509                                 last_write_time = dateTime.ToFileTime ();
510                                 break;
511                         }
512
513                         result = SetFileTime (new SafeFileHandle(handle, false), creation_time,
514                                               last_access_time,
515                                               last_write_time, out error);
516
517                         MonoIOError ignore_error;
518                         Close (handle, out ignore_error);
519                         
520                         return result;
521                 }
522
523                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
524                 private extern static void Lock (IntPtr handle,
525                                                 long position, long length,
526                                                 out MonoIOError error);
527
528                 public static void Lock (SafeHandle safeHandle,
529                                                 long position, long length,
530                                                 out MonoIOError error)
531                 {
532                         bool release = false;
533                         try {
534                                 safeHandle.DangerousAddRef (ref release);
535                                 Lock (safeHandle.DangerousGetHandle (), position, length, out error);
536                         } finally {
537                                 if (release)
538                                         safeHandle.DangerousRelease ();
539                         }
540                 }
541
542                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
543                 private extern static void Unlock (IntPtr handle,
544                                                   long position, long length,
545                                                   out MonoIOError error);
546
547                 public static void Unlock (SafeHandle safeHandle,
548                                                   long position, long length,
549                                                   out MonoIOError error)
550                 {
551                         bool release = false;
552                         try {
553                                 safeHandle.DangerousAddRef (ref release);
554                                 Unlock (safeHandle.DangerousGetHandle (), position, length, out error);
555                         } finally {
556                                 if (release)
557                                         safeHandle.DangerousRelease ();
558                         }
559                 }
560
561                 // console handles
562
563                 public extern static IntPtr ConsoleOutput {
564                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
565                         get;
566                 }
567
568                 public extern static IntPtr ConsoleInput {
569                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
570                         get;
571                 }
572
573                 public extern static IntPtr ConsoleError {
574                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
575                         get;
576                 }
577
578                 // pipe handles
579
580                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
581                 public extern static bool CreatePipe (out IntPtr read_handle, out IntPtr write_handle, out MonoIOError error);
582
583                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
584                 public extern static bool DuplicateHandle (IntPtr source_process_handle, IntPtr source_handle,
585                         IntPtr target_process_handle, out IntPtr target_handle, int access, int inherit, int options, out MonoIOError error);
586
587                 // path characters
588
589                 public extern static char VolumeSeparatorChar {
590                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
591                         get;
592                 }
593
594                 public extern static char DirectorySeparatorChar {
595                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
596                         get;
597                 }
598
599                 public extern static char AltDirectorySeparatorChar {
600                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
601                         get;
602                 }
603
604                 public extern static char PathSeparator {
605                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
606                         get;
607                 }
608
609                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
610                 extern static void DumpHandles ();
611         }
612 }
613