From 2be3db8ab8757fe9ecccd2cc545ae9044ed76574 Mon Sep 17 00:00:00 2001 From: Ilya Date: Tue, 7 May 2019 10:22:01 +0500 Subject: [PATCH] Cleanup unused pinvokes on unix (#9531) * Remove TemporaryDirectory() * Remove GetUserName() * Use Environment.UserName on Unix * Remove old comment * Use Environment.UserName on Windows too --- .../CoreCLR/CorePsPlatform.cs | 38 ------------------- .../engine/InformationRecord.cs | 4 +- .../engine/InitialSessionState.cs | 6 +-- test/xUnit/csharp/test_CorePsPlatform.cs | 3 +- 4 files changed, 6 insertions(+), 45 deletions(-) diff --git a/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs b/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs index d6b4ed8f5a..158ca19a8f 100644 --- a/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs +++ b/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs @@ -573,40 +573,6 @@ namespace System.Management.Automation return (ErrorCategory)Unix.NativeMethods.GetErrorCategory(errno); } - private static string s_userName; - public static string UserName - { - get - { - if (string.IsNullOrEmpty(s_userName)) - { - 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)) - { - 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 @@ -669,10 +635,6 @@ namespace System.Management.Automation [DllImport(psLib, CharSet = CharSet.Ansi)] internal static extern int GetErrorCategory(int errno); - [DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)] - [return: MarshalAs(UnmanagedType.LPStr)] - internal static extern string GetUserName(); - [DllImport(psLib)] internal static extern int GetPPid(int pid); diff --git a/src/System.Management.Automation/engine/InformationRecord.cs b/src/System.Management.Automation/engine/InformationRecord.cs index 360701cc52..8dd92d5de2 100644 --- a/src/System.Management.Automation/engine/InformationRecord.cs +++ b/src/System.Management.Automation/engine/InformationRecord.cs @@ -101,9 +101,9 @@ namespace System.Management.Automation { // domain\user on Windows, just user on Unix #if UNIX - this._user = Platform.Unix.UserName; + this._user = Environment.UserName; #else - this._user = System.Security.Principal.WindowsIdentity.GetCurrent().Name; + this._user = Environment.UserDomainName + "\\" + Environment.UserName; #endif } diff --git a/src/System.Management.Automation/engine/InitialSessionState.cs b/src/System.Management.Automation/engine/InitialSessionState.cs index 216dae274a..18bfdf3bd0 100644 --- a/src/System.Management.Automation/engine/InitialSessionState.cs +++ b/src/System.Management.Automation/engine/InitialSessionState.cs @@ -2768,9 +2768,9 @@ namespace System.Management.Automation.Runspaces ? this.UserDriveUserName // domain\user on Windows, just user on Unix #if UNIX - : Platform.Unix.UserName; + : Environment.UserName; #else - : System.Security.Principal.WindowsIdentity.GetCurrent().Name; + : Environment.UserDomainName + "_" + Environment.UserName; #endif // Ensure that user name contains no invalid path characters. @@ -2781,7 +2781,7 @@ namespace System.Management.Automation.Runspaces throw new PSInvalidOperationException(RemotingErrorIdStrings.InvalidUserDriveName); } - return userName.Replace("\\", "_"); + return userName; } private Exception ProcessStartupScripts(Runspace initializedRunspace) diff --git a/test/xUnit/csharp/test_CorePsPlatform.cs b/test/xUnit/csharp/test_CorePsPlatform.cs index 0a58a804cc..58d5261e49 100644 --- a/test/xUnit/csharp/test_CorePsPlatform.cs +++ b/test/xUnit/csharp/test_CorePsPlatform.cs @@ -36,8 +36,7 @@ namespace PSTests.Parallel // The process should return an exit code of 0 on success Assert.Equal(0, process.ExitCode); - // It should be the same as what our platform code returns - Assert.Equal(username, Platform.Unix.UserName()); + Assert.Equal(username, Environment.UserName); } }