mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Move Unix class under Platform
E.g. SMA.Platform.Unix, not just SMA.Unix.
This commit is contained in:
@@ -18,9 +18,6 @@ namespace System.Management.Automation
|
||||
{
|
||||
/// <summary>
|
||||
/// These are platform abstractions and platform specific implementations
|
||||
///
|
||||
/// All these properties are calling into platform specific static classes, to make
|
||||
/// sure the platform implementations are switched at runtime (including pinvokes).
|
||||
/// </summary>
|
||||
public static class Platform
|
||||
{
|
||||
@@ -31,11 +28,11 @@ namespace System.Management.Automation
|
||||
{
|
||||
get
|
||||
{
|
||||
#if CORECLR
|
||||
#if CORECLR
|
||||
return RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
|
||||
#else
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,11 +43,11 @@ namespace System.Management.Automation
|
||||
{
|
||||
get
|
||||
{
|
||||
#if CORECLR
|
||||
#if CORECLR
|
||||
return RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
|
||||
#else
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,11 +58,11 @@ namespace System.Management.Automation
|
||||
{
|
||||
get
|
||||
{
|
||||
#if CORECLR
|
||||
#if CORECLR
|
||||
return RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
|
||||
#else
|
||||
#else
|
||||
return true;
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,11 +73,11 @@ namespace System.Management.Automation
|
||||
{
|
||||
get
|
||||
{
|
||||
#if CORECLR
|
||||
#if CORECLR
|
||||
return true;
|
||||
#else
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
@@ -340,171 +337,171 @@ namespace System.Management.Automation
|
||||
// TODO:PSL clean this up
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
internal static class Unix
|
||||
{
|
||||
private static string s_userName;
|
||||
public static string UserName
|
||||
internal static class Unix
|
||||
{
|
||||
get
|
||||
private static string s_userName;
|
||||
public static string UserName
|
||||
{
|
||||
if (string.IsNullOrEmpty(s_userName))
|
||||
get
|
||||
{
|
||||
s_userName = NativeMethods.GetUserName();
|
||||
}
|
||||
return s_userName ?? string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
public static string TemporaryDirectory
|
||||
{
|
||||
get
|
||||
{
|
||||
// POSIX temporary directory environment variables
|
||||
string[] environmentVariables = { "TMPDIR", "TMP", "TEMP", "TEMPDIR" };
|
||||
string dir = string.Empty;
|
||||
foreach (string s in environmentVariables)
|
||||
{
|
||||
dir = System.Environment.GetEnvironmentVariable(s);
|
||||
if (!string.IsNullOrEmpty(dir))
|
||||
if (string.IsNullOrEmpty(s_userName))
|
||||
{
|
||||
return dir;
|
||||
s_userName = NativeMethods.GetUserName();
|
||||
}
|
||||
return s_userName ?? string.Empty;
|
||||
}
|
||||
return "/tmp";
|
||||
}
|
||||
}
|
||||
|
||||
public static bool IsHardLink(ref IntPtr handle)
|
||||
{
|
||||
// TODO:PSL implement using fstat to query inode refcount to see if it is a hard link
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public static bool IsHardLink(FileSystemInfo fs)
|
||||
{
|
||||
if (!fs.Exists || (fs.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
|
||||
public static string TemporaryDirectory
|
||||
{
|
||||
get
|
||||
{
|
||||
// POSIX temporary directory environment variables
|
||||
string[] environmentVariables = { "TMPDIR", "TMP", "TEMP", "TEMPDIR" };
|
||||
string dir = string.Empty;
|
||||
foreach (string s in environmentVariables)
|
||||
{
|
||||
dir = System.Environment.GetEnvironmentVariable(s);
|
||||
if (!string.IsNullOrEmpty(dir))
|
||||
{
|
||||
return dir;
|
||||
}
|
||||
}
|
||||
return "/tmp";
|
||||
}
|
||||
}
|
||||
|
||||
public static bool IsHardLink(ref IntPtr handle)
|
||||
{
|
||||
// TODO:PSL implement using fstat to query inode refcount to see if it is a hard link
|
||||
return false;
|
||||
}
|
||||
|
||||
int count;
|
||||
string filePath = fs.FullName;
|
||||
int ret = NativeMethods.GetLinkCount(filePath, out count);
|
||||
if (ret == 1)
|
||||
{
|
||||
return count > 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
int lastError = Marshal.GetLastWin32Error();
|
||||
throw new InvalidOperationException("Unix.IsHardLink error: " + lastError);
|
||||
}
|
||||
}
|
||||
|
||||
public static void SetDate(SetDateInfoInternal info)
|
||||
{
|
||||
int ret = NativeMethods.SetDate(info);
|
||||
if (ret == -1)
|
||||
public static bool IsHardLink(FileSystemInfo fs)
|
||||
{
|
||||
int lastError = Marshal.GetLastWin32Error();
|
||||
throw new InvalidOperationException("Unix.NonWindowsSetDate error: " + lastError);
|
||||
}
|
||||
}
|
||||
if (!fs.Exists || (fs.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public static bool CreateHardLink(string path, string strTargetPath)
|
||||
{
|
||||
int ret = NativeMethods.CreateHardLink(path, strTargetPath);
|
||||
return ret == 1 ? true : false;
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
internal class SetDateInfoInternal
|
||||
{
|
||||
public int Year;
|
||||
public int Month;
|
||||
public int Day;
|
||||
public int Hour;
|
||||
public int Minute;
|
||||
public int Second;
|
||||
public int Millisecond;
|
||||
public int DST;
|
||||
|
||||
public SetDateInfoInternal(DateTime d)
|
||||
{
|
||||
Year = d.Year;
|
||||
Month = d.Month;
|
||||
Day = d.Day;
|
||||
Hour = d.Hour;
|
||||
Minute = d.Minute;
|
||||
Second = d.Second;
|
||||
Millisecond = d.Millisecond;
|
||||
DST = d.IsDaylightSavingTime() ? 1 : 0;
|
||||
int count;
|
||||
string filePath = fs.FullName;
|
||||
int ret = NativeMethods.GetLinkCount(filePath, out count);
|
||||
if (ret == 1)
|
||||
{
|
||||
return count > 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
int lastError = Marshal.GetLastWin32Error();
|
||||
throw new InvalidOperationException("Unix.IsHardLink error: " + lastError);
|
||||
}
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
public static void SetDate(SetDateInfoInternal info)
|
||||
{
|
||||
string ret = String.Format("Year = {0}; Month = {1}; Day = {2}; Hour = {3}; Minute = {4}; Second = {5}; Millisec = {6}; DST = {7}", Year, Month, Day, Hour, Minute, Second, Millisecond, DST);
|
||||
return ret;
|
||||
int ret = NativeMethods.SetDate(info);
|
||||
if (ret == -1)
|
||||
{
|
||||
int lastError = Marshal.GetLastWin32Error();
|
||||
throw new InvalidOperationException("Unix.NonWindowsSetDate error: " + lastError);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal static class NativeMethods
|
||||
{
|
||||
private const string psLib = "libpsl-native";
|
||||
public static bool CreateHardLink(string path, string strTargetPath)
|
||||
{
|
||||
int ret = NativeMethods.CreateHardLink(path, strTargetPath);
|
||||
return ret == 1 ? true : false;
|
||||
}
|
||||
|
||||
// Ansi is a misnomer, it is hardcoded to UTF-8 on Linux and OS X
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
internal class SetDateInfoInternal
|
||||
{
|
||||
public int Year;
|
||||
public int Month;
|
||||
public int Day;
|
||||
public int Hour;
|
||||
public int Minute;
|
||||
public int Second;
|
||||
public int Millisecond;
|
||||
public int DST;
|
||||
|
||||
// C bools are 1 byte and so must be marshaled as I1
|
||||
public SetDateInfoInternal(DateTime d)
|
||||
{
|
||||
Year = d.Year;
|
||||
Month = d.Month;
|
||||
Day = d.Day;
|
||||
Hour = d.Hour;
|
||||
Minute = d.Minute;
|
||||
Second = d.Second;
|
||||
Millisecond = d.Millisecond;
|
||||
DST = d.IsDaylightSavingTime() ? 1 : 0;
|
||||
}
|
||||
|
||||
[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.LPStr)]
|
||||
internal static extern string GetUserName();
|
||||
public override string ToString()
|
||||
{
|
||||
string ret = String.Format("Year = {0}; Month = {1}; Day = {2}; Hour = {3}; Minute = {4}; Second = {5}; Millisec = {6}; DST = {7}", Year, Month, Day, Hour, Minute, Second, Millisecond, DST);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
|
||||
internal static extern int GetLinkCount([MarshalAs(UnmanagedType.LPStr)]string filePath, out int linkCount);
|
||||
internal static class NativeMethods
|
||||
{
|
||||
private const string psLib = "libpsl-native";
|
||||
|
||||
[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.I1)]
|
||||
internal static extern bool IsSymLink([MarshalAs(UnmanagedType.LPStr)]string filePath);
|
||||
// Ansi is a misnomer, it is hardcoded to UTF-8 on Linux and OS X
|
||||
|
||||
[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.I1)]
|
||||
internal static extern bool IsExecutable([MarshalAs(UnmanagedType.LPStr)]string filePath);
|
||||
// C bools are 1 byte and so must be marshaled as I1
|
||||
|
||||
[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.LPStr)]
|
||||
internal static extern string GetFullyQualifiedName();
|
||||
[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.LPStr)]
|
||||
internal static extern string GetUserName();
|
||||
|
||||
[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
|
||||
internal static extern int SetDate(SetDateInfoInternal info);
|
||||
[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
|
||||
internal static extern int GetLinkCount([MarshalAs(UnmanagedType.LPStr)]string filePath, out int linkCount);
|
||||
|
||||
[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.I1)]
|
||||
internal static extern bool CreateSymLink([MarshalAs(UnmanagedType.LPStr)]string filePath,
|
||||
[MarshalAs(UnmanagedType.LPStr)]string target);
|
||||
[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.I1)]
|
||||
internal static extern bool IsSymLink([MarshalAs(UnmanagedType.LPStr)]string filePath);
|
||||
|
||||
[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
|
||||
internal static extern int CreateHardLink([MarshalAs(UnmanagedType.LPStr)]string filePath,
|
||||
[MarshalAs(UnmanagedType.LPStr)]string target);
|
||||
[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.I1)]
|
||||
internal static extern bool IsExecutable([MarshalAs(UnmanagedType.LPStr)]string filePath);
|
||||
|
||||
[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.LPStr)]
|
||||
internal static extern string FollowSymLink([MarshalAs(UnmanagedType.LPStr)]string filePath);
|
||||
[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.LPStr)]
|
||||
internal static extern string GetFullyQualifiedName();
|
||||
|
||||
[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.LPStr)]
|
||||
internal static extern string GetUserFromPid(int pid);
|
||||
[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
|
||||
internal static extern int SetDate(SetDateInfoInternal info);
|
||||
|
||||
[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.I1)]
|
||||
internal static extern bool IsFile([MarshalAs(UnmanagedType.LPStr)]string filePath);
|
||||
[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.I1)]
|
||||
internal static extern bool CreateSymLink([MarshalAs(UnmanagedType.LPStr)]string filePath,
|
||||
[MarshalAs(UnmanagedType.LPStr)]string target);
|
||||
|
||||
[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.I1)]
|
||||
internal static extern bool IsDirectory([MarshalAs(UnmanagedType.LPStr)]string filePath);
|
||||
[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
|
||||
internal static extern int CreateHardLink([MarshalAs(UnmanagedType.LPStr)]string filePath,
|
||||
[MarshalAs(UnmanagedType.LPStr)]string target);
|
||||
|
||||
[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.LPStr)]
|
||||
internal static extern string FollowSymLink([MarshalAs(UnmanagedType.LPStr)]string filePath);
|
||||
|
||||
[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.LPStr)]
|
||||
internal static extern string GetUserFromPid(int pid);
|
||||
|
||||
[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.I1)]
|
||||
internal static extern bool IsFile([MarshalAs(UnmanagedType.LPStr)]string filePath);
|
||||
|
||||
[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.I1)]
|
||||
internal static extern bool IsDirectory([MarshalAs(UnmanagedType.LPStr)]string filePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace System.Management.Automation
|
||||
|
||||
Reference in New Issue
Block a user