3 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
5 // Permission is hereby granted, free of charge, to any person obtaining
6 // a copy of this software and associated documentation files (the
7 // "Software"), to deal in the Software without restriction, including
8 // without limitation the rights to use, copy, modify, merge, publish,
9 // distribute, sublicense, and/or sell copies of the Software, and to
10 // permit persons to whom the Software is furnished to do so, subject to
11 // the following conditions:
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 /*---------------------------------------------------------------------
31 XX XX XX XX XX XX X XX
32 XXXX XX XX XXX XXXXXXX XXXX
36 Copyright (c) 2001 Intel Corporation. All Rights Reserved.
38 CREATED: August 22, 2001
39 OWNER: Scott D Smith, Joel Marcey
41 ---------------------------------------------------------------------*/
44 using System
.Runtime
.InteropServices
;
47 using System
.Collections
;
48 using System
.Reflection
;
49 using System
.Runtime
.CompilerServices
;
54 /// Class that implements IOperatingSystem, providing the requested functionality through calls into APIs available in Linux
58 private Hashtable _environment
= null;
60 //----------------------------------
62 //----------------------------------
63 private const int EOF
= -1; // TODO: Linux: Is this true?
66 // For StdInputStream and StdOutputStream
68 private IntPtr Stdout
;
69 private IntPtr Stderr
;
71 //----------------------------------
73 //----------------------------------
75 //----------------------------------
77 //----------------------------------
80 Stdin
=GetStdHandle(0);
81 Stdout
=GetStdHandle(1);
82 Stderr
=GetStdHandle(2);
86 //-------------------------------------------------
87 // Environment Services
88 //-------------------------------------------------
90 public string NewLineSequence
98 public char DirectorySeparator
106 public char AltDirectorySeparator
114 public char VolumeSeparator
122 public char PathSeparator
130 public char[] InvalidPathChars
134 return new char[] { '\0' }
;
138 public char[] DirVolSeparatorChars
142 return new char[] { this.DirectorySeparator, this.AltDirectorySeparator, this.VolumeSeparator}
;
145 public char ExtensionCharacter
153 public string GetEnvironmentVariable(string eVar
)
155 return EnvironmentVariables
[eVar
].ToString();
158 public IDictionary EnvironmentVariables
162 if (_environment
== null) {
163 IntPtr pp
= _getEnviron(); // pointer to an array of char*
164 _environment
= new Hashtable();
166 if (pp
!= IntPtr
.Zero
) {
169 char[] delimiter
= { '=' }
;
173 p
= Marshal
.ReadIntPtr(pp
);
174 if (p
!= IntPtr
.Zero
)
176 string str
= Marshal
.PtrToStringAuto(p
);
177 string[] ar
= str
.Split(delimiter
, 2);
181 _environment
.Add(ar
[0], "");
184 _environment
.Add(ar
[0], ar
[1]);
187 //System.Diagnostics/.Debug.Assert(false); // this shouldn't happen
202 public string CommandLine
206 string path
= Path
.Combine(Path
.Combine("/proc", _getPid().ToString()), "cmdline");
207 StreamReader stream
= File
.OpenText(path
);
208 string res
= stream
.ReadToEnd();
214 public string MachineName
218 return GetEnvironmentVariable("HOSTNAME");
222 public OperatingSystem OSVersion
230 // System.Path services
232 public string ChangeExtension(string path
, string extension
)
234 //System.Diagnostics/.Debug.WriteLine("Linux:ChangeExtension(System.String, System.String): Stub Method");
238 public string GetExtension(string path
)
240 //System.Diagnostics/.Debug.WriteLine("Linux:GetExtension(System.String): Stub Method");
244 public string GetFileName(string path
)
246 //System.Diagnostics/.Debug.WriteLine("Linux:GetFileName(System.String): Stub Method");
250 public string GetFileNameWithoutExtension(string path
)
252 //System.Diagnostics/.Debug.WriteLine("Linux:GetFileNameWithoutExtension(System.String): Stub Method");
256 public string GetFullPath(string path
)
258 //System.Diagnostics/.Debug.WriteLine("Linux:GetFullPath(System.String): Stub Method");
262 public string GetPathRoot(string path
)
264 //System.Diagnostics/.Debug.WriteLine("Linux:GetPathRoot(System.String): Stub Method");
269 public string GetTempFileName()
271 //System.Diagnostics/.Debug.WriteLine("Linux:GetTempFileName(): Stub Method");
275 public string GetTempPath()
277 //System.Diagnostics/.Debug.WriteLine("Linux:GetTempPath(): Stub Method");
281 public bool HasExtension(string path
)
283 //System.Diagnostics/.Debug.WriteLine("Linux:HasExtension(System.String): Stub Method");
287 public bool IsPathRooted(string path
)
289 //System.Diagnostics/.Debug.WriteLine("Linux:IsPathRooted(System.String): Stub Method");
295 // System.Directory services
297 public void DeleteDirectory(string path
, bool recursive
)
299 //System.Diagnostics/.Debug.WriteLine("Linux:DeleteDirectory(System.String, System.Boolean): Stub Method");
302 public bool ExistsDirectory(string path
)
304 //System.Diagnostics/.Debug.WriteLine("Linux:ExistsDirectory(System.String): Stub Method");
308 public DateTime
GetCreationTimeDirectory(string path
)
310 //System.Diagnostics/.Debug.WriteLine("Linux:GetCreationTimeDirectory(System.String): Stub Method");
311 return new DateTime(0);
314 [System
.Runtime
.CompilerServices
.MethodImplAttribute(System
.Runtime
.CompilerServices
.MethodImplOptions
.InternalCall
)]
315 public extern string GetCurrentDirectory();
317 public string[] GetDirectories(string path
, string searchPattern
)
319 //System.Diagnostics/.Debug.WriteLine("Linux:GetDirectories(System.String,System.String): Stub Method");
323 public string[] GetFiles(string path
, string searchPattern
)
325 //System.Diagnostics/.Debug.WriteLine("Linux:GetFiles(System.String, System.String): Stub Method");
329 public string[] GetFileSystemEntries(string path
, string searchPattern
)
331 //System.Diagnostics/.Debug.WriteLine("Linux:GetFileSystemEntries(System.String, System.String): Stub Method");
335 public DateTime
GetLastAccessTimeDirectory(string path
)
337 //System.Diagnostics/.Debug.WriteLine("Linux:GetLastAccessTimeDirectory(System.String): Stub Method");
338 return new DateTime(0);
341 public DateTime
GetLastWriteTimeDirectory(string path
)
343 //System.Diagnostics/.Debug.WriteLine("Linux:GetLastWriteTimeDirectory(System.String): Stub Method");
344 return new DateTime(0);
347 public void MoveDirectory(string sourceDirName
, string destDirName
)
349 //System.Diagnostics/.Debug.WriteLine("Linux:MoveDirectory(System.String, System.String): Stub Method");
352 public void SetCreationTimeDirectory(string path
, DateTime creationTime
)
354 //System.Diagnostics/.Debug.WriteLine("Linux:SetCreationTimeDirectory(System.String, System.DateTime): Stub Method");
357 public void SetCurrentDirectory(string path
)
359 //System.Diagnostics/.Debug.WriteLine("Linux:SetCurrentDirectory(System.String): Stub Method");
362 public void SetLastAccessTimeDirectory(string path
, DateTime lastAccessTime
)
364 //System.Diagnostics/.Debug.WriteLine("Linux:SetLastAccessTimeDirectory(System.String, System.DateTime): Stub Method");
367 public void SetLastWriteTimeDirectory(string path
, DateTime lastWriteTime
)
369 //System.Diagnostics/.Debug.WriteLine("Linux:SetLastWriteTimeDirectory(System.String, System.DateTime): Stub Method");
372 //-----------------------------------
374 //-----------------------------------
376 [MethodImplAttribute(System
.Runtime
.CompilerServices
.MethodImplOptions
.InternalCall
)]
377 private extern IntPtr
GetStdHandle(int fd
);
379 public IntPtr StdinHandle
{
385 public IntPtr StdoutHandle
{
391 public IntPtr StderrHandle
{
397 [MethodImplAttribute(System
.Runtime
.CompilerServices
.MethodImplOptions
.InternalCall
)]
398 public extern void DeleteFile(string path
);
400 [MethodImplAttribute(System
.Runtime
.CompilerServices
.MethodImplOptions
.InternalCall
)]
401 public extern bool ExistsFile(string path
);
403 /* The long time parameters in GetFileTime and
404 * SetFileTime correspond to Windows file times (ticks
405 * from DateTime(1/1/1601 00:00 GMT))
407 [MethodImplAttribute(System
.Runtime
.CompilerServices
.MethodImplOptions
.InternalCall
)]
408 private extern static bool GetFileTime(IntPtr handle
, out long creat
, out long lastaccess
, out long lastwrite
);
410 [MethodImplAttribute(System
.Runtime
.CompilerServices
.MethodImplOptions
.InternalCall
)]
411 private extern static bool SetFileTime(IntPtr handle
, long creat
, long lastaccess
, long lastwrite
);
413 public DateTime
GetCreationTimeFile(string path
)
415 long creat
, lastaccess
, lastwrite
;
417 FileStream s
= new FileStream(path
, FileMode
.Open
, FileAccess
.Read
);
419 ret
=GetFileTime(s
.Handle
, out creat
, out lastaccess
, out lastwrite
);
422 return DateTime
.FromFileTime(creat
);
425 public DateTime
GetLastAccessTimeFile(string path
)
427 long creat
, lastaccess
, lastwrite
;
429 FileStream s
= new FileStream(path
, FileMode
.Open
, FileAccess
.Read
);
431 ret
=GetFileTime(s
.Handle
, out creat
, out lastaccess
, out lastwrite
);
434 return DateTime
.FromFileTime(lastaccess
);
437 public DateTime
GetLastWriteTimeFile(string path
)
439 long creat
, lastaccess
, lastwrite
;
441 FileStream s
= new FileStream(path
, FileMode
.Open
, FileAccess
.Read
);
443 ret
=GetFileTime(s
.Handle
, out creat
, out lastaccess
, out lastwrite
);
446 return DateTime
.FromFileTime(lastwrite
);
449 public void SetCreationTimeFile(string path
, DateTime creationTime
)
451 long creat
, lastaccess
, lastwrite
;
453 FileStream s
= new FileStream(path
, FileMode
.Open
, FileAccess
.ReadWrite
);
455 // Get the existing times first
456 ret
=GetFileTime(s
.Handle
, out creat
, out lastaccess
, out lastwrite
);
458 creat
=creationTime
.ToFileTime();
460 ret
=SetFileTime(s
.Handle
, creat
, lastaccess
, lastwrite
);
464 public void SetLastAccessTimeFile(string path
, DateTime lastAccessTime
)
466 long creat
, lastaccess
, lastwrite
;
468 FileStream s
= new FileStream(path
, FileMode
.Open
, FileAccess
.ReadWrite
);
470 // Get the existing times first
471 ret
=GetFileTime(s
.Handle
, out creat
, out lastaccess
, out lastwrite
);
473 lastaccess
=lastAccessTime
.ToFileTime();
475 ret
=SetFileTime(s
.Handle
, creat
, lastaccess
, lastwrite
);
479 public void SetLastWriteTimeFile(string path
, DateTime lastWriteTime
)
481 long creat
, lastaccess
, lastwrite
;
483 FileStream s
= new FileStream(path
, FileMode
.Open
, FileAccess
.ReadWrite
);
485 // Get the existing times first
486 ret
=GetFileTime(s
.Handle
, out creat
, out lastaccess
, out lastwrite
);
488 lastwrite
=lastWriteTime
.ToFileTime();
490 ret
=SetFileTime(s
.Handle
, creat
, lastaccess
, lastwrite
);
495 public long FileLength(string path
)
500 // Private implementation details
501 [DllImport("monowrapper", EntryPoint
="mono_wrapper_environ", CharSet
=CharSet
.Ansi
)]
502 private unsafe static extern IntPtr
_getEnviron();
504 [DllImport("libc", EntryPoint
="getpid")]
505 private unsafe static extern int _getPid();