Linux.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /*---------------------------------------------------------------------
  2. XX X XXX
  3. XX XX
  4. XXX XX XXX XXXXX XX
  5. XX XXX XX XX XX
  6. XX XX XX XX XXXXX XX
  7. XX XX XX XX XX XX X XX
  8. XXXX XX XX XXX XXXXXXX XXXX
  9. XX
  10. XXXXX
  11. Copyright (c) 2001 Intel Corporation. All Rights Reserved.
  12. CREATED: August 22, 2001
  13. OWNER: Scott D Smith, Joel Marcey
  14. VERSION: 1.0
  15. ---------------------------------------------------------------------*/
  16. using System;
  17. using System.Runtime.InteropServices;
  18. using System.Text;
  19. using System.IO;
  20. using System.Collections;
  21. using System.Reflection;
  22. using System.Runtime.CompilerServices;
  23. namespace System.PAL
  24. {
  25. /// <summary>
  26. /// Class that implements IOperatingSystem, providing the requested functionality through calls into APIs available in Linux
  27. /// </summary>
  28. internal class OpSys
  29. {
  30. private Hashtable _environment = null;
  31. //----------------------------------
  32. // Class Constants
  33. //----------------------------------
  34. private const int EOF = -1; // TODO: Linux: Is this true?
  35. // For StdInputStream and StdOutputStream
  36. private IntPtr Stdin;
  37. private IntPtr Stdout;
  38. private IntPtr Stderr;
  39. //----------------------------------
  40. // Class Fields
  41. //----------------------------------
  42. //----------------------------------
  43. // Class Constructor
  44. //----------------------------------
  45. public OpSys()
  46. {
  47. Stdin=GetStdHandle(0);
  48. Stdout=GetStdHandle(1);
  49. Stderr=GetStdHandle(2);
  50. }
  51. //-------------------------------------------------
  52. // Environment Services
  53. //-------------------------------------------------
  54. public string NewLineSequence
  55. {
  56. get
  57. {
  58. return "\n";
  59. }
  60. }
  61. public char DirectorySeparator
  62. {
  63. get
  64. {
  65. return '/';
  66. }
  67. }
  68. public char AltDirectorySeparator
  69. {
  70. get
  71. {
  72. return '\\';
  73. }
  74. }
  75. public char VolumeSeparator
  76. {
  77. get
  78. {
  79. return '/';
  80. }
  81. }
  82. public char PathSeparator
  83. {
  84. get
  85. {
  86. return ':';
  87. }
  88. }
  89. public char[] InvalidPathChars
  90. {
  91. get
  92. {
  93. return new char[] { '\0' };
  94. }
  95. }
  96. public char[] DirVolSeparatorChars
  97. {
  98. get
  99. {
  100. return new char[] { this.DirectorySeparator, this.AltDirectorySeparator, this.VolumeSeparator};
  101. }
  102. }
  103. public char ExtensionCharacter
  104. {
  105. get
  106. {
  107. return '.';
  108. }
  109. }
  110. public string GetEnvironmentVariable(string eVar)
  111. {
  112. return EnvironmentVariables[eVar].ToString();
  113. }
  114. public IDictionary EnvironmentVariables
  115. {
  116. get
  117. {
  118. if (_environment == null) {
  119. IntPtr pp = _getEnviron(); // pointer to an array of char*
  120. _environment = new Hashtable();
  121. if (pp != IntPtr.Zero) {
  122. IntPtr p;
  123. bool done = false;
  124. char[] delimiter = { '=' };
  125. while (!done)
  126. {
  127. p = Marshal.ReadIntPtr(pp);
  128. if (p != IntPtr.Zero)
  129. {
  130. string str = Marshal.PtrToStringAuto(p);
  131. string[] ar = str.Split(delimiter, 2);
  132. switch(ar.Length)
  133. {
  134. case 1:
  135. _environment.Add(ar[0], "");
  136. break;
  137. case 2:
  138. _environment.Add(ar[0], ar[1]);
  139. break;
  140. default:
  141. //System.Diagnostics/.Debug.Assert(false); // this shouldn't happen
  142. break;
  143. }
  144. }
  145. else
  146. {
  147. done = true;
  148. }
  149. }
  150. }
  151. }
  152. return _environment;
  153. }
  154. }
  155. public string CommandLine
  156. {
  157. get
  158. {
  159. string path = Path.Combine(Path.Combine("/proc", _getPid().ToString()), "cmdline");
  160. StreamReader stream = File.OpenText(path);
  161. string res = stream.ReadToEnd();
  162. stream.Close();
  163. return res;
  164. }
  165. }
  166. public string MachineName
  167. {
  168. get
  169. {
  170. return GetEnvironmentVariable("HOSTNAME");
  171. }
  172. }
  173. public OperatingSystem OSVersion
  174. {
  175. get
  176. {
  177. return null;
  178. }
  179. }
  180. // System.Path services
  181. public string ChangeExtension(string path, string extension)
  182. {
  183. //System.Diagnostics/.Debug.WriteLine("Linux:ChangeExtension(System.String, System.String): Stub Method");
  184. return null;
  185. }
  186. public string GetExtension(string path)
  187. {
  188. //System.Diagnostics/.Debug.WriteLine("Linux:GetExtension(System.String): Stub Method");
  189. return null;
  190. }
  191. public string GetFileName(string path)
  192. {
  193. //System.Diagnostics/.Debug.WriteLine("Linux:GetFileName(System.String): Stub Method");
  194. return null;
  195. }
  196. public string GetFileNameWithoutExtension(string path)
  197. {
  198. //System.Diagnostics/.Debug.WriteLine("Linux:GetFileNameWithoutExtension(System.String): Stub Method");
  199. return null;
  200. }
  201. public string GetFullPath(string path)
  202. {
  203. //System.Diagnostics/.Debug.WriteLine("Linux:GetFullPath(System.String): Stub Method");
  204. return null;
  205. }
  206. public string GetPathRoot(string path)
  207. {
  208. //System.Diagnostics/.Debug.WriteLine("Linux:GetPathRoot(System.String): Stub Method");
  209. return null;
  210. }
  211. public string GetTempFileName()
  212. {
  213. //System.Diagnostics/.Debug.WriteLine("Linux:GetTempFileName(): Stub Method");
  214. return null;
  215. }
  216. public string GetTempPath()
  217. {
  218. //System.Diagnostics/.Debug.WriteLine("Linux:GetTempPath(): Stub Method");
  219. return null;
  220. }
  221. public bool HasExtension(string path)
  222. {
  223. //System.Diagnostics/.Debug.WriteLine("Linux:HasExtension(System.String): Stub Method");
  224. return false;
  225. }
  226. public bool IsPathRooted(string path)
  227. {
  228. //System.Diagnostics/.Debug.WriteLine("Linux:IsPathRooted(System.String): Stub Method");
  229. return false;
  230. }
  231. // System.Directory services
  232. public void DeleteDirectory(string path, bool recursive)
  233. {
  234. //System.Diagnostics/.Debug.WriteLine("Linux:DeleteDirectory(System.String, System.Boolean): Stub Method");
  235. }
  236. public bool ExistsDirectory(string path)
  237. {
  238. //System.Diagnostics/.Debug.WriteLine("Linux:ExistsDirectory(System.String): Stub Method");
  239. return false;
  240. }
  241. public DateTime GetCreationTimeDirectory(string path)
  242. {
  243. //System.Diagnostics/.Debug.WriteLine("Linux:GetCreationTimeDirectory(System.String): Stub Method");
  244. return new DateTime(0);
  245. }
  246. [System.Runtime.CompilerServices.MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
  247. public extern string GetCurrentDirectory();
  248. public string[] GetDirectories(string path, string searchPattern)
  249. {
  250. //System.Diagnostics/.Debug.WriteLine("Linux:GetDirectories(System.String,System.String): Stub Method");
  251. return null;
  252. }
  253. public string[] GetFiles(string path, string searchPattern)
  254. {
  255. //System.Diagnostics/.Debug.WriteLine("Linux:GetFiles(System.String, System.String): Stub Method");
  256. return null;
  257. }
  258. public string[] GetFileSystemEntries(string path, string searchPattern)
  259. {
  260. //System.Diagnostics/.Debug.WriteLine("Linux:GetFileSystemEntries(System.String, System.String): Stub Method");
  261. return null;
  262. }
  263. public DateTime GetLastAccessTimeDirectory(string path)
  264. {
  265. //System.Diagnostics/.Debug.WriteLine("Linux:GetLastAccessTimeDirectory(System.String): Stub Method");
  266. return new DateTime(0);
  267. }
  268. public DateTime GetLastWriteTimeDirectory(string path)
  269. {
  270. //System.Diagnostics/.Debug.WriteLine("Linux:GetLastWriteTimeDirectory(System.String): Stub Method");
  271. return new DateTime(0);
  272. }
  273. public void MoveDirectory(string sourceDirName, string destDirName)
  274. {
  275. //System.Diagnostics/.Debug.WriteLine("Linux:MoveDirectory(System.String, System.String): Stub Method");
  276. }
  277. public void SetCreationTimeDirectory(string path, DateTime creationTime)
  278. {
  279. //System.Diagnostics/.Debug.WriteLine("Linux:SetCreationTimeDirectory(System.String, System.DateTime): Stub Method");
  280. }
  281. public void SetCurrentDirectory(string path)
  282. {
  283. //System.Diagnostics/.Debug.WriteLine("Linux:SetCurrentDirectory(System.String): Stub Method");
  284. }
  285. public void SetLastAccessTimeDirectory(string path, DateTime lastAccessTime)
  286. {
  287. //System.Diagnostics/.Debug.WriteLine("Linux:SetLastAccessTimeDirectory(System.String, System.DateTime): Stub Method");
  288. }
  289. public void SetLastWriteTimeDirectory(string path, DateTime lastWriteTime)
  290. {
  291. //System.Diagnostics/.Debug.WriteLine("Linux:SetLastWriteTimeDirectory(System.String, System.DateTime): Stub Method");
  292. }
  293. //-----------------------------------
  294. // I/O Services
  295. //-----------------------------------
  296. [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
  297. private extern IntPtr GetStdHandle(int fd);
  298. public IntPtr StdinHandle {
  299. get {
  300. return(Stdin);
  301. }
  302. }
  303. public IntPtr StdoutHandle {
  304. get {
  305. return(Stdout);
  306. }
  307. }
  308. public IntPtr StderrHandle {
  309. get {
  310. return(Stderr);
  311. }
  312. }
  313. [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
  314. public extern void DeleteFile(string path);
  315. [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
  316. public extern bool ExistsFile(string path);
  317. /* The long time parameters in GetFileTime and
  318. * SetFileTime correspond to Windows file times (ticks
  319. * from DateTime(1/1/1601 00:00 GMT))
  320. */
  321. [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
  322. private extern static bool GetFileTime(IntPtr handle, out long creat, out long lastaccess, out long lastwrite);
  323. [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
  324. private extern static bool SetFileTime(IntPtr handle, long creat, long lastaccess, long lastwrite);
  325. public DateTime GetCreationTimeFile(string path)
  326. {
  327. long creat, lastaccess, lastwrite;
  328. bool ret;
  329. FileStream s = new FileStream(path, FileMode.Open, FileAccess.Read);
  330. ret=GetFileTime(s.Handle, out creat, out lastaccess, out lastwrite);
  331. s.Close();
  332. return DateTime.FromFileTime(creat);
  333. }
  334. public DateTime GetLastAccessTimeFile(string path)
  335. {
  336. long creat, lastaccess, lastwrite;
  337. bool ret;
  338. FileStream s = new FileStream(path, FileMode.Open, FileAccess.Read);
  339. ret=GetFileTime(s.Handle, out creat, out lastaccess, out lastwrite);
  340. s.Close();
  341. return DateTime.FromFileTime(lastaccess);
  342. }
  343. public DateTime GetLastWriteTimeFile(string path)
  344. {
  345. long creat, lastaccess, lastwrite;
  346. bool ret;
  347. FileStream s = new FileStream(path, FileMode.Open, FileAccess.Read);
  348. ret=GetFileTime(s.Handle, out creat, out lastaccess, out lastwrite);
  349. s.Close();
  350. return DateTime.FromFileTime(lastwrite);
  351. }
  352. public void SetCreationTimeFile(string path, DateTime creationTime)
  353. {
  354. long creat, lastaccess, lastwrite;
  355. bool ret;
  356. FileStream s = new FileStream(path, FileMode.Open, FileAccess.ReadWrite);
  357. // Get the existing times first
  358. ret=GetFileTime(s.Handle, out creat, out lastaccess, out lastwrite);
  359. creat=creationTime.ToFileTime();
  360. ret=SetFileTime(s.Handle, creat, lastaccess, lastwrite);
  361. s.Close();
  362. }
  363. public void SetLastAccessTimeFile(string path, DateTime lastAccessTime)
  364. {
  365. long creat, lastaccess, lastwrite;
  366. bool ret;
  367. FileStream s = new FileStream(path, FileMode.Open, FileAccess.ReadWrite);
  368. // Get the existing times first
  369. ret=GetFileTime(s.Handle, out creat, out lastaccess, out lastwrite);
  370. lastaccess=lastAccessTime.ToFileTime();
  371. ret=SetFileTime(s.Handle, creat, lastaccess, lastwrite);
  372. s.Close();
  373. }
  374. public void SetLastWriteTimeFile(string path, DateTime lastWriteTime)
  375. {
  376. long creat, lastaccess, lastwrite;
  377. bool ret;
  378. FileStream s = new FileStream(path, FileMode.Open, FileAccess.ReadWrite);
  379. // Get the existing times first
  380. ret=GetFileTime(s.Handle, out creat, out lastaccess, out lastwrite);
  381. lastwrite=lastWriteTime.ToFileTime();
  382. ret=SetFileTime(s.Handle, creat, lastaccess, lastwrite);
  383. s.Close();
  384. }
  385. public long FileLength(string path)
  386. {
  387. return 0;
  388. }
  389. // Private implementation details
  390. [DllImport("monowrapper", EntryPoint="mono_wrapper_environ", CharSet=CharSet.Ansi)]
  391. private unsafe static extern IntPtr _getEnviron();
  392. [DllImport("libc", EntryPoint="getpid")]
  393. private unsafe static extern int _getPid();
  394. }
  395. }