New test.
[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
38 namespace System.IO
39 {
40         unsafe internal sealed class MonoIO {
41                 public static readonly FileAttributes
42                         InvalidFileAttributes = (FileAttributes)(-1);
43
44                 public static readonly IntPtr
45                         InvalidHandle = (IntPtr)(-1L);
46
47                 // error methods
48                 public static Exception GetException (MonoIOError error)
49                 {
50                         return GetException (String.Empty, error);
51                 }
52
53                 public static Exception GetException (string path,
54                                                       MonoIOError error)
55                 {
56                         string message;
57
58                         switch (error) {
59                         // FIXME: add more exception mappings here
60                         case MonoIOError.ERROR_FILE_NOT_FOUND:
61                                 message = String.Format ("Could not find file \"{0}\"", path);
62                                 return new FileNotFoundException (message,
63                                                                   path);
64
65                         case MonoIOError.ERROR_TOO_MANY_OPEN_FILES:
66                                 return new IOException ("Too many open files", unchecked((int)0x80070000) | (int)error);
67                                 
68                         case MonoIOError.ERROR_PATH_NOT_FOUND:
69                                 message = String.Format ("Could not find a part of the path \"{0}\"", path);
70                                 return new DirectoryNotFoundException (message);
71
72                         case MonoIOError.ERROR_ACCESS_DENIED:
73                                 message = String.Format ("Access to the path \"{0}\" is denied.", path);
74                                 return new UnauthorizedAccessException (message);
75
76                         case MonoIOError.ERROR_INVALID_HANDLE:
77                                 message = String.Format ("Invalid handle to path \"{0}\"", path);
78                                 return new IOException (message, unchecked((int)0x80070000) | (int)error);
79                         case MonoIOError.ERROR_INVALID_DRIVE:
80                                 message = String.Format ("Could not find the drive  '{0}'. The drive might not be ready or might not be mapped.", path);
81 #if NET_2_0
82                                 return new DriveNotFoundException (message);
83 #else
84                                 return new IOException (message, unchecked((int)0x80070000) | (int)error);
85 #endif
86                         case MonoIOError.ERROR_FILE_EXISTS:
87                                 message = String.Format ("Could not create file \"{0}\". File already exists.", path);
88                                 return new IOException (message, unchecked((int)0x80070000) | (int)error);
89
90                         case MonoIOError.ERROR_FILENAME_EXCED_RANGE:
91                                 message = String.Format ("Path is too long. Path: {0}", path); 
92                                 return new PathTooLongException (message);
93
94                         case MonoIOError.ERROR_INVALID_PARAMETER:
95                                 message = String.Format ("Invalid parameter");
96                                 return new IOException (message, unchecked((int)0x80070000) | (int)error);
97
98                         case MonoIOError.ERROR_WRITE_FAULT:
99                                 message = String.Format ("Write fault on path {0}", path);
100                                 return new IOException (message, unchecked((int)0x80070000) | (int)error);
101
102                         case MonoIOError.ERROR_SHARING_VIOLATION:
103                                 message = String.Format ("Sharing violation on path {0}", path);
104                                 return new IOException (message, unchecked((int)0x80070000) | (int)error);
105                                 
106                         case MonoIOError.ERROR_LOCK_VIOLATION:
107                                 message = String.Format ("Lock violation on path {0}", path);
108                                 return new IOException (message, unchecked((int)0x80070000) | (int)error);
109                         
110                         case MonoIOError.ERROR_HANDLE_DISK_FULL:
111                                 message = String.Format ("Disk full. Path {0}", path);
112                                 return new IOException (message, unchecked((int)0x80070000) | (int)error);
113                         
114                         case MonoIOError.ERROR_DIR_NOT_EMPTY:
115                                 message = String.Format ("Directory {0} is not empty", path);
116                                 return new IOException (message, unchecked((int)0x80070000) | (int)error);
117
118                         case MonoIOError.ERROR_ENCRYPTION_FAILED:
119                                 return new IOException ("Encryption failed", unchecked((int)0x80070000) | (int)error);
120                                 
121                         default:
122                                 message = String.Format ("Win32 IO returned {0}. Path: {1}", error, path);
123                                 return new IOException (message, unchecked((int)0x80070000) | (int)error);
124                         }
125                 }
126
127                 // directory methods
128
129                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
130                 public extern static bool CreateDirectory (string path, out MonoIOError error);
131
132                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
133                 public extern static bool RemoveDirectory (string path, out MonoIOError error);
134
135                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
136                 public extern static string [] GetFileSystemEntries (string path, string pattern, int attrs, int mask, out MonoIOError error);
137
138                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
139                 public extern static string GetCurrentDirectory (out MonoIOError error);
140
141                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
142                 public extern static bool SetCurrentDirectory (string path, out MonoIOError error);
143
144                 // file methods
145
146                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
147                 public extern static bool MoveFile (string path, string dest,
148                                                     out MonoIOError error);
149
150                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
151                 public extern static bool CopyFile (string path, string dest,
152                                                     bool overwrite,
153                                                     out MonoIOError error);
154
155                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
156                 public extern static bool DeleteFile (string path,
157                                                       out MonoIOError error);
158
159                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
160                 public extern static FileAttributes GetFileAttributes (string path, out MonoIOError error);
161
162                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
163                 public extern static bool SetFileAttributes (string path, FileAttributes attrs, out MonoIOError error);
164
165                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
166                 public extern static MonoFileType GetFileType (IntPtr handle, out MonoIOError error);
167
168                 public static bool Exists (string path, out MonoIOError error)
169                 {
170                         FileAttributes attrs = GetFileAttributes (path,
171                                                                   out error);
172                         if (attrs == InvalidFileAttributes)
173                                 return false;
174
175                         return true;
176                 }
177
178                 public static bool ExistsFile (string path,
179                                                out MonoIOError error)
180                 {
181                         FileAttributes attrs = GetFileAttributes (path,
182                                                                   out error);
183                         if (attrs == InvalidFileAttributes)
184                                 return false;
185
186                         if ((attrs & FileAttributes.Directory) != 0)
187                                 return false;
188
189                         return true;
190                 }
191
192                 public static bool ExistsDirectory (string path,
193                                                     out MonoIOError error)
194                 {
195                         FileAttributes attrs = GetFileAttributes (path,
196                                                                   out error);
197                                                                   
198                         // Actually, we are looking for a directory, not a file
199                         if (error == MonoIOError.ERROR_FILE_NOT_FOUND)
200                                 error = MonoIOError.ERROR_PATH_NOT_FOUND;
201                                 
202                         if (attrs == InvalidFileAttributes)
203                                 return false;
204
205                         if ((attrs & FileAttributes.Directory) == 0)
206                                 return false;
207
208                         return true;
209                 }
210
211                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
212                 public extern static bool GetFileStat (string path,
213                                                        out MonoIOStat stat,
214                                                        out MonoIOError error);
215
216                 // handle methods
217
218                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
219                 public extern static IntPtr Open (string filename,
220                                                   FileMode mode,
221                                                   FileAccess access,
222                                                   FileShare share,
223                                                   FileOptions options,
224                                                   out MonoIOError error);
225                 
226                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
227                 public extern static bool Close (IntPtr handle,
228                                                  out MonoIOError error);
229                 
230                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
231                 public extern static int Read (IntPtr handle, byte [] dest,
232                                                int dest_offset, int count,
233                                                out MonoIOError error);
234                 
235                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
236                 public extern static int Write (IntPtr handle, [In] byte [] src,
237                                                 int src_offset, int count,
238                                                 out MonoIOError error);
239                 
240                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
241                 public extern static long Seek (IntPtr handle, long offset,
242                                                 SeekOrigin origin,
243                                                 out MonoIOError error);
244                 
245                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
246                 public extern static bool Flush (IntPtr handle,
247                                                  out MonoIOError error);
248
249                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
250                 public extern static long GetLength (IntPtr handle,
251                                                      out MonoIOError error);
252
253                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
254                 public extern static bool SetLength (IntPtr handle,
255                                                      long length,
256                                                      out MonoIOError error);
257
258                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
259                 public extern static bool SetFileTime (IntPtr handle,
260                                                        long creation_time,
261                                                        long last_access_time,
262                                                        long last_write_time,
263                                                        out MonoIOError error);
264
265                 public static bool SetFileTime (string path,
266                                                 long creation_time,
267                                                 long last_access_time,
268                                                 long last_write_time,
269                                                 out MonoIOError error)
270                 {
271                         return SetFileTime (path,
272                                 0,
273                                 creation_time,
274                                 last_access_time,
275                                 last_write_time,
276                                 DateTime.MinValue,
277                                 out error);
278                 }
279
280                 public static bool SetCreationTime (string path,
281                                                 DateTime dateTime,
282                                                 out MonoIOError error)
283                 {
284                         return SetFileTime (path, 1, -1, -1, -1, dateTime, out error);
285                 }
286
287                 public static bool SetLastAccessTime (string path,
288                                                 DateTime dateTime,
289                                                 out MonoIOError error)
290                 {
291                         return SetFileTime (path, 2, -1, -1, -1, dateTime, out error);
292                 }
293
294                 public static bool SetLastWriteTime (string path,
295                                                 DateTime dateTime,
296                                                 out MonoIOError error)
297                 {
298                         return SetFileTime (path, 3, -1, -1, -1, dateTime, out error);
299                 }
300
301                 public static bool SetFileTime (string path,
302                                                 int type,
303                                                 long creation_time,
304                                                 long last_access_time,
305                                                 long last_write_time,
306                                                 DateTime dateTime,
307                                                 out MonoIOError error)
308                 {
309                         IntPtr handle;
310                         bool result;
311
312                         handle = Open (path, FileMode.Open,
313                                        FileAccess.ReadWrite,
314                                        FileShare.ReadWrite, FileOptions.None, out error);
315                         if (handle == MonoIO.InvalidHandle)
316                                 return false;
317
318                         switch (type) {
319                         case 1:
320                                 creation_time = dateTime.ToFileTime ();
321                                 break;
322                         case 2:
323                                 last_access_time = dateTime.ToFileTime ();
324                                 break;
325                         case 3:
326                                 last_write_time = dateTime.ToFileTime ();
327                                 break;
328                         }
329
330                         result = SetFileTime (handle, creation_time,
331                                               last_access_time,
332                                               last_write_time, out error);
333
334                         MonoIOError ignore_error;
335                         Close (handle, out ignore_error);
336                         
337                         return result;
338                 }
339
340                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
341                 public extern static void Lock (IntPtr handle,
342                                                 long position, long length,
343                                                 out MonoIOError error);
344
345                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
346                 public extern static void Unlock (IntPtr handle,
347                                                   long position, long length,
348                                                   out MonoIOError error);
349
350                 // console handles
351
352                 public extern static IntPtr ConsoleOutput {
353                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
354                         get;
355                 }
356
357                 public extern static IntPtr ConsoleInput {
358                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
359                         get;
360                 }
361
362                 public extern static IntPtr ConsoleError {
363                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
364                         get;
365                 }
366
367                 // pipe handles
368
369                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
370                 public extern static bool CreatePipe (out IntPtr read_handle, out IntPtr write_handle);
371
372                 // path characters
373
374                 public extern static char VolumeSeparatorChar {
375                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
376                         get;
377                 }
378
379                 public extern static char DirectorySeparatorChar {
380                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
381                         get;
382                 }
383
384                 public extern static char AltDirectorySeparatorChar {
385                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
386                         get;
387                 }
388
389                 public extern static char PathSeparator {
390                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
391                         get;
392                 }
393
394                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
395                 public extern static int GetTempPath(out string path);
396         }
397 }
398