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);
55 public static Exception GetException (MonoIOError error)
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.
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);
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
76 return GetException (String.Empty, error);
80 public static Exception GetException (string path,
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);
91 case MonoIOError.ERROR_TOO_MANY_OPEN_FILES:
92 return new IOException ("Too many open files", unchecked((int)0x80070000) | (int)error);
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);
98 case MonoIOError.ERROR_ACCESS_DENIED:
99 message = String.Format ("Access to the path \"{0}\" is denied.", path);
100 return new UnauthorizedAccessException (message);
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);
108 return new DriveNotFoundException (message);
110 return new IOException (message, unchecked((int)0x80070000) | (int)error);
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);
116 case MonoIOError.ERROR_FILENAME_EXCED_RANGE:
117 message = String.Format ("Path is too long. Path: {0}", path);
118 return new PathTooLongException (message);
120 case MonoIOError.ERROR_INVALID_PARAMETER:
121 message = String.Format ("Invalid parameter");
122 return new IOException (message, unchecked((int)0x80070000) | (int)error);
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);
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);
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);
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);
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);
144 case MonoIOError.ERROR_ENCRYPTION_FAILED:
145 return new IOException ("Encryption failed", unchecked((int)0x80070000) | (int)error);
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);
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);
156 message = String.Format ("Win32 IO returned {0}. Path: {1}", error, path);
157 return new IOException (message, unchecked((int)0x80070000) | (int)error);
163 [MethodImplAttribute (MethodImplOptions.InternalCall)]
164 public extern static bool CreateDirectory (string path, out MonoIOError error);
166 [MethodImplAttribute (MethodImplOptions.InternalCall)]
167 public extern static bool RemoveDirectory (string path, out MonoIOError error);
169 [MethodImplAttribute (MethodImplOptions.InternalCall)]
170 public extern static string [] GetFileSystemEntries (string path, string path_with_pattern, int attrs, int mask, out MonoIOError error);
172 [MethodImplAttribute (MethodImplOptions.InternalCall)]
173 public extern static string GetCurrentDirectory (out MonoIOError error);
175 [MethodImplAttribute (MethodImplOptions.InternalCall)]
176 public extern static bool SetCurrentDirectory (string path, out MonoIOError error);
180 [MethodImplAttribute (MethodImplOptions.InternalCall)]
181 public extern static bool MoveFile (string path, string dest,
182 out MonoIOError error);
184 [MethodImplAttribute (MethodImplOptions.InternalCall)]
185 public extern static bool CopyFile (string path, string dest,
187 out MonoIOError error);
189 [MethodImplAttribute (MethodImplOptions.InternalCall)]
190 public extern static bool DeleteFile (string path,
191 out MonoIOError error);
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);
200 [MethodImplAttribute (MethodImplOptions.InternalCall)]
201 public extern static FileAttributes GetFileAttributes (string path, out MonoIOError error);
203 [MethodImplAttribute (MethodImplOptions.InternalCall)]
204 public extern static bool SetFileAttributes (string path, FileAttributes attrs, out MonoIOError error);
206 [MethodImplAttribute (MethodImplOptions.InternalCall)]
207 private extern static MonoFileType GetFileType (IntPtr handle, out MonoIOError error);
209 public static MonoFileType GetFileType (SafeHandle safeHandle, out MonoIOError error)
211 bool release = false;
213 safeHandle.DangerousAddRef (ref release);
214 return GetFileType (safeHandle.DangerousGetHandle (), out error);
217 safeHandle.DangerousRelease ();
224 [MethodImplAttribute (MethodImplOptions.InternalCall)]
225 public extern static string FindFirst (string path, string pattern, out FileAttributes result_attr, out MonoIOError error, out IntPtr handle);
227 [MethodImplAttribute (MethodImplOptions.InternalCall)]
228 public extern static string FindNext (IntPtr handle, out FileAttributes result_attr, out MonoIOError error);
230 [MethodImplAttribute (MethodImplOptions.InternalCall)]
231 public extern static int FindClose (IntPtr handle);
233 public static bool Exists (string path, out MonoIOError error)
235 FileAttributes attrs = GetFileAttributes (path,
237 if (attrs == InvalidFileAttributes)
243 public static bool ExistsFile (string path,
244 out MonoIOError error)
246 FileAttributes attrs = GetFileAttributes (path,
248 if (attrs == InvalidFileAttributes)
251 if ((attrs & FileAttributes.Directory) != 0)
257 public static bool ExistsDirectory (string path,
258 out MonoIOError error)
260 FileAttributes attrs = GetFileAttributes (path,
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;
267 if (attrs == InvalidFileAttributes)
270 if ((attrs & FileAttributes.Directory) == 0)
276 public static bool ExistsSymlink (string path,
277 out MonoIOError error)
279 FileAttributes attrs = GetFileAttributes (path,
281 if (attrs == InvalidFileAttributes)
284 if ((attrs & FileAttributes.ReparsePoint) == 0)
290 [MethodImplAttribute (MethodImplOptions.InternalCall)]
291 public extern static bool GetFileStat (string path,
293 out MonoIOError error);
297 [MethodImplAttribute (MethodImplOptions.InternalCall)]
298 public extern static IntPtr Open (string filename,
303 out MonoIOError error);
305 [MethodImplAttribute (MethodImplOptions.InternalCall)]
306 public extern static bool Close (IntPtr handle,
307 out MonoIOError error);
309 [MethodImplAttribute (MethodImplOptions.InternalCall)]
310 private extern static int Read (IntPtr handle, byte [] dest,
311 int dest_offset, int count,
312 out MonoIOError error);
314 public static int Read (SafeHandle safeHandle, byte [] dest,
315 int dest_offset, int count,
316 out MonoIOError error)
318 bool release = false;
320 safeHandle.DangerousAddRef (ref release);
321 return Read (safeHandle.DangerousGetHandle (), dest, dest_offset, count, out error);
324 safeHandle.DangerousRelease ();
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);
333 public static int Write (SafeHandle safeHandle, byte [] src,
334 int src_offset, int count,
335 out MonoIOError error)
337 bool release = false;
339 safeHandle.DangerousAddRef (ref release);
340 return Write (safeHandle.DangerousGetHandle (), src, src_offset, count, out error);
343 safeHandle.DangerousRelease ();
347 [MethodImplAttribute (MethodImplOptions.InternalCall)]
348 private extern static long Seek (IntPtr handle, long offset,
350 out MonoIOError error);
352 public static long Seek (SafeHandle safeHandle, long offset,
354 out MonoIOError error)
356 bool release = false;
358 safeHandle.DangerousAddRef (ref release);
359 return Seek (safeHandle.DangerousGetHandle (), offset, origin, out error);
362 safeHandle.DangerousRelease ();
366 [MethodImplAttribute (MethodImplOptions.InternalCall)]
367 private extern static bool Flush (IntPtr handle,
368 out MonoIOError error);
370 public static bool Flush (SafeHandle safeHandle,
371 out MonoIOError error)
373 bool release = false;
375 safeHandle.DangerousAddRef (ref release);
376 return Flush (safeHandle.DangerousGetHandle (), out error);
379 safeHandle.DangerousRelease ();
383 [MethodImplAttribute (MethodImplOptions.InternalCall)]
384 private extern static long GetLength (IntPtr handle,
385 out MonoIOError error);
387 public static long GetLength (SafeHandle safeHandle,
388 out MonoIOError error)
390 bool release = false;
392 safeHandle.DangerousAddRef (ref release);
393 return GetLength (safeHandle.DangerousGetHandle (), out error);
396 safeHandle.DangerousRelease ();
400 [MethodImplAttribute (MethodImplOptions.InternalCall)]
401 private extern static bool SetLength (IntPtr handle,
403 out MonoIOError error);
405 public static bool SetLength (SafeHandle safeHandle,
407 out MonoIOError error)
409 bool release = false;
411 safeHandle.DangerousAddRef (ref release);
412 return SetLength (safeHandle.DangerousGetHandle (), length, out error);
415 safeHandle.DangerousRelease ();
419 [MethodImplAttribute (MethodImplOptions.InternalCall)]
420 private extern static bool SetFileTime (IntPtr handle,
422 long last_access_time,
423 long last_write_time,
424 out MonoIOError error);
426 public static bool SetFileTime (SafeHandle safeHandle,
428 long last_access_time,
429 long last_write_time,
430 out MonoIOError error)
432 bool release = false;
434 safeHandle.DangerousAddRef (ref release);
435 return SetFileTime (safeHandle.DangerousGetHandle (), creation_time, last_access_time, last_write_time, out error);
438 safeHandle.DangerousRelease ();
442 public static bool SetFileTime (string path,
444 long last_access_time,
445 long last_write_time,
446 out MonoIOError error)
448 return SetFileTime (path,
457 public static bool SetCreationTime (string path,
459 out MonoIOError error)
461 return SetFileTime (path, 1, -1, -1, -1, dateTime, out error);
464 public static bool SetLastAccessTime (string path,
466 out MonoIOError error)
468 return SetFileTime (path, 2, -1, -1, -1, dateTime, out error);
471 public static bool SetLastWriteTime (string path,
473 out MonoIOError error)
475 return SetFileTime (path, 3, -1, -1, -1, dateTime, out error);
478 public static bool SetFileTime (string path,
481 long last_access_time,
482 long last_write_time,
484 out MonoIOError error)
489 handle = Open (path, FileMode.Open,
490 FileAccess.ReadWrite,
491 FileShare.ReadWrite, FileOptions.None, out error);
492 if (handle == MonoIO.InvalidHandle)
497 creation_time = dateTime.ToFileTime ();
500 last_access_time = dateTime.ToFileTime ();
503 last_write_time = dateTime.ToFileTime ();
507 result = SetFileTime (new SafeFileHandle(handle, false), creation_time,
509 last_write_time, out error);
511 MonoIOError ignore_error;
512 Close (handle, out ignore_error);
517 [MethodImplAttribute (MethodImplOptions.InternalCall)]
518 private extern static void Lock (IntPtr handle,
519 long position, long length,
520 out MonoIOError error);
522 public static void Lock (SafeHandle safeHandle,
523 long position, long length,
524 out MonoIOError error)
526 bool release = false;
528 safeHandle.DangerousAddRef (ref release);
529 Lock (safeHandle.DangerousGetHandle (), position, length, out error);
532 safeHandle.DangerousRelease ();
536 [MethodImplAttribute (MethodImplOptions.InternalCall)]
537 private extern static void Unlock (IntPtr handle,
538 long position, long length,
539 out MonoIOError error);
541 public static void Unlock (SafeHandle safeHandle,
542 long position, long length,
543 out MonoIOError error)
545 bool release = false;
547 safeHandle.DangerousAddRef (ref release);
548 Unlock (safeHandle.DangerousGetHandle (), position, length, out error);
551 safeHandle.DangerousRelease ();
557 public extern static IntPtr ConsoleOutput {
558 [MethodImplAttribute (MethodImplOptions.InternalCall)]
562 public extern static IntPtr ConsoleInput {
563 [MethodImplAttribute (MethodImplOptions.InternalCall)]
567 public extern static IntPtr ConsoleError {
568 [MethodImplAttribute (MethodImplOptions.InternalCall)]
574 [MethodImplAttribute (MethodImplOptions.InternalCall)]
575 public extern static bool CreatePipe (out IntPtr read_handle, out IntPtr write_handle);
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);
583 public extern static char VolumeSeparatorChar {
584 [MethodImplAttribute (MethodImplOptions.InternalCall)]
588 public extern static char DirectorySeparatorChar {
589 [MethodImplAttribute (MethodImplOptions.InternalCall)]
593 public extern static char AltDirectorySeparatorChar {
594 [MethodImplAttribute (MethodImplOptions.InternalCall)]
598 public extern static char PathSeparator {
599 [MethodImplAttribute (MethodImplOptions.InternalCall)]