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 public extern static MonoFileType GetFileType (SafeHandle safeHandle, out MonoIOError error);
212 [MethodImplAttribute (MethodImplOptions.InternalCall)]
213 public extern static string FindFirst (string path, string pattern, out FileAttributes result_attr, out MonoIOError error, out IntPtr handle);
215 [MethodImplAttribute (MethodImplOptions.InternalCall)]
216 public extern static string FindNext (IntPtr handle, out FileAttributes result_attr, out MonoIOError error);
218 [MethodImplAttribute (MethodImplOptions.InternalCall)]
219 public extern static int FindClose (IntPtr handle);
221 public static bool Exists (string path, out MonoIOError error)
223 FileAttributes attrs = GetFileAttributes (path,
225 if (attrs == InvalidFileAttributes)
231 public static bool ExistsFile (string path,
232 out MonoIOError error)
234 FileAttributes attrs = GetFileAttributes (path,
236 if (attrs == InvalidFileAttributes)
239 if ((attrs & FileAttributes.Directory) != 0)
245 public static bool ExistsDirectory (string path,
246 out MonoIOError error)
248 FileAttributes attrs = GetFileAttributes (path,
251 // Actually, we are looking for a directory, not a file
252 if (error == MonoIOError.ERROR_FILE_NOT_FOUND)
253 error = MonoIOError.ERROR_PATH_NOT_FOUND;
255 if (attrs == InvalidFileAttributes)
258 if ((attrs & FileAttributes.Directory) == 0)
264 public static bool ExistsSymlink (string path,
265 out MonoIOError error)
267 FileAttributes attrs = GetFileAttributes (path,
269 if (attrs == InvalidFileAttributes)
272 if ((attrs & FileAttributes.ReparsePoint) == 0)
278 [MethodImplAttribute (MethodImplOptions.InternalCall)]
279 public extern static bool GetFileStat (string path,
281 out MonoIOError error);
285 [MethodImplAttribute (MethodImplOptions.InternalCall)]
286 public extern static IntPtr Open (string filename,
291 out MonoIOError error);
293 [MethodImplAttribute (MethodImplOptions.InternalCall)]
294 public extern static bool Close (IntPtr handle,
295 out MonoIOError error);
297 [MethodImplAttribute (MethodImplOptions.InternalCall)]
298 public extern static int Read (SafeHandle safeHandle, byte [] dest,
299 int dest_offset, int count,
300 out MonoIOError error);
302 [MethodImplAttribute (MethodImplOptions.InternalCall)]
303 public extern static int Write (SafeHandle safeHandle, [In] byte [] src,
304 int src_offset, int count,
305 out MonoIOError error);
307 [MethodImplAttribute (MethodImplOptions.InternalCall)]
308 public extern static long Seek (SafeHandle safeHandle, long offset,
310 out MonoIOError error);
312 [MethodImplAttribute (MethodImplOptions.InternalCall)]
313 public extern static bool Flush (SafeHandle safeHandle,
314 out MonoIOError error);
316 [MethodImplAttribute (MethodImplOptions.InternalCall)]
317 public extern static long GetLength (SafeHandle handle,
318 out MonoIOError error);
320 [MethodImplAttribute (MethodImplOptions.InternalCall)]
321 public extern static bool SetLength (SafeHandle safeHandle,
323 out MonoIOError error);
325 [MethodImplAttribute (MethodImplOptions.InternalCall)]
326 public extern static bool SetFileTime (SafeHandle safeHandle,
328 long last_access_time,
329 long last_write_time,
330 out MonoIOError error);
332 public static bool SetFileTime (string path,
334 long last_access_time,
335 long last_write_time,
336 out MonoIOError error)
338 return SetFileTime (path,
347 public static bool SetCreationTime (string path,
349 out MonoIOError error)
351 return SetFileTime (path, 1, -1, -1, -1, dateTime, out error);
354 public static bool SetLastAccessTime (string path,
356 out MonoIOError error)
358 return SetFileTime (path, 2, -1, -1, -1, dateTime, out error);
361 public static bool SetLastWriteTime (string path,
363 out MonoIOError error)
365 return SetFileTime (path, 3, -1, -1, -1, dateTime, out error);
368 public static bool SetFileTime (string path,
371 long last_access_time,
372 long last_write_time,
374 out MonoIOError error)
379 handle = Open (path, FileMode.Open,
380 FileAccess.ReadWrite,
381 FileShare.ReadWrite, FileOptions.None, out error);
382 if (handle == MonoIO.InvalidHandle)
387 creation_time = dateTime.ToFileTime ();
390 last_access_time = dateTime.ToFileTime ();
393 last_write_time = dateTime.ToFileTime ();
397 result = SetFileTime (new SafeFileHandle(handle, false), creation_time,
399 last_write_time, out error);
401 MonoIOError ignore_error;
402 Close (handle, out ignore_error);
407 [MethodImplAttribute (MethodImplOptions.InternalCall)]
408 public extern static void Lock (SafeHandle safeHandle,
409 long position, long length,
410 out MonoIOError error);
412 [MethodImplAttribute (MethodImplOptions.InternalCall)]
413 public extern static void Unlock (SafeHandle safeHandle,
414 long position, long length,
415 out MonoIOError error);
419 public extern static IntPtr ConsoleOutput {
420 [MethodImplAttribute (MethodImplOptions.InternalCall)]
424 public extern static IntPtr ConsoleInput {
425 [MethodImplAttribute (MethodImplOptions.InternalCall)]
429 public extern static IntPtr ConsoleError {
430 [MethodImplAttribute (MethodImplOptions.InternalCall)]
436 [MethodImplAttribute (MethodImplOptions.InternalCall)]
437 public extern static bool CreatePipe (out IntPtr read_handle, out IntPtr write_handle);
439 [MethodImplAttribute (MethodImplOptions.InternalCall)]
440 public extern static bool DuplicateHandle (IntPtr source_process_handle, IntPtr source_handle,
441 IntPtr target_process_handle, out IntPtr target_handle, int access, int inherit, int options);
445 public extern static char VolumeSeparatorChar {
446 [MethodImplAttribute (MethodImplOptions.InternalCall)]
450 public extern static char DirectorySeparatorChar {
451 [MethodImplAttribute (MethodImplOptions.InternalCall)]
455 public extern static char AltDirectorySeparatorChar {
456 [MethodImplAttribute (MethodImplOptions.InternalCall)]
460 public extern static char PathSeparator {
461 [MethodImplAttribute (MethodImplOptions.InternalCall)]
465 [MethodImplAttribute (MethodImplOptions.InternalCall)]
466 public extern static int GetTempPath(out string path);