MonoIO.cs 7.6 KB

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