2 // System.IO.MonoIO.cs: static interface to native filesystem.
5 // Dan Lewis (dihlewis@yahoo.co.uk)
6 // Dick Porter (dick@ximian.com)
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:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
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.
35 using System
.Runtime
.CompilerServices
;
36 using System
.Runtime
.InteropServices
;
37 using System
.Threading
;
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 ();
52 public static Exception
GetException (MonoIOError error
)
54 return GetException (String
.Empty
, error
);
57 public static Exception
GetException (string path
,
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
,
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
);
90 message
= String
.Format ("Win32 IO returned {0}. Path: {1}", error
, path
);
91 return new IOException (message
);
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
);
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
,
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
);
144 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
145 extern static bool GetSupportsAsync ();
147 public static bool Exists (string path
, out MonoIOError error
)
149 FileAttributes attrs
= GetFileAttributes (path
,
151 if (attrs
== InvalidFileAttributes
)
157 public static bool ExistsFile (string path
,
158 out MonoIOError error
)
160 FileAttributes attrs
= GetFileAttributes (path
,
162 if (attrs
== InvalidFileAttributes
)
165 if ((attrs
& FileAttributes
.Directory
) != 0)
171 public static bool ExistsDirectory (string path
,
172 out MonoIOError error
)
174 FileAttributes attrs
= GetFileAttributes (path
,
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
)
184 if ((attrs
& FileAttributes
.Directory
) == 0)
190 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
191 public extern static bool GetFileStat (string path
,
193 out MonoIOError error
);
197 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
198 public extern static IntPtr
Open (string filename
,
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
,
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
,
235 out MonoIOError error
);
237 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
238 public extern static bool SetFileTime (IntPtr handle
,
240 long last_access_time
,
241 long last_write_time
,
242 out MonoIOError error
);
244 public static bool SetFileTime (string path
,
246 long last_access_time
,
247 long last_write_time
,
248 out MonoIOError error
)
250 return SetFileTime (path
,
259 public static bool SetCreationTime (string path
,
261 out MonoIOError error
)
263 return SetFileTime (path
, 1, -1, -1, -1, dateTime
, out error
);
266 public static bool SetLastAccessTime (string path
,
268 out MonoIOError error
)
270 return SetFileTime (path
, 2, -1, -1, -1, dateTime
, out error
);
273 public static bool SetLastWriteTime (string path
,
275 out MonoIOError error
)
277 return SetFileTime (path
, 3, -1, -1, -1, dateTime
, out error
);
280 public static bool SetFileTime (string path
,
283 long last_access_time
,
284 long last_write_time
,
286 out MonoIOError error
)
291 handle
= Open (path
, FileMode
.Open
,
292 FileAccess
.ReadWrite
,
293 FileShare
.ReadWrite
, false, out error
);
294 if (handle
== MonoIO
.InvalidHandle
)
299 creation_time
= dateTime
.ToFileTime ();
302 last_access_time
= dateTime
.ToFileTime ();
305 last_write_time
= dateTime
.ToFileTime ();
309 result
= SetFileTime (handle
, creation_time
,
311 last_write_time
, out error
);
313 MonoIOError ignore_error
;
314 Close (handle
, out ignore_error
);
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
);
331 public extern static IntPtr ConsoleOutput
{
332 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
336 public extern static IntPtr ConsoleInput
{
337 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
341 public extern static IntPtr ConsoleError
{
342 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
348 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
349 public extern static bool CreatePipe (out IntPtr read_handle
, out IntPtr write_handle
);
353 public extern static char VolumeSeparatorChar
{
354 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
358 public extern static char DirectorySeparatorChar
{
359 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
363 public extern static char AltDirectorySeparatorChar
{
364 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
368 public extern static char PathSeparator
{
369 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
373 public extern static char [] InvalidPathChars
{
374 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
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
);