From 0e805a8502ca6735c91184a951625d1d2874aa1a Mon Sep 17 00:00:00 2001 From: iSazonov Date: Tue, 4 Sep 2018 09:29:09 +0500 Subject: [PATCH 1/9] Use PATHEXT env variable --- .../engine/NativeCommandProcessor.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/System.Management.Automation/engine/NativeCommandProcessor.cs b/src/System.Management.Automation/engine/NativeCommandProcessor.cs index c4c7c106355..70053bbc865 100644 --- a/src/System.Management.Automation/engine/NativeCommandProcessor.cs +++ b/src/System.Management.Automation/engine/NativeCommandProcessor.cs @@ -1293,9 +1293,7 @@ private bool ValidateExtension(string path) // Now check the extension and see if it's one of the ones in pathext string myExtension = System.IO.Path.GetExtension(path); - string pathext = (string)LanguagePrimitives.ConvertTo( - this.Command.Context.GetVariableValue(SpecialVariables.PathExtVarPath), - typeof(string), CultureInfo.InvariantCulture); + var pathext = Environment.GetEnvironmentVariable("PATHEXT"); string[] extensionList; if (String.IsNullOrEmpty(pathext)) { From aa406b4b922e83fc885f3ecd28a22bf62279c161 Mon Sep 17 00:00:00 2001 From: iSazonov Date: Tue, 4 Sep 2018 09:51:28 +0500 Subject: [PATCH 2/9] Don't create env:PATHEXT on Unix --- src/System.Management.Automation/engine/SpecialVariables.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/System.Management.Automation/engine/SpecialVariables.cs b/src/System.Management.Automation/engine/SpecialVariables.cs index c95689e5ba7..7d401f33758 100644 --- a/src/System.Management.Automation/engine/SpecialVariables.cs +++ b/src/System.Management.Automation/engine/SpecialVariables.cs @@ -115,10 +115,10 @@ internal static class SpecialVariables internal const string EventError = "error"; internal static readonly VariablePath EventErrorVarPath = new VariablePath("script:" + EventError); - +#if !UNIX internal const string PathExt = "env:PATHEXT"; internal static readonly VariablePath PathExtVarPath = new VariablePath(PathExt); - +#endif internal const string PSEmailServer = "PSEmailServer"; internal static readonly VariablePath PSEmailServerVarPath = new VariablePath(PSEmailServer); From a58ecc3c15e0fb09c06a1a1be5e30a687cecd768 Mon Sep 17 00:00:00 2001 From: iSazonov Date: Tue, 4 Sep 2018 10:18:44 +0500 Subject: [PATCH 3/9] Use IsExecutable() --- .../engine/NativeCommandProcessor.cs | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/System.Management.Automation/engine/NativeCommandProcessor.cs b/src/System.Management.Automation/engine/NativeCommandProcessor.cs index 70053bbc865..684c5daf4f0 100644 --- a/src/System.Management.Automation/engine/NativeCommandProcessor.cs +++ b/src/System.Management.Automation/engine/NativeCommandProcessor.cs @@ -1085,10 +1085,7 @@ private ProcessStartInfo GetProcessStartInfo(bool redirectOutput, bool redirectE ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.FileName = this.Path; - // On Windows, check the extension list and see if we should try to execute this directly. - // Otherwise, use the platform library to check executability - if ((Platform.IsWindows && ValidateExtension(this.Path)) - || (!Platform.IsWindows && Platform.NonWindowsIsExecutable(this.Path))) + if (IsExecutable(this.Path)) { startInfo.UseShellExecute = false; if (redirectInput) @@ -1288,6 +1285,21 @@ private void CalculateIORedirection(out bool redirectOutput, out bool redirectEr } } + // On Windows, check the extension list and see if we should try to execute this directly. + // Otherwise, use the platform library to check executability + private bool IsExecutable(string path) + { + if (Platform.IsWindows && ValidateExtension(this.Path)) + { + return true; + } + if (!Platform.IsWindows && Platform.NonWindowsIsExecutable(this.Path)) + { + return true; + } + + return false; + } private bool ValidateExtension(string path) { // Now check the extension and see if it's one of the ones in pathext From 12abd085cf69fb5b33c35673ce0528b9c827e23e Mon Sep 17 00:00:00 2001 From: iSazonov Date: Tue, 4 Sep 2018 10:43:09 +0500 Subject: [PATCH 4/9] Add conditional compile --- .../engine/NativeCommandProcessor.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/System.Management.Automation/engine/NativeCommandProcessor.cs b/src/System.Management.Automation/engine/NativeCommandProcessor.cs index 684c5daf4f0..18516cbb4b0 100644 --- a/src/System.Management.Automation/engine/NativeCommandProcessor.cs +++ b/src/System.Management.Automation/engine/NativeCommandProcessor.cs @@ -1289,15 +1289,17 @@ private void CalculateIORedirection(out bool redirectOutput, out bool redirectEr // Otherwise, use the platform library to check executability private bool IsExecutable(string path) { +#if !UNIX if (Platform.IsWindows && ValidateExtension(this.Path)) { return true; } +#else if (!Platform.IsWindows && Platform.NonWindowsIsExecutable(this.Path)) { return true; } - +#endif return false; } private bool ValidateExtension(string path) From f2109842d5254f47fc5c3176478fcbf8f13a6244 Mon Sep 17 00:00:00 2001 From: iSazonov Date: Tue, 4 Sep 2018 11:34:45 +0500 Subject: [PATCH 5/9] Refactor IsExecutable() Fix typo --- .../engine/NativeCommandProcessor.cs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/System.Management.Automation/engine/NativeCommandProcessor.cs b/src/System.Management.Automation/engine/NativeCommandProcessor.cs index 18516cbb4b0..0b6aa4c7941 100644 --- a/src/System.Management.Automation/engine/NativeCommandProcessor.cs +++ b/src/System.Management.Automation/engine/NativeCommandProcessor.cs @@ -1289,17 +1289,20 @@ private void CalculateIORedirection(out bool redirectOutput, out bool redirectEr // Otherwise, use the platform library to check executability private bool IsExecutable(string path) { -#if !UNIX - if (Platform.IsWindows && ValidateExtension(this.Path)) +#if UNIX + + if (Platform.NonWindowsIsExecutable(this.Path)) { return true; } #else - if (!Platform.IsWindows && Platform.NonWindowsIsExecutable(this.Path)) + + if (ValidateExtension(this.Path)) { return true; } #endif + return false; } private bool ValidateExtension(string path) From 624dd238af64f92243d66889b9abbe22968da73d Mon Sep 17 00:00:00 2001 From: iSazonov Date: Tue, 4 Sep 2018 12:25:04 +0500 Subject: [PATCH 6/9] [Feature] Remove ValidateExtension() method --- .../engine/NativeCommandProcessor.cs | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/src/System.Management.Automation/engine/NativeCommandProcessor.cs b/src/System.Management.Automation/engine/NativeCommandProcessor.cs index 0b6aa4c7941..a8d8e63838d 100644 --- a/src/System.Management.Automation/engine/NativeCommandProcessor.cs +++ b/src/System.Management.Automation/engine/NativeCommandProcessor.cs @@ -1297,17 +1297,6 @@ private bool IsExecutable(string path) } #else - if (ValidateExtension(this.Path)) - { - return true; - } -#endif - - return false; - } - private bool ValidateExtension(string path) - { - // Now check the extension and see if it's one of the ones in pathext string myExtension = System.IO.Path.GetExtension(path); var pathext = Environment.GetEnvironmentVariable("PATHEXT"); @@ -1320,6 +1309,7 @@ private bool ValidateExtension(string path) { extensionList = pathext.Split(Utils.Separators.Semicolon); } + foreach (string extension in extensionList) { if (String.Equals(extension, myExtension, StringComparison.OrdinalIgnoreCase)) @@ -1327,6 +1317,8 @@ private bool ValidateExtension(string path) return true; } } +#endif + return false; } From e18b1f4b0e67d7efa4144e8a8ce968b7642508d0 Mon Sep 17 00:00:00 2001 From: iSazonov Date: Fri, 7 Sep 2018 17:03:16 +0500 Subject: [PATCH 7/9] Add support PATHEXT on Unix --- .../engine/NativeCommandProcessor.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/System.Management.Automation/engine/NativeCommandProcessor.cs b/src/System.Management.Automation/engine/NativeCommandProcessor.cs index a8d8e63838d..f447911a727 100644 --- a/src/System.Management.Automation/engine/NativeCommandProcessor.cs +++ b/src/System.Management.Automation/engine/NativeCommandProcessor.cs @@ -1295,7 +1295,7 @@ private bool IsExecutable(string path) { return true; } -#else +#endif string myExtension = System.IO.Path.GetExtension(path); @@ -1303,7 +1303,11 @@ private bool IsExecutable(string path) string[] extensionList; if (String.IsNullOrEmpty(pathext)) { +#if UNIX + return false; +#else extensionList = new string[] { ".exe", ".com", ".bat", ".cmd" }; +#endif } else { @@ -1317,7 +1321,6 @@ private bool IsExecutable(string path) return true; } } -#endif return false; } From d8985a3f8f99e002803fb82d0528fc4d89c3b6a6 Mon Sep 17 00:00:00 2001 From: iSazonov Date: Fri, 7 Sep 2018 18:03:31 +0500 Subject: [PATCH 8/9] Fix CodeFactor issues --- .../engine/NativeCommandProcessor.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/System.Management.Automation/engine/NativeCommandProcessor.cs b/src/System.Management.Automation/engine/NativeCommandProcessor.cs index f447911a727..e4cd99e263e 100644 --- a/src/System.Management.Automation/engine/NativeCommandProcessor.cs +++ b/src/System.Management.Automation/engine/NativeCommandProcessor.cs @@ -1301,7 +1301,7 @@ private bool IsExecutable(string path) var pathext = Environment.GetEnvironmentVariable("PATHEXT"); string[] extensionList; - if (String.IsNullOrEmpty(pathext)) + if (string.IsNullOrEmpty(pathext)) { #if UNIX return false; @@ -1316,7 +1316,7 @@ private bool IsExecutable(string path) foreach (string extension in extensionList) { - if (String.Equals(extension, myExtension, StringComparison.OrdinalIgnoreCase)) + if (string.Equals(extension, myExtension, StringComparison.OrdinalIgnoreCase)) { return true; } From f5c26bb48252370c72e4ab223948dda3db590e67 Mon Sep 17 00:00:00 2001 From: iSazonov Date: Tue, 11 Sep 2018 08:44:27 +0500 Subject: [PATCH 9/9] Address feedback --- .../engine/NativeCommandProcessor.cs | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/src/System.Management.Automation/engine/NativeCommandProcessor.cs b/src/System.Management.Automation/engine/NativeCommandProcessor.cs index e4cd99e263e..7ce7ec20f16 100644 --- a/src/System.Management.Automation/engine/NativeCommandProcessor.cs +++ b/src/System.Management.Automation/engine/NativeCommandProcessor.cs @@ -1290,12 +1290,8 @@ private void CalculateIORedirection(out bool redirectOutput, out bool redirectEr private bool IsExecutable(string path) { #if UNIX - - if (Platform.NonWindowsIsExecutable(this.Path)) - { - return true; - } -#endif + return Platform.NonWindowsIsExecutable(this.Path); +#else string myExtension = System.IO.Path.GetExtension(path); @@ -1303,11 +1299,7 @@ private bool IsExecutable(string path) string[] extensionList; if (string.IsNullOrEmpty(pathext)) { -#if UNIX - return false; -#else extensionList = new string[] { ".exe", ".com", ".bat", ".cmd" }; -#endif } else { @@ -1323,6 +1315,7 @@ private bool IsExecutable(string path) } return false; +#endif } #region Interop for FindExecutable...