2010-04-06 Jb Evain <jbevain@novell.com>
[mcs.git] / class / corlib / System.IO / MonoIO.cs
blob399bdfcef5968d4681acf82818d7ef3969bbd6f6
1 // System.IO.MonoIO.cs: static interface to native filesystem.
2 //
3 // Author:
4 // Dan Lewis (dihlewis@yahoo.co.uk)
5 // Dick Porter (dick@ximian.com)
6 //
7 // (C) 2002
8 //
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:
20 //
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 //
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.
33 using System;
34 using System.Runtime.CompilerServices;
35 using System.Runtime.InteropServices;
36 using System.Threading;
37 #if NET_2_1
38 using System.IO.IsolatedStorage;
39 #endif
41 namespace System.IO
43 unsafe internal sealed class MonoIO {
44 public static readonly FileAttributes
45 InvalidFileAttributes = (FileAttributes)(-1);
47 public static readonly IntPtr
48 InvalidHandle = (IntPtr)(-1L);
50 // error methods
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.
59 switch(error) {
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);
65 default:
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
70 * the other overload
72 return GetException (String.Empty, error);
76 public static Exception GetException (string path,
77 MonoIOError error)
79 string message;
81 switch (error) {
82 // FIXME: add more exception mappings here
83 case MonoIOError.ERROR_FILE_NOT_FOUND:
84 message = String.Format ("Could not find file \"{0}\"", path);
85 #if NET_2_1
86 return new IsolatedStorageException (message);
87 #else
88 return new FileNotFoundException (message, path);
89 #endif
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);
96 #if NET_2_1
97 return new IsolatedStorageException (message);
98 #else
99 return new DirectoryNotFoundException (message);
100 #endif
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);
111 #if !NET_2_1
112 return new DriveNotFoundException (message);
113 #else
114 return new IOException (message, unchecked((int)0x80070000) | (int)error);
115 #endif
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);
159 default:
160 message = String.Format ("Win32 IO returned {0}. Path: {1}", error, path);
161 return new IOException (message, unchecked((int)0x80070000) | (int)error);
165 // directory methods
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);
182 // file methods
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,
190 bool overwrite,
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);
214 // Find file methods
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,
228 out error);
229 if (attrs == InvalidFileAttributes)
230 return false;
232 return true;
235 public static bool ExistsFile (string path,
236 out MonoIOError error)
238 FileAttributes attrs = GetFileAttributes (path,
239 out error);
240 if (attrs == InvalidFileAttributes)
241 return false;
243 if ((attrs & FileAttributes.Directory) != 0)
244 return false;
246 return true;
249 public static bool ExistsDirectory (string path,
250 out MonoIOError error)
252 FileAttributes attrs = GetFileAttributes (path,
253 out error);
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)
260 return false;
262 if ((attrs & FileAttributes.Directory) == 0)
263 return false;
265 return true;
268 public static bool ExistsSymlink (string path,
269 out MonoIOError error)
271 FileAttributes attrs = GetFileAttributes (path,
272 out error);
273 if (attrs == InvalidFileAttributes)
274 return false;
276 if ((attrs & FileAttributes.ReparsePoint) == 0)
277 return false;
279 return true;
282 [MethodImplAttribute (MethodImplOptions.InternalCall)]
283 public extern static bool GetFileStat (string path,
284 out MonoIOStat stat,
285 out MonoIOError error);
287 // handle methods
289 [MethodImplAttribute (MethodImplOptions.InternalCall)]
290 public extern static IntPtr Open (string filename,
291 FileMode mode,
292 FileAccess access,
293 FileShare share,
294 FileOptions options,
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,
313 SeekOrigin origin,
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,
326 long length,
327 out MonoIOError error);
329 [MethodImplAttribute (MethodImplOptions.InternalCall)]
330 public extern static bool SetFileTime (IntPtr handle,
331 long creation_time,
332 long last_access_time,
333 long last_write_time,
334 out MonoIOError error);
336 public static bool SetFileTime (string path,
337 long creation_time,
338 long last_access_time,
339 long last_write_time,
340 out MonoIOError error)
342 return SetFileTime (path,
344 creation_time,
345 last_access_time,
346 last_write_time,
347 DateTime.MinValue,
348 out error);
351 public static bool SetCreationTime (string path,
352 DateTime dateTime,
353 out MonoIOError error)
355 return SetFileTime (path, 1, -1, -1, -1, dateTime, out error);
358 public static bool SetLastAccessTime (string path,
359 DateTime dateTime,
360 out MonoIOError error)
362 return SetFileTime (path, 2, -1, -1, -1, dateTime, out error);
365 public static bool SetLastWriteTime (string path,
366 DateTime dateTime,
367 out MonoIOError error)
369 return SetFileTime (path, 3, -1, -1, -1, dateTime, out error);
372 public static bool SetFileTime (string path,
373 int type,
374 long creation_time,
375 long last_access_time,
376 long last_write_time,
377 DateTime dateTime,
378 out MonoIOError error)
380 IntPtr handle;
381 bool result;
383 handle = Open (path, FileMode.Open,
384 FileAccess.ReadWrite,
385 FileShare.ReadWrite, FileOptions.None, out error);
386 if (handle == MonoIO.InvalidHandle)
387 return false;
389 switch (type) {
390 case 1:
391 creation_time = dateTime.ToFileTime ();
392 break;
393 case 2:
394 last_access_time = dateTime.ToFileTime ();
395 break;
396 case 3:
397 last_write_time = dateTime.ToFileTime ();
398 break;
401 result = SetFileTime (handle, creation_time,
402 last_access_time,
403 last_write_time, out error);
405 MonoIOError ignore_error;
406 Close (handle, out ignore_error);
408 return result;
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);
421 // console handles
423 public extern static IntPtr ConsoleOutput {
424 [MethodImplAttribute (MethodImplOptions.InternalCall)]
425 get;
428 public extern static IntPtr ConsoleInput {
429 [MethodImplAttribute (MethodImplOptions.InternalCall)]
430 get;
433 public extern static IntPtr ConsoleError {
434 [MethodImplAttribute (MethodImplOptions.InternalCall)]
435 get;
438 // pipe handles
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);
447 // path characters
449 public extern static char VolumeSeparatorChar {
450 [MethodImplAttribute (MethodImplOptions.InternalCall)]
451 get;
454 public extern static char DirectorySeparatorChar {
455 [MethodImplAttribute (MethodImplOptions.InternalCall)]
456 get;
459 public extern static char AltDirectorySeparatorChar {
460 [MethodImplAttribute (MethodImplOptions.InternalCall)]
461 get;
464 public extern static char PathSeparator {
465 [MethodImplAttribute (MethodImplOptions.InternalCall)]
466 get;
469 [MethodImplAttribute (MethodImplOptions.InternalCall)]
470 public extern static int GetTempPath(out string path);