0dbd3d67570414b4ef9735761f3b46732affacfe
[mono.git] / mcs / class / corlib / System.IO / MonoIO.cs
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 //
9
10 //
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 //
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.
31 //
32
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
40
41 namespace System.IO
42 {
43         unsafe internal sealed class MonoIO {
44                 public static readonly FileAttributes
45                         InvalidFileAttributes = (FileAttributes)(-1);
46
47                 public static readonly IntPtr
48                         InvalidHandle = (IntPtr)(-1L);
49
50                 // error methods
51                 public static Exception GetException (MonoIOError error)
52                 {
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.
58                          */
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
71                                  */
72                                 return GetException (String.Empty, error);
73                         }
74                 }
75
76                 public static Exception GetException (string path,
77                                                       MonoIOError error)
78                 {
79                         string message;
80
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
90
91                         case MonoIOError.ERROR_TOO_MANY_OPEN_FILES:
92                                 return new IOException ("Too many open files", unchecked((int)0x80070000) | (int)error);
93                                 
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
101
102                         case MonoIOError.ERROR_ACCESS_DENIED:
103                                 message = String.Format ("Access to the path \"{0}\" is denied.", path);
104                                 return new UnauthorizedAccessException (message);
105
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);
119
120                         case MonoIOError.ERROR_FILENAME_EXCED_RANGE:
121                                 message = String.Format ("Path is too long. Path: {0}", path); 
122                                 return new PathTooLongException (message);
123
124                         case MonoIOError.ERROR_INVALID_PARAMETER:
125                                 message = String.Format ("Invalid parameter");
126                                 return new IOException (message, unchecked((int)0x80070000) | (int)error);
127
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);
131
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);
135                                 
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);
139                         
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);
143                         
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);
147
148                         case MonoIOError.ERROR_ENCRYPTION_FAILED:
149                                 return new IOException ("Encryption failed", unchecked((int)0x80070000) | (int)error);
150
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);
154                                 
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);
158                                 
159                         default:
160                                 message = String.Format ("Win32 IO returned {0}. Path: {1}", error, path);
161                                 return new IOException (message, unchecked((int)0x80070000) | (int)error);
162                         }
163                 }
164
165                 // directory methods
166
167                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
168                 public extern static bool CreateDirectory (string path, out MonoIOError error);
169
170                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
171                 public extern static bool RemoveDirectory (string path, out MonoIOError error);
172
173                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
174                 public extern static string [] GetFileSystemEntries (string path, string path_with_pattern, int attrs, int mask, out MonoIOError error);
175
176                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
177                 public extern static string GetCurrentDirectory (out MonoIOError error);
178
179                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
180                 public extern static bool SetCurrentDirectory (string path, out MonoIOError error);
181
182                 // file methods
183
184                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
185                 public extern static bool MoveFile (string path, string dest,
186                                                     out MonoIOError error);
187
188                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
189                 public extern static bool CopyFile (string path, string dest,
190                                                     bool overwrite,
191                                                     out MonoIOError error);
192
193                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
194                 public extern static bool DeleteFile (string path,
195                                                       out MonoIOError error);
196
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);
203
204                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
205                 public extern static FileAttributes GetFileAttributes (string path, out MonoIOError error);
206
207                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
208                 public extern static bool SetFileAttributes (string path, FileAttributes attrs, out MonoIOError error);
209
210                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
211                 public extern static MonoFileType GetFileType (IntPtr handle, out MonoIOError error);
212
213                 public static bool Exists (string path, out MonoIOError error)
214                 {
215                         FileAttributes attrs = GetFileAttributes (path,
216                                                                   out error);
217                         if (attrs == InvalidFileAttributes)
218                                 return false;
219
220                         return true;
221                 }
222
223                 public static bool ExistsFile (string path,
224                                                out MonoIOError error)
225                 {
226                         FileAttributes attrs = GetFileAttributes (path,
227                                                                   out error);
228                         if (attrs == InvalidFileAttributes)
229                                 return false;
230
231                         if ((attrs & FileAttributes.Directory) != 0)
232                                 return false;
233
234                         return true;
235                 }
236
237                 public static bool ExistsDirectory (string path,
238                                                     out MonoIOError error)
239                 {
240                         FileAttributes attrs = GetFileAttributes (path,
241                                                                   out error);
242                                                                   
243                         // Actually, we are looking for a directory, not a file
244                         if (error == MonoIOError.ERROR_FILE_NOT_FOUND)
245                                 error = MonoIOError.ERROR_PATH_NOT_FOUND;
246                                 
247                         if (attrs == InvalidFileAttributes)
248                                 return false;
249
250                         if ((attrs & FileAttributes.Directory) == 0)
251                                 return false;
252
253                         return true;
254                 }
255
256                 public static bool ExistsSymlink (string path,
257                                                   out MonoIOError error)
258                 {
259                         FileAttributes attrs = GetFileAttributes (path,
260                                                                   out error);
261                         if (attrs == InvalidFileAttributes)
262                                 return false;
263                         
264                         if ((attrs & FileAttributes.ReparsePoint) == 0)
265                                 return false;
266
267                         return true;
268                 }
269
270                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
271                 public extern static bool GetFileStat (string path,
272                                                        out MonoIOStat stat,
273                                                        out MonoIOError error);
274
275                 // handle methods
276
277                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
278                 public extern static IntPtr Open (string filename,
279                                                   FileMode mode,
280                                                   FileAccess access,
281                                                   FileShare share,
282                                                   FileOptions options,
283                                                   out MonoIOError error);
284                 
285                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
286                 public extern static bool Close (IntPtr handle,
287                                                  out MonoIOError error);
288                 
289                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
290                 public extern static int Read (IntPtr handle, byte [] dest,
291                                                int dest_offset, int count,
292                                                out MonoIOError error);
293                 
294                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
295                 public extern static int Write (IntPtr handle, [In] byte [] src,
296                                                 int src_offset, int count,
297                                                 out MonoIOError error);
298                 
299                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
300                 public extern static long Seek (IntPtr handle, long offset,
301                                                 SeekOrigin origin,
302                                                 out MonoIOError error);
303                 
304                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
305                 public extern static bool Flush (IntPtr handle,
306                                                  out MonoIOError error);
307
308                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
309                 public extern static long GetLength (IntPtr handle,
310                                                      out MonoIOError error);
311
312                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
313                 public extern static bool SetLength (IntPtr handle,
314                                                      long length,
315                                                      out MonoIOError error);
316
317                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
318                 public extern static bool SetFileTime (IntPtr handle,
319                                                        long creation_time,
320                                                        long last_access_time,
321                                                        long last_write_time,
322                                                        out MonoIOError error);
323
324                 public static bool SetFileTime (string path,
325                                                 long creation_time,
326                                                 long last_access_time,
327                                                 long last_write_time,
328                                                 out MonoIOError error)
329                 {
330                         return SetFileTime (path,
331                                 0,
332                                 creation_time,
333                                 last_access_time,
334                                 last_write_time,
335                                 DateTime.MinValue,
336                                 out error);
337                 }
338
339                 public static bool SetCreationTime (string path,
340                                                 DateTime dateTime,
341                                                 out MonoIOError error)
342                 {
343                         return SetFileTime (path, 1, -1, -1, -1, dateTime, out error);
344                 }
345
346                 public static bool SetLastAccessTime (string path,
347                                                 DateTime dateTime,
348                                                 out MonoIOError error)
349                 {
350                         return SetFileTime (path, 2, -1, -1, -1, dateTime, out error);
351                 }
352
353                 public static bool SetLastWriteTime (string path,
354                                                 DateTime dateTime,
355                                                 out MonoIOError error)
356                 {
357                         return SetFileTime (path, 3, -1, -1, -1, dateTime, out error);
358                 }
359
360                 public static bool SetFileTime (string path,
361                                                 int type,
362                                                 long creation_time,
363                                                 long last_access_time,
364                                                 long last_write_time,
365                                                 DateTime dateTime,
366                                                 out MonoIOError error)
367                 {
368                         IntPtr handle;
369                         bool result;
370
371                         handle = Open (path, FileMode.Open,
372                                        FileAccess.ReadWrite,
373                                        FileShare.ReadWrite, FileOptions.None, out error);
374                         if (handle == MonoIO.InvalidHandle)
375                                 return false;
376
377                         switch (type) {
378                         case 1:
379                                 creation_time = dateTime.ToFileTime ();
380                                 break;
381                         case 2:
382                                 last_access_time = dateTime.ToFileTime ();
383                                 break;
384                         case 3:
385                                 last_write_time = dateTime.ToFileTime ();
386                                 break;
387                         }
388
389                         result = SetFileTime (handle, creation_time,
390                                               last_access_time,
391                                               last_write_time, out error);
392
393                         MonoIOError ignore_error;
394                         Close (handle, out ignore_error);
395                         
396                         return result;
397                 }
398
399                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
400                 public extern static void Lock (IntPtr handle,
401                                                 long position, long length,
402                                                 out MonoIOError error);
403
404                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
405                 public extern static void Unlock (IntPtr handle,
406                                                   long position, long length,
407                                                   out MonoIOError error);
408
409                 // console handles
410
411                 public extern static IntPtr ConsoleOutput {
412                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
413                         get;
414                 }
415
416                 public extern static IntPtr ConsoleInput {
417                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
418                         get;
419                 }
420
421                 public extern static IntPtr ConsoleError {
422                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
423                         get;
424                 }
425
426                 // pipe handles
427
428                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
429                 public extern static bool CreatePipe (out IntPtr read_handle, out IntPtr write_handle);
430
431                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
432                 public extern static bool DuplicateHandle (IntPtr source_process_handle, IntPtr source_handle,
433                         IntPtr target_process_handle, out IntPtr target_handle, int access, int inherit, int options);
434
435                 // path characters
436
437                 public extern static char VolumeSeparatorChar {
438                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
439                         get;
440                 }
441
442                 public extern static char DirectorySeparatorChar {
443                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
444                         get;
445                 }
446
447                 public extern static char AltDirectorySeparatorChar {
448                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
449                         get;
450                 }
451
452                 public extern static char PathSeparator {
453                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
454                         get;
455                 }
456
457                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
458                 public extern static int GetTempPath(out string path);
459         }
460 }
461