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);
89 return new IsolatedStorageException (message);
91 return new FileNotFoundException (message, path);
94 case MonoIOError.ERROR_TOO_MANY_OPEN_FILES:
95 return new IOException ("Too many open files", unchecked((int)0x80070000) | (int)error);
97 case MonoIOError.ERROR_PATH_NOT_FOUND:
98 message = String.Format ("Could not find a part of the path \"{0}\"", path);
100 return new IsolatedStorageException (message);
102 return new DirectoryNotFoundException (message);
105 case MonoIOError.ERROR_ACCESS_DENIED:
106 message = String.Format ("Access to the path \"{0}\" is denied.", path);
107 return new UnauthorizedAccessException (message);
109 case MonoIOError.ERROR_INVALID_HANDLE:
110 message = String.Format ("Invalid handle to path \"{0}\"", path);
111 return new IOException (message, unchecked((int)0x80070000) | (int)error);
112 case MonoIOError.ERROR_INVALID_DRIVE:
113 message = String.Format ("Could not find the drive '{0}'. The drive might not be ready or might not be mapped.", path);
115 return new DriveNotFoundException (message);
117 return new IOException (message, unchecked((int)0x80070000) | (int)error);
119 case MonoIOError.ERROR_FILE_EXISTS:
120 message = String.Format ("Could not create file \"{0}\". File already exists.", path);
121 return new IOException (message, unchecked((int)0x80070000) | (int)error);
123 case MonoIOError.ERROR_FILENAME_EXCED_RANGE:
124 message = String.Format ("Path is too long. Path: {0}", path);
125 return new PathTooLongException (message);
127 case MonoIOError.ERROR_INVALID_PARAMETER:
128 message = String.Format ("Invalid parameter");
129 return new IOException (message, unchecked((int)0x80070000) | (int)error);
131 case MonoIOError.ERROR_WRITE_FAULT:
132 message = String.Format ("Write fault on path {0}", path);
133 return new IOException (message, unchecked((int)0x80070000) | (int)error);
135 case MonoIOError.ERROR_SHARING_VIOLATION:
136 message = String.Format ("Sharing violation on path {0}", path);
137 return new IOException (message, unchecked((int)0x80070000) | (int)error);
139 case MonoIOError.ERROR_LOCK_VIOLATION:
140 message = String.Format ("Lock violation on path {0}", path);
141 return new IOException (message, unchecked((int)0x80070000) | (int)error);
143 case MonoIOError.ERROR_HANDLE_DISK_FULL:
144 message = String.Format ("Disk full. Path {0}", path);
145 return new IOException (message, unchecked((int)0x80070000) | (int)error);
147 case MonoIOError.ERROR_DIR_NOT_EMPTY:
148 message = String.Format ("Directory {0} is not empty", path);
149 return new IOException (message, unchecked((int)0x80070000) | (int)error);
151 case MonoIOError.ERROR_ENCRYPTION_FAILED:
152 return new IOException ("Encryption failed", unchecked((int)0x80070000) | (int)error);
154 case MonoIOError.ERROR_CANNOT_MAKE:
155 message = String.Format ("Path {0} is a directory", path);
156 return new IOException (message, unchecked((int)0x80070000) | (int)error);
158 case MonoIOError.ERROR_NOT_SAME_DEVICE:
159 message = "Source and destination are not on the same device";
160 return new IOException (message, unchecked((int)0x80070000) | (int)error);
163 message = String.Format ("Win32 IO returned {0}. Path: {1}", error, path);
164 return new IOException (message, unchecked((int)0x80070000) | (int)error);
170 [MethodImplAttribute (MethodImplOptions.InternalCall)]
171 public extern static bool CreateDirectory (string path, out MonoIOError error);
173 [MethodImplAttribute (MethodImplOptions.InternalCall)]
174 public extern static bool RemoveDirectory (string path, out MonoIOError error);
176 [MethodImplAttribute (MethodImplOptions.InternalCall)]
177 public extern static string [] GetFileSystemEntries (string path, string path_with_pattern, int attrs, int mask, out MonoIOError error);
179 [MethodImplAttribute (MethodImplOptions.InternalCall)]
180 public extern static string GetCurrentDirectory (out MonoIOError error);
182 [MethodImplAttribute (MethodImplOptions.InternalCall)]
183 public extern static bool SetCurrentDirectory (string path, out MonoIOError error);
187 [MethodImplAttribute (MethodImplOptions.InternalCall)]
188 public extern static bool MoveFile (string path, string dest,
189 out MonoIOError error);
191 [MethodImplAttribute (MethodImplOptions.InternalCall)]
192 public extern static bool CopyFile (string path, string dest,
194 out MonoIOError error);
196 [MethodImplAttribute (MethodImplOptions.InternalCall)]
197 public extern static bool DeleteFile (string path,
198 out MonoIOError error);
200 [MethodImplAttribute (MethodImplOptions.InternalCall)]
201 public extern static bool ReplaceFile (string sourceFileName,
202 string destinationFileName,
203 string destinationBackupFileName,
204 bool ignoreMetadataErrors,
205 out MonoIOError error);
207 [MethodImplAttribute (MethodImplOptions.InternalCall)]
208 public extern static FileAttributes GetFileAttributes (string path, out MonoIOError error);
210 [MethodImplAttribute (MethodImplOptions.InternalCall)]
211 public extern static bool SetFileAttributes (string path, FileAttributes attrs, out MonoIOError error);
213 [MethodImplAttribute (MethodImplOptions.InternalCall)]
214 public extern static MonoFileType GetFileType (IntPtr handle, out MonoIOError error);
219 [MethodImplAttribute (MethodImplOptions.InternalCall)]
220 public extern static string FindFirst (string path, string pattern, out FileAttributes result_attr, out MonoIOError error, out IntPtr handle);
222 [MethodImplAttribute (MethodImplOptions.InternalCall)]
223 public extern static string FindNext (IntPtr handle, out FileAttributes result_attr, out MonoIOError error);
225 [MethodImplAttribute (MethodImplOptions.InternalCall)]
226 public extern static int FindClose (IntPtr handle);
228 public static bool Exists (string path, out MonoIOError error)
230 FileAttributes attrs = GetFileAttributes (path,
232 if (attrs == InvalidFileAttributes)
238 public static bool ExistsFile (string path,
239 out MonoIOError error)
241 FileAttributes attrs = GetFileAttributes (path,
243 if (attrs == InvalidFileAttributes)
246 if ((attrs & FileAttributes.Directory) != 0)
252 public static bool ExistsDirectory (string path,
253 out MonoIOError error)
255 FileAttributes attrs = GetFileAttributes (path,
258 // Actually, we are looking for a directory, not a file
259 if (error == MonoIOError.ERROR_FILE_NOT_FOUND)
260 error = MonoIOError.ERROR_PATH_NOT_FOUND;
262 if (attrs == InvalidFileAttributes)
265 if ((attrs & FileAttributes.Directory) == 0)
271 public static bool ExistsSymlink (string path,
272 out MonoIOError error)
274 FileAttributes attrs = GetFileAttributes (path,
276 if (attrs == InvalidFileAttributes)
279 if ((attrs & FileAttributes.ReparsePoint) == 0)
285 [MethodImplAttribute (MethodImplOptions.InternalCall)]
286 public extern static bool GetFileStat (string path,
288 out MonoIOError error);
292 [MethodImplAttribute (MethodImplOptions.InternalCall)]
293 public extern static IntPtr Open (string filename,
298 out MonoIOError error);
300 [MethodImplAttribute (MethodImplOptions.InternalCall)]
301 public extern static bool Close (IntPtr handle,
302 out MonoIOError error);
304 [MethodImplAttribute (MethodImplOptions.InternalCall)]
305 public extern static int Read (IntPtr handle, byte [] dest,
306 int dest_offset, int count,
307 out MonoIOError error);
309 [MethodImplAttribute (MethodImplOptions.InternalCall)]
310 public extern static int Write (IntPtr handle, [In] byte [] src,
311 int src_offset, int count,
312 out MonoIOError error);
314 [MethodImplAttribute (MethodImplOptions.InternalCall)]
315 public extern static long Seek (IntPtr handle, long offset,
317 out MonoIOError error);
319 [MethodImplAttribute (MethodImplOptions.InternalCall)]
320 public extern static bool Flush (IntPtr handle,
321 out MonoIOError error);
323 [MethodImplAttribute (MethodImplOptions.InternalCall)]
324 public extern static long GetLength (IntPtr handle,
325 out MonoIOError error);
327 [MethodImplAttribute (MethodImplOptions.InternalCall)]
328 public extern static bool SetLength (IntPtr handle,
330 out MonoIOError error);
332 [MethodImplAttribute (MethodImplOptions.InternalCall)]
333 public extern static bool SetFileTime (IntPtr handle,
335 long last_access_time,
336 long last_write_time,
337 out MonoIOError error);
339 public static bool SetFileTime (string path,
341 long last_access_time,
342 long last_write_time,
343 out MonoIOError error)
345 return SetFileTime (path,
354 public static bool SetCreationTime (string path,
356 out MonoIOError error)
358 return SetFileTime (path, 1, -1, -1, -1, dateTime, out error);
361 public static bool SetLastAccessTime (string path,
363 out MonoIOError error)
365 return SetFileTime (path, 2, -1, -1, -1, dateTime, out error);
368 public static bool SetLastWriteTime (string path,
370 out MonoIOError error)
372 return SetFileTime (path, 3, -1, -1, -1, dateTime, out error);
375 public static bool SetFileTime (string path,
378 long last_access_time,
379 long last_write_time,
381 out MonoIOError error)
386 handle = Open (path, FileMode.Open,
387 FileAccess.ReadWrite,
388 FileShare.ReadWrite, FileOptions.None, out error);
389 if (handle == MonoIO.InvalidHandle)
394 creation_time = dateTime.ToFileTime ();
397 last_access_time = dateTime.ToFileTime ();
400 last_write_time = dateTime.ToFileTime ();
404 result = SetFileTime (handle, creation_time,
406 last_write_time, out error);
408 MonoIOError ignore_error;
409 Close (handle, out ignore_error);
414 [MethodImplAttribute (MethodImplOptions.InternalCall)]
415 public extern static void Lock (IntPtr handle,
416 long position, long length,
417 out MonoIOError error);
419 [MethodImplAttribute (MethodImplOptions.InternalCall)]
420 public extern static void Unlock (IntPtr handle,
421 long position, long length,
422 out MonoIOError error);
426 public extern static IntPtr ConsoleOutput {
427 [MethodImplAttribute (MethodImplOptions.InternalCall)]
431 public extern static IntPtr ConsoleInput {
432 [MethodImplAttribute (MethodImplOptions.InternalCall)]
436 public extern static IntPtr ConsoleError {
437 [MethodImplAttribute (MethodImplOptions.InternalCall)]
443 [MethodImplAttribute (MethodImplOptions.InternalCall)]
444 public extern static bool CreatePipe (out IntPtr read_handle, out IntPtr write_handle);
446 [MethodImplAttribute (MethodImplOptions.InternalCall)]
447 public extern static bool DuplicateHandle (IntPtr source_process_handle, IntPtr source_handle,
448 IntPtr target_process_handle, out IntPtr target_handle, int access, int inherit, int options);
452 public extern static char VolumeSeparatorChar {
453 [MethodImplAttribute (MethodImplOptions.InternalCall)]
457 public extern static char DirectorySeparatorChar {
458 [MethodImplAttribute (MethodImplOptions.InternalCall)]
462 public extern static char AltDirectorySeparatorChar {
463 [MethodImplAttribute (MethodImplOptions.InternalCall)]
467 public extern static char PathSeparator {
468 [MethodImplAttribute (MethodImplOptions.InternalCall)]
472 [MethodImplAttribute (MethodImplOptions.InternalCall)]
473 public extern static int GetTempPath(out string path);