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");
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);
80 case MonoIOError.ERROR_FILE_EXISTS:
81 message = String.Format ("Could not create file \"{0}\". File already exists.", path);
82 return new IOException (message);
84 case MonoIOError.ERROR_FILENAME_EXCED_RANGE:
85 message = String.Format ("Path is too long. Path: {0}", path);
86 return new PathTooLongException (message);
88 case MonoIOError.ERROR_INVALID_PARAMETER:
89 message = String.Format ("Invalid parameter");
90 return new IOException (message);
92 case MonoIOError.ERROR_WRITE_FAULT:
93 message = String.Format ("Write fault on path {0}", path);
94 return new IOException (message);
96 case MonoIOError.ERROR_SHARING_VIOLATION:
97 message = String.Format ("Sharing violation on path {0}", path);
98 return new IOException (message);
100 case MonoIOError.ERROR_LOCK_VIOLATION:
101 message = String.Format ("Lock violation on path {0}", path);
102 return new IOException (message);
104 case MonoIOError.ERROR_DIR_NOT_EMPTY:
105 message = String.Format ("Directory {0} is not empty", path);
106 return new IOException (message);
109 message = String.Format ("Win32 IO returned {0}. Path: {1}", error, path);
110 return new IOException (message);
116 [MethodImplAttribute (MethodImplOptions.InternalCall)]
117 public extern static bool CreateDirectory (string path, out MonoIOError error);
119 [MethodImplAttribute (MethodImplOptions.InternalCall)]
120 public extern static bool RemoveDirectory (string path, out MonoIOError error);
122 [MethodImplAttribute (MethodImplOptions.InternalCall)]
123 public extern static IntPtr FindFirstFile (string path, out MonoIOStat stat, out MonoIOError error);
125 [MethodImplAttribute (MethodImplOptions.InternalCall)]
126 public extern static bool FindNextFile (IntPtr find, out MonoIOStat stat, out MonoIOError error);
128 [MethodImplAttribute (MethodImplOptions.InternalCall)]
129 public extern static bool FindClose (IntPtr find,
130 out MonoIOError error);
132 [MethodImplAttribute (MethodImplOptions.InternalCall)]
133 public extern static string GetCurrentDirectory (out MonoIOError error);
135 [MethodImplAttribute (MethodImplOptions.InternalCall)]
136 public extern static bool SetCurrentDirectory (string path, out MonoIOError error);
140 [MethodImplAttribute (MethodImplOptions.InternalCall)]
141 public extern static bool MoveFile (string path, string dest,
142 out MonoIOError error);
144 [MethodImplAttribute (MethodImplOptions.InternalCall)]
145 public extern static bool CopyFile (string path, string dest,
147 out MonoIOError error);
149 [MethodImplAttribute (MethodImplOptions.InternalCall)]
150 public extern static bool DeleteFile (string path,
151 out MonoIOError error);
153 [MethodImplAttribute (MethodImplOptions.InternalCall)]
154 public extern static FileAttributes GetFileAttributes (string path, out MonoIOError error);
156 [MethodImplAttribute (MethodImplOptions.InternalCall)]
157 public extern static bool SetFileAttributes (string path, FileAttributes attrs, out MonoIOError error);
159 [MethodImplAttribute (MethodImplOptions.InternalCall)]
160 public extern static MonoFileType GetFileType (IntPtr handle, out MonoIOError error);
162 public static bool Exists (string path, out MonoIOError error)
164 FileAttributes attrs = GetFileAttributes (path,
166 if (attrs == InvalidFileAttributes)
172 public static bool ExistsFile (string path,
173 out MonoIOError error)
175 FileAttributes attrs = GetFileAttributes (path,
177 if (attrs == InvalidFileAttributes)
180 if ((attrs & FileAttributes.Directory) != 0)
186 public static bool ExistsDirectory (string path,
187 out MonoIOError error)
189 FileAttributes attrs = GetFileAttributes (path,
192 // Actually, we are looking for a directory, not a file
193 if (error == MonoIOError.ERROR_FILE_NOT_FOUND)
194 error = MonoIOError.ERROR_PATH_NOT_FOUND;
196 if (attrs == InvalidFileAttributes)
199 if ((attrs & FileAttributes.Directory) == 0)
205 [MethodImplAttribute (MethodImplOptions.InternalCall)]
206 public extern static bool GetFileStat (string path,
208 out MonoIOError error);
212 [MethodImplAttribute (MethodImplOptions.InternalCall)]
213 public extern static IntPtr Open (string filename,
218 out MonoIOError error);
220 [MethodImplAttribute (MethodImplOptions.InternalCall)]
221 public extern static bool Close (IntPtr handle,
222 out MonoIOError error);
224 [MethodImplAttribute (MethodImplOptions.InternalCall)]
225 public extern static int Read (IntPtr handle, byte [] dest,
226 int dest_offset, int count,
227 out MonoIOError error);
229 [MethodImplAttribute (MethodImplOptions.InternalCall)]
230 public extern static int Write (IntPtr handle, [In] byte [] src,
231 int src_offset, int count,
232 out MonoIOError error);
234 [MethodImplAttribute (MethodImplOptions.InternalCall)]
235 public extern static long Seek (IntPtr handle, long offset,
237 out MonoIOError error);
239 [MethodImplAttribute (MethodImplOptions.InternalCall)]
240 public extern static bool Flush (IntPtr handle,
241 out MonoIOError error);
243 [MethodImplAttribute (MethodImplOptions.InternalCall)]
244 public extern static long GetLength (IntPtr handle,
245 out MonoIOError error);
247 [MethodImplAttribute (MethodImplOptions.InternalCall)]
248 public extern static bool SetLength (IntPtr handle,
250 out MonoIOError error);
252 [MethodImplAttribute (MethodImplOptions.InternalCall)]
253 public extern static bool SetFileTime (IntPtr handle,
255 long last_access_time,
256 long last_write_time,
257 out MonoIOError error);
259 public static bool SetFileTime (string path,
261 long last_access_time,
262 long last_write_time,
263 out MonoIOError error)
265 return SetFileTime (path,
274 public static bool SetCreationTime (string path,
276 out MonoIOError error)
278 return SetFileTime (path, 1, -1, -1, -1, dateTime, out error);
281 public static bool SetLastAccessTime (string path,
283 out MonoIOError error)
285 return SetFileTime (path, 2, -1, -1, -1, dateTime, out error);
288 public static bool SetLastWriteTime (string path,
290 out MonoIOError error)
292 return SetFileTime (path, 3, -1, -1, -1, dateTime, out error);
295 public static bool SetFileTime (string path,
298 long last_access_time,
299 long last_write_time,
301 out MonoIOError error)
306 handle = Open (path, FileMode.Open,
307 FileAccess.ReadWrite,
308 FileShare.ReadWrite, false, out error);
309 if (handle == MonoIO.InvalidHandle)
314 creation_time = dateTime.ToFileTime ();
317 last_access_time = dateTime.ToFileTime ();
320 last_write_time = dateTime.ToFileTime ();
324 result = SetFileTime (handle, creation_time,
326 last_write_time, out error);
328 MonoIOError ignore_error;
329 Close (handle, out ignore_error);
334 [MethodImplAttribute (MethodImplOptions.InternalCall)]
335 public extern static void Lock (IntPtr handle,
336 long position, long length,
337 out MonoIOError error);
339 [MethodImplAttribute (MethodImplOptions.InternalCall)]
340 public extern static void Unlock (IntPtr handle,
341 long position, long length,
342 out MonoIOError error);
346 public extern static IntPtr ConsoleOutput {
347 [MethodImplAttribute (MethodImplOptions.InternalCall)]
351 public extern static IntPtr ConsoleInput {
352 [MethodImplAttribute (MethodImplOptions.InternalCall)]
356 public extern static IntPtr ConsoleError {
357 [MethodImplAttribute (MethodImplOptions.InternalCall)]
363 [MethodImplAttribute (MethodImplOptions.InternalCall)]
364 public extern static bool CreatePipe (out IntPtr read_handle, out IntPtr write_handle);
368 public extern static char VolumeSeparatorChar {
369 [MethodImplAttribute (MethodImplOptions.InternalCall)]
373 public extern static char DirectorySeparatorChar {
374 [MethodImplAttribute (MethodImplOptions.InternalCall)]
378 public extern static char AltDirectorySeparatorChar {
379 [MethodImplAttribute (MethodImplOptions.InternalCall)]
383 public extern static char PathSeparator {
384 [MethodImplAttribute (MethodImplOptions.InternalCall)]
388 public extern static char [] InvalidPathChars {
389 [MethodImplAttribute (MethodImplOptions.InternalCall)]
393 [MethodImplAttribute (MethodImplOptions.InternalCall)]
394 public extern static int GetTempPath(out string path);