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;
40 unsafe internal sealed class MonoIO {
41 public static readonly FileAttributes
42 InvalidFileAttributes = (FileAttributes)(-1);
44 public static readonly IntPtr
45 InvalidHandle = (IntPtr)(-1L);
48 public static Exception GetException (MonoIOError error)
50 return GetException (String.Empty, error);
53 public static Exception GetException (string path,
59 // FIXME: add more exception mappings here
60 case MonoIOError.ERROR_FILE_NOT_FOUND:
61 message = String.Format ("Could not find file \"{0}\"", path);
62 return new FileNotFoundException (message,
65 case MonoIOError.ERROR_TOO_MANY_OPEN_FILES:
66 return new IOException ("Too many open files", unchecked((int)0x80070000) | (int)error);
68 case MonoIOError.ERROR_PATH_NOT_FOUND:
69 message = String.Format ("Could not find a part of the path \"{0}\"", path);
70 return new DirectoryNotFoundException (message);
72 case MonoIOError.ERROR_ACCESS_DENIED:
73 message = String.Format ("Access to the path \"{0}\" is denied.", path);
74 return new UnauthorizedAccessException (message);
76 case MonoIOError.ERROR_INVALID_HANDLE:
77 message = String.Format ("Invalid handle to path \"{0}\"", path);
78 return new IOException (message, unchecked((int)0x80070000) | (int)error);
79 case MonoIOError.ERROR_INVALID_DRIVE:
80 message = String.Format ("Could not find the drive '{0}'. The drive might not be ready or might not be mapped.", path);
82 return new DriveNotFoundException (message);
84 return new IOException (message, unchecked((int)0x80070000) | (int)error);
86 case MonoIOError.ERROR_FILE_EXISTS:
87 message = String.Format ("Could not create file \"{0}\". File already exists.", path);
88 return new IOException (message, unchecked((int)0x80070000) | (int)error);
90 case MonoIOError.ERROR_FILENAME_EXCED_RANGE:
91 message = String.Format ("Path is too long. Path: {0}", path);
92 return new PathTooLongException (message);
94 case MonoIOError.ERROR_INVALID_PARAMETER:
95 message = String.Format ("Invalid parameter");
96 return new IOException (message, unchecked((int)0x80070000) | (int)error);
98 case MonoIOError.ERROR_WRITE_FAULT:
99 message = String.Format ("Write fault on path {0}", path);
100 return new IOException (message, unchecked((int)0x80070000) | (int)error);
102 case MonoIOError.ERROR_SHARING_VIOLATION:
103 message = String.Format ("Sharing violation on path {0}", path);
104 return new IOException (message, unchecked((int)0x80070000) | (int)error);
106 case MonoIOError.ERROR_LOCK_VIOLATION:
107 message = String.Format ("Lock violation on path {0}", path);
108 return new IOException (message, unchecked((int)0x80070000) | (int)error);
110 case MonoIOError.ERROR_HANDLE_DISK_FULL:
111 message = String.Format ("Disk full. Path {0}", path);
112 return new IOException (message, unchecked((int)0x80070000) | (int)error);
114 case MonoIOError.ERROR_DIR_NOT_EMPTY:
115 message = String.Format ("Directory {0} is not empty", path);
116 return new IOException (message, unchecked((int)0x80070000) | (int)error);
118 case MonoIOError.ERROR_ENCRYPTION_FAILED:
119 return new IOException ("Encryption failed", unchecked((int)0x80070000) | (int)error);
122 message = String.Format ("Win32 IO returned {0}. Path: {1}", error, path);
123 return new IOException (message, unchecked((int)0x80070000) | (int)error);
129 [MethodImplAttribute (MethodImplOptions.InternalCall)]
130 public extern static bool CreateDirectory (string path, out MonoIOError error);
132 [MethodImplAttribute (MethodImplOptions.InternalCall)]
133 public extern static bool RemoveDirectory (string path, out MonoIOError error);
135 [MethodImplAttribute (MethodImplOptions.InternalCall)]
136 public extern static string [] GetFileSystemEntries (string path, string pattern, int attrs, int mask, out MonoIOError error);
138 [MethodImplAttribute (MethodImplOptions.InternalCall)]
139 public extern static string GetCurrentDirectory (out MonoIOError error);
141 [MethodImplAttribute (MethodImplOptions.InternalCall)]
142 public extern static bool SetCurrentDirectory (string path, out MonoIOError error);
146 [MethodImplAttribute (MethodImplOptions.InternalCall)]
147 public extern static bool MoveFile (string path, string dest,
148 out MonoIOError error);
150 [MethodImplAttribute (MethodImplOptions.InternalCall)]
151 public extern static bool CopyFile (string path, string dest,
153 out MonoIOError error);
155 [MethodImplAttribute (MethodImplOptions.InternalCall)]
156 public extern static bool DeleteFile (string path,
157 out MonoIOError error);
159 [MethodImplAttribute (MethodImplOptions.InternalCall)]
160 public extern static FileAttributes GetFileAttributes (string path, out MonoIOError error);
162 [MethodImplAttribute (MethodImplOptions.InternalCall)]
163 public extern static bool SetFileAttributes (string path, FileAttributes attrs, out MonoIOError error);
165 [MethodImplAttribute (MethodImplOptions.InternalCall)]
166 public extern static MonoFileType GetFileType (IntPtr handle, out MonoIOError error);
168 public static bool Exists (string path, out MonoIOError error)
170 FileAttributes attrs = GetFileAttributes (path,
172 if (attrs == InvalidFileAttributes)
178 public static bool ExistsFile (string path,
179 out MonoIOError error)
181 FileAttributes attrs = GetFileAttributes (path,
183 if (attrs == InvalidFileAttributes)
186 if ((attrs & FileAttributes.Directory) != 0)
192 public static bool ExistsDirectory (string path,
193 out MonoIOError error)
195 FileAttributes attrs = GetFileAttributes (path,
198 // Actually, we are looking for a directory, not a file
199 if (error == MonoIOError.ERROR_FILE_NOT_FOUND)
200 error = MonoIOError.ERROR_PATH_NOT_FOUND;
202 if (attrs == InvalidFileAttributes)
205 if ((attrs & FileAttributes.Directory) == 0)
211 [MethodImplAttribute (MethodImplOptions.InternalCall)]
212 public extern static bool GetFileStat (string path,
214 out MonoIOError error);
218 [MethodImplAttribute (MethodImplOptions.InternalCall)]
219 public extern static IntPtr Open (string filename,
224 out MonoIOError error);
226 [MethodImplAttribute (MethodImplOptions.InternalCall)]
227 public extern static bool Close (IntPtr handle,
228 out MonoIOError error);
230 [MethodImplAttribute (MethodImplOptions.InternalCall)]
231 public extern static int Read (IntPtr handle, byte [] dest,
232 int dest_offset, int count,
233 out MonoIOError error);
235 [MethodImplAttribute (MethodImplOptions.InternalCall)]
236 public extern static int Write (IntPtr handle, [In] byte [] src,
237 int src_offset, int count,
238 out MonoIOError error);
240 [MethodImplAttribute (MethodImplOptions.InternalCall)]
241 public extern static long Seek (IntPtr handle, long offset,
243 out MonoIOError error);
245 [MethodImplAttribute (MethodImplOptions.InternalCall)]
246 public extern static bool Flush (IntPtr handle,
247 out MonoIOError error);
249 [MethodImplAttribute (MethodImplOptions.InternalCall)]
250 public extern static long GetLength (IntPtr handle,
251 out MonoIOError error);
253 [MethodImplAttribute (MethodImplOptions.InternalCall)]
254 public extern static bool SetLength (IntPtr handle,
256 out MonoIOError error);
258 [MethodImplAttribute (MethodImplOptions.InternalCall)]
259 public extern static bool SetFileTime (IntPtr handle,
261 long last_access_time,
262 long last_write_time,
263 out MonoIOError error);
265 public static bool SetFileTime (string path,
267 long last_access_time,
268 long last_write_time,
269 out MonoIOError error)
271 return SetFileTime (path,
280 public static bool SetCreationTime (string path,
282 out MonoIOError error)
284 return SetFileTime (path, 1, -1, -1, -1, dateTime, out error);
287 public static bool SetLastAccessTime (string path,
289 out MonoIOError error)
291 return SetFileTime (path, 2, -1, -1, -1, dateTime, out error);
294 public static bool SetLastWriteTime (string path,
296 out MonoIOError error)
298 return SetFileTime (path, 3, -1, -1, -1, dateTime, out error);
301 public static bool SetFileTime (string path,
304 long last_access_time,
305 long last_write_time,
307 out MonoIOError error)
312 handle = Open (path, FileMode.Open,
313 FileAccess.ReadWrite,
314 FileShare.ReadWrite, FileOptions.None, out error);
315 if (handle == MonoIO.InvalidHandle)
320 creation_time = dateTime.ToFileTime ();
323 last_access_time = dateTime.ToFileTime ();
326 last_write_time = dateTime.ToFileTime ();
330 result = SetFileTime (handle, creation_time,
332 last_write_time, out error);
334 MonoIOError ignore_error;
335 Close (handle, out ignore_error);
340 [MethodImplAttribute (MethodImplOptions.InternalCall)]
341 public extern static void Lock (IntPtr handle,
342 long position, long length,
343 out MonoIOError error);
345 [MethodImplAttribute (MethodImplOptions.InternalCall)]
346 public extern static void Unlock (IntPtr handle,
347 long position, long length,
348 out MonoIOError error);
352 public extern static IntPtr ConsoleOutput {
353 [MethodImplAttribute (MethodImplOptions.InternalCall)]
357 public extern static IntPtr ConsoleInput {
358 [MethodImplAttribute (MethodImplOptions.InternalCall)]
362 public extern static IntPtr ConsoleError {
363 [MethodImplAttribute (MethodImplOptions.InternalCall)]
369 [MethodImplAttribute (MethodImplOptions.InternalCall)]
370 public extern static bool CreatePipe (out IntPtr read_handle, out IntPtr write_handle);
374 public extern static char VolumeSeparatorChar {
375 [MethodImplAttribute (MethodImplOptions.InternalCall)]
379 public extern static char DirectorySeparatorChar {
380 [MethodImplAttribute (MethodImplOptions.InternalCall)]
384 public extern static char AltDirectorySeparatorChar {
385 [MethodImplAttribute (MethodImplOptions.InternalCall)]
389 public extern static char PathSeparator {
390 [MethodImplAttribute (MethodImplOptions.InternalCall)]
394 [MethodImplAttribute (MethodImplOptions.InternalCall)]
395 public extern static int GetTempPath(out string path);