1 // System.IO.MonoIO.cs: static interface to native filesystem.
4 // Dan Lewis (dihlewis@yahoo.co.uk)
5 // Dick Porter (dick@ximian.com)
8 // Copyright 2011 Xamarin Inc (http://www.xamarin.com).
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
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.
35 using System.Runtime.CompilerServices;
36 using System.Runtime.InteropServices;
37 using System.Threading;
38 using Microsoft.Win32.SafeHandles;
40 using System.IO.IsolatedStorage;
45 unsafe static class MonoIO {
46 public const int FileAlreadyExistsHResult = unchecked ((int) 0x80070000) | (int)MonoIOError.ERROR_FILE_EXISTS;
48 public const FileAttributes
49 InvalidFileAttributes = (FileAttributes)(-1);
51 public static readonly IntPtr
52 InvalidHandle = (IntPtr)(-1L);
54 static bool dump_handles = Environment.GetEnvironmentVariable ("MONO_DUMP_HANDLES_ON_ERROR_TOO_MANY_OPEN_FILES") != null;
57 public static Exception GetException (MonoIOError error)
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.
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);
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
78 return GetException (String.Empty, error);
82 public static Exception GetException (string path,
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);
93 case MonoIOError.ERROR_TOO_MANY_OPEN_FILES:
96 return new IOException ("Too many open files", unchecked((int)0x80070000) | (int)error);
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);
102 case MonoIOError.ERROR_ACCESS_DENIED:
103 message = String.Format ("Access to the path \"{0}\" is denied.", path);
104 return new UnauthorizedAccessException (message);
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);
112 return new DriveNotFoundException (message);
114 return new IOException (message, unchecked((int)0x80070000) | (int)error);
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);
120 case MonoIOError.ERROR_FILENAME_EXCED_RANGE:
121 message = String.Format ("Path is too long. Path: {0}", path);
122 return new PathTooLongException (message);
124 case MonoIOError.ERROR_INVALID_PARAMETER:
125 message = String.Format ("Invalid parameter");
126 return new IOException (message, unchecked((int)0x80070000) | (int)error);
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);
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);
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);
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);
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);
148 case MonoIOError.ERROR_ENCRYPTION_FAILED:
149 return new IOException ("Encryption failed", unchecked((int)0x80070000) | (int)error);
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);
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);
159 case MonoIOError.ERROR_DIRECTORY:
160 message = "The directory name is invalid";
161 return new IOException (message, unchecked((int)0x80070000) | (int)error);
164 message = String.Format ("Win32 IO returned {0}. Path: {1}", error, path);
165 return new IOException (message, unchecked((int)0x80070000) | (int)error);
171 [MethodImplAttribute (MethodImplOptions.InternalCall)]
172 public extern static bool CreateDirectory (string path, out MonoIOError error);
174 [MethodImplAttribute (MethodImplOptions.InternalCall)]
175 public extern static bool RemoveDirectory (string path, out MonoIOError error);
177 [MethodImplAttribute (MethodImplOptions.InternalCall)]
178 public extern static string [] GetFileSystemEntries (string path, string path_with_pattern, int attrs, int mask, out MonoIOError error);
180 [MethodImplAttribute (MethodImplOptions.InternalCall)]
181 public extern static string GetCurrentDirectory (out MonoIOError error);
183 [MethodImplAttribute (MethodImplOptions.InternalCall)]
184 public extern static bool SetCurrentDirectory (string path, out MonoIOError error);
188 [MethodImplAttribute (MethodImplOptions.InternalCall)]
189 public extern static bool MoveFile (string path, string dest,
190 out MonoIOError error);
192 [MethodImplAttribute (MethodImplOptions.InternalCall)]
193 public extern static bool CopyFile (string path, string dest,
195 out MonoIOError error);
197 [MethodImplAttribute (MethodImplOptions.InternalCall)]
198 public extern static bool DeleteFile (string path,
199 out MonoIOError error);
201 [MethodImplAttribute (MethodImplOptions.InternalCall)]
202 public extern static bool ReplaceFile (string sourceFileName,
203 string destinationFileName,
204 string destinationBackupFileName,
205 bool ignoreMetadataErrors,
206 out MonoIOError error);
208 [MethodImplAttribute (MethodImplOptions.InternalCall)]
209 public extern static FileAttributes GetFileAttributes (string path, out MonoIOError error);
211 [MethodImplAttribute (MethodImplOptions.InternalCall)]
212 public extern static bool SetFileAttributes (string path, FileAttributes attrs, out MonoIOError error);
214 [MethodImplAttribute (MethodImplOptions.InternalCall)]
215 private extern static MonoFileType GetFileType (IntPtr handle, out MonoIOError error);
217 public static MonoFileType GetFileType (SafeHandle safeHandle, out MonoIOError error)
219 bool release = false;
221 safeHandle.DangerousAddRef (ref release);
222 return GetFileType (safeHandle.DangerousGetHandle (), out error);
225 safeHandle.DangerousRelease ();
232 [MethodImplAttribute (MethodImplOptions.InternalCall)]
233 public extern static string FindFirst (string path, string pattern, out FileAttributes result_attr, out MonoIOError error, out IntPtr handle);
235 [MethodImplAttribute (MethodImplOptions.InternalCall)]
236 public extern static string FindNext (IntPtr handle, out FileAttributes result_attr, out MonoIOError error);
238 [MethodImplAttribute (MethodImplOptions.InternalCall)]
239 public extern static int FindClose (IntPtr handle);
241 [MethodImplAttribute (MethodImplOptions.InternalCall)]
242 public extern static IntPtr FindFirstFile (string path_with_pattern, out string fileName, out int fileAttr, out int error);
244 [MethodImplAttribute (MethodImplOptions.InternalCall)]
245 public extern static bool FindNextFile (IntPtr hnd, out string fileName, out int fileAttr, out int error);
247 [MethodImplAttribute (MethodImplOptions.InternalCall)]
248 public extern static bool FindCloseFile (IntPtr hnd);
250 public static bool Exists (string path, out MonoIOError error)
252 FileAttributes attrs = GetFileAttributes (path,
254 if (attrs == InvalidFileAttributes)
260 public static bool ExistsFile (string path,
261 out MonoIOError error)
263 FileAttributes attrs = GetFileAttributes (path,
265 if (attrs == InvalidFileAttributes)
268 if ((attrs & FileAttributes.Directory) != 0)
274 public static bool ExistsDirectory (string path,
275 out MonoIOError error)
277 FileAttributes attrs = GetFileAttributes (path,
280 // Actually, we are looking for a directory, not a file
281 if (error == MonoIOError.ERROR_FILE_NOT_FOUND)
282 error = MonoIOError.ERROR_PATH_NOT_FOUND;
284 if (attrs == InvalidFileAttributes)
287 if ((attrs & FileAttributes.Directory) == 0)
293 public static bool ExistsSymlink (string path,
294 out MonoIOError error)
296 FileAttributes attrs = GetFileAttributes (path,
298 if (attrs == InvalidFileAttributes)
301 if ((attrs & FileAttributes.ReparsePoint) == 0)
307 [MethodImplAttribute (MethodImplOptions.InternalCall)]
308 public extern static bool GetFileStat (string path,
310 out MonoIOError error);
314 [MethodImplAttribute (MethodImplOptions.InternalCall)]
315 public extern static IntPtr Open (string filename,
320 out MonoIOError error);
322 [MethodImplAttribute (MethodImplOptions.InternalCall)]
323 public extern static bool Close (IntPtr handle,
324 out MonoIOError error);
326 [MethodImplAttribute (MethodImplOptions.InternalCall)]
327 private extern static int Read (IntPtr handle, byte [] dest,
328 int dest_offset, int count,
329 out MonoIOError error);
331 public static int Read (SafeHandle safeHandle, byte [] dest,
332 int dest_offset, int count,
333 out MonoIOError error)
335 bool release = false;
337 safeHandle.DangerousAddRef (ref release);
338 return Read (safeHandle.DangerousGetHandle (), dest, dest_offset, count, out error);
341 safeHandle.DangerousRelease ();
345 [MethodImplAttribute (MethodImplOptions.InternalCall)]
346 private extern static int Write (IntPtr handle, [In] byte [] src,
347 int src_offset, int count,
348 out MonoIOError error);
350 public static int Write (SafeHandle safeHandle, byte [] src,
351 int src_offset, int count,
352 out MonoIOError error)
354 bool release = false;
356 safeHandle.DangerousAddRef (ref release);
357 return Write (safeHandle.DangerousGetHandle (), src, src_offset, count, out error);
360 safeHandle.DangerousRelease ();
364 [MethodImplAttribute (MethodImplOptions.InternalCall)]
365 private extern static long Seek (IntPtr handle, long offset,
367 out MonoIOError error);
369 public static long Seek (SafeHandle safeHandle, long offset,
371 out MonoIOError error)
373 bool release = false;
375 safeHandle.DangerousAddRef (ref release);
376 return Seek (safeHandle.DangerousGetHandle (), offset, origin, out error);
379 safeHandle.DangerousRelease ();
383 [MethodImplAttribute (MethodImplOptions.InternalCall)]
384 private extern static bool Flush (IntPtr handle,
385 out MonoIOError error);
387 public static bool Flush (SafeHandle safeHandle,
388 out MonoIOError error)
390 bool release = false;
392 safeHandle.DangerousAddRef (ref release);
393 return Flush (safeHandle.DangerousGetHandle (), out error);
396 safeHandle.DangerousRelease ();
400 [MethodImplAttribute (MethodImplOptions.InternalCall)]
401 private extern static long GetLength (IntPtr handle,
402 out MonoIOError error);
404 public static long GetLength (SafeHandle safeHandle,
405 out MonoIOError error)
407 bool release = false;
409 safeHandle.DangerousAddRef (ref release);
410 return GetLength (safeHandle.DangerousGetHandle (), out error);
413 safeHandle.DangerousRelease ();
417 [MethodImplAttribute (MethodImplOptions.InternalCall)]
418 private extern static bool SetLength (IntPtr handle,
420 out MonoIOError error);
422 public static bool SetLength (SafeHandle safeHandle,
424 out MonoIOError error)
426 bool release = false;
428 safeHandle.DangerousAddRef (ref release);
429 return SetLength (safeHandle.DangerousGetHandle (), length, out error);
432 safeHandle.DangerousRelease ();
436 [MethodImplAttribute (MethodImplOptions.InternalCall)]
437 private extern static bool SetFileTime (IntPtr handle,
439 long last_access_time,
440 long last_write_time,
441 out MonoIOError error);
443 public static bool SetFileTime (SafeHandle safeHandle,
445 long last_access_time,
446 long last_write_time,
447 out MonoIOError error)
449 bool release = false;
451 safeHandle.DangerousAddRef (ref release);
452 return SetFileTime (safeHandle.DangerousGetHandle (), creation_time, last_access_time, last_write_time, out error);
455 safeHandle.DangerousRelease ();
459 public static bool SetFileTime (string path,
461 long last_access_time,
462 long last_write_time,
463 out MonoIOError error)
465 return SetFileTime (path,
474 public static bool SetCreationTime (string path,
476 out MonoIOError error)
478 return SetFileTime (path, 1, -1, -1, -1, dateTime, out error);
481 public static bool SetLastAccessTime (string path,
483 out MonoIOError error)
485 return SetFileTime (path, 2, -1, -1, -1, dateTime, out error);
488 public static bool SetLastWriteTime (string path,
490 out MonoIOError error)
492 return SetFileTime (path, 3, -1, -1, -1, dateTime, out error);
495 public static bool SetFileTime (string path,
498 long last_access_time,
499 long last_write_time,
501 out MonoIOError error)
506 handle = Open (path, FileMode.Open,
507 FileAccess.ReadWrite,
508 FileShare.ReadWrite, FileOptions.None, out error);
509 if (handle == MonoIO.InvalidHandle)
514 creation_time = dateTime.ToFileTime ();
517 last_access_time = dateTime.ToFileTime ();
520 last_write_time = dateTime.ToFileTime ();
524 result = SetFileTime (new SafeFileHandle(handle, false), creation_time,
526 last_write_time, out error);
528 MonoIOError ignore_error;
529 Close (handle, out ignore_error);
534 [MethodImplAttribute (MethodImplOptions.InternalCall)]
535 private extern static void Lock (IntPtr handle,
536 long position, long length,
537 out MonoIOError error);
539 public static void Lock (SafeHandle safeHandle,
540 long position, long length,
541 out MonoIOError error)
543 bool release = false;
545 safeHandle.DangerousAddRef (ref release);
546 Lock (safeHandle.DangerousGetHandle (), position, length, out error);
549 safeHandle.DangerousRelease ();
553 [MethodImplAttribute (MethodImplOptions.InternalCall)]
554 private extern static void Unlock (IntPtr handle,
555 long position, long length,
556 out MonoIOError error);
558 public static void Unlock (SafeHandle safeHandle,
559 long position, long length,
560 out MonoIOError error)
562 bool release = false;
564 safeHandle.DangerousAddRef (ref release);
565 Unlock (safeHandle.DangerousGetHandle (), position, length, out error);
568 safeHandle.DangerousRelease ();
574 public extern static IntPtr ConsoleOutput {
575 [MethodImplAttribute (MethodImplOptions.InternalCall)]
579 public extern static IntPtr ConsoleInput {
580 [MethodImplAttribute (MethodImplOptions.InternalCall)]
584 public extern static IntPtr ConsoleError {
585 [MethodImplAttribute (MethodImplOptions.InternalCall)]
591 [MethodImplAttribute (MethodImplOptions.InternalCall)]
592 public extern static bool CreatePipe (out IntPtr read_handle, out IntPtr write_handle, out MonoIOError error);
594 [MethodImplAttribute (MethodImplOptions.InternalCall)]
595 public extern static bool DuplicateHandle (IntPtr source_process_handle, IntPtr source_handle,
596 IntPtr target_process_handle, out IntPtr target_handle, int access, int inherit, int options, out MonoIOError error);
600 public extern static char VolumeSeparatorChar {
601 [MethodImplAttribute (MethodImplOptions.InternalCall)]
605 public extern static char DirectorySeparatorChar {
606 [MethodImplAttribute (MethodImplOptions.InternalCall)]
610 public extern static char AltDirectorySeparatorChar {
611 [MethodImplAttribute (MethodImplOptions.InternalCall)]
615 public extern static char PathSeparator {
616 [MethodImplAttribute (MethodImplOptions.InternalCall)]
620 [MethodImplAttribute (MethodImplOptions.InternalCall)]
621 extern static void DumpHandles ();