1 // System.IO.MonoIO.cs: static interface to native filesystem.
4 // Dan Lewis (dihlewis@yahoo.co.uk)
5 // Dick Porter (dick@ximian.com)
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Runtime.CompilerServices;
35 using System.Runtime.InteropServices;
36 using System.Threading;
38 using System.IO.IsolatedStorage;
43 unsafe internal sealed class MonoIO {
44 public static readonly FileAttributes
45 InvalidFileAttributes = (FileAttributes)(-1);
47 public static readonly IntPtr
48 InvalidHandle = (IntPtr)(-1L);
51 public static Exception GetException (MonoIOError error)
53 /* This overload is currently only called from
54 * File.MoveFile(), Directory.Move() and
55 * Directory.GetCurrentDirectory() -
56 * everywhere else supplies a path to format
57 * with the error text.
60 case MonoIOError.ERROR_ACCESS_DENIED:
61 return new UnauthorizedAccessException ("Access to the path is denied.");
62 case MonoIOError.ERROR_FILE_EXISTS:
63 string message = "Cannot create a file that already exist.";
64 return new IOException (message, unchecked ((int) 0x80070000) | (int) error);
66 /* Add more mappings here if other
67 * errors trigger the named but empty
68 * path bug (see bug 82141.) For
69 * everything else, fall through to
72 return GetException (String.Empty, error);
76 public static Exception GetException (string path,
82 // FIXME: add more exception mappings here
83 case MonoIOError.ERROR_FILE_NOT_FOUND:
84 message = String.Format ("Could not find file \"{0}\"", path);
86 return new IsolatedStorageException (message);
88 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);
97 return new IsolatedStorageException (message);
99 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 public extern static MonoFileType GetFileType (IntPtr handle, out MonoIOError error);
213 public static bool Exists (string path, out MonoIOError error)
215 FileAttributes attrs = GetFileAttributes (path,
217 if (attrs == InvalidFileAttributes)
223 public static bool ExistsFile (string path,
224 out MonoIOError error)
226 FileAttributes attrs = GetFileAttributes (path,
228 if (attrs == InvalidFileAttributes)
231 if ((attrs & FileAttributes.Directory) != 0)
237 public static bool ExistsDirectory (string path,
238 out MonoIOError error)
240 FileAttributes attrs = GetFileAttributes (path,
243 // Actually, we are looking for a directory, not a file
244 if (error == MonoIOError.ERROR_FILE_NOT_FOUND)
245 error = MonoIOError.ERROR_PATH_NOT_FOUND;
247 if (attrs == InvalidFileAttributes)
250 if ((attrs & FileAttributes.Directory) == 0)
256 public static bool ExistsSymlink (string path,
257 out MonoIOError error)
259 FileAttributes attrs = GetFileAttributes (path,
261 if (attrs == InvalidFileAttributes)
264 if ((attrs & FileAttributes.ReparsePoint) == 0)
270 [MethodImplAttribute (MethodImplOptions.InternalCall)]
271 public extern static bool GetFileStat (string path,
273 out MonoIOError error);
277 [MethodImplAttribute (MethodImplOptions.InternalCall)]
278 public extern static IntPtr Open (string filename,
283 out MonoIOError error);
285 [MethodImplAttribute (MethodImplOptions.InternalCall)]
286 public extern static bool Close (IntPtr handle,
287 out MonoIOError error);
289 [MethodImplAttribute (MethodImplOptions.InternalCall)]
290 public extern static int Read (IntPtr handle, byte [] dest,
291 int dest_offset, int count,
292 out MonoIOError error);
294 [MethodImplAttribute (MethodImplOptions.InternalCall)]
295 public extern static int Write (IntPtr handle, [In] byte [] src,
296 int src_offset, int count,
297 out MonoIOError error);
299 [MethodImplAttribute (MethodImplOptions.InternalCall)]
300 public extern static long Seek (IntPtr handle, long offset,
302 out MonoIOError error);
304 [MethodImplAttribute (MethodImplOptions.InternalCall)]
305 public extern static bool Flush (IntPtr handle,
306 out MonoIOError error);
308 [MethodImplAttribute (MethodImplOptions.InternalCall)]
309 public extern static long GetLength (IntPtr handle,
310 out MonoIOError error);
312 [MethodImplAttribute (MethodImplOptions.InternalCall)]
313 public extern static bool SetLength (IntPtr handle,
315 out MonoIOError error);
317 [MethodImplAttribute (MethodImplOptions.InternalCall)]
318 public extern static bool SetFileTime (IntPtr handle,
320 long last_access_time,
321 long last_write_time,
322 out MonoIOError error);
324 public static bool SetFileTime (string path,
326 long last_access_time,
327 long last_write_time,
328 out MonoIOError error)
330 return SetFileTime (path,
339 public static bool SetCreationTime (string path,
341 out MonoIOError error)
343 return SetFileTime (path, 1, -1, -1, -1, dateTime, out error);
346 public static bool SetLastAccessTime (string path,
348 out MonoIOError error)
350 return SetFileTime (path, 2, -1, -1, -1, dateTime, out error);
353 public static bool SetLastWriteTime (string path,
355 out MonoIOError error)
357 return SetFileTime (path, 3, -1, -1, -1, dateTime, out error);
360 public static bool SetFileTime (string path,
363 long last_access_time,
364 long last_write_time,
366 out MonoIOError error)
371 handle = Open (path, FileMode.Open,
372 FileAccess.ReadWrite,
373 FileShare.ReadWrite, FileOptions.None, out error);
374 if (handle == MonoIO.InvalidHandle)
379 creation_time = dateTime.ToFileTime ();
382 last_access_time = dateTime.ToFileTime ();
385 last_write_time = dateTime.ToFileTime ();
389 result = SetFileTime (handle, creation_time,
391 last_write_time, out error);
393 MonoIOError ignore_error;
394 Close (handle, out ignore_error);
399 [MethodImplAttribute (MethodImplOptions.InternalCall)]
400 public extern static void Lock (IntPtr handle,
401 long position, long length,
402 out MonoIOError error);
404 [MethodImplAttribute (MethodImplOptions.InternalCall)]
405 public extern static void Unlock (IntPtr handle,
406 long position, long length,
407 out MonoIOError error);
411 public extern static IntPtr ConsoleOutput {
412 [MethodImplAttribute (MethodImplOptions.InternalCall)]
416 public extern static IntPtr ConsoleInput {
417 [MethodImplAttribute (MethodImplOptions.InternalCall)]
421 public extern static IntPtr ConsoleError {
422 [MethodImplAttribute (MethodImplOptions.InternalCall)]
428 [MethodImplAttribute (MethodImplOptions.InternalCall)]
429 public extern static bool CreatePipe (out IntPtr read_handle, out IntPtr write_handle);
431 [MethodImplAttribute (MethodImplOptions.InternalCall)]
432 public extern static bool DuplicateHandle (IntPtr source_process_handle, IntPtr source_handle,
433 IntPtr target_process_handle, out IntPtr target_handle, int access, int inherit, int options);
437 public extern static char VolumeSeparatorChar {
438 [MethodImplAttribute (MethodImplOptions.InternalCall)]
442 public extern static char DirectorySeparatorChar {
443 [MethodImplAttribute (MethodImplOptions.InternalCall)]
447 public extern static char AltDirectorySeparatorChar {
448 [MethodImplAttribute (MethodImplOptions.InternalCall)]
452 public extern static char PathSeparator {
453 [MethodImplAttribute (MethodImplOptions.InternalCall)]
457 [MethodImplAttribute (MethodImplOptions.InternalCall)]
458 public extern static int GetTempPath(out string path);