Linux.cs 13 KB

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