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);
160 message = String.Format ("Win32 IO returned {0}. Path: {1}", error, path);
161 return new IOException (message, unchecked((int)0x80070000) | (int)error);
167 [MethodImplAttribute (MethodImplOptions.InternalCall)]
168 public extern static bool CreateDirectory (string path, out MonoIOError error);
170 [MethodImplAttribute (MethodImplOptions.InternalCall)]
171 public extern static bool RemoveDirectory (string path, out MonoIOError error);
173 [MethodImplAttribute (MethodImplOptions.InternalCall)]
174 public extern static string [] GetFileSystemEntries (string path, string path_with_pattern, int attrs, int mask, out MonoIOError error);
176 [MethodImplAttribute (MethodImplOptions.InternalCall)]
177 public extern static string GetCurrentDirectory (out MonoIOError error);
179 [MethodImplAttribute (MethodImplOptions.InternalCall)]
180 public extern static bool SetCurrentDirectory (string path, out MonoIOError error);
184 [MethodImplAttribute (MethodImplOptions.InternalCall)]
185 public extern static bool MoveFile (string path, string dest,
186 out MonoIOError error);
188 [MethodImplAttribute (MethodImplOptions.InternalCall)]
189 public extern static bool CopyFile (string path, string dest,
191 out MonoIOError error);
193 [MethodImplAttribute (MethodImplOptions.InternalCall)]
194 public extern static bool DeleteFile (string path,
195 out MonoIOError error);
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);
204 [MethodImplAttribute (MethodImplOptions.InternalCall)]
205 public extern static FileAttributes GetFileAttributes (string path, out MonoIOError error);
207 [MethodImplAttribute (MethodImplOptions.InternalCall)]
208 public extern static bool SetFileAttributes (string path, FileAttributes attrs, out MonoIOError error);
210 [MethodImplAttribute (MethodImplOptions.InternalCall)]
211 private extern static MonoFileType GetFileType (IntPtr handle, out MonoIOError error);
213 public static MonoFileType GetFileType (SafeHandle safeHandle, out MonoIOError error)
215 bool release = false;
217 safeHandle.DangerousAddRef (ref release);
218 return GetFileType (safeHandle.DangerousGetHandle (), out error);
221 safeHandle.DangerousRelease ();
228 [MethodImplAttribute (MethodImplOptions.InternalCall)]
229 public extern static string FindFirst (string path, string pattern, out FileAttributes result_attr, out MonoIOError error, out IntPtr handle);
231 [MethodImplAttribute (MethodImplOptions.InternalCall)]
232 public extern static string FindNext (IntPtr handle, out FileAttributes result_attr, out MonoIOError error);
234 [MethodImplAttribute (MethodImplOptions.InternalCall)]
235 public extern static int FindClose (IntPtr handle);
237 public static bool Exists (string path, out MonoIOError error)
239 FileAttributes attrs = GetFileAttributes (path,
241 if (attrs == InvalidFileAttributes)
247 public static bool ExistsFile (string path,
248 out MonoIOError error)
250 FileAttributes attrs = GetFileAttributes (path,
252 if (attrs == InvalidFileAttributes)
255 if ((attrs & FileAttributes.Directory) != 0)
261 public static bool ExistsDirectory (string path,
262 out MonoIOError error)
264 FileAttributes attrs = GetFileAttributes (path,
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;
271 if (attrs == InvalidFileAttributes)
274 if ((attrs & FileAttributes.Directory) == 0)
280 public static bool ExistsSymlink (string path,
281 out MonoIOError error)
283 FileAttributes attrs = GetFileAttributes (path,
285 if (attrs == InvalidFileAttributes)
288 if ((attrs & FileAttributes.ReparsePoint) == 0)
294 [MethodImplAttribute (MethodImplOptions.InternalCall)]
295 public extern static bool GetFileStat (string path,
297 out MonoIOError error);
301 [MethodImplAttribute (MethodImplOptions.InternalCall)]
302 public extern static IntPtr Open (string filename,
307 out MonoIOError error);
309 [MethodImplAttribute (MethodImplOptions.InternalCall)]
310 public extern static bool Close (IntPtr handle,
311 out MonoIOError error);
313 [MethodImplAttribute (MethodImplOptions.InternalCall)]
314 private extern static int Read (IntPtr handle, byte [] dest,
315 int dest_offset, int count,
316 out MonoIOError error);
318 public static int Read (SafeHandle safeHandle, byte [] dest,
319 int dest_offset, int count,
320 out MonoIOError error)
322 bool release = false;
324 safeHandle.DangerousAddRef (ref release);
325 return Read (safeHandle.DangerousGetHandle (), dest, dest_offset, count, out error);
328 safeHandle.DangerousRelease ();
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);
337 public static int Write (SafeHandle safeHandle, byte [] src,
338 int src_offset, int count,
339 out MonoIOError error)
341 bool release = false;
343 safeHandle.DangerousAddRef (ref release);
344 return Write (safeHandle.DangerousGetHandle (), src, src_offset, count, out error);
347 safeHandle.DangerousRelease ();
351 [MethodImplAttribute (MethodImplOptions.InternalCall)]
352 private extern static long Seek (IntPtr handle, long offset,
354 out MonoIOError error);
356 public static long Seek (SafeHandle safeHandle, long offset,
358 out MonoIOError error)
360 bool release = false;
362 safeHandle.DangerousAddRef (ref release);
363 return Seek (safeHandle.DangerousGetHandle (), offset, origin, out error);
366 safeHandle.DangerousRelease ();
370 [MethodImplAttribute (MethodImplOptions.InternalCall)]
371 private extern static bool Flush (IntPtr handle,
372 out MonoIOError error);
374 public static bool Flush (SafeHandle safeHandle,
375 out MonoIOError error)
377 bool release = false;
379 safeHandle.DangerousAddRef (ref release);
380 return Flush (safeHandle.DangerousGetHandle (), out error);
383 safeHandle.DangerousRelease ();
387 [MethodImplAttribute (MethodImplOptions.InternalCall)]
388 private extern static long GetLength (IntPtr handle,
389 out MonoIOError error);
391 public static long GetLength (SafeHandle safeHandle,
392 out MonoIOError error)
394 bool release = false;
396 safeHandle.DangerousAddRef (ref release);
397 return GetLength (safeHandle.DangerousGetHandle (), out error);
400 safeHandle.DangerousRelease ();
404 [MethodImplAttribute (MethodImplOptions.InternalCall)]
405 private extern static bool SetLength (IntPtr handle,
407 out MonoIOError error);
409 public static bool SetLength (SafeHandle safeHandle,
411 out MonoIOError error)
413 bool release = false;
415 safeHandle.DangerousAddRef (ref release);
416 return SetLength (safeHandle.DangerousGetHandle (), length, out error);
419 safeHandle.DangerousRelease ();
423 [MethodImplAttribute (MethodImplOptions.InternalCall)]
424 private extern static bool SetFileTime (IntPtr handle,
426 long last_access_time,
427 long last_write_time,
428 out MonoIOError error);
430 public static bool SetFileTime (SafeHandle safeHandle,
432 long last_access_time,
433 long last_write_time,
434 out MonoIOError error)
436 bool release = false;
438 safeHandle.DangerousAddRef (ref release);
439 return SetFileTime (safeHandle.DangerousGetHandle (), creation_time, last_access_time, last_write_time, out error);
442 safeHandle.DangerousRelease ();
446 public static bool SetFileTime (string path,
448 long last_access_time,
449 long last_write_time,
450 out MonoIOError error)
452 return SetFileTime (path,
461 public static bool SetCreationTime (string path,
463 out MonoIOError error)
465 return SetFileTime (path, 1, -1, -1, -1, dateTime, out error);
468 public static bool SetLastAccessTime (string path,
470 out MonoIOError error)
472 return SetFileTime (path, 2, -1, -1, -1, dateTime, out error);
475 public static bool SetLastWriteTime (string path,
477 out MonoIOError error)
479 return SetFileTime (path, 3, -1, -1, -1, dateTime, out error);
482 public static bool SetFileTime (string path,
485 long last_access_time,
486 long last_write_time,
488 out MonoIOError error)
493 handle = Open (path, FileMode.Open,
494 FileAccess.ReadWrite,
495 FileShare.ReadWrite, FileOptions.None, out error);
496 if (handle == MonoIO.InvalidHandle)
501 creation_time = dateTime.ToFileTime ();
504 last_access_time = dateTime.ToFileTime ();
507 last_write_time = dateTime.ToFileTime ();
511 result = SetFileTime (new SafeFileHandle(handle, false), creation_time,
513 last_write_time, out error);
515 MonoIOError ignore_error;
516 Close (handle, out ignore_error);
521 [MethodImplAttribute (MethodImplOptions.InternalCall)]
522 private extern static void Lock (IntPtr handle,
523 long position, long length,
524 out MonoIOError error);
526 public static void Lock (SafeHandle safeHandle,
527 long position, long length,
528 out MonoIOError error)
530 bool release = false;
532 safeHandle.DangerousAddRef (ref release);
533 Lock (safeHandle.DangerousGetHandle (), position, length, out error);
536 safeHandle.DangerousRelease ();
540 [MethodImplAttribute (MethodImplOptions.InternalCall)]
541 private extern static void Unlock (IntPtr handle,
542 long position, long length,
543 out MonoIOError error);
545 public static void Unlock (SafeHandle safeHandle,
546 long position, long length,
547 out MonoIOError error)
549 bool release = false;
551 safeHandle.DangerousAddRef (ref release);
552 Unlock (safeHandle.DangerousGetHandle (), position, length, out error);
555 safeHandle.DangerousRelease ();
561 public extern static IntPtr ConsoleOutput {
562 [MethodImplAttribute (MethodImplOptions.InternalCall)]
566 public extern static IntPtr ConsoleInput {
567 [MethodImplAttribute (MethodImplOptions.InternalCall)]
571 public extern static IntPtr ConsoleError {
572 [MethodImplAttribute (MethodImplOptions.InternalCall)]
578 [MethodImplAttribute (MethodImplOptions.InternalCall)]
579 public extern static bool CreatePipe (out IntPtr read_handle, out IntPtr write_handle, out MonoIOError error);
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);
587 public extern static char VolumeSeparatorChar {
588 [MethodImplAttribute (MethodImplOptions.InternalCall)]
592 public extern static char DirectorySeparatorChar {
593 [MethodImplAttribute (MethodImplOptions.InternalCall)]
597 public extern static char AltDirectorySeparatorChar {
598 [MethodImplAttribute (MethodImplOptions.InternalCall)]
602 public extern static char PathSeparator {
603 [MethodImplAttribute (MethodImplOptions.InternalCall)]
607 [MethodImplAttribute (MethodImplOptions.InternalCall)]
608 extern static void DumpHandles ();