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