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