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;
39 using System.IO.IsolatedStorage;
44 unsafe static class MonoIO {
45 public const int FileAlreadyExistsHResult = unchecked ((int) 0x80070000) | (int)MonoIOError.ERROR_FILE_EXISTS;
47 public const FileAttributes
48 InvalidFileAttributes = (FileAttributes)(-1);
50 public static readonly IntPtr
51 InvalidHandle = (IntPtr)(-1L);
54 public static Exception GetException (MonoIOError error)
56 /* This overload is currently only called from
57 * File.MoveFile(), Directory.Move() and
58 * Directory.GetCurrentDirectory() -
59 * everywhere else supplies a path to format
60 * with the error text.
63 case MonoIOError.ERROR_ACCESS_DENIED:
64 return new UnauthorizedAccessException ("Access to the path is denied.");
65 case MonoIOError.ERROR_FILE_EXISTS:
66 string message = "Cannot create a file that already exist.";
67 return new IOException (message, FileAlreadyExistsHResult);
69 /* Add more mappings here if other
70 * errors trigger the named but empty
71 * path bug (see bug 82141.) For
72 * everything else, fall through to
75 return GetException (String.Empty, error);
79 public static Exception GetException (string path,
85 // FIXME: add more exception mappings here
86 case MonoIOError.ERROR_FILE_NOT_FOUND:
87 message = String.Format ("Could not find file \"{0}\"", path);
88 return new FileNotFoundException (message, path);
90 case MonoIOError.ERROR_TOO_MANY_OPEN_FILES:
91 return new IOException ("Too many open files", unchecked((int)0x80070000) | (int)error);
93 case MonoIOError.ERROR_PATH_NOT_FOUND:
94 message = String.Format ("Could not find a part of the path \"{0}\"", path);
95 return new DirectoryNotFoundException (message);
97 case MonoIOError.ERROR_ACCESS_DENIED:
98 message = String.Format ("Access to the path \"{0}\" is denied.", path);
99 return new UnauthorizedAccessException (message);
101 case MonoIOError.ERROR_INVALID_HANDLE:
102 message = String.Format ("Invalid handle to path \"{0}\"", path);
103 return new IOException (message, unchecked((int)0x80070000) | (int)error);
104 case MonoIOError.ERROR_INVALID_DRIVE:
105 message = String.Format ("Could not find the drive '{0}'. The drive might not be ready or might not be mapped.", path);
107 return new DriveNotFoundException (message);
109 return new IOException (message, unchecked((int)0x80070000) | (int)error);
111 case MonoIOError.ERROR_FILE_EXISTS:
112 message = String.Format ("Could not create file \"{0}\". File already exists.", path);
113 return new IOException (message, unchecked((int)0x80070000) | (int)error);
115 case MonoIOError.ERROR_FILENAME_EXCED_RANGE:
116 message = String.Format ("Path is too long. Path: {0}", path);
117 return new PathTooLongException (message);
119 case MonoIOError.ERROR_INVALID_PARAMETER:
120 message = String.Format ("Invalid parameter");
121 return new IOException (message, unchecked((int)0x80070000) | (int)error);
123 case MonoIOError.ERROR_WRITE_FAULT:
124 message = String.Format ("Write fault on path {0}", path);
125 return new IOException (message, unchecked((int)0x80070000) | (int)error);
127 case MonoIOError.ERROR_SHARING_VIOLATION:
128 message = String.Format ("Sharing violation on path {0}", path);
129 return new IOException (message, unchecked((int)0x80070000) | (int)error);
131 case MonoIOError.ERROR_LOCK_VIOLATION:
132 message = String.Format ("Lock violation on path {0}", path);
133 return new IOException (message, unchecked((int)0x80070000) | (int)error);
135 case MonoIOError.ERROR_HANDLE_DISK_FULL:
136 message = String.Format ("Disk full. Path {0}", path);
137 return new IOException (message, unchecked((int)0x80070000) | (int)error);
139 case MonoIOError.ERROR_DIR_NOT_EMPTY:
140 message = String.Format ("Directory {0} is not empty", path);
141 return new IOException (message, unchecked((int)0x80070000) | (int)error);
143 case MonoIOError.ERROR_ENCRYPTION_FAILED:
144 return new IOException ("Encryption failed", unchecked((int)0x80070000) | (int)error);
146 case MonoIOError.ERROR_CANNOT_MAKE:
147 message = String.Format ("Path {0} is a directory", path);
148 return new IOException (message, unchecked((int)0x80070000) | (int)error);
150 case MonoIOError.ERROR_NOT_SAME_DEVICE:
151 message = "Source and destination are not on the same device";
152 return new IOException (message, unchecked((int)0x80070000) | (int)error);
155 message = String.Format ("Win32 IO returned {0}. Path: {1}", error, path);
156 return new IOException (message, unchecked((int)0x80070000) | (int)error);
162 [MethodImplAttribute (MethodImplOptions.InternalCall)]
163 public extern static bool CreateDirectory (string path, out MonoIOError error);
165 [MethodImplAttribute (MethodImplOptions.InternalCall)]
166 public extern static bool RemoveDirectory (string path, out MonoIOError error);
168 [MethodImplAttribute (MethodImplOptions.InternalCall)]
169 public extern static string [] GetFileSystemEntries (string path, string path_with_pattern, int attrs, int mask, out MonoIOError error);
171 [MethodImplAttribute (MethodImplOptions.InternalCall)]
172 public extern static string GetCurrentDirectory (out MonoIOError error);
174 [MethodImplAttribute (MethodImplOptions.InternalCall)]
175 public extern static bool SetCurrentDirectory (string path, out MonoIOError error);
179 [MethodImplAttribute (MethodImplOptions.InternalCall)]
180 public extern static bool MoveFile (string path, string dest,
181 out MonoIOError error);
183 [MethodImplAttribute (MethodImplOptions.InternalCall)]
184 public extern static bool CopyFile (string path, string dest,
186 out MonoIOError error);
188 [MethodImplAttribute (MethodImplOptions.InternalCall)]
189 public extern static bool DeleteFile (string path,
190 out MonoIOError error);
192 [MethodImplAttribute (MethodImplOptions.InternalCall)]
193 public extern static bool ReplaceFile (string sourceFileName,
194 string destinationFileName,
195 string destinationBackupFileName,
196 bool ignoreMetadataErrors,
197 out MonoIOError error);
199 [MethodImplAttribute (MethodImplOptions.InternalCall)]
200 public extern static FileAttributes GetFileAttributes (string path, out MonoIOError error);
202 [MethodImplAttribute (MethodImplOptions.InternalCall)]
203 public extern static bool SetFileAttributes (string path, FileAttributes attrs, out MonoIOError error);
205 [MethodImplAttribute (MethodImplOptions.InternalCall)]
206 public extern static MonoFileType GetFileType (IntPtr handle, out MonoIOError error);
211 [MethodImplAttribute (MethodImplOptions.InternalCall)]
212 public extern static string FindFirst (string path, string pattern, out FileAttributes result_attr, out MonoIOError error, out IntPtr handle);
214 [MethodImplAttribute (MethodImplOptions.InternalCall)]
215 public extern static string FindNext (IntPtr handle, out FileAttributes result_attr, out MonoIOError error);
217 [MethodImplAttribute (MethodImplOptions.InternalCall)]
218 public extern static int FindClose (IntPtr handle);
220 public static bool Exists (string path, out MonoIOError error)
222 FileAttributes attrs = GetFileAttributes (path,
224 if (attrs == InvalidFileAttributes)
230 public static bool ExistsFile (string path,
231 out MonoIOError error)
233 FileAttributes attrs = GetFileAttributes (path,
235 if (attrs == InvalidFileAttributes)
238 if ((attrs & FileAttributes.Directory) != 0)
244 public static bool ExistsDirectory (string path,
245 out MonoIOError error)
247 FileAttributes attrs = GetFileAttributes (path,
250 // Actually, we are looking for a directory, not a file
251 if (error == MonoIOError.ERROR_FILE_NOT_FOUND)
252 error = MonoIOError.ERROR_PATH_NOT_FOUND;
254 if (attrs == InvalidFileAttributes)
257 if ((attrs & FileAttributes.Directory) == 0)
263 public static bool ExistsSymlink (string path,
264 out MonoIOError error)
266 FileAttributes attrs = GetFileAttributes (path,
268 if (attrs == InvalidFileAttributes)
271 if ((attrs & FileAttributes.ReparsePoint) == 0)
277 [MethodImplAttribute (MethodImplOptions.InternalCall)]
278 public extern static bool GetFileStat (string path,
280 out MonoIOError error);
284 [MethodImplAttribute (MethodImplOptions.InternalCall)]
285 public extern static IntPtr Open (string filename,
290 out MonoIOError error);
292 [MethodImplAttribute (MethodImplOptions.InternalCall)]
293 public extern static bool Close (IntPtr handle,
294 out MonoIOError error);
296 [MethodImplAttribute (MethodImplOptions.InternalCall)]
297 public extern static int Read (IntPtr handle, byte [] dest,
298 int dest_offset, int count,
299 out MonoIOError error);
301 [MethodImplAttribute (MethodImplOptions.InternalCall)]
302 public extern static int Write (IntPtr handle, [In] byte [] src,
303 int src_offset, int count,
304 out MonoIOError error);
306 [MethodImplAttribute (MethodImplOptions.InternalCall)]
307 public extern static long Seek (IntPtr handle, long offset,
309 out MonoIOError error);
311 [MethodImplAttribute (MethodImplOptions.InternalCall)]
312 public extern static bool Flush (IntPtr handle,
313 out MonoIOError error);
315 [MethodImplAttribute (MethodImplOptions.InternalCall)]
316 public extern static long GetLength (IntPtr handle,
317 out MonoIOError error);
319 [MethodImplAttribute (MethodImplOptions.InternalCall)]
320 public extern static bool SetLength (IntPtr handle,
322 out MonoIOError error);
324 [MethodImplAttribute (MethodImplOptions.InternalCall)]
325 public extern static bool SetFileTime (IntPtr handle,
327 long last_access_time,
328 long last_write_time,
329 out MonoIOError error);
331 public static bool SetFileTime (string path,
333 long last_access_time,
334 long last_write_time,
335 out MonoIOError error)
337 return SetFileTime (path,
346 public static bool SetCreationTime (string path,
348 out MonoIOError error)
350 return SetFileTime (path, 1, -1, -1, -1, dateTime, out error);
353 public static bool SetLastAccessTime (string path,
355 out MonoIOError error)
357 return SetFileTime (path, 2, -1, -1, -1, dateTime, out error);
360 public static bool SetLastWriteTime (string path,
362 out MonoIOError error)
364 return SetFileTime (path, 3, -1, -1, -1, dateTime, out error);
367 public static bool SetFileTime (string path,
370 long last_access_time,
371 long last_write_time,
373 out MonoIOError error)
378 handle = Open (path, FileMode.Open,
379 FileAccess.ReadWrite,
380 FileShare.ReadWrite, FileOptions.None, out error);
381 if (handle == MonoIO.InvalidHandle)
386 creation_time = dateTime.ToFileTime ();
389 last_access_time = dateTime.ToFileTime ();
392 last_write_time = dateTime.ToFileTime ();
396 result = SetFileTime (handle, creation_time,
398 last_write_time, out error);
400 MonoIOError ignore_error;
401 Close (handle, out ignore_error);
406 [MethodImplAttribute (MethodImplOptions.InternalCall)]
407 public extern static void Lock (IntPtr handle,
408 long position, long length,
409 out MonoIOError error);
411 [MethodImplAttribute (MethodImplOptions.InternalCall)]
412 public extern static void Unlock (IntPtr handle,
413 long position, long length,
414 out MonoIOError error);
418 public extern static IntPtr ConsoleOutput {
419 [MethodImplAttribute (MethodImplOptions.InternalCall)]
423 public extern static IntPtr ConsoleInput {
424 [MethodImplAttribute (MethodImplOptions.InternalCall)]
428 public extern static IntPtr ConsoleError {
429 [MethodImplAttribute (MethodImplOptions.InternalCall)]
435 [MethodImplAttribute (MethodImplOptions.InternalCall)]
436 public extern static bool CreatePipe (out IntPtr read_handle, out IntPtr write_handle);
438 [MethodImplAttribute (MethodImplOptions.InternalCall)]
439 public extern static bool DuplicateHandle (IntPtr source_process_handle, IntPtr source_handle,
440 IntPtr target_process_handle, out IntPtr target_handle, int access, int inherit, int options);
444 public extern static char VolumeSeparatorChar {
445 [MethodImplAttribute (MethodImplOptions.InternalCall)]
449 public extern static char DirectorySeparatorChar {
450 [MethodImplAttribute (MethodImplOptions.InternalCall)]
454 public extern static char AltDirectorySeparatorChar {
455 [MethodImplAttribute (MethodImplOptions.InternalCall)]
459 public extern static char PathSeparator {
460 [MethodImplAttribute (MethodImplOptions.InternalCall)]
464 [MethodImplAttribute (MethodImplOptions.InternalCall)]
465 public extern static int GetTempPath(out string path);