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
;
38 using System
.IO
.IsolatedStorage
;
43 unsafe internal sealed class MonoIO
{
44 public static readonly FileAttributes
45 InvalidFileAttributes
= (FileAttributes
)(-1);
47 public static readonly IntPtr
48 InvalidHandle
= (IntPtr
)(-1L);
51 public static Exception
GetException (MonoIOError error
)
53 /* This overload is currently only called from
54 * File.MoveFile(), Directory.Move() and
55 * Directory.GetCurrentDirectory() -
56 * everywhere else supplies a path to format
57 * with the error text.
60 case MonoIOError
.ERROR_ACCESS_DENIED
:
61 return new UnauthorizedAccessException ("Access to the path is denied.");
62 case MonoIOError
.ERROR_FILE_EXISTS
:
63 string message
= "Cannot create a file that already exist.";
64 return new IOException (message
, unchecked ((int) 0x80070000) | (int) error
);
66 /* Add more mappings here if other
67 * errors trigger the named but empty
68 * path bug (see bug 82141.) For
69 * everything else, fall through to
72 return GetException (String
.Empty
, error
);
76 public static Exception
GetException (string path
,
82 // FIXME: add more exception mappings here
83 case MonoIOError
.ERROR_FILE_NOT_FOUND
:
84 message
= String
.Format ("Could not find file \"{0}\"", path
);
86 return new IsolatedStorageException (message
);
88 return new FileNotFoundException (message
, path
);
91 case MonoIOError
.ERROR_TOO_MANY_OPEN_FILES
:
92 return new IOException ("Too many open files", unchecked((int)0x80070000) | (int)error
);
94 case MonoIOError
.ERROR_PATH_NOT_FOUND
:
95 message
= String
.Format ("Could not find a part of the path \"{0}\"", path
);
97 return new IsolatedStorageException (message
);
99 return new DirectoryNotFoundException (message
);
102 case MonoIOError
.ERROR_ACCESS_DENIED
:
103 message
= String
.Format ("Access to the path \"{0}\" is denied.", path
);
104 return new UnauthorizedAccessException (message
);
106 case MonoIOError
.ERROR_INVALID_HANDLE
:
107 message
= String
.Format ("Invalid handle to path \"{0}\"", path
);
108 return new IOException (message
, unchecked((int)0x80070000) | (int)error
);
109 case MonoIOError
.ERROR_INVALID_DRIVE
:
110 message
= String
.Format ("Could not find the drive '{0}'. The drive might not be ready or might not be mapped.", path
);
112 return new DriveNotFoundException (message
);
114 return new IOException (message
, unchecked((int)0x80070000) | (int)error
);
116 case MonoIOError
.ERROR_FILE_EXISTS
:
117 message
= String
.Format ("Could not create file \"{0}\". File already exists.", path
);
118 return new IOException (message
, unchecked((int)0x80070000) | (int)error
);
120 case MonoIOError
.ERROR_FILENAME_EXCED_RANGE
:
121 message
= String
.Format ("Path is too long. Path: {0}", path
);
122 return new PathTooLongException (message
);
124 case MonoIOError
.ERROR_INVALID_PARAMETER
:
125 message
= String
.Format ("Invalid parameter");
126 return new IOException (message
, unchecked((int)0x80070000) | (int)error
);
128 case MonoIOError
.ERROR_WRITE_FAULT
:
129 message
= String
.Format ("Write fault on path {0}", path
);
130 return new IOException (message
, unchecked((int)0x80070000) | (int)error
);
132 case MonoIOError
.ERROR_SHARING_VIOLATION
:
133 message
= String
.Format ("Sharing violation on path {0}", path
);
134 return new IOException (message
, unchecked((int)0x80070000) | (int)error
);
136 case MonoIOError
.ERROR_LOCK_VIOLATION
:
137 message
= String
.Format ("Lock violation on path {0}", path
);
138 return new IOException (message
, unchecked((int)0x80070000) | (int)error
);
140 case MonoIOError
.ERROR_HANDLE_DISK_FULL
:
141 message
= String
.Format ("Disk full. Path {0}", path
);
142 return new IOException (message
, unchecked((int)0x80070000) | (int)error
);
144 case MonoIOError
.ERROR_DIR_NOT_EMPTY
:
145 message
= String
.Format ("Directory {0} is not empty", path
);
146 return new IOException (message
, unchecked((int)0x80070000) | (int)error
);
148 case MonoIOError
.ERROR_ENCRYPTION_FAILED
:
149 return new IOException ("Encryption failed", unchecked((int)0x80070000) | (int)error
);
151 case MonoIOError
.ERROR_CANNOT_MAKE
:
152 message
= String
.Format ("Path {0} is a directory", path
);
153 return new IOException (message
, unchecked((int)0x80070000) | (int)error
);
155 case MonoIOError
.ERROR_NOT_SAME_DEVICE
:
156 message
= "Source and destination are not on the same device";
157 return new IOException (message
, unchecked((int)0x80070000) | (int)error
);
160 message
= String
.Format ("Win32 IO returned {0}. Path: {1}", error
, path
);
161 return new IOException (message
, unchecked((int)0x80070000) | (int)error
);
167 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
168 public extern static bool CreateDirectory (string path
, out MonoIOError error
);
170 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
171 public extern static bool RemoveDirectory (string path
, out MonoIOError error
);
173 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
174 public extern static string [] GetFileSystemEntries (string path
, string path_with_pattern
, int attrs
, int mask
, out MonoIOError error
);
176 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
177 public extern static string GetCurrentDirectory (out MonoIOError error
);
179 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
180 public extern static bool SetCurrentDirectory (string path
, out MonoIOError error
);
184 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
185 public extern static bool MoveFile (string path
, string dest
,
186 out MonoIOError error
);
188 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
189 public extern static bool CopyFile (string path
, string dest
,
191 out MonoIOError error
);
193 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
194 public extern static bool DeleteFile (string path
,
195 out MonoIOError error
);
197 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
198 public extern static bool ReplaceFile (string sourceFileName
,
199 string destinationFileName
,
200 string destinationBackupFileName
,
201 bool ignoreMetadataErrors
,
202 out MonoIOError error
);
204 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
205 public extern static FileAttributes
GetFileAttributes (string path
, out MonoIOError error
);
207 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
208 public extern static bool SetFileAttributes (string path
, FileAttributes attrs
, out MonoIOError error
);
210 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
211 public extern static MonoFileType
GetFileType (IntPtr handle
, out MonoIOError error
);
216 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
217 public extern static string FindFirst (string path
, string pattern
, out FileAttributes result_attr
, out MonoIOError error
, out IntPtr handle
);
219 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
220 public extern static string FindNext (IntPtr handle
, out FileAttributes result_attr
, out MonoIOError error
);
222 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
223 public extern static int FindClose (IntPtr handle
);
225 public static bool Exists (string path
, out MonoIOError error
)
227 FileAttributes attrs
= GetFileAttributes (path
,
229 if (attrs
== InvalidFileAttributes
)
235 public static bool ExistsFile (string path
,
236 out MonoIOError error
)
238 FileAttributes attrs
= GetFileAttributes (path
,
240 if (attrs
== InvalidFileAttributes
)
243 if ((attrs
& FileAttributes
.Directory
) != 0)
249 public static bool ExistsDirectory (string path
,
250 out MonoIOError error
)
252 FileAttributes attrs
= GetFileAttributes (path
,
255 // Actually, we are looking for a directory, not a file
256 if (error
== MonoIOError
.ERROR_FILE_NOT_FOUND
)
257 error
= MonoIOError
.ERROR_PATH_NOT_FOUND
;
259 if (attrs
== InvalidFileAttributes
)
262 if ((attrs
& FileAttributes
.Directory
) == 0)
268 public static bool ExistsSymlink (string path
,
269 out MonoIOError error
)
271 FileAttributes attrs
= GetFileAttributes (path
,
273 if (attrs
== InvalidFileAttributes
)
276 if ((attrs
& FileAttributes
.ReparsePoint
) == 0)
282 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
283 public extern static bool GetFileStat (string path
,
285 out MonoIOError error
);
289 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
290 public extern static IntPtr
Open (string filename
,
295 out MonoIOError error
);
297 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
298 public extern static bool Close (IntPtr handle
,
299 out MonoIOError error
);
301 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
302 public extern static int Read (IntPtr handle
, byte [] dest
,
303 int dest_offset
, int count
,
304 out MonoIOError error
);
306 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
307 public extern static int Write (IntPtr handle
, [In
] byte [] src
,
308 int src_offset
, int count
,
309 out MonoIOError error
);
311 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
312 public extern static long Seek (IntPtr handle
, long offset
,
314 out MonoIOError error
);
316 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
317 public extern static bool Flush (IntPtr handle
,
318 out MonoIOError error
);
320 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
321 public extern static long GetLength (IntPtr handle
,
322 out MonoIOError error
);
324 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
325 public extern static bool SetLength (IntPtr handle
,
327 out MonoIOError error
);
329 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
330 public extern static bool SetFileTime (IntPtr handle
,
332 long last_access_time
,
333 long last_write_time
,
334 out MonoIOError error
);
336 public static bool SetFileTime (string path
,
338 long last_access_time
,
339 long last_write_time
,
340 out MonoIOError error
)
342 return SetFileTime (path
,
351 public static bool SetCreationTime (string path
,
353 out MonoIOError error
)
355 return SetFileTime (path
, 1, -1, -1, -1, dateTime
, out error
);
358 public static bool SetLastAccessTime (string path
,
360 out MonoIOError error
)
362 return SetFileTime (path
, 2, -1, -1, -1, dateTime
, out error
);
365 public static bool SetLastWriteTime (string path
,
367 out MonoIOError error
)
369 return SetFileTime (path
, 3, -1, -1, -1, dateTime
, out error
);
372 public static bool SetFileTime (string path
,
375 long last_access_time
,
376 long last_write_time
,
378 out MonoIOError error
)
383 handle
= Open (path
, FileMode
.Open
,
384 FileAccess
.ReadWrite
,
385 FileShare
.ReadWrite
, FileOptions
.None
, out error
);
386 if (handle
== MonoIO
.InvalidHandle
)
391 creation_time
= dateTime
.ToFileTime ();
394 last_access_time
= dateTime
.ToFileTime ();
397 last_write_time
= dateTime
.ToFileTime ();
401 result
= SetFileTime (handle
, creation_time
,
403 last_write_time
, out error
);
405 MonoIOError ignore_error
;
406 Close (handle
, out ignore_error
);
411 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
412 public extern static void Lock (IntPtr handle
,
413 long position
, long length
,
414 out MonoIOError error
);
416 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
417 public extern static void Unlock (IntPtr handle
,
418 long position
, long length
,
419 out MonoIOError error
);
423 public extern static IntPtr ConsoleOutput
{
424 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
428 public extern static IntPtr ConsoleInput
{
429 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
433 public extern static IntPtr ConsoleError
{
434 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
440 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
441 public extern static bool CreatePipe (out IntPtr read_handle
, out IntPtr write_handle
);
443 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
444 public extern static bool DuplicateHandle (IntPtr source_process_handle
, IntPtr source_handle
,
445 IntPtr target_process_handle
, out IntPtr target_handle
, int access
, int inherit
, int options
);
449 public extern static char VolumeSeparatorChar
{
450 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
454 public extern static char DirectorySeparatorChar
{
455 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
459 public extern static char AltDirectorySeparatorChar
{
460 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
464 public extern static char PathSeparator
{
465 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
469 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
470 public extern static int GetTempPath(out string path
);