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_SHARING_VIOLATION:
93 message = String.Format ("Sharing violation on path {0}", path);
94 return new IOException (message);
96 case MonoIOError.ERROR_LOCK_VIOLATION:
97 message = String.Format ("Lock violation on path {0}", path);
98 return new IOException (message);
101 message = String.Format ("Win32 IO returned {0}. Path: {1}", error, path);
102 return new IOException (message);
108 [MethodImplAttribute (MethodImplOptions.InternalCall)]
109 public extern static bool CreateDirectory (string path, out MonoIOError error);
111 [MethodImplAttribute (MethodImplOptions.InternalCall)]
112 public extern static bool RemoveDirectory (string path, out MonoIOError error);
114 [MethodImplAttribute (MethodImplOptions.InternalCall)]
115 public extern static IntPtr FindFirstFile (string path, out MonoIOStat stat, out MonoIOError error);
117 [MethodImplAttribute (MethodImplOptions.InternalCall)]
118 public extern static bool FindNextFile (IntPtr find, out MonoIOStat stat, out MonoIOError error);
120 [MethodImplAttribute (MethodImplOptions.InternalCall)]
121 public extern static bool FindClose (IntPtr find,
122 out MonoIOError error);
124 [MethodImplAttribute (MethodImplOptions.InternalCall)]
125 public extern static string GetCurrentDirectory (out MonoIOError error);
127 [MethodImplAttribute (MethodImplOptions.InternalCall)]
128 public extern static bool SetCurrentDirectory (string path, out MonoIOError error);
132 [MethodImplAttribute (MethodImplOptions.InternalCall)]
133 public extern static bool MoveFile (string path, string dest,
134 out MonoIOError error);
136 [MethodImplAttribute (MethodImplOptions.InternalCall)]
137 public extern static bool CopyFile (string path, string dest,
139 out MonoIOError error);
141 [MethodImplAttribute (MethodImplOptions.InternalCall)]
142 public extern static bool DeleteFile (string path,
143 out MonoIOError error);
145 [MethodImplAttribute (MethodImplOptions.InternalCall)]
146 public extern static FileAttributes GetFileAttributes (string path, out MonoIOError error);
148 [MethodImplAttribute (MethodImplOptions.InternalCall)]
149 public extern static bool SetFileAttributes (string path, FileAttributes attrs, out MonoIOError error);
151 [MethodImplAttribute (MethodImplOptions.InternalCall)]
152 public extern static MonoFileType GetFileType (IntPtr handle, out MonoIOError error);
154 public static bool Exists (string path, out MonoIOError error)
156 FileAttributes attrs = GetFileAttributes (path,
158 if (attrs == InvalidFileAttributes)
164 public static bool ExistsFile (string path,
165 out MonoIOError error)
167 FileAttributes attrs = GetFileAttributes (path,
169 if (attrs == InvalidFileAttributes)
172 if ((attrs & FileAttributes.Directory) != 0)
178 public static bool ExistsDirectory (string path,
179 out MonoIOError error)
181 FileAttributes attrs = GetFileAttributes (path,
184 // Actually, we are looking for a directory, not a file
185 if (error == MonoIOError.ERROR_FILE_NOT_FOUND)
186 error = MonoIOError.ERROR_PATH_NOT_FOUND;
188 if (attrs == InvalidFileAttributes)
191 if ((attrs & FileAttributes.Directory) == 0)
197 [MethodImplAttribute (MethodImplOptions.InternalCall)]
198 public extern static bool GetFileStat (string path,
200 out MonoIOError error);
204 [MethodImplAttribute (MethodImplOptions.InternalCall)]
205 public extern static IntPtr Open (string filename,
210 out MonoIOError error);
212 [MethodImplAttribute (MethodImplOptions.InternalCall)]
213 public extern static bool Close (IntPtr handle,
214 out MonoIOError error);
216 [MethodImplAttribute (MethodImplOptions.InternalCall)]
217 public extern static int Read (IntPtr handle, byte [] dest,
218 int dest_offset, int count,
219 out MonoIOError error);
221 [MethodImplAttribute (MethodImplOptions.InternalCall)]
222 public extern static int Write (IntPtr handle, [In] byte [] src,
223 int src_offset, int count,
224 out MonoIOError error);
226 [MethodImplAttribute (MethodImplOptions.InternalCall)]
227 public extern static long Seek (IntPtr handle, long offset,
229 out MonoIOError error);
231 [MethodImplAttribute (MethodImplOptions.InternalCall)]
232 public extern static bool Flush (IntPtr handle,
233 out MonoIOError error);
235 [MethodImplAttribute (MethodImplOptions.InternalCall)]
236 public extern static long GetLength (IntPtr handle,
237 out MonoIOError error);
239 [MethodImplAttribute (MethodImplOptions.InternalCall)]
240 public extern static bool SetLength (IntPtr handle,
242 out MonoIOError error);
244 [MethodImplAttribute (MethodImplOptions.InternalCall)]
245 public extern static bool SetFileTime (IntPtr handle,
247 long last_access_time,
248 long last_write_time,
249 out MonoIOError error);
251 public static bool SetFileTime (string path,
253 long last_access_time,
254 long last_write_time,
255 out MonoIOError error)
257 return SetFileTime (path,
266 public static bool SetCreationTime (string path,
268 out MonoIOError error)
270 return SetFileTime (path, 1, -1, -1, -1, dateTime, out error);
273 public static bool SetLastAccessTime (string path,
275 out MonoIOError error)
277 return SetFileTime (path, 2, -1, -1, -1, dateTime, out error);
280 public static bool SetLastWriteTime (string path,
282 out MonoIOError error)
284 return SetFileTime (path, 3, -1, -1, -1, dateTime, out error);
287 public static bool SetFileTime (string path,
290 long last_access_time,
291 long last_write_time,
293 out MonoIOError error)
298 handle = Open (path, FileMode.Open,
299 FileAccess.ReadWrite,
300 FileShare.ReadWrite, false, out error);
301 if (handle == MonoIO.InvalidHandle)
306 creation_time = dateTime.ToFileTime ();
309 last_access_time = dateTime.ToFileTime ();
312 last_write_time = dateTime.ToFileTime ();
316 result = SetFileTime (handle, creation_time,
318 last_write_time, out error);
320 MonoIOError ignore_error;
321 Close (handle, out ignore_error);
326 [MethodImplAttribute (MethodImplOptions.InternalCall)]
327 public extern static void Lock (IntPtr handle,
328 long position, long length,
329 out MonoIOError error);
331 [MethodImplAttribute (MethodImplOptions.InternalCall)]
332 public extern static void Unlock (IntPtr handle,
333 long position, long length,
334 out MonoIOError error);
338 public extern static IntPtr ConsoleOutput {
339 [MethodImplAttribute (MethodImplOptions.InternalCall)]
343 public extern static IntPtr ConsoleInput {
344 [MethodImplAttribute (MethodImplOptions.InternalCall)]
348 public extern static IntPtr ConsoleError {
349 [MethodImplAttribute (MethodImplOptions.InternalCall)]
355 [MethodImplAttribute (MethodImplOptions.InternalCall)]
356 public extern static bool CreatePipe (out IntPtr read_handle, out IntPtr write_handle);
360 public extern static char VolumeSeparatorChar {
361 [MethodImplAttribute (MethodImplOptions.InternalCall)]
365 public extern static char DirectorySeparatorChar {
366 [MethodImplAttribute (MethodImplOptions.InternalCall)]
370 public extern static char AltDirectorySeparatorChar {
371 [MethodImplAttribute (MethodImplOptions.InternalCall)]
375 public extern static char PathSeparator {
376 [MethodImplAttribute (MethodImplOptions.InternalCall)]
380 public extern static char [] InvalidPathChars {
381 [MethodImplAttribute (MethodImplOptions.InternalCall)]
385 [MethodImplAttribute (MethodImplOptions.InternalCall)]
386 public extern static int GetTempPath(out string path);