2003-11-14 Ben Maurer <bmaurer@users.sourceforge.net>
[mono.git] / mcs / class / corlib / System.IO / MonoIO.cs
1 //
2 // System.IO.MonoIO.cs: static interface to native filesystem.
3 //
4 // Author:
5 //   Dan Lewis (dihlewis@yahoo.co.uk)
6 //   Dick Porter (dick@ximian.com)
7 //
8 // (C) 2002
9 //
10
11 using System;
12 using System.Runtime.CompilerServices;
13
14 namespace System.IO
15 {
16         internal sealed class MonoIO {
17                 public static readonly FileAttributes
18                         InvalidFileAttributes = (FileAttributes)(-1);
19
20                 public static readonly IntPtr
21                         InvalidHandle = (IntPtr)(-1L);
22
23                 // error methods
24
25                 public static Exception GetException (MonoIOError error)
26                 {
27                         return GetException (String.Empty, error);
28                 }
29
30                 public static Exception GetException (string path,
31                                                       MonoIOError error)
32                 {
33                         string message;
34
35                         switch (error) {
36                         // FIXME: add more exception mappings here
37                         case MonoIOError.ERROR_FILE_NOT_FOUND:
38                                 message = String.Format ("Could not find file \"{0}\"", path);
39                                 return new FileNotFoundException (message);
40
41                         case MonoIOError.ERROR_PATH_NOT_FOUND:
42                                 message = String.Format ("Could not find a part of the path \"{0}\"", path);
43                                 return new DirectoryNotFoundException (message);
44
45                         case MonoIOError.ERROR_ACCESS_DENIED:
46                                 message = String.Format ("Access to the path \"{0}\" is denied.", path);
47                                 return new UnauthorizedAccessException (message);
48
49                         default:
50                                 message = String.Format ("Win32 IO returned {0}", error);
51                                 return new IOException (message);
52                         }
53                 }
54
55                 // directory methods
56
57                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
58                 public extern static bool CreateDirectory (string path, out MonoIOError error);
59
60                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
61                 public extern static bool RemoveDirectory (string path, out MonoIOError error);
62
63                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
64                 public extern static IntPtr FindFirstFile (string path, out MonoIOStat stat, out MonoIOError error);
65
66                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
67                 public extern static bool FindNextFile (IntPtr find, out MonoIOStat stat, out MonoIOError error);
68
69                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
70                 public extern static bool FindClose (IntPtr find,
71                                                      out MonoIOError error);
72
73                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
74                 public extern static string GetCurrentDirectory (out MonoIOError error);
75
76                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
77                 public extern static bool SetCurrentDirectory (string path, out MonoIOError error);
78
79                 // file methods
80
81                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
82                 public extern static bool MoveFile (string path, string dest,
83                                                     out MonoIOError error);
84
85                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
86                 public extern static bool CopyFile (string path, string dest,
87                                                     bool overwrite,
88                                                     out MonoIOError error);
89
90                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
91                 public extern static bool DeleteFile (string path,
92                                                       out MonoIOError error);
93
94                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
95                 public extern static FileAttributes GetFileAttributes (string path, out MonoIOError error);
96
97                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
98                 public extern static bool SetFileAttributes (string path, FileAttributes attrs, out MonoIOError error);
99
100                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
101                 public extern static MonoFileType GetFileType (IntPtr handle, out MonoIOError error);
102
103                 public static bool Exists (string path, out MonoIOError error)
104                 {
105                         FileAttributes attrs = GetFileAttributes (path,
106                                                                   out error);
107                         if (attrs == InvalidFileAttributes)
108                                 return false;
109
110                         return true;
111                 }
112
113                 public static bool ExistsFile (string path,
114                                                out MonoIOError error)
115                 {
116                         FileAttributes attrs = GetFileAttributes (path,
117                                                                   out error);
118                         if (attrs == InvalidFileAttributes)
119                                 return false;
120
121                         if ((attrs & FileAttributes.Directory) != 0)
122                                 return false;
123
124                         return true;
125                 }
126
127                 public static bool ExistsDirectory (string path,
128                                                     out MonoIOError error)
129                 {
130                         FileAttributes attrs = GetFileAttributes (path,
131                                                                   out error);
132                         if (attrs == InvalidFileAttributes)
133                                 return false;
134
135                         if ((attrs & FileAttributes.Directory) == 0)
136                                 return false;
137
138                         return true;
139                 }
140
141                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
142                 public extern static bool GetFileStat (string path,
143                                                        out MonoIOStat stat,
144                                                        out MonoIOError error);
145
146                 // handle methods
147
148                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
149                 public extern static IntPtr Open (string filename,
150                                                   FileMode mode,
151                                                   FileAccess access,
152                                                   FileShare share,
153                                                   out MonoIOError error);
154                 
155                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
156                 public extern static bool Close (IntPtr handle,
157                                                  out MonoIOError error);
158                 
159                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
160                 public extern static int Read (IntPtr handle, byte [] dest,
161                                                int dest_offset, int count,
162                                                out MonoIOError error);
163                 
164                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
165                 public extern static int Write (IntPtr handle, byte [] src,
166                                                 int src_offset, int count,
167                                                 out MonoIOError error);
168                 
169                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
170                 public extern static long Seek (IntPtr handle, long offset,
171                                                 SeekOrigin origin,
172                                                 out MonoIOError error);
173                 
174                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
175                 public extern static bool Flush (IntPtr handle,
176                                                  out MonoIOError error);
177
178                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
179                 public extern static long GetLength (IntPtr handle,
180                                                      out MonoIOError error);
181
182                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
183                 public extern static bool SetLength (IntPtr handle,
184                                                      long length,
185                                                      out MonoIOError error);
186
187                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
188                 public extern static bool SetFileTime (IntPtr handle,
189                                                        long creation_time,
190                                                        long last_access_time,
191                                                        long last_write_time,
192                                                        out MonoIOError error);
193
194                 public static bool SetFileTime (string path,
195                                                 long creation_time,
196                                                 long last_access_time,
197                                                 long last_write_time,
198                                                 out MonoIOError error)
199                 {
200                         IntPtr handle;
201                         bool result;
202
203                         handle = Open (path, FileMode.Open,
204                                        FileAccess.ReadWrite,
205                                        FileShare.ReadWrite, out error);
206                         if (handle == IntPtr.Zero)
207                                 return false;
208
209                         result = SetFileTime (handle, creation_time,
210                                               last_access_time,
211                                               last_write_time, out error);
212                         Close (handle, out error);
213                         
214                         return result;
215                 }
216
217                 // console handles
218
219                 public extern static IntPtr ConsoleOutput {
220                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
221                         get;
222                 }
223
224                 public extern static IntPtr ConsoleInput {
225                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
226                         get;
227                 }
228
229                 public extern static IntPtr ConsoleError {
230                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
231                         get;
232                 }
233
234                 // pipe handles
235
236                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
237                 public extern static bool CreatePipe (out IntPtr read_handle, out IntPtr write_handle);
238
239                 // path characters
240
241                 public extern static char VolumeSeparatorChar {
242                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
243                         get;
244                 }
245
246                 public extern static char DirectorySeparatorChar {
247                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
248                         get;
249                 }
250
251                 public extern static char AltDirectorySeparatorChar {
252                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
253                         get;
254                 }
255
256                 public extern static char PathSeparator {
257                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
258                         get;
259                 }
260
261                 public extern static char [] InvalidPathChars {
262                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
263                         get;
264                 }
265
266                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
267                 public extern static int GetTempPath(out string path);
268         }
269 }
270