**** Merged from MCS ****
[mono-project.git] / mcs / class / corlib / System.IO / MonoIO.cs
bloba32e6ed54084ee8f767542c90df51e2fd10404b7
1 //
2 // System.IO.MonoIO.cs: static interface to native filesystem.
3 //
4 // Author:
5 // Dan Lewis (dihlewis@yahoo.co.uk)
6 // Dick Porter (dick@ximian.com)
7 //
8 // (C) 2002
9 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 //
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 //
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System;
35 using System.Runtime.CompilerServices;
36 using System.Runtime.InteropServices;
37 using System.Threading;
39 namespace System.IO
41 unsafe internal sealed class MonoIO {
42 public static readonly FileAttributes
43 InvalidFileAttributes = (FileAttributes)(-1);
45 public static readonly IntPtr
46 InvalidHandle = (IntPtr)(-1L);
48 public static readonly bool SupportsAsync = GetSupportsAsync ();
50 // error methods
52 public static Exception GetException (MonoIOError error)
54 return GetException (String.Empty, error);
57 public static Exception GetException (string path,
58 MonoIOError error)
60 string message;
62 switch (error) {
63 // FIXME: add more exception mappings here
64 case MonoIOError.ERROR_FILE_NOT_FOUND:
65 message = String.Format ("Could not find file \"{0}\"", path);
66 return new FileNotFoundException (message,
67 path);
69 case MonoIOError.ERROR_PATH_NOT_FOUND:
70 message = String.Format ("Could not find a part of the path \"{0}\"", path);
71 return new DirectoryNotFoundException (message);
73 case MonoIOError.ERROR_ACCESS_DENIED:
74 message = String.Format ("Access to the path \"{0}\" is denied.", path);
75 return new UnauthorizedAccessException (message);
77 case MonoIOError.ERROR_FILE_EXISTS:
78 message = String.Format ("Could not create file \"{0}\". File already exists.", path);
79 return new IOException (message);
81 case MonoIOError.ERROR_FILENAME_EXCED_RANGE:
82 message = String.Format ("Path is too long. Path: {0}", path);
83 return new PathTooLongException (message);
85 case MonoIOError.ERROR_INVALID_PARAMETER:
86 message = String.Format ("Invalid parameter");
87 return new IOException (message);
89 default:
90 message = String.Format ("Win32 IO returned {0}. Path: {1}", error, path);
91 return new IOException (message);
95 // directory methods
97 [MethodImplAttribute (MethodImplOptions.InternalCall)]
98 public extern static bool CreateDirectory (string path, out MonoIOError error);
100 [MethodImplAttribute (MethodImplOptions.InternalCall)]
101 public extern static bool RemoveDirectory (string path, out MonoIOError error);
103 [MethodImplAttribute (MethodImplOptions.InternalCall)]
104 public extern static IntPtr FindFirstFile (string path, out MonoIOStat stat, out MonoIOError error);
106 [MethodImplAttribute (MethodImplOptions.InternalCall)]
107 public extern static bool FindNextFile (IntPtr find, out MonoIOStat stat, out MonoIOError error);
109 [MethodImplAttribute (MethodImplOptions.InternalCall)]
110 public extern static bool FindClose (IntPtr find,
111 out MonoIOError error);
113 [MethodImplAttribute (MethodImplOptions.InternalCall)]
114 public extern static string GetCurrentDirectory (out MonoIOError error);
116 [MethodImplAttribute (MethodImplOptions.InternalCall)]
117 public extern static bool SetCurrentDirectory (string path, out MonoIOError error);
119 // file methods
121 [MethodImplAttribute (MethodImplOptions.InternalCall)]
122 public extern static bool MoveFile (string path, string dest,
123 out MonoIOError error);
125 [MethodImplAttribute (MethodImplOptions.InternalCall)]
126 public extern static bool CopyFile (string path, string dest,
127 bool overwrite,
128 out MonoIOError error);
130 [MethodImplAttribute (MethodImplOptions.InternalCall)]
131 public extern static bool DeleteFile (string path,
132 out MonoIOError error);
134 [MethodImplAttribute (MethodImplOptions.InternalCall)]
135 public extern static FileAttributes GetFileAttributes (string path, out MonoIOError error);
137 [MethodImplAttribute (MethodImplOptions.InternalCall)]
138 public extern static bool SetFileAttributes (string path, FileAttributes attrs, out MonoIOError error);
140 [MethodImplAttribute (MethodImplOptions.InternalCall)]
141 public extern static MonoFileType GetFileType (IntPtr handle, out MonoIOError error);
143 // aio_* methods
144 [MethodImplAttribute (MethodImplOptions.InternalCall)]
145 extern static bool GetSupportsAsync ();
147 public static bool Exists (string path, out MonoIOError error)
149 FileAttributes attrs = GetFileAttributes (path,
150 out error);
151 if (attrs == InvalidFileAttributes)
152 return false;
154 return true;
157 public static bool ExistsFile (string path,
158 out MonoIOError error)
160 FileAttributes attrs = GetFileAttributes (path,
161 out error);
162 if (attrs == InvalidFileAttributes)
163 return false;
165 if ((attrs & FileAttributes.Directory) != 0)
166 return false;
168 return true;
171 public static bool ExistsDirectory (string path,
172 out MonoIOError error)
174 FileAttributes attrs = GetFileAttributes (path,
175 out error);
177 // Actually, we are looking for a directory, not a file
178 if (error == MonoIOError.ERROR_FILE_NOT_FOUND)
179 error = MonoIOError.ERROR_PATH_NOT_FOUND;
181 if (attrs == InvalidFileAttributes)
182 return false;
184 if ((attrs & FileAttributes.Directory) == 0)
185 return false;
187 return true;
190 [MethodImplAttribute (MethodImplOptions.InternalCall)]
191 public extern static bool GetFileStat (string path,
192 out MonoIOStat stat,
193 out MonoIOError error);
195 // handle methods
197 [MethodImplAttribute (MethodImplOptions.InternalCall)]
198 public extern static IntPtr Open (string filename,
199 FileMode mode,
200 FileAccess access,
201 FileShare share,
202 bool async,
203 out MonoIOError error);
205 [MethodImplAttribute (MethodImplOptions.InternalCall)]
206 public extern static bool Close (IntPtr handle,
207 out MonoIOError error);
209 [MethodImplAttribute (MethodImplOptions.InternalCall)]
210 public extern static int Read (IntPtr handle, byte [] dest,
211 int dest_offset, int count,
212 out MonoIOError error);
214 [MethodImplAttribute (MethodImplOptions.InternalCall)]
215 public extern static int Write (IntPtr handle, [In] byte [] src,
216 int src_offset, int count,
217 out MonoIOError error);
219 [MethodImplAttribute (MethodImplOptions.InternalCall)]
220 public extern static long Seek (IntPtr handle, long offset,
221 SeekOrigin origin,
222 out MonoIOError error);
224 [MethodImplAttribute (MethodImplOptions.InternalCall)]
225 public extern static bool Flush (IntPtr handle,
226 out MonoIOError error);
228 [MethodImplAttribute (MethodImplOptions.InternalCall)]
229 public extern static long GetLength (IntPtr handle,
230 out MonoIOError error);
232 [MethodImplAttribute (MethodImplOptions.InternalCall)]
233 public extern static bool SetLength (IntPtr handle,
234 long length,
235 out MonoIOError error);
237 [MethodImplAttribute (MethodImplOptions.InternalCall)]
238 public extern static bool SetFileTime (IntPtr handle,
239 long creation_time,
240 long last_access_time,
241 long last_write_time,
242 out MonoIOError error);
244 public static bool SetFileTime (string path,
245 long creation_time,
246 long last_access_time,
247 long last_write_time,
248 out MonoIOError error)
250 return SetFileTime (path,
252 creation_time,
253 last_access_time,
254 last_write_time,
255 DateTime.MinValue,
256 out error);
259 public static bool SetCreationTime (string path,
260 DateTime dateTime,
261 out MonoIOError error)
263 return SetFileTime (path, 1, -1, -1, -1, dateTime, out error);
266 public static bool SetLastAccessTime (string path,
267 DateTime dateTime,
268 out MonoIOError error)
270 return SetFileTime (path, 2, -1, -1, -1, dateTime, out error);
273 public static bool SetLastWriteTime (string path,
274 DateTime dateTime,
275 out MonoIOError error)
277 return SetFileTime (path, 3, -1, -1, -1, dateTime, out error);
280 public static bool SetFileTime (string path,
281 int type,
282 long creation_time,
283 long last_access_time,
284 long last_write_time,
285 DateTime dateTime,
286 out MonoIOError error)
288 IntPtr handle;
289 bool result;
291 handle = Open (path, FileMode.Open,
292 FileAccess.ReadWrite,
293 FileShare.ReadWrite, false, out error);
294 if (handle == MonoIO.InvalidHandle)
295 return false;
297 switch (type) {
298 case 1:
299 creation_time = dateTime.ToFileTime ();
300 break;
301 case 2:
302 last_access_time = dateTime.ToFileTime ();
303 break;
304 case 3:
305 last_write_time = dateTime.ToFileTime ();
306 break;
309 result = SetFileTime (handle, creation_time,
310 last_access_time,
311 last_write_time, out error);
313 MonoIOError ignore_error;
314 Close (handle, out ignore_error);
316 return result;
319 [MethodImplAttribute (MethodImplOptions.InternalCall)]
320 public extern static void Lock (IntPtr handle,
321 long position, long length,
322 out MonoIOError error);
324 [MethodImplAttribute (MethodImplOptions.InternalCall)]
325 public extern static void Unlock (IntPtr handle,
326 long position, long length,
327 out MonoIOError error);
329 // console handles
331 public extern static IntPtr ConsoleOutput {
332 [MethodImplAttribute (MethodImplOptions.InternalCall)]
333 get;
336 public extern static IntPtr ConsoleInput {
337 [MethodImplAttribute (MethodImplOptions.InternalCall)]
338 get;
341 public extern static IntPtr ConsoleError {
342 [MethodImplAttribute (MethodImplOptions.InternalCall)]
343 get;
346 // pipe handles
348 [MethodImplAttribute (MethodImplOptions.InternalCall)]
349 public extern static bool CreatePipe (out IntPtr read_handle, out IntPtr write_handle);
351 // path characters
353 public extern static char VolumeSeparatorChar {
354 [MethodImplAttribute (MethodImplOptions.InternalCall)]
355 get;
358 public extern static char DirectorySeparatorChar {
359 [MethodImplAttribute (MethodImplOptions.InternalCall)]
360 get;
363 public extern static char AltDirectorySeparatorChar {
364 [MethodImplAttribute (MethodImplOptions.InternalCall)]
365 get;
368 public extern static char PathSeparator {
369 [MethodImplAttribute (MethodImplOptions.InternalCall)]
370 get;
373 public extern static char [] InvalidPathChars {
374 [MethodImplAttribute (MethodImplOptions.InternalCall)]
375 get;
378 [MethodImplAttribute (MethodImplOptions.InternalCall)]
379 public extern static int GetTempPath(out string path);
381 [MethodImplAttribute (MethodImplOptions.InternalCall)]
382 public extern static void BeginWrite (IntPtr handle,FileStreamAsyncResult ares);
384 [MethodImplAttribute (MethodImplOptions.InternalCall)]
385 public extern static void BeginRead (IntPtr handle, FileStreamAsyncResult ares);