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 string [] GetFileSystemEntries (string path, string pattern, int attrs, int mask, out MonoIOError error);
125 [MethodImplAttribute (MethodImplOptions.InternalCall)]
126 public extern static string GetCurrentDirectory (out MonoIOError error);
128 [MethodImplAttribute (MethodImplOptions.InternalCall)]
129 public extern static bool SetCurrentDirectory (string path, out MonoIOError error);
133 [MethodImplAttribute (MethodImplOptions.InternalCall)]
134 public extern static bool MoveFile (string path, string dest,
135 out MonoIOError error);
137 [MethodImplAttribute (MethodImplOptions.InternalCall)]
138 public extern static bool CopyFile (string path, string dest,
140 out MonoIOError error);
142 [MethodImplAttribute (MethodImplOptions.InternalCall)]
143 public extern static bool DeleteFile (string path,
144 out MonoIOError error);
146 [MethodImplAttribute (MethodImplOptions.InternalCall)]
147 public extern static FileAttributes GetFileAttributes (string path, out MonoIOError error);
149 [MethodImplAttribute (MethodImplOptions.InternalCall)]
150 public extern static bool SetFileAttributes (string path, FileAttributes attrs, out MonoIOError error);
152 [MethodImplAttribute (MethodImplOptions.InternalCall)]
153 public extern static MonoFileType GetFileType (IntPtr handle, out MonoIOError error);
155 public static bool Exists (string path, out MonoIOError error)
157 FileAttributes attrs = GetFileAttributes (path,
159 if (attrs == InvalidFileAttributes)
165 public static bool ExistsFile (string path,
166 out MonoIOError error)
168 FileAttributes attrs = GetFileAttributes (path,
170 if (attrs == InvalidFileAttributes)
173 if ((attrs & FileAttributes.Directory) != 0)
179 public static bool ExistsDirectory (string path,
180 out MonoIOError error)
182 FileAttributes attrs = GetFileAttributes (path,
185 // Actually, we are looking for a directory, not a file
186 if (error == MonoIOError.ERROR_FILE_NOT_FOUND)
187 error = MonoIOError.ERROR_PATH_NOT_FOUND;
189 if (attrs == InvalidFileAttributes)
192 if ((attrs & FileAttributes.Directory) == 0)
198 [MethodImplAttribute (MethodImplOptions.InternalCall)]
199 public extern static bool GetFileStat (string path,
201 out MonoIOError error);
205 [MethodImplAttribute (MethodImplOptions.InternalCall)]
206 public extern static IntPtr Open (string filename,
211 out MonoIOError error);
213 [MethodImplAttribute (MethodImplOptions.InternalCall)]
214 public extern static bool Close (IntPtr handle,
215 out MonoIOError error);
217 [MethodImplAttribute (MethodImplOptions.InternalCall)]
218 public extern static int Read (IntPtr handle, byte [] dest,
219 int dest_offset, int count,
220 out MonoIOError error);
222 [MethodImplAttribute (MethodImplOptions.InternalCall)]
223 public extern static int Write (IntPtr handle, [In] byte [] src,
224 int src_offset, int count,
225 out MonoIOError error);
227 [MethodImplAttribute (MethodImplOptions.InternalCall)]
228 public extern static long Seek (IntPtr handle, long offset,
230 out MonoIOError error);
232 [MethodImplAttribute (MethodImplOptions.InternalCall)]
233 public extern static bool Flush (IntPtr handle,
234 out MonoIOError error);
236 [MethodImplAttribute (MethodImplOptions.InternalCall)]
237 public extern static long GetLength (IntPtr handle,
238 out MonoIOError error);
240 [MethodImplAttribute (MethodImplOptions.InternalCall)]
241 public extern static bool SetLength (IntPtr handle,
243 out MonoIOError error);
245 [MethodImplAttribute (MethodImplOptions.InternalCall)]
246 public extern static bool SetFileTime (IntPtr handle,
248 long last_access_time,
249 long last_write_time,
250 out MonoIOError error);
252 public static bool SetFileTime (string path,
254 long last_access_time,
255 long last_write_time,
256 out MonoIOError error)
258 return SetFileTime (path,
267 public static bool SetCreationTime (string path,
269 out MonoIOError error)
271 return SetFileTime (path, 1, -1, -1, -1, dateTime, out error);
274 public static bool SetLastAccessTime (string path,
276 out MonoIOError error)
278 return SetFileTime (path, 2, -1, -1, -1, dateTime, out error);
281 public static bool SetLastWriteTime (string path,
283 out MonoIOError error)
285 return SetFileTime (path, 3, -1, -1, -1, dateTime, out error);
288 public static bool SetFileTime (string path,
291 long last_access_time,
292 long last_write_time,
294 out MonoIOError error)
299 handle = Open (path, FileMode.Open,
300 FileAccess.ReadWrite,
301 FileShare.ReadWrite, false, out error);
302 if (handle == MonoIO.InvalidHandle)
307 creation_time = dateTime.ToFileTime ();
310 last_access_time = dateTime.ToFileTime ();
313 last_write_time = dateTime.ToFileTime ();
317 result = SetFileTime (handle, creation_time,
319 last_write_time, out error);
321 MonoIOError ignore_error;
322 Close (handle, out ignore_error);
327 [MethodImplAttribute (MethodImplOptions.InternalCall)]
328 public extern static void Lock (IntPtr handle,
329 long position, long length,
330 out MonoIOError error);
332 [MethodImplAttribute (MethodImplOptions.InternalCall)]
333 public extern static void Unlock (IntPtr handle,
334 long position, long length,
335 out MonoIOError error);
339 public extern static IntPtr ConsoleOutput {
340 [MethodImplAttribute (MethodImplOptions.InternalCall)]
344 public extern static IntPtr ConsoleInput {
345 [MethodImplAttribute (MethodImplOptions.InternalCall)]
349 public extern static IntPtr ConsoleError {
350 [MethodImplAttribute (MethodImplOptions.InternalCall)]
356 [MethodImplAttribute (MethodImplOptions.InternalCall)]
357 public extern static bool CreatePipe (out IntPtr read_handle, out IntPtr write_handle);
361 public extern static char VolumeSeparatorChar {
362 [MethodImplAttribute (MethodImplOptions.InternalCall)]
366 public extern static char DirectorySeparatorChar {
367 [MethodImplAttribute (MethodImplOptions.InternalCall)]
371 public extern static char AltDirectorySeparatorChar {
372 [MethodImplAttribute (MethodImplOptions.InternalCall)]
376 public extern static char PathSeparator {
377 [MethodImplAttribute (MethodImplOptions.InternalCall)]
381 public extern static char [] InvalidPathChars {
382 [MethodImplAttribute (MethodImplOptions.InternalCall)]
386 [MethodImplAttribute (MethodImplOptions.InternalCall)]
387 public extern static int GetTempPath(out string path);