2002-09-26 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[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)(-1);
22
23                 // error methods
24
25                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
26                 public extern static MonoIOError GetLastError ();
27
28                 public static Exception GetException ()
29                 {
30                         return GetException (String.Empty);
31                 }
32
33                 public static Exception GetException (string path)
34                 {
35                         MonoIOError error = GetLastError ();
36                         string message;
37
38                         switch (error) {
39                         // FIXME: add more exception mappings here
40                         case MonoIOError.ERROR_FILE_NOT_FOUND:
41                                 message = String.Format ("Could not find file \"{0}\"", path);
42                                 return new FileNotFoundException (message);
43
44                         case MonoIOError.ERROR_PATH_NOT_FOUND:
45                                 message = String.Format ("Could not find a part of the path \"{0}\"", path);
46                                 return new DirectoryNotFoundException (message);
47
48                         case MonoIOError.ERROR_ACCESS_DENIED:
49                                 message = String.Format ("Access to the path \"{0}\" is denied.", path);
50                                 return new UnauthorizedAccessException (message);
51
52                         default:
53                                 message = String.Format ("Win32 IO returned {0}", error);
54                                 return new IOException (message);
55                         }
56                 }
57
58                 // directory methods
59
60                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
61                 public extern static bool CreateDirectory (string path);
62
63                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
64                 public extern static bool RemoveDirectory (string path);
65
66                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
67                 public extern static IntPtr FindFirstFile (string path, out MonoIOStat stat);
68
69                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
70                 public extern static bool FindNextFile (IntPtr find, out MonoIOStat stat);
71
72                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
73                 public extern static bool FindClose (IntPtr find);
74
75                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
76                 public extern static string GetCurrentDirectory ();
77
78                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
79                 public extern static bool SetCurrentDirectory (string path);
80
81                 // file methods
82
83                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
84                 public extern static bool MoveFile (string path, string dest);
85
86                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
87                 public extern static bool CopyFile (string path, string dest, bool overwrite);
88
89                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
90                 public extern static bool DeleteFile (string path);
91
92                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
93                 public extern static FileAttributes GetFileAttributes (string path);
94
95                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
96                 public extern static bool SetFileAttributes (string path, FileAttributes attrs);
97
98                 public static bool Exists (string path)
99                 {
100                         FileAttributes attrs = GetFileAttributes (path);
101                         if (attrs == InvalidFileAttributes)
102                                 return false;
103
104                         return true;
105                 }
106
107                 public static bool ExistsFile (string path)
108                 {
109                         FileAttributes attrs = GetFileAttributes (path);
110                         if (attrs == InvalidFileAttributes)
111                                 return false;
112
113                         if ((attrs & FileAttributes.Directory) != 0)
114                                 return false;
115
116                         return true;
117                 }
118
119                 public static bool ExistsDirectory (string path)
120                 {
121                         FileAttributes attrs = GetFileAttributes (path);
122                         if (attrs == InvalidFileAttributes)
123                                 return false;
124
125                         if ((attrs & FileAttributes.Directory) == 0)
126                                 return false;
127
128                         return true;
129                 }
130
131                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
132                 public extern static bool GetFileStat (string path, out MonoIOStat stat);
133
134                 // handle methods
135
136                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
137                 public extern static IntPtr Open (string filename, FileMode mode, FileAccess access, FileShare share);
138                 
139                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
140                 public extern static bool Close (IntPtr handle);
141                 
142                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
143                 public extern static int Read (IntPtr handle, byte [] dest, int dest_offset, int count);
144                 
145                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
146                 public extern static int Write (IntPtr handle, byte [] src, int src_offset, int count);
147                 
148                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
149                 public extern static long Seek (IntPtr handle, long offset, SeekOrigin origin);
150                 
151                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
152                 public extern static bool Flush (IntPtr handle);
153
154                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
155                 public extern static long GetLength (IntPtr handle);
156
157                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
158                 public extern static bool SetLength (IntPtr handle, long length);
159
160                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
161                 public extern static bool SetFileTime (IntPtr handle, long creation_time, long last_access_time, long last_write_time);
162
163                 public static bool SetFileTime (string path, long creation_time, long last_access_time, long last_write_time)
164                 {
165                         IntPtr handle;
166                         bool result;
167
168                         handle = Open (path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
169                         if (handle == IntPtr.Zero)
170                                 return false;
171
172                         result = SetFileTime (handle, creation_time, last_access_time, last_write_time);
173                         Close (handle);
174                         
175                         return result;
176                 }
177
178                 // console handles
179
180                 public extern static IntPtr ConsoleOutput {
181                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
182                         get;
183                 }
184
185                 public extern static IntPtr ConsoleInput {
186                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
187                         get;
188                 }
189
190                 public extern static IntPtr ConsoleError {
191                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
192                         get;
193                 }
194
195                 // pipe handles
196
197                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
198                 public extern static bool CreatePipe (out IntPtr read_handle, out IntPtr write_handle);
199
200                 // path characters
201
202                 public extern static char VolumeSeparatorChar {
203                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
204                         get;
205                 }
206
207                 public extern static char DirectorySeparatorChar {
208                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
209                         get;
210                 }
211
212                 public extern static char AltDirectorySeparatorChar {
213                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
214                         get;
215                 }
216
217                 public extern static char PathSeparator {
218                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
219                         get;
220                 }
221
222                 public extern static char [] InvalidPathChars {
223                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
224                         get;
225                 }
226         }
227 }
228