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